Wei Hua | 6b4eebc | 2012-03-09 10:24:16 -0800 | [diff] [blame] | 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; |
saberian | 7b5b77b | 2012-06-04 11:19:43 -0700 | [diff] [blame^] | 47 | static private IAggregatorManager mAggregatorManager = null; |
| 48 | static private IPredictor mPredictor = null; |
Wei Hua | 6b4eebc | 2012-03-09 10:24:16 -0800 | [diff] [blame] | 49 | static private ILearning_MulticlassPA mClassifier = null; |
| 50 | static private boolean mStarted = false; |
Wei Hua | 1dd8ef5 | 2012-03-30 15:15:12 -0700 | [diff] [blame] | 51 | |
Wei Hua | 6b4eebc | 2012-03-09 10:24:16 -0800 | [diff] [blame] | 52 | public BordeauxManagerService() { |
| 53 | } |
Wei Hua | 1dd8ef5 | 2012-03-30 15:15:12 -0700 | [diff] [blame] | 54 | |
Wei Hua | 6b4eebc | 2012-03-09 10:24:16 -0800 | [diff] [blame] | 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 | } |
Wei Hua | 1dd8ef5 | 2012-03-30 15:15:12 -0700 | [diff] [blame] | 62 | |
| 63 | // Call the release, before the Context gets destroyed. |
| 64 | static public synchronized void release(Context context) { |
| 65 | if (mStarted && mConnection != null) { |
| 66 | context.unbindService(mConnection); |
| 67 | mService = null; |
| 68 | mStarted = false; |
| 69 | } |
| 70 | } |
| 71 | |
Wei Hua | 6b4eebc | 2012-03-09 10:24:16 -0800 | [diff] [blame] | 72 | static public synchronized IBordeauxService getService(Context context) { |
| 73 | if (mService == null) bindServices(context); |
| 74 | return mService; |
| 75 | } |
| 76 | |
saberian | 7b5b77b | 2012-06-04 11:19:43 -0700 | [diff] [blame^] | 77 | static public synchronized IAggregatorManager getAggregatorManager(Context context) { |
| 78 | if (mService == null) { |
| 79 | bindServices(context); |
| 80 | return null; |
| 81 | } |
| 82 | try { |
| 83 | mAggregatorManager = IAggregatorManager.Stub.asInterface( |
| 84 | mService.getAggregatorManager()); |
| 85 | } catch (RemoteException e) { |
| 86 | mAggregatorManager = null; |
| 87 | } |
| 88 | return mAggregatorManager; |
| 89 | } |
| 90 | |
| 91 | static public synchronized IPredictor getPredictor(Context context, String name) { |
| 92 | if (mService == null) { |
| 93 | bindServices(context); |
| 94 | return null; |
| 95 | } |
| 96 | try { |
| 97 | mPredictor = IPredictor.Stub.asInterface(mService.getPredictor(name)); |
| 98 | } catch (RemoteException e) { |
| 99 | mPredictor = null; |
| 100 | } |
| 101 | return mPredictor; |
| 102 | } |
| 103 | |
Wei Hua | 6b4eebc | 2012-03-09 10:24:16 -0800 | [diff] [blame] | 104 | static public synchronized ILearning_StochasticLinearRanker |
| 105 | getRanker(Context context, String name) { |
| 106 | if (mService == null) { |
| 107 | bindServices(context); |
| 108 | return null; |
| 109 | } |
| 110 | try { |
| 111 | mRanker = |
| 112 | ILearning_StochasticLinearRanker.Stub.asInterface( |
| 113 | mService.getRanker(name)); |
| 114 | } catch (RemoteException e) { |
| 115 | mRanker = null; |
| 116 | } |
| 117 | return mRanker; |
| 118 | } |
| 119 | |
| 120 | static public synchronized ILearning_MulticlassPA |
| 121 | getClassifier(Context context, String name) { |
| 122 | if (mService == null) { |
| 123 | bindServices(context); |
| 124 | return null; |
| 125 | } |
| 126 | try { |
| 127 | mClassifier = |
| 128 | ILearning_MulticlassPA.Stub.asInterface(mService.getClassifier(name)); |
| 129 | } catch (RemoteException e) { |
| 130 | mClassifier = null; |
| 131 | } |
| 132 | return mClassifier; |
| 133 | } |
Wei Hua | 1dd8ef5 | 2012-03-30 15:15:12 -0700 | [diff] [blame] | 134 | |
Wei Hua | 6b4eebc | 2012-03-09 10:24:16 -0800 | [diff] [blame] | 135 | /** |
| 136 | * Class for interacting with the main interface of the service. |
| 137 | */ |
| 138 | static private ServiceConnection mConnection = new ServiceConnection() { |
| 139 | public void onServiceConnected(ComponentName className, |
| 140 | IBinder service) { |
| 141 | // This is called when the connection with the service has been |
| 142 | // established. |
| 143 | mService = IBordeauxService.Stub.asInterface(service); |
| 144 | } |
| 145 | |
| 146 | public void onServiceDisconnected(ComponentName className) { |
| 147 | // This is called when the connection with the service has been |
| 148 | // unexpectedly disconnected -- that is, its process crashed. |
| 149 | mService = null; |
| 150 | mStarted = false; // needs to bind again |
| 151 | } |
| 152 | }; |
| 153 | } |