blob: 274ac04946cf8df7248d616fba1b7fa0eff2c46d [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 }
402 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
403 if (appAnimator != null && appAnimator.thumbnail != null) {
404 if (appAnimator.thumbnailTransactionSeq
405 != mTmpWindowAnimator.mAnimTransactionSequence) {
406 appAnimator.thumbnailTransactionSeq =
407 mTmpWindowAnimator.mAnimTransactionSequence;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800408 }
409 }
410 };
411
412 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
413 final WindowStateAnimator winAnimator = w.mWinAnimator;
414 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
415 return;
416 }
417
418 final int flags = w.mAttrs.flags;
419
420 // If this window is animating, make a note that we have an animating window and take
421 // care of a request to run a detached wallpaper animation.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100422 if (winAnimator.isAnimationSet()) {
423 final AnimationAdapter anim = w.getAnimation();
424 if (anim != null) {
425 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && anim.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800426 mTmpWindow = w;
427 }
Jorim Jaggia5e10572017-11-15 14:36:26 +0100428 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800429 if (color != 0) {
430 final TaskStack stack = w.getStack();
431 if (stack != null) {
432 stack.setAnimationBackground(winAnimator, color);
433 }
434 }
435 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800436 }
437
438 // If this window's app token is running a detached wallpaper animation, make a note so
439 // we can ensure the wallpaper is displayed behind it.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200440 final AppWindowToken atoken = winAnimator.mWin.mAppToken;
441 final AnimationAdapter animation = atoken != null ? atoken.getAnimation() : null;
442 if (animation != null) {
443 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && animation.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800444 mTmpWindow = w;
445 }
446
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200447 final int color = animation.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800448 if (color != 0) {
449 final TaskStack stack = w.getStack();
450 if (stack != null) {
451 stack.setAnimationBackground(winAnimator, color);
452 }
453 }
454 }
455 };
456
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800457 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
458 final int lostFocusUid = mTmpWindow.mOwnerUid;
459 final Handler handler = mService.mH;
460 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
461 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
462 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
463 w.mAttrs.hideTimeoutMilliseconds);
464 }
465 }
466 };
467
468 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
469 final AppWindowToken focusedApp = mService.mFocusedApp;
470 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
471 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
472
473 if (!w.canReceiveKeys()) {
474 return false;
475 }
476
477 final AppWindowToken wtoken = w.mAppToken;
478
479 // If this window's application has been removed, just skip it.
480 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
481 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
482 + (wtoken.removed ? "removed" : "sendingToBottom"));
483 return false;
484 }
485
486 if (focusedApp == null) {
487 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
488 + " using new focus @ " + w);
489 mTmpWindow = w;
490 return true;
491 }
492
493 if (!focusedApp.windowsAreFocusable()) {
494 // Current focused app windows aren't focusable...
495 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
496 + " focusable using new focus @ " + w);
497 mTmpWindow = w;
498 return true;
499 }
500
501 // Descend through all of the app tokens and find the first that either matches
502 // win.mAppToken (return win) or mFocusedApp (return null).
503 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
504 if (focusedApp.compareTo(wtoken) > 0) {
505 // App stack below focused app stack. No focus for you!!!
506 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
507 "findFocusedWindow: Reached focused app=" + focusedApp);
508 mTmpWindow = null;
509 return true;
510 }
511 }
512
513 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
514 mTmpWindow = w;
515 return true;
516 };
517
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800518 private final Consumer<WindowState> mPerformLayout = w -> {
519 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
520 // wasting time and funky changes while a window is animating away.
521 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
522 || w.isGoneForLayoutLw();
523
524 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
525 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
526 + " mLayoutAttached=" + w.mLayoutAttached
527 + " screen changed=" + w.isConfigChanged());
528 final AppWindowToken atoken = w.mAppToken;
529 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200530 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800531 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
532 + " parentHidden=" + w.isParentWindowHidden());
533 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200534 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800535 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
536 + " parentHidden=" + w.isParentWindowHidden());
537 }
538
539 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
540 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
541 // since that means "perform layout as normal, just don't display").
542 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
543 || ((w.isConfigChanged() || w.setReportResizeHints())
544 && !w.isGoneForLayoutLw() &&
545 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
546 (w.mHasSurface && w.mAppToken != null &&
547 w.mAppToken.layoutConfigChanges)))) {
548 if (!w.mLayoutAttached) {
549 if (mTmpInitial) {
550 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
551 w.mContentChanged = false;
552 }
553 if (w.mAttrs.type == TYPE_DREAM) {
554 // Don't layout windows behind a dream, so that if it does stuff like hide
555 // the status bar we won't get a bad transition when it goes away.
556 mTmpWindow = w;
557 }
558 w.mLayoutNeeded = false;
559 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200560 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000561 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800562 w.mLayoutSeq = mService.mLayoutSeq;
563
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200564 // If this is the first layout, we need to initialize the last inset values as
565 // otherwise we'd immediately cause an unnecessary resize.
566 if (firstLayout) {
567 w.updateLastInsetValues();
568 }
569
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800570 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
571 + " mContainingFrame=" + w.mContainingFrame
572 + " mDisplayFrame=" + w.mDisplayFrame);
573 }
574 }
575 };
576
577 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
578 if (w.mLayoutAttached) {
579 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
580 + " mViewVisibility=" + w.mViewVisibility
581 + " mRelayoutCalled=" + w.mRelayoutCalled);
582 // If this view is GONE, then skip it -- keep the current frame, and let the caller
583 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
584 // windows, since that means "perform layout as normal, just don't display").
585 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
586 return;
587 }
588 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
589 || w.mLayoutNeeded) {
590 if (mTmpInitial) {
591 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
592 w.mContentChanged = false;
593 }
594 w.mLayoutNeeded = false;
595 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000596 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800597 w.mLayoutSeq = mService.mLayoutSeq;
598 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
599 + " mContainingFrame=" + w.mContainingFrame
600 + " mDisplayFrame=" + w.mDisplayFrame);
601 }
602 } else if (w.mAttrs.type == TYPE_DREAM) {
603 // Don't layout windows behind a dream, so that if it does stuff like hide the
604 // status bar we won't get a bad transition when it goes away.
605 mTmpWindow = mTmpWindow2;
606 }
607 };
608
609 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
610 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
611 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
612 return w.canBeImeTarget();
613 };
614
615 private final Consumer<WindowState> mApplyPostLayoutPolicy =
616 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
617 mService.mInputMethodTarget);
618
619 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
620 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
621 final boolean obscuredChanged = w.mObscured !=
622 mTmpApplySurfaceChangesTransactionState.obscured;
623 final RootWindowContainer root = mService.mRoot;
624 // Only used if default window
625 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
626
627 // Update effect.
628 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
629 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
630 final boolean isDisplayed = w.isDisplayedLw();
631
632 if (isDisplayed && w.isObscuringDisplay()) {
633 // This window completely covers everything behind it, so we want to leave all
634 // of them as undimmed (for performance reasons).
635 root.mObscuringWindow = w;
636 mTmpApplySurfaceChangesTransactionState.obscured = true;
637 }
638
639 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
640 root.handleNotObscuredLocked(w,
641 mTmpApplySurfaceChangesTransactionState.obscured,
642 mTmpApplySurfaceChangesTransactionState.syswin);
643
644 if (w.mHasSurface && isDisplayed) {
645 final int type = w.mAttrs.type;
646 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
647 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
648 mTmpApplySurfaceChangesTransactionState.syswin = true;
649 }
650 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
651 && w.mAttrs.preferredRefreshRate != 0) {
652 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
653 = w.mAttrs.preferredRefreshRate;
654 }
655 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
656 && w.mAttrs.preferredDisplayModeId != 0) {
657 mTmpApplySurfaceChangesTransactionState.preferredModeId
658 = w.mAttrs.preferredDisplayModeId;
659 }
660 }
661 }
662
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800663 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
664 && mWallpaperController.isWallpaperTarget(w)) {
665 // This is the wallpaper target and its obscured state changed... make sure the
666 // current wallpaper's visibility has been updated accordingly.
667 mWallpaperController.updateWallpaperVisibility();
668 }
669
Jorim Jaggia5e10572017-11-15 14:36:26 +0100670 // Use mTmpTransaction instead of mPendingTransaction because we don't want to commit
671 // other changes in mPendingTransaction at this point.
672 w.handleWindowMovedIfNeeded(mTmpTransaction);
673 SurfaceControl.mergeToGlobalTransaction(mTmpTransaction);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800674
675 final WindowStateAnimator winAnimator = w.mWinAnimator;
676
677 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
678 w.mContentChanged = false;
679
680 // Moved from updateWindowsAndWallpaperLocked().
681 if (w.mHasSurface) {
682 // Take care of the window being ready to display.
683 final boolean committed = winAnimator.commitFinishDrawingLocked();
684 if (isDefaultDisplay && committed) {
685 if (w.mAttrs.type == TYPE_DREAM) {
686 // HACK: When a dream is shown, it may at that point hide the lock screen.
687 // So we need to redo the layout to let the phone window manager make this
688 // happen.
689 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
690 if (DEBUG_LAYOUT_REPEATS) {
691 surfacePlacer.debugLayoutRepeats(
692 "dream and commitFinishDrawingLocked true",
693 pendingLayoutChanges);
694 }
695 }
696 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
697 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
698 "First draw done in potential wallpaper target " + w);
699 root.mWallpaperMayChange = true;
700 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
701 if (DEBUG_LAYOUT_REPEATS) {
702 surfacePlacer.debugLayoutRepeats(
703 "wallpaper and commitFinishDrawingLocked true",
704 pendingLayoutChanges);
705 }
706 }
707 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700708 final TaskStack stack = w.getStack();
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200709 if (!winAnimator.isWaitingForOpening()
Robert Carr2117aaf2017-04-25 14:46:50 -0700710 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800711 // Updates the shown frame before we set up the surface. This is needed
712 // because the resizing could change the top-left position (in addition to
713 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
714 // position the surface.
715 //
716 // If an animation is being started, we can't call this method because the
717 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700718 // we do want to update the position if the window is animating. We make an exception
719 // for the bounds animating state, where an application may have been waiting
720 // for an exit animation to start, but instead enters PiP. We need to ensure
721 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800722 winAnimator.computeShownFrameLocked();
723 }
724 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
Jorim Jaggia5e10572017-11-15 14:36:26 +0100725
726 // Since setSurfaceBoundariesLocked applies the clipping, we need to apply the position
727 // to the surface of the window container as well. Use mTmpTransaction instead of
728 // mPendingTransaction to avoid committing any existing changes in there.
729 w.updateSurfacePosition(mTmpTransaction);
730 SurfaceControl.mergeToGlobalTransaction(mTmpTransaction);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800731 }
732
733 final AppWindowToken atoken = w.mAppToken;
734 if (atoken != null) {
735 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
736 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
737 mTmpUpdateAllDrawn.add(atoken);
738 }
739 }
740
741 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
742 && w.isDisplayedLw()) {
743 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
744 }
745
746 w.updateResizingWindowIfNeeded();
747 };
748
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800749 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800750 * Create new {@link DisplayContent} instance, add itself to the root window container and
751 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700752 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800753 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700754 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
755 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700756 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700757 DisplayContent(Display display, WindowManagerService service,
Robert Carrb1579c82017-09-05 14:54:47 -0700758 WallpaperController wallpaperController) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100759 super(service);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800760 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
761 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
762 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
763 + " new=" + display);
764 }
765
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700766 mDisplay = display;
767 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700768 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700769 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700770 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700771 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000772 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700773 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800774 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700775 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700776
Robert Carrb1579c82017-09-05 14:54:47 -0700777 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth);
778
779 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
780 .setSize(mSurfaceSize, mSurfaceSize)
781 .setOpaque(true);
782 mWindowingLayer = b.setName("Display Root").build();
783 mOverlayLayer = b.setName("Display Overlays").build();
784
Robert Carrf59b8dd2017-10-02 18:58:36 -0700785 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700786 .setLayerStack(mWindowingLayer, mDisplayId)
787 .show(mWindowingLayer)
788 .setLayer(mOverlayLayer, 1)
789 .setLayerStack(mOverlayLayer, mDisplayId)
790 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700791 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700792
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700793 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700794 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700795 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700796 super.addChild(mAboveAppWindowsContainers, null);
797 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800798
799 // Add itself as a child to the root container.
800 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700801
802 // TODO(b/62541591): evaluate whether this is the best spot to declare the
803 // {@link DisplayContent} ready for use.
804 mDisplayReady = true;
805 }
806
807 boolean isReady() {
808 // The display is ready when the system and the individual display are both ready.
809 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700810 }
811
812 int getDisplayId() {
813 return mDisplayId;
814 }
815
Wale Ogunwale02319a62016-09-26 15:21:22 -0700816 WindowToken getWindowToken(IBinder binder) {
817 return mTokenMap.get(binder);
818 }
819
820 AppWindowToken getAppWindowToken(IBinder binder) {
821 final WindowToken token = getWindowToken(binder);
822 if (token == null) {
823 return null;
824 }
825 return token.asAppWindowToken();
826 }
827
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700828 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700829 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
830 if (dc != null) {
831 // We currently don't support adding a window token to the display if the display
832 // already has the binder mapped to another token. If there is a use case for supporting
833 // this moving forward we will either need to merge the WindowTokens some how or have
834 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700835 throw new IllegalArgumentException("Can't map token=" + token + " to display="
836 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700837 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700838 if (binder == null) {
839 throw new IllegalArgumentException("Can't map token=" + token + " to display="
840 + getName() + " binder is null");
841 }
842 if (token == null) {
843 throw new IllegalArgumentException("Can't map null token to display="
844 + getName() + " binder=" + binder);
845 }
846
Wale Ogunwale02319a62016-09-26 15:21:22 -0700847 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700848
849 if (token.asAppWindowToken() == null) {
850 // Add non-app token to container hierarchy on the display. App tokens are added through
851 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700852 switch (token.windowType) {
853 case TYPE_WALLPAPER:
854 mBelowAppWindowsContainers.addChild(token);
855 break;
856 case TYPE_INPUT_METHOD:
857 case TYPE_INPUT_METHOD_DIALOG:
858 mImeWindowsContainers.addChild(token);
859 break;
860 default:
861 mAboveAppWindowsContainers.addChild(token);
862 break;
863 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700864 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700865 }
866
867 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700868 final WindowToken token = mTokenMap.remove(binder);
869 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800870 token.setExiting();
871 }
872 return token;
873 }
874
875 /** Changes the display the input window token is housed on to this one. */
876 void reParentWindowToken(WindowToken token) {
877 final DisplayContent prevDc = token.getDisplayContent();
878 if (prevDc == this) {
879 return;
880 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800881 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
882 && token.asAppWindowToken() == null) {
883 // Removed the token from the map, but made sure it's not an app token before removing
884 // from parent.
885 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700886 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800887
888 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700889 }
890
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700891 void removeAppToken(IBinder binder) {
892 final WindowToken token = removeWindowToken(binder);
893 if (token == null) {
894 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
895 return;
896 }
897
898 final AppWindowToken appToken = token.asAppWindowToken();
899
900 if (appToken == null) {
901 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
902 return;
903 }
904
905 appToken.onRemovedFromDisplay();
906 }
907
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700908 Display getDisplay() {
909 return mDisplay;
910 }
911
Craig Mautner59c00972012-07-30 12:10:24 -0700912 DisplayInfo getDisplayInfo() {
913 return mDisplayInfo;
914 }
915
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700916 DisplayMetrics getDisplayMetrics() {
917 return mDisplayMetrics;
918 }
919
Andrii Kulian8ee72852017-03-10 10:36:45 -0800920 int getRotation() {
921 return mRotation;
922 }
923
924 void setRotation(int newRotation) {
925 mRotation = newRotation;
926 }
927
928 int getLastOrientation() {
929 return mLastOrientation;
930 }
931
932 void setLastOrientation(int orientation) {
933 mLastOrientation = orientation;
934 }
935
936 boolean getAltOrientation() {
937 return mAltOrientation;
938 }
939
940 void setAltOrientation(boolean altOrientation) {
941 mAltOrientation = altOrientation;
942 }
943
944 int getLastWindowForcedOrientation() {
945 return mLastWindowForcedOrientation;
946 }
947
Andrii Kulian06d07d62017-03-14 11:11:47 -0700948 /**
949 * Update rotation of the display.
950 *
951 * Returns true if the rotation has been changed. In this case YOU MUST CALL
952 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
953 */
954 boolean updateRotationUnchecked(boolean inTransaction) {
955 if (mService.mDeferredRotationPauseCount > 0) {
956 // Rotation updates have been paused temporarily. Defer the update until
957 // updates have been resumed.
958 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
959 return false;
960 }
961
962 ScreenRotationAnimation screenRotationAnimation =
963 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
964 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
965 // Rotation updates cannot be performed while the previous rotation change
966 // animation is still in progress. Skip this update. We will try updating
967 // again after the animation is finished and the display is unfrozen.
968 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
969 return false;
970 }
971 if (mService.mDisplayFrozen) {
972 // Even if the screen rotation animation has finished (e.g. isAnimating
973 // returns false), there is still some time where we haven't yet unfrozen
974 // the display. We also need to abort rotation here.
975 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
976 "Deferring rotation, still finishing previous rotation");
977 return false;
978 }
979
980 if (!mService.mDisplayEnabled) {
981 // No point choosing a rotation if the display is not enabled.
982 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
983 return false;
984 }
985
986 final int oldRotation = mRotation;
987 final int lastOrientation = mLastOrientation;
988 final boolean oldAltOrientation = mAltOrientation;
989 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr0e007272017-10-02 13:00:55 -0700990 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
Robert Carr897215d2017-03-29 12:25:50 -0700991 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700992
Robert Carr0e007272017-10-02 13:00:55 -0700993 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700994 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
995 if (seamlessRotated != null) {
996 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
997 // to complete (that is, waiting for windows to redraw). It's tempting to check
998 // w.mSeamlessRotationCount but that could be incorrect in the case of
999 // window-removal.
1000 return false;
1001 }
Robert Carr0e007272017-10-02 13:00:55 -07001002
1003 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001004 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001005 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001006 mayRotateSeamlessly = false;
1007 }
1008 for (int i = 0; i < mService.mSessions.size(); i++) {
1009 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1010 mayRotateSeamlessly = false;
1011 break;
1012 }
1013 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001014 }
Robert Carr0e007272017-10-02 13:00:55 -07001015 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001016
1017 // TODO: Implement forced rotation changes.
1018 // Set mAltOrientation to indicate that the application is receiving
1019 // an orientation that has different metrics than it expected.
1020 // eg. Portrait instead of Landscape.
1021
1022 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
1023 lastOrientation, rotation);
1024
1025 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
1026 + ", got rotation " + rotation + " which has "
1027 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1028
1029 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1030 // No change.
1031 return false;
1032 }
1033
1034 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
1035 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1036 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1037
1038 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1039 mService.mWaitingForConfig = true;
1040 }
1041
1042 mRotation = rotation;
1043 mAltOrientation = altOrientation;
1044 if (isDefaultDisplay) {
1045 mService.mPolicy.setRotationLw(rotation);
1046 }
1047
1048 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1049 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1050 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1051 WINDOW_FREEZE_TIMEOUT_DURATION);
1052
1053 setLayoutNeeded();
1054 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001055 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001056
1057 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001058 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001059 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1060 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1061 mDisplayId);
1062 } else {
1063 // The screen rotation animation uses a screenshot to freeze the screen
1064 // while windows resize underneath.
1065 // When we are rotating seamlessly, we allow the elements to transition
1066 // to their rotated state independently and without a freeze required.
1067 screenRotationAnimation = null;
1068
1069 // We have to reset this in case a window was removed before it
1070 // finished seamless rotation.
1071 mService.mSeamlessRotationCount = 0;
1072 }
1073
1074 // We need to update our screen size information to match the new rotation. If the rotation
1075 // has actually changed then this method will return true and, according to the comment at
1076 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1077 // By updating the Display info here it will be available to
1078 // #computeScreenConfiguration() later.
1079 updateDisplayAndOrientation(getConfiguration().uiMode);
1080
1081 if (!inTransaction) {
1082 if (SHOW_TRANSACTIONS) {
1083 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1084 }
1085 mService.openSurfaceTransaction();
1086 }
1087 try {
1088 // NOTE: We disable the rotation in the emulator because
1089 // it doesn't support hardware OpenGL emulation yet.
1090 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1091 && screenRotationAnimation.hasScreenshot()) {
Robert Carrb1579c82017-09-05 14:54:47 -07001092 if (screenRotationAnimation.setRotationInTransaction(rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001093 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1094 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1095 mService.scheduleAnimationLocked();
1096 }
1097 }
1098
1099 if (rotateSeamlessly) {
1100 forAllWindows(w -> {
1101 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1102 }, true /* traverseTopToBottom */);
1103 }
1104
1105 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1106 } finally {
1107 if (!inTransaction) {
Adrian Roos111aff92017-09-27 18:11:46 +02001108 mService.closeSurfaceTransaction("setRotationUnchecked");
Andrii Kulian06d07d62017-03-14 11:11:47 -07001109 if (SHOW_LIGHT_TRANSACTIONS) {
1110 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1111 }
1112 }
1113 }
1114
1115 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001116 if (w.mHasSurface && !rotateSeamlessly) {
1117 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001118 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001119 mService.mRoot.mOrientationChangeComplete = false;
1120 w.mLastFreezeDuration = 0;
1121 }
1122 w.mReportOrientationChanged = true;
1123 }, true /* traverseTopToBottom */);
1124
1125 if (rotateSeamlessly) {
1126 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1127 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1128 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1129 }
1130
1131 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1132 final WindowManagerService.RotationWatcher rotationWatcher
1133 = mService.mRotationWatchers.get(i);
1134 if (rotationWatcher.mDisplayId == mDisplayId) {
1135 try {
1136 rotationWatcher.mWatcher.onRotationChanged(rotation);
1137 } catch (RemoteException e) {
1138 // Ignore
1139 }
1140 }
1141 }
1142
1143 // TODO (multi-display): Magnification is supported only for the default display.
1144 // Announce rotation only if we will not animate as we already have the
1145 // windows in final state. Otherwise, we make this call at the rotation end.
1146 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1147 && isDefaultDisplay) {
1148 mService.mAccessibilityController.onRotationChangedLocked(this);
1149 }
1150
1151 return true;
1152 }
1153
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001154 void configureDisplayPolicy() {
1155 mService.mPolicy.setInitialDisplaySize(getDisplay(),
1156 mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1157
1158 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
1159 }
1160
Andrii Kulian06d07d62017-03-14 11:11:47 -07001161 /**
1162 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1163 * changed.
1164 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1165 */
1166 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1167 // Use the effective "visual" dimensions based on current rotation
1168 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1169 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1170 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1171 int dw = realdw;
1172 int dh = realdh;
1173
1174 if (mAltOrientation) {
1175 if (realdw > realdh) {
1176 // Turn landscape into portrait.
1177 int maxw = (int)(realdh/1.3f);
1178 if (maxw < realdw) {
1179 dw = maxw;
1180 }
1181 } else {
1182 // Turn portrait into landscape.
1183 int maxh = (int)(realdw/1.3f);
1184 if (maxh < realdh) {
1185 dh = maxh;
1186 }
1187 }
1188 }
1189
1190 // Update application display metrics.
1191 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1192 mDisplayId);
1193 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1194 mDisplayId);
1195 mDisplayInfo.rotation = mRotation;
1196 mDisplayInfo.logicalWidth = dw;
1197 mDisplayInfo.logicalHeight = dh;
1198 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1199 mDisplayInfo.appWidth = appWidth;
1200 mDisplayInfo.appHeight = appHeight;
1201 if (isDefaultDisplay) {
1202 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1203 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1204 }
1205 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1206 if (mDisplayScalingDisabled) {
1207 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1208 } else {
1209 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1210 }
1211
1212 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1213 mDisplayInfo);
1214
1215 mBaseDisplayRect.set(0, 0, dw, dh);
1216
1217 if (isDefaultDisplay) {
1218 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1219 mCompatDisplayMetrics);
1220 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001221
1222 updateBounds();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001223 return mDisplayInfo;
1224 }
1225
1226 /**
1227 * Compute display configuration based on display properties and policy settings.
1228 * Do not call if mDisplayReady == false.
1229 */
1230 void computeScreenConfiguration(Configuration config) {
1231 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1232
1233 final int dw = displayInfo.logicalWidth;
1234 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001235 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1236 // TODO: Probably best to set this based on some setting in the display content object,
1237 // so the display can be configured for things like fullscreen.
1238 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001239
Andrii Kulian06d07d62017-03-14 11:11:47 -07001240 config.screenWidthDp =
1241 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1242 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1243 config.screenHeightDp =
1244 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1245 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001246
1247 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1248 final int leftInset = mTmpRect.left;
1249 final int topInset = mTmpRect.top;
1250 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001251 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1252 leftInset + displayInfo.appWidth /* right */,
1253 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001254 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1255 || displayInfo.rotation == Surface.ROTATION_270);
1256
1257 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1258 mDisplayMetrics.density, config);
1259
1260 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1261 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1262 ? Configuration.SCREENLAYOUT_ROUND_YES
1263 : Configuration.SCREENLAYOUT_ROUND_NO);
1264
1265 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1266 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1267 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1268 dh, mDisplayId);
1269 config.densityDpi = displayInfo.logicalDensityDpi;
1270
1271 config.colorMode =
1272 (displayInfo.isHdr()
1273 ? Configuration.COLOR_MODE_HDR_YES
1274 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001275 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001276 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1277 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1278
1279 // Update the configuration based on available input devices, lid switch,
1280 // and platform configuration.
1281 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1282 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1283 config.navigation = Configuration.NAVIGATION_NONAV;
1284
1285 int keyboardPresence = 0;
1286 int navigationPresence = 0;
1287 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1288 final int len = devices != null ? devices.length : 0;
1289 for (int i = 0; i < len; i++) {
1290 InputDevice device = devices[i];
1291 if (!device.isVirtual()) {
1292 final int sources = device.getSources();
1293 final int presenceFlag = device.isExternal() ?
1294 WindowManagerPolicy.PRESENCE_EXTERNAL :
1295 WindowManagerPolicy.PRESENCE_INTERNAL;
1296
1297 // TODO(multi-display): Configure on per-display basis.
1298 if (mService.mIsTouchDevice) {
1299 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1300 InputDevice.SOURCE_TOUCHSCREEN) {
1301 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1302 }
1303 } else {
1304 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1305 }
1306
1307 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1308 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1309 navigationPresence |= presenceFlag;
1310 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1311 && config.navigation == Configuration.NAVIGATION_NONAV) {
1312 config.navigation = Configuration.NAVIGATION_DPAD;
1313 navigationPresence |= presenceFlag;
1314 }
1315
1316 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1317 config.keyboard = Configuration.KEYBOARD_QWERTY;
1318 keyboardPresence |= presenceFlag;
1319 }
1320 }
1321 }
1322
1323 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1324 config.navigation = Configuration.NAVIGATION_DPAD;
1325 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1326 }
1327
1328 // Determine whether a hard keyboard is available and enabled.
1329 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1330 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1331 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1332 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1333 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1334 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1335 }
1336
1337 // Let the policy update hidden states.
1338 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1339 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1340 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1341 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1342 }
1343
1344 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1345 int displayId) {
1346 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1347 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1348 final int unrotDw, unrotDh;
1349 if (rotated) {
1350 unrotDw = dh;
1351 unrotDh = dw;
1352 } else {
1353 unrotDw = dw;
1354 unrotDh = dh;
1355 }
1356 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1357 displayId);
1358 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1359 displayId);
1360 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1361 displayId);
1362 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1363 displayId);
1364 return sw;
1365 }
1366
1367 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1368 DisplayMetrics dm, int dw, int dh, int displayId) {
1369 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1370 displayId);
1371 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1372 uiMode, displayId);
1373 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1374 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1375 if (curSize == 0 || size < curSize) {
1376 curSize = size;
1377 }
1378 return curSize;
1379 }
1380
1381 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1382 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1383
1384 // We need to determine the smallest width that will occur under normal
1385 // operation. To this, start with the base screen size and compute the
1386 // width under the different possible rotations. We need to un-rotate
1387 // the current screen dimensions before doing this.
1388 int unrotDw, unrotDh;
1389 if (rotated) {
1390 unrotDw = dh;
1391 unrotDh = dw;
1392 } else {
1393 unrotDw = dw;
1394 unrotDh = dh;
1395 }
1396 displayInfo.smallestNominalAppWidth = 1<<30;
1397 displayInfo.smallestNominalAppHeight = 1<<30;
1398 displayInfo.largestNominalAppWidth = 0;
1399 displayInfo.largestNominalAppHeight = 0;
1400 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1401 unrotDh);
1402 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1403 unrotDw);
1404 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1405 unrotDh);
1406 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1407 unrotDw);
1408 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1409 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1410 displayId);
1411 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1412 displayId);
1413 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1414 displayId);
1415 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1416 displayId);
1417 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1418 outConfig.screenLayout = sl;
1419 }
1420
1421 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1422 int uiMode, int displayId) {
1423 // Get the app screen size at this rotation.
1424 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1425 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1426
1427 // Compute the screen layout size class for this rotation.
1428 int longSize = w;
1429 int shortSize = h;
1430 if (longSize < shortSize) {
1431 int tmp = longSize;
1432 longSize = shortSize;
1433 shortSize = tmp;
1434 }
1435 longSize = (int)(longSize/density);
1436 shortSize = (int)(shortSize/density);
1437 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1438 }
1439
1440 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1441 int uiMode, int dw, int dh) {
1442 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1443 displayId);
1444 if (width < displayInfo.smallestNominalAppWidth) {
1445 displayInfo.smallestNominalAppWidth = width;
1446 }
1447 if (width > displayInfo.largestNominalAppWidth) {
1448 displayInfo.largestNominalAppWidth = width;
1449 }
1450 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1451 displayId);
1452 if (height < displayInfo.smallestNominalAppHeight) {
1453 displayInfo.smallestNominalAppHeight = height;
1454 }
1455 if (height > displayInfo.largestNominalAppHeight) {
1456 displayInfo.largestNominalAppHeight = height;
1457 }
1458 }
1459
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001460 DockedStackDividerController getDockedDividerController() {
1461 return mDividerControllerLocked;
1462 }
1463
Winson Chung655332c2016-10-31 13:14:28 -07001464 PinnedStackController getPinnedStackController() {
1465 return mPinnedStackControllerLocked;
1466 }
1467
Jeff Browna506a6e2013-06-04 00:02:38 -07001468 /**
1469 * Returns true if the specified UID has access to this display.
1470 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001471 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001472 return mDisplay.hasAccess(uid);
1473 }
1474
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001475 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001476 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001477 }
1478
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001479 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001480 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001481 }
1482
Wale Ogunwale61911492017-10-11 08:50:50 -07001483 /**
1484 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1485 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001486 TaskStack getSplitScreenPrimaryStack() {
1487 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001488 return (stack != null && stack.isVisible()) ? stack : null;
1489 }
1490
1491 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001492 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001493 * not visible.
1494 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001495 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1496 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001497 }
1498
1499 TaskStack getPinnedStack() {
1500 return mTaskStackContainers.getPinnedStack();
1501 }
1502
1503 private boolean hasPinnedStack() {
1504 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001505 }
1506
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001507 /**
1508 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1509 * Null is no compatible stack on the display.
1510 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001511 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001512 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1513 }
1514
1515 /**
1516 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1517 * activity type. Null is no compatible stack on the display.
1518 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001519 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001520 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001521 }
1522
Bryce Lee48f4b572017-04-10 10:54:15 -07001523 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001524 TaskStack getTopStack() {
1525 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001526 }
1527
Wale Ogunwale61911492017-10-11 08:50:50 -07001528 void onStackWindowingModeChanged(TaskStack stack) {
1529 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001530 }
1531
Andrii Kulian441e4492016-09-29 15:25:00 -07001532 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001533 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001534 super.onConfigurationChanged(newParentConfig);
1535
Andrii Kulian3a507b52016-09-19 18:14:12 -07001536 // The display size information is heavily dependent on the resources in the current
1537 // configuration, so we need to reconfigure it every time the configuration changes.
1538 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1539 mService.reconfigureDisplayLocked(this);
1540
Bryce Leef3c6a472017-11-14 14:53:06 -08001541 final DockedStackDividerController dividerController = getDockedDividerController();
1542
1543 if (dividerController != null) {
1544 getDockedDividerController().onConfigurationChanged();
1545 }
1546
1547 final PinnedStackController pinnedStackController = getPinnedStackController();
1548
1549 if (pinnedStackController != null) {
1550 getPinnedStackController().onConfigurationChanged();
1551 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001552 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001553
Andrii Kulian441e4492016-09-29 15:25:00 -07001554 /**
1555 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1556 * bounds were updated.
1557 */
1558 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001559 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1560 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001561 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001562 changedStackList.add(stack.mStackId);
1563 }
1564 }
Winson Chung32c566f2017-04-11 18:31:21 -07001565
1566 // If there was no pinned stack, we still need to notify the controller of the display info
1567 // update as a result of the config change. We do this here to consolidate the flow between
1568 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001569 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001570 mPinnedStackControllerLocked.onDisplayInfoChanged();
1571 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001572 }
1573
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001574 @Override
1575 boolean fillsParent() {
1576 return true;
1577 }
1578
1579 @Override
1580 boolean isVisible() {
1581 return true;
1582 }
1583
1584 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001585 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001586 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001587 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001588 }
1589
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001590 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001591 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1592 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1593 // target, or here in order if there isn't an IME target.
1594 if (traverseTopToBottom) {
1595 for (int i = mChildren.size() - 1; i >= 0; --i) {
1596 final DisplayChildWindowContainer child = mChildren.get(i);
1597 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1598 // In this case the Ime windows will be processed above their target so we skip
1599 // here.
1600 continue;
1601 }
1602 if (child.forAllWindows(callback, traverseTopToBottom)) {
1603 return true;
1604 }
1605 }
1606 } else {
1607 final int count = mChildren.size();
1608 for (int i = 0; i < count; i++) {
1609 final DisplayChildWindowContainer child = mChildren.get(i);
1610 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1611 // In this case the Ime windows will be processed above their target so we skip
1612 // here.
1613 continue;
1614 }
1615 if (child.forAllWindows(callback, traverseTopToBottom)) {
1616 return true;
1617 }
1618 }
1619 }
1620 return false;
1621 }
1622
1623 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1624 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1625 }
1626
Wale Ogunwale399c8692017-05-08 14:22:42 -07001627 @Override
1628 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001629 final WindowManagerPolicy policy = mService.mPolicy;
1630
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001631 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001632 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001633 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001634 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001635 // If the display is frozen, some activities may be in the middle of restarting, and
1636 // thus have removed their old window. If the window has the flag to hide the lock
1637 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1638 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001639 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001640 } else if (policy.isKeyguardLocked()) {
1641 // Use the last orientation the while the display is frozen with the keyguard
1642 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1643 // window. We don't want to check the show when locked window directly though as
1644 // things aren't stable while the display is frozen, for example the window could be
1645 // momentarily unavailable due to activity relaunch.
1646 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001647 + "return " + mLastOrientation);
1648 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001649 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001650 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001651 final int orientation = mAboveAppWindowsContainers.getOrientation();
1652 if (orientation != SCREEN_ORIENTATION_UNSET) {
1653 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001654 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001655 }
1656
Wale Ogunwale399c8692017-05-08 14:22:42 -07001657 // Top system windows are not requesting an orientation. Start searching from apps.
1658 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001659 }
1660
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001661 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001662 // Check if display metrics changed and update base values if needed.
1663 updateBaseDisplayMetricsIfNeeded();
1664
Craig Mautner722285e2012-09-07 13:55:58 -07001665 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001666 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001667
Wale Ogunwale61911492017-10-11 08:50:50 -07001668 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1669 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001670 }
Craig Mautner722285e2012-09-07 13:55:58 -07001671 }
1672
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001673 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001674 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1675 if (displayManagerInternal != null) {
1676 // Bootstrap the default logical display from the display manager.
1677 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1678 if (newDisplayInfo != null) {
1679 mDisplayInfo.copyFrom(newDisplayInfo);
1680 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001681 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001682
Andrii Kuliancd097992017-03-23 18:31:59 -07001683 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1684 mDisplayInfo.logicalDensityDpi);
1685 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1686 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1687 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001688 }
1689
Andrii Kuliancd097992017-03-23 18:31:59 -07001690 /**
1691 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1692 * values.
1693 */
1694 private void updateBaseDisplayMetricsIfNeeded() {
1695 // Get real display metrics without overrides from WM.
1696 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1697 final int orientation = mDisplayInfo.rotation;
1698 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1699 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1700 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1701 final int newDensity = mDisplayInfo.logicalDensityDpi;
1702
1703 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1704 || mInitialDisplayHeight != newHeight
1705 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1706
1707 if (displayMetricsChanged) {
1708 // Check if display size or density is forced.
1709 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1710 || mBaseDisplayHeight != mInitialDisplayHeight;
1711 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1712
1713 // If there is an override set for base values - use it, otherwise use new values.
1714 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1715 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1716 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1717
1718 // Real display metrics changed, so we should also update initial values.
1719 mInitialDisplayWidth = newWidth;
1720 mInitialDisplayHeight = newHeight;
1721 mInitialDisplayDensity = newDensity;
1722 mService.reconfigureDisplayLocked(this);
1723 }
1724 }
1725
Bryce Lee27cec322017-03-21 09:41:37 -07001726 /** Sets the maximum width the screen resolution can be */
1727 void setMaxUiWidth(int width) {
1728 if (DEBUG_DISPLAY) {
1729 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1730 }
1731
1732 mMaxUiWidth = width;
1733
1734 // Update existing metrics.
1735 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1736 }
1737
1738 /** Update base (override) display metrics. */
1739 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1740 mBaseDisplayWidth = baseWidth;
1741 mBaseDisplayHeight = baseHeight;
1742 mBaseDisplayDensity = baseDensity;
1743
1744 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1745 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1746 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1747 mBaseDisplayWidth = mMaxUiWidth;
1748
1749 if (DEBUG_DISPLAY) {
1750 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1751 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1752 + " on display:" + getDisplayId());
1753 }
1754 }
1755
1756 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08001757
1758 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07001759 }
1760
Wale Ogunwaledb506192017-12-08 10:57:32 -08001761 void getStableRect(Rect out) {
1762 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08001763 }
1764
Wale Ogunwale61911492017-10-11 08:50:50 -07001765 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001766 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1767 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001768
Wale Ogunwale61911492017-10-11 08:50:50 -07001769 final TaskStack stack = new TaskStack(mService, stackId, controller);
1770 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001771 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001772 }
1773
Andrii Kulian51c1b672017-04-07 18:39:32 -07001774 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001775 final DisplayContent prevDc = stack.getDisplayContent();
1776 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001777 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1778 + " which is not currently attached to any display");
1779 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001780 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001781 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1782 + " to its current displayId=" + mDisplayId);
1783 }
1784
Wale Ogunwale61911492017-10-11 08:50:50 -07001785 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001786 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001787 }
1788
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001789 @Override
1790 protected void addChild(DisplayChildWindowContainer child,
1791 Comparator<DisplayChildWindowContainer> comparator) {
1792 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1793 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001794
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001795 @Override
1796 protected void addChild(DisplayChildWindowContainer child, int index) {
1797 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1798 }
1799
1800 @Override
1801 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001802 // Only allow removal of direct children from this display if the display is in the process
1803 // of been removed.
1804 if (mRemovingDisplay) {
1805 super.removeChild(child);
1806 return;
1807 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001808 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001809 }
1810
Andrii Kuliand2765632016-12-12 22:26:34 -08001811 @Override
1812 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1813 // Children of the display are statically ordered, so the real intention here is to perform
1814 // the operation on the display and not the static direct children.
1815 getParent().positionChildAt(position, this, includingParents);
1816 }
1817
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001818 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001819 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1820 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001821 final int taskId = stack.taskIdFromPoint(x, y);
1822 if (taskId != -1) {
1823 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001824 }
1825 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001826 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001827 }
1828
Chong Zhang8e89b312015-09-09 15:09:30 -07001829 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001830 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001831 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001832 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001833 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001834 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001835 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07001836 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1837 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001838 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001839 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001840 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001841
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001842 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1843 if (mTmpTaskForResizePointSearchResult.searchDone) {
1844 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001845 }
1846 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001847 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001848 }
1849
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001850 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001851 // The provided task is the task on this display with focus, so if WindowManagerService's
1852 // focused app is not on this display, focusedTask will be null.
1853 if (focusedTask == null) {
1854 mTouchExcludeRegion.setEmpty();
1855 } else {
1856 mTouchExcludeRegion.set(mBaseDisplayRect);
1857 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1858 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07001859 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1860 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001861 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
1862 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08001863 }
1864 // If we removed the focused task above, add it back and only leave its
1865 // outside touch area in the exclusion. TapDectector is not interested in
1866 // any touch inside the focused task itself.
1867 if (!mTmpRect2.isEmpty()) {
1868 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1869 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001870 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001871 final WindowState inputMethod = mService.mInputMethodWindow;
1872 if (inputMethod != null && inputMethod.isVisibleLw()) {
1873 // If the input method is visible and the user is typing, we don't want these touch
1874 // events to be intercepted and used to change focus. This would likely cause a
1875 // disappearance of the input method.
1876 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001877 if (inputMethod.getDisplayId() == mDisplayId) {
1878 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1879 } else {
1880 // IME is on a different display, so we need to update its tap detector.
1881 // TODO(multidisplay): Remove when IME will always appear on same display.
1882 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1883 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001884 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001885 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1886 WindowState win = mTapExcludedWindows.get(i);
1887 win.getTouchableRegion(mTmpRegion);
1888 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1889 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001890 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07001891 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001892 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001893 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001894 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1895 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001896 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001897 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001898 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001899 }
1900
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001901 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001902 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001903 super.switchUser();
1904 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001905 }
1906
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001907 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001908 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1909 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001910 }
1911 }
1912
Wale Ogunwale10124582016-09-15 20:25:50 -07001913 @Override
1914 void removeIfPossible() {
1915 if (isAnimating()) {
1916 mDeferredRemoval = true;
1917 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001918 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001919 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001920 }
1921
Wale Ogunwale10124582016-09-15 20:25:50 -07001922 @Override
1923 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001924 mRemovingDisplay = true;
1925 try {
1926 super.removeImmediately();
1927 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07001928 if (mService.canDispatchPointerEvents()) {
1929 if (mTapDetector != null) {
1930 mService.unregisterPointerEventListener(mTapDetector);
1931 }
1932 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
1933 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1934 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001935 }
Robert Carrb1579c82017-09-05 14:54:47 -07001936 // The pending transaction won't be applied so we should
1937 // just clean up any surfaces pending destruction.
1938 onPendingTransactionApplied();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001939 } finally {
1940 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001941 }
Craig Mautner95da1082014-02-24 17:54:35 -08001942 }
1943
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001944 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001945 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001946 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001947 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1948
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001949 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001950 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001951 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001952 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001953 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001954 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001955 }
1956
Andrii Kulian0214ed92017-05-16 13:44:05 -07001957 /** @return 'true' if removal of this display content is deferred due to active animation. */
1958 boolean isRemovalDeferred() {
1959 return mDeferredRemoval;
1960 }
1961
Wale Ogunwale10124582016-09-15 20:25:50 -07001962 boolean animateForIme(float interpolatedValue, float animationTarget,
1963 float dividerAnimationTarget) {
1964 boolean updated = false;
1965
Wale Ogunwale61911492017-10-11 08:50:50 -07001966 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1967 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001968 if (stack == null || !stack.isAdjustedForIme()) {
1969 continue;
1970 }
1971
1972 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1973 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1974 updated = true;
1975 } else {
1976 mDividerControllerLocked.mLastAnimationProgress =
1977 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1978 mDividerControllerLocked.mLastDividerProgress =
1979 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1980 updated |= stack.updateAdjustForIme(
1981 mDividerControllerLocked.mLastAnimationProgress,
1982 mDividerControllerLocked.mLastDividerProgress,
1983 false /* force */);
1984 }
1985 if (interpolatedValue >= 1f) {
1986 stack.endImeAdjustAnimation();
1987 }
1988 }
1989
1990 return updated;
1991 }
1992
1993 boolean clearImeAdjustAnimation() {
1994 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07001995 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1996 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001997 if (stack != null && stack.isAdjustedForIme()) {
1998 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1999 changed = true;
2000 }
2001 }
2002 return changed;
2003 }
2004
2005 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002006 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2007 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002008 if (stack.isVisible() && stack.isAdjustedForIme()) {
2009 stack.beginImeAdjustAnimation();
2010 }
2011 }
2012 }
2013
2014 void adjustForImeIfNeeded() {
2015 final WindowState imeWin = mService.mInputMethodWindow;
2016 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2017 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002018 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002019 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2020 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2021 imeTargetStack.getDockSide() : DOCKED_INVALID;
2022 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2023 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2024 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002025 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002026 final boolean imeHeightChanged = imeVisible &&
2027 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2028
2029 // The divider could be adjusted for IME position, or be thinner than usual,
2030 // or both. There are three possible cases:
2031 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2032 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2033 // - If IME is not visible, divider is not moved and is normal width.
2034
2035 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002036 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2037 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002038 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002039 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2040 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002041 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2042 } else {
2043 stack.resetAdjustedForIme(false);
2044 }
2045 }
2046 mDividerControllerLocked.setAdjustedForIme(
2047 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2048 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002049 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2050 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002051 stack.resetAdjustedForIme(!dockVisible);
2052 }
2053 mDividerControllerLocked.setAdjustedForIme(
2054 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2055 }
Winson Chung655332c2016-10-31 13:14:28 -07002056 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002057 }
2058
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002059 void setInputMethodAnimLayerAdjustment(int adj) {
2060 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2061 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002062 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002063 }
2064
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002065 /**
2066 * If a window that has an animation specifying a colored background and the current wallpaper
2067 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2068 * suddenly disappear.
2069 */
2070 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002071 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2072 w -> w.mIsWallpaper && w.isVisibleNow());
2073
2074 if (visibleWallpaper != null) {
2075 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002076 }
2077 return winAnimator.mAnimLayer;
2078 }
2079
Wale Ogunwale10124582016-09-15 20:25:50 -07002080 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002081 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2082 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002083 stack.prepareFreezingTaskBounds();
2084 }
2085 }
2086
Wale Ogunwale94744212015-09-21 19:01:47 -07002087 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002088 getBounds(mTmpRect, newRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002089
2090 // Compute a transform matrix to undo the coordinate space transformation,
2091 // and present the window at the same physical position it previously occupied.
2092 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2093 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2094
2095 mTmpRectF.set(bounds);
2096 mTmpMatrix.mapRect(mTmpRectF);
2097 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002098 }
2099
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002100 static int deltaRotation(int oldRotation, int newRotation) {
2101 int delta = newRotation - oldRotation;
2102 if (delta < 0) delta += 4;
2103 return delta;
2104 }
2105
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002106 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002107 Matrix outMatrix) {
2108 // For rotations without Z-ordering we don't need the target rectangle's position.
2109 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2110 displayHeight, outMatrix);
2111 }
2112
2113 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2114 float displayWidth, float displayHeight, Matrix outMatrix) {
2115 switch (rotation) {
2116 case ROTATION_0:
2117 outMatrix.reset();
2118 break;
2119 case ROTATION_270:
2120 outMatrix.setRotate(270, 0, 0);
2121 outMatrix.postTranslate(0, displayHeight);
2122 outMatrix.postTranslate(rectTop, 0);
2123 break;
2124 case ROTATION_180:
2125 outMatrix.reset();
2126 break;
2127 case ROTATION_90:
2128 outMatrix.setRotate(90, 0, 0);
2129 outMatrix.postTranslate(displayWidth, 0);
2130 outMatrix.postTranslate(-rectTop, rectLeft);
2131 break;
2132 }
2133 }
2134
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002135 @CallSuper
2136 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002137 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002138 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002139 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002140 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002141 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2142 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002143 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002144 }
2145 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2146 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002147 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2148 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002149 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002150 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002151 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2152 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002153 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002154 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002155 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2156 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002157 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002158 }
2159 proto.write(DPI, mBaseDisplayDensity);
2160 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002161 proto.write(ROTATION, mRotation);
2162 final ScreenRotationAnimation screenRotationAnimation =
2163 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2164 if (screenRotationAnimation != null) {
2165 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2166 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002167 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002168 proto.end(token);
2169 }
2170
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002171 @Override
2172 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2173 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002174 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2175 final String subPrefix = " " + prefix;
2176 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2177 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2178 pw.print("dpi");
2179 if (mInitialDisplayWidth != mBaseDisplayWidth
2180 || mInitialDisplayHeight != mBaseDisplayHeight
2181 || mInitialDisplayDensity != mBaseDisplayDensity) {
2182 pw.print(" base=");
2183 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2184 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2185 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002186 if (mDisplayScalingDisabled) {
2187 pw.println(" noscale");
2188 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002189 pw.print(" cur=");
2190 pw.print(mDisplayInfo.logicalWidth);
2191 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2192 pw.print(" app=");
2193 pw.print(mDisplayInfo.appWidth);
2194 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2195 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2196 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2197 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2198 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002199 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002200 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002201 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002202
Craig Mautnerdc548482014-02-05 13:35:24 -08002203 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002204 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002205 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2206 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002207 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002208 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002209
Craig Mautnerdc548482014-02-05 13:35:24 -08002210 pw.println();
2211 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002212 pw.println();
2213 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002214 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002215 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002216 pw.print(" Exiting #"); pw.print(i);
2217 pw.print(' '); pw.print(token);
2218 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002219 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002220 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002221 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002222
Jorim Jaggi31f71702016-05-04 16:43:04 -07002223 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002224
2225 // Dump stack references
2226 final TaskStack homeStack = getHomeStack();
2227 if (homeStack != null) {
2228 pw.println(prefix + "homeStack=" + homeStack.getName());
2229 }
2230 final TaskStack pinnedStack = getPinnedStack();
2231 if (pinnedStack != null) {
2232 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2233 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002234 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002235 if (splitScreenPrimaryStack != null) {
2236 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2237 }
2238
2239 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002240 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002241 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002242 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002243
2244 if (mInputMethodAnimLayerAdjustment != 0) {
2245 pw.println(subPrefix
2246 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2247 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002248
2249 pw.println();
2250 mDisplayFrames.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002251 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002252
2253 @Override
2254 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002255 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002256 }
2257
2258 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002259 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002260 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002261
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002262 /** Returns true if the stack in the windowing mode is visible. */
2263 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002264 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002265 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002266 }
2267
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002268 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002269 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002270 final int x = (int) xf;
2271 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002272 final WindowState touchedWin = getWindow(w -> {
2273 final int flags = w.mAttrs.flags;
2274 if (!w.isVisibleLw()) {
2275 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002276 }
2277 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002278 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002279 }
2280
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002281 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002282 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002283 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002284 }
2285
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002286 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002287
2288 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002289 return mTmpRegion.contains(x, y) || touchFlags == 0;
2290 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002291
2292 return touchedWin;
2293 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002294
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002295 boolean canAddToastWindowForUid(int uid) {
2296 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002297 // Also if the app is focused adding more than one toast at
2298 // a time for better backwards compatibility.
2299 final WindowState focusedWindowForUid = getWindow(w ->
2300 w.mOwnerUid == uid && w.isFocused());
2301 if (focusedWindowForUid != null) {
2302 return true;
2303 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002304 final WindowState win = getWindow(w ->
2305 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2306 && !w.mWindowRemovalAllowed);
2307 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002308 }
2309
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002310 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002311 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2312 return;
2313 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002314
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002315 // Used to communicate the old focus to the callback method.
2316 mTmpWindow = oldFocus;
2317
2318 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002319 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002320
2321 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002322 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002323
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002324 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002325
2326 if (mTmpWindow == null) {
2327 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2328 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002329 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002330 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002331 }
2332
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002333 /** Updates the layer assignment of windows on this display. */
2334 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002335 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002336 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002337 if (setLayoutNeeded) {
2338 setLayoutNeeded();
2339 }
Robert Carrb1579c82017-09-05 14:54:47 -07002340
Robert Carrf59b8dd2017-10-02 18:58:36 -07002341 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002342 // the application of this transaction until the animation pass triggers
2343 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2344 // the hiding and showing of surfaces.
2345 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002346 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002347 }
2348
Wale Ogunwale1666e312016-12-16 11:27:18 -08002349 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2350 // moving containers or resizing them. Need to investigate the best way to have it automatically
2351 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002352 void layoutAndAssignWindowLayersIfNeeded() {
2353 mService.mWindowsChanged = true;
2354 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002355
2356 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2357 false /*updateInputWindows*/)) {
2358 assignWindowLayers(false /* setLayoutNeeded */);
2359 }
2360
2361 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2362 mService.mWindowPlacerLocked.performSurfacePlacement();
2363 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2364 }
2365
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002366 /** Returns true if a leaked surface was destroyed */
2367 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002368 // Used to indicate that a surface was leaked.
2369 mTmpWindow = null;
2370 forAllWindows(w -> {
2371 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002372 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002373 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002374 }
2375 if (!mService.mSessions.contains(wsa.mSession)) {
2376 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002377 + w + " surface=" + wsa.mSurfaceController
2378 + " token=" + w.mToken
2379 + " pid=" + w.mSession.mPid
2380 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002381 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002382 mService.mForceRemoves.add(w);
2383 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002384 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002385 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002386 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002387 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002388 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002389 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002390 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002391 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002392 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002393
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002394 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002395 }
2396
2397 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002398 * Determine and return the window that should be the IME target.
2399 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2400 * @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 +00002401 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002402 WindowState computeImeTarget(boolean updateImeTarget) {
2403 if (mService.mInputMethodWindow == null) {
2404 // There isn't an IME so there shouldn't be a target...That was easy!
2405 if (updateImeTarget) {
2406 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2407 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2408 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2409 }
2410 return null;
2411 }
2412
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002413 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2414 // same display. Or even when the current IME/target are not on the same screen as the next
2415 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002416 mUpdateImeTarget = updateImeTarget;
2417 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002418
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002419
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002420 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2421 // to be on top of it, but it is not -really- where input will go. So look down below
2422 // for a real window to target...
2423 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2424 final AppWindowToken token = target.mAppToken;
2425 if (token != null) {
2426 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2427 if (betterTarget != null) {
2428 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002429 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002430 }
2431 }
2432
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002433 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2434 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002435
2436 // Now, a special case -- if the last target's window is in the process of exiting, and is
2437 // above the new target, keep on the last target to avoid flicker. Consider for example a
2438 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2439 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2440 // scrim.
2441 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002442 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2443 && (target == null
2444 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002445 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002446 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002447 }
2448
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002449 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2450 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002451
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002452 if (target == null) {
2453 if (updateImeTarget) {
2454 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2455 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2456 + Debug.getCallers(4) : ""));
2457 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2458 }
2459
2460 return null;
2461 }
2462
2463 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002464 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2465 if (token != null) {
2466
2467 // Now some fun for dealing with window animations that modify the Z order. We need
2468 // to look at all windows below the current target that are in this app, finding the
2469 // highest visible one in layering.
2470 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002471 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002472 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002473 }
2474
2475 if (highestTarget != null) {
2476 final AppTransition appTransition = mService.mAppTransition;
2477 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2478 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2479 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002480 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002481
2482 if (appTransition.isTransitionSet()) {
2483 // If we are currently setting up for an animation, hold everything until we
2484 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002485 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2486 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002487 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002488 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002489 // If the window we are currently targeting is involved with an animation,
2490 // and it is on top of the next target we will be over, then hold off on
2491 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002492 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2493 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002494 }
2495 }
2496 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002497
2498 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2499 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2500 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002501 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002502 }
2503
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002504 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002505 }
2506
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002507 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2508 if (target == mService.mInputMethodTarget
2509 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2510 && mInputMethodAnimLayerAdjustment == layerAdj) {
2511 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002512 }
2513
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002514 mService.mInputMethodTarget = target;
2515 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2516 setInputMethodAnimLayerAdjustment(layerAdj);
2517 assignWindowLayers(false /* setLayoutNeeded */);
2518 }
2519
2520 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2521 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2522 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2523 }
2524
2525 // Used to indicate we have reached the first window in the range we are interested in.
2526 mTmpWindow = null;
2527
2528 // TODO: Figure-out a more efficient way to do this.
2529 final WindowState candidate = getWindow(w -> {
2530 if (w == top) {
2531 // Reached the first window in the range we are interested in.
2532 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002533 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002534 if (mTmpWindow == null) {
2535 return false;
2536 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002537
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002538 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2539 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002540 }
2541 // If we reached the bottom of the range of windows we are considering,
2542 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002543 if (w == bottom) {
2544 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002545 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002546 return false;
2547 });
2548
2549 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002550 }
2551
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002552 void setLayoutNeeded() {
2553 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2554 mLayoutNeeded = true;
2555 }
2556
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002557 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002558 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2559 mLayoutNeeded = false;
2560 }
2561
2562 boolean isLayoutNeeded() {
2563 return mLayoutNeeded;
2564 }
2565
Wale Ogunwale02319a62016-09-26 15:21:22 -07002566 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2567 if (mTokenMap.isEmpty()) {
2568 return;
2569 }
2570 pw.println(" Display #" + mDisplayId);
2571 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2572 while (it.hasNext()) {
2573 final WindowToken token = it.next();
2574 pw.print(" ");
2575 pw.print(token);
2576 if (dumpAll) {
2577 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002578 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07002579 } else {
2580 pw.println();
2581 }
2582 }
2583 }
2584
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002585 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002586 final int[] index = new int[1];
2587 forAllWindows(w -> {
2588 final WindowStateAnimator wAnim = w.mWinAnimator;
2589 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2590 index[0] = index[0] + 1;
2591 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002592 }
2593
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002594 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002595 forAllWindows(w -> {
2596 w.mWinAnimator.enableSurfaceTrace(fd);
2597 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002598 }
2599
2600 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002601 forAllWindows(w -> {
2602 w.mWinAnimator.disableSurfaceTrace();
2603 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002604 }
2605
Jorim Jaggife762342016-10-13 14:33:27 +02002606 /**
2607 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2608 */
2609 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2610 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002611 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002612 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2613 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002614 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02002615 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002616 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002617 }
2618
Wale Ogunwale494009b82016-10-21 09:01:38 -07002619 boolean checkWaitingForWindows() {
2620
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002621 mHaveBootMsg = false;
2622 mHaveApp = false;
2623 mHaveWallpaper = false;
2624 mHaveKeyguard = true;
2625
2626 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002627 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2628 return true;
2629 }
2630 if (w.isDrawnLw()) {
2631 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002632 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002633 } else if (w.mAttrs.type == TYPE_APPLICATION
2634 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002635 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002636 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002637 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002638 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002639 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002640 }
2641 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002642 return false;
2643 });
2644
2645 if (visibleWindow != null) {
2646 // We have a visible window.
2647 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002648 }
2649
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002650 // if the wallpaper service is disabled on the device, we're never going to have
2651 // wallpaper, don't bother waiting for it
2652 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2653 com.android.internal.R.bool.config_enableWallpaperService)
2654 && !mService.mOnlyCore;
2655
Wale Ogunwale494009b82016-10-21 09:01:38 -07002656 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2657 "******** booted=" + mService.mSystemBooted
2658 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002659 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2660 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2661 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002662
2663 // If we are turning on the screen to show the boot message, don't do it until the boot
2664 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002665 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002666 return true;
2667 }
2668
2669 // If we are turning on the screen after the boot is completed normally, don't do so until
2670 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002671 if (mService.mSystemBooted
2672 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002673 return true;
2674 }
2675
2676 return false;
2677 }
2678
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002679 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002680 mTmpWindowAnimator = animator;
2681 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002682 }
2683
2684 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002685 resetAnimationBackgroundAnimator();
2686
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002687 // Used to indicate a detached wallpaper.
2688 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002689 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002690
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002691 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002692
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002693 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002694 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002695 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2696 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002697 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2698 }
2699 }
2700
Wale Ogunwale494009b82016-10-21 09:01:38 -07002701 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002702 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002703 if (imFocus == null) {
2704 return false;
2705 }
2706
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002707 if (DEBUG_INPUT_METHOD) {
2708 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2709 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2710 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002711 }
2712
Wale Ogunwale494009b82016-10-21 09:01:38 -07002713 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2714
2715 if (DEBUG_INPUT_METHOD) {
2716 Slog.i(TAG_WM, "IM target client: " + imeClient);
2717 if (imeClient != null) {
2718 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2719 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2720 }
2721 }
2722
2723 return imeClient != null && imeClient.asBinder() == client.asBinder();
2724 }
2725
2726 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002727 final WindowState win = getWindow(
2728 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2729 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002730 }
2731
2732 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002733 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002734 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002735 final int curValue = w.mSystemUiVisibility;
2736 final int diff = (curValue ^ visibility) & globalDiff;
2737 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002738 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002739 w.mSeq++;
2740 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002741 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002742 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2743 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002744 visibility, newValue, diff);
2745 }
2746 } catch (RemoteException e) {
2747 // so sorry
2748 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002749 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002750 }
2751
2752 void onWindowFreezeTimeout() {
2753 Slog.w(TAG_WM, "Window freeze timeout expired.");
2754 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002755
2756 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002757 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002758 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002759 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002760 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002761 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2762 - mService.mDisplayFreezeTime);
2763 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002764 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002765 mService.mWindowPlacerLocked.performSurfacePlacement();
2766 }
2767
2768 void waitForAllWindowsDrawn() {
2769 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002770 forAllWindows(w -> {
2771 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2772 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2773 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002774 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002775 w.mLastContentInsets.set(-1, -1, -1, -1);
2776 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002777 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002778 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002779 }
2780
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002781 // TODO: Super crazy long method that should be broken down...
2782 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2783
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002784 final int dw = mDisplayInfo.logicalWidth;
2785 final int dh = mDisplayInfo.logicalHeight;
2786 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2787
2788 mTmpUpdateAllDrawn.clear();
2789
2790 int repeats = 0;
2791 do {
2792 repeats++;
2793 if (repeats > 6) {
2794 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2795 clearLayoutNeeded();
2796 break;
2797 }
2798
2799 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2800 pendingLayoutChanges);
2801
Andrii Kulian839def92016-11-02 10:58:58 -07002802 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2803 // the wallpaper window jumping across displays.
2804 // Remove check for default display when there will be support for multiple wallpaper
2805 // targets (on different displays).
2806 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002807 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002808 }
2809
2810 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2811 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2812 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2813 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002814 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002815 }
2816 }
2817
2818 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2819 setLayoutNeeded();
2820 }
2821
2822 // FIRST LOOP: Perform a layout, if needed.
2823 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2824 performLayout(repeats == 1, false /* updateInputWindows */);
2825 } else {
2826 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2827 }
2828
2829 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2830 pendingLayoutChanges = 0;
2831
2832 if (isDefaultDisplay) {
2833 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002834 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002835 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2836 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2837 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2838 }
2839 } while (pendingLayoutChanges != 0);
2840
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002841 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002842
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002843 mTmpRecoveringMemory = recoveringMemory;
2844 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002845
2846 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002847 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2848 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2849 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002850 true /* inTraversal, must call performTraversalInTrans... below */);
2851
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002852 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2853 if (wallpaperVisible != mLastWallpaperVisible) {
2854 mLastWallpaperVisible = wallpaperVisible;
2855 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2856 }
2857
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002858 while (!mTmpUpdateAllDrawn.isEmpty()) {
2859 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2860 // See if any windows have been drawn, so they (and others associated with them)
2861 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002862 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002863 }
2864
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002865 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002866 }
2867
Bryce Leef3c6a472017-11-14 14:53:06 -08002868 private void updateBounds() {
2869 calculateBounds(mTmpBounds);
2870 setBounds(mTmpBounds);
2871 }
2872
2873 // Determines the current display bounds based on the current state
2874 private void calculateBounds(Rect out) {
2875 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
2876 final int orientation = mDisplayInfo.rotation;
2877 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2878 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
2879 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
2880 int width = mDisplayInfo.logicalWidth;
2881 int left = (physWidth - width) / 2;
2882 int height = mDisplayInfo.logicalHeight;
2883 int top = (physHeight - height) / 2;
2884 out.set(left, top, left + width, top + height);
2885 }
2886
2887 @Override
2888 public void getBounds(Rect out) {
2889 calculateBounds(out);
2890 }
2891
2892 private void getBounds(Rect out, int orientation) {
2893 getBounds(out);
2894
2895 // Rotate the Rect if needed.
2896 final int currentRotation = mDisplayInfo.rotation;
2897 final int rotationDelta = deltaRotation(currentRotation, orientation);
2898 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
2899 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
2900 mTmpRectF.set(out);
2901 mTmpMatrix.mapRect(mTmpRectF);
2902 mTmpRectF.round(out);
2903 }
2904 }
2905
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002906 void performLayout(boolean initial, boolean updateInputWindows) {
2907 if (!isLayoutNeeded()) {
2908 return;
2909 }
2910 clearLayoutNeeded();
2911
2912 final int dw = mDisplayInfo.logicalWidth;
2913 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002914 if (DEBUG_LAYOUT) {
2915 Slog.v(TAG, "-------------------------------------");
2916 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2917 }
2918
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002919 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
2920 // TODO: Not sure if we really need to set the rotation here since we are updating from the
2921 // display info above...
2922 mDisplayFrames.mRotation = mRotation;
2923 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002924 if (isDefaultDisplay) {
2925 // Not needed on non-default displays.
2926 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2927 mService.mScreenRect.set(0, 0, dw, dh);
2928 }
2929
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002930 int seq = mService.mLayoutSeq + 1;
2931 if (seq < 0) seq = 0;
2932 mService.mLayoutSeq = seq;
2933
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002934 // Used to indicate that we have processed the dream window and all additional windows are
2935 // behind it.
2936 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002937 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002938
2939 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002940 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002941
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002942 // Used to indicate that we have processed the dream window and all additional attached
2943 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002944 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002945 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002946
2947 // Now perform layout of attached windows, which usually depend on the position of the
2948 // window they are attached to. XXX does not deal with windows that are attached to windows
2949 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002950 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002951
2952 // Window frames may have changed. Tell the input dispatcher about it.
2953 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2954 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2955 if (updateInputWindows) {
2956 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2957 }
2958
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002959 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2960 }
2961
2962 /**
2963 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2964 * In portrait mode, it grabs the full screenshot.
2965 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002966 * @param config of the output bitmap
2967 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
2968 */
chaviwfbe47df2017-11-10 16:14:49 -08002969 Bitmap screenshotDisplay(Bitmap.Config config, boolean wallpaperOnly) {
2970 synchronized (mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02002971 if (!mService.mPolicy.isScreenOn()) {
chaviwfbe47df2017-11-10 16:14:49 -08002972 if (DEBUG_SCREENSHOT) {
2973 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002974 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002975 return null;
2976 }
2977
chaviwfbe47df2017-11-10 16:14:49 -08002978 if (wallpaperOnly && !shouldScreenshotWallpaper()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002979 return null;
2980 }
2981
chaviwfbe47df2017-11-10 16:14:49 -08002982 int dw = mDisplayInfo.logicalWidth;
2983 int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002984
chaviwfbe47df2017-11-10 16:14:49 -08002985 if (dw <= 0 || dh <= 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002986 return null;
2987 }
2988
chaviwfbe47df2017-11-10 16:14:49 -08002989 final Rect frame = new Rect(0, 0, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002990
2991 // The screenshot API does not apply the current screen rotation.
2992 int rot = mDisplay.getRotation();
2993
2994 if (rot == ROTATION_90 || rot == ROTATION_270) {
2995 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
2996 }
2997
chaviwfbe47df2017-11-10 16:14:49 -08002998 // SurfaceFlinger is not aware of orientation, so convert our logical
2999 // crop to SurfaceFlinger's portrait orientation.
3000 convertCropForSurfaceFlinger(frame, rot, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003001
3002 final ScreenRotationAnimation screenRotationAnimation =
3003 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3004 final boolean inRotation = screenRotationAnimation != null &&
3005 screenRotationAnimation.isAnimating();
chaviwfbe47df2017-11-10 16:14:49 -08003006 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003007
Robert Carrb1579c82017-09-05 14:54:47 -07003008 // TODO(b/68392460): We should screenshot Task controls directly
3009 // but it's difficult at the moment as the Task doesn't have the
3010 // correct size set.
chaviwfbe47df2017-11-10 16:14:49 -08003011 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, 0, 1, inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003012 if (bitmap == null) {
Robert Carrb1579c82017-09-05 14:54:47 -07003013 Slog.w(TAG_WM, "Failed to take screenshot");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003014 return null;
3015 }
chaviwfbe47df2017-11-10 16:14:49 -08003016
3017 // Create a copy of the screenshot that is immutable and backed in ashmem.
3018 // This greatly reduces the overhead of passing the bitmap between processes.
3019 final Bitmap ret = bitmap.createAshmemBitmap(config);
3020 bitmap.recycle();
3021 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003022 }
chaviwfbe47df2017-11-10 16:14:49 -08003023 }
3024
3025 private boolean shouldScreenshotWallpaper() {
3026 MutableBoolean screenshotReady = new MutableBoolean(false);
3027
3028 forAllWindows(w -> {
3029 if (!w.mIsWallpaper) {
3030 return false;
3031 }
3032
3033 // Found the wallpaper window
3034 final WindowStateAnimator winAnim = w.mWinAnimator;
3035
3036 if (winAnim.getShown() && winAnim.mLastAlpha > 0f) {
3037 screenshotReady.value = true;
3038 }
3039
3040 return true;
3041 }, true /* traverseTopToBottom */);
3042
3043 return screenshotReady.value;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003044 }
3045
3046 // TODO: Can this use createRotationMatrix()?
3047 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3048 if (rot == Surface.ROTATION_90) {
3049 final int tmp = crop.top;
3050 crop.top = dw - crop.right;
3051 crop.right = crop.bottom;
3052 crop.bottom = dw - crop.left;
3053 crop.left = tmp;
3054 } else if (rot == Surface.ROTATION_180) {
3055 int tmp = crop.top;
3056 crop.top = dh - crop.bottom;
3057 crop.bottom = dh - tmp;
3058 tmp = crop.right;
3059 crop.right = dw - crop.left;
3060 crop.left = dw - tmp;
3061 } else if (rot == Surface.ROTATION_270) {
3062 final int tmp = crop.top;
3063 crop.top = crop.left;
3064 crop.left = dh - crop.bottom;
3065 crop.bottom = crop.right;
3066 crop.right = dh - tmp;
3067 }
3068 }
3069
3070 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003071 // Used to indicate the layout is needed.
3072 mTmpWindow = null;
3073
3074 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003075 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003076 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003077 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003078 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003079 w.setDisplayLayoutNeeded();
3080 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003081 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003082
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003083 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003084 mService.mWindowPlacerLocked.performSurfacePlacement();
3085 }
3086 }
3087
Wale Ogunwale1666e312016-12-16 11:27:18 -08003088 void setExitingTokensHasVisible(boolean hasVisible) {
3089 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3090 mExitingTokens.get(i).hasVisible = hasVisible;
3091 }
3092
3093 // Initialize state of exiting applications.
3094 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3095 }
3096
3097 void removeExistingTokensIfPossible() {
3098 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3099 final WindowToken token = mExitingTokens.get(i);
3100 if (!token.hasVisible) {
3101 mExitingTokens.remove(i);
3102 }
3103 }
3104
3105 // Time to remove any exiting applications?
3106 mTaskStackContainers.removeExistingAppTokensIfPossible();
3107 }
3108
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003109 @Override
3110 void onDescendantOverrideConfigurationChanged() {
3111 setLayoutNeeded();
3112 mService.requestTraversal();
3113 }
3114
David Stevens9440dc82017-03-16 19:00:20 -07003115 boolean okToDisplay() {
3116 if (mDisplayId == DEFAULT_DISPLAY) {
3117 return !mService.mDisplayFrozen
3118 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3119 }
3120 return mDisplayInfo.state == Display.STATE_ON;
3121 }
3122
3123 boolean okToAnimate() {
3124 return okToDisplay() &&
3125 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3126 }
3127
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003128 static final class TaskForResizePointSearchResult {
3129 boolean searchDone;
3130 Task taskForResize;
3131
3132 void reset() {
3133 searchDone = false;
3134 taskForResize = null;
3135 }
3136 }
Robert Carr3b716242016-08-16 16:02:21 -07003137
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003138 private static final class ApplySurfaceChangesTransactionState {
3139 boolean displayHasContent;
3140 boolean obscured;
3141 boolean syswin;
3142 boolean focusDisplayed;
3143 float preferredRefreshRate;
3144 int preferredModeId;
3145
3146 void reset() {
3147 displayHasContent = false;
3148 obscured = false;
3149 syswin = false;
3150 focusDisplayed = false;
3151 preferredRefreshRate = 0;
3152 preferredModeId = 0;
3153 }
3154 }
3155
3156 private static final class ScreenshotApplicationState {
3157 WindowState appWin;
3158 int maxLayer;
3159 int minLayer;
3160 boolean screenshotReady;
3161
3162 void reset(boolean screenshotReady) {
3163 appWin = null;
3164 maxLayer = 0;
3165 minLayer = 0;
3166 this.screenshotReady = screenshotReady;
3167 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3168 }
3169 }
3170
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003171 /**
3172 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3173 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3174 * homogeneous children type which is currently required by sub-classes of
3175 * {@link WindowContainer} class.
3176 */
3177 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3178
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003179 DisplayChildWindowContainer(WindowManagerService service) {
3180 super(service);
3181 }
3182
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003183 @Override
3184 boolean fillsParent() {
3185 return true;
3186 }
3187
3188 @Override
3189 boolean isVisible() {
3190 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003191 }
3192 }
3193
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003194 /**
3195 * Window container class that contains all containers on this display relating to Apps.
3196 * I.e Activities.
3197 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003198 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003199 /**
3200 * A control placed at the appropriate level for transitions to occur.
3201 */
3202 SurfaceControl mAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003203
Wale Ogunwale61911492017-10-11 08:50:50 -07003204 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3205 // through the list to find them.
3206 private TaskStack mHomeStack = null;
3207 private TaskStack mPinnedStack = null;
3208 private TaskStack mSplitScreenPrimaryStack = null;
3209
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003210 TaskStackContainers(WindowManagerService service) {
3211 super(service);
3212 }
3213
Wale Ogunwale61911492017-10-11 08:50:50 -07003214 /**
3215 * Returns the topmost stack on the display that is compatible with the input windowing mode
3216 * and activity type. Null is no compatible stack on the display.
3217 */
3218 TaskStack getStack(int windowingMode, int activityType) {
3219 if (activityType == ACTIVITY_TYPE_HOME) {
3220 return mHomeStack;
3221 }
3222 if (windowingMode == WINDOWING_MODE_PINNED) {
3223 return mPinnedStack;
3224 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3225 return mSplitScreenPrimaryStack;
3226 }
3227 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3228 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003229 if (activityType == ACTIVITY_TYPE_UNDEFINED
3230 && windowingMode == stack.getWindowingMode()) {
3231 // Passing in undefined type means we want to match the topmost stack with the
3232 // windowing mode.
3233 return stack;
3234 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003235 if (stack.isCompatible(windowingMode, activityType)) {
3236 return stack;
3237 }
3238 }
3239 return null;
3240 }
3241
3242 @VisibleForTesting
3243 TaskStack getTopStack() {
3244 return mTaskStackContainers.getChildCount() > 0
3245 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3246 }
3247
3248 TaskStack getHomeStack() {
3249 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3250 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3251 }
3252 return mHomeStack;
3253 }
3254
3255 TaskStack getPinnedStack() {
3256 return mPinnedStack;
3257 }
3258
Matthew Ng64e77cf2017-10-31 14:01:31 -07003259 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003260 return mSplitScreenPrimaryStack;
3261 }
3262
Andrii Kulian839def92016-11-02 10:58:58 -07003263 /**
3264 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003265 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003266 */
3267 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003268 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003269 addChild(stack, onTop);
3270 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003271 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003272
Wale Ogunwale61911492017-10-11 08:50:50 -07003273 void onStackWindowingModeChanged(TaskStack stack) {
3274 removeStackReferenceIfNeeded(stack);
3275 addStackReferenceIfNeeded(stack);
3276 if (stack == mPinnedStack && getTopStack() != stack) {
3277 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3278 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3279 }
3280 }
3281
3282 private void addStackReferenceIfNeeded(TaskStack stack) {
3283 if (stack.isActivityTypeHome()) {
3284 if (mHomeStack != null) {
3285 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
3286 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
3287 }
3288 mHomeStack = stack;
3289 }
3290 final int windowingMode = stack.getWindowingMode();
3291 if (windowingMode == WINDOWING_MODE_PINNED) {
3292 if (mPinnedStack != null) {
3293 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3294 + mPinnedStack + " already exist on display=" + this
3295 + " stack=" + stack);
3296 }
3297 mPinnedStack = stack;
3298 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3299 if (mSplitScreenPrimaryStack != null) {
3300 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3301 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3302 + " already exist on display=" + this + " stack=" + stack);
3303 }
3304 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003305 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003306 }
3307 }
3308
3309 private void removeStackReferenceIfNeeded(TaskStack stack) {
3310 if (stack == mHomeStack) {
3311 mHomeStack = null;
3312 } else if (stack == mPinnedStack) {
3313 mPinnedStack = null;
3314 } else if (stack == mSplitScreenPrimaryStack) {
3315 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003316 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3317 mService.setDockedStackCreateStateLocked(
3318 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3319 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003320 }
Andrii Kulian839def92016-11-02 10:58:58 -07003321 }
3322
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003323 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003324 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3325 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003326 addChild(stack, addIndex);
3327 setLayoutNeeded();
3328 }
3329
Wale Ogunwale61911492017-10-11 08:50:50 -07003330 @Override
3331 protected void removeChild(TaskStack stack) {
3332 super.removeChild(stack);
3333 removeStackReferenceIfNeeded(stack);
3334 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003335
3336 @Override
3337 boolean isOnTop() {
3338 // Considered always on top
3339 return true;
3340 }
3341
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003342 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003343 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003344 if (child.getWindowConfiguration().isAlwaysOnTop()
3345 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003346 // This stack is always-on-top, override the default behavior.
3347 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3348
3349 // Moving to its current position, as we must call super but we don't want to
3350 // perform any meaningful action.
3351 final int currentPosition = mChildren.indexOf(child);
3352 super.positionChildAt(currentPosition, child, false /* includingParents */);
3353 return;
3354 }
3355
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003356 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3357 super.positionChildAt(targetPosition, child, includingParents);
3358
3359 setLayoutNeeded();
3360 }
3361
3362 /**
3363 * When stack is added or repositioned, find a proper position for it.
3364 * This will make sure that pinned stack always stays on top.
3365 * @param requestedPosition Position requested by caller.
3366 * @param stack Stack to be added or positioned.
3367 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3368 * @return The proper position for the stack.
3369 */
3370 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3371 final int topChildPosition = mChildren.size() - 1;
3372 boolean toTop = requestedPosition == POSITION_TOP;
3373 toTop |= adding ? requestedPosition >= topChildPosition + 1
3374 : requestedPosition >= topChildPosition;
3375 int targetPosition = requestedPosition;
3376
Wale Ogunwale61911492017-10-11 08:50:50 -07003377 if (toTop && stack.getWindowingMode() != WINDOWING_MODE_PINNED && hasPinnedStack()) {
Bryce Lee48f4b572017-04-10 10:54:15 -07003378 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003379 TaskStack topStack = mChildren.get(topChildPosition);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003380 if (topStack.getWindowingMode() != WINDOWING_MODE_PINNED) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003381 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3382 }
3383
3384 // So, stack is moved just below the pinned stack.
3385 // When we're adding a new stack the target is the current pinned stack position.
3386 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003387 // stack, because WindowContainer#positionAt() first removes element and then adds
3388 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003389 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3390 }
3391
3392 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003393 }
3394
3395 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003396 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3397 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003398 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003399 if (super.forAllWindows(callback, traverseTopToBottom)) {
3400 return true;
3401 }
3402 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3403 return true;
3404 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003405 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003406 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3407 return true;
3408 }
3409 if (super.forAllWindows(callback, traverseTopToBottom)) {
3410 return true;
3411 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003412 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003413 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003414 }
3415
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003416 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003417 boolean traverseTopToBottom) {
3418 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3419 // app tokens.
3420 // TODO: Investigate if we need to continue to do this or if we can just process them
3421 // in-order.
3422 if (traverseTopToBottom) {
3423 for (int i = mChildren.size() - 1; i >= 0; --i) {
3424 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3425 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003426 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3427 traverseTopToBottom)) {
3428 return true;
3429 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003430 }
3431 }
3432 } else {
3433 final int count = mChildren.size();
3434 for (int i = 0; i < count; ++i) {
3435 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3436 final int appTokensCount = appTokens.size();
3437 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003438 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3439 traverseTopToBottom)) {
3440 return true;
3441 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003442 }
3443 }
3444 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003445 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003446 }
3447
Wale Ogunwale1666e312016-12-16 11:27:18 -08003448 void setExitingTokensHasVisible(boolean hasVisible) {
3449 for (int i = mChildren.size() - 1; i >= 0; --i) {
3450 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3451 for (int j = appTokens.size() - 1; j >= 0; --j) {
3452 appTokens.get(j).hasVisible = hasVisible;
3453 }
3454 }
3455 }
3456
3457 void removeExistingAppTokensIfPossible() {
3458 for (int i = mChildren.size() - 1; i >= 0; --i) {
3459 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3460 for (int j = appTokens.size() - 1; j >= 0; --j) {
3461 final AppWindowToken token = appTokens.get(j);
3462 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3463 && (!token.mIsExiting || token.isEmpty())) {
3464 // Make sure there is no animation running on this token, so any windows
3465 // associated with it will be removed as soon as their animations are
3466 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003467 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003468 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3469 "performLayout: App token exiting now removed" + token);
3470 token.removeIfPossible();
3471 }
3472 }
3473 }
3474 }
3475
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003476 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003477 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003478 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3479 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003480 // Apps and their containers are not allowed to specify an orientation while the
3481 // docked or freeform stack is visible...except for the home stack/task if the
3482 // docked stack is minimized and it actually set something.
3483 if (mHomeStack != null && mHomeStack.isVisible()
3484 && mDividerControllerLocked.isMinimizedDock()) {
3485 final int orientation = mHomeStack.getOrientation();
3486 if (orientation != SCREEN_ORIENTATION_UNSET) {
3487 return orientation;
3488 }
3489 }
3490 return SCREEN_ORIENTATION_UNSPECIFIED;
3491 }
3492
3493 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003494 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3495 PackageManager.FEATURE_AUTOMOTIVE);
3496 if (isCar) {
3497 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3498 // allow anything but the default orientation.
3499 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3500 "Forcing UNSPECIFIED orientation in car. Ignoring " + orientation);
3501 return SCREEN_ORIENTATION_UNSPECIFIED;
3502 }
3503
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003504 if (orientation != SCREEN_ORIENTATION_UNSET
3505 && orientation != SCREEN_ORIENTATION_BEHIND) {
3506 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3507 "App is requesting an orientation, return " + orientation);
3508 return orientation;
3509 }
3510
3511 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003512 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003513 // The next app has not been requested to be visible, so we keep the current orientation
3514 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003515 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003516 }
Robert Carrb1579c82017-09-05 14:54:47 -07003517
3518 @Override
3519 void assignChildLayers(SurfaceControl.Transaction t) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003520 int layer = 0;
Robert Carrb1579c82017-09-05 14:54:47 -07003521
3522 // We allow stacks to change visual order from the AM specified order due to
3523 // Z-boosting during animations. However we must take care to ensure TaskStacks
3524 // which are marked as alwaysOnTop remain that way.
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003525 for (int i = 0; i < mChildren.size(); i++) {
3526 final TaskStack s = mChildren.get(i);
3527 s.assignChildLayers();
3528 if (!s.needsZBoost() && !s.isAlwaysOnTop()) {
3529 s.assignLayer(t, layer++);
Robert Carrb1579c82017-09-05 14:54:47 -07003530 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003531 }
3532 for (int i = 0; i < mChildren.size(); i++) {
3533 final TaskStack s = mChildren.get(i);
3534 if (s.needsZBoost() && !s.isAlwaysOnTop()) {
3535 s.assignLayer(t, layer++);
Robert Carrb1579c82017-09-05 14:54:47 -07003536 }
3537 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003538 for (int i = 0; i < mChildren.size(); i++) {
3539 final TaskStack s = mChildren.get(i);
3540 if (s.isAlwaysOnTop()) {
3541 s.assignLayer(t, layer++);
3542 }
3543 }
3544
3545 // The appropriate place for App-Transitions to occur is right
3546 // above all other animations but still below things in the Picture-and-Picture
3547 // windowing mode.
3548 if (mAnimationLayer != null) {
3549 t.setLayer(mAnimationLayer, layer++);
3550 }
Robert Carrb1579c82017-09-05 14:54:47 -07003551 }
3552
3553 @Override
3554 void onParentSet() {
3555 super.onParentSet();
3556 if (getParent() != null) {
3557 mAnimationLayer = makeSurface().build();
3558 } else {
3559 mAnimationLayer.destroy();
3560 mAnimationLayer = null;
3561 }
3562 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003563 }
3564
Robert Carree4d4b92017-11-22 12:21:46 -08003565 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003566 AboveAppWindowContainers(String name, WindowManagerService service) {
3567 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08003568 }
3569
3570 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
3571 boolean needAssignIme = imeContainer != null
3572 && imeContainer.getSurfaceControl() != null;
3573 for (int j = 0; j < mChildren.size(); ++j) {
3574 final WindowToken wt = mChildren.get(j);
3575 wt.assignLayer(t, j);
3576 wt.assignChildLayers(t);
3577
3578 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
3579 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08003580
3581 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
3582 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003583 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08003584 needAssignIme = false;
3585 }
3586 }
3587 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003588 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08003589 }
3590 }
3591 }
3592
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003593 /**
3594 * Window container class that contains all containers on this display that are not related to
3595 * Apps. E.g. status bar.
3596 */
Robert Carree4d4b92017-11-22 12:21:46 -08003597 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07003598 /**
3599 * Compares two child window tokens returns -1 if the first is lesser than the second in
3600 * terms of z-order and 1 otherwise.
3601 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003602 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003603 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003604 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3605 token1.mOwnerCanManageAppTokens)
3606 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3607 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003608
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003609 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3610 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3611 return false;
3612 }
3613 final int req = w.mAttrs.screenOrientation;
3614 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3615 || req == SCREEN_ORIENTATION_UNSET) {
3616 return false;
3617 }
3618 return true;
3619 };
3620
Wale Ogunwale3a931692016-11-02 16:49:48 -07003621 private final String mName;
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003622 NonAppWindowContainers(String name, WindowManagerService service) {
3623 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003624 mName = name;
3625 }
3626
3627 void addChild(WindowToken token) {
3628 addChild(token, mWindowComparator);
3629 }
3630
3631 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003632 int getOrientation() {
3633 final WindowManagerPolicy policy = mService.mPolicy;
3634 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003635 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003636
3637 if (win != null) {
3638 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003639 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003640 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003641 if (mService.mKeyguardGoingAway) {
3642 // Keyguard can't affect the orientation if it is going away...
3643 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3644 return SCREEN_ORIENTATION_UNSET;
3645 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003646 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003647 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003648 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003649 }
3650
Andrii Kulian8ee72852017-03-10 10:36:45 -08003651 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003652
Jorim Jaggi75520d52017-08-24 17:23:19 +02003653 if (policy.isKeyguardShowingAndNotOccluded()
3654 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003655 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003656 }
3657
3658 return SCREEN_ORIENTATION_UNSET;
3659 }
3660
3661 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003662 String getName() {
3663 return mName;
3664 }
Robert Carr3b716242016-08-16 16:02:21 -07003665 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003666
Robert Carrb1579c82017-09-05 14:54:47 -07003667 SurfaceControl.Builder makeSurface(SurfaceSession s) {
3668 return mService.makeSurfaceBuilder(s)
3669 .setParent(mWindowingLayer);
3670 }
3671
3672 @Override
3673 SurfaceSession getSession() {
3674 return mSession;
3675 }
3676
3677 @Override
3678 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003679 SurfaceSession s = child != null ? child.getSession() : getSession();
3680 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07003681 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07003682
3683 if (child == null) {
3684 return b;
3685 }
3686
Robert Carree4d4b92017-11-22 12:21:46 -08003687 return b.setName(child.getName())
3688 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07003689 }
3690
3691 /**
3692 * The makeSurface variants are for use by the window-container
3693 * hierarchy. makeOverlay here is a function for various non windowing
3694 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
3695 * and other potpourii.
3696 */
3697 SurfaceControl.Builder makeOverlay() {
3698 return mService.makeSurfaceBuilder(mSession)
3699 .setParent(mOverlayLayer);
3700 }
3701
3702 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003703 applyMagnificationSpec(getPendingTransaction(), spec);
3704 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07003705 }
3706
3707 @Override
3708 void onParentSet() {
3709 // Since we are the top of the SurfaceControl hierarchy here
3710 // we create the root surfaces explicitly rather than chaining
3711 // up as the default implementation in onParentSet does. So we
3712 // explicitly do NOT call super here.
3713 }
3714
3715 @Override
3716 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07003717
3718 // These are layers as children of "mWindowingLayer"
3719 mBelowAppWindowsContainers.assignLayer(t, 0);
3720 mTaskStackContainers.assignLayer(t, 1);
3721 mAboveAppWindowsContainers.assignLayer(t, 2);
3722
3723 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08003724 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08003725
Robert Carree4d4b92017-11-22 12:21:46 -08003726 // In the case where we have an IME target that is not in split-screen
3727 // mode IME assignment is easy. We just need the IME to go directly above
3728 // the target. This way children of the target will naturally go above the IME
3729 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08003730 //
Robert Carree4d4b92017-11-22 12:21:46 -08003731 // In the case of split-screen windowing mode, we need to elevate the IME above the
3732 // docked divider while keeping the app itself below the docked divider, so instead
3733 // we use relative layering of the IME targets child windows, and place the
3734 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08003735 //
Robert Carree4d4b92017-11-22 12:21:46 -08003736 // In the case where we have no IME target we assign it where it's base layer would
3737 // place it in the AboveAppWindowContainers.
3738 if (imeTarget != null && !imeTarget.inSplitScreenWindowingMode()
3739 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003740 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07003741 // TODO: We need to use an extra level on the app surface to ensure
3742 // this is always above SurfaceView but always below attached window.
3743 1);
Robert Carree4d4b92017-11-22 12:21:46 -08003744 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07003745 }
3746
3747 // Above we have assigned layers to our children, now we ask them to assign
3748 // layers to their children.
3749 mBelowAppWindowsContainers.assignChildLayers(t);
3750 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08003751 mAboveAppWindowsContainers.assignChildLayers(t,
3752 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07003753 mImeWindowsContainers.assignChildLayers(t);
3754 }
3755
3756 /**
3757 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
3758 * that the IME target is one of the docked applications. We'd like the docked divider to be
3759 * above both of the applications, and we'd like the IME to be above the docked divider.
3760 * However we need child windows of the applications to be above the IME (Text drag handles).
3761 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
3762 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
3763 * with {@link #WindowState#assignLayer}
3764 */
3765 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
3766 t.setRelativeLayer(child.getSurfaceControl(), mImeWindowsContainers.getSurfaceControl(), 1);
3767 }
3768
3769 @Override
Jorim Jaggia5e10572017-11-15 14:36:26 +01003770 public void destroyAfterPendingTransaction(SurfaceControl surface) {
Robert Carrb1579c82017-09-05 14:54:47 -07003771 mPendingDestroyingSurfaces.add(surface);
3772 }
3773
3774 /**
3775 * Destroys any surfaces that have been put into the pending list with
Jorim Jaggia5e10572017-11-15 14:36:26 +01003776 * {@link #destroyAfterPendingTransaction}.
Robert Carrb1579c82017-09-05 14:54:47 -07003777 */
3778 void onPendingTransactionApplied() {
3779 for (int i = mPendingDestroyingSurfaces.size() - 1; i >= 0; i--) {
3780 mPendingDestroyingSurfaces.get(i).destroy();
3781 }
3782 mPendingDestroyingSurfaces.clear();
3783 }
Jorim Jaggi9af095b2017-12-12 17:18:57 +01003784
3785 @Override
3786 void prepareSurfaces() {
3787 final ScreenRotationAnimation screenRotationAnimation =
3788 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
3789 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
3790 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
3791 mPendingTransaction.setMatrix(mWindowingLayer,
3792 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
3793 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
3794 mPendingTransaction.setPosition(mWindowingLayer,
3795 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
3796 mPendingTransaction.setAlpha(mWindowingLayer,
3797 screenRotationAnimation.getEnterTransformation().getAlpha());
3798 }
3799 super.prepareSurfaces();
3800 }
Craig Mautner59c00972012-07-30 12:10:24 -07003801}