Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -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.power; |
| 18 | |
Alex Salo | 2b7dbe8 | 2019-04-11 14:45:42 -0700 | [diff] [blame] | 19 | import static android.provider.Settings.System.ADAPTIVE_SLEEP; |
| 20 | |
| 21 | import android.Manifest; |
Yi Jiang | f8291bb | 2019-05-24 16:32:10 -0700 | [diff] [blame] | 22 | import android.app.ActivityManager; |
| 23 | import android.app.SynchronousUserSwitchObserver; |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 24 | import android.attention.AttentionManagerInternal; |
| 25 | import android.attention.AttentionManagerInternal.AttentionCallbackInternal; |
Alex Salo | 2b7dbe8 | 2019-04-11 14:45:42 -0700 | [diff] [blame] | 26 | import android.content.ContentResolver; |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 27 | import android.content.Context; |
Alex Salo | 2b7dbe8 | 2019-04-11 14:45:42 -0700 | [diff] [blame] | 28 | import android.content.pm.PackageManager; |
Alex Salo | 9af68f1 | 2019-02-15 13:50:11 -0800 | [diff] [blame] | 29 | import android.database.ContentObserver; |
| 30 | import android.os.Handler; |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 31 | import android.os.PowerManager; |
| 32 | import android.os.PowerManagerInternal; |
Yi Jiang | f8291bb | 2019-05-24 16:32:10 -0700 | [diff] [blame] | 33 | import android.os.RemoteException; |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 34 | import android.os.SystemClock; |
Alex Salo | 9af68f1 | 2019-02-15 13:50:11 -0800 | [diff] [blame] | 35 | import android.os.UserHandle; |
| 36 | import android.provider.Settings; |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 37 | import android.service.attention.AttentionService; |
| 38 | import android.util.Slog; |
Alex Salo | d7c3eef | 2019-01-25 14:43:27 -0800 | [diff] [blame] | 39 | import android.util.StatsLog; |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 40 | |
| 41 | import com.android.internal.annotations.VisibleForTesting; |
| 42 | import com.android.server.LocalServices; |
Alex Salo | 4948976 | 2019-06-03 16:23:30 -0700 | [diff] [blame] | 43 | import com.android.server.wm.WindowManagerInternal; |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 44 | |
| 45 | import java.io.PrintWriter; |
Alex Salo | eeba023 | 2019-03-14 18:56:54 -0700 | [diff] [blame] | 46 | import java.util.concurrent.atomic.AtomicBoolean; |
Alex Salo | d7c3eef | 2019-01-25 14:43:27 -0800 | [diff] [blame] | 47 | import java.util.concurrent.atomic.AtomicLong; |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Class responsible for checking if the user is currently paying attention to the phone and |
| 51 | * notifying {@link PowerManagerService} that user activity should be renewed. |
| 52 | * |
| 53 | * This class also implements a limit of how long the extension should be, to avoid security |
| 54 | * issues where the device would never be locked. |
| 55 | */ |
| 56 | public class AttentionDetector { |
| 57 | |
| 58 | private static final String TAG = "AttentionDetector"; |
| 59 | private static final boolean DEBUG = false; |
| 60 | |
Yi Jiang | f8291bb | 2019-05-24 16:32:10 -0700 | [diff] [blame] | 61 | private Context mContext; |
| 62 | |
Alex Salo | 9af68f1 | 2019-02-15 13:50:11 -0800 | [diff] [blame] | 63 | private boolean mIsSettingEnabled; |
| 64 | |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 65 | /** |
| 66 | * Invoked whenever user attention is detected. |
| 67 | */ |
| 68 | private final Runnable mOnUserAttention; |
| 69 | |
| 70 | /** |
| 71 | * The maximum time, in millis, that the phone can stay unlocked because of attention events, |
| 72 | * triggered by any user. |
| 73 | */ |
| 74 | @VisibleForTesting |
| 75 | protected long mMaximumExtensionMillis; |
| 76 | |
| 77 | private final Object mLock; |
| 78 | |
| 79 | /** |
Alex Salo | eeba023 | 2019-03-14 18:56:54 -0700 | [diff] [blame] | 80 | * If we're currently waiting for an attention callback |
| 81 | */ |
| 82 | private final AtomicBoolean mRequested; |
| 83 | |
Alex Salo | cbe610a | 2019-05-01 14:18:46 -0700 | [diff] [blame] | 84 | private long mLastActedOnNextScreenDimming; |
| 85 | |
Alex Salo | eeba023 | 2019-03-14 18:56:54 -0700 | [diff] [blame] | 86 | /** |
Alex Salo | 69ff321 | 2019-04-06 16:24:00 -0700 | [diff] [blame] | 87 | * Monotonously increasing ID for the requests sent. |
| 88 | */ |
| 89 | @VisibleForTesting |
| 90 | protected int mRequestId; |
| 91 | |
| 92 | /** |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 93 | * {@link android.service.attention.AttentionService} API timeout. |
| 94 | */ |
| 95 | private long mMaxAttentionApiTimeoutMillis; |
| 96 | |
| 97 | /** |
| 98 | * Last known user activity. |
| 99 | */ |
| 100 | private long mLastUserActivityTime; |
| 101 | |
| 102 | @VisibleForTesting |
| 103 | protected AttentionManagerInternal mAttentionManager; |
| 104 | |
Alex Salo | 2b7dbe8 | 2019-04-11 14:45:42 -0700 | [diff] [blame] | 105 | @VisibleForTesting |
Alex Salo | 4948976 | 2019-06-03 16:23:30 -0700 | [diff] [blame] | 106 | protected WindowManagerInternal mWindowManager; |
| 107 | |
| 108 | @VisibleForTesting |
Alex Salo | 2b7dbe8 | 2019-04-11 14:45:42 -0700 | [diff] [blame] | 109 | protected PackageManager mPackageManager; |
| 110 | |
| 111 | @VisibleForTesting |
| 112 | protected ContentResolver mContentResolver; |
| 113 | |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 114 | /** |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 115 | * Current wakefulness of the device. {@see PowerManagerInternal} |
| 116 | */ |
| 117 | private int mWakefulness; |
| 118 | |
Alex Salo | d7c3eef | 2019-01-25 14:43:27 -0800 | [diff] [blame] | 119 | /** |
| 120 | * Describes how many times in a row was the timeout extended. |
| 121 | */ |
| 122 | private AtomicLong mConsecutiveTimeoutExtendedCount = new AtomicLong(0); |
| 123 | |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 124 | @VisibleForTesting |
Alex Salo | 69ff321 | 2019-04-06 16:24:00 -0700 | [diff] [blame] | 125 | AttentionCallbackInternalImpl mCallback; |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 126 | |
| 127 | public AttentionDetector(Runnable onUserAttention, Object lock) { |
| 128 | mOnUserAttention = onUserAttention; |
| 129 | mLock = lock; |
Alex Salo | eeba023 | 2019-03-14 18:56:54 -0700 | [diff] [blame] | 130 | mRequested = new AtomicBoolean(false); |
Alex Salo | 69ff321 | 2019-04-06 16:24:00 -0700 | [diff] [blame] | 131 | mRequestId = 0; |
Alex Salo | 4d9e431 | 2019-02-13 14:59:42 -0800 | [diff] [blame] | 132 | |
| 133 | // Device starts with an awake state upon boot. |
| 134 | mWakefulness = PowerManagerInternal.WAKEFULNESS_AWAKE; |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Alex Salo | 9af68f1 | 2019-02-15 13:50:11 -0800 | [diff] [blame] | 137 | @VisibleForTesting |
| 138 | void updateEnabledFromSettings(Context context) { |
| 139 | mIsSettingEnabled = Settings.System.getIntForUser(context.getContentResolver(), |
| 140 | Settings.System.ADAPTIVE_SLEEP, 0, UserHandle.USER_CURRENT) == 1; |
| 141 | } |
| 142 | |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 143 | public void systemReady(Context context) { |
Yi Jiang | f8291bb | 2019-05-24 16:32:10 -0700 | [diff] [blame] | 144 | mContext = context; |
Alex Salo | 9af68f1 | 2019-02-15 13:50:11 -0800 | [diff] [blame] | 145 | updateEnabledFromSettings(context); |
Alex Salo | 2b7dbe8 | 2019-04-11 14:45:42 -0700 | [diff] [blame] | 146 | mPackageManager = context.getPackageManager(); |
| 147 | mContentResolver = context.getContentResolver(); |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 148 | mAttentionManager = LocalServices.getService(AttentionManagerInternal.class); |
Alex Salo | 4948976 | 2019-06-03 16:23:30 -0700 | [diff] [blame] | 149 | mWindowManager = LocalServices.getService(WindowManagerInternal.class); |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 150 | mMaximumExtensionMillis = context.getResources().getInteger( |
| 151 | com.android.internal.R.integer.config_attentionMaximumExtension); |
| 152 | mMaxAttentionApiTimeoutMillis = context.getResources().getInteger( |
| 153 | com.android.internal.R.integer.config_attentionApiTimeout); |
Alex Salo | 9af68f1 | 2019-02-15 13:50:11 -0800 | [diff] [blame] | 154 | |
Yi Jiang | f8291bb | 2019-05-24 16:32:10 -0700 | [diff] [blame] | 155 | try { |
| 156 | final UserSwitchObserver observer = new UserSwitchObserver(); |
| 157 | ActivityManager.getService().registerUserSwitchObserver(observer, TAG); |
| 158 | } catch (RemoteException e) { |
| 159 | // Shouldn't happen since in-process. |
| 160 | } |
| 161 | |
Alex Salo | 9af68f1 | 2019-02-15 13:50:11 -0800 | [diff] [blame] | 162 | context.getContentResolver().registerContentObserver(Settings.System.getUriFor( |
| 163 | Settings.System.ADAPTIVE_SLEEP), |
| 164 | false, new ContentObserver(new Handler()) { |
| 165 | @Override |
| 166 | public void onChange(boolean selfChange) { |
| 167 | updateEnabledFromSettings(context); |
| 168 | } |
| 169 | }, UserHandle.USER_ALL); |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | public long updateUserActivity(long nextScreenDimming) { |
Alex Salo | 4948976 | 2019-06-03 16:23:30 -0700 | [diff] [blame] | 173 | if (nextScreenDimming == mLastActedOnNextScreenDimming |
| 174 | || !mIsSettingEnabled |
| 175 | || !isAttentionServiceSupported() |
| 176 | || mWindowManager.isKeyguardShowingAndNotOccluded()) { |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 177 | return nextScreenDimming; |
| 178 | } |
| 179 | |
Alex Salo | 2b7dbe8 | 2019-04-11 14:45:42 -0700 | [diff] [blame] | 180 | if (!serviceHasSufficientPermissions()) { |
| 181 | Settings.System.putInt(mContentResolver, ADAPTIVE_SLEEP, 0); |
| 182 | return nextScreenDimming; |
| 183 | } |
| 184 | |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 185 | final long now = SystemClock.uptimeMillis(); |
| 186 | final long whenToCheck = nextScreenDimming - getAttentionTimeout(); |
| 187 | final long whenToStopExtending = mLastUserActivityTime + mMaximumExtensionMillis; |
| 188 | if (now < whenToCheck) { |
| 189 | if (DEBUG) { |
| 190 | Slog.d(TAG, "Do not check for attention yet, wait " + (whenToCheck - now)); |
| 191 | } |
Alex Salo | 1761fce | 2019-02-13 15:54:50 -0800 | [diff] [blame] | 192 | return whenToCheck; |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 193 | } else if (whenToStopExtending < whenToCheck) { |
| 194 | if (DEBUG) { |
| 195 | Slog.d(TAG, "Let device sleep to avoid false results and improve security " |
| 196 | + (whenToCheck - whenToStopExtending)); |
| 197 | } |
| 198 | return nextScreenDimming; |
Alex Salo | eeba023 | 2019-03-14 18:56:54 -0700 | [diff] [blame] | 199 | } else if (mRequested.get()) { |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 200 | if (DEBUG) { |
Alex Salo | 69ff321 | 2019-04-06 16:24:00 -0700 | [diff] [blame] | 201 | Slog.d(TAG, "Pending attention callback with ID=" + mCallback.mId + ", wait."); |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 202 | } |
| 203 | return whenToCheck; |
| 204 | } |
| 205 | |
| 206 | // Ideally we should attribute mRequested to the result of #checkAttention, but the |
| 207 | // callback might arrive before #checkAttention returns (if there are cached results.) |
| 208 | // This means that we must assume that the request was successful, and then cancel it |
| 209 | // afterwards if AttentionManager couldn't deliver it. |
Alex Salo | eeba023 | 2019-03-14 18:56:54 -0700 | [diff] [blame] | 210 | mRequested.set(true); |
Alex Salo | 69ff321 | 2019-04-06 16:24:00 -0700 | [diff] [blame] | 211 | mRequestId++; |
Alex Salo | cbe610a | 2019-05-01 14:18:46 -0700 | [diff] [blame] | 212 | mLastActedOnNextScreenDimming = nextScreenDimming; |
Alex Salo | 69ff321 | 2019-04-06 16:24:00 -0700 | [diff] [blame] | 213 | mCallback = new AttentionCallbackInternalImpl(mRequestId); |
Alex Salo | cbe610a | 2019-05-01 14:18:46 -0700 | [diff] [blame] | 214 | Slog.v(TAG, "Checking user attention, ID: " + mRequestId); |
Alex Salo | eeba023 | 2019-03-14 18:56:54 -0700 | [diff] [blame] | 215 | final boolean sent = mAttentionManager.checkAttention(getAttentionTimeout(), mCallback); |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 216 | if (!sent) { |
Alex Salo | eeba023 | 2019-03-14 18:56:54 -0700 | [diff] [blame] | 217 | mRequested.set(false); |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 220 | return whenToCheck; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Handles user activity by cancelling any pending attention requests and keeping track of when |
| 225 | * the activity happened. |
| 226 | * |
| 227 | * @param eventTime Activity time, in uptime millis. |
Alex Salo | 9af68f1 | 2019-02-15 13:50:11 -0800 | [diff] [blame] | 228 | * @param event Activity type as defined in {@link PowerManager}. |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 229 | * @return 0 when activity was ignored, 1 when handled, -1 when invalid. |
| 230 | */ |
| 231 | public int onUserActivity(long eventTime, int event) { |
| 232 | switch (event) { |
| 233 | case PowerManager.USER_ACTIVITY_EVENT_ATTENTION: |
Alex Salo | d7c3eef | 2019-01-25 14:43:27 -0800 | [diff] [blame] | 234 | mConsecutiveTimeoutExtendedCount.incrementAndGet(); |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 235 | return 0; |
| 236 | case PowerManager.USER_ACTIVITY_EVENT_OTHER: |
| 237 | case PowerManager.USER_ACTIVITY_EVENT_BUTTON: |
| 238 | case PowerManager.USER_ACTIVITY_EVENT_TOUCH: |
| 239 | case PowerManager.USER_ACTIVITY_EVENT_ACCESSIBILITY: |
| 240 | cancelCurrentRequestIfAny(); |
| 241 | mLastUserActivityTime = eventTime; |
Alex Salo | d7c3eef | 2019-01-25 14:43:27 -0800 | [diff] [blame] | 242 | resetConsecutiveExtensionCount(); |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 243 | return 1; |
| 244 | default: |
| 245 | if (DEBUG) { |
| 246 | Slog.d(TAG, "Attention not reset. Unknown activity event: " + event); |
| 247 | } |
| 248 | return -1; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | public void onWakefulnessChangeStarted(int wakefulness) { |
| 253 | mWakefulness = wakefulness; |
| 254 | if (wakefulness != PowerManagerInternal.WAKEFULNESS_AWAKE) { |
| 255 | cancelCurrentRequestIfAny(); |
Alex Salo | d7c3eef | 2019-01-25 14:43:27 -0800 | [diff] [blame] | 256 | resetConsecutiveExtensionCount(); |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
| 260 | private void cancelCurrentRequestIfAny() { |
Alex Salo | eeba023 | 2019-03-14 18:56:54 -0700 | [diff] [blame] | 261 | if (mRequested.get()) { |
| 262 | mAttentionManager.cancelAttentionCheck(mCallback); |
| 263 | mRequested.set(false); |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
Alex Salo | d7c3eef | 2019-01-25 14:43:27 -0800 | [diff] [blame] | 267 | private void resetConsecutiveExtensionCount() { |
| 268 | final long previousCount = mConsecutiveTimeoutExtendedCount.getAndSet(0); |
| 269 | if (previousCount > 0) { |
| 270 | StatsLog.write(StatsLog.SCREEN_TIMEOUT_EXTENSION_REPORTED, previousCount); |
| 271 | } |
| 272 | } |
| 273 | |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 274 | @VisibleForTesting |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 275 | long getAttentionTimeout() { |
| 276 | return mMaxAttentionApiTimeoutMillis; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * {@see AttentionManagerInternal#isAttentionServiceSupported} |
| 281 | */ |
| 282 | @VisibleForTesting |
| 283 | boolean isAttentionServiceSupported() { |
Alex Salo | 7a6e3a6 | 2019-02-27 15:08:15 -0800 | [diff] [blame] | 284 | return mAttentionManager != null && mAttentionManager.isAttentionServiceSupported(); |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Alex Salo | 2b7dbe8 | 2019-04-11 14:45:42 -0700 | [diff] [blame] | 287 | /** |
| 288 | * Returns {@code true} if the attention service has sufficient permissions, disables the |
| 289 | * depending features otherwise. |
| 290 | */ |
| 291 | @VisibleForTesting |
| 292 | boolean serviceHasSufficientPermissions() { |
| 293 | final String attentionPackage = mPackageManager.getAttentionServicePackageName(); |
| 294 | return attentionPackage != null && mPackageManager.checkPermission( |
| 295 | Manifest.permission.CAMERA, attentionPackage) |
| 296 | == PackageManager.PERMISSION_GRANTED; |
| 297 | } |
| 298 | |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 299 | public void dump(PrintWriter pw) { |
Alex Salo | 75b0a99 | 2019-04-30 15:00:30 -0700 | [diff] [blame] | 300 | pw.println("AttentionDetector:"); |
Alex Salo | a5085f6 | 2019-06-04 18:20:12 -0700 | [diff] [blame] | 301 | pw.println(" mIsSettingEnabled=" + mIsSettingEnabled); |
Alex Salo | 75b0a99 | 2019-04-30 15:00:30 -0700 | [diff] [blame] | 302 | pw.println(" mMaximumExtensionMillis=" + mMaximumExtensionMillis); |
| 303 | pw.println(" mMaxAttentionApiTimeoutMillis=" + mMaxAttentionApiTimeoutMillis); |
| 304 | pw.println(" mLastUserActivityTime(excludingAttention)=" + mLastUserActivityTime); |
| 305 | pw.println(" mAttentionServiceSupported=" + isAttentionServiceSupported()); |
| 306 | pw.println(" mRequested=" + mRequested); |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 307 | } |
Alex Salo | 69ff321 | 2019-04-06 16:24:00 -0700 | [diff] [blame] | 308 | |
| 309 | @VisibleForTesting |
| 310 | final class AttentionCallbackInternalImpl extends AttentionCallbackInternal { |
| 311 | private final int mId; |
| 312 | |
| 313 | AttentionCallbackInternalImpl(int id) { |
| 314 | this.mId = id; |
| 315 | } |
| 316 | |
| 317 | @Override |
| 318 | public void onSuccess(int result, long timestamp) { |
| 319 | Slog.v(TAG, "onSuccess: " + result + ", ID: " + mId); |
| 320 | // If we don't check for request ID it's possible to get into a loop: success leads |
| 321 | // to the onUserAttention(), which in turn triggers updateUserActivity(), which will |
| 322 | // call back onSuccess() instantaneously if there is a cached value, and circle repeats. |
| 323 | if (mId == mRequestId && mRequested.getAndSet(false)) { |
| 324 | synchronized (mLock) { |
| 325 | if (mWakefulness != PowerManagerInternal.WAKEFULNESS_AWAKE) { |
| 326 | if (DEBUG) Slog.d(TAG, "Device slept before receiving callback."); |
| 327 | return; |
| 328 | } |
| 329 | if (result == AttentionService.ATTENTION_SUCCESS_PRESENT) { |
| 330 | mOnUserAttention.run(); |
| 331 | } else { |
| 332 | resetConsecutiveExtensionCount(); |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public void onFailure(int error) { |
| 340 | Slog.i(TAG, "Failed to check attention: " + error + ", ID: " + mId); |
| 341 | mRequested.set(false); |
| 342 | } |
| 343 | } |
Yi Jiang | f8291bb | 2019-05-24 16:32:10 -0700 | [diff] [blame] | 344 | |
| 345 | private final class UserSwitchObserver extends SynchronousUserSwitchObserver { |
| 346 | @Override |
| 347 | public void onUserSwitching(int newUserId) throws RemoteException { |
| 348 | updateEnabledFromSettings(mContext); |
| 349 | } |
| 350 | } |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 351 | } |