summaryrefslogtreecommitdiff
path: root/services/core/java/com/android/server/wm/StartingSurfaceController.java
blob: f83173bd46c012d4fdc8f0392fec489f95bba886 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.wm;

import static android.window.StartingWindowInfo.TYPE_PARAMETER_ACTIVITY_CREATED;
import static android.window.StartingWindowInfo.TYPE_PARAMETER_ACTIVITY_DRAWN;
import static android.window.StartingWindowInfo.TYPE_PARAMETER_ALLOW_HANDLE_SOLID_COLOR_SCREEN;
import static android.window.StartingWindowInfo.TYPE_PARAMETER_ALLOW_TASK_SNAPSHOT;
import static android.window.StartingWindowInfo.TYPE_PARAMETER_LEGACY_SPLASH_SCREEN;
import static android.window.StartingWindowInfo.TYPE_PARAMETER_NEW_TASK;
import static android.window.StartingWindowInfo.TYPE_PARAMETER_PROCESS_RUNNING;
import static android.window.StartingWindowInfo.TYPE_PARAMETER_TASK_SWITCH;
import static android.window.StartingWindowInfo.TYPE_PARAMETER_USE_SOLID_COLOR_SPLASH_SCREEN;

import static com.android.server.wm.ActivityRecord.STARTING_WINDOW_TYPE_SNAPSHOT;
import static com.android.server.wm.ActivityRecord.STARTING_WINDOW_TYPE_SPLASH_SCREEN;
import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.ActivityOptions;
import android.app.compat.CompatChanges;
import android.compat.annotation.ChangeId;
import android.compat.annotation.EnabledSince;
import android.content.pm.ApplicationInfo;
import android.os.UserHandle;
import android.util.Slog;
import android.window.SplashScreenView;
import android.window.TaskSnapshot;

import java.util.ArrayList;
import java.util.function.Supplier;

/**
 * Managing to create and release a starting window surface.
 */
public class StartingSurfaceController {
    private static final String TAG = TAG_WITH_CLASS_NAME
            ? StartingSurfaceController.class.getSimpleName() : TAG_WM;
    /**
     * Application is allowed to receive the
     * {@link
     * android.window.SplashScreen.OnExitAnimationListener#onSplashScreenExit(SplashScreenView)}
     * callback, even when the splash screen only shows a solid color.
     */
    @ChangeId
    @EnabledSince(targetSdkVersion = android.os.Build.VERSION_CODES.TIRAMISU)
    private static final long ALLOW_COPY_SOLID_COLOR_VIEW = 205907456L;

    private final WindowManagerService mService;
    private final SplashScreenExceptionList mSplashScreenExceptionsList;

    // Cache status while deferring add starting window
    boolean mInitProcessRunning;
    boolean mInitNewTask;
    boolean mInitTaskSwitch;
    private final ArrayList<DeferringStartingWindowRecord> mDeferringAddStartActivities =
            new ArrayList<>();
    private boolean mDeferringAddStartingWindow;

    public StartingSurfaceController(WindowManagerService wm) {
        mService = wm;
        mSplashScreenExceptionsList = new SplashScreenExceptionList(wm.mContext.getMainExecutor());
    }

    StartingSurface createSplashScreenStartingSurface(ActivityRecord activity, int theme) {

        synchronized (mService.mGlobalLock) {
            final Task task = activity.getTask();
            if (task != null && mService.mAtmService.mTaskOrganizerController.addStartingWindow(
                    task, activity, theme, null /* taskSnapshot */)) {
                return new StartingSurface(task);
            }
        }
        return null;
    }

    /**
     * @see SplashScreenExceptionList#isException(String, int, Supplier)
     */
    boolean isExceptionApp(@NonNull String packageName, int targetSdk,
            @Nullable Supplier<ApplicationInfo> infoProvider) {
        return mSplashScreenExceptionsList.isException(packageName, targetSdk, infoProvider);
    }

    static int makeStartingWindowTypeParameter(boolean newTask, boolean taskSwitch,
            boolean processRunning, boolean allowTaskSnapshot, boolean activityCreated,
            boolean isSolidColor, boolean useLegacy, boolean activityDrawn, int startingWindowType,
            String packageName, int userId) {
        int parameter = 0;
        if (newTask) {
            parameter |= TYPE_PARAMETER_NEW_TASK;
        }
        if (taskSwitch) {
            parameter |= TYPE_PARAMETER_TASK_SWITCH;
        }
        if (processRunning) {
            parameter |= TYPE_PARAMETER_PROCESS_RUNNING;
        }
        if (allowTaskSnapshot) {
            parameter |= TYPE_PARAMETER_ALLOW_TASK_SNAPSHOT;
        }
        if (activityCreated || startingWindowType == STARTING_WINDOW_TYPE_SNAPSHOT) {
            parameter |= TYPE_PARAMETER_ACTIVITY_CREATED;
        }
        if (isSolidColor) {
            parameter |= TYPE_PARAMETER_USE_SOLID_COLOR_SPLASH_SCREEN;
        }
        if (useLegacy) {
            parameter |= TYPE_PARAMETER_LEGACY_SPLASH_SCREEN;
        }
        if (activityDrawn) {
            parameter |= TYPE_PARAMETER_ACTIVITY_DRAWN;
        }
        if (startingWindowType == STARTING_WINDOW_TYPE_SPLASH_SCREEN
                && CompatChanges.isChangeEnabled(ALLOW_COPY_SOLID_COLOR_VIEW, packageName,
                UserHandle.of(userId))) {
            parameter |= TYPE_PARAMETER_ALLOW_HANDLE_SOLID_COLOR_SCREEN;
        }
        return parameter;
    }

