blob: 3b58eca2ecb90ef4289fca817947e79e5382c772 [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);
199 mRootActivityContainer.addStartingWindowsForVisibleActivities(
200 true /* taskSwitch */);
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -0700201 mWindowManager.executeAppTransition();
202 } finally {
Riddle Hsua0022cd2019-09-09 21:12:41 +0800203 mService.continueWindowLayout();
Riddle Hsu2ca561b2019-10-08 21:58:58 +0800204 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
Jorim Jaggife762342016-10-13 14:33:27 +0200205 }
206 }
207
Lucas Dupinc80c67e2017-12-04 14:29:10 -0800208 void dismissKeyguard(IBinder token, IKeyguardDismissCallback callback, CharSequence message) {
Jorim Jaggi241ae102016-11-02 21:57:33 -0700209 final ActivityRecord activityRecord = ActivityRecord.forTokenLocked(token);
210 if (activityRecord == null || !activityRecord.visibleIgnoringKeyguard) {
211 failCallback(callback);
212 return;
213 }
Jorim Jaggid7214892017-07-18 14:05:19 +0200214 Slog.i(TAG, "Activity requesting to dismiss Keyguard: " + activityRecord);
chaviw59b98852017-06-13 12:05:44 -0700215
216 // If the client has requested to dismiss the keyguard and the Activity has the flag to
217 // turn the screen on, wakeup the screen if it's the top Activity.
218 if (activityRecord.getTurnScreenOnFlag() && activityRecord.isTopRunningActivity()) {
219 mStackSupervisor.wakeUp("dismissKeyguard");
220 }
221
Lucas Dupinc80c67e2017-12-04 14:29:10 -0800222 mWindowManager.dismissKeyguard(callback, message);
Jorim Jaggi241ae102016-11-02 21:57:33 -0700223 }
224
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -0700225 private void setKeyguardGoingAway(boolean keyguardGoingAway) {
226 mKeyguardGoingAway = keyguardGoingAway;
227 mWindowManager.setKeyguardGoingAway(keyguardGoingAway);
228 }
229
Jorim Jaggi241ae102016-11-02 21:57:33 -0700230 private void failCallback(IKeyguardDismissCallback callback) {
231 try {
232 callback.onDismissError();
233 } catch (RemoteException e) {
234 Slog.w(TAG, "Failed to call callback", e);
235 }
236 }
237
Jorim Jaggife762342016-10-13 14:33:27 +0200238 private int convertTransitFlags(int keyguardGoingAwayFlags) {
239 int result = 0;
240 if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_TO_SHADE) != 0) {
241 result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_TO_SHADE;
242 }
243 if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_NO_WINDOW_ANIMATIONS) != 0) {
244 result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_NO_ANIMATION;
245 }
246 if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_WITH_WALLPAPER) != 0) {
247 result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_WITH_WALLPAPER;
248 }
Issei Suzuki5609ccb2019-06-13 15:04:08 +0200249 if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_SUBTLE_WINDOW_ANIMATIONS) != 0) {
250 result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_SUBTLE_ANIMATION;
251 }
Jorim Jaggife762342016-10-13 14:33:27 +0200252 return result;
253 }
254
255 /**
256 * Starts a batch of visibility updates.
257 */
258 void beginActivityVisibilityUpdate() {
259 mVisibilityTransactionDepth++;
260 }
261
262 /**
263 * Ends a batch of visibility updates. After all batches are done, this method makes sure to
264 * update lockscreen occluded/dismiss state if needed.
265 */
266 void endActivityVisibilityUpdate() {
267 mVisibilityTransactionDepth--;
268 if (mVisibilityTransactionDepth == 0) {
269 visibilitiesUpdated();
270 }
271 }
272
Jorim Jaggie69c9312016-10-31 18:24:38 -0700273 /**
274 * @return True if we may show an activity while Keyguard is showing because we are in the
275 * process of dismissing it anyways, false otherwise.
276 */
Jorim Jaggi07961872016-11-23 11:28:57 +0100277 boolean canShowActivityWhileKeyguardShowing(ActivityRecord r, boolean dismissKeyguard) {
278
279 // Allow to show it when we are about to dismiss Keyguard. This isn't allowed if r is
280 // already the dismissing activity, in which case we don't allow it to repeatedly dismiss
281 // Keyguard.
Lucas Dupin47a65c72018-02-15 14:16:18 -0800282 return dismissKeyguard && canDismissKeyguard() && !mAodShowing
wilsonshih0299c8a2018-08-24 15:52:57 +0800283 && (mDismissalRequested
Arthur Hungcf58b332019-04-17 16:07:10 +0800284 || (r.canShowWhenLocked()
285 && getDisplay(r.getDisplayId()).mDismissingKeyguardActivity != r));
Jorim Jaggi07961872016-11-23 11:28:57 +0100286 }
287
288 /**
289 * @return True if we may show an activity while Keyguard is occluded, false otherwise.
290 */
291 boolean canShowWhileOccluded(boolean dismissKeyguard, boolean showWhenLocked) {
Brad Stenning2bdc21e2019-03-11 14:33:22 -0700292 return showWhenLocked || dismissKeyguard
293 && !mWindowManager.isKeyguardSecure(mService.getCurrentUserId());
Jorim Jaggie69c9312016-10-31 18:24:38 -0700294 }
295
Jorim Jaggife762342016-10-13 14:33:27 +0200296 private void visibilitiesUpdated() {
wilsonshih0299c8a2018-08-24 15:52:57 +0800297 boolean requestDismissKeyguard = false;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800298 for (int displayNdx = mRootActivityContainer.getChildCount() - 1;
299 displayNdx >= 0; displayNdx--) {
300 final ActivityDisplay display = mRootActivityContainer.getChildAt(displayNdx);
wilsonshih0299c8a2018-08-24 15:52:57 +0800301 final KeyguardDisplayState state = getDisplay(display.mDisplayId);
302 state.visibilitiesUpdated(this, display);
303 requestDismissKeyguard |= state.mRequestDismissKeyguard;
Jorim Jaggife762342016-10-13 14:33:27 +0200304 }
wilsonshih0299c8a2018-08-24 15:52:57 +0800305
306 // Dismissing Keyguard happens globally using the information from all displays.
307 if (requestDismissKeyguard) {
Jorim Jaggife762342016-10-13 14:33:27 +0200308 handleDismissKeyguard();
309 }
310 }
311
312 /**
313 * Called when occluded state changed.
314 */
Issei Suzuki62356a22019-04-11 16:46:37 +0200315 private void handleOccludedChanged(int displayId) {
316 // TODO(b/113840485): Handle app transition for individual display, and apply occluded
317 // state change to secondary displays.
318 // For now, only default display fully supports occluded change. Other displays only
319 // updates keygaurd sleep token on that display.
320 if (displayId != DEFAULT_DISPLAY) {
321 updateKeyguardSleepToken(displayId);
322 return;
323 }
324
Riddle Hsu8419e4b2019-09-18 23:28:01 +0800325 mWindowManager.mPolicy.onKeyguardOccludedChangedLw(isDisplayOccluded(DEFAULT_DISPLAY));
Jorim Jaggife762342016-10-13 14:33:27 +0200326 if (isKeyguardLocked()) {
Riddle Hsua0022cd2019-09-09 21:12:41 +0800327 mService.deferWindowLayout();
Jorim Jaggife762342016-10-13 14:33:27 +0200328 try {
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800329 mRootActivityContainer.getDefaultDisplay().mDisplayContent
lumark588a3e82018-07-20 18:53:54 +0800330 .prepareAppTransition(resolveOccludeTransit(),
331 false /* alwaysKeepCurrent */, 0 /* flags */,
332 true /* forceOverride */);
Issei Suzuki62356a22019-04-11 16:46:37 +0200333 updateKeyguardSleepToken(DEFAULT_DISPLAY);
Wale Ogunwaled32da472018-11-16 07:19:28 -0800334 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
Jorim Jaggife762342016-10-13 14:33:27 +0200335 mWindowManager.executeAppTransition();
336 } finally {
Riddle Hsua0022cd2019-09-09 21:12:41 +0800337 mService.continueWindowLayout();
Jorim Jaggife762342016-10-13 14:33:27 +0200338 }
339 }
340 dismissDockedStackIfNeeded();
341 }
342
343 /**
wilsonshih0299c8a2018-08-24 15:52:57 +0800344 * Called when somebody wants to dismiss the Keyguard via the flag.
Jorim Jaggife762342016-10-13 14:33:27 +0200345 */
346 private void handleDismissKeyguard() {
Jorim Jaggi07961872016-11-23 11:28:57 +0100347 // We only allow dismissing Keyguard via the flag when Keyguard is secure for legacy
348 // reasons, because that's how apps used to dismiss Keyguard in the secure case. In the
349 // insecure case, we actually show it on top of the lockscreen. See #canShowWhileOccluded.
Brad Stenning2bdc21e2019-03-11 14:33:22 -0700350 if (!mWindowManager.isKeyguardSecure(mService.getCurrentUserId())) {
Wale Ogunwaled32da472018-11-16 07:19:28 -0800351 return;
352 }
Jorim Jaggife762342016-10-13 14:33:27 +0200353
Wale Ogunwaled32da472018-11-16 07:19:28 -0800354 mWindowManager.dismissKeyguard(null /* callback */, null /* message */);
355 mDismissalRequested = true;
356
357 // If we are about to unocclude the Keyguard, but we can dismiss it without security,
358 // we immediately dismiss the Keyguard so the activity gets shown without a flicker.
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800359 final DisplayContent dc =
360 mRootActivityContainer.getDefaultDisplay().mDisplayContent;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800361 if (mKeyguardShowing && canDismissKeyguard()
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800362 && dc.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
363 dc.prepareAppTransition(mBeforeUnoccludeTransit, false /* alwaysKeepCurrent */,
Wale Ogunwaled32da472018-11-16 07:19:28 -0800364 0 /* flags */, true /* forceOverride */);
365 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
366 mWindowManager.executeAppTransition();
Jorim Jaggife762342016-10-13 14:33:27 +0200367 }
368 }
369
Issei Suzuki16d4de52019-07-29 13:12:01 +0000370 private boolean isDisplayOccluded(int displayId) {
wilsonshih0299c8a2018-08-24 15:52:57 +0800371 return getDisplay(displayId).mOccluded;
372 }
373
Jorim Jaggife762342016-10-13 14:33:27 +0200374 /**
375 * @return true if Keyguard can be currently dismissed without entering credentials.
376 */
Andrii Kulianfc8f82b2017-01-26 13:17:27 -0800377 boolean canDismissKeyguard() {
Riddle Hsu8419e4b2019-09-18 23:28:01 +0800378 return mWindowManager.mPolicy.isKeyguardTrustedLw()
Brad Stenning2bdc21e2019-03-11 14:33:22 -0700379 || !mWindowManager.isKeyguardSecure(mService.getCurrentUserId());
Jorim Jaggife762342016-10-13 14:33:27 +0200380 }
381
382 private int resolveOccludeTransit() {
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800383 final DisplayContent dc =
384 mService.mRootActivityContainer.getDefaultDisplay().mDisplayContent;
Jorim Jaggife762342016-10-13 14:33:27 +0200385 if (mBeforeUnoccludeTransit != TRANSIT_UNSET
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800386 && dc.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
wilsonshih0299c8a2018-08-24 15:52:57 +0800387 // TODO(b/113840485): Handle app transition for individual display.
Issei Suzuki16d4de52019-07-29 13:12:01 +0000388 && isDisplayOccluded(DEFAULT_DISPLAY)) {
Jorim Jaggife762342016-10-13 14:33:27 +0200389
390 // Reuse old transit in case we are occluding Keyguard again, meaning that we never
391 // actually occclude/unocclude Keyguard, but just run a normal transition.
392 return mBeforeUnoccludeTransit;
wilsonshih0299c8a2018-08-24 15:52:57 +0800393 // TODO(b/113840485): Handle app transition for individual display.
Issei Suzuki16d4de52019-07-29 13:12:01 +0000394 } else if (!isDisplayOccluded(DEFAULT_DISPLAY)) {
Jorim Jaggife762342016-10-13 14:33:27 +0200395
396 // Save transit in case we dismiss/occlude Keyguard shortly after.
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800397 mBeforeUnoccludeTransit = dc.mAppTransition.getAppTransition();
Jorim Jaggife762342016-10-13 14:33:27 +0200398 return TRANSIT_KEYGUARD_UNOCCLUDE;
399 } else {
400 return TRANSIT_KEYGUARD_OCCLUDE;
401 }
402 }
403
404 private void dismissDockedStackIfNeeded() {
wilsonshih0299c8a2018-08-24 15:52:57 +0800405 // TODO(b/113840485): Handle docked stack for individual display.
Issei Suzuki16d4de52019-07-29 13:12:01 +0000406 if (mKeyguardShowing && isDisplayOccluded(DEFAULT_DISPLAY)) {
Jorim Jaggife762342016-10-13 14:33:27 +0200407 // The lock screen is currently showing, but is occluded by a window that can
408 // show on top of the lock screen. In this can we want to dismiss the docked
409 // stack since it will be complicated/risky to try to put the activity on top
410 // of the lock screen in the right fullscreen configuration.
Wale Ogunwaled32da472018-11-16 07:19:28 -0800411 final ActivityStack stack =
412 mRootActivityContainer.getDefaultDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700413 if (stack == null) {
414 return;
415 }
416 mStackSupervisor.moveTasksToFullscreenStackLocked(stack,
Andrii Kulian52d255c2018-07-13 11:32:19 -0700417 stack.isFocusedStackOnDisplay());
Jorim Jaggife762342016-10-13 14:33:27 +0200418 }
419 }
Jorim Jaggi8d786932016-10-26 19:08:36 -0700420
David Stevens9440dc82017-03-16 19:00:20 -0700421 private void updateKeyguardSleepToken() {
Wale Ogunwaled32da472018-11-16 07:19:28 -0800422 for (int displayNdx = mRootActivityContainer.getChildCount() - 1;
423 displayNdx >= 0; displayNdx--) {
424 final ActivityDisplay display = mRootActivityContainer.getChildAt(displayNdx);
Issei Suzuki62356a22019-04-11 16:46:37 +0200425 updateKeyguardSleepToken(display.mDisplayId);
426 }
427 }
428
429 private void updateKeyguardSleepToken(int displayId) {
430 final KeyguardDisplayState state = getDisplay(displayId);
Issei Suzuki16d4de52019-07-29 13:12:01 +0000431 if (isKeyguardUnoccludedOrAodShowing(displayId) && state.mSleepToken == null) {
Issei Suzuki62356a22019-04-11 16:46:37 +0200432 state.acquiredSleepToken();
Issei Suzuki16d4de52019-07-29 13:12:01 +0000433 } else if (!isKeyguardUnoccludedOrAodShowing(displayId) && state.mSleepToken != null) {
Issei Suzuki62356a22019-04-11 16:46:37 +0200434 state.releaseSleepToken();
wilsonshih0299c8a2018-08-24 15:52:57 +0800435 }
436 }
437
438 private KeyguardDisplayState getDisplay(int displayId) {
wilsonshihe7903ea2018-09-26 16:17:59 +0800439 KeyguardDisplayState state = mDisplayStates.get(displayId);
440 if (state == null) {
441 state = new KeyguardDisplayState(mService, displayId);
442 mDisplayStates.append(displayId, state);
wilsonshih0299c8a2018-08-24 15:52:57 +0800443 }
wilsonshihe7903ea2018-09-26 16:17:59 +0800444 return state;
wilsonshih0299c8a2018-08-24 15:52:57 +0800445 }
446
447 void onDisplayRemoved(int displayId) {
wilsonshihe7903ea2018-09-26 16:17:59 +0800448 final KeyguardDisplayState state = mDisplayStates.get(displayId);
449 if (state != null) {
450 state.onRemoved();
wilsonshih0299c8a2018-08-24 15:52:57 +0800451 mDisplayStates.remove(displayId);
452 }
453 }
454
455 /** Represents Keyguard state per individual display. */
456 private static class KeyguardDisplayState {
457 private final int mDisplayId;
458 private boolean mOccluded;
459 private ActivityRecord mDismissingKeyguardActivity;
460 private boolean mRequestDismissKeyguard;
461 private final ActivityTaskManagerService mService;
462 private SleepToken mSleepToken;
463
464 KeyguardDisplayState(ActivityTaskManagerService service, int displayId) {
465 mService = service;
466 mDisplayId = displayId;
467 }
468
469 void onRemoved() {
470 mDismissingKeyguardActivity = null;
471 releaseSleepToken();
472 }
473
474 void acquiredSleepToken() {
475 if (mSleepToken == null) {
476 mSleepToken = mService.acquireSleepToken("keyguard", mDisplayId);
477 }
478 }
479
480 void releaseSleepToken() {
481 if (mSleepToken != null) {
482 mSleepToken.release();
483 mSleepToken = null;
484 }
485 }
486
487 void visibilitiesUpdated(KeyguardController controller, ActivityDisplay display) {
488 final boolean lastOccluded = mOccluded;
489 final ActivityRecord lastDismissActivity = mDismissingKeyguardActivity;
490 mRequestDismissKeyguard = false;
491 mOccluded = false;
492 mDismissingKeyguardActivity = null;
493
wilsonshih9a10e9d2019-01-11 14:39:27 +0800494 final ActivityStack stack = getStackForControllingOccluding(display);
495 if (stack != null) {
496 final ActivityRecord topDismissing = stack.getTopDismissingKeyguardActivity();
497 mOccluded = stack.topActivityOccludesKeyguard() || (topDismissing != null
498 && stack.topRunningActivityLocked() == topDismissing
499 && controller.canShowWhileOccluded(
wilsonshih0299c8a2018-08-24 15:52:57 +0800500 true /* dismissKeyguard */,
501 false /* showWhenLocked */));
wilsonshih9a10e9d2019-01-11 14:39:27 +0800502 if (stack.getTopDismissingKeyguardActivity() != null) {
503 mDismissingKeyguardActivity = stack.getTopDismissingKeyguardActivity();
wilsonshih0299c8a2018-08-24 15:52:57 +0800504 }
wilsonshih498a4b82018-12-11 16:10:16 +0800505 // FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD only apply for secondary display.
506 if (mDisplayId != DEFAULT_DISPLAY) {
507 mOccluded |= stack.canShowWithInsecureKeyguard()
508 && controller.canDismissKeyguard();
509 }
wilsonshih0299c8a2018-08-24 15:52:57 +0800510 }
wilsonshih498a4b82018-12-11 16:10:16 +0800511 // TODO(b/123372519): isShowingDream can only works on default display.
512 if (mDisplayId == DEFAULT_DISPLAY) {
Riddle Hsu8419e4b2019-09-18 23:28:01 +0800513 mOccluded |= mService.mRootActivityContainer.getDefaultDisplay().mDisplayContent
514 .getDisplayPolicy().isShowingDreamLw();
wilsonshih498a4b82018-12-11 16:10:16 +0800515 }
wilsonshih0299c8a2018-08-24 15:52:57 +0800516
Issei Suzuki62356a22019-04-11 16:46:37 +0200517 if (lastOccluded != mOccluded) {
518 controller.handleOccludedChanged(mDisplayId);
wilsonshih0299c8a2018-08-24 15:52:57 +0800519 }
520 if (lastDismissActivity != mDismissingKeyguardActivity && !mOccluded
521 && mDismissingKeyguardActivity != null
Brad Stenning2bdc21e2019-03-11 14:33:22 -0700522 && controller.mWindowManager.isKeyguardSecure(
523 controller.mService.getCurrentUserId())) {
wilsonshih0299c8a2018-08-24 15:52:57 +0800524 mRequestDismissKeyguard = true;
525 }
526 }
527
wilsonshih9a10e9d2019-01-11 14:39:27 +0800528 /**
529 * Gets the stack used to check the occluded state.
530 * <p>
531 * Only the top non-pinned activity of the focusable stack on each display can control its
532 * occlusion state.
533 */
534 private ActivityStack getStackForControllingOccluding(ActivityDisplay display) {
535 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
536 final ActivityStack stack = display.getChildAt(stackNdx);
537 if (stack != null && stack.isFocusableAndVisible()
538 && !stack.inPinnedWindowingMode()) {
539 return stack;
540 }
541 }
542 return null;
543 }
544
wilsonshih0299c8a2018-08-24 15:52:57 +0800545 void dumpStatus(PrintWriter pw, String prefix) {
546 final StringBuilder sb = new StringBuilder();
547 sb.append(prefix);
548 sb.append(" Occluded=").append(mOccluded)
549 .append(" DismissingKeyguardActivity=")
550 .append(mDismissingKeyguardActivity)
551 .append(" at display=")
552 .append(mDisplayId);
553 pw.println(sb.toString());
554 }
555
556 void writeToProto(ProtoOutputStream proto, long fieldId) {
557 final long token = proto.start(fieldId);
558 proto.write(DISPLAY_ID, mDisplayId);
559 proto.write(KEYGUARD_OCCLUDED, mOccluded);
560 proto.end(token);
David Stevens9440dc82017-03-16 19:00:20 -0700561 }
562 }
563
Jorim Jaggi8d786932016-10-26 19:08:36 -0700564 void dump(PrintWriter pw, String prefix) {
565 pw.println(prefix + "KeyguardController:");
566 pw.println(prefix + " mKeyguardShowing=" + mKeyguardShowing);
Lucas Dupin47a65c72018-02-15 14:16:18 -0800567 pw.println(prefix + " mAodShowing=" + mAodShowing);
Jorim Jaggi8d786932016-10-26 19:08:36 -0700568 pw.println(prefix + " mKeyguardGoingAway=" + mKeyguardGoingAway);
wilsonshih0299c8a2018-08-24 15:52:57 +0800569 dumpDisplayStates(pw, prefix);
Jorim Jaggi07961872016-11-23 11:28:57 +0100570 pw.println(prefix + " mDismissalRequested=" + mDismissalRequested);
Jorim Jaggi8d786932016-10-26 19:08:36 -0700571 pw.println(prefix + " mVisibilityTransactionDepth=" + mVisibilityTransactionDepth);
572 }
Steven Timotius4346f0a2017-09-12 11:07:21 -0700573
574 void writeToProto(ProtoOutputStream proto, long fieldId) {
575 final long token = proto.start(fieldId);
Issei Suzukid6eb5a22019-02-20 23:08:03 +0100576 proto.write(AOD_SHOWING, mAodShowing);
Steven Timotius4346f0a2017-09-12 11:07:21 -0700577 proto.write(KEYGUARD_SHOWING, mKeyguardShowing);
wilsonshih0299c8a2018-08-24 15:52:57 +0800578 writeDisplayStatesToProto(proto, KEYGUARD_OCCLUDED_STATES);
Steven Timotius4346f0a2017-09-12 11:07:21 -0700579 proto.end(token);
580 }
wilsonshih0299c8a2018-08-24 15:52:57 +0800581
582 private void dumpDisplayStates(PrintWriter pw, String prefix) {
583 for (int i = 0; i < mDisplayStates.size(); i++) {
584 mDisplayStates.valueAt(i).dumpStatus(pw, prefix);
585 }
586 }
587
588 private void writeDisplayStatesToProto(ProtoOutputStream proto, long fieldId) {
589 for (int i = 0; i < mDisplayStates.size(); i++) {
590 mDisplayStates.valueAt(i).writeToProto(proto, fieldId);
591 }
592 }
Jorim Jaggife762342016-10-13 14:33:27 +0200593}