2 * Copyright © 2012-2013 Mike Beattie <mike@ethernal.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.AltosDroid;
21 import java.lang.ref.WeakReference;
24 import android.Manifest;
25 import android.app.Activity;
26 import android.app.PendingIntent;
27 import android.bluetooth.BluetoothAdapter;
28 import android.content.Intent;
29 import android.content.Context;
30 import android.content.ComponentName;
31 import android.content.ServiceConnection;
32 import android.content.DialogInterface;
33 import android.os.IBinder;
34 import android.os.Bundle;
35 import android.os.Handler;
36 import android.os.Message;
37 import android.os.Messenger;
38 import android.os.RemoteException;
39 import android.os.Parcelable;
40 import androidx.fragment.app.FragmentActivity;
41 import androidx.fragment.app.FragmentManager;
42 import android.view.*;
43 import android.widget.*;
44 import android.app.AlertDialog;
45 import android.location.Location;
46 import android.location.LocationManager;
47 import android.location.LocationListener;
48 import android.hardware.usb.*;
49 import android.content.pm.PackageManager;
50 import androidx.core.app.ActivityCompat;
51 import org.altusmetrum.altoslib_14.*;
62 SavedState(AltosState state) {
63 received_time = state.received_time;
64 this.state = state.state();
65 if (state.gps != null)
66 locked = state.gps.locked;
69 callsign = state.cal_data().callsign;
70 serial = state.cal_data().serial;
71 flight = state.cal_data().flight;
76 public class AltosDroid extends FragmentActivity implements AltosUnitsListener, LocationListener, ActivityCompat.OnRequestPermissionsResultCallback {
78 // Actions sent to the telemetry server at startup time
80 public static final String ACTION_BLUETOOTH = "org.altusmetrum.AltosDroid.BLUETOOTH";
81 public static final String ACTION_USB = "org.altusmetrum.AltosDroid.USB";
83 // Message types received by our Handler
85 public static final int MSG_STATE = 1;
86 public static final int MSG_UPDATE_AGE = 2;
87 public static final int MSG_IDLE_MODE = 3;
88 public static final int MSG_IGNITER_STATUS = 4;
90 // Intent request codes
91 public static final int REQUEST_CONNECT_DEVICE = 1;
92 public static final int REQUEST_ENABLE_BT = 2;
93 public static final int REQUEST_PRELOAD_MAPS = 3;
94 public static final int REQUEST_IDLE_MODE = 5;
95 public static final int REQUEST_IGNITERS = 6;
96 public static final int REQUEST_SETUP = 7;
97 public static final int REQUEST_SELECT_TRACKER = 8;
98 public static final int REQUEST_DELETE_TRACKER = 9;
100 public static final String EXTRA_IDLE_MODE = "idle_mode";
101 public static final String EXTRA_IDLE_RESULT = "idle_result";
102 public static final String EXTRA_FREQUENCY = "frequency";
103 public static final String EXTRA_TELEMETRY_SERVICE = "telemetry_service";
104 public static final String EXTRA_TRACKERS = "trackers";
105 public static final String EXTRA_TRACKERS_TITLE = "trackers_title";
108 public static final int SETUP_BAUD = 1;
109 public static final int SETUP_UNITS = 2;
110 public static final int SETUP_MAP_SOURCE = 4;
111 public static final int SETUP_MAP_TYPE = 8;
112 public static final int SETUP_FONT_SIZE = 16;
114 public static FragmentManager fm;
116 private BluetoothAdapter mBluetoothAdapter = null;
118 // Flight state values
119 private TextView mCallsignView;
120 private TextView mRSSIView;
121 private TextView mSerialView;
122 private TextView mFlightView;
123 private RelativeLayout mStateLayout;
124 private TextView mStateView;
125 private TextView mAgeView;
126 private boolean mAgeViewOld;
127 private int mAgeNewColor;
128 private int mAgeOldColor;
130 public static final String tab_pad_name = "pad";
131 public static final String tab_flight_name = "flight";
132 public static final String tab_recover_name = "recover";
133 public static final String tab_map_name = "map";
135 // field to display the version at the bottom of the screen
136 private TextView mVersion;
138 private boolean idle_mode = false;
140 public Location location = null;
142 private AltosState state;
143 private SavedState saved_state;
147 AltosViewPager mViewPager;
148 TabsAdapter mTabsAdapter;
149 ArrayList<AltosDroidTab> mTabs = new ArrayList<AltosDroidTab>();
152 // Timer and Saved flight state for Age calculation
155 TelemetryState telemetry_state;
158 UsbDevice pending_usb_device;
159 boolean start_with_usb;
162 private boolean mIsBound = false;
163 private Messenger mService = null;
164 final Messenger mMessenger = new Messenger(new IncomingHandler(this));
167 private AltosVoice mAltosVoice = null;
169 // The Handler that gets information back from the Telemetry Service
170 static class IncomingHandler extends Handler {
171 private final WeakReference<AltosDroid> mAltosDroid;
172 IncomingHandler(AltosDroid ad) { mAltosDroid = new WeakReference<AltosDroid>(ad); }
175 public void handleMessage(Message msg) {
176 AltosDroid ad = mAltosDroid.get();
180 if (msg.obj == null) {
181 AltosDebug.debug("telemetry_state null!");
184 ad.update_state((TelemetryState) msg.obj);
190 ad.idle_mode = (Boolean) msg.obj;
191 ad.update_state(null);
198 private ServiceConnection mConnection = new ServiceConnection() {
199 public void onServiceConnected(ComponentName className, IBinder service) {
200 mService = new Messenger(service);
202 Message msg = Message.obtain(null, TelemetryService.MSG_REGISTER_CLIENT);
203 msg.replyTo = mMessenger;
205 } catch (RemoteException e) {
206 // In this case the service has crashed before we could even do anything with it
208 if (pending_usb_device != null) {
210 mService.send(Message.obtain(null, TelemetryService.MSG_OPEN_USB, pending_usb_device));
211 pending_usb_device = null;
212 } catch (RemoteException e) {
217 public void onServiceDisconnected(ComponentName className) {
218 // This is called when the connection with the service has been unexpectedly disconnected - process crashed.
223 void doBindService() {
224 bindService(new Intent(this, TelemetryService.class), mConnection, Context.BIND_AUTO_CREATE);
228 void doUnbindService() {
230 // If we have received the service, and hence registered with it, then now is the time to unregister.
231 if (mService != null) {
233 Message msg = Message.obtain(null, TelemetryService.MSG_UNREGISTER_CLIENT);
234 msg.replyTo = mMessenger;
236 } catch (RemoteException e) {
237 // There is nothing special we need to do if the service has crashed.
240 // Detach our existing connection.
241 unbindService(mConnection);
246 public void registerTab(AltosDroidTab mTab) {
250 public void unregisterTab(AltosDroidTab mTab) {
254 public void units_changed(boolean imperial_units) {
255 for (AltosDroidTab mTab : mTabs)
256 mTab.units_changed(imperial_units);
259 void update_title(TelemetryState telemetry_state) {
260 switch (telemetry_state.connect) {
261 case TelemetryState.CONNECT_CONNECTED:
262 if (telemetry_state.config != null) {
263 String str = String.format("S/N %d %6.3f MHz%s", telemetry_state.config.serial,
264 telemetry_state.frequency, telemetry_state.idle_mode ? " (idle)" : "");
265 if (telemetry_state.telemetry_rate != AltosLib.ao_telemetry_rate_38400)
266 str = str.concat(String.format(" %d bps",
267 AltosLib.ao_telemetry_rate_values[telemetry_state.telemetry_rate]));
270 setTitle(R.string.title_connected_to);
273 case TelemetryState.CONNECT_CONNECTING:
274 if (telemetry_state.address != null)
275 setTitle(String.format("Connecting to %s...", telemetry_state.address.name));
277 setTitle("Connecting to something...");
279 case TelemetryState.CONNECT_DISCONNECTED:
280 case TelemetryState.CONNECT_NONE:
281 setTitle(R.string.title_not_connected);
289 timer.scheduleAtFixedRate(new TimerTask(){ public void run() {onTimerTick();}}, 1000L, 1000L);
301 int selected_serial = 0;
304 void set_switch_time() {
305 switch_time = System.currentTimeMillis();
309 boolean registered_units_listener;
311 void update_state(TelemetryState new_telemetry_state) {
313 if (new_telemetry_state != null)
314 telemetry_state = new_telemetry_state;
316 if (selected_frequency != AltosLib.MISSING) {
317 AltosState selected_state = telemetry_state.get(selected_serial);
318 AltosState latest_state = telemetry_state.get(telemetry_state.latest_serial);
320 if (selected_state != null && selected_state.frequency == selected_frequency) {
321 selected_frequency = AltosLib.MISSING;
322 } else if ((selected_state == null || selected_state.frequency != selected_frequency) &&
323 (latest_state != null && latest_state.frequency == selected_frequency))
325 selected_frequency = AltosLib.MISSING;
326 selected_serial = telemetry_state.latest_serial;
330 if (!telemetry_state.containsKey(selected_serial)) {
331 selected_serial = telemetry_state.latest_serial;
332 AltosDebug.debug("selected serial set to %d", selected_serial);
335 int shown_serial = selected_serial;
337 if (telemetry_state.idle_mode)
338 shown_serial = telemetry_state.latest_serial;
340 if (!registered_units_listener) {
341 registered_units_listener = true;
342 AltosPreferences.register_units_listener(this);
345 int num_trackers = 0;
347 for (AltosState s : telemetry_state.values()) {
351 trackers = new Tracker[num_trackers + 1];
354 trackers[n++] = new Tracker(0, "auto", 0.0);
356 for (AltosState s : telemetry_state.values())
357 trackers[n++] = new Tracker(s);
359 Arrays.sort(trackers);
361 if (telemetry_state.frequency != AltosLib.MISSING)
362 telem_frequency = telemetry_state.frequency;
364 update_title(telemetry_state);
366 AltosState state = telemetry_state.get(shown_serial);
368 update_ui(telemetry_state, state, telemetry_state.quiet);
373 boolean same_string(String a, String b) {
386 private int blend_component(int a, int b, double r, int shift, int mask) {
387 return ((int) (((a >> shift) & mask) * r + ((b >> shift) & mask) * (1 - r)) & mask) << shift;
389 private int blend_color(int a, int b, double r) {
390 return (blend_component(a, b, r, 0, 0xff) |
391 blend_component(a, b, r, 8, 0xff) |
392 blend_component(a, b, r, 16, 0xff) |
393 blend_component(a, b, r, 24, 0xff));
396 int state_age(long received_time) {
397 return (int) ((System.currentTimeMillis() - received_time + 500) / 1000);
400 void set_screen_on(int age) {
402 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
404 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
407 static String age_string(int age) {
410 text = String.format("%ds", age);
411 else if (age < 60 * 60)
412 text = String.format("%dm", age / 60);
413 else if (age < 60 * 60 * 24)
414 text = String.format("%dh", age / (60 * 60));
416 text = String.format("%dd", age / (24 * 60 * 60));
421 if (saved_state != null) {
422 int age = state_age(saved_state.received_time);
424 double age_scale = age / 100.0;
429 mAgeView.setTextColor(blend_color(mAgeOldColor, mAgeNewColor, age_scale));
433 mAgeView.setText(age_string(age));
437 void update_ui(TelemetryState telem_state, AltosState state, boolean quiet) {
441 int prev_state = AltosLib.ao_flight_invalid;
443 AltosGreatCircle from_receiver = null;
445 if (saved_state != null)
446 prev_state = saved_state.state;
449 set_screen_on(state_age(state.received_time));
451 if (state.state() == AltosLib.ao_flight_stateless) {
452 boolean prev_locked = false;
453 boolean locked = false;
455 if(state.gps != null)
456 locked = state.gps.locked;
457 if (saved_state != null)
458 prev_locked = saved_state.locked;
459 if (prev_locked != locked) {
460 String currentTab = mTabHost.getCurrentTabTag();
462 if (currentTab.equals(tab_pad_name)) mTabHost.setCurrentTabByTag(tab_flight_name);
464 if (currentTab.equals(tab_flight_name)) mTabHost.setCurrentTabByTag(tab_pad_name);
468 if (prev_state != state.state()) {
469 String currentTab = mTabHost.getCurrentTabTag();
470 switch (state.state()) {
471 case AltosLib.ao_flight_boost:
472 if (currentTab.equals(tab_pad_name)) mTabHost.setCurrentTabByTag(tab_flight_name);
474 case AltosLib.ao_flight_landed:
475 if (currentTab.equals(tab_flight_name)) mTabHost.setCurrentTabByTag(tab_recover_name);
477 case AltosLib.ao_flight_stateless:
478 if (currentTab.equals(tab_pad_name)) mTabHost.setCurrentTabByTag(tab_flight_name);
484 if (location != null && state.gps != null && state.gps.locked) {
486 if (location.hasAltitude())
487 altitude = location.getAltitude();
488 from_receiver = new AltosGreatCircle(location.getLatitude(),
489 location.getLongitude(),
496 if (saved_state == null || !same_string(saved_state.callsign, state.cal_data().callsign)) {
497 mCallsignView.setText(state.cal_data().callsign);
499 if (saved_state == null || state.cal_data().serial != saved_state.serial) {
500 if (state.cal_data().serial == AltosLib.MISSING)
501 mSerialView.setText("");
503 mSerialView.setText(String.format("%d", state.cal_data().serial));
505 if (saved_state == null || state.cal_data().flight != saved_state.flight) {
506 if (state.cal_data().flight == AltosLib.MISSING)
507 mFlightView.setText("");
509 mFlightView.setText(String.format("%d", state.cal_data().flight));
511 if (saved_state == null || state.state() != saved_state.state) {
512 if (state.state() == AltosLib.ao_flight_stateless) {
513 mStateLayout.setVisibility(View.GONE);
515 mStateView.setText(state.state_name());
516 mStateLayout.setVisibility(View.VISIBLE);
519 if (saved_state == null || state.rssi != saved_state.rssi) {
520 if (state.rssi == AltosLib.MISSING)
521 mRSSIView.setText("");
523 mRSSIView.setText(String.format("%d", state.rssi));
525 saved_state = new SavedState(state);
528 for (AltosDroidTab mTab : mTabs)
529 mTab.update_ui(telem_state, state, from_receiver, location, mTab == mTabsAdapter.currentItem());
531 if (mAltosVoice != null && mTabsAdapter.currentItem() != null)
532 mAltosVoice.tell(telem_state, state, from_receiver, location, (AltosDroidTab) mTabsAdapter.currentItem(), quiet);
536 private void onTimerTick() {
538 mMessenger.send(Message.obtain(null, MSG_UPDATE_AGE));
539 } catch (RemoteException e) {
543 static String pos(double p, String pos, String neg) {
545 if (p == AltosLib.MISSING)
551 int deg = (int) Math.floor(p);
552 double min = (p - Math.floor(p)) * 60.0;
553 return String.format("%d° %7.4f\" %s", deg, min, h);
556 static String number(String format, double value) {
557 if (value == AltosLib.MISSING)
559 return String.format(format, value);
562 static String integer(String format, int value) {
563 if (value == AltosLib.MISSING)
565 return String.format(format, value);
568 private View create_tab_view(String label) {
569 LayoutInflater inflater = (LayoutInflater) this.getLayoutInflater();
570 View tab_view = inflater.inflate(R.layout.tab_layout, null);
571 TextView text_view = (TextView) tab_view.findViewById (R.id.tabLabel);
572 text_view.setText(label);
576 static public int[] themes = {
583 static public int[] dialog_themes = {
584 R.style.Small_Dialog,
585 R.style.Medium_Dialog,
586 R.style.Large_Dialog,
591 public void onCreate(Bundle savedInstanceState) {
592 // Initialise preferences
593 AltosDroidPreferences.init(this);
594 setTheme(themes[AltosDroidPreferences.font_size()]);
595 super.onCreate(savedInstanceState);
596 AltosDebug.init(this);
597 AltosDebug.debug("+++ ON CREATE +++");
600 fm = getSupportFragmentManager();
602 // Set up the window layout
603 setContentView(R.layout.altosdroid);
605 // Create the Tabs and ViewPager
606 mTabHost = (TabHost)findViewById(android.R.id.tabhost);
609 mViewPager = (AltosViewPager)findViewById(R.id.pager);
610 mViewPager.setOffscreenPageLimit(4);
612 mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);
614 mTabsAdapter.addTab(mTabHost.newTabSpec(tab_pad_name).setIndicator(create_tab_view("Pad")), TabPad.class, null);
615 mTabsAdapter.addTab(mTabHost.newTabSpec(tab_flight_name).setIndicator(create_tab_view("Flight")), TabFlight.class, null);
616 mTabsAdapter.addTab(mTabHost.newTabSpec(tab_recover_name).setIndicator(create_tab_view("Recover")), TabRecover.class, null);
617 mTabsAdapter.addTab(mTabHost.newTabSpec(tab_map_name).setIndicator(create_tab_view("Map")), TabMap.class, null);
619 // Display the Version
620 mVersion = (TextView) findViewById(R.id.version);
621 mVersion.setText("Version: " + BuildInfo.version +
622 " Built: " + BuildInfo.builddate + " " + BuildInfo.buildtime + " " + BuildInfo.buildtz +
623 " (" + BuildInfo.branch + "-" + BuildInfo.commitnum + "-" + BuildInfo.commithash + ")");
625 mCallsignView = (TextView) findViewById(R.id.callsign_value);
626 mRSSIView = (TextView) findViewById(R.id.rssi_value);
627 mSerialView = (TextView) findViewById(R.id.serial_value);
628 mFlightView = (TextView) findViewById(R.id.flight_value);
629 mStateLayout = (RelativeLayout) findViewById(R.id.state_container);
630 mStateView = (TextView) findViewById(R.id.state_value);
631 mAgeView = (TextView) findViewById(R.id.age_value);
632 mAgeNewColor = mAgeView.getTextColors().getDefaultColor();
633 mAgeOldColor = getResources().getColor(R.color.old_color);
636 private void ensureBluetooth() {
637 // Get local Bluetooth adapter
638 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
640 /* if there is a BT adapter and it isn't turned on, then turn it on */
641 if (mBluetoothAdapter != null && !mBluetoothAdapter.isEnabled()) {
642 Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
643 startActivityForResult(enableIntent, AltosDroid.REQUEST_ENABLE_BT);
647 private boolean check_usb() {
648 UsbDevice device = AltosUsb.find_device(this, AltosLib.product_basestation);
650 if (device != null) {
651 Intent i = new Intent(this, AltosDroid.class);
652 PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent("hello world", null, this, AltosDroid.class), 0);
654 if (AltosUsb.request_permission(this, device, pi)) {
657 start_with_usb = true;
661 start_with_usb = false;
666 private void noticeIntent(Intent intent) {
668 /* Ok, this is pretty convenient.
670 * When a USB device is plugged in, and our 'hotplug'
671 * intent registration fires, we get an Intent with
674 * When we start up and see a usb device and request
675 * permission to access it, that queues a
676 * PendingIntent, which has the EXTRA_DEVICE added in,
677 * along with the EXTRA_PERMISSION_GRANTED field as
680 * So, in both cases, we get the device name using the
681 * same call. We check to see if access was granted,
682 * in which case we ignore the device field and do our
683 * usual startup thing.
686 UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
687 boolean granted = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, true);
689 AltosDebug.debug("intent %s device %s granted %s", intent, device, granted);
694 if (device != null) {
695 AltosDebug.debug("intent has usb device " + device.toString());
699 /* 'granted' is only false if this intent came
700 * from the request_permission call and
701 * permission was denied. In which case, we
702 * don't want to loop forever...
705 AltosDebug.debug("check for a USB device at startup");
709 AltosDebug.debug("Starting by looking for bluetooth devices");
715 public void onStart() {
717 AltosDebug.debug("++ ON START ++");
721 noticeIntent(getIntent());
723 // Start Telemetry Service
724 String action = start_with_usb ? ACTION_USB : ACTION_BLUETOOTH;
726 startService(new Intent(action, null, AltosDroid.this, TelemetryService.class));
730 if (mAltosVoice == null)
731 mAltosVoice = new AltosVoice(this);
736 public void onNewIntent(Intent intent) {
737 super.onNewIntent(intent);
738 AltosDebug.debug("onNewIntent");
739 noticeIntent(intent);
742 private void enable_location_updates() {
743 // Listen for GPS and Network position updates
744 LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
746 if (locationManager != null)
749 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
750 location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
751 } catch (Exception e) {
752 locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1, this);
753 location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
756 if (location != null)
757 AltosDebug.debug("Resume, location is %f,%f\n",
758 location.getLatitude(),
759 location.getLongitude());
760 AltosDebug.debug("Failed to get GPS updates\n");
763 update_ui(telemetry_state, state, true);
766 static final int MY_PERMISSION_REQUEST = 1001;
768 public boolean have_location_permission = false;
769 public boolean have_storage_permission = false;
770 public boolean asked_permission = false;
772 AltosMapOnline map_online;
775 tell_map_permission(AltosMapOnline map_online) {
776 this.map_online = map_online;
780 public void onRequestPermissionsResult(int requestCode, String[] permissions,
781 int[] grantResults) {
782 if (requestCode == MY_PERMISSION_REQUEST) {
783 for (int i = 0; i < grantResults.length; i++) {
784 if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
785 if (permissions[i].equals(Manifest.permission.ACCESS_FINE_LOCATION)) {
786 have_location_permission = true;
787 enable_location_updates();
788 if (map_online != null)
789 map_online.position_permission();
791 if (permissions[i].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
792 have_storage_permission = true;
800 public void onResume() {
802 AltosDebug.debug("+ ON RESUME +");
804 if (!asked_permission) {
805 asked_permission = true;
806 if (ActivityCompat.checkSelfPermission(this,
807 Manifest.permission.ACCESS_FINE_LOCATION)
808 == PackageManager.PERMISSION_GRANTED)
810 have_location_permission = true;
812 if (ActivityCompat.checkSelfPermission(this,
813 Manifest.permission.WRITE_EXTERNAL_STORAGE)
814 == PackageManager.PERMISSION_GRANTED)
816 have_storage_permission = true;
818 int count = (have_location_permission ? 0 : 1) + (have_storage_permission ? 0 : 1);
821 String[] permissions = new String[count];
823 if (!have_location_permission)
824 permissions[i++] = Manifest.permission.ACCESS_FINE_LOCATION;
825 if (!have_location_permission)
826 permissions[i++] = Manifest.permission.WRITE_EXTERNAL_STORAGE;
827 ActivityCompat.requestPermissions(this, permissions, MY_PERMISSION_REQUEST);
830 if (have_location_permission)
831 enable_location_updates();
835 public void onPause() {
837 AltosDebug.debug("- ON PAUSE -");
838 // Stop listening for location updates
839 if (have_location_permission)
840 ((LocationManager) getSystemService(Context.LOCATION_SERVICE)).removeUpdates(this);
844 public void onStop() {
846 AltosDebug.debug("-- ON STOP --");
850 public void onDestroy() {
852 AltosDebug.debug("--- ON DESTROY ---");
855 if (mAltosVoice != null) {
862 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
863 AltosDebug.debug("onActivityResult request %d result %d", requestCode, resultCode);
864 switch (requestCode) {
865 case REQUEST_CONNECT_DEVICE:
866 // When DeviceListActivity returns with a device to connect to
867 if (resultCode == Activity.RESULT_OK) {
871 case REQUEST_ENABLE_BT:
872 // When the request to enable Bluetooth returns
873 if (resultCode == Activity.RESULT_OK) {
874 // Bluetooth is now enabled, so set up a chat session
876 AltosDebug.debug("BT enabled");
877 bluetoothEnabled(data);
879 // User did not enable Bluetooth or an error occured
880 AltosDebug.debug("BT not enabled");
883 case REQUEST_IDLE_MODE:
884 if (resultCode == Activity.RESULT_OK)
887 case REQUEST_IGNITERS:
890 if (resultCode == Activity.RESULT_OK)
891 note_setup_changes(data);
893 case REQUEST_SELECT_TRACKER:
894 if (resultCode == Activity.RESULT_OK)
895 select_tracker(data);
897 case REQUEST_DELETE_TRACKER:
898 if (resultCode == Activity.RESULT_OK)
904 private void note_setup_changes(Intent data) {
905 int changes = data.getIntExtra(SetupActivity.EXTRA_SETUP_CHANGES, 0);
907 AltosDebug.debug("note_setup_changes changes %d\n", changes);
909 if ((changes & SETUP_BAUD) != 0) {
911 mService.send(Message.obtain(null, TelemetryService.MSG_SETBAUD,
912 AltosPreferences.telemetry_rate(1)));
913 } catch (RemoteException re) {
916 if ((changes & SETUP_UNITS) != 0) {
917 /* nothing to do here */
919 if ((changes & SETUP_MAP_SOURCE) != 0) {
920 /* nothing to do here */
922 if ((changes & SETUP_MAP_TYPE) != 0) {
923 /* nothing to do here */
926 if ((changes & SETUP_FONT_SIZE) != 0) {
927 AltosDebug.debug(" ==== Recreate to switch font sizes ==== ");
929 startActivity(getIntent());
933 private void connectUsb(UsbDevice device) {
934 if (mService == null)
935 pending_usb_device = device;
937 // Attempt to connect to the device
939 mService.send(Message.obtain(null, TelemetryService.MSG_OPEN_USB, device));
940 AltosDebug.debug("Sent OPEN_USB message");
941 } catch (RemoteException e) {
942 AltosDebug.debug("connect device message failed");
947 private void bluetoothEnabled(Intent data) {
948 if (mService != null) {
950 mService.send(Message.obtain(null, TelemetryService.MSG_BLUETOOTH_ENABLED, null));
951 } catch (RemoteException e) {
952 AltosDebug.debug("send BT enabled message failed");
957 private void connectDevice(Intent data) {
958 // Attempt to connect to the device
960 String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
961 String name = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_NAME);
963 AltosDebug.debug("Connecting to " + address + " " + name);
964 DeviceAddress a = new DeviceAddress(address, name);
965 mService.send(Message.obtain(null, TelemetryService.MSG_CONNECT, a));
966 AltosDebug.debug("Sent connecting message");
967 } catch (RemoteException e) {
968 AltosDebug.debug("connect device message failed");
972 private void disconnectDevice(boolean remember) {
974 mService.send(Message.obtain(null, TelemetryService.MSG_DISCONNECT, (Boolean) remember));
975 } catch (RemoteException e) {
979 private void idle_mode(Intent data) {
980 int type = data.getIntExtra(IdleModeActivity.EXTRA_IDLE_RESULT, -1);
983 AltosDebug.debug("intent idle_mode %d", type);
985 case IdleModeActivity.IDLE_MODE_CONNECT:
986 msg = Message.obtain(null, TelemetryService.MSG_MONITOR_IDLE_START);
989 } catch (RemoteException re) {
992 case IdleModeActivity.IDLE_MODE_DISCONNECT:
993 msg = Message.obtain(null, TelemetryService.MSG_MONITOR_IDLE_STOP);
996 } catch (RemoteException re) {
999 case IdleModeActivity.IDLE_MODE_REBOOT:
1000 msg = Message.obtain(null, TelemetryService.MSG_REBOOT);
1003 } catch (RemoteException re) {
1006 case IdleModeActivity.IDLE_MODE_IGNITERS:
1007 Intent serverIntent = new Intent(this, IgniterActivity.class);
1008 startActivityForResult(serverIntent, REQUEST_IGNITERS);
1014 public boolean onCreateOptionsMenu(Menu menu) {
1015 MenuInflater inflater = getMenuInflater();
1016 inflater.inflate(R.menu.option_menu, menu);
1020 double telem_frequency = 434.550;
1021 double selected_frequency = AltosLib.MISSING;
1023 void setFrequency(double freq) {
1024 telem_frequency = freq;
1025 selected_frequency = AltosLib.MISSING;
1027 mService.send(Message.obtain(null, TelemetryService.MSG_SETFREQUENCY, freq));
1029 } catch (RemoteException e) {
1033 void setFrequency(AltosFrequency frequency) {
1034 setFrequency (frequency.frequency);
1037 void setBaud(int baud) {
1039 mService.send(Message.obtain(null, TelemetryService.MSG_SETBAUD, baud));
1041 } catch (RemoteException e) {
1045 void setBaud(String baud) {
1047 int value = Integer.parseInt(baud);
1048 int rate = AltosLib.ao_telemetry_rate_38400;
1051 rate = AltosLib.ao_telemetry_rate_2400;
1054 rate = AltosLib.ao_telemetry_rate_9600;
1057 rate = AltosLib.ao_telemetry_rate_38400;
1061 } catch (NumberFormatException e) {
1065 void select_tracker(int serial, double frequency) {
1067 AltosDebug.debug("select tracker %d %7.3f\n", serial, frequency);
1069 if (serial == selected_serial) {
1070 AltosDebug.debug("%d already selected\n", serial);
1076 for (i = 0; i < trackers.length; i++)
1077 if (trackers[i].serial == serial)
1080 if (i == trackers.length) {
1081 AltosDebug.debug("attempt to select unknown tracker %d\n", serial);
1084 if (frequency != 0.0 && frequency != AltosLib.MISSING)
1085 setFrequency(frequency);
1088 selected_serial = serial;
1092 void select_tracker(Intent data) {
1093 int serial = data.getIntExtra(SelectTrackerActivity.EXTRA_SERIAL_NUMBER, 0);
1094 double frequency = data.getDoubleExtra(SelectTrackerActivity.EXTRA_FREQUENCY, 0.0);
1095 select_tracker(serial, frequency);
1098 void delete_track(int serial) {
1100 mService.send(Message.obtain(null, TelemetryService.MSG_DELETE_SERIAL, (Integer) serial));
1101 } catch (Exception ex) {
1105 void delete_track(Intent data) {
1106 int serial = data.getIntExtra(SelectTrackerActivity.EXTRA_SERIAL_NUMBER, 0);
1108 delete_track(serial);
1111 void start_select_tracker(Tracker[] select_trackers, int title_id, int request) {
1112 Intent intent = new Intent(this, SelectTrackerActivity.class);
1113 AltosDebug.debug("put title id 0x%x %s", title_id, getResources().getString(title_id));
1114 intent.putExtra(EXTRA_TRACKERS_TITLE, title_id);
1115 if (select_trackers != null) {
1116 ArrayList<Tracker> tracker_array = new ArrayList<Tracker>(Arrays.asList(select_trackers));
1117 intent.putParcelableArrayListExtra(EXTRA_TRACKERS, tracker_array);
1119 intent.putExtra(EXTRA_TRACKERS, (Parcelable[]) null);
1121 startActivityForResult(intent, request);
1124 void start_select_tracker(Tracker[] select_trackers) {
1125 start_select_tracker(select_trackers, R.string.select_tracker, REQUEST_SELECT_TRACKER);
1128 void touch_trackers(Integer[] serials) {
1129 Tracker[] my_trackers = new Tracker[serials.length];
1131 for (int i = 0; i < serials.length; i++) {
1132 AltosState s = telemetry_state.get(serials[i]);
1133 my_trackers[i] = new Tracker(s);
1135 start_select_tracker(my_trackers);
1139 public boolean onOptionsItemSelected(MenuItem item) {
1140 Intent serverIntent = null;
1141 switch (item.getItemId()) {
1142 case R.id.connect_scan:
1144 // Launch the DeviceListActivity to see devices and do scan
1145 serverIntent = new Intent(this, DeviceListActivity.class);
1146 startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
1148 case R.id.disconnect:
1149 /* Disconnect the device
1151 disconnectDevice(false);
1154 AltosDebug.debug("R.id.quit");
1155 disconnectDevice(true);
1159 serverIntent = new Intent(this, SetupActivity.class);
1160 startActivityForResult(serverIntent, REQUEST_SETUP);
1162 case R.id.select_freq:
1163 // Set the TBT radio frequency
1165 final AltosFrequency[] frequencies = AltosPreferences.common_frequencies();
1166 String[] frequency_strings = new String[frequencies.length];
1167 for (int i = 0; i < frequencies.length; i++)
1168 frequency_strings[i] = frequencies[i].toString();
1170 AlertDialog.Builder builder_freq = new AlertDialog.Builder(this);
1171 builder_freq.setTitle("Select Frequency");
1172 builder_freq.setItems(frequency_strings,
1173 new DialogInterface.OnClickListener() {
1174 public void onClick(DialogInterface dialog, int item) {
1175 setFrequency(frequencies[item]);
1176 selected_frequency = frequencies[item].frequency;
1179 AlertDialog alert_freq = builder_freq.create();
1182 case R.id.select_tracker:
1183 start_select_tracker(trackers);
1185 case R.id.delete_track:
1186 if (trackers != null && trackers.length > 0)
1187 start_select_tracker(trackers, R.string.delete_track, REQUEST_DELETE_TRACKER);
1189 case R.id.idle_mode:
1190 serverIntent = new Intent(this, IdleModeActivity.class);
1191 serverIntent.putExtra(EXTRA_IDLE_MODE, idle_mode);
1192 serverIntent.putExtra(EXTRA_FREQUENCY, telem_frequency);
1193 startActivityForResult(serverIntent, REQUEST_IDLE_MODE);
1199 static String direction(AltosGreatCircle from_receiver,
1200 Location receiver) {
1201 if (from_receiver == null)
1204 if (receiver == null)
1207 if (!receiver.hasBearing())
1210 float bearing = receiver.getBearing();
1211 float heading = (float) from_receiver.bearing - bearing;
1213 while (heading <= -180.0f)
1215 while (heading > 180.0f)
1218 int iheading = (int) (heading + 0.5f);
1220 if (-1 < iheading && iheading < 1)
1222 else if (iheading < -179 || 179 < iheading)
1224 else if (iheading < 0)
1225 return String.format("left %d°", -iheading);
1227 return String.format("right %d°", iheading);
1230 public void onLocationChanged(Location location) {
1231 this.location = location;
1232 AltosDebug.debug("Location changed to %f,%f",
1233 location.getLatitude(),
1234 location.getLongitude());
1235 update_ui(telemetry_state, state, false);
1238 public void onStatusChanged(String provider, int status, Bundle extras) {
1239 AltosDebug.debug("Location status now %d\n", status);
1242 public void onProviderEnabled(String provider) {
1243 AltosDebug.debug("Location provider enabled %s\n", provider);
1246 public void onProviderDisabled(String provider) {
1247 AltosDebug.debug("Location provider disabled %s\n", provider);