Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
| 17 | package com.android.server.location; |
| 18 | |
Arthur Ishiguro | 9aa88e0 | 2018-10-16 10:26:57 -0700 | [diff] [blame] | 19 | import android.Manifest; |
Arthur Ishiguro | a7c3797 | 2018-10-01 15:42:27 -0700 | [diff] [blame] | 20 | import android.app.PendingIntent; |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 21 | import android.content.Context; |
Arthur Ishiguro | 9aa88e0 | 2018-10-16 10:26:57 -0700 | [diff] [blame] | 22 | import android.content.Intent; |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 23 | import android.hardware.contexthub.V1_0.ContextHubMsg; |
| 24 | import android.hardware.contexthub.V1_0.IContexthub; |
| 25 | import android.hardware.contexthub.V1_0.Result; |
Arthur Ishiguro | 622ebcb | 2018-10-17 14:02:27 -0700 | [diff] [blame] | 26 | import android.hardware.location.ContextHubInfo; |
Arthur Ishiguro | 9aa88e0 | 2018-10-16 10:26:57 -0700 | [diff] [blame] | 27 | import android.hardware.location.ContextHubManager; |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 28 | import android.hardware.location.ContextHubTransaction; |
| 29 | import android.hardware.location.IContextHubClient; |
| 30 | import android.hardware.location.IContextHubClientCallback; |
| 31 | import android.hardware.location.NanoAppMessage; |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 32 | import android.os.IBinder; |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 33 | import android.os.RemoteException; |
| 34 | import android.util.Log; |
| 35 | |
Arthur Ishiguro | 9aa88e0 | 2018-10-16 10:26:57 -0700 | [diff] [blame] | 36 | import java.util.function.Supplier; |
| 37 | |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 38 | /** |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 39 | * A class that acts as a broker for the ContextHubClient, which handles messaging and life-cycle |
| 40 | * notification callbacks. This class implements the IContextHubClient object, and the implemented |
| 41 | * APIs must be thread-safe. |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 42 | * |
Arthur Ishiguro | 82ed3af | 2018-11-09 13:20:30 -0800 | [diff] [blame] | 43 | * TODO: Consider refactoring this class via inheritance |
| 44 | * |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 45 | * @hide |
| 46 | */ |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 47 | public class ContextHubClientBroker extends IContextHubClient.Stub |
| 48 | implements IBinder.DeathRecipient { |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 49 | private static final String TAG = "ContextHubClientBroker"; |
| 50 | |
| 51 | /* |
| 52 | * The context of the service. |
| 53 | */ |
| 54 | private final Context mContext; |
| 55 | |
| 56 | /* |
| 57 | * The proxy to talk to the Context Hub HAL. |
| 58 | */ |
| 59 | private final IContexthub mContextHubProxy; |
| 60 | |
| 61 | /* |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 62 | * The manager that registered this client. |
| 63 | */ |
| 64 | private final ContextHubClientManager mClientManager; |
| 65 | |
| 66 | /* |
Arthur Ishiguro | 622ebcb | 2018-10-17 14:02:27 -0700 | [diff] [blame] | 67 | * The object describing the hub that this client is attached to. |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 68 | */ |
Arthur Ishiguro | 622ebcb | 2018-10-17 14:02:27 -0700 | [diff] [blame] | 69 | private final ContextHubInfo mAttachedContextHubInfo; |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 70 | |
| 71 | /* |
| 72 | * The host end point ID of this client. |
| 73 | */ |
| 74 | private final short mHostEndPointId; |
| 75 | |
| 76 | /* |
Arthur Ishiguro | daeed3c | 2018-10-01 15:42:27 -0700 | [diff] [blame] | 77 | * The remote callback interface for this client. This will be set to null whenever the |
| 78 | * client connection is closed (either explicitly or via binder death). |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 79 | */ |
Arthur Ishiguro | daeed3c | 2018-10-01 15:42:27 -0700 | [diff] [blame] | 80 | private IContextHubClientCallback mCallbackInterface = null; |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 81 | |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 82 | /* |
Arthur Ishiguro | daeed3c | 2018-10-01 15:42:27 -0700 | [diff] [blame] | 83 | * True if the client is still registered with the Context Hub Service, false otherwise. |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 84 | */ |
Arthur Ishiguro | daeed3c | 2018-10-01 15:42:27 -0700 | [diff] [blame] | 85 | private boolean mRegistered = true; |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 86 | |
Arthur Ishiguro | 661f502 | 2018-10-10 12:17:29 -0700 | [diff] [blame] | 87 | /* |
| 88 | * Internal interface used to invoke client callbacks. |
| 89 | */ |
| 90 | private interface CallbackConsumer { |
| 91 | void accept(IContextHubClientCallback callback) throws RemoteException; |
| 92 | } |
| 93 | |
Arthur Ishiguro | 32c2bc6 | 2018-10-16 10:26:11 -0700 | [diff] [blame] | 94 | /* |
| 95 | * The PendingIntent registered with this client. |
| 96 | */ |
Arthur Ishiguro | 82ed3af | 2018-11-09 13:20:30 -0800 | [diff] [blame] | 97 | private final PendingIntentRequest mPendingIntentRequest; |
Arthur Ishiguro | 32c2bc6 | 2018-10-16 10:26:11 -0700 | [diff] [blame] | 98 | |
| 99 | /* |
| 100 | * Helper class to manage registered PendingIntent requests from the client. |
| 101 | */ |
| 102 | private class PendingIntentRequest { |
| 103 | /* |
| 104 | * The PendingIntent object to request, null if there is no open request. |
| 105 | */ |
| 106 | private PendingIntent mPendingIntent; |
| 107 | |
| 108 | /* |
| 109 | * The ID of the nanoapp the request is for, invalid if there is no open request. |
| 110 | */ |
| 111 | private long mNanoAppId; |
| 112 | |
| 113 | PendingIntentRequest() {} |
| 114 | |
| 115 | PendingIntentRequest(PendingIntent pendingIntent, long nanoAppId) { |
| 116 | mPendingIntent = pendingIntent; |
| 117 | mNanoAppId = nanoAppId; |
| 118 | } |
| 119 | |
| 120 | public long getNanoAppId() { |
| 121 | return mNanoAppId; |
| 122 | } |
| 123 | |
| 124 | public PendingIntent getPendingIntent() { |
| 125 | return mPendingIntent; |
| 126 | } |
| 127 | |
| 128 | public boolean hasPendingIntent() { |
| 129 | return mPendingIntent != null; |
| 130 | } |
| 131 | |
| 132 | public void clear() { |
| 133 | mPendingIntent = null; |
| 134 | } |
Arthur Ishiguro | 32c2bc6 | 2018-10-16 10:26:11 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 137 | /* package */ ContextHubClientBroker( |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 138 | Context context, IContexthub contextHubProxy, ContextHubClientManager clientManager, |
Arthur Ishiguro | b549286 | 2018-11-13 10:51:15 -0800 | [diff] [blame] | 139 | ContextHubInfo contextHubInfo, short hostEndPointId, |
| 140 | IContextHubClientCallback callback) { |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 141 | mContext = context; |
| 142 | mContextHubProxy = contextHubProxy; |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 143 | mClientManager = clientManager; |
Arthur Ishiguro | 622ebcb | 2018-10-17 14:02:27 -0700 | [diff] [blame] | 144 | mAttachedContextHubInfo = contextHubInfo; |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 145 | mHostEndPointId = hostEndPointId; |
Arthur Ishiguro | b549286 | 2018-11-13 10:51:15 -0800 | [diff] [blame] | 146 | mCallbackInterface = callback; |
Arthur Ishiguro | 82ed3af | 2018-11-09 13:20:30 -0800 | [diff] [blame] | 147 | mPendingIntentRequest = new PendingIntentRequest(); |
| 148 | } |
| 149 | |
| 150 | /* package */ ContextHubClientBroker( |
| 151 | Context context, IContexthub contextHubProxy, ContextHubClientManager clientManager, |
| 152 | ContextHubInfo contextHubInfo, short hostEndPointId, PendingIntent pendingIntent, |
| 153 | long nanoAppId) { |
| 154 | mContext = context; |
| 155 | mContextHubProxy = contextHubProxy; |
| 156 | mClientManager = clientManager; |
| 157 | mAttachedContextHubInfo = contextHubInfo; |
| 158 | mHostEndPointId = hostEndPointId; |
| 159 | mPendingIntentRequest = new PendingIntentRequest(pendingIntent, nanoAppId); |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 163 | * Sends from this client to a nanoapp. |
| 164 | * |
| 165 | * @param message the message to send |
| 166 | * @return the error code of sending the message |
| 167 | */ |
| 168 | @ContextHubTransaction.Result |
| 169 | @Override |
| 170 | public int sendMessageToNanoApp(NanoAppMessage message) { |
| 171 | ContextHubServiceUtil.checkPermissions(mContext); |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 172 | |
| 173 | int result; |
Arthur Ishiguro | a96fbfe | 2018-11-13 10:00:35 -0800 | [diff] [blame] | 174 | if (isRegistered()) { |
Arthur Ishiguro | daeed3c | 2018-10-01 15:42:27 -0700 | [diff] [blame] | 175 | ContextHubMsg messageToNanoApp = |
| 176 | ContextHubServiceUtil.createHidlContextHubMessage(mHostEndPointId, message); |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 177 | |
Arthur Ishiguro | 622ebcb | 2018-10-17 14:02:27 -0700 | [diff] [blame] | 178 | int contextHubId = mAttachedContextHubInfo.getId(); |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 179 | try { |
Arthur Ishiguro | 622ebcb | 2018-10-17 14:02:27 -0700 | [diff] [blame] | 180 | result = mContextHubProxy.sendMessageToHub(contextHubId, messageToNanoApp); |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 181 | } catch (RemoteException e) { |
| 182 | Log.e(TAG, "RemoteException in sendMessageToNanoApp (target hub ID = " |
Arthur Ishiguro | 622ebcb | 2018-10-17 14:02:27 -0700 | [diff] [blame] | 183 | + contextHubId + ")", e); |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 184 | result = Result.UNKNOWN_FAILURE; |
| 185 | } |
| 186 | } else { |
| 187 | Log.e(TAG, "Failed to send message to nanoapp: client connection is closed"); |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 188 | result = Result.UNKNOWN_FAILURE; |
| 189 | } |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 190 | |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 191 | return ContextHubServiceUtil.toTransactionResult(result); |
| 192 | } |
| 193 | |
| 194 | /** |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 195 | * Closes the connection for this client with the service. |
Arthur Ishiguro | 82ed3af | 2018-11-09 13:20:30 -0800 | [diff] [blame] | 196 | * |
| 197 | * If the client has a PendingIntent registered, this method also unregisters it. |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 198 | */ |
| 199 | @Override |
| 200 | public void close() { |
Arthur Ishiguro | daeed3c | 2018-10-01 15:42:27 -0700 | [diff] [blame] | 201 | synchronized (this) { |
Arthur Ishiguro | 82ed3af | 2018-11-09 13:20:30 -0800 | [diff] [blame] | 202 | mPendingIntentRequest.clear(); |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 203 | } |
Arthur Ishiguro | 82ed3af | 2018-11-09 13:20:30 -0800 | [diff] [blame] | 204 | onClientExit(); |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Invoked when the underlying binder of this broker has died at the client process. |
| 209 | */ |
Arthur Ishiguro | 9f6284e | 2018-10-10 11:09:02 -0700 | [diff] [blame] | 210 | @Override |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 211 | public void binderDied() { |
Arthur Ishiguro | 82ed3af | 2018-11-09 13:20:30 -0800 | [diff] [blame] | 212 | onClientExit(); |
Arthur Ishiguro | 49e030f | 2017-11-16 11:54:42 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 216 | * @return the ID of the context hub this client is attached to |
| 217 | */ |
| 218 | /* package */ int getAttachedContextHubId() { |
Arthur Ishiguro | 622ebcb | 2018-10-17 14:02:27 -0700 | [diff] [blame] | 219 | return mAttachedContextHubInfo.getId(); |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /** |
| 223 | * @return the host endpoint ID of this client |
| 224 | */ |
| 225 | /* package */ short getHostEndPointId() { |
| 226 | return mHostEndPointId; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Sends a message to the client associated with this object. |
| 231 | * |
| 232 | * @param message the message that came from a nanoapp |
| 233 | */ |
| 234 | /* package */ void sendMessageToClient(NanoAppMessage message) { |
Arthur Ishiguro | daeed3c | 2018-10-01 15:42:27 -0700 | [diff] [blame] | 235 | invokeCallback(callback -> callback.onMessageFromNanoApp(message)); |
Arthur Ishiguro | 9aa88e0 | 2018-10-16 10:26:57 -0700 | [diff] [blame] | 236 | |
| 237 | Supplier<Intent> supplier = |
| 238 | () -> createIntent(ContextHubManager.EVENT_NANOAPP_MESSAGE, message.getNanoAppId()) |
| 239 | .putExtra(ContextHubManager.EXTRA_MESSAGE, message); |
| 240 | sendPendingIntent(supplier); |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 241 | } |
Arthur Ishiguro | 6d47c54 | 2017-11-17 15:49:07 -0800 | [diff] [blame] | 242 | |
| 243 | /** |
Arthur Ishiguro | 02ff50b | 2017-12-18 10:02:35 -0800 | [diff] [blame] | 244 | * Notifies the client of a nanoapp load event if the connection is open. |
Arthur Ishiguro | 0069f12 | 2017-11-20 15:07:14 -0800 | [diff] [blame] | 245 | * |
| 246 | * @param nanoAppId the ID of the nanoapp that was loaded. |
| 247 | */ |
| 248 | /* package */ void onNanoAppLoaded(long nanoAppId) { |
Arthur Ishiguro | daeed3c | 2018-10-01 15:42:27 -0700 | [diff] [blame] | 249 | invokeCallback(callback -> callback.onNanoAppLoaded(nanoAppId)); |
Arthur Ishiguro | 9aa88e0 | 2018-10-16 10:26:57 -0700 | [diff] [blame] | 250 | sendPendingIntent(() -> createIntent(ContextHubManager.EVENT_NANOAPP_LOADED, nanoAppId)); |
Arthur Ishiguro | 0069f12 | 2017-11-20 15:07:14 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /** |
Arthur Ishiguro | 02ff50b | 2017-12-18 10:02:35 -0800 | [diff] [blame] | 254 | * Notifies the client of a nanoapp unload event if the connection is open. |
Arthur Ishiguro | 0069f12 | 2017-11-20 15:07:14 -0800 | [diff] [blame] | 255 | * |
| 256 | * @param nanoAppId the ID of the nanoapp that was unloaded. |
| 257 | */ |
| 258 | /* package */ void onNanoAppUnloaded(long nanoAppId) { |
Arthur Ishiguro | daeed3c | 2018-10-01 15:42:27 -0700 | [diff] [blame] | 259 | invokeCallback(callback -> callback.onNanoAppUnloaded(nanoAppId)); |
Arthur Ishiguro | 9aa88e0 | 2018-10-16 10:26:57 -0700 | [diff] [blame] | 260 | sendPendingIntent(() -> createIntent(ContextHubManager.EVENT_NANOAPP_UNLOADED, nanoAppId)); |
Arthur Ishiguro | 0069f12 | 2017-11-20 15:07:14 -0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
Arthur Ishiguro | 02ff50b | 2017-12-18 10:02:35 -0800 | [diff] [blame] | 264 | * Notifies the client of a hub reset event if the connection is open. |
Arthur Ishiguro | 6d47c54 | 2017-11-17 15:49:07 -0800 | [diff] [blame] | 265 | */ |
| 266 | /* package */ void onHubReset() { |
Arthur Ishiguro | daeed3c | 2018-10-01 15:42:27 -0700 | [diff] [blame] | 267 | invokeCallback(callback -> callback.onHubReset()); |
Arthur Ishiguro | 9aa88e0 | 2018-10-16 10:26:57 -0700 | [diff] [blame] | 268 | sendPendingIntent(() -> createIntent(ContextHubManager.EVENT_HUB_RESET)); |
Arthur Ishiguro | 6d47c54 | 2017-11-17 15:49:07 -0800 | [diff] [blame] | 269 | } |
Arthur Ishiguro | 02ff50b | 2017-12-18 10:02:35 -0800 | [diff] [blame] | 270 | |
| 271 | /** |
| 272 | * Notifies the client of a nanoapp abort event if the connection is open. |
| 273 | * |
| 274 | * @param nanoAppId the ID of the nanoapp that aborted |
| 275 | * @param abortCode the nanoapp specific abort code |
| 276 | */ |
| 277 | /* package */ void onNanoAppAborted(long nanoAppId, int abortCode) { |
Arthur Ishiguro | daeed3c | 2018-10-01 15:42:27 -0700 | [diff] [blame] | 278 | invokeCallback(callback -> callback.onNanoAppAborted(nanoAppId, abortCode)); |
Arthur Ishiguro | 9aa88e0 | 2018-10-16 10:26:57 -0700 | [diff] [blame] | 279 | |
| 280 | Supplier<Intent> supplier = |
| 281 | () -> createIntent(ContextHubManager.EVENT_NANOAPP_ABORTED, nanoAppId) |
| 282 | .putExtra(ContextHubManager.EXTRA_NANOAPP_ABORT_CODE, abortCode); |
| 283 | sendPendingIntent(supplier); |
Arthur Ishiguro | 661f502 | 2018-10-10 12:17:29 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /** |
Arthur Ishiguro | 82ed3af | 2018-11-09 13:20:30 -0800 | [diff] [blame] | 287 | * @param intent the PendingIntent to compare to |
| 288 | * @param nanoAppId the ID of the nanoapp of the PendingIntent to compare to |
Arthur Ishiguro | d3464c7 | 2018-10-16 11:08:28 -0700 | [diff] [blame] | 289 | * @return true if the given PendingIntent is currently registered, false otherwise |
| 290 | */ |
Arthur Ishiguro | 82ed3af | 2018-11-09 13:20:30 -0800 | [diff] [blame] | 291 | /* package */ boolean hasPendingIntent(PendingIntent intent, long nanoAppId) { |
Arthur Ishiguro | d3464c7 | 2018-10-16 11:08:28 -0700 | [diff] [blame] | 292 | PendingIntent pendingIntent = null; |
Arthur Ishiguro | 82ed3af | 2018-11-09 13:20:30 -0800 | [diff] [blame] | 293 | long intentNanoAppId; |
Arthur Ishiguro | d3464c7 | 2018-10-16 11:08:28 -0700 | [diff] [blame] | 294 | synchronized (this) { |
| 295 | pendingIntent = mPendingIntentRequest.getPendingIntent(); |
Arthur Ishiguro | 82ed3af | 2018-11-09 13:20:30 -0800 | [diff] [blame] | 296 | intentNanoAppId = mPendingIntentRequest.getNanoAppId(); |
Arthur Ishiguro | d3464c7 | 2018-10-16 11:08:28 -0700 | [diff] [blame] | 297 | } |
Arthur Ishiguro | 82ed3af | 2018-11-09 13:20:30 -0800 | [diff] [blame] | 298 | return (pendingIntent != null) && pendingIntent.equals(intent) |
| 299 | && intentNanoAppId == nanoAppId; |
Arthur Ishiguro | d3464c7 | 2018-10-16 11:08:28 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /** |
Arthur Ishiguro | b549286 | 2018-11-13 10:51:15 -0800 | [diff] [blame] | 303 | * Attaches the death recipient to the callback interface object, if any. |
| 304 | * |
| 305 | * @throws RemoteException if the client process already died |
| 306 | */ |
| 307 | /* package */ void attachDeathRecipient() throws RemoteException { |
| 308 | if (mCallbackInterface != null) { |
| 309 | mCallbackInterface.asBinder().linkToDeath(this, 0 /* flags */); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /** |
Arthur Ishiguro | 661f502 | 2018-10-10 12:17:29 -0700 | [diff] [blame] | 314 | * Helper function to invoke a specified client callback, if the connection is open. |
| 315 | * |
| 316 | * @param consumer the consumer specifying the callback to invoke |
| 317 | */ |
Arthur Ishiguro | daeed3c | 2018-10-01 15:42:27 -0700 | [diff] [blame] | 318 | private synchronized void invokeCallback(CallbackConsumer consumer) { |
| 319 | if (mCallbackInterface != null) { |
Arthur Ishiguro | 02ff50b | 2017-12-18 10:02:35 -0800 | [diff] [blame] | 320 | try { |
Arthur Ishiguro | 661f502 | 2018-10-10 12:17:29 -0700 | [diff] [blame] | 321 | consumer.accept(mCallbackInterface); |
Arthur Ishiguro | 02ff50b | 2017-12-18 10:02:35 -0800 | [diff] [blame] | 322 | } catch (RemoteException e) { |
Arthur Ishiguro | 661f502 | 2018-10-10 12:17:29 -0700 | [diff] [blame] | 323 | Log.e(TAG, "RemoteException while invoking client callback (host endpoint ID = " |
| 324 | + mHostEndPointId + ")", e); |
Arthur Ishiguro | 02ff50b | 2017-12-18 10:02:35 -0800 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | } |
Arthur Ishiguro | 9aa88e0 | 2018-10-16 10:26:57 -0700 | [diff] [blame] | 328 | |
| 329 | /** |
| 330 | * Creates an Intent object containing the ContextHubManager.EXTRA_EVENT_TYPE extra field |
| 331 | * |
| 332 | * @param eventType the ContextHubManager.Event type describing the event |
| 333 | * @return the Intent object |
| 334 | */ |
| 335 | private Intent createIntent(int eventType) { |
| 336 | Intent intent = new Intent(); |
| 337 | intent.putExtra(ContextHubManager.EXTRA_EVENT_TYPE, eventType); |
| 338 | intent.putExtra(ContextHubManager.EXTRA_CONTEXT_HUB_INFO, mAttachedContextHubInfo); |
| 339 | return intent; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Creates an Intent object containing the ContextHubManager.EXTRA_EVENT_TYPE and the |
| 344 | * ContextHubManager.EXTRA_NANOAPP_ID extra fields |
| 345 | * |
| 346 | * @param eventType the ContextHubManager.Event type describing the event |
| 347 | * @param nanoAppId the ID of the nanoapp this event is for |
| 348 | * @return the Intent object |
| 349 | */ |
| 350 | private Intent createIntent(int eventType, long nanoAppId) { |
| 351 | Intent intent = createIntent(eventType); |
| 352 | intent.putExtra(ContextHubManager.EXTRA_NANOAPP_ID, nanoAppId); |
| 353 | return intent; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Sends an intent to any existing PendingIntent |
| 358 | * |
| 359 | * @param supplier method to create the extra Intent |
| 360 | */ |
| 361 | private synchronized void sendPendingIntent(Supplier<Intent> supplier) { |
| 362 | if (mPendingIntentRequest.hasPendingIntent()) { |
| 363 | Intent intent = supplier.get(); |
| 364 | try { |
| 365 | mPendingIntentRequest.getPendingIntent().send( |
| 366 | mContext, 0 /* code */, intent, null /* onFinished */, null /* Handler */, |
| 367 | Manifest.permission.LOCATION_HARDWARE /* requiredPermission */, |
| 368 | null /* options */); |
| 369 | } catch (PendingIntent.CanceledException e) { |
| 370 | // The PendingIntent is no longer valid |
| 371 | Log.w(TAG, "PendingIntent has been canceled, unregistering from client" |
| 372 | + " (host endpoint ID " + mHostEndPointId + ")"); |
Arthur Ishiguro | 82ed3af | 2018-11-09 13:20:30 -0800 | [diff] [blame] | 373 | close(); |
Arthur Ishiguro | 9aa88e0 | 2018-10-16 10:26:57 -0700 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | } |
Arthur Ishiguro | a96fbfe | 2018-11-13 10:00:35 -0800 | [diff] [blame] | 377 | |
| 378 | /** |
| 379 | * @return true if the client is still registered with the service, false otherwise |
| 380 | */ |
| 381 | private synchronized boolean isRegistered() { |
| 382 | return mRegistered; |
| 383 | } |
Arthur Ishiguro | 82ed3af | 2018-11-09 13:20:30 -0800 | [diff] [blame] | 384 | |
| 385 | /** |
| 386 | * Invoked when a client exits either explicitly or by binder death. |
| 387 | */ |
| 388 | private synchronized void onClientExit() { |
| 389 | if (mCallbackInterface != null) { |
| 390 | mCallbackInterface.asBinder().unlinkToDeath(this, 0 /* flags */); |
| 391 | mCallbackInterface = null; |
| 392 | } |
| 393 | if (!mPendingIntentRequest.hasPendingIntent() && mRegistered) { |
| 394 | mClientManager.unregisterClient(mHostEndPointId); |
| 395 | mRegistered = false; |
| 396 | } |
| 397 | } |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 398 | } |