blob: b5be2ac5df9836f6c118678b0a386e8836dc9e2a [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;
22import static android.view.WindowManager.TRANSIT_FLAG_KEYGUARD_GOING_AWAY_TO_SHADE;
23import static android.view.WindowManager.TRANSIT_FLAG_KEYGUARD_GOING_AWAY_WITH_WALLPAPER;
24import static android.view.WindowManager.TRANSIT_KEYGUARD_GOING_AWAY;
25import static android.view.WindowManager.TRANSIT_KEYGUARD_OCCLUDE;
26import static android.view.WindowManager.TRANSIT_KEYGUARD_UNOCCLUDE;
27import static android.view.WindowManager.TRANSIT_UNSET;
Adrian Roose99bc052017-11-20 17:55:31 +010028import static android.view.WindowManagerPolicyConstants.KEYGUARD_GOING_AWAY_FLAG_NO_WINDOW_ANIMATIONS;
29import static android.view.WindowManagerPolicyConstants.KEYGUARD_GOING_AWAY_FLAG_TO_SHADE;
30import static android.view.WindowManagerPolicyConstants.KEYGUARD_GOING_AWAY_FLAG_WITH_WALLPAPER;
wilsonshih0299c8a2018-08-24 15:52:57 +080031
wilsonshih0299c8a2018-08-24 15:52:57 +080032import static com.android.server.am.KeyguardControllerProto.KEYGUARD_OCCLUDED_STATES;
Yi Jin6c6e9ca2018-03-20 16:53:35 -070033import static com.android.server.am.KeyguardControllerProto.KEYGUARD_SHOWING;
wilsonshih0299c8a2018-08-24 15:52:57 +080034import static com.android.server.am.KeyguardOccludedProto.DISPLAY_ID;
35import static com.android.server.am.KeyguardOccludedProto.KEYGUARD_OCCLUDED;
wilsonshihe7903ea2018-09-26 16:17:59 +080036import static com.android.server.wm.ActivityStackSupervisor.PRESERVE_WINDOWS;
37import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM;
38import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME;
Jorim Jaggife762342016-10-13 14:33:27 +020039
Jorim Jaggi241ae102016-11-02 21:57:33 -070040import android.os.IBinder;
41import android.os.RemoteException;
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -070042import android.os.Trace;
Jorim Jaggi241ae102016-11-02 21:57:33 -070043import android.util.Slog;
wilsonshih0299c8a2018-08-24 15:52:57 +080044import android.util.SparseArray;
Steven Timotius4346f0a2017-09-12 11:07:21 -070045import android.util.proto.ProtoOutputStream;
Adrian Roose99bc052017-11-20 17:55:31 +010046
Jorim Jaggi241ae102016-11-02 21:57:33 -070047import com.android.internal.policy.IKeyguardDismissCallback;
Adrian Roose99bc052017-11-20 17:55:31 +010048import com.android.server.policy.WindowManagerPolicy;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070049import com.android.server.wm.ActivityTaskManagerInternal.SleepToken;
Adrian Roose99bc052017-11-20 17:55:31 +010050
Jorim Jaggi8d786932016-10-26 19:08:36 -070051import java.io.PrintWriter;
wilsonshihe7903ea2018-09-26 16:17:59 +080052import java.util.Arrays;
Jorim Jaggife762342016-10-13 14:33:27 +020053
54/**
55 * Controls Keyguard occluding, dismissing and transitions depending on what kind of activities are
56 * currently visible.
57 * <p>
58 * Note that everything in this class should only be accessed with the AM lock being held.
59 */
60class KeyguardController {
61
Wale Ogunwale98875612018-10-12 07:53:02 -070062 private static final String TAG = TAG_WITH_CLASS_NAME ? "KeyguardController" : TAG_ATM;
Jorim Jaggi241ae102016-11-02 21:57:33 -070063
Jorim Jaggife762342016-10-13 14:33:27 +020064 private final ActivityStackSupervisor mStackSupervisor;
65 private WindowManagerService mWindowManager;
Jorim Jaggife762342016-10-13 14:33:27 +020066 private boolean mKeyguardShowing;
Lucas Dupin47a65c72018-02-15 14:16:18 -080067 private boolean mAodShowing;
Jorim Jaggi8d786932016-10-26 19:08:36 -070068 private boolean mKeyguardGoingAway;
Jorim Jaggi07961872016-11-23 11:28:57 +010069 private boolean mDismissalRequested;
wilsonshihe7903ea2018-09-26 16:17:59 +080070 private int[] mSecondaryDisplayIdsShowing;
Jorim Jaggife762342016-10-13 14:33:27 +020071 private int mBeforeUnoccludeTransit;
72 private int mVisibilityTransactionDepth;
wilsonshih0299c8a2018-08-24 15:52:57 +080073 private final SparseArray<KeyguardDisplayState> mDisplayStates = new SparseArray<>();
74 private final ActivityTaskManagerService mService;
Wale Ogunwaled32da472018-11-16 07:19:28 -080075 private RootActivityContainer mRootActivityContainer;
Jorim Jaggife762342016-10-13 14:33:27 +020076
Wale Ogunwalef6733932018-06-27 05:14:34 -070077 KeyguardController(ActivityTaskManagerService service,
Jorim Jaggife762342016-10-13 14:33:27 +020078 ActivityStackSupervisor stackSupervisor) {
79 mService = service;
80 mStackSupervisor = stackSupervisor;
81 }
82
83 void setWindowManager(WindowManagerService windowManager) {
84 mWindowManager = windowManager;
Wale Ogunwaled32da472018-11-16 07:19:28 -080085 mRootActivityContainer = mService.mRootActivityContainer;
Jorim Jaggife762342016-10-13 14:33:27 +020086 }
87
88 /**
Lucas Dupin47a65c72018-02-15 14:16:18 -080089 * @return true if either Keyguard or AOD are showing, not going away, and not being occluded
90 * on the given display, false otherwise
91 */
92 boolean isKeyguardOrAodShowing(int displayId) {
wilsonshih0299c8a2018-08-24 15:52:57 +080093 return (mKeyguardShowing || mAodShowing) && !mKeyguardGoingAway
wilsonshih498a4b82018-12-11 16:10:16 +080094 && !isDisplayOccluded(displayId);
Lucas Dupin47a65c72018-02-15 14:16:18 -080095 }
96
97 /**
David Stevens53a39ea2017-08-23 18:41:49 -070098 * @return true if Keyguard is showing, not going away, and not being occluded on the given
99 * display, false otherwise
Jorim Jaggife762342016-10-13 14:33:27 +0200100 */
David Stevens53a39ea2017-08-23 18:41:49 -0700101 boolean isKeyguardShowing(int displayId) {
wilsonshih498a4b82018-12-11 16:10:16 +0800102 return mKeyguardShowing && !mKeyguardGoingAway && !isDisplayOccluded(displayId);
Jorim Jaggife762342016-10-13 14:33:27 +0200103 }
104
105 /**
106 * @return true if Keyguard is either showing or occluded, but not going away
107 */
108 boolean isKeyguardLocked() {
109 return mKeyguardShowing && !mKeyguardGoingAway;
110 }
111
112 /**
Bryce Lee271617a2018-03-15 10:39:12 -0700113 * @return {@code true} if the keyguard is going away, {@code false} otherwise.
114 */
115 boolean isKeyguardGoingAway() {
116 // Also check keyguard showing in case value is stale.
117 return mKeyguardGoingAway && mKeyguardShowing;
118 }
119
120 /**
Jorim Jaggife762342016-10-13 14:33:27 +0200121 * Update the Keyguard showing state.
122 */
Lucas Dupin47a65c72018-02-15 14:16:18 -0800123 void setKeyguardShown(boolean keyguardShowing, boolean aodShowing,
wilsonshihe7903ea2018-09-26 16:17:59 +0800124 int[] secondaryDisplaysShowing) {
Lucas Dupin47a65c72018-02-15 14:16:18 -0800125 boolean showingChanged = keyguardShowing != mKeyguardShowing || aodShowing != mAodShowing;
Adrian Roos6ec76b72018-04-25 14:01:11 +0200126 // If keyguard is going away, but SystemUI aborted the transition, need to reset state.
127 showingChanged |= mKeyguardGoingAway && keyguardShowing;
wilsonshihe7903ea2018-09-26 16:17:59 +0800128 if (!showingChanged && Arrays.equals(secondaryDisplaysShowing,
129 mSecondaryDisplayIdsShowing)) {
Jorim Jaggife762342016-10-13 14:33:27 +0200130 return;
131 }
Lucas Dupin47a65c72018-02-15 14:16:18 -0800132 mKeyguardShowing = keyguardShowing;
133 mAodShowing = aodShowing;
wilsonshihe7903ea2018-09-26 16:17:59 +0800134 mSecondaryDisplayIdsShowing = secondaryDisplaysShowing;
chaviw0e9fb132018-06-05 16:29:13 -0700135 mWindowManager.setAodShowing(aodShowing);
David Stevens53a39ea2017-08-23 18:41:49 -0700136 if (showingChanged) {
137 dismissDockedStackIfNeeded();
Andrii Kulian0d595f32018-02-21 15:47:33 -0800138 setKeyguardGoingAway(false);
wilsonshih0299c8a2018-08-24 15:52:57 +0800139 // TODO(b/113840485): Check usage for non-default display
Adrian Roosb56b3c32018-05-17 17:32:26 +0200140 mWindowManager.setKeyguardOrAodShowingOnDefaultDisplay(
141 isKeyguardOrAodShowing(DEFAULT_DISPLAY));
Lucas Dupin47a65c72018-02-15 14:16:18 -0800142 if (keyguardShowing) {
David Stevens53a39ea2017-08-23 18:41:49 -0700143 mDismissalRequested = false;
144 }
Jorim Jaggife762342016-10-13 14:33:27 +0200145 }
Wale Ogunwaled32da472018-11-16 07:19:28 -0800146 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
David Stevens9440dc82017-03-16 19:00:20 -0700147 updateKeyguardSleepToken();
Jorim Jaggife762342016-10-13 14:33:27 +0200148 }
149
150 /**
151 * Called when Keyguard is going away.
152 *
Adrian Roose99bc052017-11-20 17:55:31 +0100153 * @param flags See {@link WindowManagerPolicy#KEYGUARD_GOING_AWAY_FLAG_TO_SHADE}
Jorim Jaggife762342016-10-13 14:33:27 +0200154 * etc.
155 */
156 void keyguardGoingAway(int flags) {
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -0700157 if (!mKeyguardShowing) {
158 return;
159 }
160 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "keyguardGoingAway");
161 mWindowManager.deferSurfaceLayout();
162 try {
163 setKeyguardGoingAway(true);
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800164 mRootActivityContainer.getDefaultDisplay().mDisplayContent
lumark588a3e82018-07-20 18:53:54 +0800165 .prepareAppTransition(TRANSIT_KEYGUARD_GOING_AWAY,
166 false /* alwaysKeepCurrent */, convertTransitFlags(flags),
167 false /* forceOverride */);
David Stevens9440dc82017-03-16 19:00:20 -0700168 updateKeyguardSleepToken();
Jorim Jaggife762342016-10-13 14:33:27 +0200169
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -0700170 // Some stack visibility might change (e.g. docked stack)
Wale Ogunwaled32da472018-11-16 07:19:28 -0800171 mRootActivityContainer.resumeFocusedStacksTopActivities();
172 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
173 mRootActivityContainer.addStartingWindowsForVisibleActivities(
174 true /* taskSwitch */);
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -0700175 mWindowManager.executeAppTransition();
176 } finally {
177 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "keyguardGoingAway: surfaceLayout");
178 mWindowManager.continueSurfaceLayout();
179 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
180
181 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Jorim Jaggife762342016-10-13 14:33:27 +0200182 }
183 }
184
Lucas Dupinc80c67e2017-12-04 14:29:10 -0800185 void dismissKeyguard(IBinder token, IKeyguardDismissCallback callback, CharSequence message) {
Jorim Jaggi241ae102016-11-02 21:57:33 -0700186 final ActivityRecord activityRecord = ActivityRecord.forTokenLocked(token);
187 if (activityRecord == null || !activityRecord.visibleIgnoringKeyguard) {
188 failCallback(callback);
189 return;
190 }
Jorim Jaggid7214892017-07-18 14:05:19 +0200191 Slog.i(TAG, "Activity requesting to dismiss Keyguard: " + activityRecord);
chaviw59b98852017-06-13 12:05:44 -0700192
193 // If the client has requested to dismiss the keyguard and the Activity has the flag to
194 // turn the screen on, wakeup the screen if it's the top Activity.
195 if (activityRecord.getTurnScreenOnFlag() && activityRecord.isTopRunningActivity()) {
196 mStackSupervisor.wakeUp("dismissKeyguard");
197 }
198
Lucas Dupinc80c67e2017-12-04 14:29:10 -0800199 mWindowManager.dismissKeyguard(callback, message);
Jorim Jaggi241ae102016-11-02 21:57:33 -0700200 }
201
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -0700202 private void setKeyguardGoingAway(boolean keyguardGoingAway) {
203 mKeyguardGoingAway = keyguardGoingAway;
204 mWindowManager.setKeyguardGoingAway(keyguardGoingAway);
205 }
206
Jorim Jaggi241ae102016-11-02 21:57:33 -0700207 private void failCallback(IKeyguardDismissCallback callback) {
208 try {
209 callback.onDismissError();
210 } catch (RemoteException e) {
211 Slog.w(TAG, "Failed to call callback", e);
212 }
213 }
214
Jorim Jaggife762342016-10-13 14:33:27 +0200215 private int convertTransitFlags(int keyguardGoingAwayFlags) {
216 int result = 0;
217 if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_TO_SHADE) != 0) {
218 result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_TO_SHADE;
219 }
220 if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_NO_WINDOW_ANIMATIONS) != 0) {
221 result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_NO_ANIMATION;
222 }
223 if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_WITH_WALLPAPER) != 0) {
224 result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_WITH_WALLPAPER;
225 }
226 return result;
227 }
228
229 /**
230 * Starts a batch of visibility updates.
231 */
232 void beginActivityVisibilityUpdate() {
233 mVisibilityTransactionDepth++;
234 }
235
236 /**
237 * Ends a batch of visibility updates. After all batches are done, this method makes sure to
238 * update lockscreen occluded/dismiss state if needed.
239 */
240 void endActivityVisibilityUpdate() {
241 mVisibilityTransactionDepth--;
242 if (mVisibilityTransactionDepth == 0) {
243 visibilitiesUpdated();
244 }
245 }
246
Jorim Jaggie69c9312016-10-31 18:24:38 -0700247 /**
248 * @return True if we may show an activity while Keyguard is showing because we are in the
249 * process of dismissing it anyways, false otherwise.
250 */
Jorim Jaggi07961872016-11-23 11:28:57 +0100251 boolean canShowActivityWhileKeyguardShowing(ActivityRecord r, boolean dismissKeyguard) {
252
253 // Allow to show it when we are about to dismiss Keyguard. This isn't allowed if r is
254 // already the dismissing activity, in which case we don't allow it to repeatedly dismiss
255 // Keyguard.
Lucas Dupin47a65c72018-02-15 14:16:18 -0800256 return dismissKeyguard && canDismissKeyguard() && !mAodShowing
wilsonshih0299c8a2018-08-24 15:52:57 +0800257 && (mDismissalRequested
258 || getDisplay(r.getDisplayId()).mDismissingKeyguardActivity != r);
Jorim Jaggi07961872016-11-23 11:28:57 +0100259 }
260
261 /**
262 * @return True if we may show an activity while Keyguard is occluded, false otherwise.
263 */
264 boolean canShowWhileOccluded(boolean dismissKeyguard, boolean showWhenLocked) {
265 return showWhenLocked || dismissKeyguard && !mWindowManager.isKeyguardSecure();
Jorim Jaggie69c9312016-10-31 18:24:38 -0700266 }
267
Jorim Jaggife762342016-10-13 14:33:27 +0200268 private void visibilitiesUpdated() {
wilsonshih0299c8a2018-08-24 15:52:57 +0800269 boolean requestDismissKeyguard = false;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800270 for (int displayNdx = mRootActivityContainer.getChildCount() - 1;
271 displayNdx >= 0; displayNdx--) {
272 final ActivityDisplay display = mRootActivityContainer.getChildAt(displayNdx);
wilsonshih0299c8a2018-08-24 15:52:57 +0800273 final KeyguardDisplayState state = getDisplay(display.mDisplayId);
274 state.visibilitiesUpdated(this, display);
275 requestDismissKeyguard |= state.mRequestDismissKeyguard;
Jorim Jaggife762342016-10-13 14:33:27 +0200276 }
wilsonshih0299c8a2018-08-24 15:52:57 +0800277
278 // Dismissing Keyguard happens globally using the information from all displays.
279 if (requestDismissKeyguard) {
Jorim Jaggife762342016-10-13 14:33:27 +0200280 handleDismissKeyguard();
281 }
282 }
283
284 /**
285 * Called when occluded state changed.
286 */
287 private void handleOccludedChanged() {
wilsonshih0299c8a2018-08-24 15:52:57 +0800288 mWindowManager.onKeyguardOccludedChanged(isDisplayOccluded(DEFAULT_DISPLAY));
Jorim Jaggife762342016-10-13 14:33:27 +0200289 if (isKeyguardLocked()) {
290 mWindowManager.deferSurfaceLayout();
291 try {
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800292 mRootActivityContainer.getDefaultDisplay().mDisplayContent
lumark588a3e82018-07-20 18:53:54 +0800293 .prepareAppTransition(resolveOccludeTransit(),
294 false /* alwaysKeepCurrent */, 0 /* flags */,
295 true /* forceOverride */);
David Stevens9440dc82017-03-16 19:00:20 -0700296 updateKeyguardSleepToken();
Wale Ogunwaled32da472018-11-16 07:19:28 -0800297 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
Jorim Jaggife762342016-10-13 14:33:27 +0200298 mWindowManager.executeAppTransition();
299 } finally {
300 mWindowManager.continueSurfaceLayout();
301 }
302 }
303 dismissDockedStackIfNeeded();
304 }
305
306 /**
wilsonshih0299c8a2018-08-24 15:52:57 +0800307 * Called when somebody wants to dismiss the Keyguard via the flag.
Jorim Jaggife762342016-10-13 14:33:27 +0200308 */
309 private void handleDismissKeyguard() {
Jorim Jaggi07961872016-11-23 11:28:57 +0100310 // We only allow dismissing Keyguard via the flag when Keyguard is secure for legacy
311 // reasons, because that's how apps used to dismiss Keyguard in the secure case. In the
312 // insecure case, we actually show it on top of the lockscreen. See #canShowWhileOccluded.
Wale Ogunwaled32da472018-11-16 07:19:28 -0800313 if (!mWindowManager.isKeyguardSecure()) {
314 return;
315 }
Jorim Jaggife762342016-10-13 14:33:27 +0200316
Wale Ogunwaled32da472018-11-16 07:19:28 -0800317 mWindowManager.dismissKeyguard(null /* callback */, null /* message */);
318 mDismissalRequested = true;
319
320 // If we are about to unocclude the Keyguard, but we can dismiss it without security,
321 // we immediately dismiss the Keyguard so the activity gets shown without a flicker.
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800322 final DisplayContent dc =
323 mRootActivityContainer.getDefaultDisplay().mDisplayContent;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800324 if (mKeyguardShowing && canDismissKeyguard()
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800325 && dc.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
326 dc.prepareAppTransition(mBeforeUnoccludeTransit, false /* alwaysKeepCurrent */,
Wale Ogunwaled32da472018-11-16 07:19:28 -0800327 0 /* flags */, true /* forceOverride */);
328 mRootActivityContainer.ensureActivitiesVisible(null, 0, !PRESERVE_WINDOWS);
329 mWindowManager.executeAppTransition();
Jorim Jaggife762342016-10-13 14:33:27 +0200330 }
331 }
332
wilsonshih0299c8a2018-08-24 15:52:57 +0800333 private boolean isDisplayOccluded(int displayId) {
334 return getDisplay(displayId).mOccluded;
335 }
336
Jorim Jaggife762342016-10-13 14:33:27 +0200337 /**
338 * @return true if Keyguard can be currently dismissed without entering credentials.
339 */
Andrii Kulianfc8f82b2017-01-26 13:17:27 -0800340 boolean canDismissKeyguard() {
Jorim Jaggife762342016-10-13 14:33:27 +0200341 return mWindowManager.isKeyguardTrusted() || !mWindowManager.isKeyguardSecure();
342 }
343
344 private int resolveOccludeTransit() {
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800345 final DisplayContent dc =
346 mService.mRootActivityContainer.getDefaultDisplay().mDisplayContent;
Jorim Jaggife762342016-10-13 14:33:27 +0200347 if (mBeforeUnoccludeTransit != TRANSIT_UNSET
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800348 && dc.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
wilsonshih0299c8a2018-08-24 15:52:57 +0800349 // TODO(b/113840485): Handle app transition for individual display.
350 && isDisplayOccluded(DEFAULT_DISPLAY)) {
Jorim Jaggife762342016-10-13 14:33:27 +0200351
352 // Reuse old transit in case we are occluding Keyguard again, meaning that we never
353 // actually occclude/unocclude Keyguard, but just run a normal transition.
354 return mBeforeUnoccludeTransit;
wilsonshih0299c8a2018-08-24 15:52:57 +0800355 // TODO(b/113840485): Handle app transition for individual display.
356 } else if (!isDisplayOccluded(DEFAULT_DISPLAY)) {
Jorim Jaggife762342016-10-13 14:33:27 +0200357
358 // Save transit in case we dismiss/occlude Keyguard shortly after.
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800359 mBeforeUnoccludeTransit = dc.mAppTransition.getAppTransition();
Jorim Jaggife762342016-10-13 14:33:27 +0200360 return TRANSIT_KEYGUARD_UNOCCLUDE;
361 } else {
362 return TRANSIT_KEYGUARD_OCCLUDE;
363 }
364 }
365
366 private void dismissDockedStackIfNeeded() {
wilsonshih0299c8a2018-08-24 15:52:57 +0800367 // TODO(b/113840485): Handle docked stack for individual display.
368 if (mKeyguardShowing && isDisplayOccluded(DEFAULT_DISPLAY)) {
Jorim Jaggife762342016-10-13 14:33:27 +0200369 // The lock screen is currently showing, but is occluded by a window that can
370 // show on top of the lock screen. In this can we want to dismiss the docked
371 // stack since it will be complicated/risky to try to put the activity on top
372 // of the lock screen in the right fullscreen configuration.
Wale Ogunwaled32da472018-11-16 07:19:28 -0800373 final ActivityStack stack =
374 mRootActivityContainer.getDefaultDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700375 if (stack == null) {
376 return;
377 }
378 mStackSupervisor.moveTasksToFullscreenStackLocked(stack,
Andrii Kulian52d255c2018-07-13 11:32:19 -0700379 stack.isFocusedStackOnDisplay());
Jorim Jaggife762342016-10-13 14:33:27 +0200380 }
381 }
Jorim Jaggi8d786932016-10-26 19:08:36 -0700382
David Stevens9440dc82017-03-16 19:00:20 -0700383 private void updateKeyguardSleepToken() {
Wale Ogunwaled32da472018-11-16 07:19:28 -0800384 for (int displayNdx = mRootActivityContainer.getChildCount() - 1;
385 displayNdx >= 0; displayNdx--) {
386 final ActivityDisplay display = mRootActivityContainer.getChildAt(displayNdx);
wilsonshih0299c8a2018-08-24 15:52:57 +0800387 final KeyguardDisplayState state = getDisplay(display.mDisplayId);
388 if (isKeyguardOrAodShowing(display.mDisplayId) && state.mSleepToken == null) {
389 state.acquiredSleepToken();
390 } else if (!isKeyguardOrAodShowing(display.mDisplayId) && state.mSleepToken != null) {
391 state.releaseSleepToken();
392 }
393 }
394 }
395
396 private KeyguardDisplayState getDisplay(int displayId) {
wilsonshihe7903ea2018-09-26 16:17:59 +0800397 KeyguardDisplayState state = mDisplayStates.get(displayId);
398 if (state == null) {
399 state = new KeyguardDisplayState(mService, displayId);
400 mDisplayStates.append(displayId, state);
wilsonshih0299c8a2018-08-24 15:52:57 +0800401 }
wilsonshihe7903ea2018-09-26 16:17:59 +0800402 return state;
wilsonshih0299c8a2018-08-24 15:52:57 +0800403 }
404
405 void onDisplayRemoved(int displayId) {
wilsonshihe7903ea2018-09-26 16:17:59 +0800406 final KeyguardDisplayState state = mDisplayStates.get(displayId);
407 if (state != null) {
408 state.onRemoved();
wilsonshih0299c8a2018-08-24 15:52:57 +0800409 mDisplayStates.remove(displayId);
410 }
411 }
412
413 /** Represents Keyguard state per individual display. */
414 private static class KeyguardDisplayState {
415 private final int mDisplayId;
416 private boolean mOccluded;
417 private ActivityRecord mDismissingKeyguardActivity;
418 private boolean mRequestDismissKeyguard;
419 private final ActivityTaskManagerService mService;
420 private SleepToken mSleepToken;
421
422 KeyguardDisplayState(ActivityTaskManagerService service, int displayId) {
423 mService = service;
424 mDisplayId = displayId;
425 }
426
427 void onRemoved() {
428 mDismissingKeyguardActivity = null;
429 releaseSleepToken();
430 }
431
432 void acquiredSleepToken() {
433 if (mSleepToken == null) {
434 mSleepToken = mService.acquireSleepToken("keyguard", mDisplayId);
435 }
436 }
437
438 void releaseSleepToken() {
439 if (mSleepToken != null) {
440 mSleepToken.release();
441 mSleepToken = null;
442 }
443 }
444
445 void visibilitiesUpdated(KeyguardController controller, ActivityDisplay display) {
446 final boolean lastOccluded = mOccluded;
447 final ActivityRecord lastDismissActivity = mDismissingKeyguardActivity;
448 mRequestDismissKeyguard = false;
449 mOccluded = false;
450 mDismissingKeyguardActivity = null;
451
wilsonshih9a10e9d2019-01-11 14:39:27 +0800452 final ActivityStack stack = getStackForControllingOccluding(display);
453 if (stack != null) {
454 final ActivityRecord topDismissing = stack.getTopDismissingKeyguardActivity();
455 mOccluded = stack.topActivityOccludesKeyguard() || (topDismissing != null
456 && stack.topRunningActivityLocked() == topDismissing
457 && controller.canShowWhileOccluded(
wilsonshih0299c8a2018-08-24 15:52:57 +0800458 true /* dismissKeyguard */,
459 false /* showWhenLocked */));
wilsonshih9a10e9d2019-01-11 14:39:27 +0800460 if (stack.getTopDismissingKeyguardActivity() != null) {
461 mDismissingKeyguardActivity = stack.getTopDismissingKeyguardActivity();
wilsonshih0299c8a2018-08-24 15:52:57 +0800462 }
wilsonshih498a4b82018-12-11 16:10:16 +0800463 // FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD only apply for secondary display.
464 if (mDisplayId != DEFAULT_DISPLAY) {
465 mOccluded |= stack.canShowWithInsecureKeyguard()
466 && controller.canDismissKeyguard();
467 }
wilsonshih0299c8a2018-08-24 15:52:57 +0800468 }
wilsonshih498a4b82018-12-11 16:10:16 +0800469 // TODO(b/123372519): isShowingDream can only works on default display.
470 if (mDisplayId == DEFAULT_DISPLAY) {
471 mOccluded |= controller.mWindowManager.isShowingDream();
472 }
wilsonshih0299c8a2018-08-24 15:52:57 +0800473
wilsonshihe7903ea2018-09-26 16:17:59 +0800474 // TODO(b/113840485): Handle app transition for individual display, and apply occluded
475 // state change to secondary displays.
wilsonshih0299c8a2018-08-24 15:52:57 +0800476 // For now, only default display can change occluded.
477 if (lastOccluded != mOccluded && mDisplayId == DEFAULT_DISPLAY) {
478 controller.handleOccludedChanged();
479 }
480 if (lastDismissActivity != mDismissingKeyguardActivity && !mOccluded
481 && mDismissingKeyguardActivity != null
482 && controller.mWindowManager.isKeyguardSecure()) {
483 mRequestDismissKeyguard = true;
484 }
485 }
486
wilsonshih9a10e9d2019-01-11 14:39:27 +0800487 /**
488 * Gets the stack used to check the occluded state.
489 * <p>
490 * Only the top non-pinned activity of the focusable stack on each display can control its
491 * occlusion state.
492 */
493 private ActivityStack getStackForControllingOccluding(ActivityDisplay display) {
494 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
495 final ActivityStack stack = display.getChildAt(stackNdx);
496 if (stack != null && stack.isFocusableAndVisible()
497 && !stack.inPinnedWindowingMode()) {
498 return stack;
499 }
500 }
501 return null;
502 }
503
wilsonshih0299c8a2018-08-24 15:52:57 +0800504 void dumpStatus(PrintWriter pw, String prefix) {
505 final StringBuilder sb = new StringBuilder();
506 sb.append(prefix);
507 sb.append(" Occluded=").append(mOccluded)
508 .append(" DismissingKeyguardActivity=")
509 .append(mDismissingKeyguardActivity)
510 .append(" at display=")
511 .append(mDisplayId);
512 pw.println(sb.toString());
513 }
514
515 void writeToProto(ProtoOutputStream proto, long fieldId) {
516 final long token = proto.start(fieldId);
517 proto.write(DISPLAY_ID, mDisplayId);
518 proto.write(KEYGUARD_OCCLUDED, mOccluded);
519 proto.end(token);
David Stevens9440dc82017-03-16 19:00:20 -0700520 }
521 }
522
Jorim Jaggi8d786932016-10-26 19:08:36 -0700523 void dump(PrintWriter pw, String prefix) {
524 pw.println(prefix + "KeyguardController:");
525 pw.println(prefix + " mKeyguardShowing=" + mKeyguardShowing);
Lucas Dupin47a65c72018-02-15 14:16:18 -0800526 pw.println(prefix + " mAodShowing=" + mAodShowing);
Jorim Jaggi8d786932016-10-26 19:08:36 -0700527 pw.println(prefix + " mKeyguardGoingAway=" + mKeyguardGoingAway);
wilsonshih0299c8a2018-08-24 15:52:57 +0800528 dumpDisplayStates(pw, prefix);
Jorim Jaggi07961872016-11-23 11:28:57 +0100529 pw.println(prefix + " mDismissalRequested=" + mDismissalRequested);
Jorim Jaggi8d786932016-10-26 19:08:36 -0700530 pw.println(prefix + " mVisibilityTransactionDepth=" + mVisibilityTransactionDepth);
531 }
Steven Timotius4346f0a2017-09-12 11:07:21 -0700532
533 void writeToProto(ProtoOutputStream proto, long fieldId) {
534 final long token = proto.start(fieldId);
535 proto.write(KEYGUARD_SHOWING, mKeyguardShowing);
wilsonshih0299c8a2018-08-24 15:52:57 +0800536 writeDisplayStatesToProto(proto, KEYGUARD_OCCLUDED_STATES);
Steven Timotius4346f0a2017-09-12 11:07:21 -0700537 proto.end(token);
538 }
wilsonshih0299c8a2018-08-24 15:52:57 +0800539
540 private void dumpDisplayStates(PrintWriter pw, String prefix) {
541 for (int i = 0; i < mDisplayStates.size(); i++) {
542 mDisplayStates.valueAt(i).dumpStatus(pw, prefix);
543 }
544 }
545
546 private void writeDisplayStatesToProto(ProtoOutputStream proto, long fieldId) {
547 for (int i = 0; i < mDisplayStates.size(); i++) {
548 mDisplayStates.valueAt(i).writeToProto(proto, fieldId);
549 }
550 }
Jorim Jaggife762342016-10-13 14:33:27 +0200551}