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 | |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 17 | package com.android.server.wm; |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 18 | |
Wale Ogunwale | dfb7fb2 | 2017-06-23 14:52:40 -0700 | [diff] [blame] | 19 | import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER; |
David Stevens | 9440dc8 | 2017-03-16 19:00:20 -0700 | [diff] [blame] | 20 | import static android.view.Display.DEFAULT_DISPLAY; |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 21 | import static android.view.WindowManager.TRANSIT_FLAG_KEYGUARD_GOING_AWAY_NO_ANIMATION; |
| 22 | import static android.view.WindowManager.TRANSIT_FLAG_KEYGUARD_GOING_AWAY_TO_SHADE; |
| 23 | import static android.view.WindowManager.TRANSIT_FLAG_KEYGUARD_GOING_AWAY_WITH_WALLPAPER; |
| 24 | import static android.view.WindowManager.TRANSIT_KEYGUARD_GOING_AWAY; |
| 25 | import static android.view.WindowManager.TRANSIT_KEYGUARD_OCCLUDE; |
| 26 | import static android.view.WindowManager.TRANSIT_KEYGUARD_UNOCCLUDE; |
| 27 | import static android.view.WindowManager.TRANSIT_UNSET; |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 28 | import static android.view.WindowManagerPolicyConstants.KEYGUARD_GOING_AWAY_FLAG_NO_WINDOW_ANIMATIONS; |
| 29 | import static android.view.WindowManagerPolicyConstants.KEYGUARD_GOING_AWAY_FLAG_TO_SHADE; |
| 30 | import static android.view.WindowManagerPolicyConstants.KEYGUARD_GOING_AWAY_FLAG_WITH_WALLPAPER; |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 31 | |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 32 | import static com.android.server.am.KeyguardControllerProto.KEYGUARD_OCCLUDED_STATES; |
Yi Jin | 6c6e9ca | 2018-03-20 16:53:35 -0700 | [diff] [blame] | 33 | import static com.android.server.am.KeyguardControllerProto.KEYGUARD_SHOWING; |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 34 | import static com.android.server.am.KeyguardOccludedProto.DISPLAY_ID; |
| 35 | import static com.android.server.am.KeyguardOccludedProto.KEYGUARD_OCCLUDED; |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame^] | 36 | import static com.android.server.wm.ActivityStackSupervisor.PRESERVE_WINDOWS; |
| 37 | import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM; |
| 38 | import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME; |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 39 | |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 40 | import android.os.IBinder; |
| 41 | import android.os.RemoteException; |
Wale Ogunwale | dfb7fb2 | 2017-06-23 14:52:40 -0700 | [diff] [blame] | 42 | import android.os.Trace; |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 43 | import android.util.Slog; |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 44 | import android.util.SparseArray; |
Steven Timotius | 4346f0a | 2017-09-12 11:07:21 -0700 | [diff] [blame] | 45 | import android.util.proto.ProtoOutputStream; |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 46 | |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 47 | import com.android.internal.policy.IKeyguardDismissCallback; |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 48 | import com.android.server.policy.WindowManagerPolicy; |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 49 | import com.android.server.wm.ActivityTaskManagerInternal.SleepToken; |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 50 | |
Jorim Jaggi | 8d78693 | 2016-10-26 19:08:36 -0700 | [diff] [blame] | 51 | import java.io.PrintWriter; |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame^] | 52 | import java.util.Arrays; |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Controls Keyguard occluding, dismissing and transitions depending on what kind of activities are |
| 56 | * currently visible. |
| 57 | * <p> |
| 58 | * Note that everything in this class should only be accessed with the AM lock being held. |
| 59 | */ |
| 60 | class KeyguardController { |
| 61 | |
Wale Ogunwale | 9887561 | 2018-10-12 07:53:02 -0700 | [diff] [blame] | 62 | private static final String TAG = TAG_WITH_CLASS_NAME ? "KeyguardController" : TAG_ATM; |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 63 | |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 64 | private final ActivityStackSupervisor mStackSupervisor; |
| 65 | private WindowManagerService mWindowManager; |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 66 | private boolean mKeyguardShowing; |
Lucas Dupin | 47a65c7 | 2018-02-15 14:16:18 -0800 | [diff] [blame] | 67 | private boolean mAodShowing; |
Jorim Jaggi | 8d78693 | 2016-10-26 19:08:36 -0700 | [diff] [blame] | 68 | private boolean mKeyguardGoingAway; |
Jorim Jaggi | 0796187 | 2016-11-23 11:28:57 +0100 | [diff] [blame] | 69 | private boolean mDismissalRequested; |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame^] | 70 | private int[] mSecondaryDisplayIdsShowing; |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 71 | private int mBeforeUnoccludeTransit; |
| 72 | private int mVisibilityTransactionDepth; |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 73 | private final SparseArray<KeyguardDisplayState> mDisplayStates = new SparseArray<>(); |
| 74 | private final ActivityTaskManagerService mService; |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 75 | |
Wale Ogunwale | f673393 | 2018-06-27 05:14:34 -0700 | [diff] [blame] | 76 | KeyguardController(ActivityTaskManagerService service, |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 77 | ActivityStackSupervisor stackSupervisor) { |
| 78 | mService = service; |
| 79 | mStackSupervisor = stackSupervisor; |
| 80 | } |
| 81 | |
| 82 | void setWindowManager(WindowManagerService windowManager) { |
| 83 | mWindowManager = windowManager; |
| 84 | } |
| 85 | |
| 86 | /** |
Lucas Dupin | 47a65c7 | 2018-02-15 14:16:18 -0800 | [diff] [blame] | 87 | * @return true if either Keyguard or AOD are showing, not going away, and not being occluded |
| 88 | * on the given display, false otherwise |
| 89 | */ |
| 90 | boolean isKeyguardOrAodShowing(int displayId) { |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 91 | return (mKeyguardShowing || mAodShowing) && !mKeyguardGoingAway |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame^] | 92 | && (displayId == DEFAULT_DISPLAY |
| 93 | ? !isDisplayOccluded(DEFAULT_DISPLAY) |
| 94 | : isShowingOnSecondaryDisplay(displayId)); |
Lucas Dupin | 47a65c7 | 2018-02-15 14:16:18 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
David Stevens | 53a39ea | 2017-08-23 18:41:49 -0700 | [diff] [blame] | 98 | * @return true if Keyguard is showing, not going away, and not being occluded on the given |
| 99 | * display, false otherwise |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 100 | */ |
David Stevens | 53a39ea | 2017-08-23 18:41:49 -0700 | [diff] [blame] | 101 | boolean isKeyguardShowing(int displayId) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame^] | 102 | return mKeyguardShowing && !mKeyguardGoingAway |
| 103 | && (displayId == DEFAULT_DISPLAY |
| 104 | ? !isDisplayOccluded(DEFAULT_DISPLAY) |
| 105 | : isShowingOnSecondaryDisplay(displayId)); |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @return true if Keyguard is either showing or occluded, but not going away |
| 110 | */ |
| 111 | boolean isKeyguardLocked() { |
| 112 | return mKeyguardShowing && !mKeyguardGoingAway; |
| 113 | } |
| 114 | |
| 115 | /** |
Bryce Lee | 271617a | 2018-03-15 10:39:12 -0700 | [diff] [blame] | 116 | * @return {@code true} if the keyguard is going away, {@code false} otherwise. |
| 117 | */ |
| 118 | boolean isKeyguardGoingAway() { |
| 119 | // Also check keyguard showing in case value is stale. |
| 120 | return mKeyguardGoingAway && mKeyguardShowing; |
| 121 | } |
| 122 | |
| 123 | /** |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 124 | * Update the Keyguard showing state. |
| 125 | */ |
Lucas Dupin | 47a65c7 | 2018-02-15 14:16:18 -0800 | [diff] [blame] | 126 | void setKeyguardShown(boolean keyguardShowing, boolean aodShowing, |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame^] | 127 | int[] secondaryDisplaysShowing) { |
Lucas Dupin | 47a65c7 | 2018-02-15 14:16:18 -0800 | [diff] [blame] | 128 | boolean showingChanged = keyguardShowing != mKeyguardShowing || aodShowing != mAodShowing; |
Adrian Roos | 6ec76b7 | 2018-04-25 14:01:11 +0200 | [diff] [blame] | 129 | // If keyguard is going away, but SystemUI aborted the transition, need to reset state. |
| 130 | showingChanged |= mKeyguardGoingAway && keyguardShowing; |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame^] | 131 | if (!showingChanged && Arrays.equals(secondaryDisplaysShowing, |
| 132 | mSecondaryDisplayIdsShowing)) { |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 133 | return; |
| 134 | } |
Lucas Dupin | 47a65c7 | 2018-02-15 14:16:18 -0800 | [diff] [blame] | 135 | mKeyguardShowing = keyguardShowing; |
| 136 | mAodShowing = aodShowing; |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame^] | 137 | mSecondaryDisplayIdsShowing = secondaryDisplaysShowing; |
chaviw | 0e9fb13 | 2018-06-05 16:29:13 -0700 | [diff] [blame] | 138 | mWindowManager.setAodShowing(aodShowing); |
David Stevens | 53a39ea | 2017-08-23 18:41:49 -0700 | [diff] [blame] | 139 | if (showingChanged) { |
| 140 | dismissDockedStackIfNeeded(); |
Andrii Kulian | 0d595f3 | 2018-02-21 15:47:33 -0800 | [diff] [blame] | 141 | setKeyguardGoingAway(false); |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 142 | // TODO(b/113840485): Check usage for non-default display |
Adrian Roos | b56b3c3 | 2018-05-17 17:32:26 +0200 | [diff] [blame] | 143 | mWindowManager.setKeyguardOrAodShowingOnDefaultDisplay( |
| 144 | isKeyguardOrAodShowing(DEFAULT_DISPLAY)); |
Lucas Dupin | 47a65c7 | 2018-02-15 14:16:18 -0800 | [diff] [blame] | 145 | if (keyguardShowing) { |
David Stevens | 53a39ea | 2017-08-23 18:41:49 -0700 | [diff] [blame] | 146 | mDismissalRequested = false; |
| 147 | } |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 148 | } |
| 149 | mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS); |
David Stevens | 9440dc8 | 2017-03-16 19:00:20 -0700 | [diff] [blame] | 150 | updateKeyguardSleepToken(); |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 151 | } |
| 152 | |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame^] | 153 | private boolean isShowingOnSecondaryDisplay(int displayId) { |
| 154 | if (mSecondaryDisplayIdsShowing == null) return false; |
| 155 | for (int showingId : mSecondaryDisplayIdsShowing) { |
| 156 | if (displayId == showingId) return true; |
| 157 | } |
| 158 | return false; |
| 159 | } |
| 160 | |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 161 | /** |
| 162 | * Called when Keyguard is going away. |
| 163 | * |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 164 | * @param flags See {@link WindowManagerPolicy#KEYGUARD_GOING_AWAY_FLAG_TO_SHADE} |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 165 | * etc. |
| 166 | */ |
| 167 | void keyguardGoingAway(int flags) { |
Wale Ogunwale | dfb7fb2 | 2017-06-23 14:52:40 -0700 | [diff] [blame] | 168 | if (!mKeyguardShowing) { |
| 169 | return; |
| 170 | } |
| 171 | Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "keyguardGoingAway"); |
| 172 | mWindowManager.deferSurfaceLayout(); |
| 173 | try { |
| 174 | setKeyguardGoingAway(true); |
lumark | 588a3e8 | 2018-07-20 18:53:54 +0800 | [diff] [blame] | 175 | mStackSupervisor.getDefaultDisplay().getWindowContainerController() |
| 176 | .prepareAppTransition(TRANSIT_KEYGUARD_GOING_AWAY, |
| 177 | false /* alwaysKeepCurrent */, convertTransitFlags(flags), |
| 178 | false /* forceOverride */); |
David Stevens | 9440dc8 | 2017-03-16 19:00:20 -0700 | [diff] [blame] | 179 | updateKeyguardSleepToken(); |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 180 | |
Wale Ogunwale | dfb7fb2 | 2017-06-23 14:52:40 -0700 | [diff] [blame] | 181 | // Some stack visibility might change (e.g. docked stack) |
Andrii Kulian | ab132ee | 2018-07-24 22:10:21 +0800 | [diff] [blame] | 182 | mStackSupervisor.resumeFocusedStacksTopActivitiesLocked(); |
Wale Ogunwale | dfb7fb2 | 2017-06-23 14:52:40 -0700 | [diff] [blame] | 183 | mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS); |
| 184 | mStackSupervisor.addStartingWindowsForVisibleActivities(true /* taskSwitch */); |
| 185 | mWindowManager.executeAppTransition(); |
| 186 | } finally { |
| 187 | Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "keyguardGoingAway: surfaceLayout"); |
| 188 | mWindowManager.continueSurfaceLayout(); |
| 189 | Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER); |
| 190 | |
| 191 | Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER); |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Lucas Dupin | c80c67e | 2017-12-04 14:29:10 -0800 | [diff] [blame] | 195 | void dismissKeyguard(IBinder token, IKeyguardDismissCallback callback, CharSequence message) { |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 196 | final ActivityRecord activityRecord = ActivityRecord.forTokenLocked(token); |
| 197 | if (activityRecord == null || !activityRecord.visibleIgnoringKeyguard) { |
| 198 | failCallback(callback); |
| 199 | return; |
| 200 | } |
Jorim Jaggi | d721489 | 2017-07-18 14:05:19 +0200 | [diff] [blame] | 201 | Slog.i(TAG, "Activity requesting to dismiss Keyguard: " + activityRecord); |
chaviw | 59b9885 | 2017-06-13 12:05:44 -0700 | [diff] [blame] | 202 | |
| 203 | // If the client has requested to dismiss the keyguard and the Activity has the flag to |
| 204 | // turn the screen on, wakeup the screen if it's the top Activity. |
| 205 | if (activityRecord.getTurnScreenOnFlag() && activityRecord.isTopRunningActivity()) { |
| 206 | mStackSupervisor.wakeUp("dismissKeyguard"); |
| 207 | } |
| 208 | |
Lucas Dupin | c80c67e | 2017-12-04 14:29:10 -0800 | [diff] [blame] | 209 | mWindowManager.dismissKeyguard(callback, message); |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Wale Ogunwale | bfa81ad | 2017-05-24 15:14:42 -0700 | [diff] [blame] | 212 | private void setKeyguardGoingAway(boolean keyguardGoingAway) { |
| 213 | mKeyguardGoingAway = keyguardGoingAway; |
| 214 | mWindowManager.setKeyguardGoingAway(keyguardGoingAway); |
| 215 | } |
| 216 | |
Jorim Jaggi | 241ae10 | 2016-11-02 21:57:33 -0700 | [diff] [blame] | 217 | private void failCallback(IKeyguardDismissCallback callback) { |
| 218 | try { |
| 219 | callback.onDismissError(); |
| 220 | } catch (RemoteException e) { |
| 221 | Slog.w(TAG, "Failed to call callback", e); |
| 222 | } |
| 223 | } |
| 224 | |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 225 | private int convertTransitFlags(int keyguardGoingAwayFlags) { |
| 226 | int result = 0; |
| 227 | if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_TO_SHADE) != 0) { |
| 228 | result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_TO_SHADE; |
| 229 | } |
| 230 | if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_NO_WINDOW_ANIMATIONS) != 0) { |
| 231 | result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_NO_ANIMATION; |
| 232 | } |
| 233 | if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_WITH_WALLPAPER) != 0) { |
| 234 | result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_WITH_WALLPAPER; |
| 235 | } |
| 236 | return result; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Starts a batch of visibility updates. |
| 241 | */ |
| 242 | void beginActivityVisibilityUpdate() { |
| 243 | mVisibilityTransactionDepth++; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Ends a batch of visibility updates. After all batches are done, this method makes sure to |
| 248 | * update lockscreen occluded/dismiss state if needed. |
| 249 | */ |
| 250 | void endActivityVisibilityUpdate() { |
| 251 | mVisibilityTransactionDepth--; |
| 252 | if (mVisibilityTransactionDepth == 0) { |
| 253 | visibilitiesUpdated(); |
| 254 | } |
| 255 | } |
| 256 | |
Jorim Jaggi | e69c931 | 2016-10-31 18:24:38 -0700 | [diff] [blame] | 257 | /** |
| 258 | * @return True if we may show an activity while Keyguard is showing because we are in the |
| 259 | * process of dismissing it anyways, false otherwise. |
| 260 | */ |
Jorim Jaggi | 0796187 | 2016-11-23 11:28:57 +0100 | [diff] [blame] | 261 | boolean canShowActivityWhileKeyguardShowing(ActivityRecord r, boolean dismissKeyguard) { |
| 262 | |
| 263 | // Allow to show it when we are about to dismiss Keyguard. This isn't allowed if r is |
| 264 | // already the dismissing activity, in which case we don't allow it to repeatedly dismiss |
| 265 | // Keyguard. |
Lucas Dupin | 47a65c7 | 2018-02-15 14:16:18 -0800 | [diff] [blame] | 266 | return dismissKeyguard && canDismissKeyguard() && !mAodShowing |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 267 | && (mDismissalRequested |
| 268 | || getDisplay(r.getDisplayId()).mDismissingKeyguardActivity != r); |
Jorim Jaggi | 0796187 | 2016-11-23 11:28:57 +0100 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /** |
| 272 | * @return True if we may show an activity while Keyguard is occluded, false otherwise. |
| 273 | */ |
| 274 | boolean canShowWhileOccluded(boolean dismissKeyguard, boolean showWhenLocked) { |
| 275 | return showWhenLocked || dismissKeyguard && !mWindowManager.isKeyguardSecure(); |
Jorim Jaggi | e69c931 | 2016-10-31 18:24:38 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 278 | private void visibilitiesUpdated() { |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 279 | boolean requestDismissKeyguard = false; |
Pat Plunkett | 40426e0 | 2017-10-31 14:06:29 -0700 | [diff] [blame] | 280 | for (int displayNdx = mStackSupervisor.getChildCount() - 1; displayNdx >= 0; displayNdx--) { |
| 281 | final ActivityDisplay display = mStackSupervisor.getChildAt(displayNdx); |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 282 | final KeyguardDisplayState state = getDisplay(display.mDisplayId); |
| 283 | state.visibilitiesUpdated(this, display); |
| 284 | requestDismissKeyguard |= state.mRequestDismissKeyguard; |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 285 | } |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 286 | |
| 287 | // Dismissing Keyguard happens globally using the information from all displays. |
| 288 | if (requestDismissKeyguard) { |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 289 | handleDismissKeyguard(); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Called when occluded state changed. |
| 295 | */ |
| 296 | private void handleOccludedChanged() { |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 297 | mWindowManager.onKeyguardOccludedChanged(isDisplayOccluded(DEFAULT_DISPLAY)); |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 298 | if (isKeyguardLocked()) { |
| 299 | mWindowManager.deferSurfaceLayout(); |
| 300 | try { |
lumark | 588a3e8 | 2018-07-20 18:53:54 +0800 | [diff] [blame] | 301 | mStackSupervisor.getDefaultDisplay().getWindowContainerController() |
| 302 | .prepareAppTransition(resolveOccludeTransit(), |
| 303 | false /* alwaysKeepCurrent */, 0 /* flags */, |
| 304 | true /* forceOverride */); |
David Stevens | 9440dc8 | 2017-03-16 19:00:20 -0700 | [diff] [blame] | 305 | updateKeyguardSleepToken(); |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 306 | mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS); |
| 307 | mWindowManager.executeAppTransition(); |
| 308 | } finally { |
| 309 | mWindowManager.continueSurfaceLayout(); |
| 310 | } |
| 311 | } |
| 312 | dismissDockedStackIfNeeded(); |
| 313 | } |
| 314 | |
| 315 | /** |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 316 | * Called when somebody wants to dismiss the Keyguard via the flag. |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 317 | */ |
| 318 | private void handleDismissKeyguard() { |
Jorim Jaggi | 0796187 | 2016-11-23 11:28:57 +0100 | [diff] [blame] | 319 | // We only allow dismissing Keyguard via the flag when Keyguard is secure for legacy |
| 320 | // reasons, because that's how apps used to dismiss Keyguard in the secure case. In the |
| 321 | // insecure case, we actually show it on top of the lockscreen. See #canShowWhileOccluded. |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 322 | if (mWindowManager.isKeyguardSecure()) { |
Lucas Dupin | c80c67e | 2017-12-04 14:29:10 -0800 | [diff] [blame] | 323 | mWindowManager.dismissKeyguard(null /* callback */, null /* message */); |
Jorim Jaggi | 0796187 | 2016-11-23 11:28:57 +0100 | [diff] [blame] | 324 | mDismissalRequested = true; |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 325 | |
| 326 | // If we are about to unocclude the Keyguard, but we can dismiss it without security, |
| 327 | // we immediately dismiss the Keyguard so the activity gets shown without a flicker. |
lumark | 588a3e8 | 2018-07-20 18:53:54 +0800 | [diff] [blame] | 328 | final DisplayWindowController dwc = |
| 329 | mStackSupervisor.getDefaultDisplay().getWindowContainerController(); |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 330 | if (mKeyguardShowing && canDismissKeyguard() |
lumark | 588a3e8 | 2018-07-20 18:53:54 +0800 | [diff] [blame] | 331 | && dwc.getPendingAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) { |
| 332 | dwc.prepareAppTransition(mBeforeUnoccludeTransit, false /* alwaysKeepCurrent */, |
| 333 | 0 /* flags */, true /* forceOverride */); |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 334 | mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS); |
| 335 | mWindowManager.executeAppTransition(); |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 340 | private boolean isDisplayOccluded(int displayId) { |
| 341 | return getDisplay(displayId).mOccluded; |
| 342 | } |
| 343 | |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 344 | /** |
| 345 | * @return true if Keyguard can be currently dismissed without entering credentials. |
| 346 | */ |
Andrii Kulian | fc8f82b | 2017-01-26 13:17:27 -0800 | [diff] [blame] | 347 | boolean canDismissKeyguard() { |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 348 | return mWindowManager.isKeyguardTrusted() || !mWindowManager.isKeyguardSecure(); |
| 349 | } |
| 350 | |
| 351 | private int resolveOccludeTransit() { |
lumark | 588a3e8 | 2018-07-20 18:53:54 +0800 | [diff] [blame] | 352 | final DisplayWindowController dwc = |
| 353 | mStackSupervisor.getDefaultDisplay().getWindowContainerController(); |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 354 | if (mBeforeUnoccludeTransit != TRANSIT_UNSET |
lumark | 588a3e8 | 2018-07-20 18:53:54 +0800 | [diff] [blame] | 355 | && dwc.getPendingAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 356 | // TODO(b/113840485): Handle app transition for individual display. |
| 357 | && isDisplayOccluded(DEFAULT_DISPLAY)) { |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 358 | |
| 359 | // Reuse old transit in case we are occluding Keyguard again, meaning that we never |
| 360 | // actually occclude/unocclude Keyguard, but just run a normal transition. |
| 361 | return mBeforeUnoccludeTransit; |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 362 | // TODO(b/113840485): Handle app transition for individual display. |
| 363 | } else if (!isDisplayOccluded(DEFAULT_DISPLAY)) { |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 364 | |
| 365 | // Save transit in case we dismiss/occlude Keyguard shortly after. |
lumark | 588a3e8 | 2018-07-20 18:53:54 +0800 | [diff] [blame] | 366 | mBeforeUnoccludeTransit = dwc.getPendingAppTransition(); |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 367 | return TRANSIT_KEYGUARD_UNOCCLUDE; |
| 368 | } else { |
| 369 | return TRANSIT_KEYGUARD_OCCLUDE; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | private void dismissDockedStackIfNeeded() { |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 374 | // TODO(b/113840485): Handle docked stack for individual display. |
| 375 | if (mKeyguardShowing && isDisplayOccluded(DEFAULT_DISPLAY)) { |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 376 | // The lock screen is currently showing, but is occluded by a window that can |
| 377 | // show on top of the lock screen. In this can we want to dismiss the docked |
| 378 | // stack since it will be complicated/risky to try to put the activity on top |
| 379 | // of the lock screen in the right fullscreen configuration. |
Wale Ogunwale | a0f5b5e | 2017-10-11 09:37:23 -0700 | [diff] [blame] | 380 | final ActivityStack stack = mStackSupervisor.getDefaultDisplay().getSplitScreenPrimaryStack(); |
Wale Ogunwale | 9dcf946 | 2017-09-19 15:13:01 -0700 | [diff] [blame] | 381 | if (stack == null) { |
| 382 | return; |
| 383 | } |
| 384 | mStackSupervisor.moveTasksToFullscreenStackLocked(stack, |
Andrii Kulian | 52d255c | 2018-07-13 11:32:19 -0700 | [diff] [blame] | 385 | stack.isFocusedStackOnDisplay()); |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 386 | } |
| 387 | } |
Jorim Jaggi | 8d78693 | 2016-10-26 19:08:36 -0700 | [diff] [blame] | 388 | |
David Stevens | 9440dc8 | 2017-03-16 19:00:20 -0700 | [diff] [blame] | 389 | private void updateKeyguardSleepToken() { |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 390 | for (int displayNdx = mStackSupervisor.getChildCount() - 1; displayNdx >= 0; displayNdx--) { |
| 391 | final ActivityDisplay display = mStackSupervisor.getChildAt(displayNdx); |
| 392 | final KeyguardDisplayState state = getDisplay(display.mDisplayId); |
| 393 | if (isKeyguardOrAodShowing(display.mDisplayId) && state.mSleepToken == null) { |
| 394 | state.acquiredSleepToken(); |
| 395 | } else if (!isKeyguardOrAodShowing(display.mDisplayId) && state.mSleepToken != null) { |
| 396 | state.releaseSleepToken(); |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | private KeyguardDisplayState getDisplay(int displayId) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame^] | 402 | KeyguardDisplayState state = mDisplayStates.get(displayId); |
| 403 | if (state == null) { |
| 404 | state = new KeyguardDisplayState(mService, displayId); |
| 405 | mDisplayStates.append(displayId, state); |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 406 | } |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame^] | 407 | return state; |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | void onDisplayRemoved(int displayId) { |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame^] | 411 | final KeyguardDisplayState state = mDisplayStates.get(displayId); |
| 412 | if (state != null) { |
| 413 | state.onRemoved(); |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 414 | mDisplayStates.remove(displayId); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /** Represents Keyguard state per individual display. */ |
| 419 | private static class KeyguardDisplayState { |
| 420 | private final int mDisplayId; |
| 421 | private boolean mOccluded; |
| 422 | private ActivityRecord mDismissingKeyguardActivity; |
| 423 | private boolean mRequestDismissKeyguard; |
| 424 | private final ActivityTaskManagerService mService; |
| 425 | private SleepToken mSleepToken; |
| 426 | |
| 427 | KeyguardDisplayState(ActivityTaskManagerService service, int displayId) { |
| 428 | mService = service; |
| 429 | mDisplayId = displayId; |
| 430 | } |
| 431 | |
| 432 | void onRemoved() { |
| 433 | mDismissingKeyguardActivity = null; |
| 434 | releaseSleepToken(); |
| 435 | } |
| 436 | |
| 437 | void acquiredSleepToken() { |
| 438 | if (mSleepToken == null) { |
| 439 | mSleepToken = mService.acquireSleepToken("keyguard", mDisplayId); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | void releaseSleepToken() { |
| 444 | if (mSleepToken != null) { |
| 445 | mSleepToken.release(); |
| 446 | mSleepToken = null; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | void visibilitiesUpdated(KeyguardController controller, ActivityDisplay display) { |
| 451 | final boolean lastOccluded = mOccluded; |
| 452 | final ActivityRecord lastDismissActivity = mDismissingKeyguardActivity; |
| 453 | mRequestDismissKeyguard = false; |
| 454 | mOccluded = false; |
| 455 | mDismissingKeyguardActivity = null; |
| 456 | |
| 457 | // Only the top activity of the focused stack on each display may control it's |
| 458 | // occluded state. |
| 459 | final ActivityStack focusedStack = display.getFocusedStack(); |
| 460 | if (focusedStack != null) { |
| 461 | final ActivityRecord topDismissing = |
| 462 | focusedStack.getTopDismissingKeyguardActivity(); |
| 463 | mOccluded = focusedStack.topActivityOccludesKeyguard() || (topDismissing != null |
| 464 | && focusedStack.topRunningActivityLocked() == topDismissing |
| 465 | && controller.canShowWhileOccluded( |
| 466 | true /* dismissKeyguard */, |
| 467 | false /* showWhenLocked */)); |
| 468 | if (focusedStack.getTopDismissingKeyguardActivity() != null) { |
| 469 | mDismissingKeyguardActivity = focusedStack.getTopDismissingKeyguardActivity(); |
| 470 | } |
| 471 | mOccluded |= controller.mWindowManager.isShowingDream(); |
| 472 | } |
| 473 | |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame^] | 474 | // TODO(b/113840485): Handle app transition for individual display, and apply occluded |
| 475 | // state change to secondary displays. |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 476 | // For now, only default display can change occluded. |
| 477 | if (lastOccluded != mOccluded && mDisplayId == DEFAULT_DISPLAY) { |
| 478 | controller.handleOccludedChanged(); |
| 479 | } |
| 480 | if (lastDismissActivity != mDismissingKeyguardActivity && !mOccluded |
| 481 | && mDismissingKeyguardActivity != null |
| 482 | && controller.mWindowManager.isKeyguardSecure()) { |
| 483 | mRequestDismissKeyguard = true; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | void dumpStatus(PrintWriter pw, String prefix) { |
| 488 | final StringBuilder sb = new StringBuilder(); |
| 489 | sb.append(prefix); |
| 490 | sb.append(" Occluded=").append(mOccluded) |
| 491 | .append(" DismissingKeyguardActivity=") |
| 492 | .append(mDismissingKeyguardActivity) |
| 493 | .append(" at display=") |
| 494 | .append(mDisplayId); |
| 495 | pw.println(sb.toString()); |
| 496 | } |
| 497 | |
| 498 | void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 499 | final long token = proto.start(fieldId); |
| 500 | proto.write(DISPLAY_ID, mDisplayId); |
| 501 | proto.write(KEYGUARD_OCCLUDED, mOccluded); |
| 502 | proto.end(token); |
David Stevens | 9440dc8 | 2017-03-16 19:00:20 -0700 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
Jorim Jaggi | 8d78693 | 2016-10-26 19:08:36 -0700 | [diff] [blame] | 506 | void dump(PrintWriter pw, String prefix) { |
| 507 | pw.println(prefix + "KeyguardController:"); |
| 508 | pw.println(prefix + " mKeyguardShowing=" + mKeyguardShowing); |
Lucas Dupin | 47a65c7 | 2018-02-15 14:16:18 -0800 | [diff] [blame] | 509 | pw.println(prefix + " mAodShowing=" + mAodShowing); |
Jorim Jaggi | 8d78693 | 2016-10-26 19:08:36 -0700 | [diff] [blame] | 510 | pw.println(prefix + " mKeyguardGoingAway=" + mKeyguardGoingAway); |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 511 | dumpDisplayStates(pw, prefix); |
Jorim Jaggi | 0796187 | 2016-11-23 11:28:57 +0100 | [diff] [blame] | 512 | pw.println(prefix + " mDismissalRequested=" + mDismissalRequested); |
Jorim Jaggi | 8d78693 | 2016-10-26 19:08:36 -0700 | [diff] [blame] | 513 | pw.println(prefix + " mVisibilityTransactionDepth=" + mVisibilityTransactionDepth); |
| 514 | } |
Steven Timotius | 4346f0a | 2017-09-12 11:07:21 -0700 | [diff] [blame] | 515 | |
| 516 | void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 517 | final long token = proto.start(fieldId); |
| 518 | proto.write(KEYGUARD_SHOWING, mKeyguardShowing); |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 519 | writeDisplayStatesToProto(proto, KEYGUARD_OCCLUDED_STATES); |
Steven Timotius | 4346f0a | 2017-09-12 11:07:21 -0700 | [diff] [blame] | 520 | proto.end(token); |
| 521 | } |
wilsonshih | 0299c8a | 2018-08-24 15:52:57 +0800 | [diff] [blame] | 522 | |
| 523 | private void dumpDisplayStates(PrintWriter pw, String prefix) { |
| 524 | for (int i = 0; i < mDisplayStates.size(); i++) { |
| 525 | mDisplayStates.valueAt(i).dumpStatus(pw, prefix); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | private void writeDisplayStatesToProto(ProtoOutputStream proto, long fieldId) { |
| 530 | for (int i = 0; i < mDisplayStates.size(); i++) { |
| 531 | mDisplayStates.valueAt(i).writeToProto(proto, fieldId); |
| 532 | } |
| 533 | } |
Jorim Jaggi | fe76234 | 2016-10-13 14:33:27 +0200 | [diff] [blame] | 534 | } |