blob: 4c10d5819c1043e0e5ad53feaf83e4dedcf5bd10 [file] [log] [blame]
Jorim Jaggife762342016-10-13 14:33:27 +02001/*
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 Ogunwale59507092018-10-29 09:00:30 -070017package com.android.server.wm;
Jorim Jaggife762342016-10-13 14:33:27 +020018
Riddle Hsu2ca561b2019-10-08 21:58:58 +080019import static android.os.Trace.TRACE_TAG_WINDOW_MANAGER;
David Stevens9440dc82017-03-16 19:00:20 -070020import static android.view.Display.DEFAULT_DISPLAY;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070021import static android.view.WindowManager.TRANSIT_FLAG_KEYGUARD_GOING_AWAY_NO_ANIMATION;
Issei Suzuki5609ccb2019-06-13 15:04:08 +020022import static android.view.WindowManager.TRANSIT_FLAG_KEYGUARD_GOING_AWAY_SUBTLE_ANIMATION;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070023import static android.view.WindowManager.TRANSIT_FLAG_KEYGUARD_GOING_AWAY_TO_SHADE;
24import static android.view.WindowManager.TRANSIT_FLAG_KEYGUARD_GOING_AWAY_WITH_WALLPAPER;
25import static android.view.WindowManager.TRANSIT_KEYGUARD_GOING_AWAY;
26import static android.view.WindowManager.TRANSIT_KEYGUARD_OCCLUDE;
27import static android.view.WindowManager.TRANSIT_KEYGUARD_UNOCCLUDE;
28import static android.view.WindowManager.TRANSIT_UNSET;
Adrian Roose99bc052017-11-20 17:55:31 +010029import static android.view.WindowManagerPolicyConstants.KEYGUARD_GOING_AWAY_FLAG_NO_WINDOW_ANIMATIONS;
Issei Suzuki5609ccb2019-06-13 15:04:08 +020030import static android.view.WindowManagerPolicyConstants.KEYGUARD_GOING_AWAY_FLAG_SUBTLE_WINDOW_ANIMATIONS;
Adrian Roose99bc052017-11-20 17:55:31 +010031import static android.view.WindowManagerPolicyConstants.KEYGUARD_GOING_AWAY_FLAG_TO_SHADE;
32import static android.view.WindowManagerPolicyConstants.KEYGUARD_GOING_AWAY_FLAG_WITH_WALLPAPER;
wilsonshih0299c8a2018-08-24 15:52:57 +080033
Evan Roskyaf9f27c2020-02-18 18:58:35 +000034import static com.android.server.wm.ActivityStackSupervisor.PRESERVE_WINDOWS;
35import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM;
36import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME;
Wale Ogunwalef342f062020-01-27 07:34:13 -080037import static com.android.server.wm.KeyguardControllerProto.AOD_SHOWING;
38import static com.android.server.wm.KeyguardControllerProto.KEYGUARD_OCCLUDED_STATES;
39import static com.android.server.wm.KeyguardControllerProto.KEYGUARD_SHOWING;
40import static com.android.server.wm.KeyguardOccludedProto.DISPLAY_ID;
41import static com.android.server.wm.KeyguardOccludedProto.KEYGUARD_OCCLUDED;
Jorim Jaggife762342016-10-13 14:33:27 +020042
Jorim Jaggi241ae102016-11-02 21:57:33 -070043import android.os.IBinder;
44import android.os.RemoteException;
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -070045import android.os.Trace;
Jorim Jaggi241ae102016-11-02 21:57:33 -070046import android.util.Slog;
wilsonshih0299c8a2018-08-24 15:52:57 +080047import android.util.SparseArray;
Steven Timotius4346f0a2017-09-12 11:07:21 -070048import android.util.proto.ProtoOutputStream;
Adrian Roose99bc052017-11-20 17:55:31 +010049
Jorim Jaggi241ae102016-11-02 21:57:33 -070050import com.android.internal.policy.IKeyguardDismissCallback;
Adrian Roose99bc052017-11-20 17:55:31 +010051import com.android.server.policy.WindowManagerPolicy;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070052import com.android.server.wm.ActivityTaskManagerInternal.SleepToken;
Adrian Roose99bc052017-11-20 17:55:31 +010053
Jorim Jaggi8d786932016-10-26 19:08:36 -070054import java.io.PrintWriter;
Jorim Jaggife762342016-10-13 14:33:27 +020055
56/**
57 * Controls Keyguard occluding, dismissing and transitions depending on what kind of activities are
58 * currently visible.
59 * <p>
60 * Note that everything in this class should only be accessed with the AM lock being held.
61 */
62class KeyguardController {
63
Wale Ogunwale98875612018-10-12 07:53:02 -070064 private static final String TAG = TAG_WITH_CLASS_NAME ? "KeyguardController" : TAG_ATM;
Jorim Jaggi241ae102016-11-02 21:57:33 -070065
Jorim Jaggife762342016-10-13 14:33:27 +020066 private final ActivityStackSupervisor mStackSupervisor;
67 private WindowManagerService mWindowManager;
Jorim Jaggife762342016-10-13 14:33:27 +020068 private boolean mKeyguardShowing;
Lucas Dupin47a65c72018-02-15 14:16:18 -080069 private boolean mAodShowing;
Jorim Jaggi8d786932016-10-26 19:08:36 -070070 private boolean mKeyguardGoingAway;
Jorim Jaggi07961872016-11-23 11:28:57 +010071 private boolean mDismissalRequested;
Jorim Jaggife762342016-10-13 14:33:27 +020072 private int mBeforeUnoccludeTransit;
wilsonshih0299c8a2018-08-24 15:52:57 +080073 private final SparseArray<KeyguardDisplayState> mDisplayStates = new SparseArray<>();
74 private final ActivityTaskManagerService mService;
Louis Chang149d5c82019-12-30 09:47:39 +080075 private RootWindowContainer mRootWindowContainer;
Jorim Jaggife762342016-10-13 14:33:27 +020076
Wale Ogunwalef6733932018-06-27 05:14:34 -070077 KeyguardController(ActivityTaskManagerService service,
Jorim Jaggife762342016-10-13 14:33:27 +020078 ActivityStackSupervisor stackSupervisor) {
79 mService = service;
80 mStackSupervisor = stackSupervisor;
81 }
82
83 void setWindowManager(WindowManagerService windowManager) {
84 mWindowManager = windowManager;
Louis Chang149d5c82019-12-30 09:47:39 +080085 mRootWindowContainer = mService.mRootWindowContainer;
Jorim Jaggife762342016-10-13 14:33:27 +020086 }
87
88 /**
Issei Suzuki16d4de52019-07-29 13:12:01 +000089 * @return true if either Keyguard or AOD are showing, not going away, and not being occluded
90 * on the given display, false otherwise.
Lucas Dupin47a65c72018-02-15 14:16:18 -080091 */
92 boolean isKeyguardOrAodShowing(int displayId) {
Issei Suzuki16d4de52019-07-29 13:12:01 +000093 return (mKeyguardShowing || mAodShowing) && !mKeyguardGoingAway
94 && !isDisplayOccluded(displayId);
95 }
96
97 /**
98 * @return {@code true} for default display when AOD is showing. Otherwise, same as
99 * {@link #isKeyguardOrAodShowing(int)}
100 * TODO(b/125198167): Replace isKeyguardOrAodShowing() by this logic.
101 */
102 boolean isKeyguardUnoccludedOrAodShowing(int displayId) {
103 if (displayId == DEFAULT_DISPLAY && mAodShowing) {
104 return true;
105 }
106 return isKeyguardOrAodShowing(displayId);
Issei Suzukid6eb5a22019-02-20 23:08:03 +0100107 }
108
109 /**
David Stevens53a39ea2017-08-23 18:41:49 -0700110 * @return true if Keyguard is showing, not going away, and not being occluded on the given
111 * display, false otherwise
Jorim Jaggife762342016-10-13 14:33:27 +0200112 */
David Stevens53a39ea2017-08-23 18:41:49 -0700113 boolean isKeyguardShowing(int displayId) {
Issei Suzuki16d4de52019-07-29 13:12:01 +0000114 return mKeyguardShowing && !mKeyguardGoingAway && !isDisplayOccluded(displayId);
Jorim Jaggife762342016-10-13 14:33:27 +0200115 }
116
117 /**
118 * @return true if Keyguard is either showing or occluded, but not going away
119 */
120 boolean isKeyguardLocked() {
121 return mKeyguardShowing && !mKeyguardGoingAway;
122 }
123
124 /**
Bryce Lee271617a2018-03-15 10:39:12 -0700125 * @return {@code true} if the keyguard is going away, {@code false} otherwise.
126 */
127 boolean isKeyguardGoingAway() {
128 // Also check keyguard showing in case value is stale.
129 return mKeyguardGoingAway && mKeyguardShowing;
130 }
131
132 /**
Jorim Jaggife762342016-10-13 14:33:27 +0200133 * Update the Keyguard showing state.
134 */
wilsonshih177261f2019-02-22 12:02:18 +0800135 void setKeyguardShown(boolean keyguardShowing, boolean aodShowing) {
Adrian Roos6ec76b72018-04-25 14:01:11 +0200136 // If keyguard is going away, but SystemUI aborted the transition, need to reset state.
wilsonshih2bc846c2019-05-02 14:35:21 +0800137 final boolean keyguardChanged = keyguardShowing != mKeyguardShowing
138 || mKeyguardGoingAway && keyguardShowing;
139 final boolean aodChanged = aodShowing != mAodShowing;
140 if (!keyguardChanged && !aodChanged) {
Jorim Jaggife762342016-10-13 14:33:27 +0200141 return;
142 }
Jeff Changd136e772019-11-05 20:33:52 +0800143 EventLogTags.writeWmSetKeyguardShown(
wilsonshih30eca702019-07-10 09:22:11 +0800144 keyguardShowing ? 1 : 0,
145 aodShowing ? 1 : 0,
146 mKeyguardGoingAway ? 1 : 0,
147 "setKeyguardShown");
Lucas Dupin47a65c72018-02-15 14:16:18 -0800148 mKeyguardShowing = keyguardShowing;
149 mAodShowing = aodShowing;
chaviw0e9fb132018-06-05 16:29:13 -0700150 mWindowManager.setAodShowing(aodShowing);
wilsonshih2bc846c2019-05-02 14:35:21 +0800151
152 if (keyguardChanged) {
153 // Irrelevant to AOD.
David Stevens53a39ea2017-08-23 18:41:49 -0700154 dismissDockedStackIfNeeded();
Andrii Kulian0d595f32018-02-21 15:47:33 -0800155 setKeyguardGoingAway(false);
Lucas Dupin47a65c72018-02-15 14:16:18 -0800156 if (keyguardShowing) {
David Stevens53a39ea2017-08-23 18:41:49 -0700157 mDismissalRequested = false;
158 }
Jorim Jaggife762342016-10-13 14:33:27 +0200159 }
wilsonshih2bc846c2019-05-02 14:35:21 +0800160 // TODO(b/113840485): Check usage for non-default display
161 mWindowManager.setKeyguardOrAodShowingOnDefaultDisplay(
162 isKeyguardOrAodShowing(DEFAULT_DISPLAY));
163
164 // Update the sleep token first such that ensureActivitiesVisible has correct sleep token
165 // state when evaluating visibilities.
David Stevens9440dc82017-03-16 19:00:20 -0700166 updateKeyguardSleepToken();
Louis Chang149d5c82019-12-30 09:47:39 +0800167 mRootWindowContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
Jorim Jaggife762342016-10-13 14:33:27 +0200168 }
169
170 /**
171 * Called when Keyguard is going away.
172 *
Adrian Roose99bc052017-11-20 17:55:31 +0100173 * @param flags See {@link WindowManagerPolicy#KEYGUARD_GOING_AWAY_FLAG_TO_SHADE}
Jorim Jaggife762342016-10-13 14:33:27 +0200174 * etc.
175 */
176 void keyguardGoingAway(int flags) {
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -0700177 if (!mKeyguardShowing) {
178 return;
179 }
Riddle Hsu2ca561b2019-10-08 21:58:58 +0800180 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "keyguardGoingAway");
Riddle Hsua0022cd2019-09-09 21:12:41 +0800181 mService.deferWindowLayout();
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -0700182 try {
183 setKeyguardGoingAway(true);
Jeff Changd136e772019-11-05 20:33:52 +0800184 EventLogTags.writeWmSetKeyguardShown(
wilsonshih30eca702019-07-10 09:22:11 +0800185 1 /* keyguardShowing */,
186 mAodShowing ? 1 : 0,
187 1 /* keyguardGoingAway */,
188 "keyguardGoingAway");
Louis Chang149d5c82019-12-30 09:47:39 +0800189 mRootWindowContainer.getDefaultDisplay().mDisplayContent
lumark588a3e82018-07-20 18:53:54 +0800190 .prepareAppTransition(TRANSIT_KEYGUARD_GOING_AWAY,
191 false /* alwaysKeepCurrent */, convertTransitFlags(flags),
192 false /* forceOverride */);
David Stevens9440dc82017-03-16 19:00:20 -0700193 updateKeyguardSleepToken();
Jorim Jaggife762342016-10-13 14:33:27 +0200194
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -0700195 // Some stack visibility might change (e.g. docked stack)
Louis Chang149d5c82019-12-30 09:47:39 +0800196 mRootWindowContainer.resumeFocusedStacksTopActivities();
197 mRootWindowContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
198 mRootWindowContainer.addStartingWindowsForVisibleActivities();
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -0700199 mWindowManager.executeAppTransition();
200 } finally {
Riddle Hsua0022cd2019-09-09 21:12:41 +0800201 mService.continueWindowLayout();
Riddle Hsu2ca561b2019-10-08 21:58:58 +0800202 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
Jorim Jaggife762342016-10-13 14:33:27 +0200203 }
204 }
205
Lucas Dupinc80c67e2017-12-04 14:29:10 -0800206 void dismissKeyguard(IBinder token, IKeyguardDismissCallback callback, CharSequence message) {
Jorim Jaggi241ae102016-11-02 21:57:33 -0700207 final ActivityRecord activityRecord = ActivityRecord.forTokenLocked(token);
208 if (activityRecord == null || !activityRecord.visibleIgnoringKeyguard) {
209 failCallback(callback);
210 return;
211 }
Jorim Jaggid7214892017-07-18 14:05:19 +0200212 Slog.i(TAG, "Activity requesting to dismiss Keyguard: " + activityRecord);
chaviw59b98852017-06-13 12:05:44 -0700213
214 // If the client has requested to dismiss the keyguard and the Activity has the flag to
215 // turn the screen on, wakeup the screen if it's the top Activity.
216 if (activityRecord.getTurnScreenOnFlag() && activityRecord.isTopRunningActivity()) {
217 mStackSupervisor.wakeUp("dismissKeyguard");
218 }
219
Lucas Dupinc80c67e2017-12-04 14:29:10 -0800220 mWindowManager.dismissKeyguard(callback, message);
Jorim Jaggi241ae102016-11-02 21:57:33 -0700221 }
222
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -0700223 private void setKeyguardGoingAway(boolean keyguardGoingAway) {
224 mKeyguardGoingAway = keyguardGoingAway;
225 mWindowManager.setKeyguardGoingAway(keyguardGoingAway);
226 }
227
Jorim Jaggi241ae102016-11-02 21:57:33 -0700228 private void failCallback(IKeyguardDismissCallback callback) {
229 try {
230 callback.onDismissError();
231 } catch (RemoteException e) {
232 Slog.w(TAG, "Failed to call callback", e);
233 }
234 }
235
Jorim Jaggife762342016-10-13 14:33:27 +0200236 private int convertTransitFlags(int keyguardGoingAwayFlags) {
237 int result = 0;
238 if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_TO_SHADE) != 0) {
239 result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_TO_SHADE;
240 }
241 if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_NO_WINDOW_ANIMATIONS) != 0) {
242 result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_NO_ANIMATION;
243 }
244 if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_WITH_WALLPAPER) != 0) {
245 result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_WITH_WALLPAPER;
246 }
Issei Suzuki5609ccb2019-06-13 15:04:08 +0200247 if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_SUBTLE_WINDOW_ANIMATIONS) != 0) {
248 result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_SUBTLE_ANIMATION;
249 }
Jorim Jaggife762342016-10-13 14:33:27 +0200250 return result;
251 }
252
253 /**
Jorim Jaggie69c9312016-10-31 18:24:38 -0700254 * @return True if we may show an activity while Keyguard is showing because we are in the
255 * process of dismissing it anyways, false otherwise.
256 */
Jorim Jaggi07961872016-11-23 11:28:57 +0100257 boolean canShowActivityWhileKeyguardShowing(ActivityRecord r, boolean dismissKeyguard) {
258
259 // Allow to show it when we are about to dismiss Keyguard. This isn't allowed if r is
260 // already the dismissing activity, in which case we don't allow it to repeatedly dismiss
261 // Keyguard.
Lucas Dupin47a65c72018-02-15 14:16:18 -0800262 return dismissKeyguard && canDismissKeyguard() && !mAodShowing
wilsonshih0299c8a2018-08-24 15:52:57 +0800263 && (mDismissalRequested
Arthur Hungcf58b332019-04-17 16:07:10 +0800264 || (r.canShowWhenLocked()
265 && getDisplay(r.getDisplayId()).mDismissingKeyguardActivity != r));
Jorim Jaggi07961872016-11-23 11:28:57 +0100266 }
267
268 /**
269 * @return True if we may show an activity while Keyguard is occluded, false otherwise.
270 */
271 boolean canShowWhileOccluded(boolean dismissKeyguard, boolean showWhenLocked) {
Brad Stenning2bdc21e2019-03-11 14:33:22 -0700272 return showWhenLocked || dismissKeyguard
273 && !mWindowManager.isKeyguardSecure(mService.getCurrentUserId());
Jorim Jaggie69c9312016-10-31 18:24:38 -0700274 }
275
Riddle Hsuf2ff25f2020-04-29 19:15:33 +0800276 /**
277 * Makes sure to update lockscreen occluded/dismiss state if needed after completing all
278 * visibility updates ({@link ActivityStackSupervisor#endActivityVisibilityUpdate}).
279 */
280 void visibilitiesUpdated() {
wilsonshih0299c8a2018-08-24 15:52:57 +0800281 boolean requestDismissKeyguard = false;
Louis Chang149d5c82019-12-30 09:47:39 +0800282 for (int displayNdx = mRootWindowContainer.getChildCount() - 1;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800283 displayNdx >= 0; displayNdx--) {
Louis Chang149d5c82019-12-30 09:47:39 +0800284 final DisplayContent display = mRootWindowContainer.getChildAt(displayNdx);
wilsonshih0299c8a2018-08-24 15:52:57 +0800285 final KeyguardDisplayState state = getDisplay(display.mDisplayId);
286 state.visibilitiesUpdated(this, display);
287 requestDismissKeyguard |= state.mRequestDismissKeyguard;
Jorim Jaggife762342016-10-13 14:33:27 +0200288 }
wilsonshih0299c8a2018-08-24 15:52:57 +0800289
290 // Dismissing Keyguard happens globally using the information from all displays.
291 if (requestDismissKeyguard) {
Jorim Jaggife762342016-10-13 14:33:27 +0200292 handleDismissKeyguard();
293 }
294 }
295
296 /**
297 * Called when occluded state changed.
298 */
Issei Suzuki62356a22019-04-11 16:46:37 +0200299 private void handleOccludedChanged(int displayId) {
300 // TODO(b/113840485): Handle app transition for individual display, and apply occluded
301 // state change to secondary displays.
302 // For now, only default display fully supports occluded change. Other displays only
303 // updates keygaurd sleep token on that display.
304 if (displayId != DEFAULT_DISPLAY) {
305 updateKeyguardSleepToken(displayId);
306 return;
307 }
308
Riddle Hsu8419e4b2019-09-18 23:28:01 +0800309 mWindowManager.mPolicy.onKeyguardOccludedChangedLw(isDisplayOccluded(DEFAULT_DISPLAY));
Jorim Jaggife762342016-10-13 14:33:27 +0200310 if (isKeyguardLocked()) {
Riddle Hsua0022cd2019-09-09 21:12:41 +0800311 mService.deferWindowLayout();
Jorim Jaggife762342016-10-13 14:33:27 +0200312 try {
Louis Chang149d5c82019-12-30 09:47:39 +0800313 mRootWindowContainer.getDefaultDisplay().mDisplayContent
lumark588a3e82018-07-20 18:53:54 +0800314 .prepareAppTransition(resolveOccludeTransit(),
315 false /* alwaysKeepCurrent */, 0 /* flags */,
316 true /* forceOverride */);
Issei Suzuki62356a22019-04-11 16:46:37 +0200317 updateKeyguardSleepToken(DEFAULT_DISPLAY);
Louis Chang149d5c82019-12-30 09:47:39 +0800318 mRootWindowContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
Jorim Jaggife762342016-10-13 14:33:27 +0200319 mWindowManager.executeAppTransition();
320 } finally {
Riddle Hsua0022cd2019-09-09 21:12:41 +0800321 mService.continueWindowLayout();
Jorim Jaggife762342016-10-13 14:33:27 +0200322 }
323 }
324 dismissDockedStackIfNeeded();
325 }
326
327 /**
wilsonshih0299c8a2018-08-24 15:52:57 +0800328 * Called when somebody wants to dismiss the Keyguard via the flag.
Jorim Jaggife762342016-10-13 14:33:27 +0200329 */
330 private void handleDismissKeyguard() {
Jorim Jaggi07961872016-11-23 11:28:57 +0100331 // We only allow dismissing Keyguard via the flag when Keyguard is secure for legacy
332 // reasons, because that's how apps used to dismiss Keyguard in the secure case. In the
333 // insecure case, we actually show it on top of the lockscreen. See #canShowWhileOccluded.
Brad Stenning2bdc21e2019-03-11 14:33:22 -0700334 if (!mWindowManager.isKeyguardSecure(mService.getCurrentUserId())) {
Wale Ogunwaled32da472018-11-16 07:19:28 -0800335 return;
336 }
Jorim Jaggife762342016-10-13 14:33:27 +0200337
Wale Ogunwaled32da472018-11-16 07:19:28 -0800338 mWindowManager.dismissKeyguard(null /* callback */, null /* message */);
339 mDismissalRequested = true;
340
341 // If we are about to unocclude the Keyguard, but we can dismiss it without security,
342 // we immediately dismiss the Keyguard so the activity gets shown without a flicker.
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800343 final DisplayContent dc =
Louis Chang149d5c82019-12-30 09:47:39 +0800344 mRootWindowContainer.getDefaultDisplay().mDisplayContent;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800345 if (mKeyguardShowing && canDismissKeyguard()
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800346 && dc.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
347 dc.prepareAppTransition(mBeforeUnoccludeTransit, false /* alwaysKeepCurrent */,
Wale Ogunwaled32da472018-11-16 07:19:28 -0800348 0 /* flags */, true /* forceOverride */);
Louis Chang149d5c82019-12-30 09:47:39 +0800349 mRootWindowContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwaled32da472018-11-16 07:19:28 -0800350 mWindowManager.executeAppTransition();
Jorim Jaggife762342016-10-13 14:33:27 +0200351 }
352 }
353
Issei Suzuki16d4de52019-07-29 13:12:01 +0000354 private boolean isDisplayOccluded(int displayId) {
wilsonshih0299c8a2018-08-24 15:52:57 +0800355 return getDisplay(displayId).mOccluded;
356 }
357
Jorim Jaggife762342016-10-13 14:33:27 +0200358 /**
359 * @return true if Keyguard can be currently dismissed without entering credentials.
360 */
Andrii Kulianfc8f82b2017-01-26 13:17:27 -0800361 boolean canDismissKeyguard() {
Riddle Hsu8419e4b2019-09-18 23:28:01 +0800362 return mWindowManager.mPolicy.isKeyguardTrustedLw()
Brad Stenning2bdc21e2019-03-11 14:33:22 -0700363 || !mWindowManager.isKeyguardSecure(mService.getCurrentUserId());
Jorim Jaggife762342016-10-13 14:33:27 +0200364 }
365
366 private int resolveOccludeTransit() {
Louis Chang149d5c82019-12-30 09:47:39 +0800367 final DisplayContent dc = mRootWindowContainer.getDefaultDisplay().mDisplayContent;
Jorim Jaggife762342016-10-13 14:33:27 +0200368 if (mBeforeUnoccludeTransit != TRANSIT_UNSET
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800369 && dc.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
wilsonshih0299c8a2018-08-24 15:52:57 +0800370 // TODO(b/113840485): Handle app transition for individual display.
Issei Suzuki16d4de52019-07-29 13:12:01 +0000371 && isDisplayOccluded(DEFAULT_DISPLAY)) {
Jorim Jaggife762342016-10-13 14:33:27 +0200372
373 // Reuse old transit in case we are occluding Keyguard again, meaning that we never
374 // actually occclude/unocclude Keyguard, but just run a normal transition.
375 return mBeforeUnoccludeTransit;
wilsonshih0299c8a2018-08-24 15:52:57 +0800376 // TODO(b/113840485): Handle app transition for individual display.
Issei Suzuki16d4de52019-07-29 13:12:01 +0000377 } else if (!isDisplayOccluded(DEFAULT_DISPLAY)) {
Jorim Jaggife762342016-10-13 14:33:27 +0200378
379 // Save transit in case we dismiss/occlude Keyguard shortly after.
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800380 mBeforeUnoccludeTransit = dc.mAppTransition.getAppTransition();
Jorim Jaggife762342016-10-13 14:33:27 +0200381 return TRANSIT_KEYGUARD_UNOCCLUDE;
382 } else {
383 return TRANSIT_KEYGUARD_OCCLUDE;
384 }
385 }
386
387 private void dismissDockedStackIfNeeded() {
wilsonshih0299c8a2018-08-24 15:52:57 +0800388 // TODO(b/113840485): Handle docked stack for individual display.
Issei Suzuki16d4de52019-07-29 13:12:01 +0000389 if (mKeyguardShowing && isDisplayOccluded(DEFAULT_DISPLAY)) {
Jorim Jaggife762342016-10-13 14:33:27 +0200390 // The lock screen is currently showing, but is occluded by a window that can
391 // show on top of the lock screen. In this can we want to dismiss the docked
392 // stack since it will be complicated/risky to try to put the activity on top
393 // of the lock screen in the right fullscreen configuration.
Andrii Kulian4c0fd0d2020-03-29 13:32:14 -0700394 final TaskDisplayArea taskDisplayArea = mRootWindowContainer
395 .getDefaultTaskDisplayArea();
396 if (!taskDisplayArea.isSplitScreenModeActivated()) {
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700397 return;
398 }
Andrii Kulian4c0fd0d2020-03-29 13:32:14 -0700399 taskDisplayArea.onSplitScreenModeDismissed();
Jorim Jaggife762342016-10-13 14:33:27 +0200400 }
401 }
Jorim Jaggi8d786932016-10-26 19:08:36 -0700402
David Stevens9440dc82017-03-16 19:00:20 -0700403 private void updateKeyguardSleepToken() {
Louis Chang149d5c82019-12-30 09:47:39 +0800404 for (int displayNdx = mRootWindowContainer.getChildCount() - 1;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800405 displayNdx >= 0; displayNdx--) {
Louis Chang149d5c82019-12-30 09:47:39 +0800406 final DisplayContent display = mRootWindowContainer.getChildAt(displayNdx);
Issei Suzuki62356a22019-04-11 16:46:37 +0200407 updateKeyguardSleepToken(display.mDisplayId);
408 }
409 }
410
411 private void updateKeyguardSleepToken(int displayId) {
412 final KeyguardDisplayState state = getDisplay(displayId);
Issei Suzuki16d4de52019-07-29 13:12:01 +0000413 if (isKeyguardUnoccludedOrAodShowing(displayId) && state.mSleepToken == null) {
Issei Suzuki62356a22019-04-11 16:46:37 +0200414 state.acquiredSleepToken();
Issei Suzuki16d4de52019-07-29 13:12:01 +0000415 } else if (!isKeyguardUnoccludedOrAodShowing(displayId) && state.mSleepToken != null) {
Issei Suzuki62356a22019-04-11 16:46:37 +0200416 state.releaseSleepToken();
wilsonshih0299c8a2018-08-24 15:52:57 +0800417 }
418 }
419
420 private KeyguardDisplayState getDisplay(int displayId) {
wilsonshihe7903ea2018-09-26 16:17:59 +0800421 KeyguardDisplayState state = mDisplayStates.get(displayId);
422 if (state == null) {
423 state = new KeyguardDisplayState(mService, displayId);
424 mDisplayStates.append(displayId, state);
wilsonshih0299c8a2018-08-24 15:52:57 +0800425 }
wilsonshihe7903ea2018-09-26 16:17:59 +0800426 return state;
wilsonshih0299c8a2018-08-24 15:52:57 +0800427 }
428
429 void onDisplayRemoved(int displayId) {
wilsonshihe7903ea2018-09-26 16:17:59 +0800430 final KeyguardDisplayState state = mDisplayStates.get(displayId);
431 if (state != null) {
432 state.onRemoved();
wilsonshih0299c8a2018-08-24 15:52:57 +0800433 mDisplayStates.remove(displayId);
434 }
435 }
436
437 /** Represents Keyguard state per individual display. */
438 private static class KeyguardDisplayState {
439 private final int mDisplayId;
440 private boolean mOccluded;
441 private ActivityRecord mDismissingKeyguardActivity;
442 private boolean mRequestDismissKeyguard;
443 private final ActivityTaskManagerService mService;
444 private SleepToken mSleepToken;
445
446 KeyguardDisplayState(ActivityTaskManagerService service, int displayId) {
447 mService = service;
448 mDisplayId = displayId;
449 }
450
451 void onRemoved() {
452 mDismissingKeyguardActivity = null;
453 releaseSleepToken();
454 }
455
456 void acquiredSleepToken() {
457 if (mSleepToken == null) {
458 mSleepToken = mService.acquireSleepToken("keyguard", mDisplayId);
459 }
460 }
461
462 void releaseSleepToken() {
463 if (mSleepToken != null) {
464 mSleepToken.release();
465 mSleepToken = null;
466 }
467 }
468
Louis Chang677921f2019-12-06 16:44:24 +0800469 void visibilitiesUpdated(KeyguardController controller, DisplayContent display) {
wilsonshih0299c8a2018-08-24 15:52:57 +0800470 final boolean lastOccluded = mOccluded;
471 final ActivityRecord lastDismissActivity = mDismissingKeyguardActivity;
472 mRequestDismissKeyguard = false;
473 mOccluded = false;
474 mDismissingKeyguardActivity = null;
475
wilsonshih9a10e9d2019-01-11 14:39:27 +0800476 final ActivityStack stack = getStackForControllingOccluding(display);
477 if (stack != null) {
478 final ActivityRecord topDismissing = stack.getTopDismissingKeyguardActivity();
479 mOccluded = stack.topActivityOccludesKeyguard() || (topDismissing != null
Wale Ogunwale85fb19a2019-12-05 10:41:05 +0900480 && stack.topRunningActivity() == topDismissing
wilsonshih9a10e9d2019-01-11 14:39:27 +0800481 && controller.canShowWhileOccluded(
wilsonshih0299c8a2018-08-24 15:52:57 +0800482 true /* dismissKeyguard */,
483 false /* showWhenLocked */));
wilsonshih9a10e9d2019-01-11 14:39:27 +0800484 if (stack.getTopDismissingKeyguardActivity() != null) {
485 mDismissingKeyguardActivity = stack.getTopDismissingKeyguardActivity();
wilsonshih0299c8a2018-08-24 15:52:57 +0800486 }
wilsonshih498a4b82018-12-11 16:10:16 +0800487 // FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD only apply for secondary display.
488 if (mDisplayId != DEFAULT_DISPLAY) {
489 mOccluded |= stack.canShowWithInsecureKeyguard()
490 && controller.canDismissKeyguard();
491 }
wilsonshih0299c8a2018-08-24 15:52:57 +0800492 }
wilsonshih498a4b82018-12-11 16:10:16 +0800493 // TODO(b/123372519): isShowingDream can only works on default display.
494 if (mDisplayId == DEFAULT_DISPLAY) {
Louis Chang149d5c82019-12-30 09:47:39 +0800495 mOccluded |= mService.mRootWindowContainer.getDefaultDisplay().mDisplayContent
Riddle Hsu8419e4b2019-09-18 23:28:01 +0800496 .getDisplayPolicy().isShowingDreamLw();
wilsonshih498a4b82018-12-11 16:10:16 +0800497 }
wilsonshih0299c8a2018-08-24 15:52:57 +0800498
Issei Suzuki62356a22019-04-11 16:46:37 +0200499 if (lastOccluded != mOccluded) {
500 controller.handleOccludedChanged(mDisplayId);
wilsonshih0299c8a2018-08-24 15:52:57 +0800501 }
502 if (lastDismissActivity != mDismissingKeyguardActivity && !mOccluded
503 && mDismissingKeyguardActivity != null
Brad Stenning2bdc21e2019-03-11 14:33:22 -0700504 && controller.mWindowManager.isKeyguardSecure(
505 controller.mService.getCurrentUserId())) {
wilsonshih0299c8a2018-08-24 15:52:57 +0800506 mRequestDismissKeyguard = true;
507 }
508 }
509
wilsonshih9a10e9d2019-01-11 14:39:27 +0800510 /**
511 * Gets the stack used to check the occluded state.
512 * <p>
513 * Only the top non-pinned activity of the focusable stack on each display can control its
514 * occlusion state.
515 */
Louis Chang677921f2019-12-06 16:44:24 +0800516 private ActivityStack getStackForControllingOccluding(DisplayContent display) {
Andrii Kulianf9df4a82020-03-31 12:09:27 -0700517 for (int tdaNdx = display.getTaskDisplayAreaCount() - 1; tdaNdx >= 0; --tdaNdx) {
518 final TaskDisplayArea taskDisplayArea = display.getTaskDisplayAreaAt(tdaNdx);
519 for (int sNdx = taskDisplayArea.getStackCount() - 1; sNdx >= 0; --sNdx) {
520 final ActivityStack stack = taskDisplayArea.getStackAt(sNdx);
521 if (stack != null && stack.isFocusableAndVisible()
522 && !stack.inPinnedWindowingMode()) {
523 return stack;
524 }
wilsonshih9a10e9d2019-01-11 14:39:27 +0800525 }
526 }
527 return null;
528 }
529
wilsonshih0299c8a2018-08-24 15:52:57 +0800530 void dumpStatus(PrintWriter pw, String prefix) {
531 final StringBuilder sb = new StringBuilder();
532 sb.append(prefix);
533 sb.append(" Occluded=").append(mOccluded)
534 .append(" DismissingKeyguardActivity=")
535 .append(mDismissingKeyguardActivity)
536 .append(" at display=")
537 .append(mDisplayId);
538 pw.println(sb.toString());
539 }
540
Jeffrey Huangcb782852019-12-05 11:28:11 -0800541 void dumpDebug(ProtoOutputStream proto, long fieldId) {
wilsonshih0299c8a2018-08-24 15:52:57 +0800542 final long token = proto.start(fieldId);
543 proto.write(DISPLAY_ID, mDisplayId);
544 proto.write(KEYGUARD_OCCLUDED, mOccluded);
545 proto.end(token);
David Stevens9440dc82017-03-16 19:00:20 -0700546 }
547 }
548
Jorim Jaggi8d786932016-10-26 19:08:36 -0700549 void dump(PrintWriter pw, String prefix) {
550 pw.println(prefix + "KeyguardController:");
551 pw.println(prefix + " mKeyguardShowing=" + mKeyguardShowing);
Lucas Dupin47a65c72018-02-15 14:16:18 -0800552 pw.println(prefix + " mAodShowing=" + mAodShowing);
Jorim Jaggi8d786932016-10-26 19:08:36 -0700553 pw.println(prefix + " mKeyguardGoingAway=" + mKeyguardGoingAway);
wilsonshih0299c8a2018-08-24 15:52:57 +0800554 dumpDisplayStates(pw, prefix);
Jorim Jaggi07961872016-11-23 11:28:57 +0100555 pw.println(prefix + " mDismissalRequested=" + mDismissalRequested);
Winson Chung268eccb2020-03-26 13:43:44 -0700556 pw.println();
Jorim Jaggi8d786932016-10-26 19:08:36 -0700557 }
Steven Timotius4346f0a2017-09-12 11:07:21 -0700558
Jeffrey Huangcb782852019-12-05 11:28:11 -0800559 void dumpDebug(ProtoOutputStream proto, long fieldId) {
Steven Timotius4346f0a2017-09-12 11:07:21 -0700560 final long token = proto.start(fieldId);
Issei Suzukid6eb5a22019-02-20 23:08:03 +0100561 proto.write(AOD_SHOWING, mAodShowing);
Steven Timotius4346f0a2017-09-12 11:07:21 -0700562 proto.write(KEYGUARD_SHOWING, mKeyguardShowing);
wilsonshih0299c8a2018-08-24 15:52:57 +0800563 writeDisplayStatesToProto(proto, KEYGUARD_OCCLUDED_STATES);
Steven Timotius4346f0a2017-09-12 11:07:21 -0700564 proto.end(token);
565 }
wilsonshih0299c8a2018-08-24 15:52:57 +0800566
567 private void dumpDisplayStates(PrintWriter pw, String prefix) {
568 for (int i = 0; i < mDisplayStates.size(); i++) {
569 mDisplayStates.valueAt(i).dumpStatus(pw, prefix);
570 }
571 }
572
573 private void writeDisplayStatesToProto(ProtoOutputStream proto, long fieldId) {
574 for (int i = 0; i < mDisplayStates.size(); i++) {
Jeffrey Huangcb782852019-12-05 11:28:11 -0800575 mDisplayStates.valueAt(i).dumpDebug(proto, fieldId);
wilsonshih0299c8a2018-08-24 15:52:57 +0800576 }
577 }
Jorim Jaggife762342016-10-13 14:33:27 +0200578}