blob: 3a0696fb3ed5f64f2a8aa3b6d424ca7b5ba86fdb [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
Issei Suzukid6eb5a22019-02-20 23:08:03 +010034import static com.android.server.am.KeyguardControllerProto.AOD_SHOWING;
wilsonshih0299c8a2018-08-24 15:52:57 +080035import static com.android.server.am.KeyguardControllerProto.KEYGUARD_OCCLUDED_STATES;
Yi Jin6c6e9ca2018-03-20 16:53:35 -070036import static com.android.server.am.KeyguardControllerProto.KEYGUARD_SHOWING;
wilsonshih0299c8a2018-08-24 15:52:57 +080037import static com.android.server.am.KeyguardOccludedProto.DISPLAY_ID;
38import static com.android.server.am.KeyguardOccludedProto.KEYGUARD_OCCLUDED;
wilsonshihe7903ea2018-09-26 16:17:59 +080039import static com.android.server.wm.ActivityStackSupervisor.PRESERVE_WINDOWS;
40import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM;
41import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME;
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;
73 private int mVisibilityTransactionDepth;
wilsonshih0299c8a2018-08-24 15:52:57 +080074 private final SparseArray<KeyguardDisplayState> mDisplayStates = new SparseArray<>();
75 private final ActivityTaskManagerService mService;
Wale Ogunwaled32da472018-11-16 07:19:28 -080076 private RootActivityContainer mRootActivityContainer;
Jorim Jaggife762342016-10-13 14:33:27 +020077
Wale Ogunwalef6733932018-06-27 05:14:34 -070078 KeyguardController(ActivityTaskManagerService service,
Jorim Jaggife762342016-10-13 14:33:27 +020079 ActivityStackSupervisor stackSupervisor) {
80 mService = service;
81 mStackSupervisor = stackSupervisor;
82 }
83
84 void setWindowManager(WindowManagerService windowManager) {
85 mWindowManager = windowManager;
Wale Ogunwaled32da472018-11-16 07:19:28 -080086 mRootActivityContainer = mService.mRootActivityContainer;
Jorim Jaggife762342016-10-13 14:33:27 +020087 }
88
89 /**
Issei Suzuki16d4de52019-07-29 13:12:01 +000090 * @return true if either Keyguard or AOD are showing, not going away, and not being occluded
91 * on the given display, false otherwise.
Lucas Dupin47a65c72018-02-15 14:16:18 -080092 */
93 boolean isKeyguardOrAodShowing(int displayId) {
Issei Suzuki16d4de52019-07-29 13:12:01 +000094 return (mKeyguardShowing || mAodShowing) && !mKeyguardGoingAway
95 && !isDisplayOccluded(displayId);
96 }
97
98 /**
99 * @return {@code true} for default display when AOD is showing. Otherwise, same as
100 * {@link #isKeyguardOrAodShowing(int)}
101 * TODO(b/125198167): Replace isKeyguardOrAodShowing() by this logic.
102 */
103 boolean isKeyguardUnoccludedOrAodShowing(int displayId) {
104 if (displayId == DEFAULT_DISPLAY && mAodShowing) {
105 return true;
106 }
107 return isKeyguardOrAodShowing(displayId);
Issei Suzukid6eb5a22019-02-20 23:08:03 +0100108 }
109
110 /**
David Stevens53a39ea2017-08-23 18:41:49 -0700111 * @return true if Keyguard is showing, not going away, and not being occluded on the given
112 * display, false otherwise
Jorim Jaggife762342016-10-13 14:33:27 +0200113 */
David Stevens53a39ea2017-08-23 18:41:49 -0700114 boolean isKeyguardShowing(int displayId) {
Issei Suzuki16d4de52019-07-29 13:12:01 +0000115 return mKeyguardShowing && !mKeyguardGoingAway && !isDisplayOccluded(displayId);
Jorim Jaggife762342016-10-13 14:33:27 +0200116 }
117
118 /**
119 * @return true if Keyguard is either showing or occluded, but not going away
120 */
121 boolean isKeyguardLocked() {
122 return mKeyguardShowing && !mKeyguardGoingAway;
123 }
124
125 /**
Bryce Lee271617a2018-03-15 10:39:12 -0700126 * @return {@code true} if the keyguard is going away, {@code false} otherwise.
127 */
128 boolean isKeyguardGoingAway() {
129 // Also check keyguard showing in case value is stale.
130 return mKeyguardGoingAway && mKeyguardShowing;
131 }
132
133 /**
Jorim Jaggife762342016-10-13 14:33:27 +0200134 * Update the Keyguard showing state.
135 */
wilsonshih177261f2019-02-22 12:02:18 +0800136 void setKeyguardShown(boolean keyguardShowing, boolean aodShowing) {
Adrian Roos6ec76b72018-04-25 14:01:11 +0200137 // If keyguard is going away, but SystemUI aborted the transition, need to reset state.
wilsonshih2bc846c2019-05-02 14:35:21 +0800138 final boolean keyguardChanged = keyguardShowing != mKeyguardShowing
139 || mKeyguardGoingAway && keyguardShowing;
140 final boolean aodChanged = aodShowing != mAodShowing;
141 if (!keyguardChanged && !aodChanged) {
Jorim Jaggife762342016-10-13 14:33:27 +0200142 return;
143 }
Jeff Changd136e772019-11-05 20:33:52 +0800144 EventLogTags.writeWmSetKeyguardShown(
wilsonshih30eca702019-07-10 09:22:11 +0800145 keyguardShowing ? 1 : 0,
146 aodShowing ? 1 : 0,
147 mKeyguardGoingAway ? 1 : 0,
148 "setKeyguardShown");
Lucas Dupin47a65c72018-02-15 14:16:18 -0800149 mKeyguardShowing = keyguardShowing;
150 mAodShowing = aodShowing;
chaviw0e9fb132018-06-05 16:29:13 -0700151 mWindowManager.setAodShowing(aodShowing);
wilsonshih2bc846c2019-05-02 14:35:21 +0800152
153 if (keyguardChanged) {
154 // Irrelevant to AOD.
David Stevens53a39ea2017-08-23 18:41:49 -0700155 dismissDockedStackIfNeeded();
Andrii Kulian0d595f32018-02-21 15:47:33 -0800156 setKeyguardGoingAway(false);
Lucas Dupin47a65c72018-02-15 14:16:18 -0800157 if (keyguardShowing) {
David Stevens53a39ea2017-08-23 18:41:49 -0700158 mDismissalRequested = false;
159 }
Jorim Jaggife762342016-10-13 14:33:27 +0200160 }
wilsonshih2bc846c2019-05-02 14:35:21 +0800161 // TODO(b/113840485): Check usage for non-default display
162 mWindowManager.setKeyguardOrAodShowingOnDefaultDisplay(
163 isKeyguardOrAodShowing(DEFAULT_DISPLAY));
164
165 // Update the sleep token first such that ensureActivitiesVisible has correct sleep token
166 // state when evaluating visibilities.
David Stevens9440dc82017-03-16 19:00:20 -0700167 updateKeyguardSleepToken();
wilsonshih2bc846c2019-05-02 14:35:21 +0800168 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
Jorim Jaggife762342016-10-13 14:33:27 +0200169 }
170
171 /**
172 * Called when Keyguard is going away.
173 *
Adrian Roose99bc052017-11-20 17:55:31 +0100174 * @param flags See {@link WindowManagerPolicy#KEYGUARD_GOING_AWAY_FLAG_TO_SHADE}
Jorim Jaggife762342016-10-13 14:33:27 +0200175 * etc.
176 */
177 void keyguardGoingAway(int flags) {
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -0700178 if (!mKeyguardShowing) {
179 return;
180 }
Riddle Hsu2ca561b2019-10-08 21:58:58 +0800181 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "keyguardGoingAway");
Riddle Hsua0022cd2019-09-09 21:12:41 +0800182 mService.deferWindowLayout();
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -0700183 try {
184 setKeyguardGoingAway(true);
Jeff Changd136e772019-11-05 20:33:52 +0800185 EventLogTags.writeWmSetKeyguardShown(
wilsonshih30eca702019-07-10 09:22:11 +0800186 1 /* keyguardShowing */,
187 mAodShowing ? 1 : 0,
188 1 /* keyguardGoingAway */,
189 "keyguardGoingAway");
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800190 mRootActivityContainer.getDefaultDisplay().mDisplayContent
lumark588a3e82018-07-20 18:53:54 +0800191 .prepareAppTransition(TRANSIT_KEYGUARD_GOING_AWAY,
192 false /* alwaysKeepCurrent */, convertTransitFlags(flags),
193 false /* forceOverride */);
David Stevens9440dc82017-03-16 19:00:20 -0700194 updateKeyguardSleepToken();
Jorim Jaggife762342016-10-13 14:33:27 +0200195
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -0700196 // Some stack visibility might change (e.g. docked stack)
Wale Ogunwaled32da472018-11-16 07:19:28 -0800197 mRootActivityContainer.resumeFocusedStacksTopActivities();
198 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
Wale Ogunwalea38654f2019-11-17 20:37:15 -0800199 mRootActivityContainer.addStartingWindowsForVisibleActivities();
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -0700200 mWindowManager.executeAppTransition();
201 } finally {
Riddle Hsua0022cd2019-09-09 21:12:41 +0800202 mService.continueWindowLayout();
Riddle Hsu2ca561b2019-10-08 21:58:58 +0800203 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
Jorim Jaggife762342016-10-13 14:33:27 +0200204 }
205 }
206
Lucas Dupinc80c67e2017-12-04 14:29:10 -0800207 void dismissKeyguard(IBinder token, IKeyguardDismissCallback callback, CharSequence message) {
Jorim Jaggi241ae102016-11-02 21:57:33 -0700208 final ActivityRecord activityRecord = ActivityRecord.forTokenLocked(token);
209 if (activityRecord == null || !activityRecord.visibleIgnoringKeyguard) {
210 failCallback(callback);
211 return;
212 }
Jorim Jaggid7214892017-07-18 14:05:19 +0200213 Slog.i(TAG, "Activity requesting to dismiss Keyguard: " + activityRecord);
chaviw59b98852017-06-13 12:05:44 -0700214
215 // If the client has requested to dismiss the keyguard and the Activity has the flag to
216 // turn the screen on, wakeup the screen if it's the top Activity.
217 if (activityRecord.getTurnScreenOnFlag() && activityRecord.isTopRunningActivity()) {
218 mStackSupervisor.wakeUp("dismissKeyguard");
219 }
220
Lucas Dupinc80c67e2017-12-04 14:29:10 -0800221 mWindowManager.dismissKeyguard(callback, message);
Jorim Jaggi241ae102016-11-02 21:57:33 -0700222 }
223
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -0700224 private void setKeyguardGoingAway(boolean keyguardGoingAway) {
225 mKeyguardGoingAway = keyguardGoingAway;
226 mWindowManager.setKeyguardGoingAway(keyguardGoingAway);
227 }
228
Jorim Jaggi241ae102016-11-02 21:57:33 -0700229 private void failCallback(IKeyguardDismissCallback callback) {
230 try {
231 callback.onDismissError();
232 } catch (RemoteException e) {
233 Slog.w(TAG, "Failed to call callback", e);
234 }
235 }
236
Jorim Jaggife762342016-10-13 14:33:27 +0200237 private int convertTransitFlags(int keyguardGoingAwayFlags) {
238 int result = 0;
239 if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_TO_SHADE) != 0) {
240 result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_TO_SHADE;
241 }
242 if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_NO_WINDOW_ANIMATIONS) != 0) {
243 result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_NO_ANIMATION;
244 }
245 if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_WITH_WALLPAPER) != 0) {
246 result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_WITH_WALLPAPER;
247 }
Issei Suzuki5609ccb2019-06-13 15:04:08 +0200248 if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_SUBTLE_WINDOW_ANIMATIONS) != 0) {
249 result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_SUBTLE_ANIMATION;
250 }
Jorim Jaggife762342016-10-13 14:33:27 +0200251 return result;
252 }
253
254 /**
255 * Starts a batch of visibility updates.
256 */
257 void beginActivityVisibilityUpdate() {
258 mVisibilityTransactionDepth++;
259 }
260
261 /**
262 * Ends a batch of visibility updates. After all batches are done, this method makes sure to
263 * update lockscreen occluded/dismiss state if needed.
264 */
265 void endActivityVisibilityUpdate() {
266 mVisibilityTransactionDepth--;
267 if (mVisibilityTransactionDepth == 0) {
268 visibilitiesUpdated();
269 }
270 }
271
Jorim Jaggie69c9312016-10-31 18:24:38 -0700272 /**
273 * @return True if we may show an activity while Keyguard is showing because we are in the
274 * process of dismissing it anyways, false otherwise.
275 */
Jorim Jaggi07961872016-11-23 11:28:57 +0100276 boolean canShowActivityWhileKeyguardShowing(ActivityRecord r, boolean dismissKeyguard) {
277
278 // Allow to show it when we are about to dismiss Keyguard. This isn't allowed if r is
279 // already the dismissing activity, in which case we don't allow it to repeatedly dismiss
280 // Keyguard.
Lucas Dupin47a65c72018-02-15 14:16:18 -0800281 return dismissKeyguard && canDismissKeyguard() && !mAodShowing
wilsonshih0299c8a2018-08-24 15:52:57 +0800282 && (mDismissalRequested
Arthur Hungcf58b332019-04-17 16:07:10 +0800283 || (r.canShowWhenLocked()
284 && getDisplay(r.getDisplayId()).mDismissingKeyguardActivity != r));
Jorim Jaggi07961872016-11-23 11:28:57 +0100285 }
286
287 /**
288 * @return True if we may show an activity while Keyguard is occluded, false otherwise.
289 */
290 boolean canShowWhileOccluded(boolean dismissKeyguard, boolean showWhenLocked) {
Brad Stenning2bdc21e2019-03-11 14:33:22 -0700291 return showWhenLocked || dismissKeyguard
292 && !mWindowManager.isKeyguardSecure(mService.getCurrentUserId());
Jorim Jaggie69c9312016-10-31 18:24:38 -0700293 }
294
Jorim Jaggife762342016-10-13 14:33:27 +0200295 private void visibilitiesUpdated() {
wilsonshih0299c8a2018-08-24 15:52:57 +0800296 boolean requestDismissKeyguard = false;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800297 for (int displayNdx = mRootActivityContainer.getChildCount() - 1;
298 displayNdx >= 0; displayNdx--) {
299 final ActivityDisplay display = mRootActivityContainer.getChildAt(displayNdx);
wilsonshih0299c8a2018-08-24 15:52:57 +0800300 final KeyguardDisplayState state = getDisplay(display.mDisplayId);
301 state.visibilitiesUpdated(this, display);
302 requestDismissKeyguard |= state.mRequestDismissKeyguard;
Jorim Jaggife762342016-10-13 14:33:27 +0200303 }
wilsonshih0299c8a2018-08-24 15:52:57 +0800304
305 // Dismissing Keyguard happens globally using the information from all displays.
306 if (requestDismissKeyguard) {
Jorim Jaggife762342016-10-13 14:33:27 +0200307 handleDismissKeyguard();
308 }
309 }
310
311 /**
312 * Called when occluded state changed.
313 */
Issei Suzuki62356a22019-04-11 16:46:37 +0200314 private void handleOccludedChanged(int displayId) {
315 // TODO(b/113840485): Handle app transition for individual display, and apply occluded
316 // state change to secondary displays.
317 // For now, only default display fully supports occluded change. Other displays only
318 // updates keygaurd sleep token on that display.
319 if (displayId != DEFAULT_DISPLAY) {
320 updateKeyguardSleepToken(displayId);
321 return;
322 }
323
Riddle Hsu8419e4b2019-09-18 23:28:01 +0800324 mWindowManager.mPolicy.onKeyguardOccludedChangedLw(isDisplayOccluded(DEFAULT_DISPLAY));
Jorim Jaggife762342016-10-13 14:33:27 +0200325 if (isKeyguardLocked()) {
Riddle Hsua0022cd2019-09-09 21:12:41 +0800326 mService.deferWindowLayout();
Jorim Jaggife762342016-10-13 14:33:27 +0200327 try {
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800328 mRootActivityContainer.getDefaultDisplay().mDisplayContent
lumark588a3e82018-07-20 18:53:54 +0800329 .prepareAppTransition(resolveOccludeTransit(),
330 false /* alwaysKeepCurrent */, 0 /* flags */,
331 true /* forceOverride */);
Issei Suzuki62356a22019-04-11 16:46:37 +0200332 updateKeyguardSleepToken(DEFAULT_DISPLAY);
Wale Ogunwaled32da472018-11-16 07:19:28 -0800333 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
Jorim Jaggife762342016-10-13 14:33:27 +0200334 mWindowManager.executeAppTransition();
335 } finally {
Riddle Hsua0022cd2019-09-09 21:12:41 +0800336 mService.continueWindowLayout();
Jorim Jaggife762342016-10-13 14:33:27 +0200337 }
338 }
339 dismissDockedStackIfNeeded();
340 }
341
342 /**
wilsonshih0299c8a2018-08-24 15:52:57 +0800343 * Called when somebody wants to dismiss the Keyguard via the flag.
Jorim Jaggife762342016-10-13 14:33:27 +0200344 */
345 private void handleDismissKeyguard() {
Jorim Jaggi07961872016-11-23 11:28:57 +0100346 // We only allow dismissing Keyguard via the flag when Keyguard is secure for legacy
347 // reasons, because that's how apps used to dismiss Keyguard in the secure case. In the
348 // insecure case, we actually show it on top of the lockscreen. See #canShowWhileOccluded.
Brad Stenning2bdc21e2019-03-11 14:33:22 -0700349 if (!mWindowManager.isKeyguardSecure(mService.getCurrentUserId())) {
Wale Ogunwaled32da472018-11-16 07:19:28 -0800350 return;
351 }
Jorim Jaggife762342016-10-13 14:33:27 +0200352
Wale Ogunwaled32da472018-11-16 07:19:28 -0800353 mWindowManager.dismissKeyguard(null /* callback */, null /* message */);
354 mDismissalRequested = true;
355
356 // If we are about to unocclude the Keyguard, but we can dismiss it without security,
357 // we immediately dismiss the Keyguard so the activity gets shown without a flicker.
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800358 final DisplayContent dc =
359 mRootActivityContainer.getDefaultDisplay().mDisplayContent;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800360 if (mKeyguardShowing && canDismissKeyguard()
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800361 && dc.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
362 dc.prepareAppTransition(mBeforeUnoccludeTransit, false /* alwaysKeepCurrent */,
Wale Ogunwaled32da472018-11-16 07:19:28 -0800363 0 /* flags */, true /* forceOverride */);
364 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
365 mWindowManager.executeAppTransition();
Jorim Jaggife762342016-10-13 14:33:27 +0200366 }
367 }
368
Issei Suzuki16d4de52019-07-29 13:12:01 +0000369 private boolean isDisplayOccluded(int displayId) {
wilsonshih0299c8a2018-08-24 15:52:57 +0800370 return getDisplay(displayId).mOccluded;
371 }
372
Jorim Jaggife762342016-10-13 14:33:27 +0200373 /**
374 * @return true if Keyguard can be currently dismissed without entering credentials.
375 */
Andrii Kulianfc8f82b2017-01-26 13:17:27 -0800376 boolean canDismissKeyguard() {
Riddle Hsu8419e4b2019-09-18 23:28:01 +0800377 return mWindowManager.mPolicy.isKeyguardTrustedLw()
Brad Stenning2bdc21e2019-03-11 14:33:22 -0700378 || !mWindowManager.isKeyguardSecure(mService.getCurrentUserId());
Jorim Jaggife762342016-10-13 14:33:27 +0200379 }
380
381 private int resolveOccludeTransit() {
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800382 final DisplayContent dc =
383 mService.mRootActivityContainer.getDefaultDisplay().mDisplayContent;
Jorim Jaggife762342016-10-13 14:33:27 +0200384 if (mBeforeUnoccludeTransit != TRANSIT_UNSET
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800385 && dc.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
wilsonshih0299c8a2018-08-24 15:52:57 +0800386 // TODO(b/113840485): Handle app transition for individual display.
Issei Suzuki16d4de52019-07-29 13:12:01 +0000387 && isDisplayOccluded(DEFAULT_DISPLAY)) {
Jorim Jaggife762342016-10-13 14:33:27 +0200388
389 // Reuse old transit in case we are occluding Keyguard again, meaning that we never
390 // actually occclude/unocclude Keyguard, but just run a normal transition.
391 return mBeforeUnoccludeTransit;
wilsonshih0299c8a2018-08-24 15:52:57 +0800392 // TODO(b/113840485): Handle app transition for individual display.
Issei Suzuki16d4de52019-07-29 13:12:01 +0000393 } else if (!isDisplayOccluded(DEFAULT_DISPLAY)) {
Jorim Jaggife762342016-10-13 14:33:27 +0200394
395 // Save transit in case we dismiss/occlude Keyguard shortly after.
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800396 mBeforeUnoccludeTransit = dc.mAppTransition.getAppTransition();
Jorim Jaggife762342016-10-13 14:33:27 +0200397 return TRANSIT_KEYGUARD_UNOCCLUDE;
398 } else {
399 return TRANSIT_KEYGUARD_OCCLUDE;
400 }
401 }
402
403 private void dismissDockedStackIfNeeded() {
wilsonshih0299c8a2018-08-24 15:52:57 +0800404 // TODO(b/113840485): Handle docked stack for individual display.
Issei Suzuki16d4de52019-07-29 13:12:01 +0000405 if (mKeyguardShowing && isDisplayOccluded(DEFAULT_DISPLAY)) {
Jorim Jaggife762342016-10-13 14:33:27 +0200406 // The lock screen is currently showing, but is occluded by a window that can
407 // show on top of the lock screen. In this can we want to dismiss the docked
408 // stack since it will be complicated/risky to try to put the activity on top
409 // of the lock screen in the right fullscreen configuration.
Wale Ogunwaled32da472018-11-16 07:19:28 -0800410 final ActivityStack stack =
411 mRootActivityContainer.getDefaultDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700412 if (stack == null) {
413 return;
414 }
415 mStackSupervisor.moveTasksToFullscreenStackLocked(stack,
Andrii Kulian52d255c2018-07-13 11:32:19 -0700416 stack.isFocusedStackOnDisplay());
Jorim Jaggife762342016-10-13 14:33:27 +0200417 }
418 }
Jorim Jaggi8d786932016-10-26 19:08:36 -0700419
David Stevens9440dc82017-03-16 19:00:20 -0700420 private void updateKeyguardSleepToken() {
Wale Ogunwaled32da472018-11-16 07:19:28 -0800421 for (int displayNdx = mRootActivityContainer.getChildCount() - 1;
422 displayNdx >= 0; displayNdx--) {
423 final ActivityDisplay display = mRootActivityContainer.getChildAt(displayNdx);
Issei Suzuki62356a22019-04-11 16:46:37 +0200424 updateKeyguardSleepToken(display.mDisplayId);
425 }
426 }
427
428 private void updateKeyguardSleepToken(int displayId) {
429 final KeyguardDisplayState state = getDisplay(displayId);
Issei Suzuki16d4de52019-07-29 13:12:01 +0000430 if (isKeyguardUnoccludedOrAodShowing(displayId) && state.mSleepToken == null) {
Issei Suzuki62356a22019-04-11 16:46:37 +0200431 state.acquiredSleepToken();
Issei Suzuki16d4de52019-07-29 13:12:01 +0000432 } else if (!isKeyguardUnoccludedOrAodShowing(displayId) && state.mSleepToken != null) {
Issei Suzuki62356a22019-04-11 16:46:37 +0200433 state.releaseSleepToken();
wilsonshih0299c8a2018-08-24 15:52:57 +0800434 }
435 }
436
437 private KeyguardDisplayState getDisplay(int displayId) {
wilsonshihe7903ea2018-09-26 16:17:59 +0800438 KeyguardDisplayState state = mDisplayStates.get(displayId);
439 if (state == null) {
440 state = new KeyguardDisplayState(mService, displayId);
441 mDisplayStates.append(displayId, state);
wilsonshih0299c8a2018-08-24 15:52:57 +0800442 }
wilsonshihe7903ea2018-09-26 16:17:59 +0800443 return state;
wilsonshih0299c8a2018-08-24 15:52:57 +0800444 }
445
446 void onDisplayRemoved(int displayId) {
wilsonshihe7903ea2018-09-26 16:17:59 +0800447 final KeyguardDisplayState state = mDisplayStates.get(displayId);
448 if (state != null) {
449 state.onRemoved();
wilsonshih0299c8a2018-08-24 15:52:57 +0800450 mDisplayStates.remove(displayId);
451 }
452 }
453
454 /** Represents Keyguard state per individual display. */
455 private static class KeyguardDisplayState {
456 private final int mDisplayId;
457 private boolean mOccluded;
458 private ActivityRecord mDismissingKeyguardActivity;
459 private boolean mRequestDismissKeyguard;
460 private final ActivityTaskManagerService mService;
461 private SleepToken mSleepToken;
462
463 KeyguardDisplayState(ActivityTaskManagerService service, int displayId) {
464 mService = service;
465 mDisplayId = displayId;
466 }
467
468 void onRemoved() {
469 mDismissingKeyguardActivity = null;
470 releaseSleepToken();
471 }
472
473 void acquiredSleepToken() {
474 if (mSleepToken == null) {
475 mSleepToken = mService.acquireSleepToken("keyguard", mDisplayId);
476 }
477 }
478
479 void releaseSleepToken() {
480 if (mSleepToken != null) {
481 mSleepToken.release();
482 mSleepToken = null;
483 }
484 }
485
486 void visibilitiesUpdated(KeyguardController controller, ActivityDisplay display) {
487 final boolean lastOccluded = mOccluded;
488 final ActivityRecord lastDismissActivity = mDismissingKeyguardActivity;
489 mRequestDismissKeyguard = false;
490 mOccluded = false;
491 mDismissingKeyguardActivity = null;
492
wilsonshih9a10e9d2019-01-11 14:39:27 +0800493 final ActivityStack stack = getStackForControllingOccluding(display);
494 if (stack != null) {
495 final ActivityRecord topDismissing = stack.getTopDismissingKeyguardActivity();
496 mOccluded = stack.topActivityOccludesKeyguard() || (topDismissing != null
Wale Ogunwale85fb19a2019-12-05 10:41:05 +0900497 && stack.topRunningActivity() == topDismissing
wilsonshih9a10e9d2019-01-11 14:39:27 +0800498 && controller.canShowWhileOccluded(
wilsonshih0299c8a2018-08-24 15:52:57 +0800499 true /* dismissKeyguard */,
500 false /* showWhenLocked */));
wilsonshih9a10e9d2019-01-11 14:39:27 +0800501 if (stack.getTopDismissingKeyguardActivity() != null) {
502 mDismissingKeyguardActivity = stack.getTopDismissingKeyguardActivity();
wilsonshih0299c8a2018-08-24 15:52:57 +0800503 }
wilsonshih498a4b82018-12-11 16:10:16 +0800504 // FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD only apply for secondary display.
505 if (mDisplayId != DEFAULT_DISPLAY) {
506 mOccluded |= stack.canShowWithInsecureKeyguard()
507 && controller.canDismissKeyguard();
508 }
wilsonshih0299c8a2018-08-24 15:52:57 +0800509 }
wilsonshih498a4b82018-12-11 16:10:16 +0800510 // TODO(b/123372519): isShowingDream can only works on default display.
511 if (mDisplayId == DEFAULT_DISPLAY) {
Riddle Hsu8419e4b2019-09-18 23:28:01 +0800512 mOccluded |= mService.mRootActivityContainer.getDefaultDisplay().mDisplayContent
513 .getDisplayPolicy().isShowingDreamLw();
wilsonshih498a4b82018-12-11 16:10:16 +0800514 }
wilsonshih0299c8a2018-08-24 15:52:57 +0800515
Issei Suzuki62356a22019-04-11 16:46:37 +0200516 if (lastOccluded != mOccluded) {
517 controller.handleOccludedChanged(mDisplayId);
wilsonshih0299c8a2018-08-24 15:52:57 +0800518 }
519 if (lastDismissActivity != mDismissingKeyguardActivity && !mOccluded
520 && mDismissingKeyguardActivity != null
Brad Stenning2bdc21e2019-03-11 14:33:22 -0700521 && controller.mWindowManager.isKeyguardSecure(
522 controller.mService.getCurrentUserId())) {
wilsonshih0299c8a2018-08-24 15:52:57 +0800523 mRequestDismissKeyguard = true;
524 }
525 }
526
wilsonshih9a10e9d2019-01-11 14:39:27 +0800527 /**
528 * Gets the stack used to check the occluded state.
529 * <p>
530 * Only the top non-pinned activity of the focusable stack on each display can control its
531 * occlusion state.
532 */
533 private ActivityStack getStackForControllingOccluding(ActivityDisplay display) {
Louis Chang2453d062019-11-19 22:30:48 +0800534 for (int stackNdx = display.getStackCount() - 1; stackNdx >= 0; --stackNdx) {
535 final ActivityStack stack = display.getStackAt(stackNdx);
wilsonshih9a10e9d2019-01-11 14:39:27 +0800536 if (stack != null && stack.isFocusableAndVisible()
537 && !stack.inPinnedWindowingMode()) {
538 return stack;
539 }
540 }
541 return null;
542 }
543
wilsonshih0299c8a2018-08-24 15:52:57 +0800544 void dumpStatus(PrintWriter pw, String prefix) {
545 final StringBuilder sb = new StringBuilder();
546 sb.append(prefix);
547 sb.append(" Occluded=").append(mOccluded)
548 .append(" DismissingKeyguardActivity=")
549 .append(mDismissingKeyguardActivity)
550 .append(" at display=")
551 .append(mDisplayId);
552 pw.println(sb.toString());
553 }
554
Jeffrey Huangcb782852019-12-05 11:28:11 -0800555 void dumpDebug(ProtoOutputStream proto, long fieldId) {
wilsonshih0299c8a2018-08-24 15:52:57 +0800556 final long token = proto.start(fieldId);
557 proto.write(DISPLAY_ID, mDisplayId);
558 proto.write(KEYGUARD_OCCLUDED, mOccluded);
559 proto.end(token);
David Stevens9440dc82017-03-16 19:00:20 -0700560 }
561 }
562
Jorim Jaggi8d786932016-10-26 19:08:36 -0700563 void dump(PrintWriter pw, String prefix) {
564 pw.println(prefix + "KeyguardController:");
565 pw.println(prefix + " mKeyguardShowing=" + mKeyguardShowing);
Lucas Dupin47a65c72018-02-15 14:16:18 -0800566 pw.println(prefix + " mAodShowing=" + mAodShowing);
Jorim Jaggi8d786932016-10-26 19:08:36 -0700567 pw.println(prefix + " mKeyguardGoingAway=" + mKeyguardGoingAway);
wilsonshih0299c8a2018-08-24 15:52:57 +0800568 dumpDisplayStates(pw, prefix);
Jorim Jaggi07961872016-11-23 11:28:57 +0100569 pw.println(prefix + " mDismissalRequested=" + mDismissalRequested);
Jorim Jaggi8d786932016-10-26 19:08:36 -0700570 pw.println(prefix + " mVisibilityTransactionDepth=" + mVisibilityTransactionDepth);
571 }
Steven Timotius4346f0a2017-09-12 11:07:21 -0700572
Jeffrey Huangcb782852019-12-05 11:28:11 -0800573 void dumpDebug(ProtoOutputStream proto, long fieldId) {
Steven Timotius4346f0a2017-09-12 11:07:21 -0700574 final long token = proto.start(fieldId);
Issei Suzukid6eb5a22019-02-20 23:08:03 +0100575 proto.write(AOD_SHOWING, mAodShowing);
Steven Timotius4346f0a2017-09-12 11:07:21 -0700576 proto.write(KEYGUARD_SHOWING, mKeyguardShowing);
wilsonshih0299c8a2018-08-24 15:52:57 +0800577 writeDisplayStatesToProto(proto, KEYGUARD_OCCLUDED_STATES);
Steven Timotius4346f0a2017-09-12 11:07:21 -0700578 proto.end(token);
579 }
wilsonshih0299c8a2018-08-24 15:52:57 +0800580
581 private void dumpDisplayStates(PrintWriter pw, String prefix) {
582 for (int i = 0; i < mDisplayStates.size(); i++) {
583 mDisplayStates.valueAt(i).dumpStatus(pw, prefix);
584 }
585 }
586
587 private void writeDisplayStatesToProto(ProtoOutputStream proto, long fieldId) {
588 for (int i = 0; i < mDisplayStates.size(); i++) {
Jeffrey Huangcb782852019-12-05 11:28:11 -0800589 mDisplayStates.valueAt(i).dumpDebug(proto, fieldId);
wilsonshih0299c8a2018-08-24 15:52:57 +0800590 }
591 }
Jorim Jaggife762342016-10-13 14:33:27 +0200592}