altosdroid: Rename Connect message, add connected message
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / TelemetryService.java
1 /*
2  * Copyright © 2012 Mike Beattie <mike@ethernal.org>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.AltosDroid;
19
20 import java.lang.ref.WeakReference;
21 import java.util.ArrayList;
22 import java.util.concurrent.LinkedBlockingQueue;
23
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.IBinder;
31 import android.os.Handler;
32 import android.os.Message;
33 import android.os.Messenger;
34 import android.util.Log;
35 import android.widget.Toast;
36
37 // Need the following import to get access to the app resources, since this
38 // class is in a sub-package.
39 //import org.altusmetrum.AltosDroid.R;
40
41 import org.altusmetrum.AltosLib.*;
42
43 public class TelemetryService extends Service {
44
45         private static final String TAG = "TelemetryService";
46         private static final boolean D = true;
47
48         static final int MSG_REGISTER_CLIENT   = 1;
49         static final int MSG_UNREGISTER_CLIENT = 2;
50         static final int MSG_CONNECT           = 3;
51         static final int MSG_CONNECTED         = 4;
52
53         public static final int STATE_NONE       = 0;
54         public static final int STATE_READY      = 1;
55         public static final int STATE_CONNECTING = 2;
56         public static final int STATE_CONNECTED  = 3;
57
58         // Unique Identification Number for the Notification.
59         // We use it on Notification start, and to cancel it.
60         private int NOTIFICATION = R.string.telemetry_service_label;
61         //private NotificationManager mNM;
62
63         ArrayList<Messenger> mClients = new ArrayList<Messenger>(); // Keeps track of all current registered clients.
64         final Messenger mMessenger = new Messenger(new IncomingHandler()); // Target we publish for clients to send messages to IncomingHandler.
65
66         // Name of the connected device
67         private String mConnectedDeviceName = null;
68         private AltosBluetooth mAltosBluetooth = null;
69
70         LinkedBlockingQueue<AltosLine> telem;
71
72         // Handler of incoming messages from clients.
73         static class IncomingHandler extends Handler {
74                 private final WeakReference<TelemetryService> service;
75                 IncomingHandler(TelemetryService s) { service = new WeakReference<TelemetryService>(s); }
76
77                 @Override
78                 public void handleMessage(Message msg) {
79                         TelemetryService s = service.get();
80                         switch (msg.what) {
81                         case MSG_REGISTER_CLIENT:
82                                 s.mClients.add(msg.replyTo);
83                                 if (D) Log.d(TAG, "Client bound to service");
84                                 break;
85                         case MSG_UNREGISTER_CLIENT:
86                                 s.mClients.remove(msg.replyTo);
87                                 if (D) Log.d(TAG, "Client unbound from service");
88                                 break;
89                         case MSG_CONNECT:
90                                 if (D) Log.d(TAG, "Connect command received");
91                                 s.startAltosBluetooth((BluetoothDevice) msg.obj);
92                                 break;
93                         case MSG_CONNECTED:
94                                 if (D) Log.d(TAG, "Connected to device");
95                                 break;
96                         default:
97                                 super.handleMessage(msg);
98                         }
99                 }
100         }
101
102         private void stopAltosBluetooth() {
103                 if (mAltosBluetooth != null) {
104                         mAltosBluetooth.close();
105                         mAltosBluetooth = null;
106                 }
107                 telem.clear();
108         }
109
110         private void startAltosBluetooth(BluetoothDevice d) {
111                 mAltosBluetooth = new AltosBluetooth(d);
112                 mAltosBluetooth.add_monitor(telem);
113         }
114
115         @Override
116         public void onCreate() {
117                 // Create a reference to the NotificationManager so that we can update our notifcation text later
118                 //mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
119
120                 telem = new LinkedBlockingQueue<AltosLine>();
121         }
122
123         @Override
124         public int onStartCommand(Intent intent, int flags, int startId) {
125                 Log.i("TelemetryService", "Received start id " + startId + ": " + intent);
126
127                 CharSequence text = getText(R.string.telemetry_service_started);
128
129                 // Create notification to be displayed while the service runs
130                 Notification notification = new Notification(R.drawable.am_status_c, text, 0);
131
132                 // The PendingIntent to launch our activity if the user selects this notification
133                 PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
134                                 new Intent(this, AltosDroid.class), 0);
135
136                 // Set the info for the views that show in the notification panel.
137                 notification.setLatestEventInfo(this, getText(R.string.telemetry_service_label), text, contentIntent);
138
139                 // Set the notification to be in the "Ongoing" section.
140                 notification.flags |= Notification.FLAG_ONGOING_EVENT;
141
142                 // Move us into the foreground.
143                 startForeground(NOTIFICATION, notification);
144
145                 // We want this service to continue running until it is explicitly
146                 // stopped, so return sticky.
147                 return START_STICKY;
148         }
149
150         @Override
151         public void onDestroy() {
152
153                 // Stop the bluetooth Comms threads
154                 stopAltosBluetooth();
155
156                 // Demote us from the foreground, and cancel the persistent notification.
157                 stopForeground(true);
158
159                 // Tell the user we stopped.
160                 Toast.makeText(this, R.string.telemetry_service_stopped, Toast.LENGTH_SHORT).show();
161         }
162
163         @Override
164         public IBinder onBind(Intent intent) {
165                 return mMessenger.getBinder();
166         }
167
168
169 }