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_7.*;
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 // field to display the version at the bottom of the screen
91 private TextView mVersion;
93 private double frequency;
94 private int telemetry_rate;
98 AltosViewPager mViewPager;
99 TabsAdapter mTabsAdapter;
100 ArrayList<AltosDroidTab> mTabs = new ArrayList<AltosDroidTab>();
103 // Timer and Saved flight state for Age calculation
105 AltosState saved_state;
106 TelemetryState telemetry_state;
109 UsbDevice pending_usb_device;
110 boolean start_with_usb;
113 private boolean mIsBound = false;
114 private Messenger mService = null;
115 final Messenger mMessenger = new Messenger(new IncomingHandler(this));
118 private AltosVoice mAltosVoice = null;
120 // The Handler that gets information back from the Telemetry Service
121 static class IncomingHandler extends Handler {
122 private final WeakReference<AltosDroid> mAltosDroid;
123 IncomingHandler(AltosDroid ad) { mAltosDroid = new WeakReference<AltosDroid>(ad); }
126 public void handleMessage(Message msg) {
127 AltosDroid ad = mAltosDroid.get();
131 AltosDebug.debug("MSG_STATE");
132 if (msg.obj == null) {
133 AltosDebug.debug("telemetry_state null!");
136 ad.update_state((TelemetryState) msg.obj);
139 AltosDebug.debug("MSG_UPDATE_AGE");
147 private ServiceConnection mConnection = new ServiceConnection() {
148 public void onServiceConnected(ComponentName className, IBinder service) {
149 mService = new Messenger(service);
151 Message msg = Message.obtain(null, TelemetryService.MSG_REGISTER_CLIENT);
152 msg.replyTo = mMessenger;
154 } catch (RemoteException e) {
155 // In this case the service has crashed before we could even do anything with it
157 if (pending_usb_device != null) {
159 mService.send(Message.obtain(null, TelemetryService.MSG_OPEN_USB, pending_usb_device));
160 pending_usb_device = null;
161 } catch (RemoteException e) {
166 public void onServiceDisconnected(ComponentName className) {
167 // This is called when the connection with the service has been unexpectedly disconnected - process crashed.
172 void doBindService() {
173 bindService(new Intent(this, TelemetryService.class), mConnection, Context.BIND_AUTO_CREATE);
177 void doUnbindService() {
179 // If we have received the service, and hence registered with it, then now is the time to unregister.
180 if (mService != null) {
182 Message msg = Message.obtain(null, TelemetryService.MSG_UNREGISTER_CLIENT);
183 msg.replyTo = mMessenger;
185 } catch (RemoteException e) {
186 // There is nothing special we need to do if the service has crashed.
189 // Detach our existing connection.
190 unbindService(mConnection);
195 public void registerTab(AltosDroidTab mTab) {
199 public void unregisterTab(AltosDroidTab mTab) {
203 public void units_changed(boolean imperial_units) {
204 for (AltosDroidTab mTab : mTabs)
205 mTab.units_changed(imperial_units);
208 void update_title(TelemetryState telemetry_state) {
209 switch (telemetry_state.connect) {
210 case TelemetryState.CONNECT_CONNECTED:
211 if (telemetry_state.config != null) {
212 String str = String.format("S/N %d %6.3f MHz", telemetry_state.config.serial,
213 telemetry_state.frequency);
214 if (telemetry_state.telemetry_rate != AltosLib.ao_telemetry_rate_38400)
215 str = str.concat(String.format(" %d bps",
216 AltosLib.ao_telemetry_rate_values[telemetry_state.telemetry_rate]));
219 setTitle(R.string.title_connected_to);
222 case TelemetryState.CONNECT_CONNECTING:
223 if (telemetry_state.address != null)
224 setTitle(String.format("Connecting to %s...", telemetry_state.address.name));
226 setTitle("Connecting to something...");
228 case TelemetryState.CONNECT_DISCONNECTED:
229 case TelemetryState.CONNECT_NONE:
230 setTitle(R.string.title_not_connected);
238 timer.scheduleAtFixedRate(new TimerTask(){ public void run() {onTimerTick();}}, 1000L, 1000L);
250 int selected_serial = 0;
254 void set_switch_time() {
255 switch_time = System.currentTimeMillis();
259 boolean registered_units_listener;
261 void update_state(TelemetryState new_telemetry_state) {
263 if (new_telemetry_state != null)
264 telemetry_state = new_telemetry_state;
266 if (selected_serial != 0)
267 current_serial = selected_serial;
269 if (current_serial == 0)
270 current_serial = telemetry_state.latest_serial;
272 if (!registered_units_listener) {
273 registered_units_listener = true;
274 AltosPreferences.register_units_listener(this);
277 serials = telemetry_state.states.keySet().toArray(new Integer[0]);
278 Arrays.sort(serials);
280 update_title(telemetry_state);
282 AltosState state = null;
285 if (telemetry_state.states.containsKey(current_serial)) {
286 state = telemetry_state.states.get(current_serial);
287 int age = state_age(state);
290 if (current_serial == selected_serial)
292 else if (switch_time != 0 && (switch_time - state.received_time) > 0)
297 AltosState newest_state = null;
300 for (int serial : telemetry_state.states.keySet()) {
301 AltosState existing = telemetry_state.states.get(serial);
302 int existing_age = state_age(existing);
304 if (newest_state == null || existing_age < newest_age) {
305 newest_state = existing;
306 newest_age = existing_age;
310 if (newest_state != null)
311 state = newest_state;
314 update_ui(telemetry_state, state, telemetry_state.location);
319 boolean same_string(String a, String b) {
332 private int blend_component(int a, int b, double r, int shift, int mask) {
333 return ((int) (((a >> shift) & mask) * r + ((b >> shift) & mask) * (1 - r)) & mask) << shift;
335 private int blend_color(int a, int b, double r) {
336 return (blend_component(a, b, r, 0, 0xff) |
337 blend_component(a, b, r, 8, 0xff) |
338 blend_component(a, b, r, 16, 0xff) |
339 blend_component(a, b, r, 24, 0xff));
342 int state_age(AltosState state) {
343 return (int) ((System.currentTimeMillis() - state.received_time + 500) / 1000);
346 void set_screen_on(int age) {
348 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
350 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
354 if (saved_state != null) {
355 int age = state_age(saved_state);
357 double age_scale = age / 100.0;
362 mAgeView.setTextColor(blend_color(mAgeOldColor, mAgeNewColor, age_scale));
368 text = String.format("%ds", age);
369 else if (age < 60 * 60)
370 text = String.format("%dm", age / 60);
371 else if (age < 60 * 60 * 24)
372 text = String.format("%dh", age / (60 * 60));
374 text = String.format("%dd", age / (24 * 60 * 60));
375 mAgeView.setText(text);
379 void update_ui(TelemetryState telem_state, AltosState state, Location location) {
381 int prev_state = AltosLib.ao_flight_invalid;
383 AltosGreatCircle from_receiver = null;
385 if (saved_state != null)
386 prev_state = saved_state.state;
389 set_screen_on(state_age(state));
391 if (state.state == AltosLib.ao_flight_stateless) {
392 boolean prev_locked = false;
393 boolean locked = false;
395 if(state.gps != null)
396 locked = state.gps.locked;
397 if (saved_state != null && saved_state.gps != null)
398 prev_locked = saved_state.gps.locked;
399 if (prev_locked != locked) {
400 String currentTab = mTabHost.getCurrentTabTag();
402 if (currentTab.equals("pad")) mTabHost.setCurrentTabByTag("descent");
404 if (currentTab.equals("descent")) mTabHost.setCurrentTabByTag("pad");
408 if (prev_state != state.state) {
409 String currentTab = mTabHost.getCurrentTabTag();
410 switch (state.state) {
411 case AltosLib.ao_flight_boost:
412 if (currentTab.equals("pad")) mTabHost.setCurrentTabByTag("ascent");
414 case AltosLib.ao_flight_drogue:
415 if (currentTab.equals("ascent")) mTabHost.setCurrentTabByTag("descent");
417 case AltosLib.ao_flight_landed:
418 if (currentTab.equals("descent")) mTabHost.setCurrentTabByTag("landed");
420 case AltosLib.ao_flight_stateless:
421 if (currentTab.equals("pad")) mTabHost.setCurrentTabByTag("descent");
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 (state != null && mAltosVoice != null)
468 mAltosVoice.tell(state, from_receiver);
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("pad").setIndicator(create_tab_view("Pad")), TabPad.class, null);
542 mTabsAdapter.addTab(mTabHost.newTabSpec("ascent").setIndicator(create_tab_view("Ascent")), TabAscent.class, null);
543 mTabsAdapter.addTab(mTabHost.newTabSpec("descent").setIndicator(create_tab_view("Descent")), TabDescent.class, null);
544 mTabsAdapter.addTab(mTabHost.newTabSpec("landed").setIndicator(create_tab_view("Landed")), TabLanded.class, null);
545 mTabsAdapter.addTab(mTabHost.newTabSpec("map").setIndicator(create_tab_view("Map")), TabMap.class, null);
547 // Display the Version
548 mVersion = (TextView) findViewById(R.id.version);
549 mVersion.setText("Version: " + BuildInfo.version +
550 " Built: " + BuildInfo.builddate + " " + BuildInfo.buildtime + " " + BuildInfo.buildtz +
551 " (" + BuildInfo.branch + "-" + BuildInfo.commitnum + "-" + BuildInfo.commithash + ")");
553 mCallsignView = (TextView) findViewById(R.id.callsign_value);
554 mRSSIView = (TextView) findViewById(R.id.rssi_value);
555 mSerialView = (TextView) findViewById(R.id.serial_value);
556 mFlightView = (TextView) findViewById(R.id.flight_value);
557 mStateLayout = (RelativeLayout) findViewById(R.id.state_container);
558 mStateView = (TextView) findViewById(R.id.state_value);
559 mAgeView = (TextView) findViewById(R.id.age_value);
560 mAgeNewColor = mAgeView.getTextColors().getDefaultColor();
561 mAgeOldColor = getResources().getColor(R.color.old_color);
564 private boolean ensureBluetooth() {
565 // Get local Bluetooth adapter
566 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
568 // If the adapter is null, then Bluetooth is not supported
569 if (mBluetoothAdapter == null) {
570 Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
574 if (!mBluetoothAdapter.isEnabled()) {
575 Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
576 startActivityForResult(enableIntent, AltosDroid.REQUEST_ENABLE_BT);
582 private boolean check_usb() {
583 UsbDevice device = AltosUsb.find_device(this, AltosLib.product_basestation);
585 if (device != null) {
586 Intent i = new Intent(this, AltosDroid.class);
587 PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent("hello world", null, this, AltosDroid.class), 0);
589 if (AltosUsb.request_permission(this, device, pi)) {
592 start_with_usb = true;
596 start_with_usb = false;
601 private void noticeIntent(Intent intent) {
603 /* Ok, this is pretty convenient.
605 * When a USB device is plugged in, and our 'hotplug'
606 * intent registration fires, we get an Intent with
609 * When we start up and see a usb device and request
610 * permission to access it, that queues a
611 * PendingIntent, which has the EXTRA_DEVICE added in,
612 * along with the EXTRA_PERMISSION_GRANTED field as
615 * So, in both cases, we get the device name using the
616 * same call. We check to see if access was granted,
617 * in which case we ignore the device field and do our
618 * usual startup thing.
621 UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
622 boolean granted = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, true);
624 AltosDebug.debug("intent %s device %s granted %s", intent, device, granted);
629 if (device != null) {
630 AltosDebug.debug("intent has usb device " + device.toString());
634 /* 'granted' is only false if this intent came
635 * from the request_permission call and
636 * permission was denied. In which case, we
637 * don't want to loop forever...
640 AltosDebug.debug("check for a USB device at startup");
644 AltosDebug.debug("Starting by looking for bluetooth devices");
645 if (ensureBluetooth())
652 public void onStart() {
654 AltosDebug.debug("++ ON START ++");
656 noticeIntent(getIntent());
658 // Start Telemetry Service
659 String action = start_with_usb ? ACTION_USB : ACTION_BLUETOOTH;
661 startService(new Intent(action, null, AltosDroid.this, TelemetryService.class));
665 if (mAltosVoice == null)
666 mAltosVoice = new AltosVoice(this);
671 public void onNewIntent(Intent intent) {
672 super.onNewIntent(intent);
673 AltosDebug.debug("onNewIntent");
674 noticeIntent(intent);
678 public void onResume() {
680 AltosDebug.debug("+ ON RESUME +");
684 public void onPause() {
686 AltosDebug.debug("- ON PAUSE -");
690 public void onStop() {
692 AltosDebug.debug("-- ON STOP --");
695 if (mAltosVoice != null) {
702 public void onDestroy() {
704 AltosDebug.debug("--- ON DESTROY ---");
706 if (mAltosVoice != null) mAltosVoice.stop();
710 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
711 AltosDebug.debug("onActivityResult " + resultCode);
712 switch (requestCode) {
713 case REQUEST_CONNECT_DEVICE:
714 // When DeviceListActivity returns with a device to connect to
715 if (resultCode == Activity.RESULT_OK) {
719 case REQUEST_ENABLE_BT:
720 // When the request to enable Bluetooth returns
721 if (resultCode == Activity.RESULT_OK) {
722 // Bluetooth is now enabled, so set up a chat session
725 // User did not enable Bluetooth or an error occured
726 AltosDebug.error("BT not enabled");
727 stopService(new Intent(AltosDroid.this, TelemetryService.class));
728 Toast.makeText(this, R.string.bt_not_enabled, Toast.LENGTH_SHORT).show();
732 case REQUEST_MAP_TYPE:
733 if (resultCode == Activity.RESULT_OK)
739 private void connectUsb(UsbDevice device) {
740 if (mService == null)
741 pending_usb_device = device;
743 // Attempt to connect to the device
745 mService.send(Message.obtain(null, TelemetryService.MSG_OPEN_USB, device));
746 AltosDebug.debug("Sent OPEN_USB message");
747 } catch (RemoteException e) {
748 AltosDebug.debug("connect device message failed");
753 private void connectDevice(Intent data) {
754 // Attempt to connect to the device
756 String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
757 String name = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_NAME);
759 AltosDebug.debug("Connecting to " + address + " " + name);
760 DeviceAddress a = new DeviceAddress(address, name);
761 mService.send(Message.obtain(null, TelemetryService.MSG_CONNECT, a));
762 AltosDebug.debug("Sent connecting message");
763 } catch (RemoteException e) {
764 AltosDebug.debug("connect device message failed");
768 private void disconnectDevice() {
770 mService.send(Message.obtain(null, TelemetryService.MSG_DISCONNECT, null));
771 } catch (RemoteException e) {
775 private void set_map_type(Intent data) {
776 int type = data.getIntExtra(MapTypeActivity.EXTRA_MAP_TYPE, -1);
778 AltosDebug.debug("intent set_map_type %d\n", type);
781 for (AltosDroidTab mTab : mTabs)
782 mTab.set_map_type(map_type);
787 public boolean onCreateOptionsMenu(Menu menu) {
788 MenuInflater inflater = getMenuInflater();
789 inflater.inflate(R.menu.option_menu, menu);
793 void setFrequency(double freq) {
795 mService.send(Message.obtain(null, TelemetryService.MSG_SETFREQUENCY, freq));
797 } catch (RemoteException e) {
801 void setFrequency(String freq) {
803 setFrequency (AltosParse.parse_double_net(freq.substring(11, 17)));
804 } catch (ParseException e) {
808 void setBaud(int baud) {
810 mService.send(Message.obtain(null, TelemetryService.MSG_SETBAUD, baud));
812 } catch (RemoteException e) {
816 void setBaud(String baud) {
818 int value = Integer.parseInt(baud);
819 int rate = AltosLib.ao_telemetry_rate_38400;
822 rate = AltosLib.ao_telemetry_rate_2400;
825 rate = AltosLib.ao_telemetry_rate_9600;
828 rate = AltosLib.ao_telemetry_rate_38400;
832 } catch (NumberFormatException e) {
836 void select_tracker(int serial) {
839 AltosDebug.debug("select tracker %d\n", serial);
841 if (serial == selected_serial) {
842 AltosDebug.debug("%d already selected\n", serial);
847 for (i = 0; i < serials.length; i++)
848 if (serials[i] == serial)
851 if (i == serials.length) {
852 AltosDebug.debug("attempt to select unknown tracker %d\n", serial);
857 current_serial = selected_serial = serial;
861 void delete_track(int serial) {
863 mService.send(Message.obtain(null, TelemetryService.MSG_DELETE_SERIAL, (Integer) serial));
864 } catch (Exception ex) {
869 public boolean onOptionsItemSelected(MenuItem item) {
870 Intent serverIntent = null;
871 switch (item.getItemId()) {
872 case R.id.connect_scan:
873 if (ensureBluetooth()) {
874 // Launch the DeviceListActivity to see devices and do scan
875 serverIntent = new Intent(this, DeviceListActivity.class);
876 startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
879 case R.id.disconnect:
880 /* Disconnect the device
885 AltosDebug.debug("R.id.quit");
889 case R.id.select_freq:
890 // Set the TBT radio frequency
892 final String[] frequencies = {
893 "Channel 0 (434.550MHz)",
894 "Channel 1 (434.650MHz)",
895 "Channel 2 (434.750MHz)",
896 "Channel 3 (434.850MHz)",
897 "Channel 4 (434.950MHz)",
898 "Channel 5 (435.050MHz)",
899 "Channel 6 (435.150MHz)",
900 "Channel 7 (435.250MHz)",
901 "Channel 8 (435.350MHz)",
902 "Channel 9 (435.450MHz)"
905 AlertDialog.Builder builder_freq = new AlertDialog.Builder(this);
906 builder_freq.setTitle("Pick a frequency");
907 builder_freq.setItems(frequencies,
908 new DialogInterface.OnClickListener() {
909 public void onClick(DialogInterface dialog, int item) {
910 setFrequency(frequencies[item]);
913 AlertDialog alert_freq = builder_freq.create();
916 case R.id.select_rate:
917 // Set the TBT baud rate
919 final String[] rates = {
925 AlertDialog.Builder builder_rate = new AlertDialog.Builder(this);
926 builder_rate.setTitle("Pick a baud rate");
927 builder_rate.setItems(rates,
928 new DialogInterface.OnClickListener() {
929 public void onClick(DialogInterface dialog, int item) {
930 setBaud(rates[item]);
933 AlertDialog alert_rate = builder_rate.create();
936 case R.id.change_units:
937 boolean imperial = AltosPreferences.imperial_units();
938 AltosPreferences.set_imperial_units(!imperial);
940 case R.id.preload_maps:
941 serverIntent = new Intent(this, PreloadMapActivity.class);
942 startActivityForResult(serverIntent, REQUEST_PRELOAD_MAPS);
945 serverIntent = new Intent(this, MapTypeActivity.class);
946 startActivityForResult(serverIntent, REQUEST_MAP_TYPE);
948 case R.id.map_source:
949 int source = AltosDroidPreferences.map_source();
950 int new_source = source == AltosDroidPreferences.MAP_SOURCE_ONLINE ? AltosDroidPreferences.MAP_SOURCE_OFFLINE : AltosDroidPreferences.MAP_SOURCE_ONLINE;
951 AltosDroidPreferences.set_map_source(new_source);
952 set_map_source(new_source);
954 case R.id.select_tracker:
955 if (serials != null) {
956 String[] trackers = new String[serials.length+1];
957 trackers[0] = "Auto";
958 for (int i = 0; i < serials.length; i++)
959 trackers[i+1] = String.format("%d", serials[i]);
960 AlertDialog.Builder builder_serial = new AlertDialog.Builder(this);
961 builder_serial.setTitle("Select a tracker");
962 builder_serial.setItems(trackers,
963 new DialogInterface.OnClickListener() {
964 public void onClick(DialogInterface dialog, int item) {
968 select_tracker(serials[item-1]);
971 AlertDialog alert_serial = builder_serial.create();
976 case R.id.delete_track:
977 if (serials != null) {
978 String[] trackers = new String[serials.length];
979 for (int i = 0; i < serials.length; i++)
980 trackers[i] = String.format("%d", serials[i]);
981 AlertDialog.Builder builder_serial = new AlertDialog.Builder(this);
982 builder_serial.setTitle("Delete a track");
983 builder_serial.setItems(trackers,
984 new DialogInterface.OnClickListener() {
985 public void onClick(DialogInterface dialog, int item) {
986 delete_track(serials[item]);
989 AlertDialog alert_serial = builder_serial.create();