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;
21 import java.util.ArrayList;
22 import java.util.Timer;
23 import java.util.TimerTask;
26 import android.app.Activity;
27 import android.app.PendingIntent;
28 import android.bluetooth.BluetoothAdapter;
29 import android.bluetooth.BluetoothDevice;
30 import android.content.Intent;
31 import android.content.Context;
32 import android.content.ComponentName;
33 import android.content.ServiceConnection;
34 import android.content.DialogInterface;
35 import android.os.IBinder;
36 import android.os.Bundle;
37 import android.os.Handler;
38 import android.os.Message;
39 import android.os.Messenger;
40 import android.os.RemoteException;
41 import android.content.res.Resources;
42 import android.support.v4.app.FragmentActivity;
43 import android.support.v4.app.FragmentManager;
44 import android.util.DisplayMetrics;
45 import android.view.*;
46 import android.widget.*;
47 import android.app.AlertDialog;
48 import android.location.Location;
49 import android.hardware.usb.*;
50 import android.graphics.*;
51 import android.graphics.drawable.*;
53 import org.altusmetrum.altoslib_7.*;
55 public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
57 // Actions sent to the telemetry server at startup time
59 public static final String ACTION_BLUETOOTH = "org.altusmetrum.AltosDroid.BLUETOOTH";
60 public static final String ACTION_USB = "org.altusmetrum.AltosDroid.USB";
62 // Message types received by our Handler
64 public static final int MSG_STATE = 1;
65 public static final int MSG_UPDATE_AGE = 2;
67 // Intent request codes
68 public static final int REQUEST_CONNECT_DEVICE = 1;
69 public static final int REQUEST_ENABLE_BT = 2;
70 public static final int REQUEST_PRELOAD_MAPS = 3;
71 public static final int REQUEST_MAP_TYPE = 4;
73 public int map_type = AltosMap.maptype_hybrid;
75 public static FragmentManager fm;
77 private BluetoothAdapter mBluetoothAdapter = null;
79 // Flight state values
80 private TextView mCallsignView;
81 private TextView mRSSIView;
82 private TextView mSerialView;
83 private TextView mFlightView;
84 private RelativeLayout mStateLayout;
85 private TextView mStateView;
86 private TextView mAgeView;
87 private boolean mAgeViewOld;
88 private int mAgeNewColor;
89 private int mAgeOldColor;
91 // field to display the version at the bottom of the screen
92 private TextView mVersion;
94 private double frequency;
95 private int telemetry_rate;
99 AltosViewPager mViewPager;
100 TabsAdapter mTabsAdapter;
101 ArrayList<AltosDroidTab> mTabs = new ArrayList<AltosDroidTab>();
104 // Timer and Saved flight state for Age calculation
106 AltosState saved_state;
107 TelemetryState telemetry_state;
110 UsbDevice pending_usb_device;
111 boolean start_with_usb;
114 private boolean mIsBound = false;
115 private Messenger mService = null;
116 final Messenger mMessenger = new Messenger(new IncomingHandler(this));
119 private AltosVoice mAltosVoice = null;
121 // The Handler that gets information back from the Telemetry Service
122 static class IncomingHandler extends Handler {
123 private final WeakReference<AltosDroid> mAltosDroid;
124 IncomingHandler(AltosDroid ad) { mAltosDroid = new WeakReference<AltosDroid>(ad); }
127 public void handleMessage(Message msg) {
128 AltosDroid ad = mAltosDroid.get();
132 AltosDebug.debug("MSG_STATE");
133 if (msg.obj == null) {
134 AltosDebug.debug("telemetry_state null!");
137 ad.update_state((TelemetryState) msg.obj);
140 AltosDebug.debug("MSG_UPDATE_AGE");
148 private ServiceConnection mConnection = new ServiceConnection() {
149 public void onServiceConnected(ComponentName className, IBinder service) {
150 mService = new Messenger(service);
152 Message msg = Message.obtain(null, TelemetryService.MSG_REGISTER_CLIENT);
153 msg.replyTo = mMessenger;
155 } catch (RemoteException e) {
156 // In this case the service has crashed before we could even do anything with it
158 if (pending_usb_device != null) {
160 mService.send(Message.obtain(null, TelemetryService.MSG_OPEN_USB, pending_usb_device));
161 pending_usb_device = null;
162 } catch (RemoteException e) {
167 public void onServiceDisconnected(ComponentName className) {
168 // This is called when the connection with the service has been unexpectedly disconnected - process crashed.
173 void doBindService() {
174 bindService(new Intent(this, TelemetryService.class), mConnection, Context.BIND_AUTO_CREATE);
178 void doUnbindService() {
180 // If we have received the service, and hence registered with it, then now is the time to unregister.
181 if (mService != null) {
183 Message msg = Message.obtain(null, TelemetryService.MSG_UNREGISTER_CLIENT);
184 msg.replyTo = mMessenger;
186 } catch (RemoteException e) {
187 // There is nothing special we need to do if the service has crashed.
190 // Detach our existing connection.
191 unbindService(mConnection);
196 public void registerTab(AltosDroidTab mTab) {
200 public void unregisterTab(AltosDroidTab mTab) {
204 public void units_changed(boolean imperial_units) {
205 for (AltosDroidTab mTab : mTabs)
206 mTab.units_changed(imperial_units);
209 void update_title(TelemetryState telemetry_state) {
210 switch (telemetry_state.connect) {
211 case TelemetryState.CONNECT_CONNECTED:
212 if (telemetry_state.config != null) {
213 String str = String.format("S/N %d %6.3f MHz", telemetry_state.config.serial,
214 telemetry_state.frequency);
215 if (telemetry_state.telemetry_rate != AltosLib.ao_telemetry_rate_38400)
216 str = str.concat(String.format(" %d bps",
217 AltosLib.ao_telemetry_rate_values[telemetry_state.telemetry_rate]));
220 setTitle(R.string.title_connected_to);
223 case TelemetryState.CONNECT_CONNECTING:
224 if (telemetry_state.address != null)
225 setTitle(String.format("Connecting to %s...", telemetry_state.address.name));
227 setTitle("Connecting to something...");
229 case TelemetryState.CONNECT_DISCONNECTED:
230 case TelemetryState.CONNECT_NONE:
231 setTitle(R.string.title_not_connected);
239 timer.scheduleAtFixedRate(new TimerTask(){ public void run() {onTimerTick();}}, 1000L, 1000L);
254 void set_switch_time() {
255 switch_time = System.currentTimeMillis();
258 boolean registered_units_listener;
260 void update_state(TelemetryState new_telemetry_state) {
262 if (new_telemetry_state != null)
263 telemetry_state = new_telemetry_state;
265 if (current_serial == 0)
266 current_serial = telemetry_state.latest_serial;
268 if (!registered_units_listener) {
269 registered_units_listener = true;
270 AltosPreferences.register_units_listener(this);
273 serials = telemetry_state.states.keySet().toArray(new Integer[0]);
275 update_title(telemetry_state);
277 AltosState state = null;
280 if (telemetry_state.states.containsKey(current_serial)) {
281 state = telemetry_state.states.get(current_serial);
282 int age = state_age(state);
285 if (switch_time != 0 && (switch_time - state.received_time) > 0)
290 AltosState newest_state = null;
293 for (int serial : telemetry_state.states.keySet()) {
294 AltosState existing = telemetry_state.states.get(serial);
295 int existing_age = state_age(existing);
297 if (newest_state == null || existing_age < newest_age) {
298 newest_state = existing;
299 newest_age = existing_age;
303 if (newest_state != null)
304 state = newest_state;
307 update_ui(telemetry_state, state, telemetry_state.location);
312 boolean same_string(String a, String b) {
325 private int blend_component(int a, int b, double r, int shift, int mask) {
326 return ((int) (((a >> shift) & mask) * r + ((b >> shift) & mask) * (1 - r)) & mask) << shift;
328 private int blend_color(int a, int b, double r) {
329 return (blend_component(a, b, r, 0, 0xff) |
330 blend_component(a, b, r, 8, 0xff) |
331 blend_component(a, b, r, 16, 0xff) |
332 blend_component(a, b, r, 24, 0xff));
335 int state_age(AltosState state) {
336 return (int) ((System.currentTimeMillis() - state.received_time + 500) / 1000);
339 void set_screen_on(int age) {
341 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
343 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
347 if (saved_state != null) {
348 int age = state_age(saved_state);
350 double age_scale = age / 100.0;
355 mAgeView.setTextColor(blend_color(mAgeOldColor, mAgeNewColor, age_scale));
361 text = String.format("%ds", age);
362 else if (age < 60 * 60)
363 text = String.format("%dm", age / 60);
364 else if (age < 60 * 60 * 24)
365 text = String.format("%dh", age / (60 * 60));
367 text = String.format("%dd", age / (24 * 60 * 60));
368 mAgeView.setText(text);
372 void update_ui(TelemetryState telem_state, AltosState state, Location location) {
374 int prev_state = AltosLib.ao_flight_invalid;
376 AltosGreatCircle from_receiver = null;
378 if (saved_state != null)
379 prev_state = saved_state.state;
382 set_screen_on(state_age(state));
384 if (state.state == AltosLib.ao_flight_stateless) {
385 boolean prev_locked = false;
386 boolean locked = false;
388 if(state.gps != null)
389 locked = state.gps.locked;
390 if (saved_state != null && saved_state.gps != null)
391 prev_locked = saved_state.gps.locked;
392 if (prev_locked != locked) {
393 String currentTab = mTabHost.getCurrentTabTag();
395 if (currentTab.equals("pad")) mTabHost.setCurrentTabByTag("descent");
397 if (currentTab.equals("descent")) mTabHost.setCurrentTabByTag("pad");
401 if (prev_state != state.state) {
402 String currentTab = mTabHost.getCurrentTabTag();
403 switch (state.state) {
404 case AltosLib.ao_flight_boost:
405 if (currentTab.equals("pad")) mTabHost.setCurrentTabByTag("ascent");
407 case AltosLib.ao_flight_drogue:
408 if (currentTab.equals("ascent")) mTabHost.setCurrentTabByTag("descent");
410 case AltosLib.ao_flight_landed:
411 if (currentTab.equals("descent")) mTabHost.setCurrentTabByTag("landed");
413 case AltosLib.ao_flight_stateless:
414 if (currentTab.equals("pad")) mTabHost.setCurrentTabByTag("descent");
420 if (location != null && state.gps != null && state.gps.locked) {
422 if (location.hasAltitude())
423 altitude = location.getAltitude();
424 from_receiver = new AltosGreatCircle(location.getLatitude(),
425 location.getLongitude(),
432 if (saved_state == null || !same_string(saved_state.callsign, state.callsign)) {
433 mCallsignView.setText(state.callsign);
435 if (saved_state == null || state.serial != saved_state.serial) {
436 mSerialView.setText(String.format("%d", state.serial));
438 if (saved_state == null || state.flight != saved_state.flight) {
439 if (state.flight == AltosLib.MISSING)
440 mFlightView.setText("");
442 mFlightView.setText(String.format("%d", state.flight));
444 if (saved_state == null || state.state != saved_state.state) {
445 if (state.state == AltosLib.ao_flight_stateless) {
446 mStateLayout.setVisibility(View.GONE);
448 mStateView.setText(state.state_name());
449 mStateLayout.setVisibility(View.VISIBLE);
452 if (saved_state == null || state.rssi != saved_state.rssi) {
453 mRSSIView.setText(String.format("%d", state.rssi));
457 for (AltosDroidTab mTab : mTabs)
458 mTab.update_ui(telem_state, state, from_receiver, location, mTab == mTabsAdapter.currentItem());
460 if (state != null && mAltosVoice != null)
461 mAltosVoice.tell(state, from_receiver);
466 private void onTimerTick() {
468 mMessenger.send(Message.obtain(null, MSG_UPDATE_AGE));
469 } catch (RemoteException e) {
473 static String pos(double p, String pos, String neg) {
475 if (p == AltosLib.MISSING)
481 int deg = (int) Math.floor(p);
482 double min = (p - Math.floor(p)) * 60.0;
483 return String.format("%d°%9.4f\" %s", deg, min, h);
486 static String number(String format, double value) {
487 if (value == AltosLib.MISSING)
489 return String.format(format, value);
492 static String integer(String format, int value) {
493 if (value == AltosLib.MISSING)
495 return String.format(format, value);
498 private View create_tab_view(String label) {
499 LayoutInflater inflater = (LayoutInflater) this.getLayoutInflater();
500 View tab_view = inflater.inflate(R.layout.tab_layout, null);
501 TextView text_view = (TextView) tab_view.findViewById (R.id.tabLabel);
502 text_view.setText(label);
506 public void set_map_source() {
510 public void onCreate(Bundle savedInstanceState) {
511 super.onCreate(savedInstanceState);
512 AltosDebug.init(this);
513 AltosDebug.debug("+++ ON CREATE +++");
515 // Initialise preferences
516 AltosDroidPreferences.init(this);
518 fm = getSupportFragmentManager();
520 // Set up the window layout
521 setContentView(R.layout.altosdroid);
523 // Create the Tabs and ViewPager
524 mTabHost = (TabHost)findViewById(android.R.id.tabhost);
527 mViewPager = (AltosViewPager)findViewById(R.id.pager);
528 mViewPager.setOffscreenPageLimit(4);
530 mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);
532 mTabsAdapter.addTab(mTabHost.newTabSpec("pad").setIndicator(create_tab_view("Pad")), TabPad.class, null);
533 mTabsAdapter.addTab(mTabHost.newTabSpec("ascent").setIndicator(create_tab_view("Ascent")), TabAscent.class, null);
534 mTabsAdapter.addTab(mTabHost.newTabSpec("descent").setIndicator(create_tab_view("Descent")), TabDescent.class, null);
535 mTabsAdapter.addTab(mTabHost.newTabSpec("landed").setIndicator(create_tab_view("Landed")), TabLanded.class, null);
536 mTabsAdapter.addTab(mTabHost.newTabSpec("map").setIndicator(create_tab_view("Map")), TabMap.class, null);
537 mTabsAdapter.addTab(mTabHost.newTabSpec("offmap").setIndicator(create_tab_view("OffMap")), TabMapOffline.class, null);
539 // Display the Version
540 mVersion = (TextView) findViewById(R.id.version);
541 mVersion.setText("Version: " + BuildInfo.version +
542 " Built: " + BuildInfo.builddate + " " + BuildInfo.buildtime + " " + BuildInfo.buildtz +
543 " (" + BuildInfo.branch + "-" + BuildInfo.commitnum + "-" + BuildInfo.commithash + ")");
545 mCallsignView = (TextView) findViewById(R.id.callsign_value);
546 mRSSIView = (TextView) findViewById(R.id.rssi_value);
547 mSerialView = (TextView) findViewById(R.id.serial_value);
548 mFlightView = (TextView) findViewById(R.id.flight_value);
549 mStateLayout = (RelativeLayout) findViewById(R.id.state_container);
550 mStateView = (TextView) findViewById(R.id.state_value);
551 mAgeView = (TextView) findViewById(R.id.age_value);
552 mAgeNewColor = mAgeView.getTextColors().getDefaultColor();
553 mAgeOldColor = getResources().getColor(R.color.old_color);
556 private boolean ensureBluetooth() {
557 // Get local Bluetooth adapter
558 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
560 // If the adapter is null, then Bluetooth is not supported
561 if (mBluetoothAdapter == null) {
562 Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
566 if (!mBluetoothAdapter.isEnabled()) {
567 Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
568 startActivityForResult(enableIntent, AltosDroid.REQUEST_ENABLE_BT);
574 private boolean check_usb() {
575 UsbDevice device = AltosUsb.find_device(this, AltosLib.product_basestation);
577 if (device != null) {
578 Intent i = new Intent(this, AltosDroid.class);
579 PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent("hello world", null, this, AltosDroid.class), 0);
581 if (AltosUsb.request_permission(this, device, pi)) {
584 start_with_usb = true;
588 start_with_usb = false;
593 private void noticeIntent(Intent intent) {
595 /* Ok, this is pretty convenient.
597 * When a USB device is plugged in, and our 'hotplug'
598 * intent registration fires, we get an Intent with
601 * When we start up and see a usb device and request
602 * permission to access it, that queues a
603 * PendingIntent, which has the EXTRA_DEVICE added in,
604 * along with the EXTRA_PERMISSION_GRANTED field as
607 * So, in both cases, we get the device name using the
608 * same call. We check to see if access was granted,
609 * in which case we ignore the device field and do our
610 * usual startup thing.
613 UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
614 boolean granted = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, true);
616 AltosDebug.debug("intent %s device %s granted %s", intent, device, granted);
621 if (device != null) {
622 AltosDebug.debug("intent has usb device " + device.toString());
626 /* 'granted' is only false if this intent came
627 * from the request_permission call and
628 * permission was denied. In which case, we
629 * don't want to loop forever...
632 AltosDebug.debug("check for a USB device at startup");
636 AltosDebug.debug("Starting by looking for bluetooth devices");
637 if (ensureBluetooth())
644 public void onStart() {
646 AltosDebug.debug("++ ON START ++");
648 noticeIntent(getIntent());
650 // Start Telemetry Service
651 String action = start_with_usb ? ACTION_USB : ACTION_BLUETOOTH;
653 startService(new Intent(action, null, AltosDroid.this, TelemetryService.class));
657 if (mAltosVoice == null)
658 mAltosVoice = new AltosVoice(this);
663 public void onNewIntent(Intent intent) {
664 super.onNewIntent(intent);
665 AltosDebug.debug("onNewIntent");
666 noticeIntent(intent);
670 public void onResume() {
672 AltosDebug.debug("+ ON RESUME +");
676 public void onPause() {
678 AltosDebug.debug("- ON PAUSE -");
682 public void onStop() {
684 AltosDebug.debug("-- ON STOP --");
687 if (mAltosVoice != null) {
694 public void onDestroy() {
696 AltosDebug.debug("--- ON DESTROY ---");
698 if (mAltosVoice != null) mAltosVoice.stop();
702 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
703 AltosDebug.debug("onActivityResult " + resultCode);
704 switch (requestCode) {
705 case REQUEST_CONNECT_DEVICE:
706 // When DeviceListActivity returns with a device to connect to
707 if (resultCode == Activity.RESULT_OK) {
711 case REQUEST_ENABLE_BT:
712 // When the request to enable Bluetooth returns
713 if (resultCode == Activity.RESULT_OK) {
714 // Bluetooth is now enabled, so set up a chat session
717 // User did not enable Bluetooth or an error occured
718 AltosDebug.error("BT not enabled");
719 stopService(new Intent(AltosDroid.this, TelemetryService.class));
720 Toast.makeText(this, R.string.bt_not_enabled, Toast.LENGTH_SHORT).show();
724 case REQUEST_MAP_TYPE:
725 if (resultCode == Activity.RESULT_OK)
731 private void connectUsb(UsbDevice device) {
732 if (mService == null)
733 pending_usb_device = device;
735 // Attempt to connect to the device
737 mService.send(Message.obtain(null, TelemetryService.MSG_OPEN_USB, device));
738 AltosDebug.debug("Sent OPEN_USB message");
739 } catch (RemoteException e) {
740 AltosDebug.debug("connect device message failed");
745 private void connectDevice(Intent data) {
746 // Attempt to connect to the device
748 String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
749 String name = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_NAME);
751 AltosDebug.debug("Connecting to " + address + " " + name);
752 DeviceAddress a = new DeviceAddress(address, name);
753 mService.send(Message.obtain(null, TelemetryService.MSG_CONNECT, a));
754 AltosDebug.debug("Sent connecting message");
755 } catch (RemoteException e) {
756 AltosDebug.debug("connect device message failed");
760 private void disconnectDevice() {
762 mService.send(Message.obtain(null, TelemetryService.MSG_DISCONNECT, null));
763 } catch (RemoteException e) {
767 private void set_map_type(Intent data) {
768 int type = data.getIntExtra(MapTypeActivity.EXTRA_MAP_TYPE, -1);
770 AltosDebug.debug("intent set_map_type %d\n", type);
773 for (AltosDroidTab mTab : mTabs)
774 mTab.set_map_type(map_type);
779 public boolean onCreateOptionsMenu(Menu menu) {
780 MenuInflater inflater = getMenuInflater();
781 inflater.inflate(R.menu.option_menu, menu);
785 void setFrequency(double freq) {
787 mService.send(Message.obtain(null, TelemetryService.MSG_SETFREQUENCY, freq));
789 } catch (RemoteException e) {
793 void setFrequency(String freq) {
795 setFrequency (AltosParse.parse_double_net(freq.substring(11, 17)));
796 } catch (ParseException e) {
800 void setBaud(int baud) {
802 mService.send(Message.obtain(null, TelemetryService.MSG_SETBAUD, baud));
804 } catch (RemoteException e) {
808 void setBaud(String baud) {
810 int value = Integer.parseInt(baud);
811 int rate = AltosLib.ao_telemetry_rate_38400;
814 rate = AltosLib.ao_telemetry_rate_2400;
817 rate = AltosLib.ao_telemetry_rate_9600;
820 rate = AltosLib.ao_telemetry_rate_38400;
824 } catch (NumberFormatException e) {
828 void select_tracker(int serial) {
830 for (i = 0; i < serials.length; i++)
831 if (serials[i] == serial)
833 if (i == serials.length)
836 current_serial = serial;
840 void delete_track(int serial) {
842 mService.send(Message.obtain(null, TelemetryService.MSG_DELETE_SERIAL, (Integer) serial));
843 } catch (Exception ex) {
848 public boolean onOptionsItemSelected(MenuItem item) {
849 Intent serverIntent = null;
850 switch (item.getItemId()) {
851 case R.id.connect_scan:
852 if (ensureBluetooth()) {
853 // Launch the DeviceListActivity to see devices and do scan
854 serverIntent = new Intent(this, DeviceListActivity.class);
855 startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
858 case R.id.disconnect:
859 /* Disconnect the device
864 AltosDebug.debug("R.id.quit");
868 case R.id.select_freq:
869 // Set the TBT radio frequency
871 final String[] frequencies = {
872 "Channel 0 (434.550MHz)",
873 "Channel 1 (434.650MHz)",
874 "Channel 2 (434.750MHz)",
875 "Channel 3 (434.850MHz)",
876 "Channel 4 (434.950MHz)",
877 "Channel 5 (435.050MHz)",
878 "Channel 6 (435.150MHz)",
879 "Channel 7 (435.250MHz)",
880 "Channel 8 (435.350MHz)",
881 "Channel 9 (435.450MHz)"
884 AlertDialog.Builder builder_freq = new AlertDialog.Builder(this);
885 builder_freq.setTitle("Pick a frequency");
886 builder_freq.setItems(frequencies,
887 new DialogInterface.OnClickListener() {
888 public void onClick(DialogInterface dialog, int item) {
889 setFrequency(frequencies[item]);
892 AlertDialog alert_freq = builder_freq.create();
895 case R.id.select_rate:
896 // Set the TBT baud rate
898 final String[] rates = {
904 AlertDialog.Builder builder_rate = new AlertDialog.Builder(this);
905 builder_rate.setTitle("Pick a baud rate");
906 builder_rate.setItems(rates,
907 new DialogInterface.OnClickListener() {
908 public void onClick(DialogInterface dialog, int item) {
909 setBaud(rates[item]);
912 AlertDialog alert_rate = builder_rate.create();
915 case R.id.change_units:
916 boolean imperial = AltosPreferences.imperial_units();
917 AltosPreferences.set_imperial_units(!imperial);
919 case R.id.preload_maps:
920 serverIntent = new Intent(this, PreloadMapActivity.class);
921 startActivityForResult(serverIntent, REQUEST_PRELOAD_MAPS);
924 serverIntent = new Intent(this, MapTypeActivity.class);
925 startActivityForResult(serverIntent, REQUEST_MAP_TYPE);
927 case R.id.map_source:
928 int source = AltosDroidPreferences.map_source();
929 int new_source = source == AltosDroidPreferences.MAP_SOURCE_ONLINE ? AltosDroidPreferences.MAP_SOURCE_OFFLINE : AltosDroidPreferences.MAP_SOURCE_ONLINE;
930 AltosDroidPreferences.set_map_source(new_source);
933 case R.id.select_tracker:
934 if (serials != null) {
935 String[] trackers = new String[serials.length];
936 for (int i = 0; i < serials.length; i++)
937 trackers[i] = String.format("%d", serials[i]);
938 AlertDialog.Builder builder_serial = new AlertDialog.Builder(this);
939 builder_serial.setTitle("Select a tracker");
940 builder_serial.setItems(trackers,
941 new DialogInterface.OnClickListener() {
942 public void onClick(DialogInterface dialog, int item) {
943 select_tracker(serials[item]);
946 AlertDialog alert_serial = builder_serial.create();
951 case R.id.delete_track:
952 if (serials != null) {
953 String[] trackers = new String[serials.length];
954 for (int i = 0; i < serials.length; i++)
955 trackers[i] = String.format("%d", serials[i]);
956 AlertDialog.Builder builder_serial = new AlertDialog.Builder(this);
957 builder_serial.setTitle("Delete a track");
958 builder_serial.setItems(trackers,
959 new DialogInterface.OnClickListener() {
960 public void onClick(DialogInterface dialog, int item) {
961 delete_track(serials[item]);
964 AlertDialog alert_serial = builder_serial.create();