blob: eda8fec7ab3920860d051df361e884f81558741a [file] [log] [blame]
Craig Mautner59c00972012-07-30 12:10:24 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
Wale Ogunwale30e441d2017-11-09 08:28:45 -080019import static android.app.ActivityManager.SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT;
Wale Ogunwale61911492017-10-11 08:50:50 -070020import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070021import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070022import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwale687b4272017-07-27 02:56:23 -070023import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070024import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
25import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070026import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070027import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
28import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale687b4272017-07-27 02:56:23 -070029import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
30import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070031import static android.view.Display.DEFAULT_DISPLAY;
32import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070033import static android.view.Surface.ROTATION_0;
34import static android.view.Surface.ROTATION_180;
35import static android.view.Surface.ROTATION_270;
36import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070037import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070038import static android.view.WindowManager.DOCKED_BOTTOM;
39import static android.view.WindowManager.DOCKED_INVALID;
40import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080041import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
42import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
43import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070044import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070045import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070046import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
47import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070048import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070049import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070050import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070051import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Wale Ogunwale494009b82016-10-21 09:01:38 -070052import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070053import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070054import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
55import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070056import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070057import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
58import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070059import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070060import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Adrian Roose99bc052017-11-20 17:55:31 +010061import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
62import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
63import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
64import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Jorim Jaggi75520d52017-08-24 17:23:19 +020065import static com.android.server.wm.AppTransition.TRANSIT_KEYGUARD_UNOCCLUDE;
Wale Ogunwale1666e312016-12-16 11:27:18 -080066import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070067import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070069import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
70import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwalec69694a2016-10-18 13:51:15 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070076import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070077import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070078import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080079import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070080import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070081import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070082import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070083import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070084import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070085import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080086import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -070087import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070088import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
89import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070090import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070091import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -070092import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
93import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070094import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER;
95import static com.android.server.wm.WindowManagerService.TYPE_LAYER_OFFSET;
96import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -070097import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070098import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070099import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700100import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700101import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700102import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700103import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700104import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700105import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700106import static com.android.server.wm.proto.DisplayProto.ABOVE_APP_WINDOWS;
107import static com.android.server.wm.proto.DisplayProto.BELOW_APP_WINDOWS;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000108import static com.android.server.wm.proto.DisplayProto.DISPLAY_FRAMES;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700109import static com.android.server.wm.proto.DisplayProto.DISPLAY_INFO;
110import static com.android.server.wm.proto.DisplayProto.DOCKED_STACK_DIVIDER_CONTROLLER;
111import static com.android.server.wm.proto.DisplayProto.DPI;
112import static com.android.server.wm.proto.DisplayProto.ID;
113import static com.android.server.wm.proto.DisplayProto.IME_WINDOWS;
114import static com.android.server.wm.proto.DisplayProto.PINNED_STACK_CONTROLLER;
Steven Timotiusf2d68892017-08-28 17:00:01 -0700115import static com.android.server.wm.proto.DisplayProto.ROTATION;
116import static com.android.server.wm.proto.DisplayProto.SCREEN_ROTATION_ANIMATION;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700117import static com.android.server.wm.proto.DisplayProto.STACKS;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700118import static com.android.server.wm.proto.DisplayProto.WINDOW_CONTAINER;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700119
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700120import android.annotation.CallSuper;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700121import android.annotation.NonNull;
Dean Harding3e5a1522017-10-06 09:19:01 -0700122import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700123import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700124import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700125import android.graphics.Bitmap;
Jorim Jaggi6a7a8592017-01-12 00:44:33 +0100126import android.graphics.GraphicBuffer;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700127import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800128import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700129import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700130import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100131import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700132import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700133import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700134import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700135import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700136import android.os.RemoteException;
137import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100138import android.os.Trace;
Chong Zhang8e89b312015-09-09 15:09:30 -0700139import android.util.DisplayMetrics;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800140import android.util.MutableBoolean;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700141import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700142import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700143import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700144import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700145import android.view.InputDevice;
Robert Carrb1579c82017-09-05 14:54:47 -0700146import android.view.MagnificationSpec;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700147import android.view.Surface;
148import android.view.SurfaceControl;
Robert Carrb1579c82017-09-05 14:54:47 -0700149import android.view.SurfaceSession;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700150
Bryce Lee48f4b572017-04-10 10:54:15 -0700151import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800152import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700153import com.android.internal.view.IInputMethodClient;
Adrian Roose99bc052017-11-20 17:55:31 +0100154import com.android.server.policy.WindowManagerPolicy;
Craig Mautner59c00972012-07-30 12:10:24 -0700155
Robert Carr3b716242016-08-16 16:02:21 -0700156import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700157import java.io.PrintWriter;
158import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700159import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700160import java.util.HashMap;
161import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700162import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700163import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800164import java.util.function.Consumer;
165import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700166
Craig Mautner59c00972012-07-30 12:10:24 -0700167/**
168 * Utility class for keeping track of the WindowStates and other pertinent contents of a
169 * particular Display.
170 *
171 * IMPORTANT: No method from this class should ever be used without holding
172 * WindowManagerService.mWindowMap.
173 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700174class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700175 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700176
177 /** Unique identifier of this stack. */
178 private final int mDisplayId;
179
Wale Ogunwale3a931692016-11-02 16:49:48 -0700180 /** The containers below are the only child containers the display can have. */
181 // Contains all window containers that are related to apps (Activities)
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100182 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700183 // Contains all non-app window containers that should be displayed above the app containers
184 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800185 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100186 new AboveAppWindowContainers("mAboveAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700187 // Contains all non-app window containers that should be displayed below the app containers
188 // (e.g. Wallpaper).
189 private final NonAppWindowContainers mBelowAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100190 new NonAppWindowContainers("mBelowAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700191 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
192 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
193 // window containers together and move them in-sync if/when needed.
194 private final NonAppWindowContainers mImeWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100195 new NonAppWindowContainers("mImeWindowsContainers", mService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700196
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000197 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800198 private WindowState mTmpWindow2;
199 private WindowAnimator mTmpWindowAnimator;
200 private boolean mTmpRecoveringMemory;
201 private boolean mUpdateImeTarget;
202 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700203 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700204
Wale Ogunwale02319a62016-09-26 15:21:22 -0700205 // Mapping from a token IBinder to a WindowToken object on this display.
206 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
207
Andrii Kuliancd097992017-03-23 18:31:59 -0700208 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700209 int mInitialDisplayWidth = 0;
210 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700211 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700212
213 /**
214 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
215 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
216 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
217 */
Craig Mautner59c00972012-07-30 12:10:24 -0700218 int mBaseDisplayWidth = 0;
219 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700220 /**
221 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
222 * but can be set from Settings or via shell command "adb shell wm density".
223 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
224 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700225 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700226 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700227 private final DisplayInfo mDisplayInfo = new DisplayInfo();
228 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700229 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000230 DisplayFrames mDisplayFrames;
231
Andrii Kulian06d07d62017-03-14 11:11:47 -0700232 /**
233 * For default display it contains real metrics, empty for others.
234 * @see WindowManagerService#createWatermarkInTransaction()
235 */
236 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
237 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
238 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700239
Andrii Kulian06d07d62017-03-14 11:11:47 -0700240 /**
241 * Compat metrics computed based on {@link #mDisplayMetrics}.
242 * @see #updateDisplayAndOrientation(int)
243 */
244 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
245
246 /** The desired scaling factor for compatible apps. */
247 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700248
Andrii Kulian8ee72852017-03-10 10:36:45 -0800249 /**
250 * Current rotation of the display.
251 * Constants as per {@link android.view.Surface.Rotation}.
252 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700253 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800254 */
255 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700256
Andrii Kulian8ee72852017-03-10 10:36:45 -0800257 /**
258 * Last applied orientation of the display.
259 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
260 *
261 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
262 */
263 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700264
Andrii Kulian8ee72852017-03-10 10:36:45 -0800265 /**
266 * Flag indicating that the application is receiving an orientation that has different metrics
267 * than it expected. E.g. Portrait instead of Landscape.
268 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700269 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800270 */
271 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700272
Andrii Kulian8ee72852017-03-10 10:36:45 -0800273 /**
274 * Orientation forced by some window. If there is no visible window that specifies orientation
275 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
276 *
277 * @see NonAppWindowContainers#getOrientation()
278 */
279 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700280
Andrii Kulian8ee72852017-03-10 10:36:45 -0800281 /**
282 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
283 * occluded.
284 *
285 * @see NonAppWindowContainers#getOrientation()
286 */
287 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
288
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700289 /**
290 * Keep track of wallpaper visibility to notify changes.
291 */
292 private boolean mLastWallpaperVisible = false;
293
Andrii Kulian06d07d62017-03-14 11:11:47 -0700294 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700295
Craig Mautner39834192012-09-02 07:47:24 -0700296 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700297 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700298 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700299 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800300 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700301
Craig Mautnerdc548482014-02-05 13:35:24 -0800302 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700303 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800304
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700305 /** Detect user tapping outside of current focused task bounds .*/
306 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700307
Craig Mautner6601b7b2013-04-29 10:29:11 -0700308 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700309 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700310
Craig Mautner6601b7b2013-04-29 10:29:11 -0700311 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800312 private final Rect mTmpRect = new Rect();
313 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700314 private final RectF mTmpRectF = new RectF();
315 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800316 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700317
Bryce Leef3c6a472017-11-14 14:53:06 -0800318 /** Used for handing back size of display */
319 private final Rect mTmpBounds = new Rect();
320
Craig Mautner95da1082014-02-24 17:54:35 -0800321 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700322 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800323
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700324 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700325 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700326
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800327 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
328
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000329 private boolean mHaveBootMsg = false;
330 private boolean mHaveApp = false;
331 private boolean mHaveWallpaper = false;
332 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700333
334 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
335
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700336 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
337 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000338 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
339 new ApplySurfaceChangesTransactionState();
340 private final ScreenshotApplicationState mScreenshotApplicationState =
341 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700342
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700343 // True if this display is in the process of being removed. Used to determine if the removal of
344 // the display's direct children should be allowed.
345 private boolean mRemovingDisplay = false;
346
Bryce Leed1871262017-06-12 14:12:29 -0700347 // {@code false} if this display is in the processing of being created.
348 private boolean mDisplayReady = false;
349
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800350 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700351 int mInputMethodAnimLayerAdjustment;
352
Robert Carrb1579c82017-09-05 14:54:47 -0700353 private final SurfaceSession mSession = new SurfaceSession();
354
355 /**
356 * We organize all top-level Surfaces in to the following layers.
357 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800358 * and omitted from Screen-Magnification, for example the strict mode flash or
359 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700360 * {@link #mWindowingLayer} contains everything else.
361 */
362 private SurfaceControl mOverlayLayer;
363
364 /**
365 * See {@link #mOverlayLayer}
366 */
367 private SurfaceControl mWindowingLayer;
368
369 /**
370 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
371 * <p>
372 * For these surfaces currently we use a surface based on the larger of width or height so we
373 * don't have to resize when rotating the display.
374 */
375 private int mSurfaceSize;
376
377 /**
378 * A list of surfaces to be destroyed after {@link #mPendingTransaction} is applied.
379 */
380 private final ArrayList<SurfaceControl> mPendingDestroyingSurfaces = new ArrayList<>();
381
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800382 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
383 WindowStateAnimator winAnimator = w.mWinAnimator;
384 if (winAnimator.hasSurface()) {
385 final boolean wasAnimating = winAnimator.mWasAnimating;
386 final boolean nowAnimating = winAnimator.stepAnimationLocked(
387 mTmpWindowAnimator.mCurrentTime);
388 winAnimator.mWasAnimating = nowAnimating;
389 mTmpWindowAnimator.orAnimating(nowAnimating);
390
391 if (DEBUG_WALLPAPER) Slog.v(TAG,
392 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
393
394 if (wasAnimating && !winAnimator.mAnimating
395 && mWallpaperController.isWallpaperTarget(w)) {
396 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
397 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
398 if (DEBUG_LAYOUT_REPEATS) {
399 mService.mWindowPlacerLocked.debugLayoutRepeats(
400 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
401 }
402 }
403 }
404
405 final AppWindowToken atoken = w.mAppToken;
406 if (winAnimator.mDrawState == READY_TO_SHOW) {
407 if (atoken == null || atoken.allDrawn) {
408 if (w.performShowLocked()) {
409 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
410 if (DEBUG_LAYOUT_REPEATS) {
411 mService.mWindowPlacerLocked.debugLayoutRepeats(
412 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
413 }
414 }
415 }
416 }
417 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
418 if (appAnimator != null && appAnimator.thumbnail != null) {
419 if (appAnimator.thumbnailTransactionSeq
420 != mTmpWindowAnimator.mAnimTransactionSequence) {
421 appAnimator.thumbnailTransactionSeq =
422 mTmpWindowAnimator.mAnimTransactionSequence;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800423 }
424 }
425 };
426
427 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
428 final WindowStateAnimator winAnimator = w.mWinAnimator;
429 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
430 return;
431 }
432
433 final int flags = w.mAttrs.flags;
434
435 // If this window is animating, make a note that we have an animating window and take
436 // care of a request to run a detached wallpaper animation.
437 if (winAnimator.mAnimating) {
438 if (winAnimator.mAnimation != null) {
439 if ((flags & FLAG_SHOW_WALLPAPER) != 0
440 && winAnimator.mAnimation.getDetachWallpaper()) {
441 mTmpWindow = w;
442 }
443 final int color = winAnimator.mAnimation.getBackgroundColor();
444 if (color != 0) {
445 final TaskStack stack = w.getStack();
446 if (stack != null) {
447 stack.setAnimationBackground(winAnimator, color);
448 }
449 }
450 }
451 mTmpWindowAnimator.setAnimating(true);
452 }
453
454 // If this window's app token is running a detached wallpaper animation, make a note so
455 // we can ensure the wallpaper is displayed behind it.
456 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
457 if (appAnimator != null && appAnimator.animation != null
458 && appAnimator.animating) {
459 if ((flags & FLAG_SHOW_WALLPAPER) != 0
460 && appAnimator.animation.getDetachWallpaper()) {
461 mTmpWindow = w;
462 }
463
464 final int color = appAnimator.animation.getBackgroundColor();
465 if (color != 0) {
466 final TaskStack stack = w.getStack();
467 if (stack != null) {
468 stack.setAnimationBackground(winAnimator, color);
469 }
470 }
471 }
472 };
473
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800474 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
475 final int lostFocusUid = mTmpWindow.mOwnerUid;
476 final Handler handler = mService.mH;
477 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
478 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
479 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
480 w.mAttrs.hideTimeoutMilliseconds);
481 }
482 }
483 };
484
485 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
486 final AppWindowToken focusedApp = mService.mFocusedApp;
487 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
488 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
489
490 if (!w.canReceiveKeys()) {
491 return false;
492 }
493
494 final AppWindowToken wtoken = w.mAppToken;
495
496 // If this window's application has been removed, just skip it.
497 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
498 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
499 + (wtoken.removed ? "removed" : "sendingToBottom"));
500 return false;
501 }
502
503 if (focusedApp == null) {
504 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
505 + " using new focus @ " + w);
506 mTmpWindow = w;
507 return true;
508 }
509
510 if (!focusedApp.windowsAreFocusable()) {
511 // Current focused app windows aren't focusable...
512 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
513 + " focusable using new focus @ " + w);
514 mTmpWindow = w;
515 return true;
516 }
517
518 // Descend through all of the app tokens and find the first that either matches
519 // win.mAppToken (return win) or mFocusedApp (return null).
520 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
521 if (focusedApp.compareTo(wtoken) > 0) {
522 // App stack below focused app stack. No focus for you!!!
523 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
524 "findFocusedWindow: Reached focused app=" + focusedApp);
525 mTmpWindow = null;
526 return true;
527 }
528 }
529
530 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
531 mTmpWindow = w;
532 return true;
533 };
534
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800535 private final Consumer<WindowState> mPerformLayout = w -> {
536 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
537 // wasting time and funky changes while a window is animating away.
538 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
539 || w.isGoneForLayoutLw();
540
541 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
542 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
543 + " mLayoutAttached=" + w.mLayoutAttached
544 + " screen changed=" + w.isConfigChanged());
545 final AppWindowToken atoken = w.mAppToken;
546 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
547 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
548 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
549 + " parentHidden=" + w.isParentWindowHidden());
550 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
551 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
552 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
553 + " parentHidden=" + w.isParentWindowHidden());
554 }
555
556 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
557 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
558 // since that means "perform layout as normal, just don't display").
559 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
560 || ((w.isConfigChanged() || w.setReportResizeHints())
561 && !w.isGoneForLayoutLw() &&
562 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
563 (w.mHasSurface && w.mAppToken != null &&
564 w.mAppToken.layoutConfigChanges)))) {
565 if (!w.mLayoutAttached) {
566 if (mTmpInitial) {
567 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
568 w.mContentChanged = false;
569 }
570 if (w.mAttrs.type == TYPE_DREAM) {
571 // Don't layout windows behind a dream, so that if it does stuff like hide
572 // the status bar we won't get a bad transition when it goes away.
573 mTmpWindow = w;
574 }
575 w.mLayoutNeeded = false;
576 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200577 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000578 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800579 w.mLayoutSeq = mService.mLayoutSeq;
580
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200581 // If this is the first layout, we need to initialize the last inset values as
582 // otherwise we'd immediately cause an unnecessary resize.
583 if (firstLayout) {
584 w.updateLastInsetValues();
585 }
586
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800587 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
588 + " mContainingFrame=" + w.mContainingFrame
589 + " mDisplayFrame=" + w.mDisplayFrame);
590 }
591 }
592 };
593
594 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
595 if (w.mLayoutAttached) {
596 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
597 + " mViewVisibility=" + w.mViewVisibility
598 + " mRelayoutCalled=" + w.mRelayoutCalled);
599 // If this view is GONE, then skip it -- keep the current frame, and let the caller
600 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
601 // windows, since that means "perform layout as normal, just don't display").
602 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
603 return;
604 }
605 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
606 || w.mLayoutNeeded) {
607 if (mTmpInitial) {
608 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
609 w.mContentChanged = false;
610 }
611 w.mLayoutNeeded = false;
612 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000613 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800614 w.mLayoutSeq = mService.mLayoutSeq;
615 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
616 + " mContainingFrame=" + w.mContainingFrame
617 + " mDisplayFrame=" + w.mDisplayFrame);
618 }
619 } else if (w.mAttrs.type == TYPE_DREAM) {
620 // Don't layout windows behind a dream, so that if it does stuff like hide the
621 // status bar we won't get a bad transition when it goes away.
622 mTmpWindow = mTmpWindow2;
623 }
624 };
625
626 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
627 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
628 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
629 return w.canBeImeTarget();
630 };
631
632 private final Consumer<WindowState> mApplyPostLayoutPolicy =
633 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
634 mService.mInputMethodTarget);
635
636 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
637 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
638 final boolean obscuredChanged = w.mObscured !=
639 mTmpApplySurfaceChangesTransactionState.obscured;
640 final RootWindowContainer root = mService.mRoot;
641 // Only used if default window
642 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
643
644 // Update effect.
645 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
646 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
647 final boolean isDisplayed = w.isDisplayedLw();
648
649 if (isDisplayed && w.isObscuringDisplay()) {
650 // This window completely covers everything behind it, so we want to leave all
651 // of them as undimmed (for performance reasons).
652 root.mObscuringWindow = w;
653 mTmpApplySurfaceChangesTransactionState.obscured = true;
654 }
655
656 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
657 root.handleNotObscuredLocked(w,
658 mTmpApplySurfaceChangesTransactionState.obscured,
659 mTmpApplySurfaceChangesTransactionState.syswin);
660
661 if (w.mHasSurface && isDisplayed) {
662 final int type = w.mAttrs.type;
663 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
664 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
665 mTmpApplySurfaceChangesTransactionState.syswin = true;
666 }
667 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
668 && w.mAttrs.preferredRefreshRate != 0) {
669 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
670 = w.mAttrs.preferredRefreshRate;
671 }
672 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
673 && w.mAttrs.preferredDisplayModeId != 0) {
674 mTmpApplySurfaceChangesTransactionState.preferredModeId
675 = w.mAttrs.preferredDisplayModeId;
676 }
677 }
678 }
679
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800680 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
681 && mWallpaperController.isWallpaperTarget(w)) {
682 // This is the wallpaper target and its obscured state changed... make sure the
683 // current wallpaper's visibility has been updated accordingly.
684 mWallpaperController.updateWallpaperVisibility();
685 }
686
687 w.handleWindowMovedIfNeeded();
688
689 final WindowStateAnimator winAnimator = w.mWinAnimator;
690
691 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
692 w.mContentChanged = false;
693
694 // Moved from updateWindowsAndWallpaperLocked().
695 if (w.mHasSurface) {
696 // Take care of the window being ready to display.
697 final boolean committed = winAnimator.commitFinishDrawingLocked();
698 if (isDefaultDisplay && committed) {
699 if (w.mAttrs.type == TYPE_DREAM) {
700 // HACK: When a dream is shown, it may at that point hide the lock screen.
701 // So we need to redo the layout to let the phone window manager make this
702 // happen.
703 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
704 if (DEBUG_LAYOUT_REPEATS) {
705 surfacePlacer.debugLayoutRepeats(
706 "dream and commitFinishDrawingLocked true",
707 pendingLayoutChanges);
708 }
709 }
710 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
711 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
712 "First draw done in potential wallpaper target " + w);
713 root.mWallpaperMayChange = true;
714 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
715 if (DEBUG_LAYOUT_REPEATS) {
716 surfacePlacer.debugLayoutRepeats(
717 "wallpaper and commitFinishDrawingLocked true",
718 pendingLayoutChanges);
719 }
720 }
721 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700722 final TaskStack stack = w.getStack();
723 if ((!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening())
724 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800725 // Updates the shown frame before we set up the surface. This is needed
726 // because the resizing could change the top-left position (in addition to
727 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
728 // position the surface.
729 //
730 // If an animation is being started, we can't call this method because the
731 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700732 // we do want to update the position if the window is animating. We make an exception
733 // for the bounds animating state, where an application may have been waiting
734 // for an exit animation to start, but instead enters PiP. We need to ensure
735 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800736 winAnimator.computeShownFrameLocked();
737 }
738 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
739 }
740
741 final AppWindowToken atoken = w.mAppToken;
742 if (atoken != null) {
743 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
744 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
745 mTmpUpdateAllDrawn.add(atoken);
746 }
747 }
748
749 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
750 && w.isDisplayedLw()) {
751 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
752 }
753
754 w.updateResizingWindowIfNeeded();
755 };
756
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800757 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800758 * Create new {@link DisplayContent} instance, add itself to the root window container and
759 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700760 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800761 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700762 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
763 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700764 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700765 DisplayContent(Display display, WindowManagerService service,
Robert Carrb1579c82017-09-05 14:54:47 -0700766 WallpaperController wallpaperController) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100767 super(service);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800768 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
769 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
770 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
771 + " new=" + display);
772 }
773
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700774 mDisplay = display;
775 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700776 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700777 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700778 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700779 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000780 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700781 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800782 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700783 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700784
Robert Carrb1579c82017-09-05 14:54:47 -0700785 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth);
786
787 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
788 .setSize(mSurfaceSize, mSurfaceSize)
789 .setOpaque(true);
790 mWindowingLayer = b.setName("Display Root").build();
791 mOverlayLayer = b.setName("Display Overlays").build();
792
Robert Carrf59b8dd2017-10-02 18:58:36 -0700793 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700794 .setLayerStack(mWindowingLayer, mDisplayId)
795 .show(mWindowingLayer)
796 .setLayer(mOverlayLayer, 1)
797 .setLayerStack(mOverlayLayer, mDisplayId)
798 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700799 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700800
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700801 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700802 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700803 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700804 super.addChild(mAboveAppWindowsContainers, null);
805 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800806
807 // Add itself as a child to the root container.
808 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700809
810 // TODO(b/62541591): evaluate whether this is the best spot to declare the
811 // {@link DisplayContent} ready for use.
812 mDisplayReady = true;
813 }
814
815 boolean isReady() {
816 // The display is ready when the system and the individual display are both ready.
817 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700818 }
819
820 int getDisplayId() {
821 return mDisplayId;
822 }
823
Wale Ogunwale02319a62016-09-26 15:21:22 -0700824 WindowToken getWindowToken(IBinder binder) {
825 return mTokenMap.get(binder);
826 }
827
828 AppWindowToken getAppWindowToken(IBinder binder) {
829 final WindowToken token = getWindowToken(binder);
830 if (token == null) {
831 return null;
832 }
833 return token.asAppWindowToken();
834 }
835
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700836 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700837 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
838 if (dc != null) {
839 // We currently don't support adding a window token to the display if the display
840 // already has the binder mapped to another token. If there is a use case for supporting
841 // this moving forward we will either need to merge the WindowTokens some how or have
842 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700843 throw new IllegalArgumentException("Can't map token=" + token + " to display="
844 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700845 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700846 if (binder == null) {
847 throw new IllegalArgumentException("Can't map token=" + token + " to display="
848 + getName() + " binder is null");
849 }
850 if (token == null) {
851 throw new IllegalArgumentException("Can't map null token to display="
852 + getName() + " binder=" + binder);
853 }
854
Wale Ogunwale02319a62016-09-26 15:21:22 -0700855 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700856
857 if (token.asAppWindowToken() == null) {
858 // Add non-app token to container hierarchy on the display. App tokens are added through
859 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700860 switch (token.windowType) {
861 case TYPE_WALLPAPER:
862 mBelowAppWindowsContainers.addChild(token);
863 break;
864 case TYPE_INPUT_METHOD:
865 case TYPE_INPUT_METHOD_DIALOG:
866 mImeWindowsContainers.addChild(token);
867 break;
868 default:
869 mAboveAppWindowsContainers.addChild(token);
870 break;
871 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700872 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700873 }
874
875 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700876 final WindowToken token = mTokenMap.remove(binder);
877 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800878 token.setExiting();
879 }
880 return token;
881 }
882
883 /** Changes the display the input window token is housed on to this one. */
884 void reParentWindowToken(WindowToken token) {
885 final DisplayContent prevDc = token.getDisplayContent();
886 if (prevDc == this) {
887 return;
888 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800889 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
890 && token.asAppWindowToken() == null) {
891 // Removed the token from the map, but made sure it's not an app token before removing
892 // from parent.
893 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700894 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800895
896 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700897 }
898
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700899 void removeAppToken(IBinder binder) {
900 final WindowToken token = removeWindowToken(binder);
901 if (token == null) {
902 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
903 return;
904 }
905
906 final AppWindowToken appToken = token.asAppWindowToken();
907
908 if (appToken == null) {
909 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
910 return;
911 }
912
913 appToken.onRemovedFromDisplay();
914 }
915
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700916 Display getDisplay() {
917 return mDisplay;
918 }
919
Craig Mautner59c00972012-07-30 12:10:24 -0700920 DisplayInfo getDisplayInfo() {
921 return mDisplayInfo;
922 }
923
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700924 DisplayMetrics getDisplayMetrics() {
925 return mDisplayMetrics;
926 }
927
Andrii Kulian8ee72852017-03-10 10:36:45 -0800928 int getRotation() {
929 return mRotation;
930 }
931
932 void setRotation(int newRotation) {
933 mRotation = newRotation;
934 }
935
936 int getLastOrientation() {
937 return mLastOrientation;
938 }
939
940 void setLastOrientation(int orientation) {
941 mLastOrientation = orientation;
942 }
943
944 boolean getAltOrientation() {
945 return mAltOrientation;
946 }
947
948 void setAltOrientation(boolean altOrientation) {
949 mAltOrientation = altOrientation;
950 }
951
952 int getLastWindowForcedOrientation() {
953 return mLastWindowForcedOrientation;
954 }
955
Andrii Kulian06d07d62017-03-14 11:11:47 -0700956 /**
957 * Update rotation of the display.
958 *
959 * Returns true if the rotation has been changed. In this case YOU MUST CALL
960 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
961 */
962 boolean updateRotationUnchecked(boolean inTransaction) {
963 if (mService.mDeferredRotationPauseCount > 0) {
964 // Rotation updates have been paused temporarily. Defer the update until
965 // updates have been resumed.
966 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
967 return false;
968 }
969
970 ScreenRotationAnimation screenRotationAnimation =
971 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
972 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
973 // Rotation updates cannot be performed while the previous rotation change
974 // animation is still in progress. Skip this update. We will try updating
975 // again after the animation is finished and the display is unfrozen.
976 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
977 return false;
978 }
979 if (mService.mDisplayFrozen) {
980 // Even if the screen rotation animation has finished (e.g. isAnimating
981 // returns false), there is still some time where we haven't yet unfrozen
982 // the display. We also need to abort rotation here.
983 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
984 "Deferring rotation, still finishing previous rotation");
985 return false;
986 }
987
988 if (!mService.mDisplayEnabled) {
989 // No point choosing a rotation if the display is not enabled.
990 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
991 return false;
992 }
993
994 final int oldRotation = mRotation;
995 final int lastOrientation = mLastOrientation;
996 final boolean oldAltOrientation = mAltOrientation;
997 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr0e007272017-10-02 13:00:55 -0700998 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
Robert Carr897215d2017-03-29 12:25:50 -0700999 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001000
Robert Carr0e007272017-10-02 13:00:55 -07001001 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001002 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
1003 if (seamlessRotated != null) {
1004 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1005 // to complete (that is, waiting for windows to redraw). It's tempting to check
1006 // w.mSeamlessRotationCount but that could be incorrect in the case of
1007 // window-removal.
1008 return false;
1009 }
Robert Carr0e007272017-10-02 13:00:55 -07001010
1011 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001012 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001013 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001014 mayRotateSeamlessly = false;
1015 }
1016 for (int i = 0; i < mService.mSessions.size(); i++) {
1017 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1018 mayRotateSeamlessly = false;
1019 break;
1020 }
1021 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001022 }
Robert Carr0e007272017-10-02 13:00:55 -07001023 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001024
1025 // TODO: Implement forced rotation changes.
1026 // Set mAltOrientation to indicate that the application is receiving
1027 // an orientation that has different metrics than it expected.
1028 // eg. Portrait instead of Landscape.
1029
1030 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
1031 lastOrientation, rotation);
1032
1033 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
1034 + ", got rotation " + rotation + " which has "
1035 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1036
1037 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1038 // No change.
1039 return false;
1040 }
1041
1042 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
1043 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1044 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1045
1046 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1047 mService.mWaitingForConfig = true;
1048 }
1049
1050 mRotation = rotation;
1051 mAltOrientation = altOrientation;
1052 if (isDefaultDisplay) {
1053 mService.mPolicy.setRotationLw(rotation);
1054 }
1055
1056 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1057 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1058 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1059 WINDOW_FREEZE_TIMEOUT_DURATION);
1060
1061 setLayoutNeeded();
1062 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001063 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001064
1065 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001066 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001067 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1068 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1069 mDisplayId);
1070 } else {
1071 // The screen rotation animation uses a screenshot to freeze the screen
1072 // while windows resize underneath.
1073 // When we are rotating seamlessly, we allow the elements to transition
1074 // to their rotated state independently and without a freeze required.
1075 screenRotationAnimation = null;
1076
1077 // We have to reset this in case a window was removed before it
1078 // finished seamless rotation.
1079 mService.mSeamlessRotationCount = 0;
1080 }
1081
1082 // We need to update our screen size information to match the new rotation. If the rotation
1083 // has actually changed then this method will return true and, according to the comment at
1084 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1085 // By updating the Display info here it will be available to
1086 // #computeScreenConfiguration() later.
1087 updateDisplayAndOrientation(getConfiguration().uiMode);
1088
1089 if (!inTransaction) {
1090 if (SHOW_TRANSACTIONS) {
1091 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1092 }
1093 mService.openSurfaceTransaction();
1094 }
1095 try {
1096 // NOTE: We disable the rotation in the emulator because
1097 // it doesn't support hardware OpenGL emulation yet.
1098 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1099 && screenRotationAnimation.hasScreenshot()) {
Robert Carrb1579c82017-09-05 14:54:47 -07001100 if (screenRotationAnimation.setRotationInTransaction(rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001101 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1102 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1103 mService.scheduleAnimationLocked();
1104 }
1105 }
1106
1107 if (rotateSeamlessly) {
1108 forAllWindows(w -> {
1109 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1110 }, true /* traverseTopToBottom */);
1111 }
1112
1113 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1114 } finally {
1115 if (!inTransaction) {
Adrian Roos111aff92017-09-27 18:11:46 +02001116 mService.closeSurfaceTransaction("setRotationUnchecked");
Andrii Kulian06d07d62017-03-14 11:11:47 -07001117 if (SHOW_LIGHT_TRANSACTIONS) {
1118 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1119 }
1120 }
1121 }
1122
1123 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001124 if (w.mHasSurface && !rotateSeamlessly) {
1125 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001126 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001127 mService.mRoot.mOrientationChangeComplete = false;
1128 w.mLastFreezeDuration = 0;
1129 }
1130 w.mReportOrientationChanged = true;
1131 }, true /* traverseTopToBottom */);
1132
1133 if (rotateSeamlessly) {
1134 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1135 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1136 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1137 }
1138
1139 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1140 final WindowManagerService.RotationWatcher rotationWatcher
1141 = mService.mRotationWatchers.get(i);
1142 if (rotationWatcher.mDisplayId == mDisplayId) {
1143 try {
1144 rotationWatcher.mWatcher.onRotationChanged(rotation);
1145 } catch (RemoteException e) {
1146 // Ignore
1147 }
1148 }
1149 }
1150
1151 // TODO (multi-display): Magnification is supported only for the default display.
1152 // Announce rotation only if we will not animate as we already have the
1153 // windows in final state. Otherwise, we make this call at the rotation end.
1154 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1155 && isDefaultDisplay) {
1156 mService.mAccessibilityController.onRotationChangedLocked(this);
1157 }
1158
1159 return true;
1160 }
1161
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001162 void configureDisplayPolicy() {
1163 mService.mPolicy.setInitialDisplaySize(getDisplay(),
1164 mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1165
1166 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
1167 }
1168
Andrii Kulian06d07d62017-03-14 11:11:47 -07001169 /**
1170 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1171 * changed.
1172 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1173 */
1174 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1175 // Use the effective "visual" dimensions based on current rotation
1176 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1177 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1178 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1179 int dw = realdw;
1180 int dh = realdh;
1181
1182 if (mAltOrientation) {
1183 if (realdw > realdh) {
1184 // Turn landscape into portrait.
1185 int maxw = (int)(realdh/1.3f);
1186 if (maxw < realdw) {
1187 dw = maxw;
1188 }
1189 } else {
1190 // Turn portrait into landscape.
1191 int maxh = (int)(realdw/1.3f);
1192 if (maxh < realdh) {
1193 dh = maxh;
1194 }
1195 }
1196 }
1197
1198 // Update application display metrics.
1199 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1200 mDisplayId);
1201 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1202 mDisplayId);
1203 mDisplayInfo.rotation = mRotation;
1204 mDisplayInfo.logicalWidth = dw;
1205 mDisplayInfo.logicalHeight = dh;
1206 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1207 mDisplayInfo.appWidth = appWidth;
1208 mDisplayInfo.appHeight = appHeight;
1209 if (isDefaultDisplay) {
1210 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1211 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1212 }
1213 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1214 if (mDisplayScalingDisabled) {
1215 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1216 } else {
1217 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1218 }
1219
1220 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1221 mDisplayInfo);
1222
1223 mBaseDisplayRect.set(0, 0, dw, dh);
1224
1225 if (isDefaultDisplay) {
1226 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1227 mCompatDisplayMetrics);
1228 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001229
1230 updateBounds();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001231 return mDisplayInfo;
1232 }
1233
1234 /**
1235 * Compute display configuration based on display properties and policy settings.
1236 * Do not call if mDisplayReady == false.
1237 */
1238 void computeScreenConfiguration(Configuration config) {
1239 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1240
1241 final int dw = displayInfo.logicalWidth;
1242 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001243 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1244 // TODO: Probably best to set this based on some setting in the display content object,
1245 // so the display can be configured for things like fullscreen.
1246 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001247
Andrii Kulian06d07d62017-03-14 11:11:47 -07001248 config.screenWidthDp =
1249 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1250 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1251 config.screenHeightDp =
1252 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1253 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001254
1255 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1256 final int leftInset = mTmpRect.left;
1257 final int topInset = mTmpRect.top;
1258 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001259 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1260 leftInset + displayInfo.appWidth /* right */,
1261 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001262 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1263 || displayInfo.rotation == Surface.ROTATION_270);
1264
1265 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1266 mDisplayMetrics.density, config);
1267
1268 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1269 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1270 ? Configuration.SCREENLAYOUT_ROUND_YES
1271 : Configuration.SCREENLAYOUT_ROUND_NO);
1272
1273 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1274 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1275 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1276 dh, mDisplayId);
1277 config.densityDpi = displayInfo.logicalDensityDpi;
1278
1279 config.colorMode =
1280 (displayInfo.isHdr()
1281 ? Configuration.COLOR_MODE_HDR_YES
1282 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001283 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001284 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1285 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1286
1287 // Update the configuration based on available input devices, lid switch,
1288 // and platform configuration.
1289 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1290 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1291 config.navigation = Configuration.NAVIGATION_NONAV;
1292
1293 int keyboardPresence = 0;
1294 int navigationPresence = 0;
1295 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1296 final int len = devices != null ? devices.length : 0;
1297 for (int i = 0; i < len; i++) {
1298 InputDevice device = devices[i];
1299 if (!device.isVirtual()) {
1300 final int sources = device.getSources();
1301 final int presenceFlag = device.isExternal() ?
1302 WindowManagerPolicy.PRESENCE_EXTERNAL :
1303 WindowManagerPolicy.PRESENCE_INTERNAL;
1304
1305 // TODO(multi-display): Configure on per-display basis.
1306 if (mService.mIsTouchDevice) {
1307 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1308 InputDevice.SOURCE_TOUCHSCREEN) {
1309 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1310 }
1311 } else {
1312 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1313 }
1314
1315 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1316 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1317 navigationPresence |= presenceFlag;
1318 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1319 && config.navigation == Configuration.NAVIGATION_NONAV) {
1320 config.navigation = Configuration.NAVIGATION_DPAD;
1321 navigationPresence |= presenceFlag;
1322 }
1323
1324 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1325 config.keyboard = Configuration.KEYBOARD_QWERTY;
1326 keyboardPresence |= presenceFlag;
1327 }
1328 }
1329 }
1330
1331 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1332 config.navigation = Configuration.NAVIGATION_DPAD;
1333 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1334 }
1335
1336 // Determine whether a hard keyboard is available and enabled.
1337 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1338 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1339 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1340 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1341 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1342 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1343 }
1344
1345 // Let the policy update hidden states.
1346 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1347 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1348 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1349 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1350 }
1351
1352 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1353 int displayId) {
1354 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1355 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1356 final int unrotDw, unrotDh;
1357 if (rotated) {
1358 unrotDw = dh;
1359 unrotDh = dw;
1360 } else {
1361 unrotDw = dw;
1362 unrotDh = dh;
1363 }
1364 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1365 displayId);
1366 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1367 displayId);
1368 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1369 displayId);
1370 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1371 displayId);
1372 return sw;
1373 }
1374
1375 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1376 DisplayMetrics dm, int dw, int dh, int displayId) {
1377 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1378 displayId);
1379 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1380 uiMode, displayId);
1381 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1382 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1383 if (curSize == 0 || size < curSize) {
1384 curSize = size;
1385 }
1386 return curSize;
1387 }
1388
1389 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1390 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1391
1392 // We need to determine the smallest width that will occur under normal
1393 // operation. To this, start with the base screen size and compute the
1394 // width under the different possible rotations. We need to un-rotate
1395 // the current screen dimensions before doing this.
1396 int unrotDw, unrotDh;
1397 if (rotated) {
1398 unrotDw = dh;
1399 unrotDh = dw;
1400 } else {
1401 unrotDw = dw;
1402 unrotDh = dh;
1403 }
1404 displayInfo.smallestNominalAppWidth = 1<<30;
1405 displayInfo.smallestNominalAppHeight = 1<<30;
1406 displayInfo.largestNominalAppWidth = 0;
1407 displayInfo.largestNominalAppHeight = 0;
1408 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1409 unrotDh);
1410 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1411 unrotDw);
1412 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1413 unrotDh);
1414 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1415 unrotDw);
1416 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1417 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1418 displayId);
1419 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1420 displayId);
1421 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1422 displayId);
1423 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1424 displayId);
1425 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1426 outConfig.screenLayout = sl;
1427 }
1428
1429 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1430 int uiMode, int displayId) {
1431 // Get the app screen size at this rotation.
1432 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1433 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1434
1435 // Compute the screen layout size class for this rotation.
1436 int longSize = w;
1437 int shortSize = h;
1438 if (longSize < shortSize) {
1439 int tmp = longSize;
1440 longSize = shortSize;
1441 shortSize = tmp;
1442 }
1443 longSize = (int)(longSize/density);
1444 shortSize = (int)(shortSize/density);
1445 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1446 }
1447
1448 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1449 int uiMode, int dw, int dh) {
1450 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1451 displayId);
1452 if (width < displayInfo.smallestNominalAppWidth) {
1453 displayInfo.smallestNominalAppWidth = width;
1454 }
1455 if (width > displayInfo.largestNominalAppWidth) {
1456 displayInfo.largestNominalAppWidth = width;
1457 }
1458 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1459 displayId);
1460 if (height < displayInfo.smallestNominalAppHeight) {
1461 displayInfo.smallestNominalAppHeight = height;
1462 }
1463 if (height > displayInfo.largestNominalAppHeight) {
1464 displayInfo.largestNominalAppHeight = height;
1465 }
1466 }
1467
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001468 DockedStackDividerController getDockedDividerController() {
1469 return mDividerControllerLocked;
1470 }
1471
Winson Chung655332c2016-10-31 13:14:28 -07001472 PinnedStackController getPinnedStackController() {
1473 return mPinnedStackControllerLocked;
1474 }
1475
Jeff Browna506a6e2013-06-04 00:02:38 -07001476 /**
1477 * Returns true if the specified UID has access to this display.
1478 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001479 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001480 return mDisplay.hasAccess(uid);
1481 }
1482
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001483 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001484 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001485 }
1486
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001487 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001488 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001489 }
1490
Wale Ogunwale61911492017-10-11 08:50:50 -07001491 /**
1492 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1493 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001494 TaskStack getSplitScreenPrimaryStack() {
1495 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001496 return (stack != null && stack.isVisible()) ? stack : null;
1497 }
1498
1499 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001500 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001501 * not visible.
1502 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001503 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1504 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001505 }
1506
1507 TaskStack getPinnedStack() {
1508 return mTaskStackContainers.getPinnedStack();
1509 }
1510
1511 private boolean hasPinnedStack() {
1512 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001513 }
1514
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001515 /**
1516 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1517 * Null is no compatible stack on the display.
1518 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001519 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001520 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1521 }
1522
1523 /**
1524 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1525 * activity type. Null is no compatible stack on the display.
1526 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001527 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001528 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001529 }
1530
Bryce Lee48f4b572017-04-10 10:54:15 -07001531 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001532 TaskStack getTopStack() {
1533 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001534 }
1535
Wale Ogunwale61911492017-10-11 08:50:50 -07001536 void onStackWindowingModeChanged(TaskStack stack) {
1537 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001538 }
1539
Andrii Kulian441e4492016-09-29 15:25:00 -07001540 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001541 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001542 super.onConfigurationChanged(newParentConfig);
1543
Andrii Kulian3a507b52016-09-19 18:14:12 -07001544 // The display size information is heavily dependent on the resources in the current
1545 // configuration, so we need to reconfigure it every time the configuration changes.
1546 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1547 mService.reconfigureDisplayLocked(this);
1548
Bryce Leef3c6a472017-11-14 14:53:06 -08001549 final DockedStackDividerController dividerController = getDockedDividerController();
1550
1551 if (dividerController != null) {
1552 getDockedDividerController().onConfigurationChanged();
1553 }
1554
1555 final PinnedStackController pinnedStackController = getPinnedStackController();
1556
1557 if (pinnedStackController != null) {
1558 getPinnedStackController().onConfigurationChanged();
1559 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001560 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001561
Andrii Kulian441e4492016-09-29 15:25:00 -07001562 /**
1563 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1564 * bounds were updated.
1565 */
1566 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001567 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1568 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001569 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001570 changedStackList.add(stack.mStackId);
1571 }
1572 }
Winson Chung32c566f2017-04-11 18:31:21 -07001573
1574 // If there was no pinned stack, we still need to notify the controller of the display info
1575 // update as a result of the config change. We do this here to consolidate the flow between
1576 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001577 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001578 mPinnedStackControllerLocked.onDisplayInfoChanged();
1579 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001580 }
1581
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001582 @Override
1583 boolean fillsParent() {
1584 return true;
1585 }
1586
1587 @Override
1588 boolean isVisible() {
1589 return true;
1590 }
1591
1592 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001593 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001594 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001595 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001596 }
1597
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001598 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001599 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1600 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1601 // target, or here in order if there isn't an IME target.
1602 if (traverseTopToBottom) {
1603 for (int i = mChildren.size() - 1; i >= 0; --i) {
1604 final DisplayChildWindowContainer child = mChildren.get(i);
1605 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1606 // In this case the Ime windows will be processed above their target so we skip
1607 // here.
1608 continue;
1609 }
1610 if (child.forAllWindows(callback, traverseTopToBottom)) {
1611 return true;
1612 }
1613 }
1614 } else {
1615 final int count = mChildren.size();
1616 for (int i = 0; i < count; i++) {
1617 final DisplayChildWindowContainer child = mChildren.get(i);
1618 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1619 // In this case the Ime windows will be processed above their target so we skip
1620 // here.
1621 continue;
1622 }
1623 if (child.forAllWindows(callback, traverseTopToBottom)) {
1624 return true;
1625 }
1626 }
1627 }
1628 return false;
1629 }
1630
1631 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1632 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1633 }
1634
Wale Ogunwale399c8692017-05-08 14:22:42 -07001635 @Override
1636 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001637 final WindowManagerPolicy policy = mService.mPolicy;
1638
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001639 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001640 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001641 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001642 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001643 // If the display is frozen, some activities may be in the middle of restarting, and
1644 // thus have removed their old window. If the window has the flag to hide the lock
1645 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1646 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001647 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001648 } else if (policy.isKeyguardLocked()) {
1649 // Use the last orientation the while the display is frozen with the keyguard
1650 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1651 // window. We don't want to check the show when locked window directly though as
1652 // things aren't stable while the display is frozen, for example the window could be
1653 // momentarily unavailable due to activity relaunch.
1654 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001655 + "return " + mLastOrientation);
1656 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001657 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001658 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001659 final int orientation = mAboveAppWindowsContainers.getOrientation();
1660 if (orientation != SCREEN_ORIENTATION_UNSET) {
1661 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001662 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001663 }
1664
Wale Ogunwale399c8692017-05-08 14:22:42 -07001665 // Top system windows are not requesting an orientation. Start searching from apps.
1666 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001667 }
1668
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001669 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001670 // Check if display metrics changed and update base values if needed.
1671 updateBaseDisplayMetricsIfNeeded();
1672
Craig Mautner722285e2012-09-07 13:55:58 -07001673 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001674 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001675
Wale Ogunwale61911492017-10-11 08:50:50 -07001676 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1677 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001678 }
Craig Mautner722285e2012-09-07 13:55:58 -07001679 }
1680
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001681 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001682 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1683 if (displayManagerInternal != null) {
1684 // Bootstrap the default logical display from the display manager.
1685 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1686 if (newDisplayInfo != null) {
1687 mDisplayInfo.copyFrom(newDisplayInfo);
1688 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001689 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001690
Andrii Kuliancd097992017-03-23 18:31:59 -07001691 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1692 mDisplayInfo.logicalDensityDpi);
1693 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1694 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1695 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001696 }
1697
Andrii Kuliancd097992017-03-23 18:31:59 -07001698 /**
1699 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1700 * values.
1701 */
1702 private void updateBaseDisplayMetricsIfNeeded() {
1703 // Get real display metrics without overrides from WM.
1704 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1705 final int orientation = mDisplayInfo.rotation;
1706 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1707 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1708 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1709 final int newDensity = mDisplayInfo.logicalDensityDpi;
1710
1711 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1712 || mInitialDisplayHeight != newHeight
1713 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1714
1715 if (displayMetricsChanged) {
1716 // Check if display size or density is forced.
1717 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1718 || mBaseDisplayHeight != mInitialDisplayHeight;
1719 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1720
1721 // If there is an override set for base values - use it, otherwise use new values.
1722 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1723 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1724 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1725
1726 // Real display metrics changed, so we should also update initial values.
1727 mInitialDisplayWidth = newWidth;
1728 mInitialDisplayHeight = newHeight;
1729 mInitialDisplayDensity = newDensity;
1730 mService.reconfigureDisplayLocked(this);
1731 }
1732 }
1733
Bryce Lee27cec322017-03-21 09:41:37 -07001734 /** Sets the maximum width the screen resolution can be */
1735 void setMaxUiWidth(int width) {
1736 if (DEBUG_DISPLAY) {
1737 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1738 }
1739
1740 mMaxUiWidth = width;
1741
1742 // Update existing metrics.
1743 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1744 }
1745
1746 /** Update base (override) display metrics. */
1747 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1748 mBaseDisplayWidth = baseWidth;
1749 mBaseDisplayHeight = baseHeight;
1750 mBaseDisplayDensity = baseDensity;
1751
1752 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1753 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1754 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1755 mBaseDisplayWidth = mMaxUiWidth;
1756
1757 if (DEBUG_DISPLAY) {
1758 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1759 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1760 + " on display:" + getDisplayId());
1761 }
1762 }
1763
1764 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08001765
1766 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07001767 }
1768
Wale Ogunwaledb506192017-12-08 10:57:32 -08001769 void getStableRect(Rect out) {
1770 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08001771 }
1772
Wale Ogunwale61911492017-10-11 08:50:50 -07001773 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001774 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1775 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001776
Wale Ogunwale61911492017-10-11 08:50:50 -07001777 final TaskStack stack = new TaskStack(mService, stackId, controller);
1778 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001779 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001780 }
1781
Andrii Kulian51c1b672017-04-07 18:39:32 -07001782 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001783 final DisplayContent prevDc = stack.getDisplayContent();
1784 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001785 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1786 + " which is not currently attached to any display");
1787 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001788 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001789 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1790 + " to its current displayId=" + mDisplayId);
1791 }
1792
Wale Ogunwale61911492017-10-11 08:50:50 -07001793 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001794 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001795 }
1796
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001797 @Override
1798 protected void addChild(DisplayChildWindowContainer child,
1799 Comparator<DisplayChildWindowContainer> comparator) {
1800 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1801 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001802
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001803 @Override
1804 protected void addChild(DisplayChildWindowContainer child, int index) {
1805 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1806 }
1807
1808 @Override
1809 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001810 // Only allow removal of direct children from this display if the display is in the process
1811 // of been removed.
1812 if (mRemovingDisplay) {
1813 super.removeChild(child);
1814 return;
1815 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001816 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001817 }
1818
Andrii Kuliand2765632016-12-12 22:26:34 -08001819 @Override
1820 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1821 // Children of the display are statically ordered, so the real intention here is to perform
1822 // the operation on the display and not the static direct children.
1823 getParent().positionChildAt(position, this, includingParents);
1824 }
1825
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001826 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001827 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1828 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001829 final int taskId = stack.taskIdFromPoint(x, y);
1830 if (taskId != -1) {
1831 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001832 }
1833 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001834 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001835 }
1836
Chong Zhang8e89b312015-09-09 15:09:30 -07001837 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001838 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001839 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001840 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001841 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001842 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001843 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07001844 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1845 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001846 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001847 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001848 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001849
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001850 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1851 if (mTmpTaskForResizePointSearchResult.searchDone) {
1852 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001853 }
1854 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001855 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001856 }
1857
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001858 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001859 // The provided task is the task on this display with focus, so if WindowManagerService's
1860 // focused app is not on this display, focusedTask will be null.
1861 if (focusedTask == null) {
1862 mTouchExcludeRegion.setEmpty();
1863 } else {
1864 mTouchExcludeRegion.set(mBaseDisplayRect);
1865 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1866 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07001867 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1868 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001869 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
1870 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08001871 }
1872 // If we removed the focused task above, add it back and only leave its
1873 // outside touch area in the exclusion. TapDectector is not interested in
1874 // any touch inside the focused task itself.
1875 if (!mTmpRect2.isEmpty()) {
1876 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1877 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001878 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001879 final WindowState inputMethod = mService.mInputMethodWindow;
1880 if (inputMethod != null && inputMethod.isVisibleLw()) {
1881 // If the input method is visible and the user is typing, we don't want these touch
1882 // events to be intercepted and used to change focus. This would likely cause a
1883 // disappearance of the input method.
1884 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001885 if (inputMethod.getDisplayId() == mDisplayId) {
1886 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1887 } else {
1888 // IME is on a different display, so we need to update its tap detector.
1889 // TODO(multidisplay): Remove when IME will always appear on same display.
1890 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1891 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001892 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001893 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1894 WindowState win = mTapExcludedWindows.get(i);
1895 win.getTouchableRegion(mTmpRegion);
1896 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1897 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001898 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07001899 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001900 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001901 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001902 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1903 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001904 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001905 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001906 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001907 }
1908
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001909 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001910 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001911 super.switchUser();
1912 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001913 }
1914
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001915 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001916 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1917 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001918 }
1919 }
1920
Wale Ogunwale10124582016-09-15 20:25:50 -07001921 @Override
1922 void removeIfPossible() {
1923 if (isAnimating()) {
1924 mDeferredRemoval = true;
1925 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001926 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001927 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001928 }
1929
Wale Ogunwale10124582016-09-15 20:25:50 -07001930 @Override
1931 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001932 mRemovingDisplay = true;
1933 try {
1934 super.removeImmediately();
1935 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07001936 if (mService.canDispatchPointerEvents()) {
1937 if (mTapDetector != null) {
1938 mService.unregisterPointerEventListener(mTapDetector);
1939 }
1940 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
1941 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1942 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001943 }
Robert Carrb1579c82017-09-05 14:54:47 -07001944 // The pending transaction won't be applied so we should
1945 // just clean up any surfaces pending destruction.
1946 onPendingTransactionApplied();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001947 } finally {
1948 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001949 }
Craig Mautner95da1082014-02-24 17:54:35 -08001950 }
1951
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001952 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001953 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001954 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001955 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1956
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001957 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001958 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001959 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001960 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001961 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001962 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001963 }
1964
Andrii Kulian0214ed92017-05-16 13:44:05 -07001965 /** @return 'true' if removal of this display content is deferred due to active animation. */
1966 boolean isRemovalDeferred() {
1967 return mDeferredRemoval;
1968 }
1969
Wale Ogunwale10124582016-09-15 20:25:50 -07001970 boolean animateForIme(float interpolatedValue, float animationTarget,
1971 float dividerAnimationTarget) {
1972 boolean updated = false;
1973
Wale Ogunwale61911492017-10-11 08:50:50 -07001974 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1975 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001976 if (stack == null || !stack.isAdjustedForIme()) {
1977 continue;
1978 }
1979
1980 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1981 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1982 updated = true;
1983 } else {
1984 mDividerControllerLocked.mLastAnimationProgress =
1985 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1986 mDividerControllerLocked.mLastDividerProgress =
1987 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1988 updated |= stack.updateAdjustForIme(
1989 mDividerControllerLocked.mLastAnimationProgress,
1990 mDividerControllerLocked.mLastDividerProgress,
1991 false /* force */);
1992 }
1993 if (interpolatedValue >= 1f) {
1994 stack.endImeAdjustAnimation();
1995 }
1996 }
1997
1998 return updated;
1999 }
2000
2001 boolean clearImeAdjustAnimation() {
2002 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002003 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2004 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002005 if (stack != null && stack.isAdjustedForIme()) {
2006 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2007 changed = true;
2008 }
2009 }
2010 return changed;
2011 }
2012
2013 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002014 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2015 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002016 if (stack.isVisible() && stack.isAdjustedForIme()) {
2017 stack.beginImeAdjustAnimation();
2018 }
2019 }
2020 }
2021
2022 void adjustForImeIfNeeded() {
2023 final WindowState imeWin = mService.mInputMethodWindow;
2024 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2025 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002026 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002027 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2028 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2029 imeTargetStack.getDockSide() : DOCKED_INVALID;
2030 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2031 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2032 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002033 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002034 final boolean imeHeightChanged = imeVisible &&
2035 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2036
2037 // The divider could be adjusted for IME position, or be thinner than usual,
2038 // or both. There are three possible cases:
2039 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2040 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2041 // - If IME is not visible, divider is not moved and is normal width.
2042
2043 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002044 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2045 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002046 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002047 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2048 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002049 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2050 } else {
2051 stack.resetAdjustedForIme(false);
2052 }
2053 }
2054 mDividerControllerLocked.setAdjustedForIme(
2055 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2056 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002057 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2058 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002059 stack.resetAdjustedForIme(!dockVisible);
2060 }
2061 mDividerControllerLocked.setAdjustedForIme(
2062 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2063 }
Winson Chung655332c2016-10-31 13:14:28 -07002064 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002065 }
2066
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002067 void setInputMethodAnimLayerAdjustment(int adj) {
2068 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2069 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002070 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002071 }
2072
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002073 /**
2074 * If a window that has an animation specifying a colored background and the current wallpaper
2075 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2076 * suddenly disappear.
2077 */
2078 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002079 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2080 w -> w.mIsWallpaper && w.isVisibleNow());
2081
2082 if (visibleWallpaper != null) {
2083 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002084 }
2085 return winAnimator.mAnimLayer;
2086 }
2087
Wale Ogunwale10124582016-09-15 20:25:50 -07002088 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002089 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2090 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002091 stack.prepareFreezingTaskBounds();
2092 }
2093 }
2094
Wale Ogunwale94744212015-09-21 19:01:47 -07002095 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002096 getBounds(mTmpRect, newRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002097
2098 // Compute a transform matrix to undo the coordinate space transformation,
2099 // and present the window at the same physical position it previously occupied.
2100 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2101 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2102
2103 mTmpRectF.set(bounds);
2104 mTmpMatrix.mapRect(mTmpRectF);
2105 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002106 }
2107
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002108 static int deltaRotation(int oldRotation, int newRotation) {
2109 int delta = newRotation - oldRotation;
2110 if (delta < 0) delta += 4;
2111 return delta;
2112 }
2113
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002114 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002115 Matrix outMatrix) {
2116 // For rotations without Z-ordering we don't need the target rectangle's position.
2117 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2118 displayHeight, outMatrix);
2119 }
2120
2121 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2122 float displayWidth, float displayHeight, Matrix outMatrix) {
2123 switch (rotation) {
2124 case ROTATION_0:
2125 outMatrix.reset();
2126 break;
2127 case ROTATION_270:
2128 outMatrix.setRotate(270, 0, 0);
2129 outMatrix.postTranslate(0, displayHeight);
2130 outMatrix.postTranslate(rectTop, 0);
2131 break;
2132 case ROTATION_180:
2133 outMatrix.reset();
2134 break;
2135 case ROTATION_90:
2136 outMatrix.setRotate(90, 0, 0);
2137 outMatrix.postTranslate(displayWidth, 0);
2138 outMatrix.postTranslate(-rectTop, rectLeft);
2139 break;
2140 }
2141 }
2142
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002143 @CallSuper
2144 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002145 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002146 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002147 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002148 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002149 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2150 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002151 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002152 }
2153 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2154 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002155 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2156 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002157 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002158 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002159 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2160 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002161 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002162 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002163 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2164 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002165 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002166 }
2167 proto.write(DPI, mBaseDisplayDensity);
2168 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002169 proto.write(ROTATION, mRotation);
2170 final ScreenRotationAnimation screenRotationAnimation =
2171 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2172 if (screenRotationAnimation != null) {
2173 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2174 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002175 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002176 proto.end(token);
2177 }
2178
Craig Mautnera91f9e22012-09-14 16:22:08 -07002179 public void dump(String prefix, PrintWriter pw) {
2180 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2181 final String subPrefix = " " + prefix;
2182 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2183 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2184 pw.print("dpi");
2185 if (mInitialDisplayWidth != mBaseDisplayWidth
2186 || mInitialDisplayHeight != mBaseDisplayHeight
2187 || mInitialDisplayDensity != mBaseDisplayDensity) {
2188 pw.print(" base=");
2189 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2190 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2191 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002192 if (mDisplayScalingDisabled) {
2193 pw.println(" noscale");
2194 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002195 pw.print(" cur=");
2196 pw.print(mDisplayInfo.logicalWidth);
2197 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2198 pw.print(" app=");
2199 pw.print(mDisplayInfo.appWidth);
2200 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2201 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2202 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2203 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2204 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002205 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002206 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002207 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002208
Craig Mautnerdc548482014-02-05 13:35:24 -08002209 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002210 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002211 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2212 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002213 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002214 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002215
Craig Mautnerdc548482014-02-05 13:35:24 -08002216 pw.println();
2217 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002218 pw.println();
2219 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002220 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002221 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002222 pw.print(" Exiting #"); pw.print(i);
2223 pw.print(' '); pw.print(token);
2224 pw.println(':');
2225 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002226 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002227 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002228
Jorim Jaggi31f71702016-05-04 16:43:04 -07002229 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002230
2231 // Dump stack references
2232 final TaskStack homeStack = getHomeStack();
2233 if (homeStack != null) {
2234 pw.println(prefix + "homeStack=" + homeStack.getName());
2235 }
2236 final TaskStack pinnedStack = getPinnedStack();
2237 if (pinnedStack != null) {
2238 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2239 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002240 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002241 if (splitScreenPrimaryStack != null) {
2242 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2243 }
2244
2245 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002246 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002247 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002248 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002249
2250 if (mInputMethodAnimLayerAdjustment != 0) {
2251 pw.println(subPrefix
2252 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2253 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002254
2255 pw.println();
2256 mDisplayFrames.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002257 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002258
2259 @Override
2260 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002261 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002262 }
2263
2264 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002265 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002266 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002267
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002268 /** Returns true if the stack in the windowing mode is visible. */
2269 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002270 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002271 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002272 }
2273
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002274 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002275 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002276 final int x = (int) xf;
2277 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002278 final WindowState touchedWin = getWindow(w -> {
2279 final int flags = w.mAttrs.flags;
2280 if (!w.isVisibleLw()) {
2281 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002282 }
2283 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002284 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002285 }
2286
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002287 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002288 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002289 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002290 }
2291
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002292 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002293
2294 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002295 return mTmpRegion.contains(x, y) || touchFlags == 0;
2296 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002297
2298 return touchedWin;
2299 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002300
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002301 boolean canAddToastWindowForUid(int uid) {
2302 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002303 // Also if the app is focused adding more than one toast at
2304 // a time for better backwards compatibility.
2305 final WindowState focusedWindowForUid = getWindow(w ->
2306 w.mOwnerUid == uid && w.isFocused());
2307 if (focusedWindowForUid != null) {
2308 return true;
2309 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002310 final WindowState win = getWindow(w ->
2311 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2312 && !w.mWindowRemovalAllowed);
2313 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002314 }
2315
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002316 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002317 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2318 return;
2319 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002320
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002321 // Used to communicate the old focus to the callback method.
2322 mTmpWindow = oldFocus;
2323
2324 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002325 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002326
2327 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002328 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002329
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002330 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002331
2332 if (mTmpWindow == null) {
2333 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2334 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002335 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002336 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002337 }
2338
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002339 /** Updates the layer assignment of windows on this display. */
2340 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002341 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002342 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002343 if (setLayoutNeeded) {
2344 setLayoutNeeded();
2345 }
Robert Carrb1579c82017-09-05 14:54:47 -07002346
Robert Carrf59b8dd2017-10-02 18:58:36 -07002347 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002348 // the application of this transaction until the animation pass triggers
2349 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2350 // the hiding and showing of surfaces.
2351 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002352 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002353 }
2354
Wale Ogunwale1666e312016-12-16 11:27:18 -08002355 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2356 // moving containers or resizing them. Need to investigate the best way to have it automatically
2357 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002358 void layoutAndAssignWindowLayersIfNeeded() {
2359 mService.mWindowsChanged = true;
2360 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002361
2362 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2363 false /*updateInputWindows*/)) {
2364 assignWindowLayers(false /* setLayoutNeeded */);
2365 }
2366
2367 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2368 mService.mWindowPlacerLocked.performSurfacePlacement();
2369 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2370 }
2371
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002372 /** Returns true if a leaked surface was destroyed */
2373 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002374 // Used to indicate that a surface was leaked.
2375 mTmpWindow = null;
2376 forAllWindows(w -> {
2377 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002378 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002379 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002380 }
2381 if (!mService.mSessions.contains(wsa.mSession)) {
2382 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002383 + w + " surface=" + wsa.mSurfaceController
2384 + " token=" + w.mToken
2385 + " pid=" + w.mSession.mPid
2386 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002387 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002388 mService.mForceRemoves.add(w);
2389 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002390 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002391 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002392 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002393 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002394 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002395 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002396 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002397 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002398 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002399
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002400 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002401 }
2402
2403 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002404 * Determine and return the window that should be the IME target.
2405 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2406 * @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 +00002407 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002408 WindowState computeImeTarget(boolean updateImeTarget) {
2409 if (mService.mInputMethodWindow == null) {
2410 // There isn't an IME so there shouldn't be a target...That was easy!
2411 if (updateImeTarget) {
2412 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2413 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2414 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2415 }
2416 return null;
2417 }
2418
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002419 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2420 // same display. Or even when the current IME/target are not on the same screen as the next
2421 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002422 mUpdateImeTarget = updateImeTarget;
2423 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002424
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002425
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002426 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2427 // to be on top of it, but it is not -really- where input will go. So look down below
2428 // for a real window to target...
2429 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2430 final AppWindowToken token = target.mAppToken;
2431 if (token != null) {
2432 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2433 if (betterTarget != null) {
2434 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002435 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002436 }
2437 }
2438
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002439 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2440 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002441
2442 // Now, a special case -- if the last target's window is in the process of exiting, and is
2443 // above the new target, keep on the last target to avoid flicker. Consider for example a
2444 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2445 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2446 // scrim.
2447 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002448 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2449 && (target == null
2450 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002451 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002452 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002453 }
2454
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002455 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2456 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002457
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002458 if (target == null) {
2459 if (updateImeTarget) {
2460 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2461 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2462 + Debug.getCallers(4) : ""));
2463 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2464 }
2465
2466 return null;
2467 }
2468
2469 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002470 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2471 if (token != null) {
2472
2473 // Now some fun for dealing with window animations that modify the Z order. We need
2474 // to look at all windows below the current target that are in this app, finding the
2475 // highest visible one in layering.
2476 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002477 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002478 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002479 }
2480
2481 if (highestTarget != null) {
2482 final AppTransition appTransition = mService.mAppTransition;
2483 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2484 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2485 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002486 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002487
2488 if (appTransition.isTransitionSet()) {
2489 // If we are currently setting up for an animation, hold everything until we
2490 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002491 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2492 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002493 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002494 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002495 // If the window we are currently targeting is involved with an animation,
2496 // and it is on top of the next target we will be over, then hold off on
2497 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002498 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2499 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002500 }
2501 }
2502 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002503
2504 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2505 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2506 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002507 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002508 }
2509
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002510 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002511 }
2512
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002513 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2514 if (target == mService.mInputMethodTarget
2515 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2516 && mInputMethodAnimLayerAdjustment == layerAdj) {
2517 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002518 }
2519
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002520 mService.mInputMethodTarget = target;
2521 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2522 setInputMethodAnimLayerAdjustment(layerAdj);
2523 assignWindowLayers(false /* setLayoutNeeded */);
2524 }
2525
2526 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2527 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2528 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2529 }
2530
2531 // Used to indicate we have reached the first window in the range we are interested in.
2532 mTmpWindow = null;
2533
2534 // TODO: Figure-out a more efficient way to do this.
2535 final WindowState candidate = getWindow(w -> {
2536 if (w == top) {
2537 // Reached the first window in the range we are interested in.
2538 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002539 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002540 if (mTmpWindow == null) {
2541 return false;
2542 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002543
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002544 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2545 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002546 }
2547 // If we reached the bottom of the range of windows we are considering,
2548 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002549 if (w == bottom) {
2550 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002551 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002552 return false;
2553 });
2554
2555 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002556 }
2557
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002558 void setLayoutNeeded() {
2559 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2560 mLayoutNeeded = true;
2561 }
2562
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002563 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002564 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2565 mLayoutNeeded = false;
2566 }
2567
2568 boolean isLayoutNeeded() {
2569 return mLayoutNeeded;
2570 }
2571
Wale Ogunwale02319a62016-09-26 15:21:22 -07002572 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2573 if (mTokenMap.isEmpty()) {
2574 return;
2575 }
2576 pw.println(" Display #" + mDisplayId);
2577 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2578 while (it.hasNext()) {
2579 final WindowToken token = it.next();
2580 pw.print(" ");
2581 pw.print(token);
2582 if (dumpAll) {
2583 pw.println(':');
2584 token.dump(pw, " ");
2585 } else {
2586 pw.println();
2587 }
2588 }
2589 }
2590
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002591 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002592 final int[] index = new int[1];
2593 forAllWindows(w -> {
2594 final WindowStateAnimator wAnim = w.mWinAnimator;
2595 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2596 index[0] = index[0] + 1;
2597 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002598 }
2599
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002600 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002601 forAllWindows(w -> {
2602 w.mWinAnimator.enableSurfaceTrace(fd);
2603 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002604 }
2605
2606 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002607 forAllWindows(w -> {
2608 w.mWinAnimator.disableSurfaceTrace();
2609 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002610 }
2611
Jorim Jaggife762342016-10-13 14:33:27 +02002612 /**
2613 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2614 */
2615 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2616 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002617 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002618 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2619 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002620 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002621 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2622 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002623 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002624 }
2625
Wale Ogunwale494009b82016-10-21 09:01:38 -07002626 boolean checkWaitingForWindows() {
2627
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002628 mHaveBootMsg = false;
2629 mHaveApp = false;
2630 mHaveWallpaper = false;
2631 mHaveKeyguard = true;
2632
2633 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002634 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2635 return true;
2636 }
2637 if (w.isDrawnLw()) {
2638 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002639 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002640 } else if (w.mAttrs.type == TYPE_APPLICATION
2641 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002642 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002643 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002644 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002645 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002646 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002647 }
2648 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002649 return false;
2650 });
2651
2652 if (visibleWindow != null) {
2653 // We have a visible window.
2654 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002655 }
2656
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002657 // if the wallpaper service is disabled on the device, we're never going to have
2658 // wallpaper, don't bother waiting for it
2659 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2660 com.android.internal.R.bool.config_enableWallpaperService)
2661 && !mService.mOnlyCore;
2662
Wale Ogunwale494009b82016-10-21 09:01:38 -07002663 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2664 "******** booted=" + mService.mSystemBooted
2665 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002666 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2667 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2668 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002669
2670 // If we are turning on the screen to show the boot message, don't do it until the boot
2671 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002672 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002673 return true;
2674 }
2675
2676 // If we are turning on the screen after the boot is completed normally, don't do so until
2677 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002678 if (mService.mSystemBooted
2679 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002680 return true;
2681 }
2682
2683 return false;
2684 }
2685
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002686 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002687 mTmpWindowAnimator = animator;
2688 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002689 }
2690
2691 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002692 resetAnimationBackgroundAnimator();
2693
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002694 // Used to indicate a detached wallpaper.
2695 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002696 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002697
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002698 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002699
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002700 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002701 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002702 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2703 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002704 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2705 }
2706 }
2707
Wale Ogunwale494009b82016-10-21 09:01:38 -07002708 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002709 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002710 if (imFocus == null) {
2711 return false;
2712 }
2713
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002714 if (DEBUG_INPUT_METHOD) {
2715 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2716 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2717 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002718 }
2719
Wale Ogunwale494009b82016-10-21 09:01:38 -07002720 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2721
2722 if (DEBUG_INPUT_METHOD) {
2723 Slog.i(TAG_WM, "IM target client: " + imeClient);
2724 if (imeClient != null) {
2725 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2726 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2727 }
2728 }
2729
2730 return imeClient != null && imeClient.asBinder() == client.asBinder();
2731 }
2732
2733 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002734 final WindowState win = getWindow(
2735 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2736 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002737 }
2738
2739 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002740 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002741 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002742 final int curValue = w.mSystemUiVisibility;
2743 final int diff = (curValue ^ visibility) & globalDiff;
2744 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002745 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002746 w.mSeq++;
2747 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002748 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002749 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2750 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002751 visibility, newValue, diff);
2752 }
2753 } catch (RemoteException e) {
2754 // so sorry
2755 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002756 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002757 }
2758
2759 void onWindowFreezeTimeout() {
2760 Slog.w(TAG_WM, "Window freeze timeout expired.");
2761 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002762
2763 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002764 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002765 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002766 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002767 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002768 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2769 - mService.mDisplayFreezeTime);
2770 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002771 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002772 mService.mWindowPlacerLocked.performSurfacePlacement();
2773 }
2774
2775 void waitForAllWindowsDrawn() {
2776 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002777 forAllWindows(w -> {
2778 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2779 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2780 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002781 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002782 w.mLastContentInsets.set(-1, -1, -1, -1);
2783 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002784 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002785 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002786 }
2787
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002788 // TODO: Super crazy long method that should be broken down...
2789 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2790
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002791 final int dw = mDisplayInfo.logicalWidth;
2792 final int dh = mDisplayInfo.logicalHeight;
2793 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2794
2795 mTmpUpdateAllDrawn.clear();
2796
2797 int repeats = 0;
2798 do {
2799 repeats++;
2800 if (repeats > 6) {
2801 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2802 clearLayoutNeeded();
2803 break;
2804 }
2805
2806 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2807 pendingLayoutChanges);
2808
Andrii Kulian839def92016-11-02 10:58:58 -07002809 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2810 // the wallpaper window jumping across displays.
2811 // Remove check for default display when there will be support for multiple wallpaper
2812 // targets (on different displays).
2813 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002814 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002815 }
2816
2817 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2818 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2819 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2820 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002821 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002822 }
2823 }
2824
2825 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2826 setLayoutNeeded();
2827 }
2828
2829 // FIRST LOOP: Perform a layout, if needed.
2830 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2831 performLayout(repeats == 1, false /* updateInputWindows */);
2832 } else {
2833 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2834 }
2835
2836 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2837 pendingLayoutChanges = 0;
2838
2839 if (isDefaultDisplay) {
2840 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002841 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002842 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2843 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2844 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2845 }
2846 } while (pendingLayoutChanges != 0);
2847
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002848 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002849
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002850 mTmpRecoveringMemory = recoveringMemory;
2851 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002852
2853 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002854 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2855 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2856 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002857 true /* inTraversal, must call performTraversalInTrans... below */);
2858
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002859 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2860 if (wallpaperVisible != mLastWallpaperVisible) {
2861 mLastWallpaperVisible = wallpaperVisible;
2862 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2863 }
2864
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002865 while (!mTmpUpdateAllDrawn.isEmpty()) {
2866 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2867 // See if any windows have been drawn, so they (and others associated with them)
2868 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002869 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002870 }
2871
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002872 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002873 }
2874
Bryce Leef3c6a472017-11-14 14:53:06 -08002875 private void updateBounds() {
2876 calculateBounds(mTmpBounds);
2877 setBounds(mTmpBounds);
2878 }
2879
2880 // Determines the current display bounds based on the current state
2881 private void calculateBounds(Rect out) {
2882 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
2883 final int orientation = mDisplayInfo.rotation;
2884 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2885 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
2886 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
2887 int width = mDisplayInfo.logicalWidth;
2888 int left = (physWidth - width) / 2;
2889 int height = mDisplayInfo.logicalHeight;
2890 int top = (physHeight - height) / 2;
2891 out.set(left, top, left + width, top + height);
2892 }
2893
2894 @Override
2895 public void getBounds(Rect out) {
2896 calculateBounds(out);
2897 }
2898
2899 private void getBounds(Rect out, int orientation) {
2900 getBounds(out);
2901
2902 // Rotate the Rect if needed.
2903 final int currentRotation = mDisplayInfo.rotation;
2904 final int rotationDelta = deltaRotation(currentRotation, orientation);
2905 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
2906 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
2907 mTmpRectF.set(out);
2908 mTmpMatrix.mapRect(mTmpRectF);
2909 mTmpRectF.round(out);
2910 }
2911 }
2912
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002913 void performLayout(boolean initial, boolean updateInputWindows) {
2914 if (!isLayoutNeeded()) {
2915 return;
2916 }
2917 clearLayoutNeeded();
2918
2919 final int dw = mDisplayInfo.logicalWidth;
2920 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002921 if (DEBUG_LAYOUT) {
2922 Slog.v(TAG, "-------------------------------------");
2923 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2924 }
2925
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002926 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
2927 // TODO: Not sure if we really need to set the rotation here since we are updating from the
2928 // display info above...
2929 mDisplayFrames.mRotation = mRotation;
2930 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002931 if (isDefaultDisplay) {
2932 // Not needed on non-default displays.
2933 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2934 mService.mScreenRect.set(0, 0, dw, dh);
2935 }
2936
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002937 int seq = mService.mLayoutSeq + 1;
2938 if (seq < 0) seq = 0;
2939 mService.mLayoutSeq = seq;
2940
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002941 // Used to indicate that we have processed the dream window and all additional windows are
2942 // behind it.
2943 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002944 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002945
2946 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002947 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002948
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002949 // Used to indicate that we have processed the dream window and all additional attached
2950 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002951 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002952 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002953
2954 // Now perform layout of attached windows, which usually depend on the position of the
2955 // window they are attached to. XXX does not deal with windows that are attached to windows
2956 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002957 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002958
2959 // Window frames may have changed. Tell the input dispatcher about it.
2960 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2961 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2962 if (updateInputWindows) {
2963 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2964 }
2965
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002966 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2967 }
2968
2969 /**
2970 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2971 * In portrait mode, it grabs the full screenshot.
2972 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002973 * @param config of the output bitmap
2974 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
2975 */
chaviwfbe47df2017-11-10 16:14:49 -08002976 Bitmap screenshotDisplay(Bitmap.Config config, boolean wallpaperOnly) {
2977 synchronized (mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02002978 if (!mService.mPolicy.isScreenOn()) {
chaviwfbe47df2017-11-10 16:14:49 -08002979 if (DEBUG_SCREENSHOT) {
2980 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002981 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002982 return null;
2983 }
2984
chaviwfbe47df2017-11-10 16:14:49 -08002985 if (wallpaperOnly && !shouldScreenshotWallpaper()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002986 return null;
2987 }
2988
chaviwfbe47df2017-11-10 16:14:49 -08002989 int dw = mDisplayInfo.logicalWidth;
2990 int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002991
chaviwfbe47df2017-11-10 16:14:49 -08002992 if (dw <= 0 || dh <= 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002993 return null;
2994 }
2995
chaviwfbe47df2017-11-10 16:14:49 -08002996 final Rect frame = new Rect(0, 0, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002997
2998 // The screenshot API does not apply the current screen rotation.
2999 int rot = mDisplay.getRotation();
3000
3001 if (rot == ROTATION_90 || rot == ROTATION_270) {
3002 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3003 }
3004
chaviwfbe47df2017-11-10 16:14:49 -08003005 // SurfaceFlinger is not aware of orientation, so convert our logical
3006 // crop to SurfaceFlinger's portrait orientation.
3007 convertCropForSurfaceFlinger(frame, rot, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003008
3009 final ScreenRotationAnimation screenRotationAnimation =
3010 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3011 final boolean inRotation = screenRotationAnimation != null &&
3012 screenRotationAnimation.isAnimating();
chaviwfbe47df2017-11-10 16:14:49 -08003013 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003014
Robert Carrb1579c82017-09-05 14:54:47 -07003015 // TODO(b/68392460): We should screenshot Task controls directly
3016 // but it's difficult at the moment as the Task doesn't have the
3017 // correct size set.
chaviwfbe47df2017-11-10 16:14:49 -08003018 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, 0, 1, inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003019 if (bitmap == null) {
Robert Carrb1579c82017-09-05 14:54:47 -07003020 Slog.w(TAG_WM, "Failed to take screenshot");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003021 return null;
3022 }
chaviwfbe47df2017-11-10 16:14:49 -08003023
3024 // Create a copy of the screenshot that is immutable and backed in ashmem.
3025 // This greatly reduces the overhead of passing the bitmap between processes.
3026 final Bitmap ret = bitmap.createAshmemBitmap(config);
3027 bitmap.recycle();
3028 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003029 }
chaviwfbe47df2017-11-10 16:14:49 -08003030 }
3031
3032 private boolean shouldScreenshotWallpaper() {
3033 MutableBoolean screenshotReady = new MutableBoolean(false);
3034
3035 forAllWindows(w -> {
3036 if (!w.mIsWallpaper) {
3037 return false;
3038 }
3039
3040 // Found the wallpaper window
3041 final WindowStateAnimator winAnim = w.mWinAnimator;
3042
3043 if (winAnim.getShown() && winAnim.mLastAlpha > 0f) {
3044 screenshotReady.value = true;
3045 }
3046
3047 return true;
3048 }, true /* traverseTopToBottom */);
3049
3050 return screenshotReady.value;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003051 }
3052
3053 // TODO: Can this use createRotationMatrix()?
3054 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3055 if (rot == Surface.ROTATION_90) {
3056 final int tmp = crop.top;
3057 crop.top = dw - crop.right;
3058 crop.right = crop.bottom;
3059 crop.bottom = dw - crop.left;
3060 crop.left = tmp;
3061 } else if (rot == Surface.ROTATION_180) {
3062 int tmp = crop.top;
3063 crop.top = dh - crop.bottom;
3064 crop.bottom = dh - tmp;
3065 tmp = crop.right;
3066 crop.right = dw - crop.left;
3067 crop.left = dw - tmp;
3068 } else if (rot == Surface.ROTATION_270) {
3069 final int tmp = crop.top;
3070 crop.top = crop.left;
3071 crop.left = dh - crop.bottom;
3072 crop.bottom = crop.right;
3073 crop.right = dh - tmp;
3074 }
3075 }
3076
3077 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003078 // Used to indicate the layout is needed.
3079 mTmpWindow = null;
3080
3081 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003082 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003083 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003084 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003085 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003086 w.setDisplayLayoutNeeded();
3087 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003088 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003089
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003090 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003091 mService.mWindowPlacerLocked.performSurfacePlacement();
3092 }
3093 }
3094
Wale Ogunwale1666e312016-12-16 11:27:18 -08003095 void setExitingTokensHasVisible(boolean hasVisible) {
3096 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3097 mExitingTokens.get(i).hasVisible = hasVisible;
3098 }
3099
3100 // Initialize state of exiting applications.
3101 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3102 }
3103
3104 void removeExistingTokensIfPossible() {
3105 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3106 final WindowToken token = mExitingTokens.get(i);
3107 if (!token.hasVisible) {
3108 mExitingTokens.remove(i);
3109 }
3110 }
3111
3112 // Time to remove any exiting applications?
3113 mTaskStackContainers.removeExistingAppTokensIfPossible();
3114 }
3115
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003116 @Override
3117 void onDescendantOverrideConfigurationChanged() {
3118 setLayoutNeeded();
3119 mService.requestTraversal();
3120 }
3121
David Stevens9440dc82017-03-16 19:00:20 -07003122 boolean okToDisplay() {
3123 if (mDisplayId == DEFAULT_DISPLAY) {
3124 return !mService.mDisplayFrozen
3125 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3126 }
3127 return mDisplayInfo.state == Display.STATE_ON;
3128 }
3129
3130 boolean okToAnimate() {
3131 return okToDisplay() &&
3132 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3133 }
3134
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003135 static final class TaskForResizePointSearchResult {
3136 boolean searchDone;
3137 Task taskForResize;
3138
3139 void reset() {
3140 searchDone = false;
3141 taskForResize = null;
3142 }
3143 }
Robert Carr3b716242016-08-16 16:02:21 -07003144
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003145 private static final class ApplySurfaceChangesTransactionState {
3146 boolean displayHasContent;
3147 boolean obscured;
3148 boolean syswin;
3149 boolean focusDisplayed;
3150 float preferredRefreshRate;
3151 int preferredModeId;
3152
3153 void reset() {
3154 displayHasContent = false;
3155 obscured = false;
3156 syswin = false;
3157 focusDisplayed = false;
3158 preferredRefreshRate = 0;
3159 preferredModeId = 0;
3160 }
3161 }
3162
3163 private static final class ScreenshotApplicationState {
3164 WindowState appWin;
3165 int maxLayer;
3166 int minLayer;
3167 boolean screenshotReady;
3168
3169 void reset(boolean screenshotReady) {
3170 appWin = null;
3171 maxLayer = 0;
3172 minLayer = 0;
3173 this.screenshotReady = screenshotReady;
3174 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3175 }
3176 }
3177
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003178 /**
3179 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3180 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3181 * homogeneous children type which is currently required by sub-classes of
3182 * {@link WindowContainer} class.
3183 */
3184 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3185
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003186 DisplayChildWindowContainer(WindowManagerService service) {
3187 super(service);
3188 }
3189
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003190 @Override
3191 boolean fillsParent() {
3192 return true;
3193 }
3194
3195 @Override
3196 boolean isVisible() {
3197 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003198 }
3199 }
3200
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003201 /**
3202 * Window container class that contains all containers on this display relating to Apps.
3203 * I.e Activities.
3204 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003205 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003206 /**
3207 * A control placed at the appropriate level for transitions to occur.
3208 */
3209 SurfaceControl mAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003210
Wale Ogunwale61911492017-10-11 08:50:50 -07003211 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3212 // through the list to find them.
3213 private TaskStack mHomeStack = null;
3214 private TaskStack mPinnedStack = null;
3215 private TaskStack mSplitScreenPrimaryStack = null;
3216
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003217 TaskStackContainers(WindowManagerService service) {
3218 super(service);
3219 }
3220
Wale Ogunwale61911492017-10-11 08:50:50 -07003221 /**
3222 * Returns the topmost stack on the display that is compatible with the input windowing mode
3223 * and activity type. Null is no compatible stack on the display.
3224 */
3225 TaskStack getStack(int windowingMode, int activityType) {
3226 if (activityType == ACTIVITY_TYPE_HOME) {
3227 return mHomeStack;
3228 }
3229 if (windowingMode == WINDOWING_MODE_PINNED) {
3230 return mPinnedStack;
3231 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3232 return mSplitScreenPrimaryStack;
3233 }
3234 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3235 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003236 if (activityType == ACTIVITY_TYPE_UNDEFINED
3237 && windowingMode == stack.getWindowingMode()) {
3238 // Passing in undefined type means we want to match the topmost stack with the
3239 // windowing mode.
3240 return stack;
3241 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003242 if (stack.isCompatible(windowingMode, activityType)) {
3243 return stack;
3244 }
3245 }
3246 return null;
3247 }
3248
3249 @VisibleForTesting
3250 TaskStack getTopStack() {
3251 return mTaskStackContainers.getChildCount() > 0
3252 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3253 }
3254
3255 TaskStack getHomeStack() {
3256 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3257 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3258 }
3259 return mHomeStack;
3260 }
3261
3262 TaskStack getPinnedStack() {
3263 return mPinnedStack;
3264 }
3265
Matthew Ng64e77cf2017-10-31 14:01:31 -07003266 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003267 return mSplitScreenPrimaryStack;
3268 }
3269
Andrii Kulian839def92016-11-02 10:58:58 -07003270 /**
3271 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003272 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003273 */
3274 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003275 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003276 addChild(stack, onTop);
3277 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003278 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003279
Wale Ogunwale61911492017-10-11 08:50:50 -07003280 void onStackWindowingModeChanged(TaskStack stack) {
3281 removeStackReferenceIfNeeded(stack);
3282 addStackReferenceIfNeeded(stack);
3283 if (stack == mPinnedStack && getTopStack() != stack) {
3284 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3285 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3286 }
3287 }
3288
3289 private void addStackReferenceIfNeeded(TaskStack stack) {
3290 if (stack.isActivityTypeHome()) {
3291 if (mHomeStack != null) {
3292 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
3293 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
3294 }
3295 mHomeStack = stack;
3296 }
3297 final int windowingMode = stack.getWindowingMode();
3298 if (windowingMode == WINDOWING_MODE_PINNED) {
3299 if (mPinnedStack != null) {
3300 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3301 + mPinnedStack + " already exist on display=" + this
3302 + " stack=" + stack);
3303 }
3304 mPinnedStack = stack;
3305 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3306 if (mSplitScreenPrimaryStack != null) {
3307 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3308 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3309 + " already exist on display=" + this + " stack=" + stack);
3310 }
3311 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003312 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003313 }
3314 }
3315
3316 private void removeStackReferenceIfNeeded(TaskStack stack) {
3317 if (stack == mHomeStack) {
3318 mHomeStack = null;
3319 } else if (stack == mPinnedStack) {
3320 mPinnedStack = null;
3321 } else if (stack == mSplitScreenPrimaryStack) {
3322 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003323 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3324 mService.setDockedStackCreateStateLocked(
3325 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3326 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003327 }
Andrii Kulian839def92016-11-02 10:58:58 -07003328 }
3329
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003330 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003331 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3332 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003333 addChild(stack, addIndex);
3334 setLayoutNeeded();
3335 }
3336
Wale Ogunwale61911492017-10-11 08:50:50 -07003337 @Override
3338 protected void removeChild(TaskStack stack) {
3339 super.removeChild(stack);
3340 removeStackReferenceIfNeeded(stack);
3341 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003342
3343 @Override
3344 boolean isOnTop() {
3345 // Considered always on top
3346 return true;
3347 }
3348
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003349 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003350 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003351 if (child.getWindowConfiguration().isAlwaysOnTop()
3352 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003353 // This stack is always-on-top, override the default behavior.
3354 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3355
3356 // Moving to its current position, as we must call super but we don't want to
3357 // perform any meaningful action.
3358 final int currentPosition = mChildren.indexOf(child);
3359 super.positionChildAt(currentPosition, child, false /* includingParents */);
3360 return;
3361 }
3362
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003363 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3364 super.positionChildAt(targetPosition, child, includingParents);
3365
3366 setLayoutNeeded();
3367 }
3368
3369 /**
3370 * When stack is added or repositioned, find a proper position for it.
3371 * This will make sure that pinned stack always stays on top.
3372 * @param requestedPosition Position requested by caller.
3373 * @param stack Stack to be added or positioned.
3374 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3375 * @return The proper position for the stack.
3376 */
3377 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3378 final int topChildPosition = mChildren.size() - 1;
3379 boolean toTop = requestedPosition == POSITION_TOP;
3380 toTop |= adding ? requestedPosition >= topChildPosition + 1
3381 : requestedPosition >= topChildPosition;
3382 int targetPosition = requestedPosition;
3383
Wale Ogunwale61911492017-10-11 08:50:50 -07003384 if (toTop && stack.getWindowingMode() != WINDOWING_MODE_PINNED && hasPinnedStack()) {
Bryce Lee48f4b572017-04-10 10:54:15 -07003385 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003386 TaskStack topStack = mChildren.get(topChildPosition);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003387 if (topStack.getWindowingMode() != WINDOWING_MODE_PINNED) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003388 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3389 }
3390
3391 // So, stack is moved just below the pinned stack.
3392 // When we're adding a new stack the target is the current pinned stack position.
3393 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003394 // stack, because WindowContainer#positionAt() first removes element and then adds
3395 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003396 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3397 }
3398
3399 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003400 }
3401
3402 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003403 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3404 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003405 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003406 if (super.forAllWindows(callback, traverseTopToBottom)) {
3407 return true;
3408 }
3409 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3410 return true;
3411 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003412 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003413 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3414 return true;
3415 }
3416 if (super.forAllWindows(callback, traverseTopToBottom)) {
3417 return true;
3418 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003419 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003420 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003421 }
3422
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003423 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003424 boolean traverseTopToBottom) {
3425 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3426 // app tokens.
3427 // TODO: Investigate if we need to continue to do this or if we can just process them
3428 // in-order.
3429 if (traverseTopToBottom) {
3430 for (int i = mChildren.size() - 1; i >= 0; --i) {
3431 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3432 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003433 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3434 traverseTopToBottom)) {
3435 return true;
3436 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003437 }
3438 }
3439 } else {
3440 final int count = mChildren.size();
3441 for (int i = 0; i < count; ++i) {
3442 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3443 final int appTokensCount = appTokens.size();
3444 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003445 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3446 traverseTopToBottom)) {
3447 return true;
3448 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003449 }
3450 }
3451 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003452 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003453 }
3454
Wale Ogunwale1666e312016-12-16 11:27:18 -08003455 void setExitingTokensHasVisible(boolean hasVisible) {
3456 for (int i = mChildren.size() - 1; i >= 0; --i) {
3457 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3458 for (int j = appTokens.size() - 1; j >= 0; --j) {
3459 appTokens.get(j).hasVisible = hasVisible;
3460 }
3461 }
3462 }
3463
3464 void removeExistingAppTokensIfPossible() {
3465 for (int i = mChildren.size() - 1; i >= 0; --i) {
3466 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3467 for (int j = appTokens.size() - 1; j >= 0; --j) {
3468 final AppWindowToken token = appTokens.get(j);
3469 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3470 && (!token.mIsExiting || token.isEmpty())) {
3471 // Make sure there is no animation running on this token, so any windows
3472 // associated with it will be removed as soon as their animations are
3473 // complete.
3474 token.mAppAnimator.clearAnimation();
3475 token.mAppAnimator.animating = false;
3476 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3477 "performLayout: App token exiting now removed" + token);
3478 token.removeIfPossible();
3479 }
3480 }
3481 }
3482 }
3483
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003484 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003485 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003486 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3487 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003488 // Apps and their containers are not allowed to specify an orientation while the
3489 // docked or freeform stack is visible...except for the home stack/task if the
3490 // docked stack is minimized and it actually set something.
3491 if (mHomeStack != null && mHomeStack.isVisible()
3492 && mDividerControllerLocked.isMinimizedDock()) {
3493 final int orientation = mHomeStack.getOrientation();
3494 if (orientation != SCREEN_ORIENTATION_UNSET) {
3495 return orientation;
3496 }
3497 }
3498 return SCREEN_ORIENTATION_UNSPECIFIED;
3499 }
3500
3501 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003502 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3503 PackageManager.FEATURE_AUTOMOTIVE);
3504 if (isCar) {
3505 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3506 // allow anything but the default orientation.
3507 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3508 "Forcing UNSPECIFIED orientation in car. Ignoring " + orientation);
3509 return SCREEN_ORIENTATION_UNSPECIFIED;
3510 }
3511
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003512 if (orientation != SCREEN_ORIENTATION_UNSET
3513 && orientation != SCREEN_ORIENTATION_BEHIND) {
3514 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3515 "App is requesting an orientation, return " + orientation);
3516 return orientation;
3517 }
3518
3519 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003520 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003521 // The next app has not been requested to be visible, so we keep the current orientation
3522 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003523 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003524 }
Robert Carrb1579c82017-09-05 14:54:47 -07003525
3526 @Override
3527 void assignChildLayers(SurfaceControl.Transaction t) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003528 int layer = 0;
Robert Carrb1579c82017-09-05 14:54:47 -07003529
3530 // We allow stacks to change visual order from the AM specified order due to
3531 // Z-boosting during animations. However we must take care to ensure TaskStacks
3532 // which are marked as alwaysOnTop remain that way.
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003533 for (int i = 0; i < mChildren.size(); i++) {
3534 final TaskStack s = mChildren.get(i);
3535 s.assignChildLayers();
3536 if (!s.needsZBoost() && !s.isAlwaysOnTop()) {
3537 s.assignLayer(t, layer++);
Robert Carrb1579c82017-09-05 14:54:47 -07003538 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003539 }
3540 for (int i = 0; i < mChildren.size(); i++) {
3541 final TaskStack s = mChildren.get(i);
3542 if (s.needsZBoost() && !s.isAlwaysOnTop()) {
3543 s.assignLayer(t, layer++);
Robert Carrb1579c82017-09-05 14:54:47 -07003544 }
3545 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003546 for (int i = 0; i < mChildren.size(); i++) {
3547 final TaskStack s = mChildren.get(i);
3548 if (s.isAlwaysOnTop()) {
3549 s.assignLayer(t, layer++);
3550 }
3551 }
3552
3553 // The appropriate place for App-Transitions to occur is right
3554 // above all other animations but still below things in the Picture-and-Picture
3555 // windowing mode.
3556 if (mAnimationLayer != null) {
3557 t.setLayer(mAnimationLayer, layer++);
3558 }
Robert Carrb1579c82017-09-05 14:54:47 -07003559 }
3560
3561 @Override
3562 void onParentSet() {
3563 super.onParentSet();
3564 if (getParent() != null) {
3565 mAnimationLayer = makeSurface().build();
3566 } else {
3567 mAnimationLayer.destroy();
3568 mAnimationLayer = null;
3569 }
3570 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003571 }
3572
Robert Carree4d4b92017-11-22 12:21:46 -08003573 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003574 AboveAppWindowContainers(String name, WindowManagerService service) {
3575 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08003576 }
3577
3578 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
3579 boolean needAssignIme = imeContainer != null
3580 && imeContainer.getSurfaceControl() != null;
3581 for (int j = 0; j < mChildren.size(); ++j) {
3582 final WindowToken wt = mChildren.get(j);
3583 wt.assignLayer(t, j);
3584 wt.assignChildLayers(t);
3585
3586 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
3587 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08003588
3589 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
3590 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003591 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08003592 needAssignIme = false;
3593 }
3594 }
3595 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003596 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08003597 }
3598 }
3599 }
3600
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003601 /**
3602 * Window container class that contains all containers on this display that are not related to
3603 * Apps. E.g. status bar.
3604 */
Robert Carree4d4b92017-11-22 12:21:46 -08003605 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07003606 /**
3607 * Compares two child window tokens returns -1 if the first is lesser than the second in
3608 * terms of z-order and 1 otherwise.
3609 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003610 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003611 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003612 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3613 token1.mOwnerCanManageAppTokens)
3614 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3615 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003616
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003617 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3618 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3619 return false;
3620 }
3621 final int req = w.mAttrs.screenOrientation;
3622 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3623 || req == SCREEN_ORIENTATION_UNSET) {
3624 return false;
3625 }
3626 return true;
3627 };
3628
Wale Ogunwale3a931692016-11-02 16:49:48 -07003629 private final String mName;
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003630 NonAppWindowContainers(String name, WindowManagerService service) {
3631 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003632 mName = name;
3633 }
3634
3635 void addChild(WindowToken token) {
3636 addChild(token, mWindowComparator);
3637 }
3638
3639 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003640 int getOrientation() {
3641 final WindowManagerPolicy policy = mService.mPolicy;
3642 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003643 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003644
3645 if (win != null) {
3646 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003647 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003648 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003649 if (mService.mKeyguardGoingAway) {
3650 // Keyguard can't affect the orientation if it is going away...
3651 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3652 return SCREEN_ORIENTATION_UNSET;
3653 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003654 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003655 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003656 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003657 }
3658
Andrii Kulian8ee72852017-03-10 10:36:45 -08003659 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003660
Jorim Jaggi75520d52017-08-24 17:23:19 +02003661 if (policy.isKeyguardShowingAndNotOccluded()
3662 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003663 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003664 }
3665
3666 return SCREEN_ORIENTATION_UNSET;
3667 }
3668
3669 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003670 String getName() {
3671 return mName;
3672 }
Robert Carr3b716242016-08-16 16:02:21 -07003673 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003674
Robert Carrb1579c82017-09-05 14:54:47 -07003675 SurfaceControl.Builder makeSurface(SurfaceSession s) {
3676 return mService.makeSurfaceBuilder(s)
3677 .setParent(mWindowingLayer);
3678 }
3679
3680 @Override
3681 SurfaceSession getSession() {
3682 return mSession;
3683 }
3684
3685 @Override
3686 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003687 SurfaceSession s = child != null ? child.getSession() : getSession();
3688 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07003689 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07003690
3691 if (child == null) {
3692 return b;
3693 }
3694
Robert Carree4d4b92017-11-22 12:21:46 -08003695 return b.setName(child.getName())
3696 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07003697 }
3698
3699 /**
3700 * The makeSurface variants are for use by the window-container
3701 * hierarchy. makeOverlay here is a function for various non windowing
3702 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
3703 * and other potpourii.
3704 */
3705 SurfaceControl.Builder makeOverlay() {
3706 return mService.makeSurfaceBuilder(mSession)
3707 .setParent(mOverlayLayer);
3708 }
3709
3710 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003711 applyMagnificationSpec(getPendingTransaction(), spec);
3712 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07003713 }
3714
3715 @Override
3716 void onParentSet() {
3717 // Since we are the top of the SurfaceControl hierarchy here
3718 // we create the root surfaces explicitly rather than chaining
3719 // up as the default implementation in onParentSet does. So we
3720 // explicitly do NOT call super here.
3721 }
3722
3723 @Override
3724 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07003725
3726 // These are layers as children of "mWindowingLayer"
3727 mBelowAppWindowsContainers.assignLayer(t, 0);
3728 mTaskStackContainers.assignLayer(t, 1);
3729 mAboveAppWindowsContainers.assignLayer(t, 2);
3730
3731 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08003732 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08003733
Robert Carree4d4b92017-11-22 12:21:46 -08003734 // In the case where we have an IME target that is not in split-screen
3735 // mode IME assignment is easy. We just need the IME to go directly above
3736 // the target. This way children of the target will naturally go above the IME
3737 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08003738 //
Robert Carree4d4b92017-11-22 12:21:46 -08003739 // In the case of split-screen windowing mode, we need to elevate the IME above the
3740 // docked divider while keeping the app itself below the docked divider, so instead
3741 // we use relative layering of the IME targets child windows, and place the
3742 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08003743 //
Robert Carree4d4b92017-11-22 12:21:46 -08003744 // In the case where we have no IME target we assign it where it's base layer would
3745 // place it in the AboveAppWindowContainers.
3746 if (imeTarget != null && !imeTarget.inSplitScreenWindowingMode()
3747 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003748 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07003749 // TODO: We need to use an extra level on the app surface to ensure
3750 // this is always above SurfaceView but always below attached window.
3751 1);
Robert Carree4d4b92017-11-22 12:21:46 -08003752 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07003753 }
3754
3755 // Above we have assigned layers to our children, now we ask them to assign
3756 // layers to their children.
3757 mBelowAppWindowsContainers.assignChildLayers(t);
3758 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08003759 mAboveAppWindowsContainers.assignChildLayers(t,
3760 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07003761 mImeWindowsContainers.assignChildLayers(t);
3762 }
3763
3764 /**
3765 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
3766 * that the IME target is one of the docked applications. We'd like the docked divider to be
3767 * above both of the applications, and we'd like the IME to be above the docked divider.
3768 * However we need child windows of the applications to be above the IME (Text drag handles).
3769 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
3770 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
3771 * with {@link #WindowState#assignLayer}
3772 */
3773 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
3774 t.setRelativeLayer(child.getSurfaceControl(), mImeWindowsContainers.getSurfaceControl(), 1);
3775 }
3776
3777 @Override
3778 void destroyAfterPendingTransaction(SurfaceControl surface) {
3779 mPendingDestroyingSurfaces.add(surface);
3780 }
3781
3782 /**
3783 * Destroys any surfaces that have been put into the pending list with
3784 * {@link #destroyAfterTransaction}.
3785 */
3786 void onPendingTransactionApplied() {
3787 for (int i = mPendingDestroyingSurfaces.size() - 1; i >= 0; i--) {
3788 mPendingDestroyingSurfaces.get(i).destroy();
3789 }
3790 mPendingDestroyingSurfaces.clear();
3791 }
Craig Mautner59c00972012-07-30 12:10:24 -07003792}