blob: 35a65a65c3a93e226549b53ad1a398b8988b1fb1 [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;
Robert Carr11408822018-01-03 18:07:51 -080052import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
Wale Ogunwale494009b82016-10-21 09:01:38 -070053import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070054import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070055import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
56import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070057import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070058import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
59import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070060import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070061import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Adrian Roose99bc052017-11-20 17:55:31 +010062import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
63import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
64import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
65import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Jorim Jaggi75520d52017-08-24 17:23:19 +020066import static com.android.server.wm.AppTransition.TRANSIT_KEYGUARD_UNOCCLUDE;
Wale Ogunwale1666e312016-12-16 11:27:18 -080067import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070069import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070070import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
71import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070076import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070077import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070078import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080079import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070080import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070081import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070082import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070083import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070084import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070085import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080086import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -070087import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070088import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
89import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070090import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070091import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -070092import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
93import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070094import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -070095import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070096import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070097import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070098import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070099import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700100import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700101import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700102import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700103import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700104import static com.android.server.wm.proto.DisplayProto.ABOVE_APP_WINDOWS;
105import static com.android.server.wm.proto.DisplayProto.BELOW_APP_WINDOWS;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000106import static com.android.server.wm.proto.DisplayProto.DISPLAY_FRAMES;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700107import static com.android.server.wm.proto.DisplayProto.DISPLAY_INFO;
108import static com.android.server.wm.proto.DisplayProto.DOCKED_STACK_DIVIDER_CONTROLLER;
109import static com.android.server.wm.proto.DisplayProto.DPI;
110import static com.android.server.wm.proto.DisplayProto.ID;
111import static com.android.server.wm.proto.DisplayProto.IME_WINDOWS;
112import static com.android.server.wm.proto.DisplayProto.PINNED_STACK_CONTROLLER;
Steven Timotiusf2d68892017-08-28 17:00:01 -0700113import static com.android.server.wm.proto.DisplayProto.ROTATION;
114import static com.android.server.wm.proto.DisplayProto.SCREEN_ROTATION_ANIMATION;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700115import static com.android.server.wm.proto.DisplayProto.STACKS;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700116import static com.android.server.wm.proto.DisplayProto.WINDOW_CONTAINER;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700117
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700118import android.annotation.CallSuper;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700119import android.annotation.NonNull;
Dean Harding3e5a1522017-10-06 09:19:01 -0700120import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700121import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700122import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700123import android.graphics.Bitmap;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700124import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800125import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700126import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700127import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100128import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700129import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700130import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700131import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700132import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700133import android.os.RemoteException;
134import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100135import android.os.Trace;
Chong Zhang8e89b312015-09-09 15:09:30 -0700136import android.util.DisplayMetrics;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800137import android.util.MutableBoolean;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700138import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700139import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700140import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700141import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700142import android.view.InputDevice;
Robert Carrb1579c82017-09-05 14:54:47 -0700143import android.view.MagnificationSpec;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700144import android.view.Surface;
145import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100146import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700147import android.view.SurfaceSession;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700148
Bryce Lee48f4b572017-04-10 10:54:15 -0700149import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800150import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700151import com.android.internal.view.IInputMethodClient;
Adrian Roose99bc052017-11-20 17:55:31 +0100152import com.android.server.policy.WindowManagerPolicy;
Craig Mautner59c00972012-07-30 12:10:24 -0700153
Robert Carr3b716242016-08-16 16:02:21 -0700154import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700155import java.io.PrintWriter;
156import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700157import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700158import java.util.HashMap;
159import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700160import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700161import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800162import java.util.function.Consumer;
163import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700164
Craig Mautner59c00972012-07-30 12:10:24 -0700165/**
166 * Utility class for keeping track of the WindowStates and other pertinent contents of a
167 * particular Display.
168 *
169 * IMPORTANT: No method from this class should ever be used without holding
170 * WindowManagerService.mWindowMap.
171 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700172class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700173 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700174
175 /** Unique identifier of this stack. */
176 private final int mDisplayId;
177
Wale Ogunwale3a931692016-11-02 16:49:48 -0700178 /** The containers below are the only child containers the display can have. */
179 // Contains all window containers that are related to apps (Activities)
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100180 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700181 // Contains all non-app window containers that should be displayed above the app containers
182 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800183 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100184 new AboveAppWindowContainers("mAboveAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700185 // Contains all non-app window containers that should be displayed below the app containers
186 // (e.g. Wallpaper).
187 private final NonAppWindowContainers mBelowAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100188 new NonAppWindowContainers("mBelowAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700189 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
190 // 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 -0800191 // window containers together and move them in-sync if/when needed. We use a subclass of
192 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
193 private final NonMagnifiableWindowContainers mImeWindowsContainers =
194 new NonMagnifiableWindowContainers("mImeWindowsContainers", mService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700195
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000196 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800197 private WindowState mTmpWindow2;
198 private WindowAnimator mTmpWindowAnimator;
199 private boolean mTmpRecoveringMemory;
200 private boolean mUpdateImeTarget;
201 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700202 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700203
Wale Ogunwale02319a62016-09-26 15:21:22 -0700204 // Mapping from a token IBinder to a WindowToken object on this display.
205 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
206
Andrii Kuliancd097992017-03-23 18:31:59 -0700207 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700208 int mInitialDisplayWidth = 0;
209 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700210 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700211
212 /**
213 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
214 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
215 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
216 */
Craig Mautner59c00972012-07-30 12:10:24 -0700217 int mBaseDisplayWidth = 0;
218 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700219 /**
220 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
221 * but can be set from Settings or via shell command "adb shell wm density".
222 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
223 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700224 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700225 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700226 private final DisplayInfo mDisplayInfo = new DisplayInfo();
227 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700228 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000229 DisplayFrames mDisplayFrames;
230
Andrii Kulian06d07d62017-03-14 11:11:47 -0700231 /**
232 * For default display it contains real metrics, empty for others.
233 * @see WindowManagerService#createWatermarkInTransaction()
234 */
235 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
236 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
237 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700238
Andrii Kulian06d07d62017-03-14 11:11:47 -0700239 /**
240 * Compat metrics computed based on {@link #mDisplayMetrics}.
241 * @see #updateDisplayAndOrientation(int)
242 */
243 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
244
245 /** The desired scaling factor for compatible apps. */
246 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700247
Andrii Kulian8ee72852017-03-10 10:36:45 -0800248 /**
249 * Current rotation of the display.
250 * Constants as per {@link android.view.Surface.Rotation}.
251 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700252 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800253 */
254 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700255
Andrii Kulian8ee72852017-03-10 10:36:45 -0800256 /**
257 * Last applied orientation of the display.
258 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
259 *
260 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
261 */
262 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700263
Andrii Kulian8ee72852017-03-10 10:36:45 -0800264 /**
265 * Flag indicating that the application is receiving an orientation that has different metrics
266 * than it expected. E.g. Portrait instead of Landscape.
267 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700268 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800269 */
270 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700271
Andrii Kulian8ee72852017-03-10 10:36:45 -0800272 /**
273 * Orientation forced by some window. If there is no visible window that specifies orientation
274 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
275 *
276 * @see NonAppWindowContainers#getOrientation()
277 */
278 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700279
Andrii Kulian8ee72852017-03-10 10:36:45 -0800280 /**
281 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
282 * occluded.
283 *
284 * @see NonAppWindowContainers#getOrientation()
285 */
286 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
287
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700288 /**
289 * Keep track of wallpaper visibility to notify changes.
290 */
291 private boolean mLastWallpaperVisible = false;
292
Andrii Kulian06d07d62017-03-14 11:11:47 -0700293 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700294
Craig Mautner39834192012-09-02 07:47:24 -0700295 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700296 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700297 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700298 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800299 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700300
Craig Mautnerdc548482014-02-05 13:35:24 -0800301 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700302 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800303
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700304 /** Detect user tapping outside of current focused task bounds .*/
305 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700306
Craig Mautner6601b7b2013-04-29 10:29:11 -0700307 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700308 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700309
Craig Mautner6601b7b2013-04-29 10:29:11 -0700310 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800311 private final Rect mTmpRect = new Rect();
312 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700313 private final RectF mTmpRectF = new RectF();
314 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800315 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700316
Bryce Leef3c6a472017-11-14 14:53:06 -0800317 /** Used for handing back size of display */
318 private final Rect mTmpBounds = new Rect();
319
Craig Mautner95da1082014-02-24 17:54:35 -0800320 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700321 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800322
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700323 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700324 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700325
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800326 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
327
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000328 private boolean mHaveBootMsg = false;
329 private boolean mHaveApp = false;
330 private boolean mHaveWallpaper = false;
331 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700332
333 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
334
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700335 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
336 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000337 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
338 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700339
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700340 // True if this display is in the process of being removed. Used to determine if the removal of
341 // the display's direct children should be allowed.
342 private boolean mRemovingDisplay = false;
343
Bryce Leed1871262017-06-12 14:12:29 -0700344 // {@code false} if this display is in the processing of being created.
345 private boolean mDisplayReady = false;
346
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800347 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700348
Robert Carrb1579c82017-09-05 14:54:47 -0700349 private final SurfaceSession mSession = new SurfaceSession();
350
351 /**
352 * We organize all top-level Surfaces in to the following layers.
353 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800354 * and omitted from Screen-Magnification, for example the strict mode flash or
355 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700356 * {@link #mWindowingLayer} contains everything else.
357 */
358 private SurfaceControl mOverlayLayer;
359
360 /**
361 * See {@link #mOverlayLayer}
362 */
363 private SurfaceControl mWindowingLayer;
364
365 /**
366 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
367 * <p>
368 * For these surfaces currently we use a surface based on the larger of width or height so we
369 * don't have to resize when rotating the display.
370 */
371 private int mSurfaceSize;
372
373 /**
374 * A list of surfaces to be destroyed after {@link #mPendingTransaction} is applied.
375 */
376 private final ArrayList<SurfaceControl> mPendingDestroyingSurfaces = new ArrayList<>();
377
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100378 /** Temporary float array to retrieve 3x3 matrix values. */
379 private final float[] mTmpFloats = new float[9];
380
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800381 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
382 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800383 final AppWindowToken atoken = w.mAppToken;
384 if (winAnimator.mDrawState == READY_TO_SHOW) {
385 if (atoken == null || atoken.allDrawn) {
386 if (w.performShowLocked()) {
387 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
388 if (DEBUG_LAYOUT_REPEATS) {
389 mService.mWindowPlacerLocked.debugLayoutRepeats(
390 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
391 }
392 }
393 }
394 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800395 };
396
397 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
398 final WindowStateAnimator winAnimator = w.mWinAnimator;
399 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
400 return;
401 }
402
403 final int flags = w.mAttrs.flags;
404
405 // If this window is animating, make a note that we have an animating window and take
406 // care of a request to run a detached wallpaper animation.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100407 if (winAnimator.isAnimationSet()) {
408 final AnimationAdapter anim = w.getAnimation();
409 if (anim != null) {
410 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && anim.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800411 mTmpWindow = w;
412 }
Jorim Jaggia5e10572017-11-15 14:36:26 +0100413 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800414 if (color != 0) {
415 final TaskStack stack = w.getStack();
416 if (stack != null) {
417 stack.setAnimationBackground(winAnimator, color);
418 }
419 }
420 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800421 }
422
423 // If this window's app token is running a detached wallpaper animation, make a note so
424 // we can ensure the wallpaper is displayed behind it.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200425 final AppWindowToken atoken = winAnimator.mWin.mAppToken;
426 final AnimationAdapter animation = atoken != null ? atoken.getAnimation() : null;
427 if (animation != null) {
428 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && animation.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800429 mTmpWindow = w;
430 }
431
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200432 final int color = animation.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800433 if (color != 0) {
434 final TaskStack stack = w.getStack();
435 if (stack != null) {
436 stack.setAnimationBackground(winAnimator, color);
437 }
438 }
439 }
440 };
441
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800442 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
443 final int lostFocusUid = mTmpWindow.mOwnerUid;
444 final Handler handler = mService.mH;
445 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
446 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
447 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
448 w.mAttrs.hideTimeoutMilliseconds);
449 }
450 }
451 };
452
453 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
454 final AppWindowToken focusedApp = mService.mFocusedApp;
455 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
456 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
457
458 if (!w.canReceiveKeys()) {
459 return false;
460 }
461
462 final AppWindowToken wtoken = w.mAppToken;
463
464 // If this window's application has been removed, just skip it.
465 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
466 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
467 + (wtoken.removed ? "removed" : "sendingToBottom"));
468 return false;
469 }
470
471 if (focusedApp == null) {
472 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
473 + " using new focus @ " + w);
474 mTmpWindow = w;
475 return true;
476 }
477
478 if (!focusedApp.windowsAreFocusable()) {
479 // Current focused app windows aren't focusable...
480 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
481 + " focusable using new focus @ " + w);
482 mTmpWindow = w;
483 return true;
484 }
485
486 // Descend through all of the app tokens and find the first that either matches
487 // win.mAppToken (return win) or mFocusedApp (return null).
488 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
489 if (focusedApp.compareTo(wtoken) > 0) {
490 // App stack below focused app stack. No focus for you!!!
491 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
492 "findFocusedWindow: Reached focused app=" + focusedApp);
493 mTmpWindow = null;
494 return true;
495 }
496 }
497
498 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
499 mTmpWindow = w;
500 return true;
501 };
502
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800503 private final Consumer<WindowState> mPerformLayout = w -> {
504 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
505 // wasting time and funky changes while a window is animating away.
506 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
507 || w.isGoneForLayoutLw();
508
509 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
510 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
511 + " mLayoutAttached=" + w.mLayoutAttached
512 + " screen changed=" + w.isConfigChanged());
513 final AppWindowToken atoken = w.mAppToken;
514 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200515 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800516 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
517 + " parentHidden=" + w.isParentWindowHidden());
518 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200519 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800520 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
521 + " parentHidden=" + w.isParentWindowHidden());
522 }
523
524 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
525 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
526 // since that means "perform layout as normal, just don't display").
527 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
528 || ((w.isConfigChanged() || w.setReportResizeHints())
529 && !w.isGoneForLayoutLw() &&
530 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
531 (w.mHasSurface && w.mAppToken != null &&
532 w.mAppToken.layoutConfigChanges)))) {
533 if (!w.mLayoutAttached) {
534 if (mTmpInitial) {
535 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
536 w.mContentChanged = false;
537 }
538 if (w.mAttrs.type == TYPE_DREAM) {
539 // Don't layout windows behind a dream, so that if it does stuff like hide
540 // the status bar we won't get a bad transition when it goes away.
541 mTmpWindow = w;
542 }
543 w.mLayoutNeeded = false;
544 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200545 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000546 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800547 w.mLayoutSeq = mService.mLayoutSeq;
548
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200549 // If this is the first layout, we need to initialize the last inset values as
550 // otherwise we'd immediately cause an unnecessary resize.
551 if (firstLayout) {
552 w.updateLastInsetValues();
553 }
554
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800555 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
556 + " mContainingFrame=" + w.mContainingFrame
557 + " mDisplayFrame=" + w.mDisplayFrame);
558 }
559 }
560 };
561
562 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
563 if (w.mLayoutAttached) {
564 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
565 + " mViewVisibility=" + w.mViewVisibility
566 + " mRelayoutCalled=" + w.mRelayoutCalled);
567 // If this view is GONE, then skip it -- keep the current frame, and let the caller
568 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
569 // windows, since that means "perform layout as normal, just don't display").
570 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
571 return;
572 }
573 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
574 || w.mLayoutNeeded) {
575 if (mTmpInitial) {
576 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
577 w.mContentChanged = false;
578 }
579 w.mLayoutNeeded = false;
580 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000581 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800582 w.mLayoutSeq = mService.mLayoutSeq;
583 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
584 + " mContainingFrame=" + w.mContainingFrame
585 + " mDisplayFrame=" + w.mDisplayFrame);
586 }
587 } else if (w.mAttrs.type == TYPE_DREAM) {
588 // Don't layout windows behind a dream, so that if it does stuff like hide the
589 // status bar we won't get a bad transition when it goes away.
590 mTmpWindow = mTmpWindow2;
591 }
592 };
593
594 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
595 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
596 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
597 return w.canBeImeTarget();
598 };
599
600 private final Consumer<WindowState> mApplyPostLayoutPolicy =
601 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
602 mService.mInputMethodTarget);
603
604 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
605 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
606 final boolean obscuredChanged = w.mObscured !=
607 mTmpApplySurfaceChangesTransactionState.obscured;
608 final RootWindowContainer root = mService.mRoot;
609 // Only used if default window
610 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
611
612 // Update effect.
613 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
614 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
615 final boolean isDisplayed = w.isDisplayedLw();
616
617 if (isDisplayed && w.isObscuringDisplay()) {
618 // This window completely covers everything behind it, so we want to leave all
619 // of them as undimmed (for performance reasons).
620 root.mObscuringWindow = w;
621 mTmpApplySurfaceChangesTransactionState.obscured = true;
622 }
623
624 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
625 root.handleNotObscuredLocked(w,
626 mTmpApplySurfaceChangesTransactionState.obscured,
627 mTmpApplySurfaceChangesTransactionState.syswin);
628
629 if (w.mHasSurface && isDisplayed) {
630 final int type = w.mAttrs.type;
631 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
632 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
633 mTmpApplySurfaceChangesTransactionState.syswin = true;
634 }
635 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
636 && w.mAttrs.preferredRefreshRate != 0) {
637 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
638 = w.mAttrs.preferredRefreshRate;
639 }
640 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
641 && w.mAttrs.preferredDisplayModeId != 0) {
642 mTmpApplySurfaceChangesTransactionState.preferredModeId
643 = w.mAttrs.preferredDisplayModeId;
644 }
645 }
646 }
647
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800648 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
649 && mWallpaperController.isWallpaperTarget(w)) {
650 // This is the wallpaper target and its obscured state changed... make sure the
651 // current wallpaper's visibility has been updated accordingly.
652 mWallpaperController.updateWallpaperVisibility();
653 }
654
Jorim Jaggi6b0f8462018-01-05 17:23:47 +0100655 w.handleWindowMovedIfNeeded(mPendingTransaction);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800656
657 final WindowStateAnimator winAnimator = w.mWinAnimator;
658
659 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
660 w.mContentChanged = false;
661
662 // Moved from updateWindowsAndWallpaperLocked().
663 if (w.mHasSurface) {
664 // Take care of the window being ready to display.
665 final boolean committed = winAnimator.commitFinishDrawingLocked();
666 if (isDefaultDisplay && committed) {
667 if (w.mAttrs.type == TYPE_DREAM) {
668 // HACK: When a dream is shown, it may at that point hide the lock screen.
669 // So we need to redo the layout to let the phone window manager make this
670 // happen.
671 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
672 if (DEBUG_LAYOUT_REPEATS) {
673 surfacePlacer.debugLayoutRepeats(
674 "dream and commitFinishDrawingLocked true",
675 pendingLayoutChanges);
676 }
677 }
678 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
679 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
680 "First draw done in potential wallpaper target " + w);
681 root.mWallpaperMayChange = true;
682 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
683 if (DEBUG_LAYOUT_REPEATS) {
684 surfacePlacer.debugLayoutRepeats(
685 "wallpaper and commitFinishDrawingLocked true",
686 pendingLayoutChanges);
687 }
688 }
689 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800690 }
691
692 final AppWindowToken atoken = w.mAppToken;
693 if (atoken != null) {
694 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
695 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
696 mTmpUpdateAllDrawn.add(atoken);
697 }
698 }
699
700 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
701 && w.isDisplayedLw()) {
702 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
703 }
704
705 w.updateResizingWindowIfNeeded();
706 };
707
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800708 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800709 * Create new {@link DisplayContent} instance, add itself to the root window container and
710 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700711 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800712 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700713 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
714 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700715 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700716 DisplayContent(Display display, WindowManagerService service,
Robert Carrb1579c82017-09-05 14:54:47 -0700717 WallpaperController wallpaperController) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100718 super(service);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800719 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
720 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
721 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
722 + " new=" + display);
723 }
724
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700725 mDisplay = display;
726 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700727 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700728 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700729 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700730 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000731 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700732 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800733 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700734 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700735
Robert Carrb1579c82017-09-05 14:54:47 -0700736 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth);
737
738 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
739 .setSize(mSurfaceSize, mSurfaceSize)
740 .setOpaque(true);
741 mWindowingLayer = b.setName("Display Root").build();
742 mOverlayLayer = b.setName("Display Overlays").build();
743
Robert Carrf59b8dd2017-10-02 18:58:36 -0700744 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700745 .setLayerStack(mWindowingLayer, mDisplayId)
746 .show(mWindowingLayer)
747 .setLayer(mOverlayLayer, 1)
748 .setLayerStack(mOverlayLayer, mDisplayId)
749 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700750 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700751
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700752 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700753 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700754 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700755 super.addChild(mAboveAppWindowsContainers, null);
756 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800757
758 // Add itself as a child to the root container.
759 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700760
761 // TODO(b/62541591): evaluate whether this is the best spot to declare the
762 // {@link DisplayContent} ready for use.
763 mDisplayReady = true;
764 }
765
766 boolean isReady() {
767 // The display is ready when the system and the individual display are both ready.
768 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700769 }
770
771 int getDisplayId() {
772 return mDisplayId;
773 }
774
Wale Ogunwale02319a62016-09-26 15:21:22 -0700775 WindowToken getWindowToken(IBinder binder) {
776 return mTokenMap.get(binder);
777 }
778
779 AppWindowToken getAppWindowToken(IBinder binder) {
780 final WindowToken token = getWindowToken(binder);
781 if (token == null) {
782 return null;
783 }
784 return token.asAppWindowToken();
785 }
786
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700787 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700788 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
789 if (dc != null) {
790 // We currently don't support adding a window token to the display if the display
791 // already has the binder mapped to another token. If there is a use case for supporting
792 // this moving forward we will either need to merge the WindowTokens some how or have
793 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700794 throw new IllegalArgumentException("Can't map token=" + token + " to display="
795 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700796 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700797 if (binder == null) {
798 throw new IllegalArgumentException("Can't map token=" + token + " to display="
799 + getName() + " binder is null");
800 }
801 if (token == null) {
802 throw new IllegalArgumentException("Can't map null token to display="
803 + getName() + " binder=" + binder);
804 }
805
Wale Ogunwale02319a62016-09-26 15:21:22 -0700806 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700807
808 if (token.asAppWindowToken() == null) {
809 // Add non-app token to container hierarchy on the display. App tokens are added through
810 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700811 switch (token.windowType) {
812 case TYPE_WALLPAPER:
813 mBelowAppWindowsContainers.addChild(token);
814 break;
815 case TYPE_INPUT_METHOD:
816 case TYPE_INPUT_METHOD_DIALOG:
817 mImeWindowsContainers.addChild(token);
818 break;
819 default:
820 mAboveAppWindowsContainers.addChild(token);
821 break;
822 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700823 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700824 }
825
826 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700827 final WindowToken token = mTokenMap.remove(binder);
828 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800829 token.setExiting();
830 }
831 return token;
832 }
833
834 /** Changes the display the input window token is housed on to this one. */
835 void reParentWindowToken(WindowToken token) {
836 final DisplayContent prevDc = token.getDisplayContent();
837 if (prevDc == this) {
838 return;
839 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800840 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
841 && token.asAppWindowToken() == null) {
842 // Removed the token from the map, but made sure it's not an app token before removing
843 // from parent.
844 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700845 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800846
847 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700848 }
849
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700850 void removeAppToken(IBinder binder) {
851 final WindowToken token = removeWindowToken(binder);
852 if (token == null) {
853 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
854 return;
855 }
856
857 final AppWindowToken appToken = token.asAppWindowToken();
858
859 if (appToken == null) {
860 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
861 return;
862 }
863
864 appToken.onRemovedFromDisplay();
865 }
866
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700867 Display getDisplay() {
868 return mDisplay;
869 }
870
Craig Mautner59c00972012-07-30 12:10:24 -0700871 DisplayInfo getDisplayInfo() {
872 return mDisplayInfo;
873 }
874
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700875 DisplayMetrics getDisplayMetrics() {
876 return mDisplayMetrics;
877 }
878
Andrii Kulian8ee72852017-03-10 10:36:45 -0800879 int getRotation() {
880 return mRotation;
881 }
882
883 void setRotation(int newRotation) {
884 mRotation = newRotation;
885 }
886
887 int getLastOrientation() {
888 return mLastOrientation;
889 }
890
891 void setLastOrientation(int orientation) {
892 mLastOrientation = orientation;
893 }
894
895 boolean getAltOrientation() {
896 return mAltOrientation;
897 }
898
899 void setAltOrientation(boolean altOrientation) {
900 mAltOrientation = altOrientation;
901 }
902
903 int getLastWindowForcedOrientation() {
904 return mLastWindowForcedOrientation;
905 }
906
Andrii Kulian06d07d62017-03-14 11:11:47 -0700907 /**
908 * Update rotation of the display.
909 *
910 * Returns true if the rotation has been changed. In this case YOU MUST CALL
911 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
912 */
913 boolean updateRotationUnchecked(boolean inTransaction) {
914 if (mService.mDeferredRotationPauseCount > 0) {
915 // Rotation updates have been paused temporarily. Defer the update until
916 // updates have been resumed.
917 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
918 return false;
919 }
920
921 ScreenRotationAnimation screenRotationAnimation =
922 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
923 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
924 // Rotation updates cannot be performed while the previous rotation change
925 // animation is still in progress. Skip this update. We will try updating
926 // again after the animation is finished and the display is unfrozen.
927 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
928 return false;
929 }
930 if (mService.mDisplayFrozen) {
931 // Even if the screen rotation animation has finished (e.g. isAnimating
932 // returns false), there is still some time where we haven't yet unfrozen
933 // the display. We also need to abort rotation here.
934 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
935 "Deferring rotation, still finishing previous rotation");
936 return false;
937 }
938
939 if (!mService.mDisplayEnabled) {
940 // No point choosing a rotation if the display is not enabled.
941 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
942 return false;
943 }
944
945 final int oldRotation = mRotation;
946 final int lastOrientation = mLastOrientation;
947 final boolean oldAltOrientation = mAltOrientation;
948 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr0e007272017-10-02 13:00:55 -0700949 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
Robert Carr897215d2017-03-29 12:25:50 -0700950 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700951
Robert Carr0e007272017-10-02 13:00:55 -0700952 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700953 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
954 if (seamlessRotated != null) {
955 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
956 // to complete (that is, waiting for windows to redraw). It's tempting to check
957 // w.mSeamlessRotationCount but that could be incorrect in the case of
958 // window-removal.
959 return false;
960 }
Robert Carr0e007272017-10-02 13:00:55 -0700961
962 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700963 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -0700964 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -0700965 mayRotateSeamlessly = false;
966 }
967 for (int i = 0; i < mService.mSessions.size(); i++) {
968 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
969 mayRotateSeamlessly = false;
970 break;
971 }
972 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700973 }
Robert Carr0e007272017-10-02 13:00:55 -0700974 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700975
976 // TODO: Implement forced rotation changes.
977 // Set mAltOrientation to indicate that the application is receiving
978 // an orientation that has different metrics than it expected.
979 // eg. Portrait instead of Landscape.
980
981 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
982 lastOrientation, rotation);
983
984 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
985 + ", got rotation " + rotation + " which has "
986 + (altOrientation ? "incompatible" : "compatible") + " metrics");
987
988 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
989 // No change.
990 return false;
991 }
992
993 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
994 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
995 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
996
997 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
998 mService.mWaitingForConfig = true;
999 }
1000
1001 mRotation = rotation;
1002 mAltOrientation = altOrientation;
1003 if (isDefaultDisplay) {
1004 mService.mPolicy.setRotationLw(rotation);
1005 }
1006
1007 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1008 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1009 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1010 WINDOW_FREEZE_TIMEOUT_DURATION);
1011
1012 setLayoutNeeded();
1013 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001014 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001015
1016 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001017 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001018 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1019 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1020 mDisplayId);
1021 } else {
1022 // The screen rotation animation uses a screenshot to freeze the screen
1023 // while windows resize underneath.
1024 // When we are rotating seamlessly, we allow the elements to transition
1025 // to their rotated state independently and without a freeze required.
1026 screenRotationAnimation = null;
1027
1028 // We have to reset this in case a window was removed before it
1029 // finished seamless rotation.
1030 mService.mSeamlessRotationCount = 0;
1031 }
1032
1033 // We need to update our screen size information to match the new rotation. If the rotation
1034 // has actually changed then this method will return true and, according to the comment at
1035 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1036 // By updating the Display info here it will be available to
1037 // #computeScreenConfiguration() later.
1038 updateDisplayAndOrientation(getConfiguration().uiMode);
1039
1040 if (!inTransaction) {
1041 if (SHOW_TRANSACTIONS) {
1042 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1043 }
1044 mService.openSurfaceTransaction();
1045 }
1046 try {
1047 // NOTE: We disable the rotation in the emulator because
1048 // it doesn't support hardware OpenGL emulation yet.
1049 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1050 && screenRotationAnimation.hasScreenshot()) {
Robert Carrb1579c82017-09-05 14:54:47 -07001051 if (screenRotationAnimation.setRotationInTransaction(rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001052 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1053 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1054 mService.scheduleAnimationLocked();
1055 }
1056 }
1057
1058 if (rotateSeamlessly) {
1059 forAllWindows(w -> {
1060 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1061 }, true /* traverseTopToBottom */);
1062 }
1063
1064 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1065 } finally {
1066 if (!inTransaction) {
Adrian Roos111aff92017-09-27 18:11:46 +02001067 mService.closeSurfaceTransaction("setRotationUnchecked");
Andrii Kulian06d07d62017-03-14 11:11:47 -07001068 if (SHOW_LIGHT_TRANSACTIONS) {
1069 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1070 }
1071 }
1072 }
1073
1074 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001075 if (w.mHasSurface && !rotateSeamlessly) {
1076 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001077 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001078 mService.mRoot.mOrientationChangeComplete = false;
1079 w.mLastFreezeDuration = 0;
1080 }
1081 w.mReportOrientationChanged = true;
1082 }, true /* traverseTopToBottom */);
1083
1084 if (rotateSeamlessly) {
1085 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1086 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1087 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1088 }
1089
1090 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1091 final WindowManagerService.RotationWatcher rotationWatcher
1092 = mService.mRotationWatchers.get(i);
1093 if (rotationWatcher.mDisplayId == mDisplayId) {
1094 try {
1095 rotationWatcher.mWatcher.onRotationChanged(rotation);
1096 } catch (RemoteException e) {
1097 // Ignore
1098 }
1099 }
1100 }
1101
1102 // TODO (multi-display): Magnification is supported only for the default display.
1103 // Announce rotation only if we will not animate as we already have the
1104 // windows in final state. Otherwise, we make this call at the rotation end.
1105 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1106 && isDefaultDisplay) {
1107 mService.mAccessibilityController.onRotationChangedLocked(this);
1108 }
1109
1110 return true;
1111 }
1112
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001113 void configureDisplayPolicy() {
1114 mService.mPolicy.setInitialDisplaySize(getDisplay(),
1115 mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1116
1117 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
1118 }
1119
Andrii Kulian06d07d62017-03-14 11:11:47 -07001120 /**
1121 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1122 * changed.
1123 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1124 */
1125 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1126 // Use the effective "visual" dimensions based on current rotation
1127 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1128 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1129 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1130 int dw = realdw;
1131 int dh = realdh;
1132
1133 if (mAltOrientation) {
1134 if (realdw > realdh) {
1135 // Turn landscape into portrait.
1136 int maxw = (int)(realdh/1.3f);
1137 if (maxw < realdw) {
1138 dw = maxw;
1139 }
1140 } else {
1141 // Turn portrait into landscape.
1142 int maxh = (int)(realdw/1.3f);
1143 if (maxh < realdh) {
1144 dh = maxh;
1145 }
1146 }
1147 }
1148
1149 // Update application display metrics.
1150 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1151 mDisplayId);
1152 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1153 mDisplayId);
1154 mDisplayInfo.rotation = mRotation;
1155 mDisplayInfo.logicalWidth = dw;
1156 mDisplayInfo.logicalHeight = dh;
1157 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1158 mDisplayInfo.appWidth = appWidth;
1159 mDisplayInfo.appHeight = appHeight;
1160 if (isDefaultDisplay) {
1161 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1162 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1163 }
1164 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1165 if (mDisplayScalingDisabled) {
1166 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1167 } else {
1168 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1169 }
1170
1171 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1172 mDisplayInfo);
1173
1174 mBaseDisplayRect.set(0, 0, dw, dh);
1175
1176 if (isDefaultDisplay) {
1177 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1178 mCompatDisplayMetrics);
1179 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001180
1181 updateBounds();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001182 return mDisplayInfo;
1183 }
1184
1185 /**
1186 * Compute display configuration based on display properties and policy settings.
1187 * Do not call if mDisplayReady == false.
1188 */
1189 void computeScreenConfiguration(Configuration config) {
1190 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1191
1192 final int dw = displayInfo.logicalWidth;
1193 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001194 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1195 // TODO: Probably best to set this based on some setting in the display content object,
1196 // so the display can be configured for things like fullscreen.
1197 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001198
Andrii Kulian06d07d62017-03-14 11:11:47 -07001199 config.screenWidthDp =
1200 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1201 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1202 config.screenHeightDp =
1203 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1204 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001205
1206 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1207 final int leftInset = mTmpRect.left;
1208 final int topInset = mTmpRect.top;
1209 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001210 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1211 leftInset + displayInfo.appWidth /* right */,
1212 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001213 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1214 || displayInfo.rotation == Surface.ROTATION_270);
1215
1216 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1217 mDisplayMetrics.density, config);
1218
1219 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1220 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1221 ? Configuration.SCREENLAYOUT_ROUND_YES
1222 : Configuration.SCREENLAYOUT_ROUND_NO);
1223
1224 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1225 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1226 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1227 dh, mDisplayId);
1228 config.densityDpi = displayInfo.logicalDensityDpi;
1229
1230 config.colorMode =
1231 (displayInfo.isHdr()
1232 ? Configuration.COLOR_MODE_HDR_YES
1233 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001234 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001235 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1236 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1237
1238 // Update the configuration based on available input devices, lid switch,
1239 // and platform configuration.
1240 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1241 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1242 config.navigation = Configuration.NAVIGATION_NONAV;
1243
1244 int keyboardPresence = 0;
1245 int navigationPresence = 0;
1246 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1247 final int len = devices != null ? devices.length : 0;
1248 for (int i = 0; i < len; i++) {
1249 InputDevice device = devices[i];
1250 if (!device.isVirtual()) {
1251 final int sources = device.getSources();
1252 final int presenceFlag = device.isExternal() ?
1253 WindowManagerPolicy.PRESENCE_EXTERNAL :
1254 WindowManagerPolicy.PRESENCE_INTERNAL;
1255
1256 // TODO(multi-display): Configure on per-display basis.
1257 if (mService.mIsTouchDevice) {
1258 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1259 InputDevice.SOURCE_TOUCHSCREEN) {
1260 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1261 }
1262 } else {
1263 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1264 }
1265
1266 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1267 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1268 navigationPresence |= presenceFlag;
1269 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1270 && config.navigation == Configuration.NAVIGATION_NONAV) {
1271 config.navigation = Configuration.NAVIGATION_DPAD;
1272 navigationPresence |= presenceFlag;
1273 }
1274
1275 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1276 config.keyboard = Configuration.KEYBOARD_QWERTY;
1277 keyboardPresence |= presenceFlag;
1278 }
1279 }
1280 }
1281
1282 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1283 config.navigation = Configuration.NAVIGATION_DPAD;
1284 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1285 }
1286
1287 // Determine whether a hard keyboard is available and enabled.
1288 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1289 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1290 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1291 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1292 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1293 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1294 }
1295
1296 // Let the policy update hidden states.
1297 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1298 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1299 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1300 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1301 }
1302
1303 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1304 int displayId) {
1305 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1306 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1307 final int unrotDw, unrotDh;
1308 if (rotated) {
1309 unrotDw = dh;
1310 unrotDh = dw;
1311 } else {
1312 unrotDw = dw;
1313 unrotDh = dh;
1314 }
1315 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1316 displayId);
1317 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1318 displayId);
1319 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1320 displayId);
1321 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1322 displayId);
1323 return sw;
1324 }
1325
1326 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1327 DisplayMetrics dm, int dw, int dh, int displayId) {
1328 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1329 displayId);
1330 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1331 uiMode, displayId);
1332 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1333 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1334 if (curSize == 0 || size < curSize) {
1335 curSize = size;
1336 }
1337 return curSize;
1338 }
1339
1340 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1341 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1342
1343 // We need to determine the smallest width that will occur under normal
1344 // operation. To this, start with the base screen size and compute the
1345 // width under the different possible rotations. We need to un-rotate
1346 // the current screen dimensions before doing this.
1347 int unrotDw, unrotDh;
1348 if (rotated) {
1349 unrotDw = dh;
1350 unrotDh = dw;
1351 } else {
1352 unrotDw = dw;
1353 unrotDh = dh;
1354 }
1355 displayInfo.smallestNominalAppWidth = 1<<30;
1356 displayInfo.smallestNominalAppHeight = 1<<30;
1357 displayInfo.largestNominalAppWidth = 0;
1358 displayInfo.largestNominalAppHeight = 0;
1359 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1360 unrotDh);
1361 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1362 unrotDw);
1363 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1364 unrotDh);
1365 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1366 unrotDw);
1367 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1368 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1369 displayId);
1370 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1371 displayId);
1372 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1373 displayId);
1374 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1375 displayId);
1376 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1377 outConfig.screenLayout = sl;
1378 }
1379
1380 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1381 int uiMode, int displayId) {
1382 // Get the app screen size at this rotation.
1383 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1384 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1385
1386 // Compute the screen layout size class for this rotation.
1387 int longSize = w;
1388 int shortSize = h;
1389 if (longSize < shortSize) {
1390 int tmp = longSize;
1391 longSize = shortSize;
1392 shortSize = tmp;
1393 }
1394 longSize = (int)(longSize/density);
1395 shortSize = (int)(shortSize/density);
1396 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1397 }
1398
1399 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1400 int uiMode, int dw, int dh) {
1401 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1402 displayId);
1403 if (width < displayInfo.smallestNominalAppWidth) {
1404 displayInfo.smallestNominalAppWidth = width;
1405 }
1406 if (width > displayInfo.largestNominalAppWidth) {
1407 displayInfo.largestNominalAppWidth = width;
1408 }
1409 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1410 displayId);
1411 if (height < displayInfo.smallestNominalAppHeight) {
1412 displayInfo.smallestNominalAppHeight = height;
1413 }
1414 if (height > displayInfo.largestNominalAppHeight) {
1415 displayInfo.largestNominalAppHeight = height;
1416 }
1417 }
1418
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001419 DockedStackDividerController getDockedDividerController() {
1420 return mDividerControllerLocked;
1421 }
1422
Winson Chung655332c2016-10-31 13:14:28 -07001423 PinnedStackController getPinnedStackController() {
1424 return mPinnedStackControllerLocked;
1425 }
1426
Jeff Browna506a6e2013-06-04 00:02:38 -07001427 /**
1428 * Returns true if the specified UID has access to this display.
1429 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001430 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001431 return mDisplay.hasAccess(uid);
1432 }
1433
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001434 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001435 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001436 }
1437
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001438 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001439 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001440 }
1441
Wale Ogunwale61911492017-10-11 08:50:50 -07001442 /**
1443 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1444 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001445 TaskStack getSplitScreenPrimaryStack() {
1446 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001447 return (stack != null && stack.isVisible()) ? stack : null;
1448 }
1449
1450 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001451 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001452 * not visible.
1453 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001454 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1455 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001456 }
1457
1458 TaskStack getPinnedStack() {
1459 return mTaskStackContainers.getPinnedStack();
1460 }
1461
1462 private boolean hasPinnedStack() {
1463 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001464 }
1465
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001466 /**
1467 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1468 * Null is no compatible stack on the display.
1469 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001470 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001471 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1472 }
1473
1474 /**
1475 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1476 * activity type. Null is no compatible stack on the display.
1477 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001478 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001479 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001480 }
1481
Bryce Lee48f4b572017-04-10 10:54:15 -07001482 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001483 TaskStack getTopStack() {
1484 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001485 }
1486
Wale Ogunwale61911492017-10-11 08:50:50 -07001487 void onStackWindowingModeChanged(TaskStack stack) {
1488 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001489 }
1490
Andrii Kulian441e4492016-09-29 15:25:00 -07001491 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001492 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001493 super.onConfigurationChanged(newParentConfig);
1494
Andrii Kulian3a507b52016-09-19 18:14:12 -07001495 // The display size information is heavily dependent on the resources in the current
1496 // configuration, so we need to reconfigure it every time the configuration changes.
1497 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1498 mService.reconfigureDisplayLocked(this);
1499
Bryce Leef3c6a472017-11-14 14:53:06 -08001500 final DockedStackDividerController dividerController = getDockedDividerController();
1501
1502 if (dividerController != null) {
1503 getDockedDividerController().onConfigurationChanged();
1504 }
1505
1506 final PinnedStackController pinnedStackController = getPinnedStackController();
1507
1508 if (pinnedStackController != null) {
1509 getPinnedStackController().onConfigurationChanged();
1510 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001511 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001512
Andrii Kulian441e4492016-09-29 15:25:00 -07001513 /**
1514 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1515 * bounds were updated.
1516 */
1517 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001518 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1519 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001520 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001521 changedStackList.add(stack.mStackId);
1522 }
1523 }
Winson Chung32c566f2017-04-11 18:31:21 -07001524
1525 // If there was no pinned stack, we still need to notify the controller of the display info
1526 // update as a result of the config change. We do this here to consolidate the flow between
1527 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001528 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001529 mPinnedStackControllerLocked.onDisplayInfoChanged();
1530 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001531 }
1532
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001533 @Override
1534 boolean fillsParent() {
1535 return true;
1536 }
1537
1538 @Override
1539 boolean isVisible() {
1540 return true;
1541 }
1542
1543 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001544 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001545 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001546 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001547 }
1548
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001549 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001550 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1551 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1552 // target, or here in order if there isn't an IME target.
1553 if (traverseTopToBottom) {
1554 for (int i = mChildren.size() - 1; i >= 0; --i) {
1555 final DisplayChildWindowContainer child = mChildren.get(i);
1556 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1557 // In this case the Ime windows will be processed above their target so we skip
1558 // here.
1559 continue;
1560 }
1561 if (child.forAllWindows(callback, traverseTopToBottom)) {
1562 return true;
1563 }
1564 }
1565 } else {
1566 final int count = mChildren.size();
1567 for (int i = 0; i < count; i++) {
1568 final DisplayChildWindowContainer child = mChildren.get(i);
1569 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1570 // In this case the Ime windows will be processed above their target so we skip
1571 // here.
1572 continue;
1573 }
1574 if (child.forAllWindows(callback, traverseTopToBottom)) {
1575 return true;
1576 }
1577 }
1578 }
1579 return false;
1580 }
1581
1582 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1583 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1584 }
1585
Wale Ogunwale399c8692017-05-08 14:22:42 -07001586 @Override
1587 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001588 final WindowManagerPolicy policy = mService.mPolicy;
1589
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001590 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001591 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001592 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001593 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001594 // If the display is frozen, some activities may be in the middle of restarting, and
1595 // thus have removed their old window. If the window has the flag to hide the lock
1596 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1597 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001598 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001599 } else if (policy.isKeyguardLocked()) {
1600 // Use the last orientation the while the display is frozen with the keyguard
1601 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1602 // window. We don't want to check the show when locked window directly though as
1603 // things aren't stable while the display is frozen, for example the window could be
1604 // momentarily unavailable due to activity relaunch.
1605 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001606 + "return " + mLastOrientation);
1607 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001608 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001609 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001610 final int orientation = mAboveAppWindowsContainers.getOrientation();
1611 if (orientation != SCREEN_ORIENTATION_UNSET) {
1612 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001613 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001614 }
1615
Wale Ogunwale399c8692017-05-08 14:22:42 -07001616 // Top system windows are not requesting an orientation. Start searching from apps.
1617 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001618 }
1619
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001620 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001621 // Check if display metrics changed and update base values if needed.
1622 updateBaseDisplayMetricsIfNeeded();
1623
Craig Mautner722285e2012-09-07 13:55:58 -07001624 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001625 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001626
Wale Ogunwale61911492017-10-11 08:50:50 -07001627 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1628 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001629 }
Craig Mautner722285e2012-09-07 13:55:58 -07001630 }
1631
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001632 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001633 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1634 if (displayManagerInternal != null) {
1635 // Bootstrap the default logical display from the display manager.
1636 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1637 if (newDisplayInfo != null) {
1638 mDisplayInfo.copyFrom(newDisplayInfo);
1639 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001640 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001641
Andrii Kuliancd097992017-03-23 18:31:59 -07001642 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1643 mDisplayInfo.logicalDensityDpi);
1644 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1645 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1646 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001647 }
1648
Andrii Kuliancd097992017-03-23 18:31:59 -07001649 /**
1650 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1651 * values.
1652 */
1653 private void updateBaseDisplayMetricsIfNeeded() {
1654 // Get real display metrics without overrides from WM.
1655 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1656 final int orientation = mDisplayInfo.rotation;
1657 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1658 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1659 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1660 final int newDensity = mDisplayInfo.logicalDensityDpi;
1661
1662 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1663 || mInitialDisplayHeight != newHeight
1664 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1665
1666 if (displayMetricsChanged) {
1667 // Check if display size or density is forced.
1668 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1669 || mBaseDisplayHeight != mInitialDisplayHeight;
1670 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1671
1672 // If there is an override set for base values - use it, otherwise use new values.
1673 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1674 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1675 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1676
1677 // Real display metrics changed, so we should also update initial values.
1678 mInitialDisplayWidth = newWidth;
1679 mInitialDisplayHeight = newHeight;
1680 mInitialDisplayDensity = newDensity;
1681 mService.reconfigureDisplayLocked(this);
1682 }
1683 }
1684
Bryce Lee27cec322017-03-21 09:41:37 -07001685 /** Sets the maximum width the screen resolution can be */
1686 void setMaxUiWidth(int width) {
1687 if (DEBUG_DISPLAY) {
1688 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1689 }
1690
1691 mMaxUiWidth = width;
1692
1693 // Update existing metrics.
1694 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1695 }
1696
1697 /** Update base (override) display metrics. */
1698 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1699 mBaseDisplayWidth = baseWidth;
1700 mBaseDisplayHeight = baseHeight;
1701 mBaseDisplayDensity = baseDensity;
1702
1703 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1704 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1705 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1706 mBaseDisplayWidth = mMaxUiWidth;
1707
1708 if (DEBUG_DISPLAY) {
1709 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1710 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1711 + " on display:" + getDisplayId());
1712 }
1713 }
1714
1715 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08001716
1717 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07001718 }
1719
Wale Ogunwaledb506192017-12-08 10:57:32 -08001720 void getStableRect(Rect out) {
1721 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08001722 }
1723
Wale Ogunwale61911492017-10-11 08:50:50 -07001724 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001725 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1726 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001727
Wale Ogunwale61911492017-10-11 08:50:50 -07001728 final TaskStack stack = new TaskStack(mService, stackId, controller);
1729 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001730 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001731 }
1732
Andrii Kulian51c1b672017-04-07 18:39:32 -07001733 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001734 final DisplayContent prevDc = stack.getDisplayContent();
1735 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001736 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1737 + " which is not currently attached to any display");
1738 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001739 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001740 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1741 + " to its current displayId=" + mDisplayId);
1742 }
1743
Wale Ogunwale61911492017-10-11 08:50:50 -07001744 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001745 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001746 }
1747
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001748 @Override
1749 protected void addChild(DisplayChildWindowContainer child,
1750 Comparator<DisplayChildWindowContainer> comparator) {
1751 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1752 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001753
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001754 @Override
1755 protected void addChild(DisplayChildWindowContainer child, int index) {
1756 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1757 }
1758
1759 @Override
1760 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001761 // Only allow removal of direct children from this display if the display is in the process
1762 // of been removed.
1763 if (mRemovingDisplay) {
1764 super.removeChild(child);
1765 return;
1766 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001767 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001768 }
1769
Andrii Kuliand2765632016-12-12 22:26:34 -08001770 @Override
1771 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1772 // Children of the display are statically ordered, so the real intention here is to perform
1773 // the operation on the display and not the static direct children.
1774 getParent().positionChildAt(position, this, includingParents);
1775 }
1776
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001777 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001778 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1779 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001780 final int taskId = stack.taskIdFromPoint(x, y);
1781 if (taskId != -1) {
1782 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001783 }
1784 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001785 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001786 }
1787
Chong Zhang8e89b312015-09-09 15:09:30 -07001788 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001789 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001790 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001791 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001792 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001793 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001794 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07001795 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1796 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001797 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001798 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001799 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001800
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001801 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1802 if (mTmpTaskForResizePointSearchResult.searchDone) {
1803 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001804 }
1805 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001806 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001807 }
1808
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001809 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001810 // The provided task is the task on this display with focus, so if WindowManagerService's
1811 // focused app is not on this display, focusedTask will be null.
1812 if (focusedTask == null) {
1813 mTouchExcludeRegion.setEmpty();
1814 } else {
1815 mTouchExcludeRegion.set(mBaseDisplayRect);
1816 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1817 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07001818 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1819 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001820 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
1821 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08001822 }
1823 // If we removed the focused task above, add it back and only leave its
1824 // outside touch area in the exclusion. TapDectector is not interested in
1825 // any touch inside the focused task itself.
1826 if (!mTmpRect2.isEmpty()) {
1827 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1828 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001829 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001830 final WindowState inputMethod = mService.mInputMethodWindow;
1831 if (inputMethod != null && inputMethod.isVisibleLw()) {
1832 // If the input method is visible and the user is typing, we don't want these touch
1833 // events to be intercepted and used to change focus. This would likely cause a
1834 // disappearance of the input method.
1835 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001836 if (inputMethod.getDisplayId() == mDisplayId) {
1837 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1838 } else {
1839 // IME is on a different display, so we need to update its tap detector.
1840 // TODO(multidisplay): Remove when IME will always appear on same display.
1841 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1842 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001843 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001844 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1845 WindowState win = mTapExcludedWindows.get(i);
1846 win.getTouchableRegion(mTmpRegion);
1847 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1848 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001849 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07001850 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001851 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001852 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001853 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1854 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001855 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001856 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001857 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001858 }
1859
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001860 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001861 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001862 super.switchUser();
1863 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001864 }
1865
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001866 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001867 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1868 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001869 }
1870 }
1871
Wale Ogunwale10124582016-09-15 20:25:50 -07001872 @Override
1873 void removeIfPossible() {
1874 if (isAnimating()) {
1875 mDeferredRemoval = true;
1876 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001877 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001878 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001879 }
1880
Wale Ogunwale10124582016-09-15 20:25:50 -07001881 @Override
1882 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001883 mRemovingDisplay = true;
1884 try {
1885 super.removeImmediately();
1886 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07001887 if (mService.canDispatchPointerEvents()) {
1888 if (mTapDetector != null) {
1889 mService.unregisterPointerEventListener(mTapDetector);
1890 }
1891 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
1892 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1893 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001894 }
Jorim Jaggia3844032018-01-03 15:54:43 +01001895 mService.mAnimator.removeDisplayLocked(mDisplayId);
1896
Robert Carrb1579c82017-09-05 14:54:47 -07001897 // The pending transaction won't be applied so we should
1898 // just clean up any surfaces pending destruction.
1899 onPendingTransactionApplied();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001900 } finally {
1901 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001902 }
Craig Mautner95da1082014-02-24 17:54:35 -08001903 }
1904
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001905 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001906 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001907 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001908 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1909
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001910 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001911 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001912 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001913 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001914 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001915 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001916 }
1917
Andrii Kulian0214ed92017-05-16 13:44:05 -07001918 /** @return 'true' if removal of this display content is deferred due to active animation. */
1919 boolean isRemovalDeferred() {
1920 return mDeferredRemoval;
1921 }
1922
Wale Ogunwale10124582016-09-15 20:25:50 -07001923 boolean animateForIme(float interpolatedValue, float animationTarget,
1924 float dividerAnimationTarget) {
1925 boolean updated = false;
1926
Wale Ogunwale61911492017-10-11 08:50:50 -07001927 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1928 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001929 if (stack == null || !stack.isAdjustedForIme()) {
1930 continue;
1931 }
1932
1933 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1934 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1935 updated = true;
1936 } else {
1937 mDividerControllerLocked.mLastAnimationProgress =
1938 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1939 mDividerControllerLocked.mLastDividerProgress =
1940 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1941 updated |= stack.updateAdjustForIme(
1942 mDividerControllerLocked.mLastAnimationProgress,
1943 mDividerControllerLocked.mLastDividerProgress,
1944 false /* force */);
1945 }
1946 if (interpolatedValue >= 1f) {
1947 stack.endImeAdjustAnimation();
1948 }
1949 }
1950
1951 return updated;
1952 }
1953
1954 boolean clearImeAdjustAnimation() {
1955 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07001956 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1957 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001958 if (stack != null && stack.isAdjustedForIme()) {
1959 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1960 changed = true;
1961 }
1962 }
1963 return changed;
1964 }
1965
1966 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001967 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1968 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001969 if (stack.isVisible() && stack.isAdjustedForIme()) {
1970 stack.beginImeAdjustAnimation();
1971 }
1972 }
1973 }
1974
1975 void adjustForImeIfNeeded() {
1976 final WindowState imeWin = mService.mInputMethodWindow;
1977 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
1978 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001979 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07001980 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
1981 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
1982 imeTargetStack.getDockSide() : DOCKED_INVALID;
1983 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
1984 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
1985 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001986 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07001987 final boolean imeHeightChanged = imeVisible &&
1988 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
1989
1990 // The divider could be adjusted for IME position, or be thinner than usual,
1991 // or both. There are three possible cases:
1992 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
1993 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
1994 // - If IME is not visible, divider is not moved and is normal width.
1995
1996 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
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 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002000 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2001 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002002 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2003 } else {
2004 stack.resetAdjustedForIme(false);
2005 }
2006 }
2007 mDividerControllerLocked.setAdjustedForIme(
2008 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2009 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002010 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2011 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002012 stack.resetAdjustedForIme(!dockVisible);
2013 }
2014 mDividerControllerLocked.setAdjustedForIme(
2015 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2016 }
Winson Chung655332c2016-10-31 13:14:28 -07002017 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002018 }
2019
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002020 /**
2021 * If a window that has an animation specifying a colored background and the current wallpaper
2022 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2023 * suddenly disappear.
2024 */
2025 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002026 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2027 w -> w.mIsWallpaper && w.isVisibleNow());
2028
2029 if (visibleWallpaper != null) {
2030 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002031 }
2032 return winAnimator.mAnimLayer;
2033 }
2034
Wale Ogunwale10124582016-09-15 20:25:50 -07002035 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002036 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2037 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002038 stack.prepareFreezingTaskBounds();
2039 }
2040 }
2041
Wale Ogunwale94744212015-09-21 19:01:47 -07002042 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002043 getBounds(mTmpRect, newRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002044
2045 // Compute a transform matrix to undo the coordinate space transformation,
2046 // and present the window at the same physical position it previously occupied.
2047 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2048 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2049
2050 mTmpRectF.set(bounds);
2051 mTmpMatrix.mapRect(mTmpRectF);
2052 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002053 }
2054
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002055 static int deltaRotation(int oldRotation, int newRotation) {
2056 int delta = newRotation - oldRotation;
2057 if (delta < 0) delta += 4;
2058 return delta;
2059 }
2060
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002061 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002062 Matrix outMatrix) {
2063 // For rotations without Z-ordering we don't need the target rectangle's position.
2064 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2065 displayHeight, outMatrix);
2066 }
2067
2068 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2069 float displayWidth, float displayHeight, Matrix outMatrix) {
2070 switch (rotation) {
2071 case ROTATION_0:
2072 outMatrix.reset();
2073 break;
2074 case ROTATION_270:
2075 outMatrix.setRotate(270, 0, 0);
2076 outMatrix.postTranslate(0, displayHeight);
2077 outMatrix.postTranslate(rectTop, 0);
2078 break;
2079 case ROTATION_180:
2080 outMatrix.reset();
2081 break;
2082 case ROTATION_90:
2083 outMatrix.setRotate(90, 0, 0);
2084 outMatrix.postTranslate(displayWidth, 0);
2085 outMatrix.postTranslate(-rectTop, rectLeft);
2086 break;
2087 }
2088 }
2089
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002090 @CallSuper
2091 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002092 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002093 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002094 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002095 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002096 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2097 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002098 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002099 }
2100 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2101 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002102 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2103 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002104 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002105 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002106 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2107 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002108 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002109 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002110 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2111 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002112 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002113 }
2114 proto.write(DPI, mBaseDisplayDensity);
2115 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002116 proto.write(ROTATION, mRotation);
2117 final ScreenRotationAnimation screenRotationAnimation =
2118 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2119 if (screenRotationAnimation != null) {
2120 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2121 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002122 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002123 proto.end(token);
2124 }
2125
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002126 @Override
2127 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2128 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002129 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2130 final String subPrefix = " " + prefix;
2131 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2132 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2133 pw.print("dpi");
2134 if (mInitialDisplayWidth != mBaseDisplayWidth
2135 || mInitialDisplayHeight != mBaseDisplayHeight
2136 || mInitialDisplayDensity != mBaseDisplayDensity) {
2137 pw.print(" base=");
2138 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2139 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2140 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002141 if (mDisplayScalingDisabled) {
2142 pw.println(" noscale");
2143 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002144 pw.print(" cur=");
2145 pw.print(mDisplayInfo.logicalWidth);
2146 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2147 pw.print(" app=");
2148 pw.print(mDisplayInfo.appWidth);
2149 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2150 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2151 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2152 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2153 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002154 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002155 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002156 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002157
Craig Mautnerdc548482014-02-05 13:35:24 -08002158 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002159 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002160 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2161 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002162 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002163 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002164
Craig Mautnerdc548482014-02-05 13:35:24 -08002165 pw.println();
2166 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002167 pw.println();
2168 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002169 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002170 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002171 pw.print(" Exiting #"); pw.print(i);
2172 pw.print(' '); pw.print(token);
2173 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002174 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002175 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002176 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002177
Jorim Jaggi31f71702016-05-04 16:43:04 -07002178 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002179
2180 // Dump stack references
2181 final TaskStack homeStack = getHomeStack();
2182 if (homeStack != null) {
2183 pw.println(prefix + "homeStack=" + homeStack.getName());
2184 }
2185 final TaskStack pinnedStack = getPinnedStack();
2186 if (pinnedStack != null) {
2187 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2188 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002189 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002190 if (splitScreenPrimaryStack != null) {
2191 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2192 }
2193
2194 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002195 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002196 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002197 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002198
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002199 pw.println();
2200 mDisplayFrames.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002201 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002202
2203 @Override
2204 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002205 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002206 }
2207
2208 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002209 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002210 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002211
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002212 /** Returns true if the stack in the windowing mode is visible. */
2213 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002214 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002215 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002216 }
2217
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002218 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002219 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002220 final int x = (int) xf;
2221 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002222 final WindowState touchedWin = getWindow(w -> {
2223 final int flags = w.mAttrs.flags;
2224 if (!w.isVisibleLw()) {
2225 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002226 }
2227 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002228 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002229 }
2230
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002231 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002232 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002233 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002234 }
2235
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002236 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002237
2238 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002239 return mTmpRegion.contains(x, y) || touchFlags == 0;
2240 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002241
2242 return touchedWin;
2243 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002244
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002245 boolean canAddToastWindowForUid(int uid) {
2246 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002247 // Also if the app is focused adding more than one toast at
2248 // a time for better backwards compatibility.
2249 final WindowState focusedWindowForUid = getWindow(w ->
2250 w.mOwnerUid == uid && w.isFocused());
2251 if (focusedWindowForUid != null) {
2252 return true;
2253 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002254 final WindowState win = getWindow(w ->
2255 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2256 && !w.mWindowRemovalAllowed);
2257 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002258 }
2259
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002260 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002261 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2262 return;
2263 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002264
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002265 // Used to communicate the old focus to the callback method.
2266 mTmpWindow = oldFocus;
2267
2268 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002269 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002270
2271 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002272 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002273
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002274 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002275
2276 if (mTmpWindow == null) {
2277 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2278 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002279 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002280 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002281 }
2282
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002283 /** Updates the layer assignment of windows on this display. */
2284 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002285 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002286 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002287 if (setLayoutNeeded) {
2288 setLayoutNeeded();
2289 }
Robert Carrb1579c82017-09-05 14:54:47 -07002290
Robert Carrf59b8dd2017-10-02 18:58:36 -07002291 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002292 // the application of this transaction until the animation pass triggers
2293 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2294 // the hiding and showing of surfaces.
2295 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002296 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002297 }
2298
Wale Ogunwale1666e312016-12-16 11:27:18 -08002299 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2300 // moving containers or resizing them. Need to investigate the best way to have it automatically
2301 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002302 void layoutAndAssignWindowLayersIfNeeded() {
2303 mService.mWindowsChanged = true;
2304 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002305
2306 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2307 false /*updateInputWindows*/)) {
2308 assignWindowLayers(false /* setLayoutNeeded */);
2309 }
2310
2311 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2312 mService.mWindowPlacerLocked.performSurfacePlacement();
2313 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2314 }
2315
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002316 /** Returns true if a leaked surface was destroyed */
2317 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002318 // Used to indicate that a surface was leaked.
2319 mTmpWindow = null;
2320 forAllWindows(w -> {
2321 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002322 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002323 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002324 }
2325 if (!mService.mSessions.contains(wsa.mSession)) {
2326 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002327 + w + " surface=" + wsa.mSurfaceController
2328 + " token=" + w.mToken
2329 + " pid=" + w.mSession.mPid
2330 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002331 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002332 mService.mForceRemoves.add(w);
2333 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002334 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002335 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002336 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002337 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002338 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002339 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002340 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002341 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002342 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002343
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002344 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002345 }
2346
2347 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002348 * Determine and return the window that should be the IME target.
2349 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2350 * @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 +00002351 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002352 WindowState computeImeTarget(boolean updateImeTarget) {
2353 if (mService.mInputMethodWindow == null) {
2354 // There isn't an IME so there shouldn't be a target...That was easy!
2355 if (updateImeTarget) {
2356 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2357 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002358 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002359 }
2360 return null;
2361 }
2362
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002363 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2364 // same display. Or even when the current IME/target are not on the same screen as the next
2365 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002366 mUpdateImeTarget = updateImeTarget;
2367 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002368
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002369
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002370 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2371 // to be on top of it, but it is not -really- where input will go. So look down below
2372 // for a real window to target...
2373 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2374 final AppWindowToken token = target.mAppToken;
2375 if (token != null) {
2376 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2377 if (betterTarget != null) {
2378 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002379 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002380 }
2381 }
2382
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002383 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2384 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002385
2386 // Now, a special case -- if the last target's window is in the process of exiting, and is
2387 // above the new target, keep on the last target to avoid flicker. Consider for example a
2388 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2389 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2390 // scrim.
2391 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002392 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2393 && (target == null
2394 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002395 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002396 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002397 }
2398
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002399 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2400 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002401
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002402 if (target == null) {
2403 if (updateImeTarget) {
2404 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2405 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2406 + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002407 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002408 }
2409
2410 return null;
2411 }
2412
2413 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002414 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2415 if (token != null) {
2416
2417 // Now some fun for dealing with window animations that modify the Z order. We need
2418 // to look at all windows below the current target that are in this app, finding the
2419 // highest visible one in layering.
2420 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002421 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002422 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002423 }
2424
2425 if (highestTarget != null) {
2426 final AppTransition appTransition = mService.mAppTransition;
2427 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2428 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2429 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002430 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002431
2432 if (appTransition.isTransitionSet()) {
2433 // If we are currently setting up for an animation, hold everything until we
2434 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002435 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002436 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002437 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002438 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002439 // If the window we are currently targeting is involved with an animation,
2440 // and it is on top of the next target we will be over, then hold off on
2441 // moving until that is done.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002442 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002443 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002444 }
2445 }
2446 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002447
2448 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2449 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002450 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002451 }
2452
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002453 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002454 }
2455
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002456 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002457 if (target == mService.mInputMethodTarget
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002458 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002459 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002460 }
2461
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002462 mService.mInputMethodTarget = target;
2463 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002464 assignWindowLayers(false /* setLayoutNeeded */);
2465 }
2466
2467 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2468 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2469 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2470 }
2471
2472 // Used to indicate we have reached the first window in the range we are interested in.
2473 mTmpWindow = null;
2474
2475 // TODO: Figure-out a more efficient way to do this.
2476 final WindowState candidate = getWindow(w -> {
2477 if (w == top) {
2478 // Reached the first window in the range we are interested in.
2479 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002480 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002481 if (mTmpWindow == null) {
2482 return false;
2483 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002484
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002485 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2486 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002487 }
2488 // If we reached the bottom of the range of windows we are considering,
2489 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002490 if (w == bottom) {
2491 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002492 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002493 return false;
2494 });
2495
2496 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002497 }
2498
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002499 void setLayoutNeeded() {
2500 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2501 mLayoutNeeded = true;
2502 }
2503
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002504 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002505 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2506 mLayoutNeeded = false;
2507 }
2508
2509 boolean isLayoutNeeded() {
2510 return mLayoutNeeded;
2511 }
2512
Wale Ogunwale02319a62016-09-26 15:21:22 -07002513 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2514 if (mTokenMap.isEmpty()) {
2515 return;
2516 }
2517 pw.println(" Display #" + mDisplayId);
2518 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2519 while (it.hasNext()) {
2520 final WindowToken token = it.next();
2521 pw.print(" ");
2522 pw.print(token);
2523 if (dumpAll) {
2524 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002525 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07002526 } else {
2527 pw.println();
2528 }
2529 }
2530 }
2531
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002532 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002533 final int[] index = new int[1];
2534 forAllWindows(w -> {
2535 final WindowStateAnimator wAnim = w.mWinAnimator;
2536 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2537 index[0] = index[0] + 1;
2538 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002539 }
2540
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002541 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002542 forAllWindows(w -> {
2543 w.mWinAnimator.enableSurfaceTrace(fd);
2544 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002545 }
2546
2547 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002548 forAllWindows(w -> {
2549 w.mWinAnimator.disableSurfaceTrace();
2550 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002551 }
2552
Jorim Jaggife762342016-10-13 14:33:27 +02002553 /**
2554 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2555 */
2556 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2557 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002558 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002559 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2560 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002561 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02002562 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002563 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002564 }
2565
Wale Ogunwale494009b82016-10-21 09:01:38 -07002566 boolean checkWaitingForWindows() {
2567
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002568 mHaveBootMsg = false;
2569 mHaveApp = false;
2570 mHaveWallpaper = false;
2571 mHaveKeyguard = true;
2572
2573 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002574 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2575 return true;
2576 }
2577 if (w.isDrawnLw()) {
2578 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002579 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002580 } else if (w.mAttrs.type == TYPE_APPLICATION
2581 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002582 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002583 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002584 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002585 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002586 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002587 }
2588 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002589 return false;
2590 });
2591
2592 if (visibleWindow != null) {
2593 // We have a visible window.
2594 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002595 }
2596
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002597 // if the wallpaper service is disabled on the device, we're never going to have
2598 // wallpaper, don't bother waiting for it
2599 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2600 com.android.internal.R.bool.config_enableWallpaperService)
2601 && !mService.mOnlyCore;
2602
Wale Ogunwale494009b82016-10-21 09:01:38 -07002603 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2604 "******** booted=" + mService.mSystemBooted
2605 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002606 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2607 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2608 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002609
2610 // If we are turning on the screen to show the boot message, don't do it until the boot
2611 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002612 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002613 return true;
2614 }
2615
2616 // If we are turning on the screen after the boot is completed normally, don't do so until
2617 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002618 if (mService.mSystemBooted
2619 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002620 return true;
2621 }
2622
2623 return false;
2624 }
2625
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002626 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002627 mTmpWindowAnimator = animator;
2628 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002629 }
2630
2631 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002632 resetAnimationBackgroundAnimator();
2633
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002634 // Used to indicate a detached wallpaper.
2635 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002636 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002637
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002638 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002639
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002640 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002641 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002642 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2643 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002644 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2645 }
2646 }
2647
Wale Ogunwale494009b82016-10-21 09:01:38 -07002648 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002649 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002650 if (imFocus == null) {
2651 return false;
2652 }
2653
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002654 if (DEBUG_INPUT_METHOD) {
2655 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2656 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2657 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002658 }
2659
Wale Ogunwale494009b82016-10-21 09:01:38 -07002660 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2661
2662 if (DEBUG_INPUT_METHOD) {
2663 Slog.i(TAG_WM, "IM target client: " + imeClient);
2664 if (imeClient != null) {
2665 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2666 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2667 }
2668 }
2669
2670 return imeClient != null && imeClient.asBinder() == client.asBinder();
2671 }
2672
2673 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002674 final WindowState win = getWindow(
2675 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2676 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002677 }
2678
2679 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002680 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002681 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002682 final int curValue = w.mSystemUiVisibility;
2683 final int diff = (curValue ^ visibility) & globalDiff;
2684 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002685 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002686 w.mSeq++;
2687 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002688 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002689 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2690 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002691 visibility, newValue, diff);
2692 }
2693 } catch (RemoteException e) {
2694 // so sorry
2695 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002696 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002697 }
2698
2699 void onWindowFreezeTimeout() {
2700 Slog.w(TAG_WM, "Window freeze timeout expired.");
2701 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002702
2703 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002704 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002705 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002706 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002707 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002708 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2709 - mService.mDisplayFreezeTime);
2710 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002711 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002712 mService.mWindowPlacerLocked.performSurfacePlacement();
2713 }
2714
2715 void waitForAllWindowsDrawn() {
2716 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002717 forAllWindows(w -> {
2718 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2719 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2720 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002721 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002722 w.mLastContentInsets.set(-1, -1, -1, -1);
2723 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002724 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002725 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002726 }
2727
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002728 // TODO: Super crazy long method that should be broken down...
2729 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2730
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002731 final int dw = mDisplayInfo.logicalWidth;
2732 final int dh = mDisplayInfo.logicalHeight;
2733 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2734
2735 mTmpUpdateAllDrawn.clear();
2736
2737 int repeats = 0;
2738 do {
2739 repeats++;
2740 if (repeats > 6) {
2741 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2742 clearLayoutNeeded();
2743 break;
2744 }
2745
2746 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2747 pendingLayoutChanges);
2748
Andrii Kulian839def92016-11-02 10:58:58 -07002749 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2750 // the wallpaper window jumping across displays.
2751 // Remove check for default display when there will be support for multiple wallpaper
2752 // targets (on different displays).
2753 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002754 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002755 }
2756
2757 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2758 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2759 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2760 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002761 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002762 }
2763 }
2764
2765 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2766 setLayoutNeeded();
2767 }
2768
2769 // FIRST LOOP: Perform a layout, if needed.
2770 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2771 performLayout(repeats == 1, false /* updateInputWindows */);
2772 } else {
2773 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2774 }
2775
2776 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2777 pendingLayoutChanges = 0;
2778
2779 if (isDefaultDisplay) {
2780 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002781 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002782 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2783 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2784 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2785 }
2786 } while (pendingLayoutChanges != 0);
2787
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002788 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002789
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002790 mTmpRecoveringMemory = recoveringMemory;
2791 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01002792 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002793
2794 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002795 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2796 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2797 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002798 true /* inTraversal, must call performTraversalInTrans... below */);
2799
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002800 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2801 if (wallpaperVisible != mLastWallpaperVisible) {
2802 mLastWallpaperVisible = wallpaperVisible;
2803 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2804 }
2805
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002806 while (!mTmpUpdateAllDrawn.isEmpty()) {
2807 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2808 // See if any windows have been drawn, so they (and others associated with them)
2809 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002810 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002811 }
2812
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002813 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002814 }
2815
Bryce Leef3c6a472017-11-14 14:53:06 -08002816 private void updateBounds() {
2817 calculateBounds(mTmpBounds);
2818 setBounds(mTmpBounds);
2819 }
2820
2821 // Determines the current display bounds based on the current state
2822 private void calculateBounds(Rect out) {
2823 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
2824 final int orientation = mDisplayInfo.rotation;
2825 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2826 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
2827 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
2828 int width = mDisplayInfo.logicalWidth;
2829 int left = (physWidth - width) / 2;
2830 int height = mDisplayInfo.logicalHeight;
2831 int top = (physHeight - height) / 2;
2832 out.set(left, top, left + width, top + height);
2833 }
2834
2835 @Override
2836 public void getBounds(Rect out) {
2837 calculateBounds(out);
2838 }
2839
2840 private void getBounds(Rect out, int orientation) {
2841 getBounds(out);
2842
2843 // Rotate the Rect if needed.
2844 final int currentRotation = mDisplayInfo.rotation;
2845 final int rotationDelta = deltaRotation(currentRotation, orientation);
2846 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
2847 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
2848 mTmpRectF.set(out);
2849 mTmpMatrix.mapRect(mTmpRectF);
2850 mTmpRectF.round(out);
2851 }
2852 }
2853
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002854 void performLayout(boolean initial, boolean updateInputWindows) {
2855 if (!isLayoutNeeded()) {
2856 return;
2857 }
2858 clearLayoutNeeded();
2859
2860 final int dw = mDisplayInfo.logicalWidth;
2861 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002862 if (DEBUG_LAYOUT) {
2863 Slog.v(TAG, "-------------------------------------");
2864 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2865 }
2866
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002867 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
2868 // TODO: Not sure if we really need to set the rotation here since we are updating from the
2869 // display info above...
2870 mDisplayFrames.mRotation = mRotation;
2871 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002872 if (isDefaultDisplay) {
2873 // Not needed on non-default displays.
2874 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2875 mService.mScreenRect.set(0, 0, dw, dh);
2876 }
2877
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002878 int seq = mService.mLayoutSeq + 1;
2879 if (seq < 0) seq = 0;
2880 mService.mLayoutSeq = seq;
2881
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002882 // Used to indicate that we have processed the dream window and all additional windows are
2883 // behind it.
2884 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002885 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002886
2887 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002888 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002889
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002890 // Used to indicate that we have processed the dream window and all additional attached
2891 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002892 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002893 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002894
2895 // Now perform layout of attached windows, which usually depend on the position of the
2896 // window they are attached to. XXX does not deal with windows that are attached to windows
2897 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002898 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002899
2900 // Window frames may have changed. Tell the input dispatcher about it.
2901 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2902 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2903 if (updateInputWindows) {
2904 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2905 }
2906
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002907 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2908 }
2909
2910 /**
2911 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2912 * In portrait mode, it grabs the full screenshot.
2913 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002914 * @param config of the output bitmap
2915 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
2916 */
chaviwfbe47df2017-11-10 16:14:49 -08002917 Bitmap screenshotDisplay(Bitmap.Config config, boolean wallpaperOnly) {
2918 synchronized (mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02002919 if (!mService.mPolicy.isScreenOn()) {
chaviwfbe47df2017-11-10 16:14:49 -08002920 if (DEBUG_SCREENSHOT) {
2921 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002922 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002923 return null;
2924 }
2925
chaviwfbe47df2017-11-10 16:14:49 -08002926 if (wallpaperOnly && !shouldScreenshotWallpaper()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002927 return null;
2928 }
2929
chaviwfbe47df2017-11-10 16:14:49 -08002930 int dw = mDisplayInfo.logicalWidth;
2931 int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002932
chaviwfbe47df2017-11-10 16:14:49 -08002933 if (dw <= 0 || dh <= 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002934 return null;
2935 }
2936
chaviwfbe47df2017-11-10 16:14:49 -08002937 final Rect frame = new Rect(0, 0, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002938
2939 // The screenshot API does not apply the current screen rotation.
2940 int rot = mDisplay.getRotation();
2941
2942 if (rot == ROTATION_90 || rot == ROTATION_270) {
2943 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
2944 }
2945
chaviwfbe47df2017-11-10 16:14:49 -08002946 // SurfaceFlinger is not aware of orientation, so convert our logical
2947 // crop to SurfaceFlinger's portrait orientation.
2948 convertCropForSurfaceFlinger(frame, rot, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002949
2950 final ScreenRotationAnimation screenRotationAnimation =
2951 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
2952 final boolean inRotation = screenRotationAnimation != null &&
2953 screenRotationAnimation.isAnimating();
chaviwfbe47df2017-11-10 16:14:49 -08002954 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002955
Robert Carrb1579c82017-09-05 14:54:47 -07002956 // TODO(b/68392460): We should screenshot Task controls directly
2957 // but it's difficult at the moment as the Task doesn't have the
2958 // correct size set.
chaviwfbe47df2017-11-10 16:14:49 -08002959 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, 0, 1, inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002960 if (bitmap == null) {
Robert Carrb1579c82017-09-05 14:54:47 -07002961 Slog.w(TAG_WM, "Failed to take screenshot");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002962 return null;
2963 }
chaviwfbe47df2017-11-10 16:14:49 -08002964
2965 // Create a copy of the screenshot that is immutable and backed in ashmem.
2966 // This greatly reduces the overhead of passing the bitmap between processes.
2967 final Bitmap ret = bitmap.createAshmemBitmap(config);
2968 bitmap.recycle();
2969 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002970 }
chaviwfbe47df2017-11-10 16:14:49 -08002971 }
2972
2973 private boolean shouldScreenshotWallpaper() {
2974 MutableBoolean screenshotReady = new MutableBoolean(false);
2975
2976 forAllWindows(w -> {
2977 if (!w.mIsWallpaper) {
2978 return false;
2979 }
2980
2981 // Found the wallpaper window
2982 final WindowStateAnimator winAnim = w.mWinAnimator;
2983
2984 if (winAnim.getShown() && winAnim.mLastAlpha > 0f) {
2985 screenshotReady.value = true;
2986 }
2987
2988 return true;
2989 }, true /* traverseTopToBottom */);
2990
2991 return screenshotReady.value;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002992 }
2993
2994 // TODO: Can this use createRotationMatrix()?
2995 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
2996 if (rot == Surface.ROTATION_90) {
2997 final int tmp = crop.top;
2998 crop.top = dw - crop.right;
2999 crop.right = crop.bottom;
3000 crop.bottom = dw - crop.left;
3001 crop.left = tmp;
3002 } else if (rot == Surface.ROTATION_180) {
3003 int tmp = crop.top;
3004 crop.top = dh - crop.bottom;
3005 crop.bottom = dh - tmp;
3006 tmp = crop.right;
3007 crop.right = dw - crop.left;
3008 crop.left = dw - tmp;
3009 } else if (rot == Surface.ROTATION_270) {
3010 final int tmp = crop.top;
3011 crop.top = crop.left;
3012 crop.left = dh - crop.bottom;
3013 crop.bottom = crop.right;
3014 crop.right = dh - tmp;
3015 }
3016 }
3017
3018 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003019 // Used to indicate the layout is needed.
3020 mTmpWindow = null;
3021
3022 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003023 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003024 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003025 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003026 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003027 w.setDisplayLayoutNeeded();
3028 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003029 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003030
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003031 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003032 mService.mWindowPlacerLocked.performSurfacePlacement();
3033 }
3034 }
3035
Wale Ogunwale1666e312016-12-16 11:27:18 -08003036 void setExitingTokensHasVisible(boolean hasVisible) {
3037 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3038 mExitingTokens.get(i).hasVisible = hasVisible;
3039 }
3040
3041 // Initialize state of exiting applications.
3042 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3043 }
3044
3045 void removeExistingTokensIfPossible() {
3046 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3047 final WindowToken token = mExitingTokens.get(i);
3048 if (!token.hasVisible) {
3049 mExitingTokens.remove(i);
3050 }
3051 }
3052
3053 // Time to remove any exiting applications?
3054 mTaskStackContainers.removeExistingAppTokensIfPossible();
3055 }
3056
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003057 @Override
3058 void onDescendantOverrideConfigurationChanged() {
3059 setLayoutNeeded();
3060 mService.requestTraversal();
3061 }
3062
David Stevens9440dc82017-03-16 19:00:20 -07003063 boolean okToDisplay() {
3064 if (mDisplayId == DEFAULT_DISPLAY) {
3065 return !mService.mDisplayFrozen
3066 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3067 }
3068 return mDisplayInfo.state == Display.STATE_ON;
3069 }
3070
3071 boolean okToAnimate() {
3072 return okToDisplay() &&
3073 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3074 }
3075
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003076 static final class TaskForResizePointSearchResult {
3077 boolean searchDone;
3078 Task taskForResize;
3079
3080 void reset() {
3081 searchDone = false;
3082 taskForResize = null;
3083 }
3084 }
Robert Carr3b716242016-08-16 16:02:21 -07003085
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003086 private static final class ApplySurfaceChangesTransactionState {
3087 boolean displayHasContent;
3088 boolean obscured;
3089 boolean syswin;
3090 boolean focusDisplayed;
3091 float preferredRefreshRate;
3092 int preferredModeId;
3093
3094 void reset() {
3095 displayHasContent = false;
3096 obscured = false;
3097 syswin = false;
3098 focusDisplayed = false;
3099 preferredRefreshRate = 0;
3100 preferredModeId = 0;
3101 }
3102 }
3103
3104 private static final class ScreenshotApplicationState {
3105 WindowState appWin;
3106 int maxLayer;
3107 int minLayer;
3108 boolean screenshotReady;
3109
3110 void reset(boolean screenshotReady) {
3111 appWin = null;
3112 maxLayer = 0;
3113 minLayer = 0;
3114 this.screenshotReady = screenshotReady;
3115 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3116 }
3117 }
3118
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003119 /**
3120 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3121 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3122 * homogeneous children type which is currently required by sub-classes of
3123 * {@link WindowContainer} class.
3124 */
3125 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3126
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003127 DisplayChildWindowContainer(WindowManagerService service) {
3128 super(service);
3129 }
3130
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003131 @Override
3132 boolean fillsParent() {
3133 return true;
3134 }
3135
3136 @Override
3137 boolean isVisible() {
3138 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003139 }
3140 }
3141
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003142 /**
3143 * Window container class that contains all containers on this display relating to Apps.
3144 * I.e Activities.
3145 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003146 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003147 /**
3148 * A control placed at the appropriate level for transitions to occur.
3149 */
chaviw23ee71c2017-12-18 11:29:41 -08003150 SurfaceControl mAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003151
Wale Ogunwale61911492017-10-11 08:50:50 -07003152 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3153 // through the list to find them.
3154 private TaskStack mHomeStack = null;
3155 private TaskStack mPinnedStack = null;
3156 private TaskStack mSplitScreenPrimaryStack = null;
3157
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003158 TaskStackContainers(WindowManagerService service) {
3159 super(service);
3160 }
3161
Wale Ogunwale61911492017-10-11 08:50:50 -07003162 /**
3163 * Returns the topmost stack on the display that is compatible with the input windowing mode
3164 * and activity type. Null is no compatible stack on the display.
3165 */
3166 TaskStack getStack(int windowingMode, int activityType) {
3167 if (activityType == ACTIVITY_TYPE_HOME) {
3168 return mHomeStack;
3169 }
3170 if (windowingMode == WINDOWING_MODE_PINNED) {
3171 return mPinnedStack;
3172 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3173 return mSplitScreenPrimaryStack;
3174 }
3175 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3176 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003177 if (activityType == ACTIVITY_TYPE_UNDEFINED
3178 && windowingMode == stack.getWindowingMode()) {
3179 // Passing in undefined type means we want to match the topmost stack with the
3180 // windowing mode.
3181 return stack;
3182 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003183 if (stack.isCompatible(windowingMode, activityType)) {
3184 return stack;
3185 }
3186 }
3187 return null;
3188 }
3189
3190 @VisibleForTesting
3191 TaskStack getTopStack() {
3192 return mTaskStackContainers.getChildCount() > 0
3193 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3194 }
3195
3196 TaskStack getHomeStack() {
3197 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3198 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3199 }
3200 return mHomeStack;
3201 }
3202
3203 TaskStack getPinnedStack() {
3204 return mPinnedStack;
3205 }
3206
Matthew Ng64e77cf2017-10-31 14:01:31 -07003207 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003208 return mSplitScreenPrimaryStack;
3209 }
3210
Andrii Kulian839def92016-11-02 10:58:58 -07003211 /**
3212 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003213 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003214 */
3215 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003216 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003217 addChild(stack, onTop);
3218 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003219 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003220
Wale Ogunwale61911492017-10-11 08:50:50 -07003221 void onStackWindowingModeChanged(TaskStack stack) {
3222 removeStackReferenceIfNeeded(stack);
3223 addStackReferenceIfNeeded(stack);
3224 if (stack == mPinnedStack && getTopStack() != stack) {
3225 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3226 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3227 }
3228 }
3229
3230 private void addStackReferenceIfNeeded(TaskStack stack) {
3231 if (stack.isActivityTypeHome()) {
3232 if (mHomeStack != null) {
3233 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
3234 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
3235 }
3236 mHomeStack = stack;
3237 }
3238 final int windowingMode = stack.getWindowingMode();
3239 if (windowingMode == WINDOWING_MODE_PINNED) {
3240 if (mPinnedStack != null) {
3241 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3242 + mPinnedStack + " already exist on display=" + this
3243 + " stack=" + stack);
3244 }
3245 mPinnedStack = stack;
3246 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3247 if (mSplitScreenPrimaryStack != null) {
3248 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3249 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3250 + " already exist on display=" + this + " stack=" + stack);
3251 }
3252 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003253 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003254 }
3255 }
3256
3257 private void removeStackReferenceIfNeeded(TaskStack stack) {
3258 if (stack == mHomeStack) {
3259 mHomeStack = null;
3260 } else if (stack == mPinnedStack) {
3261 mPinnedStack = null;
3262 } else if (stack == mSplitScreenPrimaryStack) {
3263 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003264 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3265 mService.setDockedStackCreateStateLocked(
3266 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3267 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003268 }
Andrii Kulian839def92016-11-02 10:58:58 -07003269 }
3270
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003271 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003272 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3273 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003274 addChild(stack, addIndex);
3275 setLayoutNeeded();
3276 }
3277
Wale Ogunwale61911492017-10-11 08:50:50 -07003278 @Override
3279 protected void removeChild(TaskStack stack) {
3280 super.removeChild(stack);
3281 removeStackReferenceIfNeeded(stack);
3282 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003283
3284 @Override
3285 boolean isOnTop() {
3286 // Considered always on top
3287 return true;
3288 }
3289
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003290 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003291 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003292 if (child.getWindowConfiguration().isAlwaysOnTop()
3293 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003294 // This stack is always-on-top, override the default behavior.
3295 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3296
3297 // Moving to its current position, as we must call super but we don't want to
3298 // perform any meaningful action.
3299 final int currentPosition = mChildren.indexOf(child);
3300 super.positionChildAt(currentPosition, child, false /* includingParents */);
3301 return;
3302 }
3303
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003304 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3305 super.positionChildAt(targetPosition, child, includingParents);
3306
3307 setLayoutNeeded();
3308 }
3309
3310 /**
3311 * When stack is added or repositioned, find a proper position for it.
3312 * This will make sure that pinned stack always stays on top.
3313 * @param requestedPosition Position requested by caller.
3314 * @param stack Stack to be added or positioned.
3315 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3316 * @return The proper position for the stack.
3317 */
3318 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3319 final int topChildPosition = mChildren.size() - 1;
3320 boolean toTop = requestedPosition == POSITION_TOP;
3321 toTop |= adding ? requestedPosition >= topChildPosition + 1
3322 : requestedPosition >= topChildPosition;
3323 int targetPosition = requestedPosition;
3324
Wale Ogunwale61911492017-10-11 08:50:50 -07003325 if (toTop && stack.getWindowingMode() != WINDOWING_MODE_PINNED && hasPinnedStack()) {
Bryce Lee48f4b572017-04-10 10:54:15 -07003326 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003327 TaskStack topStack = mChildren.get(topChildPosition);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003328 if (topStack.getWindowingMode() != WINDOWING_MODE_PINNED) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003329 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3330 }
3331
3332 // So, stack is moved just below the pinned stack.
3333 // When we're adding a new stack the target is the current pinned stack position.
3334 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003335 // stack, because WindowContainer#positionAt() first removes element and then adds
3336 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003337 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3338 }
3339
3340 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003341 }
3342
3343 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003344 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3345 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003346 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003347 if (super.forAllWindows(callback, traverseTopToBottom)) {
3348 return true;
3349 }
3350 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3351 return true;
3352 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003353 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003354 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3355 return true;
3356 }
3357 if (super.forAllWindows(callback, traverseTopToBottom)) {
3358 return true;
3359 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003360 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003361 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003362 }
3363
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003364 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003365 boolean traverseTopToBottom) {
3366 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3367 // app tokens.
3368 // TODO: Investigate if we need to continue to do this or if we can just process them
3369 // in-order.
3370 if (traverseTopToBottom) {
3371 for (int i = mChildren.size() - 1; i >= 0; --i) {
3372 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3373 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003374 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3375 traverseTopToBottom)) {
3376 return true;
3377 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003378 }
3379 }
3380 } else {
3381 final int count = mChildren.size();
3382 for (int i = 0; i < count; ++i) {
3383 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3384 final int appTokensCount = appTokens.size();
3385 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003386 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3387 traverseTopToBottom)) {
3388 return true;
3389 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003390 }
3391 }
3392 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003393 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003394 }
3395
Wale Ogunwale1666e312016-12-16 11:27:18 -08003396 void setExitingTokensHasVisible(boolean hasVisible) {
3397 for (int i = mChildren.size() - 1; i >= 0; --i) {
3398 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3399 for (int j = appTokens.size() - 1; j >= 0; --j) {
3400 appTokens.get(j).hasVisible = hasVisible;
3401 }
3402 }
3403 }
3404
3405 void removeExistingAppTokensIfPossible() {
3406 for (int i = mChildren.size() - 1; i >= 0; --i) {
3407 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3408 for (int j = appTokens.size() - 1; j >= 0; --j) {
3409 final AppWindowToken token = appTokens.get(j);
3410 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3411 && (!token.mIsExiting || token.isEmpty())) {
3412 // Make sure there is no animation running on this token, so any windows
3413 // associated with it will be removed as soon as their animations are
3414 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003415 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003416 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3417 "performLayout: App token exiting now removed" + token);
3418 token.removeIfPossible();
3419 }
3420 }
3421 }
3422 }
3423
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003424 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003425 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003426 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3427 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003428 // Apps and their containers are not allowed to specify an orientation while the
3429 // docked or freeform stack is visible...except for the home stack/task if the
3430 // docked stack is minimized and it actually set something.
3431 if (mHomeStack != null && mHomeStack.isVisible()
3432 && mDividerControllerLocked.isMinimizedDock()) {
3433 final int orientation = mHomeStack.getOrientation();
3434 if (orientation != SCREEN_ORIENTATION_UNSET) {
3435 return orientation;
3436 }
3437 }
3438 return SCREEN_ORIENTATION_UNSPECIFIED;
3439 }
3440
3441 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003442 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3443 PackageManager.FEATURE_AUTOMOTIVE);
3444 if (isCar) {
3445 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3446 // allow anything but the default orientation.
3447 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3448 "Forcing UNSPECIFIED orientation in car. Ignoring " + orientation);
3449 return SCREEN_ORIENTATION_UNSPECIFIED;
3450 }
3451
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003452 if (orientation != SCREEN_ORIENTATION_UNSET
3453 && orientation != SCREEN_ORIENTATION_BEHIND) {
3454 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3455 "App is requesting an orientation, return " + orientation);
3456 return orientation;
3457 }
3458
3459 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003460 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003461 // The next app has not been requested to be visible, so we keep the current orientation
3462 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003463 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003464 }
Robert Carrb1579c82017-09-05 14:54:47 -07003465
3466 @Override
3467 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrd92e7eb2017-12-06 13:17:07 -08003468
3469 final int NORMAL_STACK_STATE = 0;
3470 final int SPLIT_SCREEN_STACK_STATE = 1;
3471 final int ASSISTANT_STACK_STATE = 2;
3472 final int BOOSTED_STATE = 3;
3473 final int ALWAYS_ON_TOP_STATE = 4;
3474
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003475 int layer = 0;
Robert Carrd92e7eb2017-12-06 13:17:07 -08003476 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
3477 for (int i = 0; i < mChildren.size(); i++) {
3478 final TaskStack s = mChildren.get(i);
3479 layer++;
3480 if (state == NORMAL_STACK_STATE && !s.inSplitScreenPrimaryWindowingMode() &&
3481 !s.isActivityTypeAssistant() &&
3482 !s.needsZBoost() && !s.isAlwaysOnTop()) {
3483 s.assignLayer(t, layer);
3484 } else if (state == SPLIT_SCREEN_STACK_STATE &&
3485 s.inSplitScreenPrimaryWindowingMode()) {
3486 s.assignLayer(t, layer);
3487 } else if (state == ASSISTANT_STACK_STATE &&
3488 s.isActivityTypeAssistant()) {
3489 s.assignLayer(t, layer);
3490 } else if (state == BOOSTED_STATE && s.needsZBoost()) {
3491 s.assignLayer(t, layer);
3492 } else if (state == ALWAYS_ON_TOP_STATE &&
3493 s.isAlwaysOnTop()) {
3494 s.assignLayer(t, layer);
3495 }
3496 }
3497 // The appropriate place for App-Transitions to occur is right
3498 // above all other animations but still below things in the Picture-and-Picture
3499 // windowing mode.
3500 if (state == BOOSTED_STATE && mAppAnimationLayer != null) {
3501 t.setLayer(mAppAnimationLayer, layer++);
Robert Carrb1579c82017-09-05 14:54:47 -07003502 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003503 }
3504 for (int i = 0; i < mChildren.size(); i++) {
3505 final TaskStack s = mChildren.get(i);
Robert Carrd92e7eb2017-12-06 13:17:07 -08003506 s.assignChildLayers(t);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003507 }
3508
Robert Carrb1579c82017-09-05 14:54:47 -07003509 }
3510
3511 @Override
chaviw23ee71c2017-12-18 11:29:41 -08003512 SurfaceControl getAppAnimationLayer() {
3513 return mAppAnimationLayer;
3514 }
3515
3516 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07003517 void onParentSet() {
3518 super.onParentSet();
3519 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08003520 mAppAnimationLayer = makeChildSurface(null)
3521 .setName("animationLayer")
3522 .build();
3523 getPendingTransaction().show(mAppAnimationLayer);
3524 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07003525 } else {
chaviw23ee71c2017-12-18 11:29:41 -08003526 mAppAnimationLayer.destroy();
3527 mAppAnimationLayer = null;
Robert Carrb1579c82017-09-05 14:54:47 -07003528 }
3529 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003530 }
3531
Robert Carree4d4b92017-11-22 12:21:46 -08003532 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003533 AboveAppWindowContainers(String name, WindowManagerService service) {
3534 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08003535 }
3536
3537 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
3538 boolean needAssignIme = imeContainer != null
3539 && imeContainer.getSurfaceControl() != null;
3540 for (int j = 0; j < mChildren.size(); ++j) {
3541 final WindowToken wt = mChildren.get(j);
Robert Carr11408822018-01-03 18:07:51 -08003542
3543 // The divider is unique in that it does not have an AppWindowToken but needs to be
3544 // interleaved with them. In particular it must be above any split-screen stacks
3545 // but below any always-on-top stacks.
3546 if (wt.windowType == TYPE_DOCK_DIVIDER) {
3547 final TaskStack dockedStack = getSplitScreenPrimaryStack();
3548 if (dockedStack != null) {
3549 wt.assignRelativeLayer(t, dockedStack.getSurfaceControl(),
3550 Integer.MAX_VALUE);
3551 continue;
3552 }
3553 }
Robert Carree4d4b92017-11-22 12:21:46 -08003554 wt.assignLayer(t, j);
3555 wt.assignChildLayers(t);
3556
3557 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
3558 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08003559
3560 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
3561 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003562 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08003563 needAssignIme = false;
3564 }
3565 }
3566 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003567 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08003568 }
3569 }
3570 }
3571
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003572 /**
3573 * Window container class that contains all containers on this display that are not related to
3574 * Apps. E.g. status bar.
3575 */
Robert Carree4d4b92017-11-22 12:21:46 -08003576 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07003577 /**
3578 * Compares two child window tokens returns -1 if the first is lesser than the second in
3579 * terms of z-order and 1 otherwise.
3580 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003581 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003582 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003583 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3584 token1.mOwnerCanManageAppTokens)
3585 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3586 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003587
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003588 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3589 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3590 return false;
3591 }
3592 final int req = w.mAttrs.screenOrientation;
3593 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3594 || req == SCREEN_ORIENTATION_UNSET) {
3595 return false;
3596 }
3597 return true;
3598 };
3599
Wale Ogunwale3a931692016-11-02 16:49:48 -07003600 private final String mName;
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003601 NonAppWindowContainers(String name, WindowManagerService service) {
3602 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003603 mName = name;
3604 }
3605
3606 void addChild(WindowToken token) {
3607 addChild(token, mWindowComparator);
3608 }
3609
3610 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003611 int getOrientation() {
3612 final WindowManagerPolicy policy = mService.mPolicy;
3613 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003614 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003615
3616 if (win != null) {
3617 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003618 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003619 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003620 if (mService.mKeyguardGoingAway) {
3621 // Keyguard can't affect the orientation if it is going away...
3622 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3623 return SCREEN_ORIENTATION_UNSET;
3624 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003625 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003626 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003627 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003628 }
3629
Andrii Kulian8ee72852017-03-10 10:36:45 -08003630 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003631
Jorim Jaggi75520d52017-08-24 17:23:19 +02003632 if (policy.isKeyguardShowingAndNotOccluded()
3633 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003634 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003635 }
3636
3637 return SCREEN_ORIENTATION_UNSET;
3638 }
3639
3640 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003641 String getName() {
3642 return mName;
3643 }
Robert Carr3b716242016-08-16 16:02:21 -07003644 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003645
Robert Carr9034d962018-01-04 18:27:42 -08003646 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
3647 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
3648 super(name, service);
3649 }
3650
3651 @Override
3652 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
3653 }
3654 };
3655
Robert Carrb1579c82017-09-05 14:54:47 -07003656 SurfaceControl.Builder makeSurface(SurfaceSession s) {
3657 return mService.makeSurfaceBuilder(s)
3658 .setParent(mWindowingLayer);
3659 }
3660
3661 @Override
3662 SurfaceSession getSession() {
3663 return mSession;
3664 }
3665
3666 @Override
3667 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003668 SurfaceSession s = child != null ? child.getSession() : getSession();
3669 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07003670 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07003671
3672 if (child == null) {
3673 return b;
3674 }
3675
Robert Carree4d4b92017-11-22 12:21:46 -08003676 return b.setName(child.getName())
3677 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07003678 }
3679
3680 /**
3681 * The makeSurface variants are for use by the window-container
3682 * hierarchy. makeOverlay here is a function for various non windowing
3683 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
3684 * and other potpourii.
3685 */
3686 SurfaceControl.Builder makeOverlay() {
3687 return mService.makeSurfaceBuilder(mSession)
3688 .setParent(mOverlayLayer);
3689 }
3690
3691 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003692 applyMagnificationSpec(getPendingTransaction(), spec);
3693 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07003694 }
3695
3696 @Override
3697 void onParentSet() {
3698 // Since we are the top of the SurfaceControl hierarchy here
3699 // we create the root surfaces explicitly rather than chaining
3700 // up as the default implementation in onParentSet does. So we
3701 // explicitly do NOT call super here.
3702 }
3703
3704 @Override
3705 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07003706
3707 // These are layers as children of "mWindowingLayer"
3708 mBelowAppWindowsContainers.assignLayer(t, 0);
3709 mTaskStackContainers.assignLayer(t, 1);
3710 mAboveAppWindowsContainers.assignLayer(t, 2);
3711
3712 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08003713 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08003714
Robert Carree4d4b92017-11-22 12:21:46 -08003715 // In the case where we have an IME target that is not in split-screen
3716 // mode IME assignment is easy. We just need the IME to go directly above
3717 // the target. This way children of the target will naturally go above the IME
3718 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08003719 //
Robert Carree4d4b92017-11-22 12:21:46 -08003720 // In the case of split-screen windowing mode, we need to elevate the IME above the
3721 // docked divider while keeping the app itself below the docked divider, so instead
3722 // we use relative layering of the IME targets child windows, and place the
3723 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08003724 //
Robert Carree4d4b92017-11-22 12:21:46 -08003725 // In the case where we have no IME target we assign it where it's base layer would
3726 // place it in the AboveAppWindowContainers.
3727 if (imeTarget != null && !imeTarget.inSplitScreenWindowingMode()
3728 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003729 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07003730 // TODO: We need to use an extra level on the app surface to ensure
3731 // this is always above SurfaceView but always below attached window.
3732 1);
Robert Carree4d4b92017-11-22 12:21:46 -08003733 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07003734 }
3735
3736 // Above we have assigned layers to our children, now we ask them to assign
3737 // layers to their children.
3738 mBelowAppWindowsContainers.assignChildLayers(t);
3739 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08003740 mAboveAppWindowsContainers.assignChildLayers(t,
3741 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07003742 mImeWindowsContainers.assignChildLayers(t);
3743 }
3744
3745 /**
3746 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
3747 * that the IME target is one of the docked applications. We'd like the docked divider to be
3748 * above both of the applications, and we'd like the IME to be above the docked divider.
3749 * However we need child windows of the applications to be above the IME (Text drag handles).
3750 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
3751 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
3752 * with {@link #WindowState#assignLayer}
3753 */
3754 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
3755 t.setRelativeLayer(child.getSurfaceControl(), mImeWindowsContainers.getSurfaceControl(), 1);
3756 }
3757
3758 @Override
Jorim Jaggia5e10572017-11-15 14:36:26 +01003759 public void destroyAfterPendingTransaction(SurfaceControl surface) {
Robert Carrb1579c82017-09-05 14:54:47 -07003760 mPendingDestroyingSurfaces.add(surface);
3761 }
3762
3763 /**
3764 * Destroys any surfaces that have been put into the pending list with
Jorim Jaggia5e10572017-11-15 14:36:26 +01003765 * {@link #destroyAfterPendingTransaction}.
Robert Carrb1579c82017-09-05 14:54:47 -07003766 */
3767 void onPendingTransactionApplied() {
3768 for (int i = mPendingDestroyingSurfaces.size() - 1; i >= 0; i--) {
3769 mPendingDestroyingSurfaces.get(i).destroy();
3770 }
3771 mPendingDestroyingSurfaces.clear();
3772 }
Jorim Jaggi9af095b2017-12-12 17:18:57 +01003773
3774 @Override
3775 void prepareSurfaces() {
3776 final ScreenRotationAnimation screenRotationAnimation =
3777 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
3778 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
3779 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
3780 mPendingTransaction.setMatrix(mWindowingLayer,
3781 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
3782 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
3783 mPendingTransaction.setPosition(mWindowingLayer,
3784 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
3785 mPendingTransaction.setAlpha(mWindowingLayer,
3786 screenRotationAnimation.getEnterTransformation().getAlpha());
3787 }
3788 super.prepareSurfaces();
3789 }
Craig Mautner59c00972012-07-30 12:10:24 -07003790}