BordeauxService.java revision 2fc454f62ff5e11eb16a93a3d515a4e4fba89b5e
1/* 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; 27import android.content.pm.PackageManager; 28import android.content.ServiceConnection; 29import android.os.Bundle; 30import android.os.Handler; 31import android.os.IBinder; 32import android.os.Message; 33import android.os.Process; 34import android.os.RemoteCallbackList; 35import android.os.RemoteException; 36import android.view.View; 37import android.view.View.OnClickListener; 38import android.widget.Button; 39import android.widget.TextView; 40import android.widget.Toast; 41 42//import android.bordeaux.R; 43import android.util.Log; 44 45import java.io.*; 46 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 66 BordeauxSessionManager mSessionManager; 67 AggregatorManager mAggregatorManager; 68 TimeStatsAggregator mTimeStatsAggregator; 69 LocationStatsAggregator mLocationStatsAggregator; 70 MotionStatsAggregator mMotionStatsAggregator; 71 72 @Override 73 public void onCreate() { 74 Log.i(TAG, "Bordeaux service created."); 75 //mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 76 mSessionManager = new BordeauxSessionManager(this); 77 mMotionStatsAggregator = new MotionStatsAggregator(); 78 mLocationStatsAggregator = new LocationStatsAggregator(this); 79 mTimeStatsAggregator = new TimeStatsAggregator(); 80 mAggregatorManager = AggregatorManager.getInstance(); 81 mAggregatorManager.registerAggregator(mMotionStatsAggregator, mAggregatorManager); 82 mAggregatorManager.registerAggregator(mLocationStatsAggregator, mAggregatorManager); 83 mAggregatorManager.registerAggregator(mTimeStatsAggregator, mAggregatorManager); 84 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() { 94 // Save the sessions 95 mSessionManager.saveSessions(); 96 97 // Cancel the persistent notification. 98 //mNotificationManager.cancel(R.string.remote_service_started); 99 100 // Tell the user we stopped. 101 //Toast.makeText(this, R.string.remote_service_stopped, Toast.LENGTH_SHORT).show(); 102 103 // Unregister all callbacks. 104 mCallbacks.kill(); 105 106 Log.i(TAG, "Bordeaux service stopped."); 107 } 108 109 @Override 110 public IBinder onBind(Intent intent) { 111 // Return the requested interface. 112 if (IBordeauxService.class.getName().equals(intent.getAction())) { 113 return mBinder; 114 } 115 return null; 116 } 117 118 119 // The main interface implemented by the service. 120 private final IBordeauxService.Stub mBinder = new IBordeauxService.Stub() { 121 private IBinder getLearningSession(Class learnerClass, String name) { 122 PackageManager pm = getPackageManager(); 123 String uidname = pm.getNameForUid(getCallingUid()); 124 Log.i(TAG,"Name for uid: " + uidname); 125 BordeauxSessionManager.SessionKey key = 126 mSessionManager.getSessionKey(uidname, learnerClass, name); 127 Log.i(TAG, "request learning session: " + key.value); 128 try { 129 IBinder iLearner = mSessionManager.getSessionBinder(learnerClass, key); 130 return iLearner; 131 } catch (RuntimeException e) { 132 Log.e(TAG, "Error getting learning interface" + e); 133 return null; 134 } 135 } 136 137 public IBinder getClassifier(String name) { 138 return getLearningSession(Learning_MulticlassPA.class, name); 139 } 140 141 public IBinder getRanker(String name) { 142 return getLearningSession(Learning_StochasticLinearRanker.class, name); 143 } 144 145 public IBinder getPredictor(String name) { 146 return getLearningSession(Predictor.class, name); 147 } 148 149 public IBinder getAggregatorManager() { 150 return (IBinder) mAggregatorManager; 151 } 152 153 public void registerCallback(IBordeauxServiceCallback cb) { 154 if (cb != null) mCallbacks.register(cb); 155 } 156 157 public void unregisterCallback(IBordeauxServiceCallback cb) { 158 if (cb != null) mCallbacks.unregister(cb); 159 } 160 }; 161 162 @Override 163 public void onTaskRemoved(Intent rootIntent) { 164 Toast.makeText(this, "Task removed: " + rootIntent, Toast.LENGTH_LONG).show(); 165 } 166 167 /** 168 * Show a notification while this service is running. 169 * TODO: remove the code after production (when service is loaded 170 * automatically by the system). 171 */ 172 private void showNotification() { 173 /*// In this sample, we'll use the same text for the ticker and the expanded notification 174 CharSequence text = getText(R.string.remote_service_started); 175 176 // The PendingIntent to launch our activity if the user selects this notification 177 PendingIntent contentIntent = 178 PendingIntent.getActivity(this, 0, 179 new Intent("android.bordeaux.DEBUG_CONTROLLER"), 0); 180 181 // // Set the info for the views that show in the notification panel. 182 183 Notification.Builder builder = new Notification.Builder(this); 184 builder.setSmallIcon(R.drawable.ic_bordeaux); 185 builder.setWhen(System.currentTimeMillis()); 186 builder.setTicker(text); 187 builder.setContentTitle(text); 188 builder.setContentIntent(contentIntent); 189 Notification notification = builder.getNotification(); 190 // Send the notification. 191 // We use a string id because it is a unique number. We use it later to cancel. 192 mNotificationManager.notify(R.string.remote_service_started, notification); */ 193 } 194 195} 196