blob: c92de2b84df6820e2256bfbce4e124bf1ccfe8c3 [file] [log] [blame]
Wale Ogunwale076c3b12019-11-20 12:17:22 -08001/*
2 * Copyright (C) 2019 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.wm;
18
19import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
20
21import static com.android.server.wm.ActivityStack.TAG_VISIBILITY;
22import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_VISIBILITY;
23
24import android.util.Slog;
25
26import com.android.internal.util.function.pooled.PooledConsumer;
27import com.android.internal.util.function.pooled.PooledLambda;
28
29/** Helper class to ensure activities are in the right visible state for a container. */
30class EnsureActivitiesVisibleHelper {
31 private final ActivityStack mContiner;
32 private ActivityRecord mTop;
33 private ActivityRecord mStarting;
34 private boolean mAboveTop;
35 private boolean mContainerShouldBeVisible;
36 private boolean mBehindFullscreenActivity;
37 private int mConfigChanges;
38 private boolean mPreserveWindows;
39 private boolean mNotifyClients;
40
41 EnsureActivitiesVisibleHelper(ActivityStack container) {
42 mContiner = container;
43 }
44
45 void reset(ActivityRecord starting, int configChanges, boolean preserveWindows,
46 boolean notifyClients) {
47 mStarting = starting;
Wale Ogunwale85fb19a2019-12-05 10:41:05 +090048 mTop = mContiner.topRunningActivity();
Wale Ogunwale076c3b12019-11-20 12:17:22 -080049 // If the top activity is not fullscreen, then we need to make sure any activities under it
50 // are now visible.
51 mAboveTop = mTop != null;
52 mContainerShouldBeVisible = mContiner.shouldBeVisible(mStarting);
53 mBehindFullscreenActivity = !mContainerShouldBeVisible;
54 mConfigChanges = configChanges;
55 mPreserveWindows = preserveWindows;
56 mNotifyClients = notifyClients;
57 }
58
59 /**
60 * Ensure visibility with an option to also update the configuration of visible activities.
61 * @see ActivityStack#ensureActivitiesVisible(ActivityRecord, int, boolean)
Louis Chang149d5c82019-12-30 09:47:39 +080062 * @see RootWindowContainer#ensureActivitiesVisible(ActivityRecord, int, boolean)
Wale Ogunwale076c3b12019-11-20 12:17:22 -080063 */
Diego Vela25cbe9d2020-05-14 01:06:02 +000064 void process(ActivityRecord starting, int configChanges, boolean preserveWindows,
Wale Ogunwale076c3b12019-11-20 12:17:22 -080065 boolean notifyClients) {
66 reset(starting, configChanges, preserveWindows, notifyClients);
67
68 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "ensureActivitiesVisible behind " + mTop
69 + " configChanges=0x" + Integer.toHexString(configChanges));
70 if (mTop != null) {
71 mContiner.checkTranslucentActivityWaiting(mTop);
72 }
73
74 // We should not resume activities that being launched behind because these
75 // activities are actually behind other fullscreen activities, but still required
76 // to be visible (such as performing Recents animation).
77 final boolean resumeTopActivity = mTop != null && !mTop.mLaunchTaskBehind
Evan Rosky226de132020-01-03 18:00:29 -080078 && mContiner.isTopActivityFocusable()
Wale Ogunwale0d465192020-01-23 19:14:44 -080079 && (starting == null || !starting.isDescendantOf(mContiner));
Wale Ogunwale076c3b12019-11-20 12:17:22 -080080
81 final PooledConsumer f = PooledLambda.obtainConsumer(
82 EnsureActivitiesVisibleHelper::setActivityVisibilityState, this,
Andrii Kulianb9faa032019-10-17 23:11:54 -070083 PooledLambda.__(ActivityRecord.class), starting, resumeTopActivity);
Wale Ogunwale076c3b12019-11-20 12:17:22 -080084 mContiner.forAllActivities(f);
85 f.recycle();
86 }
87
Andrii Kulianb9faa032019-10-17 23:11:54 -070088 private void setActivityVisibilityState(ActivityRecord r, ActivityRecord starting,
89 final boolean resumeTopActivity) {
Wale Ogunwale076c3b12019-11-20 12:17:22 -080090 final boolean isTop = r == mTop;
91 if (mAboveTop && !isTop) {
92 return;
93 }
94 mAboveTop = false;
95
96 final boolean reallyVisible = r.shouldBeVisible(
97 mBehindFullscreenActivity, false /* ignoringKeyguard */);
98
99 // Check whether activity should be visible without Keyguard influence
100 if (r.visibleIgnoringKeyguard) {
101 if (r.occludesParent()) {
102 // At this point, nothing else needs to be shown in this task.
103 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Fullscreen: at " + r
104 + " stackVisible=" + mContainerShouldBeVisible
105 + " behindFullscreen=" + mBehindFullscreenActivity);
106 mBehindFullscreenActivity = true;
107 } else {
108 mBehindFullscreenActivity = false;
109 }
110 }
111
112 if (reallyVisible) {
113 if (r.finishing) {
114 return;
115 }
116 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make visible? " + r
117 + " finishing=" + r.finishing + " state=" + r.getState());
118 // First: if this is not the current activity being started, make
119 // sure it matches the current configuration.
120 if (r != mStarting && mNotifyClients) {
121 r.ensureActivityConfiguration(0 /* globalChanges */, mPreserveWindows,
122 true /* ignoreVisibility */);
123 }
124
125 if (!r.attachedToProcess()) {
126 makeVisibleAndRestartIfNeeded(mStarting, mConfigChanges, isTop,
127 resumeTopActivity && isTop, r);
128 } else if (r.mVisibleRequested) {
129 // If this activity is already visible, then there is nothing to do here.
130 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY,
131 "Skipping: already visible at " + r);
132
133 if (r.mClientVisibilityDeferred && mNotifyClients) {
Andrii Kulianb9faa032019-10-17 23:11:54 -0700134 r.makeActiveIfNeeded(r.mClientVisibilityDeferred ? null : starting);
135 r.mClientVisibilityDeferred = false;
Wale Ogunwale076c3b12019-11-20 12:17:22 -0800136 }
137
138 r.handleAlreadyVisible();
139 if (mNotifyClients) {
140 r.makeActiveIfNeeded(mStarting);
141 }
142 } else {
143 r.makeVisibleIfNeeded(mStarting, mNotifyClients);
144 }
145 // Aggregate current change flags.
146 mConfigChanges |= r.configChangeFlags;
147 } else {
148 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Make invisible? " + r
149 + " finishing=" + r.finishing + " state=" + r.getState()
150 + " stackShouldBeVisible=" + mContainerShouldBeVisible
151 + " behindFullscreenActivity=" + mBehindFullscreenActivity
152 + " mLaunchTaskBehind=" + r.mLaunchTaskBehind);
153 r.makeInvisible();
154 }
155
156 final int windowingMode = mContiner.getWindowingMode();
157 if (windowingMode == WINDOWING_MODE_FREEFORM) {
158 // The visibility of tasks and the activities they contain in freeform stack are
159 // determined individually unlike other stacks where the visibility or fullscreen
160 // status of an activity in a previous task affects other.
161 mBehindFullscreenActivity = !mContainerShouldBeVisible;
Jeff Chang24b0bf62019-12-19 19:15:07 +0800162 } else if (!mBehindFullscreenActivity && mContiner.isActivityTypeHome()
163 && r.isRootOfTask()) {
Wale Ogunwale076c3b12019-11-20 12:17:22 -0800164 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Home task: at " + mContiner
165 + " stackShouldBeVisible=" + mContainerShouldBeVisible
166 + " behindFullscreenActivity=" + mBehindFullscreenActivity);
167 // No other task in the home stack should be visible behind the home activity.
168 // Home activities is usually a translucent activity with the wallpaper behind
169 // them. However, when they don't have the wallpaper behind them, we want to
170 // show activities in the next application stack behind them vs. another
171 // task in the home stack like recents.
172 mBehindFullscreenActivity = true;
173 }
174 }
175
176 private void makeVisibleAndRestartIfNeeded(ActivityRecord starting, int configChanges,
177 boolean isTop, boolean andResume, ActivityRecord r) {
178 // We need to make sure the app is running if it's the top, or it is just made visible from
179 // invisible. If the app is already visible, it must have died while it was visible. In this
180 // case, we'll show the dead window but will not restart the app. Otherwise we could end up
181 // thrashing.
182 if (!isTop && r.mVisibleRequested) {
183 return;
184 }
185
186 // This activity needs to be visible, but isn't even running...
187 // get it started and resume if no other stack in this stack is resumed.
188 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Start and freeze screen for " + r);
189 if (r != starting) {
190 r.startFreezingScreenLocked(configChanges);
191 }
192 if (!r.mVisibleRequested || r.mLaunchTaskBehind) {
193 if (DEBUG_VISIBILITY) Slog.v(TAG_VISIBILITY, "Starting and making visible: " + r);
194 r.setVisibility(true);
195 }
196 if (r != starting) {
197 mContiner.mStackSupervisor.startSpecificActivity(r, andResume, true /* checkConfig */);
198 }
199 }
200}