blob: c6256334ffa410338e194d5b0a8652d13c53f6ef [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;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070061import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070062import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
63import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070064import static android.view.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 Ogunwaleae9adbf2016-10-18 15:17:06 -0700149import android.view.WindowManagerPolicy;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700150
Bryce Lee48f4b572017-04-10 10:54:15 -0700151import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800152import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700153import com.android.internal.view.IInputMethodClient;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000154import android.view.DisplayFrames;
Craig Mautner59c00972012-07-30 12:10:24 -0700155
Robert Carr3b716242016-08-16 16:02:21 -0700156import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700157import java.io.PrintWriter;
158import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700159import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700160import java.util.HashMap;
161import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700162import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700163import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800164import java.util.function.Consumer;
165import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700166
Craig Mautner59c00972012-07-30 12:10:24 -0700167/**
168 * Utility class for keeping track of the WindowStates and other pertinent contents of a
169 * particular Display.
170 *
171 * IMPORTANT: No method from this class should ever be used without holding
172 * WindowManagerService.mWindowMap.
173 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700174class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700175 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700176
177 /** Unique identifier of this stack. */
178 private final int mDisplayId;
179
Wale Ogunwale3a931692016-11-02 16:49:48 -0700180 /** The containers below are the only child containers the display can have. */
181 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700182 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700183 // Contains all non-app window containers that should be displayed above the app containers
184 // (e.g. Status bar)
185 private final NonAppWindowContainers mAboveAppWindowsContainers =
186 new NonAppWindowContainers("mAboveAppWindowsContainers");
187 // Contains all non-app window containers that should be displayed below the app containers
188 // (e.g. Wallpaper).
189 private final NonAppWindowContainers mBelowAppWindowsContainers =
190 new NonAppWindowContainers("mBelowAppWindowsContainers");
191 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
192 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
193 // window containers together and move them in-sync if/when needed.
194 private final NonAppWindowContainers mImeWindowsContainers =
195 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700196
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000197 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800198 private WindowState mTmpWindow2;
199 private WindowAnimator mTmpWindowAnimator;
200 private boolean mTmpRecoveringMemory;
201 private boolean mUpdateImeTarget;
202 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700203 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700204
Wale Ogunwale02319a62016-09-26 15:21:22 -0700205 // Mapping from a token IBinder to a WindowToken object on this display.
206 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
207
Andrii Kuliancd097992017-03-23 18:31:59 -0700208 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700209 int mInitialDisplayWidth = 0;
210 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700211 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700212
213 /**
214 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
215 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
216 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
217 */
Craig Mautner59c00972012-07-30 12:10:24 -0700218 int mBaseDisplayWidth = 0;
219 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700220 /**
221 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
222 * but can be set from Settings or via shell command "adb shell wm density".
223 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
224 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700225 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700226 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700227 private final DisplayInfo mDisplayInfo = new DisplayInfo();
228 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700229 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000230 DisplayFrames mDisplayFrames;
231
Andrii Kulian06d07d62017-03-14 11:11:47 -0700232 /**
233 * For default display it contains real metrics, empty for others.
234 * @see WindowManagerService#createWatermarkInTransaction()
235 */
236 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
237 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
238 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700239
Andrii Kulian06d07d62017-03-14 11:11:47 -0700240 /**
241 * Compat metrics computed based on {@link #mDisplayMetrics}.
242 * @see #updateDisplayAndOrientation(int)
243 */
244 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
245
246 /** The desired scaling factor for compatible apps. */
247 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700248
Andrii Kulian8ee72852017-03-10 10:36:45 -0800249 /**
250 * Current rotation of the display.
251 * Constants as per {@link android.view.Surface.Rotation}.
252 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700253 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800254 */
255 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700256
Andrii Kulian8ee72852017-03-10 10:36:45 -0800257 /**
258 * Last applied orientation of the display.
259 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
260 *
261 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
262 */
263 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700264
Andrii Kulian8ee72852017-03-10 10:36:45 -0800265 /**
266 * Flag indicating that the application is receiving an orientation that has different metrics
267 * than it expected. E.g. Portrait instead of Landscape.
268 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700269 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800270 */
271 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700272
Andrii Kulian8ee72852017-03-10 10:36:45 -0800273 /**
274 * Orientation forced by some window. If there is no visible window that specifies orientation
275 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
276 *
277 * @see NonAppWindowContainers#getOrientation()
278 */
279 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700280
Andrii Kulian8ee72852017-03-10 10:36:45 -0800281 /**
282 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
283 * occluded.
284 *
285 * @see NonAppWindowContainers#getOrientation()
286 */
287 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
288
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700289 /**
290 * Keep track of wallpaper visibility to notify changes.
291 */
292 private boolean mLastWallpaperVisible = false;
293
Andrii Kulian06d07d62017-03-14 11:11:47 -0700294 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700295
Craig Mautner39834192012-09-02 07:47:24 -0700296 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700297 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700298 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700299 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800300 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700301
Craig Mautnerdc548482014-02-05 13:35:24 -0800302 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700303 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800304
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700305 /** Detect user tapping outside of current focused task bounds .*/
306 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700307
Craig Mautner6601b7b2013-04-29 10:29:11 -0700308 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700309 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700310
Craig Mautner6601b7b2013-04-29 10:29:11 -0700311 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800312 private final Rect mTmpRect = new Rect();
313 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700314 private final RectF mTmpRectF = new RectF();
315 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800316 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700317
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800318 WindowManagerService mService;
Craig Mautner9d808b12013-08-06 18:00:25 -0700319
Craig Mautner95da1082014-02-24 17:54:35 -0800320 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700321 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800322
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700323 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700324 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700325
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800326 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
327
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000328 private boolean mHaveBootMsg = false;
329 private boolean mHaveApp = false;
330 private boolean mHaveWallpaper = false;
331 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700332
333 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
334
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700335 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
336 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000337 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
338 new ApplySurfaceChangesTransactionState();
339 private final ScreenshotApplicationState mScreenshotApplicationState =
340 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700341
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700342 // True if this display is in the process of being removed. Used to determine if the removal of
343 // the display's direct children should be allowed.
344 private boolean mRemovingDisplay = false;
345
Bryce Leed1871262017-06-12 14:12:29 -0700346 // {@code false} if this display is in the processing of being created.
347 private boolean mDisplayReady = false;
348
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800349 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700350 int mInputMethodAnimLayerAdjustment;
351
Robert Carrb1579c82017-09-05 14:54:47 -0700352 private final SurfaceSession mSession = new SurfaceSession();
353
354 /**
355 * We organize all top-level Surfaces in to the following layers.
356 * mOverlayLayer contains a few Surfaces which are always on top of others
357 * and omitted from Screen-Magnification ({@link WindowState#isScreenOverlay})
358 * {@link #mWindowingLayer} contains everything else.
359 */
360 private SurfaceControl mOverlayLayer;
361
362 /**
363 * See {@link #mOverlayLayer}
364 */
365 private SurfaceControl mWindowingLayer;
366
367 /**
368 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
369 * <p>
370 * For these surfaces currently we use a surface based on the larger of width or height so we
371 * don't have to resize when rotating the display.
372 */
373 private int mSurfaceSize;
374
375 /**
376 * A list of surfaces to be destroyed after {@link #mPendingTransaction} is applied.
377 */
378 private final ArrayList<SurfaceControl> mPendingDestroyingSurfaces = new ArrayList<>();
379
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800380 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
381 WindowStateAnimator winAnimator = w.mWinAnimator;
382 if (winAnimator.hasSurface()) {
383 final boolean wasAnimating = winAnimator.mWasAnimating;
384 final boolean nowAnimating = winAnimator.stepAnimationLocked(
385 mTmpWindowAnimator.mCurrentTime);
386 winAnimator.mWasAnimating = nowAnimating;
387 mTmpWindowAnimator.orAnimating(nowAnimating);
388
389 if (DEBUG_WALLPAPER) Slog.v(TAG,
390 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
391
392 if (wasAnimating && !winAnimator.mAnimating
393 && mWallpaperController.isWallpaperTarget(w)) {
394 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
395 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
396 if (DEBUG_LAYOUT_REPEATS) {
397 mService.mWindowPlacerLocked.debugLayoutRepeats(
398 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
399 }
400 }
401 }
402
403 final AppWindowToken atoken = w.mAppToken;
404 if (winAnimator.mDrawState == READY_TO_SHOW) {
405 if (atoken == null || atoken.allDrawn) {
406 if (w.performShowLocked()) {
407 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
408 if (DEBUG_LAYOUT_REPEATS) {
409 mService.mWindowPlacerLocked.debugLayoutRepeats(
410 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
411 }
412 }
413 }
414 }
415 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
416 if (appAnimator != null && appAnimator.thumbnail != null) {
417 if (appAnimator.thumbnailTransactionSeq
418 != mTmpWindowAnimator.mAnimTransactionSequence) {
419 appAnimator.thumbnailTransactionSeq =
420 mTmpWindowAnimator.mAnimTransactionSequence;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800421 }
422 }
423 };
424
425 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
426 final WindowStateAnimator winAnimator = w.mWinAnimator;
427 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
428 return;
429 }
430
431 final int flags = w.mAttrs.flags;
432
433 // If this window is animating, make a note that we have an animating window and take
434 // care of a request to run a detached wallpaper animation.
435 if (winAnimator.mAnimating) {
436 if (winAnimator.mAnimation != null) {
437 if ((flags & FLAG_SHOW_WALLPAPER) != 0
438 && winAnimator.mAnimation.getDetachWallpaper()) {
439 mTmpWindow = w;
440 }
441 final int color = winAnimator.mAnimation.getBackgroundColor();
442 if (color != 0) {
443 final TaskStack stack = w.getStack();
444 if (stack != null) {
445 stack.setAnimationBackground(winAnimator, color);
446 }
447 }
448 }
449 mTmpWindowAnimator.setAnimating(true);
450 }
451
452 // If this window's app token is running a detached wallpaper animation, make a note so
453 // we can ensure the wallpaper is displayed behind it.
454 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
455 if (appAnimator != null && appAnimator.animation != null
456 && appAnimator.animating) {
457 if ((flags & FLAG_SHOW_WALLPAPER) != 0
458 && appAnimator.animation.getDetachWallpaper()) {
459 mTmpWindow = w;
460 }
461
462 final int color = appAnimator.animation.getBackgroundColor();
463 if (color != 0) {
464 final TaskStack stack = w.getStack();
465 if (stack != null) {
466 stack.setAnimationBackground(winAnimator, color);
467 }
468 }
469 }
470 };
471
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800472 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
473 final int lostFocusUid = mTmpWindow.mOwnerUid;
474 final Handler handler = mService.mH;
475 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
476 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
477 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
478 w.mAttrs.hideTimeoutMilliseconds);
479 }
480 }
481 };
482
483 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
484 final AppWindowToken focusedApp = mService.mFocusedApp;
485 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
486 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
487
488 if (!w.canReceiveKeys()) {
489 return false;
490 }
491
492 final AppWindowToken wtoken = w.mAppToken;
493
494 // If this window's application has been removed, just skip it.
495 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
496 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
497 + (wtoken.removed ? "removed" : "sendingToBottom"));
498 return false;
499 }
500
501 if (focusedApp == null) {
502 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
503 + " using new focus @ " + w);
504 mTmpWindow = w;
505 return true;
506 }
507
508 if (!focusedApp.windowsAreFocusable()) {
509 // Current focused app windows aren't focusable...
510 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
511 + " focusable using new focus @ " + w);
512 mTmpWindow = w;
513 return true;
514 }
515
516 // Descend through all of the app tokens and find the first that either matches
517 // win.mAppToken (return win) or mFocusedApp (return null).
518 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
519 if (focusedApp.compareTo(wtoken) > 0) {
520 // App stack below focused app stack. No focus for you!!!
521 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
522 "findFocusedWindow: Reached focused app=" + focusedApp);
523 mTmpWindow = null;
524 return true;
525 }
526 }
527
528 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
529 mTmpWindow = w;
530 return true;
531 };
532
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800533 private final Consumer<WindowState> mPerformLayout = w -> {
534 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
535 // wasting time and funky changes while a window is animating away.
536 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
537 || w.isGoneForLayoutLw();
538
539 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
540 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
541 + " mLayoutAttached=" + w.mLayoutAttached
542 + " screen changed=" + w.isConfigChanged());
543 final AppWindowToken atoken = w.mAppToken;
544 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
545 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
546 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
547 + " parentHidden=" + w.isParentWindowHidden());
548 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
549 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
550 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
551 + " parentHidden=" + w.isParentWindowHidden());
552 }
553
554 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
555 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
556 // since that means "perform layout as normal, just don't display").
557 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
558 || ((w.isConfigChanged() || w.setReportResizeHints())
559 && !w.isGoneForLayoutLw() &&
560 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
561 (w.mHasSurface && w.mAppToken != null &&
562 w.mAppToken.layoutConfigChanges)))) {
563 if (!w.mLayoutAttached) {
564 if (mTmpInitial) {
565 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
566 w.mContentChanged = false;
567 }
568 if (w.mAttrs.type == TYPE_DREAM) {
569 // Don't layout windows behind a dream, so that if it does stuff like hide
570 // the status bar we won't get a bad transition when it goes away.
571 mTmpWindow = w;
572 }
573 w.mLayoutNeeded = false;
574 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200575 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000576 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800577 w.mLayoutSeq = mService.mLayoutSeq;
578
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200579 // If this is the first layout, we need to initialize the last inset values as
580 // otherwise we'd immediately cause an unnecessary resize.
581 if (firstLayout) {
582 w.updateLastInsetValues();
583 }
584
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800585 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
586 + " mContainingFrame=" + w.mContainingFrame
587 + " mDisplayFrame=" + w.mDisplayFrame);
588 }
589 }
590 };
591
592 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
593 if (w.mLayoutAttached) {
594 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
595 + " mViewVisibility=" + w.mViewVisibility
596 + " mRelayoutCalled=" + w.mRelayoutCalled);
597 // If this view is GONE, then skip it -- keep the current frame, and let the caller
598 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
599 // windows, since that means "perform layout as normal, just don't display").
600 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
601 return;
602 }
603 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
604 || w.mLayoutNeeded) {
605 if (mTmpInitial) {
606 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
607 w.mContentChanged = false;
608 }
609 w.mLayoutNeeded = false;
610 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000611 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800612 w.mLayoutSeq = mService.mLayoutSeq;
613 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
614 + " mContainingFrame=" + w.mContainingFrame
615 + " mDisplayFrame=" + w.mDisplayFrame);
616 }
617 } else if (w.mAttrs.type == TYPE_DREAM) {
618 // Don't layout windows behind a dream, so that if it does stuff like hide the
619 // status bar we won't get a bad transition when it goes away.
620 mTmpWindow = mTmpWindow2;
621 }
622 };
623
624 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
625 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
626 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
627 return w.canBeImeTarget();
628 };
629
630 private final Consumer<WindowState> mApplyPostLayoutPolicy =
631 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
632 mService.mInputMethodTarget);
633
634 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
635 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
636 final boolean obscuredChanged = w.mObscured !=
637 mTmpApplySurfaceChangesTransactionState.obscured;
638 final RootWindowContainer root = mService.mRoot;
639 // Only used if default window
640 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
641
642 // Update effect.
643 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
644 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
645 final boolean isDisplayed = w.isDisplayedLw();
646
647 if (isDisplayed && w.isObscuringDisplay()) {
648 // This window completely covers everything behind it, so we want to leave all
649 // of them as undimmed (for performance reasons).
650 root.mObscuringWindow = w;
651 mTmpApplySurfaceChangesTransactionState.obscured = true;
652 }
653
654 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
655 root.handleNotObscuredLocked(w,
656 mTmpApplySurfaceChangesTransactionState.obscured,
657 mTmpApplySurfaceChangesTransactionState.syswin);
658
659 if (w.mHasSurface && isDisplayed) {
660 final int type = w.mAttrs.type;
661 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
662 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
663 mTmpApplySurfaceChangesTransactionState.syswin = true;
664 }
665 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
666 && w.mAttrs.preferredRefreshRate != 0) {
667 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
668 = w.mAttrs.preferredRefreshRate;
669 }
670 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
671 && w.mAttrs.preferredDisplayModeId != 0) {
672 mTmpApplySurfaceChangesTransactionState.preferredModeId
673 = w.mAttrs.preferredDisplayModeId;
674 }
675 }
676 }
677
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800678 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
679 && mWallpaperController.isWallpaperTarget(w)) {
680 // This is the wallpaper target and its obscured state changed... make sure the
681 // current wallpaper's visibility has been updated accordingly.
682 mWallpaperController.updateWallpaperVisibility();
683 }
684
685 w.handleWindowMovedIfNeeded();
686
687 final WindowStateAnimator winAnimator = w.mWinAnimator;
688
689 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
690 w.mContentChanged = false;
691
692 // Moved from updateWindowsAndWallpaperLocked().
693 if (w.mHasSurface) {
694 // Take care of the window being ready to display.
695 final boolean committed = winAnimator.commitFinishDrawingLocked();
696 if (isDefaultDisplay && committed) {
697 if (w.mAttrs.type == TYPE_DREAM) {
698 // HACK: When a dream is shown, it may at that point hide the lock screen.
699 // So we need to redo the layout to let the phone window manager make this
700 // happen.
701 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
702 if (DEBUG_LAYOUT_REPEATS) {
703 surfacePlacer.debugLayoutRepeats(
704 "dream and commitFinishDrawingLocked true",
705 pendingLayoutChanges);
706 }
707 }
708 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
709 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
710 "First draw done in potential wallpaper target " + w);
711 root.mWallpaperMayChange = true;
712 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
713 if (DEBUG_LAYOUT_REPEATS) {
714 surfacePlacer.debugLayoutRepeats(
715 "wallpaper and commitFinishDrawingLocked true",
716 pendingLayoutChanges);
717 }
718 }
719 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700720 final TaskStack stack = w.getStack();
721 if ((!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening())
722 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800723 // Updates the shown frame before we set up the surface. This is needed
724 // because the resizing could change the top-left position (in addition to
725 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
726 // position the surface.
727 //
728 // If an animation is being started, we can't call this method because the
729 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700730 // we do want to update the position if the window is animating. We make an exception
731 // for the bounds animating state, where an application may have been waiting
732 // for an exit animation to start, but instead enters PiP. We need to ensure
733 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800734 winAnimator.computeShownFrameLocked();
735 }
736 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
737 }
738
739 final AppWindowToken atoken = w.mAppToken;
740 if (atoken != null) {
741 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
742 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
743 mTmpUpdateAllDrawn.add(atoken);
744 }
745 }
746
747 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
748 && w.isDisplayedLw()) {
749 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
750 }
751
752 w.updateResizingWindowIfNeeded();
753 };
754
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800755 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800756 * Create new {@link DisplayContent} instance, add itself to the root window container and
757 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700758 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800759 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700760 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
761 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700762 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700763 DisplayContent(Display display, WindowManagerService service,
Robert Carrb1579c82017-09-05 14:54:47 -0700764 WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800765 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
766 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
767 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
768 + " new=" + display);
769 }
770
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700771 mDisplay = display;
772 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700773 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700774 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700775 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700776 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700777 mService = service;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000778 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700779 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800780 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700781 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700782
Robert Carrb1579c82017-09-05 14:54:47 -0700783 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth);
784
785 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
786 .setSize(mSurfaceSize, mSurfaceSize)
787 .setOpaque(true);
788 mWindowingLayer = b.setName("Display Root").build();
789 mOverlayLayer = b.setName("Display Overlays").build();
790
Robert Carrf59b8dd2017-10-02 18:58:36 -0700791 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700792 .setLayerStack(mWindowingLayer, mDisplayId)
793 .show(mWindowingLayer)
794 .setLayer(mOverlayLayer, 1)
795 .setLayerStack(mOverlayLayer, mDisplayId)
796 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700797 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700798
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700799 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700800 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700801 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700802 super.addChild(mAboveAppWindowsContainers, null);
803 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800804
805 // Add itself as a child to the root container.
806 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700807
808 // TODO(b/62541591): evaluate whether this is the best spot to declare the
809 // {@link DisplayContent} ready for use.
810 mDisplayReady = true;
811 }
812
813 boolean isReady() {
814 // The display is ready when the system and the individual display are both ready.
815 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700816 }
817
818 int getDisplayId() {
819 return mDisplayId;
820 }
821
Wale Ogunwale02319a62016-09-26 15:21:22 -0700822 WindowToken getWindowToken(IBinder binder) {
823 return mTokenMap.get(binder);
824 }
825
826 AppWindowToken getAppWindowToken(IBinder binder) {
827 final WindowToken token = getWindowToken(binder);
828 if (token == null) {
829 return null;
830 }
831 return token.asAppWindowToken();
832 }
833
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700834 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700835 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
836 if (dc != null) {
837 // We currently don't support adding a window token to the display if the display
838 // already has the binder mapped to another token. If there is a use case for supporting
839 // this moving forward we will either need to merge the WindowTokens some how or have
840 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700841 throw new IllegalArgumentException("Can't map token=" + token + " to display="
842 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700843 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700844 if (binder == null) {
845 throw new IllegalArgumentException("Can't map token=" + token + " to display="
846 + getName() + " binder is null");
847 }
848 if (token == null) {
849 throw new IllegalArgumentException("Can't map null token to display="
850 + getName() + " binder=" + binder);
851 }
852
Wale Ogunwale02319a62016-09-26 15:21:22 -0700853 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700854
855 if (token.asAppWindowToken() == null) {
856 // Add non-app token to container hierarchy on the display. App tokens are added through
857 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700858 switch (token.windowType) {
859 case TYPE_WALLPAPER:
860 mBelowAppWindowsContainers.addChild(token);
861 break;
862 case TYPE_INPUT_METHOD:
863 case TYPE_INPUT_METHOD_DIALOG:
864 mImeWindowsContainers.addChild(token);
865 break;
866 default:
867 mAboveAppWindowsContainers.addChild(token);
868 break;
869 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700870 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700871 }
872
873 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700874 final WindowToken token = mTokenMap.remove(binder);
875 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800876 token.setExiting();
877 }
878 return token;
879 }
880
881 /** Changes the display the input window token is housed on to this one. */
882 void reParentWindowToken(WindowToken token) {
883 final DisplayContent prevDc = token.getDisplayContent();
884 if (prevDc == this) {
885 return;
886 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800887 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
888 && token.asAppWindowToken() == null) {
889 // Removed the token from the map, but made sure it's not an app token before removing
890 // from parent.
891 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700892 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800893
894 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700895 }
896
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700897 void removeAppToken(IBinder binder) {
898 final WindowToken token = removeWindowToken(binder);
899 if (token == null) {
900 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
901 return;
902 }
903
904 final AppWindowToken appToken = token.asAppWindowToken();
905
906 if (appToken == null) {
907 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
908 return;
909 }
910
911 appToken.onRemovedFromDisplay();
912 }
913
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700914 Display getDisplay() {
915 return mDisplay;
916 }
917
Craig Mautner59c00972012-07-30 12:10:24 -0700918 DisplayInfo getDisplayInfo() {
919 return mDisplayInfo;
920 }
921
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700922 DisplayMetrics getDisplayMetrics() {
923 return mDisplayMetrics;
924 }
925
Andrii Kulian8ee72852017-03-10 10:36:45 -0800926 int getRotation() {
927 return mRotation;
928 }
929
930 void setRotation(int newRotation) {
931 mRotation = newRotation;
932 }
933
934 int getLastOrientation() {
935 return mLastOrientation;
936 }
937
938 void setLastOrientation(int orientation) {
939 mLastOrientation = orientation;
940 }
941
942 boolean getAltOrientation() {
943 return mAltOrientation;
944 }
945
946 void setAltOrientation(boolean altOrientation) {
947 mAltOrientation = altOrientation;
948 }
949
950 int getLastWindowForcedOrientation() {
951 return mLastWindowForcedOrientation;
952 }
953
Andrii Kulian06d07d62017-03-14 11:11:47 -0700954 /**
955 * Update rotation of the display.
956 *
957 * Returns true if the rotation has been changed. In this case YOU MUST CALL
958 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
959 */
960 boolean updateRotationUnchecked(boolean inTransaction) {
961 if (mService.mDeferredRotationPauseCount > 0) {
962 // Rotation updates have been paused temporarily. Defer the update until
963 // updates have been resumed.
964 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
965 return false;
966 }
967
968 ScreenRotationAnimation screenRotationAnimation =
969 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
970 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
971 // Rotation updates cannot be performed while the previous rotation change
972 // animation is still in progress. Skip this update. We will try updating
973 // again after the animation is finished and the display is unfrozen.
974 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
975 return false;
976 }
977 if (mService.mDisplayFrozen) {
978 // Even if the screen rotation animation has finished (e.g. isAnimating
979 // returns false), there is still some time where we haven't yet unfrozen
980 // the display. We also need to abort rotation here.
981 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
982 "Deferring rotation, still finishing previous rotation");
983 return false;
984 }
985
986 if (!mService.mDisplayEnabled) {
987 // No point choosing a rotation if the display is not enabled.
988 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
989 return false;
990 }
991
992 final int oldRotation = mRotation;
993 final int lastOrientation = mLastOrientation;
994 final boolean oldAltOrientation = mAltOrientation;
995 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr0e007272017-10-02 13:00:55 -0700996 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
Robert Carr897215d2017-03-29 12:25:50 -0700997 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700998
Robert Carr0e007272017-10-02 13:00:55 -0700999 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001000 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
1001 if (seamlessRotated != null) {
1002 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1003 // to complete (that is, waiting for windows to redraw). It's tempting to check
1004 // w.mSeamlessRotationCount but that could be incorrect in the case of
1005 // window-removal.
1006 return false;
1007 }
Robert Carr0e007272017-10-02 13:00:55 -07001008
1009 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001010 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001011 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001012 mayRotateSeamlessly = false;
1013 }
1014 for (int i = 0; i < mService.mSessions.size(); i++) {
1015 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1016 mayRotateSeamlessly = false;
1017 break;
1018 }
1019 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001020 }
Robert Carr0e007272017-10-02 13:00:55 -07001021 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001022
1023 // TODO: Implement forced rotation changes.
1024 // Set mAltOrientation to indicate that the application is receiving
1025 // an orientation that has different metrics than it expected.
1026 // eg. Portrait instead of Landscape.
1027
1028 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
1029 lastOrientation, rotation);
1030
1031 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
1032 + ", got rotation " + rotation + " which has "
1033 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1034
1035 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1036 // No change.
1037 return false;
1038 }
1039
1040 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
1041 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1042 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1043
1044 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1045 mService.mWaitingForConfig = true;
1046 }
1047
1048 mRotation = rotation;
1049 mAltOrientation = altOrientation;
1050 if (isDefaultDisplay) {
1051 mService.mPolicy.setRotationLw(rotation);
1052 }
1053
1054 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1055 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1056 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1057 WINDOW_FREEZE_TIMEOUT_DURATION);
1058
1059 setLayoutNeeded();
1060 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001061 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001062
1063 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001064 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001065 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1066 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1067 mDisplayId);
1068 } else {
1069 // The screen rotation animation uses a screenshot to freeze the screen
1070 // while windows resize underneath.
1071 // When we are rotating seamlessly, we allow the elements to transition
1072 // to their rotated state independently and without a freeze required.
1073 screenRotationAnimation = null;
1074
1075 // We have to reset this in case a window was removed before it
1076 // finished seamless rotation.
1077 mService.mSeamlessRotationCount = 0;
1078 }
1079
1080 // We need to update our screen size information to match the new rotation. If the rotation
1081 // has actually changed then this method will return true and, according to the comment at
1082 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1083 // By updating the Display info here it will be available to
1084 // #computeScreenConfiguration() later.
1085 updateDisplayAndOrientation(getConfiguration().uiMode);
1086
1087 if (!inTransaction) {
1088 if (SHOW_TRANSACTIONS) {
1089 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1090 }
1091 mService.openSurfaceTransaction();
1092 }
1093 try {
1094 // NOTE: We disable the rotation in the emulator because
1095 // it doesn't support hardware OpenGL emulation yet.
1096 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1097 && screenRotationAnimation.hasScreenshot()) {
Robert Carrb1579c82017-09-05 14:54:47 -07001098 if (screenRotationAnimation.setRotationInTransaction(rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001099 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1100 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1101 mService.scheduleAnimationLocked();
1102 }
1103 }
1104
1105 if (rotateSeamlessly) {
1106 forAllWindows(w -> {
1107 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1108 }, true /* traverseTopToBottom */);
1109 }
1110
1111 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1112 } finally {
1113 if (!inTransaction) {
Adrian Roos111aff92017-09-27 18:11:46 +02001114 mService.closeSurfaceTransaction("setRotationUnchecked");
Andrii Kulian06d07d62017-03-14 11:11:47 -07001115 if (SHOW_LIGHT_TRANSACTIONS) {
1116 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1117 }
1118 }
1119 }
1120
1121 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001122 if (w.mHasSurface && !rotateSeamlessly) {
1123 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001124 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001125 mService.mRoot.mOrientationChangeComplete = false;
1126 w.mLastFreezeDuration = 0;
1127 }
1128 w.mReportOrientationChanged = true;
1129 }, true /* traverseTopToBottom */);
1130
1131 if (rotateSeamlessly) {
1132 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1133 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1134 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1135 }
1136
1137 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1138 final WindowManagerService.RotationWatcher rotationWatcher
1139 = mService.mRotationWatchers.get(i);
1140 if (rotationWatcher.mDisplayId == mDisplayId) {
1141 try {
1142 rotationWatcher.mWatcher.onRotationChanged(rotation);
1143 } catch (RemoteException e) {
1144 // Ignore
1145 }
1146 }
1147 }
1148
1149 // TODO (multi-display): Magnification is supported only for the default display.
1150 // Announce rotation only if we will not animate as we already have the
1151 // windows in final state. Otherwise, we make this call at the rotation end.
1152 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1153 && isDefaultDisplay) {
1154 mService.mAccessibilityController.onRotationChangedLocked(this);
1155 }
1156
1157 return true;
1158 }
1159
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001160 void configureDisplayPolicy() {
1161 mService.mPolicy.setInitialDisplaySize(getDisplay(),
1162 mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1163
1164 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
1165 }
1166
Andrii Kulian06d07d62017-03-14 11:11:47 -07001167 /**
1168 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1169 * changed.
1170 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1171 */
1172 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1173 // Use the effective "visual" dimensions based on current rotation
1174 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1175 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1176 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1177 int dw = realdw;
1178 int dh = realdh;
1179
1180 if (mAltOrientation) {
1181 if (realdw > realdh) {
1182 // Turn landscape into portrait.
1183 int maxw = (int)(realdh/1.3f);
1184 if (maxw < realdw) {
1185 dw = maxw;
1186 }
1187 } else {
1188 // Turn portrait into landscape.
1189 int maxh = (int)(realdw/1.3f);
1190 if (maxh < realdh) {
1191 dh = maxh;
1192 }
1193 }
1194 }
1195
1196 // Update application display metrics.
1197 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1198 mDisplayId);
1199 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1200 mDisplayId);
1201 mDisplayInfo.rotation = mRotation;
1202 mDisplayInfo.logicalWidth = dw;
1203 mDisplayInfo.logicalHeight = dh;
1204 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1205 mDisplayInfo.appWidth = appWidth;
1206 mDisplayInfo.appHeight = appHeight;
1207 if (isDefaultDisplay) {
1208 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1209 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1210 }
1211 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1212 if (mDisplayScalingDisabled) {
1213 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1214 } else {
1215 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1216 }
1217
1218 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1219 mDisplayInfo);
1220
1221 mBaseDisplayRect.set(0, 0, dw, dh);
1222
1223 if (isDefaultDisplay) {
1224 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1225 mCompatDisplayMetrics);
1226 }
1227 return mDisplayInfo;
1228 }
1229
1230 /**
1231 * Compute display configuration based on display properties and policy settings.
1232 * Do not call if mDisplayReady == false.
1233 */
1234 void computeScreenConfiguration(Configuration config) {
1235 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1236
1237 final int dw = displayInfo.logicalWidth;
1238 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001239 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1240 // TODO: Probably best to set this based on some setting in the display content object,
1241 // so the display can be configured for things like fullscreen.
1242 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001243
Andrii Kulian06d07d62017-03-14 11:11:47 -07001244 config.screenWidthDp =
1245 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1246 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1247 config.screenHeightDp =
1248 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1249 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001250
1251 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1252 final int leftInset = mTmpRect.left;
1253 final int topInset = mTmpRect.top;
1254 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001255 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1256 leftInset + displayInfo.appWidth /* right */,
1257 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001258 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1259 || displayInfo.rotation == Surface.ROTATION_270);
1260
1261 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1262 mDisplayMetrics.density, config);
1263
1264 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1265 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1266 ? Configuration.SCREENLAYOUT_ROUND_YES
1267 : Configuration.SCREENLAYOUT_ROUND_NO);
1268
1269 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1270 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1271 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1272 dh, mDisplayId);
1273 config.densityDpi = displayInfo.logicalDensityDpi;
1274
1275 config.colorMode =
1276 (displayInfo.isHdr()
1277 ? Configuration.COLOR_MODE_HDR_YES
1278 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001279 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001280 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1281 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1282
1283 // Update the configuration based on available input devices, lid switch,
1284 // and platform configuration.
1285 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1286 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1287 config.navigation = Configuration.NAVIGATION_NONAV;
1288
1289 int keyboardPresence = 0;
1290 int navigationPresence = 0;
1291 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1292 final int len = devices != null ? devices.length : 0;
1293 for (int i = 0; i < len; i++) {
1294 InputDevice device = devices[i];
1295 if (!device.isVirtual()) {
1296 final int sources = device.getSources();
1297 final int presenceFlag = device.isExternal() ?
1298 WindowManagerPolicy.PRESENCE_EXTERNAL :
1299 WindowManagerPolicy.PRESENCE_INTERNAL;
1300
1301 // TODO(multi-display): Configure on per-display basis.
1302 if (mService.mIsTouchDevice) {
1303 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1304 InputDevice.SOURCE_TOUCHSCREEN) {
1305 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1306 }
1307 } else {
1308 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1309 }
1310
1311 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1312 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1313 navigationPresence |= presenceFlag;
1314 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1315 && config.navigation == Configuration.NAVIGATION_NONAV) {
1316 config.navigation = Configuration.NAVIGATION_DPAD;
1317 navigationPresence |= presenceFlag;
1318 }
1319
1320 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1321 config.keyboard = Configuration.KEYBOARD_QWERTY;
1322 keyboardPresence |= presenceFlag;
1323 }
1324 }
1325 }
1326
1327 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1328 config.navigation = Configuration.NAVIGATION_DPAD;
1329 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1330 }
1331
1332 // Determine whether a hard keyboard is available and enabled.
1333 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1334 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1335 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1336 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1337 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1338 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1339 }
1340
1341 // Let the policy update hidden states.
1342 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1343 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1344 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1345 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1346 }
1347
1348 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1349 int displayId) {
1350 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1351 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1352 final int unrotDw, unrotDh;
1353 if (rotated) {
1354 unrotDw = dh;
1355 unrotDh = dw;
1356 } else {
1357 unrotDw = dw;
1358 unrotDh = dh;
1359 }
1360 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1361 displayId);
1362 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1363 displayId);
1364 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1365 displayId);
1366 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1367 displayId);
1368 return sw;
1369 }
1370
1371 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1372 DisplayMetrics dm, int dw, int dh, int displayId) {
1373 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1374 displayId);
1375 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1376 uiMode, displayId);
1377 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1378 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1379 if (curSize == 0 || size < curSize) {
1380 curSize = size;
1381 }
1382 return curSize;
1383 }
1384
1385 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1386 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1387
1388 // We need to determine the smallest width that will occur under normal
1389 // operation. To this, start with the base screen size and compute the
1390 // width under the different possible rotations. We need to un-rotate
1391 // the current screen dimensions before doing this.
1392 int unrotDw, unrotDh;
1393 if (rotated) {
1394 unrotDw = dh;
1395 unrotDh = dw;
1396 } else {
1397 unrotDw = dw;
1398 unrotDh = dh;
1399 }
1400 displayInfo.smallestNominalAppWidth = 1<<30;
1401 displayInfo.smallestNominalAppHeight = 1<<30;
1402 displayInfo.largestNominalAppWidth = 0;
1403 displayInfo.largestNominalAppHeight = 0;
1404 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1405 unrotDh);
1406 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1407 unrotDw);
1408 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1409 unrotDh);
1410 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1411 unrotDw);
1412 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1413 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1414 displayId);
1415 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1416 displayId);
1417 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1418 displayId);
1419 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1420 displayId);
1421 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1422 outConfig.screenLayout = sl;
1423 }
1424
1425 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1426 int uiMode, int displayId) {
1427 // Get the app screen size at this rotation.
1428 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1429 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1430
1431 // Compute the screen layout size class for this rotation.
1432 int longSize = w;
1433 int shortSize = h;
1434 if (longSize < shortSize) {
1435 int tmp = longSize;
1436 longSize = shortSize;
1437 shortSize = tmp;
1438 }
1439 longSize = (int)(longSize/density);
1440 shortSize = (int)(shortSize/density);
1441 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1442 }
1443
1444 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1445 int uiMode, int dw, int dh) {
1446 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1447 displayId);
1448 if (width < displayInfo.smallestNominalAppWidth) {
1449 displayInfo.smallestNominalAppWidth = width;
1450 }
1451 if (width > displayInfo.largestNominalAppWidth) {
1452 displayInfo.largestNominalAppWidth = width;
1453 }
1454 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1455 displayId);
1456 if (height < displayInfo.smallestNominalAppHeight) {
1457 displayInfo.smallestNominalAppHeight = height;
1458 }
1459 if (height > displayInfo.largestNominalAppHeight) {
1460 displayInfo.largestNominalAppHeight = height;
1461 }
1462 }
1463
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001464 DockedStackDividerController getDockedDividerController() {
1465 return mDividerControllerLocked;
1466 }
1467
Winson Chung655332c2016-10-31 13:14:28 -07001468 PinnedStackController getPinnedStackController() {
1469 return mPinnedStackControllerLocked;
1470 }
1471
Jeff Browna506a6e2013-06-04 00:02:38 -07001472 /**
1473 * Returns true if the specified UID has access to this display.
1474 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001475 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001476 return mDisplay.hasAccess(uid);
1477 }
1478
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001479 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001480 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001481 }
1482
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001483 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001484 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001485 }
1486
Wale Ogunwale61911492017-10-11 08:50:50 -07001487 /**
1488 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1489 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001490 TaskStack getSplitScreenPrimaryStack() {
1491 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001492 return (stack != null && stack.isVisible()) ? stack : null;
1493 }
1494
1495 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001496 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001497 * not visible.
1498 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001499 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1500 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001501 }
1502
1503 TaskStack getPinnedStack() {
1504 return mTaskStackContainers.getPinnedStack();
1505 }
1506
1507 private boolean hasPinnedStack() {
1508 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001509 }
1510
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001511 /**
1512 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1513 * Null is no compatible stack on the display.
1514 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001515 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001516 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1517 }
1518
1519 /**
1520 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1521 * activity type. Null is no compatible stack on the display.
1522 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001523 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001524 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001525 }
1526
Bryce Lee48f4b572017-04-10 10:54:15 -07001527 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001528 TaskStack getTopStack() {
1529 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001530 }
1531
Wale Ogunwale61911492017-10-11 08:50:50 -07001532 void onStackWindowingModeChanged(TaskStack stack) {
1533 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001534 }
1535
Andrii Kulian441e4492016-09-29 15:25:00 -07001536 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001537 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001538 super.onConfigurationChanged(newParentConfig);
1539
Andrii Kulian3a507b52016-09-19 18:14:12 -07001540 // The display size information is heavily dependent on the resources in the current
1541 // configuration, so we need to reconfigure it every time the configuration changes.
1542 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1543 mService.reconfigureDisplayLocked(this);
1544
1545 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001546 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001547 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001548
Andrii Kulian441e4492016-09-29 15:25:00 -07001549 /**
1550 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1551 * bounds were updated.
1552 */
1553 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001554 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1555 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001556 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001557 changedStackList.add(stack.mStackId);
1558 }
1559 }
Winson Chung32c566f2017-04-11 18:31:21 -07001560
1561 // If there was no pinned stack, we still need to notify the controller of the display info
1562 // update as a result of the config change. We do this here to consolidate the flow between
1563 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001564 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001565 mPinnedStackControllerLocked.onDisplayInfoChanged();
1566 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001567 }
1568
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001569 @Override
1570 boolean fillsParent() {
1571 return true;
1572 }
1573
1574 @Override
1575 boolean isVisible() {
1576 return true;
1577 }
1578
1579 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001580 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001581 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001582 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001583 }
1584
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001585 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001586 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1587 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1588 // target, or here in order if there isn't an IME target.
1589 if (traverseTopToBottom) {
1590 for (int i = mChildren.size() - 1; i >= 0; --i) {
1591 final DisplayChildWindowContainer child = mChildren.get(i);
1592 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1593 // In this case the Ime windows will be processed above their target so we skip
1594 // here.
1595 continue;
1596 }
1597 if (child.forAllWindows(callback, traverseTopToBottom)) {
1598 return true;
1599 }
1600 }
1601 } else {
1602 final int count = mChildren.size();
1603 for (int i = 0; i < count; i++) {
1604 final DisplayChildWindowContainer child = mChildren.get(i);
1605 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1606 // In this case the Ime windows will be processed above their target so we skip
1607 // here.
1608 continue;
1609 }
1610 if (child.forAllWindows(callback, traverseTopToBottom)) {
1611 return true;
1612 }
1613 }
1614 }
1615 return false;
1616 }
1617
1618 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1619 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1620 }
1621
Wale Ogunwale399c8692017-05-08 14:22:42 -07001622 @Override
1623 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001624 final WindowManagerPolicy policy = mService.mPolicy;
1625
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001626 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001627 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001628 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001629 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001630 // If the display is frozen, some activities may be in the middle of restarting, and
1631 // thus have removed their old window. If the window has the flag to hide the lock
1632 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1633 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001634 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001635 } else if (policy.isKeyguardLocked()) {
1636 // Use the last orientation the while the display is frozen with the keyguard
1637 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1638 // window. We don't want to check the show when locked window directly though as
1639 // things aren't stable while the display is frozen, for example the window could be
1640 // momentarily unavailable due to activity relaunch.
1641 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001642 + "return " + mLastOrientation);
1643 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001644 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001645 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001646 final int orientation = mAboveAppWindowsContainers.getOrientation();
1647 if (orientation != SCREEN_ORIENTATION_UNSET) {
1648 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001649 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001650 }
1651
Wale Ogunwale399c8692017-05-08 14:22:42 -07001652 // Top system windows are not requesting an orientation. Start searching from apps.
1653 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001654 }
1655
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001656 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001657 // Check if display metrics changed and update base values if needed.
1658 updateBaseDisplayMetricsIfNeeded();
1659
Craig Mautner722285e2012-09-07 13:55:58 -07001660 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001661 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001662
Wale Ogunwale61911492017-10-11 08:50:50 -07001663 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1664 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001665 }
Craig Mautner722285e2012-09-07 13:55:58 -07001666 }
1667
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001668 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001669 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1670 if (displayManagerInternal != null) {
1671 // Bootstrap the default logical display from the display manager.
1672 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1673 if (newDisplayInfo != null) {
1674 mDisplayInfo.copyFrom(newDisplayInfo);
1675 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001676 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001677
Andrii Kuliancd097992017-03-23 18:31:59 -07001678 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1679 mDisplayInfo.logicalDensityDpi);
1680 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1681 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1682 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001683 }
1684
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001685 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001686 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001687 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001688 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001689 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1690 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001691 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001692 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001693 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001694 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001695 out.set(left, top, left + width, top + height);
1696 }
1697
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001698 private void getLogicalDisplayRect(Rect out, int orientation) {
1699 getLogicalDisplayRect(out);
1700
1701 // Rotate the Rect if needed.
1702 final int currentRotation = mDisplayInfo.rotation;
1703 final int rotationDelta = deltaRotation(currentRotation, orientation);
1704 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1705 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1706 mTmpRectF.set(out);
1707 mTmpMatrix.mapRect(mTmpRectF);
1708 mTmpRectF.round(out);
1709 }
1710 }
1711
Andrii Kuliancd097992017-03-23 18:31:59 -07001712 /**
1713 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1714 * values.
1715 */
1716 private void updateBaseDisplayMetricsIfNeeded() {
1717 // Get real display metrics without overrides from WM.
1718 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1719 final int orientation = mDisplayInfo.rotation;
1720 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1721 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1722 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1723 final int newDensity = mDisplayInfo.logicalDensityDpi;
1724
1725 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1726 || mInitialDisplayHeight != newHeight
1727 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1728
1729 if (displayMetricsChanged) {
1730 // Check if display size or density is forced.
1731 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1732 || mBaseDisplayHeight != mInitialDisplayHeight;
1733 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1734
1735 // If there is an override set for base values - use it, otherwise use new values.
1736 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1737 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1738 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1739
1740 // Real display metrics changed, so we should also update initial values.
1741 mInitialDisplayWidth = newWidth;
1742 mInitialDisplayHeight = newHeight;
1743 mInitialDisplayDensity = newDensity;
1744 mService.reconfigureDisplayLocked(this);
1745 }
1746 }
1747
Bryce Lee27cec322017-03-21 09:41:37 -07001748 /** Sets the maximum width the screen resolution can be */
1749 void setMaxUiWidth(int width) {
1750 if (DEBUG_DISPLAY) {
1751 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1752 }
1753
1754 mMaxUiWidth = width;
1755
1756 // Update existing metrics.
1757 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1758 }
1759
1760 /** Update base (override) display metrics. */
1761 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1762 mBaseDisplayWidth = baseWidth;
1763 mBaseDisplayHeight = baseHeight;
1764 mBaseDisplayDensity = baseDensity;
1765
1766 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1767 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1768 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1769 mBaseDisplayWidth = mMaxUiWidth;
1770
1771 if (DEBUG_DISPLAY) {
1772 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1773 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1774 + " on display:" + getDisplayId());
1775 }
1776 }
1777
1778 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1779 }
1780
Chong Zhangf66db432016-01-13 10:39:51 -08001781 void getContentRect(Rect out) {
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001782 out.set(mDisplayFrames.mContent);
Chong Zhangf66db432016-01-13 10:39:51 -08001783 }
1784
Wale Ogunwale61911492017-10-11 08:50:50 -07001785 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001786 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1787 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001788
Wale Ogunwale61911492017-10-11 08:50:50 -07001789 final TaskStack stack = new TaskStack(mService, stackId, controller);
1790 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001791 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001792 }
1793
Andrii Kulian51c1b672017-04-07 18:39:32 -07001794 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001795 final DisplayContent prevDc = stack.getDisplayContent();
1796 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001797 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1798 + " which is not currently attached to any display");
1799 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001800 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001801 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1802 + " to its current displayId=" + mDisplayId);
1803 }
1804
Wale Ogunwale61911492017-10-11 08:50:50 -07001805 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001806 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001807 }
1808
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001809 @Override
1810 protected void addChild(DisplayChildWindowContainer child,
1811 Comparator<DisplayChildWindowContainer> comparator) {
1812 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1813 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001814
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001815 @Override
1816 protected void addChild(DisplayChildWindowContainer child, int index) {
1817 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1818 }
1819
1820 @Override
1821 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001822 // Only allow removal of direct children from this display if the display is in the process
1823 // of been removed.
1824 if (mRemovingDisplay) {
1825 super.removeChild(child);
1826 return;
1827 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001828 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001829 }
1830
Andrii Kuliand2765632016-12-12 22:26:34 -08001831 @Override
1832 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1833 // Children of the display are statically ordered, so the real intention here is to perform
1834 // the operation on the display and not the static direct children.
1835 getParent().positionChildAt(position, this, includingParents);
1836 }
1837
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001838 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001839 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1840 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001841 final int taskId = stack.taskIdFromPoint(x, y);
1842 if (taskId != -1) {
1843 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001844 }
1845 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001846 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001847 }
1848
Chong Zhang8e89b312015-09-09 15:09:30 -07001849 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001850 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001851 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001852 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001853 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001854 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001855 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07001856 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1857 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001858 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001859 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001860 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001861
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001862 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1863 if (mTmpTaskForResizePointSearchResult.searchDone) {
1864 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001865 }
1866 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001867 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001868 }
1869
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001870 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001871 // The provided task is the task on this display with focus, so if WindowManagerService's
1872 // focused app is not on this display, focusedTask will be null.
1873 if (focusedTask == null) {
1874 mTouchExcludeRegion.setEmpty();
1875 } else {
1876 mTouchExcludeRegion.set(mBaseDisplayRect);
1877 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1878 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07001879 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1880 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001881 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
1882 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08001883 }
1884 // If we removed the focused task above, add it back and only leave its
1885 // outside touch area in the exclusion. TapDectector is not interested in
1886 // any touch inside the focused task itself.
1887 if (!mTmpRect2.isEmpty()) {
1888 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1889 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001890 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001891 final WindowState inputMethod = mService.mInputMethodWindow;
1892 if (inputMethod != null && inputMethod.isVisibleLw()) {
1893 // If the input method is visible and the user is typing, we don't want these touch
1894 // events to be intercepted and used to change focus. This would likely cause a
1895 // disappearance of the input method.
1896 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001897 if (inputMethod.getDisplayId() == mDisplayId) {
1898 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1899 } else {
1900 // IME is on a different display, so we need to update its tap detector.
1901 // TODO(multidisplay): Remove when IME will always appear on same display.
1902 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1903 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001904 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001905 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1906 WindowState win = mTapExcludedWindows.get(i);
1907 win.getTouchableRegion(mTmpRegion);
1908 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1909 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001910 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07001911 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001912 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001913 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001914 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1915 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001916 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001917 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001918 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001919 }
1920
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001921 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001922 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001923 super.switchUser();
1924 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001925 }
1926
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001927 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001928 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1929 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001930 }
1931 }
1932
Wale Ogunwale10124582016-09-15 20:25:50 -07001933 @Override
1934 void removeIfPossible() {
1935 if (isAnimating()) {
1936 mDeferredRemoval = true;
1937 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001938 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001939 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001940 }
1941
Wale Ogunwale10124582016-09-15 20:25:50 -07001942 @Override
1943 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001944 mRemovingDisplay = true;
1945 try {
1946 super.removeImmediately();
1947 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07001948 if (mService.canDispatchPointerEvents()) {
1949 if (mTapDetector != null) {
1950 mService.unregisterPointerEventListener(mTapDetector);
1951 }
1952 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
1953 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1954 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001955 }
Robert Carrb1579c82017-09-05 14:54:47 -07001956 // The pending transaction won't be applied so we should
1957 // just clean up any surfaces pending destruction.
1958 onPendingTransactionApplied();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001959 } finally {
1960 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001961 }
Craig Mautner95da1082014-02-24 17:54:35 -08001962 }
1963
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001964 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001965 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001966 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001967 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1968
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001969 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001970 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001971 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001972 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001973 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001974 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001975 }
1976
Andrii Kulian0214ed92017-05-16 13:44:05 -07001977 /** @return 'true' if removal of this display content is deferred due to active animation. */
1978 boolean isRemovalDeferred() {
1979 return mDeferredRemoval;
1980 }
1981
Wale Ogunwale10124582016-09-15 20:25:50 -07001982 boolean animateForIme(float interpolatedValue, float animationTarget,
1983 float dividerAnimationTarget) {
1984 boolean updated = false;
1985
Wale Ogunwale61911492017-10-11 08:50:50 -07001986 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1987 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001988 if (stack == null || !stack.isAdjustedForIme()) {
1989 continue;
1990 }
1991
1992 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1993 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1994 updated = true;
1995 } else {
1996 mDividerControllerLocked.mLastAnimationProgress =
1997 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1998 mDividerControllerLocked.mLastDividerProgress =
1999 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2000 updated |= stack.updateAdjustForIme(
2001 mDividerControllerLocked.mLastAnimationProgress,
2002 mDividerControllerLocked.mLastDividerProgress,
2003 false /* force */);
2004 }
2005 if (interpolatedValue >= 1f) {
2006 stack.endImeAdjustAnimation();
2007 }
2008 }
2009
2010 return updated;
2011 }
2012
2013 boolean clearImeAdjustAnimation() {
2014 boolean changed = false;
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 != null && stack.isAdjustedForIme()) {
2018 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2019 changed = true;
2020 }
2021 }
2022 return changed;
2023 }
2024
2025 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002026 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2027 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002028 if (stack.isVisible() && stack.isAdjustedForIme()) {
2029 stack.beginImeAdjustAnimation();
2030 }
2031 }
2032 }
2033
2034 void adjustForImeIfNeeded() {
2035 final WindowState imeWin = mService.mInputMethodWindow;
2036 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2037 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002038 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002039 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2040 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2041 imeTargetStack.getDockSide() : DOCKED_INVALID;
2042 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2043 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2044 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002045 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002046 final boolean imeHeightChanged = imeVisible &&
2047 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2048
2049 // The divider could be adjusted for IME position, or be thinner than usual,
2050 // or both. There are three possible cases:
2051 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2052 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2053 // - If IME is not visible, divider is not moved and is normal width.
2054
2055 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002056 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2057 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002058 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002059 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2060 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002061 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2062 } else {
2063 stack.resetAdjustedForIme(false);
2064 }
2065 }
2066 mDividerControllerLocked.setAdjustedForIme(
2067 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2068 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002069 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2070 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002071 stack.resetAdjustedForIme(!dockVisible);
2072 }
2073 mDividerControllerLocked.setAdjustedForIme(
2074 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2075 }
Winson Chung655332c2016-10-31 13:14:28 -07002076 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002077 }
2078
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002079 void setInputMethodAnimLayerAdjustment(int adj) {
2080 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2081 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002082 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002083 }
2084
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002085 /**
2086 * If a window that has an animation specifying a colored background and the current wallpaper
2087 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2088 * suddenly disappear.
2089 */
2090 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002091 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2092 w -> w.mIsWallpaper && w.isVisibleNow());
2093
2094 if (visibleWallpaper != null) {
2095 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002096 }
2097 return winAnimator.mAnimLayer;
2098 }
2099
Wale Ogunwale10124582016-09-15 20:25:50 -07002100 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002101 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2102 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002103 stack.prepareFreezingTaskBounds();
2104 }
2105 }
2106
Wale Ogunwale94744212015-09-21 19:01:47 -07002107 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002108 getLogicalDisplayRect(mTmpRect, newRotation);
2109
2110 // Compute a transform matrix to undo the coordinate space transformation,
2111 // and present the window at the same physical position it previously occupied.
2112 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2113 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2114
2115 mTmpRectF.set(bounds);
2116 mTmpMatrix.mapRect(mTmpRectF);
2117 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002118 }
2119
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002120 static int deltaRotation(int oldRotation, int newRotation) {
2121 int delta = newRotation - oldRotation;
2122 if (delta < 0) delta += 4;
2123 return delta;
2124 }
2125
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002126 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002127 Matrix outMatrix) {
2128 // For rotations without Z-ordering we don't need the target rectangle's position.
2129 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2130 displayHeight, outMatrix);
2131 }
2132
2133 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2134 float displayWidth, float displayHeight, Matrix outMatrix) {
2135 switch (rotation) {
2136 case ROTATION_0:
2137 outMatrix.reset();
2138 break;
2139 case ROTATION_270:
2140 outMatrix.setRotate(270, 0, 0);
2141 outMatrix.postTranslate(0, displayHeight);
2142 outMatrix.postTranslate(rectTop, 0);
2143 break;
2144 case ROTATION_180:
2145 outMatrix.reset();
2146 break;
2147 case ROTATION_90:
2148 outMatrix.setRotate(90, 0, 0);
2149 outMatrix.postTranslate(displayWidth, 0);
2150 outMatrix.postTranslate(-rectTop, rectLeft);
2151 break;
2152 }
2153 }
2154
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002155 @CallSuper
2156 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002157 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002158 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002159 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002160 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002161 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2162 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002163 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002164 }
2165 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2166 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002167 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2168 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002169 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002170 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002171 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2172 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002173 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002174 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002175 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2176 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002177 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002178 }
2179 proto.write(DPI, mBaseDisplayDensity);
2180 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002181 proto.write(ROTATION, mRotation);
2182 final ScreenRotationAnimation screenRotationAnimation =
2183 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2184 if (screenRotationAnimation != null) {
2185 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2186 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002187 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002188 proto.end(token);
2189 }
2190
Craig Mautnera91f9e22012-09-14 16:22:08 -07002191 public void dump(String prefix, PrintWriter pw) {
2192 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2193 final String subPrefix = " " + prefix;
2194 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2195 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2196 pw.print("dpi");
2197 if (mInitialDisplayWidth != mBaseDisplayWidth
2198 || mInitialDisplayHeight != mBaseDisplayHeight
2199 || mInitialDisplayDensity != mBaseDisplayDensity) {
2200 pw.print(" base=");
2201 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2202 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2203 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002204 if (mDisplayScalingDisabled) {
2205 pw.println(" noscale");
2206 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002207 pw.print(" cur=");
2208 pw.print(mDisplayInfo.logicalWidth);
2209 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2210 pw.print(" app=");
2211 pw.print(mDisplayInfo.appWidth);
2212 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2213 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2214 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2215 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2216 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002217 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002218 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002219 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002220
Craig Mautnerdc548482014-02-05 13:35:24 -08002221 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002222 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002223 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2224 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002225 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002226 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002227
Craig Mautnerdc548482014-02-05 13:35:24 -08002228 pw.println();
2229 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002230 pw.println();
2231 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002232 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002233 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002234 pw.print(" Exiting #"); pw.print(i);
2235 pw.print(' '); pw.print(token);
2236 pw.println(':');
2237 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002238 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002239 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002240
Jorim Jaggi31f71702016-05-04 16:43:04 -07002241 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002242
2243 // Dump stack references
2244 final TaskStack homeStack = getHomeStack();
2245 if (homeStack != null) {
2246 pw.println(prefix + "homeStack=" + homeStack.getName());
2247 }
2248 final TaskStack pinnedStack = getPinnedStack();
2249 if (pinnedStack != null) {
2250 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2251 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002252 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002253 if (splitScreenPrimaryStack != null) {
2254 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2255 }
2256
2257 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002258 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002259 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002260 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002261
2262 if (mInputMethodAnimLayerAdjustment != 0) {
2263 pw.println(subPrefix
2264 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2265 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002266
2267 pw.println();
2268 mDisplayFrames.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002269 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002270
2271 @Override
2272 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002273 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002274 }
2275
2276 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002277 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002278 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002279
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002280 /** Returns true if the stack in the windowing mode is visible. */
2281 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002282 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002283 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002284 }
2285
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002286 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002287 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002288 final int x = (int) xf;
2289 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002290 final WindowState touchedWin = getWindow(w -> {
2291 final int flags = w.mAttrs.flags;
2292 if (!w.isVisibleLw()) {
2293 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002294 }
2295 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002296 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002297 }
2298
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002299 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002300 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002301 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002302 }
2303
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002304 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002305
2306 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002307 return mTmpRegion.contains(x, y) || touchFlags == 0;
2308 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002309
2310 return touchedWin;
2311 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002312
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002313 boolean canAddToastWindowForUid(int uid) {
2314 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002315 // Also if the app is focused adding more than one toast at
2316 // a time for better backwards compatibility.
2317 final WindowState focusedWindowForUid = getWindow(w ->
2318 w.mOwnerUid == uid && w.isFocused());
2319 if (focusedWindowForUid != null) {
2320 return true;
2321 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002322 final WindowState win = getWindow(w ->
2323 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2324 && !w.mWindowRemovalAllowed);
2325 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002326 }
2327
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002328 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002329 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2330 return;
2331 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002332
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002333 // Used to communicate the old focus to the callback method.
2334 mTmpWindow = oldFocus;
2335
2336 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002337 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002338
2339 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002340 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002341
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002342 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002343
2344 if (mTmpWindow == null) {
2345 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2346 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002347 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002348 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002349 }
2350
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002351 /** Updates the layer assignment of windows on this display. */
2352 void assignWindowLayers(boolean setLayoutNeeded) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07002353 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002354 if (setLayoutNeeded) {
2355 setLayoutNeeded();
2356 }
Robert Carrb1579c82017-09-05 14:54:47 -07002357
Robert Carrf59b8dd2017-10-02 18:58:36 -07002358 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002359 // the application of this transaction until the animation pass triggers
2360 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2361 // the hiding and showing of surfaces.
2362 scheduleAnimation();
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002363 }
2364
Wale Ogunwale1666e312016-12-16 11:27:18 -08002365 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2366 // moving containers or resizing them. Need to investigate the best way to have it automatically
2367 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002368 void layoutAndAssignWindowLayersIfNeeded() {
2369 mService.mWindowsChanged = true;
2370 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002371
2372 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2373 false /*updateInputWindows*/)) {
2374 assignWindowLayers(false /* setLayoutNeeded */);
2375 }
2376
2377 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2378 mService.mWindowPlacerLocked.performSurfacePlacement();
2379 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2380 }
2381
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002382 /** Returns true if a leaked surface was destroyed */
2383 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002384 // Used to indicate that a surface was leaked.
2385 mTmpWindow = null;
2386 forAllWindows(w -> {
2387 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002388 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002389 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002390 }
2391 if (!mService.mSessions.contains(wsa.mSession)) {
2392 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002393 + w + " surface=" + wsa.mSurfaceController
2394 + " token=" + w.mToken
2395 + " pid=" + w.mSession.mPid
2396 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002397 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002398 mService.mForceRemoves.add(w);
2399 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002400 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002401 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002402 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002403 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002404 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002405 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002406 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002407 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002408 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002409
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002410 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002411 }
2412
2413 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002414 * Determine and return the window that should be the IME target.
2415 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2416 * @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 +00002417 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002418 WindowState computeImeTarget(boolean updateImeTarget) {
2419 if (mService.mInputMethodWindow == null) {
2420 // There isn't an IME so there shouldn't be a target...That was easy!
2421 if (updateImeTarget) {
2422 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2423 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2424 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2425 }
2426 return null;
2427 }
2428
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002429 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2430 // same display. Or even when the current IME/target are not on the same screen as the next
2431 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002432 mUpdateImeTarget = updateImeTarget;
2433 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002434
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002435
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002436 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2437 // to be on top of it, but it is not -really- where input will go. So look down below
2438 // for a real window to target...
2439 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2440 final AppWindowToken token = target.mAppToken;
2441 if (token != null) {
2442 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2443 if (betterTarget != null) {
2444 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002445 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002446 }
2447 }
2448
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002449 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2450 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002451
2452 // Now, a special case -- if the last target's window is in the process of exiting, and is
2453 // above the new target, keep on the last target to avoid flicker. Consider for example a
2454 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2455 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2456 // scrim.
2457 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002458 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2459 && (target == null
2460 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002461 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002462 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002463 }
2464
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002465 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2466 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002467
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002468 if (target == null) {
2469 if (updateImeTarget) {
2470 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2471 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2472 + Debug.getCallers(4) : ""));
2473 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2474 }
2475
2476 return null;
2477 }
2478
2479 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002480 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2481 if (token != null) {
2482
2483 // Now some fun for dealing with window animations that modify the Z order. We need
2484 // to look at all windows below the current target that are in this app, finding the
2485 // highest visible one in layering.
2486 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002487 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002488 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002489 }
2490
2491 if (highestTarget != null) {
2492 final AppTransition appTransition = mService.mAppTransition;
2493 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2494 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2495 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002496 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002497
2498 if (appTransition.isTransitionSet()) {
2499 // If we are currently setting up for an animation, hold everything until we
2500 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002501 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2502 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002503 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002504 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002505 // If the window we are currently targeting is involved with an animation,
2506 // and it is on top of the next target we will be over, then hold off on
2507 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002508 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2509 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002510 }
2511 }
2512 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002513
2514 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2515 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2516 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002517 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002518 }
2519
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002520 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002521 }
2522
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002523 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2524 if (target == mService.mInputMethodTarget
2525 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2526 && mInputMethodAnimLayerAdjustment == layerAdj) {
2527 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002528 }
2529
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002530 mService.mInputMethodTarget = target;
2531 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2532 setInputMethodAnimLayerAdjustment(layerAdj);
2533 assignWindowLayers(false /* setLayoutNeeded */);
2534 }
2535
2536 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2537 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2538 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2539 }
2540
2541 // Used to indicate we have reached the first window in the range we are interested in.
2542 mTmpWindow = null;
2543
2544 // TODO: Figure-out a more efficient way to do this.
2545 final WindowState candidate = getWindow(w -> {
2546 if (w == top) {
2547 // Reached the first window in the range we are interested in.
2548 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002549 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002550 if (mTmpWindow == null) {
2551 return false;
2552 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002553
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002554 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2555 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002556 }
2557 // If we reached the bottom of the range of windows we are considering,
2558 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002559 if (w == bottom) {
2560 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002561 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002562 return false;
2563 });
2564
2565 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002566 }
2567
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002568 void setLayoutNeeded() {
2569 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2570 mLayoutNeeded = true;
2571 }
2572
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002573 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002574 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2575 mLayoutNeeded = false;
2576 }
2577
2578 boolean isLayoutNeeded() {
2579 return mLayoutNeeded;
2580 }
2581
Wale Ogunwale02319a62016-09-26 15:21:22 -07002582 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2583 if (mTokenMap.isEmpty()) {
2584 return;
2585 }
2586 pw.println(" Display #" + mDisplayId);
2587 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2588 while (it.hasNext()) {
2589 final WindowToken token = it.next();
2590 pw.print(" ");
2591 pw.print(token);
2592 if (dumpAll) {
2593 pw.println(':');
2594 token.dump(pw, " ");
2595 } else {
2596 pw.println();
2597 }
2598 }
2599 }
2600
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002601 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002602 final int[] index = new int[1];
2603 forAllWindows(w -> {
2604 final WindowStateAnimator wAnim = w.mWinAnimator;
2605 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2606 index[0] = index[0] + 1;
2607 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002608 }
2609
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002610 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002611 forAllWindows(w -> {
2612 w.mWinAnimator.enableSurfaceTrace(fd);
2613 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002614 }
2615
2616 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002617 forAllWindows(w -> {
2618 w.mWinAnimator.disableSurfaceTrace();
2619 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002620 }
2621
Jorim Jaggife762342016-10-13 14:33:27 +02002622 /**
2623 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2624 */
2625 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2626 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002627 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002628 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2629 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002630 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002631 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2632 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002633 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002634 }
2635
Wale Ogunwale494009b82016-10-21 09:01:38 -07002636 boolean checkWaitingForWindows() {
2637
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002638 mHaveBootMsg = false;
2639 mHaveApp = false;
2640 mHaveWallpaper = false;
2641 mHaveKeyguard = true;
2642
2643 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002644 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2645 return true;
2646 }
2647 if (w.isDrawnLw()) {
2648 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002649 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002650 } else if (w.mAttrs.type == TYPE_APPLICATION
2651 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002652 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002653 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002654 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002655 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002656 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002657 }
2658 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002659 return false;
2660 });
2661
2662 if (visibleWindow != null) {
2663 // We have a visible window.
2664 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002665 }
2666
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002667 // if the wallpaper service is disabled on the device, we're never going to have
2668 // wallpaper, don't bother waiting for it
2669 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2670 com.android.internal.R.bool.config_enableWallpaperService)
2671 && !mService.mOnlyCore;
2672
Wale Ogunwale494009b82016-10-21 09:01:38 -07002673 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2674 "******** booted=" + mService.mSystemBooted
2675 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002676 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2677 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2678 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002679
2680 // If we are turning on the screen to show the boot message, don't do it until the boot
2681 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002682 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002683 return true;
2684 }
2685
2686 // If we are turning on the screen after the boot is completed normally, don't do so until
2687 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002688 if (mService.mSystemBooted
2689 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002690 return true;
2691 }
2692
2693 return false;
2694 }
2695
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002696 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002697 mTmpWindowAnimator = animator;
2698 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002699 }
2700
2701 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002702 resetAnimationBackgroundAnimator();
2703
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002704 // Used to indicate a detached wallpaper.
2705 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002706 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002707
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002708 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002709
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002710 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002711 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002712 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2713 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002714 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2715 }
2716 }
2717
Wale Ogunwale494009b82016-10-21 09:01:38 -07002718 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002719 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002720 if (imFocus == null) {
2721 return false;
2722 }
2723
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002724 if (DEBUG_INPUT_METHOD) {
2725 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2726 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2727 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002728 }
2729
Wale Ogunwale494009b82016-10-21 09:01:38 -07002730 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2731
2732 if (DEBUG_INPUT_METHOD) {
2733 Slog.i(TAG_WM, "IM target client: " + imeClient);
2734 if (imeClient != null) {
2735 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2736 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2737 }
2738 }
2739
2740 return imeClient != null && imeClient.asBinder() == client.asBinder();
2741 }
2742
2743 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002744 final WindowState win = getWindow(
2745 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2746 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002747 }
2748
2749 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002750 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002751 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002752 final int curValue = w.mSystemUiVisibility;
2753 final int diff = (curValue ^ visibility) & globalDiff;
2754 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002755 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002756 w.mSeq++;
2757 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002758 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002759 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2760 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002761 visibility, newValue, diff);
2762 }
2763 } catch (RemoteException e) {
2764 // so sorry
2765 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002766 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002767 }
2768
2769 void onWindowFreezeTimeout() {
2770 Slog.w(TAG_WM, "Window freeze timeout expired.");
2771 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002772
2773 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002774 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002775 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002776 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002777 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002778 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2779 - mService.mDisplayFreezeTime);
2780 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002781 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002782 mService.mWindowPlacerLocked.performSurfacePlacement();
2783 }
2784
2785 void waitForAllWindowsDrawn() {
2786 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002787 forAllWindows(w -> {
2788 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2789 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2790 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002791 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002792 w.mLastContentInsets.set(-1, -1, -1, -1);
2793 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002794 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002795 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002796 }
2797
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002798 // TODO: Super crazy long method that should be broken down...
2799 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2800
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002801 final int dw = mDisplayInfo.logicalWidth;
2802 final int dh = mDisplayInfo.logicalHeight;
2803 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2804
2805 mTmpUpdateAllDrawn.clear();
2806
2807 int repeats = 0;
2808 do {
2809 repeats++;
2810 if (repeats > 6) {
2811 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2812 clearLayoutNeeded();
2813 break;
2814 }
2815
2816 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2817 pendingLayoutChanges);
2818
Andrii Kulian839def92016-11-02 10:58:58 -07002819 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2820 // the wallpaper window jumping across displays.
2821 // Remove check for default display when there will be support for multiple wallpaper
2822 // targets (on different displays).
2823 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002824 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002825 }
2826
2827 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2828 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2829 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2830 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002831 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002832 }
2833 }
2834
2835 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2836 setLayoutNeeded();
2837 }
2838
2839 // FIRST LOOP: Perform a layout, if needed.
2840 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2841 performLayout(repeats == 1, false /* updateInputWindows */);
2842 } else {
2843 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2844 }
2845
2846 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2847 pendingLayoutChanges = 0;
2848
2849 if (isDefaultDisplay) {
2850 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002851 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002852 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2853 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2854 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2855 }
2856 } while (pendingLayoutChanges != 0);
2857
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002858 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002859
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002860 mTmpRecoveringMemory = recoveringMemory;
2861 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002862
2863 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002864 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2865 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2866 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002867 true /* inTraversal, must call performTraversalInTrans... below */);
2868
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002869 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2870 if (wallpaperVisible != mLastWallpaperVisible) {
2871 mLastWallpaperVisible = wallpaperVisible;
2872 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2873 }
2874
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002875 while (!mTmpUpdateAllDrawn.isEmpty()) {
2876 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2877 // See if any windows have been drawn, so they (and others associated with them)
2878 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002879 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002880 }
2881
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002882 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002883 }
2884
2885 void performLayout(boolean initial, boolean updateInputWindows) {
2886 if (!isLayoutNeeded()) {
2887 return;
2888 }
2889 clearLayoutNeeded();
2890
2891 final int dw = mDisplayInfo.logicalWidth;
2892 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002893 if (DEBUG_LAYOUT) {
2894 Slog.v(TAG, "-------------------------------------");
2895 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2896 }
2897
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002898 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
2899 // TODO: Not sure if we really need to set the rotation here since we are updating from the
2900 // display info above...
2901 mDisplayFrames.mRotation = mRotation;
2902 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002903 if (isDefaultDisplay) {
2904 // Not needed on non-default displays.
2905 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2906 mService.mScreenRect.set(0, 0, dw, dh);
2907 }
2908
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002909 int seq = mService.mLayoutSeq + 1;
2910 if (seq < 0) seq = 0;
2911 mService.mLayoutSeq = seq;
2912
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002913 // Used to indicate that we have processed the dream window and all additional windows are
2914 // behind it.
2915 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002916 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002917
2918 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002919 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002920
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002921 // Used to indicate that we have processed the dream window and all additional attached
2922 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002923 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002924 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002925
2926 // Now perform layout of attached windows, which usually depend on the position of the
2927 // window they are attached to. XXX does not deal with windows that are attached to windows
2928 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002929 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002930
2931 // Window frames may have changed. Tell the input dispatcher about it.
2932 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2933 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2934 if (updateInputWindows) {
2935 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2936 }
2937
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002938 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2939 }
2940
2941 /**
2942 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2943 * In portrait mode, it grabs the full screenshot.
2944 *
2945 * @param width the width of the target bitmap
2946 * @param height the height of the target bitmap
2947 * @param includeFullDisplay true if the screen should not be cropped before capture
2948 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2949 * @param config of the output bitmap
2950 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002951 * @param includeDecor whether to include window decors, like the status or navigation bar
2952 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002953 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002954 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002955 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002956 boolean wallpaperOnly, boolean includeDecor) {
2957 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2958 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002959 if (bitmap == null) {
2960 return null;
2961 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002962
2963 if (DEBUG_SCREENSHOT) {
2964 // TEST IF IT's ALL BLACK
2965 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2966 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2967 bitmap.getHeight());
2968 boolean allBlack = true;
2969 final int firstColor = buffer[0];
2970 for (int i = 0; i < buffer.length; i++) {
2971 if (buffer[i] != firstColor) {
2972 allBlack = false;
2973 break;
2974 }
2975 }
2976 if (allBlack) {
2977 final WindowState appWin = mScreenshotApplicationState.appWin;
2978 final int maxLayer = mScreenshotApplicationState.maxLayer;
2979 final int minLayer = mScreenshotApplicationState.minLayer;
2980 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2981 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2982 (appWin != null ?
2983 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2984 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2985 }
2986 }
2987
2988 // Create a copy of the screenshot that is immutable and backed in ashmem.
2989 // This greatly reduces the overhead of passing the bitmap between processes.
2990 Bitmap ret = bitmap.createAshmemBitmap(config);
2991 bitmap.recycle();
2992 return ret;
2993 }
2994
2995 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2996 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2997 boolean includeDecor) {
2998 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2999 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
3000 }
3001
3002 private <E> E screenshotApplications(IBinder appToken, int width, int height,
3003 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
3004 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003005 int dw = mDisplayInfo.logicalWidth;
3006 int dh = mDisplayInfo.logicalHeight;
3007 if (dw == 0 || dh == 0) {
3008 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3009 + ": returning null. logical widthxheight=" + dw + "x" + dh);
3010 return null;
3011 }
3012
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003013 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003014
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003015 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003016 final Rect frame = new Rect();
3017 final Rect stackBounds = new Rect();
3018
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003019 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003020 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08003021 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003022 synchronized(mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02003023 if (!mService.mPolicy.isScreenOn()) {
3024 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Attempted to take screenshot while display"
3025 + " was off.");
3026 return null;
3027 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003028 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003029 mScreenshotApplicationState.appWin = null;
3030 forAllWindows(w -> {
3031 if (!w.mHasSurface) {
3032 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003033 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003034 if (w.mLayer >= aboveAppLayer) {
3035 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003036 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003037 if (wallpaperOnly && !w.mIsWallpaper) {
3038 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003039 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003040 if (w.mIsImWindow) {
Jorim Jaggieb59d6e2017-05-04 13:54:22 +02003041 return false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003042 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003043 // If this is the wallpaper layer and we're only looking for the wallpaper layer
3044 // then the target window state is this one.
3045 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003046 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003047 }
3048
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003049 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003050 // We have not ran across the target window yet, so it is probably behind
3051 // the wallpaper. This can happen when the keyguard is up and all windows
3052 // are moved behind the wallpaper. We don't want to include the wallpaper
3053 // layer in the screenshot as it will cover-up the layer of the target
3054 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003055 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003056 }
3057 // Fall through. The target window is in front of the wallpaper. For this
3058 // case we want to include the wallpaper layer in the screenshot because
3059 // the target window might have some transparent areas.
3060 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003061 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003062 // This app window is of no interest if it is not associated with the
3063 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003064 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003065 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003066 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003067 }
3068
3069 // Include this window.
3070
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003071 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003072
3073 // Don't include wallpaper in bounds calculation
Jorim Jaggi70f59482017-05-04 14:05:59 +02003074 if (!w.mIsWallpaper && !mutableIncludeFullDisplay.value) {
3075 if (includeDecor) {
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003076 final Task task = w.getTask();
3077 if (task != null) {
3078 task.getBounds(frame);
3079 } else {
Jorim Jaggi30d64f32017-04-07 16:33:17 +02003080
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003081 // No task bounds? Too bad! Ain't no screenshot then.
3082 return true;
3083 }
Jorim Jaggi70f59482017-05-04 14:05:59 +02003084 } else {
3085 final Rect wf = w.mFrame;
3086 final Rect cr = w.mContentInsets;
3087 int left = wf.left + cr.left;
3088 int top = wf.top + cr.top;
3089 int right = wf.right - cr.right;
3090 int bottom = wf.bottom - cr.bottom;
3091 frame.union(left, top, right, bottom);
3092 w.getVisibleBounds(stackBounds);
3093 if (!Rect.intersects(frame, stackBounds)) {
3094 // Set frame empty if there's no intersection.
3095 frame.setEmpty();
3096 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003097 }
3098 }
3099
3100 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003101 (w.mAppToken != null && w.mAppToken.token == appToken)
3102 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi406386a82017-09-15 14:17:40 +02003103 if (foundTargetWs && winAnim.getShown() && winAnim.mLastAlpha > 0f) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003104 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003105 }
3106
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003107 if (w.isObscuringDisplay()){
3108 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003109 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003110 return false;
3111 }, true /* traverseTopToBottom */);
3112
3113 final WindowState appWin = mScreenshotApplicationState.appWin;
3114 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003115
3116 if (appToken != null && appWin == null) {
3117 // Can't find a window to snapshot.
3118 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
3119 "Screenshot: Couldn't find a surface matching " + appToken);
3120 return null;
3121 }
3122
3123 if (!screenshotReady) {
3124 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3125 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3126 appWin.mWinAnimator.mDrawState)));
3127 return null;
3128 }
3129
3130 // Screenshot is ready to be taken. Everything from here below will continue
3131 // through the bottom of the loop and return a value. We only stay in the loop
3132 // because we don't want to release the mWindowMap lock until the screenshot is
3133 // taken.
3134
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003135
Jorim Jaggi02886a82016-12-06 09:10:06 -08003136 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003137 // Constrain frame to the screen size.
3138 if (!frame.intersect(0, 0, dw, dh)) {
3139 frame.setEmpty();
3140 }
3141 } else {
3142 // Caller just wants entire display.
3143 frame.set(0, 0, dw, dh);
3144 }
3145 if (frame.isEmpty()) {
3146 return null;
3147 }
3148
3149 if (width < 0) {
3150 width = (int) (frame.width() * frameScale);
3151 }
3152 if (height < 0) {
3153 height = (int) (frame.height() * frameScale);
3154 }
3155
3156 // Tell surface flinger what part of the image to crop. Take the top
3157 // right part of the application, and crop the larger dimension to fit.
3158 Rect crop = new Rect(frame);
3159 if (width / (float) frame.width() < height / (float) frame.height()) {
3160 int cropWidth = (int)((float)width / (float)height * frame.height());
3161 crop.right = crop.left + cropWidth;
3162 } else {
3163 int cropHeight = (int)((float)height / (float)width * frame.width());
3164 crop.bottom = crop.top + cropHeight;
3165 }
3166
3167 // The screenshot API does not apply the current screen rotation.
3168 int rot = mDisplay.getRotation();
3169
3170 if (rot == ROTATION_90 || rot == ROTATION_270) {
3171 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3172 }
3173
3174 // Surfaceflinger is not aware of orientation, so convert our logical
3175 // crop to surfaceflinger's portrait orientation.
3176 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3177
3178 if (DEBUG_SCREENSHOT) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003179 forAllWindows(w -> {
3180 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3181 Slog.i(TAG_WM, w + ": " + w.mLayer
3182 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003183 + " surfaceLayer=" + ((controller == null)
3184 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003185 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003186 }
3187
3188 final ScreenRotationAnimation screenRotationAnimation =
3189 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3190 final boolean inRotation = screenRotationAnimation != null &&
3191 screenRotationAnimation.isAnimating();
3192 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3193 "Taking screenshot while rotating");
3194
3195 // We force pending transactions to flush before taking
3196 // the screenshot by pushing an empty synchronous transaction.
3197 SurfaceControl.openTransaction();
3198 SurfaceControl.closeTransactionSync();
3199
Robert Carrb1579c82017-09-05 14:54:47 -07003200 // TODO(b/68392460): We should screenshot Task controls directly
3201 // but it's difficult at the moment as the Task doesn't have the
3202 // correct size set.
3203 bitmap = screenshoter.screenshot(crop, width, height, 0, 1,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003204 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003205 if (bitmap == null) {
Robert Carrb1579c82017-09-05 14:54:47 -07003206 Slog.w(TAG_WM, "Failed to take screenshot");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003207 return null;
3208 }
3209 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003210 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003211 }
3212
3213 // TODO: Can this use createRotationMatrix()?
3214 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3215 if (rot == Surface.ROTATION_90) {
3216 final int tmp = crop.top;
3217 crop.top = dw - crop.right;
3218 crop.right = crop.bottom;
3219 crop.bottom = dw - crop.left;
3220 crop.left = tmp;
3221 } else if (rot == Surface.ROTATION_180) {
3222 int tmp = crop.top;
3223 crop.top = dh - crop.bottom;
3224 crop.bottom = dh - tmp;
3225 tmp = crop.right;
3226 crop.right = dw - crop.left;
3227 crop.left = dw - tmp;
3228 } else if (rot == Surface.ROTATION_270) {
3229 final int tmp = crop.top;
3230 crop.top = crop.left;
3231 crop.left = dh - crop.bottom;
3232 crop.bottom = crop.right;
3233 crop.right = dh - tmp;
3234 }
3235 }
3236
3237 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003238 // Used to indicate the layout is needed.
3239 mTmpWindow = null;
3240
3241 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003242 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003243 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003244 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003245 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003246 w.setDisplayLayoutNeeded();
3247 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003248 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003249
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003250 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003251 mService.mWindowPlacerLocked.performSurfacePlacement();
3252 }
3253 }
3254
Wale Ogunwale1666e312016-12-16 11:27:18 -08003255 void setExitingTokensHasVisible(boolean hasVisible) {
3256 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3257 mExitingTokens.get(i).hasVisible = hasVisible;
3258 }
3259
3260 // Initialize state of exiting applications.
3261 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3262 }
3263
3264 void removeExistingTokensIfPossible() {
3265 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3266 final WindowToken token = mExitingTokens.get(i);
3267 if (!token.hasVisible) {
3268 mExitingTokens.remove(i);
3269 }
3270 }
3271
3272 // Time to remove any exiting applications?
3273 mTaskStackContainers.removeExistingAppTokensIfPossible();
3274 }
3275
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003276 @Override
3277 void onDescendantOverrideConfigurationChanged() {
3278 setLayoutNeeded();
3279 mService.requestTraversal();
3280 }
3281
David Stevens9440dc82017-03-16 19:00:20 -07003282 boolean okToDisplay() {
3283 if (mDisplayId == DEFAULT_DISPLAY) {
3284 return !mService.mDisplayFrozen
3285 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3286 }
3287 return mDisplayInfo.state == Display.STATE_ON;
3288 }
3289
3290 boolean okToAnimate() {
3291 return okToDisplay() &&
3292 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3293 }
3294
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003295 static final class TaskForResizePointSearchResult {
3296 boolean searchDone;
3297 Task taskForResize;
3298
3299 void reset() {
3300 searchDone = false;
3301 taskForResize = null;
3302 }
3303 }
Robert Carr3b716242016-08-16 16:02:21 -07003304
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003305 private static final class ApplySurfaceChangesTransactionState {
3306 boolean displayHasContent;
3307 boolean obscured;
3308 boolean syswin;
3309 boolean focusDisplayed;
3310 float preferredRefreshRate;
3311 int preferredModeId;
3312
3313 void reset() {
3314 displayHasContent = false;
3315 obscured = false;
3316 syswin = false;
3317 focusDisplayed = false;
3318 preferredRefreshRate = 0;
3319 preferredModeId = 0;
3320 }
3321 }
3322
3323 private static final class ScreenshotApplicationState {
3324 WindowState appWin;
3325 int maxLayer;
3326 int minLayer;
3327 boolean screenshotReady;
3328
3329 void reset(boolean screenshotReady) {
3330 appWin = null;
3331 maxLayer = 0;
3332 minLayer = 0;
3333 this.screenshotReady = screenshotReady;
3334 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3335 }
3336 }
3337
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003338 /**
3339 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3340 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3341 * homogeneous children type which is currently required by sub-classes of
3342 * {@link WindowContainer} class.
3343 */
3344 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3345
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003346 @Override
3347 boolean fillsParent() {
3348 return true;
3349 }
3350
3351 @Override
3352 boolean isVisible() {
3353 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003354 }
3355 }
3356
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003357 /**
3358 * Window container class that contains all containers on this display relating to Apps.
3359 * I.e Activities.
3360 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003361 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003362 /**
3363 * A control placed at the appropriate level for transitions to occur.
3364 */
3365 SurfaceControl mAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003366
Wale Ogunwale61911492017-10-11 08:50:50 -07003367 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3368 // through the list to find them.
3369 private TaskStack mHomeStack = null;
3370 private TaskStack mPinnedStack = null;
3371 private TaskStack mSplitScreenPrimaryStack = null;
3372
3373 /**
3374 * Returns the topmost stack on the display that is compatible with the input windowing mode
3375 * and activity type. Null is no compatible stack on the display.
3376 */
3377 TaskStack getStack(int windowingMode, int activityType) {
3378 if (activityType == ACTIVITY_TYPE_HOME) {
3379 return mHomeStack;
3380 }
3381 if (windowingMode == WINDOWING_MODE_PINNED) {
3382 return mPinnedStack;
3383 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3384 return mSplitScreenPrimaryStack;
3385 }
3386 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3387 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003388 if (activityType == ACTIVITY_TYPE_UNDEFINED
3389 && windowingMode == stack.getWindowingMode()) {
3390 // Passing in undefined type means we want to match the topmost stack with the
3391 // windowing mode.
3392 return stack;
3393 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003394 if (stack.isCompatible(windowingMode, activityType)) {
3395 return stack;
3396 }
3397 }
3398 return null;
3399 }
3400
3401 @VisibleForTesting
3402 TaskStack getTopStack() {
3403 return mTaskStackContainers.getChildCount() > 0
3404 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3405 }
3406
3407 TaskStack getHomeStack() {
3408 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3409 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3410 }
3411 return mHomeStack;
3412 }
3413
3414 TaskStack getPinnedStack() {
3415 return mPinnedStack;
3416 }
3417
Matthew Ng64e77cf2017-10-31 14:01:31 -07003418 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003419 return mSplitScreenPrimaryStack;
3420 }
3421
Andrii Kulian839def92016-11-02 10:58:58 -07003422 /**
3423 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003424 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003425 */
3426 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003427 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003428 addChild(stack, onTop);
3429 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003430 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003431
Wale Ogunwale61911492017-10-11 08:50:50 -07003432 void onStackWindowingModeChanged(TaskStack stack) {
3433 removeStackReferenceIfNeeded(stack);
3434 addStackReferenceIfNeeded(stack);
3435 if (stack == mPinnedStack && getTopStack() != stack) {
3436 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3437 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3438 }
3439 }
3440
3441 private void addStackReferenceIfNeeded(TaskStack stack) {
3442 if (stack.isActivityTypeHome()) {
3443 if (mHomeStack != null) {
3444 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
3445 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
3446 }
3447 mHomeStack = stack;
3448 }
3449 final int windowingMode = stack.getWindowingMode();
3450 if (windowingMode == WINDOWING_MODE_PINNED) {
3451 if (mPinnedStack != null) {
3452 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3453 + mPinnedStack + " already exist on display=" + this
3454 + " stack=" + stack);
3455 }
3456 mPinnedStack = stack;
3457 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3458 if (mSplitScreenPrimaryStack != null) {
3459 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3460 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3461 + " already exist on display=" + this + " stack=" + stack);
3462 }
3463 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003464 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003465 }
3466 }
3467
3468 private void removeStackReferenceIfNeeded(TaskStack stack) {
3469 if (stack == mHomeStack) {
3470 mHomeStack = null;
3471 } else if (stack == mPinnedStack) {
3472 mPinnedStack = null;
3473 } else if (stack == mSplitScreenPrimaryStack) {
3474 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003475 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3476 mService.setDockedStackCreateStateLocked(
3477 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3478 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003479 }
Andrii Kulian839def92016-11-02 10:58:58 -07003480 }
3481
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003482 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003483 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3484 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003485 addChild(stack, addIndex);
3486 setLayoutNeeded();
3487 }
3488
Wale Ogunwale61911492017-10-11 08:50:50 -07003489 @Override
3490 protected void removeChild(TaskStack stack) {
3491 super.removeChild(stack);
3492 removeStackReferenceIfNeeded(stack);
3493 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003494
3495 @Override
3496 boolean isOnTop() {
3497 // Considered always on top
3498 return true;
3499 }
3500
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003501 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003502 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003503 if (child.getWindowConfiguration().isAlwaysOnTop()
3504 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003505 // This stack is always-on-top, override the default behavior.
3506 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3507
3508 // Moving to its current position, as we must call super but we don't want to
3509 // perform any meaningful action.
3510 final int currentPosition = mChildren.indexOf(child);
3511 super.positionChildAt(currentPosition, child, false /* includingParents */);
3512 return;
3513 }
3514
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003515 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3516 super.positionChildAt(targetPosition, child, includingParents);
3517
3518 setLayoutNeeded();
3519 }
3520
3521 /**
3522 * When stack is added or repositioned, find a proper position for it.
3523 * This will make sure that pinned stack always stays on top.
3524 * @param requestedPosition Position requested by caller.
3525 * @param stack Stack to be added or positioned.
3526 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3527 * @return The proper position for the stack.
3528 */
3529 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3530 final int topChildPosition = mChildren.size() - 1;
3531 boolean toTop = requestedPosition == POSITION_TOP;
3532 toTop |= adding ? requestedPosition >= topChildPosition + 1
3533 : requestedPosition >= topChildPosition;
3534 int targetPosition = requestedPosition;
3535
Wale Ogunwale61911492017-10-11 08:50:50 -07003536 if (toTop && stack.getWindowingMode() != WINDOWING_MODE_PINNED && hasPinnedStack()) {
Bryce Lee48f4b572017-04-10 10:54:15 -07003537 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003538 TaskStack topStack = mChildren.get(topChildPosition);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003539 if (topStack.getWindowingMode() != WINDOWING_MODE_PINNED) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003540 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3541 }
3542
3543 // So, stack is moved just below the pinned stack.
3544 // When we're adding a new stack the target is the current pinned stack position.
3545 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003546 // stack, because WindowContainer#positionAt() first removes element and then adds
3547 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003548 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3549 }
3550
3551 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003552 }
3553
3554 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003555 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3556 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003557 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003558 if (super.forAllWindows(callback, traverseTopToBottom)) {
3559 return true;
3560 }
3561 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3562 return true;
3563 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003564 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003565 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3566 return true;
3567 }
3568 if (super.forAllWindows(callback, traverseTopToBottom)) {
3569 return true;
3570 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003571 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003572 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003573 }
3574
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003575 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003576 boolean traverseTopToBottom) {
3577 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3578 // app tokens.
3579 // TODO: Investigate if we need to continue to do this or if we can just process them
3580 // in-order.
3581 if (traverseTopToBottom) {
3582 for (int i = mChildren.size() - 1; i >= 0; --i) {
3583 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3584 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003585 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3586 traverseTopToBottom)) {
3587 return true;
3588 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003589 }
3590 }
3591 } else {
3592 final int count = mChildren.size();
3593 for (int i = 0; i < count; ++i) {
3594 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3595 final int appTokensCount = appTokens.size();
3596 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003597 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3598 traverseTopToBottom)) {
3599 return true;
3600 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003601 }
3602 }
3603 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003604 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003605 }
3606
Wale Ogunwale1666e312016-12-16 11:27:18 -08003607 void setExitingTokensHasVisible(boolean hasVisible) {
3608 for (int i = mChildren.size() - 1; i >= 0; --i) {
3609 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3610 for (int j = appTokens.size() - 1; j >= 0; --j) {
3611 appTokens.get(j).hasVisible = hasVisible;
3612 }
3613 }
3614 }
3615
3616 void removeExistingAppTokensIfPossible() {
3617 for (int i = mChildren.size() - 1; i >= 0; --i) {
3618 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3619 for (int j = appTokens.size() - 1; j >= 0; --j) {
3620 final AppWindowToken token = appTokens.get(j);
3621 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3622 && (!token.mIsExiting || token.isEmpty())) {
3623 // Make sure there is no animation running on this token, so any windows
3624 // associated with it will be removed as soon as their animations are
3625 // complete.
3626 token.mAppAnimator.clearAnimation();
3627 token.mAppAnimator.animating = false;
3628 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3629 "performLayout: App token exiting now removed" + token);
3630 token.removeIfPossible();
3631 }
3632 }
3633 }
3634 }
3635
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003636 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003637 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003638 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3639 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003640 // Apps and their containers are not allowed to specify an orientation while the
3641 // docked or freeform stack is visible...except for the home stack/task if the
3642 // docked stack is minimized and it actually set something.
3643 if (mHomeStack != null && mHomeStack.isVisible()
3644 && mDividerControllerLocked.isMinimizedDock()) {
3645 final int orientation = mHomeStack.getOrientation();
3646 if (orientation != SCREEN_ORIENTATION_UNSET) {
3647 return orientation;
3648 }
3649 }
3650 return SCREEN_ORIENTATION_UNSPECIFIED;
3651 }
3652
3653 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003654 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3655 PackageManager.FEATURE_AUTOMOTIVE);
3656 if (isCar) {
3657 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3658 // allow anything but the default orientation.
3659 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3660 "Forcing UNSPECIFIED orientation in car. Ignoring " + orientation);
3661 return SCREEN_ORIENTATION_UNSPECIFIED;
3662 }
3663
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003664 if (orientation != SCREEN_ORIENTATION_UNSET
3665 && orientation != SCREEN_ORIENTATION_BEHIND) {
3666 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3667 "App is requesting an orientation, return " + orientation);
3668 return orientation;
3669 }
3670
3671 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003672 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003673 // The next app has not been requested to be visible, so we keep the current orientation
3674 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003675 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003676 }
Robert Carrb1579c82017-09-05 14:54:47 -07003677
3678 @Override
3679 void assignChildLayers(SurfaceControl.Transaction t) {
3680 final int NORMAL_STACK_STATE = 0;
3681 final int BOOSTED_STATE = 1;
3682 final int ALWAYS_ON_TOP_STATE = 2;
3683
3684 // We allow stacks to change visual order from the AM specified order due to
3685 // Z-boosting during animations. However we must take care to ensure TaskStacks
3686 // which are marked as alwaysOnTop remain that way.
3687 int layer = 0;
3688 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
3689 for (int i = 0; i < mChildren.size(); i++) {
3690 final TaskStack s = mChildren.get(i);
3691 layer++;
3692 if (state == NORMAL_STACK_STATE) {
3693 s.assignLayer(t, layer);
3694 } else if (state == BOOSTED_STATE && s.needsZBoost()) {
3695 s.assignLayer(t, layer);
3696 } else if (state == ALWAYS_ON_TOP_STATE &&
3697 s.isAlwaysOnTop()) {
3698 s.assignLayer(t, layer);
3699 }
3700 s.assignChildLayers(t);
3701 }
3702 // The appropriate place for App-Transitions to occur is right
3703 // above all other animations but still below things in the Picture-and-Picture
3704 // windowing mode.
3705 if (state == BOOSTED_STATE && mAnimationLayer != null) {
3706 t.setLayer(mAnimationLayer, layer + 1);
3707 }
3708 }
3709 }
3710
3711 @Override
3712 void onParentSet() {
3713 super.onParentSet();
3714 if (getParent() != null) {
3715 mAnimationLayer = makeSurface().build();
3716 } else {
3717 mAnimationLayer.destroy();
3718 mAnimationLayer = null;
3719 }
3720 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003721 }
3722
3723 /**
3724 * Window container class that contains all containers on this display that are not related to
3725 * Apps. E.g. status bar.
3726 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003727 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3728 /**
3729 * Compares two child window tokens returns -1 if the first is lesser than the second in
3730 * terms of z-order and 1 otherwise.
3731 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003732 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003733 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003734 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3735 token1.mOwnerCanManageAppTokens)
3736 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3737 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003738
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003739 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3740 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3741 return false;
3742 }
3743 final int req = w.mAttrs.screenOrientation;
3744 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3745 || req == SCREEN_ORIENTATION_UNSET) {
3746 return false;
3747 }
3748 return true;
3749 };
3750
Wale Ogunwale3a931692016-11-02 16:49:48 -07003751 private final String mName;
3752 NonAppWindowContainers(String name) {
3753 mName = name;
3754 }
3755
3756 void addChild(WindowToken token) {
3757 addChild(token, mWindowComparator);
3758 }
3759
3760 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003761 int getOrientation() {
3762 final WindowManagerPolicy policy = mService.mPolicy;
3763 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003764 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003765
3766 if (win != null) {
3767 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003768 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003769 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003770 if (mService.mKeyguardGoingAway) {
3771 // Keyguard can't affect the orientation if it is going away...
3772 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3773 return SCREEN_ORIENTATION_UNSET;
3774 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003775 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003776 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003777 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003778 }
3779
Andrii Kulian8ee72852017-03-10 10:36:45 -08003780 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003781
Jorim Jaggi75520d52017-08-24 17:23:19 +02003782 if (policy.isKeyguardShowingAndNotOccluded()
3783 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003784 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003785 }
3786
3787 return SCREEN_ORIENTATION_UNSET;
3788 }
3789
3790 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003791 String getName() {
3792 return mName;
3793 }
Robert Carr3b716242016-08-16 16:02:21 -07003794 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003795
3796 /**
3797 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3798 */
3799 @FunctionalInterface
3800 private interface Screenshoter<E> {
3801 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3802 boolean useIdentityTransform, int rotation);
3803 }
Robert Carrb1579c82017-09-05 14:54:47 -07003804
3805 SurfaceControl.Builder makeSurface(SurfaceSession s) {
3806 return mService.makeSurfaceBuilder(s)
3807 .setParent(mWindowingLayer);
3808 }
3809
3810 @Override
3811 SurfaceSession getSession() {
3812 return mSession;
3813 }
3814
3815 @Override
3816 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003817 SurfaceSession s = child != null ? child.getSession() : getSession();
3818 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07003819 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07003820
3821 if (child == null) {
3822 return b;
3823 }
3824
3825 b.setName(child.getName());
Robert Carrb1579c82017-09-05 14:54:47 -07003826 if (child.isScreenOverlay()) {
3827 return b.setParent(mOverlayLayer);
3828 } else {
3829 return b.setParent(mWindowingLayer);
3830 }
3831 }
3832
3833 /**
3834 * The makeSurface variants are for use by the window-container
3835 * hierarchy. makeOverlay here is a function for various non windowing
3836 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
3837 * and other potpourii.
3838 */
3839 SurfaceControl.Builder makeOverlay() {
3840 return mService.makeSurfaceBuilder(mSession)
3841 .setParent(mOverlayLayer);
3842 }
3843
3844 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003845 applyMagnificationSpec(getPendingTransaction(), spec);
3846 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07003847 }
3848
3849 @Override
3850 void onParentSet() {
3851 // Since we are the top of the SurfaceControl hierarchy here
3852 // we create the root surfaces explicitly rather than chaining
3853 // up as the default implementation in onParentSet does. So we
3854 // explicitly do NOT call super here.
3855 }
3856
3857 @Override
3858 void assignChildLayers(SurfaceControl.Transaction t) {
3859 t.setLayer(mOverlayLayer, 1)
3860 .setLayer(mWindowingLayer, 0);
3861
3862 // These are layers as children of "mWindowingLayer"
3863 mBelowAppWindowsContainers.assignLayer(t, 0);
3864 mTaskStackContainers.assignLayer(t, 1);
3865 mAboveAppWindowsContainers.assignLayer(t, 2);
3866
3867 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carr1cafdf82017-11-22 13:56:43 -08003868
3869 // A brief summary of IME layer assignment:
3870 //
3871 // In case we have no IME target but we have an IME we are typically in
3872 // a transition state and keeping the IME on top of everything except overlays
3873 // seems to work best.
3874 //
3875 // In split-screen windowing mode we can't layer the
3876 // IME relative to the IME target because it needs to
3877 // go over the docked divider, so instead we place it on top
3878 // of everything and use relative layering of windows which need
3879 // to go above it (see special logic in WindowState#assignLayer)
3880 //
3881 // There is a third case, where the IME target has no SurfaceControl, for
3882 // example if Layer assignment were triggered due to removal of the
3883 // IME target while it was still the IME target.
3884 if (imeTarget == null ||
3885 imeTarget.inSplitScreenWindowingMode() ||
3886 imeTarget.getSurfaceControl() == null) {
Robert Carrb1579c82017-09-05 14:54:47 -07003887 mImeWindowsContainers.assignLayer(t, 3);
3888 } else {
3889 t.setRelativeLayer(mImeWindowsContainers.getSurfaceControl(),
3890 imeTarget.getSurfaceControl(),
3891 // TODO: We need to use an extra level on the app surface to ensure
3892 // this is always above SurfaceView but always below attached window.
3893 1);
3894 }
3895
3896 // Above we have assigned layers to our children, now we ask them to assign
3897 // layers to their children.
3898 mBelowAppWindowsContainers.assignChildLayers(t);
3899 mTaskStackContainers.assignChildLayers(t);
3900 mAboveAppWindowsContainers.assignChildLayers(t);
3901 mImeWindowsContainers.assignChildLayers(t);
3902 }
3903
3904 /**
3905 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
3906 * that the IME target is one of the docked applications. We'd like the docked divider to be
3907 * above both of the applications, and we'd like the IME to be above the docked divider.
3908 * However we need child windows of the applications to be above the IME (Text drag handles).
3909 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
3910 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
3911 * with {@link #WindowState#assignLayer}
3912 */
3913 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
3914 t.setRelativeLayer(child.getSurfaceControl(), mImeWindowsContainers.getSurfaceControl(), 1);
3915 }
3916
3917 @Override
3918 void destroyAfterPendingTransaction(SurfaceControl surface) {
3919 mPendingDestroyingSurfaces.add(surface);
3920 }
3921
3922 /**
3923 * Destroys any surfaces that have been put into the pending list with
3924 * {@link #destroyAfterTransaction}.
3925 */
3926 void onPendingTransactionApplied() {
3927 for (int i = mPendingDestroyingSurfaces.size() - 1; i >= 0; i--) {
3928 mPendingDestroyingSurfaces.get(i).destroy();
3929 }
3930 mPendingDestroyingSurfaces.clear();
3931 }
Craig Mautner59c00972012-07-30 12:10:24 -07003932}