From 72c676e343e4598cfdf2b3f5543c780a03e7ac6e Mon Sep 17 00:00:00 2001 From: Arthur Ishiguro Date: Wed, 7 Nov 2018 13:39:12 -0800 Subject: Reject PendingIntents owned by another client Removes ambiguity when regenerating a ContextHubClient. Bug: 117612105 Test: Compile only Change-Id: Ie105366d8b46b69c332859d9b096f6e6c05ca271 --- core/java/android/hardware/location/ContextHubClient.java | 7 ++++--- .../android/server/location/ContextHubClientBroker.java | 4 ++++ .../android/server/location/ContextHubClientManager.java | 15 ++++++++++++++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/core/java/android/hardware/location/ContextHubClient.java b/core/java/android/hardware/location/ContextHubClient.java index 9f11246d7372..2717c4ee5313 100644 --- a/core/java/android/hardware/location/ContextHubClient.java +++ b/core/java/android/hardware/location/ContextHubClient.java @@ -107,9 +107,10 @@ public class ContextHubClient implements Closeable { * This method should be used if the caller wants to receive notifications even after the * process exits. The client must have an open connection with the Context Hub Service (i.e. it * cannot have been closed through the {@link #close()} method). Only one PendingIntent can be - * registered at a time for a single ContextHubClient. If registered successfully, intents will - * be delivered regarding events for the specified nanoapp from the attached Context Hub. Any - * unicast messages for this client will also be delivered. The intent will have an extra + * registered at a time for a single ContextHubClient, and the PendingIntent cannot be + * registered if already registered by a ContextHubClient. If registered successfully, intents + * will be delivered regarding events for the specified nanoapp from the attached Context Hub. + * Any unicast messages for this client will also be delivered. The intent will have an extra * {@link ContextHubManager.EXTRA_CONTEXT_HUB_INFO} of type {@link ContextHubInfo}, which * describes the Context Hub the intent event was for. The intent will also have an extra * {@link ContextHubManager.EXTRA_EVENT_TYPE} of type {@link ContextHubManager.Event}, which diff --git a/services/core/java/com/android/server/location/ContextHubClientBroker.java b/services/core/java/com/android/server/location/ContextHubClientBroker.java index 642347021201..002d4e1049fb 100644 --- a/services/core/java/com/android/server/location/ContextHubClientBroker.java +++ b/services/core/java/com/android/server/location/ContextHubClientBroker.java @@ -211,6 +211,10 @@ public class ContextHubClientBroker extends IContextHubClient.Stub @Override public boolean registerIntent(PendingIntent pendingIntent, long nanoAppId) { ContextHubServiceUtil.checkPermissions(mContext); + if (mClientManager.isPendingIntentRegistered(pendingIntent)) { + Log.e(TAG, "Failed to register PendingIntent: already registered"); + return false; + } boolean success = false; synchronized (this) { diff --git a/services/core/java/com/android/server/location/ContextHubClientManager.java b/services/core/java/com/android/server/location/ContextHubClientManager.java index 72879ddc2018..fe93a1a0f613 100644 --- a/services/core/java/com/android/server/location/ContextHubClientManager.java +++ b/services/core/java/com/android/server/location/ContextHubClientManager.java @@ -24,7 +24,6 @@ import android.hardware.location.ContextHubInfo; import android.hardware.location.IContextHubClient; import android.hardware.location.IContextHubClientCallback; import android.hardware.location.NanoAppMessage; -import android.os.RemoteException; import android.util.Log; import java.util.concurrent.ConcurrentHashMap; @@ -203,6 +202,20 @@ import java.util.function.Consumer; forEachClientOfHub(contextHubId, client -> client.onNanoAppAborted(nanoAppId, abortCode)); } + /** + * @param pendingIntent the PendingIntent to check + * @return true if the given PendingIntent is registered by a client, false otherwise + */ + /* package */ boolean isPendingIntentRegistered(PendingIntent pendingIntent) { + for (ContextHubClientBroker broker : mHostEndPointIdToClientMap.values()) { + if (broker.hasPendingIntent(pendingIntent)) { + return true; + } + } + + return false; + } + /** * Creates a new ContextHubClientBroker object for a client and registers it with the client * manager. -- cgit v1.2.3-59-g8ed1b