blob: 0d7eab626ff00a847cb9fa02fbf9638a44d34b2a [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
17package com.android.server.am;
18
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;
David Stevens53a39ea2017-08-23 18:41:49 -070021import static android.view.Display.INVALID_DISPLAY;
Adrian Roose99bc052017-11-20 17:55:31 +010022import static android.view.WindowManagerPolicyConstants.KEYGUARD_GOING_AWAY_FLAG_NO_WINDOW_ANIMATIONS;
23import static android.view.WindowManagerPolicyConstants.KEYGUARD_GOING_AWAY_FLAG_TO_SHADE;
24import static android.view.WindowManagerPolicyConstants.KEYGUARD_GOING_AWAY_FLAG_WITH_WALLPAPER;
Jorim Jaggi241ae102016-11-02 21:57:33 -070025import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
26import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Jorim Jaggife762342016-10-13 14:33:27 +020027import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
Steven Timotius4346f0a2017-09-12 11:07:21 -070028import static com.android.server.am.proto.KeyguardControllerProto.KEYGUARD_OCCLUDED;
29import static com.android.server.am.proto.KeyguardControllerProto.KEYGUARD_SHOWING;
Jorim Jaggif84e2f62018-01-16 14:17:59 +010030import static android.view.WindowManager.TRANSIT_FLAG_KEYGUARD_GOING_AWAY_NO_ANIMATION;
31import static android.view.WindowManager.TRANSIT_FLAG_KEYGUARD_GOING_AWAY_TO_SHADE;
32import static android.view.WindowManager.TRANSIT_FLAG_KEYGUARD_GOING_AWAY_WITH_WALLPAPER;
33import static android.view.WindowManager.TRANSIT_KEYGUARD_GOING_AWAY;
34import static android.view.WindowManager.TRANSIT_KEYGUARD_OCCLUDE;
35import static android.view.WindowManager.TRANSIT_KEYGUARD_UNOCCLUDE;
36import static android.view.WindowManager.TRANSIT_UNSET;
Jorim Jaggife762342016-10-13 14:33:27 +020037
David Stevens9440dc82017-03-16 19:00:20 -070038import android.app.ActivityManagerInternal.SleepToken;
Jorim Jaggi241ae102016-11-02 21:57:33 -070039import android.os.IBinder;
40import android.os.RemoteException;
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -070041import android.os.Trace;
Jorim Jaggi241ae102016-11-02 21:57:33 -070042import android.util.Slog;
Steven Timotius4346f0a2017-09-12 11:07:21 -070043import android.util.proto.ProtoOutputStream;
Adrian Roose99bc052017-11-20 17:55:31 +010044
Jorim Jaggi241ae102016-11-02 21:57:33 -070045import com.android.internal.policy.IKeyguardDismissCallback;
Adrian Roose99bc052017-11-20 17:55:31 +010046import com.android.server.policy.WindowManagerPolicy;
Jorim Jaggife762342016-10-13 14:33:27 +020047import com.android.server.wm.WindowManagerService;
Adrian Roose99bc052017-11-20 17:55:31 +010048
Jorim Jaggi8d786932016-10-26 19:08:36 -070049import java.io.PrintWriter;
Jorim Jaggife762342016-10-13 14:33:27 +020050
51/**
52 * Controls Keyguard occluding, dismissing and transitions depending on what kind of activities are
53 * currently visible.
54 * <p>
55 * Note that everything in this class should only be accessed with the AM lock being held.
56 */
57class KeyguardController {
58
Jorim Jaggi241ae102016-11-02 21:57:33 -070059 private static final String TAG = TAG_WITH_CLASS_NAME ? "KeyguardController" : TAG_AM;
60
Jorim Jaggife762342016-10-13 14:33:27 +020061 private final ActivityManagerService mService;
62 private final ActivityStackSupervisor mStackSupervisor;
63 private WindowManagerService mWindowManager;
Jorim Jaggife762342016-10-13 14:33:27 +020064 private boolean mKeyguardShowing;
Jorim Jaggi8d786932016-10-26 19:08:36 -070065 private boolean mKeyguardGoingAway;
Jorim Jaggife762342016-10-13 14:33:27 +020066 private boolean mOccluded;
Jorim Jaggi07961872016-11-23 11:28:57 +010067 private boolean mDismissalRequested;
Jorim Jaggife762342016-10-13 14:33:27 +020068 private ActivityRecord mDismissingKeyguardActivity;
69 private int mBeforeUnoccludeTransit;
70 private int mVisibilityTransactionDepth;
David Stevens9440dc82017-03-16 19:00:20 -070071 private SleepToken mSleepToken;
David Stevens53a39ea2017-08-23 18:41:49 -070072 private int mSecondaryDisplayShowing = INVALID_DISPLAY;
Jorim Jaggife762342016-10-13 14:33:27 +020073
74 KeyguardController(ActivityManagerService service,
75 ActivityStackSupervisor stackSupervisor) {
76 mService = service;
77 mStackSupervisor = stackSupervisor;
78 }
79
80 void setWindowManager(WindowManagerService windowManager) {
81 mWindowManager = windowManager;
82 }
83
84 /**
David Stevens53a39ea2017-08-23 18:41:49 -070085 * @return true if Keyguard is showing, not going away, and not being occluded on the given
86 * display, false otherwise
Jorim Jaggife762342016-10-13 14:33:27 +020087 */
David Stevens53a39ea2017-08-23 18:41:49 -070088 boolean isKeyguardShowing(int displayId) {
Bryce Leed939cf02018-03-12 09:04:44 -070089 return mKeyguardShowing && !mKeyguardGoingAway &&
90 (displayId == DEFAULT_DISPLAY ? !mOccluded : displayId == mSecondaryDisplayShowing);
Jorim Jaggife762342016-10-13 14:33:27 +020091 }
92
93 /**
94 * @return true if Keyguard is either showing or occluded, but not going away
95 */
96 boolean isKeyguardLocked() {
97 return mKeyguardShowing && !mKeyguardGoingAway;
98 }
99
100 /**
Bryce Lee271617a2018-03-15 10:39:12 -0700101 * @return {@code true} if the keyguard is going away, {@code false} otherwise.
102 */
103 boolean isKeyguardGoingAway() {
104 // Also check keyguard showing in case value is stale.
105 return mKeyguardGoingAway && mKeyguardShowing;
106 }
107
108 /**
Jorim Jaggife762342016-10-13 14:33:27 +0200109 * Update the Keyguard showing state.
110 */
David Stevens53a39ea2017-08-23 18:41:49 -0700111 void setKeyguardShown(boolean showing, int secondaryDisplayShowing) {
112 boolean showingChanged = showing != mKeyguardShowing;
113 if (!showingChanged && secondaryDisplayShowing == mSecondaryDisplayShowing) {
Jorim Jaggife762342016-10-13 14:33:27 +0200114 return;
115 }
116 mKeyguardShowing = showing;
David Stevens53a39ea2017-08-23 18:41:49 -0700117 mSecondaryDisplayShowing = secondaryDisplayShowing;
118 if (showingChanged) {
119 dismissDockedStackIfNeeded();
Andrii Kulian0d595f32018-02-21 15:47:33 -0800120 setKeyguardGoingAway(false);
David Stevens53a39ea2017-08-23 18:41:49 -0700121 if (showing) {
David Stevens53a39ea2017-08-23 18:41:49 -0700122 mDismissalRequested = false;
123 }
Jorim Jaggife762342016-10-13 14:33:27 +0200124 }
125 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
David Stevens9440dc82017-03-16 19:00:20 -0700126 updateKeyguardSleepToken();
Jorim Jaggife762342016-10-13 14:33:27 +0200127 }
128
129 /**
130 * Called when Keyguard is going away.
131 *
Adrian Roose99bc052017-11-20 17:55:31 +0100132 * @param flags See {@link WindowManagerPolicy#KEYGUARD_GOING_AWAY_FLAG_TO_SHADE}
Jorim Jaggife762342016-10-13 14:33:27 +0200133 * etc.
134 */
135 void keyguardGoingAway(int flags) {
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -0700136 if (!mKeyguardShowing) {
137 return;
138 }
139 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "keyguardGoingAway");
140 mWindowManager.deferSurfaceLayout();
141 try {
142 setKeyguardGoingAway(true);
143 mWindowManager.prepareAppTransition(TRANSIT_KEYGUARD_GOING_AWAY,
144 false /* alwaysKeepCurrent */, convertTransitFlags(flags),
145 false /* forceOverride */);
David Stevens9440dc82017-03-16 19:00:20 -0700146 updateKeyguardSleepToken();
Jorim Jaggife762342016-10-13 14:33:27 +0200147
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -0700148 // Some stack visibility might change (e.g. docked stack)
Andrii Kulian0d595f32018-02-21 15:47:33 -0800149 mStackSupervisor.resumeFocusedStackTopActivityLocked();
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -0700150 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
151 mStackSupervisor.addStartingWindowsForVisibleActivities(true /* taskSwitch */);
152 mWindowManager.executeAppTransition();
153 } finally {
154 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "keyguardGoingAway: surfaceLayout");
155 mWindowManager.continueSurfaceLayout();
156 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
157
158 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
Jorim Jaggife762342016-10-13 14:33:27 +0200159 }
160 }
161
Lucas Dupinc80c67e2017-12-04 14:29:10 -0800162 void dismissKeyguard(IBinder token, IKeyguardDismissCallback callback, CharSequence message) {
Jorim Jaggi241ae102016-11-02 21:57:33 -0700163 final ActivityRecord activityRecord = ActivityRecord.forTokenLocked(token);
164 if (activityRecord == null || !activityRecord.visibleIgnoringKeyguard) {
165 failCallback(callback);
166 return;
167 }
Jorim Jaggid7214892017-07-18 14:05:19 +0200168 Slog.i(TAG, "Activity requesting to dismiss Keyguard: " + activityRecord);
chaviw59b98852017-06-13 12:05:44 -0700169
170 // If the client has requested to dismiss the keyguard and the Activity has the flag to
171 // turn the screen on, wakeup the screen if it's the top Activity.
172 if (activityRecord.getTurnScreenOnFlag() && activityRecord.isTopRunningActivity()) {
173 mStackSupervisor.wakeUp("dismissKeyguard");
174 }
175
Lucas Dupinc80c67e2017-12-04 14:29:10 -0800176 mWindowManager.dismissKeyguard(callback, message);
Jorim Jaggi241ae102016-11-02 21:57:33 -0700177 }
178
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -0700179 private void setKeyguardGoingAway(boolean keyguardGoingAway) {
180 mKeyguardGoingAway = keyguardGoingAway;
181 mWindowManager.setKeyguardGoingAway(keyguardGoingAway);
182 }
183
Jorim Jaggi241ae102016-11-02 21:57:33 -0700184 private void failCallback(IKeyguardDismissCallback callback) {
185 try {
186 callback.onDismissError();
187 } catch (RemoteException e) {
188 Slog.w(TAG, "Failed to call callback", e);
189 }
190 }
191
Jorim Jaggife762342016-10-13 14:33:27 +0200192 private int convertTransitFlags(int keyguardGoingAwayFlags) {
193 int result = 0;
194 if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_TO_SHADE) != 0) {
195 result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_TO_SHADE;
196 }
197 if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_NO_WINDOW_ANIMATIONS) != 0) {
198 result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_NO_ANIMATION;
199 }
200 if ((keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_WITH_WALLPAPER) != 0) {
201 result |= TRANSIT_FLAG_KEYGUARD_GOING_AWAY_WITH_WALLPAPER;
202 }
203 return result;
204 }
205
206 /**
207 * Starts a batch of visibility updates.
208 */
209 void beginActivityVisibilityUpdate() {
210 mVisibilityTransactionDepth++;
211 }
212
213 /**
214 * Ends a batch of visibility updates. After all batches are done, this method makes sure to
215 * update lockscreen occluded/dismiss state if needed.
216 */
217 void endActivityVisibilityUpdate() {
218 mVisibilityTransactionDepth--;
219 if (mVisibilityTransactionDepth == 0) {
220 visibilitiesUpdated();
221 }
222 }
223
Jorim Jaggie69c9312016-10-31 18:24:38 -0700224 /**
225 * @return True if we may show an activity while Keyguard is showing because we are in the
226 * process of dismissing it anyways, false otherwise.
227 */
Jorim Jaggi07961872016-11-23 11:28:57 +0100228 boolean canShowActivityWhileKeyguardShowing(ActivityRecord r, boolean dismissKeyguard) {
229
230 // Allow to show it when we are about to dismiss Keyguard. This isn't allowed if r is
231 // already the dismissing activity, in which case we don't allow it to repeatedly dismiss
232 // Keyguard.
233 return dismissKeyguard && canDismissKeyguard() &&
234 (mDismissalRequested || r != mDismissingKeyguardActivity);
235 }
236
237 /**
238 * @return True if we may show an activity while Keyguard is occluded, false otherwise.
239 */
240 boolean canShowWhileOccluded(boolean dismissKeyguard, boolean showWhenLocked) {
241 return showWhenLocked || dismissKeyguard && !mWindowManager.isKeyguardSecure();
Jorim Jaggie69c9312016-10-31 18:24:38 -0700242 }
243
Jorim Jaggife762342016-10-13 14:33:27 +0200244 private void visibilitiesUpdated() {
245 final boolean lastOccluded = mOccluded;
246 final ActivityRecord lastDismissingKeyguardActivity = mDismissingKeyguardActivity;
247 mOccluded = false;
248 mDismissingKeyguardActivity = null;
Jorim Jaggife762342016-10-13 14:33:27 +0200249
Pat Plunkett40426e02017-10-31 14:06:29 -0700250 for (int displayNdx = mStackSupervisor.getChildCount() - 1; displayNdx >= 0; displayNdx--) {
251 final ActivityDisplay display = mStackSupervisor.getChildAt(displayNdx);
252 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
253 final ActivityStack stack = display.getChildAt(stackNdx);
Jorim Jaggi07961872016-11-23 11:28:57 +0100254
Pat Plunkett40426e02017-10-31 14:06:29 -0700255 // Only the top activity of the focused stack on the default display may control
256 // occluded state.
257 if (display.mDisplayId == DEFAULT_DISPLAY
258 && mStackSupervisor.isFocusedStack(stack)) {
259
260 // A dismissing activity occludes Keyguard in the insecure case for legacy
261 // reasons.
262 final ActivityRecord topDismissing = stack.getTopDismissingKeyguardActivity();
263 mOccluded =
264 stack.topActivityOccludesKeyguard()
265 || (topDismissing != null
266 && stack.topRunningActivityLocked() == topDismissing
267 && canShowWhileOccluded(
268 true /* dismissKeyguard */,
269 false /* showWhenLocked */));
270 }
271
272 if (mDismissingKeyguardActivity == null
273 && stack.getTopDismissingKeyguardActivity() != null) {
274 mDismissingKeyguardActivity = stack.getTopDismissingKeyguardActivity();
275 }
Jorim Jaggife762342016-10-13 14:33:27 +0200276 }
277 }
Jorim Jaggi77e10432016-10-26 17:43:56 -0700278 mOccluded |= mWindowManager.isShowingDream();
Jorim Jaggife762342016-10-13 14:33:27 +0200279 if (mOccluded != lastOccluded) {
280 handleOccludedChanged();
281 }
282 if (mDismissingKeyguardActivity != lastDismissingKeyguardActivity) {
283 handleDismissKeyguard();
284 }
285 }
286
287 /**
288 * Called when occluded state changed.
289 */
290 private void handleOccludedChanged() {
291 mWindowManager.onKeyguardOccludedChanged(mOccluded);
292 if (isKeyguardLocked()) {
293 mWindowManager.deferSurfaceLayout();
294 try {
295 mWindowManager.prepareAppTransition(resolveOccludeTransit(),
296 false /* alwaysKeepCurrent */, 0 /* flags */, true /* forceOverride */);
David Stevens9440dc82017-03-16 19:00:20 -0700297 updateKeyguardSleepToken();
Jorim Jaggife762342016-10-13 14:33:27 +0200298 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
299 mWindowManager.executeAppTransition();
300 } finally {
301 mWindowManager.continueSurfaceLayout();
302 }
303 }
304 dismissDockedStackIfNeeded();
305 }
306
307 /**
308 * Called when somebody might want to dismiss the Keyguard.
309 */
310 private void handleDismissKeyguard() {
Jorim Jaggi07961872016-11-23 11:28:57 +0100311 // We only allow dismissing Keyguard via the flag when Keyguard is secure for legacy
312 // reasons, because that's how apps used to dismiss Keyguard in the secure case. In the
313 // insecure case, we actually show it on top of the lockscreen. See #canShowWhileOccluded.
314 if (!mOccluded && mDismissingKeyguardActivity != null
315 && mWindowManager.isKeyguardSecure()) {
Lucas Dupinc80c67e2017-12-04 14:29:10 -0800316 mWindowManager.dismissKeyguard(null /* callback */, null /* message */);
Jorim Jaggi07961872016-11-23 11:28:57 +0100317 mDismissalRequested = true;
Jorim Jaggife762342016-10-13 14:33:27 +0200318
319 // If we are about to unocclude the Keyguard, but we can dismiss it without security,
320 // we immediately dismiss the Keyguard so the activity gets shown without a flicker.
321 if (mKeyguardShowing && canDismissKeyguard()
322 && mWindowManager.getPendingAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
323 mWindowManager.prepareAppTransition(mBeforeUnoccludeTransit,
324 false /* alwaysKeepCurrent */, 0 /* flags */, true /* forceOverride */);
Jorim Jaggife762342016-10-13 14:33:27 +0200325 mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
326 mWindowManager.executeAppTransition();
327 }
328 }
329 }
330
331 /**
332 * @return true if Keyguard can be currently dismissed without entering credentials.
333 */
Andrii Kulianfc8f82b2017-01-26 13:17:27 -0800334 boolean canDismissKeyguard() {
Jorim Jaggife762342016-10-13 14:33:27 +0200335 return mWindowManager.isKeyguardTrusted() || !mWindowManager.isKeyguardSecure();
336 }
337
338 private int resolveOccludeTransit() {
339 if (mBeforeUnoccludeTransit != TRANSIT_UNSET
340 && mWindowManager.getPendingAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
341 && mOccluded) {
342
343 // Reuse old transit in case we are occluding Keyguard again, meaning that we never
344 // actually occclude/unocclude Keyguard, but just run a normal transition.
345 return mBeforeUnoccludeTransit;
346 } else if (!mOccluded) {
347
348 // Save transit in case we dismiss/occlude Keyguard shortly after.
349 mBeforeUnoccludeTransit = mWindowManager.getPendingAppTransition();
350 return TRANSIT_KEYGUARD_UNOCCLUDE;
351 } else {
352 return TRANSIT_KEYGUARD_OCCLUDE;
353 }
354 }
355
356 private void dismissDockedStackIfNeeded() {
357 if (mKeyguardShowing && mOccluded) {
358 // The lock screen is currently showing, but is occluded by a window that can
359 // show on top of the lock screen. In this can we want to dismiss the docked
360 // stack since it will be complicated/risky to try to put the activity on top
361 // of the lock screen in the right fullscreen configuration.
Wale Ogunwalea0f5b5e2017-10-11 09:37:23 -0700362 final ActivityStack stack = mStackSupervisor.getDefaultDisplay().getSplitScreenPrimaryStack();
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700363 if (stack == null) {
364 return;
365 }
366 mStackSupervisor.moveTasksToFullscreenStackLocked(stack,
367 mStackSupervisor.mFocusedStack == stack);
Jorim Jaggife762342016-10-13 14:33:27 +0200368 }
369 }
Jorim Jaggi8d786932016-10-26 19:08:36 -0700370
David Stevens9440dc82017-03-16 19:00:20 -0700371 private void updateKeyguardSleepToken() {
David Stevens53a39ea2017-08-23 18:41:49 -0700372 if (mSleepToken == null && isKeyguardShowing(DEFAULT_DISPLAY)) {
David Stevens9440dc82017-03-16 19:00:20 -0700373 mSleepToken = mService.acquireSleepToken("Keyguard", DEFAULT_DISPLAY);
David Stevens53a39ea2017-08-23 18:41:49 -0700374 } else if (mSleepToken != null && !isKeyguardShowing(DEFAULT_DISPLAY)) {
David Stevens9440dc82017-03-16 19:00:20 -0700375 mSleepToken.release();
376 mSleepToken = null;
377 }
378 }
379
Jorim Jaggi8d786932016-10-26 19:08:36 -0700380 void dump(PrintWriter pw, String prefix) {
381 pw.println(prefix + "KeyguardController:");
382 pw.println(prefix + " mKeyguardShowing=" + mKeyguardShowing);
383 pw.println(prefix + " mKeyguardGoingAway=" + mKeyguardGoingAway);
384 pw.println(prefix + " mOccluded=" + mOccluded);
385 pw.println(prefix + " mDismissingKeyguardActivity=" + mDismissingKeyguardActivity);
Jorim Jaggi07961872016-11-23 11:28:57 +0100386 pw.println(prefix + " mDismissalRequested=" + mDismissalRequested);
Jorim Jaggi8d786932016-10-26 19:08:36 -0700387 pw.println(prefix + " mVisibilityTransactionDepth=" + mVisibilityTransactionDepth);
388 }
Steven Timotius4346f0a2017-09-12 11:07:21 -0700389
390 void writeToProto(ProtoOutputStream proto, long fieldId) {
391 final long token = proto.start(fieldId);
392 proto.write(KEYGUARD_SHOWING, mKeyguardShowing);
393 proto.write(KEYGUARD_OCCLUDED, mOccluded);
394 proto.end(token);
395 }
Jorim Jaggife762342016-10-13 14:33:27 +0200396}