2 * Copyright © 2012 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;
22 import android.app.Activity;
23 import android.bluetooth.BluetoothAdapter;
24 import android.bluetooth.BluetoothDevice;
25 import android.content.Intent;
26 import android.content.Context;
27 import android.content.ComponentName;
28 import android.content.ServiceConnection;
29 import android.os.IBinder;
30 import android.os.Bundle;
31 import android.os.Handler;
32 import android.os.Message;
33 import android.os.Messenger;
34 import android.os.RemoteException;
35 import android.speech.tts.TextToSpeech;
36 import android.speech.tts.TextToSpeech.OnInitListener;
37 import android.text.method.ScrollingMovementMethod;
38 import android.util.Log;
39 import android.view.Menu;
40 import android.view.MenuInflater;
41 import android.view.MenuItem;
42 import android.view.Window;
43 import android.widget.TextView;
44 import android.widget.Toast;
46 import org.altusmetrum.AltosLib.*;
49 * This is the main Activity that displays the current chat session.
51 public class AltosDroid extends Activity {
53 private static final String TAG = "AltosDroid";
54 private static final boolean D = true;
56 // Message types received by our Handler
57 public static final int MSG_STATE_CHANGE = 1;
58 public static final int MSG_TELEMETRY = 2;
60 // Intent request codes
61 private static final int REQUEST_CONNECT_DEVICE = 1;
62 private static final int REQUEST_ENABLE_BT = 2;
65 private TextView mTitle;
66 private TextView mSerialView;
69 private boolean mIsBound = false;
70 private Messenger mService = null;
71 final Messenger mMessenger = new Messenger(new IncomingHandler(this));
74 private AltosConfigData mConfigData = null;
75 // Local Bluetooth adapter
76 private BluetoothAdapter mBluetoothAdapter = null;
79 private TextToSpeech tts = null;
80 private boolean tts_enabled = false;
82 // The Handler that gets information back from the Telemetry Service
83 static class IncomingHandler extends Handler {
84 private final WeakReference<AltosDroid> mAltosDroid;
85 IncomingHandler(AltosDroid ad) { mAltosDroid = new WeakReference<AltosDroid>(ad); }
88 public void handleMessage(Message msg) {
89 AltosDroid ad = mAltosDroid.get();
91 case MSG_STATE_CHANGE:
92 if(D) Log.d(TAG, "MSG_STATE_CHANGE: " + msg.arg1);
94 case TelemetryService.STATE_CONNECTED:
95 ad.mConfigData = (AltosConfigData) msg.obj;
96 String str = String.format(" %s S/N: %d", ad.mConfigData.product, ad.mConfigData.serial);
97 ad.mTitle.setText(R.string.title_connected_to);
98 ad.mTitle.append(str);
99 Toast.makeText(ad.getApplicationContext(), "Connected to " + str, Toast.LENGTH_SHORT).show();
101 case TelemetryService.STATE_CONNECTING:
102 ad.mTitle.setText(R.string.title_connecting);
104 case TelemetryService.STATE_READY:
105 case TelemetryService.STATE_NONE:
106 ad.mConfigData = null;
107 ad.mTitle.setText(R.string.title_not_connected);
108 ad.mSerialView.setText("");
113 //byte[] buf = (byte[]) msg.obj;
114 // construct a string from the buffer
115 //String telem = new String(buf);
116 //ad.mSerialView.append(telem);
123 private ServiceConnection mConnection = new ServiceConnection() {
124 public void onServiceConnected(ComponentName className, IBinder service) {
125 mService = new Messenger(service);
127 Message msg = Message.obtain(null, TelemetryService.MSG_REGISTER_CLIENT);
128 msg.replyTo = mMessenger;
130 } catch (RemoteException e) {
131 // In this case the service has crashed before we could even do anything with it
135 public void onServiceDisconnected(ComponentName className) {
136 // This is called when the connection with the service has been unexpectedly disconnected - process crashed.
142 void doBindService() {
143 bindService(new Intent(this, TelemetryService.class), mConnection, Context.BIND_AUTO_CREATE);
147 void doUnbindService() {
149 // If we have received the service, and hence registered with it, then now is the time to unregister.
150 if (mService != null) {
152 Message msg = Message.obtain(null, TelemetryService.MSG_UNREGISTER_CLIENT);
153 msg.replyTo = mMessenger;
155 } catch (RemoteException e) {
156 // There is nothing special we need to do if the service has crashed.
159 // Detach our existing connection.
160 unbindService(mConnection);
167 public void onCreate(Bundle savedInstanceState) {
168 super.onCreate(savedInstanceState);
169 if(D) Log.e(TAG, "+++ ON CREATE +++");
171 // Set up the window layout
172 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
173 setContentView(R.layout.main);
174 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
176 // Set up the custom title
177 mTitle = (TextView) findViewById(R.id.title_left_text);
178 mTitle.setText(R.string.app_name);
179 mTitle = (TextView) findViewById(R.id.title_right_text);
181 // Set up the temporary Text View
182 mSerialView = (TextView) findViewById(R.id.in);
183 mSerialView.setMovementMethod(new ScrollingMovementMethod());
184 mSerialView.setClickable(false);
185 mSerialView.setLongClickable(false);
187 // Get local Bluetooth adapter
188 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
190 // If the adapter is null, then Bluetooth is not supported
191 if (mBluetoothAdapter == null) {
192 Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
197 // Enable Text to Speech
198 tts = new TextToSpeech(this, new OnInitListener() {
199 public void onInit(int status) {
200 if (status == TextToSpeech.SUCCESS) tts_enabled = true;
201 if (tts_enabled) tts.speak("AltosDroid ready", TextToSpeech.QUEUE_ADD, null );
208 public void onStart() {
210 if(D) Log.e(TAG, "++ ON START ++");
212 if (!mBluetoothAdapter.isEnabled()) {
213 Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
214 startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
217 // Start Telemetry Service
218 startService(new Intent(AltosDroid.this, TelemetryService.class));
224 public synchronized void onResume() {
226 if(D) Log.e(TAG, "+ ON RESUME +");
230 public synchronized void onPause() {
232 if(D) Log.e(TAG, "- ON PAUSE -");
236 public void onStop() {
238 if(D) Log.e(TAG, "-- ON STOP --");
244 public void onDestroy() {
246 if(D) Log.e(TAG, "--- ON DESTROY ---");
248 if (tts != null) tts.shutdown();
254 public void onActivityResult(int requestCode, int resultCode, Intent data) {
255 if(D) Log.d(TAG, "onActivityResult " + resultCode);
256 switch (requestCode) {
257 case REQUEST_CONNECT_DEVICE:
258 // When DeviceListActivity returns with a device to connect to
259 if (resultCode == Activity.RESULT_OK) {
263 case REQUEST_ENABLE_BT:
264 // When the request to enable Bluetooth returns
265 if (resultCode == Activity.RESULT_OK) {
266 // Bluetooth is now enabled, so set up a chat session
269 // User did not enable Bluetooth or an error occured
270 Log.e(TAG, "BT not enabled");
271 stopService(new Intent(AltosDroid.this, TelemetryService.class));
272 Toast.makeText(this, R.string.bt_not_enabled, Toast.LENGTH_SHORT).show();
279 private void connectDevice(Intent data) {
280 // Get the device MAC address
281 String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
282 // Get the BLuetoothDevice object
283 BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
284 // Attempt to connect to the device
286 if (D) Log.d(TAG, "Connecting to " + device.getName());
287 mService.send(Message.obtain(null, TelemetryService.MSG_CONNECT, device));
288 } catch (RemoteException e) {
293 public boolean onCreateOptionsMenu(Menu menu) {
294 MenuInflater inflater = getMenuInflater();
295 inflater.inflate(R.menu.option_menu, menu);
300 public boolean onOptionsItemSelected(MenuItem item) {
301 Intent serverIntent = null;
302 switch (item.getItemId()) {
303 case R.id.connect_scan:
304 // Launch the DeviceListActivity to see devices and do scan
305 serverIntent = new Intent(this, DeviceListActivity.class);
306 startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);