altos: spiradio debug serial is port 1, not port 0
[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_label;
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         // 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         // Demote us from the foreground, and cancel the persistent notification.
89         stopForeground(true);
90
91         // Tell the user we stopped.
92         Toast.makeText(this, R.string.telemetry_service_stopped, Toast.LENGTH_SHORT).show();
93     }
94
95     @Override
96     public IBinder onBind(Intent intent) {
97         return mBinder;
98     }
99
100     // This is the object that receives interactions from clients.  See
101     // RemoteService for a more complete example.
102     private final IBinder mBinder = new TelemetryBinder();
103
104 }