Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.am; |
| 18 | |
| 19 | import static android.app.ActivityManager.StackId.DOCKED_STACK_ID; |
| 20 | import static android.view.WindowManagerPolicy.KEYGUARD_GOING_AWAY_FLAG_NO_WINDOW_ANIMATIONS; |
| 21 | import static android.view.WindowManagerPolicy.KEYGUARD_GOING_AWAY_FLAG_TO_SHADE; |
| 22 | import static android.view.WindowManagerPolicy.KEYGUARD_GOING_AWAY_FLAG_WITH_WALLPAPER; |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 23 | import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM; |
| 24 | import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME; |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 25 | import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS; |
| 26 | import static com.android.server.wm.AppTransition.TRANSIT_FLAG_KEYGUARD_GOING_AWAY_NO_ANIMATION; |
| 27 | import static com.android.server.wm.AppTransition.TRANSIT_FLAG_KEYGUARD_GOING_AWAY_TO_SHADE; |
| 28 | import static com.android.server.wm.AppTransition.TRANSIT_FLAG_KEYGUARD_GOING_AWAY_WITH_WALLPAPER; |
| 29 | import static com.android.server.wm.AppTransition.TRANSIT_KEYGUARD_GOING_AWAY; |
| 30 | import static com.android.server.wm.AppTransition.TRANSIT_KEYGUARD_OCCLUDE; |
| 31 | import static com.android.server.wm.AppTransition.TRANSIT_KEYGUARD_UNOCCLUDE; |
| 32 | import static com.android.server.wm.AppTransition.TRANSIT_UNSET; |
| 33 | |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 34 | import android.os.IBinder; |
| 35 | import android.os.RemoteException; |
| 36 | import android.util.Slog; |
| 37 | |
| 38 | import com.android.internal.policy.IKeyguardDismissCallback; |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 39 | import com.android.server.wm.WindowManagerService; |
| 40 | |
Jorim Jaggi | 8d78693 | 2016-10-26 19:08:36 -0700 | [diff] [blame] | 41 | import java.io.PrintWriter; |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 42 | import java.util.ArrayList; |
| 43 | |
| 44 | /** |
| 45 | * Controls Keyguard occluding, dismissing and transitions depending on what kind of activities are |
| 46 | * currently visible. |
| 47 | * <p> |
| 48 | * Note that everything in this class should only be accessed with the AM lock being held. |
| 49 | */ |
| 50 | class KeyguardController { |
| 51 | |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 52 | private static final String TAG = TAG_WITH_CLASS_NAME ? "KeyguardController" : TAG_AM; |
| 53 | |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 54 | private final ActivityManagerService mService; |
| 55 | private final ActivityStackSupervisor mStackSupervisor; |
| 56 | private WindowManagerService mWindowManager; |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 57 | private boolean mKeyguardShowing; |
Jorim Jaggi | 8d78693 | 2016-10-26 19:08:36 -0700 | [diff] [blame] | 58 | private boolean mKeyguardGoingAway; |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 59 | private boolean mOccluded; |
Jorim Jaggi | 0796187 | 2016-11-23 11:28:57 +0100 | [diff] [blame^] | 60 | private boolean mDismissalRequested; |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 61 | private ActivityRecord mDismissingKeyguardActivity; |
| 62 | private int mBeforeUnoccludeTransit; |
| 63 | private int mVisibilityTransactionDepth; |
| 64 | |
| 65 | KeyguardController(ActivityManagerService service, |
| 66 | ActivityStackSupervisor stackSupervisor) { |
| 67 | mService = service; |
| 68 | mStackSupervisor = stackSupervisor; |
| 69 | } |
| 70 | |
| 71 | void setWindowManager(WindowManagerService windowManager) { |
| 72 | mWindowManager = windowManager; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @return true if Keyguard is showing, not going away, and not being occluded, false otherwise |
| 77 | */ |
| 78 | boolean isKeyguardShowing() { |
| 79 | return mKeyguardShowing && !mKeyguardGoingAway && !mOccluded; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @return true if Keyguard is either showing or occluded, but not going away |
| 84 | */ |
| 85 | boolean isKeyguardLocked() { |
| 86 | return mKeyguardShowing && !mKeyguardGoingAway; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Update the Keyguard showing state. |
| 91 | */ |
| 92 | void setKeyguardShown(boolean showing) { |
| 93 | if (showing == mKeyguardShowing) { |
| 94 | return; |
| 95 | } |
| 96 | mKeyguardShowing = showing; |
| 97 | if (showing) { |
| 98 | mKeyguardGoingAway = false; |
Jorim Jaggi | 0796187 | 2016-11-23 11:28:57 +0100 | [diff] [blame^] | 99 | mDismissalRequested = false; |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 100 | } |
| 101 | mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS); |
| 102 | mService.updateSleepIfNeededLocked(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Called when Keyguard is going away. |
| 107 | * |
| 108 | * @param flags See {@link android.view.WindowManagerPolicy#KEYGUARD_GOING_AWAY_FLAG_TO_SHADE} |
| 109 | * etc. |
| 110 | */ |
| 111 | void keyguardGoingAway(int flags) { |
| 112 | if (mKeyguardShowing) { |
Jorim Jaggi | ab7ad38 | 2016-10-26 18:22:04 -0700 | [diff] [blame] | 113 | mWindowManager.deferSurfaceLayout(); |
| 114 | try { |
| 115 | mKeyguardGoingAway = true; |
| 116 | mWindowManager.prepareAppTransition(TRANSIT_KEYGUARD_GOING_AWAY, |
| 117 | false /* alwaysKeepCurrent */, convertTransitFlags(flags), |
| 118 | false /* forceOverride */); |
Jorim Jaggi | ab7ad38 | 2016-10-26 18:22:04 -0700 | [diff] [blame] | 119 | mService.updateSleepIfNeededLocked(); |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 120 | |
Jorim Jaggi | ab7ad38 | 2016-10-26 18:22:04 -0700 | [diff] [blame] | 121 | // Some stack visibility might change (e.g. docked stack) |
| 122 | mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS); |
| 123 | mWindowManager.executeAppTransition(); |
| 124 | mService.applyVrModeIfNeededLocked(mStackSupervisor.getResumedActivityLocked(), |
| 125 | true /* enable */); |
| 126 | } finally { |
| 127 | mWindowManager.continueSurfaceLayout(); |
| 128 | } |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 132 | void dismissKeyguard(IBinder token, IKeyguardDismissCallback callback) { |
| 133 | final ActivityRecord activityRecord = ActivityRecord.forTokenLocked(token); |
| 134 | if (activityRecord == null || !activityRecord.visibleIgnoringKeyguard) { |
| 135 | failCallback(callback); |
| 136 | return; |
| 137 | } |
| 138 | mWindowManager.dismissKeyguard(callback); |
| 139 | } |
| 140 | |
| 141 | private void failCallback(IKeyguardDismissCallback callback) { |
| 142 | try { |
| 143 | callback.onDismissError(); |
| 144 | } catch (RemoteException e) { |
| 145 | Slog.w(TAG, "Failed to call callback", e); |
| 146 | } |
| 147 | } |
| 148 | |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 149 | private int convertTransitFlags(int keyguardGoingAwayFlags) { |
| 150 | int result = 0; |
| 151 | if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_TO_SHADE) != 0) { |
| 152 | result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_TO_SHADE; |
| 153 | } |
| 154 | if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_NO_WINDOW_ANIMATIONS) != 0) { |
| 155 | result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_NO_ANIMATION; |
| 156 | } |
| 157 | if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_WITH_WALLPAPER) != 0) { |
| 158 | result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_WITH_WALLPAPER; |
| 159 | } |
| 160 | return result; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Starts a batch of visibility updates. |
| 165 | */ |
| 166 | void beginActivityVisibilityUpdate() { |
| 167 | mVisibilityTransactionDepth++; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Ends a batch of visibility updates. After all batches are done, this method makes sure to |
| 172 | * update lockscreen occluded/dismiss state if needed. |
| 173 | */ |
| 174 | void endActivityVisibilityUpdate() { |
| 175 | mVisibilityTransactionDepth--; |
| 176 | if (mVisibilityTransactionDepth == 0) { |
| 177 | visibilitiesUpdated(); |
| 178 | } |
| 179 | } |
| 180 | |
Jorim Jaggi | e69c931 | 2016-10-31 18:24:38 -0700 | [diff] [blame] | 181 | /** |
| 182 | * @return True if we may show an activity while Keyguard is showing because we are in the |
| 183 | * process of dismissing it anyways, false otherwise. |
| 184 | */ |
Jorim Jaggi | 0796187 | 2016-11-23 11:28:57 +0100 | [diff] [blame^] | 185 | boolean canShowActivityWhileKeyguardShowing(ActivityRecord r, boolean dismissKeyguard) { |
| 186 | |
| 187 | // Allow to show it when we are about to dismiss Keyguard. This isn't allowed if r is |
| 188 | // already the dismissing activity, in which case we don't allow it to repeatedly dismiss |
| 189 | // Keyguard. |
| 190 | return dismissKeyguard && canDismissKeyguard() && |
| 191 | (mDismissalRequested || r != mDismissingKeyguardActivity); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * @return True if we may show an activity while Keyguard is occluded, false otherwise. |
| 196 | */ |
| 197 | boolean canShowWhileOccluded(boolean dismissKeyguard, boolean showWhenLocked) { |
| 198 | return showWhenLocked || dismissKeyguard && !mWindowManager.isKeyguardSecure(); |
Jorim Jaggi | e69c931 | 2016-10-31 18:24:38 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 201 | private void visibilitiesUpdated() { |
| 202 | final boolean lastOccluded = mOccluded; |
| 203 | final ActivityRecord lastDismissingKeyguardActivity = mDismissingKeyguardActivity; |
| 204 | mOccluded = false; |
| 205 | mDismissingKeyguardActivity = null; |
| 206 | final ArrayList<ActivityStack> stacks = mStackSupervisor.getStacksOnDefaultDisplay(); |
| 207 | final int topStackNdx = stacks.size() - 1; |
| 208 | for (int stackNdx = topStackNdx; stackNdx >= 0; --stackNdx) { |
| 209 | final ActivityStack stack = stacks.get(stackNdx); |
| 210 | |
| 211 | // Only the very top activity may control occluded state |
| 212 | if (stackNdx == topStackNdx) { |
Jorim Jaggi | 0796187 | 2016-11-23 11:28:57 +0100 | [diff] [blame^] | 213 | |
| 214 | // A dismissing activity occludes Keyguard in the insecure case for legacy reasons. |
| 215 | final ActivityRecord topDismissing = stack.getTopDismissingKeyguardActivity(); |
| 216 | mOccluded = stack.topActivityOccludesKeyguard() |
| 217 | || (topDismissing != null |
| 218 | && stack.topRunningActivityLocked() == topDismissing |
| 219 | && canShowWhileOccluded(true /* dismissKeyguard */, |
| 220 | false /* showWhenLocked */)); |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 221 | } |
| 222 | if (mDismissingKeyguardActivity == null |
| 223 | && stack.getTopDismissingKeyguardActivity() != null) { |
| 224 | mDismissingKeyguardActivity = stack.getTopDismissingKeyguardActivity(); |
| 225 | } |
| 226 | } |
Jorim Jaggi | 77e1043 | 2016-10-26 17:43:56 -0700 | [diff] [blame] | 227 | mOccluded |= mWindowManager.isShowingDream(); |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 228 | if (mOccluded != lastOccluded) { |
| 229 | handleOccludedChanged(); |
| 230 | } |
| 231 | if (mDismissingKeyguardActivity != lastDismissingKeyguardActivity) { |
| 232 | handleDismissKeyguard(); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Called when occluded state changed. |
| 238 | */ |
| 239 | private void handleOccludedChanged() { |
| 240 | mWindowManager.onKeyguardOccludedChanged(mOccluded); |
| 241 | if (isKeyguardLocked()) { |
| 242 | mWindowManager.deferSurfaceLayout(); |
| 243 | try { |
| 244 | mWindowManager.prepareAppTransition(resolveOccludeTransit(), |
| 245 | false /* alwaysKeepCurrent */, 0 /* flags */, true /* forceOverride */); |
| 246 | mService.updateSleepIfNeededLocked(); |
| 247 | mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS); |
| 248 | mWindowManager.executeAppTransition(); |
| 249 | } finally { |
| 250 | mWindowManager.continueSurfaceLayout(); |
| 251 | } |
| 252 | } |
| 253 | dismissDockedStackIfNeeded(); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Called when somebody might want to dismiss the Keyguard. |
| 258 | */ |
| 259 | private void handleDismissKeyguard() { |
Jorim Jaggi | 0796187 | 2016-11-23 11:28:57 +0100 | [diff] [blame^] | 260 | // We only allow dismissing Keyguard via the flag when Keyguard is secure for legacy |
| 261 | // reasons, because that's how apps used to dismiss Keyguard in the secure case. In the |
| 262 | // insecure case, we actually show it on top of the lockscreen. See #canShowWhileOccluded. |
| 263 | if (!mOccluded && mDismissingKeyguardActivity != null |
| 264 | && mWindowManager.isKeyguardSecure()) { |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 265 | mWindowManager.dismissKeyguard(null /* callback */); |
Jorim Jaggi | 0796187 | 2016-11-23 11:28:57 +0100 | [diff] [blame^] | 266 | mDismissalRequested = true; |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 267 | |
| 268 | // If we are about to unocclude the Keyguard, but we can dismiss it without security, |
| 269 | // we immediately dismiss the Keyguard so the activity gets shown without a flicker. |
| 270 | if (mKeyguardShowing && canDismissKeyguard() |
| 271 | && mWindowManager.getPendingAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) { |
| 272 | mWindowManager.prepareAppTransition(mBeforeUnoccludeTransit, |
| 273 | false /* alwaysKeepCurrent */, 0 /* flags */, true /* forceOverride */); |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 274 | mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS); |
| 275 | mWindowManager.executeAppTransition(); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * @return true if Keyguard can be currently dismissed without entering credentials. |
| 282 | */ |
| 283 | private boolean canDismissKeyguard() { |
| 284 | return mWindowManager.isKeyguardTrusted() || !mWindowManager.isKeyguardSecure(); |
| 285 | } |
| 286 | |
| 287 | private int resolveOccludeTransit() { |
| 288 | if (mBeforeUnoccludeTransit != TRANSIT_UNSET |
| 289 | && mWindowManager.getPendingAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE |
| 290 | && mOccluded) { |
| 291 | |
| 292 | // Reuse old transit in case we are occluding Keyguard again, meaning that we never |
| 293 | // actually occclude/unocclude Keyguard, but just run a normal transition. |
| 294 | return mBeforeUnoccludeTransit; |
| 295 | } else if (!mOccluded) { |
| 296 | |
| 297 | // Save transit in case we dismiss/occlude Keyguard shortly after. |
| 298 | mBeforeUnoccludeTransit = mWindowManager.getPendingAppTransition(); |
| 299 | return TRANSIT_KEYGUARD_UNOCCLUDE; |
| 300 | } else { |
| 301 | return TRANSIT_KEYGUARD_OCCLUDE; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | private void dismissDockedStackIfNeeded() { |
| 306 | if (mKeyguardShowing && mOccluded) { |
| 307 | // The lock screen is currently showing, but is occluded by a window that can |
| 308 | // show on top of the lock screen. In this can we want to dismiss the docked |
| 309 | // stack since it will be complicated/risky to try to put the activity on top |
| 310 | // of the lock screen in the right fullscreen configuration. |
| 311 | mStackSupervisor.moveTasksToFullscreenStackLocked(DOCKED_STACK_ID, |
| 312 | mStackSupervisor.mFocusedStack.getStackId() == DOCKED_STACK_ID); |
| 313 | } |
| 314 | } |
Jorim Jaggi | 8d78693 | 2016-10-26 19:08:36 -0700 | [diff] [blame] | 315 | |
| 316 | void dump(PrintWriter pw, String prefix) { |
| 317 | pw.println(prefix + "KeyguardController:"); |
| 318 | pw.println(prefix + " mKeyguardShowing=" + mKeyguardShowing); |
| 319 | pw.println(prefix + " mKeyguardGoingAway=" + mKeyguardGoingAway); |
| 320 | pw.println(prefix + " mOccluded=" + mOccluded); |
| 321 | pw.println(prefix + " mDismissingKeyguardActivity=" + mDismissingKeyguardActivity); |
Jorim Jaggi | 0796187 | 2016-11-23 11:28:57 +0100 | [diff] [blame^] | 322 | pw.println(prefix + " mDismissalRequested=" + mDismissalRequested); |
Jorim Jaggi | 8d78693 | 2016-10-26 19:08:36 -0700 | [diff] [blame] | 323 | pw.println(prefix + " mVisibilityTransactionDepth=" + mVisibilityTransactionDepth); |
| 324 | } |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 325 | } |