Wale Ogunwale | 076c3b1 | 2019-11-20 12:17:22 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.wm; |
| 18 | |
| 19 | import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM; |
| 20 | |
| 21 | import static com.android.server.wm.ActivityStack.TAG_VISIBILITY; |
| 22 | import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_VISIBILITY; |
| 23 | |
| 24 | import android.util.Slog; |
| 25 | |
| 26 | import com.android.internal.util.function.pooled.PooledConsumer; |
| 27 | import com.android.internal.util.function.pooled.PooledLambda; |
| 28 | |
| 29 | /** Helper class to ensure activities are in the right visible state for a container. */ |
| 30 | class 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 Ogunwale | 85fb19a | 2019-12-05 10:41:05 +0900 | [diff] [blame] | 48 | mTop = mContiner.topRunningActivity(); |
Wale Ogunwale | 076c3b1 | 2019-11-20 12:17:22 -0800 | [diff] [blame] | 49 | // 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 Chang | 149d5c8 | 2019-12-30 09:47:39 +0800 | [diff] [blame] | 62 | * @see RootWindowContainer#ensureActivitiesVisible(ActivityRecord, int, boolean) |
Wale Ogunwale | 076c3b1 | 2019-11-20 12:17:22 -0800 | [diff] [blame] | 63 | */ |
Diego Vela | 25cbe9d | 2020-05-14 01:06:02 +0000 | [diff] [blame] | 64 | void process(ActivityRecord starting, int configChanges, boolean preserveWindows, |
Wale Ogunwale | 076c3b1 | 2019-11-20 12:17:22 -0800 | [diff] [blame] | 65 | 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 Rosky | 226de13 | 2020-01-03 18:00:29 -0800 | [diff] [blame] | 78 | && mContiner.isTopActivityFocusable() |
Wale Ogunwale | 0d46519 | 2020-01-23 19:14:44 -0800 | [diff] [blame] | 79 | && (starting == null || !starting.isDescendantOf(mContiner)); |
Wale Ogunwale | 076c3b1 | 2019-11-20 12:17:22 -0800 | [diff] [blame] | 80 | |
| 81 | final PooledConsumer f = PooledLambda.obtainConsumer( |
| 82 | EnsureActivitiesVisibleHelper::setActivityVisibilityState, this, |
Andrii Kulian | b9faa03 | 2019-10-17 23:11:54 -0700 | [diff] [blame] | 83 | PooledLambda.__(ActivityRecord.class), starting, resumeTopActivity); |
Wale Ogunwale | 076c3b1 | 2019-11-20 12:17:22 -0800 | [diff] [blame] | 84 | mContiner.forAllActivities(f); |
| 85 | f.recycle(); |
| 86 | } |
| 87 | |
Andrii Kulian | b9faa03 | 2019-10-17 23:11:54 -0700 | [diff] [blame] | 88 | private void setActivityVisibilityState(ActivityRecord r, ActivityRecord starting, |
| 89 | final boolean resumeTopActivity) { |
Wale Ogunwale | 076c3b1 | 2019-11-20 12:17:22 -0800 | [diff] [blame] | 90 | 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 Kulian | b9faa03 | 2019-10-17 23:11:54 -0700 | [diff] [blame] | 134 | r.makeActiveIfNeeded(r.mClientVisibilityDeferred ? null : starting); |
| 135 | r.mClientVisibilityDeferred = false; |
Wale Ogunwale | 076c3b1 | 2019-11-20 12:17:22 -0800 | [diff] [blame] | 136 | } |
| 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 Chang | 24b0bf6 | 2019-12-19 19:15:07 +0800 | [diff] [blame] | 162 | } else if (!mBehindFullscreenActivity && mContiner.isActivityTypeHome() |
| 163 | && r.isRootOfTask()) { |
Wale Ogunwale | 076c3b1 | 2019-11-20 12:17:22 -0800 | [diff] [blame] | 164 | 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 | } |