Add TelemetryService.java and associated files
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / TelemetryService.java
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.altusmetrum.AltosDroid;
18
19 import android.app.Notification;
20 import android.app.NotificationManager;
21 import android.app.PendingIntent;
22 import android.app.Service;
23 import android.content.Intent;
24 import android.os.Binder;
25 import android.os.IBinder;
26 import android.util.Log;
27 import android.widget.Toast;
28
29 // Need the following import to get access to the app resources, since this
30 // class is in a sub-package.
31 import org.altusmetrum.AltosDroid.R;
32
33
34
35 public class TelemetryService extends Service {
36     private NotificationManager mNM;
37
38     // Unique Identification Number for the Notification.
39     // We use it on Notification start, and to cancel it.
40     private int NOTIFICATION = R.string.telemetry_service_started;
41
42     /**
43      * Class for clients to access.  Because we know this service always
44      * runs in the same process as its clients, we don't need to deal with
45      * IPC.
46      */
47     public class TelemetryBinder extends Binder {
48         TelemetryService getService() {
49             return TelemetryService.this;
50         }
51     }
52
53     @Override
54     public void onCreate() {
55         mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
56
57         // Display a notification about us starting.  We put an icon in the status bar.
58         showNotification();
59     }
60
61     @Override
62     public int onStartCommand(Intent intent, int flags, int startId) {
63         Log.i("TelemetryService", "Received start id " + startId + ": " + intent);
64         // We want this service to continue running until it is explicitly
65         // stopped, so return sticky.
66         return START_STICKY;
67     }
68
69     @Override
70     public void onDestroy() {
71         // Cancel the persistent notification.
72         mNM.cancel(NOTIFICATION);
73
74         // Tell the user we stopped.
75         Toast.makeText(this, R.string.telemetry_service_stopped, Toast.LENGTH_SHORT).show();
76     }
77
78     @Override
79     public IBinder onBind(Intent intent) {
80         return mBinder;
81     }
82
83     // This is the object that receives interactions from clients.  See
84     // RemoteService for a more complete example.
85     private final IBinder mBinder = new TelemetryBinder();
86
87     /**
88      * Show a notification while this service is running.
89      */
90     private void showNotification() {
91         // In this sample, we'll use the same text for the ticker and the expanded notification
92         CharSequence text = getText(R.string.telemetry_service_started);
93
94         // Set the icon, scrolling text and timestamp
95         Notification notification = new Notification(R.drawable.am_status, text,
96                 System.currentTimeMillis());
97
98         // The PendingIntent to launch our activity if the user selects this notification
99         PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
100                 new Intent(this, TelemetryServiceActivities.Controller.class), 0);
101
102         // Set the info for the views that show in the notification panel.
103         notification.setLatestEventInfo(this, getText(R.string.telemetry_service_label),
104                        text, contentIntent);
105
106         // Send the notification.
107         mNM.notify(NOTIFICATION, notification);
108     }
109 }