    StartingSurface createTaskSnapshotSurface(ActivityRecord activity, TaskSnapshot taskSnapshot) {
        final WindowState topFullscreenOpaqueWindow;
        final Task task;
        synchronized (mService.mGlobalLock) {
            task = activity.getTask();
            if (task == null) {
                Slog.w(TAG, "TaskSnapshotSurface.create: Failed to find task for activity="
                        + activity);
                return null;
            }
            final ActivityRecord topFullscreenActivity =
                    activity.getTask().getTopFullscreenActivity();
            if (topFullscreenActivity == null) {
                Slog.w(TAG, "TaskSnapshotSurface.create: Failed to find top fullscreen for task="
                        + task);
                return null;
            }
            topFullscreenOpaqueWindow = topFullscreenActivity.getTopFullscreenOpaqueWindow();
            if (topFullscreenOpaqueWindow == null) {
                Slog.w(TAG, "TaskSnapshotSurface.create: no opaque window in "
                        + topFullscreenActivity);
                return null;
            }
            if (activity.mDisplayContent.getRotation() != taskSnapshot.getRotation()) {
                // The snapshot should have been checked by ActivityRecord#isSnapshotCompatible
                // that the activity will be updated to the same rotation as the snapshot. Since
                // the transition is not started yet, fixed rotation transform needs to be applied
                // earlier to make the snapshot show in a rotated container.
                activity.mDisplayContent.handleTopActivityLaunchingInDifferentOrientation(
                        activity, false /* checkOpening */);
            }
                mService.mAtmService.mTaskOrganizerController.addStartingWindow(task,
                        activity, 0 /* launchTheme */, taskSnapshot);
            return new StartingSurface(task);
        }
    }

    private static final class DeferringStartingWindowRecord {
        final ActivityRecord mDeferring;
        final ActivityRecord mPrev;
        final ActivityRecord mSource;

        DeferringStartingWindowRecord(ActivityRecord deferring, ActivityRecord prev,
                ActivityRecord source) {
            mDeferring = deferring;
            mPrev = prev;
            mSource = source;
        }
    }

    /**
     * Shows a starting window while starting a new activity. Do not use this method to create a
     * starting window for an existing activity.
     */
    void showStartingWindow(ActivityRecord target, ActivityRecord prev,
            boolean newTask, boolean isTaskSwitch, ActivityRecord source) {
        if (mDeferringAddStartingWindow) {
            addDeferringRecord(target, prev, newTask, isTaskSwitch, source);
        } else {
            target.showStartingWindow(prev, newTask, isTaskSwitch, true /* startActivity */,
                    source);
        }
    }

    /**
     * Queueing the starting activity status while deferring add starting window.
     * @see Task#startActivityLocked
     */
    private void addDeferringRecord(ActivityRecord deferring, ActivityRecord prev,
            boolean newTask, boolean isTaskSwitch, ActivityRecord source) {
        // Set newTask, taskSwitch, processRunning form first activity because those can change
        // after first activity started.
        if (mDeferringAddStartActivities.isEmpty()) {
            mInitProcessRunning = deferring.isProcessRunning();
            mInitNewTask = newTask;
            mInitTaskSwitch = isTaskSwitch;
        }
        mDeferringAddStartActivities.add(new DeferringStartingWindowRecord(
                deferring, prev, source));
    }

    private void showStartingWindowFromDeferringActivities(ActivityOptions topOptions) {
        // Attempt to add starting window from the top-most activity.
        for (int i = mDeferringAddStartActivities.size() - 1; i >= 0; --i) {
            final DeferringStartingWindowRecord next = mDeferringAddStartActivities.get(i);
            next.mDeferring.showStartingWindow(next.mPrev, mInitNewTask, mInitTaskSwitch,
                    mInitProcessRunning, true /* startActivity */, next.mSource, topOptions);
            // If one succeeds, it is done.
            if (next.mDeferring.mStartingData != null) {
                break;
            }
        }
        mDeferringAddStartActivities.clear();
    }

    /**
     * Begin deferring add starting window in one pass.
     * This is used to deferring add starting window while starting multiples activities because
     * system only need to provide a starting window to the top-visible activity.
     * Most call {@link #endDeferAddStartingWindow} when starting activities process finished.
     * @see #endDeferAddStartingWindow()
     */
    void beginDeferAddStartingWindow() {
        mDeferringAddStartingWindow = true;
    }

    /**
     * End deferring add starting window.
     */
    void endDeferAddStartingWindow(ActivityOptions topOptions) {
        mDeferringAddStartingWindow = false;
        showStartingWindowFromDeferringActivities(topOptions);
    }

    final class StartingSurface {
        private final Task mTask;

        StartingSurface(Task task) {
            mTask = task;
        }

        /**
         * Removes the starting window surface. Do not hold the window manager lock when calling
         * this method!
         * @param animate Whether need to play the default exit animation for starting window.
         */
        public void remove(boolean animate) {
            synchronized (mService.mGlobalLock) {
                mService.mAtmService.mTaskOrganizerController.removeStartingWindow(mTask, animate);
            }
        }
    }
}