+ // Text to Speech
+ private TextToSpeech tts = null;
+ private boolean tts_enabled = false;
+
+ // 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();
+ //TEST!
+ ad.mSerialView.setText(Dumper.dump(ad.mConfigData));
+ 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);
+ ad.mSerialView.setText("");
+ break;
+ }
+ break;
+ case MSG_TELEMETRY:
+ // TEST!
+ ad.mSerialView.setText(Dumper.dump(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;
+ }
+ }
+
+