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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 package org.altusmetrum.AltosDroid;
20 import java.lang.ref.WeakReference;
25 import android.app.Activity;
26 import android.app.PendingIntent;
27 import android.bluetooth.BluetoothAdapter;
28 import android.bluetooth.BluetoothDevice;
29 import android.content.Intent;
30 import android.content.Context;
31 import android.content.ComponentName;
32 import android.content.ServiceConnection;
33 import android.content.DialogInterface;
34 import android.os.IBinder;
35 import android.os.Bundle;
36 import android.os.Handler;
37 import android.os.Message;
38 import android.os.Messenger;
39 import android.os.RemoteException;
40 import android.content.res.Resources;
41 import android.support.v4.app.FragmentActivity;
42 import android.support.v4.app.FragmentManager;
43 import android.util.DisplayMetrics;
44 import android.view.*;
45 import android.widget.*;
46 import android.app.AlertDialog;
47 import android.location.Location;
48 import android.hardware.usb.*;
49 import android.graphics.*;
50 import android.graphics.drawable.*;
52 import org.altusmetrum.altoslib_9.*;
54 public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
56 // Actions sent to the telemetry server at startup time
58 public static final String ACTION_BLUETOOTH = "org.altusmetrum.AltosDroid.BLUETOOTH";
59 public static final String ACTION_USB = "org.altusmetrum.AltosDroid.USB";
61 // Message types received by our Handler
63 public static final int MSG_STATE = 1;
64 public static final int MSG_UPDATE_AGE = 2;
66 // Intent request codes
67 public static final int REQUEST_CONNECT_DEVICE = 1;
68 public static final int REQUEST_ENABLE_BT = 2;
69 public static final int REQUEST_PRELOAD_MAPS = 3;
70 public static final int REQUEST_MAP_TYPE = 4;
72 public int map_type = AltosMap.maptype_hybrid;
74 public static FragmentManager fm;
76 private BluetoothAdapter mBluetoothAdapter = null;
78 // Flight state values
79 private TextView mCallsignView;
80 private TextView mRSSIView;
81 private TextView mSerialView;
82 private TextView mFlightView;
83 private RelativeLayout mStateLayout;
84 private TextView mStateView;
85 private TextView mAgeView;
86 private boolean mAgeViewOld;
87 private int mAgeNewColor;
88 private int mAgeOldColor;
90 public static final String tab_pad_name = "pad";
91 public static final String tab_flight_name = "flight";
92 public static final String tab_recover_name = "recover";
93 public static final String tab_map_name = "map";
95 // field to display the version at the bottom of the screen
96 private TextView mVersion;
98 private double frequency;
99 private int telemetry_rate;
103 AltosViewPager mViewPager;
104 TabsAdapter mTabsAdapter;
105 ArrayList<AltosDroidTab> mTabs = new ArrayList<AltosDroidTab>();
108 // Timer and Saved flight state for Age calculation
110 AltosState saved_state;
111 TelemetryState telemetry_state;
114 UsbDevice pending_usb_device;
115 boolean start_with_usb;
118 private boolean mIsBound = false;
119 private Messenger mService = null;
120 final Messenger mMessenger = new Messenger(new IncomingHandler(this));
123 private AltosVoice mAltosVoice = null;
125 // The Handler that gets information back from the Telemetry Service
126 static class IncomingHandler extends Handler {
127 private final WeakReference<AltosDroid> mAltosDroid;
128 IncomingHandler(AltosDroid ad) { mAltosDroid = new WeakReference<AltosDroid>(ad); }
131 public void handleMessage(Message msg) {
132 AltosDroid ad = mAltosDroid.get();
136 if (msg.obj == null) {
137 AltosDebug.debug("telemetry_state null!");
140 ad.update_state((TelemetryState) msg.obj);
150 private ServiceConnection mConnection = new ServiceConnection() {
151 public void onServiceConnected(ComponentName className, IBinder service) {
152 mService = new Messenger(service);
154 Message msg = Message.obtain(null, TelemetryService.MSG_REGISTER_CLIENT);
155 msg.replyTo = mMessenger;
157 } catch (RemoteException e) {
158 // In this case the service has crashed before we could even do anything with it
160 if (pending_usb_device != null) {
162 mService.send(Message.obtain(null, TelemetryService.MSG_OPEN_USB, pending_usb_device));
163 pending_usb_device = null;
164 } catch (RemoteException e) {
169 public void onServiceDisconnected(ComponentName className) {
170 // This is called when the connection with the service has been unexpectedly disconnected - process crashed.
175 void doBindService() {
176 bindService(new Intent(this, TelemetryService.class), mConnection, Context.BIND_AUTO_CREATE);
180 void doUnbindService() {
182 // If we have received the service, and hence registered with it, then now is the time to unregister.
183 if (mService != null) {
185 Message msg = Message.obtain(null, TelemetryService.MSG_UNREGISTER_CLIENT);
186 msg.replyTo = mMessenger;
188 } catch (RemoteException e) {
189 // There is nothing special we need to do if the service has crashed.
192 // Detach our existing connection.
193 unbindService(mConnection);
198 public void registerTab(AltosDroidTab mTab) {
202 public void unregisterTab(AltosDroidTab mTab) {
206 public void units_changed(boolean imperial_units) {
207 for (AltosDroidTab mTab : mTabs)
208 mTab.units_changed(imperial_units);
211 void update_title(TelemetryState telemetry_state) {
212 switch (telemetry_state.connect) {
213 case TelemetryState.CONNECT_CONNECTED:
214 if (telemetry_state.config != null) {
215 String str = String.format("S/N %d %6.3f MHz", telemetry_state.config.serial,
216 telemetry_state.frequency);
217 if (telemetry_state.telemetry_rate != AltosLib.ao_telemetry_rate_38400)
218 str = str.concat(String.format(" %d bps",
219 AltosLib.ao_telemetry_rate_values[telemetry_state.telemetry_rate]));
222 setTitle(R.string.title_connected_to);
225 case TelemetryState.CONNECT_CONNECTING:
226 if (telemetry_state.address != null)
227 setTitle(String.format("Connecting to %s...", telemetry_state.address.name));
229 setTitle("Connecting to something...");
231 case TelemetryState.CONNECT_DISCONNECTED:
232 case TelemetryState.CONNECT_NONE:
233 setTitle(R.string.title_not_connected);
241 timer.scheduleAtFixedRate(new TimerTask(){ public void run() {onTimerTick();}}, 1000L, 1000L);
253 int selected_serial = 0;
257 void set_switch_time() {
258 switch_time = System.currentTimeMillis();
262 boolean registered_units_listener;
264 void update_state(TelemetryState new_telemetry_state) {
266 if (new_telemetry_state != null)
267 telemetry_state = new_telemetry_state;
269 if (selected_serial != 0)
270 current_serial = selected_serial;
272 if (current_serial == 0)
273 current_serial = telemetry_state.latest_serial;
275 if (!registered_units_listener) {
276 registered_units_listener = true;
277 AltosPreferences.register_units_listener(this);
280 serials = telemetry_state.states.keySet().toArray(new Integer[0]);
281 Arrays.sort(serials);
283 update_title(telemetry_state);
285 AltosState state = null;
288 if (telemetry_state.states.containsKey(current_serial)) {
289 state = telemetry_state.states.get(current_serial);
290 int age = state_age(state);
293 if (current_serial == selected_serial)
295 else if (switch_time != 0 && (switch_time - state.received_time) > 0)
300 AltosState newest_state = null;
303 for (int serial : telemetry_state.states.keySet()) {
304 AltosState existing = telemetry_state.states.get(serial);
305 int existing_age = state_age(existing);
307 if (newest_state == null || existing_age < newest_age) {
308 newest_state = existing;
309 newest_age = existing_age;
313 if (newest_state != null)
314 state = newest_state;
317 update_ui(telemetry_state, state, telemetry_state.location);
322 boolean same_string(String a, String b) {
335 private int blend_component(int a, int b, double r, int shift, int mask) {
336 return ((int) (((a >> shift) & mask) * r + ((b >> shift) & mask) * (1 - r)) & mask) << shift;
338 private int blend_color(int a, int b, double r) {
339 return (blend_component(a, b, r, 0, 0xff) |
340 blend_component(a, b, r, 8, 0xff) |
341 blend_component(a, b, r, 16, 0xff) |
342 blend_component(a, b, r, 24, 0xff));
345 int state_age(AltosState state) {
346 return (int) ((System.currentTimeMillis() - state.received_time + 500) / 1000);
349 void set_screen_on(int age) {
351 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
353 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
357 if (saved_state != null) {
358 int age = state_age(saved_state);
360 double age_scale = age / 100.0;
365 mAgeView.setTextColor(blend_color(mAgeOldColor, mAgeNewColor, age_scale));
371 text = String.format("%ds", age);
372 else if (age < 60 * 60)
373 text = String.format("%dm", age / 60);
374 else if (age < 60 * 60 * 24)
375 text = String.format("%dh", age / (60 * 60));
377 text = String.format("%dd", age / (24 * 60 * 60));
378 mAgeView.setText(text);
382 void update_ui(TelemetryState telem_state, AltosState state, Location location) {
384 int prev_state = AltosLib.ao_flight_invalid;
386 AltosGreatCircle from_receiver = null;
388 if (saved_state != null)
389 prev_state = saved_state.state();
392 set_screen_on(state_age(state));
394 if (state.state() == AltosLib.ao_flight_stateless) {
395 boolean prev_locked = false;
396 boolean locked = false;
398 if(state.gps != null)
399 locked = state.gps.locked;
400 if (saved_state != null && saved_state.gps != null)
401 prev_locked = saved_state.gps.locked;
402 if (prev_locked != locked) {
403 String currentTab = mTabHost.getCurrentTabTag();
405 if (currentTab.equals(tab_pad_name)) mTabHost.setCurrentTabByTag(tab_flight_name);
407 if (currentTab.equals(tab_flight_name)) mTabHost.setCurrentTabByTag(tab_pad_name);
411 if (prev_state != state.state()) {
412 String currentTab = mTabHost.getCurrentTabTag();
413 switch (state.state()) {
414 case AltosLib.ao_flight_boost:
415 if (currentTab.equals(tab_pad_name)) mTabHost.setCurrentTabByTag(tab_flight_name);
417 case AltosLib.ao_flight_landed:
418 if (currentTab.equals(tab_flight_name)) mTabHost.setCurrentTabByTag(tab_recover_name);
420 case AltosLib.ao_flight_stateless:
421 if (currentTab.equals(tab_pad_name)) mTabHost.setCurrentTabByTag(tab_flight_name);
427 if (location != null && state.gps != null && state.gps.locked) {
429 if (location.hasAltitude())
430 altitude = location.getAltitude();
431 from_receiver = new AltosGreatCircle(location.getLatitude(),
432 location.getLongitude(),
439 if (saved_state == null || !same_string(saved_state.callsign, state.callsign)) {
440 mCallsignView.setText(state.callsign);
442 if (saved_state == null || state.serial != saved_state.serial) {
443 mSerialView.setText(String.format("%d", state.serial));
445 if (saved_state == null || state.flight != saved_state.flight) {
446 if (state.flight == AltosLib.MISSING)
447 mFlightView.setText("");
449 mFlightView.setText(String.format("%d", state.flight));
451 if (saved_state == null || state.state() != saved_state.state()) {
452 if (state.state() == AltosLib.ao_flight_stateless) {
453 mStateLayout.setVisibility(View.GONE);
455 mStateView.setText(state.state_name());
456 mStateLayout.setVisibility(View.VISIBLE);
459 if (saved_state == null || state.rssi != saved_state.rssi) {
460 mRSSIView.setText(String.format("%d", state.rssi));
464 for (AltosDroidTab mTab : mTabs)
465 mTab.update_ui(telem_state, state, from_receiver, location, mTab == mTabsAdapter.currentItem());
467 if (mAltosVoice != null)
468 mAltosVoice.tell(telem_state, state, from_receiver, location, (AltosDroidTab) mTabsAdapter.currentItem());
473 private void onTimerTick() {
475 mMessenger.send(Message.obtain(null, MSG_UPDATE_AGE));
476 } catch (RemoteException e) {
480 static String pos(double p, String pos, String neg) {
482 if (p == AltosLib.MISSING)
488 int deg = (int) Math.floor(p);
489 double min = (p - Math.floor(p)) * 60.0;
490 return String.format("%d°%9.4f\" %s", deg, min, h);
493 static String number(String format, double value) {
494 if (value == AltosLib.MISSING)
496 return String.format(format, value);
499 static String integer(String format, int value) {
500 if (value == AltosLib.MISSING)
502 return String.format(format, value);
505 private View create_tab_view(String label) {
506 LayoutInflater inflater = (LayoutInflater) this.getLayoutInflater();
507 View tab_view = inflater.inflate(R.layout.tab_layout, null);
508 TextView text_view = (TextView) tab_view.findViewById (R.id.tabLabel);
509 text_view.setText(label);
513 public void set_map_source(int source) {
514 for (AltosDroidTab mTab : mTabs)
515 mTab.set_map_source(source);
519 public void onCreate(Bundle savedInstanceState) {
520 super.onCreate(savedInstanceState);
521 AltosDebug.init(this);
522 AltosDebug.debug("+++ ON CREATE +++");
524 // Initialise preferences
525 AltosDroidPreferences.init(this);
527 fm = getSupportFragmentManager();
529 // Set up the window layout
530 setContentView(R.layout.altosdroid);
532 // Create the Tabs and ViewPager
533 mTabHost = (TabHost)findViewById(android.R.id.tabhost);
536 mViewPager = (AltosViewPager)findViewById(R.id.pager);
537 mViewPager.setOffscreenPageLimit(4);
539 mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);
541 mTabsAdapter.addTab(mTabHost.newTabSpec(tab_pad_name).setIndicator(create_tab_view("Pad")), TabPad.class, null);
542 mTabsAdapter.addTab(mTabHost.newTabSpec(tab_flight_name).setIndicator(create_tab_view("Flight")), TabFlight.class, null);
543 mTabsAdapter.addTab(mTabHost.newTabSpec(tab_recover_name).setIndicator(create_tab_view("Recover")), TabRecover.class, null);
544 mTabsAdapter.addTab(mTabHost.newTabSpec(tab_map_name).setIndicator(create_tab_view("Map")), TabMap.class, null);
546 // Display the Version
547 mVersion = (TextView) findViewById(R.id.version);
548 mVersion.setText("Version: " + BuildInfo.version +
549 " Built: " + BuildInfo.builddate + " " + BuildInfo.buildtime + " " + BuildInfo.buildtz +
550 " (" + BuildInfo.branch + "-" + BuildInfo.commitnum + "-" + BuildInfo.commithash + ")");
552 mCallsignView = (TextView) findViewById(R.id.callsign_value);
553 mRSSIView = (TextView) findViewById(R.id.rssi_value);
554 mSerialView = (TextView) findViewById(R.id.serial_value);
555 mFlightView = (TextView) findViewById(R.id.flight_value);
556 mStateLayout = (RelativeLayout) findViewById(R.id.state_container);
557 mStateView = (TextView) findViewById(R.id.state_value);
558 mAgeView = (TextView) findViewById(R.id.age_value);
559 mAgeNewColor = mAgeView.getTextColors().getDefaultColor();
560 mAgeOldColor = getResources().getColor(R.color.old_color);
563 private boolean ensureBluetooth() {
564 // Get local Bluetooth adapter
565 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
567 // If the adapter is null, then Bluetooth is not supported
568 if (mBluetoothAdapter == null) {
569 Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
573 if (!mBluetoothAdapter.isEnabled()) {
574 Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
575 startActivityForResult(enableIntent, AltosDroid.REQUEST_ENABLE_BT);
581 private boolean check_usb() {
582 UsbDevice device = AltosUsb.find_device(this, AltosLib.product_basestation);
584 if (device != null) {
585 Intent i = new Intent(this, AltosDroid.class);
586 PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent("hello world", null, this, AltosDroid.class), 0);
588 if (AltosUsb.request_permission(this, device, pi)) {
591 start_with_usb = true;
595 start_with_usb = false;
600 private void noticeIntent(Intent intent) {
602 /* Ok, this is pretty convenient.
604 * When a USB device is plugged in, and our 'hotplug'
605 * intent registration fires, we get an Intent with
608 * When we start up and see a usb device and request
609 * permission to access it, that queues a
610 * PendingIntent, which has the EXTRA_DEVICE added in,
611 * along with the EXTRA_PERMISSION_GRANTED field as
614 * So, in both cases, we get the device name using the
615 * same call. We check to see if access was granted,
616 * in which case we ignore the device field and do our
617 * usual startup thing.
620 UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
621 boolean granted = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, true);
623 AltosDebug.debug("intent %s device %s granted %s", intent, device, granted);
628 if (device != null) {
629 AltosDebug.debug("intent has usb device " + device.toString());
633 /* 'granted' is only false if this intent came
634 * from the request_permission call and
635 * permission was denied. In which case, we
636 * don't want to loop forever...
639 AltosDebug.debug("check for a USB device at startup");
643 AltosDebug.debug("Starting by looking for bluetooth devices");
644 if (ensureBluetooth())
651 public void onStart() {
653 AltosDebug.debug("++ ON START ++");
657 noticeIntent(getIntent());
659 // Start Telemetry Service
660 String action = start_with_usb ? ACTION_USB : ACTION_BLUETOOTH;
662 startService(new Intent(action, null, AltosDroid.this, TelemetryService.class));
666 if (mAltosVoice == null)
667 mAltosVoice = new AltosVoice(this);
672 public void onNewIntent(Intent intent) {
673 super.onNewIntent(intent);
674 AltosDebug.debug("onNewIntent");
675 noticeIntent(intent);
679 public void onResume() {
681 AltosDebug.debug("+ ON RESUME +");
685 public void onPause() {
687 AltosDebug.debug("- ON PAUSE -");
691 public void onStop() {
693 AltosDebug.debug("-- ON STOP --");
697 public void onDestroy() {
699 AltosDebug.debug("--- ON DESTROY ---");
702 if (mAltosVoice != null) {
709 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
710 AltosDebug.debug("onActivityResult " + resultCode);
711 switch (requestCode) {
712 case REQUEST_CONNECT_DEVICE:
713 // When DeviceListActivity returns with a device to connect to
714 if (resultCode == Activity.RESULT_OK) {
718 case REQUEST_ENABLE_BT:
719 // When the request to enable Bluetooth returns
720 if (resultCode == Activity.RESULT_OK) {
721 // Bluetooth is now enabled, so set up a chat session
724 // User did not enable Bluetooth or an error occured
725 AltosDebug.error("BT not enabled");
726 stopService(new Intent(AltosDroid.this, TelemetryService.class));
727 Toast.makeText(this, R.string.bt_not_enabled, Toast.LENGTH_SHORT).show();
731 case REQUEST_MAP_TYPE:
732 if (resultCode == Activity.RESULT_OK)
738 private void connectUsb(UsbDevice device) {
739 if (mService == null)
740 pending_usb_device = device;
742 // Attempt to connect to the device
744 mService.send(Message.obtain(null, TelemetryService.MSG_OPEN_USB, device));
745 AltosDebug.debug("Sent OPEN_USB message");
746 } catch (RemoteException e) {
747 AltosDebug.debug("connect device message failed");
752 private void connectDevice(Intent data) {
753 // Attempt to connect to the device
755 String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
756 String name = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_NAME);
758 AltosDebug.debug("Connecting to " + address + " " + name);
759 DeviceAddress a = new DeviceAddress(address, name);
760 mService.send(Message.obtain(null, TelemetryService.MSG_CONNECT, a));
761 AltosDebug.debug("Sent connecting message");
762 } catch (RemoteException e) {
763 AltosDebug.debug("connect device message failed");
767 private void disconnectDevice() {
769 mService.send(Message.obtain(null, TelemetryService.MSG_DISCONNECT, null));
770 } catch (RemoteException e) {
774 private void set_map_type(Intent data) {
775 int type = data.getIntExtra(MapTypeActivity.EXTRA_MAP_TYPE, -1);
777 AltosDebug.debug("intent set_map_type %d\n", type);
780 for (AltosDroidTab mTab : mTabs)
781 mTab.set_map_type(map_type);
786 public boolean onCreateOptionsMenu(Menu menu) {
787 MenuInflater inflater = getMenuInflater();
788 inflater.inflate(R.menu.option_menu, menu);
792 void setFrequency(double freq) {
794 mService.send(Message.obtain(null, TelemetryService.MSG_SETFREQUENCY, freq));
796 } catch (RemoteException e) {
800 void setFrequency(String freq) {
802 setFrequency (AltosParse.parse_double_net(freq.substring(11, 17)));
803 } catch (ParseException e) {
807 void setBaud(int baud) {
809 mService.send(Message.obtain(null, TelemetryService.MSG_SETBAUD, baud));
811 } catch (RemoteException e) {
815 void setBaud(String baud) {
817 int value = Integer.parseInt(baud);
818 int rate = AltosLib.ao_telemetry_rate_38400;
821 rate = AltosLib.ao_telemetry_rate_2400;
824 rate = AltosLib.ao_telemetry_rate_9600;
827 rate = AltosLib.ao_telemetry_rate_38400;
831 } catch (NumberFormatException e) {
835 void select_tracker(int serial) {
838 AltosDebug.debug("select tracker %d\n", serial);
840 if (serial == selected_serial) {
841 AltosDebug.debug("%d already selected\n", serial);
846 for (i = 0; i < serials.length; i++)
847 if (serials[i] == serial)
850 if (i == serials.length) {
851 AltosDebug.debug("attempt to select unknown tracker %d\n", serial);
856 current_serial = selected_serial = serial;
860 void touch_trackers(Integer[] serials) {
861 AlertDialog.Builder builder_tracker = new AlertDialog.Builder(this);
862 builder_tracker.setTitle("Select Tracker");
863 final String[] trackers = new String[serials.length + 1];
864 trackers[0] = "Auto";
865 for (int i = 0; i < serials.length; i++)
866 trackers[i+1] = String.format("%d", serials[i]);
867 builder_tracker.setItems(trackers,
868 new DialogInterface.OnClickListener() {
869 public void onClick(DialogInterface dialog, int item) {
873 select_tracker(Integer.parseInt(trackers[item]));
876 AlertDialog alert_tracker = builder_tracker.create();
877 alert_tracker.show();
880 void delete_track(int serial) {
882 mService.send(Message.obtain(null, TelemetryService.MSG_DELETE_SERIAL, (Integer) serial));
883 } catch (Exception ex) {
888 public boolean onOptionsItemSelected(MenuItem item) {
889 Intent serverIntent = null;
890 switch (item.getItemId()) {
891 case R.id.connect_scan:
892 if (ensureBluetooth()) {
893 // Launch the DeviceListActivity to see devices and do scan
894 serverIntent = new Intent(this, DeviceListActivity.class);
895 startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
898 case R.id.disconnect:
899 /* Disconnect the device
904 AltosDebug.debug("R.id.quit");
908 case R.id.select_freq:
909 // Set the TBT radio frequency
911 final String[] frequencies = {
912 "Channel 0 (434.550MHz)",
913 "Channel 1 (434.650MHz)",
914 "Channel 2 (434.750MHz)",
915 "Channel 3 (434.850MHz)",
916 "Channel 4 (434.950MHz)",
917 "Channel 5 (435.050MHz)",
918 "Channel 6 (435.150MHz)",
919 "Channel 7 (435.250MHz)",
920 "Channel 8 (435.350MHz)",
921 "Channel 9 (435.450MHz)"
924 AlertDialog.Builder builder_freq = new AlertDialog.Builder(this);
925 builder_freq.setTitle("Pick a frequency");
926 builder_freq.setItems(frequencies,
927 new DialogInterface.OnClickListener() {
928 public void onClick(DialogInterface dialog, int item) {
929 setFrequency(frequencies[item]);
932 AlertDialog alert_freq = builder_freq.create();
935 case R.id.select_rate:
936 // Set the TBT baud rate
938 final String[] rates = {
944 AlertDialog.Builder builder_rate = new AlertDialog.Builder(this);
945 builder_rate.setTitle("Pick a baud rate");
946 builder_rate.setItems(rates,
947 new DialogInterface.OnClickListener() {
948 public void onClick(DialogInterface dialog, int item) {
949 setBaud(rates[item]);
952 AlertDialog alert_rate = builder_rate.create();
955 case R.id.change_units:
956 boolean imperial = AltosPreferences.imperial_units();
957 AltosPreferences.set_imperial_units(!imperial);
959 case R.id.preload_maps:
960 serverIntent = new Intent(this, PreloadMapActivity.class);
961 startActivityForResult(serverIntent, REQUEST_PRELOAD_MAPS);
964 serverIntent = new Intent(this, MapTypeActivity.class);
965 startActivityForResult(serverIntent, REQUEST_MAP_TYPE);
967 case R.id.map_source:
968 int source = AltosDroidPreferences.map_source();
969 int new_source = source == AltosDroidPreferences.MAP_SOURCE_ONLINE ? AltosDroidPreferences.MAP_SOURCE_OFFLINE : AltosDroidPreferences.MAP_SOURCE_ONLINE;
970 AltosDroidPreferences.set_map_source(new_source);
971 set_map_source(new_source);
973 case R.id.select_tracker:
974 if (serials != null) {
975 String[] trackers = new String[serials.length+1];
976 trackers[0] = "Auto";
977 for (int i = 0; i < serials.length; i++)
978 trackers[i+1] = String.format("%d", serials[i]);
979 AlertDialog.Builder builder_serial = new AlertDialog.Builder(this);
980 builder_serial.setTitle("Select a tracker");
981 builder_serial.setItems(trackers,
982 new DialogInterface.OnClickListener() {
983 public void onClick(DialogInterface dialog, int item) {
987 select_tracker(serials[item-1]);
990 AlertDialog alert_serial = builder_serial.create();
995 case R.id.delete_track:
996 if (serials != null) {
997 String[] trackers = new String[serials.length];
998 for (int i = 0; i < serials.length; i++)
999 trackers[i] = String.format("%d", serials[i]);
1000 AlertDialog.Builder builder_serial = new AlertDialog.Builder(this);
1001 builder_serial.setTitle("Delete a track");
1002 builder_serial.setItems(trackers,
1003 new DialogInterface.OnClickListener() {
1004 public void onClick(DialogInterface dialog, int item) {
1005 delete_track(serials[item]);
1008 AlertDialog alert_serial = builder_serial.create();
1009 alert_serial.show();
1017 static String direction(AltosGreatCircle from_receiver,
1018 Location receiver) {
1019 if (from_receiver == null)
1022 if (receiver == null)
1025 if (!receiver.hasBearing())
1028 float bearing = receiver.getBearing();
1029 float heading = (float) from_receiver.bearing - bearing;
1031 while (heading <= -180.0f)
1033 while (heading > 180.0f)
1036 int iheading = (int) (heading + 0.5f);
1038 if (-1 < iheading && iheading < 1)
1040 else if (iheading < -179 || 179 < iheading)
1042 else if (iheading < 0)
1043 return String.format("left %d°", -iheading);
1045 return String.format("right %d°", iheading);