blob: 6dc384a8831e14d313174580045b6f33973ae942 [file] [log] [blame]
Craig Mautner59c00972012-07-30 12:10:24 -07001/*
2 * Copyright (C) 2012 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
Wale Ogunwale30e441d2017-11-09 08:28:45 -080019import static android.app.ActivityManager.SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT;
Wale Ogunwale61911492017-10-11 08:50:50 -070020import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070021import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070022import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwale687b4272017-07-27 02:56:23 -070023import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070024import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
25import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070026import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070027import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
28import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale687b4272017-07-27 02:56:23 -070029import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
30import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070031import static android.view.Display.DEFAULT_DISPLAY;
32import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070033import static android.view.Surface.ROTATION_0;
34import static android.view.Surface.ROTATION_180;
35import static android.view.Surface.ROTATION_270;
36import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070037import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070038import static android.view.WindowManager.DOCKED_BOTTOM;
39import static android.view.WindowManager.DOCKED_INVALID;
40import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080041import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
42import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
43import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070044import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070045import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070046import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
47import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070048import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070049import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070050import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070051import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Robert Carrf7a7ca82017-12-06 13:17:07 -080052import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
Wale Ogunwale494009b82016-10-21 09:01:38 -070053import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070054import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070055import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
56import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070057import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070058import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
59import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070060import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070061import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Adrian Roose99bc052017-11-20 17:55:31 +010062import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
63import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
64import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
65import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Adrian Roos1cf585052018-01-03 18:43:27 +010066import static com.android.server.wm.utils.CoordinateTransforms.transformPhysicalToLogicalCoordinates;
Jorim Jaggif84e2f62018-01-16 14:17:59 +010067import static android.view.WindowManager.TRANSIT_KEYGUARD_UNOCCLUDE;
Wale Ogunwale1666e312016-12-16 11:27:18 -080068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070069import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070070import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
72import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020076import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070077import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070078import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070079import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080080import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070081import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070082import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070083import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070084import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070085import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070086import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080087import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -070088import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070089import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
90import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070091import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070092import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -070093import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
94import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070095import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -070096import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070097import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070098import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070099import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700100import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700101import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700102import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700103import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700104import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700105import static com.android.server.wm.proto.DisplayProto.ABOVE_APP_WINDOWS;
106import static com.android.server.wm.proto.DisplayProto.BELOW_APP_WINDOWS;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000107import static com.android.server.wm.proto.DisplayProto.DISPLAY_FRAMES;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700108import static com.android.server.wm.proto.DisplayProto.DISPLAY_INFO;
109import static com.android.server.wm.proto.DisplayProto.DOCKED_STACK_DIVIDER_CONTROLLER;
110import static com.android.server.wm.proto.DisplayProto.DPI;
111import static com.android.server.wm.proto.DisplayProto.ID;
112import static com.android.server.wm.proto.DisplayProto.IME_WINDOWS;
113import static com.android.server.wm.proto.DisplayProto.PINNED_STACK_CONTROLLER;
Steven Timotiusf2d68892017-08-28 17:00:01 -0700114import static com.android.server.wm.proto.DisplayProto.ROTATION;
115import static com.android.server.wm.proto.DisplayProto.SCREEN_ROTATION_ANIMATION;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700116import static com.android.server.wm.proto.DisplayProto.STACKS;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700117import static com.android.server.wm.proto.DisplayProto.WINDOW_CONTAINER;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700118
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700119import android.annotation.CallSuper;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700120import android.annotation.NonNull;
Dean Harding3e5a1522017-10-06 09:19:01 -0700121import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700122import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700123import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700124import android.graphics.Bitmap;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700125import android.graphics.Matrix;
Adrian Roos1cf585052018-01-03 18:43:27 +0100126import android.graphics.Path;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800127import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700128import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700129import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100130import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700131import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700132import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700133import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700134import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700135import android.os.RemoteException;
136import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100137import android.os.Trace;
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800138import android.util.ArraySet;
Chong Zhang8e89b312015-09-09 15:09:30 -0700139import android.util.DisplayMetrics;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800140import android.util.MutableBoolean;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700141import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700142import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700143import android.view.Display;
Adrian Roos1cf585052018-01-03 18:43:27 +0100144import android.view.DisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700145import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700146import android.view.InputDevice;
Robert Carrb1579c82017-09-05 14:54:47 -0700147import android.view.MagnificationSpec;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700148import android.view.Surface;
149import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100150import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700151import android.view.SurfaceSession;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700152
Bryce Lee48f4b572017-04-10 10:54:15 -0700153import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800154import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700155import com.android.internal.view.IInputMethodClient;
Adrian Roose99bc052017-11-20 17:55:31 +0100156import com.android.server.policy.WindowManagerPolicy;
Craig Mautner59c00972012-07-30 12:10:24 -0700157
Robert Carr3b716242016-08-16 16:02:21 -0700158import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700159import java.io.PrintWriter;
160import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700161import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700162import java.util.HashMap;
163import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700164import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700165import java.util.List;
Adrian Roos1cf585052018-01-03 18:43:27 +0100166import java.util.Objects;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800167import java.util.function.Consumer;
168import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700169
Craig Mautner59c00972012-07-30 12:10:24 -0700170/**
171 * Utility class for keeping track of the WindowStates and other pertinent contents of a
172 * particular Display.
173 *
174 * IMPORTANT: No method from this class should ever be used without holding
175 * WindowManagerService.mWindowMap.
176 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700177class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700178 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700179
180 /** Unique identifier of this stack. */
181 private final int mDisplayId;
182
Wale Ogunwale3a931692016-11-02 16:49:48 -0700183 /** The containers below are the only child containers the display can have. */
184 // Contains all window containers that are related to apps (Activities)
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100185 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700186 // Contains all non-app window containers that should be displayed above the app containers
187 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800188 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100189 new AboveAppWindowContainers("mAboveAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700190 // Contains all non-app window containers that should be displayed below the app containers
191 // (e.g. Wallpaper).
192 private final NonAppWindowContainers mBelowAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100193 new NonAppWindowContainers("mBelowAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700194 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
195 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
Robert Carr9034d962018-01-04 18:27:42 -0800196 // window containers together and move them in-sync if/when needed. We use a subclass of
197 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
198 private final NonMagnifiableWindowContainers mImeWindowsContainers =
199 new NonMagnifiableWindowContainers("mImeWindowsContainers", mService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700200
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000201 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800202 private WindowState mTmpWindow2;
203 private WindowAnimator mTmpWindowAnimator;
204 private boolean mTmpRecoveringMemory;
205 private boolean mUpdateImeTarget;
206 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700207 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700208
Wale Ogunwale02319a62016-09-26 15:21:22 -0700209 // Mapping from a token IBinder to a WindowToken object on this display.
210 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
211
Andrii Kuliancd097992017-03-23 18:31:59 -0700212 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700213 int mInitialDisplayWidth = 0;
214 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700215 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700216
Adrian Roos1cf585052018-01-03 18:43:27 +0100217 DisplayCutout mInitialDisplayCutout;
218 DisplayCutout mDisplayCutoutOverride;
219
Andrii Kuliancd097992017-03-23 18:31:59 -0700220 /**
221 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
222 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
223 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
224 */
Craig Mautner59c00972012-07-30 12:10:24 -0700225 int mBaseDisplayWidth = 0;
226 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700227 /**
228 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
229 * but can be set from Settings or via shell command "adb shell wm density".
230 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
231 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700232 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700233 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700234 private final DisplayInfo mDisplayInfo = new DisplayInfo();
235 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700236 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000237 DisplayFrames mDisplayFrames;
238
Andrii Kulian06d07d62017-03-14 11:11:47 -0700239 /**
240 * For default display it contains real metrics, empty for others.
241 * @see WindowManagerService#createWatermarkInTransaction()
242 */
243 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
244 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
245 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700246
Andrii Kulian06d07d62017-03-14 11:11:47 -0700247 /**
248 * Compat metrics computed based on {@link #mDisplayMetrics}.
249 * @see #updateDisplayAndOrientation(int)
250 */
251 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
252
253 /** The desired scaling factor for compatible apps. */
254 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700255
Andrii Kulian8ee72852017-03-10 10:36:45 -0800256 /**
257 * Current rotation of the display.
258 * Constants as per {@link android.view.Surface.Rotation}.
259 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700260 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800261 */
262 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700263
Andrii Kulian8ee72852017-03-10 10:36:45 -0800264 /**
265 * Last applied orientation of the display.
266 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
267 *
268 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
269 */
270 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700271
Andrii Kulian8ee72852017-03-10 10:36:45 -0800272 /**
273 * Flag indicating that the application is receiving an orientation that has different metrics
274 * than it expected. E.g. Portrait instead of Landscape.
275 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700276 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800277 */
278 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700279
Andrii Kulian8ee72852017-03-10 10:36:45 -0800280 /**
281 * Orientation forced by some window. If there is no visible window that specifies orientation
282 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
283 *
284 * @see NonAppWindowContainers#getOrientation()
285 */
286 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700287
Andrii Kulian8ee72852017-03-10 10:36:45 -0800288 /**
289 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
290 * occluded.
291 *
292 * @see NonAppWindowContainers#getOrientation()
293 */
294 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
295
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700296 /**
297 * Keep track of wallpaper visibility to notify changes.
298 */
299 private boolean mLastWallpaperVisible = false;
300
Andrii Kulian06d07d62017-03-14 11:11:47 -0700301 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700302
Craig Mautner39834192012-09-02 07:47:24 -0700303 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700304 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700305 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700306 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800307 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700308
Craig Mautnerdc548482014-02-05 13:35:24 -0800309 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700310 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800311
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700312 /** Detect user tapping outside of current focused task bounds .*/
313 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700314
Craig Mautner6601b7b2013-04-29 10:29:11 -0700315 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700316 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700317
Craig Mautner6601b7b2013-04-29 10:29:11 -0700318 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800319 private final Rect mTmpRect = new Rect();
320 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700321 private final RectF mTmpRectF = new RectF();
322 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800323 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700324
Bryce Leef3c6a472017-11-14 14:53:06 -0800325 /** Used for handing back size of display */
326 private final Rect mTmpBounds = new Rect();
327
Craig Mautner95da1082014-02-24 17:54:35 -0800328 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700329 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800330
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700331 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700332 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700333
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800334 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800335 /** A collection of windows that provide tap exclude regions inside of them. */
336 final ArraySet<WindowState> mTapExcludeProvidingWindows = new ArraySet<>();
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800337
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000338 private boolean mHaveBootMsg = false;
339 private boolean mHaveApp = false;
340 private boolean mHaveWallpaper = false;
341 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700342
343 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
344
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700345 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
346 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000347 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
348 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700349
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700350 // True if this display is in the process of being removed. Used to determine if the removal of
351 // the display's direct children should be allowed.
352 private boolean mRemovingDisplay = false;
353
Bryce Leed1871262017-06-12 14:12:29 -0700354 // {@code false} if this display is in the processing of being created.
355 private boolean mDisplayReady = false;
356
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800357 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700358
Robert Carrb1579c82017-09-05 14:54:47 -0700359 private final SurfaceSession mSession = new SurfaceSession();
360
361 /**
362 * We organize all top-level Surfaces in to the following layers.
363 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800364 * and omitted from Screen-Magnification, for example the strict mode flash or
365 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700366 * {@link #mWindowingLayer} contains everything else.
367 */
368 private SurfaceControl mOverlayLayer;
369
370 /**
371 * See {@link #mOverlayLayer}
372 */
373 private SurfaceControl mWindowingLayer;
374
375 /**
376 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
377 * <p>
378 * For these surfaces currently we use a surface based on the larger of width or height so we
379 * don't have to resize when rotating the display.
380 */
381 private int mSurfaceSize;
382
383 /**
384 * A list of surfaces to be destroyed after {@link #mPendingTransaction} is applied.
385 */
386 private final ArrayList<SurfaceControl> mPendingDestroyingSurfaces = new ArrayList<>();
387
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100388 /** Temporary float array to retrieve 3x3 matrix values. */
389 private final float[] mTmpFloats = new float[9];
390
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800391 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
392 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800393 final AppWindowToken atoken = w.mAppToken;
394 if (winAnimator.mDrawState == READY_TO_SHOW) {
395 if (atoken == null || atoken.allDrawn) {
396 if (w.performShowLocked()) {
397 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
398 if (DEBUG_LAYOUT_REPEATS) {
399 mService.mWindowPlacerLocked.debugLayoutRepeats(
400 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
401 }
402 }
403 }
404 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800405 };
406
407 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
408 final WindowStateAnimator winAnimator = w.mWinAnimator;
409 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
410 return;
411 }
412
413 final int flags = w.mAttrs.flags;
414
415 // If this window is animating, make a note that we have an animating window and take
416 // care of a request to run a detached wallpaper animation.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100417 if (winAnimator.isAnimationSet()) {
418 final AnimationAdapter anim = w.getAnimation();
419 if (anim != null) {
420 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && anim.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800421 mTmpWindow = w;
422 }
Jorim Jaggia5e10572017-11-15 14:36:26 +0100423 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800424 if (color != 0) {
425 final TaskStack stack = w.getStack();
426 if (stack != null) {
427 stack.setAnimationBackground(winAnimator, color);
428 }
429 }
430 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800431 }
432
433 // If this window's app token is running a detached wallpaper animation, make a note so
434 // we can ensure the wallpaper is displayed behind it.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200435 final AppWindowToken atoken = winAnimator.mWin.mAppToken;
436 final AnimationAdapter animation = atoken != null ? atoken.getAnimation() : null;
437 if (animation != null) {
438 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && animation.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800439 mTmpWindow = w;
440 }
441
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200442 final int color = animation.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800443 if (color != 0) {
444 final TaskStack stack = w.getStack();
445 if (stack != null) {
446 stack.setAnimationBackground(winAnimator, color);
447 }
448 }
449 }
450 };
451
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800452 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
453 final int lostFocusUid = mTmpWindow.mOwnerUid;
454 final Handler handler = mService.mH;
455 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
456 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
457 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
458 w.mAttrs.hideTimeoutMilliseconds);
459 }
460 }
461 };
462
463 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
464 final AppWindowToken focusedApp = mService.mFocusedApp;
465 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
466 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
467
468 if (!w.canReceiveKeys()) {
469 return false;
470 }
471
472 final AppWindowToken wtoken = w.mAppToken;
473
474 // If this window's application has been removed, just skip it.
475 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
476 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
477 + (wtoken.removed ? "removed" : "sendingToBottom"));
478 return false;
479 }
480
481 if (focusedApp == null) {
482 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
483 + " using new focus @ " + w);
484 mTmpWindow = w;
485 return true;
486 }
487
488 if (!focusedApp.windowsAreFocusable()) {
489 // Current focused app windows aren't focusable...
490 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
491 + " focusable using new focus @ " + w);
492 mTmpWindow = w;
493 return true;
494 }
495
496 // Descend through all of the app tokens and find the first that either matches
497 // win.mAppToken (return win) or mFocusedApp (return null).
498 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
499 if (focusedApp.compareTo(wtoken) > 0) {
500 // App stack below focused app stack. No focus for you!!!
501 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
502 "findFocusedWindow: Reached focused app=" + focusedApp);
503 mTmpWindow = null;
504 return true;
505 }
506 }
507
508 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
509 mTmpWindow = w;
510 return true;
511 };
512
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800513 private final Consumer<WindowState> mPerformLayout = w -> {
514 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
515 // wasting time and funky changes while a window is animating away.
516 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
517 || w.isGoneForLayoutLw();
518
519 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
520 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
521 + " mLayoutAttached=" + w.mLayoutAttached
522 + " screen changed=" + w.isConfigChanged());
523 final AppWindowToken atoken = w.mAppToken;
524 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200525 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800526 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
527 + " parentHidden=" + w.isParentWindowHidden());
528 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200529 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800530 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
531 + " parentHidden=" + w.isParentWindowHidden());
532 }
533
534 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
535 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
536 // since that means "perform layout as normal, just don't display").
537 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
538 || ((w.isConfigChanged() || w.setReportResizeHints())
539 && !w.isGoneForLayoutLw() &&
540 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
541 (w.mHasSurface && w.mAppToken != null &&
542 w.mAppToken.layoutConfigChanges)))) {
543 if (!w.mLayoutAttached) {
544 if (mTmpInitial) {
545 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
546 w.mContentChanged = false;
547 }
548 if (w.mAttrs.type == TYPE_DREAM) {
549 // Don't layout windows behind a dream, so that if it does stuff like hide
550 // the status bar we won't get a bad transition when it goes away.
551 mTmpWindow = w;
552 }
553 w.mLayoutNeeded = false;
554 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200555 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000556 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800557 w.mLayoutSeq = mService.mLayoutSeq;
558
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200559 // If this is the first layout, we need to initialize the last inset values as
560 // otherwise we'd immediately cause an unnecessary resize.
561 if (firstLayout) {
562 w.updateLastInsetValues();
563 }
564
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800565 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
566 + " mContainingFrame=" + w.mContainingFrame
567 + " mDisplayFrame=" + w.mDisplayFrame);
568 }
569 }
570 };
571
572 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
573 if (w.mLayoutAttached) {
574 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
575 + " mViewVisibility=" + w.mViewVisibility
576 + " mRelayoutCalled=" + w.mRelayoutCalled);
577 // If this view is GONE, then skip it -- keep the current frame, and let the caller
578 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
579 // windows, since that means "perform layout as normal, just don't display").
580 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
581 return;
582 }
583 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
584 || w.mLayoutNeeded) {
585 if (mTmpInitial) {
586 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
587 w.mContentChanged = false;
588 }
589 w.mLayoutNeeded = false;
590 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000591 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800592 w.mLayoutSeq = mService.mLayoutSeq;
593 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
594 + " mContainingFrame=" + w.mContainingFrame
595 + " mDisplayFrame=" + w.mDisplayFrame);
596 }
597 } else if (w.mAttrs.type == TYPE_DREAM) {
598 // Don't layout windows behind a dream, so that if it does stuff like hide the
599 // status bar we won't get a bad transition when it goes away.
600 mTmpWindow = mTmpWindow2;
601 }
602 };
603
604 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
605 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
606 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
607 return w.canBeImeTarget();
608 };
609
610 private final Consumer<WindowState> mApplyPostLayoutPolicy =
611 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
612 mService.mInputMethodTarget);
613
614 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
615 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
616 final boolean obscuredChanged = w.mObscured !=
617 mTmpApplySurfaceChangesTransactionState.obscured;
618 final RootWindowContainer root = mService.mRoot;
619 // Only used if default window
620 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
621
622 // Update effect.
623 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
624 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
625 final boolean isDisplayed = w.isDisplayedLw();
626
627 if (isDisplayed && w.isObscuringDisplay()) {
628 // This window completely covers everything behind it, so we want to leave all
629 // of them as undimmed (for performance reasons).
630 root.mObscuringWindow = w;
631 mTmpApplySurfaceChangesTransactionState.obscured = true;
632 }
633
634 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
635 root.handleNotObscuredLocked(w,
636 mTmpApplySurfaceChangesTransactionState.obscured,
637 mTmpApplySurfaceChangesTransactionState.syswin);
638
639 if (w.mHasSurface && isDisplayed) {
640 final int type = w.mAttrs.type;
641 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
642 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
643 mTmpApplySurfaceChangesTransactionState.syswin = true;
644 }
645 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
646 && w.mAttrs.preferredRefreshRate != 0) {
647 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
648 = w.mAttrs.preferredRefreshRate;
649 }
650 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
651 && w.mAttrs.preferredDisplayModeId != 0) {
652 mTmpApplySurfaceChangesTransactionState.preferredModeId
653 = w.mAttrs.preferredDisplayModeId;
654 }
655 }
656 }
657
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800658 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
659 && mWallpaperController.isWallpaperTarget(w)) {
660 // This is the wallpaper target and its obscured state changed... make sure the
661 // current wallpaper's visibility has been updated accordingly.
662 mWallpaperController.updateWallpaperVisibility();
663 }
664
Jorim Jaggi6b0f8462018-01-05 17:23:47 +0100665 w.handleWindowMovedIfNeeded(mPendingTransaction);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800666
667 final WindowStateAnimator winAnimator = w.mWinAnimator;
668
669 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
670 w.mContentChanged = false;
671
672 // Moved from updateWindowsAndWallpaperLocked().
673 if (w.mHasSurface) {
674 // Take care of the window being ready to display.
675 final boolean committed = winAnimator.commitFinishDrawingLocked();
676 if (isDefaultDisplay && committed) {
677 if (w.mAttrs.type == TYPE_DREAM) {
678 // HACK: When a dream is shown, it may at that point hide the lock screen.
679 // So we need to redo the layout to let the phone window manager make this
680 // happen.
681 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
682 if (DEBUG_LAYOUT_REPEATS) {
683 surfacePlacer.debugLayoutRepeats(
684 "dream and commitFinishDrawingLocked true",
685 pendingLayoutChanges);
686 }
687 }
688 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
689 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
690 "First draw done in potential wallpaper target " + w);
691 root.mWallpaperMayChange = true;
692 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
693 if (DEBUG_LAYOUT_REPEATS) {
694 surfacePlacer.debugLayoutRepeats(
695 "wallpaper and commitFinishDrawingLocked true",
696 pendingLayoutChanges);
697 }
698 }
699 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800700 }
701
702 final AppWindowToken atoken = w.mAppToken;
703 if (atoken != null) {
Adrian Roos4d18a2e2017-12-19 19:08:05 +0100704 atoken.updateLetterbox(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800705 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
706 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
707 mTmpUpdateAllDrawn.add(atoken);
708 }
709 }
710
711 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
712 && w.isDisplayedLw()) {
713 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
714 }
715
716 w.updateResizingWindowIfNeeded();
717 };
718
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800719 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800720 * Create new {@link DisplayContent} instance, add itself to the root window container and
721 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700722 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800723 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700724 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
725 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700726 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700727 DisplayContent(Display display, WindowManagerService service,
Robert Carrb1579c82017-09-05 14:54:47 -0700728 WallpaperController wallpaperController) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100729 super(service);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800730 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
731 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
732 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
733 + " new=" + display);
734 }
735
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700736 mDisplay = display;
737 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700738 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700739 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700740 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700741 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000742 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700743 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800744 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700745 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700746
Robert Carrb1579c82017-09-05 14:54:47 -0700747 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth);
748
749 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
750 .setSize(mSurfaceSize, mSurfaceSize)
751 .setOpaque(true);
752 mWindowingLayer = b.setName("Display Root").build();
753 mOverlayLayer = b.setName("Display Overlays").build();
754
Robert Carrf59b8dd2017-10-02 18:58:36 -0700755 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700756 .setLayerStack(mWindowingLayer, mDisplayId)
757 .show(mWindowingLayer)
758 .setLayer(mOverlayLayer, 1)
759 .setLayerStack(mOverlayLayer, mDisplayId)
760 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700761 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700762
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700763 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700764 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700765 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700766 super.addChild(mAboveAppWindowsContainers, null);
767 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800768
769 // Add itself as a child to the root container.
770 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700771
772 // TODO(b/62541591): evaluate whether this is the best spot to declare the
773 // {@link DisplayContent} ready for use.
774 mDisplayReady = true;
775 }
776
777 boolean isReady() {
778 // The display is ready when the system and the individual display are both ready.
779 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700780 }
781
782 int getDisplayId() {
783 return mDisplayId;
784 }
785
Wale Ogunwale02319a62016-09-26 15:21:22 -0700786 WindowToken getWindowToken(IBinder binder) {
787 return mTokenMap.get(binder);
788 }
789
790 AppWindowToken getAppWindowToken(IBinder binder) {
791 final WindowToken token = getWindowToken(binder);
792 if (token == null) {
793 return null;
794 }
795 return token.asAppWindowToken();
796 }
797
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700798 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700799 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
800 if (dc != null) {
801 // We currently don't support adding a window token to the display if the display
802 // already has the binder mapped to another token. If there is a use case for supporting
803 // this moving forward we will either need to merge the WindowTokens some how or have
804 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700805 throw new IllegalArgumentException("Can't map token=" + token + " to display="
806 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700807 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700808 if (binder == null) {
809 throw new IllegalArgumentException("Can't map token=" + token + " to display="
810 + getName() + " binder is null");
811 }
812 if (token == null) {
813 throw new IllegalArgumentException("Can't map null token to display="
814 + getName() + " binder=" + binder);
815 }
816
Wale Ogunwale02319a62016-09-26 15:21:22 -0700817 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700818
819 if (token.asAppWindowToken() == null) {
820 // Add non-app token to container hierarchy on the display. App tokens are added through
821 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700822 switch (token.windowType) {
823 case TYPE_WALLPAPER:
824 mBelowAppWindowsContainers.addChild(token);
825 break;
826 case TYPE_INPUT_METHOD:
827 case TYPE_INPUT_METHOD_DIALOG:
828 mImeWindowsContainers.addChild(token);
829 break;
830 default:
831 mAboveAppWindowsContainers.addChild(token);
832 break;
833 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700834 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700835 }
836
837 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700838 final WindowToken token = mTokenMap.remove(binder);
839 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800840 token.setExiting();
841 }
842 return token;
843 }
844
845 /** Changes the display the input window token is housed on to this one. */
846 void reParentWindowToken(WindowToken token) {
847 final DisplayContent prevDc = token.getDisplayContent();
848 if (prevDc == this) {
849 return;
850 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800851 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
852 && token.asAppWindowToken() == null) {
853 // Removed the token from the map, but made sure it's not an app token before removing
854 // from parent.
855 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700856 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800857
858 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700859 }
860
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700861 void removeAppToken(IBinder binder) {
862 final WindowToken token = removeWindowToken(binder);
863 if (token == null) {
864 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
865 return;
866 }
867
868 final AppWindowToken appToken = token.asAppWindowToken();
869
870 if (appToken == null) {
871 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
872 return;
873 }
874
875 appToken.onRemovedFromDisplay();
876 }
877
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700878 Display getDisplay() {
879 return mDisplay;
880 }
881
Craig Mautner59c00972012-07-30 12:10:24 -0700882 DisplayInfo getDisplayInfo() {
883 return mDisplayInfo;
884 }
885
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700886 DisplayMetrics getDisplayMetrics() {
887 return mDisplayMetrics;
888 }
889
Andrii Kulian8ee72852017-03-10 10:36:45 -0800890 int getRotation() {
891 return mRotation;
892 }
893
894 void setRotation(int newRotation) {
895 mRotation = newRotation;
896 }
897
898 int getLastOrientation() {
899 return mLastOrientation;
900 }
901
902 void setLastOrientation(int orientation) {
903 mLastOrientation = orientation;
904 }
905
906 boolean getAltOrientation() {
907 return mAltOrientation;
908 }
909
910 void setAltOrientation(boolean altOrientation) {
911 mAltOrientation = altOrientation;
912 }
913
914 int getLastWindowForcedOrientation() {
915 return mLastWindowForcedOrientation;
916 }
917
Andrii Kulian06d07d62017-03-14 11:11:47 -0700918 /**
919 * Update rotation of the display.
920 *
921 * Returns true if the rotation has been changed. In this case YOU MUST CALL
922 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
923 */
924 boolean updateRotationUnchecked(boolean inTransaction) {
925 if (mService.mDeferredRotationPauseCount > 0) {
926 // Rotation updates have been paused temporarily. Defer the update until
927 // updates have been resumed.
928 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
929 return false;
930 }
931
932 ScreenRotationAnimation screenRotationAnimation =
933 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
934 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
935 // Rotation updates cannot be performed while the previous rotation change
936 // animation is still in progress. Skip this update. We will try updating
937 // again after the animation is finished and the display is unfrozen.
938 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
939 return false;
940 }
941 if (mService.mDisplayFrozen) {
942 // Even if the screen rotation animation has finished (e.g. isAnimating
943 // returns false), there is still some time where we haven't yet unfrozen
944 // the display. We also need to abort rotation here.
945 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
946 "Deferring rotation, still finishing previous rotation");
947 return false;
948 }
949
950 if (!mService.mDisplayEnabled) {
951 // No point choosing a rotation if the display is not enabled.
952 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
953 return false;
954 }
955
956 final int oldRotation = mRotation;
957 final int lastOrientation = mLastOrientation;
958 final boolean oldAltOrientation = mAltOrientation;
959 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr0e007272017-10-02 13:00:55 -0700960 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
Robert Carr897215d2017-03-29 12:25:50 -0700961 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700962
Robert Carr0e007272017-10-02 13:00:55 -0700963 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700964 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
965 if (seamlessRotated != null) {
966 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
967 // to complete (that is, waiting for windows to redraw). It's tempting to check
968 // w.mSeamlessRotationCount but that could be incorrect in the case of
969 // window-removal.
970 return false;
971 }
Robert Carr0e007272017-10-02 13:00:55 -0700972
973 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700974 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -0700975 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -0700976 mayRotateSeamlessly = false;
977 }
978 for (int i = 0; i < mService.mSessions.size(); i++) {
979 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
980 mayRotateSeamlessly = false;
981 break;
982 }
983 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700984 }
Robert Carr0e007272017-10-02 13:00:55 -0700985 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700986
987 // TODO: Implement forced rotation changes.
988 // Set mAltOrientation to indicate that the application is receiving
989 // an orientation that has different metrics than it expected.
990 // eg. Portrait instead of Landscape.
991
992 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
993 lastOrientation, rotation);
994
995 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
996 + ", got rotation " + rotation + " which has "
997 + (altOrientation ? "incompatible" : "compatible") + " metrics");
998
999 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1000 // No change.
1001 return false;
1002 }
1003
1004 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
1005 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1006 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1007
1008 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1009 mService.mWaitingForConfig = true;
1010 }
1011
1012 mRotation = rotation;
1013 mAltOrientation = altOrientation;
1014 if (isDefaultDisplay) {
1015 mService.mPolicy.setRotationLw(rotation);
1016 }
1017
1018 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1019 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1020 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1021 WINDOW_FREEZE_TIMEOUT_DURATION);
1022
1023 setLayoutNeeded();
1024 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001025 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001026
1027 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001028 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001029 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1030 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1031 mDisplayId);
1032 } else {
1033 // The screen rotation animation uses a screenshot to freeze the screen
1034 // while windows resize underneath.
1035 // When we are rotating seamlessly, we allow the elements to transition
1036 // to their rotated state independently and without a freeze required.
1037 screenRotationAnimation = null;
1038
1039 // We have to reset this in case a window was removed before it
1040 // finished seamless rotation.
1041 mService.mSeamlessRotationCount = 0;
1042 }
1043
1044 // We need to update our screen size information to match the new rotation. If the rotation
1045 // has actually changed then this method will return true and, according to the comment at
1046 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1047 // By updating the Display info here it will be available to
1048 // #computeScreenConfiguration() later.
1049 updateDisplayAndOrientation(getConfiguration().uiMode);
1050
1051 if (!inTransaction) {
1052 if (SHOW_TRANSACTIONS) {
1053 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1054 }
1055 mService.openSurfaceTransaction();
1056 }
1057 try {
1058 // NOTE: We disable the rotation in the emulator because
1059 // it doesn't support hardware OpenGL emulation yet.
1060 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1061 && screenRotationAnimation.hasScreenshot()) {
Robert Carrb1579c82017-09-05 14:54:47 -07001062 if (screenRotationAnimation.setRotationInTransaction(rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001063 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1064 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1065 mService.scheduleAnimationLocked();
1066 }
1067 }
1068
1069 if (rotateSeamlessly) {
1070 forAllWindows(w -> {
1071 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1072 }, true /* traverseTopToBottom */);
1073 }
1074
1075 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1076 } finally {
1077 if (!inTransaction) {
Adrian Roos111aff92017-09-27 18:11:46 +02001078 mService.closeSurfaceTransaction("setRotationUnchecked");
Andrii Kulian06d07d62017-03-14 11:11:47 -07001079 if (SHOW_LIGHT_TRANSACTIONS) {
1080 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1081 }
1082 }
1083 }
1084
1085 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001086 if (w.mHasSurface && !rotateSeamlessly) {
1087 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001088 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001089 mService.mRoot.mOrientationChangeComplete = false;
1090 w.mLastFreezeDuration = 0;
1091 }
1092 w.mReportOrientationChanged = true;
1093 }, true /* traverseTopToBottom */);
1094
1095 if (rotateSeamlessly) {
1096 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1097 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1098 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1099 }
1100
1101 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1102 final WindowManagerService.RotationWatcher rotationWatcher
1103 = mService.mRotationWatchers.get(i);
1104 if (rotationWatcher.mDisplayId == mDisplayId) {
1105 try {
1106 rotationWatcher.mWatcher.onRotationChanged(rotation);
1107 } catch (RemoteException e) {
1108 // Ignore
1109 }
1110 }
1111 }
1112
1113 // TODO (multi-display): Magnification is supported only for the default display.
1114 // Announce rotation only if we will not animate as we already have the
1115 // windows in final state. Otherwise, we make this call at the rotation end.
1116 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1117 && isDefaultDisplay) {
1118 mService.mAccessibilityController.onRotationChangedLocked(this);
1119 }
1120
1121 return true;
1122 }
1123
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001124 void configureDisplayPolicy() {
1125 mService.mPolicy.setInitialDisplaySize(getDisplay(),
1126 mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1127
1128 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
1129 }
1130
Andrii Kulian06d07d62017-03-14 11:11:47 -07001131 /**
1132 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1133 * changed.
1134 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1135 */
1136 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1137 // Use the effective "visual" dimensions based on current rotation
1138 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1139 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1140 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1141 int dw = realdw;
1142 int dh = realdh;
1143
1144 if (mAltOrientation) {
1145 if (realdw > realdh) {
1146 // Turn landscape into portrait.
1147 int maxw = (int)(realdh/1.3f);
1148 if (maxw < realdw) {
1149 dw = maxw;
1150 }
1151 } else {
1152 // Turn portrait into landscape.
1153 int maxh = (int)(realdw/1.3f);
1154 if (maxh < realdh) {
1155 dh = maxh;
1156 }
1157 }
1158 }
1159
1160 // Update application display metrics.
1161 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1162 mDisplayId);
1163 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1164 mDisplayId);
1165 mDisplayInfo.rotation = mRotation;
1166 mDisplayInfo.logicalWidth = dw;
1167 mDisplayInfo.logicalHeight = dh;
1168 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1169 mDisplayInfo.appWidth = appWidth;
1170 mDisplayInfo.appHeight = appHeight;
1171 if (isDefaultDisplay) {
1172 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1173 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1174 }
Adrian Roos1cf585052018-01-03 18:43:27 +01001175 mDisplayInfo.displayCutout = calculateDisplayCutoutForCurrentRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001176 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1177 if (mDisplayScalingDisabled) {
1178 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1179 } else {
1180 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1181 }
1182
1183 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1184 mDisplayInfo);
1185
1186 mBaseDisplayRect.set(0, 0, dw, dh);
1187
1188 if (isDefaultDisplay) {
1189 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1190 mCompatDisplayMetrics);
1191 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001192
1193 updateBounds();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001194 return mDisplayInfo;
1195 }
1196
Adrian Roos1cf585052018-01-03 18:43:27 +01001197 DisplayCutout calculateDisplayCutoutForCurrentRotation() {
1198 final DisplayCutout cutout = mInitialDisplayCutout;
1199 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT || mRotation == ROTATION_0) {
1200 return cutout;
1201 }
1202 final Path bounds = cutout.getBounds().getBoundaryPath();
1203 transformPhysicalToLogicalCoordinates(mRotation, mInitialDisplayWidth,
1204 mInitialDisplayHeight, mTmpMatrix);
1205 bounds.transform(mTmpMatrix);
1206 return DisplayCutout.fromBounds(bounds);
1207 }
1208
Andrii Kulian06d07d62017-03-14 11:11:47 -07001209 /**
1210 * Compute display configuration based on display properties and policy settings.
1211 * Do not call if mDisplayReady == false.
1212 */
1213 void computeScreenConfiguration(Configuration config) {
1214 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1215
1216 final int dw = displayInfo.logicalWidth;
1217 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001218 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1219 // TODO: Probably best to set this based on some setting in the display content object,
1220 // so the display can be configured for things like fullscreen.
1221 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001222
Andrii Kulian06d07d62017-03-14 11:11:47 -07001223 config.screenWidthDp =
1224 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1225 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1226 config.screenHeightDp =
1227 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1228 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001229
1230 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1231 final int leftInset = mTmpRect.left;
1232 final int topInset = mTmpRect.top;
1233 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001234 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1235 leftInset + displayInfo.appWidth /* right */,
1236 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001237 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1238 || displayInfo.rotation == Surface.ROTATION_270);
1239
1240 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1241 mDisplayMetrics.density, config);
1242
1243 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1244 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1245 ? Configuration.SCREENLAYOUT_ROUND_YES
1246 : Configuration.SCREENLAYOUT_ROUND_NO);
1247
1248 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1249 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1250 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1251 dh, mDisplayId);
1252 config.densityDpi = displayInfo.logicalDensityDpi;
1253
1254 config.colorMode =
1255 (displayInfo.isHdr()
1256 ? Configuration.COLOR_MODE_HDR_YES
1257 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001258 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001259 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1260 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1261
1262 // Update the configuration based on available input devices, lid switch,
1263 // and platform configuration.
1264 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1265 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1266 config.navigation = Configuration.NAVIGATION_NONAV;
1267
1268 int keyboardPresence = 0;
1269 int navigationPresence = 0;
1270 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1271 final int len = devices != null ? devices.length : 0;
1272 for (int i = 0; i < len; i++) {
1273 InputDevice device = devices[i];
1274 if (!device.isVirtual()) {
1275 final int sources = device.getSources();
1276 final int presenceFlag = device.isExternal() ?
1277 WindowManagerPolicy.PRESENCE_EXTERNAL :
1278 WindowManagerPolicy.PRESENCE_INTERNAL;
1279
1280 // TODO(multi-display): Configure on per-display basis.
1281 if (mService.mIsTouchDevice) {
1282 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1283 InputDevice.SOURCE_TOUCHSCREEN) {
1284 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1285 }
1286 } else {
1287 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1288 }
1289
1290 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1291 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1292 navigationPresence |= presenceFlag;
1293 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1294 && config.navigation == Configuration.NAVIGATION_NONAV) {
1295 config.navigation = Configuration.NAVIGATION_DPAD;
1296 navigationPresence |= presenceFlag;
1297 }
1298
1299 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1300 config.keyboard = Configuration.KEYBOARD_QWERTY;
1301 keyboardPresence |= presenceFlag;
1302 }
1303 }
1304 }
1305
1306 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1307 config.navigation = Configuration.NAVIGATION_DPAD;
1308 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1309 }
1310
1311 // Determine whether a hard keyboard is available and enabled.
1312 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1313 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1314 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1315 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1316 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1317 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1318 }
1319
1320 // Let the policy update hidden states.
1321 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1322 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1323 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1324 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1325 }
1326
1327 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1328 int displayId) {
1329 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1330 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1331 final int unrotDw, unrotDh;
1332 if (rotated) {
1333 unrotDw = dh;
1334 unrotDh = dw;
1335 } else {
1336 unrotDw = dw;
1337 unrotDh = dh;
1338 }
1339 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1340 displayId);
1341 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1342 displayId);
1343 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1344 displayId);
1345 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1346 displayId);
1347 return sw;
1348 }
1349
1350 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1351 DisplayMetrics dm, int dw, int dh, int displayId) {
1352 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1353 displayId);
1354 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1355 uiMode, displayId);
1356 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1357 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1358 if (curSize == 0 || size < curSize) {
1359 curSize = size;
1360 }
1361 return curSize;
1362 }
1363
1364 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1365 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1366
1367 // We need to determine the smallest width that will occur under normal
1368 // operation. To this, start with the base screen size and compute the
1369 // width under the different possible rotations. We need to un-rotate
1370 // the current screen dimensions before doing this.
1371 int unrotDw, unrotDh;
1372 if (rotated) {
1373 unrotDw = dh;
1374 unrotDh = dw;
1375 } else {
1376 unrotDw = dw;
1377 unrotDh = dh;
1378 }
1379 displayInfo.smallestNominalAppWidth = 1<<30;
1380 displayInfo.smallestNominalAppHeight = 1<<30;
1381 displayInfo.largestNominalAppWidth = 0;
1382 displayInfo.largestNominalAppHeight = 0;
1383 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1384 unrotDh);
1385 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1386 unrotDw);
1387 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1388 unrotDh);
1389 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1390 unrotDw);
1391 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1392 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1393 displayId);
1394 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1395 displayId);
1396 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1397 displayId);
1398 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1399 displayId);
1400 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1401 outConfig.screenLayout = sl;
1402 }
1403
1404 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1405 int uiMode, int displayId) {
1406 // Get the app screen size at this rotation.
1407 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1408 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1409
1410 // Compute the screen layout size class for this rotation.
1411 int longSize = w;
1412 int shortSize = h;
1413 if (longSize < shortSize) {
1414 int tmp = longSize;
1415 longSize = shortSize;
1416 shortSize = tmp;
1417 }
1418 longSize = (int)(longSize/density);
1419 shortSize = (int)(shortSize/density);
1420 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1421 }
1422
1423 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1424 int uiMode, int dw, int dh) {
1425 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1426 displayId);
1427 if (width < displayInfo.smallestNominalAppWidth) {
1428 displayInfo.smallestNominalAppWidth = width;
1429 }
1430 if (width > displayInfo.largestNominalAppWidth) {
1431 displayInfo.largestNominalAppWidth = width;
1432 }
1433 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1434 displayId);
1435 if (height < displayInfo.smallestNominalAppHeight) {
1436 displayInfo.smallestNominalAppHeight = height;
1437 }
1438 if (height > displayInfo.largestNominalAppHeight) {
1439 displayInfo.largestNominalAppHeight = height;
1440 }
1441 }
1442
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001443 DockedStackDividerController getDockedDividerController() {
1444 return mDividerControllerLocked;
1445 }
1446
Winson Chung655332c2016-10-31 13:14:28 -07001447 PinnedStackController getPinnedStackController() {
1448 return mPinnedStackControllerLocked;
1449 }
1450
Jeff Browna506a6e2013-06-04 00:02:38 -07001451 /**
1452 * Returns true if the specified UID has access to this display.
1453 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001454 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001455 return mDisplay.hasAccess(uid);
1456 }
1457
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001458 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001459 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001460 }
1461
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001462 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001463 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001464 }
1465
Wale Ogunwale61911492017-10-11 08:50:50 -07001466 /**
1467 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1468 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001469 TaskStack getSplitScreenPrimaryStack() {
1470 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001471 return (stack != null && stack.isVisible()) ? stack : null;
1472 }
1473
1474 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001475 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001476 * not visible.
1477 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001478 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1479 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001480 }
1481
1482 TaskStack getPinnedStack() {
1483 return mTaskStackContainers.getPinnedStack();
1484 }
1485
1486 private boolean hasPinnedStack() {
1487 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001488 }
1489
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001490 /**
1491 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1492 * Null is no compatible stack on the display.
1493 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001494 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001495 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1496 }
1497
1498 /**
1499 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1500 * activity type. Null is no compatible stack on the display.
1501 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001502 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001503 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001504 }
1505
Bryce Lee48f4b572017-04-10 10:54:15 -07001506 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001507 TaskStack getTopStack() {
1508 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001509 }
1510
Wale Ogunwale61911492017-10-11 08:50:50 -07001511 void onStackWindowingModeChanged(TaskStack stack) {
1512 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001513 }
1514
Andrii Kulian441e4492016-09-29 15:25:00 -07001515 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001516 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001517 super.onConfigurationChanged(newParentConfig);
1518
Andrii Kulian3a507b52016-09-19 18:14:12 -07001519 // The display size information is heavily dependent on the resources in the current
1520 // configuration, so we need to reconfigure it every time the configuration changes.
1521 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1522 mService.reconfigureDisplayLocked(this);
1523
Bryce Leef3c6a472017-11-14 14:53:06 -08001524 final DockedStackDividerController dividerController = getDockedDividerController();
1525
1526 if (dividerController != null) {
1527 getDockedDividerController().onConfigurationChanged();
1528 }
1529
1530 final PinnedStackController pinnedStackController = getPinnedStackController();
1531
1532 if (pinnedStackController != null) {
1533 getPinnedStackController().onConfigurationChanged();
1534 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001535 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001536
Andrii Kulian441e4492016-09-29 15:25:00 -07001537 /**
1538 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1539 * bounds were updated.
1540 */
1541 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001542 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1543 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001544 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001545 changedStackList.add(stack.mStackId);
1546 }
1547 }
Winson Chung32c566f2017-04-11 18:31:21 -07001548
1549 // If there was no pinned stack, we still need to notify the controller of the display info
1550 // update as a result of the config change. We do this here to consolidate the flow between
1551 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001552 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001553 mPinnedStackControllerLocked.onDisplayInfoChanged();
1554 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001555 }
1556
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001557 @Override
1558 boolean fillsParent() {
1559 return true;
1560 }
1561
1562 @Override
1563 boolean isVisible() {
1564 return true;
1565 }
1566
1567 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001568 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001569 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001570 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001571 }
1572
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001573 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001574 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1575 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1576 // target, or here in order if there isn't an IME target.
1577 if (traverseTopToBottom) {
1578 for (int i = mChildren.size() - 1; i >= 0; --i) {
1579 final DisplayChildWindowContainer child = mChildren.get(i);
1580 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1581 // In this case the Ime windows will be processed above their target so we skip
1582 // here.
1583 continue;
1584 }
1585 if (child.forAllWindows(callback, traverseTopToBottom)) {
1586 return true;
1587 }
1588 }
1589 } else {
1590 final int count = mChildren.size();
1591 for (int i = 0; i < count; i++) {
1592 final DisplayChildWindowContainer child = mChildren.get(i);
1593 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1594 // In this case the Ime windows will be processed above their target so we skip
1595 // here.
1596 continue;
1597 }
1598 if (child.forAllWindows(callback, traverseTopToBottom)) {
1599 return true;
1600 }
1601 }
1602 }
1603 return false;
1604 }
1605
1606 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1607 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1608 }
1609
Wale Ogunwale399c8692017-05-08 14:22:42 -07001610 @Override
1611 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001612 final WindowManagerPolicy policy = mService.mPolicy;
1613
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001614 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001615 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001616 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001617 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001618 // If the display is frozen, some activities may be in the middle of restarting, and
1619 // thus have removed their old window. If the window has the flag to hide the lock
1620 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1621 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001622 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001623 } else if (policy.isKeyguardLocked()) {
1624 // Use the last orientation the while the display is frozen with the keyguard
1625 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1626 // window. We don't want to check the show when locked window directly though as
1627 // things aren't stable while the display is frozen, for example the window could be
1628 // momentarily unavailable due to activity relaunch.
1629 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001630 + "return " + mLastOrientation);
1631 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001632 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001633 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001634 final int orientation = mAboveAppWindowsContainers.getOrientation();
1635 if (orientation != SCREEN_ORIENTATION_UNSET) {
1636 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001637 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001638 }
1639
Wale Ogunwale399c8692017-05-08 14:22:42 -07001640 // Top system windows are not requesting an orientation. Start searching from apps.
1641 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001642 }
1643
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001644 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001645 // Check if display metrics changed and update base values if needed.
1646 updateBaseDisplayMetricsIfNeeded();
1647
Craig Mautner722285e2012-09-07 13:55:58 -07001648 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001649 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001650
Wale Ogunwale61911492017-10-11 08:50:50 -07001651 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1652 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001653 }
Craig Mautner722285e2012-09-07 13:55:58 -07001654 }
1655
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001656 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001657 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1658 if (displayManagerInternal != null) {
1659 // Bootstrap the default logical display from the display manager.
1660 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1661 if (newDisplayInfo != null) {
1662 mDisplayInfo.copyFrom(newDisplayInfo);
1663 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001664 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001665
Andrii Kuliancd097992017-03-23 18:31:59 -07001666 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1667 mDisplayInfo.logicalDensityDpi);
1668 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1669 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1670 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001671 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001672 }
1673
Andrii Kuliancd097992017-03-23 18:31:59 -07001674 /**
1675 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1676 * values.
1677 */
1678 private void updateBaseDisplayMetricsIfNeeded() {
1679 // Get real display metrics without overrides from WM.
1680 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1681 final int orientation = mDisplayInfo.rotation;
1682 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1683 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1684 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1685 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001686 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001687
1688 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1689 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01001690 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
1691 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07001692
1693 if (displayMetricsChanged) {
1694 // Check if display size or density is forced.
1695 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1696 || mBaseDisplayHeight != mInitialDisplayHeight;
1697 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1698
1699 // If there is an override set for base values - use it, otherwise use new values.
1700 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1701 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1702 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1703
1704 // Real display metrics changed, so we should also update initial values.
1705 mInitialDisplayWidth = newWidth;
1706 mInitialDisplayHeight = newHeight;
1707 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01001708 mInitialDisplayCutout = newCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001709 mService.reconfigureDisplayLocked(this);
1710 }
1711 }
1712
Bryce Lee27cec322017-03-21 09:41:37 -07001713 /** Sets the maximum width the screen resolution can be */
1714 void setMaxUiWidth(int width) {
1715 if (DEBUG_DISPLAY) {
1716 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1717 }
1718
1719 mMaxUiWidth = width;
1720
1721 // Update existing metrics.
1722 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1723 }
1724
1725 /** Update base (override) display metrics. */
1726 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1727 mBaseDisplayWidth = baseWidth;
1728 mBaseDisplayHeight = baseHeight;
1729 mBaseDisplayDensity = baseDensity;
1730
1731 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1732 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1733 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1734 mBaseDisplayWidth = mMaxUiWidth;
1735
1736 if (DEBUG_DISPLAY) {
1737 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1738 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1739 + " on display:" + getDisplayId());
1740 }
1741 }
1742
1743 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08001744
1745 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07001746 }
1747
Wale Ogunwaledb506192017-12-08 10:57:32 -08001748 void getStableRect(Rect out) {
1749 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08001750 }
1751
Wale Ogunwale61911492017-10-11 08:50:50 -07001752 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001753 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1754 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001755
Wale Ogunwale61911492017-10-11 08:50:50 -07001756 final TaskStack stack = new TaskStack(mService, stackId, controller);
1757 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001758 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001759 }
1760
Andrii Kulian51c1b672017-04-07 18:39:32 -07001761 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001762 final DisplayContent prevDc = stack.getDisplayContent();
1763 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001764 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1765 + " which is not currently attached to any display");
1766 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001767 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001768 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1769 + " to its current displayId=" + mDisplayId);
1770 }
1771
Wale Ogunwale61911492017-10-11 08:50:50 -07001772 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001773 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001774 }
1775
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001776 @Override
1777 protected void addChild(DisplayChildWindowContainer child,
1778 Comparator<DisplayChildWindowContainer> comparator) {
1779 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1780 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001781
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001782 @Override
1783 protected void addChild(DisplayChildWindowContainer child, int index) {
1784 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1785 }
1786
1787 @Override
1788 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001789 // Only allow removal of direct children from this display if the display is in the process
1790 // of been removed.
1791 if (mRemovingDisplay) {
1792 super.removeChild(child);
1793 return;
1794 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001795 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001796 }
1797
Andrii Kuliand2765632016-12-12 22:26:34 -08001798 @Override
1799 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1800 // Children of the display are statically ordered, so the real intention here is to perform
1801 // the operation on the display and not the static direct children.
1802 getParent().positionChildAt(position, this, includingParents);
1803 }
1804
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001805 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001806 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1807 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001808 final int taskId = stack.taskIdFromPoint(x, y);
1809 if (taskId != -1) {
1810 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001811 }
1812 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001813 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001814 }
1815
Chong Zhang8e89b312015-09-09 15:09:30 -07001816 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001817 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001818 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001819 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001820 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001821 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001822 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07001823 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1824 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001825 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001826 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001827 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001828
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001829 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1830 if (mTmpTaskForResizePointSearchResult.searchDone) {
1831 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001832 }
1833 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001834 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001835 }
1836
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001837 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001838 // The provided task is the task on this display with focus, so if WindowManagerService's
1839 // focused app is not on this display, focusedTask will be null.
1840 if (focusedTask == null) {
1841 mTouchExcludeRegion.setEmpty();
1842 } else {
1843 mTouchExcludeRegion.set(mBaseDisplayRect);
1844 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1845 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07001846 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1847 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001848 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
1849 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08001850 }
1851 // If we removed the focused task above, add it back and only leave its
1852 // outside touch area in the exclusion. TapDectector is not interested in
1853 // any touch inside the focused task itself.
1854 if (!mTmpRect2.isEmpty()) {
1855 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1856 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001857 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001858 final WindowState inputMethod = mService.mInputMethodWindow;
1859 if (inputMethod != null && inputMethod.isVisibleLw()) {
1860 // If the input method is visible and the user is typing, we don't want these touch
1861 // events to be intercepted and used to change focus. This would likely cause a
1862 // disappearance of the input method.
1863 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001864 if (inputMethod.getDisplayId() == mDisplayId) {
1865 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1866 } else {
1867 // IME is on a different display, so we need to update its tap detector.
1868 // TODO(multidisplay): Remove when IME will always appear on same display.
1869 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1870 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001871 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001872 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08001873 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001874 win.getTouchableRegion(mTmpRegion);
1875 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1876 }
Andrii Kulian4b6599e2018-01-15 17:24:08 -08001877 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
1878 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
1879 win.amendTapExcludeRegion(mTouchExcludeRegion);
1880 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001881 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07001882 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001883 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001884 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001885 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1886 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001887 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001888 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001889 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001890 }
1891
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001892 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001893 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001894 super.switchUser();
1895 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001896 }
1897
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001898 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001899 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1900 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001901 }
1902 }
1903
Wale Ogunwale10124582016-09-15 20:25:50 -07001904 @Override
1905 void removeIfPossible() {
1906 if (isAnimating()) {
1907 mDeferredRemoval = true;
1908 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001909 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001910 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001911 }
1912
Wale Ogunwale10124582016-09-15 20:25:50 -07001913 @Override
1914 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001915 mRemovingDisplay = true;
1916 try {
1917 super.removeImmediately();
1918 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07001919 if (mService.canDispatchPointerEvents()) {
1920 if (mTapDetector != null) {
1921 mService.unregisterPointerEventListener(mTapDetector);
1922 }
1923 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
1924 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1925 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001926 }
Jorim Jaggia3844032018-01-03 15:54:43 +01001927 mService.mAnimator.removeDisplayLocked(mDisplayId);
1928
Robert Carrb1579c82017-09-05 14:54:47 -07001929 // The pending transaction won't be applied so we should
1930 // just clean up any surfaces pending destruction.
1931 onPendingTransactionApplied();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001932 } finally {
1933 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001934 }
Craig Mautner95da1082014-02-24 17:54:35 -08001935 }
1936
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001937 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001938 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001939 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001940 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1941
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001942 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001943 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001944 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001945 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001946 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001947 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001948 }
1949
Andrii Kulian0214ed92017-05-16 13:44:05 -07001950 /** @return 'true' if removal of this display content is deferred due to active animation. */
1951 boolean isRemovalDeferred() {
1952 return mDeferredRemoval;
1953 }
1954
Wale Ogunwale10124582016-09-15 20:25:50 -07001955 boolean animateForIme(float interpolatedValue, float animationTarget,
1956 float dividerAnimationTarget) {
1957 boolean updated = false;
1958
Wale Ogunwale61911492017-10-11 08:50:50 -07001959 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1960 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001961 if (stack == null || !stack.isAdjustedForIme()) {
1962 continue;
1963 }
1964
1965 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1966 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1967 updated = true;
1968 } else {
1969 mDividerControllerLocked.mLastAnimationProgress =
1970 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1971 mDividerControllerLocked.mLastDividerProgress =
1972 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1973 updated |= stack.updateAdjustForIme(
1974 mDividerControllerLocked.mLastAnimationProgress,
1975 mDividerControllerLocked.mLastDividerProgress,
1976 false /* force */);
1977 }
1978 if (interpolatedValue >= 1f) {
1979 stack.endImeAdjustAnimation();
1980 }
1981 }
1982
1983 return updated;
1984 }
1985
1986 boolean clearImeAdjustAnimation() {
1987 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07001988 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1989 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001990 if (stack != null && stack.isAdjustedForIme()) {
1991 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1992 changed = true;
1993 }
1994 }
1995 return changed;
1996 }
1997
1998 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001999 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2000 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002001 if (stack.isVisible() && stack.isAdjustedForIme()) {
2002 stack.beginImeAdjustAnimation();
2003 }
2004 }
2005 }
2006
2007 void adjustForImeIfNeeded() {
2008 final WindowState imeWin = mService.mInputMethodWindow;
2009 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2010 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002011 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002012 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2013 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2014 imeTargetStack.getDockSide() : DOCKED_INVALID;
2015 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2016 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2017 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002018 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002019 final boolean imeHeightChanged = imeVisible &&
2020 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2021
2022 // The divider could be adjusted for IME position, or be thinner than usual,
2023 // or both. There are three possible cases:
2024 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2025 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2026 // - If IME is not visible, divider is not moved and is normal width.
2027
2028 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002029 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2030 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002031 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002032 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2033 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002034 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2035 } else {
2036 stack.resetAdjustedForIme(false);
2037 }
2038 }
2039 mDividerControllerLocked.setAdjustedForIme(
2040 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2041 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002042 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2043 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002044 stack.resetAdjustedForIme(!dockVisible);
2045 }
2046 mDividerControllerLocked.setAdjustedForIme(
2047 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2048 }
Winson Chung655332c2016-10-31 13:14:28 -07002049 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002050 }
2051
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002052 /**
2053 * If a window that has an animation specifying a colored background and the current wallpaper
2054 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2055 * suddenly disappear.
2056 */
2057 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002058 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2059 w -> w.mIsWallpaper && w.isVisibleNow());
2060
2061 if (visibleWallpaper != null) {
2062 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002063 }
2064 return winAnimator.mAnimLayer;
2065 }
2066
Wale Ogunwale10124582016-09-15 20:25:50 -07002067 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002068 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2069 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002070 stack.prepareFreezingTaskBounds();
2071 }
2072 }
2073
Wale Ogunwale94744212015-09-21 19:01:47 -07002074 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002075 getBounds(mTmpRect, newRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002076
2077 // Compute a transform matrix to undo the coordinate space transformation,
2078 // and present the window at the same physical position it previously occupied.
2079 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2080 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2081
2082 mTmpRectF.set(bounds);
2083 mTmpMatrix.mapRect(mTmpRectF);
2084 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002085 }
2086
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002087 static int deltaRotation(int oldRotation, int newRotation) {
2088 int delta = newRotation - oldRotation;
2089 if (delta < 0) delta += 4;
2090 return delta;
2091 }
2092
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002093 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002094 Matrix outMatrix) {
2095 // For rotations without Z-ordering we don't need the target rectangle's position.
2096 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2097 displayHeight, outMatrix);
2098 }
2099
2100 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2101 float displayWidth, float displayHeight, Matrix outMatrix) {
2102 switch (rotation) {
2103 case ROTATION_0:
2104 outMatrix.reset();
2105 break;
2106 case ROTATION_270:
2107 outMatrix.setRotate(270, 0, 0);
2108 outMatrix.postTranslate(0, displayHeight);
2109 outMatrix.postTranslate(rectTop, 0);
2110 break;
2111 case ROTATION_180:
2112 outMatrix.reset();
2113 break;
2114 case ROTATION_90:
2115 outMatrix.setRotate(90, 0, 0);
2116 outMatrix.postTranslate(displayWidth, 0);
2117 outMatrix.postTranslate(-rectTop, rectLeft);
2118 break;
2119 }
2120 }
2121
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002122 @CallSuper
2123 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002124 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002125 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002126 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002127 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002128 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2129 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002130 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002131 }
2132 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2133 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002134 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2135 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002136 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002137 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002138 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2139 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002140 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002141 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002142 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2143 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002144 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002145 }
2146 proto.write(DPI, mBaseDisplayDensity);
2147 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002148 proto.write(ROTATION, mRotation);
2149 final ScreenRotationAnimation screenRotationAnimation =
2150 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2151 if (screenRotationAnimation != null) {
2152 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2153 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002154 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002155 proto.end(token);
2156 }
2157
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002158 @Override
2159 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2160 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002161 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2162 final String subPrefix = " " + prefix;
2163 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2164 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2165 pw.print("dpi");
2166 if (mInitialDisplayWidth != mBaseDisplayWidth
2167 || mInitialDisplayHeight != mBaseDisplayHeight
2168 || mInitialDisplayDensity != mBaseDisplayDensity) {
2169 pw.print(" base=");
2170 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2171 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2172 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002173 if (mDisplayScalingDisabled) {
2174 pw.println(" noscale");
2175 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002176 pw.print(" cur=");
2177 pw.print(mDisplayInfo.logicalWidth);
2178 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2179 pw.print(" app=");
2180 pw.print(mDisplayInfo.appWidth);
2181 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2182 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2183 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2184 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2185 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002186 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002187 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002188 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002189
Craig Mautnerdc548482014-02-05 13:35:24 -08002190 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002191 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002192 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2193 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002194 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002195 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002196
Craig Mautnerdc548482014-02-05 13:35:24 -08002197 pw.println();
2198 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002199 pw.println();
2200 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002201 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002202 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002203 pw.print(" Exiting #"); pw.print(i);
2204 pw.print(' '); pw.print(token);
2205 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002206 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002207 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002208 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002209
Jorim Jaggi31f71702016-05-04 16:43:04 -07002210 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002211
2212 // Dump stack references
2213 final TaskStack homeStack = getHomeStack();
2214 if (homeStack != null) {
2215 pw.println(prefix + "homeStack=" + homeStack.getName());
2216 }
2217 final TaskStack pinnedStack = getPinnedStack();
2218 if (pinnedStack != null) {
2219 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2220 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002221 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002222 if (splitScreenPrimaryStack != null) {
2223 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2224 }
2225
2226 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002227 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002228 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002229 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002230
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002231 pw.println();
2232 mDisplayFrames.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002233 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002234
2235 @Override
2236 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002237 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002238 }
2239
2240 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002241 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002242 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002243
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002244 /** Returns true if the stack in the windowing mode is visible. */
2245 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002246 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002247 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002248 }
2249
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002250 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002251 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002252 final int x = (int) xf;
2253 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002254 final WindowState touchedWin = getWindow(w -> {
2255 final int flags = w.mAttrs.flags;
2256 if (!w.isVisibleLw()) {
2257 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002258 }
2259 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002260 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002261 }
2262
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002263 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002264 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002265 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002266 }
2267
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002268 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002269
2270 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002271 return mTmpRegion.contains(x, y) || touchFlags == 0;
2272 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002273
2274 return touchedWin;
2275 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002276
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002277 boolean canAddToastWindowForUid(int uid) {
2278 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002279 // Also if the app is focused adding more than one toast at
2280 // a time for better backwards compatibility.
2281 final WindowState focusedWindowForUid = getWindow(w ->
2282 w.mOwnerUid == uid && w.isFocused());
2283 if (focusedWindowForUid != null) {
2284 return true;
2285 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002286 final WindowState win = getWindow(w ->
2287 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2288 && !w.mWindowRemovalAllowed);
2289 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002290 }
2291
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002292 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002293 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2294 return;
2295 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002296
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002297 // Used to communicate the old focus to the callback method.
2298 mTmpWindow = oldFocus;
2299
2300 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002301 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002302
2303 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002304 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002305
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002306 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002307
2308 if (mTmpWindow == null) {
2309 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2310 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002311 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002312 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002313 }
2314
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002315 /** Updates the layer assignment of windows on this display. */
2316 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002317 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002318 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002319 if (setLayoutNeeded) {
2320 setLayoutNeeded();
2321 }
Robert Carrb1579c82017-09-05 14:54:47 -07002322
Robert Carrf59b8dd2017-10-02 18:58:36 -07002323 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002324 // the application of this transaction until the animation pass triggers
2325 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2326 // the hiding and showing of surfaces.
2327 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002328 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002329 }
2330
Wale Ogunwale1666e312016-12-16 11:27:18 -08002331 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2332 // moving containers or resizing them. Need to investigate the best way to have it automatically
2333 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002334 void layoutAndAssignWindowLayersIfNeeded() {
2335 mService.mWindowsChanged = true;
2336 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002337
2338 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2339 false /*updateInputWindows*/)) {
2340 assignWindowLayers(false /* setLayoutNeeded */);
2341 }
2342
2343 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2344 mService.mWindowPlacerLocked.performSurfacePlacement();
2345 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2346 }
2347
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002348 /** Returns true if a leaked surface was destroyed */
2349 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002350 // Used to indicate that a surface was leaked.
2351 mTmpWindow = null;
2352 forAllWindows(w -> {
2353 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002354 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002355 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002356 }
2357 if (!mService.mSessions.contains(wsa.mSession)) {
2358 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002359 + w + " surface=" + wsa.mSurfaceController
2360 + " token=" + w.mToken
2361 + " pid=" + w.mSession.mPid
2362 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002363 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002364 mService.mForceRemoves.add(w);
2365 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002366 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002367 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002368 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002369 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002370 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002371 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002372 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002373 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002374 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002375
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002376 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002377 }
2378
2379 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002380 * Determine and return the window that should be the IME target.
2381 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2382 * @return The window that should be used as the IME target or null if there isn't any.
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002383 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002384 WindowState computeImeTarget(boolean updateImeTarget) {
2385 if (mService.mInputMethodWindow == null) {
2386 // There isn't an IME so there shouldn't be a target...That was easy!
2387 if (updateImeTarget) {
2388 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2389 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002390 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002391 }
2392 return null;
2393 }
2394
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002395 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2396 // same display. Or even when the current IME/target are not on the same screen as the next
2397 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002398 mUpdateImeTarget = updateImeTarget;
2399 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002400
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002401
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002402 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2403 // to be on top of it, but it is not -really- where input will go. So look down below
2404 // for a real window to target...
2405 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2406 final AppWindowToken token = target.mAppToken;
2407 if (token != null) {
2408 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2409 if (betterTarget != null) {
2410 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002411 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002412 }
2413 }
2414
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002415 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2416 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002417
2418 // Now, a special case -- if the last target's window is in the process of exiting, and is
2419 // above the new target, keep on the last target to avoid flicker. Consider for example a
2420 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2421 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2422 // scrim.
2423 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002424 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2425 && (target == null
2426 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002427 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002428 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002429 }
2430
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002431 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2432 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002433
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002434 if (target == null) {
2435 if (updateImeTarget) {
2436 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2437 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2438 + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002439 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002440 }
2441
2442 return null;
2443 }
2444
2445 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002446 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2447 if (token != null) {
2448
2449 // Now some fun for dealing with window animations that modify the Z order. We need
2450 // to look at all windows below the current target that are in this app, finding the
2451 // highest visible one in layering.
2452 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002453 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002454 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002455 }
2456
2457 if (highestTarget != null) {
2458 final AppTransition appTransition = mService.mAppTransition;
2459 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2460 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2461 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002462 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002463
2464 if (appTransition.isTransitionSet()) {
2465 // If we are currently setting up for an animation, hold everything until we
2466 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002467 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002468 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002469 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002470 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002471 // If the window we are currently targeting is involved with an animation,
2472 // and it is on top of the next target we will be over, then hold off on
2473 // moving until that is done.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002474 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002475 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002476 }
2477 }
2478 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002479
2480 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2481 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002482 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002483 }
2484
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002485 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002486 }
2487
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002488 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002489 if (target == mService.mInputMethodTarget
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002490 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002491 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002492 }
2493
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002494 mService.mInputMethodTarget = target;
2495 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002496 assignWindowLayers(false /* setLayoutNeeded */);
2497 }
2498
2499 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2500 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2501 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2502 }
2503
2504 // Used to indicate we have reached the first window in the range we are interested in.
2505 mTmpWindow = null;
2506
2507 // TODO: Figure-out a more efficient way to do this.
2508 final WindowState candidate = getWindow(w -> {
2509 if (w == top) {
2510 // Reached the first window in the range we are interested in.
2511 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002512 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002513 if (mTmpWindow == null) {
2514 return false;
2515 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002516
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002517 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2518 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002519 }
2520 // If we reached the bottom of the range of windows we are considering,
2521 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002522 if (w == bottom) {
2523 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002524 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002525 return false;
2526 });
2527
2528 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002529 }
2530
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002531 void setLayoutNeeded() {
2532 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2533 mLayoutNeeded = true;
2534 }
2535
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002536 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002537 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2538 mLayoutNeeded = false;
2539 }
2540
2541 boolean isLayoutNeeded() {
2542 return mLayoutNeeded;
2543 }
2544
Wale Ogunwale02319a62016-09-26 15:21:22 -07002545 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2546 if (mTokenMap.isEmpty()) {
2547 return;
2548 }
2549 pw.println(" Display #" + mDisplayId);
2550 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2551 while (it.hasNext()) {
2552 final WindowToken token = it.next();
2553 pw.print(" ");
2554 pw.print(token);
2555 if (dumpAll) {
2556 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002557 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07002558 } else {
2559 pw.println();
2560 }
2561 }
2562 }
2563
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002564 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002565 final int[] index = new int[1];
2566 forAllWindows(w -> {
2567 final WindowStateAnimator wAnim = w.mWinAnimator;
2568 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2569 index[0] = index[0] + 1;
2570 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002571 }
2572
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002573 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002574 forAllWindows(w -> {
2575 w.mWinAnimator.enableSurfaceTrace(fd);
2576 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002577 }
2578
2579 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002580 forAllWindows(w -> {
2581 w.mWinAnimator.disableSurfaceTrace();
2582 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002583 }
2584
Jorim Jaggife762342016-10-13 14:33:27 +02002585 /**
2586 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2587 */
2588 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2589 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002590 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002591 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2592 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002593 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02002594 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002595 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002596 }
2597
Wale Ogunwale494009b82016-10-21 09:01:38 -07002598 boolean checkWaitingForWindows() {
2599
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002600 mHaveBootMsg = false;
2601 mHaveApp = false;
2602 mHaveWallpaper = false;
2603 mHaveKeyguard = true;
2604
2605 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002606 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2607 return true;
2608 }
2609 if (w.isDrawnLw()) {
2610 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002611 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002612 } else if (w.mAttrs.type == TYPE_APPLICATION
2613 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002614 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002615 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002616 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002617 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002618 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002619 }
2620 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002621 return false;
2622 });
2623
2624 if (visibleWindow != null) {
2625 // We have a visible window.
2626 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002627 }
2628
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002629 // if the wallpaper service is disabled on the device, we're never going to have
2630 // wallpaper, don't bother waiting for it
2631 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2632 com.android.internal.R.bool.config_enableWallpaperService)
2633 && !mService.mOnlyCore;
2634
Wale Ogunwale494009b82016-10-21 09:01:38 -07002635 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2636 "******** booted=" + mService.mSystemBooted
2637 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002638 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2639 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2640 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002641
2642 // If we are turning on the screen to show the boot message, don't do it until the boot
2643 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002644 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002645 return true;
2646 }
2647
2648 // If we are turning on the screen after the boot is completed normally, don't do so until
2649 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002650 if (mService.mSystemBooted
2651 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002652 return true;
2653 }
2654
2655 return false;
2656 }
2657
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002658 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002659 mTmpWindowAnimator = animator;
2660 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002661 }
2662
2663 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002664 resetAnimationBackgroundAnimator();
2665
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002666 // Used to indicate a detached wallpaper.
2667 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002668 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002669
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002670 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002671
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002672 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002673 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002674 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2675 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002676 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2677 }
2678 }
2679
Wale Ogunwale494009b82016-10-21 09:01:38 -07002680 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002681 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002682 if (imFocus == null) {
2683 return false;
2684 }
2685
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002686 if (DEBUG_INPUT_METHOD) {
2687 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2688 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2689 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002690 }
2691
Wale Ogunwale494009b82016-10-21 09:01:38 -07002692 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2693
2694 if (DEBUG_INPUT_METHOD) {
2695 Slog.i(TAG_WM, "IM target client: " + imeClient);
2696 if (imeClient != null) {
2697 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2698 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2699 }
2700 }
2701
2702 return imeClient != null && imeClient.asBinder() == client.asBinder();
2703 }
2704
2705 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002706 final WindowState win = getWindow(
2707 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2708 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002709 }
2710
2711 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002712 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002713 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002714 final int curValue = w.mSystemUiVisibility;
2715 final int diff = (curValue ^ visibility) & globalDiff;
2716 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002717 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002718 w.mSeq++;
2719 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002720 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002721 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2722 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002723 visibility, newValue, diff);
2724 }
2725 } catch (RemoteException e) {
2726 // so sorry
2727 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002728 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002729 }
2730
2731 void onWindowFreezeTimeout() {
2732 Slog.w(TAG_WM, "Window freeze timeout expired.");
2733 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002734
2735 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002736 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002737 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002738 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002739 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002740 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2741 - mService.mDisplayFreezeTime);
2742 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002743 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002744 mService.mWindowPlacerLocked.performSurfacePlacement();
2745 }
2746
2747 void waitForAllWindowsDrawn() {
2748 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002749 forAllWindows(w -> {
2750 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2751 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2752 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002753 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002754 w.mLastContentInsets.set(-1, -1, -1, -1);
2755 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002756 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002757 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002758 }
2759
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002760 // TODO: Super crazy long method that should be broken down...
2761 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2762
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002763 final int dw = mDisplayInfo.logicalWidth;
2764 final int dh = mDisplayInfo.logicalHeight;
2765 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2766
2767 mTmpUpdateAllDrawn.clear();
2768
2769 int repeats = 0;
2770 do {
2771 repeats++;
2772 if (repeats > 6) {
2773 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2774 clearLayoutNeeded();
2775 break;
2776 }
2777
2778 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2779 pendingLayoutChanges);
2780
Andrii Kulian839def92016-11-02 10:58:58 -07002781 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2782 // the wallpaper window jumping across displays.
2783 // Remove check for default display when there will be support for multiple wallpaper
2784 // targets (on different displays).
2785 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002786 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002787 }
2788
2789 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2790 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2791 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2792 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002793 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002794 }
2795 }
2796
2797 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2798 setLayoutNeeded();
2799 }
2800
2801 // FIRST LOOP: Perform a layout, if needed.
2802 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2803 performLayout(repeats == 1, false /* updateInputWindows */);
2804 } else {
2805 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2806 }
2807
2808 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2809 pendingLayoutChanges = 0;
2810
2811 if (isDefaultDisplay) {
2812 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002813 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002814 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2815 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2816 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2817 }
2818 } while (pendingLayoutChanges != 0);
2819
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002820 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002821
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002822 mTmpRecoveringMemory = recoveringMemory;
2823 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01002824 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002825
2826 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002827 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2828 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2829 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002830 true /* inTraversal, must call performTraversalInTrans... below */);
2831
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002832 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2833 if (wallpaperVisible != mLastWallpaperVisible) {
2834 mLastWallpaperVisible = wallpaperVisible;
2835 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2836 }
2837
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002838 while (!mTmpUpdateAllDrawn.isEmpty()) {
2839 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2840 // See if any windows have been drawn, so they (and others associated with them)
2841 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002842 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002843 }
2844
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002845 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002846 }
2847
Bryce Leef3c6a472017-11-14 14:53:06 -08002848 private void updateBounds() {
2849 calculateBounds(mTmpBounds);
2850 setBounds(mTmpBounds);
2851 }
2852
2853 // Determines the current display bounds based on the current state
2854 private void calculateBounds(Rect out) {
2855 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
2856 final int orientation = mDisplayInfo.rotation;
2857 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2858 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
2859 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
2860 int width = mDisplayInfo.logicalWidth;
2861 int left = (physWidth - width) / 2;
2862 int height = mDisplayInfo.logicalHeight;
2863 int top = (physHeight - height) / 2;
2864 out.set(left, top, left + width, top + height);
2865 }
2866
2867 @Override
2868 public void getBounds(Rect out) {
2869 calculateBounds(out);
2870 }
2871
2872 private void getBounds(Rect out, int orientation) {
2873 getBounds(out);
2874
2875 // Rotate the Rect if needed.
2876 final int currentRotation = mDisplayInfo.rotation;
2877 final int rotationDelta = deltaRotation(currentRotation, orientation);
2878 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
2879 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
2880 mTmpRectF.set(out);
2881 mTmpMatrix.mapRect(mTmpRectF);
2882 mTmpRectF.round(out);
2883 }
2884 }
2885
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002886 void performLayout(boolean initial, boolean updateInputWindows) {
2887 if (!isLayoutNeeded()) {
2888 return;
2889 }
2890 clearLayoutNeeded();
2891
2892 final int dw = mDisplayInfo.logicalWidth;
2893 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002894 if (DEBUG_LAYOUT) {
2895 Slog.v(TAG, "-------------------------------------");
2896 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2897 }
2898
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002899 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
2900 // TODO: Not sure if we really need to set the rotation here since we are updating from the
2901 // display info above...
2902 mDisplayFrames.mRotation = mRotation;
2903 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002904 if (isDefaultDisplay) {
2905 // Not needed on non-default displays.
2906 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2907 mService.mScreenRect.set(0, 0, dw, dh);
2908 }
2909
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002910 int seq = mService.mLayoutSeq + 1;
2911 if (seq < 0) seq = 0;
2912 mService.mLayoutSeq = seq;
2913
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002914 // Used to indicate that we have processed the dream window and all additional windows are
2915 // behind it.
2916 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002917 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002918
2919 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002920 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002921
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002922 // Used to indicate that we have processed the dream window and all additional attached
2923 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002924 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002925 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002926
2927 // Now perform layout of attached windows, which usually depend on the position of the
2928 // window they are attached to. XXX does not deal with windows that are attached to windows
2929 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002930 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002931
2932 // Window frames may have changed. Tell the input dispatcher about it.
2933 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2934 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2935 if (updateInputWindows) {
2936 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2937 }
2938
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002939 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2940 }
2941
2942 /**
2943 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2944 * In portrait mode, it grabs the full screenshot.
2945 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002946 * @param config of the output bitmap
2947 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
2948 */
chaviwfbe47df2017-11-10 16:14:49 -08002949 Bitmap screenshotDisplay(Bitmap.Config config, boolean wallpaperOnly) {
2950 synchronized (mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02002951 if (!mService.mPolicy.isScreenOn()) {
chaviwfbe47df2017-11-10 16:14:49 -08002952 if (DEBUG_SCREENSHOT) {
2953 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002954 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002955 return null;
2956 }
2957
chaviwfbe47df2017-11-10 16:14:49 -08002958 if (wallpaperOnly && !shouldScreenshotWallpaper()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002959 return null;
2960 }
2961
chaviwfbe47df2017-11-10 16:14:49 -08002962 int dw = mDisplayInfo.logicalWidth;
2963 int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002964
chaviwfbe47df2017-11-10 16:14:49 -08002965 if (dw <= 0 || dh <= 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002966 return null;
2967 }
2968
chaviwfbe47df2017-11-10 16:14:49 -08002969 final Rect frame = new Rect(0, 0, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002970
2971 // The screenshot API does not apply the current screen rotation.
2972 int rot = mDisplay.getRotation();
2973
2974 if (rot == ROTATION_90 || rot == ROTATION_270) {
2975 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
2976 }
2977
chaviwfbe47df2017-11-10 16:14:49 -08002978 // SurfaceFlinger is not aware of orientation, so convert our logical
2979 // crop to SurfaceFlinger's portrait orientation.
2980 convertCropForSurfaceFlinger(frame, rot, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002981
2982 final ScreenRotationAnimation screenRotationAnimation =
2983 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
2984 final boolean inRotation = screenRotationAnimation != null &&
2985 screenRotationAnimation.isAnimating();
chaviwfbe47df2017-11-10 16:14:49 -08002986 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002987
Robert Carrb1579c82017-09-05 14:54:47 -07002988 // TODO(b/68392460): We should screenshot Task controls directly
2989 // but it's difficult at the moment as the Task doesn't have the
2990 // correct size set.
chaviwfbe47df2017-11-10 16:14:49 -08002991 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, 0, 1, inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002992 if (bitmap == null) {
Robert Carrb1579c82017-09-05 14:54:47 -07002993 Slog.w(TAG_WM, "Failed to take screenshot");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002994 return null;
2995 }
chaviwfbe47df2017-11-10 16:14:49 -08002996
2997 // Create a copy of the screenshot that is immutable and backed in ashmem.
2998 // This greatly reduces the overhead of passing the bitmap between processes.
2999 final Bitmap ret = bitmap.createAshmemBitmap(config);
3000 bitmap.recycle();
3001 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003002 }
chaviwfbe47df2017-11-10 16:14:49 -08003003 }
3004
3005 private boolean shouldScreenshotWallpaper() {
3006 MutableBoolean screenshotReady = new MutableBoolean(false);
3007
3008 forAllWindows(w -> {
3009 if (!w.mIsWallpaper) {
3010 return false;
3011 }
3012
3013 // Found the wallpaper window
3014 final WindowStateAnimator winAnim = w.mWinAnimator;
3015
3016 if (winAnim.getShown() && winAnim.mLastAlpha > 0f) {
3017 screenshotReady.value = true;
3018 }
3019
3020 return true;
3021 }, true /* traverseTopToBottom */);
3022
3023 return screenshotReady.value;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003024 }
3025
3026 // TODO: Can this use createRotationMatrix()?
3027 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3028 if (rot == Surface.ROTATION_90) {
3029 final int tmp = crop.top;
3030 crop.top = dw - crop.right;
3031 crop.right = crop.bottom;
3032 crop.bottom = dw - crop.left;
3033 crop.left = tmp;
3034 } else if (rot == Surface.ROTATION_180) {
3035 int tmp = crop.top;
3036 crop.top = dh - crop.bottom;
3037 crop.bottom = dh - tmp;
3038 tmp = crop.right;
3039 crop.right = dw - crop.left;
3040 crop.left = dw - tmp;
3041 } else if (rot == Surface.ROTATION_270) {
3042 final int tmp = crop.top;
3043 crop.top = crop.left;
3044 crop.left = dh - crop.bottom;
3045 crop.bottom = crop.right;
3046 crop.right = dh - tmp;
3047 }
3048 }
3049
3050 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003051 // Used to indicate the layout is needed.
3052 mTmpWindow = null;
3053
3054 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003055 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003056 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003057 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003058 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003059 w.setDisplayLayoutNeeded();
3060 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003061 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003062
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003063 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003064 mService.mWindowPlacerLocked.performSurfacePlacement();
3065 }
3066 }
3067
Wale Ogunwale1666e312016-12-16 11:27:18 -08003068 void setExitingTokensHasVisible(boolean hasVisible) {
3069 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3070 mExitingTokens.get(i).hasVisible = hasVisible;
3071 }
3072
3073 // Initialize state of exiting applications.
3074 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3075 }
3076
3077 void removeExistingTokensIfPossible() {
3078 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3079 final WindowToken token = mExitingTokens.get(i);
3080 if (!token.hasVisible) {
3081 mExitingTokens.remove(i);
3082 }
3083 }
3084
3085 // Time to remove any exiting applications?
3086 mTaskStackContainers.removeExistingAppTokensIfPossible();
3087 }
3088
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003089 @Override
3090 void onDescendantOverrideConfigurationChanged() {
3091 setLayoutNeeded();
3092 mService.requestTraversal();
3093 }
3094
David Stevens9440dc82017-03-16 19:00:20 -07003095 boolean okToDisplay() {
3096 if (mDisplayId == DEFAULT_DISPLAY) {
3097 return !mService.mDisplayFrozen
3098 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3099 }
3100 return mDisplayInfo.state == Display.STATE_ON;
3101 }
3102
3103 boolean okToAnimate() {
3104 return okToDisplay() &&
3105 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3106 }
3107
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003108 static final class TaskForResizePointSearchResult {
3109 boolean searchDone;
3110 Task taskForResize;
3111
3112 void reset() {
3113 searchDone = false;
3114 taskForResize = null;
3115 }
3116 }
Robert Carr3b716242016-08-16 16:02:21 -07003117
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003118 private static final class ApplySurfaceChangesTransactionState {
3119 boolean displayHasContent;
3120 boolean obscured;
3121 boolean syswin;
3122 boolean focusDisplayed;
3123 float preferredRefreshRate;
3124 int preferredModeId;
3125
3126 void reset() {
3127 displayHasContent = false;
3128 obscured = false;
3129 syswin = false;
3130 focusDisplayed = false;
3131 preferredRefreshRate = 0;
3132 preferredModeId = 0;
3133 }
3134 }
3135
3136 private static final class ScreenshotApplicationState {
3137 WindowState appWin;
3138 int maxLayer;
3139 int minLayer;
3140 boolean screenshotReady;
3141
3142 void reset(boolean screenshotReady) {
3143 appWin = null;
3144 maxLayer = 0;
3145 minLayer = 0;
3146 this.screenshotReady = screenshotReady;
3147 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3148 }
3149 }
3150
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003151 /**
3152 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3153 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3154 * homogeneous children type which is currently required by sub-classes of
3155 * {@link WindowContainer} class.
3156 */
3157 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3158
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003159 DisplayChildWindowContainer(WindowManagerService service) {
3160 super(service);
3161 }
3162
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003163 @Override
3164 boolean fillsParent() {
3165 return true;
3166 }
3167
3168 @Override
3169 boolean isVisible() {
3170 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003171 }
3172 }
3173
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003174 /**
3175 * Window container class that contains all containers on this display relating to Apps.
3176 * I.e Activities.
3177 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003178 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003179 /**
3180 * A control placed at the appropriate level for transitions to occur.
3181 */
chaviw23ee71c2017-12-18 11:29:41 -08003182 SurfaceControl mAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003183
Robert Carrf7a7ca82017-12-06 13:17:07 -08003184 /**
3185 * Given that the split-screen divider does not have an AppWindowToken, it
3186 * will have to live inside of a "NonAppWindowContainer", in particular
3187 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
3188 * it will need to be interleaved with some of our children, appearing on top of
3189 * both docked stacks but underneath any assistant stacks.
3190 *
3191 * To solve this problem we have this anchor control, which will always exist so
3192 * we can always assign it the correct value in our {@link #assignChildLayers}.
3193 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
3194 * assign the divider a layer relative to it. This way we prevent linking lifecycle
3195 * events between the two containers.
3196 */
3197 SurfaceControl mSplitScreenDividerAnchor = null;
3198
Wale Ogunwale61911492017-10-11 08:50:50 -07003199 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3200 // through the list to find them.
3201 private TaskStack mHomeStack = null;
3202 private TaskStack mPinnedStack = null;
3203 private TaskStack mSplitScreenPrimaryStack = null;
3204
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003205 TaskStackContainers(WindowManagerService service) {
3206 super(service);
3207 }
3208
Wale Ogunwale61911492017-10-11 08:50:50 -07003209 /**
3210 * Returns the topmost stack on the display that is compatible with the input windowing mode
3211 * and activity type. Null is no compatible stack on the display.
3212 */
3213 TaskStack getStack(int windowingMode, int activityType) {
3214 if (activityType == ACTIVITY_TYPE_HOME) {
3215 return mHomeStack;
3216 }
3217 if (windowingMode == WINDOWING_MODE_PINNED) {
3218 return mPinnedStack;
3219 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3220 return mSplitScreenPrimaryStack;
3221 }
3222 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3223 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003224 if (activityType == ACTIVITY_TYPE_UNDEFINED
3225 && windowingMode == stack.getWindowingMode()) {
3226 // Passing in undefined type means we want to match the topmost stack with the
3227 // windowing mode.
3228 return stack;
3229 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003230 if (stack.isCompatible(windowingMode, activityType)) {
3231 return stack;
3232 }
3233 }
3234 return null;
3235 }
3236
3237 @VisibleForTesting
3238 TaskStack getTopStack() {
3239 return mTaskStackContainers.getChildCount() > 0
3240 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3241 }
3242
3243 TaskStack getHomeStack() {
3244 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3245 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3246 }
3247 return mHomeStack;
3248 }
3249
3250 TaskStack getPinnedStack() {
3251 return mPinnedStack;
3252 }
3253
Matthew Ng64e77cf2017-10-31 14:01:31 -07003254 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003255 return mSplitScreenPrimaryStack;
3256 }
3257
Andrii Kulian839def92016-11-02 10:58:58 -07003258 /**
3259 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003260 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003261 */
3262 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003263 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003264 addChild(stack, onTop);
3265 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003266 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003267
Wale Ogunwale61911492017-10-11 08:50:50 -07003268 void onStackWindowingModeChanged(TaskStack stack) {
3269 removeStackReferenceIfNeeded(stack);
3270 addStackReferenceIfNeeded(stack);
3271 if (stack == mPinnedStack && getTopStack() != stack) {
3272 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3273 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3274 }
3275 }
3276
3277 private void addStackReferenceIfNeeded(TaskStack stack) {
3278 if (stack.isActivityTypeHome()) {
3279 if (mHomeStack != null) {
3280 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
3281 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
3282 }
3283 mHomeStack = stack;
3284 }
3285 final int windowingMode = stack.getWindowingMode();
3286 if (windowingMode == WINDOWING_MODE_PINNED) {
3287 if (mPinnedStack != null) {
3288 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3289 + mPinnedStack + " already exist on display=" + this
3290 + " stack=" + stack);
3291 }
3292 mPinnedStack = stack;
3293 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3294 if (mSplitScreenPrimaryStack != null) {
3295 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3296 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3297 + " already exist on display=" + this + " stack=" + stack);
3298 }
3299 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003300 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003301 }
3302 }
3303
3304 private void removeStackReferenceIfNeeded(TaskStack stack) {
3305 if (stack == mHomeStack) {
3306 mHomeStack = null;
3307 } else if (stack == mPinnedStack) {
3308 mPinnedStack = null;
3309 } else if (stack == mSplitScreenPrimaryStack) {
3310 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003311 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3312 mService.setDockedStackCreateStateLocked(
3313 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3314 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003315 }
Andrii Kulian839def92016-11-02 10:58:58 -07003316 }
3317
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003318 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003319 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3320 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003321 addChild(stack, addIndex);
3322 setLayoutNeeded();
3323 }
3324
Wale Ogunwale61911492017-10-11 08:50:50 -07003325 @Override
3326 protected void removeChild(TaskStack stack) {
3327 super.removeChild(stack);
3328 removeStackReferenceIfNeeded(stack);
3329 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003330
3331 @Override
3332 boolean isOnTop() {
3333 // Considered always on top
3334 return true;
3335 }
3336
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003337 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003338 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003339 if (child.getWindowConfiguration().isAlwaysOnTop()
3340 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003341 // This stack is always-on-top, override the default behavior.
3342 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3343
3344 // Moving to its current position, as we must call super but we don't want to
3345 // perform any meaningful action.
3346 final int currentPosition = mChildren.indexOf(child);
3347 super.positionChildAt(currentPosition, child, false /* includingParents */);
3348 return;
3349 }
3350
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003351 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3352 super.positionChildAt(targetPosition, child, includingParents);
3353
3354 setLayoutNeeded();
3355 }
3356
3357 /**
3358 * When stack is added or repositioned, find a proper position for it.
3359 * This will make sure that pinned stack always stays on top.
3360 * @param requestedPosition Position requested by caller.
3361 * @param stack Stack to be added or positioned.
3362 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3363 * @return The proper position for the stack.
3364 */
3365 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3366 final int topChildPosition = mChildren.size() - 1;
3367 boolean toTop = requestedPosition == POSITION_TOP;
3368 toTop |= adding ? requestedPosition >= topChildPosition + 1
3369 : requestedPosition >= topChildPosition;
3370 int targetPosition = requestedPosition;
3371
Wale Ogunwale61911492017-10-11 08:50:50 -07003372 if (toTop && stack.getWindowingMode() != WINDOWING_MODE_PINNED && hasPinnedStack()) {
Bryce Lee48f4b572017-04-10 10:54:15 -07003373 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003374 TaskStack topStack = mChildren.get(topChildPosition);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003375 if (topStack.getWindowingMode() != WINDOWING_MODE_PINNED) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003376 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3377 }
3378
3379 // So, stack is moved just below the pinned stack.
3380 // When we're adding a new stack the target is the current pinned stack position.
3381 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003382 // stack, because WindowContainer#positionAt() first removes element and then adds
3383 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003384 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3385 }
3386
3387 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003388 }
3389
3390 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003391 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3392 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003393 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003394 if (super.forAllWindows(callback, traverseTopToBottom)) {
3395 return true;
3396 }
3397 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3398 return true;
3399 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003400 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003401 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3402 return true;
3403 }
3404 if (super.forAllWindows(callback, traverseTopToBottom)) {
3405 return true;
3406 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003407 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003408 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003409 }
3410
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003411 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003412 boolean traverseTopToBottom) {
3413 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3414 // app tokens.
3415 // TODO: Investigate if we need to continue to do this or if we can just process them
3416 // in-order.
3417 if (traverseTopToBottom) {
3418 for (int i = mChildren.size() - 1; i >= 0; --i) {
3419 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3420 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003421 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3422 traverseTopToBottom)) {
3423 return true;
3424 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003425 }
3426 }
3427 } else {
3428 final int count = mChildren.size();
3429 for (int i = 0; i < count; ++i) {
3430 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3431 final int appTokensCount = appTokens.size();
3432 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003433 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3434 traverseTopToBottom)) {
3435 return true;
3436 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003437 }
3438 }
3439 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003440 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003441 }
3442
Wale Ogunwale1666e312016-12-16 11:27:18 -08003443 void setExitingTokensHasVisible(boolean hasVisible) {
3444 for (int i = mChildren.size() - 1; i >= 0; --i) {
3445 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3446 for (int j = appTokens.size() - 1; j >= 0; --j) {
3447 appTokens.get(j).hasVisible = hasVisible;
3448 }
3449 }
3450 }
3451
3452 void removeExistingAppTokensIfPossible() {
3453 for (int i = mChildren.size() - 1; i >= 0; --i) {
3454 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3455 for (int j = appTokens.size() - 1; j >= 0; --j) {
3456 final AppWindowToken token = appTokens.get(j);
3457 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3458 && (!token.mIsExiting || token.isEmpty())) {
3459 // Make sure there is no animation running on this token, so any windows
3460 // associated with it will be removed as soon as their animations are
3461 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003462 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003463 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3464 "performLayout: App token exiting now removed" + token);
3465 token.removeIfPossible();
3466 }
3467 }
3468 }
3469 }
3470
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003471 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003472 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003473 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3474 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003475 // Apps and their containers are not allowed to specify an orientation while the
3476 // docked or freeform stack is visible...except for the home stack/task if the
3477 // docked stack is minimized and it actually set something.
3478 if (mHomeStack != null && mHomeStack.isVisible()
3479 && mDividerControllerLocked.isMinimizedDock()) {
3480 final int orientation = mHomeStack.getOrientation();
3481 if (orientation != SCREEN_ORIENTATION_UNSET) {
3482 return orientation;
3483 }
3484 }
3485 return SCREEN_ORIENTATION_UNSPECIFIED;
3486 }
3487
3488 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003489 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3490 PackageManager.FEATURE_AUTOMOTIVE);
3491 if (isCar) {
3492 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3493 // allow anything but the default orientation.
3494 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3495 "Forcing UNSPECIFIED orientation in car. Ignoring " + orientation);
3496 return SCREEN_ORIENTATION_UNSPECIFIED;
3497 }
3498
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003499 if (orientation != SCREEN_ORIENTATION_UNSET
3500 && orientation != SCREEN_ORIENTATION_BEHIND) {
3501 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3502 "App is requesting an orientation, return " + orientation);
3503 return orientation;
3504 }
3505
3506 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003507 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003508 // The next app has not been requested to be visible, so we keep the current orientation
3509 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003510 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003511 }
Robert Carrb1579c82017-09-05 14:54:47 -07003512
3513 @Override
3514 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrf7a7ca82017-12-06 13:17:07 -08003515
3516 final int HOME_STACK_STATE = 0;
3517 final int NORMAL_STACK_STATE = 1;
3518 final int ALWAYS_ON_TOP_STATE = 2;
3519
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003520 int layer = 0;
Robert Carrf7a7ca82017-12-06 13:17:07 -08003521 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
3522 for (int i = 0; i < mChildren.size(); i++) {
3523 final TaskStack s = mChildren.get(i);
3524 if (state == HOME_STACK_STATE && s.isActivityTypeHome()) {
3525 s.assignLayer(t, layer++);
3526 } else if (state == NORMAL_STACK_STATE && !s.isActivityTypeHome()
3527 && !s.isAlwaysOnTop()) {
3528 s.assignLayer(t, layer++);
3529 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
3530 t.setLayer(mSplitScreenDividerAnchor, layer++);
3531 }
3532 } else if (state == ALWAYS_ON_TOP_STATE && s.isAlwaysOnTop()) {
3533 s.assignLayer(t, layer++);
3534 }
3535 }
3536 // The appropriate place for App-Transitions to occur is right
3537 // above all other animations but still below things in the Picture-and-Picture
3538 // windowing mode.
3539 if (state == NORMAL_STACK_STATE && mAppAnimationLayer != null) {
3540 t.setLayer(mAppAnimationLayer, layer++);
Robert Carrb1579c82017-09-05 14:54:47 -07003541 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003542 }
3543 for (int i = 0; i < mChildren.size(); i++) {
3544 final TaskStack s = mChildren.get(i);
Robert Carrf7a7ca82017-12-06 13:17:07 -08003545 s.assignChildLayers(t);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003546 }
3547
Robert Carrb1579c82017-09-05 14:54:47 -07003548 }
3549
3550 @Override
chaviw23ee71c2017-12-18 11:29:41 -08003551 SurfaceControl getAppAnimationLayer() {
3552 return mAppAnimationLayer;
3553 }
3554
Robert Carrf7a7ca82017-12-06 13:17:07 -08003555 SurfaceControl getSplitScreenDividerAnchor() {
3556 return mSplitScreenDividerAnchor;
3557 }
3558
chaviw23ee71c2017-12-18 11:29:41 -08003559 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07003560 void onParentSet() {
3561 super.onParentSet();
3562 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08003563 mAppAnimationLayer = makeChildSurface(null)
3564 .setName("animationLayer")
3565 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08003566 mSplitScreenDividerAnchor = makeChildSurface(null)
3567 .setName("splitScreenDividerAnchor")
3568 .build();
3569 getPendingTransaction()
3570 .show(mAppAnimationLayer)
3571 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08003572 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07003573 } else {
chaviw23ee71c2017-12-18 11:29:41 -08003574 mAppAnimationLayer.destroy();
3575 mAppAnimationLayer = null;
Robert Carrf7a7ca82017-12-06 13:17:07 -08003576 mSplitScreenDividerAnchor.destroy();
3577 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07003578 }
3579 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003580 }
3581
Robert Carree4d4b92017-11-22 12:21:46 -08003582 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003583 AboveAppWindowContainers(String name, WindowManagerService service) {
3584 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08003585 }
3586
3587 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
3588 boolean needAssignIme = imeContainer != null
3589 && imeContainer.getSurfaceControl() != null;
3590 for (int j = 0; j < mChildren.size(); ++j) {
3591 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08003592
3593 // See {@link mSplitScreenDividerAnchor}
3594 if (wt.windowType == TYPE_DOCK_DIVIDER) {
3595 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
3596 continue;
3597 }
Robert Carree4d4b92017-11-22 12:21:46 -08003598 wt.assignLayer(t, j);
3599 wt.assignChildLayers(t);
3600
3601 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
3602 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08003603
3604 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
3605 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003606 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08003607 needAssignIme = false;
3608 }
3609 }
3610 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003611 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08003612 }
3613 }
3614 }
3615
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003616 /**
3617 * Window container class that contains all containers on this display that are not related to
3618 * Apps. E.g. status bar.
3619 */
Robert Carree4d4b92017-11-22 12:21:46 -08003620 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07003621 /**
3622 * Compares two child window tokens returns -1 if the first is lesser than the second in
3623 * terms of z-order and 1 otherwise.
3624 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003625 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003626 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003627 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3628 token1.mOwnerCanManageAppTokens)
3629 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3630 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003631
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003632 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3633 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3634 return false;
3635 }
3636 final int req = w.mAttrs.screenOrientation;
3637 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3638 || req == SCREEN_ORIENTATION_UNSET) {
3639 return false;
3640 }
3641 return true;
3642 };
3643
Wale Ogunwale3a931692016-11-02 16:49:48 -07003644 private final String mName;
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003645 NonAppWindowContainers(String name, WindowManagerService service) {
3646 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003647 mName = name;
3648 }
3649
3650 void addChild(WindowToken token) {
3651 addChild(token, mWindowComparator);
3652 }
3653
3654 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003655 int getOrientation() {
3656 final WindowManagerPolicy policy = mService.mPolicy;
3657 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003658 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003659
3660 if (win != null) {
3661 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003662 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003663 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003664 if (mService.mKeyguardGoingAway) {
3665 // Keyguard can't affect the orientation if it is going away...
3666 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3667 return SCREEN_ORIENTATION_UNSET;
3668 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003669 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003670 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003671 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003672 }
3673
Andrii Kulian8ee72852017-03-10 10:36:45 -08003674 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003675
Jorim Jaggi75520d52017-08-24 17:23:19 +02003676 if (policy.isKeyguardShowingAndNotOccluded()
3677 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003678 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003679 }
3680
3681 return SCREEN_ORIENTATION_UNSET;
3682 }
3683
3684 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003685 String getName() {
3686 return mName;
3687 }
Robert Carr3b716242016-08-16 16:02:21 -07003688 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003689
Robert Carr9034d962018-01-04 18:27:42 -08003690 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
3691 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
3692 super(name, service);
3693 }
3694
3695 @Override
3696 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
3697 }
3698 };
3699
Robert Carrb1579c82017-09-05 14:54:47 -07003700 SurfaceControl.Builder makeSurface(SurfaceSession s) {
3701 return mService.makeSurfaceBuilder(s)
3702 .setParent(mWindowingLayer);
3703 }
3704
3705 @Override
3706 SurfaceSession getSession() {
3707 return mSession;
3708 }
3709
3710 @Override
3711 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003712 SurfaceSession s = child != null ? child.getSession() : getSession();
3713 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07003714 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07003715
3716 if (child == null) {
3717 return b;
3718 }
3719
Robert Carree4d4b92017-11-22 12:21:46 -08003720 return b.setName(child.getName())
3721 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07003722 }
3723
3724 /**
3725 * The makeSurface variants are for use by the window-container
3726 * hierarchy. makeOverlay here is a function for various non windowing
3727 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
3728 * and other potpourii.
3729 */
3730 SurfaceControl.Builder makeOverlay() {
3731 return mService.makeSurfaceBuilder(mSession)
3732 .setParent(mOverlayLayer);
3733 }
3734
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09003735 /**
3736 * Reparents the given surface to mOverlayLayer.
3737 */
3738 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
3739 transaction.reparent(surface, mOverlayLayer.getHandle());
3740 }
3741
Robert Carrb1579c82017-09-05 14:54:47 -07003742 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003743 applyMagnificationSpec(getPendingTransaction(), spec);
3744 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07003745 }
3746
3747 @Override
3748 void onParentSet() {
3749 // Since we are the top of the SurfaceControl hierarchy here
3750 // we create the root surfaces explicitly rather than chaining
3751 // up as the default implementation in onParentSet does. So we
3752 // explicitly do NOT call super here.
3753 }
3754
3755 @Override
3756 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07003757
3758 // These are layers as children of "mWindowingLayer"
3759 mBelowAppWindowsContainers.assignLayer(t, 0);
3760 mTaskStackContainers.assignLayer(t, 1);
3761 mAboveAppWindowsContainers.assignLayer(t, 2);
3762
3763 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08003764 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08003765
Robert Carree4d4b92017-11-22 12:21:46 -08003766 // In the case where we have an IME target that is not in split-screen
3767 // mode IME assignment is easy. We just need the IME to go directly above
3768 // the target. This way children of the target will naturally go above the IME
3769 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08003770 //
Robert Carree4d4b92017-11-22 12:21:46 -08003771 // In the case of split-screen windowing mode, we need to elevate the IME above the
3772 // docked divider while keeping the app itself below the docked divider, so instead
3773 // we use relative layering of the IME targets child windows, and place the
3774 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08003775 //
Robert Carree4d4b92017-11-22 12:21:46 -08003776 // In the case where we have no IME target we assign it where it's base layer would
3777 // place it in the AboveAppWindowContainers.
3778 if (imeTarget != null && !imeTarget.inSplitScreenWindowingMode()
3779 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003780 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07003781 // TODO: We need to use an extra level on the app surface to ensure
3782 // this is always above SurfaceView but always below attached window.
3783 1);
Robert Carree4d4b92017-11-22 12:21:46 -08003784 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07003785 }
3786
3787 // Above we have assigned layers to our children, now we ask them to assign
3788 // layers to their children.
3789 mBelowAppWindowsContainers.assignChildLayers(t);
3790 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08003791 mAboveAppWindowsContainers.assignChildLayers(t,
3792 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07003793 mImeWindowsContainers.assignChildLayers(t);
3794 }
3795
3796 /**
3797 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
3798 * that the IME target is one of the docked applications. We'd like the docked divider to be
3799 * above both of the applications, and we'd like the IME to be above the docked divider.
3800 * However we need child windows of the applications to be above the IME (Text drag handles).
3801 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
3802 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
3803 * with {@link #WindowState#assignLayer}
3804 */
3805 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
3806 t.setRelativeLayer(child.getSurfaceControl(), mImeWindowsContainers.getSurfaceControl(), 1);
3807 }
3808
3809 @Override
Jorim Jaggia5e10572017-11-15 14:36:26 +01003810 public void destroyAfterPendingTransaction(SurfaceControl surface) {
Robert Carrb1579c82017-09-05 14:54:47 -07003811 mPendingDestroyingSurfaces.add(surface);
3812 }
3813
3814 /**
3815 * Destroys any surfaces that have been put into the pending list with
Jorim Jaggia5e10572017-11-15 14:36:26 +01003816 * {@link #destroyAfterPendingTransaction}.
Robert Carrb1579c82017-09-05 14:54:47 -07003817 */
3818 void onPendingTransactionApplied() {
3819 for (int i = mPendingDestroyingSurfaces.size() - 1; i >= 0; i--) {
3820 mPendingDestroyingSurfaces.get(i).destroy();
3821 }
3822 mPendingDestroyingSurfaces.clear();
3823 }
Jorim Jaggi9af095b2017-12-12 17:18:57 +01003824
3825 @Override
3826 void prepareSurfaces() {
3827 final ScreenRotationAnimation screenRotationAnimation =
3828 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
3829 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
3830 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
3831 mPendingTransaction.setMatrix(mWindowingLayer,
3832 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
3833 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
3834 mPendingTransaction.setPosition(mWindowingLayer,
3835 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
3836 mPendingTransaction.setAlpha(mWindowingLayer,
3837 screenRotationAnimation.getEnterTransformation().getAlpha());
3838 }
3839 super.prepareSurfaces();
3840 }
Craig Mautner59c00972012-07-30 12:10:24 -07003841}