blob: 056ece3da1e866f65723185a3cfd5c4fd8a9a7e7 [file] [log] [blame]
Craig Mautner59c00972012-07-30 12:10:24 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
Wale Ogunwale30e441d2017-11-09 08:28:45 -080019import static android.app.ActivityManager.SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT;
Wale Ogunwale61911492017-10-11 08:50:50 -070020import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070021import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070022import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwale687b4272017-07-27 02:56:23 -070023import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070024import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
25import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070026import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070027import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
28import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale687b4272017-07-27 02:56:23 -070029import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
30import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070031import static android.view.Display.DEFAULT_DISPLAY;
32import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070033import static android.view.Surface.ROTATION_0;
34import static android.view.Surface.ROTATION_180;
35import static android.view.Surface.ROTATION_270;
36import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070037import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070038import static android.view.WindowManager.DOCKED_BOTTOM;
39import static android.view.WindowManager.DOCKED_INVALID;
40import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080041import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
42import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
43import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070044import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070045import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070046import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
47import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070048import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070049import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070050import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070051import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Wale Ogunwale494009b82016-10-21 09:01:38 -070052import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070053import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070054import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
55import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070056import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070057import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
58import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070059import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070060import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Adrian Roose99bc052017-11-20 17:55:31 +010061import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
62import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
63import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
64import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Jorim Jaggi75520d52017-08-24 17:23:19 +020065import static com.android.server.wm.AppTransition.TRANSIT_KEYGUARD_UNOCCLUDE;
Wale Ogunwale1666e312016-12-16 11:27:18 -080066import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070067import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070069import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
70import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwalec69694a2016-10-18 13:51:15 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070076import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070077import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070078import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080079import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070080import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070081import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070082import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070083import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070084import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070085import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080086import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -070087import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070088import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
89import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070090import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070091import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -070092import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
93import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070094import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER;
95import static com.android.server.wm.WindowManagerService.TYPE_LAYER_OFFSET;
96import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -070097import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070098import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070099import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700100import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700101import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700102import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700103import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700104import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700105import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700106import static com.android.server.wm.proto.DisplayProto.ABOVE_APP_WINDOWS;
107import static com.android.server.wm.proto.DisplayProto.BELOW_APP_WINDOWS;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000108import static com.android.server.wm.proto.DisplayProto.DISPLAY_FRAMES;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700109import static com.android.server.wm.proto.DisplayProto.DISPLAY_INFO;
110import static com.android.server.wm.proto.DisplayProto.DOCKED_STACK_DIVIDER_CONTROLLER;
111import static com.android.server.wm.proto.DisplayProto.DPI;
112import static com.android.server.wm.proto.DisplayProto.ID;
113import static com.android.server.wm.proto.DisplayProto.IME_WINDOWS;
114import static com.android.server.wm.proto.DisplayProto.PINNED_STACK_CONTROLLER;
Steven Timotiusf2d68892017-08-28 17:00:01 -0700115import static com.android.server.wm.proto.DisplayProto.ROTATION;
116import static com.android.server.wm.proto.DisplayProto.SCREEN_ROTATION_ANIMATION;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700117import static com.android.server.wm.proto.DisplayProto.STACKS;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700118import static com.android.server.wm.proto.DisplayProto.WINDOW_CONTAINER;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700119
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700120import android.annotation.CallSuper;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700121import android.annotation.NonNull;
Dean Harding3e5a1522017-10-06 09:19:01 -0700122import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700123import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700124import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700125import android.graphics.Bitmap;
Jorim Jaggi6a7a8592017-01-12 00:44:33 +0100126import android.graphics.GraphicBuffer;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700127import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800128import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700129import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700130import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100131import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700132import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700133import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700134import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700135import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700136import android.os.RemoteException;
137import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100138import android.os.Trace;
Chong Zhang8e89b312015-09-09 15:09:30 -0700139import android.util.DisplayMetrics;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800140import android.util.MutableBoolean;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700141import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700142import android.util.proto.ProtoOutputStream;
Robert Carr44643c12017-09-27 14:57:38 -0700143import android.view.animation.Transformation;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700144import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700145import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700146import android.view.InputDevice;
Robert Carrb1579c82017-09-05 14:54:47 -0700147import android.view.MagnificationSpec;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700148import android.view.Surface;
149import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100150import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700151import android.view.SurfaceSession;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700152
Bryce Lee48f4b572017-04-10 10:54:15 -0700153import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800154import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700155import com.android.internal.view.IInputMethodClient;
Adrian Roose99bc052017-11-20 17:55:31 +0100156import com.android.server.policy.WindowManagerPolicy;
Craig Mautner59c00972012-07-30 12:10:24 -0700157
Robert Carr3b716242016-08-16 16:02:21 -0700158import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700159import java.io.PrintWriter;
160import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700161import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700162import java.util.HashMap;
163import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700164import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700165import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800166import java.util.function.Consumer;
167import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700168
Craig Mautner59c00972012-07-30 12:10:24 -0700169/**
170 * Utility class for keeping track of the WindowStates and other pertinent contents of a
171 * particular Display.
172 *
173 * IMPORTANT: No method from this class should ever be used without holding
174 * WindowManagerService.mWindowMap.
175 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700176class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700177 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700178
179 /** Unique identifier of this stack. */
180 private final int mDisplayId;
181
Wale Ogunwale3a931692016-11-02 16:49:48 -0700182 /** The containers below are the only child containers the display can have. */
183 // Contains all window containers that are related to apps (Activities)
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100184 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700185 // Contains all non-app window containers that should be displayed above the app containers
186 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800187 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100188 new AboveAppWindowContainers("mAboveAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700189 // Contains all non-app window containers that should be displayed below the app containers
190 // (e.g. Wallpaper).
191 private final NonAppWindowContainers mBelowAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100192 new NonAppWindowContainers("mBelowAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700193 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
194 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
195 // window containers together and move them in-sync if/when needed.
196 private final NonAppWindowContainers mImeWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100197 new NonAppWindowContainers("mImeWindowsContainers", mService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700198
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000199 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800200 private WindowState mTmpWindow2;
201 private WindowAnimator mTmpWindowAnimator;
202 private boolean mTmpRecoveringMemory;
203 private boolean mUpdateImeTarget;
204 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700205 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700206
Wale Ogunwale02319a62016-09-26 15:21:22 -0700207 // Mapping from a token IBinder to a WindowToken object on this display.
208 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
209
Andrii Kuliancd097992017-03-23 18:31:59 -0700210 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700211 int mInitialDisplayWidth = 0;
212 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700213 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700214
215 /**
216 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
217 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
218 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
219 */
Craig Mautner59c00972012-07-30 12:10:24 -0700220 int mBaseDisplayWidth = 0;
221 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700222 /**
223 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
224 * but can be set from Settings or via shell command "adb shell wm density".
225 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
226 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700227 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700228 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700229 private final DisplayInfo mDisplayInfo = new DisplayInfo();
230 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700231 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000232 DisplayFrames mDisplayFrames;
233
Andrii Kulian06d07d62017-03-14 11:11:47 -0700234 /**
235 * For default display it contains real metrics, empty for others.
236 * @see WindowManagerService#createWatermarkInTransaction()
237 */
238 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
239 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
240 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700241
Andrii Kulian06d07d62017-03-14 11:11:47 -0700242 /**
243 * Compat metrics computed based on {@link #mDisplayMetrics}.
244 * @see #updateDisplayAndOrientation(int)
245 */
246 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
247
248 /** The desired scaling factor for compatible apps. */
249 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700250
Andrii Kulian8ee72852017-03-10 10:36:45 -0800251 /**
252 * Current rotation of the display.
253 * Constants as per {@link android.view.Surface.Rotation}.
254 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700255 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800256 */
257 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700258
Andrii Kulian8ee72852017-03-10 10:36:45 -0800259 /**
260 * Last applied orientation of the display.
261 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
262 *
263 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
264 */
265 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700266
Andrii Kulian8ee72852017-03-10 10:36:45 -0800267 /**
268 * Flag indicating that the application is receiving an orientation that has different metrics
269 * than it expected. E.g. Portrait instead of Landscape.
270 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700271 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800272 */
273 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700274
Andrii Kulian8ee72852017-03-10 10:36:45 -0800275 /**
276 * Orientation forced by some window. If there is no visible window that specifies orientation
277 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
278 *
279 * @see NonAppWindowContainers#getOrientation()
280 */
281 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700282
Andrii Kulian8ee72852017-03-10 10:36:45 -0800283 /**
284 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
285 * occluded.
286 *
287 * @see NonAppWindowContainers#getOrientation()
288 */
289 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
290
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700291 /**
292 * Keep track of wallpaper visibility to notify changes.
293 */
294 private boolean mLastWallpaperVisible = false;
295
Andrii Kulian06d07d62017-03-14 11:11:47 -0700296 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700297
Craig Mautner39834192012-09-02 07:47:24 -0700298 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700299 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700300 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700301 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800302 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700303
Craig Mautnerdc548482014-02-05 13:35:24 -0800304 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700305 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800306
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700307 /** Detect user tapping outside of current focused task bounds .*/
308 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700309
Craig Mautner6601b7b2013-04-29 10:29:11 -0700310 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700311 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700312
Craig Mautner6601b7b2013-04-29 10:29:11 -0700313 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800314 private final Rect mTmpRect = new Rect();
315 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700316 private final RectF mTmpRectF = new RectF();
317 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800318 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700319
Bryce Leef3c6a472017-11-14 14:53:06 -0800320 /** Used for handing back size of display */
321 private final Rect mTmpBounds = new Rect();
322
Craig Mautner95da1082014-02-24 17:54:35 -0800323 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700324 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800325
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700326 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700327 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700328
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800329 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
330
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000331 private boolean mHaveBootMsg = false;
332 private boolean mHaveApp = false;
333 private boolean mHaveWallpaper = false;
334 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700335
336 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
337
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700338 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
339 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000340 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
341 new ApplySurfaceChangesTransactionState();
342 private final ScreenshotApplicationState mScreenshotApplicationState =
343 new ScreenshotApplicationState();
Jorim Jaggia5e10572017-11-15 14:36:26 +0100344 private final Transaction mTmpTransaction = new Transaction();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700345
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700346 // True if this display is in the process of being removed. Used to determine if the removal of
347 // the display's direct children should be allowed.
348 private boolean mRemovingDisplay = false;
349
Bryce Leed1871262017-06-12 14:12:29 -0700350 // {@code false} if this display is in the processing of being created.
351 private boolean mDisplayReady = false;
352
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800353 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700354 int mInputMethodAnimLayerAdjustment;
355
Robert Carrb1579c82017-09-05 14:54:47 -0700356 private final SurfaceSession mSession = new SurfaceSession();
357
358 /**
359 * We organize all top-level Surfaces in to the following layers.
360 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800361 * and omitted from Screen-Magnification, for example the strict mode flash or
362 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700363 * {@link #mWindowingLayer} contains everything else.
364 */
365 private SurfaceControl mOverlayLayer;
366
367 /**
368 * See {@link #mOverlayLayer}
369 */
370 private SurfaceControl mWindowingLayer;
371
372 /**
373 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
374 * <p>
375 * For these surfaces currently we use a surface based on the larger of width or height so we
376 * don't have to resize when rotating the display.
377 */
378 private int mSurfaceSize;
379
380 /**
381 * A list of surfaces to be destroyed after {@link #mPendingTransaction} is applied.
382 */
383 private final ArrayList<SurfaceControl> mPendingDestroyingSurfaces = new ArrayList<>();
384
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100385 /** Temporary float array to retrieve 3x3 matrix values. */
386 private final float[] mTmpFloats = new float[9];
387
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800388 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
389 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800390 final AppWindowToken atoken = w.mAppToken;
391 if (winAnimator.mDrawState == READY_TO_SHOW) {
392 if (atoken == null || atoken.allDrawn) {
393 if (w.performShowLocked()) {
394 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
395 if (DEBUG_LAYOUT_REPEATS) {
396 mService.mWindowPlacerLocked.debugLayoutRepeats(
397 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
398 }
399 }
400 }
401 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800402 };
403
404 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
405 final WindowStateAnimator winAnimator = w.mWinAnimator;
406 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
407 return;
408 }
409
410 final int flags = w.mAttrs.flags;
411
412 // If this window is animating, make a note that we have an animating window and take
413 // care of a request to run a detached wallpaper animation.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100414 if (winAnimator.isAnimationSet()) {
415 final AnimationAdapter anim = w.getAnimation();
416 if (anim != null) {
417 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && anim.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800418 mTmpWindow = w;
419 }
Jorim Jaggia5e10572017-11-15 14:36:26 +0100420 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800421 if (color != 0) {
422 final TaskStack stack = w.getStack();
423 if (stack != null) {
424 stack.setAnimationBackground(winAnimator, color);
425 }
426 }
427 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800428 }
429
430 // If this window's app token is running a detached wallpaper animation, make a note so
431 // we can ensure the wallpaper is displayed behind it.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200432 final AppWindowToken atoken = winAnimator.mWin.mAppToken;
433 final AnimationAdapter animation = atoken != null ? atoken.getAnimation() : null;
434 if (animation != null) {
435 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && animation.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800436 mTmpWindow = w;
437 }
438
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200439 final int color = animation.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800440 if (color != 0) {
441 final TaskStack stack = w.getStack();
442 if (stack != null) {
443 stack.setAnimationBackground(winAnimator, color);
444 }
445 }
446 }
447 };
448
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800449 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
450 final int lostFocusUid = mTmpWindow.mOwnerUid;
451 final Handler handler = mService.mH;
452 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
453 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
454 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
455 w.mAttrs.hideTimeoutMilliseconds);
456 }
457 }
458 };
459
460 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
461 final AppWindowToken focusedApp = mService.mFocusedApp;
462 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
463 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
464
465 if (!w.canReceiveKeys()) {
466 return false;
467 }
468
469 final AppWindowToken wtoken = w.mAppToken;
470
471 // If this window's application has been removed, just skip it.
472 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
473 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
474 + (wtoken.removed ? "removed" : "sendingToBottom"));
475 return false;
476 }
477
478 if (focusedApp == null) {
479 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
480 + " using new focus @ " + w);
481 mTmpWindow = w;
482 return true;
483 }
484
485 if (!focusedApp.windowsAreFocusable()) {
486 // Current focused app windows aren't focusable...
487 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
488 + " focusable using new focus @ " + w);
489 mTmpWindow = w;
490 return true;
491 }
492
493 // Descend through all of the app tokens and find the first that either matches
494 // win.mAppToken (return win) or mFocusedApp (return null).
495 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
496 if (focusedApp.compareTo(wtoken) > 0) {
497 // App stack below focused app stack. No focus for you!!!
498 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
499 "findFocusedWindow: Reached focused app=" + focusedApp);
500 mTmpWindow = null;
501 return true;
502 }
503 }
504
505 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
506 mTmpWindow = w;
507 return true;
508 };
509
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800510 private final Consumer<WindowState> mPerformLayout = w -> {
511 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
512 // wasting time and funky changes while a window is animating away.
513 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
514 || w.isGoneForLayoutLw();
515
516 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
517 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
518 + " mLayoutAttached=" + w.mLayoutAttached
519 + " screen changed=" + w.isConfigChanged());
520 final AppWindowToken atoken = w.mAppToken;
521 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200522 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800523 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
524 + " parentHidden=" + w.isParentWindowHidden());
525 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200526 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800527 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
528 + " parentHidden=" + w.isParentWindowHidden());
529 }
530
531 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
532 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
533 // since that means "perform layout as normal, just don't display").
534 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
535 || ((w.isConfigChanged() || w.setReportResizeHints())
536 && !w.isGoneForLayoutLw() &&
537 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
538 (w.mHasSurface && w.mAppToken != null &&
539 w.mAppToken.layoutConfigChanges)))) {
540 if (!w.mLayoutAttached) {
541 if (mTmpInitial) {
542 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
543 w.mContentChanged = false;
544 }
545 if (w.mAttrs.type == TYPE_DREAM) {
546 // Don't layout windows behind a dream, so that if it does stuff like hide
547 // the status bar we won't get a bad transition when it goes away.
548 mTmpWindow = w;
549 }
550 w.mLayoutNeeded = false;
551 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200552 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000553 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800554 w.mLayoutSeq = mService.mLayoutSeq;
555
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200556 // If this is the first layout, we need to initialize the last inset values as
557 // otherwise we'd immediately cause an unnecessary resize.
558 if (firstLayout) {
559 w.updateLastInsetValues();
560 }
561
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800562 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
563 + " mContainingFrame=" + w.mContainingFrame
564 + " mDisplayFrame=" + w.mDisplayFrame);
565 }
566 }
567 };
568
569 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
570 if (w.mLayoutAttached) {
571 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
572 + " mViewVisibility=" + w.mViewVisibility
573 + " mRelayoutCalled=" + w.mRelayoutCalled);
574 // If this view is GONE, then skip it -- keep the current frame, and let the caller
575 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
576 // windows, since that means "perform layout as normal, just don't display").
577 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
578 return;
579 }
580 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
581 || w.mLayoutNeeded) {
582 if (mTmpInitial) {
583 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
584 w.mContentChanged = false;
585 }
586 w.mLayoutNeeded = false;
587 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000588 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800589 w.mLayoutSeq = mService.mLayoutSeq;
590 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
591 + " mContainingFrame=" + w.mContainingFrame
592 + " mDisplayFrame=" + w.mDisplayFrame);
593 }
594 } else if (w.mAttrs.type == TYPE_DREAM) {
595 // Don't layout windows behind a dream, so that if it does stuff like hide the
596 // status bar we won't get a bad transition when it goes away.
597 mTmpWindow = mTmpWindow2;
598 }
599 };
600
601 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
602 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
603 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
604 return w.canBeImeTarget();
605 };
606
607 private final Consumer<WindowState> mApplyPostLayoutPolicy =
608 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
609 mService.mInputMethodTarget);
610
611 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
612 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
613 final boolean obscuredChanged = w.mObscured !=
614 mTmpApplySurfaceChangesTransactionState.obscured;
615 final RootWindowContainer root = mService.mRoot;
616 // Only used if default window
617 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
618
619 // Update effect.
620 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
621 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
622 final boolean isDisplayed = w.isDisplayedLw();
623
624 if (isDisplayed && w.isObscuringDisplay()) {
625 // This window completely covers everything behind it, so we want to leave all
626 // of them as undimmed (for performance reasons).
627 root.mObscuringWindow = w;
628 mTmpApplySurfaceChangesTransactionState.obscured = true;
629 }
630
631 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
632 root.handleNotObscuredLocked(w,
633 mTmpApplySurfaceChangesTransactionState.obscured,
634 mTmpApplySurfaceChangesTransactionState.syswin);
635
636 if (w.mHasSurface && isDisplayed) {
637 final int type = w.mAttrs.type;
638 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
639 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
640 mTmpApplySurfaceChangesTransactionState.syswin = true;
641 }
642 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
643 && w.mAttrs.preferredRefreshRate != 0) {
644 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
645 = w.mAttrs.preferredRefreshRate;
646 }
647 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
648 && w.mAttrs.preferredDisplayModeId != 0) {
649 mTmpApplySurfaceChangesTransactionState.preferredModeId
650 = w.mAttrs.preferredDisplayModeId;
651 }
652 }
653 }
654
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800655 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
656 && mWallpaperController.isWallpaperTarget(w)) {
657 // This is the wallpaper target and its obscured state changed... make sure the
658 // current wallpaper's visibility has been updated accordingly.
659 mWallpaperController.updateWallpaperVisibility();
660 }
661
Jorim Jaggia5e10572017-11-15 14:36:26 +0100662 // Use mTmpTransaction instead of mPendingTransaction because we don't want to commit
663 // other changes in mPendingTransaction at this point.
664 w.handleWindowMovedIfNeeded(mTmpTransaction);
665 SurfaceControl.mergeToGlobalTransaction(mTmpTransaction);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800666
667 final WindowStateAnimator winAnimator = w.mWinAnimator;
668
669 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
670 w.mContentChanged = false;
671
672 // Moved from updateWindowsAndWallpaperLocked().
673 if (w.mHasSurface) {
674 // Take care of the window being ready to display.
675 final boolean committed = winAnimator.commitFinishDrawingLocked();
676 if (isDefaultDisplay && committed) {
677 if (w.mAttrs.type == TYPE_DREAM) {
678 // HACK: When a dream is shown, it may at that point hide the lock screen.
679 // So we need to redo the layout to let the phone window manager make this
680 // happen.
681 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
682 if (DEBUG_LAYOUT_REPEATS) {
683 surfacePlacer.debugLayoutRepeats(
684 "dream and commitFinishDrawingLocked true",
685 pendingLayoutChanges);
686 }
687 }
688 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
689 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
690 "First draw done in potential wallpaper target " + w);
691 root.mWallpaperMayChange = true;
692 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
693 if (DEBUG_LAYOUT_REPEATS) {
694 surfacePlacer.debugLayoutRepeats(
695 "wallpaper and commitFinishDrawingLocked true",
696 pendingLayoutChanges);
697 }
698 }
699 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700700 final TaskStack stack = w.getStack();
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200701 if (!winAnimator.isWaitingForOpening()
Robert Carr2117aaf2017-04-25 14:46:50 -0700702 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800703 // Updates the shown frame before we set up the surface. This is needed
704 // because the resizing could change the top-left position (in addition to
705 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
706 // position the surface.
707 //
708 // If an animation is being started, we can't call this method because the
709 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700710 // we do want to update the position if the window is animating. We make an exception
711 // for the bounds animating state, where an application may have been waiting
712 // for an exit animation to start, but instead enters PiP. We need to ensure
713 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800714 winAnimator.computeShownFrameLocked();
715 }
716 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
Jorim Jaggia5e10572017-11-15 14:36:26 +0100717
718 // Since setSurfaceBoundariesLocked applies the clipping, we need to apply the position
719 // to the surface of the window container as well. Use mTmpTransaction instead of
720 // mPendingTransaction to avoid committing any existing changes in there.
721 w.updateSurfacePosition(mTmpTransaction);
722 SurfaceControl.mergeToGlobalTransaction(mTmpTransaction);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800723 }
724
725 final AppWindowToken atoken = w.mAppToken;
726 if (atoken != null) {
727 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
728 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
729 mTmpUpdateAllDrawn.add(atoken);
730 }
731 }
732
733 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
734 && w.isDisplayedLw()) {
735 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
736 }
737
738 w.updateResizingWindowIfNeeded();
739 };
740
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800741 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800742 * Create new {@link DisplayContent} instance, add itself to the root window container and
743 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700744 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800745 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700746 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
747 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700748 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700749 DisplayContent(Display display, WindowManagerService service,
Robert Carrb1579c82017-09-05 14:54:47 -0700750 WallpaperController wallpaperController) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100751 super(service);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800752 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
753 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
754 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
755 + " new=" + display);
756 }
757
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700758 mDisplay = display;
759 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700760 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700761 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700762 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700763 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000764 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700765 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800766 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700767 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700768
Robert Carrb1579c82017-09-05 14:54:47 -0700769 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth);
770
771 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
772 .setSize(mSurfaceSize, mSurfaceSize)
773 .setOpaque(true);
774 mWindowingLayer = b.setName("Display Root").build();
775 mOverlayLayer = b.setName("Display Overlays").build();
776
Robert Carrf59b8dd2017-10-02 18:58:36 -0700777 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700778 .setLayerStack(mWindowingLayer, mDisplayId)
779 .show(mWindowingLayer)
780 .setLayer(mOverlayLayer, 1)
781 .setLayerStack(mOverlayLayer, mDisplayId)
782 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700783 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700784
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700785 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700786 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700787 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700788 super.addChild(mAboveAppWindowsContainers, null);
789 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800790
791 // Add itself as a child to the root container.
792 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700793
794 // TODO(b/62541591): evaluate whether this is the best spot to declare the
795 // {@link DisplayContent} ready for use.
796 mDisplayReady = true;
797 }
798
799 boolean isReady() {
800 // The display is ready when the system and the individual display are both ready.
801 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700802 }
803
804 int getDisplayId() {
805 return mDisplayId;
806 }
807
Wale Ogunwale02319a62016-09-26 15:21:22 -0700808 WindowToken getWindowToken(IBinder binder) {
809 return mTokenMap.get(binder);
810 }
811
812 AppWindowToken getAppWindowToken(IBinder binder) {
813 final WindowToken token = getWindowToken(binder);
814 if (token == null) {
815 return null;
816 }
817 return token.asAppWindowToken();
818 }
819
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700820 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700821 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
822 if (dc != null) {
823 // We currently don't support adding a window token to the display if the display
824 // already has the binder mapped to another token. If there is a use case for supporting
825 // this moving forward we will either need to merge the WindowTokens some how or have
826 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700827 throw new IllegalArgumentException("Can't map token=" + token + " to display="
828 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700829 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700830 if (binder == null) {
831 throw new IllegalArgumentException("Can't map token=" + token + " to display="
832 + getName() + " binder is null");
833 }
834 if (token == null) {
835 throw new IllegalArgumentException("Can't map null token to display="
836 + getName() + " binder=" + binder);
837 }
838
Wale Ogunwale02319a62016-09-26 15:21:22 -0700839 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700840
841 if (token.asAppWindowToken() == null) {
842 // Add non-app token to container hierarchy on the display. App tokens are added through
843 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700844 switch (token.windowType) {
845 case TYPE_WALLPAPER:
846 mBelowAppWindowsContainers.addChild(token);
847 break;
848 case TYPE_INPUT_METHOD:
849 case TYPE_INPUT_METHOD_DIALOG:
850 mImeWindowsContainers.addChild(token);
851 break;
852 default:
853 mAboveAppWindowsContainers.addChild(token);
854 break;
855 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700856 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700857 }
858
859 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700860 final WindowToken token = mTokenMap.remove(binder);
861 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800862 token.setExiting();
863 }
864 return token;
865 }
866
867 /** Changes the display the input window token is housed on to this one. */
868 void reParentWindowToken(WindowToken token) {
869 final DisplayContent prevDc = token.getDisplayContent();
870 if (prevDc == this) {
871 return;
872 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800873 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
874 && token.asAppWindowToken() == null) {
875 // Removed the token from the map, but made sure it's not an app token before removing
876 // from parent.
877 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700878 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800879
880 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700881 }
882
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700883 void removeAppToken(IBinder binder) {
884 final WindowToken token = removeWindowToken(binder);
885 if (token == null) {
886 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
887 return;
888 }
889
890 final AppWindowToken appToken = token.asAppWindowToken();
891
892 if (appToken == null) {
893 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
894 return;
895 }
896
897 appToken.onRemovedFromDisplay();
898 }
899
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700900 Display getDisplay() {
901 return mDisplay;
902 }
903
Craig Mautner59c00972012-07-30 12:10:24 -0700904 DisplayInfo getDisplayInfo() {
905 return mDisplayInfo;
906 }
907
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700908 DisplayMetrics getDisplayMetrics() {
909 return mDisplayMetrics;
910 }
911
Andrii Kulian8ee72852017-03-10 10:36:45 -0800912 int getRotation() {
913 return mRotation;
914 }
915
916 void setRotation(int newRotation) {
917 mRotation = newRotation;
918 }
919
920 int getLastOrientation() {
921 return mLastOrientation;
922 }
923
924 void setLastOrientation(int orientation) {
925 mLastOrientation = orientation;
926 }
927
928 boolean getAltOrientation() {
929 return mAltOrientation;
930 }
931
932 void setAltOrientation(boolean altOrientation) {
933 mAltOrientation = altOrientation;
934 }
935
936 int getLastWindowForcedOrientation() {
937 return mLastWindowForcedOrientation;
938 }
939
Andrii Kulian06d07d62017-03-14 11:11:47 -0700940 /**
941 * Update rotation of the display.
942 *
943 * Returns true if the rotation has been changed. In this case YOU MUST CALL
944 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
945 */
946 boolean updateRotationUnchecked(boolean inTransaction) {
947 if (mService.mDeferredRotationPauseCount > 0) {
948 // Rotation updates have been paused temporarily. Defer the update until
949 // updates have been resumed.
950 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
951 return false;
952 }
953
954 ScreenRotationAnimation screenRotationAnimation =
955 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
956 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
957 // Rotation updates cannot be performed while the previous rotation change
958 // animation is still in progress. Skip this update. We will try updating
959 // again after the animation is finished and the display is unfrozen.
960 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
961 return false;
962 }
963 if (mService.mDisplayFrozen) {
964 // Even if the screen rotation animation has finished (e.g. isAnimating
965 // returns false), there is still some time where we haven't yet unfrozen
966 // the display. We also need to abort rotation here.
967 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
968 "Deferring rotation, still finishing previous rotation");
969 return false;
970 }
971
972 if (!mService.mDisplayEnabled) {
973 // No point choosing a rotation if the display is not enabled.
974 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
975 return false;
976 }
977
978 final int oldRotation = mRotation;
979 final int lastOrientation = mLastOrientation;
980 final boolean oldAltOrientation = mAltOrientation;
981 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr0e007272017-10-02 13:00:55 -0700982 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
Robert Carr897215d2017-03-29 12:25:50 -0700983 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700984
Robert Carr0e007272017-10-02 13:00:55 -0700985 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700986 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
987 if (seamlessRotated != null) {
988 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
989 // to complete (that is, waiting for windows to redraw). It's tempting to check
990 // w.mSeamlessRotationCount but that could be incorrect in the case of
991 // window-removal.
992 return false;
993 }
Robert Carr0e007272017-10-02 13:00:55 -0700994
995 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700996 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -0700997 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -0700998 mayRotateSeamlessly = false;
999 }
1000 for (int i = 0; i < mService.mSessions.size(); i++) {
1001 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1002 mayRotateSeamlessly = false;
1003 break;
1004 }
1005 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001006 }
Robert Carr0e007272017-10-02 13:00:55 -07001007 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001008
1009 // TODO: Implement forced rotation changes.
1010 // Set mAltOrientation to indicate that the application is receiving
1011 // an orientation that has different metrics than it expected.
1012 // eg. Portrait instead of Landscape.
1013
1014 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
1015 lastOrientation, rotation);
1016
1017 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
1018 + ", got rotation " + rotation + " which has "
1019 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1020
1021 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1022 // No change.
1023 return false;
1024 }
1025
1026 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
1027 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1028 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1029
1030 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1031 mService.mWaitingForConfig = true;
1032 }
1033
1034 mRotation = rotation;
1035 mAltOrientation = altOrientation;
1036 if (isDefaultDisplay) {
1037 mService.mPolicy.setRotationLw(rotation);
1038 }
1039
1040 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1041 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1042 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1043 WINDOW_FREEZE_TIMEOUT_DURATION);
1044
1045 setLayoutNeeded();
1046 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001047 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001048
1049 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001050 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001051 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1052 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1053 mDisplayId);
1054 } else {
1055 // The screen rotation animation uses a screenshot to freeze the screen
1056 // while windows resize underneath.
1057 // When we are rotating seamlessly, we allow the elements to transition
1058 // to their rotated state independently and without a freeze required.
1059 screenRotationAnimation = null;
1060
1061 // We have to reset this in case a window was removed before it
1062 // finished seamless rotation.
1063 mService.mSeamlessRotationCount = 0;
1064 }
1065
1066 // We need to update our screen size information to match the new rotation. If the rotation
1067 // has actually changed then this method will return true and, according to the comment at
1068 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1069 // By updating the Display info here it will be available to
1070 // #computeScreenConfiguration() later.
1071 updateDisplayAndOrientation(getConfiguration().uiMode);
1072
1073 if (!inTransaction) {
1074 if (SHOW_TRANSACTIONS) {
1075 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1076 }
1077 mService.openSurfaceTransaction();
1078 }
1079 try {
1080 // NOTE: We disable the rotation in the emulator because
1081 // it doesn't support hardware OpenGL emulation yet.
1082 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1083 && screenRotationAnimation.hasScreenshot()) {
Robert Carrb1579c82017-09-05 14:54:47 -07001084 if (screenRotationAnimation.setRotationInTransaction(rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001085 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1086 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1087 mService.scheduleAnimationLocked();
1088 }
1089 }
1090
1091 if (rotateSeamlessly) {
1092 forAllWindows(w -> {
1093 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1094 }, true /* traverseTopToBottom */);
1095 }
1096
1097 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1098 } finally {
1099 if (!inTransaction) {
Adrian Roos111aff92017-09-27 18:11:46 +02001100 mService.closeSurfaceTransaction("setRotationUnchecked");
Andrii Kulian06d07d62017-03-14 11:11:47 -07001101 if (SHOW_LIGHT_TRANSACTIONS) {
1102 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1103 }
1104 }
1105 }
1106
1107 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001108 if (w.mHasSurface && !rotateSeamlessly) {
1109 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001110 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001111 mService.mRoot.mOrientationChangeComplete = false;
1112 w.mLastFreezeDuration = 0;
1113 }
1114 w.mReportOrientationChanged = true;
1115 }, true /* traverseTopToBottom */);
1116
1117 if (rotateSeamlessly) {
1118 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1119 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1120 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1121 }
1122
1123 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1124 final WindowManagerService.RotationWatcher rotationWatcher
1125 = mService.mRotationWatchers.get(i);
1126 if (rotationWatcher.mDisplayId == mDisplayId) {
1127 try {
1128 rotationWatcher.mWatcher.onRotationChanged(rotation);
1129 } catch (RemoteException e) {
1130 // Ignore
1131 }
1132 }
1133 }
1134
1135 // TODO (multi-display): Magnification is supported only for the default display.
1136 // Announce rotation only if we will not animate as we already have the
1137 // windows in final state. Otherwise, we make this call at the rotation end.
1138 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1139 && isDefaultDisplay) {
1140 mService.mAccessibilityController.onRotationChangedLocked(this);
1141 }
1142
1143 return true;
1144 }
1145
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001146 void configureDisplayPolicy() {
1147 mService.mPolicy.setInitialDisplaySize(getDisplay(),
1148 mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1149
1150 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
1151 }
1152
Andrii Kulian06d07d62017-03-14 11:11:47 -07001153 /**
1154 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1155 * changed.
1156 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1157 */
1158 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1159 // Use the effective "visual" dimensions based on current rotation
1160 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1161 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1162 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1163 int dw = realdw;
1164 int dh = realdh;
1165
1166 if (mAltOrientation) {
1167 if (realdw > realdh) {
1168 // Turn landscape into portrait.
1169 int maxw = (int)(realdh/1.3f);
1170 if (maxw < realdw) {
1171 dw = maxw;
1172 }
1173 } else {
1174 // Turn portrait into landscape.
1175 int maxh = (int)(realdw/1.3f);
1176 if (maxh < realdh) {
1177 dh = maxh;
1178 }
1179 }
1180 }
1181
1182 // Update application display metrics.
1183 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1184 mDisplayId);
1185 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1186 mDisplayId);
1187 mDisplayInfo.rotation = mRotation;
1188 mDisplayInfo.logicalWidth = dw;
1189 mDisplayInfo.logicalHeight = dh;
1190 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1191 mDisplayInfo.appWidth = appWidth;
1192 mDisplayInfo.appHeight = appHeight;
1193 if (isDefaultDisplay) {
1194 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1195 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1196 }
1197 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1198 if (mDisplayScalingDisabled) {
1199 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1200 } else {
1201 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1202 }
1203
1204 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1205 mDisplayInfo);
1206
1207 mBaseDisplayRect.set(0, 0, dw, dh);
1208
1209 if (isDefaultDisplay) {
1210 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1211 mCompatDisplayMetrics);
1212 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001213
1214 updateBounds();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001215 return mDisplayInfo;
1216 }
1217
1218 /**
1219 * Compute display configuration based on display properties and policy settings.
1220 * Do not call if mDisplayReady == false.
1221 */
1222 void computeScreenConfiguration(Configuration config) {
1223 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1224
1225 final int dw = displayInfo.logicalWidth;
1226 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001227 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1228 // TODO: Probably best to set this based on some setting in the display content object,
1229 // so the display can be configured for things like fullscreen.
1230 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001231
Andrii Kulian06d07d62017-03-14 11:11:47 -07001232 config.screenWidthDp =
1233 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1234 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1235 config.screenHeightDp =
1236 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1237 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001238
1239 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1240 final int leftInset = mTmpRect.left;
1241 final int topInset = mTmpRect.top;
1242 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001243 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1244 leftInset + displayInfo.appWidth /* right */,
1245 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001246 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1247 || displayInfo.rotation == Surface.ROTATION_270);
1248
1249 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1250 mDisplayMetrics.density, config);
1251
1252 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1253 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1254 ? Configuration.SCREENLAYOUT_ROUND_YES
1255 : Configuration.SCREENLAYOUT_ROUND_NO);
1256
1257 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1258 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1259 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1260 dh, mDisplayId);
1261 config.densityDpi = displayInfo.logicalDensityDpi;
1262
1263 config.colorMode =
1264 (displayInfo.isHdr()
1265 ? Configuration.COLOR_MODE_HDR_YES
1266 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001267 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001268 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1269 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1270
1271 // Update the configuration based on available input devices, lid switch,
1272 // and platform configuration.
1273 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1274 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1275 config.navigation = Configuration.NAVIGATION_NONAV;
1276
1277 int keyboardPresence = 0;
1278 int navigationPresence = 0;
1279 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1280 final int len = devices != null ? devices.length : 0;
1281 for (int i = 0; i < len; i++) {
1282 InputDevice device = devices[i];
1283 if (!device.isVirtual()) {
1284 final int sources = device.getSources();
1285 final int presenceFlag = device.isExternal() ?
1286 WindowManagerPolicy.PRESENCE_EXTERNAL :
1287 WindowManagerPolicy.PRESENCE_INTERNAL;
1288
1289 // TODO(multi-display): Configure on per-display basis.
1290 if (mService.mIsTouchDevice) {
1291 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1292 InputDevice.SOURCE_TOUCHSCREEN) {
1293 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1294 }
1295 } else {
1296 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1297 }
1298
1299 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1300 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1301 navigationPresence |= presenceFlag;
1302 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1303 && config.navigation == Configuration.NAVIGATION_NONAV) {
1304 config.navigation = Configuration.NAVIGATION_DPAD;
1305 navigationPresence |= presenceFlag;
1306 }
1307
1308 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1309 config.keyboard = Configuration.KEYBOARD_QWERTY;
1310 keyboardPresence |= presenceFlag;
1311 }
1312 }
1313 }
1314
1315 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1316 config.navigation = Configuration.NAVIGATION_DPAD;
1317 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1318 }
1319
1320 // Determine whether a hard keyboard is available and enabled.
1321 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1322 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1323 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1324 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1325 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1326 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1327 }
1328
1329 // Let the policy update hidden states.
1330 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1331 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1332 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1333 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1334 }
1335
1336 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1337 int displayId) {
1338 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1339 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1340 final int unrotDw, unrotDh;
1341 if (rotated) {
1342 unrotDw = dh;
1343 unrotDh = dw;
1344 } else {
1345 unrotDw = dw;
1346 unrotDh = dh;
1347 }
1348 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1349 displayId);
1350 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1351 displayId);
1352 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1353 displayId);
1354 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1355 displayId);
1356 return sw;
1357 }
1358
1359 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1360 DisplayMetrics dm, int dw, int dh, int displayId) {
1361 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1362 displayId);
1363 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1364 uiMode, displayId);
1365 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1366 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1367 if (curSize == 0 || size < curSize) {
1368 curSize = size;
1369 }
1370 return curSize;
1371 }
1372
1373 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1374 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1375
1376 // We need to determine the smallest width that will occur under normal
1377 // operation. To this, start with the base screen size and compute the
1378 // width under the different possible rotations. We need to un-rotate
1379 // the current screen dimensions before doing this.
1380 int unrotDw, unrotDh;
1381 if (rotated) {
1382 unrotDw = dh;
1383 unrotDh = dw;
1384 } else {
1385 unrotDw = dw;
1386 unrotDh = dh;
1387 }
1388 displayInfo.smallestNominalAppWidth = 1<<30;
1389 displayInfo.smallestNominalAppHeight = 1<<30;
1390 displayInfo.largestNominalAppWidth = 0;
1391 displayInfo.largestNominalAppHeight = 0;
1392 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1393 unrotDh);
1394 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1395 unrotDw);
1396 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1397 unrotDh);
1398 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1399 unrotDw);
1400 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1401 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1402 displayId);
1403 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1404 displayId);
1405 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1406 displayId);
1407 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1408 displayId);
1409 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1410 outConfig.screenLayout = sl;
1411 }
1412
1413 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1414 int uiMode, int displayId) {
1415 // Get the app screen size at this rotation.
1416 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1417 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1418
1419 // Compute the screen layout size class for this rotation.
1420 int longSize = w;
1421 int shortSize = h;
1422 if (longSize < shortSize) {
1423 int tmp = longSize;
1424 longSize = shortSize;
1425 shortSize = tmp;
1426 }
1427 longSize = (int)(longSize/density);
1428 shortSize = (int)(shortSize/density);
1429 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1430 }
1431
1432 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1433 int uiMode, int dw, int dh) {
1434 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1435 displayId);
1436 if (width < displayInfo.smallestNominalAppWidth) {
1437 displayInfo.smallestNominalAppWidth = width;
1438 }
1439 if (width > displayInfo.largestNominalAppWidth) {
1440 displayInfo.largestNominalAppWidth = width;
1441 }
1442 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1443 displayId);
1444 if (height < displayInfo.smallestNominalAppHeight) {
1445 displayInfo.smallestNominalAppHeight = height;
1446 }
1447 if (height > displayInfo.largestNominalAppHeight) {
1448 displayInfo.largestNominalAppHeight = height;
1449 }
1450 }
1451
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001452 DockedStackDividerController getDockedDividerController() {
1453 return mDividerControllerLocked;
1454 }
1455
Winson Chung655332c2016-10-31 13:14:28 -07001456 PinnedStackController getPinnedStackController() {
1457 return mPinnedStackControllerLocked;
1458 }
1459
Jeff Browna506a6e2013-06-04 00:02:38 -07001460 /**
1461 * Returns true if the specified UID has access to this display.
1462 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001463 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001464 return mDisplay.hasAccess(uid);
1465 }
1466
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001467 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001468 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001469 }
1470
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001471 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001472 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001473 }
1474
Wale Ogunwale61911492017-10-11 08:50:50 -07001475 /**
1476 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1477 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001478 TaskStack getSplitScreenPrimaryStack() {
1479 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001480 return (stack != null && stack.isVisible()) ? stack : null;
1481 }
1482
1483 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001484 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001485 * not visible.
1486 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001487 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1488 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001489 }
1490
1491 TaskStack getPinnedStack() {
1492 return mTaskStackContainers.getPinnedStack();
1493 }
1494
1495 private boolean hasPinnedStack() {
1496 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001497 }
1498
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001499 /**
1500 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1501 * Null is no compatible stack on the display.
1502 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001503 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001504 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1505 }
1506
1507 /**
1508 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1509 * activity type. Null is no compatible stack on the display.
1510 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001511 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001512 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001513 }
1514
Bryce Lee48f4b572017-04-10 10:54:15 -07001515 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001516 TaskStack getTopStack() {
1517 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001518 }
1519
Wale Ogunwale61911492017-10-11 08:50:50 -07001520 void onStackWindowingModeChanged(TaskStack stack) {
1521 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001522 }
1523
Andrii Kulian441e4492016-09-29 15:25:00 -07001524 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001525 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001526 super.onConfigurationChanged(newParentConfig);
1527
Andrii Kulian3a507b52016-09-19 18:14:12 -07001528 // The display size information is heavily dependent on the resources in the current
1529 // configuration, so we need to reconfigure it every time the configuration changes.
1530 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1531 mService.reconfigureDisplayLocked(this);
1532
Bryce Leef3c6a472017-11-14 14:53:06 -08001533 final DockedStackDividerController dividerController = getDockedDividerController();
1534
1535 if (dividerController != null) {
1536 getDockedDividerController().onConfigurationChanged();
1537 }
1538
1539 final PinnedStackController pinnedStackController = getPinnedStackController();
1540
1541 if (pinnedStackController != null) {
1542 getPinnedStackController().onConfigurationChanged();
1543 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001544 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001545
Andrii Kulian441e4492016-09-29 15:25:00 -07001546 /**
1547 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1548 * bounds were updated.
1549 */
1550 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001551 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1552 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001553 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001554 changedStackList.add(stack.mStackId);
1555 }
1556 }
Winson Chung32c566f2017-04-11 18:31:21 -07001557
1558 // If there was no pinned stack, we still need to notify the controller of the display info
1559 // update as a result of the config change. We do this here to consolidate the flow between
1560 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001561 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001562 mPinnedStackControllerLocked.onDisplayInfoChanged();
1563 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001564 }
1565
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001566 @Override
1567 boolean fillsParent() {
1568 return true;
1569 }
1570
1571 @Override
1572 boolean isVisible() {
1573 return true;
1574 }
1575
1576 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001577 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001578 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001579 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001580 }
1581
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001582 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001583 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1584 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1585 // target, or here in order if there isn't an IME target.
1586 if (traverseTopToBottom) {
1587 for (int i = mChildren.size() - 1; i >= 0; --i) {
1588 final DisplayChildWindowContainer child = mChildren.get(i);
1589 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1590 // In this case the Ime windows will be processed above their target so we skip
1591 // here.
1592 continue;
1593 }
1594 if (child.forAllWindows(callback, traverseTopToBottom)) {
1595 return true;
1596 }
1597 }
1598 } else {
1599 final int count = mChildren.size();
1600 for (int i = 0; i < count; i++) {
1601 final DisplayChildWindowContainer child = mChildren.get(i);
1602 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1603 // In this case the Ime windows will be processed above their target so we skip
1604 // here.
1605 continue;
1606 }
1607 if (child.forAllWindows(callback, traverseTopToBottom)) {
1608 return true;
1609 }
1610 }
1611 }
1612 return false;
1613 }
1614
1615 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1616 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1617 }
1618
Wale Ogunwale399c8692017-05-08 14:22:42 -07001619 @Override
1620 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001621 final WindowManagerPolicy policy = mService.mPolicy;
1622
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001623 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001624 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001625 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001626 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001627 // If the display is frozen, some activities may be in the middle of restarting, and
1628 // thus have removed their old window. If the window has the flag to hide the lock
1629 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1630 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001631 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001632 } else if (policy.isKeyguardLocked()) {
1633 // Use the last orientation the while the display is frozen with the keyguard
1634 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1635 // window. We don't want to check the show when locked window directly though as
1636 // things aren't stable while the display is frozen, for example the window could be
1637 // momentarily unavailable due to activity relaunch.
1638 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001639 + "return " + mLastOrientation);
1640 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001641 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001642 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001643 final int orientation = mAboveAppWindowsContainers.getOrientation();
1644 if (orientation != SCREEN_ORIENTATION_UNSET) {
1645 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001646 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001647 }
1648
Wale Ogunwale399c8692017-05-08 14:22:42 -07001649 // Top system windows are not requesting an orientation. Start searching from apps.
1650 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001651 }
1652
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001653 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001654 // Check if display metrics changed and update base values if needed.
1655 updateBaseDisplayMetricsIfNeeded();
1656
Craig Mautner722285e2012-09-07 13:55:58 -07001657 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001658 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001659
Wale Ogunwale61911492017-10-11 08:50:50 -07001660 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1661 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001662 }
Craig Mautner722285e2012-09-07 13:55:58 -07001663 }
1664
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001665 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001666 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1667 if (displayManagerInternal != null) {
1668 // Bootstrap the default logical display from the display manager.
1669 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1670 if (newDisplayInfo != null) {
1671 mDisplayInfo.copyFrom(newDisplayInfo);
1672 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001673 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001674
Andrii Kuliancd097992017-03-23 18:31:59 -07001675 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1676 mDisplayInfo.logicalDensityDpi);
1677 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1678 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1679 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001680 }
1681
Andrii Kuliancd097992017-03-23 18:31:59 -07001682 /**
1683 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1684 * values.
1685 */
1686 private void updateBaseDisplayMetricsIfNeeded() {
1687 // Get real display metrics without overrides from WM.
1688 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1689 final int orientation = mDisplayInfo.rotation;
1690 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1691 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1692 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1693 final int newDensity = mDisplayInfo.logicalDensityDpi;
1694
1695 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1696 || mInitialDisplayHeight != newHeight
1697 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1698
1699 if (displayMetricsChanged) {
1700 // Check if display size or density is forced.
1701 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1702 || mBaseDisplayHeight != mInitialDisplayHeight;
1703 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1704
1705 // If there is an override set for base values - use it, otherwise use new values.
1706 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1707 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1708 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1709
1710 // Real display metrics changed, so we should also update initial values.
1711 mInitialDisplayWidth = newWidth;
1712 mInitialDisplayHeight = newHeight;
1713 mInitialDisplayDensity = newDensity;
1714 mService.reconfigureDisplayLocked(this);
1715 }
1716 }
1717
Bryce Lee27cec322017-03-21 09:41:37 -07001718 /** Sets the maximum width the screen resolution can be */
1719 void setMaxUiWidth(int width) {
1720 if (DEBUG_DISPLAY) {
1721 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1722 }
1723
1724 mMaxUiWidth = width;
1725
1726 // Update existing metrics.
1727 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1728 }
1729
1730 /** Update base (override) display metrics. */
1731 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1732 mBaseDisplayWidth = baseWidth;
1733 mBaseDisplayHeight = baseHeight;
1734 mBaseDisplayDensity = baseDensity;
1735
1736 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1737 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1738 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1739 mBaseDisplayWidth = mMaxUiWidth;
1740
1741 if (DEBUG_DISPLAY) {
1742 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1743 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1744 + " on display:" + getDisplayId());
1745 }
1746 }
1747
1748 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08001749
1750 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07001751 }
1752
Wale Ogunwaledb506192017-12-08 10:57:32 -08001753 void getStableRect(Rect out) {
1754 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08001755 }
1756
Wale Ogunwale61911492017-10-11 08:50:50 -07001757 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001758 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1759 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001760
Wale Ogunwale61911492017-10-11 08:50:50 -07001761 final TaskStack stack = new TaskStack(mService, stackId, controller);
1762 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001763 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001764 }
1765
Andrii Kulian51c1b672017-04-07 18:39:32 -07001766 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001767 final DisplayContent prevDc = stack.getDisplayContent();
1768 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001769 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1770 + " which is not currently attached to any display");
1771 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001772 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001773 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1774 + " to its current displayId=" + mDisplayId);
1775 }
1776
Wale Ogunwale61911492017-10-11 08:50:50 -07001777 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001778 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001779 }
1780
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001781 @Override
1782 protected void addChild(DisplayChildWindowContainer child,
1783 Comparator<DisplayChildWindowContainer> comparator) {
1784 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1785 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001786
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001787 @Override
1788 protected void addChild(DisplayChildWindowContainer child, int index) {
1789 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1790 }
1791
1792 @Override
1793 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001794 // Only allow removal of direct children from this display if the display is in the process
1795 // of been removed.
1796 if (mRemovingDisplay) {
1797 super.removeChild(child);
1798 return;
1799 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001800 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001801 }
1802
Andrii Kuliand2765632016-12-12 22:26:34 -08001803 @Override
1804 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1805 // Children of the display are statically ordered, so the real intention here is to perform
1806 // the operation on the display and not the static direct children.
1807 getParent().positionChildAt(position, this, includingParents);
1808 }
1809
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001810 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001811 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1812 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001813 final int taskId = stack.taskIdFromPoint(x, y);
1814 if (taskId != -1) {
1815 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001816 }
1817 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001818 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001819 }
1820
Chong Zhang8e89b312015-09-09 15:09:30 -07001821 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001822 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001823 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001824 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001825 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001826 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001827 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07001828 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1829 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001830 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001831 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001832 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001833
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001834 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1835 if (mTmpTaskForResizePointSearchResult.searchDone) {
1836 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001837 }
1838 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001839 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001840 }
1841
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001842 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001843 // The provided task is the task on this display with focus, so if WindowManagerService's
1844 // focused app is not on this display, focusedTask will be null.
1845 if (focusedTask == null) {
1846 mTouchExcludeRegion.setEmpty();
1847 } else {
1848 mTouchExcludeRegion.set(mBaseDisplayRect);
1849 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1850 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07001851 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1852 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001853 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
1854 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08001855 }
1856 // If we removed the focused task above, add it back and only leave its
1857 // outside touch area in the exclusion. TapDectector is not interested in
1858 // any touch inside the focused task itself.
1859 if (!mTmpRect2.isEmpty()) {
1860 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1861 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001862 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001863 final WindowState inputMethod = mService.mInputMethodWindow;
1864 if (inputMethod != null && inputMethod.isVisibleLw()) {
1865 // If the input method is visible and the user is typing, we don't want these touch
1866 // events to be intercepted and used to change focus. This would likely cause a
1867 // disappearance of the input method.
1868 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001869 if (inputMethod.getDisplayId() == mDisplayId) {
1870 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1871 } else {
1872 // IME is on a different display, so we need to update its tap detector.
1873 // TODO(multidisplay): Remove when IME will always appear on same display.
1874 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1875 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001876 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001877 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1878 WindowState win = mTapExcludedWindows.get(i);
1879 win.getTouchableRegion(mTmpRegion);
1880 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1881 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001882 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07001883 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001884 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001885 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001886 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1887 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001888 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001889 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001890 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001891 }
1892
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001893 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001894 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001895 super.switchUser();
1896 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001897 }
1898
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001899 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001900 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1901 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001902 }
1903 }
1904
Wale Ogunwale10124582016-09-15 20:25:50 -07001905 @Override
1906 void removeIfPossible() {
1907 if (isAnimating()) {
1908 mDeferredRemoval = true;
1909 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001910 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001911 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001912 }
1913
Wale Ogunwale10124582016-09-15 20:25:50 -07001914 @Override
1915 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001916 mRemovingDisplay = true;
1917 try {
1918 super.removeImmediately();
1919 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07001920 if (mService.canDispatchPointerEvents()) {
1921 if (mTapDetector != null) {
1922 mService.unregisterPointerEventListener(mTapDetector);
1923 }
1924 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
1925 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1926 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001927 }
Robert Carrb1579c82017-09-05 14:54:47 -07001928 // The pending transaction won't be applied so we should
1929 // just clean up any surfaces pending destruction.
1930 onPendingTransactionApplied();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001931 } finally {
1932 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001933 }
Craig Mautner95da1082014-02-24 17:54:35 -08001934 }
1935
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001936 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001937 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001938 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001939 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1940
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001941 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001942 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001943 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001944 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001945 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001946 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001947 }
1948
Andrii Kulian0214ed92017-05-16 13:44:05 -07001949 /** @return 'true' if removal of this display content is deferred due to active animation. */
1950 boolean isRemovalDeferred() {
1951 return mDeferredRemoval;
1952 }
1953
Wale Ogunwale10124582016-09-15 20:25:50 -07001954 boolean animateForIme(float interpolatedValue, float animationTarget,
1955 float dividerAnimationTarget) {
1956 boolean updated = false;
1957
Wale Ogunwale61911492017-10-11 08:50:50 -07001958 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1959 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001960 if (stack == null || !stack.isAdjustedForIme()) {
1961 continue;
1962 }
1963
1964 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1965 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1966 updated = true;
1967 } else {
1968 mDividerControllerLocked.mLastAnimationProgress =
1969 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1970 mDividerControllerLocked.mLastDividerProgress =
1971 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1972 updated |= stack.updateAdjustForIme(
1973 mDividerControllerLocked.mLastAnimationProgress,
1974 mDividerControllerLocked.mLastDividerProgress,
1975 false /* force */);
1976 }
1977 if (interpolatedValue >= 1f) {
1978 stack.endImeAdjustAnimation();
1979 }
1980 }
1981
1982 return updated;
1983 }
1984
1985 boolean clearImeAdjustAnimation() {
1986 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07001987 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1988 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001989 if (stack != null && stack.isAdjustedForIme()) {
1990 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1991 changed = true;
1992 }
1993 }
1994 return changed;
1995 }
1996
1997 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001998 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1999 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002000 if (stack.isVisible() && stack.isAdjustedForIme()) {
2001 stack.beginImeAdjustAnimation();
2002 }
2003 }
2004 }
2005
2006 void adjustForImeIfNeeded() {
2007 final WindowState imeWin = mService.mInputMethodWindow;
2008 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2009 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002010 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002011 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2012 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2013 imeTargetStack.getDockSide() : DOCKED_INVALID;
2014 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2015 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2016 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002017 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002018 final boolean imeHeightChanged = imeVisible &&
2019 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2020
2021 // The divider could be adjusted for IME position, or be thinner than usual,
2022 // or both. There are three possible cases:
2023 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2024 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2025 // - If IME is not visible, divider is not moved and is normal width.
2026
2027 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002028 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2029 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002030 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002031 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2032 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002033 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2034 } else {
2035 stack.resetAdjustedForIme(false);
2036 }
2037 }
2038 mDividerControllerLocked.setAdjustedForIme(
2039 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2040 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002041 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2042 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002043 stack.resetAdjustedForIme(!dockVisible);
2044 }
2045 mDividerControllerLocked.setAdjustedForIme(
2046 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2047 }
Winson Chung655332c2016-10-31 13:14:28 -07002048 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002049 }
2050
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002051 void setInputMethodAnimLayerAdjustment(int adj) {
2052 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2053 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002054 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002055 }
2056
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002057 /**
2058 * If a window that has an animation specifying a colored background and the current wallpaper
2059 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2060 * suddenly disappear.
2061 */
2062 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002063 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2064 w -> w.mIsWallpaper && w.isVisibleNow());
2065
2066 if (visibleWallpaper != null) {
2067 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002068 }
2069 return winAnimator.mAnimLayer;
2070 }
2071
Wale Ogunwale10124582016-09-15 20:25:50 -07002072 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002073 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2074 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002075 stack.prepareFreezingTaskBounds();
2076 }
2077 }
2078
Wale Ogunwale94744212015-09-21 19:01:47 -07002079 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002080 getBounds(mTmpRect, newRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002081
2082 // Compute a transform matrix to undo the coordinate space transformation,
2083 // and present the window at the same physical position it previously occupied.
2084 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2085 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2086
2087 mTmpRectF.set(bounds);
2088 mTmpMatrix.mapRect(mTmpRectF);
2089 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002090 }
2091
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002092 static int deltaRotation(int oldRotation, int newRotation) {
2093 int delta = newRotation - oldRotation;
2094 if (delta < 0) delta += 4;
2095 return delta;
2096 }
2097
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002098 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002099 Matrix outMatrix) {
2100 // For rotations without Z-ordering we don't need the target rectangle's position.
2101 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2102 displayHeight, outMatrix);
2103 }
2104
2105 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2106 float displayWidth, float displayHeight, Matrix outMatrix) {
2107 switch (rotation) {
2108 case ROTATION_0:
2109 outMatrix.reset();
2110 break;
2111 case ROTATION_270:
2112 outMatrix.setRotate(270, 0, 0);
2113 outMatrix.postTranslate(0, displayHeight);
2114 outMatrix.postTranslate(rectTop, 0);
2115 break;
2116 case ROTATION_180:
2117 outMatrix.reset();
2118 break;
2119 case ROTATION_90:
2120 outMatrix.setRotate(90, 0, 0);
2121 outMatrix.postTranslate(displayWidth, 0);
2122 outMatrix.postTranslate(-rectTop, rectLeft);
2123 break;
2124 }
2125 }
2126
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002127 @CallSuper
2128 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002129 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002130 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002131 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002132 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002133 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2134 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002135 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002136 }
2137 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2138 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002139 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2140 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002141 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002142 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002143 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2144 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002145 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002146 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002147 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2148 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002149 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002150 }
2151 proto.write(DPI, mBaseDisplayDensity);
2152 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002153 proto.write(ROTATION, mRotation);
2154 final ScreenRotationAnimation screenRotationAnimation =
2155 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2156 if (screenRotationAnimation != null) {
2157 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2158 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002159 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002160 proto.end(token);
2161 }
2162
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002163 @Override
2164 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2165 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002166 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2167 final String subPrefix = " " + prefix;
2168 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2169 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2170 pw.print("dpi");
2171 if (mInitialDisplayWidth != mBaseDisplayWidth
2172 || mInitialDisplayHeight != mBaseDisplayHeight
2173 || mInitialDisplayDensity != mBaseDisplayDensity) {
2174 pw.print(" base=");
2175 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2176 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2177 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002178 if (mDisplayScalingDisabled) {
2179 pw.println(" noscale");
2180 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002181 pw.print(" cur=");
2182 pw.print(mDisplayInfo.logicalWidth);
2183 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2184 pw.print(" app=");
2185 pw.print(mDisplayInfo.appWidth);
2186 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2187 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2188 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2189 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2190 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002191 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002192 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002193 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002194
Craig Mautnerdc548482014-02-05 13:35:24 -08002195 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002196 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002197 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2198 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002199 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002200 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002201
Craig Mautnerdc548482014-02-05 13:35:24 -08002202 pw.println();
2203 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002204 pw.println();
2205 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002206 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002207 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002208 pw.print(" Exiting #"); pw.print(i);
2209 pw.print(' '); pw.print(token);
2210 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002211 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002212 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002213 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002214
Jorim Jaggi31f71702016-05-04 16:43:04 -07002215 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002216
2217 // Dump stack references
2218 final TaskStack homeStack = getHomeStack();
2219 if (homeStack != null) {
2220 pw.println(prefix + "homeStack=" + homeStack.getName());
2221 }
2222 final TaskStack pinnedStack = getPinnedStack();
2223 if (pinnedStack != null) {
2224 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2225 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002226 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002227 if (splitScreenPrimaryStack != null) {
2228 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2229 }
2230
2231 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002232 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002233 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002234 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002235
2236 if (mInputMethodAnimLayerAdjustment != 0) {
2237 pw.println(subPrefix
2238 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2239 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002240
2241 pw.println();
2242 mDisplayFrames.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002243 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002244
2245 @Override
2246 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002247 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002248 }
2249
2250 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002251 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002252 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002253
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002254 /** Returns true if the stack in the windowing mode is visible. */
2255 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002256 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002257 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002258 }
2259
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002260 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002261 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002262 final int x = (int) xf;
2263 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002264 final WindowState touchedWin = getWindow(w -> {
2265 final int flags = w.mAttrs.flags;
2266 if (!w.isVisibleLw()) {
2267 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002268 }
2269 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002270 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002271 }
2272
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002273 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002274 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002275 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002276 }
2277
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002278 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002279
2280 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002281 return mTmpRegion.contains(x, y) || touchFlags == 0;
2282 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002283
2284 return touchedWin;
2285 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002286
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002287 boolean canAddToastWindowForUid(int uid) {
2288 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002289 // Also if the app is focused adding more than one toast at
2290 // a time for better backwards compatibility.
2291 final WindowState focusedWindowForUid = getWindow(w ->
2292 w.mOwnerUid == uid && w.isFocused());
2293 if (focusedWindowForUid != null) {
2294 return true;
2295 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002296 final WindowState win = getWindow(w ->
2297 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2298 && !w.mWindowRemovalAllowed);
2299 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002300 }
2301
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002302 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002303 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2304 return;
2305 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002306
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002307 // Used to communicate the old focus to the callback method.
2308 mTmpWindow = oldFocus;
2309
2310 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002311 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002312
2313 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002314 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002315
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002316 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002317
2318 if (mTmpWindow == null) {
2319 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2320 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002321 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002322 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002323 }
2324
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002325 /** Updates the layer assignment of windows on this display. */
2326 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002327 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002328 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002329 if (setLayoutNeeded) {
2330 setLayoutNeeded();
2331 }
Robert Carrb1579c82017-09-05 14:54:47 -07002332
Robert Carrf59b8dd2017-10-02 18:58:36 -07002333 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002334 // the application of this transaction until the animation pass triggers
2335 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2336 // the hiding and showing of surfaces.
2337 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002338 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002339 }
2340
Wale Ogunwale1666e312016-12-16 11:27:18 -08002341 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2342 // moving containers or resizing them. Need to investigate the best way to have it automatically
2343 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002344 void layoutAndAssignWindowLayersIfNeeded() {
2345 mService.mWindowsChanged = true;
2346 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002347
2348 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2349 false /*updateInputWindows*/)) {
2350 assignWindowLayers(false /* setLayoutNeeded */);
2351 }
2352
2353 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2354 mService.mWindowPlacerLocked.performSurfacePlacement();
2355 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2356 }
2357
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002358 /** Returns true if a leaked surface was destroyed */
2359 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002360 // Used to indicate that a surface was leaked.
2361 mTmpWindow = null;
2362 forAllWindows(w -> {
2363 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002364 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002365 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002366 }
2367 if (!mService.mSessions.contains(wsa.mSession)) {
2368 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002369 + w + " surface=" + wsa.mSurfaceController
2370 + " token=" + w.mToken
2371 + " pid=" + w.mSession.mPid
2372 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002373 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002374 mService.mForceRemoves.add(w);
2375 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002376 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002377 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002378 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002379 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002380 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002381 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002382 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002383 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002384 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002385
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002386 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002387 }
2388
2389 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002390 * Determine and return the window that should be the IME target.
2391 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2392 * @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 +00002393 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002394 WindowState computeImeTarget(boolean updateImeTarget) {
2395 if (mService.mInputMethodWindow == null) {
2396 // There isn't an IME so there shouldn't be a target...That was easy!
2397 if (updateImeTarget) {
2398 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2399 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2400 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2401 }
2402 return null;
2403 }
2404
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002405 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2406 // same display. Or even when the current IME/target are not on the same screen as the next
2407 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002408 mUpdateImeTarget = updateImeTarget;
2409 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002410
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002411
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002412 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2413 // to be on top of it, but it is not -really- where input will go. So look down below
2414 // for a real window to target...
2415 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2416 final AppWindowToken token = target.mAppToken;
2417 if (token != null) {
2418 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2419 if (betterTarget != null) {
2420 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002421 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002422 }
2423 }
2424
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002425 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2426 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002427
2428 // Now, a special case -- if the last target's window is in the process of exiting, and is
2429 // above the new target, keep on the last target to avoid flicker. Consider for example a
2430 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2431 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2432 // scrim.
2433 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002434 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2435 && (target == null
2436 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002437 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002438 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002439 }
2440
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002441 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2442 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002443
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002444 if (target == null) {
2445 if (updateImeTarget) {
2446 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2447 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2448 + Debug.getCallers(4) : ""));
2449 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2450 }
2451
2452 return null;
2453 }
2454
2455 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002456 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2457 if (token != null) {
2458
2459 // Now some fun for dealing with window animations that modify the Z order. We need
2460 // to look at all windows below the current target that are in this app, finding the
2461 // highest visible one in layering.
2462 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002463 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002464 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002465 }
2466
2467 if (highestTarget != null) {
2468 final AppTransition appTransition = mService.mAppTransition;
2469 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2470 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2471 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002472 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002473
2474 if (appTransition.isTransitionSet()) {
2475 // If we are currently setting up for an animation, hold everything until we
2476 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002477 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2478 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002479 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002480 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002481 // If the window we are currently targeting is involved with an animation,
2482 // and it is on top of the next target we will be over, then hold off on
2483 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002484 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2485 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002486 }
2487 }
2488 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002489
2490 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2491 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2492 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002493 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002494 }
2495
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002496 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002497 }
2498
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002499 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2500 if (target == mService.mInputMethodTarget
2501 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2502 && mInputMethodAnimLayerAdjustment == layerAdj) {
2503 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002504 }
2505
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002506 mService.mInputMethodTarget = target;
2507 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2508 setInputMethodAnimLayerAdjustment(layerAdj);
2509 assignWindowLayers(false /* setLayoutNeeded */);
2510 }
2511
2512 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2513 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2514 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2515 }
2516
2517 // Used to indicate we have reached the first window in the range we are interested in.
2518 mTmpWindow = null;
2519
2520 // TODO: Figure-out a more efficient way to do this.
2521 final WindowState candidate = getWindow(w -> {
2522 if (w == top) {
2523 // Reached the first window in the range we are interested in.
2524 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002525 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002526 if (mTmpWindow == null) {
2527 return false;
2528 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002529
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002530 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2531 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002532 }
2533 // If we reached the bottom of the range of windows we are considering,
2534 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002535 if (w == bottom) {
2536 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002537 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002538 return false;
2539 });
2540
2541 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002542 }
2543
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002544 void setLayoutNeeded() {
2545 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2546 mLayoutNeeded = true;
2547 }
2548
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002549 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002550 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2551 mLayoutNeeded = false;
2552 }
2553
2554 boolean isLayoutNeeded() {
2555 return mLayoutNeeded;
2556 }
2557
Wale Ogunwale02319a62016-09-26 15:21:22 -07002558 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2559 if (mTokenMap.isEmpty()) {
2560 return;
2561 }
2562 pw.println(" Display #" + mDisplayId);
2563 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2564 while (it.hasNext()) {
2565 final WindowToken token = it.next();
2566 pw.print(" ");
2567 pw.print(token);
2568 if (dumpAll) {
2569 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002570 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07002571 } else {
2572 pw.println();
2573 }
2574 }
2575 }
2576
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002577 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002578 final int[] index = new int[1];
2579 forAllWindows(w -> {
2580 final WindowStateAnimator wAnim = w.mWinAnimator;
2581 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2582 index[0] = index[0] + 1;
2583 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002584 }
2585
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002586 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002587 forAllWindows(w -> {
2588 w.mWinAnimator.enableSurfaceTrace(fd);
2589 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002590 }
2591
2592 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002593 forAllWindows(w -> {
2594 w.mWinAnimator.disableSurfaceTrace();
2595 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002596 }
2597
Jorim Jaggife762342016-10-13 14:33:27 +02002598 /**
2599 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2600 */
2601 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2602 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002603 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002604 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2605 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002606 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02002607 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002608 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002609 }
2610
Wale Ogunwale494009b82016-10-21 09:01:38 -07002611 boolean checkWaitingForWindows() {
2612
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002613 mHaveBootMsg = false;
2614 mHaveApp = false;
2615 mHaveWallpaper = false;
2616 mHaveKeyguard = true;
2617
2618 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002619 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2620 return true;
2621 }
2622 if (w.isDrawnLw()) {
2623 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002624 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002625 } else if (w.mAttrs.type == TYPE_APPLICATION
2626 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002627 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002628 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002629 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002630 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002631 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002632 }
2633 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002634 return false;
2635 });
2636
2637 if (visibleWindow != null) {
2638 // We have a visible window.
2639 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002640 }
2641
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002642 // if the wallpaper service is disabled on the device, we're never going to have
2643 // wallpaper, don't bother waiting for it
2644 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2645 com.android.internal.R.bool.config_enableWallpaperService)
2646 && !mService.mOnlyCore;
2647
Wale Ogunwale494009b82016-10-21 09:01:38 -07002648 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2649 "******** booted=" + mService.mSystemBooted
2650 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002651 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2652 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2653 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002654
2655 // If we are turning on the screen to show the boot message, don't do it until the boot
2656 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002657 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002658 return true;
2659 }
2660
2661 // If we are turning on the screen after the boot is completed normally, don't do so until
2662 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002663 if (mService.mSystemBooted
2664 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002665 return true;
2666 }
2667
2668 return false;
2669 }
2670
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002671 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002672 mTmpWindowAnimator = animator;
2673 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002674 }
2675
2676 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002677 resetAnimationBackgroundAnimator();
2678
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002679 // Used to indicate a detached wallpaper.
2680 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002681 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002682
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002683 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002684
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002685 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002686 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002687 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2688 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002689 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2690 }
2691 }
2692
Wale Ogunwale494009b82016-10-21 09:01:38 -07002693 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002694 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002695 if (imFocus == null) {
2696 return false;
2697 }
2698
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002699 if (DEBUG_INPUT_METHOD) {
2700 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2701 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2702 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002703 }
2704
Wale Ogunwale494009b82016-10-21 09:01:38 -07002705 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2706
2707 if (DEBUG_INPUT_METHOD) {
2708 Slog.i(TAG_WM, "IM target client: " + imeClient);
2709 if (imeClient != null) {
2710 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2711 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2712 }
2713 }
2714
2715 return imeClient != null && imeClient.asBinder() == client.asBinder();
2716 }
2717
2718 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002719 final WindowState win = getWindow(
2720 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2721 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002722 }
2723
2724 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002725 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002726 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002727 final int curValue = w.mSystemUiVisibility;
2728 final int diff = (curValue ^ visibility) & globalDiff;
2729 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002730 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002731 w.mSeq++;
2732 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002733 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002734 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2735 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002736 visibility, newValue, diff);
2737 }
2738 } catch (RemoteException e) {
2739 // so sorry
2740 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002741 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002742 }
2743
2744 void onWindowFreezeTimeout() {
2745 Slog.w(TAG_WM, "Window freeze timeout expired.");
2746 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002747
2748 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002749 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002750 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002751 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002752 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002753 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2754 - mService.mDisplayFreezeTime);
2755 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002756 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002757 mService.mWindowPlacerLocked.performSurfacePlacement();
2758 }
2759
2760 void waitForAllWindowsDrawn() {
2761 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002762 forAllWindows(w -> {
2763 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2764 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2765 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002766 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002767 w.mLastContentInsets.set(-1, -1, -1, -1);
2768 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002769 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002770 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002771 }
2772
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002773 // TODO: Super crazy long method that should be broken down...
2774 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2775
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002776 final int dw = mDisplayInfo.logicalWidth;
2777 final int dh = mDisplayInfo.logicalHeight;
2778 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2779
2780 mTmpUpdateAllDrawn.clear();
2781
2782 int repeats = 0;
2783 do {
2784 repeats++;
2785 if (repeats > 6) {
2786 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2787 clearLayoutNeeded();
2788 break;
2789 }
2790
2791 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2792 pendingLayoutChanges);
2793
Andrii Kulian839def92016-11-02 10:58:58 -07002794 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2795 // the wallpaper window jumping across displays.
2796 // Remove check for default display when there will be support for multiple wallpaper
2797 // targets (on different displays).
2798 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002799 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002800 }
2801
2802 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2803 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2804 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2805 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002806 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002807 }
2808 }
2809
2810 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2811 setLayoutNeeded();
2812 }
2813
2814 // FIRST LOOP: Perform a layout, if needed.
2815 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2816 performLayout(repeats == 1, false /* updateInputWindows */);
2817 } else {
2818 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2819 }
2820
2821 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2822 pendingLayoutChanges = 0;
2823
2824 if (isDefaultDisplay) {
2825 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002826 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002827 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2828 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2829 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2830 }
2831 } while (pendingLayoutChanges != 0);
2832
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002833 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002834
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002835 mTmpRecoveringMemory = recoveringMemory;
2836 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002837
2838 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002839 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2840 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2841 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002842 true /* inTraversal, must call performTraversalInTrans... below */);
2843
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002844 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2845 if (wallpaperVisible != mLastWallpaperVisible) {
2846 mLastWallpaperVisible = wallpaperVisible;
2847 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2848 }
2849
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002850 while (!mTmpUpdateAllDrawn.isEmpty()) {
2851 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2852 // See if any windows have been drawn, so they (and others associated with them)
2853 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002854 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002855 }
2856
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002857 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002858 }
2859
Bryce Leef3c6a472017-11-14 14:53:06 -08002860 private void updateBounds() {
2861 calculateBounds(mTmpBounds);
2862 setBounds(mTmpBounds);
2863 }
2864
2865 // Determines the current display bounds based on the current state
2866 private void calculateBounds(Rect out) {
2867 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
2868 final int orientation = mDisplayInfo.rotation;
2869 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2870 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
2871 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
2872 int width = mDisplayInfo.logicalWidth;
2873 int left = (physWidth - width) / 2;
2874 int height = mDisplayInfo.logicalHeight;
2875 int top = (physHeight - height) / 2;
2876 out.set(left, top, left + width, top + height);
2877 }
2878
2879 @Override
2880 public void getBounds(Rect out) {
2881 calculateBounds(out);
2882 }
2883
2884 private void getBounds(Rect out, int orientation) {
2885 getBounds(out);
2886
2887 // Rotate the Rect if needed.
2888 final int currentRotation = mDisplayInfo.rotation;
2889 final int rotationDelta = deltaRotation(currentRotation, orientation);
2890 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
2891 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
2892 mTmpRectF.set(out);
2893 mTmpMatrix.mapRect(mTmpRectF);
2894 mTmpRectF.round(out);
2895 }
2896 }
2897
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002898 void performLayout(boolean initial, boolean updateInputWindows) {
2899 if (!isLayoutNeeded()) {
2900 return;
2901 }
2902 clearLayoutNeeded();
2903
2904 final int dw = mDisplayInfo.logicalWidth;
2905 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002906 if (DEBUG_LAYOUT) {
2907 Slog.v(TAG, "-------------------------------------");
2908 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2909 }
2910
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002911 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
2912 // TODO: Not sure if we really need to set the rotation here since we are updating from the
2913 // display info above...
2914 mDisplayFrames.mRotation = mRotation;
2915 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002916 if (isDefaultDisplay) {
2917 // Not needed on non-default displays.
2918 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2919 mService.mScreenRect.set(0, 0, dw, dh);
2920 }
2921
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002922 int seq = mService.mLayoutSeq + 1;
2923 if (seq < 0) seq = 0;
2924 mService.mLayoutSeq = seq;
2925
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002926 // Used to indicate that we have processed the dream window and all additional windows are
2927 // behind it.
2928 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002929 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002930
2931 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002932 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002933
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002934 // Used to indicate that we have processed the dream window and all additional attached
2935 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002936 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002937 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002938
2939 // Now perform layout of attached windows, which usually depend on the position of the
2940 // window they are attached to. XXX does not deal with windows that are attached to windows
2941 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002942 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002943
2944 // Window frames may have changed. Tell the input dispatcher about it.
2945 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2946 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2947 if (updateInputWindows) {
2948 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2949 }
2950
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002951 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2952 }
2953
2954 /**
2955 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2956 * In portrait mode, it grabs the full screenshot.
2957 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002958 * @param config of the output bitmap
2959 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
2960 */
chaviwfbe47df2017-11-10 16:14:49 -08002961 Bitmap screenshotDisplay(Bitmap.Config config, boolean wallpaperOnly) {
2962 synchronized (mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02002963 if (!mService.mPolicy.isScreenOn()) {
chaviwfbe47df2017-11-10 16:14:49 -08002964 if (DEBUG_SCREENSHOT) {
2965 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002966 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002967 return null;
2968 }
2969
chaviwfbe47df2017-11-10 16:14:49 -08002970 if (wallpaperOnly && !shouldScreenshotWallpaper()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002971 return null;
2972 }
2973
chaviwfbe47df2017-11-10 16:14:49 -08002974 int dw = mDisplayInfo.logicalWidth;
2975 int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002976
chaviwfbe47df2017-11-10 16:14:49 -08002977 if (dw <= 0 || dh <= 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002978 return null;
2979 }
2980
chaviwfbe47df2017-11-10 16:14:49 -08002981 final Rect frame = new Rect(0, 0, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002982
2983 // The screenshot API does not apply the current screen rotation.
2984 int rot = mDisplay.getRotation();
2985
2986 if (rot == ROTATION_90 || rot == ROTATION_270) {
2987 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
2988 }
2989
chaviwfbe47df2017-11-10 16:14:49 -08002990 // SurfaceFlinger is not aware of orientation, so convert our logical
2991 // crop to SurfaceFlinger's portrait orientation.
2992 convertCropForSurfaceFlinger(frame, rot, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002993
2994 final ScreenRotationAnimation screenRotationAnimation =
2995 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
2996 final boolean inRotation = screenRotationAnimation != null &&
2997 screenRotationAnimation.isAnimating();
chaviwfbe47df2017-11-10 16:14:49 -08002998 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002999
Robert Carrb1579c82017-09-05 14:54:47 -07003000 // TODO(b/68392460): We should screenshot Task controls directly
3001 // but it's difficult at the moment as the Task doesn't have the
3002 // correct size set.
chaviwfbe47df2017-11-10 16:14:49 -08003003 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, 0, 1, inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003004 if (bitmap == null) {
Robert Carrb1579c82017-09-05 14:54:47 -07003005 Slog.w(TAG_WM, "Failed to take screenshot");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003006 return null;
3007 }
chaviwfbe47df2017-11-10 16:14:49 -08003008
3009 // Create a copy of the screenshot that is immutable and backed in ashmem.
3010 // This greatly reduces the overhead of passing the bitmap between processes.
3011 final Bitmap ret = bitmap.createAshmemBitmap(config);
3012 bitmap.recycle();
3013 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003014 }
chaviwfbe47df2017-11-10 16:14:49 -08003015 }
3016
3017 private boolean shouldScreenshotWallpaper() {
3018 MutableBoolean screenshotReady = new MutableBoolean(false);
3019
3020 forAllWindows(w -> {
3021 if (!w.mIsWallpaper) {
3022 return false;
3023 }
3024
3025 // Found the wallpaper window
3026 final WindowStateAnimator winAnim = w.mWinAnimator;
3027
3028 if (winAnim.getShown() && winAnim.mLastAlpha > 0f) {
3029 screenshotReady.value = true;
3030 }
3031
3032 return true;
3033 }, true /* traverseTopToBottom */);
3034
3035 return screenshotReady.value;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003036 }
3037
3038 // TODO: Can this use createRotationMatrix()?
3039 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3040 if (rot == Surface.ROTATION_90) {
3041 final int tmp = crop.top;
3042 crop.top = dw - crop.right;
3043 crop.right = crop.bottom;
3044 crop.bottom = dw - crop.left;
3045 crop.left = tmp;
3046 } else if (rot == Surface.ROTATION_180) {
3047 int tmp = crop.top;
3048 crop.top = dh - crop.bottom;
3049 crop.bottom = dh - tmp;
3050 tmp = crop.right;
3051 crop.right = dw - crop.left;
3052 crop.left = dw - tmp;
3053 } else if (rot == Surface.ROTATION_270) {
3054 final int tmp = crop.top;
3055 crop.top = crop.left;
3056 crop.left = dh - crop.bottom;
3057 crop.bottom = crop.right;
3058 crop.right = dh - tmp;
3059 }
3060 }
3061
3062 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003063 // Used to indicate the layout is needed.
3064 mTmpWindow = null;
3065
3066 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003067 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003068 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003069 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003070 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003071 w.setDisplayLayoutNeeded();
3072 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003073 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003074
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003075 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003076 mService.mWindowPlacerLocked.performSurfacePlacement();
3077 }
3078 }
3079
Wale Ogunwale1666e312016-12-16 11:27:18 -08003080 void setExitingTokensHasVisible(boolean hasVisible) {
3081 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3082 mExitingTokens.get(i).hasVisible = hasVisible;
3083 }
3084
3085 // Initialize state of exiting applications.
3086 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3087 }
3088
3089 void removeExistingTokensIfPossible() {
3090 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3091 final WindowToken token = mExitingTokens.get(i);
3092 if (!token.hasVisible) {
3093 mExitingTokens.remove(i);
3094 }
3095 }
3096
3097 // Time to remove any exiting applications?
3098 mTaskStackContainers.removeExistingAppTokensIfPossible();
3099 }
3100
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003101 @Override
3102 void onDescendantOverrideConfigurationChanged() {
3103 setLayoutNeeded();
3104 mService.requestTraversal();
3105 }
3106
David Stevens9440dc82017-03-16 19:00:20 -07003107 boolean okToDisplay() {
3108 if (mDisplayId == DEFAULT_DISPLAY) {
3109 return !mService.mDisplayFrozen
3110 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3111 }
3112 return mDisplayInfo.state == Display.STATE_ON;
3113 }
3114
3115 boolean okToAnimate() {
3116 return okToDisplay() &&
3117 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3118 }
3119
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003120 static final class TaskForResizePointSearchResult {
3121 boolean searchDone;
3122 Task taskForResize;
3123
3124 void reset() {
3125 searchDone = false;
3126 taskForResize = null;
3127 }
3128 }
Robert Carr3b716242016-08-16 16:02:21 -07003129
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003130 private static final class ApplySurfaceChangesTransactionState {
3131 boolean displayHasContent;
3132 boolean obscured;
3133 boolean syswin;
3134 boolean focusDisplayed;
3135 float preferredRefreshRate;
3136 int preferredModeId;
3137
3138 void reset() {
3139 displayHasContent = false;
3140 obscured = false;
3141 syswin = false;
3142 focusDisplayed = false;
3143 preferredRefreshRate = 0;
3144 preferredModeId = 0;
3145 }
3146 }
3147
3148 private static final class ScreenshotApplicationState {
3149 WindowState appWin;
3150 int maxLayer;
3151 int minLayer;
3152 boolean screenshotReady;
3153
3154 void reset(boolean screenshotReady) {
3155 appWin = null;
3156 maxLayer = 0;
3157 minLayer = 0;
3158 this.screenshotReady = screenshotReady;
3159 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3160 }
3161 }
3162
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003163 /**
3164 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3165 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3166 * homogeneous children type which is currently required by sub-classes of
3167 * {@link WindowContainer} class.
3168 */
3169 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3170
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003171 DisplayChildWindowContainer(WindowManagerService service) {
3172 super(service);
3173 }
3174
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003175 @Override
3176 boolean fillsParent() {
3177 return true;
3178 }
3179
3180 @Override
3181 boolean isVisible() {
3182 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003183 }
3184 }
3185
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003186 /**
3187 * Window container class that contains all containers on this display relating to Apps.
3188 * I.e Activities.
3189 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003190 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003191 /**
3192 * A control placed at the appropriate level for transitions to occur.
3193 */
3194 SurfaceControl mAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003195
Wale Ogunwale61911492017-10-11 08:50:50 -07003196 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3197 // through the list to find them.
3198 private TaskStack mHomeStack = null;
3199 private TaskStack mPinnedStack = null;
3200 private TaskStack mSplitScreenPrimaryStack = null;
3201
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003202 TaskStackContainers(WindowManagerService service) {
3203 super(service);
3204 }
3205
Wale Ogunwale61911492017-10-11 08:50:50 -07003206 /**
3207 * Returns the topmost stack on the display that is compatible with the input windowing mode
3208 * and activity type. Null is no compatible stack on the display.
3209 */
3210 TaskStack getStack(int windowingMode, int activityType) {
3211 if (activityType == ACTIVITY_TYPE_HOME) {
3212 return mHomeStack;
3213 }
3214 if (windowingMode == WINDOWING_MODE_PINNED) {
3215 return mPinnedStack;
3216 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3217 return mSplitScreenPrimaryStack;
3218 }
3219 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3220 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003221 if (activityType == ACTIVITY_TYPE_UNDEFINED
3222 && windowingMode == stack.getWindowingMode()) {
3223 // Passing in undefined type means we want to match the topmost stack with the
3224 // windowing mode.
3225 return stack;
3226 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003227 if (stack.isCompatible(windowingMode, activityType)) {
3228 return stack;
3229 }
3230 }
3231 return null;
3232 }
3233
3234 @VisibleForTesting
3235 TaskStack getTopStack() {
3236 return mTaskStackContainers.getChildCount() > 0
3237 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3238 }
3239
3240 TaskStack getHomeStack() {
3241 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3242 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3243 }
3244 return mHomeStack;
3245 }
3246
3247 TaskStack getPinnedStack() {
3248 return mPinnedStack;
3249 }
3250
Matthew Ng64e77cf2017-10-31 14:01:31 -07003251 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003252 return mSplitScreenPrimaryStack;
3253 }
3254
Andrii Kulian839def92016-11-02 10:58:58 -07003255 /**
3256 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003257 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003258 */
3259 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003260 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003261 addChild(stack, onTop);
3262 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003263 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003264
Wale Ogunwale61911492017-10-11 08:50:50 -07003265 void onStackWindowingModeChanged(TaskStack stack) {
3266 removeStackReferenceIfNeeded(stack);
3267 addStackReferenceIfNeeded(stack);
3268 if (stack == mPinnedStack && getTopStack() != stack) {
3269 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3270 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3271 }
3272 }
3273
3274 private void addStackReferenceIfNeeded(TaskStack stack) {
3275 if (stack.isActivityTypeHome()) {
3276 if (mHomeStack != null) {
3277 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
3278 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
3279 }
3280 mHomeStack = stack;
3281 }
3282 final int windowingMode = stack.getWindowingMode();
3283 if (windowingMode == WINDOWING_MODE_PINNED) {
3284 if (mPinnedStack != null) {
3285 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3286 + mPinnedStack + " already exist on display=" + this
3287 + " stack=" + stack);
3288 }
3289 mPinnedStack = stack;
3290 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3291 if (mSplitScreenPrimaryStack != null) {
3292 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3293 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3294 + " already exist on display=" + this + " stack=" + stack);
3295 }
3296 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003297 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003298 }
3299 }
3300
3301 private void removeStackReferenceIfNeeded(TaskStack stack) {
3302 if (stack == mHomeStack) {
3303 mHomeStack = null;
3304 } else if (stack == mPinnedStack) {
3305 mPinnedStack = null;
3306 } else if (stack == mSplitScreenPrimaryStack) {
3307 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003308 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3309 mService.setDockedStackCreateStateLocked(
3310 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3311 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003312 }
Andrii Kulian839def92016-11-02 10:58:58 -07003313 }
3314
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003315 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003316 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3317 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003318 addChild(stack, addIndex);
3319 setLayoutNeeded();
3320 }
3321
Wale Ogunwale61911492017-10-11 08:50:50 -07003322 @Override
3323 protected void removeChild(TaskStack stack) {
3324 super.removeChild(stack);
3325 removeStackReferenceIfNeeded(stack);
3326 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003327
3328 @Override
3329 boolean isOnTop() {
3330 // Considered always on top
3331 return true;
3332 }
3333
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003334 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003335 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003336 if (child.getWindowConfiguration().isAlwaysOnTop()
3337 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003338 // This stack is always-on-top, override the default behavior.
3339 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3340
3341 // Moving to its current position, as we must call super but we don't want to
3342 // perform any meaningful action.
3343 final int currentPosition = mChildren.indexOf(child);
3344 super.positionChildAt(currentPosition, child, false /* includingParents */);
3345 return;
3346 }
3347
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003348 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3349 super.positionChildAt(targetPosition, child, includingParents);
3350
3351 setLayoutNeeded();
3352 }
3353
3354 /**
3355 * When stack is added or repositioned, find a proper position for it.
3356 * This will make sure that pinned stack always stays on top.
3357 * @param requestedPosition Position requested by caller.
3358 * @param stack Stack to be added or positioned.
3359 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3360 * @return The proper position for the stack.
3361 */
3362 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3363 final int topChildPosition = mChildren.size() - 1;
3364 boolean toTop = requestedPosition == POSITION_TOP;
3365 toTop |= adding ? requestedPosition >= topChildPosition + 1
3366 : requestedPosition >= topChildPosition;
3367 int targetPosition = requestedPosition;
3368
Wale Ogunwale61911492017-10-11 08:50:50 -07003369 if (toTop && stack.getWindowingMode() != WINDOWING_MODE_PINNED && hasPinnedStack()) {
Bryce Lee48f4b572017-04-10 10:54:15 -07003370 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003371 TaskStack topStack = mChildren.get(topChildPosition);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003372 if (topStack.getWindowingMode() != WINDOWING_MODE_PINNED) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003373 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3374 }
3375
3376 // So, stack is moved just below the pinned stack.
3377 // When we're adding a new stack the target is the current pinned stack position.
3378 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003379 // stack, because WindowContainer#positionAt() first removes element and then adds
3380 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003381 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3382 }
3383
3384 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003385 }
3386
3387 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003388 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3389 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003390 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003391 if (super.forAllWindows(callback, traverseTopToBottom)) {
3392 return true;
3393 }
3394 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3395 return true;
3396 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003397 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003398 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3399 return true;
3400 }
3401 if (super.forAllWindows(callback, traverseTopToBottom)) {
3402 return true;
3403 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003404 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003405 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003406 }
3407
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003408 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003409 boolean traverseTopToBottom) {
3410 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3411 // app tokens.
3412 // TODO: Investigate if we need to continue to do this or if we can just process them
3413 // in-order.
3414 if (traverseTopToBottom) {
3415 for (int i = mChildren.size() - 1; i >= 0; --i) {
3416 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3417 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003418 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3419 traverseTopToBottom)) {
3420 return true;
3421 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003422 }
3423 }
3424 } else {
3425 final int count = mChildren.size();
3426 for (int i = 0; i < count; ++i) {
3427 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3428 final int appTokensCount = appTokens.size();
3429 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003430 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3431 traverseTopToBottom)) {
3432 return true;
3433 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003434 }
3435 }
3436 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003437 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003438 }
3439
Wale Ogunwale1666e312016-12-16 11:27:18 -08003440 void setExitingTokensHasVisible(boolean hasVisible) {
3441 for (int i = mChildren.size() - 1; i >= 0; --i) {
3442 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3443 for (int j = appTokens.size() - 1; j >= 0; --j) {
3444 appTokens.get(j).hasVisible = hasVisible;
3445 }
3446 }
3447 }
3448
3449 void removeExistingAppTokensIfPossible() {
3450 for (int i = mChildren.size() - 1; i >= 0; --i) {
3451 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3452 for (int j = appTokens.size() - 1; j >= 0; --j) {
3453 final AppWindowToken token = appTokens.get(j);
3454 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3455 && (!token.mIsExiting || token.isEmpty())) {
3456 // Make sure there is no animation running on this token, so any windows
3457 // associated with it will be removed as soon as their animations are
3458 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003459 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003460 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3461 "performLayout: App token exiting now removed" + token);
3462 token.removeIfPossible();
3463 }
3464 }
3465 }
3466 }
3467
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003468 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003469 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003470 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3471 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003472 // Apps and their containers are not allowed to specify an orientation while the
3473 // docked or freeform stack is visible...except for the home stack/task if the
3474 // docked stack is minimized and it actually set something.
3475 if (mHomeStack != null && mHomeStack.isVisible()
3476 && mDividerControllerLocked.isMinimizedDock()) {
3477 final int orientation = mHomeStack.getOrientation();
3478 if (orientation != SCREEN_ORIENTATION_UNSET) {
3479 return orientation;
3480 }
3481 }
3482 return SCREEN_ORIENTATION_UNSPECIFIED;
3483 }
3484
3485 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003486 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3487 PackageManager.FEATURE_AUTOMOTIVE);
3488 if (isCar) {
3489 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3490 // allow anything but the default orientation.
3491 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3492 "Forcing UNSPECIFIED orientation in car. Ignoring " + orientation);
3493 return SCREEN_ORIENTATION_UNSPECIFIED;
3494 }
3495
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003496 if (orientation != SCREEN_ORIENTATION_UNSET
3497 && orientation != SCREEN_ORIENTATION_BEHIND) {
3498 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3499 "App is requesting an orientation, return " + orientation);
3500 return orientation;
3501 }
3502
3503 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003504 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003505 // The next app has not been requested to be visible, so we keep the current orientation
3506 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003507 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003508 }
Robert Carrb1579c82017-09-05 14:54:47 -07003509
3510 @Override
3511 void assignChildLayers(SurfaceControl.Transaction t) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003512 int layer = 0;
Robert Carrb1579c82017-09-05 14:54:47 -07003513
3514 // We allow stacks to change visual order from the AM specified order due to
3515 // Z-boosting during animations. However we must take care to ensure TaskStacks
3516 // which are marked as alwaysOnTop remain that way.
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003517 for (int i = 0; i < mChildren.size(); i++) {
3518 final TaskStack s = mChildren.get(i);
3519 s.assignChildLayers();
3520 if (!s.needsZBoost() && !s.isAlwaysOnTop()) {
3521 s.assignLayer(t, layer++);
Robert Carrb1579c82017-09-05 14:54:47 -07003522 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003523 }
3524 for (int i = 0; i < mChildren.size(); i++) {
3525 final TaskStack s = mChildren.get(i);
3526 if (s.needsZBoost() && !s.isAlwaysOnTop()) {
3527 s.assignLayer(t, layer++);
Robert Carrb1579c82017-09-05 14:54:47 -07003528 }
3529 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003530 for (int i = 0; i < mChildren.size(); i++) {
3531 final TaskStack s = mChildren.get(i);
3532 if (s.isAlwaysOnTop()) {
3533 s.assignLayer(t, layer++);
3534 }
3535 }
3536
3537 // The appropriate place for App-Transitions to occur is right
3538 // above all other animations but still below things in the Picture-and-Picture
3539 // windowing mode.
3540 if (mAnimationLayer != null) {
3541 t.setLayer(mAnimationLayer, layer++);
3542 }
Robert Carrb1579c82017-09-05 14:54:47 -07003543 }
3544
3545 @Override
3546 void onParentSet() {
3547 super.onParentSet();
3548 if (getParent() != null) {
3549 mAnimationLayer = makeSurface().build();
3550 } else {
3551 mAnimationLayer.destroy();
3552 mAnimationLayer = null;
3553 }
3554 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003555 }
3556
Robert Carree4d4b92017-11-22 12:21:46 -08003557 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003558 AboveAppWindowContainers(String name, WindowManagerService service) {
3559 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08003560 }
3561
3562 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
3563 boolean needAssignIme = imeContainer != null
3564 && imeContainer.getSurfaceControl() != null;
3565 for (int j = 0; j < mChildren.size(); ++j) {
3566 final WindowToken wt = mChildren.get(j);
3567 wt.assignLayer(t, j);
3568 wt.assignChildLayers(t);
3569
3570 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
3571 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08003572
3573 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
3574 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003575 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08003576 needAssignIme = false;
3577 }
3578 }
3579 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003580 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08003581 }
3582 }
3583 }
3584
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003585 /**
3586 * Window container class that contains all containers on this display that are not related to
3587 * Apps. E.g. status bar.
3588 */
Robert Carree4d4b92017-11-22 12:21:46 -08003589 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07003590 /**
3591 * Compares two child window tokens returns -1 if the first is lesser than the second in
3592 * terms of z-order and 1 otherwise.
3593 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003594 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003595 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003596 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3597 token1.mOwnerCanManageAppTokens)
3598 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3599 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003600
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003601 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3602 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3603 return false;
3604 }
3605 final int req = w.mAttrs.screenOrientation;
3606 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3607 || req == SCREEN_ORIENTATION_UNSET) {
3608 return false;
3609 }
3610 return true;
3611 };
3612
Wale Ogunwale3a931692016-11-02 16:49:48 -07003613 private final String mName;
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003614 NonAppWindowContainers(String name, WindowManagerService service) {
3615 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003616 mName = name;
3617 }
3618
3619 void addChild(WindowToken token) {
3620 addChild(token, mWindowComparator);
3621 }
3622
3623 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003624 int getOrientation() {
3625 final WindowManagerPolicy policy = mService.mPolicy;
3626 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003627 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003628
3629 if (win != null) {
3630 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003631 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003632 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003633 if (mService.mKeyguardGoingAway) {
3634 // Keyguard can't affect the orientation if it is going away...
3635 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3636 return SCREEN_ORIENTATION_UNSET;
3637 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003638 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003639 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003640 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003641 }
3642
Andrii Kulian8ee72852017-03-10 10:36:45 -08003643 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003644
Jorim Jaggi75520d52017-08-24 17:23:19 +02003645 if (policy.isKeyguardShowingAndNotOccluded()
3646 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003647 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003648 }
3649
3650 return SCREEN_ORIENTATION_UNSET;
3651 }
3652
3653 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003654 String getName() {
3655 return mName;
3656 }
Robert Carr3b716242016-08-16 16:02:21 -07003657 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003658
Robert Carrb1579c82017-09-05 14:54:47 -07003659 SurfaceControl.Builder makeSurface(SurfaceSession s) {
3660 return mService.makeSurfaceBuilder(s)
3661 .setParent(mWindowingLayer);
3662 }
3663
3664 @Override
3665 SurfaceSession getSession() {
3666 return mSession;
3667 }
3668
3669 @Override
3670 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003671 SurfaceSession s = child != null ? child.getSession() : getSession();
3672 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07003673 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07003674
3675 if (child == null) {
3676 return b;
3677 }
3678
Robert Carree4d4b92017-11-22 12:21:46 -08003679 return b.setName(child.getName())
3680 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07003681 }
3682
3683 /**
3684 * The makeSurface variants are for use by the window-container
3685 * hierarchy. makeOverlay here is a function for various non windowing
3686 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
3687 * and other potpourii.
3688 */
3689 SurfaceControl.Builder makeOverlay() {
3690 return mService.makeSurfaceBuilder(mSession)
3691 .setParent(mOverlayLayer);
3692 }
3693
3694 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003695 applyMagnificationSpec(getPendingTransaction(), spec);
3696 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07003697 }
3698
3699 @Override
3700 void onParentSet() {
3701 // Since we are the top of the SurfaceControl hierarchy here
3702 // we create the root surfaces explicitly rather than chaining
3703 // up as the default implementation in onParentSet does. So we
3704 // explicitly do NOT call super here.
3705 }
3706
3707 @Override
3708 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07003709
3710 // These are layers as children of "mWindowingLayer"
3711 mBelowAppWindowsContainers.assignLayer(t, 0);
3712 mTaskStackContainers.assignLayer(t, 1);
3713 mAboveAppWindowsContainers.assignLayer(t, 2);
3714
3715 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08003716 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08003717
Robert Carree4d4b92017-11-22 12:21:46 -08003718 // In the case where we have an IME target that is not in split-screen
3719 // mode IME assignment is easy. We just need the IME to go directly above
3720 // the target. This way children of the target will naturally go above the IME
3721 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08003722 //
Robert Carree4d4b92017-11-22 12:21:46 -08003723 // In the case of split-screen windowing mode, we need to elevate the IME above the
3724 // docked divider while keeping the app itself below the docked divider, so instead
3725 // we use relative layering of the IME targets child windows, and place the
3726 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08003727 //
Robert Carree4d4b92017-11-22 12:21:46 -08003728 // In the case where we have no IME target we assign it where it's base layer would
3729 // place it in the AboveAppWindowContainers.
3730 if (imeTarget != null && !imeTarget.inSplitScreenWindowingMode()
3731 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003732 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07003733 // TODO: We need to use an extra level on the app surface to ensure
3734 // this is always above SurfaceView but always below attached window.
3735 1);
Robert Carree4d4b92017-11-22 12:21:46 -08003736 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07003737 }
3738
3739 // Above we have assigned layers to our children, now we ask them to assign
3740 // layers to their children.
3741 mBelowAppWindowsContainers.assignChildLayers(t);
3742 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08003743 mAboveAppWindowsContainers.assignChildLayers(t,
3744 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07003745 mImeWindowsContainers.assignChildLayers(t);
3746 }
3747
3748 /**
3749 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
3750 * that the IME target is one of the docked applications. We'd like the docked divider to be
3751 * above both of the applications, and we'd like the IME to be above the docked divider.
3752 * However we need child windows of the applications to be above the IME (Text drag handles).
3753 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
3754 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
3755 * with {@link #WindowState#assignLayer}
3756 */
3757 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
3758 t.setRelativeLayer(child.getSurfaceControl(), mImeWindowsContainers.getSurfaceControl(), 1);
3759 }
3760
3761 @Override
Jorim Jaggia5e10572017-11-15 14:36:26 +01003762 public void destroyAfterPendingTransaction(SurfaceControl surface) {
Robert Carrb1579c82017-09-05 14:54:47 -07003763 mPendingDestroyingSurfaces.add(surface);
3764 }
3765
3766 /**
3767 * Destroys any surfaces that have been put into the pending list with
Jorim Jaggia5e10572017-11-15 14:36:26 +01003768 * {@link #destroyAfterPendingTransaction}.
Robert Carrb1579c82017-09-05 14:54:47 -07003769 */
3770 void onPendingTransactionApplied() {
3771 for (int i = mPendingDestroyingSurfaces.size() - 1; i >= 0; i--) {
3772 mPendingDestroyingSurfaces.get(i).destroy();
3773 }
3774 mPendingDestroyingSurfaces.clear();
3775 }
Jorim Jaggi9af095b2017-12-12 17:18:57 +01003776
3777 @Override
3778 void prepareSurfaces() {
3779 final ScreenRotationAnimation screenRotationAnimation =
3780 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
3781 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
3782 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
3783 mPendingTransaction.setMatrix(mWindowingLayer,
3784 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
3785 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
3786 mPendingTransaction.setPosition(mWindowingLayer,
3787 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
3788 mPendingTransaction.setAlpha(mWindowingLayer,
3789 screenRotationAnimation.getEnterTransformation().getAlpha());
3790 }
3791 super.prepareSurfaces();
3792 }
Craig Mautner59c00972012-07-30 12:10:24 -07003793}