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