1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.hardware.location;
import android.annotation.RequiresPermission;
import android.annotation.SuppressLint;
import android.annotation.SystemApi;
import android.annotation.SystemService;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.ServiceManager.ServiceNotFoundException;
import android.util.Log;
/**
* A class that exposes the Context hubs on a device to applications.
*
* Please note that this class is not expected to be used by unbundled applications. Also, calling
* applications are expected to have LOCATION_HARDWARE permissions to use this class.
*
* @hide
*/
@SystemApi
@SystemService(Context.CONTEXTHUB_SERVICE)
public final class ContextHubManager {
private static final String TAG = "ContextHubManager";
private final Looper mMainLooper;
private final IContextHubService mService;
private Callback mCallback;
private Handler mCallbackHandler;
/**
* @deprecated Use {@code mCallback} instead.
*/
@Deprecated
private ICallback mLocalCallback;
/**
* An interface to receive asynchronous communication from the context hub.
*/
public abstract static class Callback {
protected Callback() {}
/**
* Callback function called on message receipt from context hub.
*
* @param hubHandle Handle (system-wide unique identifier) of the hub of the message.
* @param nanoAppHandle Handle (unique identifier) for app instance that sent the message.
* @param message The context hub message.
*
* @see ContextHubMessage
*/
public abstract void onMessageReceipt(
int hubHandle,
int nanoAppHandle,
ContextHubMessage message);
}
/**
* @deprecated Use {@link Callback} instead.
* @hide
*/
@Deprecated
public interface ICallback {
/**
* Callback function called on message receipt from context hub.
*
* @param hubHandle Handle (system-wide unique identifier) of the hub of the message.
* @param nanoAppHandle Handle (unique identifier) for app instance that sent the message.
* @param message The context hub message.
*
* @see ContextHubMessage
*/
void onMessageReceipt(int hubHandle, int nanoAppHandle, ContextHubMessage message);
}
/**
* Get a handle to all the context hubs in the system
* @return array of context hub handles
*/
@RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE)
public int[] getContextHubHandles() {
try {
return mService.getContextHubHandles();
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Get more information about a specific hub.
*
* @param hubHandle Handle (system-wide unique identifier) of a context hub.
* @return ContextHubInfo Information about the requested context hub.
*
* @see ContextHubInfo
*/
@RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE)
public ContextHubInfo getContextHubInfo(int hubHandle) {
try {
return mService.getContextHubInfo(hubHandle);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Load a nano app on a specified context hub.
*
* Note that loading is asynchronous. When we return from this method,
* the nano app (probably) hasn't loaded yet. Assuming a return of 0
* from this method, then the final success/failure for the load, along
* with the "handle" for the nanoapp, is all delivered in a byte
* string via a call to Callback.onMessageReceipt.
*
* TODO(b/30784270): Provide a better success/failure and "handle" delivery.
*
* @param hubHandle handle of context hub to load the app on.
* @param app the nanoApp to load on the hub
*
* @return 0 if the command for loading was sent to the context hub;
* -1 otherwise
*
* @see NanoApp
*/
@RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE)
public int loadNanoApp(int hubHandle, NanoApp app) {
try {
return mService.loadNanoApp(hubHandle, app);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Unload a specified nanoApp
*
* Note that unloading is asynchronous. When we return from this method,
* the nano app (probably) hasn't unloaded yet. Assuming a return of 0
* from this method, then the final success/failure for the unload is
* delivered in a byte string via a call to Callback.onMessageReceipt.
*
* TODO(b/30784270): Provide a better success/failure delivery.
*
* @param nanoAppHandle handle of the nanoApp to unload
*
* @return 0 if the command for unloading was sent to the context hub;
* -1 otherwise
*/
@RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE)
public int unloadNanoApp(int nanoAppHandle) {
try {
return mService.unloadNanoApp(nanoAppHandle);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* get information about the nano app instance
*
* NOTE: The returned NanoAppInstanceInfo does _not_ contain correct
* information for several fields, specifically:
* - getName()
* - getPublisher()
* - getNeededExecMemBytes()
* - getNeededReadMemBytes()
* - getNeededWriteMemBytes()
*
* For example, say you call loadNanoApp() with a NanoApp that has
* getName() returning "My Name". Later, if you call getNanoAppInstanceInfo
* for that nanoapp, the returned NanoAppInstanceInfo's getName()
* method will claim "Preloaded app, unknown", even though you would
* have expected "My Name". For now, as the user, you'll need to
* separately track the above fields if they are of interest to you.
*
* TODO(b/30943489): Have the returned NanoAppInstanceInfo contain the
* correct information.
*
* @param nanoAppHandle handle of the nanoAppInstance
* @return NanoAppInstanceInfo Information about the nano app instance.
*
* @see NanoAppInstanceInfo
*/
@RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE)
public NanoAppInstanceInfo getNanoAppInstanceInfo(int nanoAppHandle) {
try {
return mService.getNanoAppInstanceInfo(nanoAppHandle);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Find a specified nano app on the system
*
* @param hubHandle handle of hub to search for nano app
* @param filter filter specifying the search criteria for app
*
* @see NanoAppFilter
*
* @return int[] Array of handles to any found nano apps
*/
@RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE)
public int[] findNanoAppOnHub(int hubHandle, NanoAppFilter filter) {
try {
return mService.findNanoAppOnHub(hubHandle, filter);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Send a message to a specific nano app instance on a context hub.
*
* Note that the return value of this method only speaks of success
* up to the point of sending this to the Context Hub. It is not
* an assurance that the Context Hub successfully sent this message
* on to the nanoapp. If assurance is desired, a protocol should be
* established between your code and the nanoapp, with the nanoapp
* sending a confirmation message (which will be reported via
* Callback.onMessageReceipt).
*
* @param hubHandle handle of the hub to send the message to
* @param nanoAppHandle handle of the nano app to send to
* @param message Message to be sent
*
* @see ContextHubMessage
*
* @return int 0 on success, -1 otherwise
*/
@RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE)
public int sendMessage(int hubHandle, int nanoAppHandle, ContextHubMessage message) {
try {
return mService.sendMessage(hubHandle, nanoAppHandle, message);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Set a callback to receive messages from the context hub
*
* @param callback Callback object
*
* @see Callback
*
* @return int 0 on success, -1 otherwise
*/
@SuppressLint("Doclava125")
public int registerCallback(Callback callback) {
return registerCallback(callback, null);
}
/**
* @deprecated Use {@link #registerCallback(Callback)} instead.
* @hide
*/
@Deprecated
public int registerCallback(ICallback callback) {
if (mLocalCallback != null) {
Log.w(TAG, "Max number of local callbacks reached!");
return -1;
}
mLocalCallback = callback;
return 0;
}
/**
* Set a callback to receive messages from the context hub
*
* @param callback Callback object
* @param handler Handler object
*
* @see Callback
*
* @return int 0 on success, -1 otherwise
*/
@SuppressLint("Doclava125")
public int registerCallback(Callback callback, Handler handler) {
synchronized(this) {
if (mCallback != null) {
Log.w(TAG, "Max number of callbacks reached!");
return -1;
}
mCallback = callback;
mCallbackHandler = handler;
}
return 0;
}
/**
* Unregister a callback for receive messages from the context hub.
*
* @see Callback
*
* @param callback method to deregister
*
* @return int 0 on success, -1 otherwise
*/
@SuppressLint("Doclava125")
public int unregisterCallback(Callback callback) {
synchronized(this) {
if (callback != mCallback) {
Log.w(TAG, "Cannot recognize callback!");
return -1;
}
mCallback = null;
mCallbackHandler = null;
}
return 0;
}
/**
* @deprecated Use {@link #unregisterCallback(Callback)} instead.
* @hide
*/
@Deprecated
public synchronized int unregisterCallback(ICallback callback) {
if (callback != mLocalCallback) {
Log.w(TAG, "Cannot recognize local callback!");
return -1;
}
mLocalCallback = null;
return 0;
}
private final IContextHubCallback.Stub mClientCallback = new IContextHubCallback.Stub() {
@Override
public void onMessageReceipt(final int hubId, final int nanoAppId,
final ContextHubMessage message) {
if (mCallback != null) {
synchronized(this) {
final Callback callback = mCallback;
Handler handler = mCallbackHandler == null ?
new Handler(mMainLooper) : mCallbackHandler;
handler.post(new Runnable() {
@Override
public void run() {
callback.onMessageReceipt(hubId, nanoAppId, message);
}
});
}
} else if (mLocalCallback != null) {
// we always ensure that mCallback takes precedence, because mLocalCallback is only
// for internal compatibility
synchronized (this) {
mLocalCallback.onMessageReceipt(hubId, nanoAppId, message);
}
} else {
Log.d(TAG, "Context hub manager client callback is NULL");
}
}
};
/** @throws ServiceNotFoundException
* @hide */
public ContextHubManager(Context context, Looper mainLooper) throws ServiceNotFoundException {
mMainLooper = mainLooper;
mService = IContextHubService.Stub.asInterface(
ServiceManager.getServiceOrThrow(Context.CONTEXTHUB_SERVICE));
try {
mService.registerCallback(mClientCallback);
} catch (RemoteException e) {
Log.w(TAG, "Could not register callback:" + e);
}
}
}
|