From 6d47c54abc44e7df6ce02160428562765b6193ac Mon Sep 17 00:00:00 2001 From: Arthur Ishiguro Date: Fri, 17 Nov 2017 15:49:07 -0800 Subject: [PATCH] Handles hub reset at ContextHubClientManager Bug: 67734082 Test: make and flash device, do adb shell pkill chre and verify onMessageReceipt for reset is triggered by service (by reading logs) Change-Id: I4ea2f2930763dd8182e4e062cdb9312c3e23855e --- .../location/ContextHubClientBroker.java | 14 ++++++++++ .../location/ContextHubClientManager.java | 26 ++++++++++++++++--- .../server/location/ContextHubService.java | 5 ++-- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/services/core/java/com/android/server/location/ContextHubClientBroker.java b/services/core/java/com/android/server/location/ContextHubClientBroker.java index 276866d2a604f..1ba60b7fa7cc5 100644 --- a/services/core/java/com/android/server/location/ContextHubClientBroker.java +++ b/services/core/java/com/android/server/location/ContextHubClientBroker.java @@ -177,4 +177,18 @@ public class ContextHubClientBroker extends IContextHubClient.Stub } } } + + /** + * Handles a hub reset for this client. + */ + /* package */ void onHubReset() { + if (mConnectionOpen.get()) { + try { + mCallbackInterface.onHubReset(); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException while calling onHubReset on client" + + " (host endpoint ID = " + mHostEndPointId + ")", e); + } + } + } } diff --git a/services/core/java/com/android/server/location/ContextHubClientManager.java b/services/core/java/com/android/server/location/ContextHubClientManager.java index 11450092b7ba9..5d0d41f27e178 100644 --- a/services/core/java/com/android/server/location/ContextHubClientManager.java +++ b/services/core/java/com/android/server/location/ContextHubClientManager.java @@ -27,6 +27,7 @@ import android.util.Log; import java.util.NoSuchElementException; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Consumer; /** * A class that manages registration/unregistration of clients and manages messages to/from clients. @@ -147,6 +148,15 @@ import java.util.concurrent.ConcurrentHashMap; } } + /** + * Handles a hub reset. + * + * @param contextHubId the ID of the hub that has reset. + */ + /* package */ void onHubReset(int contextHubId) { + forEachClientOfHub(contextHubId, client -> client.onHubReset()); + } + /** * Creates a new ContextHubClientBroker object for a client and registers it with the client * manager. @@ -188,9 +198,19 @@ import java.util.concurrent.ConcurrentHashMap; * @param message the message send by a nanoapp */ private void broadcastMessage(int contextHubId, NanoAppMessage message) { - for (ContextHubClientBroker proxy : mHostEndPointIdToClientMap.values()) { - if (proxy.getAttachedContextHubId() == contextHubId) { - proxy.sendMessageToClient(message); + forEachClientOfHub(contextHubId, client -> client.sendMessageToClient(message)); + } + + /** + * Runs a command for each client that is attached to a hub with the given ID. + * + * @param contextHubId the ID of the hub + * @param callback the command to invoke for the client + */ + private void forEachClientOfHub(int contextHubId, Consumer callback) { + for (ContextHubClientBroker broker : mHostEndPointIdToClientMap.values()) { + if (broker.getAttachedContextHubId() == contextHubId) { + callback.accept(broker); } } } diff --git a/services/core/java/com/android/server/location/ContextHubService.java b/services/core/java/com/android/server/location/ContextHubService.java index c37a047d77c85..f285eb24d19b2 100644 --- a/services/core/java/com/android/server/location/ContextHubService.java +++ b/services/core/java/com/android/server/location/ContextHubService.java @@ -225,6 +225,8 @@ public class ContextHubService extends IContextHubService.Stub { @Override public void onHubReset() { + byte[] data = {TransactionResult.SUCCESS}; + onMessageReceiptOldApi(MSG_HUB_RESET, contextHubId, OS_APP_INSTANCE, data); } @Override @@ -634,8 +636,7 @@ public class ContextHubService extends IContextHubService.Stub { mTransactionManager.onHubReset(); queryNanoAppsInternal(contextHubId); - byte[] data = {TransactionResult.SUCCESS}; - onMessageReceiptOldApi(MSG_HUB_RESET, contextHubId, OS_APP_INSTANCE, data); + mClientManager.onHubReset(contextHubId); } else { Log.i(TAG, "Received unknown hub event (hub ID = " + contextHubId + ", type = " + eventType + ")");