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.util.Log;
46 import android.view.Menu;
47 import android.view.MenuInflater;
48 import android.view.MenuItem;
49 import android.view.Window;
50 import android.view.View;
51 import android.view.LayoutInflater;
52 import android.widget.TabHost;
53 import android.widget.TextView;
54 import android.widget.RelativeLayout;
55 import android.widget.Toast;
56 import android.app.AlertDialog;
57 import android.location.Location;
58 import android.hardware.usb.*;
60 import org.altusmetrum.altoslib_7.*;
62 public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
64 static final String TAG = "AltosDroid";
65 static final boolean D = true;
67 // Actions sent to the telemetry server at startup time
69 public static final String ACTION_BLUETOOTH = "org.altusmetrum.AltosDroid.BLUETOOTH";
70 public static final String ACTION_USB = "org.altusmetrum.AltosDroid.USB";
72 // Message types received by our Handler
74 public static final int MSG_STATE = 1;
75 public static final int MSG_UPDATE_AGE = 2;
77 // Intent request codes
78 public static final int REQUEST_CONNECT_DEVICE = 1;
79 public static final int REQUEST_ENABLE_BT = 2;
81 public static FragmentManager fm;
83 private BluetoothAdapter mBluetoothAdapter = null;
86 private TextView mTitle;
88 // Flight state values
89 private TextView mCallsignView;
90 private TextView mRSSIView;
91 private TextView mSerialView;
92 private TextView mFlightView;
93 private RelativeLayout mStateLayout;
94 private TextView mStateView;
95 private TextView mAgeView;
97 // field to display the version at the bottom of the screen
98 private TextView mVersion;
100 private double frequency;
101 private int telemetry_rate;
105 AltosViewPager mViewPager;
106 TabsAdapter mTabsAdapter;
107 ArrayList<AltosDroidTab> mTabs = new ArrayList<AltosDroidTab>();
110 // Timer and Saved flight state for Age calculation
112 AltosState saved_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(D) Log.d(TAG, "MSG_STATE");
137 TelemetryState telemetry_state = (TelemetryState) msg.obj;
138 if (telemetry_state == null) {
139 Log.d(TAG, "telemetry_state null!");
143 ad.update_state(telemetry_state);
146 if(D) Log.d(TAG, "MSG_UPDATE_AGE");
154 private ServiceConnection mConnection = new ServiceConnection() {
155 public void onServiceConnected(ComponentName className, IBinder service) {
156 mService = new Messenger(service);
158 Message msg = Message.obtain(null, TelemetryService.MSG_REGISTER_CLIENT);
159 msg.replyTo = mMessenger;
161 } catch (RemoteException e) {
162 // In this case the service has crashed before we could even do anything with it
164 if (pending_usb_device != null) {
166 mService.send(Message.obtain(null, TelemetryService.MSG_OPEN_USB, pending_usb_device));
167 pending_usb_device = null;
168 } catch (RemoteException e) {
173 public void onServiceDisconnected(ComponentName className) {
174 // This is called when the connection with the service has been unexpectedly disconnected - process crashed.
179 void doBindService() {
180 bindService(new Intent(this, TelemetryService.class), mConnection, Context.BIND_AUTO_CREATE);
184 void doUnbindService() {
186 // If we have received the service, and hence registered with it, then now is the time to unregister.
187 if (mService != null) {
189 Message msg = Message.obtain(null, TelemetryService.MSG_UNREGISTER_CLIENT);
190 msg.replyTo = mMessenger;
192 } catch (RemoteException e) {
193 // There is nothing special we need to do if the service has crashed.
196 // Detach our existing connection.
197 unbindService(mConnection);
202 public void registerTab(AltosDroidTab mTab) {
206 public void unregisterTab(AltosDroidTab mTab) {
210 public void units_changed(boolean imperial_units) {
211 for (AltosDroidTab mTab : mTabs)
212 mTab.units_changed(imperial_units);
215 void update_title(TelemetryState telemetry_state) {
216 switch (telemetry_state.connect) {
217 case TelemetryState.CONNECT_CONNECTED:
218 if (telemetry_state.config != null) {
219 String str = String.format("S/N %d %6.3f MHz", telemetry_state.config.serial,
220 telemetry_state.frequency);
221 if (telemetry_state.telemetry_rate != AltosLib.ao_telemetry_rate_38400)
222 str = str.concat(String.format(" %d bps",
223 AltosLib.ao_telemetry_rate_values[telemetry_state.telemetry_rate]));
226 mTitle.setText(R.string.title_connected_to);
229 case TelemetryState.CONNECT_CONNECTING:
230 if (telemetry_state.address != null)
231 mTitle.setText(String.format("Connecting to %s...", telemetry_state.address.name));
233 mTitle.setText("Connecting to something...");
235 case TelemetryState.CONNECT_DISCONNECTED:
236 case TelemetryState.CONNECT_NONE:
237 mTitle.setText(R.string.title_not_connected);
245 timer.scheduleAtFixedRate(new TimerTask(){ public void run() {onTimerTick();}}, 1000L, 1000L);
257 boolean registered_units_listener;
259 void update_state(TelemetryState telemetry_state) {
261 if (!registered_units_listener) {
262 registered_units_listener = true;
263 AltosPreferences.register_units_listener(this);
266 update_title(telemetry_state);
267 update_ui(telemetry_state.state, telemetry_state.location);
268 if (telemetry_state.connect == TelemetryState.CONNECT_CONNECTED)
272 boolean same_string(String a, String b) {
285 if (saved_state != null)
286 mAgeView.setText(String.format("%d", (System.currentTimeMillis() - saved_state.received_time + 500) / 1000));
289 void update_ui(AltosState state, Location location) {
291 int prev_state = AltosLib.ao_flight_invalid;
293 AltosGreatCircle from_receiver = null;
295 if (saved_state != null)
296 prev_state = saved_state.state;
299 if (state.state == AltosLib.ao_flight_stateless) {
300 boolean prev_locked = false;
301 boolean locked = false;
303 if(state.gps != null)
304 locked = state.gps.locked;
305 if (saved_state != null && saved_state.gps != null)
306 prev_locked = saved_state.gps.locked;
307 if (prev_locked != locked) {
308 String currentTab = mTabHost.getCurrentTabTag();
310 if (currentTab.equals("pad")) mTabHost.setCurrentTabByTag("descent");
312 if (currentTab.equals("descent")) mTabHost.setCurrentTabByTag("pad");
316 if (prev_state != state.state) {
317 String currentTab = mTabHost.getCurrentTabTag();
318 switch (state.state) {
319 case AltosLib.ao_flight_boost:
320 if (currentTab.equals("pad")) mTabHost.setCurrentTabByTag("ascent");
322 case AltosLib.ao_flight_drogue:
323 if (currentTab.equals("ascent")) mTabHost.setCurrentTabByTag("descent");
325 case AltosLib.ao_flight_landed:
326 if (currentTab.equals("descent")) mTabHost.setCurrentTabByTag("landed");
328 case AltosLib.ao_flight_stateless:
329 if (currentTab.equals("pad")) mTabHost.setCurrentTabByTag("descent");
335 if (location != null && state.gps != null && state.gps.locked) {
337 if (location.hasAltitude())
338 altitude = location.getAltitude();
339 from_receiver = new AltosGreatCircle(location.getLatitude(),
340 location.getLongitude(),
347 if (saved_state == null || !same_string(saved_state.callsign, state.callsign)) {
348 mCallsignView.setText(state.callsign);
350 if (saved_state == null || state.serial != saved_state.serial) {
351 mSerialView.setText(String.format("%d", state.serial));
353 if (saved_state == null || state.flight != saved_state.flight) {
354 if (state.flight == AltosLib.MISSING)
355 mFlightView.setText("");
357 mFlightView.setText(String.format("%d", state.flight));
359 if (saved_state == null || state.state != saved_state.state) {
360 if (state.state == AltosLib.ao_flight_stateless) {
361 mStateLayout.setVisibility(View.GONE);
363 mStateView.setText(state.state_name());
364 mStateLayout.setVisibility(View.VISIBLE);
367 if (saved_state == null || state.rssi != saved_state.rssi) {
368 mRSSIView.setText(String.format("%d", state.rssi));
372 for (AltosDroidTab mTab : mTabs)
373 mTab.update_ui(state, from_receiver, location, mTab == mTabsAdapter.currentItem());
375 if (state != null && mAltosVoice != null)
376 mAltosVoice.tell(state, from_receiver);
381 private void onTimerTick() {
383 mMessenger.send(Message.obtain(null, MSG_UPDATE_AGE));
384 } catch (RemoteException e) {
388 static String pos(double p, String pos, String neg) {
390 if (p == AltosLib.MISSING)
396 int deg = (int) Math.floor(p);
397 double min = (p - Math.floor(p)) * 60.0;
398 return String.format("%d°%9.4f\" %s", deg, min, h);
401 static String number(String format, double value) {
402 if (value == AltosLib.MISSING)
404 return String.format(format, value);
407 static String integer(String format, int value) {
408 if (value == AltosLib.MISSING)
410 return String.format(format, value);
413 private View create_tab_view(String label) {
414 LayoutInflater inflater = (LayoutInflater) this.getLayoutInflater();
415 View tab_view = inflater.inflate(R.layout.tab_layout, null);
416 TextView text_view = (TextView) tab_view.findViewById (R.id.tabLabel);
417 text_view.setText(label);
422 public void onCreate(Bundle savedInstanceState) {
423 super.onCreate(savedInstanceState);
424 if(D) Log.e(TAG, "+++ ON CREATE +++");
426 fm = getSupportFragmentManager();
428 // Set up the window layout
429 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
430 setContentView(R.layout.altosdroid);
431 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
433 // Create the Tabs and ViewPager
434 mTabHost = (TabHost)findViewById(android.R.id.tabhost);
437 mViewPager = (AltosViewPager)findViewById(R.id.pager);
438 mViewPager.setOffscreenPageLimit(4);
440 mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);
442 mTabsAdapter.addTab(mTabHost.newTabSpec("pad").setIndicator(create_tab_view("Pad")), TabPad.class, null);
443 mTabsAdapter.addTab(mTabHost.newTabSpec("ascent").setIndicator(create_tab_view("Ascent")), TabAscent.class, null);
444 mTabsAdapter.addTab(mTabHost.newTabSpec("descent").setIndicator(create_tab_view("Descent")), TabDescent.class, null);
445 mTabsAdapter.addTab(mTabHost.newTabSpec("landed").setIndicator(create_tab_view("Landed")), TabLanded.class, null);
446 mTabsAdapter.addTab(mTabHost.newTabSpec("map").setIndicator(create_tab_view("Map")), TabMap.class, null);
447 mTabsAdapter.addTab(mTabHost.newTabSpec("offmap").setIndicator(create_tab_view("OffMap")), TabMapOffline.class, null);
449 // Set up the custom title
450 mTitle = (TextView) findViewById(R.id.title_left_text);
451 mTitle.setText(R.string.app_name);
452 mTitle = (TextView) findViewById(R.id.title_right_text);
454 // Display the Version
455 mVersion = (TextView) findViewById(R.id.version);
456 mVersion.setText("Version: " + BuildInfo.version +
457 " Built: " + BuildInfo.builddate + " " + BuildInfo.buildtime + " " + BuildInfo.buildtz +
458 " (" + BuildInfo.branch + "-" + BuildInfo.commitnum + "-" + BuildInfo.commithash + ")");
460 mCallsignView = (TextView) findViewById(R.id.callsign_value);
461 mRSSIView = (TextView) findViewById(R.id.rssi_value);
462 mSerialView = (TextView) findViewById(R.id.serial_value);
463 mFlightView = (TextView) findViewById(R.id.flight_value);
464 mStateLayout = (RelativeLayout) findViewById(R.id.state_container);
465 mStateView = (TextView) findViewById(R.id.state_value);
466 mAgeView = (TextView) findViewById(R.id.age_value);
469 private boolean ensureBluetooth() {
470 // Get local Bluetooth adapter
471 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
473 // If the adapter is null, then Bluetooth is not supported
474 if (mBluetoothAdapter == null) {
475 Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
479 if (!mBluetoothAdapter.isEnabled()) {
480 Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
481 startActivityForResult(enableIntent, AltosDroid.REQUEST_ENABLE_BT);
487 private boolean check_usb() {
488 UsbDevice device = AltosUsb.find_device(this, AltosLib.product_basestation);
490 if (device != null) {
491 Intent i = new Intent(this, AltosDroid.class);
492 PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent("hello world", null, this, AltosDroid.class), 0);
494 if (AltosUsb.request_permission(this, device, pi)) {
497 start_with_usb = true;
501 start_with_usb = false;
506 private void noticeIntent(Intent intent) {
508 /* Ok, this is pretty convenient.
510 * When a USB device is plugged in, and our 'hotplug'
511 * intent registration fires, we get an Intent with
514 * When we start up and see a usb device and request
515 * permission to access it, that queues a
516 * PendingIntent, which has the EXTRA_DEVICE added in,
517 * along with the EXTRA_PERMISSION_GRANTED field as
520 * So, in both cases, we get the device name using the
521 * same call. We check to see if access was granted,
522 * in which case we ignore the device field and do our
523 * usual startup thing.
526 UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
527 boolean granted = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, true);
529 if (D) Log.e(TAG, "intent " + intent + " device " + device + " granted " + granted);
534 if (device != null) {
535 if (D) Log.d(TAG, "intent has usb device " + device.toString());
539 /* 'granted' is only false if this intent came
540 * from the request_permission call and
541 * permission was denied. In which case, we
542 * don't want to loop forever...
545 if (D) Log.d(TAG, "check for a USB device at startup");
549 if (D) Log.d(TAG, "Starting by looking for bluetooth devices");
550 if (ensureBluetooth())
557 public void onStart() {
559 if(D) Log.e(TAG, "++ ON START ++");
561 noticeIntent(getIntent());
563 // Start Telemetry Service
564 String action = start_with_usb ? ACTION_USB : ACTION_BLUETOOTH;
566 startService(new Intent(action, null, AltosDroid.this, TelemetryService.class));
570 if (mAltosVoice == null)
571 mAltosVoice = new AltosVoice(this);
576 public void onNewIntent(Intent intent) {
577 super.onNewIntent(intent);
578 if(D) Log.d(TAG, "onNewIntent");
579 noticeIntent(intent);
583 public void onResume() {
585 if(D) Log.e(TAG, "+ ON RESUME +");
589 public void onPause() {
591 if(D) Log.e(TAG, "- ON PAUSE -");
595 public void onStop() {
597 if(D) Log.e(TAG, "-- ON STOP --");
600 if (mAltosVoice != null) {
607 public void onDestroy() {
609 if(D) Log.e(TAG, "--- ON DESTROY ---");
611 if (mAltosVoice != null) mAltosVoice.stop();
615 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
616 if(D) Log.d(TAG, "onActivityResult " + resultCode);
617 switch (requestCode) {
618 case REQUEST_CONNECT_DEVICE:
619 // When DeviceListActivity returns with a device to connect to
620 if (resultCode == Activity.RESULT_OK) {
624 case REQUEST_ENABLE_BT:
625 // When the request to enable Bluetooth returns
626 if (resultCode == Activity.RESULT_OK) {
627 // Bluetooth is now enabled, so set up a chat session
630 // User did not enable Bluetooth or an error occured
631 Log.e(TAG, "BT not enabled");
632 stopService(new Intent(AltosDroid.this, TelemetryService.class));
633 Toast.makeText(this, R.string.bt_not_enabled, Toast.LENGTH_SHORT).show();
640 private void connectUsb(UsbDevice device) {
641 if (mService == null)
642 pending_usb_device = device;
644 // Attempt to connect to the device
646 mService.send(Message.obtain(null, TelemetryService.MSG_OPEN_USB, device));
647 if (D) Log.d(TAG, "Sent OPEN_USB message");
648 } catch (RemoteException e) {
649 if (D) Log.e(TAG, "connect device message failed");
654 private void connectDevice(Intent data) {
655 // Attempt to connect to the device
657 String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
658 String name = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_NAME);
660 if (D) Log.d(TAG, "Connecting to " + address + " " + name);
661 DeviceAddress a = new DeviceAddress(address, name);
662 mService.send(Message.obtain(null, TelemetryService.MSG_CONNECT, a));
663 if (D) Log.d(TAG, "Sent connecting message");
664 } catch (RemoteException e) {
665 if (D) Log.e(TAG, "connect device message failed");
669 private void disconnectDevice() {
671 mService.send(Message.obtain(null, TelemetryService.MSG_DISCONNECT, null));
672 } catch (RemoteException e) {
677 public boolean onCreateOptionsMenu(Menu menu) {
678 MenuInflater inflater = getMenuInflater();
679 inflater.inflate(R.menu.option_menu, menu);
683 void setFrequency(double freq) {
685 mService.send(Message.obtain(null, TelemetryService.MSG_SETFREQUENCY, freq));
686 } catch (RemoteException e) {
690 void setFrequency(String freq) {
692 setFrequency (AltosParse.parse_double_net(freq.substring(11, 17)));
693 } catch (ParseException e) {
697 void setBaud(int baud) {
699 mService.send(Message.obtain(null, TelemetryService.MSG_SETBAUD, baud));
700 } catch (RemoteException e) {
704 void setBaud(String baud) {
706 int value = Integer.parseInt(baud);
707 int rate = AltosLib.ao_telemetry_rate_38400;
710 rate = AltosLib.ao_telemetry_rate_2400;
713 rate = AltosLib.ao_telemetry_rate_9600;
716 rate = AltosLib.ao_telemetry_rate_38400;
720 } catch (NumberFormatException e) {
725 public boolean onOptionsItemSelected(MenuItem item) {
726 Intent serverIntent = null;
727 switch (item.getItemId()) {
728 case R.id.connect_scan:
729 if (ensureBluetooth()) {
730 // Launch the DeviceListActivity to see devices and do scan
731 serverIntent = new Intent(this, DeviceListActivity.class);
732 startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
735 case R.id.disconnect:
736 /* Disconnect the device
741 Log.d(TAG, "R.id.quit");
745 case R.id.select_freq:
746 // Set the TBT radio frequency
748 final String[] frequencies = {
749 "Channel 0 (434.550MHz)",
750 "Channel 1 (434.650MHz)",
751 "Channel 2 (434.750MHz)",
752 "Channel 3 (434.850MHz)",
753 "Channel 4 (434.950MHz)",
754 "Channel 5 (435.050MHz)",
755 "Channel 6 (435.150MHz)",
756 "Channel 7 (435.250MHz)",
757 "Channel 8 (435.350MHz)",
758 "Channel 9 (435.450MHz)"
761 AlertDialog.Builder builder_freq = new AlertDialog.Builder(this);
762 builder_freq.setTitle("Pick a frequency");
763 builder_freq.setItems(frequencies,
764 new DialogInterface.OnClickListener() {
765 public void onClick(DialogInterface dialog, int item) {
766 setFrequency(frequencies[item]);
769 AlertDialog alert_freq = builder_freq.create();
772 case R.id.select_rate:
773 // Set the TBT baud rate
775 final String[] rates = {
781 AlertDialog.Builder builder_rate = new AlertDialog.Builder(this);
782 builder_rate.setTitle("Pick a baud rate");
783 builder_rate.setItems(rates,
784 new DialogInterface.OnClickListener() {
785 public void onClick(DialogInterface dialog, int item) {
786 setBaud(rates[item]);
789 AlertDialog alert_rate = builder_rate.create();
792 case R.id.change_units:
793 boolean imperial = AltosPreferences.imperial_units();
794 AltosPreferences.set_imperial_units(!imperial);