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