blob: 9494ade74b2848729e54221795b8af3c821566ca [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
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800326 DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700327
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800328 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
329
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000330 private boolean mHaveBootMsg = false;
331 private boolean mHaveApp = false;
332 private boolean mHaveWallpaper = false;
333 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700334
335 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
336
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700337 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
338 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000339 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
340 new ApplySurfaceChangesTransactionState();
341 private final ScreenshotApplicationState mScreenshotApplicationState =
342 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700343
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700344 // True if this display is in the process of being removed. Used to determine if the removal of
345 // the display's direct children should be allowed.
346 private boolean mRemovingDisplay = false;
347
Bryce Leed1871262017-06-12 14:12:29 -0700348 // {@code false} if this display is in the processing of being created.
349 private boolean mDisplayReady = false;
350
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800351 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700352 int mInputMethodAnimLayerAdjustment;
353
Robert Carrb1579c82017-09-05 14:54:47 -0700354 private final SurfaceSession mSession = new SurfaceSession();
355
356 /**
357 * We organize all top-level Surfaces in to the following layers.
358 * mOverlayLayer contains a few Surfaces which are always on top of others
359 * and omitted from Screen-Magnification ({@link WindowState#isScreenOverlay})
360 * {@link #mWindowingLayer} contains everything else.
361 */
362 private SurfaceControl mOverlayLayer;
363
364 /**
365 * See {@link #mOverlayLayer}
366 */
367 private SurfaceControl mWindowingLayer;
368
369 /**
370 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
371 * <p>
372 * For these surfaces currently we use a surface based on the larger of width or height so we
373 * don't have to resize when rotating the display.
374 */
375 private int mSurfaceSize;
376
377 /**
378 * A list of surfaces to be destroyed after {@link #mPendingTransaction} is applied.
379 */
380 private final ArrayList<SurfaceControl> mPendingDestroyingSurfaces = new ArrayList<>();
381
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800382 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
383 WindowStateAnimator winAnimator = w.mWinAnimator;
384 if (winAnimator.hasSurface()) {
385 final boolean wasAnimating = winAnimator.mWasAnimating;
386 final boolean nowAnimating = winAnimator.stepAnimationLocked(
387 mTmpWindowAnimator.mCurrentTime);
388 winAnimator.mWasAnimating = nowAnimating;
389 mTmpWindowAnimator.orAnimating(nowAnimating);
390
391 if (DEBUG_WALLPAPER) Slog.v(TAG,
392 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
393
394 if (wasAnimating && !winAnimator.mAnimating
395 && mWallpaperController.isWallpaperTarget(w)) {
396 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
397 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
398 if (DEBUG_LAYOUT_REPEATS) {
399 mService.mWindowPlacerLocked.debugLayoutRepeats(
400 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
401 }
402 }
403 }
404
405 final AppWindowToken atoken = w.mAppToken;
406 if (winAnimator.mDrawState == READY_TO_SHOW) {
407 if (atoken == null || atoken.allDrawn) {
408 if (w.performShowLocked()) {
409 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
410 if (DEBUG_LAYOUT_REPEATS) {
411 mService.mWindowPlacerLocked.debugLayoutRepeats(
412 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
413 }
414 }
415 }
416 }
417 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
418 if (appAnimator != null && appAnimator.thumbnail != null) {
419 if (appAnimator.thumbnailTransactionSeq
420 != mTmpWindowAnimator.mAnimTransactionSequence) {
421 appAnimator.thumbnailTransactionSeq =
422 mTmpWindowAnimator.mAnimTransactionSequence;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800423 }
424 }
425 };
426
427 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
428 final WindowStateAnimator winAnimator = w.mWinAnimator;
429 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
430 return;
431 }
432
433 final int flags = w.mAttrs.flags;
434
435 // If this window is animating, make a note that we have an animating window and take
436 // care of a request to run a detached wallpaper animation.
437 if (winAnimator.mAnimating) {
438 if (winAnimator.mAnimation != null) {
439 if ((flags & FLAG_SHOW_WALLPAPER) != 0
440 && winAnimator.mAnimation.getDetachWallpaper()) {
441 mTmpWindow = w;
442 }
443 final int color = winAnimator.mAnimation.getBackgroundColor();
444 if (color != 0) {
445 final TaskStack stack = w.getStack();
446 if (stack != null) {
447 stack.setAnimationBackground(winAnimator, color);
448 }
449 }
450 }
451 mTmpWindowAnimator.setAnimating(true);
452 }
453
454 // If this window's app token is running a detached wallpaper animation, make a note so
455 // we can ensure the wallpaper is displayed behind it.
456 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
457 if (appAnimator != null && appAnimator.animation != null
458 && appAnimator.animating) {
459 if ((flags & FLAG_SHOW_WALLPAPER) != 0
460 && appAnimator.animation.getDetachWallpaper()) {
461 mTmpWindow = w;
462 }
463
464 final int color = appAnimator.animation.getBackgroundColor();
465 if (color != 0) {
466 final TaskStack stack = w.getStack();
467 if (stack != null) {
468 stack.setAnimationBackground(winAnimator, color);
469 }
470 }
471 }
472 };
473
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800474 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
475 final int lostFocusUid = mTmpWindow.mOwnerUid;
476 final Handler handler = mService.mH;
477 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
478 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
479 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
480 w.mAttrs.hideTimeoutMilliseconds);
481 }
482 }
483 };
484
485 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
486 final AppWindowToken focusedApp = mService.mFocusedApp;
487 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
488 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
489
490 if (!w.canReceiveKeys()) {
491 return false;
492 }
493
494 final AppWindowToken wtoken = w.mAppToken;
495
496 // If this window's application has been removed, just skip it.
497 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
498 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
499 + (wtoken.removed ? "removed" : "sendingToBottom"));
500 return false;
501 }
502
503 if (focusedApp == null) {
504 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
505 + " using new focus @ " + w);
506 mTmpWindow = w;
507 return true;
508 }
509
510 if (!focusedApp.windowsAreFocusable()) {
511 // Current focused app windows aren't focusable...
512 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
513 + " focusable using new focus @ " + w);
514 mTmpWindow = w;
515 return true;
516 }
517
518 // Descend through all of the app tokens and find the first that either matches
519 // win.mAppToken (return win) or mFocusedApp (return null).
520 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
521 if (focusedApp.compareTo(wtoken) > 0) {
522 // App stack below focused app stack. No focus for you!!!
523 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
524 "findFocusedWindow: Reached focused app=" + focusedApp);
525 mTmpWindow = null;
526 return true;
527 }
528 }
529
530 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
531 mTmpWindow = w;
532 return true;
533 };
534
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800535 private final Consumer<WindowState> mPerformLayout = w -> {
536 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
537 // wasting time and funky changes while a window is animating away.
538 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
539 || w.isGoneForLayoutLw();
540
541 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
542 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
543 + " mLayoutAttached=" + w.mLayoutAttached
544 + " screen changed=" + w.isConfigChanged());
545 final AppWindowToken atoken = w.mAppToken;
546 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
547 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
548 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
549 + " parentHidden=" + w.isParentWindowHidden());
550 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
551 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
552 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
553 + " parentHidden=" + w.isParentWindowHidden());
554 }
555
556 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
557 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
558 // since that means "perform layout as normal, just don't display").
559 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
560 || ((w.isConfigChanged() || w.setReportResizeHints())
561 && !w.isGoneForLayoutLw() &&
562 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
563 (w.mHasSurface && w.mAppToken != null &&
564 w.mAppToken.layoutConfigChanges)))) {
565 if (!w.mLayoutAttached) {
566 if (mTmpInitial) {
567 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
568 w.mContentChanged = false;
569 }
570 if (w.mAttrs.type == TYPE_DREAM) {
571 // Don't layout windows behind a dream, so that if it does stuff like hide
572 // the status bar we won't get a bad transition when it goes away.
573 mTmpWindow = w;
574 }
575 w.mLayoutNeeded = false;
576 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200577 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000578 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800579 w.mLayoutSeq = mService.mLayoutSeq;
580
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200581 // If this is the first layout, we need to initialize the last inset values as
582 // otherwise we'd immediately cause an unnecessary resize.
583 if (firstLayout) {
584 w.updateLastInsetValues();
585 }
586
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800587 // Window frames may have changed. Update dim layer with the new bounds.
588 final Task task = w.getTask();
589 if (task != null) {
590 mDimLayerController.updateDimLayer(task);
591 }
592
593 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
594 + " mContainingFrame=" + w.mContainingFrame
595 + " mDisplayFrame=" + w.mDisplayFrame);
596 }
597 }
598 };
599
600 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
601 if (w.mLayoutAttached) {
602 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
603 + " mViewVisibility=" + w.mViewVisibility
604 + " mRelayoutCalled=" + w.mRelayoutCalled);
605 // If this view is GONE, then skip it -- keep the current frame, and let the caller
606 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
607 // windows, since that means "perform layout as normal, just don't display").
608 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
609 return;
610 }
611 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
612 || w.mLayoutNeeded) {
613 if (mTmpInitial) {
614 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
615 w.mContentChanged = false;
616 }
617 w.mLayoutNeeded = false;
618 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000619 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800620 w.mLayoutSeq = mService.mLayoutSeq;
621 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
622 + " mContainingFrame=" + w.mContainingFrame
623 + " mDisplayFrame=" + w.mDisplayFrame);
624 }
625 } else if (w.mAttrs.type == TYPE_DREAM) {
626 // Don't layout windows behind a dream, so that if it does stuff like hide the
627 // status bar we won't get a bad transition when it goes away.
628 mTmpWindow = mTmpWindow2;
629 }
630 };
631
632 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
633 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
634 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
635 return w.canBeImeTarget();
636 };
637
638 private final Consumer<WindowState> mApplyPostLayoutPolicy =
639 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
640 mService.mInputMethodTarget);
641
642 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
643 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
644 final boolean obscuredChanged = w.mObscured !=
645 mTmpApplySurfaceChangesTransactionState.obscured;
646 final RootWindowContainer root = mService.mRoot;
647 // Only used if default window
648 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
649
650 // Update effect.
651 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
652 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
653 final boolean isDisplayed = w.isDisplayedLw();
654
655 if (isDisplayed && w.isObscuringDisplay()) {
656 // This window completely covers everything behind it, so we want to leave all
657 // of them as undimmed (for performance reasons).
658 root.mObscuringWindow = w;
659 mTmpApplySurfaceChangesTransactionState.obscured = true;
660 }
661
662 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
663 root.handleNotObscuredLocked(w,
664 mTmpApplySurfaceChangesTransactionState.obscured,
665 mTmpApplySurfaceChangesTransactionState.syswin);
666
667 if (w.mHasSurface && isDisplayed) {
668 final int type = w.mAttrs.type;
669 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
670 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
671 mTmpApplySurfaceChangesTransactionState.syswin = true;
672 }
673 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
674 && w.mAttrs.preferredRefreshRate != 0) {
675 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
676 = w.mAttrs.preferredRefreshRate;
677 }
678 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
679 && w.mAttrs.preferredDisplayModeId != 0) {
680 mTmpApplySurfaceChangesTransactionState.preferredModeId
681 = w.mAttrs.preferredDisplayModeId;
682 }
683 }
684 }
685
686 w.applyDimLayerIfNeeded();
687
688 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
689 && mWallpaperController.isWallpaperTarget(w)) {
690 // This is the wallpaper target and its obscured state changed... make sure the
691 // current wallpaper's visibility has been updated accordingly.
692 mWallpaperController.updateWallpaperVisibility();
693 }
694
695 w.handleWindowMovedIfNeeded();
696
697 final WindowStateAnimator winAnimator = w.mWinAnimator;
698
699 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
700 w.mContentChanged = false;
701
702 // Moved from updateWindowsAndWallpaperLocked().
703 if (w.mHasSurface) {
704 // Take care of the window being ready to display.
705 final boolean committed = winAnimator.commitFinishDrawingLocked();
706 if (isDefaultDisplay && committed) {
707 if (w.mAttrs.type == TYPE_DREAM) {
708 // HACK: When a dream is shown, it may at that point hide the lock screen.
709 // So we need to redo the layout to let the phone window manager make this
710 // happen.
711 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
712 if (DEBUG_LAYOUT_REPEATS) {
713 surfacePlacer.debugLayoutRepeats(
714 "dream and commitFinishDrawingLocked true",
715 pendingLayoutChanges);
716 }
717 }
718 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
719 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
720 "First draw done in potential wallpaper target " + w);
721 root.mWallpaperMayChange = true;
722 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
723 if (DEBUG_LAYOUT_REPEATS) {
724 surfacePlacer.debugLayoutRepeats(
725 "wallpaper and commitFinishDrawingLocked true",
726 pendingLayoutChanges);
727 }
728 }
729 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700730 final TaskStack stack = w.getStack();
731 if ((!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening())
732 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800733 // Updates the shown frame before we set up the surface. This is needed
734 // because the resizing could change the top-left position (in addition to
735 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
736 // position the surface.
737 //
738 // If an animation is being started, we can't call this method because the
739 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700740 // we do want to update the position if the window is animating. We make an exception
741 // for the bounds animating state, where an application may have been waiting
742 // for an exit animation to start, but instead enters PiP. We need to ensure
743 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800744 winAnimator.computeShownFrameLocked();
745 }
746 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
747 }
748
749 final AppWindowToken atoken = w.mAppToken;
750 if (atoken != null) {
751 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
752 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
753 mTmpUpdateAllDrawn.add(atoken);
754 }
755 }
756
757 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
758 && w.isDisplayedLw()) {
759 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
760 }
761
762 w.updateResizingWindowIfNeeded();
763 };
764
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800765 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800766 * Create new {@link DisplayContent} instance, add itself to the root window container and
767 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700768 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800769 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700770 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
771 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700772 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700773 DisplayContent(Display display, WindowManagerService service,
Robert Carrb1579c82017-09-05 14:54:47 -0700774 WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800775 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
776 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
777 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
778 + " new=" + display);
779 }
780
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700781 mDisplay = display;
782 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700783 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700784 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700785 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700786 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700787 mService = service;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000788 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700789 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800790 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700791 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800792 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700793
Robert Carrb1579c82017-09-05 14:54:47 -0700794 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth);
795
796 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
797 .setSize(mSurfaceSize, mSurfaceSize)
798 .setOpaque(true);
799 mWindowingLayer = b.setName("Display Root").build();
800 mOverlayLayer = b.setName("Display Overlays").build();
801
802 mPendingTransaction.setLayer(mWindowingLayer, 0)
803 .setLayerStack(mWindowingLayer, mDisplayId)
804 .show(mWindowingLayer)
805 .setLayer(mOverlayLayer, 1)
806 .setLayerStack(mOverlayLayer, mDisplayId)
807 .show(mOverlayLayer);
808 mPendingTransaction.apply();
809
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700810 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700811 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700812 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700813 super.addChild(mAboveAppWindowsContainers, null);
814 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800815
816 // Add itself as a child to the root container.
817 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700818
819 // TODO(b/62541591): evaluate whether this is the best spot to declare the
820 // {@link DisplayContent} ready for use.
821 mDisplayReady = true;
822 }
823
824 boolean isReady() {
825 // The display is ready when the system and the individual display are both ready.
826 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700827 }
828
829 int getDisplayId() {
830 return mDisplayId;
831 }
832
Wale Ogunwale02319a62016-09-26 15:21:22 -0700833 WindowToken getWindowToken(IBinder binder) {
834 return mTokenMap.get(binder);
835 }
836
837 AppWindowToken getAppWindowToken(IBinder binder) {
838 final WindowToken token = getWindowToken(binder);
839 if (token == null) {
840 return null;
841 }
842 return token.asAppWindowToken();
843 }
844
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700845 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700846 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
847 if (dc != null) {
848 // We currently don't support adding a window token to the display if the display
849 // already has the binder mapped to another token. If there is a use case for supporting
850 // this moving forward we will either need to merge the WindowTokens some how or have
851 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700852 throw new IllegalArgumentException("Can't map token=" + token + " to display="
853 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700854 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700855 if (binder == null) {
856 throw new IllegalArgumentException("Can't map token=" + token + " to display="
857 + getName() + " binder is null");
858 }
859 if (token == null) {
860 throw new IllegalArgumentException("Can't map null token to display="
861 + getName() + " binder=" + binder);
862 }
863
Wale Ogunwale02319a62016-09-26 15:21:22 -0700864 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700865
866 if (token.asAppWindowToken() == null) {
867 // Add non-app token to container hierarchy on the display. App tokens are added through
868 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700869 switch (token.windowType) {
870 case TYPE_WALLPAPER:
871 mBelowAppWindowsContainers.addChild(token);
872 break;
873 case TYPE_INPUT_METHOD:
874 case TYPE_INPUT_METHOD_DIALOG:
875 mImeWindowsContainers.addChild(token);
876 break;
877 default:
878 mAboveAppWindowsContainers.addChild(token);
879 break;
880 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700881 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700882 }
883
884 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700885 final WindowToken token = mTokenMap.remove(binder);
886 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800887 token.setExiting();
888 }
889 return token;
890 }
891
892 /** Changes the display the input window token is housed on to this one. */
893 void reParentWindowToken(WindowToken token) {
894 final DisplayContent prevDc = token.getDisplayContent();
895 if (prevDc == this) {
896 return;
897 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800898 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
899 && token.asAppWindowToken() == null) {
900 // Removed the token from the map, but made sure it's not an app token before removing
901 // from parent.
902 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700903 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800904
905 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700906 }
907
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700908 void removeAppToken(IBinder binder) {
909 final WindowToken token = removeWindowToken(binder);
910 if (token == null) {
911 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
912 return;
913 }
914
915 final AppWindowToken appToken = token.asAppWindowToken();
916
917 if (appToken == null) {
918 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
919 return;
920 }
921
922 appToken.onRemovedFromDisplay();
923 }
924
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700925 Display getDisplay() {
926 return mDisplay;
927 }
928
Craig Mautner59c00972012-07-30 12:10:24 -0700929 DisplayInfo getDisplayInfo() {
930 return mDisplayInfo;
931 }
932
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700933 DisplayMetrics getDisplayMetrics() {
934 return mDisplayMetrics;
935 }
936
Andrii Kulian8ee72852017-03-10 10:36:45 -0800937 int getRotation() {
938 return mRotation;
939 }
940
941 void setRotation(int newRotation) {
942 mRotation = newRotation;
943 }
944
945 int getLastOrientation() {
946 return mLastOrientation;
947 }
948
949 void setLastOrientation(int orientation) {
950 mLastOrientation = orientation;
951 }
952
953 boolean getAltOrientation() {
954 return mAltOrientation;
955 }
956
957 void setAltOrientation(boolean altOrientation) {
958 mAltOrientation = altOrientation;
959 }
960
961 int getLastWindowForcedOrientation() {
962 return mLastWindowForcedOrientation;
963 }
964
Andrii Kulian06d07d62017-03-14 11:11:47 -0700965 /**
966 * Update rotation of the display.
967 *
968 * Returns true if the rotation has been changed. In this case YOU MUST CALL
969 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
970 */
971 boolean updateRotationUnchecked(boolean inTransaction) {
972 if (mService.mDeferredRotationPauseCount > 0) {
973 // Rotation updates have been paused temporarily. Defer the update until
974 // updates have been resumed.
975 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
976 return false;
977 }
978
979 ScreenRotationAnimation screenRotationAnimation =
980 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
981 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
982 // Rotation updates cannot be performed while the previous rotation change
983 // animation is still in progress. Skip this update. We will try updating
984 // again after the animation is finished and the display is unfrozen.
985 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
986 return false;
987 }
988 if (mService.mDisplayFrozen) {
989 // Even if the screen rotation animation has finished (e.g. isAnimating
990 // returns false), there is still some time where we haven't yet unfrozen
991 // the display. We also need to abort rotation here.
992 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
993 "Deferring rotation, still finishing previous rotation");
994 return false;
995 }
996
997 if (!mService.mDisplayEnabled) {
998 // No point choosing a rotation if the display is not enabled.
999 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
1000 return false;
1001 }
1002
1003 final int oldRotation = mRotation;
1004 final int lastOrientation = mLastOrientation;
1005 final boolean oldAltOrientation = mAltOrientation;
1006 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr0e007272017-10-02 13:00:55 -07001007 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
Robert Carr897215d2017-03-29 12:25:50 -07001008 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001009
Robert Carr0e007272017-10-02 13:00:55 -07001010 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001011 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
1012 if (seamlessRotated != null) {
1013 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1014 // to complete (that is, waiting for windows to redraw). It's tempting to check
1015 // w.mSeamlessRotationCount but that could be incorrect in the case of
1016 // window-removal.
1017 return false;
1018 }
Robert Carr0e007272017-10-02 13:00:55 -07001019
1020 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001021 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001022 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001023 mayRotateSeamlessly = false;
1024 }
1025 for (int i = 0; i < mService.mSessions.size(); i++) {
1026 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1027 mayRotateSeamlessly = false;
1028 break;
1029 }
1030 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001031 }
Robert Carr0e007272017-10-02 13:00:55 -07001032 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001033
1034 // TODO: Implement forced rotation changes.
1035 // Set mAltOrientation to indicate that the application is receiving
1036 // an orientation that has different metrics than it expected.
1037 // eg. Portrait instead of Landscape.
1038
1039 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
1040 lastOrientation, rotation);
1041
1042 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
1043 + ", got rotation " + rotation + " which has "
1044 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1045
1046 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1047 // No change.
1048 return false;
1049 }
1050
1051 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
1052 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1053 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1054
1055 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1056 mService.mWaitingForConfig = true;
1057 }
1058
1059 mRotation = rotation;
1060 mAltOrientation = altOrientation;
1061 if (isDefaultDisplay) {
1062 mService.mPolicy.setRotationLw(rotation);
1063 }
1064
1065 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1066 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1067 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1068 WINDOW_FREEZE_TIMEOUT_DURATION);
1069
1070 setLayoutNeeded();
1071 final int[] anim = new int[2];
1072 if (isDimming()) {
1073 anim[0] = anim[1] = 0;
1074 } else {
1075 mService.mPolicy.selectRotationAnimationLw(anim);
1076 }
1077
1078 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001079 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001080 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1081 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1082 mDisplayId);
1083 } else {
1084 // The screen rotation animation uses a screenshot to freeze the screen
1085 // while windows resize underneath.
1086 // When we are rotating seamlessly, we allow the elements to transition
1087 // to their rotated state independently and without a freeze required.
1088 screenRotationAnimation = null;
1089
1090 // We have to reset this in case a window was removed before it
1091 // finished seamless rotation.
1092 mService.mSeamlessRotationCount = 0;
1093 }
1094
1095 // We need to update our screen size information to match the new rotation. If the rotation
1096 // has actually changed then this method will return true and, according to the comment at
1097 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1098 // By updating the Display info here it will be available to
1099 // #computeScreenConfiguration() later.
1100 updateDisplayAndOrientation(getConfiguration().uiMode);
1101
1102 if (!inTransaction) {
1103 if (SHOW_TRANSACTIONS) {
1104 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1105 }
1106 mService.openSurfaceTransaction();
1107 }
1108 try {
1109 // NOTE: We disable the rotation in the emulator because
1110 // it doesn't support hardware OpenGL emulation yet.
1111 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1112 && screenRotationAnimation.hasScreenshot()) {
Robert Carrb1579c82017-09-05 14:54:47 -07001113 if (screenRotationAnimation.setRotationInTransaction(rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001114 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1115 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1116 mService.scheduleAnimationLocked();
1117 }
1118 }
1119
1120 if (rotateSeamlessly) {
1121 forAllWindows(w -> {
1122 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1123 }, true /* traverseTopToBottom */);
1124 }
1125
1126 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1127 } finally {
1128 if (!inTransaction) {
Adrian Roos111aff92017-09-27 18:11:46 +02001129 mService.closeSurfaceTransaction("setRotationUnchecked");
Andrii Kulian06d07d62017-03-14 11:11:47 -07001130 if (SHOW_LIGHT_TRANSACTIONS) {
1131 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1132 }
1133 }
1134 }
1135
1136 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001137 if (w.mHasSurface && !rotateSeamlessly) {
1138 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001139 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001140 mService.mRoot.mOrientationChangeComplete = false;
1141 w.mLastFreezeDuration = 0;
1142 }
1143 w.mReportOrientationChanged = true;
1144 }, true /* traverseTopToBottom */);
1145
1146 if (rotateSeamlessly) {
1147 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1148 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1149 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1150 }
1151
1152 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1153 final WindowManagerService.RotationWatcher rotationWatcher
1154 = mService.mRotationWatchers.get(i);
1155 if (rotationWatcher.mDisplayId == mDisplayId) {
1156 try {
1157 rotationWatcher.mWatcher.onRotationChanged(rotation);
1158 } catch (RemoteException e) {
1159 // Ignore
1160 }
1161 }
1162 }
1163
1164 // TODO (multi-display): Magnification is supported only for the default display.
1165 // Announce rotation only if we will not animate as we already have the
1166 // windows in final state. Otherwise, we make this call at the rotation end.
1167 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1168 && isDefaultDisplay) {
1169 mService.mAccessibilityController.onRotationChangedLocked(this);
1170 }
1171
1172 return true;
1173 }
1174
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001175 void configureDisplayPolicy() {
1176 mService.mPolicy.setInitialDisplaySize(getDisplay(),
1177 mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1178
1179 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
1180 }
1181
Andrii Kulian06d07d62017-03-14 11:11:47 -07001182 /**
1183 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1184 * changed.
1185 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1186 */
1187 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1188 // Use the effective "visual" dimensions based on current rotation
1189 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1190 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1191 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1192 int dw = realdw;
1193 int dh = realdh;
1194
1195 if (mAltOrientation) {
1196 if (realdw > realdh) {
1197 // Turn landscape into portrait.
1198 int maxw = (int)(realdh/1.3f);
1199 if (maxw < realdw) {
1200 dw = maxw;
1201 }
1202 } else {
1203 // Turn portrait into landscape.
1204 int maxh = (int)(realdw/1.3f);
1205 if (maxh < realdh) {
1206 dh = maxh;
1207 }
1208 }
1209 }
1210
1211 // Update application display metrics.
1212 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1213 mDisplayId);
1214 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1215 mDisplayId);
1216 mDisplayInfo.rotation = mRotation;
1217 mDisplayInfo.logicalWidth = dw;
1218 mDisplayInfo.logicalHeight = dh;
1219 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1220 mDisplayInfo.appWidth = appWidth;
1221 mDisplayInfo.appHeight = appHeight;
1222 if (isDefaultDisplay) {
1223 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1224 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1225 }
1226 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1227 if (mDisplayScalingDisabled) {
1228 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1229 } else {
1230 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1231 }
1232
1233 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1234 mDisplayInfo);
1235
1236 mBaseDisplayRect.set(0, 0, dw, dh);
1237
1238 if (isDefaultDisplay) {
1239 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1240 mCompatDisplayMetrics);
1241 }
1242 return mDisplayInfo;
1243 }
1244
1245 /**
1246 * Compute display configuration based on display properties and policy settings.
1247 * Do not call if mDisplayReady == false.
1248 */
1249 void computeScreenConfiguration(Configuration config) {
1250 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1251
1252 final int dw = displayInfo.logicalWidth;
1253 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001254 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1255 // TODO: Probably best to set this based on some setting in the display content object,
1256 // so the display can be configured for things like fullscreen.
1257 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001258
Andrii Kulian06d07d62017-03-14 11:11:47 -07001259 config.screenWidthDp =
1260 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1261 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1262 config.screenHeightDp =
1263 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1264 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001265
1266 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1267 final int leftInset = mTmpRect.left;
1268 final int topInset = mTmpRect.top;
1269 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001270 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1271 leftInset + displayInfo.appWidth /* right */,
1272 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001273 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1274 || displayInfo.rotation == Surface.ROTATION_270);
1275
1276 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1277 mDisplayMetrics.density, config);
1278
1279 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1280 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1281 ? Configuration.SCREENLAYOUT_ROUND_YES
1282 : Configuration.SCREENLAYOUT_ROUND_NO);
1283
1284 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1285 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1286 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1287 dh, mDisplayId);
1288 config.densityDpi = displayInfo.logicalDensityDpi;
1289
1290 config.colorMode =
1291 (displayInfo.isHdr()
1292 ? Configuration.COLOR_MODE_HDR_YES
1293 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001294 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001295 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1296 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1297
1298 // Update the configuration based on available input devices, lid switch,
1299 // and platform configuration.
1300 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1301 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1302 config.navigation = Configuration.NAVIGATION_NONAV;
1303
1304 int keyboardPresence = 0;
1305 int navigationPresence = 0;
1306 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1307 final int len = devices != null ? devices.length : 0;
1308 for (int i = 0; i < len; i++) {
1309 InputDevice device = devices[i];
1310 if (!device.isVirtual()) {
1311 final int sources = device.getSources();
1312 final int presenceFlag = device.isExternal() ?
1313 WindowManagerPolicy.PRESENCE_EXTERNAL :
1314 WindowManagerPolicy.PRESENCE_INTERNAL;
1315
1316 // TODO(multi-display): Configure on per-display basis.
1317 if (mService.mIsTouchDevice) {
1318 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1319 InputDevice.SOURCE_TOUCHSCREEN) {
1320 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1321 }
1322 } else {
1323 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1324 }
1325
1326 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1327 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1328 navigationPresence |= presenceFlag;
1329 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1330 && config.navigation == Configuration.NAVIGATION_NONAV) {
1331 config.navigation = Configuration.NAVIGATION_DPAD;
1332 navigationPresence |= presenceFlag;
1333 }
1334
1335 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1336 config.keyboard = Configuration.KEYBOARD_QWERTY;
1337 keyboardPresence |= presenceFlag;
1338 }
1339 }
1340 }
1341
1342 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1343 config.navigation = Configuration.NAVIGATION_DPAD;
1344 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1345 }
1346
1347 // Determine whether a hard keyboard is available and enabled.
1348 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1349 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1350 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1351 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1352 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1353 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1354 }
1355
1356 // Let the policy update hidden states.
1357 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1358 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1359 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1360 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1361 }
1362
1363 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1364 int displayId) {
1365 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1366 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1367 final int unrotDw, unrotDh;
1368 if (rotated) {
1369 unrotDw = dh;
1370 unrotDh = dw;
1371 } else {
1372 unrotDw = dw;
1373 unrotDh = dh;
1374 }
1375 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1376 displayId);
1377 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1378 displayId);
1379 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1380 displayId);
1381 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1382 displayId);
1383 return sw;
1384 }
1385
1386 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1387 DisplayMetrics dm, int dw, int dh, int displayId) {
1388 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1389 displayId);
1390 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1391 uiMode, displayId);
1392 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1393 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1394 if (curSize == 0 || size < curSize) {
1395 curSize = size;
1396 }
1397 return curSize;
1398 }
1399
1400 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1401 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1402
1403 // We need to determine the smallest width that will occur under normal
1404 // operation. To this, start with the base screen size and compute the
1405 // width under the different possible rotations. We need to un-rotate
1406 // the current screen dimensions before doing this.
1407 int unrotDw, unrotDh;
1408 if (rotated) {
1409 unrotDw = dh;
1410 unrotDh = dw;
1411 } else {
1412 unrotDw = dw;
1413 unrotDh = dh;
1414 }
1415 displayInfo.smallestNominalAppWidth = 1<<30;
1416 displayInfo.smallestNominalAppHeight = 1<<30;
1417 displayInfo.largestNominalAppWidth = 0;
1418 displayInfo.largestNominalAppHeight = 0;
1419 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1420 unrotDh);
1421 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1422 unrotDw);
1423 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1424 unrotDh);
1425 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1426 unrotDw);
1427 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1428 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1429 displayId);
1430 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1431 displayId);
1432 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1433 displayId);
1434 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1435 displayId);
1436 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1437 outConfig.screenLayout = sl;
1438 }
1439
1440 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1441 int uiMode, int displayId) {
1442 // Get the app screen size at this rotation.
1443 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1444 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1445
1446 // Compute the screen layout size class for this rotation.
1447 int longSize = w;
1448 int shortSize = h;
1449 if (longSize < shortSize) {
1450 int tmp = longSize;
1451 longSize = shortSize;
1452 shortSize = tmp;
1453 }
1454 longSize = (int)(longSize/density);
1455 shortSize = (int)(shortSize/density);
1456 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1457 }
1458
1459 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1460 int uiMode, int dw, int dh) {
1461 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1462 displayId);
1463 if (width < displayInfo.smallestNominalAppWidth) {
1464 displayInfo.smallestNominalAppWidth = width;
1465 }
1466 if (width > displayInfo.largestNominalAppWidth) {
1467 displayInfo.largestNominalAppWidth = width;
1468 }
1469 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1470 displayId);
1471 if (height < displayInfo.smallestNominalAppHeight) {
1472 displayInfo.smallestNominalAppHeight = height;
1473 }
1474 if (height > displayInfo.largestNominalAppHeight) {
1475 displayInfo.largestNominalAppHeight = height;
1476 }
1477 }
1478
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001479 DockedStackDividerController getDockedDividerController() {
1480 return mDividerControllerLocked;
1481 }
1482
Winson Chung655332c2016-10-31 13:14:28 -07001483 PinnedStackController getPinnedStackController() {
1484 return mPinnedStackControllerLocked;
1485 }
1486
Jeff Browna506a6e2013-06-04 00:02:38 -07001487 /**
1488 * Returns true if the specified UID has access to this display.
1489 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001490 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001491 return mDisplay.hasAccess(uid);
1492 }
1493
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001494 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001495 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001496 }
1497
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001498 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001499 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001500 }
1501
Wale Ogunwale61911492017-10-11 08:50:50 -07001502 /**
1503 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1504 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001505 TaskStack getSplitScreenPrimaryStack() {
1506 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001507 return (stack != null && stack.isVisible()) ? stack : null;
1508 }
1509
1510 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001511 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001512 * not visible.
1513 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001514 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1515 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001516 }
1517
1518 TaskStack getPinnedStack() {
1519 return mTaskStackContainers.getPinnedStack();
1520 }
1521
1522 private boolean hasPinnedStack() {
1523 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001524 }
1525
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001526 /**
1527 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1528 * Null is no compatible stack on the display.
1529 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001530 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001531 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1532 }
1533
1534 /**
1535 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1536 * activity type. Null is no compatible stack on the display.
1537 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001538 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001539 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001540 }
1541
Bryce Lee48f4b572017-04-10 10:54:15 -07001542 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001543 TaskStack getTopStack() {
1544 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001545 }
1546
Wale Ogunwale61911492017-10-11 08:50:50 -07001547 void onStackWindowingModeChanged(TaskStack stack) {
1548 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001549 }
1550
Andrii Kulian441e4492016-09-29 15:25:00 -07001551 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001552 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001553 super.onConfigurationChanged(newParentConfig);
1554
Andrii Kulian3a507b52016-09-19 18:14:12 -07001555 // The display size information is heavily dependent on the resources in the current
1556 // configuration, so we need to reconfigure it every time the configuration changes.
1557 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1558 mService.reconfigureDisplayLocked(this);
1559
1560 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001561 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001562 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001563
Andrii Kulian441e4492016-09-29 15:25:00 -07001564 /**
1565 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1566 * bounds were updated.
1567 */
1568 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001569 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1570 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001571 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001572 changedStackList.add(stack.mStackId);
1573 }
1574 }
Winson Chung32c566f2017-04-11 18:31:21 -07001575
1576 // If there was no pinned stack, we still need to notify the controller of the display info
1577 // update as a result of the config change. We do this here to consolidate the flow between
1578 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001579 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001580 mPinnedStackControllerLocked.onDisplayInfoChanged();
1581 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001582 }
1583
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001584 @Override
1585 boolean fillsParent() {
1586 return true;
1587 }
1588
1589 @Override
1590 boolean isVisible() {
1591 return true;
1592 }
1593
1594 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001595 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001596 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001597 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001598 }
1599
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001600 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001601 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1602 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1603 // target, or here in order if there isn't an IME target.
1604 if (traverseTopToBottom) {
1605 for (int i = mChildren.size() - 1; i >= 0; --i) {
1606 final DisplayChildWindowContainer child = mChildren.get(i);
1607 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1608 // In this case the Ime windows will be processed above their target so we skip
1609 // here.
1610 continue;
1611 }
1612 if (child.forAllWindows(callback, traverseTopToBottom)) {
1613 return true;
1614 }
1615 }
1616 } else {
1617 final int count = mChildren.size();
1618 for (int i = 0; i < count; i++) {
1619 final DisplayChildWindowContainer child = mChildren.get(i);
1620 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1621 // In this case the Ime windows will be processed above their target so we skip
1622 // here.
1623 continue;
1624 }
1625 if (child.forAllWindows(callback, traverseTopToBottom)) {
1626 return true;
1627 }
1628 }
1629 }
1630 return false;
1631 }
1632
1633 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1634 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1635 }
1636
Wale Ogunwale399c8692017-05-08 14:22:42 -07001637 @Override
1638 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001639 final WindowManagerPolicy policy = mService.mPolicy;
1640
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001641 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001642 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001643 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001644 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001645 // If the display is frozen, some activities may be in the middle of restarting, and
1646 // thus have removed their old window. If the window has the flag to hide the lock
1647 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1648 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001649 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001650 } else if (policy.isKeyguardLocked()) {
1651 // Use the last orientation the while the display is frozen with the keyguard
1652 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1653 // window. We don't want to check the show when locked window directly though as
1654 // things aren't stable while the display is frozen, for example the window could be
1655 // momentarily unavailable due to activity relaunch.
1656 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001657 + "return " + mLastOrientation);
1658 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001659 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001660 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001661 final int orientation = mAboveAppWindowsContainers.getOrientation();
1662 if (orientation != SCREEN_ORIENTATION_UNSET) {
1663 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001664 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001665 }
1666
Wale Ogunwale399c8692017-05-08 14:22:42 -07001667 // Top system windows are not requesting an orientation. Start searching from apps.
1668 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001669 }
1670
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001671 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001672 // Check if display metrics changed and update base values if needed.
1673 updateBaseDisplayMetricsIfNeeded();
1674
Craig Mautner722285e2012-09-07 13:55:58 -07001675 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001676 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001677
Wale Ogunwale61911492017-10-11 08:50:50 -07001678 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1679 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001680 }
Craig Mautner722285e2012-09-07 13:55:58 -07001681 }
1682
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001683 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001684 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1685 if (displayManagerInternal != null) {
1686 // Bootstrap the default logical display from the display manager.
1687 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1688 if (newDisplayInfo != null) {
1689 mDisplayInfo.copyFrom(newDisplayInfo);
1690 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001691 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001692
Andrii Kuliancd097992017-03-23 18:31:59 -07001693 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1694 mDisplayInfo.logicalDensityDpi);
1695 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1696 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1697 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001698 }
1699
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001700 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001701 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001702 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001703 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001704 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1705 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001706 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001707 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001708 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001709 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001710 out.set(left, top, left + width, top + height);
1711 }
1712
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001713 private void getLogicalDisplayRect(Rect out, int orientation) {
1714 getLogicalDisplayRect(out);
1715
1716 // Rotate the Rect if needed.
1717 final int currentRotation = mDisplayInfo.rotation;
1718 final int rotationDelta = deltaRotation(currentRotation, orientation);
1719 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1720 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1721 mTmpRectF.set(out);
1722 mTmpMatrix.mapRect(mTmpRectF);
1723 mTmpRectF.round(out);
1724 }
1725 }
1726
Andrii Kuliancd097992017-03-23 18:31:59 -07001727 /**
1728 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1729 * values.
1730 */
1731 private void updateBaseDisplayMetricsIfNeeded() {
1732 // Get real display metrics without overrides from WM.
1733 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1734 final int orientation = mDisplayInfo.rotation;
1735 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1736 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1737 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1738 final int newDensity = mDisplayInfo.logicalDensityDpi;
1739
1740 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1741 || mInitialDisplayHeight != newHeight
1742 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1743
1744 if (displayMetricsChanged) {
1745 // Check if display size or density is forced.
1746 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1747 || mBaseDisplayHeight != mInitialDisplayHeight;
1748 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1749
1750 // If there is an override set for base values - use it, otherwise use new values.
1751 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1752 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1753 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1754
1755 // Real display metrics changed, so we should also update initial values.
1756 mInitialDisplayWidth = newWidth;
1757 mInitialDisplayHeight = newHeight;
1758 mInitialDisplayDensity = newDensity;
1759 mService.reconfigureDisplayLocked(this);
1760 }
1761 }
1762
Bryce Lee27cec322017-03-21 09:41:37 -07001763 /** Sets the maximum width the screen resolution can be */
1764 void setMaxUiWidth(int width) {
1765 if (DEBUG_DISPLAY) {
1766 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1767 }
1768
1769 mMaxUiWidth = width;
1770
1771 // Update existing metrics.
1772 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1773 }
1774
1775 /** Update base (override) display metrics. */
1776 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1777 mBaseDisplayWidth = baseWidth;
1778 mBaseDisplayHeight = baseHeight;
1779 mBaseDisplayDensity = baseDensity;
1780
1781 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1782 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1783 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1784 mBaseDisplayWidth = mMaxUiWidth;
1785
1786 if (DEBUG_DISPLAY) {
1787 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1788 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1789 + " on display:" + getDisplayId());
1790 }
1791 }
1792
1793 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1794 }
1795
Chong Zhangf66db432016-01-13 10:39:51 -08001796 void getContentRect(Rect out) {
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001797 out.set(mDisplayFrames.mContent);
Chong Zhangf66db432016-01-13 10:39:51 -08001798 }
1799
Wale Ogunwale61911492017-10-11 08:50:50 -07001800 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001801 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1802 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001803
Wale Ogunwale61911492017-10-11 08:50:50 -07001804 final TaskStack stack = new TaskStack(mService, stackId, controller);
1805 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001806 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001807 }
1808
Andrii Kulian51c1b672017-04-07 18:39:32 -07001809 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001810 final DisplayContent prevDc = stack.getDisplayContent();
1811 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001812 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1813 + " which is not currently attached to any display");
1814 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001815 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001816 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1817 + " to its current displayId=" + mDisplayId);
1818 }
1819
Wale Ogunwale61911492017-10-11 08:50:50 -07001820 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001821 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001822 }
1823
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001824 @Override
1825 protected void addChild(DisplayChildWindowContainer child,
1826 Comparator<DisplayChildWindowContainer> comparator) {
1827 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1828 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001829
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001830 @Override
1831 protected void addChild(DisplayChildWindowContainer child, int index) {
1832 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1833 }
1834
1835 @Override
1836 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001837 // Only allow removal of direct children from this display if the display is in the process
1838 // of been removed.
1839 if (mRemovingDisplay) {
1840 super.removeChild(child);
1841 return;
1842 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001843 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001844 }
1845
Andrii Kuliand2765632016-12-12 22:26:34 -08001846 @Override
1847 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1848 // Children of the display are statically ordered, so the real intention here is to perform
1849 // the operation on the display and not the static direct children.
1850 getParent().positionChildAt(position, this, includingParents);
1851 }
1852
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001853 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001854 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1855 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001856 final int taskId = stack.taskIdFromPoint(x, y);
1857 if (taskId != -1) {
1858 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001859 }
1860 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001861 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001862 }
1863
Chong Zhang8e89b312015-09-09 15:09:30 -07001864 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001865 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001866 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001867 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001868 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001869 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001870 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07001871 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1872 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001873 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001874 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001875 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001876
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001877 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1878 if (mTmpTaskForResizePointSearchResult.searchDone) {
1879 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001880 }
1881 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001882 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001883 }
1884
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001885 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001886 // The provided task is the task on this display with focus, so if WindowManagerService's
1887 // focused app is not on this display, focusedTask will be null.
1888 if (focusedTask == null) {
1889 mTouchExcludeRegion.setEmpty();
1890 } else {
1891 mTouchExcludeRegion.set(mBaseDisplayRect);
1892 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1893 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07001894 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1895 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001896 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
1897 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08001898 }
1899 // If we removed the focused task above, add it back and only leave its
1900 // outside touch area in the exclusion. TapDectector is not interested in
1901 // any touch inside the focused task itself.
1902 if (!mTmpRect2.isEmpty()) {
1903 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1904 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001905 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001906 final WindowState inputMethod = mService.mInputMethodWindow;
1907 if (inputMethod != null && inputMethod.isVisibleLw()) {
1908 // If the input method is visible and the user is typing, we don't want these touch
1909 // events to be intercepted and used to change focus. This would likely cause a
1910 // disappearance of the input method.
1911 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001912 if (inputMethod.getDisplayId() == mDisplayId) {
1913 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1914 } else {
1915 // IME is on a different display, so we need to update its tap detector.
1916 // TODO(multidisplay): Remove when IME will always appear on same display.
1917 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1918 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001919 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001920 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1921 WindowState win = mTapExcludedWindows.get(i);
1922 win.getTouchableRegion(mTmpRegion);
1923 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1924 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001925 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07001926 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001927 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001928 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001929 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1930 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001931 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001932 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001933 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001934 }
1935
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001936 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001937 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001938 super.switchUser();
1939 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001940 }
1941
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001942 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001943 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1944 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001945 }
1946 }
1947
1948 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001949 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001950 }
1951
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001952 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001953 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001954 }
1955
1956 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001957 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001958 }
1959
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001960 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001961 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001962 }
1963
Wale Ogunwale10124582016-09-15 20:25:50 -07001964 @Override
1965 void removeIfPossible() {
1966 if (isAnimating()) {
1967 mDeferredRemoval = true;
1968 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001969 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001970 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001971 }
1972
Wale Ogunwale10124582016-09-15 20:25:50 -07001973 @Override
1974 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001975 mRemovingDisplay = true;
1976 try {
1977 super.removeImmediately();
1978 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1979 mDimLayerController.close();
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07001980 if (mService.canDispatchPointerEvents()) {
1981 if (mTapDetector != null) {
1982 mService.unregisterPointerEventListener(mTapDetector);
1983 }
1984 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
1985 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1986 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001987 }
Robert Carrb1579c82017-09-05 14:54:47 -07001988 // The pending transaction won't be applied so we should
1989 // just clean up any surfaces pending destruction.
1990 onPendingTransactionApplied();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001991 } finally {
1992 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001993 }
Craig Mautner95da1082014-02-24 17:54:35 -08001994 }
1995
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001996 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001997 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001998 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001999 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2000
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002001 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002002 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08002003 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002004 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002005 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002006 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002007 }
2008
Andrii Kulian0214ed92017-05-16 13:44:05 -07002009 /** @return 'true' if removal of this display content is deferred due to active animation. */
2010 boolean isRemovalDeferred() {
2011 return mDeferredRemoval;
2012 }
2013
Wale Ogunwale10124582016-09-15 20:25:50 -07002014 boolean animateForIme(float interpolatedValue, float animationTarget,
2015 float dividerAnimationTarget) {
2016 boolean updated = false;
2017
Wale Ogunwale61911492017-10-11 08:50:50 -07002018 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2019 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002020 if (stack == null || !stack.isAdjustedForIme()) {
2021 continue;
2022 }
2023
2024 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2025 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2026 updated = true;
2027 } else {
2028 mDividerControllerLocked.mLastAnimationProgress =
2029 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2030 mDividerControllerLocked.mLastDividerProgress =
2031 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2032 updated |= stack.updateAdjustForIme(
2033 mDividerControllerLocked.mLastAnimationProgress,
2034 mDividerControllerLocked.mLastDividerProgress,
2035 false /* force */);
2036 }
2037 if (interpolatedValue >= 1f) {
2038 stack.endImeAdjustAnimation();
2039 }
2040 }
2041
2042 return updated;
2043 }
2044
2045 boolean clearImeAdjustAnimation() {
2046 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002047 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2048 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002049 if (stack != null && stack.isAdjustedForIme()) {
2050 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2051 changed = true;
2052 }
2053 }
2054 return changed;
2055 }
2056
2057 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002058 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2059 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002060 if (stack.isVisible() && stack.isAdjustedForIme()) {
2061 stack.beginImeAdjustAnimation();
2062 }
2063 }
2064 }
2065
2066 void adjustForImeIfNeeded() {
2067 final WindowState imeWin = mService.mInputMethodWindow;
2068 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2069 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002070 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002071 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2072 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2073 imeTargetStack.getDockSide() : DOCKED_INVALID;
2074 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2075 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2076 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002077 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002078 final boolean imeHeightChanged = imeVisible &&
2079 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2080
2081 // The divider could be adjusted for IME position, or be thinner than usual,
2082 // or both. There are three possible cases:
2083 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2084 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2085 // - If IME is not visible, divider is not moved and is normal width.
2086
2087 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002088 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2089 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002090 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002091 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2092 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002093 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2094 } else {
2095 stack.resetAdjustedForIme(false);
2096 }
2097 }
2098 mDividerControllerLocked.setAdjustedForIme(
2099 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2100 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002101 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2102 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002103 stack.resetAdjustedForIme(!dockVisible);
2104 }
2105 mDividerControllerLocked.setAdjustedForIme(
2106 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2107 }
Winson Chung655332c2016-10-31 13:14:28 -07002108 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002109 }
2110
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002111 void setInputMethodAnimLayerAdjustment(int adj) {
2112 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2113 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002114 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002115 }
2116
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002117 /**
2118 * If a window that has an animation specifying a colored background and the current wallpaper
2119 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2120 * suddenly disappear.
2121 */
2122 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002123 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2124 w -> w.mIsWallpaper && w.isVisibleNow());
2125
2126 if (visibleWallpaper != null) {
2127 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002128 }
2129 return winAnimator.mAnimLayer;
2130 }
2131
Wale Ogunwale10124582016-09-15 20:25:50 -07002132 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002133 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2134 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002135 stack.prepareFreezingTaskBounds();
2136 }
2137 }
2138
Wale Ogunwale94744212015-09-21 19:01:47 -07002139 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002140 getLogicalDisplayRect(mTmpRect, newRotation);
2141
2142 // Compute a transform matrix to undo the coordinate space transformation,
2143 // and present the window at the same physical position it previously occupied.
2144 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2145 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2146
2147 mTmpRectF.set(bounds);
2148 mTmpMatrix.mapRect(mTmpRectF);
2149 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002150 }
2151
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002152 static int deltaRotation(int oldRotation, int newRotation) {
2153 int delta = newRotation - oldRotation;
2154 if (delta < 0) delta += 4;
2155 return delta;
2156 }
2157
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002158 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002159 Matrix outMatrix) {
2160 // For rotations without Z-ordering we don't need the target rectangle's position.
2161 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2162 displayHeight, outMatrix);
2163 }
2164
2165 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2166 float displayWidth, float displayHeight, Matrix outMatrix) {
2167 switch (rotation) {
2168 case ROTATION_0:
2169 outMatrix.reset();
2170 break;
2171 case ROTATION_270:
2172 outMatrix.setRotate(270, 0, 0);
2173 outMatrix.postTranslate(0, displayHeight);
2174 outMatrix.postTranslate(rectTop, 0);
2175 break;
2176 case ROTATION_180:
2177 outMatrix.reset();
2178 break;
2179 case ROTATION_90:
2180 outMatrix.setRotate(90, 0, 0);
2181 outMatrix.postTranslate(displayWidth, 0);
2182 outMatrix.postTranslate(-rectTop, rectLeft);
2183 break;
2184 }
2185 }
2186
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002187 @CallSuper
2188 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002189 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002190 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002191 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002192 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002193 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2194 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002195 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002196 }
2197 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2198 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002199 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2200 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002201 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002202 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002203 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2204 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002205 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002206 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002207 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2208 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002209 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002210 }
2211 proto.write(DPI, mBaseDisplayDensity);
2212 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002213 proto.write(ROTATION, mRotation);
2214 final ScreenRotationAnimation screenRotationAnimation =
2215 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2216 if (screenRotationAnimation != null) {
2217 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2218 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002219 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002220 proto.end(token);
2221 }
2222
Craig Mautnera91f9e22012-09-14 16:22:08 -07002223 public void dump(String prefix, PrintWriter pw) {
2224 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2225 final String subPrefix = " " + prefix;
2226 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2227 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2228 pw.print("dpi");
2229 if (mInitialDisplayWidth != mBaseDisplayWidth
2230 || mInitialDisplayHeight != mBaseDisplayHeight
2231 || mInitialDisplayDensity != mBaseDisplayDensity) {
2232 pw.print(" base=");
2233 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2234 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2235 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002236 if (mDisplayScalingDisabled) {
2237 pw.println(" noscale");
2238 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002239 pw.print(" cur=");
2240 pw.print(mDisplayInfo.logicalWidth);
2241 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2242 pw.print(" app=");
2243 pw.print(mDisplayInfo.appWidth);
2244 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2245 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2246 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2247 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2248 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002249 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002250 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002251 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002252
Craig Mautnerdc548482014-02-05 13:35:24 -08002253 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002254 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002255 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2256 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002257 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002258 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002259
Craig Mautnerdc548482014-02-05 13:35:24 -08002260 pw.println();
2261 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002262 pw.println();
2263 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002264 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002265 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002266 pw.print(" Exiting #"); pw.print(i);
2267 pw.print(' '); pw.print(token);
2268 pw.println(':');
2269 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002270 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002271 }
Craig Mautner59c00972012-07-30 12:10:24 -07002272 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002273 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002274 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002275
2276 // Dump stack references
2277 final TaskStack homeStack = getHomeStack();
2278 if (homeStack != null) {
2279 pw.println(prefix + "homeStack=" + homeStack.getName());
2280 }
2281 final TaskStack pinnedStack = getPinnedStack();
2282 if (pinnedStack != null) {
2283 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2284 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002285 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002286 if (splitScreenPrimaryStack != null) {
2287 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2288 }
2289
2290 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002291 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002292 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002293 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002294
2295 if (mInputMethodAnimLayerAdjustment != 0) {
2296 pw.println(subPrefix
2297 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2298 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002299
2300 pw.println();
2301 mDisplayFrames.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002302 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002303
2304 @Override
2305 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002306 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002307 }
2308
2309 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002310 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002311 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002312
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002313 /** Returns true if the stack in the windowing mode is visible. */
2314 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002315 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002316 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002317 }
2318
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002319 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002320 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002321 final int x = (int) xf;
2322 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002323 final WindowState touchedWin = getWindow(w -> {
2324 final int flags = w.mAttrs.flags;
2325 if (!w.isVisibleLw()) {
2326 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002327 }
2328 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002329 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002330 }
2331
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002332 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002333 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002334 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002335 }
2336
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002337 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002338
2339 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002340 return mTmpRegion.contains(x, y) || touchFlags == 0;
2341 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002342
2343 return touchedWin;
2344 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002345
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002346 boolean canAddToastWindowForUid(int uid) {
2347 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002348 // Also if the app is focused adding more than one toast at
2349 // a time for better backwards compatibility.
2350 final WindowState focusedWindowForUid = getWindow(w ->
2351 w.mOwnerUid == uid && w.isFocused());
2352 if (focusedWindowForUid != null) {
2353 return true;
2354 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002355 final WindowState win = getWindow(w ->
2356 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2357 && !w.mWindowRemovalAllowed);
2358 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002359 }
2360
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002361 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002362 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2363 return;
2364 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002365
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002366 // Used to communicate the old focus to the callback method.
2367 mTmpWindow = oldFocus;
2368
2369 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002370 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002371
2372 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002373 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002374
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002375 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002376
2377 if (mTmpWindow == null) {
2378 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2379 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002380 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002381 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002382 }
2383
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002384 /** Updates the layer assignment of windows on this display. */
2385 void assignWindowLayers(boolean setLayoutNeeded) {
Robert Carrb1579c82017-09-05 14:54:47 -07002386 assignChildLayers(mPendingTransaction);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002387 if (setLayoutNeeded) {
2388 setLayoutNeeded();
2389 }
Robert Carrb1579c82017-09-05 14:54:47 -07002390
2391 // We accumlate the layer changes in-to "mPendingTransaction" but we defer
2392 // the application of this transaction until the animation pass triggers
2393 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2394 // the hiding and showing of surfaces.
2395 scheduleAnimation();
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002396 }
2397
Wale Ogunwale1666e312016-12-16 11:27:18 -08002398 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2399 // moving containers or resizing them. Need to investigate the best way to have it automatically
2400 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002401 void layoutAndAssignWindowLayersIfNeeded() {
2402 mService.mWindowsChanged = true;
2403 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002404
2405 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2406 false /*updateInputWindows*/)) {
2407 assignWindowLayers(false /* setLayoutNeeded */);
2408 }
2409
2410 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2411 mService.mWindowPlacerLocked.performSurfacePlacement();
2412 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2413 }
2414
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002415 /** Returns true if a leaked surface was destroyed */
2416 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002417 // Used to indicate that a surface was leaked.
2418 mTmpWindow = null;
2419 forAllWindows(w -> {
2420 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002421 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002422 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002423 }
2424 if (!mService.mSessions.contains(wsa.mSession)) {
2425 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002426 + w + " surface=" + wsa.mSurfaceController
2427 + " token=" + w.mToken
2428 + " pid=" + w.mSession.mPid
2429 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002430 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002431 mService.mForceRemoves.add(w);
2432 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002433 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002434 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002435 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002436 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002437 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002438 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002439 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002440 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002441 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002442
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002443 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002444 }
2445
2446 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002447 * Determine and return the window that should be the IME target.
2448 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2449 * @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 +00002450 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002451 WindowState computeImeTarget(boolean updateImeTarget) {
2452 if (mService.mInputMethodWindow == null) {
2453 // There isn't an IME so there shouldn't be a target...That was easy!
2454 if (updateImeTarget) {
2455 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2456 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2457 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2458 }
2459 return null;
2460 }
2461
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002462 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2463 // same display. Or even when the current IME/target are not on the same screen as the next
2464 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002465 mUpdateImeTarget = updateImeTarget;
2466 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002467
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002468
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002469 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2470 // to be on top of it, but it is not -really- where input will go. So look down below
2471 // for a real window to target...
2472 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2473 final AppWindowToken token = target.mAppToken;
2474 if (token != null) {
2475 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2476 if (betterTarget != null) {
2477 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002478 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002479 }
2480 }
2481
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002482 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2483 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002484
2485 // Now, a special case -- if the last target's window is in the process of exiting, and is
2486 // above the new target, keep on the last target to avoid flicker. Consider for example a
2487 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2488 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2489 // scrim.
2490 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002491 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2492 && (target == null
2493 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002494 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002495 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002496 }
2497
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002498 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2499 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002500
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002501 if (target == null) {
2502 if (updateImeTarget) {
2503 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2504 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2505 + Debug.getCallers(4) : ""));
2506 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2507 }
2508
2509 return null;
2510 }
2511
2512 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002513 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2514 if (token != null) {
2515
2516 // Now some fun for dealing with window animations that modify the Z order. We need
2517 // to look at all windows below the current target that are in this app, finding the
2518 // highest visible one in layering.
2519 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002520 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002521 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002522 }
2523
2524 if (highestTarget != null) {
2525 final AppTransition appTransition = mService.mAppTransition;
2526 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2527 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2528 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002529 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002530
2531 if (appTransition.isTransitionSet()) {
2532 // If we are currently setting up for an animation, hold everything until we
2533 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002534 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2535 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002536 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002537 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002538 // If the window we are currently targeting is involved with an animation,
2539 // and it is on top of the next target we will be over, then hold off on
2540 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002541 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2542 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002543 }
2544 }
2545 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002546
2547 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2548 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2549 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002550 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002551 }
2552
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002553 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002554 }
2555
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002556 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2557 if (target == mService.mInputMethodTarget
2558 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2559 && mInputMethodAnimLayerAdjustment == layerAdj) {
2560 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002561 }
2562
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002563 mService.mInputMethodTarget = target;
2564 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2565 setInputMethodAnimLayerAdjustment(layerAdj);
2566 assignWindowLayers(false /* setLayoutNeeded */);
2567 }
2568
2569 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2570 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2571 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2572 }
2573
2574 // Used to indicate we have reached the first window in the range we are interested in.
2575 mTmpWindow = null;
2576
2577 // TODO: Figure-out a more efficient way to do this.
2578 final WindowState candidate = getWindow(w -> {
2579 if (w == top) {
2580 // Reached the first window in the range we are interested in.
2581 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002582 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002583 if (mTmpWindow == null) {
2584 return false;
2585 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002586
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002587 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2588 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002589 }
2590 // If we reached the bottom of the range of windows we are considering,
2591 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002592 if (w == bottom) {
2593 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002594 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002595 return false;
2596 });
2597
2598 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002599 }
2600
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002601 void setLayoutNeeded() {
2602 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2603 mLayoutNeeded = true;
2604 }
2605
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002606 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002607 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2608 mLayoutNeeded = false;
2609 }
2610
2611 boolean isLayoutNeeded() {
2612 return mLayoutNeeded;
2613 }
2614
Wale Ogunwale02319a62016-09-26 15:21:22 -07002615 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2616 if (mTokenMap.isEmpty()) {
2617 return;
2618 }
2619 pw.println(" Display #" + mDisplayId);
2620 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2621 while (it.hasNext()) {
2622 final WindowToken token = it.next();
2623 pw.print(" ");
2624 pw.print(token);
2625 if (dumpAll) {
2626 pw.println(':');
2627 token.dump(pw, " ");
2628 } else {
2629 pw.println();
2630 }
2631 }
2632 }
2633
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002634 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002635 final int[] index = new int[1];
2636 forAllWindows(w -> {
2637 final WindowStateAnimator wAnim = w.mWinAnimator;
2638 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2639 index[0] = index[0] + 1;
2640 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002641 }
2642
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002643 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002644 forAllWindows(w -> {
2645 w.mWinAnimator.enableSurfaceTrace(fd);
2646 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002647 }
2648
2649 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002650 forAllWindows(w -> {
2651 w.mWinAnimator.disableSurfaceTrace();
2652 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002653 }
2654
Jorim Jaggife762342016-10-13 14:33:27 +02002655 /**
2656 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2657 */
2658 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2659 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002660 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002661 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2662 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002663 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002664 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2665 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002666 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002667 }
2668
Wale Ogunwale494009b82016-10-21 09:01:38 -07002669 boolean checkWaitingForWindows() {
2670
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002671 mHaveBootMsg = false;
2672 mHaveApp = false;
2673 mHaveWallpaper = false;
2674 mHaveKeyguard = true;
2675
2676 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002677 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2678 return true;
2679 }
2680 if (w.isDrawnLw()) {
2681 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002682 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002683 } else if (w.mAttrs.type == TYPE_APPLICATION
2684 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002685 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002686 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002687 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002688 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002689 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002690 }
2691 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002692 return false;
2693 });
2694
2695 if (visibleWindow != null) {
2696 // We have a visible window.
2697 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002698 }
2699
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002700 // if the wallpaper service is disabled on the device, we're never going to have
2701 // wallpaper, don't bother waiting for it
2702 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2703 com.android.internal.R.bool.config_enableWallpaperService)
2704 && !mService.mOnlyCore;
2705
Wale Ogunwale494009b82016-10-21 09:01:38 -07002706 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2707 "******** booted=" + mService.mSystemBooted
2708 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002709 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2710 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2711 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002712
2713 // If we are turning on the screen to show the boot message, don't do it until the boot
2714 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002715 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002716 return true;
2717 }
2718
2719 // If we are turning on the screen after the boot is completed normally, don't do so until
2720 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002721 if (mService.mSystemBooted
2722 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002723 return true;
2724 }
2725
2726 return false;
2727 }
2728
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002729 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002730 mTmpWindowAnimator = animator;
2731 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002732 }
2733
2734 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002735 resetAnimationBackgroundAnimator();
2736
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002737 // Used to indicate a detached wallpaper.
2738 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002739 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002740
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002741 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002742
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002743 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002744 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002745 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2746 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002747 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2748 }
2749 }
2750
Wale Ogunwale494009b82016-10-21 09:01:38 -07002751 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002752 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002753 if (imFocus == null) {
2754 return false;
2755 }
2756
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002757 if (DEBUG_INPUT_METHOD) {
2758 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2759 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2760 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002761 }
2762
Wale Ogunwale494009b82016-10-21 09:01:38 -07002763 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2764
2765 if (DEBUG_INPUT_METHOD) {
2766 Slog.i(TAG_WM, "IM target client: " + imeClient);
2767 if (imeClient != null) {
2768 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2769 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2770 }
2771 }
2772
2773 return imeClient != null && imeClient.asBinder() == client.asBinder();
2774 }
2775
2776 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002777 final WindowState win = getWindow(
2778 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2779 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002780 }
2781
2782 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002783 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002784 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002785 final int curValue = w.mSystemUiVisibility;
2786 final int diff = (curValue ^ visibility) & globalDiff;
2787 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002788 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002789 w.mSeq++;
2790 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002791 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002792 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2793 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002794 visibility, newValue, diff);
2795 }
2796 } catch (RemoteException e) {
2797 // so sorry
2798 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002799 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002800 }
2801
2802 void onWindowFreezeTimeout() {
2803 Slog.w(TAG_WM, "Window freeze timeout expired.");
2804 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002805
2806 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002807 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002808 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002809 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002810 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002811 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2812 - mService.mDisplayFreezeTime);
2813 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002814 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002815 mService.mWindowPlacerLocked.performSurfacePlacement();
2816 }
2817
2818 void waitForAllWindowsDrawn() {
2819 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002820 forAllWindows(w -> {
2821 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2822 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2823 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002824 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002825 w.mLastContentInsets.set(-1, -1, -1, -1);
2826 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002827 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002828 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002829 }
2830
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002831 // TODO: Super crazy long method that should be broken down...
2832 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2833
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002834 final int dw = mDisplayInfo.logicalWidth;
2835 final int dh = mDisplayInfo.logicalHeight;
2836 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2837
2838 mTmpUpdateAllDrawn.clear();
2839
2840 int repeats = 0;
2841 do {
2842 repeats++;
2843 if (repeats > 6) {
2844 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2845 clearLayoutNeeded();
2846 break;
2847 }
2848
2849 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2850 pendingLayoutChanges);
2851
Andrii Kulian839def92016-11-02 10:58:58 -07002852 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2853 // the wallpaper window jumping across displays.
2854 // Remove check for default display when there will be support for multiple wallpaper
2855 // targets (on different displays).
2856 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002857 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002858 }
2859
2860 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2861 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2862 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2863 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002864 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002865 }
2866 }
2867
2868 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2869 setLayoutNeeded();
2870 }
2871
2872 // FIRST LOOP: Perform a layout, if needed.
2873 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2874 performLayout(repeats == 1, false /* updateInputWindows */);
2875 } else {
2876 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2877 }
2878
2879 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2880 pendingLayoutChanges = 0;
2881
2882 if (isDefaultDisplay) {
2883 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002884 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002885 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2886 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2887 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2888 }
2889 } while (pendingLayoutChanges != 0);
2890
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002891 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002892 resetDimming();
2893
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002894 mTmpRecoveringMemory = recoveringMemory;
2895 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002896
2897 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002898 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2899 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2900 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002901 true /* inTraversal, must call performTraversalInTrans... below */);
2902
2903 stopDimmingIfNeeded();
2904
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002905 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2906 if (wallpaperVisible != mLastWallpaperVisible) {
2907 mLastWallpaperVisible = wallpaperVisible;
2908 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2909 }
2910
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002911 while (!mTmpUpdateAllDrawn.isEmpty()) {
2912 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2913 // See if any windows have been drawn, so they (and others associated with them)
2914 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002915 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002916 }
2917
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002918 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002919 }
2920
2921 void performLayout(boolean initial, boolean updateInputWindows) {
2922 if (!isLayoutNeeded()) {
2923 return;
2924 }
2925 clearLayoutNeeded();
2926
2927 final int dw = mDisplayInfo.logicalWidth;
2928 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002929 if (DEBUG_LAYOUT) {
2930 Slog.v(TAG, "-------------------------------------");
2931 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2932 }
2933
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002934 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
2935 // TODO: Not sure if we really need to set the rotation here since we are updating from the
2936 // display info above...
2937 mDisplayFrames.mRotation = mRotation;
2938 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002939 if (isDefaultDisplay) {
2940 // Not needed on non-default displays.
2941 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2942 mService.mScreenRect.set(0, 0, dw, dh);
2943 }
2944
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002945 int seq = mService.mLayoutSeq + 1;
2946 if (seq < 0) seq = 0;
2947 mService.mLayoutSeq = seq;
2948
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002949 // Used to indicate that we have processed the dream window and all additional windows are
2950 // behind it.
2951 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002952 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002953
2954 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002955 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002956
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002957 // Used to indicate that we have processed the dream window and all additional attached
2958 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002959 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002960 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002961
2962 // Now perform layout of attached windows, which usually depend on the position of the
2963 // window they are attached to. XXX does not deal with windows that are attached to windows
2964 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002965 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002966
2967 // Window frames may have changed. Tell the input dispatcher about it.
2968 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2969 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2970 if (updateInputWindows) {
2971 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2972 }
2973
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002974 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2975 }
2976
2977 /**
2978 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2979 * In portrait mode, it grabs the full screenshot.
2980 *
2981 * @param width the width of the target bitmap
2982 * @param height the height of the target bitmap
2983 * @param includeFullDisplay true if the screen should not be cropped before capture
2984 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2985 * @param config of the output bitmap
2986 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002987 * @param includeDecor whether to include window decors, like the status or navigation bar
2988 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002989 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002990 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002991 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002992 boolean wallpaperOnly, boolean includeDecor) {
2993 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2994 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002995 if (bitmap == null) {
2996 return null;
2997 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002998
2999 if (DEBUG_SCREENSHOT) {
3000 // TEST IF IT's ALL BLACK
3001 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
3002 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
3003 bitmap.getHeight());
3004 boolean allBlack = true;
3005 final int firstColor = buffer[0];
3006 for (int i = 0; i < buffer.length; i++) {
3007 if (buffer[i] != firstColor) {
3008 allBlack = false;
3009 break;
3010 }
3011 }
3012 if (allBlack) {
3013 final WindowState appWin = mScreenshotApplicationState.appWin;
3014 final int maxLayer = mScreenshotApplicationState.maxLayer;
3015 final int minLayer = mScreenshotApplicationState.minLayer;
3016 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
3017 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
3018 (appWin != null ?
3019 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
3020 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
3021 }
3022 }
3023
3024 // Create a copy of the screenshot that is immutable and backed in ashmem.
3025 // This greatly reduces the overhead of passing the bitmap between processes.
3026 Bitmap ret = bitmap.createAshmemBitmap(config);
3027 bitmap.recycle();
3028 return ret;
3029 }
3030
3031 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
3032 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
3033 boolean includeDecor) {
3034 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
3035 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
3036 }
3037
3038 private <E> E screenshotApplications(IBinder appToken, int width, int height,
3039 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
3040 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003041 int dw = mDisplayInfo.logicalWidth;
3042 int dh = mDisplayInfo.logicalHeight;
3043 if (dw == 0 || dh == 0) {
3044 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3045 + ": returning null. logical widthxheight=" + dw + "x" + dh);
3046 return null;
3047 }
3048
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003049 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003050
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003051 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003052 final Rect frame = new Rect();
3053 final Rect stackBounds = new Rect();
3054
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003055 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003056 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08003057 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003058 synchronized(mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02003059 if (!mService.mPolicy.isScreenOn()) {
3060 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Attempted to take screenshot while display"
3061 + " was off.");
3062 return null;
3063 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003064 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003065 mScreenshotApplicationState.appWin = null;
3066 forAllWindows(w -> {
3067 if (!w.mHasSurface) {
3068 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003069 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003070 if (w.mLayer >= aboveAppLayer) {
3071 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003072 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003073 if (wallpaperOnly && !w.mIsWallpaper) {
3074 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003075 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003076 if (w.mIsImWindow) {
Jorim Jaggieb59d6e2017-05-04 13:54:22 +02003077 return false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003078 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003079 // If this is the wallpaper layer and we're only looking for the wallpaper layer
3080 // then the target window state is this one.
3081 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003082 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003083 }
3084
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003085 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003086 // We have not ran across the target window yet, so it is probably behind
3087 // the wallpaper. This can happen when the keyguard is up and all windows
3088 // are moved behind the wallpaper. We don't want to include the wallpaper
3089 // layer in the screenshot as it will cover-up the layer of the target
3090 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003091 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003092 }
3093 // Fall through. The target window is in front of the wallpaper. For this
3094 // case we want to include the wallpaper layer in the screenshot because
3095 // the target window might have some transparent areas.
3096 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003097 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003098 // This app window is of no interest if it is not associated with the
3099 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003100 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003101 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003102 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003103 }
3104
3105 // Include this window.
3106
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003107 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003108
3109 // Don't include wallpaper in bounds calculation
Jorim Jaggi70f59482017-05-04 14:05:59 +02003110 if (!w.mIsWallpaper && !mutableIncludeFullDisplay.value) {
3111 if (includeDecor) {
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003112 final Task task = w.getTask();
3113 if (task != null) {
3114 task.getBounds(frame);
3115 } else {
Jorim Jaggi30d64f32017-04-07 16:33:17 +02003116
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003117 // No task bounds? Too bad! Ain't no screenshot then.
3118 return true;
3119 }
Jorim Jaggi70f59482017-05-04 14:05:59 +02003120 } else {
3121 final Rect wf = w.mFrame;
3122 final Rect cr = w.mContentInsets;
3123 int left = wf.left + cr.left;
3124 int top = wf.top + cr.top;
3125 int right = wf.right - cr.right;
3126 int bottom = wf.bottom - cr.bottom;
3127 frame.union(left, top, right, bottom);
3128 w.getVisibleBounds(stackBounds);
3129 if (!Rect.intersects(frame, stackBounds)) {
3130 // Set frame empty if there's no intersection.
3131 frame.setEmpty();
3132 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003133 }
3134 }
3135
3136 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003137 (w.mAppToken != null && w.mAppToken.token == appToken)
3138 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi406386a82017-09-15 14:17:40 +02003139 if (foundTargetWs && winAnim.getShown() && winAnim.mLastAlpha > 0f) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003140 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003141 }
3142
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003143 if (w.isObscuringDisplay()){
3144 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003145 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003146 return false;
3147 }, true /* traverseTopToBottom */);
3148
3149 final WindowState appWin = mScreenshotApplicationState.appWin;
3150 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003151
3152 if (appToken != null && appWin == null) {
3153 // Can't find a window to snapshot.
3154 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
3155 "Screenshot: Couldn't find a surface matching " + appToken);
3156 return null;
3157 }
3158
3159 if (!screenshotReady) {
3160 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3161 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3162 appWin.mWinAnimator.mDrawState)));
3163 return null;
3164 }
3165
3166 // Screenshot is ready to be taken. Everything from here below will continue
3167 // through the bottom of the loop and return a value. We only stay in the loop
3168 // because we don't want to release the mWindowMap lock until the screenshot is
3169 // taken.
3170
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003171
Jorim Jaggi02886a82016-12-06 09:10:06 -08003172 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003173 // Constrain frame to the screen size.
3174 if (!frame.intersect(0, 0, dw, dh)) {
3175 frame.setEmpty();
3176 }
3177 } else {
3178 // Caller just wants entire display.
3179 frame.set(0, 0, dw, dh);
3180 }
3181 if (frame.isEmpty()) {
3182 return null;
3183 }
3184
3185 if (width < 0) {
3186 width = (int) (frame.width() * frameScale);
3187 }
3188 if (height < 0) {
3189 height = (int) (frame.height() * frameScale);
3190 }
3191
3192 // Tell surface flinger what part of the image to crop. Take the top
3193 // right part of the application, and crop the larger dimension to fit.
3194 Rect crop = new Rect(frame);
3195 if (width / (float) frame.width() < height / (float) frame.height()) {
3196 int cropWidth = (int)((float)width / (float)height * frame.height());
3197 crop.right = crop.left + cropWidth;
3198 } else {
3199 int cropHeight = (int)((float)height / (float)width * frame.width());
3200 crop.bottom = crop.top + cropHeight;
3201 }
3202
3203 // The screenshot API does not apply the current screen rotation.
3204 int rot = mDisplay.getRotation();
3205
3206 if (rot == ROTATION_90 || rot == ROTATION_270) {
3207 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3208 }
3209
3210 // Surfaceflinger is not aware of orientation, so convert our logical
3211 // crop to surfaceflinger's portrait orientation.
3212 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3213
3214 if (DEBUG_SCREENSHOT) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003215 forAllWindows(w -> {
3216 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3217 Slog.i(TAG_WM, w + ": " + w.mLayer
3218 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003219 + " surfaceLayer=" + ((controller == null)
3220 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003221 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003222 }
3223
3224 final ScreenRotationAnimation screenRotationAnimation =
3225 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3226 final boolean inRotation = screenRotationAnimation != null &&
3227 screenRotationAnimation.isAnimating();
3228 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3229 "Taking screenshot while rotating");
3230
3231 // We force pending transactions to flush before taking
3232 // the screenshot by pushing an empty synchronous transaction.
3233 SurfaceControl.openTransaction();
3234 SurfaceControl.closeTransactionSync();
3235
Robert Carrb1579c82017-09-05 14:54:47 -07003236 // TODO(b/68392460): We should screenshot Task controls directly
3237 // but it's difficult at the moment as the Task doesn't have the
3238 // correct size set.
3239 bitmap = screenshoter.screenshot(crop, width, height, 0, 1,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003240 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003241 if (bitmap == null) {
Robert Carrb1579c82017-09-05 14:54:47 -07003242 Slog.w(TAG_WM, "Failed to take screenshot");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003243 return null;
3244 }
3245 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003246 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003247 }
3248
3249 // TODO: Can this use createRotationMatrix()?
3250 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3251 if (rot == Surface.ROTATION_90) {
3252 final int tmp = crop.top;
3253 crop.top = dw - crop.right;
3254 crop.right = crop.bottom;
3255 crop.bottom = dw - crop.left;
3256 crop.left = tmp;
3257 } else if (rot == Surface.ROTATION_180) {
3258 int tmp = crop.top;
3259 crop.top = dh - crop.bottom;
3260 crop.bottom = dh - tmp;
3261 tmp = crop.right;
3262 crop.right = dw - crop.left;
3263 crop.left = dw - tmp;
3264 } else if (rot == Surface.ROTATION_270) {
3265 final int tmp = crop.top;
3266 crop.top = crop.left;
3267 crop.left = dh - crop.bottom;
3268 crop.bottom = crop.right;
3269 crop.right = dh - tmp;
3270 }
3271 }
3272
3273 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003274 // Used to indicate the layout is needed.
3275 mTmpWindow = null;
3276
3277 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003278 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003279 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003280 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003281 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003282 w.setDisplayLayoutNeeded();
3283 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003284 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003285
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003286 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003287 mService.mWindowPlacerLocked.performSurfacePlacement();
3288 }
3289 }
3290
Wale Ogunwale1666e312016-12-16 11:27:18 -08003291 void setExitingTokensHasVisible(boolean hasVisible) {
3292 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3293 mExitingTokens.get(i).hasVisible = hasVisible;
3294 }
3295
3296 // Initialize state of exiting applications.
3297 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3298 }
3299
3300 void removeExistingTokensIfPossible() {
3301 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3302 final WindowToken token = mExitingTokens.get(i);
3303 if (!token.hasVisible) {
3304 mExitingTokens.remove(i);
3305 }
3306 }
3307
3308 // Time to remove any exiting applications?
3309 mTaskStackContainers.removeExistingAppTokensIfPossible();
3310 }
3311
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003312 @Override
3313 void onDescendantOverrideConfigurationChanged() {
3314 setLayoutNeeded();
3315 mService.requestTraversal();
3316 }
3317
David Stevens9440dc82017-03-16 19:00:20 -07003318 boolean okToDisplay() {
3319 if (mDisplayId == DEFAULT_DISPLAY) {
3320 return !mService.mDisplayFrozen
3321 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3322 }
3323 return mDisplayInfo.state == Display.STATE_ON;
3324 }
3325
3326 boolean okToAnimate() {
3327 return okToDisplay() &&
3328 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3329 }
3330
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003331 static final class TaskForResizePointSearchResult {
3332 boolean searchDone;
3333 Task taskForResize;
3334
3335 void reset() {
3336 searchDone = false;
3337 taskForResize = null;
3338 }
3339 }
Robert Carr3b716242016-08-16 16:02:21 -07003340
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003341 private static final class ApplySurfaceChangesTransactionState {
3342 boolean displayHasContent;
3343 boolean obscured;
3344 boolean syswin;
3345 boolean focusDisplayed;
3346 float preferredRefreshRate;
3347 int preferredModeId;
3348
3349 void reset() {
3350 displayHasContent = false;
3351 obscured = false;
3352 syswin = false;
3353 focusDisplayed = false;
3354 preferredRefreshRate = 0;
3355 preferredModeId = 0;
3356 }
3357 }
3358
3359 private static final class ScreenshotApplicationState {
3360 WindowState appWin;
3361 int maxLayer;
3362 int minLayer;
3363 boolean screenshotReady;
3364
3365 void reset(boolean screenshotReady) {
3366 appWin = null;
3367 maxLayer = 0;
3368 minLayer = 0;
3369 this.screenshotReady = screenshotReady;
3370 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3371 }
3372 }
3373
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003374 /**
3375 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3376 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3377 * homogeneous children type which is currently required by sub-classes of
3378 * {@link WindowContainer} class.
3379 */
3380 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3381
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003382 @Override
3383 boolean fillsParent() {
3384 return true;
3385 }
3386
3387 @Override
3388 boolean isVisible() {
3389 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003390 }
3391 }
3392
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003393 /**
3394 * Window container class that contains all containers on this display relating to Apps.
3395 * I.e Activities.
3396 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003397 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003398 /**
3399 * A control placed at the appropriate level for transitions to occur.
3400 */
3401 SurfaceControl mAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003402
Wale Ogunwale61911492017-10-11 08:50:50 -07003403 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3404 // through the list to find them.
3405 private TaskStack mHomeStack = null;
3406 private TaskStack mPinnedStack = null;
3407 private TaskStack mSplitScreenPrimaryStack = null;
3408
3409 /**
3410 * Returns the topmost stack on the display that is compatible with the input windowing mode
3411 * and activity type. Null is no compatible stack on the display.
3412 */
3413 TaskStack getStack(int windowingMode, int activityType) {
3414 if (activityType == ACTIVITY_TYPE_HOME) {
3415 return mHomeStack;
3416 }
3417 if (windowingMode == WINDOWING_MODE_PINNED) {
3418 return mPinnedStack;
3419 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3420 return mSplitScreenPrimaryStack;
3421 }
3422 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3423 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003424 if (activityType == ACTIVITY_TYPE_UNDEFINED
3425 && windowingMode == stack.getWindowingMode()) {
3426 // Passing in undefined type means we want to match the topmost stack with the
3427 // windowing mode.
3428 return stack;
3429 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003430 if (stack.isCompatible(windowingMode, activityType)) {
3431 return stack;
3432 }
3433 }
3434 return null;
3435 }
3436
3437 @VisibleForTesting
3438 TaskStack getTopStack() {
3439 return mTaskStackContainers.getChildCount() > 0
3440 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3441 }
3442
3443 TaskStack getHomeStack() {
3444 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3445 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3446 }
3447 return mHomeStack;
3448 }
3449
3450 TaskStack getPinnedStack() {
3451 return mPinnedStack;
3452 }
3453
Matthew Ng64e77cf2017-10-31 14:01:31 -07003454 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003455 return mSplitScreenPrimaryStack;
3456 }
3457
Andrii Kulian839def92016-11-02 10:58:58 -07003458 /**
3459 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003460 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003461 */
3462 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003463 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003464 addChild(stack, onTop);
3465 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003466 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003467
Wale Ogunwale61911492017-10-11 08:50:50 -07003468 void onStackWindowingModeChanged(TaskStack stack) {
3469 removeStackReferenceIfNeeded(stack);
3470 addStackReferenceIfNeeded(stack);
3471 if (stack == mPinnedStack && getTopStack() != stack) {
3472 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3473 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3474 }
3475 }
3476
3477 private void addStackReferenceIfNeeded(TaskStack stack) {
3478 if (stack.isActivityTypeHome()) {
3479 if (mHomeStack != null) {
3480 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
3481 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
3482 }
3483 mHomeStack = stack;
3484 }
3485 final int windowingMode = stack.getWindowingMode();
3486 if (windowingMode == WINDOWING_MODE_PINNED) {
3487 if (mPinnedStack != null) {
3488 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3489 + mPinnedStack + " already exist on display=" + this
3490 + " stack=" + stack);
3491 }
3492 mPinnedStack = stack;
3493 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3494 if (mSplitScreenPrimaryStack != null) {
3495 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3496 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3497 + " already exist on display=" + this + " stack=" + stack);
3498 }
3499 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003500 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003501 }
3502 }
3503
3504 private void removeStackReferenceIfNeeded(TaskStack stack) {
3505 if (stack == mHomeStack) {
3506 mHomeStack = null;
3507 } else if (stack == mPinnedStack) {
3508 mPinnedStack = null;
3509 } else if (stack == mSplitScreenPrimaryStack) {
3510 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003511 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3512 mService.setDockedStackCreateStateLocked(
3513 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3514 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003515 }
Andrii Kulian839def92016-11-02 10:58:58 -07003516 }
3517
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003518 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003519 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3520 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003521 addChild(stack, addIndex);
3522 setLayoutNeeded();
3523 }
3524
Wale Ogunwale61911492017-10-11 08:50:50 -07003525 @Override
3526 protected void removeChild(TaskStack stack) {
3527 super.removeChild(stack);
3528 removeStackReferenceIfNeeded(stack);
3529 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003530
3531 @Override
3532 boolean isOnTop() {
3533 // Considered always on top
3534 return true;
3535 }
3536
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003537 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003538 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003539 if (child.getWindowConfiguration().isAlwaysOnTop()
3540 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003541 // This stack is always-on-top, override the default behavior.
3542 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3543
3544 // Moving to its current position, as we must call super but we don't want to
3545 // perform any meaningful action.
3546 final int currentPosition = mChildren.indexOf(child);
3547 super.positionChildAt(currentPosition, child, false /* includingParents */);
3548 return;
3549 }
3550
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003551 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3552 super.positionChildAt(targetPosition, child, includingParents);
3553
3554 setLayoutNeeded();
3555 }
3556
3557 /**
3558 * When stack is added or repositioned, find a proper position for it.
3559 * This will make sure that pinned stack always stays on top.
3560 * @param requestedPosition Position requested by caller.
3561 * @param stack Stack to be added or positioned.
3562 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3563 * @return The proper position for the stack.
3564 */
3565 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3566 final int topChildPosition = mChildren.size() - 1;
3567 boolean toTop = requestedPosition == POSITION_TOP;
3568 toTop |= adding ? requestedPosition >= topChildPosition + 1
3569 : requestedPosition >= topChildPosition;
3570 int targetPosition = requestedPosition;
3571
Wale Ogunwale61911492017-10-11 08:50:50 -07003572 if (toTop && stack.getWindowingMode() != WINDOWING_MODE_PINNED && hasPinnedStack()) {
Bryce Lee48f4b572017-04-10 10:54:15 -07003573 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003574 TaskStack topStack = mChildren.get(topChildPosition);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003575 if (topStack.getWindowingMode() != WINDOWING_MODE_PINNED) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003576 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3577 }
3578
3579 // So, stack is moved just below the pinned stack.
3580 // When we're adding a new stack the target is the current pinned stack position.
3581 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003582 // stack, because WindowContainer#positionAt() first removes element and then adds
3583 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003584 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3585 }
3586
3587 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003588 }
3589
3590 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003591 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3592 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003593 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003594 if (super.forAllWindows(callback, traverseTopToBottom)) {
3595 return true;
3596 }
3597 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3598 return true;
3599 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003600 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003601 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3602 return true;
3603 }
3604 if (super.forAllWindows(callback, traverseTopToBottom)) {
3605 return true;
3606 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003607 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003608 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003609 }
3610
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003611 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003612 boolean traverseTopToBottom) {
3613 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3614 // app tokens.
3615 // TODO: Investigate if we need to continue to do this or if we can just process them
3616 // in-order.
3617 if (traverseTopToBottom) {
3618 for (int i = mChildren.size() - 1; i >= 0; --i) {
3619 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3620 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003621 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3622 traverseTopToBottom)) {
3623 return true;
3624 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003625 }
3626 }
3627 } else {
3628 final int count = mChildren.size();
3629 for (int i = 0; i < count; ++i) {
3630 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3631 final int appTokensCount = appTokens.size();
3632 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003633 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3634 traverseTopToBottom)) {
3635 return true;
3636 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003637 }
3638 }
3639 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003640 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003641 }
3642
Wale Ogunwale1666e312016-12-16 11:27:18 -08003643 void setExitingTokensHasVisible(boolean hasVisible) {
3644 for (int i = mChildren.size() - 1; i >= 0; --i) {
3645 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3646 for (int j = appTokens.size() - 1; j >= 0; --j) {
3647 appTokens.get(j).hasVisible = hasVisible;
3648 }
3649 }
3650 }
3651
3652 void removeExistingAppTokensIfPossible() {
3653 for (int i = mChildren.size() - 1; i >= 0; --i) {
3654 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3655 for (int j = appTokens.size() - 1; j >= 0; --j) {
3656 final AppWindowToken token = appTokens.get(j);
3657 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3658 && (!token.mIsExiting || token.isEmpty())) {
3659 // Make sure there is no animation running on this token, so any windows
3660 // associated with it will be removed as soon as their animations are
3661 // complete.
3662 token.mAppAnimator.clearAnimation();
3663 token.mAppAnimator.animating = false;
3664 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3665 "performLayout: App token exiting now removed" + token);
3666 token.removeIfPossible();
3667 }
3668 }
3669 }
3670 }
3671
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003672 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003673 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003674 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3675 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003676 // Apps and their containers are not allowed to specify an orientation while the
3677 // docked or freeform stack is visible...except for the home stack/task if the
3678 // docked stack is minimized and it actually set something.
3679 if (mHomeStack != null && mHomeStack.isVisible()
3680 && mDividerControllerLocked.isMinimizedDock()) {
3681 final int orientation = mHomeStack.getOrientation();
3682 if (orientation != SCREEN_ORIENTATION_UNSET) {
3683 return orientation;
3684 }
3685 }
3686 return SCREEN_ORIENTATION_UNSPECIFIED;
3687 }
3688
3689 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003690 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3691 PackageManager.FEATURE_AUTOMOTIVE);
3692 if (isCar) {
3693 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3694 // allow anything but the default orientation.
3695 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3696 "Forcing UNSPECIFIED orientation in car. Ignoring " + orientation);
3697 return SCREEN_ORIENTATION_UNSPECIFIED;
3698 }
3699
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003700 if (orientation != SCREEN_ORIENTATION_UNSET
3701 && orientation != SCREEN_ORIENTATION_BEHIND) {
3702 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3703 "App is requesting an orientation, return " + orientation);
3704 return orientation;
3705 }
3706
3707 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003708 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003709 // The next app has not been requested to be visible, so we keep the current orientation
3710 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003711 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003712 }
Robert Carrb1579c82017-09-05 14:54:47 -07003713
3714 @Override
3715 void assignChildLayers(SurfaceControl.Transaction t) {
3716 final int NORMAL_STACK_STATE = 0;
3717 final int BOOSTED_STATE = 1;
3718 final int ALWAYS_ON_TOP_STATE = 2;
3719
3720 // We allow stacks to change visual order from the AM specified order due to
3721 // Z-boosting during animations. However we must take care to ensure TaskStacks
3722 // which are marked as alwaysOnTop remain that way.
3723 int layer = 0;
3724 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
3725 for (int i = 0; i < mChildren.size(); i++) {
3726 final TaskStack s = mChildren.get(i);
3727 layer++;
3728 if (state == NORMAL_STACK_STATE) {
3729 s.assignLayer(t, layer);
3730 } else if (state == BOOSTED_STATE && s.needsZBoost()) {
3731 s.assignLayer(t, layer);
3732 } else if (state == ALWAYS_ON_TOP_STATE &&
3733 s.isAlwaysOnTop()) {
3734 s.assignLayer(t, layer);
3735 }
3736 s.assignChildLayers(t);
3737 }
3738 // The appropriate place for App-Transitions to occur is right
3739 // above all other animations but still below things in the Picture-and-Picture
3740 // windowing mode.
3741 if (state == BOOSTED_STATE && mAnimationLayer != null) {
3742 t.setLayer(mAnimationLayer, layer + 1);
3743 }
3744 }
3745 }
3746
3747 @Override
3748 void onParentSet() {
3749 super.onParentSet();
3750 if (getParent() != null) {
3751 mAnimationLayer = makeSurface().build();
3752 } else {
3753 mAnimationLayer.destroy();
3754 mAnimationLayer = null;
3755 }
3756 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003757 }
3758
3759 /**
3760 * Window container class that contains all containers on this display that are not related to
3761 * Apps. E.g. status bar.
3762 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003763 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3764 /**
3765 * Compares two child window tokens returns -1 if the first is lesser than the second in
3766 * terms of z-order and 1 otherwise.
3767 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003768 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003769 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003770 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3771 token1.mOwnerCanManageAppTokens)
3772 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3773 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003774
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003775 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3776 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3777 return false;
3778 }
3779 final int req = w.mAttrs.screenOrientation;
3780 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3781 || req == SCREEN_ORIENTATION_UNSET) {
3782 return false;
3783 }
3784 return true;
3785 };
3786
Wale Ogunwale3a931692016-11-02 16:49:48 -07003787 private final String mName;
3788 NonAppWindowContainers(String name) {
3789 mName = name;
3790 }
3791
3792 void addChild(WindowToken token) {
3793 addChild(token, mWindowComparator);
3794 }
3795
3796 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003797 int getOrientation() {
3798 final WindowManagerPolicy policy = mService.mPolicy;
3799 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003800 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003801
3802 if (win != null) {
3803 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003804 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003805 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003806 if (mService.mKeyguardGoingAway) {
3807 // Keyguard can't affect the orientation if it is going away...
3808 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3809 return SCREEN_ORIENTATION_UNSET;
3810 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003811 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003812 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003813 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003814 }
3815
Andrii Kulian8ee72852017-03-10 10:36:45 -08003816 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003817
Jorim Jaggi75520d52017-08-24 17:23:19 +02003818 if (policy.isKeyguardShowingAndNotOccluded()
3819 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003820 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003821 }
3822
3823 return SCREEN_ORIENTATION_UNSET;
3824 }
3825
3826 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003827 String getName() {
3828 return mName;
3829 }
Robert Carr3b716242016-08-16 16:02:21 -07003830 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003831
3832 /**
3833 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3834 */
3835 @FunctionalInterface
3836 private interface Screenshoter<E> {
3837 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3838 boolean useIdentityTransform, int rotation);
3839 }
Robert Carrb1579c82017-09-05 14:54:47 -07003840
3841 SurfaceControl.Builder makeSurface(SurfaceSession s) {
3842 return mService.makeSurfaceBuilder(s)
3843 .setParent(mWindowingLayer);
3844 }
3845
3846 @Override
3847 SurfaceSession getSession() {
3848 return mSession;
3849 }
3850
3851 @Override
3852 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
3853 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(child.getSession());
3854 b.setName(child.getName());
3855
3856 b.setSize(mSurfaceSize, mSurfaceSize);
3857 if (child.isScreenOverlay()) {
3858 return b.setParent(mOverlayLayer);
3859 } else {
3860 return b.setParent(mWindowingLayer);
3861 }
3862 }
3863
3864 /**
3865 * The makeSurface variants are for use by the window-container
3866 * hierarchy. makeOverlay here is a function for various non windowing
3867 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
3868 * and other potpourii.
3869 */
3870 SurfaceControl.Builder makeOverlay() {
3871 return mService.makeSurfaceBuilder(mSession)
3872 .setParent(mOverlayLayer);
3873 }
3874
3875 void applyMagnificationSpec(MagnificationSpec spec) {
3876 applyMagnificationSpec(mPendingTransaction, spec);
3877 mPendingTransaction.apply();
3878 }
3879
3880 @Override
3881 void onParentSet() {
3882 // Since we are the top of the SurfaceControl hierarchy here
3883 // we create the root surfaces explicitly rather than chaining
3884 // up as the default implementation in onParentSet does. So we
3885 // explicitly do NOT call super here.
3886 }
3887
3888 @Override
3889 void assignChildLayers(SurfaceControl.Transaction t) {
3890 t.setLayer(mOverlayLayer, 1)
3891 .setLayer(mWindowingLayer, 0);
3892
3893 // These are layers as children of "mWindowingLayer"
3894 mBelowAppWindowsContainers.assignLayer(t, 0);
3895 mTaskStackContainers.assignLayer(t, 1);
3896 mAboveAppWindowsContainers.assignLayer(t, 2);
3897
3898 WindowState imeTarget = mService.mInputMethodTarget;
3899 if (imeTarget == null || imeTarget.inSplitScreenWindowingMode()) {
3900 // In split-screen windowing mode we can't layer the
3901 // IME relative to the IME target because it needs to
3902 // go over the docked divider, so instead we place it on top
3903 // of everything and use relative layering of windows which need
3904 // to go above it (see special logic in WindowState#assignLayer)
3905 mImeWindowsContainers.assignLayer(t, 3);
3906 } else {
3907 t.setRelativeLayer(mImeWindowsContainers.getSurfaceControl(),
3908 imeTarget.getSurfaceControl(),
3909 // TODO: We need to use an extra level on the app surface to ensure
3910 // this is always above SurfaceView but always below attached window.
3911 1);
3912 }
3913
3914 // Above we have assigned layers to our children, now we ask them to assign
3915 // layers to their children.
3916 mBelowAppWindowsContainers.assignChildLayers(t);
3917 mTaskStackContainers.assignChildLayers(t);
3918 mAboveAppWindowsContainers.assignChildLayers(t);
3919 mImeWindowsContainers.assignChildLayers(t);
3920 }
3921
3922 /**
3923 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
3924 * that the IME target is one of the docked applications. We'd like the docked divider to be
3925 * above both of the applications, and we'd like the IME to be above the docked divider.
3926 * However we need child windows of the applications to be above the IME (Text drag handles).
3927 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
3928 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
3929 * with {@link #WindowState#assignLayer}
3930 */
3931 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
3932 t.setRelativeLayer(child.getSurfaceControl(), mImeWindowsContainers.getSurfaceControl(), 1);
3933 }
3934
3935 @Override
3936 void destroyAfterPendingTransaction(SurfaceControl surface) {
3937 mPendingDestroyingSurfaces.add(surface);
3938 }
3939
3940 /**
3941 * Destroys any surfaces that have been put into the pending list with
3942 * {@link #destroyAfterTransaction}.
3943 */
3944 void onPendingTransactionApplied() {
3945 for (int i = mPendingDestroyingSurfaces.size() - 1; i >= 0; i--) {
3946 mPendingDestroyingSurfaces.get(i).destroy();
3947 }
3948 mPendingDestroyingSurfaces.clear();
3949 }
Craig Mautner59c00972012-07-30 12:10:24 -07003950}