altosdroid: begin adding IPC to TelemetryService
[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.util.ArrayList;
21 import java.util.concurrent.LinkedBlockingQueue;
22
23 import android.app.Notification;
24 import android.app.NotificationManager;
25 import android.app.PendingIntent;
26 import android.app.Service;
27 import android.bluetooth.BluetoothDevice;
28 import android.content.Intent;
29 //import android.os.Binder;
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_TELEBT    = 3;
51
52         // Unique Identification Number for the Notification.
53         // We use it on Notification start, and to cancel it.
54         private int NOTIFICATION = R.string.telemetry_service_label;
55         private NotificationManager mNM;
56
57         ArrayList<Messenger> mClients = new ArrayList<Messenger>(); // Keeps track of all current registered clients.
58         final Messenger mMessenger = new Messenger(new IncomingHandler()); // Target we publish for clients to send messages to IncomingHandler.
59
60         // Name of the connected device
61         private String mConnectedDeviceName = null;
62         private AltosBluetooth mAltosBluetooth = null;
63
64         LinkedBlockingQueue<AltosLine> telem;
65
66         // Handler of incoming messages from clients.
67         class IncomingHandler extends Handler {
68                 @Override
69                 public void handleMessage(Message msg) {
70                         switch (msg.what) {
71                         case MSG_REGISTER_CLIENT:
72                                 mClients.add(msg.replyTo);
73                                 if (D) Log.d(TAG, "Client bound to service");
74                                 break;
75                         case MSG_UNREGISTER_CLIENT:
76                                 mClients.remove(msg.replyTo);
77                                 if (D) Log.d(TAG, "Client unbound from service");
78                                 break;
79                         case MSG_CONNECT_TELEBT:
80                                 if (D) Log.d(TAG, "Connect command received");
81                                 TeleBT_stop();
82                                 TeleBT_start((BluetoothDevice) msg.obj);
83                                 break;
84                         default:
85                                 super.handleMessage(msg);
86                         }
87                 }
88         }
89
90         private void TeleBT_stop() {
91                 if (mAltosBluetooth != null) {
92                         mAltosBluetooth.close();
93                         mAltosBluetooth = null;
94                 }
95                 telem.clear();
96         }
97
98         private void TeleBT_start(BluetoothDevice d) {
99                 mAltosBluetooth = new AltosBluetooth(d);
100                 mAltosBluetooth.add_monitor(telem);
101         }
102
103         @Override
104         public void onCreate() {
105                 // Create a reference to the NotificationManager so that we can update our notifcation text later
106                 mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
107         }
108
109         @Override
110         public int onStartCommand(Intent intent, int flags, int startId) {
111                 Log.i("TelemetryService", "Received start id " + startId + ": " + intent);
112
113                 CharSequence text = getText(R.string.telemetry_service_started);
114
115                 // Create notification to be displayed while the service runs
116                 Notification notification = new Notification(R.drawable.am_status_c, text, 0);
117
118                 // The PendingIntent to launch our activity if the user selects this notification
119                 PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
120                                 new Intent(this, AltosDroid.class), 0);
121
122                 // Set the info for the views that show in the notification panel.
123                 notification.setLatestEventInfo(this, getText(R.string.telemetry_service_label), text, contentIntent);
124
125                 // Set the notification to be in the "Ongoing" section.
126                 notification.flags |= Notification.FLAG_ONGOING_EVENT;
127
128                 // Move us into the foreground.
129                 startForeground(NOTIFICATION, notification);
130
131                 // We want this service to continue running until it is explicitly
132                 // stopped, so return sticky.
133                 return START_STICKY;
134         }
135
136         @Override
137         public void onDestroy() {
138
139                 // Stop the bluetooth Comms threads
140                 TeleBT_stop();
141
142                 // Demote us from the foreground, and cancel the persistent notification.
143                 stopForeground(true);
144
145                 // Tell the user we stopped.
146                 Toast.makeText(this, R.string.telemetry_service_stopped, Toast.LENGTH_SHORT).show();
147         }
148
149         @Override
150         public IBinder onBind(Intent intent) {
151                 return mMessenger.getBinder();
152         }
153
154
155 }