| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.hardware.location; |
| 17 |
|
| 18 |
import android.annotation.CallbackExecutor; |
| 19 |
import android.annotation.IntDef; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.annotation.RequiresFeature; |
| 23 |
import android.annotation.RequiresPermission; |
| 24 |
import android.annotation.SuppressLint; |
| 25 |
import android.annotation.SystemApi; |
| 26 |
import android.annotation.SystemService; |
| 27 |
import android.app.ActivityThread; |
| 28 |
import android.app.PendingIntent; |
| 29 |
import android.content.Context; |
| 30 |
import android.content.Intent; |
| 31 |
import android.content.pm.PackageManager; |
| 32 |
import android.os.Handler; |
| 33 |
import android.os.HandlerExecutor; |
| 34 |
import android.os.Looper; |
| 35 |
import android.os.RemoteException; |
| 36 |
import android.os.ServiceManager; |
| 37 |
import android.os.ServiceManager.ServiceNotFoundException; |
| 38 |
import android.util.Log; |
| 39 |
|
| 40 |
import java.lang.annotation.Retention; |
| 41 |
import java.lang.annotation.RetentionPolicy; |
| 42 |
import java.util.List; |
| 43 |
import java.util.Objects; |
| 44 |
import java.util.concurrent.Executor; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
@SystemApi |
| 55 |
@SystemService(Context.CONTEXTHUB_SERVICE) |
| 56 |
@RequiresFeature(PackageManager.FEATURE_CONTEXT_HUB) |
| 57 |
public final class ContextHubManager { |
| 58 |
private static final String TAG = "ContextHubManager"; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public static final String EXTRA_CLIENT_AUTHORIZATION_STATE = |
| 65 |
"android.hardware.location.extra.CLIENT_AUTHORIZATION_STATE"; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public static final String EXTRA_CONTEXT_HUB_INFO = |
| 71 |
"android.hardware.location.extra.CONTEXT_HUB_INFO"; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public static final String EXTRA_EVENT_TYPE = "android.hardware.location.extra.EVENT_TYPE"; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public static final String EXTRA_NANOAPP_ID = "android.hardware.location.extra.NANOAPP_ID"; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public static final String EXTRA_NANOAPP_ABORT_CODE = |
| 87 |
"android.hardware.location.extra.NANOAPP_ABORT_CODE"; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public static final String EXTRA_MESSAGE = "android.hardware.location.extra.MESSAGE"; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
@Retention(RetentionPolicy.SOURCE) |
| 101 |
@IntDef(prefix = { "AUTHORIZATION_" }, value = { |
| 102 |
AUTHORIZATION_DENIED, |
| 103 |
AUTHORIZATION_DENIED_GRACE_PERIOD, |
| 104 |
AUTHORIZATION_GRANTED, |
| 105 |
}) |
| 106 |
public @interface AuthorizationState { } |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public static final int AUTHORIZATION_DENIED = 0; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public static final int AUTHORIZATION_DENIED_GRACE_PERIOD = 1; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public static final int AUTHORIZATION_GRANTED = 2; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
@Retention(RetentionPolicy.SOURCE) |
| 134 |
@IntDef(prefix = { "EVENT_" }, value = { |
| 135 |
EVENT_NANOAPP_LOADED, |
| 136 |
EVENT_NANOAPP_UNLOADED, |
| 137 |
EVENT_NANOAPP_ENABLED, |
| 138 |
EVENT_NANOAPP_DISABLED, |
| 139 |
EVENT_NANOAPP_ABORTED, |
| 140 |
EVENT_NANOAPP_MESSAGE, |
| 141 |
EVENT_HUB_RESET, |
| 142 |
EVENT_CLIENT_AUTHORIZATION, |
| 143 |
}) |
| 144 |
public @interface Event { } |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public static final int EVENT_NANOAPP_LOADED = 0; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public static final int EVENT_NANOAPP_UNLOADED = 1; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public static final int EVENT_NANOAPP_ENABLED = 2; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public static final int EVENT_NANOAPP_DISABLED = 3; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public static final int EVENT_NANOAPP_ABORTED = 4; |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public static final int EVENT_NANOAPP_MESSAGE = 5; |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public static final int EVENT_HUB_RESET = 6; |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public static final int EVENT_CLIENT_AUTHORIZATION = 7; |
| 190 |
|
| 191 |
private final Looper mMainLooper; |
| 192 |
private final IContextHubService mService; |
| 193 |
private Callback mCallback; |
| 194 |
private Handler mCallbackHandler; |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
@Deprecated |
| 200 |
private ICallback mLocalCallback; |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
@Deprecated |
| 209 |
public abstract static class Callback { |
| 210 |
protected Callback() {} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public abstract void onMessageReceipt( |
| 222 |
int hubHandle, |
| 223 |
int nanoAppHandle, |
| 224 |
@NonNull ContextHubMessage message); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
@Deprecated |
| 232 |
public interface ICallback { |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
void onMessageReceipt(int hubHandle, int nanoAppHandle, ContextHubMessage message); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
@Deprecated |
| 254 |
@RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) |
| 255 |
public int[] getContextHubHandles() { |
| 256 |
try { |
| 257 |
return mService.getContextHubHandles(); |
| 258 |
} catch (RemoteException e) { |
| 259 |
throw e.rethrowFromSystemServer(); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
@Deprecated |
| 275 |
@RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) |
| 276 |
public ContextHubInfo getContextHubInfo(int hubHandle) { |
| 277 |
try { |
| 278 |
return mService.getContextHubInfo(hubHandle); |
| 279 |
} catch (RemoteException e) { |
| 280 |
throw e.rethrowFromSystemServer(); |
| 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 |
@Deprecated |
| 306 |
@RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) |
| 307 |
public int loadNanoApp(int hubHandle, @NonNull NanoApp app) { |
| 308 |
try { |
| 309 |
return mService.loadNanoApp(hubHandle, app); |
| 310 |
} catch (RemoteException e) { |
| 311 |
throw e.rethrowFromSystemServer(); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
@Deprecated |
| 333 |
@RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) |
| 334 |
public int unloadNanoApp(int nanoAppHandle) { |
| 335 |
try { |
| 336 |
return mService.unloadNanoApp(nanoAppHandle); |
| 337 |
} catch (RemoteException e) { |
| 338 |
throw e.rethrowFromSystemServer(); |
| 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 |
@Deprecated |
| 373 |
@RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) |
| 374 |
@Nullable public NanoAppInstanceInfo getNanoAppInstanceInfo(int nanoAppHandle) { |
| 375 |
try { |
| 376 |
return mService.getNanoAppInstanceInfo(nanoAppHandle); |
| 377 |
} catch (RemoteException e) { |
| 378 |
throw e.rethrowFromSystemServer(); |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
@Deprecated |
| 396 |
@RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) |
| 397 |
@NonNull public int[] findNanoAppOnHub(int hubHandle, @NonNull NanoAppFilter filter) { |
| 398 |
try { |
| 399 |
return mService.findNanoAppOnHub(hubHandle, filter); |
| 400 |
} catch (RemoteException e) { |
| 401 |
throw e.rethrowFromSystemServer(); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
@Deprecated |
| 431 |
@RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) |
| 432 |
public int sendMessage(int hubHandle, int nanoAppHandle, @NonNull ContextHubMessage message) { |
| 433 |
try { |
| 434 |
return mService.sendMessage(hubHandle, nanoAppHandle, message); |
| 435 |
} catch (RemoteException e) { |
| 436 |
throw e.rethrowFromSystemServer(); |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
@RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) |
| 448 |
@NonNull public List<ContextHubInfo> getContextHubs() { |
| 449 |
try { |
| 450 |
return mService.getContextHubs(); |
| 451 |
} catch (RemoteException e) { |
| 452 |
throw e.rethrowFromSystemServer(); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
private IContextHubTransactionCallback createTransactionCallback( |
| 466 |
ContextHubTransaction<Void> transaction) { |
| 467 |
return new IContextHubTransactionCallback.Stub() { |
| 468 |
@Override |
| 469 |
public void onQueryResponse(int result, List<NanoAppState> nanoappList) { |
| 470 |
Log.e(TAG, "Received a query callback on a non-query request"); |
| 471 |
transaction.setResponse(new ContextHubTransaction.Response<Void>( |
| 472 |
ContextHubTransaction.RESULT_FAILED_SERVICE_INTERNAL_FAILURE, null)); |
| 473 |
} |
| 474 |
|
| 475 |
@Override |
| 476 |
public void onTransactionComplete(int result) { |
| 477 |
transaction.setResponse(new ContextHubTransaction.Response<Void>(result, null)); |
| 478 |
} |
| 479 |
}; |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
private IContextHubTransactionCallback createQueryCallback( |
| 492 |
ContextHubTransaction<List<NanoAppState>> transaction) { |
| 493 |
return new IContextHubTransactionCallback.Stub() { |
| 494 |
@Override |
| 495 |
public void onQueryResponse(int result, List<NanoAppState> nanoappList) { |
| 496 |
transaction.setResponse(new ContextHubTransaction.Response<List<NanoAppState>>( |
| 497 |
result, nanoappList)); |
| 498 |
} |
| 499 |
|
| 500 |
@Override |
| 501 |
public void onTransactionComplete(int result) { |
| 502 |
Log.e(TAG, "Received a non-query callback on a query request"); |
| 503 |
transaction.setResponse(new ContextHubTransaction.Response<List<NanoAppState>>( |
| 504 |
ContextHubTransaction.RESULT_FAILED_SERVICE_INTERNAL_FAILURE, null)); |
| 505 |
} |
| 506 |
}; |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
@RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) |
| 525 |
@NonNull public ContextHubTransaction<Void> loadNanoApp( |
| 526 |
@NonNull ContextHubInfo hubInfo, @NonNull NanoAppBinary appBinary) { |
| 527 |
Objects.requireNonNull(hubInfo, "ContextHubInfo cannot be null"); |
| 528 |
Objects.requireNonNull(appBinary, "NanoAppBinary cannot be null"); |
| 529 |
|
| 530 |
ContextHubTransaction<Void> transaction = |
| 531 |
new ContextHubTransaction<>(ContextHubTransaction.TYPE_LOAD_NANOAPP); |
| 532 |
IContextHubTransactionCallback callback = createTransactionCallback(transaction); |
| 533 |
|
| 534 |
try { |
| 535 |
mService.loadNanoAppOnHub(hubInfo.getId(), callback, appBinary); |
| 536 |
} catch (RemoteException e) { |
| 537 |
throw e.rethrowFromSystemServer(); |
| 538 |
} |
| 539 |
|
| 540 |
return transaction; |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
@RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) |
| 554 |
@NonNull public ContextHubTransaction<Void> unloadNanoApp( |
| 555 |
@NonNull ContextHubInfo hubInfo, long nanoAppId) { |
| 556 |
Objects.requireNonNull(hubInfo, "ContextHubInfo cannot be null"); |
| 557 |
|
| 558 |
ContextHubTransaction<Void> transaction = |
| 559 |
new ContextHubTransaction<>(ContextHubTransaction.TYPE_UNLOAD_NANOAPP); |
| 560 |
IContextHubTransactionCallback callback = createTransactionCallback(transaction); |
| 561 |
|
| 562 |
try { |
| 563 |
mService.unloadNanoAppFromHub(hubInfo.getId(), callback, nanoAppId); |
| 564 |
} catch (RemoteException e) { |
| 565 |
throw e.rethrowFromSystemServer(); |
| 566 |
} |
| 567 |
|
| 568 |
return transaction; |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
@RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) |
| 582 |
@NonNull public ContextHubTransaction<Void> enableNanoApp( |
| 583 |
@NonNull ContextHubInfo hubInfo, long nanoAppId) { |
| 584 |
Objects.requireNonNull(hubInfo, "ContextHubInfo cannot be null"); |
| 585 |
|
| 586 |
ContextHubTransaction<Void> transaction = |
| 587 |
new ContextHubTransaction<>(ContextHubTransaction.TYPE_ENABLE_NANOAPP); |
| 588 |
IContextHubTransactionCallback callback = createTransactionCallback(transaction); |
| 589 |
|
| 590 |
try { |
| 591 |
mService.enableNanoApp(hubInfo.getId(), callback, nanoAppId); |
| 592 |
} catch (RemoteException e) { |
| 593 |
throw e.rethrowFromSystemServer(); |
| 594 |
} |
| 595 |
|
| 596 |
return transaction; |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
@RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) |
| 610 |
@NonNull public ContextHubTransaction<Void> disableNanoApp( |
| 611 |
@NonNull ContextHubInfo hubInfo, long nanoAppId) { |
| 612 |
Objects.requireNonNull(hubInfo, "ContextHubInfo cannot be null"); |
| 613 |
|
| 614 |
ContextHubTransaction<Void> transaction = |
| 615 |
new ContextHubTransaction<>(ContextHubTransaction.TYPE_DISABLE_NANOAPP); |
| 616 |
IContextHubTransactionCallback callback = createTransactionCallback(transaction); |
| 617 |
|
| 618 |
try { |
| 619 |
mService.disableNanoApp(hubInfo.getId(), callback, nanoAppId); |
| 620 |
} catch (RemoteException e) { |
| 621 |
throw e.rethrowFromSystemServer(); |
| 622 |
} |
| 623 |
|
| 624 |
return transaction; |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
@RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) |
| 637 |
@NonNull public ContextHubTransaction<List<NanoAppState>> queryNanoApps( |
| 638 |
@NonNull ContextHubInfo hubInfo) { |
| 639 |
Objects.requireNonNull(hubInfo, "ContextHubInfo cannot be null"); |
| 640 |
|
| 641 |
ContextHubTransaction<List<NanoAppState>> transaction = |
| 642 |
new ContextHubTransaction<>(ContextHubTransaction.TYPE_QUERY_NANOAPPS); |
| 643 |
IContextHubTransactionCallback callback = createQueryCallback(transaction); |
| 644 |
|
| 645 |
try { |
| 646 |
mService.queryNanoApps(hubInfo.getId(), callback); |
| 647 |
} catch (RemoteException e) { |
| 648 |
throw e.rethrowFromSystemServer(); |
| 649 |
} |
| 650 |
|
| 651 |
return transaction; |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
@Deprecated |
| 668 |
@SuppressLint("RequiresPermission") |
| 669 |
public int registerCallback(@NonNull Callback callback) { |
| 670 |
return registerCallback(callback, null); |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
@Deprecated |
| 678 |
public int registerCallback(ICallback callback) { |
| 679 |
if (mLocalCallback != null) { |
| 680 |
Log.w(TAG, "Max number of local callbacks reached!"); |
| 681 |
return -1; |
| 682 |
} |
| 683 |
mLocalCallback = callback; |
| 684 |
return 0; |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
@Deprecated |
| 702 |
@SuppressLint("RequiresPermission") |
| 703 |
public int registerCallback(Callback callback, Handler handler) { |
| 704 |
synchronized(this) { |
| 705 |
if (mCallback != null) { |
| 706 |
Log.w(TAG, "Max number of callbacks reached!"); |
| 707 |
return -1; |
| 708 |
} |
| 709 |
mCallback = callback; |
| 710 |
mCallbackHandler = (handler == null) ? new Handler(mMainLooper) : handler; |
| 711 |
} |
| 712 |
return 0; |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
private IContextHubClientCallback createClientCallback( |
| 725 |
ContextHubClient client, ContextHubClientCallback callback, Executor executor) { |
| 726 |
return new IContextHubClientCallback.Stub() { |
| 727 |
@Override |
| 728 |
public void onMessageFromNanoApp(NanoAppMessage message) { |
| 729 |
executor.execute(() -> callback.onMessageFromNanoApp(client, message)); |
| 730 |
} |
| 731 |
|
| 732 |
@Override |
| 733 |
public void onHubReset() { |
| 734 |
executor.execute(() -> callback.onHubReset(client)); |
| 735 |
} |
| 736 |
|
| 737 |
@Override |
| 738 |
public void onNanoAppAborted(long nanoAppId, int abortCode) { |
| 739 |
executor.execute(() -> callback.onNanoAppAborted(client, nanoAppId, abortCode)); |
| 740 |
} |
| 741 |
|
| 742 |
@Override |
| 743 |
public void onNanoAppLoaded(long nanoAppId) { |
| 744 |
executor.execute(() -> callback.onNanoAppLoaded(client, nanoAppId)); |
| 745 |
} |
| 746 |
|
| 747 |
@Override |
| 748 |
public void onNanoAppUnloaded(long nanoAppId) { |
| 749 |
executor.execute(() -> callback.onNanoAppUnloaded(client, nanoAppId)); |
| 750 |
} |
| 751 |
|
| 752 |
@Override |
| 753 |
public void onNanoAppEnabled(long nanoAppId) { |
| 754 |
executor.execute(() -> callback.onNanoAppEnabled(client, nanoAppId)); |
| 755 |
} |
| 756 |
|
| 757 |
@Override |
| 758 |
public void onNanoAppDisabled(long nanoAppId) { |
| 759 |
executor.execute(() -> callback.onNanoAppDisabled(client, nanoAppId)); |
| 760 |
} |
| 761 |
|
| 762 |
@Override |
| 763 |
public void onClientAuthorizationChanged( |
| 764 |
long nanoAppId, @ContextHubManager.AuthorizationState int authorization) { |
| 765 |
executor.execute( |
| 766 |
() -> callback.onClientAuthorizationChanged( |
| 767 |
client, nanoAppId, authorization)); |
| 768 |
} |
| 769 |
}; |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
@RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) |
| 792 |
@NonNull public ContextHubClient createClient( |
| 793 |
@Nullable Context context, @NonNull ContextHubInfo hubInfo, |
| 794 |
@NonNull @CallbackExecutor Executor executor, |
| 795 |
@NonNull ContextHubClientCallback callback) { |
| 796 |
Objects.requireNonNull(callback, "Callback cannot be null"); |
| 797 |
Objects.requireNonNull(hubInfo, "ContextHubInfo cannot be null"); |
| 798 |
Objects.requireNonNull(executor, "Executor cannot be null"); |
| 799 |
|
| 800 |
ContextHubClient client = new ContextHubClient(hubInfo, false ); |
| 801 |
IContextHubClientCallback clientInterface = createClientCallback( |
| 802 |
client, callback, executor); |
| 803 |
|
| 804 |
String attributionTag = null; |
| 805 |
if (context != null) { |
| 806 |
attributionTag = context.getAttributionTag(); |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
String packageName; |
| 811 |
if (context != null) { |
| 812 |
packageName = context.getPackageName(); |
| 813 |
} else { |
| 814 |
packageName = ActivityThread.currentPackageName(); |
| 815 |
} |
| 816 |
|
| 817 |
IContextHubClient clientProxy; |
| 818 |
try { |
| 819 |
clientProxy = mService.createClient( |
| 820 |
hubInfo.getId(), clientInterface, attributionTag, packageName); |
| 821 |
} catch (RemoteException e) { |
| 822 |
throw e.rethrowFromSystemServer(); |
| 823 |
} |
| 824 |
|
| 825 |
client.setClientProxy(clientProxy); |
| 826 |
return client; |
| 827 |
} |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
@RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) |
| 836 |
@NonNull public ContextHubClient createClient( |
| 837 |
@NonNull ContextHubInfo hubInfo, @NonNull ContextHubClientCallback callback, |
| 838 |
@NonNull @CallbackExecutor Executor executor) { |
| 839 |
return createClient(null , hubInfo, executor, callback); |
| 840 |
} |
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
@RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) |
| 847 |
@NonNull public ContextHubClient createClient( |
| 848 |
@NonNull ContextHubInfo hubInfo, @NonNull ContextHubClientCallback callback) { |
| 849 |
return createClient(null , hubInfo, new HandlerExecutor(Handler.getMain()), |
| 850 |
callback); |
| 851 |
} |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
@RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) |
| 900 |
@NonNull public ContextHubClient createClient( |
| 901 |
@Nullable Context context, @NonNull ContextHubInfo hubInfo, |
| 902 |
@NonNull PendingIntent pendingIntent, long nanoAppId) { |
| 903 |
Objects.requireNonNull(pendingIntent); |
| 904 |
Objects.requireNonNull(hubInfo); |
| 905 |
if (pendingIntent.isImmutable()) { |
| 906 |
throw new IllegalArgumentException("PendingIntent must be mutable"); |
| 907 |
} |
| 908 |
|
| 909 |
ContextHubClient client = new ContextHubClient(hubInfo, true ); |
| 910 |
|
| 911 |
String attributionTag = null; |
| 912 |
if (context != null) { |
| 913 |
attributionTag = context.getAttributionTag(); |
| 914 |
} |
| 915 |
|
| 916 |
IContextHubClient clientProxy; |
| 917 |
try { |
| 918 |
clientProxy = mService.createPendingIntentClient( |
| 919 |
hubInfo.getId(), pendingIntent, nanoAppId, attributionTag); |
| 920 |
} catch (RemoteException e) { |
| 921 |
throw e.rethrowFromSystemServer(); |
| 922 |
} |
| 923 |
|
| 924 |
client.setClientProxy(clientProxy); |
| 925 |
return client; |
| 926 |
} |
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
@RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) |
| 933 |
@NonNull public ContextHubClient createClient( |
| 934 |
@NonNull ContextHubInfo hubInfo, @NonNull PendingIntent pendingIntent, long nanoAppId) { |
| 935 |
return createClient(null , hubInfo, pendingIntent, nanoAppId); |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
@SuppressLint("RequiresPermission") |
| 951 |
@Deprecated |
| 952 |
public int unregisterCallback(@NonNull Callback callback) { |
| 953 |
synchronized(this) { |
| 954 |
if (callback != mCallback) { |
| 955 |
Log.w(TAG, "Cannot recognize callback!"); |
| 956 |
return -1; |
| 957 |
} |
| 958 |
|
| 959 |
mCallback = null; |
| 960 |
mCallbackHandler = null; |
| 961 |
} |
| 962 |
return 0; |
| 963 |
} |
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
@Deprecated |
| 970 |
public synchronized int unregisterCallback(ICallback callback) { |
| 971 |
if (callback != mLocalCallback) { |
| 972 |
Log.w(TAG, "Cannot recognize local callback!"); |
| 973 |
return -1; |
| 974 |
} |
| 975 |
mLocalCallback = null; |
| 976 |
return 0; |
| 977 |
} |
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
private synchronized void invokeOnMessageReceiptCallback( |
| 987 |
int hubId, int nanoAppId, ContextHubMessage message) { |
| 988 |
if (mCallback != null) { |
| 989 |
mCallback.onMessageReceipt(hubId, nanoAppId, message); |
| 990 |
} |
| 991 |
} |
| 992 |
|
| 993 |
private final IContextHubCallback.Stub mClientCallback = new IContextHubCallback.Stub() { |
| 994 |
@Override |
| 995 |
public void onMessageReceipt( |
| 996 |
final int hubId, final int nanoAppId, final ContextHubMessage message) { |
| 997 |
synchronized (ContextHubManager.this) { |
| 998 |
if (mCallback != null) { |
| 999 |
mCallbackHandler.post( |
| 1000 |
() -> invokeOnMessageReceiptCallback(hubId, nanoAppId, message)); |
| 1001 |
} else if (mLocalCallback != null) { |
| 1002 |
|
| 1003 |
|
| 1004 |
mLocalCallback.onMessageReceipt(hubId, nanoAppId, message); |
| 1005 |
} |
| 1006 |
} |
| 1007 |
} |
| 1008 |
}; |
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
public ContextHubManager(Context context, Looper mainLooper) throws ServiceNotFoundException { |
| 1013 |
mMainLooper = mainLooper; |
| 1014 |
mService = IContextHubService.Stub.asInterface( |
| 1015 |
ServiceManager.getServiceOrThrow(Context.CONTEXTHUB_SERVICE)); |
| 1016 |
try { |
| 1017 |
mService.registerCallback(mClientCallback); |
| 1018 |
} catch (RemoteException e) { |
| 1019 |
throw e.rethrowFromSystemServer(); |
| 1020 |
} |
| 1021 |
} |
| 1022 |
} |