blob: e136ca60ad90bc67f1959be5f040322a99c5f759 [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.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.ServiceConnection;
23import android.graphics.Canvas;
24import android.graphics.Paint;
25import android.graphics.PointF;
26import android.os.Bundle;
27import android.os.Handler;
28import android.os.IBinder;
29import android.os.Message;
30import android.os.Process;
31import android.os.RemoteException;
32import 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 */
42public 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 ILearning_MulticlassPA mClassifier = null;
48 static private boolean mStarted = false;
Wei Hua1dd8ef52012-03-30 15:15:12 -070049
Wei Hua6b4eebc2012-03-09 10:24:16 -080050 public BordeauxManagerService() {
51 }
Wei Hua1dd8ef52012-03-30 15:15:12 -070052
Wei Hua6b4eebc2012-03-09 10:24:16 -080053 static private synchronized void bindServices(Context context) {
54 if (mStarted) return;
55 context.bindService(new Intent(IBordeauxService.class.getName()),
56 mConnection, Context.BIND_AUTO_CREATE);
57 mStarted = true;
58
59 }
Wei Hua1dd8ef52012-03-30 15:15:12 -070060
61 // Call the release, before the Context gets destroyed.
62 static public synchronized void release(Context context) {
63 if (mStarted && mConnection != null) {
64 context.unbindService(mConnection);
65 mService = null;
66 mStarted = false;
67 }
68 }
69
Wei Hua6b4eebc2012-03-09 10:24:16 -080070 static public synchronized IBordeauxService getService(Context context) {
71 if (mService == null) bindServices(context);
72 return mService;
73 }
74
75 static public synchronized ILearning_StochasticLinearRanker
76 getRanker(Context context, String name) {
77 if (mService == null) {
78 bindServices(context);
79 return null;
80 }
81 try {
82 mRanker =
83 ILearning_StochasticLinearRanker.Stub.asInterface(
84 mService.getRanker(name));
85 } catch (RemoteException e) {
86 mRanker = null;
87 }
88 return mRanker;
89 }
90
91 static public synchronized ILearning_MulticlassPA
92 getClassifier(Context context, String name) {
93 if (mService == null) {
94 bindServices(context);
95 return null;
96 }
97 try {
98 mClassifier =
99 ILearning_MulticlassPA.Stub.asInterface(mService.getClassifier(name));
100 } catch (RemoteException e) {
101 mClassifier = null;
102 }
103 return mClassifier;
104 }
Wei Hua1dd8ef52012-03-30 15:15:12 -0700105
Wei Hua6b4eebc2012-03-09 10:24:16 -0800106 /**
107 * Class for interacting with the main interface of the service.
108 */
109 static private ServiceConnection mConnection = new ServiceConnection() {
110 public void onServiceConnected(ComponentName className,
111 IBinder service) {
112 // This is called when the connection with the service has been
113 // established.
114 mService = IBordeauxService.Stub.asInterface(service);
115 }
116
117 public void onServiceDisconnected(ComponentName className) {
118 // This is called when the connection with the service has been
119 // unexpectedly disconnected -- that is, its process crashed.
120 mService = null;
121 mStarted = false; // needs to bind again
122 }
123 };
124}