Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.attention; |
| 18 | |
Matt Pape | aea2831 | 2019-03-07 13:51:38 -0800 | [diff] [blame] | 19 | import static android.provider.DeviceConfig.NAMESPACE_ATTENTION_MANAGER_SERVICE; |
Alex Salo | eeba023 | 2019-03-14 18:56:54 -0700 | [diff] [blame] | 20 | import static android.service.attention.AttentionService.ATTENTION_FAILURE_CANCELLED; |
Alex Salo | 1080034 | 2019-04-02 18:45:20 -0700 | [diff] [blame] | 21 | import static android.service.attention.AttentionService.ATTENTION_FAILURE_UNKNOWN; |
Alex Salo | 440fe3d | 2019-01-25 11:50:38 -0800 | [diff] [blame] | 22 | |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 23 | import android.Manifest; |
| 24 | import android.annotation.Nullable; |
| 25 | import android.annotation.UserIdInt; |
| 26 | import android.app.ActivityManager; |
| 27 | import android.attention.AttentionManagerInternal; |
| 28 | import android.attention.AttentionManagerInternal.AttentionCallbackInternal; |
| 29 | import android.content.BroadcastReceiver; |
| 30 | import android.content.ComponentName; |
| 31 | import android.content.Context; |
| 32 | import android.content.Intent; |
| 33 | import android.content.IntentFilter; |
| 34 | import android.content.ServiceConnection; |
| 35 | import android.content.pm.PackageManager; |
| 36 | import android.content.pm.ResolveInfo; |
| 37 | import android.content.pm.ServiceInfo; |
| 38 | import android.os.Binder; |
| 39 | import android.os.Handler; |
| 40 | import android.os.IBinder; |
| 41 | import android.os.Looper; |
| 42 | import android.os.Message; |
| 43 | import android.os.PowerManager; |
| 44 | import android.os.RemoteException; |
Aaron Li | 7565f8b | 2019-04-22 18:24:39 -0700 | [diff] [blame] | 45 | import android.os.ResultReceiver; |
| 46 | import android.os.ShellCallback; |
| 47 | import android.os.ShellCommand; |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 48 | import android.os.SystemClock; |
| 49 | import android.os.UserHandle; |
Alex Salo | 440fe3d | 2019-01-25 11:50:38 -0800 | [diff] [blame] | 50 | import android.provider.DeviceConfig; |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 51 | import android.service.attention.AttentionService; |
| 52 | import android.service.attention.AttentionService.AttentionFailureCodes; |
Alex Salo | 6382930 | 2019-04-06 14:57:30 -0700 | [diff] [blame] | 53 | import android.service.attention.AttentionService.AttentionSuccessCodes; |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 54 | import android.service.attention.IAttentionCallback; |
| 55 | import android.service.attention.IAttentionService; |
| 56 | import android.text.TextUtils; |
| 57 | import android.util.Slog; |
| 58 | import android.util.SparseArray; |
Alex Salo | a060aee | 2019-01-21 14:36:41 -0800 | [diff] [blame] | 59 | import android.util.StatsLog; |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 60 | |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 61 | import com.android.internal.annotations.GuardedBy; |
Yi Jiang | f9c9c16 | 2019-04-10 13:36:27 -0700 | [diff] [blame] | 62 | import com.android.internal.annotations.VisibleForTesting; |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 63 | import com.android.internal.util.DumpUtils; |
| 64 | import com.android.internal.util.IndentingPrintWriter; |
| 65 | import com.android.internal.util.Preconditions; |
| 66 | import com.android.server.SystemService; |
| 67 | |
Alex Salo | 5f5b5f4 | 2019-02-27 10:49:00 -0800 | [diff] [blame] | 68 | import java.io.FileDescriptor; |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 69 | import java.io.PrintWriter; |
| 70 | |
| 71 | /** |
| 72 | * An attention service implementation that runs in System Server process. |
| 73 | * This service publishes a LocalService and reroutes calls to a {@link AttentionService} that it |
| 74 | * manages. |
| 75 | */ |
| 76 | public class AttentionManagerService extends SystemService { |
| 77 | private static final String LOG_TAG = "AttentionManagerService"; |
Alex Salo | 6382930 | 2019-04-06 14:57:30 -0700 | [diff] [blame] | 78 | private static final boolean DEBUG = false; |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 79 | |
Alex Salo | 440fe3d | 2019-01-25 11:50:38 -0800 | [diff] [blame] | 80 | /** Default value in absence of {@link DeviceConfig} override. */ |
| 81 | private static final boolean DEFAULT_SERVICE_ENABLED = true; |
| 82 | |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 83 | /** Service will unbind if connection is not used for that amount of time. */ |
| 84 | private static final long CONNECTION_TTL_MILLIS = 60_000; |
| 85 | |
| 86 | /** If the check attention called within that period - cached value will be returned. */ |
| 87 | private static final long STALE_AFTER_MILLIS = 5_000; |
| 88 | |
Matt Pape | aea2831 | 2019-03-07 13:51:38 -0800 | [diff] [blame] | 89 | /** DeviceConfig flag name, if {@code true}, enables AttentionManagerService features. */ |
| 90 | private static final String SERVICE_ENABLED = "service_enabled"; |
Aaron Li | 7565f8b | 2019-04-22 18:24:39 -0700 | [diff] [blame] | 91 | private static String sTestAttentionServicePackage; |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 92 | private final Context mContext; |
| 93 | private final PowerManager mPowerManager; |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 94 | private final Object mLock; |
| 95 | @GuardedBy("mLock") |
| 96 | private final SparseArray<UserState> mUserStates = new SparseArray<>(); |
Yi Jiang | f9c9c16 | 2019-04-10 13:36:27 -0700 | [diff] [blame] | 97 | private AttentionHandler mAttentionHandler; |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 98 | |
Yi Jiang | f9c9c16 | 2019-04-10 13:36:27 -0700 | [diff] [blame] | 99 | @VisibleForTesting |
| 100 | ComponentName mComponentName; |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 101 | |
| 102 | public AttentionManagerService(Context context) { |
Yi Jiang | f9c9c16 | 2019-04-10 13:36:27 -0700 | [diff] [blame] | 103 | this(context, (PowerManager) context.getSystemService(Context.POWER_SERVICE), |
| 104 | new Object(), null); |
| 105 | mAttentionHandler = new AttentionHandler(); |
| 106 | } |
| 107 | |
| 108 | @VisibleForTesting |
| 109 | AttentionManagerService(Context context, PowerManager powerManager, Object lock, |
| 110 | AttentionHandler handler) { |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 111 | super(context); |
| 112 | mContext = Preconditions.checkNotNull(context); |
Yi Jiang | f9c9c16 | 2019-04-10 13:36:27 -0700 | [diff] [blame] | 113 | mPowerManager = powerManager; |
| 114 | mLock = lock; |
| 115 | mAttentionHandler = handler; |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
Alex Salo | 207e92d | 2019-05-20 15:59:48 -0700 | [diff] [blame] | 119 | public void onBootPhase(int phase) { |
| 120 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
| 121 | mContext.registerReceiver(new ScreenStateReceiver(), |
| 122 | new IntentFilter(Intent.ACTION_SCREEN_OFF)); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | @Override |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 127 | public void onStart() { |
Alex Salo | 5f5b5f4 | 2019-02-27 10:49:00 -0800 | [diff] [blame] | 128 | publishBinderService(Context.ATTENTION_SERVICE, new BinderService()); |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 129 | publishLocalService(AttentionManagerInternal.class, new LocalService()); |
| 130 | } |
| 131 | |
| 132 | @Override |
Alex Salo | cde8430 | 2019-01-21 15:31:07 -0800 | [diff] [blame] | 133 | public void onSwitchUser(int userId) { |
Alex Salo | 1e32c07 | 2019-02-13 16:58:01 -0800 | [diff] [blame] | 134 | cancelAndUnbindLocked(peekUserStateLocked(userId)); |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Alex Salo | 7a6e3a6 | 2019-02-27 15:08:15 -0800 | [diff] [blame] | 137 | /** Returns {@code true} if attention service is configured on this device. */ |
| 138 | public static boolean isServiceConfigured(Context context) { |
Aaron Li | 7565f8b | 2019-04-22 18:24:39 -0700 | [diff] [blame] | 139 | return !TextUtils.isEmpty(getServiceConfigPackage(context)); |
Alex Salo | 7a6e3a6 | 2019-02-27 15:08:15 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Alex Salo | 50b701e | 2019-02-01 13:34:24 -0800 | [diff] [blame] | 142 | /** Resolves and sets up the attention service if it had not been done yet. */ |
| 143 | private boolean isServiceAvailable() { |
| 144 | if (mComponentName == null) { |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 145 | mComponentName = resolveAttentionService(mContext); |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 146 | } |
Alex Salo | 50b701e | 2019-02-01 13:34:24 -0800 | [diff] [blame] | 147 | return mComponentName != null; |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Returns {@code true} if attention service is supported on this device. |
| 152 | */ |
Yi Jiang | 99a8479 | 2019-03-13 15:32:48 -0700 | [diff] [blame] | 153 | private boolean isAttentionServiceSupported() { |
Alex Salo | 50b701e | 2019-02-01 13:34:24 -0800 | [diff] [blame] | 154 | return isServiceEnabled() && isServiceAvailable(); |
Alex Salo | 440fe3d | 2019-01-25 11:50:38 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Yi Jiang | f9c9c16 | 2019-04-10 13:36:27 -0700 | [diff] [blame] | 157 | @VisibleForTesting |
| 158 | protected boolean isServiceEnabled() { |
Matt Pape | aea2831 | 2019-03-07 13:51:38 -0800 | [diff] [blame] | 159 | return DeviceConfig.getBoolean(NAMESPACE_ATTENTION_MANAGER_SERVICE, SERVICE_ENABLED, |
| 160 | DEFAULT_SERVICE_ENABLED); |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Checks whether user attention is at the screen and calls in the provided callback. |
| 165 | * |
Alex Salo | 6382930 | 2019-04-06 14:57:30 -0700 | [diff] [blame] | 166 | * Calling this multiple times quickly in a row will result in either a) returning a cached |
| 167 | * value, if present, or b) returning {@code false} because only one active request at a time is |
| 168 | * allowed. |
| 169 | * |
| 170 | * @return {@code true} if the framework was able to dispatch the request |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 171 | */ |
Yi Jiang | f9c9c16 | 2019-04-10 13:36:27 -0700 | [diff] [blame] | 172 | @VisibleForTesting |
| 173 | boolean checkAttention(long timeout, AttentionCallbackInternal callbackInternal) { |
Alex Salo | eeba023 | 2019-03-14 18:56:54 -0700 | [diff] [blame] | 174 | Preconditions.checkNotNull(callbackInternal); |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 175 | |
| 176 | if (!isAttentionServiceSupported()) { |
| 177 | Slog.w(LOG_TAG, "Trying to call checkAttention() on an unsupported device."); |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | // don't allow attention check in screen off state |
| 182 | if (!mPowerManager.isInteractive()) { |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | synchronized (mLock) { |
Alex Salo | 1e32c07 | 2019-02-13 16:58:01 -0800 | [diff] [blame] | 187 | final long now = SystemClock.uptimeMillis(); |
Alex Salo | eeba023 | 2019-03-14 18:56:54 -0700 | [diff] [blame] | 188 | // schedule shutting down the connection if no one resets this timer |
Alex Salo | 1e32c07 | 2019-02-13 16:58:01 -0800 | [diff] [blame] | 189 | freeIfInactiveLocked(); |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 190 | |
| 191 | final UserState userState = getOrCreateCurrentUserStateLocked(); |
| 192 | // lazily start the service, which should be very lightweight to start |
Alex Salo | 042e43a | 2019-05-30 15:24:54 -0700 | [diff] [blame] | 193 | userState.bindLocked(); |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 194 | |
Alex Salo | 6382930 | 2019-04-06 14:57:30 -0700 | [diff] [blame] | 195 | // throttle frequent requests |
| 196 | final AttentionCheckCache cache = userState.mAttentionCheckCache; |
| 197 | if (cache != null && now < cache.mLastComputed + STALE_AFTER_MILLIS) { |
| 198 | callbackInternal.onSuccess(cache.mResult, cache.mTimestamp); |
| 199 | return true; |
| 200 | } |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 201 | |
Alex Salo | 6382930 | 2019-04-06 14:57:30 -0700 | [diff] [blame] | 202 | // prevent spamming with multiple requests, only one at a time is allowed |
| 203 | if (userState.mCurrentAttentionCheck != null) { |
| 204 | if (!userState.mCurrentAttentionCheck.mIsDispatched |
| 205 | || !userState.mCurrentAttentionCheck.mIsFulfilled) { |
| 206 | return false; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | userState.mCurrentAttentionCheck = createAttentionCheck(callbackInternal, userState); |
| 211 | |
| 212 | if (userState.mService != null) { |
| 213 | try { |
Alex Salo | eeba023 | 2019-03-14 18:56:54 -0700 | [diff] [blame] | 214 | // schedule request cancellation if not returned by that point yet |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 215 | cancelAfterTimeoutLocked(timeout); |
Alex Salo | eeba023 | 2019-03-14 18:56:54 -0700 | [diff] [blame] | 216 | userState.mService.checkAttention( |
| 217 | userState.mCurrentAttentionCheck.mIAttentionCallback); |
Alex Salo | 6382930 | 2019-04-06 14:57:30 -0700 | [diff] [blame] | 218 | userState.mCurrentAttentionCheck.mIsDispatched = true; |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 219 | } catch (RemoteException e) { |
| 220 | Slog.e(LOG_TAG, "Cannot call into the AttentionService"); |
| 221 | return false; |
| 222 | } |
| 223 | } |
| 224 | return true; |
| 225 | } |
| 226 | } |
| 227 | |
Alex Salo | 6382930 | 2019-04-06 14:57:30 -0700 | [diff] [blame] | 228 | private AttentionCheck createAttentionCheck(AttentionCallbackInternal callbackInternal, |
| 229 | UserState userState) { |
| 230 | final IAttentionCallback iAttentionCallback = new IAttentionCallback.Stub() { |
| 231 | @Override |
| 232 | public void onSuccess(@AttentionSuccessCodes int result, long timestamp) { |
| 233 | // the callback might have been cancelled already |
| 234 | if (!userState.mCurrentAttentionCheck.mIsFulfilled) { |
| 235 | callbackInternal.onSuccess(result, timestamp); |
| 236 | userState.mCurrentAttentionCheck.mIsFulfilled = true; |
| 237 | } |
| 238 | |
| 239 | synchronized (mLock) { |
| 240 | userState.mAttentionCheckCache = new AttentionCheckCache( |
| 241 | SystemClock.uptimeMillis(), result, |
| 242 | timestamp); |
| 243 | } |
| 244 | StatsLog.write( |
| 245 | StatsLog.ATTENTION_MANAGER_SERVICE_RESULT_REPORTED, |
| 246 | result); |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public void onFailure(@AttentionFailureCodes int error) { |
| 251 | // the callback might have been cancelled already |
| 252 | if (!userState.mCurrentAttentionCheck.mIsFulfilled) { |
| 253 | callbackInternal.onFailure(error); |
| 254 | userState.mCurrentAttentionCheck.mIsFulfilled = true; |
| 255 | } |
| 256 | |
| 257 | StatsLog.write( |
| 258 | StatsLog.ATTENTION_MANAGER_SERVICE_RESULT_REPORTED, |
| 259 | error); |
| 260 | } |
| 261 | }; |
| 262 | |
| 263 | return new AttentionCheck(callbackInternal, iAttentionCallback); |
| 264 | } |
| 265 | |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 266 | /** Cancels the specified attention check. */ |
Yi Jiang | f9c9c16 | 2019-04-10 13:36:27 -0700 | [diff] [blame] | 267 | @VisibleForTesting |
| 268 | void cancelAttentionCheck(AttentionCallbackInternal callbackInternal) { |
Alex Salo | cde8430 | 2019-01-21 15:31:07 -0800 | [diff] [blame] | 269 | synchronized (mLock) { |
Alex Salo | 1e32c07 | 2019-02-13 16:58:01 -0800 | [diff] [blame] | 270 | final UserState userState = peekCurrentUserStateLocked(); |
| 271 | if (userState == null) { |
| 272 | return; |
| 273 | } |
Alex Salo | 6382930 | 2019-04-06 14:57:30 -0700 | [diff] [blame] | 274 | if (!userState.mCurrentAttentionCheck.mCallbackInternal.equals(callbackInternal)) { |
Alex Salo | cbe610a | 2019-05-01 14:18:46 -0700 | [diff] [blame] | 275 | Slog.w(LOG_TAG, "Cannot cancel a non-current request"); |
Alex Salo | cde8430 | 2019-01-21 15:31:07 -0800 | [diff] [blame] | 276 | return; |
| 277 | } |
Alex Salo | 6382930 | 2019-04-06 14:57:30 -0700 | [diff] [blame] | 278 | cancel(userState); |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | @GuardedBy("mLock") |
Yi Jiang | f9c9c16 | 2019-04-10 13:36:27 -0700 | [diff] [blame] | 283 | @VisibleForTesting |
| 284 | protected void freeIfInactiveLocked() { |
Alex Salo | 1e32c07 | 2019-02-13 16:58:01 -0800 | [diff] [blame] | 285 | // If we are called here, it means someone used the API again - reset the timer then. |
| 286 | mAttentionHandler.removeMessages(AttentionHandler.CHECK_CONNECTION_EXPIRATION); |
| 287 | |
| 288 | // Schedule resources cleanup if no one calls the API again. |
| 289 | mAttentionHandler.sendEmptyMessageDelayed(AttentionHandler.CHECK_CONNECTION_EXPIRATION, |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 290 | CONNECTION_TTL_MILLIS); |
| 291 | } |
| 292 | |
| 293 | @GuardedBy("mLock") |
| 294 | private void cancelAfterTimeoutLocked(long timeout) { |
| 295 | mAttentionHandler.sendEmptyMessageDelayed(AttentionHandler.ATTENTION_CHECK_TIMEOUT, |
| 296 | timeout); |
| 297 | } |
| 298 | |
| 299 | |
| 300 | @GuardedBy("mLock") |
Yi Jiang | f9c9c16 | 2019-04-10 13:36:27 -0700 | [diff] [blame] | 301 | @VisibleForTesting |
| 302 | protected UserState getOrCreateCurrentUserStateLocked() { |
Alex Salo | cde8430 | 2019-01-21 15:31:07 -0800 | [diff] [blame] | 303 | return getOrCreateUserStateLocked(ActivityManager.getCurrentUser()); |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | @GuardedBy("mLock") |
Yi Jiang | fa323e7 | 2019-04-16 17:23:11 -0700 | [diff] [blame] | 307 | @VisibleForTesting |
| 308 | protected UserState getOrCreateUserStateLocked(int userId) { |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 309 | UserState result = mUserStates.get(userId); |
| 310 | if (result == null) { |
Alex Salo | 042e43a | 2019-05-30 15:24:54 -0700 | [diff] [blame] | 311 | result = new UserState(userId, mContext, mLock, mAttentionHandler, mComponentName); |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 312 | mUserStates.put(userId, result); |
| 313 | } |
| 314 | return result; |
| 315 | } |
| 316 | |
| 317 | @GuardedBy("mLock") |
Alex Salo | 1e32c07 | 2019-02-13 16:58:01 -0800 | [diff] [blame] | 318 | @Nullable |
Yi Jiang | f9c9c16 | 2019-04-10 13:36:27 -0700 | [diff] [blame] | 319 | @VisibleForTesting |
| 320 | protected UserState peekCurrentUserStateLocked() { |
Alex Salo | cde8430 | 2019-01-21 15:31:07 -0800 | [diff] [blame] | 321 | return peekUserStateLocked(ActivityManager.getCurrentUser()); |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | @GuardedBy("mLock") |
Alex Salo | 1e32c07 | 2019-02-13 16:58:01 -0800 | [diff] [blame] | 325 | @Nullable |
| 326 | private UserState peekUserStateLocked(int userId) { |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 327 | return mUserStates.get(userId); |
| 328 | } |
| 329 | |
Aaron Li | 7565f8b | 2019-04-22 18:24:39 -0700 | [diff] [blame] | 330 | private static String getServiceConfigPackage(Context context) { |
| 331 | return context.getPackageManager().getAttentionServicePackageName(); |
Alex Salo | 7a6e3a6 | 2019-02-27 15:08:15 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 334 | /** |
| 335 | * Provides attention service component name at runtime, making sure it's provided by the |
| 336 | * system. |
| 337 | */ |
| 338 | private static ComponentName resolveAttentionService(Context context) { |
Aaron Li | 7565f8b | 2019-04-22 18:24:39 -0700 | [diff] [blame] | 339 | final String serviceConfigPackage = getServiceConfigPackage(context); |
Alex Salo | 440fe3d | 2019-01-25 11:50:38 -0800 | [diff] [blame] | 340 | |
Aaron Li | 7565f8b | 2019-04-22 18:24:39 -0700 | [diff] [blame] | 341 | String resolvedPackage; |
| 342 | int flags = PackageManager.MATCH_SYSTEM_ONLY; |
| 343 | if (!TextUtils.isEmpty(sTestAttentionServicePackage)) { |
| 344 | resolvedPackage = sTestAttentionServicePackage; |
| 345 | flags = PackageManager.GET_META_DATA; |
| 346 | } else if (!TextUtils.isEmpty(serviceConfigPackage)) { |
| 347 | resolvedPackage = serviceConfigPackage; |
| 348 | } else { |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 349 | return null; |
| 350 | } |
| 351 | |
| 352 | final Intent intent = new Intent(AttentionService.SERVICE_INTERFACE).setPackage( |
Aaron Li | 7565f8b | 2019-04-22 18:24:39 -0700 | [diff] [blame] | 353 | resolvedPackage); |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 354 | |
Aaron Li | 7565f8b | 2019-04-22 18:24:39 -0700 | [diff] [blame] | 355 | final ResolveInfo resolveInfo = context.getPackageManager().resolveService(intent, flags); |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 356 | if (resolveInfo == null || resolveInfo.serviceInfo == null) { |
| 357 | Slog.wtf(LOG_TAG, String.format("Service %s not found in package %s", |
Aaron Li | 7565f8b | 2019-04-22 18:24:39 -0700 | [diff] [blame] | 358 | AttentionService.SERVICE_INTERFACE, serviceConfigPackage |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 359 | )); |
| 360 | return null; |
| 361 | } |
| 362 | |
| 363 | final ServiceInfo serviceInfo = resolveInfo.serviceInfo; |
| 364 | final String permission = serviceInfo.permission; |
| 365 | if (Manifest.permission.BIND_ATTENTION_SERVICE.equals(permission)) { |
| 366 | return serviceInfo.getComponentName(); |
| 367 | } |
| 368 | Slog.e(LOG_TAG, String.format( |
| 369 | "Service %s should require %s permission. Found %s permission", |
| 370 | serviceInfo.getComponentName(), |
| 371 | Manifest.permission.BIND_ATTENTION_SERVICE, |
| 372 | serviceInfo.permission)); |
| 373 | return null; |
| 374 | } |
| 375 | |
Alex Salo | 5f5b5f4 | 2019-02-27 10:49:00 -0800 | [diff] [blame] | 376 | private void dumpInternal(IndentingPrintWriter ipw) { |
| 377 | ipw.println("Attention Manager Service (dumpsys attention) state:\n"); |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 378 | |
Alex Salo | 75b0a99 | 2019-04-30 15:00:30 -0700 | [diff] [blame] | 379 | ipw.println("AttentionServicePackageName=" + getServiceConfigPackage(mContext)); |
| 380 | ipw.println("Resolved component:"); |
| 381 | if (mComponentName != null) { |
| 382 | ipw.increaseIndent(); |
| 383 | ipw.println("Component=" + mComponentName.getPackageName()); |
| 384 | ipw.println("Class=" + mComponentName.getClassName()); |
| 385 | ipw.decreaseIndent(); |
| 386 | } |
| 387 | |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 388 | synchronized (mLock) { |
| 389 | int size = mUserStates.size(); |
| 390 | ipw.print("Number user states: "); |
Alex Salo | 5f5b5f4 | 2019-02-27 10:49:00 -0800 | [diff] [blame] | 391 | ipw.println(size); |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 392 | if (size > 0) { |
| 393 | ipw.increaseIndent(); |
| 394 | for (int i = 0; i < size; i++) { |
| 395 | UserState userState = mUserStates.valueAt(i); |
| 396 | ipw.print(i); |
| 397 | ipw.print(":"); |
| 398 | userState.dump(ipw); |
| 399 | ipw.println(); |
| 400 | } |
| 401 | ipw.decreaseIndent(); |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | private final class LocalService extends AttentionManagerInternal { |
| 407 | @Override |
| 408 | public boolean isAttentionServiceSupported() { |
| 409 | return AttentionManagerService.this.isAttentionServiceSupported(); |
| 410 | } |
| 411 | |
| 412 | @Override |
Alex Salo | eeba023 | 2019-03-14 18:56:54 -0700 | [diff] [blame] | 413 | public boolean checkAttention(long timeout, AttentionCallbackInternal callbackInternal) { |
| 414 | return AttentionManagerService.this.checkAttention(timeout, callbackInternal); |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | @Override |
Alex Salo | eeba023 | 2019-03-14 18:56:54 -0700 | [diff] [blame] | 418 | public void cancelAttentionCheck(AttentionCallbackInternal callbackInternal) { |
| 419 | AttentionManagerService.this.cancelAttentionCheck(callbackInternal); |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
| 423 | private static final class AttentionCheckCache { |
| 424 | private final long mLastComputed; |
| 425 | private final int mResult; |
| 426 | private final long mTimestamp; |
| 427 | |
| 428 | AttentionCheckCache(long lastComputed, @AttentionService.AttentionSuccessCodes int result, |
| 429 | long timestamp) { |
| 430 | mLastComputed = lastComputed; |
| 431 | mResult = result; |
| 432 | mTimestamp = timestamp; |
| 433 | } |
| 434 | } |
| 435 | |
Yi Jiang | f9c9c16 | 2019-04-10 13:36:27 -0700 | [diff] [blame] | 436 | @VisibleForTesting |
| 437 | static final class AttentionCheck { |
Alex Salo | eeba023 | 2019-03-14 18:56:54 -0700 | [diff] [blame] | 438 | private final AttentionCallbackInternal mCallbackInternal; |
| 439 | private final IAttentionCallback mIAttentionCallback; |
Alex Salo | 6382930 | 2019-04-06 14:57:30 -0700 | [diff] [blame] | 440 | private boolean mIsDispatched; |
| 441 | private boolean mIsFulfilled; |
Alex Salo | eeba023 | 2019-03-14 18:56:54 -0700 | [diff] [blame] | 442 | |
| 443 | AttentionCheck(AttentionCallbackInternal callbackInternal, |
| 444 | IAttentionCallback iAttentionCallback) { |
| 445 | mCallbackInternal = callbackInternal; |
| 446 | mIAttentionCallback = iAttentionCallback; |
| 447 | } |
Alex Salo | 6382930 | 2019-04-06 14:57:30 -0700 | [diff] [blame] | 448 | |
| 449 | void cancelInternal() { |
Aaron Li | 7565f8b | 2019-04-22 18:24:39 -0700 | [diff] [blame] | 450 | mIsFulfilled = true; |
Alex Salo | 6382930 | 2019-04-06 14:57:30 -0700 | [diff] [blame] | 451 | mCallbackInternal.onFailure(ATTENTION_FAILURE_CANCELLED); |
| 452 | } |
Alex Salo | eeba023 | 2019-03-14 18:56:54 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Yi Jiang | f9c9c16 | 2019-04-10 13:36:27 -0700 | [diff] [blame] | 455 | @VisibleForTesting |
| 456 | protected static class UserState { |
Alex Salo | 042e43a | 2019-05-30 15:24:54 -0700 | [diff] [blame] | 457 | private final ComponentName mComponentName; |
| 458 | private final AttentionServiceConnection mConnection = new AttentionServiceConnection(); |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 459 | |
| 460 | @GuardedBy("mLock") |
| 461 | IAttentionService mService; |
| 462 | @GuardedBy("mLock") |
Alex Salo | eeba023 | 2019-03-14 18:56:54 -0700 | [diff] [blame] | 463 | AttentionCheck mCurrentAttentionCheck; |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 464 | @GuardedBy("mLock") |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 465 | AttentionCheckCache mAttentionCheckCache; |
Alex Salo | 042e43a | 2019-05-30 15:24:54 -0700 | [diff] [blame] | 466 | @GuardedBy("mLock") |
| 467 | private boolean mBinding; |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 468 | |
| 469 | @UserIdInt |
Alex Salo | 042e43a | 2019-05-30 15:24:54 -0700 | [diff] [blame] | 470 | private final int mUserId; |
| 471 | private final Context mContext; |
| 472 | private final Object mLock; |
| 473 | private final Handler mAttentionHandler; |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 474 | |
Alex Salo | 042e43a | 2019-05-30 15:24:54 -0700 | [diff] [blame] | 475 | UserState(int userId, Context context, Object lock, Handler handler, |
| 476 | ComponentName componentName) { |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 477 | mUserId = userId; |
| 478 | mContext = Preconditions.checkNotNull(context); |
| 479 | mLock = Preconditions.checkNotNull(lock); |
Alex Salo | 50b701e | 2019-02-01 13:34:24 -0800 | [diff] [blame] | 480 | mComponentName = Preconditions.checkNotNull(componentName); |
Alex Salo | 042e43a | 2019-05-30 15:24:54 -0700 | [diff] [blame] | 481 | mAttentionHandler = handler; |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 482 | } |
| 483 | |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 484 | @GuardedBy("mLock") |
| 485 | private void handlePendingCallbackLocked() { |
Alex Salo | 6382930 | 2019-04-06 14:57:30 -0700 | [diff] [blame] | 486 | if (!mCurrentAttentionCheck.mIsDispatched) { |
| 487 | if (mService != null) { |
| 488 | try { |
| 489 | mService.checkAttention(mCurrentAttentionCheck.mIAttentionCallback); |
| 490 | mCurrentAttentionCheck.mIsDispatched = true; |
| 491 | } catch (RemoteException e) { |
| 492 | Slog.e(LOG_TAG, "Cannot call into the AttentionService"); |
| 493 | } |
| 494 | } else { |
| 495 | mCurrentAttentionCheck.mCallbackInternal.onFailure(ATTENTION_FAILURE_UNKNOWN); |
| 496 | } |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
| 500 | /** Binds to the system's AttentionService which provides an actual implementation. */ |
| 501 | @GuardedBy("mLock") |
Alex Salo | 042e43a | 2019-05-30 15:24:54 -0700 | [diff] [blame] | 502 | private void bindLocked() { |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 503 | // No need to bind if service is binding or has already been bound. |
| 504 | if (mBinding || mService != null) { |
Alex Salo | 042e43a | 2019-05-30 15:24:54 -0700 | [diff] [blame] | 505 | return; |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Alex Salo | 042e43a | 2019-05-30 15:24:54 -0700 | [diff] [blame] | 508 | mBinding = true; |
| 509 | // mContext.bindServiceAsUser() calls into ActivityManagerService which it may already |
| 510 | // hold the lock and had called into PowerManagerService, which holds a lock. |
| 511 | // That would create a deadlock. To solve that, putting it on a handler. |
| 512 | mAttentionHandler.post(() -> { |
| 513 | final Intent serviceIntent = new Intent( |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 514 | AttentionService.SERVICE_INTERFACE).setComponent( |
Alex Salo | 50b701e | 2019-02-01 13:34:24 -0800 | [diff] [blame] | 515 | mComponentName); |
Alex Salo | 042e43a | 2019-05-30 15:24:54 -0700 | [diff] [blame] | 516 | // Note: no reason to clear the calling identity, we won't have one in a handler. |
| 517 | mContext.bindServiceAsUser(serviceIntent, mConnection, |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 518 | Context.BIND_AUTO_CREATE, UserHandle.CURRENT); |
Alex Salo | 042e43a | 2019-05-30 15:24:54 -0700 | [diff] [blame] | 519 | |
| 520 | }); |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | private void dump(IndentingPrintWriter pw) { |
Alex Salo | 75b0a99 | 2019-04-30 15:00:30 -0700 | [diff] [blame] | 524 | pw.println("userId=" + mUserId); |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 525 | synchronized (mLock) { |
Alex Salo | 75b0a99 | 2019-04-30 15:00:30 -0700 | [diff] [blame] | 526 | pw.println("binding=" + mBinding); |
| 527 | pw.println("current attention check:"); |
| 528 | if (mCurrentAttentionCheck != null) { |
| 529 | pw.increaseIndent(); |
| 530 | pw.println("is dispatched=" + mCurrentAttentionCheck.mIsDispatched); |
| 531 | pw.println("is fulfilled:=" + mCurrentAttentionCheck.mIsFulfilled); |
| 532 | pw.decreaseIndent(); |
| 533 | } |
| 534 | pw.println("attention check cache:"); |
| 535 | if (mAttentionCheckCache != null) { |
| 536 | pw.increaseIndent(); |
| 537 | pw.println("last computed=" + mAttentionCheckCache.mLastComputed); |
| 538 | pw.println("timestamp=" + mAttentionCheckCache.mTimestamp); |
| 539 | pw.println("result=" + mAttentionCheckCache.mResult); |
| 540 | pw.decreaseIndent(); |
| 541 | } |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
Yi Jiang | f9c9c16 | 2019-04-10 13:36:27 -0700 | [diff] [blame] | 545 | private class AttentionServiceConnection implements ServiceConnection { |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 546 | @Override |
| 547 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 548 | init(IAttentionService.Stub.asInterface(service)); |
| 549 | } |
| 550 | |
| 551 | @Override |
| 552 | public void onServiceDisconnected(ComponentName name) { |
| 553 | cleanupService(); |
| 554 | } |
| 555 | |
| 556 | @Override |
| 557 | public void onBindingDied(ComponentName name) { |
| 558 | cleanupService(); |
| 559 | } |
| 560 | |
| 561 | @Override |
| 562 | public void onNullBinding(ComponentName name) { |
| 563 | cleanupService(); |
| 564 | } |
| 565 | |
| 566 | void cleanupService() { |
| 567 | init(null); |
| 568 | } |
| 569 | |
| 570 | private void init(@Nullable IAttentionService service) { |
| 571 | synchronized (mLock) { |
| 572 | mService = service; |
| 573 | mBinding = false; |
| 574 | handlePendingCallbackLocked(); |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | |
Yi Jiang | f9c9c16 | 2019-04-10 13:36:27 -0700 | [diff] [blame] | 580 | @VisibleForTesting |
| 581 | protected class AttentionHandler extends Handler { |
Alex Salo | 1e32c07 | 2019-02-13 16:58:01 -0800 | [diff] [blame] | 582 | private static final int CHECK_CONNECTION_EXPIRATION = 1; |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 583 | private static final int ATTENTION_CHECK_TIMEOUT = 2; |
| 584 | |
| 585 | AttentionHandler() { |
| 586 | super(Looper.myLooper()); |
| 587 | } |
| 588 | |
Alex Salo | 042e43a | 2019-05-30 15:24:54 -0700 | [diff] [blame] | 589 | @Override |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 590 | public void handleMessage(Message msg) { |
| 591 | switch (msg.what) { |
| 592 | // Do not occupy resources when not in use - unbind proactively. |
Alex Salo | 1e32c07 | 2019-02-13 16:58:01 -0800 | [diff] [blame] | 593 | case CHECK_CONNECTION_EXPIRATION: { |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 594 | for (int i = 0; i < mUserStates.size(); i++) { |
Alex Salo | 1e32c07 | 2019-02-13 16:58:01 -0800 | [diff] [blame] | 595 | cancelAndUnbindLocked(mUserStates.valueAt(i)); |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 596 | } |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 597 | } |
| 598 | break; |
| 599 | |
| 600 | // Callee is no longer interested in the attention check result - cancel. |
| 601 | case ATTENTION_CHECK_TIMEOUT: { |
Alex Salo | 1e32c07 | 2019-02-13 16:58:01 -0800 | [diff] [blame] | 602 | synchronized (mLock) { |
Alex Salo | 6382930 | 2019-04-06 14:57:30 -0700 | [diff] [blame] | 603 | cancel(peekCurrentUserStateLocked()); |
Alex Salo | 1e32c07 | 2019-02-13 16:58:01 -0800 | [diff] [blame] | 604 | } |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 605 | } |
| 606 | break; |
| 607 | |
| 608 | default: |
| 609 | break; |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | |
Yi Jiang | f9c9c16 | 2019-04-10 13:36:27 -0700 | [diff] [blame] | 614 | @VisibleForTesting |
| 615 | void cancel(UserState userState) { |
Alex Salo | 6382930 | 2019-04-06 14:57:30 -0700 | [diff] [blame] | 616 | if (userState == null || userState.mCurrentAttentionCheck == null) { |
| 617 | return; |
| 618 | } |
Alex Salo | 1e32c07 | 2019-02-13 16:58:01 -0800 | [diff] [blame] | 619 | |
Alex Salo | 6382930 | 2019-04-06 14:57:30 -0700 | [diff] [blame] | 620 | if (userState.mCurrentAttentionCheck.mIsFulfilled) { |
| 621 | if (DEBUG) { |
| 622 | Slog.d(LOG_TAG, "Trying to cancel the check that has been already fulfilled."); |
Alex Salo | 1e32c07 | 2019-02-13 16:58:01 -0800 | [diff] [blame] | 623 | } |
Alex Salo | 6382930 | 2019-04-06 14:57:30 -0700 | [diff] [blame] | 624 | return; |
| 625 | } |
Alex Salo | 6382930 | 2019-04-06 14:57:30 -0700 | [diff] [blame] | 626 | |
| 627 | if (userState.mService == null) { |
| 628 | userState.mCurrentAttentionCheck.cancelInternal(); |
| 629 | return; |
| 630 | } |
| 631 | |
| 632 | try { |
| 633 | userState.mService.cancelAttentionCheck( |
| 634 | userState.mCurrentAttentionCheck.mIAttentionCallback); |
| 635 | } catch (RemoteException e) { |
| 636 | Slog.e(LOG_TAG, "Unable to cancel attention check"); |
| 637 | userState.mCurrentAttentionCheck.cancelInternal(); |
Alex Salo | 1e32c07 | 2019-02-13 16:58:01 -0800 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | |
| 641 | @GuardedBy("mLock") |
| 642 | private void cancelAndUnbindLocked(UserState userState) { |
| 643 | synchronized (mLock) { |
Yi Jiang | 4dfc368 | 2019-03-20 16:50:40 -0700 | [diff] [blame] | 644 | if (userState == null) { |
| 645 | return; |
| 646 | } |
Alex Salo | 6382930 | 2019-04-06 14:57:30 -0700 | [diff] [blame] | 647 | |
| 648 | cancel(userState); |
| 649 | |
| 650 | if (userState.mService == null) { |
| 651 | return; |
| 652 | } |
Alex Salo | 1e32c07 | 2019-02-13 16:58:01 -0800 | [diff] [blame] | 653 | |
Alex Salo | 042e43a | 2019-05-30 15:24:54 -0700 | [diff] [blame] | 654 | mAttentionHandler.post(() -> mContext.unbindService(userState.mConnection)); |
| 655 | // Note: this will set mBinding to false even though it could still be trying to bind |
| 656 | // (i.e. the runnable was posted in bindLocked but then cancelAndUnbindLocked was |
| 657 | // called before it's run yet). This is a safe state at the moment, |
| 658 | // since it will eventually, but feels like a source for confusion down the road and |
| 659 | // may cause some expensive and unnecessary work to be done. |
Alex Salo | 1e32c07 | 2019-02-13 16:58:01 -0800 | [diff] [blame] | 660 | userState.mConnection.cleanupService(); |
| 661 | mUserStates.remove(userState.mUserId); |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
| 665 | /** |
| 666 | * Unbinds and stops the service when the screen off intent is received. |
| 667 | * Attention service only makes sense when screen is ON; disconnect and stop service otherwise. |
| 668 | */ |
| 669 | private final class ScreenStateReceiver extends BroadcastReceiver { |
| 670 | @Override |
| 671 | public void onReceive(Context context, Intent intent) { |
| 672 | if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) { |
Alex Salo | 1e32c07 | 2019-02-13 16:58:01 -0800 | [diff] [blame] | 673 | cancelAndUnbindLocked(peekCurrentUserStateLocked()); |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | } |
Alex Salo | 5f5b5f4 | 2019-02-27 10:49:00 -0800 | [diff] [blame] | 677 | |
Aaron Li | 7565f8b | 2019-04-22 18:24:39 -0700 | [diff] [blame] | 678 | private final class AttentionManagerServiceShellCommand extends ShellCommand { |
| 679 | class TestableAttentionCallbackInternal extends AttentionCallbackInternal { |
| 680 | private int mLastCallbackCode = -1; |
| 681 | |
| 682 | @Override |
| 683 | public void onSuccess(int result, long timestamp) { |
| 684 | mLastCallbackCode = result; |
| 685 | } |
| 686 | |
| 687 | @Override |
| 688 | public void onFailure(int error) { |
| 689 | mLastCallbackCode = error; |
| 690 | } |
| 691 | |
| 692 | public void reset() { |
| 693 | mLastCallbackCode = -1; |
| 694 | } |
| 695 | |
| 696 | public int getLastCallbackCode() { |
| 697 | return mLastCallbackCode; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | final TestableAttentionCallbackInternal mTestableAttentionCallback = |
| 702 | new TestableAttentionCallbackInternal(); |
| 703 | |
| 704 | @Override |
| 705 | public int onCommand(@Nullable final String cmd) { |
| 706 | if (cmd == null) { |
| 707 | return handleDefaultCommands(cmd); |
| 708 | } |
| 709 | final PrintWriter err = getErrPrintWriter(); |
| 710 | try { |
| 711 | switch (cmd) { |
| 712 | case "getAttentionServiceComponent": |
| 713 | return cmdResolveAttentionServiceComponent(); |
| 714 | case "call": |
| 715 | switch (getNextArgRequired()) { |
| 716 | case "checkAttention": |
| 717 | return cmdCallCheckAttention(); |
| 718 | case "cancelCheckAttention": |
| 719 | return cmdCallCancelAttention(); |
| 720 | default: |
| 721 | throw new IllegalArgumentException("Invalid argument"); |
| 722 | } |
| 723 | case "setTestableAttentionService": |
| 724 | return cmdSetTestableAttentionService(getNextArgRequired()); |
| 725 | case "clearTestableAttentionService": |
| 726 | return cmdClearTestableAttentionService(); |
| 727 | case "getLastTestCallbackCode": |
| 728 | return cmdGetLastTestCallbackCode(); |
| 729 | default: |
| 730 | return handleDefaultCommands(cmd); |
| 731 | } |
| 732 | } catch (IllegalArgumentException e) { |
| 733 | err.println("Error: " + e.getMessage()); |
| 734 | } |
| 735 | return -1; |
| 736 | } |
| 737 | |
| 738 | private int cmdSetTestableAttentionService(String testingServicePackage) { |
| 739 | final PrintWriter out = getOutPrintWriter(); |
| 740 | if (TextUtils.isEmpty(testingServicePackage)) { |
| 741 | out.println("false"); |
| 742 | } else { |
| 743 | sTestAttentionServicePackage = testingServicePackage; |
| 744 | resetStates(); |
| 745 | out.println(mComponentName != null ? "true" : "false"); |
| 746 | } |
| 747 | return 0; |
| 748 | } |
| 749 | |
| 750 | private int cmdClearTestableAttentionService() { |
| 751 | sTestAttentionServicePackage = ""; |
| 752 | mTestableAttentionCallback.reset(); |
| 753 | resetStates(); |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | private int cmdCallCheckAttention() { |
| 758 | final PrintWriter out = getOutPrintWriter(); |
| 759 | boolean calledSuccessfully = checkAttention(2000, mTestableAttentionCallback); |
| 760 | out.println(calledSuccessfully ? "true" : "false"); |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | private int cmdCallCancelAttention() { |
| 765 | final PrintWriter out = getOutPrintWriter(); |
| 766 | cancelAttentionCheck(mTestableAttentionCallback); |
| 767 | out.println("true"); |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | private int cmdResolveAttentionServiceComponent() { |
| 772 | final PrintWriter out = getOutPrintWriter(); |
| 773 | ComponentName resolvedComponent = resolveAttentionService(mContext); |
| 774 | out.println(resolvedComponent != null ? resolvedComponent.flattenToShortString() : ""); |
| 775 | return 0; |
| 776 | } |
| 777 | |
| 778 | private int cmdGetLastTestCallbackCode() { |
| 779 | final PrintWriter out = getOutPrintWriter(); |
| 780 | out.println(mTestableAttentionCallback.getLastCallbackCode()); |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | private void resetStates() { |
| 785 | mComponentName = resolveAttentionService(mContext); |
| 786 | mUserStates.clear(); |
| 787 | } |
| 788 | |
| 789 | @Override |
| 790 | public void onHelp() { |
| 791 | final PrintWriter out = getOutPrintWriter(); |
| 792 | out.println("Attention commands: "); |
| 793 | out.println(" setTestableAttentionService <service_package>: Bind to a custom" |
| 794 | + " implementation of attention service"); |
| 795 | out.println(" ---<service_package>:"); |
| 796 | out.println( |
| 797 | " := Package containing the Attention Service implementation to bind to"); |
| 798 | out.println(" ---returns:"); |
| 799 | out.println(" := true, if was bound successfully"); |
| 800 | out.println(" := false, if was not bound successfully"); |
| 801 | out.println(" clearTestableAttentionService: Undo custom bindings. Revert to previous" |
| 802 | + " behavior"); |
| 803 | out.println(" getAttentionServiceComponent: Get the current service component string"); |
| 804 | out.println(" ---returns:"); |
| 805 | out.println(" := If valid, the component string (in shorten form) for the" |
| 806 | + " currently bound service."); |
| 807 | out.println(" := else, empty string"); |
| 808 | out.println(" call checkAttention: Calls check attention"); |
| 809 | out.println(" ---returns:"); |
| 810 | out.println( |
| 811 | " := true, if the call was successfully dispatched to the service " |
| 812 | + "implementation." |
| 813 | + " (to see the result, call getLastTestCallbackCode)"); |
| 814 | out.println(" := false, otherwise"); |
| 815 | out.println(" call cancelCheckAttention: Cancels check attention"); |
| 816 | out.println(" getLastTestCallbackCode"); |
| 817 | out.println(" ---returns:"); |
| 818 | out.println( |
| 819 | " := An integer, representing the last callback code received from the " |
| 820 | + "bounded implementation. If none, it will return -1"); |
| 821 | } |
| 822 | } |
| 823 | |
Alex Salo | 5f5b5f4 | 2019-02-27 10:49:00 -0800 | [diff] [blame] | 824 | private final class BinderService extends Binder { |
Aaron Li | 7565f8b | 2019-04-22 18:24:39 -0700 | [diff] [blame] | 825 | AttentionManagerServiceShellCommand mAttentionManagerServiceShellCommand = |
| 826 | new AttentionManagerServiceShellCommand(); |
| 827 | |
| 828 | @Override |
| 829 | public void onShellCommand(FileDescriptor in, FileDescriptor out, |
| 830 | FileDescriptor err, |
| 831 | String[] args, ShellCallback callback, |
| 832 | ResultReceiver resultReceiver) { |
| 833 | mAttentionManagerServiceShellCommand.exec(this, in, out, err, args, callback, |
| 834 | resultReceiver); |
| 835 | } |
| 836 | |
Alex Salo | 5f5b5f4 | 2019-02-27 10:49:00 -0800 | [diff] [blame] | 837 | @Override |
| 838 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 839 | if (!DumpUtils.checkDumpPermission(mContext, LOG_TAG, pw)) { |
| 840 | return; |
| 841 | } |
| 842 | |
| 843 | dumpInternal(new IndentingPrintWriter(pw, " ")); |
| 844 | } |
| 845 | } |
Alex Salo | b81472f | 2018-12-12 14:44:28 -0800 | [diff] [blame] | 846 | } |