blob: 48d41d94209ba9cd169874a3f6faab60e6f25173 [file] [log] [blame]
Wei Hua6b4eebc2012-03-09 10:24:16 -08001/*
2 * Copyright (C) 2012 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
17package android.bordeaux.services;
18
19import android.app.Activity;
20import android.app.Notification;
21import android.app.NotificationManager;
22import android.app.PendingIntent;
23import android.app.Service;
24import android.content.ComponentName;
25import android.content.Context;
26import android.content.Intent;
Wei Hua1dd8ef52012-03-30 15:15:12 -070027import android.content.pm.PackageManager;
Wei Hua6b4eebc2012-03-09 10:24:16 -080028import android.content.ServiceConnection;
29import android.os.Bundle;
Wei Hua6b4eebc2012-03-09 10:24:16 -080030import android.os.Handler;
31import android.os.IBinder;
32import android.os.Message;
33import android.os.Process;
34import android.os.RemoteCallbackList;
Wei Hua1dd8ef52012-03-30 15:15:12 -070035import android.os.RemoteException;
Wei Hua6b4eebc2012-03-09 10:24:16 -080036import android.view.View;
37import android.view.View.OnClickListener;
38import android.widget.Button;
39import android.widget.TextView;
40import android.widget.Toast;
Wei Hua6b4eebc2012-03-09 10:24:16 -080041
saberian984e52f2012-06-05 18:23:53 -070042//import android.bordeaux.R;
Wei Hua1dd8ef52012-03-30 15:15:12 -070043import android.util.Log;
44
45import java.io.*;
Wei Hua6b4eebc2012-03-09 10:24:16 -080046
47/**
48 * Machine Learning service that runs in a remote process.
49 * The application doesn't use this class directly.
50 *
51 */
52public class BordeauxService extends Service {
53 private final String TAG = "BordeauxService";
54 /**
55 * This is a list of callbacks that have been registered with the
56 * service.
57 * It's a place holder for future communications with all registered
58 * clients.
59 */
60 final RemoteCallbackList<IBordeauxServiceCallback> mCallbacks =
61 new RemoteCallbackList<IBordeauxServiceCallback>();
62
63 int mValue = 0;
64 NotificationManager mNotificationManager;
65
Wei Hua1dd8ef52012-03-30 15:15:12 -070066 BordeauxSessionManager mSessionManager;
saberian984e52f2012-06-05 18:23:53 -070067 AggregatorManager mAggregatorManager;
68 TimeStatsAggregator mTimeStatsAggregator;
69 LocationStatsAggregator mLocationStatsAggregator;
70 MotionStatsAggregator mMotionStatsAggregator;
Wei Hua6b4eebc2012-03-09 10:24:16 -080071
72 @Override
73 public void onCreate() {
Wei Hua1dd8ef52012-03-30 15:15:12 -070074 Log.i(TAG, "Bordeaux service created.");
saberian984e52f2012-06-05 18:23:53 -070075 //mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Wei Hua1dd8ef52012-03-30 15:15:12 -070076 mSessionManager = new BordeauxSessionManager(this);
saberian984e52f2012-06-05 18:23:53 -070077 mMotionStatsAggregator = new MotionStatsAggregator();
Ruei-sung Linf0f78442012-08-13 19:04:29 -070078 mLocationStatsAggregator = new LocationStatsAggregator(this);
saberian984e52f2012-06-05 18:23:53 -070079 mTimeStatsAggregator = new TimeStatsAggregator();
80 mAggregatorManager = AggregatorManager.getInstance();
81 mAggregatorManager.registerAggregator(mMotionStatsAggregator, mAggregatorManager);
82 mAggregatorManager.registerAggregator(mLocationStatsAggregator, mAggregatorManager);
83 mAggregatorManager.registerAggregator(mTimeStatsAggregator, mAggregatorManager);
Wei Hua6b4eebc2012-03-09 10:24:16 -080084
85 // Display a notification about us starting.
86 // TODO: don't display the notification after the service is
87 // automatically started by the system, currently it's useful for
88 // debugging.
89 showNotification();
90 }
91
92 @Override
93 public void onDestroy() {
Wei Hua1dd8ef52012-03-30 15:15:12 -070094 // Save the sessions
95 mSessionManager.saveSessions();
96
Wei Hua6b4eebc2012-03-09 10:24:16 -080097 // Cancel the persistent notification.
saberian984e52f2012-06-05 18:23:53 -070098 //mNotificationManager.cancel(R.string.remote_service_started);
Wei Hua6b4eebc2012-03-09 10:24:16 -080099
100 // Tell the user we stopped.
saberian984e52f2012-06-05 18:23:53 -0700101 //Toast.makeText(this, R.string.remote_service_stopped, Toast.LENGTH_SHORT).show();
Wei Hua6b4eebc2012-03-09 10:24:16 -0800102
103 // Unregister all callbacks.
104 mCallbacks.kill();
Wei Hua1dd8ef52012-03-30 15:15:12 -0700105
Ruei-sung Lin47c0dc02012-09-19 18:12:24 -0700106 mLocationStatsAggregator.release();
107
Wei Hua1dd8ef52012-03-30 15:15:12 -0700108 Log.i(TAG, "Bordeaux service stopped.");
Wei Hua6b4eebc2012-03-09 10:24:16 -0800109 }
110
111 @Override
112 public IBinder onBind(Intent intent) {
113 // Return the requested interface.
114 if (IBordeauxService.class.getName().equals(intent.getAction())) {
115 return mBinder;
116 }
117 return null;
118 }
119
Wei Hua1dd8ef52012-03-30 15:15:12 -0700120
Wei Hua6b4eebc2012-03-09 10:24:16 -0800121 // The main interface implemented by the service.
122 private final IBordeauxService.Stub mBinder = new IBordeauxService.Stub() {
Wei Hua1dd8ef52012-03-30 15:15:12 -0700123 private IBinder getLearningSession(Class learnerClass, String name) {
Wei Hua6b4eebc2012-03-09 10:24:16 -0800124 PackageManager pm = getPackageManager();
125 String uidname = pm.getNameForUid(getCallingUid());
126 Log.i(TAG,"Name for uid: " + uidname);
Wei Hua1dd8ef52012-03-30 15:15:12 -0700127 BordeauxSessionManager.SessionKey key =
128 mSessionManager.getSessionKey(uidname, learnerClass, name);
129 Log.i(TAG, "request learning session: " + key.value);
130 try {
131 IBinder iLearner = mSessionManager.getSessionBinder(learnerClass, key);
132 return iLearner;
133 } catch (RuntimeException e) {
134 Log.e(TAG, "Error getting learning interface" + e);
135 return null;
Wei Hua6b4eebc2012-03-09 10:24:16 -0800136 }
Wei Hua1dd8ef52012-03-30 15:15:12 -0700137 }
138
139 public IBinder getClassifier(String name) {
140 return getLearningSession(Learning_MulticlassPA.class, name);
Wei Hua6b4eebc2012-03-09 10:24:16 -0800141 }
142
143 public IBinder getRanker(String name) {
Wei Hua1dd8ef52012-03-30 15:15:12 -0700144 return getLearningSession(Learning_StochasticLinearRanker.class, name);
Wei Hua6b4eebc2012-03-09 10:24:16 -0800145 }
146
saberian984e52f2012-06-05 18:23:53 -0700147 public IBinder getPredictor(String name) {
148 return getLearningSession(Predictor.class, name);
149 }
150
151 public IBinder getAggregatorManager() {
152 return (IBinder) mAggregatorManager;
153 }
154
Wei Hua6b4eebc2012-03-09 10:24:16 -0800155 public void registerCallback(IBordeauxServiceCallback cb) {
156 if (cb != null) mCallbacks.register(cb);
157 }
158
159 public void unregisterCallback(IBordeauxServiceCallback cb) {
160 if (cb != null) mCallbacks.unregister(cb);
161 }
162 };
163
Wei Hua6b4eebc2012-03-09 10:24:16 -0800164 @Override
165 public void onTaskRemoved(Intent rootIntent) {
166 Toast.makeText(this, "Task removed: " + rootIntent, Toast.LENGTH_LONG).show();
167 }
168
169 /**
170 * Show a notification while this service is running.
171 * TODO: remove the code after production (when service is loaded
172 * automatically by the system).
173 */
174 private void showNotification() {
saberian984e52f2012-06-05 18:23:53 -0700175 /*// In this sample, we'll use the same text for the ticker and the expanded notification
Wei Hua6b4eebc2012-03-09 10:24:16 -0800176 CharSequence text = getText(R.string.remote_service_started);
177
178 // The PendingIntent to launch our activity if the user selects this notification
179 PendingIntent contentIntent =
180 PendingIntent.getActivity(this, 0,
181 new Intent("android.bordeaux.DEBUG_CONTROLLER"), 0);
182
183 // // Set the info for the views that show in the notification panel.
184
185 Notification.Builder builder = new Notification.Builder(this);
186 builder.setSmallIcon(R.drawable.ic_bordeaux);
187 builder.setWhen(System.currentTimeMillis());
188 builder.setTicker(text);
189 builder.setContentTitle(text);
190 builder.setContentIntent(contentIntent);
191 Notification notification = builder.getNotification();
192 // Send the notification.
193 // We use a string id because it is a unique number. We use it later to cancel.
saberian984e52f2012-06-05 18:23:53 -0700194 mNotificationManager.notify(R.string.remote_service_started, notification); */
Wei Hua6b4eebc2012-03-09 10:24:16 -0800195 }
196
197}