blob: ca8ffca00ee767b0d6b867eb7f6cdbd6256ca491 [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;
Wale Ogunwale494009b82016-10-21 09:01:38 -070052import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070053import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070054import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
55import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070056import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070057import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
58import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070059import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070060import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Adrian Roose99bc052017-11-20 17:55:31 +010061import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
62import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
63import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
64import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Jorim Jaggi75520d52017-08-24 17:23:19 +020065import static com.android.server.wm.AppTransition.TRANSIT_KEYGUARD_UNOCCLUDE;
Wale Ogunwale1666e312016-12-16 11:27:18 -080066import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070067import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070069import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
70import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwalec69694a2016-10-18 13:51:15 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070076import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070077import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070078import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080079import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070080import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070081import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070082import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070083import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070084import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070085import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080086import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -070087import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070088import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
89import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070090import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070091import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -070092import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
93import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070094import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER;
95import static com.android.server.wm.WindowManagerService.TYPE_LAYER_OFFSET;
96import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -070097import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070098import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070099import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700100import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700101import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700102import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700103import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700104import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700105import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700106import static com.android.server.wm.proto.DisplayProto.ABOVE_APP_WINDOWS;
107import static com.android.server.wm.proto.DisplayProto.BELOW_APP_WINDOWS;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000108import static com.android.server.wm.proto.DisplayProto.DISPLAY_FRAMES;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700109import static com.android.server.wm.proto.DisplayProto.DISPLAY_INFO;
110import static com.android.server.wm.proto.DisplayProto.DOCKED_STACK_DIVIDER_CONTROLLER;
111import static com.android.server.wm.proto.DisplayProto.DPI;
112import static com.android.server.wm.proto.DisplayProto.ID;
113import static com.android.server.wm.proto.DisplayProto.IME_WINDOWS;
114import static com.android.server.wm.proto.DisplayProto.PINNED_STACK_CONTROLLER;
Steven Timotiusf2d68892017-08-28 17:00:01 -0700115import static com.android.server.wm.proto.DisplayProto.ROTATION;
116import static com.android.server.wm.proto.DisplayProto.SCREEN_ROTATION_ANIMATION;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700117import static com.android.server.wm.proto.DisplayProto.STACKS;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700118import static com.android.server.wm.proto.DisplayProto.WINDOW_CONTAINER;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700119
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700120import android.annotation.CallSuper;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700121import android.annotation.NonNull;
Dean Harding3e5a1522017-10-06 09:19:01 -0700122import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700123import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700124import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700125import android.graphics.Bitmap;
Jorim Jaggi6a7a8592017-01-12 00:44:33 +0100126import android.graphics.GraphicBuffer;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700127import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800128import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700129import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700130import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100131import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700132import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700133import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700134import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700135import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700136import android.os.RemoteException;
137import android.os.SystemClock;
Chong Zhang8e89b312015-09-09 15:09:30 -0700138import android.util.DisplayMetrics;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800139import android.util.MutableBoolean;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700140import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700141import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700142import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700143import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700144import android.view.InputDevice;
Robert Carrb1579c82017-09-05 14:54:47 -0700145import android.view.MagnificationSpec;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700146import android.view.Surface;
147import android.view.SurfaceControl;
Robert Carrb1579c82017-09-05 14:54:47 -0700148import android.view.SurfaceSession;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700149
Bryce Lee48f4b572017-04-10 10:54:15 -0700150import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800151import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700152import com.android.internal.view.IInputMethodClient;
Adrian Roose99bc052017-11-20 17:55:31 +0100153import com.android.server.policy.WindowManagerPolicy;
Craig Mautner59c00972012-07-30 12:10:24 -0700154
Robert Carr3b716242016-08-16 16:02:21 -0700155import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700156import java.io.PrintWriter;
157import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700158import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700159import java.util.HashMap;
160import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700161import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700162import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800163import java.util.function.Consumer;
164import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700165
Craig Mautner59c00972012-07-30 12:10:24 -0700166/**
167 * Utility class for keeping track of the WindowStates and other pertinent contents of a
168 * particular Display.
169 *
170 * IMPORTANT: No method from this class should ever be used without holding
171 * WindowManagerService.mWindowMap.
172 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700173class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700174 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700175
176 /** Unique identifier of this stack. */
177 private final int mDisplayId;
178
Wale Ogunwale3a931692016-11-02 16:49:48 -0700179 /** The containers below are the only child containers the display can have. */
180 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700181 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700182 // Contains all non-app window containers that should be displayed above the app containers
183 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800184 private final AboveAppWindowContainers mAboveAppWindowsContainers =
185 new AboveAppWindowContainers("mAboveAppWindowsContainers");
Wale Ogunwale3a931692016-11-02 16:49:48 -0700186 // Contains all non-app window containers that should be displayed below the app containers
187 // (e.g. Wallpaper).
188 private final NonAppWindowContainers mBelowAppWindowsContainers =
189 new NonAppWindowContainers("mBelowAppWindowsContainers");
190 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
191 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
192 // window containers together and move them in-sync if/when needed.
193 private final NonAppWindowContainers mImeWindowsContainers =
194 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700195
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000196 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800197 private WindowState mTmpWindow2;
198 private WindowAnimator mTmpWindowAnimator;
199 private boolean mTmpRecoveringMemory;
200 private boolean mUpdateImeTarget;
201 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700202 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700203
Wale Ogunwale02319a62016-09-26 15:21:22 -0700204 // Mapping from a token IBinder to a WindowToken object on this display.
205 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
206
Andrii Kuliancd097992017-03-23 18:31:59 -0700207 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700208 int mInitialDisplayWidth = 0;
209 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700210 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700211
212 /**
213 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
214 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
215 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
216 */
Craig Mautner59c00972012-07-30 12:10:24 -0700217 int mBaseDisplayWidth = 0;
218 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700219 /**
220 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
221 * but can be set from Settings or via shell command "adb shell wm density".
222 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
223 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700224 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700225 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700226 private final DisplayInfo mDisplayInfo = new DisplayInfo();
227 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700228 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000229 DisplayFrames mDisplayFrames;
230
Andrii Kulian06d07d62017-03-14 11:11:47 -0700231 /**
232 * For default display it contains real metrics, empty for others.
233 * @see WindowManagerService#createWatermarkInTransaction()
234 */
235 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
236 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
237 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700238
Andrii Kulian06d07d62017-03-14 11:11:47 -0700239 /**
240 * Compat metrics computed based on {@link #mDisplayMetrics}.
241 * @see #updateDisplayAndOrientation(int)
242 */
243 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
244
245 /** The desired scaling factor for compatible apps. */
246 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700247
Andrii Kulian8ee72852017-03-10 10:36:45 -0800248 /**
249 * Current rotation of the display.
250 * Constants as per {@link android.view.Surface.Rotation}.
251 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700252 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800253 */
254 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700255
Andrii Kulian8ee72852017-03-10 10:36:45 -0800256 /**
257 * Last applied orientation of the display.
258 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
259 *
260 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
261 */
262 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700263
Andrii Kulian8ee72852017-03-10 10:36:45 -0800264 /**
265 * Flag indicating that the application is receiving an orientation that has different metrics
266 * than it expected. E.g. Portrait instead of Landscape.
267 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700268 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800269 */
270 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700271
Andrii Kulian8ee72852017-03-10 10:36:45 -0800272 /**
273 * Orientation forced by some window. If there is no visible window that specifies orientation
274 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
275 *
276 * @see NonAppWindowContainers#getOrientation()
277 */
278 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700279
Andrii Kulian8ee72852017-03-10 10:36:45 -0800280 /**
281 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
282 * occluded.
283 *
284 * @see NonAppWindowContainers#getOrientation()
285 */
286 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
287
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700288 /**
289 * Keep track of wallpaper visibility to notify changes.
290 */
291 private boolean mLastWallpaperVisible = false;
292
Andrii Kulian06d07d62017-03-14 11:11:47 -0700293 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700294
Craig Mautner39834192012-09-02 07:47:24 -0700295 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700296 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700297 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700298 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800299 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700300
Craig Mautnerdc548482014-02-05 13:35:24 -0800301 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700302 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800303
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700304 /** Detect user tapping outside of current focused task bounds .*/
305 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700306
Craig Mautner6601b7b2013-04-29 10:29:11 -0700307 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700308 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700309
Craig Mautner6601b7b2013-04-29 10:29:11 -0700310 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800311 private final Rect mTmpRect = new Rect();
312 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700313 private final RectF mTmpRectF = new RectF();
314 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800315 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700316
Bryce Leef3c6a472017-11-14 14:53:06 -0800317 /** Used for handing back size of display */
318 private final Rect mTmpBounds = new Rect();
319
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800320 WindowManagerService mService;
Craig Mautner9d808b12013-08-06 18:00:25 -0700321
Craig Mautner95da1082014-02-24 17:54:35 -0800322 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700323 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800324
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700325 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700326 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700327
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800328 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
329
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000330 private boolean mHaveBootMsg = false;
331 private boolean mHaveApp = false;
332 private boolean mHaveWallpaper = false;
333 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700334
335 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
336
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700337 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
338 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000339 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
340 new ApplySurfaceChangesTransactionState();
341 private final ScreenshotApplicationState mScreenshotApplicationState =
342 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700343
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700344 // True if this display is in the process of being removed. Used to determine if the removal of
345 // the display's direct children should be allowed.
346 private boolean mRemovingDisplay = false;
347
Bryce Leed1871262017-06-12 14:12:29 -0700348 // {@code false} if this display is in the processing of being created.
349 private boolean mDisplayReady = false;
350
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800351 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700352 int mInputMethodAnimLayerAdjustment;
353
Robert Carrb1579c82017-09-05 14:54:47 -0700354 private final SurfaceSession mSession = new SurfaceSession();
355
356 /**
357 * We organize all top-level Surfaces in to the following layers.
358 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800359 * and omitted from Screen-Magnification, for example the strict mode flash or
360 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700361 * {@link #mWindowingLayer} contains everything else.
362 */
363 private SurfaceControl mOverlayLayer;
364
365 /**
366 * See {@link #mOverlayLayer}
367 */
368 private SurfaceControl mWindowingLayer;
369
370 /**
371 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
372 * <p>
373 * For these surfaces currently we use a surface based on the larger of width or height so we
374 * don't have to resize when rotating the display.
375 */
376 private int mSurfaceSize;
377
378 /**
379 * A list of surfaces to be destroyed after {@link #mPendingTransaction} is applied.
380 */
381 private final ArrayList<SurfaceControl> mPendingDestroyingSurfaces = new ArrayList<>();
382
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800383 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
384 WindowStateAnimator winAnimator = w.mWinAnimator;
385 if (winAnimator.hasSurface()) {
386 final boolean wasAnimating = winAnimator.mWasAnimating;
387 final boolean nowAnimating = winAnimator.stepAnimationLocked(
388 mTmpWindowAnimator.mCurrentTime);
389 winAnimator.mWasAnimating = nowAnimating;
390 mTmpWindowAnimator.orAnimating(nowAnimating);
391
392 if (DEBUG_WALLPAPER) Slog.v(TAG,
393 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
394
395 if (wasAnimating && !winAnimator.mAnimating
396 && mWallpaperController.isWallpaperTarget(w)) {
397 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
398 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
399 if (DEBUG_LAYOUT_REPEATS) {
400 mService.mWindowPlacerLocked.debugLayoutRepeats(
401 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
402 }
403 }
404 }
405
406 final AppWindowToken atoken = w.mAppToken;
407 if (winAnimator.mDrawState == READY_TO_SHOW) {
408 if (atoken == null || atoken.allDrawn) {
409 if (w.performShowLocked()) {
410 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
411 if (DEBUG_LAYOUT_REPEATS) {
412 mService.mWindowPlacerLocked.debugLayoutRepeats(
413 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
414 }
415 }
416 }
417 }
418 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
419 if (appAnimator != null && appAnimator.thumbnail != null) {
420 if (appAnimator.thumbnailTransactionSeq
421 != mTmpWindowAnimator.mAnimTransactionSequence) {
422 appAnimator.thumbnailTransactionSeq =
423 mTmpWindowAnimator.mAnimTransactionSequence;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800424 }
425 }
426 };
427
428 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
429 final WindowStateAnimator winAnimator = w.mWinAnimator;
430 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
431 return;
432 }
433
434 final int flags = w.mAttrs.flags;
435
436 // If this window is animating, make a note that we have an animating window and take
437 // care of a request to run a detached wallpaper animation.
438 if (winAnimator.mAnimating) {
439 if (winAnimator.mAnimation != null) {
440 if ((flags & FLAG_SHOW_WALLPAPER) != 0
441 && winAnimator.mAnimation.getDetachWallpaper()) {
442 mTmpWindow = w;
443 }
444 final int color = winAnimator.mAnimation.getBackgroundColor();
445 if (color != 0) {
446 final TaskStack stack = w.getStack();
447 if (stack != null) {
448 stack.setAnimationBackground(winAnimator, color);
449 }
450 }
451 }
452 mTmpWindowAnimator.setAnimating(true);
453 }
454
455 // If this window's app token is running a detached wallpaper animation, make a note so
456 // we can ensure the wallpaper is displayed behind it.
457 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
458 if (appAnimator != null && appAnimator.animation != null
459 && appAnimator.animating) {
460 if ((flags & FLAG_SHOW_WALLPAPER) != 0
461 && appAnimator.animation.getDetachWallpaper()) {
462 mTmpWindow = w;
463 }
464
465 final int color = appAnimator.animation.getBackgroundColor();
466 if (color != 0) {
467 final TaskStack stack = w.getStack();
468 if (stack != null) {
469 stack.setAnimationBackground(winAnimator, color);
470 }
471 }
472 }
473 };
474
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800475 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
476 final int lostFocusUid = mTmpWindow.mOwnerUid;
477 final Handler handler = mService.mH;
478 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
479 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
480 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
481 w.mAttrs.hideTimeoutMilliseconds);
482 }
483 }
484 };
485
486 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
487 final AppWindowToken focusedApp = mService.mFocusedApp;
488 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
489 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
490
491 if (!w.canReceiveKeys()) {
492 return false;
493 }
494
495 final AppWindowToken wtoken = w.mAppToken;
496
497 // If this window's application has been removed, just skip it.
498 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
499 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
500 + (wtoken.removed ? "removed" : "sendingToBottom"));
501 return false;
502 }
503
504 if (focusedApp == null) {
505 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
506 + " using new focus @ " + w);
507 mTmpWindow = w;
508 return true;
509 }
510
511 if (!focusedApp.windowsAreFocusable()) {
512 // Current focused app windows aren't focusable...
513 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
514 + " focusable using new focus @ " + w);
515 mTmpWindow = w;
516 return true;
517 }
518
519 // Descend through all of the app tokens and find the first that either matches
520 // win.mAppToken (return win) or mFocusedApp (return null).
521 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
522 if (focusedApp.compareTo(wtoken) > 0) {
523 // App stack below focused app stack. No focus for you!!!
524 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
525 "findFocusedWindow: Reached focused app=" + focusedApp);
526 mTmpWindow = null;
527 return true;
528 }
529 }
530
531 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
532 mTmpWindow = w;
533 return true;
534 };
535
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800536 private final Consumer<WindowState> mPerformLayout = w -> {
537 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
538 // wasting time and funky changes while a window is animating away.
539 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
540 || w.isGoneForLayoutLw();
541
542 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
543 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
544 + " mLayoutAttached=" + w.mLayoutAttached
545 + " screen changed=" + w.isConfigChanged());
546 final AppWindowToken atoken = w.mAppToken;
547 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
548 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
549 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
550 + " parentHidden=" + w.isParentWindowHidden());
551 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
552 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
553 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
554 + " parentHidden=" + w.isParentWindowHidden());
555 }
556
557 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
558 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
559 // since that means "perform layout as normal, just don't display").
560 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
561 || ((w.isConfigChanged() || w.setReportResizeHints())
562 && !w.isGoneForLayoutLw() &&
563 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
564 (w.mHasSurface && w.mAppToken != null &&
565 w.mAppToken.layoutConfigChanges)))) {
566 if (!w.mLayoutAttached) {
567 if (mTmpInitial) {
568 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
569 w.mContentChanged = false;
570 }
571 if (w.mAttrs.type == TYPE_DREAM) {
572 // Don't layout windows behind a dream, so that if it does stuff like hide
573 // the status bar we won't get a bad transition when it goes away.
574 mTmpWindow = w;
575 }
576 w.mLayoutNeeded = false;
577 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200578 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000579 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800580 w.mLayoutSeq = mService.mLayoutSeq;
581
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200582 // If this is the first layout, we need to initialize the last inset values as
583 // otherwise we'd immediately cause an unnecessary resize.
584 if (firstLayout) {
585 w.updateLastInsetValues();
586 }
587
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800588 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
589 + " mContainingFrame=" + w.mContainingFrame
590 + " mDisplayFrame=" + w.mDisplayFrame);
591 }
592 }
593 };
594
595 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
596 if (w.mLayoutAttached) {
597 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
598 + " mViewVisibility=" + w.mViewVisibility
599 + " mRelayoutCalled=" + w.mRelayoutCalled);
600 // If this view is GONE, then skip it -- keep the current frame, and let the caller
601 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
602 // windows, since that means "perform layout as normal, just don't display").
603 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
604 return;
605 }
606 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
607 || w.mLayoutNeeded) {
608 if (mTmpInitial) {
609 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
610 w.mContentChanged = false;
611 }
612 w.mLayoutNeeded = false;
613 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000614 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800615 w.mLayoutSeq = mService.mLayoutSeq;
616 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
617 + " mContainingFrame=" + w.mContainingFrame
618 + " mDisplayFrame=" + w.mDisplayFrame);
619 }
620 } else if (w.mAttrs.type == TYPE_DREAM) {
621 // Don't layout windows behind a dream, so that if it does stuff like hide the
622 // status bar we won't get a bad transition when it goes away.
623 mTmpWindow = mTmpWindow2;
624 }
625 };
626
627 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
628 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
629 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
630 return w.canBeImeTarget();
631 };
632
633 private final Consumer<WindowState> mApplyPostLayoutPolicy =
634 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
635 mService.mInputMethodTarget);
636
637 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
638 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
639 final boolean obscuredChanged = w.mObscured !=
640 mTmpApplySurfaceChangesTransactionState.obscured;
641 final RootWindowContainer root = mService.mRoot;
642 // Only used if default window
643 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
644
645 // Update effect.
646 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
647 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
648 final boolean isDisplayed = w.isDisplayedLw();
649
650 if (isDisplayed && w.isObscuringDisplay()) {
651 // This window completely covers everything behind it, so we want to leave all
652 // of them as undimmed (for performance reasons).
653 root.mObscuringWindow = w;
654 mTmpApplySurfaceChangesTransactionState.obscured = true;
655 }
656
657 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
658 root.handleNotObscuredLocked(w,
659 mTmpApplySurfaceChangesTransactionState.obscured,
660 mTmpApplySurfaceChangesTransactionState.syswin);
661
662 if (w.mHasSurface && isDisplayed) {
663 final int type = w.mAttrs.type;
664 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
665 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
666 mTmpApplySurfaceChangesTransactionState.syswin = true;
667 }
668 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
669 && w.mAttrs.preferredRefreshRate != 0) {
670 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
671 = w.mAttrs.preferredRefreshRate;
672 }
673 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
674 && w.mAttrs.preferredDisplayModeId != 0) {
675 mTmpApplySurfaceChangesTransactionState.preferredModeId
676 = w.mAttrs.preferredDisplayModeId;
677 }
678 }
679 }
680
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800681 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
682 && mWallpaperController.isWallpaperTarget(w)) {
683 // This is the wallpaper target and its obscured state changed... make sure the
684 // current wallpaper's visibility has been updated accordingly.
685 mWallpaperController.updateWallpaperVisibility();
686 }
687
688 w.handleWindowMovedIfNeeded();
689
690 final WindowStateAnimator winAnimator = w.mWinAnimator;
691
692 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
693 w.mContentChanged = false;
694
695 // Moved from updateWindowsAndWallpaperLocked().
696 if (w.mHasSurface) {
697 // Take care of the window being ready to display.
698 final boolean committed = winAnimator.commitFinishDrawingLocked();
699 if (isDefaultDisplay && committed) {
700 if (w.mAttrs.type == TYPE_DREAM) {
701 // HACK: When a dream is shown, it may at that point hide the lock screen.
702 // So we need to redo the layout to let the phone window manager make this
703 // happen.
704 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
705 if (DEBUG_LAYOUT_REPEATS) {
706 surfacePlacer.debugLayoutRepeats(
707 "dream and commitFinishDrawingLocked true",
708 pendingLayoutChanges);
709 }
710 }
711 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
712 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
713 "First draw done in potential wallpaper target " + w);
714 root.mWallpaperMayChange = true;
715 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
716 if (DEBUG_LAYOUT_REPEATS) {
717 surfacePlacer.debugLayoutRepeats(
718 "wallpaper and commitFinishDrawingLocked true",
719 pendingLayoutChanges);
720 }
721 }
722 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700723 final TaskStack stack = w.getStack();
724 if ((!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening())
725 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800726 // Updates the shown frame before we set up the surface. This is needed
727 // because the resizing could change the top-left position (in addition to
728 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
729 // position the surface.
730 //
731 // If an animation is being started, we can't call this method because the
732 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700733 // we do want to update the position if the window is animating. We make an exception
734 // for the bounds animating state, where an application may have been waiting
735 // for an exit animation to start, but instead enters PiP. We need to ensure
736 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800737 winAnimator.computeShownFrameLocked();
738 }
739 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
740 }
741
742 final AppWindowToken atoken = w.mAppToken;
743 if (atoken != null) {
744 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
745 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
746 mTmpUpdateAllDrawn.add(atoken);
747 }
748 }
749
750 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
751 && w.isDisplayedLw()) {
752 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
753 }
754
755 w.updateResizingWindowIfNeeded();
756 };
757
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800758 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800759 * Create new {@link DisplayContent} instance, add itself to the root window container and
760 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700761 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800762 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700763 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
764 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700765 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700766 DisplayContent(Display display, WindowManagerService service,
Robert Carrb1579c82017-09-05 14:54:47 -0700767 WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800768 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
769 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
770 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
771 + " new=" + display);
772 }
773
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700774 mDisplay = display;
775 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700776 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700777 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700778 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700779 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700780 mService = service;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000781 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700782 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800783 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700784 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700785
Robert Carrb1579c82017-09-05 14:54:47 -0700786 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth);
787
788 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
789 .setSize(mSurfaceSize, mSurfaceSize)
790 .setOpaque(true);
791 mWindowingLayer = b.setName("Display Root").build();
792 mOverlayLayer = b.setName("Display Overlays").build();
793
Robert Carrf59b8dd2017-10-02 18:58:36 -0700794 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700795 .setLayerStack(mWindowingLayer, mDisplayId)
796 .show(mWindowingLayer)
797 .setLayer(mOverlayLayer, 1)
798 .setLayerStack(mOverlayLayer, mDisplayId)
799 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700800 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700801
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700802 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700803 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700804 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700805 super.addChild(mAboveAppWindowsContainers, null);
806 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800807
808 // Add itself as a child to the root container.
809 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700810
811 // TODO(b/62541591): evaluate whether this is the best spot to declare the
812 // {@link DisplayContent} ready for use.
813 mDisplayReady = true;
814 }
815
816 boolean isReady() {
817 // The display is ready when the system and the individual display are both ready.
818 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700819 }
820
821 int getDisplayId() {
822 return mDisplayId;
823 }
824
Wale Ogunwale02319a62016-09-26 15:21:22 -0700825 WindowToken getWindowToken(IBinder binder) {
826 return mTokenMap.get(binder);
827 }
828
829 AppWindowToken getAppWindowToken(IBinder binder) {
830 final WindowToken token = getWindowToken(binder);
831 if (token == null) {
832 return null;
833 }
834 return token.asAppWindowToken();
835 }
836
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700837 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700838 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
839 if (dc != null) {
840 // We currently don't support adding a window token to the display if the display
841 // already has the binder mapped to another token. If there is a use case for supporting
842 // this moving forward we will either need to merge the WindowTokens some how or have
843 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700844 throw new IllegalArgumentException("Can't map token=" + token + " to display="
845 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700846 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700847 if (binder == null) {
848 throw new IllegalArgumentException("Can't map token=" + token + " to display="
849 + getName() + " binder is null");
850 }
851 if (token == null) {
852 throw new IllegalArgumentException("Can't map null token to display="
853 + getName() + " binder=" + binder);
854 }
855
Wale Ogunwale02319a62016-09-26 15:21:22 -0700856 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700857
858 if (token.asAppWindowToken() == null) {
859 // Add non-app token to container hierarchy on the display. App tokens are added through
860 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700861 switch (token.windowType) {
862 case TYPE_WALLPAPER:
863 mBelowAppWindowsContainers.addChild(token);
864 break;
865 case TYPE_INPUT_METHOD:
866 case TYPE_INPUT_METHOD_DIALOG:
867 mImeWindowsContainers.addChild(token);
868 break;
869 default:
870 mAboveAppWindowsContainers.addChild(token);
871 break;
872 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700873 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700874 }
875
876 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700877 final WindowToken token = mTokenMap.remove(binder);
878 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800879 token.setExiting();
880 }
881 return token;
882 }
883
884 /** Changes the display the input window token is housed on to this one. */
885 void reParentWindowToken(WindowToken token) {
886 final DisplayContent prevDc = token.getDisplayContent();
887 if (prevDc == this) {
888 return;
889 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800890 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
891 && token.asAppWindowToken() == null) {
892 // Removed the token from the map, but made sure it's not an app token before removing
893 // from parent.
894 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700895 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800896
897 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700898 }
899
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700900 void removeAppToken(IBinder binder) {
901 final WindowToken token = removeWindowToken(binder);
902 if (token == null) {
903 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
904 return;
905 }
906
907 final AppWindowToken appToken = token.asAppWindowToken();
908
909 if (appToken == null) {
910 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
911 return;
912 }
913
914 appToken.onRemovedFromDisplay();
915 }
916
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700917 Display getDisplay() {
918 return mDisplay;
919 }
920
Craig Mautner59c00972012-07-30 12:10:24 -0700921 DisplayInfo getDisplayInfo() {
922 return mDisplayInfo;
923 }
924
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700925 DisplayMetrics getDisplayMetrics() {
926 return mDisplayMetrics;
927 }
928
Andrii Kulian8ee72852017-03-10 10:36:45 -0800929 int getRotation() {
930 return mRotation;
931 }
932
933 void setRotation(int newRotation) {
934 mRotation = newRotation;
935 }
936
937 int getLastOrientation() {
938 return mLastOrientation;
939 }
940
941 void setLastOrientation(int orientation) {
942 mLastOrientation = orientation;
943 }
944
945 boolean getAltOrientation() {
946 return mAltOrientation;
947 }
948
949 void setAltOrientation(boolean altOrientation) {
950 mAltOrientation = altOrientation;
951 }
952
953 int getLastWindowForcedOrientation() {
954 return mLastWindowForcedOrientation;
955 }
956
Andrii Kulian06d07d62017-03-14 11:11:47 -0700957 /**
958 * Update rotation of the display.
959 *
960 * Returns true if the rotation has been changed. In this case YOU MUST CALL
961 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
962 */
963 boolean updateRotationUnchecked(boolean inTransaction) {
964 if (mService.mDeferredRotationPauseCount > 0) {
965 // Rotation updates have been paused temporarily. Defer the update until
966 // updates have been resumed.
967 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
968 return false;
969 }
970
971 ScreenRotationAnimation screenRotationAnimation =
972 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
973 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
974 // Rotation updates cannot be performed while the previous rotation change
975 // animation is still in progress. Skip this update. We will try updating
976 // again after the animation is finished and the display is unfrozen.
977 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
978 return false;
979 }
980 if (mService.mDisplayFrozen) {
981 // Even if the screen rotation animation has finished (e.g. isAnimating
982 // returns false), there is still some time where we haven't yet unfrozen
983 // the display. We also need to abort rotation here.
984 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
985 "Deferring rotation, still finishing previous rotation");
986 return false;
987 }
988
989 if (!mService.mDisplayEnabled) {
990 // No point choosing a rotation if the display is not enabled.
991 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
992 return false;
993 }
994
995 final int oldRotation = mRotation;
996 final int lastOrientation = mLastOrientation;
997 final boolean oldAltOrientation = mAltOrientation;
998 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr0e007272017-10-02 13:00:55 -0700999 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
Robert Carr897215d2017-03-29 12:25:50 -07001000 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001001
Robert Carr0e007272017-10-02 13:00:55 -07001002 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001003 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
1004 if (seamlessRotated != null) {
1005 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1006 // to complete (that is, waiting for windows to redraw). It's tempting to check
1007 // w.mSeamlessRotationCount but that could be incorrect in the case of
1008 // window-removal.
1009 return false;
1010 }
Robert Carr0e007272017-10-02 13:00:55 -07001011
1012 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001013 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001014 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001015 mayRotateSeamlessly = false;
1016 }
1017 for (int i = 0; i < mService.mSessions.size(); i++) {
1018 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1019 mayRotateSeamlessly = false;
1020 break;
1021 }
1022 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001023 }
Robert Carr0e007272017-10-02 13:00:55 -07001024 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001025
1026 // TODO: Implement forced rotation changes.
1027 // Set mAltOrientation to indicate that the application is receiving
1028 // an orientation that has different metrics than it expected.
1029 // eg. Portrait instead of Landscape.
1030
1031 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
1032 lastOrientation, rotation);
1033
1034 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
1035 + ", got rotation " + rotation + " which has "
1036 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1037
1038 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1039 // No change.
1040 return false;
1041 }
1042
1043 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
1044 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1045 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1046
1047 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1048 mService.mWaitingForConfig = true;
1049 }
1050
1051 mRotation = rotation;
1052 mAltOrientation = altOrientation;
1053 if (isDefaultDisplay) {
1054 mService.mPolicy.setRotationLw(rotation);
1055 }
1056
1057 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1058 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1059 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1060 WINDOW_FREEZE_TIMEOUT_DURATION);
1061
1062 setLayoutNeeded();
1063 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001064 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001065
1066 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001067 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001068 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1069 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1070 mDisplayId);
1071 } else {
1072 // The screen rotation animation uses a screenshot to freeze the screen
1073 // while windows resize underneath.
1074 // When we are rotating seamlessly, we allow the elements to transition
1075 // to their rotated state independently and without a freeze required.
1076 screenRotationAnimation = null;
1077
1078 // We have to reset this in case a window was removed before it
1079 // finished seamless rotation.
1080 mService.mSeamlessRotationCount = 0;
1081 }
1082
1083 // We need to update our screen size information to match the new rotation. If the rotation
1084 // has actually changed then this method will return true and, according to the comment at
1085 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1086 // By updating the Display info here it will be available to
1087 // #computeScreenConfiguration() later.
1088 updateDisplayAndOrientation(getConfiguration().uiMode);
1089
1090 if (!inTransaction) {
1091 if (SHOW_TRANSACTIONS) {
1092 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1093 }
1094 mService.openSurfaceTransaction();
1095 }
1096 try {
1097 // NOTE: We disable the rotation in the emulator because
1098 // it doesn't support hardware OpenGL emulation yet.
1099 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1100 && screenRotationAnimation.hasScreenshot()) {
Robert Carrb1579c82017-09-05 14:54:47 -07001101 if (screenRotationAnimation.setRotationInTransaction(rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001102 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1103 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1104 mService.scheduleAnimationLocked();
1105 }
1106 }
1107
1108 if (rotateSeamlessly) {
1109 forAllWindows(w -> {
1110 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1111 }, true /* traverseTopToBottom */);
1112 }
1113
1114 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1115 } finally {
1116 if (!inTransaction) {
Adrian Roos111aff92017-09-27 18:11:46 +02001117 mService.closeSurfaceTransaction("setRotationUnchecked");
Andrii Kulian06d07d62017-03-14 11:11:47 -07001118 if (SHOW_LIGHT_TRANSACTIONS) {
1119 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1120 }
1121 }
1122 }
1123
1124 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001125 if (w.mHasSurface && !rotateSeamlessly) {
1126 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001127 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001128 mService.mRoot.mOrientationChangeComplete = false;
1129 w.mLastFreezeDuration = 0;
1130 }
1131 w.mReportOrientationChanged = true;
1132 }, true /* traverseTopToBottom */);
1133
1134 if (rotateSeamlessly) {
1135 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1136 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1137 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1138 }
1139
1140 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1141 final WindowManagerService.RotationWatcher rotationWatcher
1142 = mService.mRotationWatchers.get(i);
1143 if (rotationWatcher.mDisplayId == mDisplayId) {
1144 try {
1145 rotationWatcher.mWatcher.onRotationChanged(rotation);
1146 } catch (RemoteException e) {
1147 // Ignore
1148 }
1149 }
1150 }
1151
1152 // TODO (multi-display): Magnification is supported only for the default display.
1153 // Announce rotation only if we will not animate as we already have the
1154 // windows in final state. Otherwise, we make this call at the rotation end.
1155 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1156 && isDefaultDisplay) {
1157 mService.mAccessibilityController.onRotationChangedLocked(this);
1158 }
1159
1160 return true;
1161 }
1162
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001163 void configureDisplayPolicy() {
1164 mService.mPolicy.setInitialDisplaySize(getDisplay(),
1165 mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1166
1167 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
1168 }
1169
Andrii Kulian06d07d62017-03-14 11:11:47 -07001170 /**
1171 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1172 * changed.
1173 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1174 */
1175 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1176 // Use the effective "visual" dimensions based on current rotation
1177 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1178 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1179 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1180 int dw = realdw;
1181 int dh = realdh;
1182
1183 if (mAltOrientation) {
1184 if (realdw > realdh) {
1185 // Turn landscape into portrait.
1186 int maxw = (int)(realdh/1.3f);
1187 if (maxw < realdw) {
1188 dw = maxw;
1189 }
1190 } else {
1191 // Turn portrait into landscape.
1192 int maxh = (int)(realdw/1.3f);
1193 if (maxh < realdh) {
1194 dh = maxh;
1195 }
1196 }
1197 }
1198
1199 // Update application display metrics.
1200 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1201 mDisplayId);
1202 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1203 mDisplayId);
1204 mDisplayInfo.rotation = mRotation;
1205 mDisplayInfo.logicalWidth = dw;
1206 mDisplayInfo.logicalHeight = dh;
1207 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1208 mDisplayInfo.appWidth = appWidth;
1209 mDisplayInfo.appHeight = appHeight;
1210 if (isDefaultDisplay) {
1211 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1212 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1213 }
1214 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1215 if (mDisplayScalingDisabled) {
1216 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1217 } else {
1218 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1219 }
1220
1221 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1222 mDisplayInfo);
1223
1224 mBaseDisplayRect.set(0, 0, dw, dh);
1225
1226 if (isDefaultDisplay) {
1227 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1228 mCompatDisplayMetrics);
1229 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001230
1231 updateBounds();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001232 return mDisplayInfo;
1233 }
1234
1235 /**
1236 * Compute display configuration based on display properties and policy settings.
1237 * Do not call if mDisplayReady == false.
1238 */
1239 void computeScreenConfiguration(Configuration config) {
1240 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1241
1242 final int dw = displayInfo.logicalWidth;
1243 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001244 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1245 // TODO: Probably best to set this based on some setting in the display content object,
1246 // so the display can be configured for things like fullscreen.
1247 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001248
Andrii Kulian06d07d62017-03-14 11:11:47 -07001249 config.screenWidthDp =
1250 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1251 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1252 config.screenHeightDp =
1253 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1254 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001255
1256 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1257 final int leftInset = mTmpRect.left;
1258 final int topInset = mTmpRect.top;
1259 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001260 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1261 leftInset + displayInfo.appWidth /* right */,
1262 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001263 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1264 || displayInfo.rotation == Surface.ROTATION_270);
1265
1266 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1267 mDisplayMetrics.density, config);
1268
1269 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1270 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1271 ? Configuration.SCREENLAYOUT_ROUND_YES
1272 : Configuration.SCREENLAYOUT_ROUND_NO);
1273
1274 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1275 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1276 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1277 dh, mDisplayId);
1278 config.densityDpi = displayInfo.logicalDensityDpi;
1279
1280 config.colorMode =
1281 (displayInfo.isHdr()
1282 ? Configuration.COLOR_MODE_HDR_YES
1283 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001284 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001285 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1286 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1287
1288 // Update the configuration based on available input devices, lid switch,
1289 // and platform configuration.
1290 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1291 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1292 config.navigation = Configuration.NAVIGATION_NONAV;
1293
1294 int keyboardPresence = 0;
1295 int navigationPresence = 0;
1296 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1297 final int len = devices != null ? devices.length : 0;
1298 for (int i = 0; i < len; i++) {
1299 InputDevice device = devices[i];
1300 if (!device.isVirtual()) {
1301 final int sources = device.getSources();
1302 final int presenceFlag = device.isExternal() ?
1303 WindowManagerPolicy.PRESENCE_EXTERNAL :
1304 WindowManagerPolicy.PRESENCE_INTERNAL;
1305
1306 // TODO(multi-display): Configure on per-display basis.
1307 if (mService.mIsTouchDevice) {
1308 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1309 InputDevice.SOURCE_TOUCHSCREEN) {
1310 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1311 }
1312 } else {
1313 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1314 }
1315
1316 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1317 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1318 navigationPresence |= presenceFlag;
1319 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1320 && config.navigation == Configuration.NAVIGATION_NONAV) {
1321 config.navigation = Configuration.NAVIGATION_DPAD;
1322 navigationPresence |= presenceFlag;
1323 }
1324
1325 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1326 config.keyboard = Configuration.KEYBOARD_QWERTY;
1327 keyboardPresence |= presenceFlag;
1328 }
1329 }
1330 }
1331
1332 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1333 config.navigation = Configuration.NAVIGATION_DPAD;
1334 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1335 }
1336
1337 // Determine whether a hard keyboard is available and enabled.
1338 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1339 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1340 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1341 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1342 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1343 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1344 }
1345
1346 // Let the policy update hidden states.
1347 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1348 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1349 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1350 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1351 }
1352
1353 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1354 int displayId) {
1355 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1356 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1357 final int unrotDw, unrotDh;
1358 if (rotated) {
1359 unrotDw = dh;
1360 unrotDh = dw;
1361 } else {
1362 unrotDw = dw;
1363 unrotDh = dh;
1364 }
1365 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1366 displayId);
1367 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1368 displayId);
1369 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1370 displayId);
1371 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1372 displayId);
1373 return sw;
1374 }
1375
1376 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1377 DisplayMetrics dm, int dw, int dh, int displayId) {
1378 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1379 displayId);
1380 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1381 uiMode, displayId);
1382 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1383 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1384 if (curSize == 0 || size < curSize) {
1385 curSize = size;
1386 }
1387 return curSize;
1388 }
1389
1390 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1391 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1392
1393 // We need to determine the smallest width that will occur under normal
1394 // operation. To this, start with the base screen size and compute the
1395 // width under the different possible rotations. We need to un-rotate
1396 // the current screen dimensions before doing this.
1397 int unrotDw, unrotDh;
1398 if (rotated) {
1399 unrotDw = dh;
1400 unrotDh = dw;
1401 } else {
1402 unrotDw = dw;
1403 unrotDh = dh;
1404 }
1405 displayInfo.smallestNominalAppWidth = 1<<30;
1406 displayInfo.smallestNominalAppHeight = 1<<30;
1407 displayInfo.largestNominalAppWidth = 0;
1408 displayInfo.largestNominalAppHeight = 0;
1409 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1410 unrotDh);
1411 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1412 unrotDw);
1413 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1414 unrotDh);
1415 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1416 unrotDw);
1417 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1418 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1419 displayId);
1420 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1421 displayId);
1422 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1423 displayId);
1424 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1425 displayId);
1426 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1427 outConfig.screenLayout = sl;
1428 }
1429
1430 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1431 int uiMode, int displayId) {
1432 // Get the app screen size at this rotation.
1433 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1434 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1435
1436 // Compute the screen layout size class for this rotation.
1437 int longSize = w;
1438 int shortSize = h;
1439 if (longSize < shortSize) {
1440 int tmp = longSize;
1441 longSize = shortSize;
1442 shortSize = tmp;
1443 }
1444 longSize = (int)(longSize/density);
1445 shortSize = (int)(shortSize/density);
1446 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1447 }
1448
1449 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1450 int uiMode, int dw, int dh) {
1451 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1452 displayId);
1453 if (width < displayInfo.smallestNominalAppWidth) {
1454 displayInfo.smallestNominalAppWidth = width;
1455 }
1456 if (width > displayInfo.largestNominalAppWidth) {
1457 displayInfo.largestNominalAppWidth = width;
1458 }
1459 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1460 displayId);
1461 if (height < displayInfo.smallestNominalAppHeight) {
1462 displayInfo.smallestNominalAppHeight = height;
1463 }
1464 if (height > displayInfo.largestNominalAppHeight) {
1465 displayInfo.largestNominalAppHeight = height;
1466 }
1467 }
1468
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001469 DockedStackDividerController getDockedDividerController() {
1470 return mDividerControllerLocked;
1471 }
1472
Winson Chung655332c2016-10-31 13:14:28 -07001473 PinnedStackController getPinnedStackController() {
1474 return mPinnedStackControllerLocked;
1475 }
1476
Jeff Browna506a6e2013-06-04 00:02:38 -07001477 /**
1478 * Returns true if the specified UID has access to this display.
1479 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001480 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001481 return mDisplay.hasAccess(uid);
1482 }
1483
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001484 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001485 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001486 }
1487
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001488 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001489 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001490 }
1491
Wale Ogunwale61911492017-10-11 08:50:50 -07001492 /**
1493 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1494 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001495 TaskStack getSplitScreenPrimaryStack() {
1496 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001497 return (stack != null && stack.isVisible()) ? stack : null;
1498 }
1499
1500 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001501 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001502 * not visible.
1503 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001504 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1505 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001506 }
1507
1508 TaskStack getPinnedStack() {
1509 return mTaskStackContainers.getPinnedStack();
1510 }
1511
1512 private boolean hasPinnedStack() {
1513 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001514 }
1515
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001516 /**
1517 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1518 * Null is no compatible stack on the display.
1519 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001520 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001521 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1522 }
1523
1524 /**
1525 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1526 * activity type. Null is no compatible stack on the display.
1527 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001528 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001529 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001530 }
1531
Bryce Lee48f4b572017-04-10 10:54:15 -07001532 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001533 TaskStack getTopStack() {
1534 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001535 }
1536
Wale Ogunwale61911492017-10-11 08:50:50 -07001537 void onStackWindowingModeChanged(TaskStack stack) {
1538 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001539 }
1540
Andrii Kulian441e4492016-09-29 15:25:00 -07001541 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001542 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001543 super.onConfigurationChanged(newParentConfig);
1544
Andrii Kulian3a507b52016-09-19 18:14:12 -07001545 // The display size information is heavily dependent on the resources in the current
1546 // configuration, so we need to reconfigure it every time the configuration changes.
1547 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1548 mService.reconfigureDisplayLocked(this);
1549
Bryce Leef3c6a472017-11-14 14:53:06 -08001550 final DockedStackDividerController dividerController = getDockedDividerController();
1551
1552 if (dividerController != null) {
1553 getDockedDividerController().onConfigurationChanged();
1554 }
1555
1556 final PinnedStackController pinnedStackController = getPinnedStackController();
1557
1558 if (pinnedStackController != null) {
1559 getPinnedStackController().onConfigurationChanged();
1560 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001561 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001562
Andrii Kulian441e4492016-09-29 15:25:00 -07001563 /**
1564 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1565 * bounds were updated.
1566 */
1567 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001568 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1569 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001570 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001571 changedStackList.add(stack.mStackId);
1572 }
1573 }
Winson Chung32c566f2017-04-11 18:31:21 -07001574
1575 // If there was no pinned stack, we still need to notify the controller of the display info
1576 // update as a result of the config change. We do this here to consolidate the flow between
1577 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001578 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001579 mPinnedStackControllerLocked.onDisplayInfoChanged();
1580 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001581 }
1582
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001583 @Override
1584 boolean fillsParent() {
1585 return true;
1586 }
1587
1588 @Override
1589 boolean isVisible() {
1590 return true;
1591 }
1592
1593 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001594 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001595 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001596 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001597 }
1598
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001599 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001600 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1601 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1602 // target, or here in order if there isn't an IME target.
1603 if (traverseTopToBottom) {
1604 for (int i = mChildren.size() - 1; i >= 0; --i) {
1605 final DisplayChildWindowContainer child = mChildren.get(i);
1606 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1607 // In this case the Ime windows will be processed above their target so we skip
1608 // here.
1609 continue;
1610 }
1611 if (child.forAllWindows(callback, traverseTopToBottom)) {
1612 return true;
1613 }
1614 }
1615 } else {
1616 final int count = mChildren.size();
1617 for (int i = 0; i < count; i++) {
1618 final DisplayChildWindowContainer child = mChildren.get(i);
1619 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1620 // In this case the Ime windows will be processed above their target so we skip
1621 // here.
1622 continue;
1623 }
1624 if (child.forAllWindows(callback, traverseTopToBottom)) {
1625 return true;
1626 }
1627 }
1628 }
1629 return false;
1630 }
1631
1632 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1633 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1634 }
1635
Wale Ogunwale399c8692017-05-08 14:22:42 -07001636 @Override
1637 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001638 final WindowManagerPolicy policy = mService.mPolicy;
1639
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001640 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001641 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001642 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001643 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001644 // If the display is frozen, some activities may be in the middle of restarting, and
1645 // thus have removed their old window. If the window has the flag to hide the lock
1646 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1647 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001648 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001649 } else if (policy.isKeyguardLocked()) {
1650 // Use the last orientation the while the display is frozen with the keyguard
1651 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1652 // window. We don't want to check the show when locked window directly though as
1653 // things aren't stable while the display is frozen, for example the window could be
1654 // momentarily unavailable due to activity relaunch.
1655 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001656 + "return " + mLastOrientation);
1657 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001658 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001659 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001660 final int orientation = mAboveAppWindowsContainers.getOrientation();
1661 if (orientation != SCREEN_ORIENTATION_UNSET) {
1662 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001663 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001664 }
1665
Wale Ogunwale399c8692017-05-08 14:22:42 -07001666 // Top system windows are not requesting an orientation. Start searching from apps.
1667 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001668 }
1669
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001670 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001671 // Check if display metrics changed and update base values if needed.
1672 updateBaseDisplayMetricsIfNeeded();
1673
Craig Mautner722285e2012-09-07 13:55:58 -07001674 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001675 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001676
Wale Ogunwale61911492017-10-11 08:50:50 -07001677 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1678 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001679 }
Craig Mautner722285e2012-09-07 13:55:58 -07001680 }
1681
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001682 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001683 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1684 if (displayManagerInternal != null) {
1685 // Bootstrap the default logical display from the display manager.
1686 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1687 if (newDisplayInfo != null) {
1688 mDisplayInfo.copyFrom(newDisplayInfo);
1689 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001690 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001691
Andrii Kuliancd097992017-03-23 18:31:59 -07001692 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1693 mDisplayInfo.logicalDensityDpi);
1694 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1695 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1696 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001697 }
1698
Andrii Kuliancd097992017-03-23 18:31:59 -07001699 /**
1700 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1701 * values.
1702 */
1703 private void updateBaseDisplayMetricsIfNeeded() {
1704 // Get real display metrics without overrides from WM.
1705 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1706 final int orientation = mDisplayInfo.rotation;
1707 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1708 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1709 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1710 final int newDensity = mDisplayInfo.logicalDensityDpi;
1711
1712 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1713 || mInitialDisplayHeight != newHeight
1714 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1715
1716 if (displayMetricsChanged) {
1717 // Check if display size or density is forced.
1718 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1719 || mBaseDisplayHeight != mInitialDisplayHeight;
1720 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1721
1722 // If there is an override set for base values - use it, otherwise use new values.
1723 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1724 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1725 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1726
1727 // Real display metrics changed, so we should also update initial values.
1728 mInitialDisplayWidth = newWidth;
1729 mInitialDisplayHeight = newHeight;
1730 mInitialDisplayDensity = newDensity;
1731 mService.reconfigureDisplayLocked(this);
1732 }
1733 }
1734
Bryce Lee27cec322017-03-21 09:41:37 -07001735 /** Sets the maximum width the screen resolution can be */
1736 void setMaxUiWidth(int width) {
1737 if (DEBUG_DISPLAY) {
1738 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1739 }
1740
1741 mMaxUiWidth = width;
1742
1743 // Update existing metrics.
1744 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1745 }
1746
1747 /** Update base (override) display metrics. */
1748 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1749 mBaseDisplayWidth = baseWidth;
1750 mBaseDisplayHeight = baseHeight;
1751 mBaseDisplayDensity = baseDensity;
1752
1753 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1754 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1755 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1756 mBaseDisplayWidth = mMaxUiWidth;
1757
1758 if (DEBUG_DISPLAY) {
1759 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1760 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1761 + " on display:" + getDisplayId());
1762 }
1763 }
1764
1765 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08001766
1767 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07001768 }
1769
Chong Zhangf66db432016-01-13 10:39:51 -08001770 void getContentRect(Rect out) {
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001771 out.set(mDisplayFrames.mContent);
Chong Zhangf66db432016-01-13 10:39:51 -08001772 }
1773
Wale Ogunwale61911492017-10-11 08:50:50 -07001774 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001775 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1776 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001777
Wale Ogunwale61911492017-10-11 08:50:50 -07001778 final TaskStack stack = new TaskStack(mService, stackId, controller);
1779 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001780 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001781 }
1782
Andrii Kulian51c1b672017-04-07 18:39:32 -07001783 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001784 final DisplayContent prevDc = stack.getDisplayContent();
1785 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001786 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1787 + " which is not currently attached to any display");
1788 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001789 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001790 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1791 + " to its current displayId=" + mDisplayId);
1792 }
1793
Wale Ogunwale61911492017-10-11 08:50:50 -07001794 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001795 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001796 }
1797
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001798 @Override
1799 protected void addChild(DisplayChildWindowContainer child,
1800 Comparator<DisplayChildWindowContainer> comparator) {
1801 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1802 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001803
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001804 @Override
1805 protected void addChild(DisplayChildWindowContainer child, int index) {
1806 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1807 }
1808
1809 @Override
1810 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001811 // Only allow removal of direct children from this display if the display is in the process
1812 // of been removed.
1813 if (mRemovingDisplay) {
1814 super.removeChild(child);
1815 return;
1816 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001817 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001818 }
1819
Andrii Kuliand2765632016-12-12 22:26:34 -08001820 @Override
1821 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1822 // Children of the display are statically ordered, so the real intention here is to perform
1823 // the operation on the display and not the static direct children.
1824 getParent().positionChildAt(position, this, includingParents);
1825 }
1826
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001827 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001828 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1829 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001830 final int taskId = stack.taskIdFromPoint(x, y);
1831 if (taskId != -1) {
1832 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001833 }
1834 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001835 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001836 }
1837
Chong Zhang8e89b312015-09-09 15:09:30 -07001838 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001839 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001840 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001841 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001842 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001843 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001844 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07001845 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1846 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001847 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001848 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001849 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001850
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001851 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1852 if (mTmpTaskForResizePointSearchResult.searchDone) {
1853 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001854 }
1855 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001856 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001857 }
1858
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001859 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001860 // The provided task is the task on this display with focus, so if WindowManagerService's
1861 // focused app is not on this display, focusedTask will be null.
1862 if (focusedTask == null) {
1863 mTouchExcludeRegion.setEmpty();
1864 } else {
1865 mTouchExcludeRegion.set(mBaseDisplayRect);
1866 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1867 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07001868 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1869 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001870 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
1871 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08001872 }
1873 // If we removed the focused task above, add it back and only leave its
1874 // outside touch area in the exclusion. TapDectector is not interested in
1875 // any touch inside the focused task itself.
1876 if (!mTmpRect2.isEmpty()) {
1877 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1878 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001879 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001880 final WindowState inputMethod = mService.mInputMethodWindow;
1881 if (inputMethod != null && inputMethod.isVisibleLw()) {
1882 // If the input method is visible and the user is typing, we don't want these touch
1883 // events to be intercepted and used to change focus. This would likely cause a
1884 // disappearance of the input method.
1885 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001886 if (inputMethod.getDisplayId() == mDisplayId) {
1887 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1888 } else {
1889 // IME is on a different display, so we need to update its tap detector.
1890 // TODO(multidisplay): Remove when IME will always appear on same display.
1891 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1892 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001893 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001894 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1895 WindowState win = mTapExcludedWindows.get(i);
1896 win.getTouchableRegion(mTmpRegion);
1897 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1898 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001899 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07001900 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001901 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001902 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001903 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1904 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001905 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001906 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001907 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001908 }
1909
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001910 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001911 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001912 super.switchUser();
1913 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001914 }
1915
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001916 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001917 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1918 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001919 }
1920 }
1921
Wale Ogunwale10124582016-09-15 20:25:50 -07001922 @Override
1923 void removeIfPossible() {
1924 if (isAnimating()) {
1925 mDeferredRemoval = true;
1926 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001927 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001928 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001929 }
1930
Wale Ogunwale10124582016-09-15 20:25:50 -07001931 @Override
1932 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001933 mRemovingDisplay = true;
1934 try {
1935 super.removeImmediately();
1936 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07001937 if (mService.canDispatchPointerEvents()) {
1938 if (mTapDetector != null) {
1939 mService.unregisterPointerEventListener(mTapDetector);
1940 }
1941 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
1942 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1943 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001944 }
Robert Carrb1579c82017-09-05 14:54:47 -07001945 // The pending transaction won't be applied so we should
1946 // just clean up any surfaces pending destruction.
1947 onPendingTransactionApplied();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001948 } finally {
1949 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001950 }
Craig Mautner95da1082014-02-24 17:54:35 -08001951 }
1952
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001953 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001954 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001955 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001956 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1957
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001958 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001959 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001960 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001961 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001962 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001963 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001964 }
1965
Andrii Kulian0214ed92017-05-16 13:44:05 -07001966 /** @return 'true' if removal of this display content is deferred due to active animation. */
1967 boolean isRemovalDeferred() {
1968 return mDeferredRemoval;
1969 }
1970
Wale Ogunwale10124582016-09-15 20:25:50 -07001971 boolean animateForIme(float interpolatedValue, float animationTarget,
1972 float dividerAnimationTarget) {
1973 boolean updated = false;
1974
Wale Ogunwale61911492017-10-11 08:50:50 -07001975 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1976 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001977 if (stack == null || !stack.isAdjustedForIme()) {
1978 continue;
1979 }
1980
1981 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1982 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1983 updated = true;
1984 } else {
1985 mDividerControllerLocked.mLastAnimationProgress =
1986 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1987 mDividerControllerLocked.mLastDividerProgress =
1988 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1989 updated |= stack.updateAdjustForIme(
1990 mDividerControllerLocked.mLastAnimationProgress,
1991 mDividerControllerLocked.mLastDividerProgress,
1992 false /* force */);
1993 }
1994 if (interpolatedValue >= 1f) {
1995 stack.endImeAdjustAnimation();
1996 }
1997 }
1998
1999 return updated;
2000 }
2001
2002 boolean clearImeAdjustAnimation() {
2003 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002004 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2005 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002006 if (stack != null && stack.isAdjustedForIme()) {
2007 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2008 changed = true;
2009 }
2010 }
2011 return changed;
2012 }
2013
2014 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002015 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2016 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002017 if (stack.isVisible() && stack.isAdjustedForIme()) {
2018 stack.beginImeAdjustAnimation();
2019 }
2020 }
2021 }
2022
2023 void adjustForImeIfNeeded() {
2024 final WindowState imeWin = mService.mInputMethodWindow;
2025 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2026 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002027 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002028 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2029 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2030 imeTargetStack.getDockSide() : DOCKED_INVALID;
2031 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2032 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2033 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002034 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002035 final boolean imeHeightChanged = imeVisible &&
2036 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2037
2038 // The divider could be adjusted for IME position, or be thinner than usual,
2039 // or both. There are three possible cases:
2040 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2041 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2042 // - If IME is not visible, divider is not moved and is normal width.
2043
2044 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002045 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2046 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002047 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002048 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2049 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002050 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2051 } else {
2052 stack.resetAdjustedForIme(false);
2053 }
2054 }
2055 mDividerControllerLocked.setAdjustedForIme(
2056 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2057 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002058 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2059 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002060 stack.resetAdjustedForIme(!dockVisible);
2061 }
2062 mDividerControllerLocked.setAdjustedForIme(
2063 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2064 }
Winson Chung655332c2016-10-31 13:14:28 -07002065 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002066 }
2067
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002068 void setInputMethodAnimLayerAdjustment(int adj) {
2069 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2070 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002071 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002072 }
2073
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002074 /**
2075 * If a window that has an animation specifying a colored background and the current wallpaper
2076 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2077 * suddenly disappear.
2078 */
2079 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002080 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2081 w -> w.mIsWallpaper && w.isVisibleNow());
2082
2083 if (visibleWallpaper != null) {
2084 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002085 }
2086 return winAnimator.mAnimLayer;
2087 }
2088
Wale Ogunwale10124582016-09-15 20:25:50 -07002089 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002090 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2091 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002092 stack.prepareFreezingTaskBounds();
2093 }
2094 }
2095
Wale Ogunwale94744212015-09-21 19:01:47 -07002096 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002097 getBounds(mTmpRect, newRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002098
2099 // Compute a transform matrix to undo the coordinate space transformation,
2100 // and present the window at the same physical position it previously occupied.
2101 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2102 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2103
2104 mTmpRectF.set(bounds);
2105 mTmpMatrix.mapRect(mTmpRectF);
2106 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002107 }
2108
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002109 static int deltaRotation(int oldRotation, int newRotation) {
2110 int delta = newRotation - oldRotation;
2111 if (delta < 0) delta += 4;
2112 return delta;
2113 }
2114
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002115 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002116 Matrix outMatrix) {
2117 // For rotations without Z-ordering we don't need the target rectangle's position.
2118 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2119 displayHeight, outMatrix);
2120 }
2121
2122 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2123 float displayWidth, float displayHeight, Matrix outMatrix) {
2124 switch (rotation) {
2125 case ROTATION_0:
2126 outMatrix.reset();
2127 break;
2128 case ROTATION_270:
2129 outMatrix.setRotate(270, 0, 0);
2130 outMatrix.postTranslate(0, displayHeight);
2131 outMatrix.postTranslate(rectTop, 0);
2132 break;
2133 case ROTATION_180:
2134 outMatrix.reset();
2135 break;
2136 case ROTATION_90:
2137 outMatrix.setRotate(90, 0, 0);
2138 outMatrix.postTranslate(displayWidth, 0);
2139 outMatrix.postTranslate(-rectTop, rectLeft);
2140 break;
2141 }
2142 }
2143
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002144 @CallSuper
2145 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002146 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002147 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002148 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002149 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002150 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2151 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002152 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002153 }
2154 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2155 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002156 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2157 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002158 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002159 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002160 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2161 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002162 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002163 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002164 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2165 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002166 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002167 }
2168 proto.write(DPI, mBaseDisplayDensity);
2169 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002170 proto.write(ROTATION, mRotation);
2171 final ScreenRotationAnimation screenRotationAnimation =
2172 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2173 if (screenRotationAnimation != null) {
2174 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2175 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002176 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002177 proto.end(token);
2178 }
2179
Craig Mautnera91f9e22012-09-14 16:22:08 -07002180 public void dump(String prefix, PrintWriter pw) {
2181 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2182 final String subPrefix = " " + prefix;
2183 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2184 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2185 pw.print("dpi");
2186 if (mInitialDisplayWidth != mBaseDisplayWidth
2187 || mInitialDisplayHeight != mBaseDisplayHeight
2188 || mInitialDisplayDensity != mBaseDisplayDensity) {
2189 pw.print(" base=");
2190 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2191 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2192 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002193 if (mDisplayScalingDisabled) {
2194 pw.println(" noscale");
2195 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002196 pw.print(" cur=");
2197 pw.print(mDisplayInfo.logicalWidth);
2198 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2199 pw.print(" app=");
2200 pw.print(mDisplayInfo.appWidth);
2201 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2202 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2203 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2204 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2205 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002206 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002207 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002208 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002209
Craig Mautnerdc548482014-02-05 13:35:24 -08002210 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002211 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002212 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2213 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002214 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002215 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002216
Craig Mautnerdc548482014-02-05 13:35:24 -08002217 pw.println();
2218 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002219 pw.println();
2220 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002221 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002222 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002223 pw.print(" Exiting #"); pw.print(i);
2224 pw.print(' '); pw.print(token);
2225 pw.println(':');
2226 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002227 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002228 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002229
Jorim Jaggi31f71702016-05-04 16:43:04 -07002230 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002231
2232 // Dump stack references
2233 final TaskStack homeStack = getHomeStack();
2234 if (homeStack != null) {
2235 pw.println(prefix + "homeStack=" + homeStack.getName());
2236 }
2237 final TaskStack pinnedStack = getPinnedStack();
2238 if (pinnedStack != null) {
2239 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2240 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002241 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002242 if (splitScreenPrimaryStack != null) {
2243 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2244 }
2245
2246 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002247 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002248 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002249 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002250
2251 if (mInputMethodAnimLayerAdjustment != 0) {
2252 pw.println(subPrefix
2253 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2254 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002255
2256 pw.println();
2257 mDisplayFrames.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002258 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002259
2260 @Override
2261 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002262 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002263 }
2264
2265 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002266 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002267 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002268
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002269 /** Returns true if the stack in the windowing mode is visible. */
2270 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002271 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002272 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002273 }
2274
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002275 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002276 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002277 final int x = (int) xf;
2278 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002279 final WindowState touchedWin = getWindow(w -> {
2280 final int flags = w.mAttrs.flags;
2281 if (!w.isVisibleLw()) {
2282 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002283 }
2284 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002285 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002286 }
2287
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002288 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002289 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002290 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002291 }
2292
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002293 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002294
2295 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002296 return mTmpRegion.contains(x, y) || touchFlags == 0;
2297 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002298
2299 return touchedWin;
2300 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002301
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002302 boolean canAddToastWindowForUid(int uid) {
2303 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002304 // Also if the app is focused adding more than one toast at
2305 // a time for better backwards compatibility.
2306 final WindowState focusedWindowForUid = getWindow(w ->
2307 w.mOwnerUid == uid && w.isFocused());
2308 if (focusedWindowForUid != null) {
2309 return true;
2310 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002311 final WindowState win = getWindow(w ->
2312 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2313 && !w.mWindowRemovalAllowed);
2314 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002315 }
2316
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002317 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002318 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2319 return;
2320 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002321
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002322 // Used to communicate the old focus to the callback method.
2323 mTmpWindow = oldFocus;
2324
2325 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002326 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002327
2328 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002329 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002330
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002331 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002332
2333 if (mTmpWindow == null) {
2334 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2335 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002336 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002337 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002338 }
2339
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002340 /** Updates the layer assignment of windows on this display. */
2341 void assignWindowLayers(boolean setLayoutNeeded) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07002342 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002343 if (setLayoutNeeded) {
2344 setLayoutNeeded();
2345 }
Robert Carrb1579c82017-09-05 14:54:47 -07002346
Robert Carrf59b8dd2017-10-02 18:58:36 -07002347 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002348 // the application of this transaction until the animation pass triggers
2349 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2350 // the hiding and showing of surfaces.
2351 scheduleAnimation();
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002352 }
2353
Wale Ogunwale1666e312016-12-16 11:27:18 -08002354 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2355 // moving containers or resizing them. Need to investigate the best way to have it automatically
2356 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002357 void layoutAndAssignWindowLayersIfNeeded() {
2358 mService.mWindowsChanged = true;
2359 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002360
2361 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2362 false /*updateInputWindows*/)) {
2363 assignWindowLayers(false /* setLayoutNeeded */);
2364 }
2365
2366 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2367 mService.mWindowPlacerLocked.performSurfacePlacement();
2368 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2369 }
2370
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002371 /** Returns true if a leaked surface was destroyed */
2372 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002373 // Used to indicate that a surface was leaked.
2374 mTmpWindow = null;
2375 forAllWindows(w -> {
2376 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002377 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002378 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002379 }
2380 if (!mService.mSessions.contains(wsa.mSession)) {
2381 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002382 + w + " surface=" + wsa.mSurfaceController
2383 + " token=" + w.mToken
2384 + " pid=" + w.mSession.mPid
2385 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002386 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002387 mService.mForceRemoves.add(w);
2388 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002389 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002390 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002391 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002392 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002393 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002394 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002395 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002396 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002397 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002398
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002399 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002400 }
2401
2402 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002403 * Determine and return the window that should be the IME target.
2404 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2405 * @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 +00002406 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002407 WindowState computeImeTarget(boolean updateImeTarget) {
2408 if (mService.mInputMethodWindow == null) {
2409 // There isn't an IME so there shouldn't be a target...That was easy!
2410 if (updateImeTarget) {
2411 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2412 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2413 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2414 }
2415 return null;
2416 }
2417
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002418 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2419 // same display. Or even when the current IME/target are not on the same screen as the next
2420 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002421 mUpdateImeTarget = updateImeTarget;
2422 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002423
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002424
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002425 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2426 // to be on top of it, but it is not -really- where input will go. So look down below
2427 // for a real window to target...
2428 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2429 final AppWindowToken token = target.mAppToken;
2430 if (token != null) {
2431 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2432 if (betterTarget != null) {
2433 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002434 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002435 }
2436 }
2437
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002438 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2439 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002440
2441 // Now, a special case -- if the last target's window is in the process of exiting, and is
2442 // above the new target, keep on the last target to avoid flicker. Consider for example a
2443 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2444 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2445 // scrim.
2446 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002447 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2448 && (target == null
2449 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002450 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002451 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002452 }
2453
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002454 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2455 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002456
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002457 if (target == null) {
2458 if (updateImeTarget) {
2459 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2460 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2461 + Debug.getCallers(4) : ""));
2462 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2463 }
2464
2465 return null;
2466 }
2467
2468 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002469 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2470 if (token != null) {
2471
2472 // Now some fun for dealing with window animations that modify the Z order. We need
2473 // to look at all windows below the current target that are in this app, finding the
2474 // highest visible one in layering.
2475 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002476 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002477 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002478 }
2479
2480 if (highestTarget != null) {
2481 final AppTransition appTransition = mService.mAppTransition;
2482 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2483 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2484 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002485 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002486
2487 if (appTransition.isTransitionSet()) {
2488 // If we are currently setting up for an animation, hold everything until we
2489 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002490 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2491 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002492 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002493 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002494 // If the window we are currently targeting is involved with an animation,
2495 // and it is on top of the next target we will be over, then hold off on
2496 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002497 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2498 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002499 }
2500 }
2501 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002502
2503 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2504 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2505 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002506 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002507 }
2508
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002509 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002510 }
2511
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002512 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2513 if (target == mService.mInputMethodTarget
2514 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2515 && mInputMethodAnimLayerAdjustment == layerAdj) {
2516 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002517 }
2518
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002519 mService.mInputMethodTarget = target;
2520 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2521 setInputMethodAnimLayerAdjustment(layerAdj);
2522 assignWindowLayers(false /* setLayoutNeeded */);
2523 }
2524
2525 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2526 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2527 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2528 }
2529
2530 // Used to indicate we have reached the first window in the range we are interested in.
2531 mTmpWindow = null;
2532
2533 // TODO: Figure-out a more efficient way to do this.
2534 final WindowState candidate = getWindow(w -> {
2535 if (w == top) {
2536 // Reached the first window in the range we are interested in.
2537 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002538 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002539 if (mTmpWindow == null) {
2540 return false;
2541 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002542
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002543 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2544 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002545 }
2546 // If we reached the bottom of the range of windows we are considering,
2547 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002548 if (w == bottom) {
2549 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002550 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002551 return false;
2552 });
2553
2554 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002555 }
2556
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002557 void setLayoutNeeded() {
2558 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2559 mLayoutNeeded = true;
2560 }
2561
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002562 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002563 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2564 mLayoutNeeded = false;
2565 }
2566
2567 boolean isLayoutNeeded() {
2568 return mLayoutNeeded;
2569 }
2570
Wale Ogunwale02319a62016-09-26 15:21:22 -07002571 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2572 if (mTokenMap.isEmpty()) {
2573 return;
2574 }
2575 pw.println(" Display #" + mDisplayId);
2576 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2577 while (it.hasNext()) {
2578 final WindowToken token = it.next();
2579 pw.print(" ");
2580 pw.print(token);
2581 if (dumpAll) {
2582 pw.println(':');
2583 token.dump(pw, " ");
2584 } else {
2585 pw.println();
2586 }
2587 }
2588 }
2589
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002590 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002591 final int[] index = new int[1];
2592 forAllWindows(w -> {
2593 final WindowStateAnimator wAnim = w.mWinAnimator;
2594 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2595 index[0] = index[0] + 1;
2596 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002597 }
2598
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002599 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002600 forAllWindows(w -> {
2601 w.mWinAnimator.enableSurfaceTrace(fd);
2602 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002603 }
2604
2605 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002606 forAllWindows(w -> {
2607 w.mWinAnimator.disableSurfaceTrace();
2608 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002609 }
2610
Jorim Jaggife762342016-10-13 14:33:27 +02002611 /**
2612 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2613 */
2614 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2615 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002616 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002617 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2618 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002619 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002620 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2621 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002622 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002623 }
2624
Wale Ogunwale494009b82016-10-21 09:01:38 -07002625 boolean checkWaitingForWindows() {
2626
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002627 mHaveBootMsg = false;
2628 mHaveApp = false;
2629 mHaveWallpaper = false;
2630 mHaveKeyguard = true;
2631
2632 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002633 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2634 return true;
2635 }
2636 if (w.isDrawnLw()) {
2637 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002638 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002639 } else if (w.mAttrs.type == TYPE_APPLICATION
2640 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002641 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002642 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002643 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002644 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002645 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002646 }
2647 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002648 return false;
2649 });
2650
2651 if (visibleWindow != null) {
2652 // We have a visible window.
2653 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002654 }
2655
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002656 // if the wallpaper service is disabled on the device, we're never going to have
2657 // wallpaper, don't bother waiting for it
2658 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2659 com.android.internal.R.bool.config_enableWallpaperService)
2660 && !mService.mOnlyCore;
2661
Wale Ogunwale494009b82016-10-21 09:01:38 -07002662 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2663 "******** booted=" + mService.mSystemBooted
2664 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002665 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2666 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2667 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002668
2669 // If we are turning on the screen to show the boot message, don't do it until the boot
2670 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002671 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002672 return true;
2673 }
2674
2675 // If we are turning on the screen after the boot is completed normally, don't do so until
2676 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002677 if (mService.mSystemBooted
2678 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002679 return true;
2680 }
2681
2682 return false;
2683 }
2684
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002685 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002686 mTmpWindowAnimator = animator;
2687 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002688 }
2689
2690 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002691 resetAnimationBackgroundAnimator();
2692
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002693 // Used to indicate a detached wallpaper.
2694 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002695 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002696
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002697 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002698
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002699 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002700 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002701 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2702 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002703 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2704 }
2705 }
2706
Wale Ogunwale494009b82016-10-21 09:01:38 -07002707 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002708 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002709 if (imFocus == null) {
2710 return false;
2711 }
2712
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002713 if (DEBUG_INPUT_METHOD) {
2714 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2715 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2716 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002717 }
2718
Wale Ogunwale494009b82016-10-21 09:01:38 -07002719 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2720
2721 if (DEBUG_INPUT_METHOD) {
2722 Slog.i(TAG_WM, "IM target client: " + imeClient);
2723 if (imeClient != null) {
2724 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2725 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2726 }
2727 }
2728
2729 return imeClient != null && imeClient.asBinder() == client.asBinder();
2730 }
2731
2732 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002733 final WindowState win = getWindow(
2734 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2735 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002736 }
2737
2738 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002739 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002740 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002741 final int curValue = w.mSystemUiVisibility;
2742 final int diff = (curValue ^ visibility) & globalDiff;
2743 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002744 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002745 w.mSeq++;
2746 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002747 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002748 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2749 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002750 visibility, newValue, diff);
2751 }
2752 } catch (RemoteException e) {
2753 // so sorry
2754 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002755 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002756 }
2757
2758 void onWindowFreezeTimeout() {
2759 Slog.w(TAG_WM, "Window freeze timeout expired.");
2760 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002761
2762 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002763 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002764 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002765 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002766 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002767 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2768 - mService.mDisplayFreezeTime);
2769 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002770 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002771 mService.mWindowPlacerLocked.performSurfacePlacement();
2772 }
2773
2774 void waitForAllWindowsDrawn() {
2775 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002776 forAllWindows(w -> {
2777 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2778 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2779 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002780 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002781 w.mLastContentInsets.set(-1, -1, -1, -1);
2782 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002783 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002784 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002785 }
2786
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002787 // TODO: Super crazy long method that should be broken down...
2788 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2789
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002790 final int dw = mDisplayInfo.logicalWidth;
2791 final int dh = mDisplayInfo.logicalHeight;
2792 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2793
2794 mTmpUpdateAllDrawn.clear();
2795
2796 int repeats = 0;
2797 do {
2798 repeats++;
2799 if (repeats > 6) {
2800 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2801 clearLayoutNeeded();
2802 break;
2803 }
2804
2805 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2806 pendingLayoutChanges);
2807
Andrii Kulian839def92016-11-02 10:58:58 -07002808 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2809 // the wallpaper window jumping across displays.
2810 // Remove check for default display when there will be support for multiple wallpaper
2811 // targets (on different displays).
2812 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002813 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002814 }
2815
2816 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2817 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2818 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2819 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002820 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002821 }
2822 }
2823
2824 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2825 setLayoutNeeded();
2826 }
2827
2828 // FIRST LOOP: Perform a layout, if needed.
2829 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2830 performLayout(repeats == 1, false /* updateInputWindows */);
2831 } else {
2832 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2833 }
2834
2835 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2836 pendingLayoutChanges = 0;
2837
2838 if (isDefaultDisplay) {
2839 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002840 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002841 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2842 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2843 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2844 }
2845 } while (pendingLayoutChanges != 0);
2846
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002847 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002848
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002849 mTmpRecoveringMemory = recoveringMemory;
2850 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002851
2852 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002853 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2854 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2855 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002856 true /* inTraversal, must call performTraversalInTrans... below */);
2857
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002858 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2859 if (wallpaperVisible != mLastWallpaperVisible) {
2860 mLastWallpaperVisible = wallpaperVisible;
2861 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2862 }
2863
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002864 while (!mTmpUpdateAllDrawn.isEmpty()) {
2865 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2866 // See if any windows have been drawn, so they (and others associated with them)
2867 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002868 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002869 }
2870
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002871 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002872 }
2873
Bryce Leef3c6a472017-11-14 14:53:06 -08002874 private void updateBounds() {
2875 calculateBounds(mTmpBounds);
2876 setBounds(mTmpBounds);
2877 }
2878
2879 // Determines the current display bounds based on the current state
2880 private void calculateBounds(Rect out) {
2881 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
2882 final int orientation = mDisplayInfo.rotation;
2883 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2884 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
2885 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
2886 int width = mDisplayInfo.logicalWidth;
2887 int left = (physWidth - width) / 2;
2888 int height = mDisplayInfo.logicalHeight;
2889 int top = (physHeight - height) / 2;
2890 out.set(left, top, left + width, top + height);
2891 }
2892
2893 @Override
2894 public void getBounds(Rect out) {
2895 calculateBounds(out);
2896 }
2897
2898 private void getBounds(Rect out, int orientation) {
2899 getBounds(out);
2900
2901 // Rotate the Rect if needed.
2902 final int currentRotation = mDisplayInfo.rotation;
2903 final int rotationDelta = deltaRotation(currentRotation, orientation);
2904 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
2905 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
2906 mTmpRectF.set(out);
2907 mTmpMatrix.mapRect(mTmpRectF);
2908 mTmpRectF.round(out);
2909 }
2910 }
2911
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002912 void performLayout(boolean initial, boolean updateInputWindows) {
2913 if (!isLayoutNeeded()) {
2914 return;
2915 }
2916 clearLayoutNeeded();
2917
2918 final int dw = mDisplayInfo.logicalWidth;
2919 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002920 if (DEBUG_LAYOUT) {
2921 Slog.v(TAG, "-------------------------------------");
2922 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2923 }
2924
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002925 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
2926 // TODO: Not sure if we really need to set the rotation here since we are updating from the
2927 // display info above...
2928 mDisplayFrames.mRotation = mRotation;
2929 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002930 if (isDefaultDisplay) {
2931 // Not needed on non-default displays.
2932 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2933 mService.mScreenRect.set(0, 0, dw, dh);
2934 }
2935
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002936 int seq = mService.mLayoutSeq + 1;
2937 if (seq < 0) seq = 0;
2938 mService.mLayoutSeq = seq;
2939
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002940 // Used to indicate that we have processed the dream window and all additional windows are
2941 // behind it.
2942 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002943 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002944
2945 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002946 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002947
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002948 // Used to indicate that we have processed the dream window and all additional attached
2949 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002950 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002951 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002952
2953 // Now perform layout of attached windows, which usually depend on the position of the
2954 // window they are attached to. XXX does not deal with windows that are attached to windows
2955 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002956 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002957
2958 // Window frames may have changed. Tell the input dispatcher about it.
2959 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2960 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2961 if (updateInputWindows) {
2962 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2963 }
2964
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002965 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2966 }
2967
2968 /**
2969 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2970 * In portrait mode, it grabs the full screenshot.
2971 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002972 * @param config of the output bitmap
2973 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
2974 */
chaviwfbe47df2017-11-10 16:14:49 -08002975 Bitmap screenshotDisplay(Bitmap.Config config, boolean wallpaperOnly) {
2976 synchronized (mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02002977 if (!mService.mPolicy.isScreenOn()) {
chaviwfbe47df2017-11-10 16:14:49 -08002978 if (DEBUG_SCREENSHOT) {
2979 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002980 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002981 return null;
2982 }
2983
chaviwfbe47df2017-11-10 16:14:49 -08002984 if (wallpaperOnly && !shouldScreenshotWallpaper()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002985 return null;
2986 }
2987
chaviwfbe47df2017-11-10 16:14:49 -08002988 int dw = mDisplayInfo.logicalWidth;
2989 int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002990
chaviwfbe47df2017-11-10 16:14:49 -08002991 if (dw <= 0 || dh <= 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002992 return null;
2993 }
2994
chaviwfbe47df2017-11-10 16:14:49 -08002995 final Rect frame = new Rect(0, 0, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002996
2997 // The screenshot API does not apply the current screen rotation.
2998 int rot = mDisplay.getRotation();
2999
3000 if (rot == ROTATION_90 || rot == ROTATION_270) {
3001 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3002 }
3003
chaviwfbe47df2017-11-10 16:14:49 -08003004 // SurfaceFlinger is not aware of orientation, so convert our logical
3005 // crop to SurfaceFlinger's portrait orientation.
3006 convertCropForSurfaceFlinger(frame, rot, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003007
3008 final ScreenRotationAnimation screenRotationAnimation =
3009 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3010 final boolean inRotation = screenRotationAnimation != null &&
3011 screenRotationAnimation.isAnimating();
chaviwfbe47df2017-11-10 16:14:49 -08003012 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003013
Robert Carrb1579c82017-09-05 14:54:47 -07003014 // TODO(b/68392460): We should screenshot Task controls directly
3015 // but it's difficult at the moment as the Task doesn't have the
3016 // correct size set.
chaviwfbe47df2017-11-10 16:14:49 -08003017 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, 0, 1, inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003018 if (bitmap == null) {
Robert Carrb1579c82017-09-05 14:54:47 -07003019 Slog.w(TAG_WM, "Failed to take screenshot");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003020 return null;
3021 }
chaviwfbe47df2017-11-10 16:14:49 -08003022
3023 // Create a copy of the screenshot that is immutable and backed in ashmem.
3024 // This greatly reduces the overhead of passing the bitmap between processes.
3025 final Bitmap ret = bitmap.createAshmemBitmap(config);
3026 bitmap.recycle();
3027 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003028 }
chaviwfbe47df2017-11-10 16:14:49 -08003029 }
3030
3031 private boolean shouldScreenshotWallpaper() {
3032 MutableBoolean screenshotReady = new MutableBoolean(false);
3033
3034 forAllWindows(w -> {
3035 if (!w.mIsWallpaper) {
3036 return false;
3037 }
3038
3039 // Found the wallpaper window
3040 final WindowStateAnimator winAnim = w.mWinAnimator;
3041
3042 if (winAnim.getShown() && winAnim.mLastAlpha > 0f) {
3043 screenshotReady.value = true;
3044 }
3045
3046 return true;
3047 }, true /* traverseTopToBottom */);
3048
3049 return screenshotReady.value;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003050 }
3051
3052 // TODO: Can this use createRotationMatrix()?
3053 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3054 if (rot == Surface.ROTATION_90) {
3055 final int tmp = crop.top;
3056 crop.top = dw - crop.right;
3057 crop.right = crop.bottom;
3058 crop.bottom = dw - crop.left;
3059 crop.left = tmp;
3060 } else if (rot == Surface.ROTATION_180) {
3061 int tmp = crop.top;
3062 crop.top = dh - crop.bottom;
3063 crop.bottom = dh - tmp;
3064 tmp = crop.right;
3065 crop.right = dw - crop.left;
3066 crop.left = dw - tmp;
3067 } else if (rot == Surface.ROTATION_270) {
3068 final int tmp = crop.top;
3069 crop.top = crop.left;
3070 crop.left = dh - crop.bottom;
3071 crop.bottom = crop.right;
3072 crop.right = dh - tmp;
3073 }
3074 }
3075
3076 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003077 // Used to indicate the layout is needed.
3078 mTmpWindow = null;
3079
3080 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003081 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003082 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003083 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003084 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003085 w.setDisplayLayoutNeeded();
3086 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003087 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003088
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003089 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003090 mService.mWindowPlacerLocked.performSurfacePlacement();
3091 }
3092 }
3093
Wale Ogunwale1666e312016-12-16 11:27:18 -08003094 void setExitingTokensHasVisible(boolean hasVisible) {
3095 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3096 mExitingTokens.get(i).hasVisible = hasVisible;
3097 }
3098
3099 // Initialize state of exiting applications.
3100 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3101 }
3102
3103 void removeExistingTokensIfPossible() {
3104 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3105 final WindowToken token = mExitingTokens.get(i);
3106 if (!token.hasVisible) {
3107 mExitingTokens.remove(i);
3108 }
3109 }
3110
3111 // Time to remove any exiting applications?
3112 mTaskStackContainers.removeExistingAppTokensIfPossible();
3113 }
3114
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003115 @Override
3116 void onDescendantOverrideConfigurationChanged() {
3117 setLayoutNeeded();
3118 mService.requestTraversal();
3119 }
3120
David Stevens9440dc82017-03-16 19:00:20 -07003121 boolean okToDisplay() {
3122 if (mDisplayId == DEFAULT_DISPLAY) {
3123 return !mService.mDisplayFrozen
3124 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3125 }
3126 return mDisplayInfo.state == Display.STATE_ON;
3127 }
3128
3129 boolean okToAnimate() {
3130 return okToDisplay() &&
3131 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3132 }
3133
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003134 static final class TaskForResizePointSearchResult {
3135 boolean searchDone;
3136 Task taskForResize;
3137
3138 void reset() {
3139 searchDone = false;
3140 taskForResize = null;
3141 }
3142 }
Robert Carr3b716242016-08-16 16:02:21 -07003143
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003144 private static final class ApplySurfaceChangesTransactionState {
3145 boolean displayHasContent;
3146 boolean obscured;
3147 boolean syswin;
3148 boolean focusDisplayed;
3149 float preferredRefreshRate;
3150 int preferredModeId;
3151
3152 void reset() {
3153 displayHasContent = false;
3154 obscured = false;
3155 syswin = false;
3156 focusDisplayed = false;
3157 preferredRefreshRate = 0;
3158 preferredModeId = 0;
3159 }
3160 }
3161
3162 private static final class ScreenshotApplicationState {
3163 WindowState appWin;
3164 int maxLayer;
3165 int minLayer;
3166 boolean screenshotReady;
3167
3168 void reset(boolean screenshotReady) {
3169 appWin = null;
3170 maxLayer = 0;
3171 minLayer = 0;
3172 this.screenshotReady = screenshotReady;
3173 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3174 }
3175 }
3176
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003177 /**
3178 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3179 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3180 * homogeneous children type which is currently required by sub-classes of
3181 * {@link WindowContainer} class.
3182 */
3183 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3184
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003185 @Override
3186 boolean fillsParent() {
3187 return true;
3188 }
3189
3190 @Override
3191 boolean isVisible() {
3192 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003193 }
3194 }
3195
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003196 /**
3197 * Window container class that contains all containers on this display relating to Apps.
3198 * I.e Activities.
3199 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003200 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003201 /**
3202 * A control placed at the appropriate level for transitions to occur.
3203 */
3204 SurfaceControl mAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003205
Wale Ogunwale61911492017-10-11 08:50:50 -07003206 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3207 // through the list to find them.
3208 private TaskStack mHomeStack = null;
3209 private TaskStack mPinnedStack = null;
3210 private TaskStack mSplitScreenPrimaryStack = null;
3211
3212 /**
3213 * Returns the topmost stack on the display that is compatible with the input windowing mode
3214 * and activity type. Null is no compatible stack on the display.
3215 */
3216 TaskStack getStack(int windowingMode, int activityType) {
3217 if (activityType == ACTIVITY_TYPE_HOME) {
3218 return mHomeStack;
3219 }
3220 if (windowingMode == WINDOWING_MODE_PINNED) {
3221 return mPinnedStack;
3222 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3223 return mSplitScreenPrimaryStack;
3224 }
3225 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3226 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003227 if (activityType == ACTIVITY_TYPE_UNDEFINED
3228 && windowingMode == stack.getWindowingMode()) {
3229 // Passing in undefined type means we want to match the topmost stack with the
3230 // windowing mode.
3231 return stack;
3232 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003233 if (stack.isCompatible(windowingMode, activityType)) {
3234 return stack;
3235 }
3236 }
3237 return null;
3238 }
3239
3240 @VisibleForTesting
3241 TaskStack getTopStack() {
3242 return mTaskStackContainers.getChildCount() > 0
3243 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3244 }
3245
3246 TaskStack getHomeStack() {
3247 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3248 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3249 }
3250 return mHomeStack;
3251 }
3252
3253 TaskStack getPinnedStack() {
3254 return mPinnedStack;
3255 }
3256
Matthew Ng64e77cf2017-10-31 14:01:31 -07003257 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003258 return mSplitScreenPrimaryStack;
3259 }
3260
Andrii Kulian839def92016-11-02 10:58:58 -07003261 /**
3262 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003263 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003264 */
3265 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003266 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003267 addChild(stack, onTop);
3268 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003269 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003270
Wale Ogunwale61911492017-10-11 08:50:50 -07003271 void onStackWindowingModeChanged(TaskStack stack) {
3272 removeStackReferenceIfNeeded(stack);
3273 addStackReferenceIfNeeded(stack);
3274 if (stack == mPinnedStack && getTopStack() != stack) {
3275 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3276 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3277 }
3278 }
3279
3280 private void addStackReferenceIfNeeded(TaskStack stack) {
3281 if (stack.isActivityTypeHome()) {
3282 if (mHomeStack != null) {
3283 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
3284 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
3285 }
3286 mHomeStack = stack;
3287 }
3288 final int windowingMode = stack.getWindowingMode();
3289 if (windowingMode == WINDOWING_MODE_PINNED) {
3290 if (mPinnedStack != null) {
3291 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3292 + mPinnedStack + " already exist on display=" + this
3293 + " stack=" + stack);
3294 }
3295 mPinnedStack = stack;
3296 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3297 if (mSplitScreenPrimaryStack != null) {
3298 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3299 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3300 + " already exist on display=" + this + " stack=" + stack);
3301 }
3302 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003303 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003304 }
3305 }
3306
3307 private void removeStackReferenceIfNeeded(TaskStack stack) {
3308 if (stack == mHomeStack) {
3309 mHomeStack = null;
3310 } else if (stack == mPinnedStack) {
3311 mPinnedStack = null;
3312 } else if (stack == mSplitScreenPrimaryStack) {
3313 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003314 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3315 mService.setDockedStackCreateStateLocked(
3316 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3317 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003318 }
Andrii Kulian839def92016-11-02 10:58:58 -07003319 }
3320
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003321 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003322 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3323 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003324 addChild(stack, addIndex);
3325 setLayoutNeeded();
3326 }
3327
Wale Ogunwale61911492017-10-11 08:50:50 -07003328 @Override
3329 protected void removeChild(TaskStack stack) {
3330 super.removeChild(stack);
3331 removeStackReferenceIfNeeded(stack);
3332 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003333
3334 @Override
3335 boolean isOnTop() {
3336 // Considered always on top
3337 return true;
3338 }
3339
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003340 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003341 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003342 if (child.getWindowConfiguration().isAlwaysOnTop()
3343 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003344 // This stack is always-on-top, override the default behavior.
3345 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3346
3347 // Moving to its current position, as we must call super but we don't want to
3348 // perform any meaningful action.
3349 final int currentPosition = mChildren.indexOf(child);
3350 super.positionChildAt(currentPosition, child, false /* includingParents */);
3351 return;
3352 }
3353
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003354 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3355 super.positionChildAt(targetPosition, child, includingParents);
3356
3357 setLayoutNeeded();
3358 }
3359
3360 /**
3361 * When stack is added or repositioned, find a proper position for it.
3362 * This will make sure that pinned stack always stays on top.
3363 * @param requestedPosition Position requested by caller.
3364 * @param stack Stack to be added or positioned.
3365 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3366 * @return The proper position for the stack.
3367 */
3368 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3369 final int topChildPosition = mChildren.size() - 1;
3370 boolean toTop = requestedPosition == POSITION_TOP;
3371 toTop |= adding ? requestedPosition >= topChildPosition + 1
3372 : requestedPosition >= topChildPosition;
3373 int targetPosition = requestedPosition;
3374
Wale Ogunwale61911492017-10-11 08:50:50 -07003375 if (toTop && stack.getWindowingMode() != WINDOWING_MODE_PINNED && hasPinnedStack()) {
Bryce Lee48f4b572017-04-10 10:54:15 -07003376 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003377 TaskStack topStack = mChildren.get(topChildPosition);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003378 if (topStack.getWindowingMode() != WINDOWING_MODE_PINNED) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003379 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3380 }
3381
3382 // So, stack is moved just below the pinned stack.
3383 // When we're adding a new stack the target is the current pinned stack position.
3384 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003385 // stack, because WindowContainer#positionAt() first removes element and then adds
3386 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003387 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3388 }
3389
3390 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003391 }
3392
3393 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003394 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3395 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003396 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003397 if (super.forAllWindows(callback, traverseTopToBottom)) {
3398 return true;
3399 }
3400 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3401 return true;
3402 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003403 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003404 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3405 return true;
3406 }
3407 if (super.forAllWindows(callback, traverseTopToBottom)) {
3408 return true;
3409 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003410 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003411 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003412 }
3413
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003414 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003415 boolean traverseTopToBottom) {
3416 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3417 // app tokens.
3418 // TODO: Investigate if we need to continue to do this or if we can just process them
3419 // in-order.
3420 if (traverseTopToBottom) {
3421 for (int i = mChildren.size() - 1; i >= 0; --i) {
3422 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3423 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003424 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3425 traverseTopToBottom)) {
3426 return true;
3427 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003428 }
3429 }
3430 } else {
3431 final int count = mChildren.size();
3432 for (int i = 0; i < count; ++i) {
3433 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3434 final int appTokensCount = appTokens.size();
3435 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003436 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3437 traverseTopToBottom)) {
3438 return true;
3439 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003440 }
3441 }
3442 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003443 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003444 }
3445
Wale Ogunwale1666e312016-12-16 11:27:18 -08003446 void setExitingTokensHasVisible(boolean hasVisible) {
3447 for (int i = mChildren.size() - 1; i >= 0; --i) {
3448 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3449 for (int j = appTokens.size() - 1; j >= 0; --j) {
3450 appTokens.get(j).hasVisible = hasVisible;
3451 }
3452 }
3453 }
3454
3455 void removeExistingAppTokensIfPossible() {
3456 for (int i = mChildren.size() - 1; i >= 0; --i) {
3457 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3458 for (int j = appTokens.size() - 1; j >= 0; --j) {
3459 final AppWindowToken token = appTokens.get(j);
3460 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3461 && (!token.mIsExiting || token.isEmpty())) {
3462 // Make sure there is no animation running on this token, so any windows
3463 // associated with it will be removed as soon as their animations are
3464 // complete.
3465 token.mAppAnimator.clearAnimation();
3466 token.mAppAnimator.animating = false;
3467 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3468 "performLayout: App token exiting now removed" + token);
3469 token.removeIfPossible();
3470 }
3471 }
3472 }
3473 }
3474
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003475 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003476 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003477 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3478 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003479 // Apps and their containers are not allowed to specify an orientation while the
3480 // docked or freeform stack is visible...except for the home stack/task if the
3481 // docked stack is minimized and it actually set something.
3482 if (mHomeStack != null && mHomeStack.isVisible()
3483 && mDividerControllerLocked.isMinimizedDock()) {
3484 final int orientation = mHomeStack.getOrientation();
3485 if (orientation != SCREEN_ORIENTATION_UNSET) {
3486 return orientation;
3487 }
3488 }
3489 return SCREEN_ORIENTATION_UNSPECIFIED;
3490 }
3491
3492 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003493 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3494 PackageManager.FEATURE_AUTOMOTIVE);
3495 if (isCar) {
3496 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3497 // allow anything but the default orientation.
3498 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3499 "Forcing UNSPECIFIED orientation in car. Ignoring " + orientation);
3500 return SCREEN_ORIENTATION_UNSPECIFIED;
3501 }
3502
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003503 if (orientation != SCREEN_ORIENTATION_UNSET
3504 && orientation != SCREEN_ORIENTATION_BEHIND) {
3505 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3506 "App is requesting an orientation, return " + orientation);
3507 return orientation;
3508 }
3509
3510 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003511 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003512 // The next app has not been requested to be visible, so we keep the current orientation
3513 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003514 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003515 }
Robert Carrb1579c82017-09-05 14:54:47 -07003516
3517 @Override
3518 void assignChildLayers(SurfaceControl.Transaction t) {
3519 final int NORMAL_STACK_STATE = 0;
3520 final int BOOSTED_STATE = 1;
3521 final int ALWAYS_ON_TOP_STATE = 2;
3522
3523 // We allow stacks to change visual order from the AM specified order due to
3524 // Z-boosting during animations. However we must take care to ensure TaskStacks
3525 // which are marked as alwaysOnTop remain that way.
3526 int layer = 0;
3527 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
3528 for (int i = 0; i < mChildren.size(); i++) {
3529 final TaskStack s = mChildren.get(i);
3530 layer++;
3531 if (state == NORMAL_STACK_STATE) {
3532 s.assignLayer(t, layer);
3533 } else if (state == BOOSTED_STATE && s.needsZBoost()) {
3534 s.assignLayer(t, layer);
3535 } else if (state == ALWAYS_ON_TOP_STATE &&
3536 s.isAlwaysOnTop()) {
3537 s.assignLayer(t, layer);
3538 }
3539 s.assignChildLayers(t);
3540 }
3541 // The appropriate place for App-Transitions to occur is right
3542 // above all other animations but still below things in the Picture-and-Picture
3543 // windowing mode.
3544 if (state == BOOSTED_STATE && mAnimationLayer != null) {
3545 t.setLayer(mAnimationLayer, layer + 1);
3546 }
3547 }
3548 }
3549
3550 @Override
3551 void onParentSet() {
3552 super.onParentSet();
3553 if (getParent() != null) {
3554 mAnimationLayer = makeSurface().build();
3555 } else {
3556 mAnimationLayer.destroy();
3557 mAnimationLayer = null;
3558 }
3559 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003560 }
3561
Robert Carree4d4b92017-11-22 12:21:46 -08003562 private final class AboveAppWindowContainers extends NonAppWindowContainers {
3563 AboveAppWindowContainers(String name) {
3564 super(name);
3565 }
3566
3567 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
3568 boolean needAssignIme = imeContainer != null
3569 && imeContainer.getSurfaceControl() != null;
3570 for (int j = 0; j < mChildren.size(); ++j) {
3571 final WindowToken wt = mChildren.get(j);
3572 wt.assignLayer(t, j);
3573 wt.assignChildLayers(t);
3574
3575 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
3576 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08003577
3578 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
3579 TYPE_INPUT_METHOD_DIALOG, true)) {
Robert Carree4d4b92017-11-22 12:21:46 -08003580 t.setRelativeLayer(imeContainer.getSurfaceControl(),
3581 wt.getSurfaceControl(), -1);
3582 needAssignIme = false;
3583 }
3584 }
3585 if (needAssignIme) {
3586 t.setRelativeLayer(imeContainer.getSurfaceControl(),
Robert Carrb8e78fa2017-11-28 13:07:58 -08003587 getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08003588 }
3589 }
3590 }
3591
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003592 /**
3593 * Window container class that contains all containers on this display that are not related to
3594 * Apps. E.g. status bar.
3595 */
Robert Carree4d4b92017-11-22 12:21:46 -08003596 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07003597 /**
3598 * Compares two child window tokens returns -1 if the first is lesser than the second in
3599 * terms of z-order and 1 otherwise.
3600 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003601 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003602 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003603 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3604 token1.mOwnerCanManageAppTokens)
3605 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3606 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003607
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003608 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3609 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3610 return false;
3611 }
3612 final int req = w.mAttrs.screenOrientation;
3613 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3614 || req == SCREEN_ORIENTATION_UNSET) {
3615 return false;
3616 }
3617 return true;
3618 };
3619
Wale Ogunwale3a931692016-11-02 16:49:48 -07003620 private final String mName;
3621 NonAppWindowContainers(String name) {
3622 mName = name;
3623 }
3624
3625 void addChild(WindowToken token) {
3626 addChild(token, mWindowComparator);
3627 }
3628
3629 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003630 int getOrientation() {
3631 final WindowManagerPolicy policy = mService.mPolicy;
3632 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003633 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003634
3635 if (win != null) {
3636 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003637 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003638 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003639 if (mService.mKeyguardGoingAway) {
3640 // Keyguard can't affect the orientation if it is going away...
3641 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3642 return SCREEN_ORIENTATION_UNSET;
3643 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003644 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003645 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003646 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003647 }
3648
Andrii Kulian8ee72852017-03-10 10:36:45 -08003649 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003650
Jorim Jaggi75520d52017-08-24 17:23:19 +02003651 if (policy.isKeyguardShowingAndNotOccluded()
3652 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003653 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003654 }
3655
3656 return SCREEN_ORIENTATION_UNSET;
3657 }
3658
3659 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003660 String getName() {
3661 return mName;
3662 }
Robert Carr3b716242016-08-16 16:02:21 -07003663 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003664
Robert Carrb1579c82017-09-05 14:54:47 -07003665 SurfaceControl.Builder makeSurface(SurfaceSession s) {
3666 return mService.makeSurfaceBuilder(s)
3667 .setParent(mWindowingLayer);
3668 }
3669
3670 @Override
3671 SurfaceSession getSession() {
3672 return mSession;
3673 }
3674
3675 @Override
3676 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003677 SurfaceSession s = child != null ? child.getSession() : getSession();
3678 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07003679 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07003680
3681 if (child == null) {
3682 return b;
3683 }
3684
Robert Carree4d4b92017-11-22 12:21:46 -08003685 return b.setName(child.getName())
3686 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07003687 }
3688
3689 /**
3690 * The makeSurface variants are for use by the window-container
3691 * hierarchy. makeOverlay here is a function for various non windowing
3692 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
3693 * and other potpourii.
3694 */
3695 SurfaceControl.Builder makeOverlay() {
3696 return mService.makeSurfaceBuilder(mSession)
3697 .setParent(mOverlayLayer);
3698 }
3699
3700 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003701 applyMagnificationSpec(getPendingTransaction(), spec);
3702 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07003703 }
3704
3705 @Override
3706 void onParentSet() {
3707 // Since we are the top of the SurfaceControl hierarchy here
3708 // we create the root surfaces explicitly rather than chaining
3709 // up as the default implementation in onParentSet does. So we
3710 // explicitly do NOT call super here.
3711 }
3712
3713 @Override
3714 void assignChildLayers(SurfaceControl.Transaction t) {
3715 t.setLayer(mOverlayLayer, 1)
3716 .setLayer(mWindowingLayer, 0);
3717
3718 // These are layers as children of "mWindowingLayer"
3719 mBelowAppWindowsContainers.assignLayer(t, 0);
3720 mTaskStackContainers.assignLayer(t, 1);
3721 mAboveAppWindowsContainers.assignLayer(t, 2);
3722
3723 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08003724 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08003725
Robert Carree4d4b92017-11-22 12:21:46 -08003726 // In the case where we have an IME target that is not in split-screen
3727 // mode IME assignment is easy. We just need the IME to go directly above
3728 // the target. This way children of the target will naturally go above the IME
3729 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08003730 //
Robert Carree4d4b92017-11-22 12:21:46 -08003731 // In the case of split-screen windowing mode, we need to elevate the IME above the
3732 // docked divider while keeping the app itself below the docked divider, so instead
3733 // we use relative layering of the IME targets child windows, and place the
3734 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08003735 //
Robert Carree4d4b92017-11-22 12:21:46 -08003736 // In the case where we have no IME target we assign it where it's base layer would
3737 // place it in the AboveAppWindowContainers.
3738 if (imeTarget != null && !imeTarget.inSplitScreenWindowingMode()
3739 && (imeTarget.getSurfaceControl() != null)) {
Robert Carrb1579c82017-09-05 14:54:47 -07003740 t.setRelativeLayer(mImeWindowsContainers.getSurfaceControl(),
3741 imeTarget.getSurfaceControl(),
3742 // TODO: We need to use an extra level on the app surface to ensure
3743 // this is always above SurfaceView but always below attached window.
3744 1);
Robert Carree4d4b92017-11-22 12:21:46 -08003745 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07003746 }
3747
3748 // Above we have assigned layers to our children, now we ask them to assign
3749 // layers to their children.
3750 mBelowAppWindowsContainers.assignChildLayers(t);
3751 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08003752 mAboveAppWindowsContainers.assignChildLayers(t,
3753 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07003754 mImeWindowsContainers.assignChildLayers(t);
3755 }
3756
3757 /**
3758 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
3759 * that the IME target is one of the docked applications. We'd like the docked divider to be
3760 * above both of the applications, and we'd like the IME to be above the docked divider.
3761 * However we need child windows of the applications to be above the IME (Text drag handles).
3762 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
3763 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
3764 * with {@link #WindowState#assignLayer}
3765 */
3766 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
3767 t.setRelativeLayer(child.getSurfaceControl(), mImeWindowsContainers.getSurfaceControl(), 1);
3768 }
3769
3770 @Override
3771 void destroyAfterPendingTransaction(SurfaceControl surface) {
3772 mPendingDestroyingSurfaces.add(surface);
3773 }
3774
3775 /**
3776 * Destroys any surfaces that have been put into the pending list with
3777 * {@link #destroyAfterTransaction}.
3778 */
3779 void onPendingTransactionApplied() {
3780 for (int i = mPendingDestroyingSurfaces.size() - 1; i >= 0; i--) {
3781 mPendingDestroyingSurfaces.get(i).destroy();
3782 }
3783 mPendingDestroyingSurfaces.clear();
3784 }
Craig Mautner59c00972012-07-30 12:10:24 -07003785}