blob: ed073586ea051d04cacc01063f28b6b236d797e9 [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 Ogunwale2b06bfc2016-09-28 14:17:05 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070076import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070077import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080078import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070079import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070080import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070081import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070082import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070083import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070084import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080085import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -070086import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070087import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
88import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070089import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070090import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -070091import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
92import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070093import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -070094import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070095import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070096import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070097import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070098import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070099import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700100import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700101import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700102import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700103import static com.android.server.wm.proto.DisplayProto.ABOVE_APP_WINDOWS;
104import static com.android.server.wm.proto.DisplayProto.BELOW_APP_WINDOWS;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000105import static com.android.server.wm.proto.DisplayProto.DISPLAY_FRAMES;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700106import static com.android.server.wm.proto.DisplayProto.DISPLAY_INFO;
107import static com.android.server.wm.proto.DisplayProto.DOCKED_STACK_DIVIDER_CONTROLLER;
108import static com.android.server.wm.proto.DisplayProto.DPI;
109import static com.android.server.wm.proto.DisplayProto.ID;
110import static com.android.server.wm.proto.DisplayProto.IME_WINDOWS;
111import static com.android.server.wm.proto.DisplayProto.PINNED_STACK_CONTROLLER;
Steven Timotiusf2d68892017-08-28 17:00:01 -0700112import static com.android.server.wm.proto.DisplayProto.ROTATION;
113import static com.android.server.wm.proto.DisplayProto.SCREEN_ROTATION_ANIMATION;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700114import static com.android.server.wm.proto.DisplayProto.STACKS;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700115import static com.android.server.wm.proto.DisplayProto.WINDOW_CONTAINER;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700116
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700117import android.annotation.CallSuper;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700118import android.annotation.NonNull;
Dean Harding3e5a1522017-10-06 09:19:01 -0700119import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700120import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700121import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700122import android.graphics.Bitmap;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700123import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800124import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700125import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700126import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100127import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700128import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700129import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700130import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700131import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700132import android.os.RemoteException;
133import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100134import android.os.Trace;
Chong Zhang8e89b312015-09-09 15:09:30 -0700135import android.util.DisplayMetrics;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800136import android.util.MutableBoolean;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700137import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700138import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700139import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700140import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700141import android.view.InputDevice;
Robert Carrb1579c82017-09-05 14:54:47 -0700142import android.view.MagnificationSpec;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700143import android.view.Surface;
144import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100145import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700146import android.view.SurfaceSession;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700147
Bryce Lee48f4b572017-04-10 10:54:15 -0700148import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800149import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700150import com.android.internal.view.IInputMethodClient;
Adrian Roose99bc052017-11-20 17:55:31 +0100151import com.android.server.policy.WindowManagerPolicy;
Craig Mautner59c00972012-07-30 12:10:24 -0700152
Robert Carr3b716242016-08-16 16:02:21 -0700153import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700154import java.io.PrintWriter;
155import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700156import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700157import java.util.HashMap;
158import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700159import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700160import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800161import java.util.function.Consumer;
162import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700163
Craig Mautner59c00972012-07-30 12:10:24 -0700164/**
165 * Utility class for keeping track of the WindowStates and other pertinent contents of a
166 * particular Display.
167 *
168 * IMPORTANT: No method from this class should ever be used without holding
169 * WindowManagerService.mWindowMap.
170 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700171class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700172 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700173
174 /** Unique identifier of this stack. */
175 private final int mDisplayId;
176
Wale Ogunwale3a931692016-11-02 16:49:48 -0700177 /** The containers below are the only child containers the display can have. */
178 // Contains all window containers that are related to apps (Activities)
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100179 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700180 // Contains all non-app window containers that should be displayed above the app containers
181 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800182 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100183 new AboveAppWindowContainers("mAboveAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700184 // Contains all non-app window containers that should be displayed below the app containers
185 // (e.g. Wallpaper).
186 private final NonAppWindowContainers mBelowAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100187 new NonAppWindowContainers("mBelowAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700188 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
189 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
Robert Carr9034d962018-01-04 18:27:42 -0800190 // window containers together and move them in-sync if/when needed. We use a subclass of
191 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
192 private final NonMagnifiableWindowContainers mImeWindowsContainers =
193 new NonMagnifiableWindowContainers("mImeWindowsContainers", mService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700194
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000195 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800196 private WindowState mTmpWindow2;
197 private WindowAnimator mTmpWindowAnimator;
198 private boolean mTmpRecoveringMemory;
199 private boolean mUpdateImeTarget;
200 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700201 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700202
Wale Ogunwale02319a62016-09-26 15:21:22 -0700203 // Mapping from a token IBinder to a WindowToken object on this display.
204 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
205
Andrii Kuliancd097992017-03-23 18:31:59 -0700206 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700207 int mInitialDisplayWidth = 0;
208 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700209 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700210
211 /**
212 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
213 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
214 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
215 */
Craig Mautner59c00972012-07-30 12:10:24 -0700216 int mBaseDisplayWidth = 0;
217 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700218 /**
219 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
220 * but can be set from Settings or via shell command "adb shell wm density".
221 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
222 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700223 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700224 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700225 private final DisplayInfo mDisplayInfo = new DisplayInfo();
226 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700227 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000228 DisplayFrames mDisplayFrames;
229
Andrii Kulian06d07d62017-03-14 11:11:47 -0700230 /**
231 * For default display it contains real metrics, empty for others.
232 * @see WindowManagerService#createWatermarkInTransaction()
233 */
234 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
235 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
236 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700237
Andrii Kulian06d07d62017-03-14 11:11:47 -0700238 /**
239 * Compat metrics computed based on {@link #mDisplayMetrics}.
240 * @see #updateDisplayAndOrientation(int)
241 */
242 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
243
244 /** The desired scaling factor for compatible apps. */
245 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700246
Andrii Kulian8ee72852017-03-10 10:36:45 -0800247 /**
248 * Current rotation of the display.
249 * Constants as per {@link android.view.Surface.Rotation}.
250 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700251 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800252 */
253 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700254
Andrii Kulian8ee72852017-03-10 10:36:45 -0800255 /**
256 * Last applied orientation of the display.
257 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
258 *
259 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
260 */
261 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700262
Andrii Kulian8ee72852017-03-10 10:36:45 -0800263 /**
264 * Flag indicating that the application is receiving an orientation that has different metrics
265 * than it expected. E.g. Portrait instead of Landscape.
266 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700267 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800268 */
269 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700270
Andrii Kulian8ee72852017-03-10 10:36:45 -0800271 /**
272 * Orientation forced by some window. If there is no visible window that specifies orientation
273 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
274 *
275 * @see NonAppWindowContainers#getOrientation()
276 */
277 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700278
Andrii Kulian8ee72852017-03-10 10:36:45 -0800279 /**
280 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
281 * occluded.
282 *
283 * @see NonAppWindowContainers#getOrientation()
284 */
285 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
286
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700287 /**
288 * Keep track of wallpaper visibility to notify changes.
289 */
290 private boolean mLastWallpaperVisible = false;
291
Andrii Kulian06d07d62017-03-14 11:11:47 -0700292 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700293
Craig Mautner39834192012-09-02 07:47:24 -0700294 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700295 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700296 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700297 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800298 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700299
Craig Mautnerdc548482014-02-05 13:35:24 -0800300 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700301 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800302
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700303 /** Detect user tapping outside of current focused task bounds .*/
304 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700305
Craig Mautner6601b7b2013-04-29 10:29:11 -0700306 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700307 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700308
Craig Mautner6601b7b2013-04-29 10:29:11 -0700309 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800310 private final Rect mTmpRect = new Rect();
311 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700312 private final RectF mTmpRectF = new RectF();
313 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800314 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700315
Bryce Leef3c6a472017-11-14 14:53:06 -0800316 /** Used for handing back size of display */
317 private final Rect mTmpBounds = new Rect();
318
Craig Mautner95da1082014-02-24 17:54:35 -0800319 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700320 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800321
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700322 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700323 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700324
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800325 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
326
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000327 private boolean mHaveBootMsg = false;
328 private boolean mHaveApp = false;
329 private boolean mHaveWallpaper = false;
330 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700331
332 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
333
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700334 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
335 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000336 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
337 new ApplySurfaceChangesTransactionState();
338 private final ScreenshotApplicationState mScreenshotApplicationState =
339 new ScreenshotApplicationState();
Jorim Jaggia5e10572017-11-15 14:36:26 +0100340 private final Transaction mTmpTransaction = new Transaction();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700341
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700342 // True if this display is in the process of being removed. Used to determine if the removal of
343 // the display's direct children should be allowed.
344 private boolean mRemovingDisplay = false;
345
Bryce Leed1871262017-06-12 14:12:29 -0700346 // {@code false} if this display is in the processing of being created.
347 private boolean mDisplayReady = false;
348
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800349 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700350
Robert Carrb1579c82017-09-05 14:54:47 -0700351 private final SurfaceSession mSession = new SurfaceSession();
352
353 /**
354 * We organize all top-level Surfaces in to the following layers.
355 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800356 * and omitted from Screen-Magnification, for example the strict mode flash or
357 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700358 * {@link #mWindowingLayer} contains everything else.
359 */
360 private SurfaceControl mOverlayLayer;
361
362 /**
363 * See {@link #mOverlayLayer}
364 */
365 private SurfaceControl mWindowingLayer;
366
367 /**
368 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
369 * <p>
370 * For these surfaces currently we use a surface based on the larger of width or height so we
371 * don't have to resize when rotating the display.
372 */
373 private int mSurfaceSize;
374
375 /**
376 * A list of surfaces to be destroyed after {@link #mPendingTransaction} is applied.
377 */
378 private final ArrayList<SurfaceControl> mPendingDestroyingSurfaces = new ArrayList<>();
379
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100380 /** Temporary float array to retrieve 3x3 matrix values. */
381 private final float[] mTmpFloats = new float[9];
382
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800383 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
384 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800385 final AppWindowToken atoken = w.mAppToken;
386 if (winAnimator.mDrawState == READY_TO_SHOW) {
387 if (atoken == null || atoken.allDrawn) {
388 if (w.performShowLocked()) {
389 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
390 if (DEBUG_LAYOUT_REPEATS) {
391 mService.mWindowPlacerLocked.debugLayoutRepeats(
392 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
393 }
394 }
395 }
396 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800397 };
398
399 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
400 final WindowStateAnimator winAnimator = w.mWinAnimator;
401 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
402 return;
403 }
404
405 final int flags = w.mAttrs.flags;
406
407 // If this window is animating, make a note that we have an animating window and take
408 // care of a request to run a detached wallpaper animation.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100409 if (winAnimator.isAnimationSet()) {
410 final AnimationAdapter anim = w.getAnimation();
411 if (anim != null) {
412 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && anim.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800413 mTmpWindow = w;
414 }
Jorim Jaggia5e10572017-11-15 14:36:26 +0100415 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800416 if (color != 0) {
417 final TaskStack stack = w.getStack();
418 if (stack != null) {
419 stack.setAnimationBackground(winAnimator, color);
420 }
421 }
422 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800423 }
424
425 // If this window's app token is running a detached wallpaper animation, make a note so
426 // we can ensure the wallpaper is displayed behind it.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200427 final AppWindowToken atoken = winAnimator.mWin.mAppToken;
428 final AnimationAdapter animation = atoken != null ? atoken.getAnimation() : null;
429 if (animation != null) {
430 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && animation.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800431 mTmpWindow = w;
432 }
433
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200434 final int color = animation.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800435 if (color != 0) {
436 final TaskStack stack = w.getStack();
437 if (stack != null) {
438 stack.setAnimationBackground(winAnimator, color);
439 }
440 }
441 }
442 };
443
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800444 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
445 final int lostFocusUid = mTmpWindow.mOwnerUid;
446 final Handler handler = mService.mH;
447 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
448 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
449 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
450 w.mAttrs.hideTimeoutMilliseconds);
451 }
452 }
453 };
454
455 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
456 final AppWindowToken focusedApp = mService.mFocusedApp;
457 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
458 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
459
460 if (!w.canReceiveKeys()) {
461 return false;
462 }
463
464 final AppWindowToken wtoken = w.mAppToken;
465
466 // If this window's application has been removed, just skip it.
467 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
468 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
469 + (wtoken.removed ? "removed" : "sendingToBottom"));
470 return false;
471 }
472
473 if (focusedApp == null) {
474 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
475 + " using new focus @ " + w);
476 mTmpWindow = w;
477 return true;
478 }
479
480 if (!focusedApp.windowsAreFocusable()) {
481 // Current focused app windows aren't focusable...
482 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
483 + " focusable using new focus @ " + w);
484 mTmpWindow = w;
485 return true;
486 }
487
488 // Descend through all of the app tokens and find the first that either matches
489 // win.mAppToken (return win) or mFocusedApp (return null).
490 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
491 if (focusedApp.compareTo(wtoken) > 0) {
492 // App stack below focused app stack. No focus for you!!!
493 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
494 "findFocusedWindow: Reached focused app=" + focusedApp);
495 mTmpWindow = null;
496 return true;
497 }
498 }
499
500 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
501 mTmpWindow = w;
502 return true;
503 };
504
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800505 private final Consumer<WindowState> mPerformLayout = w -> {
506 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
507 // wasting time and funky changes while a window is animating away.
508 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
509 || w.isGoneForLayoutLw();
510
511 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
512 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
513 + " mLayoutAttached=" + w.mLayoutAttached
514 + " screen changed=" + w.isConfigChanged());
515 final AppWindowToken atoken = w.mAppToken;
516 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200517 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800518 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
519 + " parentHidden=" + w.isParentWindowHidden());
520 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200521 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800522 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
523 + " parentHidden=" + w.isParentWindowHidden());
524 }
525
526 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
527 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
528 // since that means "perform layout as normal, just don't display").
529 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
530 || ((w.isConfigChanged() || w.setReportResizeHints())
531 && !w.isGoneForLayoutLw() &&
532 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
533 (w.mHasSurface && w.mAppToken != null &&
534 w.mAppToken.layoutConfigChanges)))) {
535 if (!w.mLayoutAttached) {
536 if (mTmpInitial) {
537 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
538 w.mContentChanged = false;
539 }
540 if (w.mAttrs.type == TYPE_DREAM) {
541 // Don't layout windows behind a dream, so that if it does stuff like hide
542 // the status bar we won't get a bad transition when it goes away.
543 mTmpWindow = w;
544 }
545 w.mLayoutNeeded = false;
546 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200547 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000548 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800549 w.mLayoutSeq = mService.mLayoutSeq;
550
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200551 // If this is the first layout, we need to initialize the last inset values as
552 // otherwise we'd immediately cause an unnecessary resize.
553 if (firstLayout) {
554 w.updateLastInsetValues();
555 }
556
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800557 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
558 + " mContainingFrame=" + w.mContainingFrame
559 + " mDisplayFrame=" + w.mDisplayFrame);
560 }
561 }
562 };
563
564 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
565 if (w.mLayoutAttached) {
566 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
567 + " mViewVisibility=" + w.mViewVisibility
568 + " mRelayoutCalled=" + w.mRelayoutCalled);
569 // If this view is GONE, then skip it -- keep the current frame, and let the caller
570 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
571 // windows, since that means "perform layout as normal, just don't display").
572 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
573 return;
574 }
575 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
576 || w.mLayoutNeeded) {
577 if (mTmpInitial) {
578 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
579 w.mContentChanged = false;
580 }
581 w.mLayoutNeeded = false;
582 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000583 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800584 w.mLayoutSeq = mService.mLayoutSeq;
585 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
586 + " mContainingFrame=" + w.mContainingFrame
587 + " mDisplayFrame=" + w.mDisplayFrame);
588 }
589 } else if (w.mAttrs.type == TYPE_DREAM) {
590 // Don't layout windows behind a dream, so that if it does stuff like hide the
591 // status bar we won't get a bad transition when it goes away.
592 mTmpWindow = mTmpWindow2;
593 }
594 };
595
596 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
597 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
598 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
599 return w.canBeImeTarget();
600 };
601
602 private final Consumer<WindowState> mApplyPostLayoutPolicy =
603 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
604 mService.mInputMethodTarget);
605
606 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
607 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
608 final boolean obscuredChanged = w.mObscured !=
609 mTmpApplySurfaceChangesTransactionState.obscured;
610 final RootWindowContainer root = mService.mRoot;
611 // Only used if default window
612 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
613
614 // Update effect.
615 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
616 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
617 final boolean isDisplayed = w.isDisplayedLw();
618
619 if (isDisplayed && w.isObscuringDisplay()) {
620 // This window completely covers everything behind it, so we want to leave all
621 // of them as undimmed (for performance reasons).
622 root.mObscuringWindow = w;
623 mTmpApplySurfaceChangesTransactionState.obscured = true;
624 }
625
626 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
627 root.handleNotObscuredLocked(w,
628 mTmpApplySurfaceChangesTransactionState.obscured,
629 mTmpApplySurfaceChangesTransactionState.syswin);
630
631 if (w.mHasSurface && isDisplayed) {
632 final int type = w.mAttrs.type;
633 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
634 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
635 mTmpApplySurfaceChangesTransactionState.syswin = true;
636 }
637 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
638 && w.mAttrs.preferredRefreshRate != 0) {
639 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
640 = w.mAttrs.preferredRefreshRate;
641 }
642 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
643 && w.mAttrs.preferredDisplayModeId != 0) {
644 mTmpApplySurfaceChangesTransactionState.preferredModeId
645 = w.mAttrs.preferredDisplayModeId;
646 }
647 }
648 }
649
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800650 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
651 && mWallpaperController.isWallpaperTarget(w)) {
652 // This is the wallpaper target and its obscured state changed... make sure the
653 // current wallpaper's visibility has been updated accordingly.
654 mWallpaperController.updateWallpaperVisibility();
655 }
656
Jorim Jaggia5e10572017-11-15 14:36:26 +0100657 // Use mTmpTransaction instead of mPendingTransaction because we don't want to commit
658 // other changes in mPendingTransaction at this point.
659 w.handleWindowMovedIfNeeded(mTmpTransaction);
660 SurfaceControl.mergeToGlobalTransaction(mTmpTransaction);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800661
662 final WindowStateAnimator winAnimator = w.mWinAnimator;
663
664 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
665 w.mContentChanged = false;
666
667 // Moved from updateWindowsAndWallpaperLocked().
668 if (w.mHasSurface) {
669 // Take care of the window being ready to display.
670 final boolean committed = winAnimator.commitFinishDrawingLocked();
671 if (isDefaultDisplay && committed) {
672 if (w.mAttrs.type == TYPE_DREAM) {
673 // HACK: When a dream is shown, it may at that point hide the lock screen.
674 // So we need to redo the layout to let the phone window manager make this
675 // happen.
676 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
677 if (DEBUG_LAYOUT_REPEATS) {
678 surfacePlacer.debugLayoutRepeats(
679 "dream and commitFinishDrawingLocked true",
680 pendingLayoutChanges);
681 }
682 }
683 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
684 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
685 "First draw done in potential wallpaper target " + w);
686 root.mWallpaperMayChange = true;
687 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
688 if (DEBUG_LAYOUT_REPEATS) {
689 surfacePlacer.debugLayoutRepeats(
690 "wallpaper and commitFinishDrawingLocked true",
691 pendingLayoutChanges);
692 }
693 }
694 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700695 final TaskStack stack = w.getStack();
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200696 if (!winAnimator.isWaitingForOpening()
Robert Carr2117aaf2017-04-25 14:46:50 -0700697 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800698 // Updates the shown frame before we set up the surface. This is needed
699 // because the resizing could change the top-left position (in addition to
700 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
701 // position the surface.
702 //
703 // If an animation is being started, we can't call this method because the
704 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700705 // we do want to update the position if the window is animating. We make an exception
706 // for the bounds animating state, where an application may have been waiting
707 // for an exit animation to start, but instead enters PiP. We need to ensure
708 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800709 winAnimator.computeShownFrameLocked();
710 }
711 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
Jorim Jaggia5e10572017-11-15 14:36:26 +0100712
713 // Since setSurfaceBoundariesLocked applies the clipping, we need to apply the position
chaviwe07246a2017-12-12 16:18:29 -0800714 // to the surface of the window container and also the position of the stack window
715 // container as well. Use mTmpTransaction instead of mPendingTransaction to avoid
716 // committing any existing changes in there.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100717 w.updateSurfacePosition(mTmpTransaction);
chaviwe07246a2017-12-12 16:18:29 -0800718 if (stack != null) {
719 stack.updateSurfaceBounds(mTmpTransaction);
720 }
Jorim Jaggia5e10572017-11-15 14:36:26 +0100721 SurfaceControl.mergeToGlobalTransaction(mTmpTransaction);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800722 }
723
724 final AppWindowToken atoken = w.mAppToken;
725 if (atoken != null) {
726 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
727 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
728 mTmpUpdateAllDrawn.add(atoken);
729 }
730 }
731
732 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
733 && w.isDisplayedLw()) {
734 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
735 }
736
737 w.updateResizingWindowIfNeeded();
738 };
739
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800740 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800741 * Create new {@link DisplayContent} instance, add itself to the root window container and
742 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700743 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800744 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700745 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
746 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700747 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700748 DisplayContent(Display display, WindowManagerService service,
Robert Carrb1579c82017-09-05 14:54:47 -0700749 WallpaperController wallpaperController) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100750 super(service);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800751 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
752 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
753 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
754 + " new=" + display);
755 }
756
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700757 mDisplay = display;
758 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700759 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700760 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700761 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700762 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000763 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700764 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800765 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700766 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700767
Robert Carrb1579c82017-09-05 14:54:47 -0700768 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth);
769
770 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
771 .setSize(mSurfaceSize, mSurfaceSize)
772 .setOpaque(true);
773 mWindowingLayer = b.setName("Display Root").build();
774 mOverlayLayer = b.setName("Display Overlays").build();
775
Robert Carrf59b8dd2017-10-02 18:58:36 -0700776 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700777 .setLayerStack(mWindowingLayer, mDisplayId)
778 .show(mWindowingLayer)
779 .setLayer(mOverlayLayer, 1)
780 .setLayerStack(mOverlayLayer, mDisplayId)
781 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700782 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700783
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700784 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700785 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700786 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700787 super.addChild(mAboveAppWindowsContainers, null);
788 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800789
790 // Add itself as a child to the root container.
791 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700792
793 // TODO(b/62541591): evaluate whether this is the best spot to declare the
794 // {@link DisplayContent} ready for use.
795 mDisplayReady = true;
796 }
797
798 boolean isReady() {
799 // The display is ready when the system and the individual display are both ready.
800 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700801 }
802
803 int getDisplayId() {
804 return mDisplayId;
805 }
806
Wale Ogunwale02319a62016-09-26 15:21:22 -0700807 WindowToken getWindowToken(IBinder binder) {
808 return mTokenMap.get(binder);
809 }
810
811 AppWindowToken getAppWindowToken(IBinder binder) {
812 final WindowToken token = getWindowToken(binder);
813 if (token == null) {
814 return null;
815 }
816 return token.asAppWindowToken();
817 }
818
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700819 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700820 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
821 if (dc != null) {
822 // We currently don't support adding a window token to the display if the display
823 // already has the binder mapped to another token. If there is a use case for supporting
824 // this moving forward we will either need to merge the WindowTokens some how or have
825 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700826 throw new IllegalArgumentException("Can't map token=" + token + " to display="
827 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700828 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700829 if (binder == null) {
830 throw new IllegalArgumentException("Can't map token=" + token + " to display="
831 + getName() + " binder is null");
832 }
833 if (token == null) {
834 throw new IllegalArgumentException("Can't map null token to display="
835 + getName() + " binder=" + binder);
836 }
837
Wale Ogunwale02319a62016-09-26 15:21:22 -0700838 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700839
840 if (token.asAppWindowToken() == null) {
841 // Add non-app token to container hierarchy on the display. App tokens are added through
842 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700843 switch (token.windowType) {
844 case TYPE_WALLPAPER:
845 mBelowAppWindowsContainers.addChild(token);
846 break;
847 case TYPE_INPUT_METHOD:
848 case TYPE_INPUT_METHOD_DIALOG:
849 mImeWindowsContainers.addChild(token);
850 break;
851 default:
852 mAboveAppWindowsContainers.addChild(token);
853 break;
854 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700855 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700856 }
857
858 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700859 final WindowToken token = mTokenMap.remove(binder);
860 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800861 token.setExiting();
862 }
863 return token;
864 }
865
866 /** Changes the display the input window token is housed on to this one. */
867 void reParentWindowToken(WindowToken token) {
868 final DisplayContent prevDc = token.getDisplayContent();
869 if (prevDc == this) {
870 return;
871 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800872 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
873 && token.asAppWindowToken() == null) {
874 // Removed the token from the map, but made sure it's not an app token before removing
875 // from parent.
876 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700877 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800878
879 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700880 }
881
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700882 void removeAppToken(IBinder binder) {
883 final WindowToken token = removeWindowToken(binder);
884 if (token == null) {
885 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
886 return;
887 }
888
889 final AppWindowToken appToken = token.asAppWindowToken();
890
891 if (appToken == null) {
892 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
893 return;
894 }
895
896 appToken.onRemovedFromDisplay();
897 }
898
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700899 Display getDisplay() {
900 return mDisplay;
901 }
902
Craig Mautner59c00972012-07-30 12:10:24 -0700903 DisplayInfo getDisplayInfo() {
904 return mDisplayInfo;
905 }
906
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700907 DisplayMetrics getDisplayMetrics() {
908 return mDisplayMetrics;
909 }
910
Andrii Kulian8ee72852017-03-10 10:36:45 -0800911 int getRotation() {
912 return mRotation;
913 }
914
915 void setRotation(int newRotation) {
916 mRotation = newRotation;
917 }
918
919 int getLastOrientation() {
920 return mLastOrientation;
921 }
922
923 void setLastOrientation(int orientation) {
924 mLastOrientation = orientation;
925 }
926
927 boolean getAltOrientation() {
928 return mAltOrientation;
929 }
930
931 void setAltOrientation(boolean altOrientation) {
932 mAltOrientation = altOrientation;
933 }
934
935 int getLastWindowForcedOrientation() {
936 return mLastWindowForcedOrientation;
937 }
938
Andrii Kulian06d07d62017-03-14 11:11:47 -0700939 /**
940 * Update rotation of the display.
941 *
942 * Returns true if the rotation has been changed. In this case YOU MUST CALL
943 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
944 */
945 boolean updateRotationUnchecked(boolean inTransaction) {
946 if (mService.mDeferredRotationPauseCount > 0) {
947 // Rotation updates have been paused temporarily. Defer the update until
948 // updates have been resumed.
949 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
950 return false;
951 }
952
953 ScreenRotationAnimation screenRotationAnimation =
954 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
955 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
956 // Rotation updates cannot be performed while the previous rotation change
957 // animation is still in progress. Skip this update. We will try updating
958 // again after the animation is finished and the display is unfrozen.
959 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
960 return false;
961 }
962 if (mService.mDisplayFrozen) {
963 // Even if the screen rotation animation has finished (e.g. isAnimating
964 // returns false), there is still some time where we haven't yet unfrozen
965 // the display. We also need to abort rotation here.
966 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
967 "Deferring rotation, still finishing previous rotation");
968 return false;
969 }
970
971 if (!mService.mDisplayEnabled) {
972 // No point choosing a rotation if the display is not enabled.
973 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
974 return false;
975 }
976
977 final int oldRotation = mRotation;
978 final int lastOrientation = mLastOrientation;
979 final boolean oldAltOrientation = mAltOrientation;
980 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr0e007272017-10-02 13:00:55 -0700981 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
Robert Carr897215d2017-03-29 12:25:50 -0700982 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700983
Robert Carr0e007272017-10-02 13:00:55 -0700984 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700985 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
986 if (seamlessRotated != null) {
987 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
988 // to complete (that is, waiting for windows to redraw). It's tempting to check
989 // w.mSeamlessRotationCount but that could be incorrect in the case of
990 // window-removal.
991 return false;
992 }
Robert Carr0e007272017-10-02 13:00:55 -0700993
994 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700995 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -0700996 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -0700997 mayRotateSeamlessly = false;
998 }
999 for (int i = 0; i < mService.mSessions.size(); i++) {
1000 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1001 mayRotateSeamlessly = false;
1002 break;
1003 }
1004 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001005 }
Robert Carr0e007272017-10-02 13:00:55 -07001006 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001007
1008 // TODO: Implement forced rotation changes.
1009 // Set mAltOrientation to indicate that the application is receiving
1010 // an orientation that has different metrics than it expected.
1011 // eg. Portrait instead of Landscape.
1012
1013 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
1014 lastOrientation, rotation);
1015
1016 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
1017 + ", got rotation " + rotation + " which has "
1018 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1019
1020 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1021 // No change.
1022 return false;
1023 }
1024
1025 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
1026 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1027 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1028
1029 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1030 mService.mWaitingForConfig = true;
1031 }
1032
1033 mRotation = rotation;
1034 mAltOrientation = altOrientation;
1035 if (isDefaultDisplay) {
1036 mService.mPolicy.setRotationLw(rotation);
1037 }
1038
1039 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1040 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1041 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1042 WINDOW_FREEZE_TIMEOUT_DURATION);
1043
1044 setLayoutNeeded();
1045 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001046 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001047
1048 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001049 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001050 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1051 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1052 mDisplayId);
1053 } else {
1054 // The screen rotation animation uses a screenshot to freeze the screen
1055 // while windows resize underneath.
1056 // When we are rotating seamlessly, we allow the elements to transition
1057 // to their rotated state independently and without a freeze required.
1058 screenRotationAnimation = null;
1059
1060 // We have to reset this in case a window was removed before it
1061 // finished seamless rotation.
1062 mService.mSeamlessRotationCount = 0;
1063 }
1064
1065 // We need to update our screen size information to match the new rotation. If the rotation
1066 // has actually changed then this method will return true and, according to the comment at
1067 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1068 // By updating the Display info here it will be available to
1069 // #computeScreenConfiguration() later.
1070 updateDisplayAndOrientation(getConfiguration().uiMode);
1071
1072 if (!inTransaction) {
1073 if (SHOW_TRANSACTIONS) {
1074 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1075 }
1076 mService.openSurfaceTransaction();
1077 }
1078 try {
1079 // NOTE: We disable the rotation in the emulator because
1080 // it doesn't support hardware OpenGL emulation yet.
1081 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1082 && screenRotationAnimation.hasScreenshot()) {
Robert Carrb1579c82017-09-05 14:54:47 -07001083 if (screenRotationAnimation.setRotationInTransaction(rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001084 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1085 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1086 mService.scheduleAnimationLocked();
1087 }
1088 }
1089
1090 if (rotateSeamlessly) {
1091 forAllWindows(w -> {
1092 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1093 }, true /* traverseTopToBottom */);
1094 }
1095
1096 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1097 } finally {
1098 if (!inTransaction) {
Adrian Roos111aff92017-09-27 18:11:46 +02001099 mService.closeSurfaceTransaction("setRotationUnchecked");
Andrii Kulian06d07d62017-03-14 11:11:47 -07001100 if (SHOW_LIGHT_TRANSACTIONS) {
1101 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1102 }
1103 }
1104 }
1105
1106 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001107 if (w.mHasSurface && !rotateSeamlessly) {
1108 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001109 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001110 mService.mRoot.mOrientationChangeComplete = false;
1111 w.mLastFreezeDuration = 0;
1112 }
1113 w.mReportOrientationChanged = true;
1114 }, true /* traverseTopToBottom */);
1115
1116 if (rotateSeamlessly) {
1117 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1118 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1119 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1120 }
1121
1122 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1123 final WindowManagerService.RotationWatcher rotationWatcher
1124 = mService.mRotationWatchers.get(i);
1125 if (rotationWatcher.mDisplayId == mDisplayId) {
1126 try {
1127 rotationWatcher.mWatcher.onRotationChanged(rotation);
1128 } catch (RemoteException e) {
1129 // Ignore
1130 }
1131 }
1132 }
1133
1134 // TODO (multi-display): Magnification is supported only for the default display.
1135 // Announce rotation only if we will not animate as we already have the
1136 // windows in final state. Otherwise, we make this call at the rotation end.
1137 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1138 && isDefaultDisplay) {
1139 mService.mAccessibilityController.onRotationChangedLocked(this);
1140 }
1141
1142 return true;
1143 }
1144
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001145 void configureDisplayPolicy() {
1146 mService.mPolicy.setInitialDisplaySize(getDisplay(),
1147 mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1148
1149 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
1150 }
1151
Andrii Kulian06d07d62017-03-14 11:11:47 -07001152 /**
1153 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1154 * changed.
1155 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1156 */
1157 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1158 // Use the effective "visual" dimensions based on current rotation
1159 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1160 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1161 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1162 int dw = realdw;
1163 int dh = realdh;
1164
1165 if (mAltOrientation) {
1166 if (realdw > realdh) {
1167 // Turn landscape into portrait.
1168 int maxw = (int)(realdh/1.3f);
1169 if (maxw < realdw) {
1170 dw = maxw;
1171 }
1172 } else {
1173 // Turn portrait into landscape.
1174 int maxh = (int)(realdw/1.3f);
1175 if (maxh < realdh) {
1176 dh = maxh;
1177 }
1178 }
1179 }
1180
1181 // Update application display metrics.
1182 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1183 mDisplayId);
1184 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1185 mDisplayId);
1186 mDisplayInfo.rotation = mRotation;
1187 mDisplayInfo.logicalWidth = dw;
1188 mDisplayInfo.logicalHeight = dh;
1189 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1190 mDisplayInfo.appWidth = appWidth;
1191 mDisplayInfo.appHeight = appHeight;
1192 if (isDefaultDisplay) {
1193 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1194 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1195 }
1196 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1197 if (mDisplayScalingDisabled) {
1198 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1199 } else {
1200 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1201 }
1202
1203 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1204 mDisplayInfo);
1205
1206 mBaseDisplayRect.set(0, 0, dw, dh);
1207
1208 if (isDefaultDisplay) {
1209 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1210 mCompatDisplayMetrics);
1211 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001212
1213 updateBounds();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001214 return mDisplayInfo;
1215 }
1216
1217 /**
1218 * Compute display configuration based on display properties and policy settings.
1219 * Do not call if mDisplayReady == false.
1220 */
1221 void computeScreenConfiguration(Configuration config) {
1222 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1223
1224 final int dw = displayInfo.logicalWidth;
1225 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001226 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1227 // TODO: Probably best to set this based on some setting in the display content object,
1228 // so the display can be configured for things like fullscreen.
1229 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001230
Andrii Kulian06d07d62017-03-14 11:11:47 -07001231 config.screenWidthDp =
1232 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1233 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1234 config.screenHeightDp =
1235 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1236 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001237
1238 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1239 final int leftInset = mTmpRect.left;
1240 final int topInset = mTmpRect.top;
1241 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001242 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1243 leftInset + displayInfo.appWidth /* right */,
1244 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001245 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1246 || displayInfo.rotation == Surface.ROTATION_270);
1247
1248 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1249 mDisplayMetrics.density, config);
1250
1251 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1252 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1253 ? Configuration.SCREENLAYOUT_ROUND_YES
1254 : Configuration.SCREENLAYOUT_ROUND_NO);
1255
1256 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1257 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1258 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1259 dh, mDisplayId);
1260 config.densityDpi = displayInfo.logicalDensityDpi;
1261
1262 config.colorMode =
1263 (displayInfo.isHdr()
1264 ? Configuration.COLOR_MODE_HDR_YES
1265 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001266 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001267 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1268 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1269
1270 // Update the configuration based on available input devices, lid switch,
1271 // and platform configuration.
1272 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1273 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1274 config.navigation = Configuration.NAVIGATION_NONAV;
1275
1276 int keyboardPresence = 0;
1277 int navigationPresence = 0;
1278 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1279 final int len = devices != null ? devices.length : 0;
1280 for (int i = 0; i < len; i++) {
1281 InputDevice device = devices[i];
1282 if (!device.isVirtual()) {
1283 final int sources = device.getSources();
1284 final int presenceFlag = device.isExternal() ?
1285 WindowManagerPolicy.PRESENCE_EXTERNAL :
1286 WindowManagerPolicy.PRESENCE_INTERNAL;
1287
1288 // TODO(multi-display): Configure on per-display basis.
1289 if (mService.mIsTouchDevice) {
1290 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1291 InputDevice.SOURCE_TOUCHSCREEN) {
1292 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1293 }
1294 } else {
1295 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1296 }
1297
1298 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1299 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1300 navigationPresence |= presenceFlag;
1301 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1302 && config.navigation == Configuration.NAVIGATION_NONAV) {
1303 config.navigation = Configuration.NAVIGATION_DPAD;
1304 navigationPresence |= presenceFlag;
1305 }
1306
1307 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1308 config.keyboard = Configuration.KEYBOARD_QWERTY;
1309 keyboardPresence |= presenceFlag;
1310 }
1311 }
1312 }
1313
1314 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1315 config.navigation = Configuration.NAVIGATION_DPAD;
1316 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1317 }
1318
1319 // Determine whether a hard keyboard is available and enabled.
1320 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1321 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1322 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1323 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1324 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1325 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1326 }
1327
1328 // Let the policy update hidden states.
1329 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1330 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1331 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1332 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1333 }
1334
1335 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1336 int displayId) {
1337 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1338 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1339 final int unrotDw, unrotDh;
1340 if (rotated) {
1341 unrotDw = dh;
1342 unrotDh = dw;
1343 } else {
1344 unrotDw = dw;
1345 unrotDh = dh;
1346 }
1347 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1348 displayId);
1349 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1350 displayId);
1351 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1352 displayId);
1353 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1354 displayId);
1355 return sw;
1356 }
1357
1358 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1359 DisplayMetrics dm, int dw, int dh, int displayId) {
1360 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1361 displayId);
1362 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1363 uiMode, displayId);
1364 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1365 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1366 if (curSize == 0 || size < curSize) {
1367 curSize = size;
1368 }
1369 return curSize;
1370 }
1371
1372 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1373 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1374
1375 // We need to determine the smallest width that will occur under normal
1376 // operation. To this, start with the base screen size and compute the
1377 // width under the different possible rotations. We need to un-rotate
1378 // the current screen dimensions before doing this.
1379 int unrotDw, unrotDh;
1380 if (rotated) {
1381 unrotDw = dh;
1382 unrotDh = dw;
1383 } else {
1384 unrotDw = dw;
1385 unrotDh = dh;
1386 }
1387 displayInfo.smallestNominalAppWidth = 1<<30;
1388 displayInfo.smallestNominalAppHeight = 1<<30;
1389 displayInfo.largestNominalAppWidth = 0;
1390 displayInfo.largestNominalAppHeight = 0;
1391 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1392 unrotDh);
1393 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1394 unrotDw);
1395 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1396 unrotDh);
1397 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1398 unrotDw);
1399 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1400 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1401 displayId);
1402 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1403 displayId);
1404 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1405 displayId);
1406 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1407 displayId);
1408 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1409 outConfig.screenLayout = sl;
1410 }
1411
1412 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1413 int uiMode, int displayId) {
1414 // Get the app screen size at this rotation.
1415 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1416 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1417
1418 // Compute the screen layout size class for this rotation.
1419 int longSize = w;
1420 int shortSize = h;
1421 if (longSize < shortSize) {
1422 int tmp = longSize;
1423 longSize = shortSize;
1424 shortSize = tmp;
1425 }
1426 longSize = (int)(longSize/density);
1427 shortSize = (int)(shortSize/density);
1428 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1429 }
1430
1431 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1432 int uiMode, int dw, int dh) {
1433 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1434 displayId);
1435 if (width < displayInfo.smallestNominalAppWidth) {
1436 displayInfo.smallestNominalAppWidth = width;
1437 }
1438 if (width > displayInfo.largestNominalAppWidth) {
1439 displayInfo.largestNominalAppWidth = width;
1440 }
1441 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1442 displayId);
1443 if (height < displayInfo.smallestNominalAppHeight) {
1444 displayInfo.smallestNominalAppHeight = height;
1445 }
1446 if (height > displayInfo.largestNominalAppHeight) {
1447 displayInfo.largestNominalAppHeight = height;
1448 }
1449 }
1450
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001451 DockedStackDividerController getDockedDividerController() {
1452 return mDividerControllerLocked;
1453 }
1454
Winson Chung655332c2016-10-31 13:14:28 -07001455 PinnedStackController getPinnedStackController() {
1456 return mPinnedStackControllerLocked;
1457 }
1458
Jeff Browna506a6e2013-06-04 00:02:38 -07001459 /**
1460 * Returns true if the specified UID has access to this display.
1461 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001462 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001463 return mDisplay.hasAccess(uid);
1464 }
1465
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001466 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001467 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001468 }
1469
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001470 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001471 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001472 }
1473
Wale Ogunwale61911492017-10-11 08:50:50 -07001474 /**
1475 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1476 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001477 TaskStack getSplitScreenPrimaryStack() {
1478 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001479 return (stack != null && stack.isVisible()) ? stack : null;
1480 }
1481
1482 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001483 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001484 * not visible.
1485 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001486 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1487 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001488 }
1489
1490 TaskStack getPinnedStack() {
1491 return mTaskStackContainers.getPinnedStack();
1492 }
1493
1494 private boolean hasPinnedStack() {
1495 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001496 }
1497
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001498 /**
1499 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1500 * Null is no compatible stack on the display.
1501 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001502 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001503 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1504 }
1505
1506 /**
1507 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1508 * activity type. Null is no compatible stack on the display.
1509 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001510 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001511 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001512 }
1513
Bryce Lee48f4b572017-04-10 10:54:15 -07001514 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001515 TaskStack getTopStack() {
1516 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001517 }
1518
Wale Ogunwale61911492017-10-11 08:50:50 -07001519 void onStackWindowingModeChanged(TaskStack stack) {
1520 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001521 }
1522
Andrii Kulian441e4492016-09-29 15:25:00 -07001523 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001524 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001525 super.onConfigurationChanged(newParentConfig);
1526
Andrii Kulian3a507b52016-09-19 18:14:12 -07001527 // The display size information is heavily dependent on the resources in the current
1528 // configuration, so we need to reconfigure it every time the configuration changes.
1529 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1530 mService.reconfigureDisplayLocked(this);
1531
Bryce Leef3c6a472017-11-14 14:53:06 -08001532 final DockedStackDividerController dividerController = getDockedDividerController();
1533
1534 if (dividerController != null) {
1535 getDockedDividerController().onConfigurationChanged();
1536 }
1537
1538 final PinnedStackController pinnedStackController = getPinnedStackController();
1539
1540 if (pinnedStackController != null) {
1541 getPinnedStackController().onConfigurationChanged();
1542 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001543 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001544
Andrii Kulian441e4492016-09-29 15:25:00 -07001545 /**
1546 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1547 * bounds were updated.
1548 */
1549 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001550 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1551 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001552 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001553 changedStackList.add(stack.mStackId);
1554 }
1555 }
Winson Chung32c566f2017-04-11 18:31:21 -07001556
1557 // If there was no pinned stack, we still need to notify the controller of the display info
1558 // update as a result of the config change. We do this here to consolidate the flow between
1559 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001560 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001561 mPinnedStackControllerLocked.onDisplayInfoChanged();
1562 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001563 }
1564
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001565 @Override
1566 boolean fillsParent() {
1567 return true;
1568 }
1569
1570 @Override
1571 boolean isVisible() {
1572 return true;
1573 }
1574
1575 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001576 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001577 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001578 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001579 }
1580
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001581 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001582 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1583 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1584 // target, or here in order if there isn't an IME target.
1585 if (traverseTopToBottom) {
1586 for (int i = mChildren.size() - 1; i >= 0; --i) {
1587 final DisplayChildWindowContainer child = mChildren.get(i);
1588 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1589 // In this case the Ime windows will be processed above their target so we skip
1590 // here.
1591 continue;
1592 }
1593 if (child.forAllWindows(callback, traverseTopToBottom)) {
1594 return true;
1595 }
1596 }
1597 } else {
1598 final int count = mChildren.size();
1599 for (int i = 0; i < count; i++) {
1600 final DisplayChildWindowContainer child = mChildren.get(i);
1601 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1602 // In this case the Ime windows will be processed above their target so we skip
1603 // here.
1604 continue;
1605 }
1606 if (child.forAllWindows(callback, traverseTopToBottom)) {
1607 return true;
1608 }
1609 }
1610 }
1611 return false;
1612 }
1613
1614 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1615 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1616 }
1617
Wale Ogunwale399c8692017-05-08 14:22:42 -07001618 @Override
1619 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001620 final WindowManagerPolicy policy = mService.mPolicy;
1621
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001622 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001623 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001624 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001625 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001626 // If the display is frozen, some activities may be in the middle of restarting, and
1627 // thus have removed their old window. If the window has the flag to hide the lock
1628 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1629 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001630 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001631 } else if (policy.isKeyguardLocked()) {
1632 // Use the last orientation the while the display is frozen with the keyguard
1633 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1634 // window. We don't want to check the show when locked window directly though as
1635 // things aren't stable while the display is frozen, for example the window could be
1636 // momentarily unavailable due to activity relaunch.
1637 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001638 + "return " + mLastOrientation);
1639 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001640 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001641 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001642 final int orientation = mAboveAppWindowsContainers.getOrientation();
1643 if (orientation != SCREEN_ORIENTATION_UNSET) {
1644 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001645 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001646 }
1647
Wale Ogunwale399c8692017-05-08 14:22:42 -07001648 // Top system windows are not requesting an orientation. Start searching from apps.
1649 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001650 }
1651
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001652 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001653 // Check if display metrics changed and update base values if needed.
1654 updateBaseDisplayMetricsIfNeeded();
1655
Craig Mautner722285e2012-09-07 13:55:58 -07001656 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001657 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001658
Wale Ogunwale61911492017-10-11 08:50:50 -07001659 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1660 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001661 }
Craig Mautner722285e2012-09-07 13:55:58 -07001662 }
1663
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001664 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001665 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1666 if (displayManagerInternal != null) {
1667 // Bootstrap the default logical display from the display manager.
1668 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1669 if (newDisplayInfo != null) {
1670 mDisplayInfo.copyFrom(newDisplayInfo);
1671 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001672 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001673
Andrii Kuliancd097992017-03-23 18:31:59 -07001674 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1675 mDisplayInfo.logicalDensityDpi);
1676 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1677 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1678 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001679 }
1680
Andrii Kuliancd097992017-03-23 18:31:59 -07001681 /**
1682 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1683 * values.
1684 */
1685 private void updateBaseDisplayMetricsIfNeeded() {
1686 // Get real display metrics without overrides from WM.
1687 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1688 final int orientation = mDisplayInfo.rotation;
1689 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1690 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1691 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1692 final int newDensity = mDisplayInfo.logicalDensityDpi;
1693
1694 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1695 || mInitialDisplayHeight != newHeight
1696 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1697
1698 if (displayMetricsChanged) {
1699 // Check if display size or density is forced.
1700 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1701 || mBaseDisplayHeight != mInitialDisplayHeight;
1702 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1703
1704 // If there is an override set for base values - use it, otherwise use new values.
1705 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1706 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1707 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1708
1709 // Real display metrics changed, so we should also update initial values.
1710 mInitialDisplayWidth = newWidth;
1711 mInitialDisplayHeight = newHeight;
1712 mInitialDisplayDensity = newDensity;
1713 mService.reconfigureDisplayLocked(this);
1714 }
1715 }
1716
Bryce Lee27cec322017-03-21 09:41:37 -07001717 /** Sets the maximum width the screen resolution can be */
1718 void setMaxUiWidth(int width) {
1719 if (DEBUG_DISPLAY) {
1720 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1721 }
1722
1723 mMaxUiWidth = width;
1724
1725 // Update existing metrics.
1726 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1727 }
1728
1729 /** Update base (override) display metrics. */
1730 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1731 mBaseDisplayWidth = baseWidth;
1732 mBaseDisplayHeight = baseHeight;
1733 mBaseDisplayDensity = baseDensity;
1734
1735 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1736 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1737 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1738 mBaseDisplayWidth = mMaxUiWidth;
1739
1740 if (DEBUG_DISPLAY) {
1741 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1742 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1743 + " on display:" + getDisplayId());
1744 }
1745 }
1746
1747 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08001748
1749 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07001750 }
1751
Wale Ogunwaledb506192017-12-08 10:57:32 -08001752 void getStableRect(Rect out) {
1753 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08001754 }
1755
Wale Ogunwale61911492017-10-11 08:50:50 -07001756 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001757 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1758 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001759
Wale Ogunwale61911492017-10-11 08:50:50 -07001760 final TaskStack stack = new TaskStack(mService, stackId, controller);
1761 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001762 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001763 }
1764
Andrii Kulian51c1b672017-04-07 18:39:32 -07001765 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001766 final DisplayContent prevDc = stack.getDisplayContent();
1767 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001768 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1769 + " which is not currently attached to any display");
1770 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001771 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001772 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1773 + " to its current displayId=" + mDisplayId);
1774 }
1775
Wale Ogunwale61911492017-10-11 08:50:50 -07001776 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001777 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001778 }
1779
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001780 @Override
1781 protected void addChild(DisplayChildWindowContainer child,
1782 Comparator<DisplayChildWindowContainer> comparator) {
1783 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1784 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001785
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001786 @Override
1787 protected void addChild(DisplayChildWindowContainer child, int index) {
1788 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1789 }
1790
1791 @Override
1792 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001793 // Only allow removal of direct children from this display if the display is in the process
1794 // of been removed.
1795 if (mRemovingDisplay) {
1796 super.removeChild(child);
1797 return;
1798 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001799 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001800 }
1801
Andrii Kuliand2765632016-12-12 22:26:34 -08001802 @Override
1803 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1804 // Children of the display are statically ordered, so the real intention here is to perform
1805 // the operation on the display and not the static direct children.
1806 getParent().positionChildAt(position, this, includingParents);
1807 }
1808
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001809 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001810 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1811 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001812 final int taskId = stack.taskIdFromPoint(x, y);
1813 if (taskId != -1) {
1814 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001815 }
1816 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001817 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001818 }
1819
Chong Zhang8e89b312015-09-09 15:09:30 -07001820 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001821 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001822 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001823 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001824 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001825 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001826 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07001827 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1828 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001829 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001830 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001831 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001832
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001833 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1834 if (mTmpTaskForResizePointSearchResult.searchDone) {
1835 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001836 }
1837 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001838 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001839 }
1840
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001841 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001842 // The provided task is the task on this display with focus, so if WindowManagerService's
1843 // focused app is not on this display, focusedTask will be null.
1844 if (focusedTask == null) {
1845 mTouchExcludeRegion.setEmpty();
1846 } else {
1847 mTouchExcludeRegion.set(mBaseDisplayRect);
1848 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1849 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07001850 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1851 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001852 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
1853 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08001854 }
1855 // If we removed the focused task above, add it back and only leave its
1856 // outside touch area in the exclusion. TapDectector is not interested in
1857 // any touch inside the focused task itself.
1858 if (!mTmpRect2.isEmpty()) {
1859 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1860 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001861 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001862 final WindowState inputMethod = mService.mInputMethodWindow;
1863 if (inputMethod != null && inputMethod.isVisibleLw()) {
1864 // If the input method is visible and the user is typing, we don't want these touch
1865 // events to be intercepted and used to change focus. This would likely cause a
1866 // disappearance of the input method.
1867 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001868 if (inputMethod.getDisplayId() == mDisplayId) {
1869 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1870 } else {
1871 // IME is on a different display, so we need to update its tap detector.
1872 // TODO(multidisplay): Remove when IME will always appear on same display.
1873 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1874 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001875 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001876 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1877 WindowState win = mTapExcludedWindows.get(i);
1878 win.getTouchableRegion(mTmpRegion);
1879 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1880 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001881 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07001882 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001883 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001884 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001885 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1886 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001887 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001888 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001889 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001890 }
1891
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001892 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001893 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001894 super.switchUser();
1895 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001896 }
1897
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001898 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001899 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1900 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001901 }
1902 }
1903
Wale Ogunwale10124582016-09-15 20:25:50 -07001904 @Override
1905 void removeIfPossible() {
1906 if (isAnimating()) {
1907 mDeferredRemoval = true;
1908 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001909 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001910 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001911 }
1912
Wale Ogunwale10124582016-09-15 20:25:50 -07001913 @Override
1914 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001915 mRemovingDisplay = true;
1916 try {
1917 super.removeImmediately();
1918 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07001919 if (mService.canDispatchPointerEvents()) {
1920 if (mTapDetector != null) {
1921 mService.unregisterPointerEventListener(mTapDetector);
1922 }
1923 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
1924 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1925 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001926 }
Robert Carrb1579c82017-09-05 14:54:47 -07001927 // The pending transaction won't be applied so we should
1928 // just clean up any surfaces pending destruction.
1929 onPendingTransactionApplied();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001930 } finally {
1931 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001932 }
Craig Mautner95da1082014-02-24 17:54:35 -08001933 }
1934
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001935 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001936 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001937 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001938 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1939
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001940 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001941 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001942 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001943 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001944 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001945 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001946 }
1947
Andrii Kulian0214ed92017-05-16 13:44:05 -07001948 /** @return 'true' if removal of this display content is deferred due to active animation. */
1949 boolean isRemovalDeferred() {
1950 return mDeferredRemoval;
1951 }
1952
Wale Ogunwale10124582016-09-15 20:25:50 -07001953 boolean animateForIme(float interpolatedValue, float animationTarget,
1954 float dividerAnimationTarget) {
1955 boolean updated = false;
1956
Wale Ogunwale61911492017-10-11 08:50:50 -07001957 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1958 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001959 if (stack == null || !stack.isAdjustedForIme()) {
1960 continue;
1961 }
1962
1963 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1964 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1965 updated = true;
1966 } else {
1967 mDividerControllerLocked.mLastAnimationProgress =
1968 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1969 mDividerControllerLocked.mLastDividerProgress =
1970 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1971 updated |= stack.updateAdjustForIme(
1972 mDividerControllerLocked.mLastAnimationProgress,
1973 mDividerControllerLocked.mLastDividerProgress,
1974 false /* force */);
1975 }
1976 if (interpolatedValue >= 1f) {
1977 stack.endImeAdjustAnimation();
1978 }
1979 }
1980
1981 return updated;
1982 }
1983
1984 boolean clearImeAdjustAnimation() {
1985 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07001986 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1987 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001988 if (stack != null && stack.isAdjustedForIme()) {
1989 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1990 changed = true;
1991 }
1992 }
1993 return changed;
1994 }
1995
1996 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001997 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1998 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001999 if (stack.isVisible() && stack.isAdjustedForIme()) {
2000 stack.beginImeAdjustAnimation();
2001 }
2002 }
2003 }
2004
2005 void adjustForImeIfNeeded() {
2006 final WindowState imeWin = mService.mInputMethodWindow;
2007 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2008 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002009 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002010 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2011 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2012 imeTargetStack.getDockSide() : DOCKED_INVALID;
2013 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2014 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2015 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002016 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002017 final boolean imeHeightChanged = imeVisible &&
2018 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2019
2020 // The divider could be adjusted for IME position, or be thinner than usual,
2021 // or both. There are three possible cases:
2022 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2023 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2024 // - If IME is not visible, divider is not moved and is normal width.
2025
2026 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002027 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2028 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002029 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002030 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2031 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002032 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2033 } else {
2034 stack.resetAdjustedForIme(false);
2035 }
2036 }
2037 mDividerControllerLocked.setAdjustedForIme(
2038 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2039 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002040 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2041 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002042 stack.resetAdjustedForIme(!dockVisible);
2043 }
2044 mDividerControllerLocked.setAdjustedForIme(
2045 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2046 }
Winson Chung655332c2016-10-31 13:14:28 -07002047 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002048 }
2049
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002050 /**
2051 * If a window that has an animation specifying a colored background and the current wallpaper
2052 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2053 * suddenly disappear.
2054 */
2055 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002056 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2057 w -> w.mIsWallpaper && w.isVisibleNow());
2058
2059 if (visibleWallpaper != null) {
2060 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002061 }
2062 return winAnimator.mAnimLayer;
2063 }
2064
Wale Ogunwale10124582016-09-15 20:25:50 -07002065 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002066 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2067 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002068 stack.prepareFreezingTaskBounds();
2069 }
2070 }
2071
Wale Ogunwale94744212015-09-21 19:01:47 -07002072 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002073 getBounds(mTmpRect, newRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002074
2075 // Compute a transform matrix to undo the coordinate space transformation,
2076 // and present the window at the same physical position it previously occupied.
2077 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2078 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2079
2080 mTmpRectF.set(bounds);
2081 mTmpMatrix.mapRect(mTmpRectF);
2082 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002083 }
2084
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002085 static int deltaRotation(int oldRotation, int newRotation) {
2086 int delta = newRotation - oldRotation;
2087 if (delta < 0) delta += 4;
2088 return delta;
2089 }
2090
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002091 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002092 Matrix outMatrix) {
2093 // For rotations without Z-ordering we don't need the target rectangle's position.
2094 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2095 displayHeight, outMatrix);
2096 }
2097
2098 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2099 float displayWidth, float displayHeight, Matrix outMatrix) {
2100 switch (rotation) {
2101 case ROTATION_0:
2102 outMatrix.reset();
2103 break;
2104 case ROTATION_270:
2105 outMatrix.setRotate(270, 0, 0);
2106 outMatrix.postTranslate(0, displayHeight);
2107 outMatrix.postTranslate(rectTop, 0);
2108 break;
2109 case ROTATION_180:
2110 outMatrix.reset();
2111 break;
2112 case ROTATION_90:
2113 outMatrix.setRotate(90, 0, 0);
2114 outMatrix.postTranslate(displayWidth, 0);
2115 outMatrix.postTranslate(-rectTop, rectLeft);
2116 break;
2117 }
2118 }
2119
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002120 @CallSuper
2121 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002122 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002123 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002124 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002125 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002126 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2127 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002128 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002129 }
2130 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2131 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002132 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2133 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002134 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002135 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002136 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2137 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002138 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002139 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002140 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2141 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002142 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002143 }
2144 proto.write(DPI, mBaseDisplayDensity);
2145 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002146 proto.write(ROTATION, mRotation);
2147 final ScreenRotationAnimation screenRotationAnimation =
2148 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2149 if (screenRotationAnimation != null) {
2150 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2151 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002152 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002153 proto.end(token);
2154 }
2155
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002156 @Override
2157 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2158 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002159 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2160 final String subPrefix = " " + prefix;
2161 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2162 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2163 pw.print("dpi");
2164 if (mInitialDisplayWidth != mBaseDisplayWidth
2165 || mInitialDisplayHeight != mBaseDisplayHeight
2166 || mInitialDisplayDensity != mBaseDisplayDensity) {
2167 pw.print(" base=");
2168 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2169 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2170 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002171 if (mDisplayScalingDisabled) {
2172 pw.println(" noscale");
2173 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002174 pw.print(" cur=");
2175 pw.print(mDisplayInfo.logicalWidth);
2176 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2177 pw.print(" app=");
2178 pw.print(mDisplayInfo.appWidth);
2179 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2180 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2181 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2182 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2183 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002184 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002185 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002186 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002187
Craig Mautnerdc548482014-02-05 13:35:24 -08002188 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002189 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002190 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2191 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002192 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002193 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002194
Craig Mautnerdc548482014-02-05 13:35:24 -08002195 pw.println();
2196 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002197 pw.println();
2198 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002199 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002200 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002201 pw.print(" Exiting #"); pw.print(i);
2202 pw.print(' '); pw.print(token);
2203 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002204 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002205 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002206 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002207
Jorim Jaggi31f71702016-05-04 16:43:04 -07002208 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002209
2210 // Dump stack references
2211 final TaskStack homeStack = getHomeStack();
2212 if (homeStack != null) {
2213 pw.println(prefix + "homeStack=" + homeStack.getName());
2214 }
2215 final TaskStack pinnedStack = getPinnedStack();
2216 if (pinnedStack != null) {
2217 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2218 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002219 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002220 if (splitScreenPrimaryStack != null) {
2221 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2222 }
2223
2224 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002225 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002226 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002227 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002228
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002229 pw.println();
2230 mDisplayFrames.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002231 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002232
2233 @Override
2234 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002235 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002236 }
2237
2238 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002239 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002240 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002241
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002242 /** Returns true if the stack in the windowing mode is visible. */
2243 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002244 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002245 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002246 }
2247
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002248 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002249 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002250 final int x = (int) xf;
2251 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002252 final WindowState touchedWin = getWindow(w -> {
2253 final int flags = w.mAttrs.flags;
2254 if (!w.isVisibleLw()) {
2255 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002256 }
2257 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002258 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002259 }
2260
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002261 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002262 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002263 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002264 }
2265
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002266 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002267
2268 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002269 return mTmpRegion.contains(x, y) || touchFlags == 0;
2270 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002271
2272 return touchedWin;
2273 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002274
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002275 boolean canAddToastWindowForUid(int uid) {
2276 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002277 // Also if the app is focused adding more than one toast at
2278 // a time for better backwards compatibility.
2279 final WindowState focusedWindowForUid = getWindow(w ->
2280 w.mOwnerUid == uid && w.isFocused());
2281 if (focusedWindowForUid != null) {
2282 return true;
2283 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002284 final WindowState win = getWindow(w ->
2285 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2286 && !w.mWindowRemovalAllowed);
2287 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002288 }
2289
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002290 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002291 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2292 return;
2293 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002294
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002295 // Used to communicate the old focus to the callback method.
2296 mTmpWindow = oldFocus;
2297
2298 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002299 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002300
2301 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002302 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002303
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002304 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002305
2306 if (mTmpWindow == null) {
2307 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2308 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002309 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002310 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002311 }
2312
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002313 /** Updates the layer assignment of windows on this display. */
2314 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002315 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002316 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002317 if (setLayoutNeeded) {
2318 setLayoutNeeded();
2319 }
Robert Carrb1579c82017-09-05 14:54:47 -07002320
Robert Carrf59b8dd2017-10-02 18:58:36 -07002321 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002322 // the application of this transaction until the animation pass triggers
2323 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2324 // the hiding and showing of surfaces.
2325 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002326 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002327 }
2328
Wale Ogunwale1666e312016-12-16 11:27:18 -08002329 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2330 // moving containers or resizing them. Need to investigate the best way to have it automatically
2331 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002332 void layoutAndAssignWindowLayersIfNeeded() {
2333 mService.mWindowsChanged = true;
2334 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002335
2336 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2337 false /*updateInputWindows*/)) {
2338 assignWindowLayers(false /* setLayoutNeeded */);
2339 }
2340
2341 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2342 mService.mWindowPlacerLocked.performSurfacePlacement();
2343 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2344 }
2345
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002346 /** Returns true if a leaked surface was destroyed */
2347 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002348 // Used to indicate that a surface was leaked.
2349 mTmpWindow = null;
2350 forAllWindows(w -> {
2351 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002352 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002353 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002354 }
2355 if (!mService.mSessions.contains(wsa.mSession)) {
2356 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002357 + w + " surface=" + wsa.mSurfaceController
2358 + " token=" + w.mToken
2359 + " pid=" + w.mSession.mPid
2360 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002361 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002362 mService.mForceRemoves.add(w);
2363 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002364 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002365 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002366 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002367 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002368 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002369 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002370 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002371 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002372 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002373
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002374 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002375 }
2376
2377 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002378 * Determine and return the window that should be the IME target.
2379 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2380 * @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 +00002381 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002382 WindowState computeImeTarget(boolean updateImeTarget) {
2383 if (mService.mInputMethodWindow == null) {
2384 // There isn't an IME so there shouldn't be a target...That was easy!
2385 if (updateImeTarget) {
2386 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2387 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002388 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002389 }
2390 return null;
2391 }
2392
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002393 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2394 // same display. Or even when the current IME/target are not on the same screen as the next
2395 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002396 mUpdateImeTarget = updateImeTarget;
2397 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002398
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002399
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002400 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2401 // to be on top of it, but it is not -really- where input will go. So look down below
2402 // for a real window to target...
2403 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2404 final AppWindowToken token = target.mAppToken;
2405 if (token != null) {
2406 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2407 if (betterTarget != null) {
2408 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002409 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002410 }
2411 }
2412
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002413 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2414 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002415
2416 // Now, a special case -- if the last target's window is in the process of exiting, and is
2417 // above the new target, keep on the last target to avoid flicker. Consider for example a
2418 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2419 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2420 // scrim.
2421 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002422 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2423 && (target == null
2424 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002425 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002426 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002427 }
2428
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002429 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2430 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002431
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002432 if (target == null) {
2433 if (updateImeTarget) {
2434 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2435 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2436 + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002437 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002438 }
2439
2440 return null;
2441 }
2442
2443 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002444 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2445 if (token != null) {
2446
2447 // Now some fun for dealing with window animations that modify the Z order. We need
2448 // to look at all windows below the current target that are in this app, finding the
2449 // highest visible one in layering.
2450 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002451 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002452 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002453 }
2454
2455 if (highestTarget != null) {
2456 final AppTransition appTransition = mService.mAppTransition;
2457 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2458 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2459 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002460 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002461
2462 if (appTransition.isTransitionSet()) {
2463 // If we are currently setting up for an animation, hold everything until we
2464 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002465 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002466 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002467 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002468 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002469 // If the window we are currently targeting is involved with an animation,
2470 // and it is on top of the next target we will be over, then hold off on
2471 // moving until that is done.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002472 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002473 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002474 }
2475 }
2476 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002477
2478 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2479 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002480 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002481 }
2482
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002483 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002484 }
2485
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002486 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002487 if (target == mService.mInputMethodTarget
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002488 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002489 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002490 }
2491
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002492 mService.mInputMethodTarget = target;
2493 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002494 assignWindowLayers(false /* setLayoutNeeded */);
2495 }
2496
2497 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2498 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2499 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2500 }
2501
2502 // Used to indicate we have reached the first window in the range we are interested in.
2503 mTmpWindow = null;
2504
2505 // TODO: Figure-out a more efficient way to do this.
2506 final WindowState candidate = getWindow(w -> {
2507 if (w == top) {
2508 // Reached the first window in the range we are interested in.
2509 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002510 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002511 if (mTmpWindow == null) {
2512 return false;
2513 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002514
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002515 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2516 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002517 }
2518 // If we reached the bottom of the range of windows we are considering,
2519 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002520 if (w == bottom) {
2521 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002522 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002523 return false;
2524 });
2525
2526 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002527 }
2528
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002529 void setLayoutNeeded() {
2530 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2531 mLayoutNeeded = true;
2532 }
2533
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002534 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002535 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2536 mLayoutNeeded = false;
2537 }
2538
2539 boolean isLayoutNeeded() {
2540 return mLayoutNeeded;
2541 }
2542
Wale Ogunwale02319a62016-09-26 15:21:22 -07002543 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2544 if (mTokenMap.isEmpty()) {
2545 return;
2546 }
2547 pw.println(" Display #" + mDisplayId);
2548 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2549 while (it.hasNext()) {
2550 final WindowToken token = it.next();
2551 pw.print(" ");
2552 pw.print(token);
2553 if (dumpAll) {
2554 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002555 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07002556 } else {
2557 pw.println();
2558 }
2559 }
2560 }
2561
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002562 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002563 final int[] index = new int[1];
2564 forAllWindows(w -> {
2565 final WindowStateAnimator wAnim = w.mWinAnimator;
2566 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2567 index[0] = index[0] + 1;
2568 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002569 }
2570
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002571 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002572 forAllWindows(w -> {
2573 w.mWinAnimator.enableSurfaceTrace(fd);
2574 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002575 }
2576
2577 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002578 forAllWindows(w -> {
2579 w.mWinAnimator.disableSurfaceTrace();
2580 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002581 }
2582
Jorim Jaggife762342016-10-13 14:33:27 +02002583 /**
2584 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2585 */
2586 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2587 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002588 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002589 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2590 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002591 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02002592 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002593 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002594 }
2595
Wale Ogunwale494009b82016-10-21 09:01:38 -07002596 boolean checkWaitingForWindows() {
2597
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002598 mHaveBootMsg = false;
2599 mHaveApp = false;
2600 mHaveWallpaper = false;
2601 mHaveKeyguard = true;
2602
2603 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002604 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2605 return true;
2606 }
2607 if (w.isDrawnLw()) {
2608 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002609 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002610 } else if (w.mAttrs.type == TYPE_APPLICATION
2611 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002612 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002613 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002614 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002615 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002616 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002617 }
2618 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002619 return false;
2620 });
2621
2622 if (visibleWindow != null) {
2623 // We have a visible window.
2624 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002625 }
2626
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002627 // if the wallpaper service is disabled on the device, we're never going to have
2628 // wallpaper, don't bother waiting for it
2629 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2630 com.android.internal.R.bool.config_enableWallpaperService)
2631 && !mService.mOnlyCore;
2632
Wale Ogunwale494009b82016-10-21 09:01:38 -07002633 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2634 "******** booted=" + mService.mSystemBooted
2635 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002636 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2637 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2638 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002639
2640 // If we are turning on the screen to show the boot message, don't do it until the boot
2641 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002642 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002643 return true;
2644 }
2645
2646 // If we are turning on the screen after the boot is completed normally, don't do so until
2647 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002648 if (mService.mSystemBooted
2649 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002650 return true;
2651 }
2652
2653 return false;
2654 }
2655
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002656 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002657 mTmpWindowAnimator = animator;
2658 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002659 }
2660
2661 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002662 resetAnimationBackgroundAnimator();
2663
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002664 // Used to indicate a detached wallpaper.
2665 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002666 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002667
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002668 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002669
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002670 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002671 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002672 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2673 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002674 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2675 }
2676 }
2677
Wale Ogunwale494009b82016-10-21 09:01:38 -07002678 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002679 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002680 if (imFocus == null) {
2681 return false;
2682 }
2683
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002684 if (DEBUG_INPUT_METHOD) {
2685 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2686 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2687 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002688 }
2689
Wale Ogunwale494009b82016-10-21 09:01:38 -07002690 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2691
2692 if (DEBUG_INPUT_METHOD) {
2693 Slog.i(TAG_WM, "IM target client: " + imeClient);
2694 if (imeClient != null) {
2695 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2696 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2697 }
2698 }
2699
2700 return imeClient != null && imeClient.asBinder() == client.asBinder();
2701 }
2702
2703 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002704 final WindowState win = getWindow(
2705 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2706 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002707 }
2708
2709 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002710 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002711 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002712 final int curValue = w.mSystemUiVisibility;
2713 final int diff = (curValue ^ visibility) & globalDiff;
2714 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002715 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002716 w.mSeq++;
2717 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002718 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002719 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2720 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002721 visibility, newValue, diff);
2722 }
2723 } catch (RemoteException e) {
2724 // so sorry
2725 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002726 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002727 }
2728
2729 void onWindowFreezeTimeout() {
2730 Slog.w(TAG_WM, "Window freeze timeout expired.");
2731 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002732
2733 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002734 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002735 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002736 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002737 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002738 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2739 - mService.mDisplayFreezeTime);
2740 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002741 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002742 mService.mWindowPlacerLocked.performSurfacePlacement();
2743 }
2744
2745 void waitForAllWindowsDrawn() {
2746 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002747 forAllWindows(w -> {
2748 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2749 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2750 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002751 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002752 w.mLastContentInsets.set(-1, -1, -1, -1);
2753 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002754 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002755 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002756 }
2757
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002758 // TODO: Super crazy long method that should be broken down...
2759 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2760
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002761 final int dw = mDisplayInfo.logicalWidth;
2762 final int dh = mDisplayInfo.logicalHeight;
2763 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2764
2765 mTmpUpdateAllDrawn.clear();
2766
2767 int repeats = 0;
2768 do {
2769 repeats++;
2770 if (repeats > 6) {
2771 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2772 clearLayoutNeeded();
2773 break;
2774 }
2775
2776 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2777 pendingLayoutChanges);
2778
Andrii Kulian839def92016-11-02 10:58:58 -07002779 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2780 // the wallpaper window jumping across displays.
2781 // Remove check for default display when there will be support for multiple wallpaper
2782 // targets (on different displays).
2783 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002784 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002785 }
2786
2787 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2788 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2789 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2790 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002791 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002792 }
2793 }
2794
2795 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2796 setLayoutNeeded();
2797 }
2798
2799 // FIRST LOOP: Perform a layout, if needed.
2800 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2801 performLayout(repeats == 1, false /* updateInputWindows */);
2802 } else {
2803 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2804 }
2805
2806 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2807 pendingLayoutChanges = 0;
2808
2809 if (isDefaultDisplay) {
2810 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002811 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002812 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2813 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2814 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2815 }
2816 } while (pendingLayoutChanges != 0);
2817
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002818 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002819
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002820 mTmpRecoveringMemory = recoveringMemory;
2821 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002822
2823 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002824 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2825 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2826 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002827 true /* inTraversal, must call performTraversalInTrans... below */);
2828
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002829 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2830 if (wallpaperVisible != mLastWallpaperVisible) {
2831 mLastWallpaperVisible = wallpaperVisible;
2832 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2833 }
2834
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002835 while (!mTmpUpdateAllDrawn.isEmpty()) {
2836 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2837 // See if any windows have been drawn, so they (and others associated with them)
2838 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002839 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002840 }
2841
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002842 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002843 }
2844
Bryce Leef3c6a472017-11-14 14:53:06 -08002845 private void updateBounds() {
2846 calculateBounds(mTmpBounds);
2847 setBounds(mTmpBounds);
2848 }
2849
2850 // Determines the current display bounds based on the current state
2851 private void calculateBounds(Rect out) {
2852 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
2853 final int orientation = mDisplayInfo.rotation;
2854 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2855 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
2856 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
2857 int width = mDisplayInfo.logicalWidth;
2858 int left = (physWidth - width) / 2;
2859 int height = mDisplayInfo.logicalHeight;
2860 int top = (physHeight - height) / 2;
2861 out.set(left, top, left + width, top + height);
2862 }
2863
2864 @Override
2865 public void getBounds(Rect out) {
2866 calculateBounds(out);
2867 }
2868
2869 private void getBounds(Rect out, int orientation) {
2870 getBounds(out);
2871
2872 // Rotate the Rect if needed.
2873 final int currentRotation = mDisplayInfo.rotation;
2874 final int rotationDelta = deltaRotation(currentRotation, orientation);
2875 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
2876 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
2877 mTmpRectF.set(out);
2878 mTmpMatrix.mapRect(mTmpRectF);
2879 mTmpRectF.round(out);
2880 }
2881 }
2882
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002883 void performLayout(boolean initial, boolean updateInputWindows) {
2884 if (!isLayoutNeeded()) {
2885 return;
2886 }
2887 clearLayoutNeeded();
2888
2889 final int dw = mDisplayInfo.logicalWidth;
2890 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002891 if (DEBUG_LAYOUT) {
2892 Slog.v(TAG, "-------------------------------------");
2893 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2894 }
2895
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002896 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
2897 // TODO: Not sure if we really need to set the rotation here since we are updating from the
2898 // display info above...
2899 mDisplayFrames.mRotation = mRotation;
2900 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002901 if (isDefaultDisplay) {
2902 // Not needed on non-default displays.
2903 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2904 mService.mScreenRect.set(0, 0, dw, dh);
2905 }
2906
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002907 int seq = mService.mLayoutSeq + 1;
2908 if (seq < 0) seq = 0;
2909 mService.mLayoutSeq = seq;
2910
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002911 // Used to indicate that we have processed the dream window and all additional windows are
2912 // behind it.
2913 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002914 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002915
2916 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002917 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002918
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002919 // Used to indicate that we have processed the dream window and all additional attached
2920 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002921 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002922 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002923
2924 // Now perform layout of attached windows, which usually depend on the position of the
2925 // window they are attached to. XXX does not deal with windows that are attached to windows
2926 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002927 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002928
2929 // Window frames may have changed. Tell the input dispatcher about it.
2930 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2931 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2932 if (updateInputWindows) {
2933 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2934 }
2935
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002936 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2937 }
2938
2939 /**
2940 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2941 * In portrait mode, it grabs the full screenshot.
2942 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002943 * @param config of the output bitmap
2944 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
2945 */
chaviwfbe47df2017-11-10 16:14:49 -08002946 Bitmap screenshotDisplay(Bitmap.Config config, boolean wallpaperOnly) {
2947 synchronized (mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02002948 if (!mService.mPolicy.isScreenOn()) {
chaviwfbe47df2017-11-10 16:14:49 -08002949 if (DEBUG_SCREENSHOT) {
2950 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002951 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002952 return null;
2953 }
2954
chaviwfbe47df2017-11-10 16:14:49 -08002955 if (wallpaperOnly && !shouldScreenshotWallpaper()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002956 return null;
2957 }
2958
chaviwfbe47df2017-11-10 16:14:49 -08002959 int dw = mDisplayInfo.logicalWidth;
2960 int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002961
chaviwfbe47df2017-11-10 16:14:49 -08002962 if (dw <= 0 || dh <= 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002963 return null;
2964 }
2965
chaviwfbe47df2017-11-10 16:14:49 -08002966 final Rect frame = new Rect(0, 0, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002967
2968 // The screenshot API does not apply the current screen rotation.
2969 int rot = mDisplay.getRotation();
2970
2971 if (rot == ROTATION_90 || rot == ROTATION_270) {
2972 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
2973 }
2974
chaviwfbe47df2017-11-10 16:14:49 -08002975 // SurfaceFlinger is not aware of orientation, so convert our logical
2976 // crop to SurfaceFlinger's portrait orientation.
2977 convertCropForSurfaceFlinger(frame, rot, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002978
2979 final ScreenRotationAnimation screenRotationAnimation =
2980 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
2981 final boolean inRotation = screenRotationAnimation != null &&
2982 screenRotationAnimation.isAnimating();
chaviwfbe47df2017-11-10 16:14:49 -08002983 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002984
Robert Carrb1579c82017-09-05 14:54:47 -07002985 // TODO(b/68392460): We should screenshot Task controls directly
2986 // but it's difficult at the moment as the Task doesn't have the
2987 // correct size set.
chaviwfbe47df2017-11-10 16:14:49 -08002988 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, 0, 1, inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002989 if (bitmap == null) {
Robert Carrb1579c82017-09-05 14:54:47 -07002990 Slog.w(TAG_WM, "Failed to take screenshot");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002991 return null;
2992 }
chaviwfbe47df2017-11-10 16:14:49 -08002993
2994 // Create a copy of the screenshot that is immutable and backed in ashmem.
2995 // This greatly reduces the overhead of passing the bitmap between processes.
2996 final Bitmap ret = bitmap.createAshmemBitmap(config);
2997 bitmap.recycle();
2998 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002999 }
chaviwfbe47df2017-11-10 16:14:49 -08003000 }
3001
3002 private boolean shouldScreenshotWallpaper() {
3003 MutableBoolean screenshotReady = new MutableBoolean(false);
3004
3005 forAllWindows(w -> {
3006 if (!w.mIsWallpaper) {
3007 return false;
3008 }
3009
3010 // Found the wallpaper window
3011 final WindowStateAnimator winAnim = w.mWinAnimator;
3012
3013 if (winAnim.getShown() && winAnim.mLastAlpha > 0f) {
3014 screenshotReady.value = true;
3015 }
3016
3017 return true;
3018 }, true /* traverseTopToBottom */);
3019
3020 return screenshotReady.value;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003021 }
3022
3023 // TODO: Can this use createRotationMatrix()?
3024 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3025 if (rot == Surface.ROTATION_90) {
3026 final int tmp = crop.top;
3027 crop.top = dw - crop.right;
3028 crop.right = crop.bottom;
3029 crop.bottom = dw - crop.left;
3030 crop.left = tmp;
3031 } else if (rot == Surface.ROTATION_180) {
3032 int tmp = crop.top;
3033 crop.top = dh - crop.bottom;
3034 crop.bottom = dh - tmp;
3035 tmp = crop.right;
3036 crop.right = dw - crop.left;
3037 crop.left = dw - tmp;
3038 } else if (rot == Surface.ROTATION_270) {
3039 final int tmp = crop.top;
3040 crop.top = crop.left;
3041 crop.left = dh - crop.bottom;
3042 crop.bottom = crop.right;
3043 crop.right = dh - tmp;
3044 }
3045 }
3046
3047 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003048 // Used to indicate the layout is needed.
3049 mTmpWindow = null;
3050
3051 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003052 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003053 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003054 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003055 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003056 w.setDisplayLayoutNeeded();
3057 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003058 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003059
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003060 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003061 mService.mWindowPlacerLocked.performSurfacePlacement();
3062 }
3063 }
3064
Wale Ogunwale1666e312016-12-16 11:27:18 -08003065 void setExitingTokensHasVisible(boolean hasVisible) {
3066 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3067 mExitingTokens.get(i).hasVisible = hasVisible;
3068 }
3069
3070 // Initialize state of exiting applications.
3071 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3072 }
3073
3074 void removeExistingTokensIfPossible() {
3075 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3076 final WindowToken token = mExitingTokens.get(i);
3077 if (!token.hasVisible) {
3078 mExitingTokens.remove(i);
3079 }
3080 }
3081
3082 // Time to remove any exiting applications?
3083 mTaskStackContainers.removeExistingAppTokensIfPossible();
3084 }
3085
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003086 @Override
3087 void onDescendantOverrideConfigurationChanged() {
3088 setLayoutNeeded();
3089 mService.requestTraversal();
3090 }
3091
David Stevens9440dc82017-03-16 19:00:20 -07003092 boolean okToDisplay() {
3093 if (mDisplayId == DEFAULT_DISPLAY) {
3094 return !mService.mDisplayFrozen
3095 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3096 }
3097 return mDisplayInfo.state == Display.STATE_ON;
3098 }
3099
3100 boolean okToAnimate() {
3101 return okToDisplay() &&
3102 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3103 }
3104
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003105 static final class TaskForResizePointSearchResult {
3106 boolean searchDone;
3107 Task taskForResize;
3108
3109 void reset() {
3110 searchDone = false;
3111 taskForResize = null;
3112 }
3113 }
Robert Carr3b716242016-08-16 16:02:21 -07003114
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003115 private static final class ApplySurfaceChangesTransactionState {
3116 boolean displayHasContent;
3117 boolean obscured;
3118 boolean syswin;
3119 boolean focusDisplayed;
3120 float preferredRefreshRate;
3121 int preferredModeId;
3122
3123 void reset() {
3124 displayHasContent = false;
3125 obscured = false;
3126 syswin = false;
3127 focusDisplayed = false;
3128 preferredRefreshRate = 0;
3129 preferredModeId = 0;
3130 }
3131 }
3132
3133 private static final class ScreenshotApplicationState {
3134 WindowState appWin;
3135 int maxLayer;
3136 int minLayer;
3137 boolean screenshotReady;
3138
3139 void reset(boolean screenshotReady) {
3140 appWin = null;
3141 maxLayer = 0;
3142 minLayer = 0;
3143 this.screenshotReady = screenshotReady;
3144 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3145 }
3146 }
3147
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003148 /**
3149 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3150 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3151 * homogeneous children type which is currently required by sub-classes of
3152 * {@link WindowContainer} class.
3153 */
3154 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3155
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003156 DisplayChildWindowContainer(WindowManagerService service) {
3157 super(service);
3158 }
3159
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003160 @Override
3161 boolean fillsParent() {
3162 return true;
3163 }
3164
3165 @Override
3166 boolean isVisible() {
3167 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003168 }
3169 }
3170
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003171 /**
3172 * Window container class that contains all containers on this display relating to Apps.
3173 * I.e Activities.
3174 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003175 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003176 /**
3177 * A control placed at the appropriate level for transitions to occur.
3178 */
chaviw23ee71c2017-12-18 11:29:41 -08003179 SurfaceControl mAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003180
Wale Ogunwale61911492017-10-11 08:50:50 -07003181 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3182 // through the list to find them.
3183 private TaskStack mHomeStack = null;
3184 private TaskStack mPinnedStack = null;
3185 private TaskStack mSplitScreenPrimaryStack = null;
3186
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003187 TaskStackContainers(WindowManagerService service) {
3188 super(service);
3189 }
3190
Wale Ogunwale61911492017-10-11 08:50:50 -07003191 /**
3192 * Returns the topmost stack on the display that is compatible with the input windowing mode
3193 * and activity type. Null is no compatible stack on the display.
3194 */
3195 TaskStack getStack(int windowingMode, int activityType) {
3196 if (activityType == ACTIVITY_TYPE_HOME) {
3197 return mHomeStack;
3198 }
3199 if (windowingMode == WINDOWING_MODE_PINNED) {
3200 return mPinnedStack;
3201 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3202 return mSplitScreenPrimaryStack;
3203 }
3204 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3205 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003206 if (activityType == ACTIVITY_TYPE_UNDEFINED
3207 && windowingMode == stack.getWindowingMode()) {
3208 // Passing in undefined type means we want to match the topmost stack with the
3209 // windowing mode.
3210 return stack;
3211 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003212 if (stack.isCompatible(windowingMode, activityType)) {
3213 return stack;
3214 }
3215 }
3216 return null;
3217 }
3218
3219 @VisibleForTesting
3220 TaskStack getTopStack() {
3221 return mTaskStackContainers.getChildCount() > 0
3222 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3223 }
3224
3225 TaskStack getHomeStack() {
3226 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3227 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3228 }
3229 return mHomeStack;
3230 }
3231
3232 TaskStack getPinnedStack() {
3233 return mPinnedStack;
3234 }
3235
Matthew Ng64e77cf2017-10-31 14:01:31 -07003236 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003237 return mSplitScreenPrimaryStack;
3238 }
3239
Andrii Kulian839def92016-11-02 10:58:58 -07003240 /**
3241 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003242 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003243 */
3244 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003245 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003246 addChild(stack, onTop);
3247 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003248 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003249
Wale Ogunwale61911492017-10-11 08:50:50 -07003250 void onStackWindowingModeChanged(TaskStack stack) {
3251 removeStackReferenceIfNeeded(stack);
3252 addStackReferenceIfNeeded(stack);
3253 if (stack == mPinnedStack && getTopStack() != stack) {
3254 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3255 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3256 }
3257 }
3258
3259 private void addStackReferenceIfNeeded(TaskStack stack) {
3260 if (stack.isActivityTypeHome()) {
3261 if (mHomeStack != null) {
3262 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
3263 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
3264 }
3265 mHomeStack = stack;
3266 }
3267 final int windowingMode = stack.getWindowingMode();
3268 if (windowingMode == WINDOWING_MODE_PINNED) {
3269 if (mPinnedStack != null) {
3270 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3271 + mPinnedStack + " already exist on display=" + this
3272 + " stack=" + stack);
3273 }
3274 mPinnedStack = stack;
3275 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3276 if (mSplitScreenPrimaryStack != null) {
3277 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3278 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3279 + " already exist on display=" + this + " stack=" + stack);
3280 }
3281 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003282 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003283 }
3284 }
3285
3286 private void removeStackReferenceIfNeeded(TaskStack stack) {
3287 if (stack == mHomeStack) {
3288 mHomeStack = null;
3289 } else if (stack == mPinnedStack) {
3290 mPinnedStack = null;
3291 } else if (stack == mSplitScreenPrimaryStack) {
3292 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003293 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3294 mService.setDockedStackCreateStateLocked(
3295 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3296 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003297 }
Andrii Kulian839def92016-11-02 10:58:58 -07003298 }
3299
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003300 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003301 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3302 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003303 addChild(stack, addIndex);
3304 setLayoutNeeded();
3305 }
3306
Wale Ogunwale61911492017-10-11 08:50:50 -07003307 @Override
3308 protected void removeChild(TaskStack stack) {
3309 super.removeChild(stack);
3310 removeStackReferenceIfNeeded(stack);
3311 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003312
3313 @Override
3314 boolean isOnTop() {
3315 // Considered always on top
3316 return true;
3317 }
3318
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003319 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003320 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003321 if (child.getWindowConfiguration().isAlwaysOnTop()
3322 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003323 // This stack is always-on-top, override the default behavior.
3324 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3325
3326 // Moving to its current position, as we must call super but we don't want to
3327 // perform any meaningful action.
3328 final int currentPosition = mChildren.indexOf(child);
3329 super.positionChildAt(currentPosition, child, false /* includingParents */);
3330 return;
3331 }
3332
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003333 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3334 super.positionChildAt(targetPosition, child, includingParents);
3335
3336 setLayoutNeeded();
3337 }
3338
3339 /**
3340 * When stack is added or repositioned, find a proper position for it.
3341 * This will make sure that pinned stack always stays on top.
3342 * @param requestedPosition Position requested by caller.
3343 * @param stack Stack to be added or positioned.
3344 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3345 * @return The proper position for the stack.
3346 */
3347 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3348 final int topChildPosition = mChildren.size() - 1;
3349 boolean toTop = requestedPosition == POSITION_TOP;
3350 toTop |= adding ? requestedPosition >= topChildPosition + 1
3351 : requestedPosition >= topChildPosition;
3352 int targetPosition = requestedPosition;
3353
Wale Ogunwale61911492017-10-11 08:50:50 -07003354 if (toTop && stack.getWindowingMode() != WINDOWING_MODE_PINNED && hasPinnedStack()) {
Bryce Lee48f4b572017-04-10 10:54:15 -07003355 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003356 TaskStack topStack = mChildren.get(topChildPosition);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003357 if (topStack.getWindowingMode() != WINDOWING_MODE_PINNED) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003358 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3359 }
3360
3361 // So, stack is moved just below the pinned stack.
3362 // When we're adding a new stack the target is the current pinned stack position.
3363 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003364 // stack, because WindowContainer#positionAt() first removes element and then adds
3365 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003366 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3367 }
3368
3369 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003370 }
3371
3372 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003373 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3374 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003375 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003376 if (super.forAllWindows(callback, traverseTopToBottom)) {
3377 return true;
3378 }
3379 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3380 return true;
3381 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003382 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003383 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3384 return true;
3385 }
3386 if (super.forAllWindows(callback, traverseTopToBottom)) {
3387 return true;
3388 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003389 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003390 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003391 }
3392
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003393 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003394 boolean traverseTopToBottom) {
3395 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3396 // app tokens.
3397 // TODO: Investigate if we need to continue to do this or if we can just process them
3398 // in-order.
3399 if (traverseTopToBottom) {
3400 for (int i = mChildren.size() - 1; i >= 0; --i) {
3401 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3402 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003403 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3404 traverseTopToBottom)) {
3405 return true;
3406 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003407 }
3408 }
3409 } else {
3410 final int count = mChildren.size();
3411 for (int i = 0; i < count; ++i) {
3412 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3413 final int appTokensCount = appTokens.size();
3414 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003415 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3416 traverseTopToBottom)) {
3417 return true;
3418 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003419 }
3420 }
3421 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003422 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003423 }
3424
Wale Ogunwale1666e312016-12-16 11:27:18 -08003425 void setExitingTokensHasVisible(boolean hasVisible) {
3426 for (int i = mChildren.size() - 1; i >= 0; --i) {
3427 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3428 for (int j = appTokens.size() - 1; j >= 0; --j) {
3429 appTokens.get(j).hasVisible = hasVisible;
3430 }
3431 }
3432 }
3433
3434 void removeExistingAppTokensIfPossible() {
3435 for (int i = mChildren.size() - 1; i >= 0; --i) {
3436 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3437 for (int j = appTokens.size() - 1; j >= 0; --j) {
3438 final AppWindowToken token = appTokens.get(j);
3439 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3440 && (!token.mIsExiting || token.isEmpty())) {
3441 // Make sure there is no animation running on this token, so any windows
3442 // associated with it will be removed as soon as their animations are
3443 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003444 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003445 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3446 "performLayout: App token exiting now removed" + token);
3447 token.removeIfPossible();
3448 }
3449 }
3450 }
3451 }
3452
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003453 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003454 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003455 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3456 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003457 // Apps and their containers are not allowed to specify an orientation while the
3458 // docked or freeform stack is visible...except for the home stack/task if the
3459 // docked stack is minimized and it actually set something.
3460 if (mHomeStack != null && mHomeStack.isVisible()
3461 && mDividerControllerLocked.isMinimizedDock()) {
3462 final int orientation = mHomeStack.getOrientation();
3463 if (orientation != SCREEN_ORIENTATION_UNSET) {
3464 return orientation;
3465 }
3466 }
3467 return SCREEN_ORIENTATION_UNSPECIFIED;
3468 }
3469
3470 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003471 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3472 PackageManager.FEATURE_AUTOMOTIVE);
3473 if (isCar) {
3474 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3475 // allow anything but the default orientation.
3476 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3477 "Forcing UNSPECIFIED orientation in car. Ignoring " + orientation);
3478 return SCREEN_ORIENTATION_UNSPECIFIED;
3479 }
3480
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003481 if (orientation != SCREEN_ORIENTATION_UNSET
3482 && orientation != SCREEN_ORIENTATION_BEHIND) {
3483 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3484 "App is requesting an orientation, return " + orientation);
3485 return orientation;
3486 }
3487
3488 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003489 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003490 // The next app has not been requested to be visible, so we keep the current orientation
3491 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003492 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003493 }
Robert Carrb1579c82017-09-05 14:54:47 -07003494
3495 @Override
3496 void assignChildLayers(SurfaceControl.Transaction t) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003497 int layer = 0;
Robert Carrb1579c82017-09-05 14:54:47 -07003498
3499 // We allow stacks to change visual order from the AM specified order due to
3500 // Z-boosting during animations. However we must take care to ensure TaskStacks
3501 // which are marked as alwaysOnTop remain that way.
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003502 for (int i = 0; i < mChildren.size(); i++) {
3503 final TaskStack s = mChildren.get(i);
3504 s.assignChildLayers();
3505 if (!s.needsZBoost() && !s.isAlwaysOnTop()) {
3506 s.assignLayer(t, layer++);
Robert Carrb1579c82017-09-05 14:54:47 -07003507 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003508 }
3509 for (int i = 0; i < mChildren.size(); i++) {
3510 final TaskStack s = mChildren.get(i);
3511 if (s.needsZBoost() && !s.isAlwaysOnTop()) {
3512 s.assignLayer(t, layer++);
Robert Carrb1579c82017-09-05 14:54:47 -07003513 }
3514 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003515 for (int i = 0; i < mChildren.size(); i++) {
3516 final TaskStack s = mChildren.get(i);
3517 if (s.isAlwaysOnTop()) {
3518 s.assignLayer(t, layer++);
3519 }
3520 }
3521
3522 // The appropriate place for App-Transitions to occur is right
3523 // above all other animations but still below things in the Picture-and-Picture
3524 // windowing mode.
chaviw23ee71c2017-12-18 11:29:41 -08003525 if (mAppAnimationLayer != null) {
3526 t.setLayer(mAppAnimationLayer, layer++);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003527 }
Robert Carrb1579c82017-09-05 14:54:47 -07003528 }
3529
3530 @Override
chaviw23ee71c2017-12-18 11:29:41 -08003531 SurfaceControl getAppAnimationLayer() {
3532 return mAppAnimationLayer;
3533 }
3534
3535 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07003536 void onParentSet() {
3537 super.onParentSet();
3538 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08003539 mAppAnimationLayer = makeChildSurface(null)
3540 .setName("animationLayer")
3541 .build();
3542 getPendingTransaction().show(mAppAnimationLayer);
3543 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07003544 } else {
chaviw23ee71c2017-12-18 11:29:41 -08003545 mAppAnimationLayer.destroy();
3546 mAppAnimationLayer = null;
Robert Carrb1579c82017-09-05 14:54:47 -07003547 }
3548 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003549 }
3550
Robert Carree4d4b92017-11-22 12:21:46 -08003551 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003552 AboveAppWindowContainers(String name, WindowManagerService service) {
3553 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08003554 }
3555
3556 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
3557 boolean needAssignIme = imeContainer != null
3558 && imeContainer.getSurfaceControl() != null;
3559 for (int j = 0; j < mChildren.size(); ++j) {
3560 final WindowToken wt = mChildren.get(j);
3561 wt.assignLayer(t, j);
3562 wt.assignChildLayers(t);
3563
3564 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
3565 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08003566
3567 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
3568 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003569 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08003570 needAssignIme = false;
3571 }
3572 }
3573 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003574 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08003575 }
3576 }
3577 }
3578
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003579 /**
3580 * Window container class that contains all containers on this display that are not related to
3581 * Apps. E.g. status bar.
3582 */
Robert Carree4d4b92017-11-22 12:21:46 -08003583 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07003584 /**
3585 * Compares two child window tokens returns -1 if the first is lesser than the second in
3586 * terms of z-order and 1 otherwise.
3587 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003588 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003589 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003590 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3591 token1.mOwnerCanManageAppTokens)
3592 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3593 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003594
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003595 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3596 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3597 return false;
3598 }
3599 final int req = w.mAttrs.screenOrientation;
3600 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3601 || req == SCREEN_ORIENTATION_UNSET) {
3602 return false;
3603 }
3604 return true;
3605 };
3606
Wale Ogunwale3a931692016-11-02 16:49:48 -07003607 private final String mName;
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003608 NonAppWindowContainers(String name, WindowManagerService service) {
3609 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003610 mName = name;
3611 }
3612
3613 void addChild(WindowToken token) {
3614 addChild(token, mWindowComparator);
3615 }
3616
3617 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003618 int getOrientation() {
3619 final WindowManagerPolicy policy = mService.mPolicy;
3620 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003621 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003622
3623 if (win != null) {
3624 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003625 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003626 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003627 if (mService.mKeyguardGoingAway) {
3628 // Keyguard can't affect the orientation if it is going away...
3629 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3630 return SCREEN_ORIENTATION_UNSET;
3631 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003632 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003633 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003634 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003635 }
3636
Andrii Kulian8ee72852017-03-10 10:36:45 -08003637 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003638
Jorim Jaggi75520d52017-08-24 17:23:19 +02003639 if (policy.isKeyguardShowingAndNotOccluded()
3640 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003641 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003642 }
3643
3644 return SCREEN_ORIENTATION_UNSET;
3645 }
3646
3647 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003648 String getName() {
3649 return mName;
3650 }
Robert Carr3b716242016-08-16 16:02:21 -07003651 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003652
Robert Carr9034d962018-01-04 18:27:42 -08003653 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
3654 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
3655 super(name, service);
3656 }
3657
3658 @Override
3659 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
3660 }
3661 };
3662
Robert Carrb1579c82017-09-05 14:54:47 -07003663 SurfaceControl.Builder makeSurface(SurfaceSession s) {
3664 return mService.makeSurfaceBuilder(s)
3665 .setParent(mWindowingLayer);
3666 }
3667
3668 @Override
3669 SurfaceSession getSession() {
3670 return mSession;
3671 }
3672
3673 @Override
3674 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003675 SurfaceSession s = child != null ? child.getSession() : getSession();
3676 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07003677 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07003678
3679 if (child == null) {
3680 return b;
3681 }
3682
Robert Carree4d4b92017-11-22 12:21:46 -08003683 return b.setName(child.getName())
3684 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07003685 }
3686
3687 /**
3688 * The makeSurface variants are for use by the window-container
3689 * hierarchy. makeOverlay here is a function for various non windowing
3690 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
3691 * and other potpourii.
3692 */
3693 SurfaceControl.Builder makeOverlay() {
3694 return mService.makeSurfaceBuilder(mSession)
3695 .setParent(mOverlayLayer);
3696 }
3697
3698 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003699 applyMagnificationSpec(getPendingTransaction(), spec);
3700 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07003701 }
3702
3703 @Override
3704 void onParentSet() {
3705 // Since we are the top of the SurfaceControl hierarchy here
3706 // we create the root surfaces explicitly rather than chaining
3707 // up as the default implementation in onParentSet does. So we
3708 // explicitly do NOT call super here.
3709 }
3710
3711 @Override
3712 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07003713
3714 // These are layers as children of "mWindowingLayer"
3715 mBelowAppWindowsContainers.assignLayer(t, 0);
3716 mTaskStackContainers.assignLayer(t, 1);
3717 mAboveAppWindowsContainers.assignLayer(t, 2);
3718
3719 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08003720 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08003721
Robert Carree4d4b92017-11-22 12:21:46 -08003722 // In the case where we have an IME target that is not in split-screen
3723 // mode IME assignment is easy. We just need the IME to go directly above
3724 // the target. This way children of the target will naturally go above the IME
3725 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08003726 //
Robert Carree4d4b92017-11-22 12:21:46 -08003727 // In the case of split-screen windowing mode, we need to elevate the IME above the
3728 // docked divider while keeping the app itself below the docked divider, so instead
3729 // we use relative layering of the IME targets child windows, and place the
3730 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08003731 //
Robert Carree4d4b92017-11-22 12:21:46 -08003732 // In the case where we have no IME target we assign it where it's base layer would
3733 // place it in the AboveAppWindowContainers.
3734 if (imeTarget != null && !imeTarget.inSplitScreenWindowingMode()
3735 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003736 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07003737 // TODO: We need to use an extra level on the app surface to ensure
3738 // this is always above SurfaceView but always below attached window.
3739 1);
Robert Carree4d4b92017-11-22 12:21:46 -08003740 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07003741 }
3742
3743 // Above we have assigned layers to our children, now we ask them to assign
3744 // layers to their children.
3745 mBelowAppWindowsContainers.assignChildLayers(t);
3746 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08003747 mAboveAppWindowsContainers.assignChildLayers(t,
3748 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07003749 mImeWindowsContainers.assignChildLayers(t);
3750 }
3751
3752 /**
3753 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
3754 * that the IME target is one of the docked applications. We'd like the docked divider to be
3755 * above both of the applications, and we'd like the IME to be above the docked divider.
3756 * However we need child windows of the applications to be above the IME (Text drag handles).
3757 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
3758 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
3759 * with {@link #WindowState#assignLayer}
3760 */
3761 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
3762 t.setRelativeLayer(child.getSurfaceControl(), mImeWindowsContainers.getSurfaceControl(), 1);
3763 }
3764
3765 @Override
Jorim Jaggia5e10572017-11-15 14:36:26 +01003766 public void destroyAfterPendingTransaction(SurfaceControl surface) {
Robert Carrb1579c82017-09-05 14:54:47 -07003767 mPendingDestroyingSurfaces.add(surface);
3768 }
3769
3770 /**
3771 * Destroys any surfaces that have been put into the pending list with
Jorim Jaggia5e10572017-11-15 14:36:26 +01003772 * {@link #destroyAfterPendingTransaction}.
Robert Carrb1579c82017-09-05 14:54:47 -07003773 */
3774 void onPendingTransactionApplied() {
3775 for (int i = mPendingDestroyingSurfaces.size() - 1; i >= 0; i--) {
3776 mPendingDestroyingSurfaces.get(i).destroy();
3777 }
3778 mPendingDestroyingSurfaces.clear();
3779 }
Jorim Jaggi9af095b2017-12-12 17:18:57 +01003780
3781 @Override
3782 void prepareSurfaces() {
3783 final ScreenRotationAnimation screenRotationAnimation =
3784 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
3785 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
3786 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
3787 mPendingTransaction.setMatrix(mWindowingLayer,
3788 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
3789 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
3790 mPendingTransaction.setPosition(mWindowingLayer,
3791 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
3792 mPendingTransaction.setAlpha(mWindowingLayer,
3793 screenRotationAnimation.getEnterTransformation().getAlpha());
3794 }
3795 super.prepareSurfaces();
3796 }
Craig Mautner59c00972012-07-30 12:10:24 -07003797}