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 android.app.Notification;
21 import android.app.NotificationManager;
22 import android.app.PendingIntent;
23 import android.app.Service;
24 import android.content.Intent;
25 import android.os.Binder;
26 import android.os.IBinder;
27 import android.util.Log;
28 import android.widget.Toast;
30 // Need the following import to get access to the app resources, since this
31 // class is in a sub-package.
32 //import org.altusmetrum.AltosDroid.R;
34 import org.altusmetrum.AltosLib.*;
36 public class TelemetryService extends Service {
38 private static final String TAG = "TelemetryService";
39 private static final boolean D = true;
42 * Class for clients to access. Because we know this service always
43 * runs in the same process as its clients, we don't need to deal with
46 public class TelemetryBinder extends Binder {
47 TelemetryService getService() {
48 return TelemetryService.this;
51 // Unique Identification Number for the Notification.
52 // We use it on Notification start, and to cancel it.
53 private int NOTIFICATION = R.string.telemetry_service_label;
54 private NotificationManager mNM;
57 // Name of the connected device
58 private String mConnectedDeviceName = null;
59 private AltosBluetooth mAltosBluetooth = null;
61 LinkedBlockingQueue<AltosLine> telem;
64 public void onCreate() {
65 // Create a reference to the NotificationManager so that we can update our notifcation text later
66 mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
70 public int onStartCommand(Intent intent, int flags, int startId) {
71 Log.i("TelemetryService", "Received start id " + startId + ": " + intent);
73 CharSequence text = getText(R.string.telemetry_service_started);
75 // Create notification to be displayed while the service runs
76 Notification notification = new Notification(R.drawable.am_status_c, text, 0);
78 // The PendingIntent to launch our activity if the user selects this notification
79 PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
80 new Intent(this, AltosDroid.class), 0);
82 // Set the info for the views that show in the notification panel.
83 notification.setLatestEventInfo(this, getText(R.string.telemetry_service_label), text, contentIntent);
85 // Set the notification to be in the "Ongoing" section.
86 notification.flags |= Notification.FLAG_ONGOING_EVENT;
88 // Move us into the foreground.
89 startForeground(NOTIFICATION, notification);
91 // We want this service to continue running until it is explicitly
92 // stopped, so return sticky.
97 public void onDestroy() {
100 // Demote us from the foreground, and cancel the persistent notification.
101 stopForeground(true);
103 // Tell the user we stopped.
104 Toast.makeText(this, R.string.telemetry_service_stopped, Toast.LENGTH_SHORT).show();
108 public IBinder onBind(Intent intent) {
112 // This is the object that receives interactions from clients. See
113 // RemoteService for a more complete example.
114 private final IBinder mBinder = new TelemetryBinder();