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 17 package android.bordeaux.services; 18 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.ServiceConnection; 23 import android.graphics.Canvas; 24 import android.graphics.Paint; 25 import android.graphics.PointF; 26 import android.os.Bundle; 27 import android.os.Handler; 28 import android.os.IBinder; 29 import android.os.Message; 30 import android.os.Process; 31 import android.os.RemoteException; 32 import android.util.Log; 33 34 /** 35 * {@hide} 36 * This is used to provide a convenience to access the actual remote running 37 * service. 38 * TODO: Eventally the remote service will be running in the system server, and 39 * this will need to be served as a stub for the remote running service. And 40 * extends from IBordeauxManager.stub 41 */ 42 public class BordeauxManagerService { 43 44 static private final String TAG = "BordeauxMangerService"; 45 static private IBordeauxService mService = null; 46 static private ILearning_StochasticLinearRanker mRanker = null; 47 static private IAggregatorManager mAggregatorManager = null; 48 static private IPredictor mPredictor = null; 49 static private ILearning_MulticlassPA mClassifier = null; 50 static private boolean mStarted = false; 51 BordeauxManagerService()52 public BordeauxManagerService() { 53 } 54 bindServices(Context context)55 static private synchronized void bindServices(Context context) { 56 if (mStarted) return; 57 context.bindService(new Intent(IBordeauxService.class.getName()), 58 mConnection, Context.BIND_AUTO_CREATE); 59 mStarted = true; 60 } 61 62 // Call the release, before the Context gets destroyed. release(Context context)63 static public synchronized void release(Context context) { 64 if (mStarted && mConnection != null) { 65 context.unbindService(mConnection); 66 mService = null; 67 mStarted = false; 68 } 69 } 70 getService(Context context)71 static public synchronized IBordeauxService getService(Context context) { 72 if (mService == null) bindServices(context); 73 return mService; 74 } 75 getAggregatorManager(Context context)76 static public synchronized IAggregatorManager getAggregatorManager(Context context) { 77 if (mService == null) { 78 bindServices(context); 79 return null; 80 } 81 try { 82 mAggregatorManager = IAggregatorManager.Stub.asInterface( 83 mService.getAggregatorManager()); 84 } catch (RemoteException e) { 85 mAggregatorManager = null; 86 } 87 return mAggregatorManager; 88 } 89 getPredictor(Context context, String name)90 static public synchronized IPredictor getPredictor(Context context, String name) { 91 if (mService == null) { 92 bindServices(context); 93 return null; 94 } 95 try { 96 mPredictor = IPredictor.Stub.asInterface(mService.getPredictor(name)); 97 } catch (RemoteException e) { 98 mPredictor = null; 99 } 100 return mPredictor; 101 } 102 103 static public synchronized ILearning_StochasticLinearRanker getRanker(Context context, String name)104 getRanker(Context context, String name) { 105 if (mService == null) { 106 bindServices(context); 107 return null; 108 } 109 try { 110 mRanker = 111 ILearning_StochasticLinearRanker.Stub.asInterface( 112 mService.getRanker(name)); 113 } catch (RemoteException e) { 114 mRanker = null; 115 } 116 return mRanker; 117 } 118 119 static public synchronized ILearning_MulticlassPA getClassifier(Context context, String name)120 getClassifier(Context context, String name) { 121 if (mService == null) { 122 bindServices(context); 123 return null; 124 } 125 try { 126 mClassifier = 127 ILearning_MulticlassPA.Stub.asInterface(mService.getClassifier(name)); 128 } catch (RemoteException e) { 129 mClassifier = null; 130 } 131 return mClassifier; 132 } 133 134 /** 135 * Class for interacting with the main interface of the service. 136 */ 137 static private ServiceConnection mConnection = new ServiceConnection() { 138 public void onServiceConnected(ComponentName className, 139 IBinder service) { 140 // This is called when the connection with the service has been 141 // established. 142 mService = IBordeauxService.Stub.asInterface(service); 143 } 144 145 public void onServiceDisconnected(ComponentName className) { 146 // This is called when the connection with the service has been 147 // unexpectedly disconnected -- that is, its process crashed. 148 mService = null; 149 mStarted = false; // needs to bind again 150 } 151 }; 152 } 153