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;
21 import java.util.ArrayList;
22 import java.util.concurrent.LinkedBlockingQueue;
24 import android.app.Notification;
25 //import android.app.NotificationManager;
26 import android.app.PendingIntent;
27 import android.app.Service;
28 import android.bluetooth.BluetoothDevice;
29 import android.content.Intent;
30 //import android.os.Bundle;
31 import android.os.IBinder;
32 import android.os.Handler;
33 import android.os.Message;
34 import android.os.Messenger;
35 import android.os.RemoteException;
36 import android.util.Log;
37 import android.widget.Toast;
39 import org.altusmetrum.AltosLib.*;
41 public class TelemetryService extends Service {
43 private static final String TAG = "TelemetryService";
44 private static final boolean D = true;
46 static final int MSG_REGISTER_CLIENT = 1;
47 static final int MSG_UNREGISTER_CLIENT = 2;
48 static final int MSG_CONNECT = 3;
49 static final int MSG_CONNECTED = 4;
51 public static final int STATE_NONE = 0;
52 public static final int STATE_READY = 1;
53 public static final int STATE_CONNECTING = 2;
54 public static final int STATE_CONNECTED = 3;
56 // Unique Identification Number for the Notification.
57 // We use it on Notification start, and to cancel it.
58 private int NOTIFICATION = R.string.telemetry_service_label;
59 //private NotificationManager mNM;
61 ArrayList<Messenger> mClients = new ArrayList<Messenger>(); // Keeps track of all current registered clients.
62 final Handler mHandler = new IncomingHandler(this);
63 final Messenger mMessenger = new Messenger(mHandler); // Target we publish for clients to send messages to IncomingHandler.
65 // Name of the connected device
66 private BluetoothDevice device = null;
67 private AltosBluetooth mAltosBluetooth = null;
68 private int state = STATE_NONE;
69 LinkedBlockingQueue<AltosLine> telem;
71 // Handler of incoming messages from clients.
72 static class IncomingHandler extends Handler {
73 private final WeakReference<TelemetryService> service;
74 IncomingHandler(TelemetryService s) { service = new WeakReference<TelemetryService>(s); }
77 public void handleMessage(Message msg) {
78 TelemetryService s = service.get();
80 case MSG_REGISTER_CLIENT:
81 s.mClients.add(msg.replyTo);
83 msg.replyTo.send(Message.obtain(null, AltosDroid.MSG_STATE_CHANGE, s.state, -1));
84 } catch (RemoteException e) {
85 s.mClients.remove(msg.replyTo);
87 if (D) Log.d(TAG, "Client bound to service");
89 case MSG_UNREGISTER_CLIENT:
90 s.mClients.remove(msg.replyTo);
91 if (D) Log.d(TAG, "Client unbound from service");
94 if (D) Log.d(TAG, "Connect command received");
95 s.device = (BluetoothDevice) msg.obj;
96 s.startAltosBluetooth();
99 if (D) Log.d(TAG, "Connected to device");
100 s.sendMessageToClients(Message.obtain(null, AltosDroid.MSG_DEVNAME, s.device.getName()));
101 s.setState(STATE_CONNECTED);
102 s.mAltosBluetooth.add_monitor(s.telem);
105 super.handleMessage(msg);
110 private void sendMessageToClients(Message m) {
111 for (int i=mClients.size()-1; i>=0; i--) {
113 mClients.get(i).send(m);
114 } catch (RemoteException e) {
120 private void stopAltosBluetooth() {
121 if (D) Log.i(TAG, "Stopping BT");
122 setState(STATE_READY);
123 if (mAltosBluetooth != null) {
124 if (D) Log.i(TAG, "Closing AltosBluetooth");
125 mAltosBluetooth.close();
126 mAltosBluetooth = null;
132 private void startAltosBluetooth() {
133 if (mAltosBluetooth == null) {
134 if (D) Log.i(TAG, "Connecting to " + device.getName());
135 mAltosBluetooth = new AltosBluetooth(device, mHandler);
136 setState(STATE_CONNECTING);
138 stopAltosBluetooth();
139 mHandler.sendMessageDelayed(Message.obtain(null, MSG_CONNECT, device), 1000);
143 private synchronized void setState(int s) {
144 if (D) Log.d(TAG, "setState() " + state + " -> " + s);
147 sendMessageToClients(Message.obtain(null, AltosDroid.MSG_STATE_CHANGE, state, -1));
151 public void onCreate() {
152 // Create a reference to the NotificationManager so that we can update our notifcation text later
153 //mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
155 telem = new LinkedBlockingQueue<AltosLine>();
156 setState(STATE_READY);
160 public int onStartCommand(Intent intent, int flags, int startId) {
161 Log.i("TelemetryService", "Received start id " + startId + ": " + intent);
163 CharSequence text = getText(R.string.telemetry_service_started);
165 // Create notification to be displayed while the service runs
166 Notification notification = new Notification(R.drawable.am_status_c, text, 0);
168 // The PendingIntent to launch our activity if the user selects this notification
169 PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
170 new Intent(this, AltosDroid.class), 0);
172 // Set the info for the views that show in the notification panel.
173 notification.setLatestEventInfo(this, getText(R.string.telemetry_service_label), text, contentIntent);
175 // Set the notification to be in the "Ongoing" section.
176 notification.flags |= Notification.FLAG_ONGOING_EVENT;
178 // Move us into the foreground.
179 startForeground(NOTIFICATION, notification);
181 // We want this service to continue running until it is explicitly
182 // stopped, so return sticky.
187 public void onDestroy() {
189 // Stop the bluetooth Comms threads
190 stopAltosBluetooth();
192 // Demote us from the foreground, and cancel the persistent notification.
193 stopForeground(true);
195 // Tell the user we stopped.
196 Toast.makeText(this, R.string.telemetry_service_stopped, Toast.LENGTH_SHORT).show();
200 public IBinder onBind(Intent intent) {
201 return mMessenger.getBinder();