+ // Text to Speech
+ private AltosVoice mAltosVoice = null;
+
+ // The Handler that gets information back from the Telemetry Service
+ static class IncomingHandler extends Handler {
+ private final WeakReference<AltosDroid> mAltosDroid;
+ IncomingHandler(AltosDroid ad) { mAltosDroid = new WeakReference<AltosDroid>(ad); }
+
+ @Override
+ public void handleMessage(Message msg) {
+ AltosDroid ad = mAltosDroid.get();
+ switch (msg.what) {
+ case MSG_STATE_CHANGE:
+ if(D) Log.d(TAG, "MSG_STATE_CHANGE: " + msg.arg1);
+ switch (msg.arg1) {
+ case TelemetryService.STATE_CONNECTED:
+ ad.mConfigData = (AltosConfigData) msg.obj;
+ String str = String.format(" %s S/N: %d", ad.mConfigData.product, ad.mConfigData.serial);
+ ad.mTitle.setText(R.string.title_connected_to);
+ ad.mTitle.append(str);
+ Toast.makeText(ad.getApplicationContext(), "Connected to " + str, Toast.LENGTH_SHORT).show();
+ ad.mAltosVoice.speak("Connected");
+ break;
+ case TelemetryService.STATE_CONNECTING:
+ ad.mTitle.setText(R.string.title_connecting);
+ break;
+ case TelemetryService.STATE_READY:
+ case TelemetryService.STATE_NONE:
+ ad.mConfigData = null;
+ ad.mTitle.setText(R.string.title_not_connected);
+ break;
+ }
+ break;
+ case MSG_TELEMETRY:
+ ad.update_ui((AltosState) msg.obj);
+ break;
+ }
+ }
+ };
+
+
+ private ServiceConnection mConnection = new ServiceConnection() {
+ public void onServiceConnected(ComponentName className, IBinder service) {
+ mService = new Messenger(service);
+ try {
+ Message msg = Message.obtain(null, TelemetryService.MSG_REGISTER_CLIENT);
+ msg.replyTo = mMessenger;
+ mService.send(msg);
+ } catch (RemoteException e) {
+ // In this case the service has crashed before we could even do anything with it
+ }
+ }
+
+ public void onServiceDisconnected(ComponentName className) {
+ // This is called when the connection with the service has been unexpectedly disconnected - process crashed.
+ mService = null;
+ }
+ };
+
+ void doBindService() {
+ bindService(new Intent(this, TelemetryService.class), mConnection, Context.BIND_AUTO_CREATE);
+ mIsBound = true;
+ }
+
+ void doUnbindService() {
+ if (mIsBound) {
+ // If we have received the service, and hence registered with it, then now is the time to unregister.
+ if (mService != null) {
+ try {
+ Message msg = Message.obtain(null, TelemetryService.MSG_UNREGISTER_CLIENT);
+ msg.replyTo = mMessenger;
+ mService.send(msg);
+ } catch (RemoteException e) {
+ // There is nothing special we need to do if the service has crashed.
+ }
+ }
+ // Detach our existing connection.
+ unbindService(mConnection);
+ mIsBound = false;
+ }
+ }
+
+ void update_ui(AltosState state) {
+ mCallsignView.setText(state.data.callsign);
+ mRSSIView.setText(String.format("%d", state.data.rssi));
+ mSerialView.setText(String.format("%d", state.data.serial));
+ mFlightView.setText(String.format("%d", state.data.flight));
+ mStateView.setText(state.data.state());
+ mSpeedView.setText(String.format("%6.0f m/s", state.speed()));
+ mAccelView.setText(String.format("%6.0f m/s²", state.acceleration));
+ mRangeView.setText(String.format("%6.0f m", state.range));
+ mHeightView.setText(String.format("%6.0f m", state.height));
+ mElevationView.setText(String.format("%3.0f°", state.elevation));
+ if (state.from_pad != null)
+ mBearingView.setText(String.format("%3.0f°", state.from_pad.bearing));
+ mLatitudeView.setText(pos(state.gps.lat, "N", "S"));
+ mLongitudeView.setText(pos(state.gps.lon, "W", "E"));
+
+ mAltosVoice.tell(state);
+ }
+
+ static String pos(double p, String pos, String neg) {
+ String h = pos;
+ if (p < 0) {
+ h = neg;
+ p = -p;
+ }
+ int deg = (int) Math.floor(p);
+ double min = (p - Math.floor(p)) * 60.0;
+ return String.format("%d° %9.6f\" %s", deg, min, h);
+ }
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {