blob: d528ef6ec6a5b0719f36e3704273a6114a182b21 [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
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -070019import static android.os.Trace.TRACE_TAG_ACTIVITY_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;
wilsonshih30eca702019-07-10 09:22:11 +080046import android.util.EventLog;
Jorim Jaggi241ae102016-11-02 21:57:33 -070047import android.util.Slog;
wilsonshih0299c8a2018-08-24 15:52:57 +080048import android.util.SparseArray;
Steven Timotius4346f0a2017-09-12 11:07:21 -070049import android.util.proto.ProtoOutputStream;
Adrian Roose99bc052017-11-20 17:55:31 +010050
Jorim Jaggi241ae102016-11-02 21:57:33 -070051import com.android.internal.policy.IKeyguardDismissCallback;
wilsonshih30eca702019-07-10 09:22:11 +080052import com.android.server.am.EventLogTags;
Adrian Roose99bc052017-11-20 17:55:31 +010053import com.android.server.policy.WindowManagerPolicy;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070054import com.android.server.wm.ActivityTaskManagerInternal.SleepToken;
Adrian Roose99bc052017-11-20 17:55:31 +010055
Jorim Jaggi8d786932016-10-26 19:08:36 -070056import java.io.PrintWriter;
Jorim Jaggife762342016-10-13 14:33:27 +020057
58/**
59 * Controls Keyguard occluding, dismissing and transitions depending on what kind of activities are
60 * currently visible.
61 * <p>
62 * Note that everything in this class should only be accessed with the AM lock being held.
63 */
64class KeyguardController {
65
Wale Ogunwale98875612018-10-12 07:53:02 -070066 private static final String TAG = TAG_WITH_CLASS_NAME ? "KeyguardController" : TAG_ATM;
Jorim Jaggi241ae102016-11-02 21:57:33 -070067
Jorim Jaggife762342016-10-13 14:33:27 +020068 private final ActivityStackSupervisor mStackSupervisor;
69 private WindowManagerService mWindowManager;
Jorim Jaggife762342016-10-13 14:33:27 +020070 private boolean mKeyguardShowing;
Lucas Dupin47a65c72018-02-15 14:16:18 -080071 private boolean mAodShowing;
Jorim Jaggi8d786932016-10-26 19:08:36 -070072 private boolean mKeyguardGoingAway;
Jorim Jaggi07961872016-11-23 11:28:57 +010073 private boolean mDismissalRequested;
wilsonshihe7903ea2018-09-26 16:17:59 +080074 private int[] mSecondaryDisplayIdsShowing;
Jorim Jaggife762342016-10-13 14:33:27 +020075 private int mBeforeUnoccludeTransit;
76 private int mVisibilityTransactionDepth;
wilsonshih0299c8a2018-08-24 15:52:57 +080077 private final SparseArray<KeyguardDisplayState> mDisplayStates = new SparseArray<>();
78 private final ActivityTaskManagerService mService;
Wale Ogunwaled32da472018-11-16 07:19:28 -080079 private RootActivityContainer mRootActivityContainer;
Jorim Jaggife762342016-10-13 14:33:27 +020080
Wale Ogunwalef6733932018-06-27 05:14:34 -070081 KeyguardController(ActivityTaskManagerService service,
Jorim Jaggife762342016-10-13 14:33:27 +020082 ActivityStackSupervisor stackSupervisor) {
83 mService = service;
84 mStackSupervisor = stackSupervisor;
85 }
86
87 void setWindowManager(WindowManagerService windowManager) {
88 mWindowManager = windowManager;
Wale Ogunwaled32da472018-11-16 07:19:28 -080089 mRootActivityContainer = mService.mRootActivityContainer;
Jorim Jaggife762342016-10-13 14:33:27 +020090 }
91
92 /**
Issei Suzuki16d4de52019-07-29 13:12:01 +000093 * @return true if either Keyguard or AOD are showing, not going away, and not being occluded
94 * on the given display, false otherwise.
Lucas Dupin47a65c72018-02-15 14:16:18 -080095 */
96 boolean isKeyguardOrAodShowing(int displayId) {
Issei Suzuki16d4de52019-07-29 13:12:01 +000097 return (mKeyguardShowing || mAodShowing) && !mKeyguardGoingAway
98 && !isDisplayOccluded(displayId);
99 }
100
101 /**
102 * @return {@code true} for default display when AOD is showing. Otherwise, same as
103 * {@link #isKeyguardOrAodShowing(int)}
104 * TODO(b/125198167): Replace isKeyguardOrAodShowing() by this logic.
105 */
106 boolean isKeyguardUnoccludedOrAodShowing(int displayId) {
107 if (displayId == DEFAULT_DISPLAY && mAodShowing) {
108 return true;
109 }
110 return isKeyguardOrAodShowing(displayId);
Issei Suzukid6eb5a22019-02-20 23:08:03 +0100111 }
112
113 /**
David Stevens53a39ea2017-08-23 18:41:49 -0700114 * @return true if Keyguard is showing, not going away, and not being occluded on the given
115 * display, false otherwise
Jorim Jaggife762342016-10-13 14:33:27 +0200116 */
David Stevens53a39ea2017-08-23 18:41:49 -0700117 boolean isKeyguardShowing(int displayId) {
Issei Suzuki16d4de52019-07-29 13:12:01 +0000118 return mKeyguardShowing && !mKeyguardGoingAway && !isDisplayOccluded(displayId);
Jorim Jaggife762342016-10-13 14:33:27 +0200119 }
120
121 /**
122 * @return true if Keyguard is either showing or occluded, but not going away
123 */
124 boolean isKeyguardLocked() {
125 return mKeyguardShowing && !mKeyguardGoingAway;
126 }
127
128 /**
Bryce Lee271617a2018-03-15 10:39:12 -0700129 * @return {@code true} if the keyguard is going away, {@code false} otherwise.
130 */
131 boolean isKeyguardGoingAway() {
132 // Also check keyguard showing in case value is stale.
133 return mKeyguardGoingAway && mKeyguardShowing;
134 }
135
136 /**
Jorim Jaggife762342016-10-13 14:33:27 +0200137 * Update the Keyguard showing state.
138 */
wilsonshih177261f2019-02-22 12:02:18 +0800139 void setKeyguardShown(boolean keyguardShowing, boolean aodShowing) {
Adrian Roos6ec76b72018-04-25 14:01:11 +0200140 // If keyguard is going away, but SystemUI aborted the transition, need to reset state.
wilsonshih2bc846c2019-05-02 14:35:21 +0800141 final boolean keyguardChanged = keyguardShowing != mKeyguardShowing
142 || mKeyguardGoingAway && keyguardShowing;
143 final boolean aodChanged = aodShowing != mAodShowing;
144 if (!keyguardChanged && !aodChanged) {
Jorim Jaggife762342016-10-13 14:33:27 +0200145 return;
146 }
wilsonshih30eca702019-07-10 09:22:11 +0800147 EventLog.writeEvent(EventLogTags.AM_SET_KEYGUARD_SHOWN,
148 keyguardShowing ? 1 : 0,
149 aodShowing ? 1 : 0,
150 mKeyguardGoingAway ? 1 : 0,
151 "setKeyguardShown");
Lucas Dupin47a65c72018-02-15 14:16:18 -0800152 mKeyguardShowing = keyguardShowing;
153 mAodShowing = aodShowing;
chaviw0e9fb132018-06-05 16:29:13 -0700154 mWindowManager.setAodShowing(aodShowing);
wilsonshih2bc846c2019-05-02 14:35:21 +0800155
156 if (keyguardChanged) {
157 // Irrelevant to AOD.
David Stevens53a39ea2017-08-23 18:41:49 -0700158 dismissDockedStackIfNeeded();
Andrii Kulian0d595f32018-02-21 15:47:33 -0800159 setKeyguardGoingAway(false);
Lucas Dupin47a65c72018-02-15 14:16:18 -0800160 if (keyguardShowing) {
David Stevens53a39ea2017-08-23 18:41:49 -0700161 mDismissalRequested = false;
162 }
Jorim Jaggife762342016-10-13 14:33:27 +0200163 }
wilsonshih2bc846c2019-05-02 14:35:21 +0800164 // TODO(b/113840485): Check usage for non-default display
165 mWindowManager.setKeyguardOrAodShowingOnDefaultDisplay(
166 isKeyguardOrAodShowing(DEFAULT_DISPLAY));
167
168 // Update the sleep token first such that ensureActivitiesVisible has correct sleep token
169 // state when evaluating visibilities.
David Stevens9440dc82017-03-16 19:00:20 -0700170 updateKeyguardSleepToken();
wilsonshih2bc846c2019-05-02 14:35:21 +0800171 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
Jorim Jaggife762342016-10-13 14:33:27 +0200172 }
173
174 /**
175 * Called when Keyguard is going away.
176 *
Adrian Roose99bc052017-11-20 17:55:31 +0100177 * @param flags See {@link WindowManagerPolicy#KEYGUARD_GOING_AWAY_FLAG_TO_SHADE}
Jorim Jaggife762342016-10-13 14:33:27 +0200178 * etc.
179 */
180 void keyguardGoingAway(int flags) {
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -0700181 if (!mKeyguardShowing) {
182 return;
183 }
184 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "keyguardGoingAway");
Riddle Hsua0022cd2019-09-09 21:12:41 +0800185 mService.deferWindowLayout();
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -0700186 try {
187 setKeyguardGoingAway(true);
wilsonshih30eca702019-07-10 09:22:11 +0800188 EventLog.writeEvent(EventLogTags.AM_SET_KEYGUARD_SHOWN,
189 1 /* keyguardShowing */,
190 mAodShowing ? 1 : 0,
191 1 /* keyguardGoingAway */,
192 "keyguardGoingAway");
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800193 mRootActivityContainer.getDefaultDisplay().mDisplayContent
lumark588a3e82018-07-20 18:53:54 +0800194 .prepareAppTransition(TRANSIT_KEYGUARD_GOING_AWAY,
195 false /* alwaysKeepCurrent */, convertTransitFlags(flags),
196 false /* forceOverride */);
David Stevens9440dc82017-03-16 19:00:20 -0700197 updateKeyguardSleepToken();
Jorim Jaggife762342016-10-13 14:33:27 +0200198
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -0700199 // Some stack visibility might change (e.g. docked stack)
Wale Ogunwaled32da472018-11-16 07:19:28 -0800200 mRootActivityContainer.resumeFocusedStacksTopActivities();
201 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
202 mRootActivityContainer.addStartingWindowsForVisibleActivities(
203 true /* taskSwitch */);
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -0700204 mWindowManager.executeAppTransition();
205 } finally {
206 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "keyguardGoingAway: surfaceLayout");
Riddle Hsua0022cd2019-09-09 21:12:41 +0800207 mService.continueWindowLayout();
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -0700208 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
209
210 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Jorim Jaggife762342016-10-13 14:33:27 +0200211 }
212 }
213
Lucas Dupinc80c67e2017-12-04 14:29:10 -0800214 void dismissKeyguard(IBinder token, IKeyguardDismissCallback callback, CharSequence message) {
Jorim Jaggi241ae102016-11-02 21:57:33 -0700215 final ActivityRecord activityRecord = ActivityRecord.forTokenLocked(token);
216 if (activityRecord == null || !activityRecord.visibleIgnoringKeyguard) {
217 failCallback(callback);
218 return;
219 }
Jorim Jaggid7214892017-07-18 14:05:19 +0200220 Slog.i(TAG, "Activity requesting to dismiss Keyguard: " + activityRecord);
chaviw59b98852017-06-13 12:05:44 -0700221
222 // If the client has requested to dismiss the keyguard and the Activity has the flag to
223 // turn the screen on, wakeup the screen if it's the top Activity.
224 if (activityRecord.getTurnScreenOnFlag() && activityRecord.isTopRunningActivity()) {
225 mStackSupervisor.wakeUp("dismissKeyguard");
226 }
227
Lucas Dupinc80c67e2017-12-04 14:29:10 -0800228 mWindowManager.dismissKeyguard(callback, message);
Jorim Jaggi241ae102016-11-02 21:57:33 -0700229 }
230
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -0700231 private void setKeyguardGoingAway(boolean keyguardGoingAway) {
232 mKeyguardGoingAway = keyguardGoingAway;
233 mWindowManager.setKeyguardGoingAway(keyguardGoingAway);
234 }
235
Jorim Jaggi241ae102016-11-02 21:57:33 -0700236 private void failCallback(IKeyguardDismissCallback callback) {
237 try {
238 callback.onDismissError();
239 } catch (RemoteException e) {
240 Slog.w(TAG, "Failed to call callback", e);
241 }
242 }
243
Jorim Jaggife762342016-10-13 14:33:27 +0200244 private int convertTransitFlags(int keyguardGoingAwayFlags) {
245 int result = 0;
246 if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_TO_SHADE) != 0) {
247 result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_TO_SHADE;
248 }
249 if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_NO_WINDOW_ANIMATIONS) != 0) {
250 result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_NO_ANIMATION;
251 }
252 if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_WITH_WALLPAPER) != 0) {
253 result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_WITH_WALLPAPER;
254 }
Issei Suzuki5609ccb2019-06-13 15:04:08 +0200255 if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_SUBTLE_WINDOW_ANIMATIONS) != 0) {
256 result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_SUBTLE_ANIMATION;
257 }
Jorim Jaggife762342016-10-13 14:33:27 +0200258 return result;
259 }
260
261 /**
262 * Starts a batch of visibility updates.
263 */
264 void beginActivityVisibilityUpdate() {
265 mVisibilityTransactionDepth++;
266 }
267
268 /**
269 * Ends a batch of visibility updates. After all batches are done, this method makes sure to
270 * update lockscreen occluded/dismiss state if needed.
271 */
272 void endActivityVisibilityUpdate() {
273 mVisibilityTransactionDepth--;
274 if (mVisibilityTransactionDepth == 0) {
275 visibilitiesUpdated();
276 }
277 }
278
Jorim Jaggie69c9312016-10-31 18:24:38 -0700279 /**
280 * @return True if we may show an activity while Keyguard is showing because we are in the
281 * process of dismissing it anyways, false otherwise.
282 */
Jorim Jaggi07961872016-11-23 11:28:57 +0100283 boolean canShowActivityWhileKeyguardShowing(ActivityRecord r, boolean dismissKeyguard) {
284
285 // Allow to show it when we are about to dismiss Keyguard. This isn't allowed if r is
286 // already the dismissing activity, in which case we don't allow it to repeatedly dismiss
287 // Keyguard.
Lucas Dupin47a65c72018-02-15 14:16:18 -0800288 return dismissKeyguard && canDismissKeyguard() && !mAodShowing
wilsonshih0299c8a2018-08-24 15:52:57 +0800289 && (mDismissalRequested
Arthur Hungcf58b332019-04-17 16:07:10 +0800290 || (r.canShowWhenLocked()
291 && getDisplay(r.getDisplayId()).mDismissingKeyguardActivity != r));
Jorim Jaggi07961872016-11-23 11:28:57 +0100292 }
293
294 /**
295 * @return True if we may show an activity while Keyguard is occluded, false otherwise.
296 */
297 boolean canShowWhileOccluded(boolean dismissKeyguard, boolean showWhenLocked) {
Brad Stenning2bdc21e2019-03-11 14:33:22 -0700298 return showWhenLocked || dismissKeyguard
299 && !mWindowManager.isKeyguardSecure(mService.getCurrentUserId());
Jorim Jaggie69c9312016-10-31 18:24:38 -0700300 }
301
Jorim Jaggife762342016-10-13 14:33:27 +0200302 private void visibilitiesUpdated() {
wilsonshih0299c8a2018-08-24 15:52:57 +0800303 boolean requestDismissKeyguard = false;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800304 for (int displayNdx = mRootActivityContainer.getChildCount() - 1;
305 displayNdx >= 0; displayNdx--) {
306 final ActivityDisplay display = mRootActivityContainer.getChildAt(displayNdx);
wilsonshih0299c8a2018-08-24 15:52:57 +0800307 final KeyguardDisplayState state = getDisplay(display.mDisplayId);
308 state.visibilitiesUpdated(this, display);
309 requestDismissKeyguard |= state.mRequestDismissKeyguard;
Jorim Jaggife762342016-10-13 14:33:27 +0200310 }
wilsonshih0299c8a2018-08-24 15:52:57 +0800311
312 // Dismissing Keyguard happens globally using the information from all displays.
313 if (requestDismissKeyguard) {
Jorim Jaggife762342016-10-13 14:33:27 +0200314 handleDismissKeyguard();
315 }
316 }
317
318 /**
319 * Called when occluded state changed.
320 */
Issei Suzuki62356a22019-04-11 16:46:37 +0200321 private void handleOccludedChanged(int displayId) {
322 // TODO(b/113840485): Handle app transition for individual display, and apply occluded
323 // state change to secondary displays.
324 // For now, only default display fully supports occluded change. Other displays only
325 // updates keygaurd sleep token on that display.
326 if (displayId != DEFAULT_DISPLAY) {
327 updateKeyguardSleepToken(displayId);
328 return;
329 }
330
Issei Suzuki16d4de52019-07-29 13:12:01 +0000331 mWindowManager.onKeyguardOccludedChanged(isDisplayOccluded(DEFAULT_DISPLAY));
Jorim Jaggife762342016-10-13 14:33:27 +0200332 if (isKeyguardLocked()) {
Riddle Hsua0022cd2019-09-09 21:12:41 +0800333 mService.deferWindowLayout();
Jorim Jaggife762342016-10-13 14:33:27 +0200334 try {
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800335 mRootActivityContainer.getDefaultDisplay().mDisplayContent
lumark588a3e82018-07-20 18:53:54 +0800336 .prepareAppTransition(resolveOccludeTransit(),
337 false /* alwaysKeepCurrent */, 0 /* flags */,
338 true /* forceOverride */);
Issei Suzuki62356a22019-04-11 16:46:37 +0200339 updateKeyguardSleepToken(DEFAULT_DISPLAY);
Wale Ogunwaled32da472018-11-16 07:19:28 -0800340 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
Jorim Jaggife762342016-10-13 14:33:27 +0200341 mWindowManager.executeAppTransition();
342 } finally {
Riddle Hsua0022cd2019-09-09 21:12:41 +0800343 mService.continueWindowLayout();
Jorim Jaggife762342016-10-13 14:33:27 +0200344 }
345 }
346 dismissDockedStackIfNeeded();
347 }
348
349 /**
wilsonshih0299c8a2018-08-24 15:52:57 +0800350 * Called when somebody wants to dismiss the Keyguard via the flag.
Jorim Jaggife762342016-10-13 14:33:27 +0200351 */
352 private void handleDismissKeyguard() {
Jorim Jaggi07961872016-11-23 11:28:57 +0100353 // We only allow dismissing Keyguard via the flag when Keyguard is secure for legacy
354 // reasons, because that's how apps used to dismiss Keyguard in the secure case. In the
355 // insecure case, we actually show it on top of the lockscreen. See #canShowWhileOccluded.
Brad Stenning2bdc21e2019-03-11 14:33:22 -0700356 if (!mWindowManager.isKeyguardSecure(mService.getCurrentUserId())) {
Wale Ogunwaled32da472018-11-16 07:19:28 -0800357 return;
358 }
Jorim Jaggife762342016-10-13 14:33:27 +0200359
Wale Ogunwaled32da472018-11-16 07:19:28 -0800360 mWindowManager.dismissKeyguard(null /* callback */, null /* message */);
361 mDismissalRequested = true;
362
363 // If we are about to unocclude the Keyguard, but we can dismiss it without security,
364 // we immediately dismiss the Keyguard so the activity gets shown without a flicker.
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800365 final DisplayContent dc =
366 mRootActivityContainer.getDefaultDisplay().mDisplayContent;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800367 if (mKeyguardShowing && canDismissKeyguard()
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800368 && dc.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
369 dc.prepareAppTransition(mBeforeUnoccludeTransit, false /* alwaysKeepCurrent */,
Wale Ogunwaled32da472018-11-16 07:19:28 -0800370 0 /* flags */, true /* forceOverride */);
371 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
372 mWindowManager.executeAppTransition();
Jorim Jaggife762342016-10-13 14:33:27 +0200373 }
374 }
375
Issei Suzuki16d4de52019-07-29 13:12:01 +0000376 private boolean isDisplayOccluded(int displayId) {
wilsonshih0299c8a2018-08-24 15:52:57 +0800377 return getDisplay(displayId).mOccluded;
378 }
379
Jorim Jaggife762342016-10-13 14:33:27 +0200380 /**
381 * @return true if Keyguard can be currently dismissed without entering credentials.
382 */
Andrii Kulianfc8f82b2017-01-26 13:17:27 -0800383 boolean canDismissKeyguard() {
Brad Stenning2bdc21e2019-03-11 14:33:22 -0700384 return mWindowManager.isKeyguardTrusted()
385 || !mWindowManager.isKeyguardSecure(mService.getCurrentUserId());
Jorim Jaggife762342016-10-13 14:33:27 +0200386 }
387
388 private int resolveOccludeTransit() {
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800389 final DisplayContent dc =
390 mService.mRootActivityContainer.getDefaultDisplay().mDisplayContent;
Jorim Jaggife762342016-10-13 14:33:27 +0200391 if (mBeforeUnoccludeTransit != TRANSIT_UNSET
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800392 && dc.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
wilsonshih0299c8a2018-08-24 15:52:57 +0800393 // TODO(b/113840485): Handle app transition for individual display.
Issei Suzuki16d4de52019-07-29 13:12:01 +0000394 && isDisplayOccluded(DEFAULT_DISPLAY)) {
Jorim Jaggife762342016-10-13 14:33:27 +0200395
396 // Reuse old transit in case we are occluding Keyguard again, meaning that we never
397 // actually occclude/unocclude Keyguard, but just run a normal transition.
398 return mBeforeUnoccludeTransit;
wilsonshih0299c8a2018-08-24 15:52:57 +0800399 // TODO(b/113840485): Handle app transition for individual display.
Issei Suzuki16d4de52019-07-29 13:12:01 +0000400 } else if (!isDisplayOccluded(DEFAULT_DISPLAY)) {
Jorim Jaggife762342016-10-13 14:33:27 +0200401
402 // Save transit in case we dismiss/occlude Keyguard shortly after.
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800403 mBeforeUnoccludeTransit = dc.mAppTransition.getAppTransition();
Jorim Jaggife762342016-10-13 14:33:27 +0200404 return TRANSIT_KEYGUARD_UNOCCLUDE;
405 } else {
406 return TRANSIT_KEYGUARD_OCCLUDE;
407 }
408 }
409
410 private void dismissDockedStackIfNeeded() {
wilsonshih0299c8a2018-08-24 15:52:57 +0800411 // TODO(b/113840485): Handle docked stack for individual display.
Issei Suzuki16d4de52019-07-29 13:12:01 +0000412 if (mKeyguardShowing && isDisplayOccluded(DEFAULT_DISPLAY)) {
Jorim Jaggife762342016-10-13 14:33:27 +0200413 // The lock screen is currently showing, but is occluded by a window that can
414 // show on top of the lock screen. In this can we want to dismiss the docked
415 // stack since it will be complicated/risky to try to put the activity on top
416 // of the lock screen in the right fullscreen configuration.
Wale Ogunwaled32da472018-11-16 07:19:28 -0800417 final ActivityStack stack =
418 mRootActivityContainer.getDefaultDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700419 if (stack == null) {
420 return;
421 }
422 mStackSupervisor.moveTasksToFullscreenStackLocked(stack,
Andrii Kulian52d255c2018-07-13 11:32:19 -0700423 stack.isFocusedStackOnDisplay());
Jorim Jaggife762342016-10-13 14:33:27 +0200424 }
425 }
Jorim Jaggi8d786932016-10-26 19:08:36 -0700426
David Stevens9440dc82017-03-16 19:00:20 -0700427 private void updateKeyguardSleepToken() {
Wale Ogunwaled32da472018-11-16 07:19:28 -0800428 for (int displayNdx = mRootActivityContainer.getChildCount() - 1;
429 displayNdx >= 0; displayNdx--) {
430 final ActivityDisplay display = mRootActivityContainer.getChildAt(displayNdx);
Issei Suzuki62356a22019-04-11 16:46:37 +0200431 updateKeyguardSleepToken(display.mDisplayId);
432 }
433 }
434
435 private void updateKeyguardSleepToken(int displayId) {
436 final KeyguardDisplayState state = getDisplay(displayId);
Issei Suzuki16d4de52019-07-29 13:12:01 +0000437 if (isKeyguardUnoccludedOrAodShowing(displayId) && state.mSleepToken == null) {
Issei Suzuki62356a22019-04-11 16:46:37 +0200438 state.acquiredSleepToken();
Issei Suzuki16d4de52019-07-29 13:12:01 +0000439 } else if (!isKeyguardUnoccludedOrAodShowing(displayId) && state.mSleepToken != null) {
Issei Suzuki62356a22019-04-11 16:46:37 +0200440 state.releaseSleepToken();
wilsonshih0299c8a2018-08-24 15:52:57 +0800441 }
442 }
443
444 private KeyguardDisplayState getDisplay(int displayId) {
wilsonshihe7903ea2018-09-26 16:17:59 +0800445 KeyguardDisplayState state = mDisplayStates.get(displayId);
446 if (state == null) {
447 state = new KeyguardDisplayState(mService, displayId);
448 mDisplayStates.append(displayId, state);
wilsonshih0299c8a2018-08-24 15:52:57 +0800449 }
wilsonshihe7903ea2018-09-26 16:17:59 +0800450 return state;
wilsonshih0299c8a2018-08-24 15:52:57 +0800451 }
452
453 void onDisplayRemoved(int displayId) {
wilsonshihe7903ea2018-09-26 16:17:59 +0800454 final KeyguardDisplayState state = mDisplayStates.get(displayId);
455 if (state != null) {
456 state.onRemoved();
wilsonshih0299c8a2018-08-24 15:52:57 +0800457 mDisplayStates.remove(displayId);
458 }
459 }
460
461 /** Represents Keyguard state per individual display. */
462 private static class KeyguardDisplayState {
463 private final int mDisplayId;
464 private boolean mOccluded;
465 private ActivityRecord mDismissingKeyguardActivity;
466 private boolean mRequestDismissKeyguard;
467 private final ActivityTaskManagerService mService;
468 private SleepToken mSleepToken;
469
470 KeyguardDisplayState(ActivityTaskManagerService service, int displayId) {
471 mService = service;
472 mDisplayId = displayId;
473 }
474
475 void onRemoved() {
476 mDismissingKeyguardActivity = null;
477 releaseSleepToken();
478 }
479
480 void acquiredSleepToken() {
481 if (mSleepToken == null) {
482 mSleepToken = mService.acquireSleepToken("keyguard", mDisplayId);
483 }
484 }
485
486 void releaseSleepToken() {
487 if (mSleepToken != null) {
488 mSleepToken.release();
489 mSleepToken = null;
490 }
491 }
492
493 void visibilitiesUpdated(KeyguardController controller, ActivityDisplay display) {
494 final boolean lastOccluded = mOccluded;
495 final ActivityRecord lastDismissActivity = mDismissingKeyguardActivity;
496 mRequestDismissKeyguard = false;
497 mOccluded = false;
498 mDismissingKeyguardActivity = null;
499
wilsonshih9a10e9d2019-01-11 14:39:27 +0800500 final ActivityStack stack = getStackForControllingOccluding(display);
501 if (stack != null) {
502 final ActivityRecord topDismissing = stack.getTopDismissingKeyguardActivity();
503 mOccluded = stack.topActivityOccludesKeyguard() || (topDismissing != null
504 && stack.topRunningActivityLocked() == topDismissing
505 && controller.canShowWhileOccluded(
wilsonshih0299c8a2018-08-24 15:52:57 +0800506 true /* dismissKeyguard */,
507 false /* showWhenLocked */));
wilsonshih9a10e9d2019-01-11 14:39:27 +0800508 if (stack.getTopDismissingKeyguardActivity() != null) {
509 mDismissingKeyguardActivity = stack.getTopDismissingKeyguardActivity();
wilsonshih0299c8a2018-08-24 15:52:57 +0800510 }
wilsonshih498a4b82018-12-11 16:10:16 +0800511 // FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD only apply for secondary display.
512 if (mDisplayId != DEFAULT_DISPLAY) {
513 mOccluded |= stack.canShowWithInsecureKeyguard()
514 && controller.canDismissKeyguard();
515 }
wilsonshih0299c8a2018-08-24 15:52:57 +0800516 }
wilsonshih498a4b82018-12-11 16:10:16 +0800517 // TODO(b/123372519): isShowingDream can only works on default display.
518 if (mDisplayId == DEFAULT_DISPLAY) {
519 mOccluded |= controller.mWindowManager.isShowingDream();
520 }
wilsonshih0299c8a2018-08-24 15:52:57 +0800521
Issei Suzuki62356a22019-04-11 16:46:37 +0200522 if (lastOccluded != mOccluded) {
523 controller.handleOccludedChanged(mDisplayId);
wilsonshih0299c8a2018-08-24 15:52:57 +0800524 }
525 if (lastDismissActivity != mDismissingKeyguardActivity && !mOccluded
526 && mDismissingKeyguardActivity != null
Brad Stenning2bdc21e2019-03-11 14:33:22 -0700527 && controller.mWindowManager.isKeyguardSecure(
528 controller.mService.getCurrentUserId())) {
wilsonshih0299c8a2018-08-24 15:52:57 +0800529 mRequestDismissKeyguard = true;
530 }
531 }
532
wilsonshih9a10e9d2019-01-11 14:39:27 +0800533 /**
534 * Gets the stack used to check the occluded state.
535 * <p>
536 * Only the top non-pinned activity of the focusable stack on each display can control its
537 * occlusion state.
538 */
539 private ActivityStack getStackForControllingOccluding(ActivityDisplay display) {
540 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
541 final ActivityStack stack = display.getChildAt(stackNdx);
542 if (stack != null && stack.isFocusableAndVisible()
543 && !stack.inPinnedWindowingMode()) {
544 return stack;
545 }
546 }
547 return null;
548 }
549
wilsonshih0299c8a2018-08-24 15:52:57 +0800550 void dumpStatus(PrintWriter pw, String prefix) {
551 final StringBuilder sb = new StringBuilder();
552 sb.append(prefix);
553 sb.append(" Occluded=").append(mOccluded)
554 .append(" DismissingKeyguardActivity=")
555 .append(mDismissingKeyguardActivity)
556 .append(" at display=")
557 .append(mDisplayId);
558 pw.println(sb.toString());
559 }
560
561 void writeToProto(ProtoOutputStream proto, long fieldId) {
562 final long token = proto.start(fieldId);
563 proto.write(DISPLAY_ID, mDisplayId);
564 proto.write(KEYGUARD_OCCLUDED, mOccluded);
565 proto.end(token);
David Stevens9440dc82017-03-16 19:00:20 -0700566 }
567 }
568
Jorim Jaggi8d786932016-10-26 19:08:36 -0700569 void dump(PrintWriter pw, String prefix) {
570 pw.println(prefix + "KeyguardController:");
571 pw.println(prefix + " mKeyguardShowing=" + mKeyguardShowing);
Lucas Dupin47a65c72018-02-15 14:16:18 -0800572 pw.println(prefix + " mAodShowing=" + mAodShowing);
Jorim Jaggi8d786932016-10-26 19:08:36 -0700573 pw.println(prefix + " mKeyguardGoingAway=" + mKeyguardGoingAway);
wilsonshih0299c8a2018-08-24 15:52:57 +0800574 dumpDisplayStates(pw, prefix);
Jorim Jaggi07961872016-11-23 11:28:57 +0100575 pw.println(prefix + " mDismissalRequested=" + mDismissalRequested);
Jorim Jaggi8d786932016-10-26 19:08:36 -0700576 pw.println(prefix + " mVisibilityTransactionDepth=" + mVisibilityTransactionDepth);
577 }
Steven Timotius4346f0a2017-09-12 11:07:21 -0700578
579 void writeToProto(ProtoOutputStream proto, long fieldId) {
580 final long token = proto.start(fieldId);
Issei Suzukid6eb5a22019-02-20 23:08:03 +0100581 proto.write(AOD_SHOWING, mAodShowing);
Steven Timotius4346f0a2017-09-12 11:07:21 -0700582 proto.write(KEYGUARD_SHOWING, mKeyguardShowing);
wilsonshih0299c8a2018-08-24 15:52:57 +0800583 writeDisplayStatesToProto(proto, KEYGUARD_OCCLUDED_STATES);
Steven Timotius4346f0a2017-09-12 11:07:21 -0700584 proto.end(token);
585 }
wilsonshih0299c8a2018-08-24 15:52:57 +0800586
587 private void dumpDisplayStates(PrintWriter pw, String prefix) {
588 for (int i = 0; i < mDisplayStates.size(); i++) {
589 mDisplayStates.valueAt(i).dumpStatus(pw, prefix);
590 }
591 }
592
593 private void writeDisplayStatesToProto(ProtoOutputStream proto, long fieldId) {
594 for (int i = 0; i < mDisplayStates.size(); i++) {
595 mDisplayStates.valueAt(i).writeToProto(proto, fieldId);
596 }
597 }
Jorim Jaggife762342016-10-13 14:33:27 +0200598}