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