altosdroid: Update copyrights
[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 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;
29
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;
33
34
35
36 public class TelemetryService extends Service {
37     private NotificationManager mNM;
38
39     // Unique Identification Number for the Notification.
40     // We use it on Notification start, and to cancel it.
41     private int NOTIFICATION = R.string.telemetry_service_label;
42
43     /**
44      * Class for clients to access.  Because we know this service always
45      * runs in the same process as its clients, we don't need to deal with
46      * IPC.
47      */
48     public class TelemetryBinder extends Binder {
49         TelemetryService getService() {
50             return TelemetryService.this;
51         }
52     }
53
54     @Override
55     public void onCreate() {
56         // Create a reference to the NotificationManager so that we can update our notifcation text later
57         mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
58     }
59
60     @Override
61     public int onStartCommand(Intent intent, int flags, int startId) {
62         Log.i("TelemetryService", "Received start id " + startId + ": " + intent);
63
64         CharSequence text = getText(R.string.telemetry_service_started);
65
66         // Create notification to be displayed while the service runs
67         Notification notification = new Notification(R.drawable.am_status_c, text, 0);
68
69         // The PendingIntent to launch our activity if the user selects this notification
70         PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
71                 new Intent(this, TelemetryServiceActivities.Controller.class), 0);
72
73         // Set the info for the views that show in the notification panel.
74         notification.setLatestEventInfo(this, getText(R.string.telemetry_service_label), text, contentIntent);
75
76         // Set the notification to be in the "Ongoing" section.
77         notification.flags |= Notification.FLAG_ONGOING_EVENT;
78
79         // Move us into the foreground.
80         startForeground(NOTIFICATION, notification);
81
82         // We want this service to continue running until it is explicitly
83         // stopped, so return sticky.
84         return START_STICKY;
85     }
86
87     @Override
88     public void onDestroy() {
89         // Demote us from the foreground, and cancel the persistent notification.
90         stopForeground(true);
91
92         // Tell the user we stopped.
93         Toast.makeText(this, R.string.telemetry_service_stopped, Toast.LENGTH_SHORT).show();
94     }
95
96     @Override
97     public IBinder onBind(Intent intent) {
98         return mBinder;
99     }
100
101     // This is the object that receives interactions from clients.  See
102     // RemoteService for a more complete example.
103     private final IBinder mBinder = new TelemetryBinder();
104
105 }