blob: f458457520544069f4734ca70831a0aec6b22434 [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;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100149import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700150import android.view.SurfaceSession;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700151
Bryce Lee48f4b572017-04-10 10:54:15 -0700152import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800153import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700154import com.android.internal.view.IInputMethodClient;
Adrian Roose99bc052017-11-20 17:55:31 +0100155import com.android.server.policy.WindowManagerPolicy;
Craig Mautner59c00972012-07-30 12:10:24 -0700156
Robert Carr3b716242016-08-16 16:02:21 -0700157import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700158import java.io.PrintWriter;
159import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700160import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700161import java.util.HashMap;
162import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700163import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700164import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800165import java.util.function.Consumer;
166import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700167
Craig Mautner59c00972012-07-30 12:10:24 -0700168/**
169 * Utility class for keeping track of the WindowStates and other pertinent contents of a
170 * particular Display.
171 *
172 * IMPORTANT: No method from this class should ever be used without holding
173 * WindowManagerService.mWindowMap.
174 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700175class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700176 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700177
178 /** Unique identifier of this stack. */
179 private final int mDisplayId;
180
Wale Ogunwale3a931692016-11-02 16:49:48 -0700181 /** The containers below are the only child containers the display can have. */
182 // Contains all window containers that are related to apps (Activities)
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100183 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700184 // Contains all non-app window containers that should be displayed above the app containers
185 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800186 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100187 new AboveAppWindowContainers("mAboveAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700188 // Contains all non-app window containers that should be displayed below the app containers
189 // (e.g. Wallpaper).
190 private final NonAppWindowContainers mBelowAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100191 new NonAppWindowContainers("mBelowAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700192 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
193 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
194 // window containers together and move them in-sync if/when needed.
195 private final NonAppWindowContainers mImeWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100196 new NonAppWindowContainers("mImeWindowsContainers", mService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700197
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000198 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800199 private WindowState mTmpWindow2;
200 private WindowAnimator mTmpWindowAnimator;
201 private boolean mTmpRecoveringMemory;
202 private boolean mUpdateImeTarget;
203 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700204 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700205
Wale Ogunwale02319a62016-09-26 15:21:22 -0700206 // Mapping from a token IBinder to a WindowToken object on this display.
207 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
208
Andrii Kuliancd097992017-03-23 18:31:59 -0700209 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700210 int mInitialDisplayWidth = 0;
211 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700212 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700213
214 /**
215 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
216 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
217 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
218 */
Craig Mautner59c00972012-07-30 12:10:24 -0700219 int mBaseDisplayWidth = 0;
220 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700221 /**
222 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
223 * but can be set from Settings or via shell command "adb shell wm density".
224 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
225 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700226 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700227 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700228 private final DisplayInfo mDisplayInfo = new DisplayInfo();
229 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700230 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000231 DisplayFrames mDisplayFrames;
232
Andrii Kulian06d07d62017-03-14 11:11:47 -0700233 /**
234 * For default display it contains real metrics, empty for others.
235 * @see WindowManagerService#createWatermarkInTransaction()
236 */
237 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
238 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
239 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700240
Andrii Kulian06d07d62017-03-14 11:11:47 -0700241 /**
242 * Compat metrics computed based on {@link #mDisplayMetrics}.
243 * @see #updateDisplayAndOrientation(int)
244 */
245 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
246
247 /** The desired scaling factor for compatible apps. */
248 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700249
Andrii Kulian8ee72852017-03-10 10:36:45 -0800250 /**
251 * Current rotation of the display.
252 * Constants as per {@link android.view.Surface.Rotation}.
253 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700254 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800255 */
256 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700257
Andrii Kulian8ee72852017-03-10 10:36:45 -0800258 /**
259 * Last applied orientation of the display.
260 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
261 *
262 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
263 */
264 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700265
Andrii Kulian8ee72852017-03-10 10:36:45 -0800266 /**
267 * Flag indicating that the application is receiving an orientation that has different metrics
268 * than it expected. E.g. Portrait instead of Landscape.
269 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700270 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800271 */
272 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700273
Andrii Kulian8ee72852017-03-10 10:36:45 -0800274 /**
275 * Orientation forced by some window. If there is no visible window that specifies orientation
276 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
277 *
278 * @see NonAppWindowContainers#getOrientation()
279 */
280 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700281
Andrii Kulian8ee72852017-03-10 10:36:45 -0800282 /**
283 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
284 * occluded.
285 *
286 * @see NonAppWindowContainers#getOrientation()
287 */
288 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
289
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700290 /**
291 * Keep track of wallpaper visibility to notify changes.
292 */
293 private boolean mLastWallpaperVisible = false;
294
Andrii Kulian06d07d62017-03-14 11:11:47 -0700295 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700296
Craig Mautner39834192012-09-02 07:47:24 -0700297 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700298 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700299 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700300 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800301 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700302
Craig Mautnerdc548482014-02-05 13:35:24 -0800303 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700304 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800305
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700306 /** Detect user tapping outside of current focused task bounds .*/
307 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700308
Craig Mautner6601b7b2013-04-29 10:29:11 -0700309 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700310 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700311
Craig Mautner6601b7b2013-04-29 10:29:11 -0700312 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800313 private final Rect mTmpRect = new Rect();
314 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700315 private final RectF mTmpRectF = new RectF();
316 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800317 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700318
Bryce Leef3c6a472017-11-14 14:53:06 -0800319 /** Used for handing back size of display */
320 private final Rect mTmpBounds = new Rect();
321
Craig Mautner95da1082014-02-24 17:54:35 -0800322 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700323 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800324
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700325 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700326 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700327
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800328 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
329
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000330 private boolean mHaveBootMsg = false;
331 private boolean mHaveApp = false;
332 private boolean mHaveWallpaper = false;
333 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700334
335 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
336
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700337 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
338 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000339 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
340 new ApplySurfaceChangesTransactionState();
341 private final ScreenshotApplicationState mScreenshotApplicationState =
342 new ScreenshotApplicationState();
Jorim Jaggia5e10572017-11-15 14:36:26 +0100343 private final Transaction mTmpTransaction = new Transaction();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700344
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700345 // True if this display is in the process of being removed. Used to determine if the removal of
346 // the display's direct children should be allowed.
347 private boolean mRemovingDisplay = false;
348
Bryce Leed1871262017-06-12 14:12:29 -0700349 // {@code false} if this display is in the processing of being created.
350 private boolean mDisplayReady = false;
351
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800352 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700353 int mInputMethodAnimLayerAdjustment;
354
Robert Carrb1579c82017-09-05 14:54:47 -0700355 private final SurfaceSession mSession = new SurfaceSession();
356
357 /**
358 * We organize all top-level Surfaces in to the following layers.
359 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800360 * and omitted from Screen-Magnification, for example the strict mode flash or
361 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700362 * {@link #mWindowingLayer} contains everything else.
363 */
364 private SurfaceControl mOverlayLayer;
365
366 /**
367 * See {@link #mOverlayLayer}
368 */
369 private SurfaceControl mWindowingLayer;
370
371 /**
372 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
373 * <p>
374 * For these surfaces currently we use a surface based on the larger of width or height so we
375 * don't have to resize when rotating the display.
376 */
377 private int mSurfaceSize;
378
379 /**
380 * A list of surfaces to be destroyed after {@link #mPendingTransaction} is applied.
381 */
382 private final ArrayList<SurfaceControl> mPendingDestroyingSurfaces = new ArrayList<>();
383
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100384 /** Temporary float array to retrieve 3x3 matrix values. */
385 private final float[] mTmpFloats = new float[9];
386
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800387 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
388 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800389 final AppWindowToken atoken = w.mAppToken;
390 if (winAnimator.mDrawState == READY_TO_SHOW) {
391 if (atoken == null || atoken.allDrawn) {
392 if (w.performShowLocked()) {
393 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
394 if (DEBUG_LAYOUT_REPEATS) {
395 mService.mWindowPlacerLocked.debugLayoutRepeats(
396 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
397 }
398 }
399 }
400 }
401 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
402 if (appAnimator != null && appAnimator.thumbnail != null) {
403 if (appAnimator.thumbnailTransactionSeq
404 != mTmpWindowAnimator.mAnimTransactionSequence) {
405 appAnimator.thumbnailTransactionSeq =
406 mTmpWindowAnimator.mAnimTransactionSequence;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800407 }
408 }
409 };
410
411 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
412 final WindowStateAnimator winAnimator = w.mWinAnimator;
413 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
414 return;
415 }
416
417 final int flags = w.mAttrs.flags;
418
419 // If this window is animating, make a note that we have an animating window and take
420 // care of a request to run a detached wallpaper animation.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100421 if (winAnimator.isAnimationSet()) {
422 final AnimationAdapter anim = w.getAnimation();
423 if (anim != null) {
424 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && anim.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800425 mTmpWindow = w;
426 }
Jorim Jaggia5e10572017-11-15 14:36:26 +0100427 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800428 if (color != 0) {
429 final TaskStack stack = w.getStack();
430 if (stack != null) {
431 stack.setAnimationBackground(winAnimator, color);
432 }
433 }
434 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800435 }
436
437 // If this window's app token is running a detached wallpaper animation, make a note so
438 // we can ensure the wallpaper is displayed behind it.
439 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
440 if (appAnimator != null && appAnimator.animation != null
441 && appAnimator.animating) {
442 if ((flags & FLAG_SHOW_WALLPAPER) != 0
443 && appAnimator.animation.getDetachWallpaper()) {
444 mTmpWindow = w;
445 }
446
447 final int color = appAnimator.animation.getBackgroundColor();
448 if (color != 0) {
449 final TaskStack stack = w.getStack();
450 if (stack != null) {
451 stack.setAnimationBackground(winAnimator, color);
452 }
453 }
454 }
455 };
456
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800457 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
458 final int lostFocusUid = mTmpWindow.mOwnerUid;
459 final Handler handler = mService.mH;
460 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
461 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
462 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
463 w.mAttrs.hideTimeoutMilliseconds);
464 }
465 }
466 };
467
468 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
469 final AppWindowToken focusedApp = mService.mFocusedApp;
470 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
471 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
472
473 if (!w.canReceiveKeys()) {
474 return false;
475 }
476
477 final AppWindowToken wtoken = w.mAppToken;
478
479 // If this window's application has been removed, just skip it.
480 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
481 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
482 + (wtoken.removed ? "removed" : "sendingToBottom"));
483 return false;
484 }
485
486 if (focusedApp == null) {
487 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
488 + " using new focus @ " + w);
489 mTmpWindow = w;
490 return true;
491 }
492
493 if (!focusedApp.windowsAreFocusable()) {
494 // Current focused app windows aren't focusable...
495 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
496 + " focusable using new focus @ " + w);
497 mTmpWindow = w;
498 return true;
499 }
500
501 // Descend through all of the app tokens and find the first that either matches
502 // win.mAppToken (return win) or mFocusedApp (return null).
503 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
504 if (focusedApp.compareTo(wtoken) > 0) {
505 // App stack below focused app stack. No focus for you!!!
506 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
507 "findFocusedWindow: Reached focused app=" + focusedApp);
508 mTmpWindow = null;
509 return true;
510 }
511 }
512
513 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
514 mTmpWindow = w;
515 return true;
516 };
517
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800518 private final Consumer<WindowState> mPerformLayout = w -> {
519 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
520 // wasting time and funky changes while a window is animating away.
521 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
522 || w.isGoneForLayoutLw();
523
524 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
525 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
526 + " mLayoutAttached=" + w.mLayoutAttached
527 + " screen changed=" + w.isConfigChanged());
528 final AppWindowToken atoken = w.mAppToken;
529 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
530 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
531 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
532 + " parentHidden=" + w.isParentWindowHidden());
533 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
534 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
535 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
536 + " parentHidden=" + w.isParentWindowHidden());
537 }
538
539 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
540 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
541 // since that means "perform layout as normal, just don't display").
542 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
543 || ((w.isConfigChanged() || w.setReportResizeHints())
544 && !w.isGoneForLayoutLw() &&
545 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
546 (w.mHasSurface && w.mAppToken != null &&
547 w.mAppToken.layoutConfigChanges)))) {
548 if (!w.mLayoutAttached) {
549 if (mTmpInitial) {
550 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
551 w.mContentChanged = false;
552 }
553 if (w.mAttrs.type == TYPE_DREAM) {
554 // Don't layout windows behind a dream, so that if it does stuff like hide
555 // the status bar we won't get a bad transition when it goes away.
556 mTmpWindow = w;
557 }
558 w.mLayoutNeeded = false;
559 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200560 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000561 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800562 w.mLayoutSeq = mService.mLayoutSeq;
563
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200564 // If this is the first layout, we need to initialize the last inset values as
565 // otherwise we'd immediately cause an unnecessary resize.
566 if (firstLayout) {
567 w.updateLastInsetValues();
568 }
569
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800570 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
571 + " mContainingFrame=" + w.mContainingFrame
572 + " mDisplayFrame=" + w.mDisplayFrame);
573 }
574 }
575 };
576
577 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
578 if (w.mLayoutAttached) {
579 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
580 + " mViewVisibility=" + w.mViewVisibility
581 + " mRelayoutCalled=" + w.mRelayoutCalled);
582 // If this view is GONE, then skip it -- keep the current frame, and let the caller
583 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
584 // windows, since that means "perform layout as normal, just don't display").
585 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
586 return;
587 }
588 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
589 || w.mLayoutNeeded) {
590 if (mTmpInitial) {
591 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
592 w.mContentChanged = false;
593 }
594 w.mLayoutNeeded = false;
595 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000596 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800597 w.mLayoutSeq = mService.mLayoutSeq;
598 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
599 + " mContainingFrame=" + w.mContainingFrame
600 + " mDisplayFrame=" + w.mDisplayFrame);
601 }
602 } else if (w.mAttrs.type == TYPE_DREAM) {
603 // Don't layout windows behind a dream, so that if it does stuff like hide the
604 // status bar we won't get a bad transition when it goes away.
605 mTmpWindow = mTmpWindow2;
606 }
607 };
608
609 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
610 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
611 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
612 return w.canBeImeTarget();
613 };
614
615 private final Consumer<WindowState> mApplyPostLayoutPolicy =
616 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
617 mService.mInputMethodTarget);
618
619 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
620 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
621 final boolean obscuredChanged = w.mObscured !=
622 mTmpApplySurfaceChangesTransactionState.obscured;
623 final RootWindowContainer root = mService.mRoot;
624 // Only used if default window
625 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
626
627 // Update effect.
628 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
629 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
630 final boolean isDisplayed = w.isDisplayedLw();
631
632 if (isDisplayed && w.isObscuringDisplay()) {
633 // This window completely covers everything behind it, so we want to leave all
634 // of them as undimmed (for performance reasons).
635 root.mObscuringWindow = w;
636 mTmpApplySurfaceChangesTransactionState.obscured = true;
637 }
638
639 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
640 root.handleNotObscuredLocked(w,
641 mTmpApplySurfaceChangesTransactionState.obscured,
642 mTmpApplySurfaceChangesTransactionState.syswin);
643
644 if (w.mHasSurface && isDisplayed) {
645 final int type = w.mAttrs.type;
646 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
647 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
648 mTmpApplySurfaceChangesTransactionState.syswin = true;
649 }
650 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
651 && w.mAttrs.preferredRefreshRate != 0) {
652 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
653 = w.mAttrs.preferredRefreshRate;
654 }
655 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
656 && w.mAttrs.preferredDisplayModeId != 0) {
657 mTmpApplySurfaceChangesTransactionState.preferredModeId
658 = w.mAttrs.preferredDisplayModeId;
659 }
660 }
661 }
662
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800663 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
664 && mWallpaperController.isWallpaperTarget(w)) {
665 // This is the wallpaper target and its obscured state changed... make sure the
666 // current wallpaper's visibility has been updated accordingly.
667 mWallpaperController.updateWallpaperVisibility();
668 }
669
Jorim Jaggia5e10572017-11-15 14:36:26 +0100670 // Use mTmpTransaction instead of mPendingTransaction because we don't want to commit
671 // other changes in mPendingTransaction at this point.
672 w.handleWindowMovedIfNeeded(mTmpTransaction);
673 SurfaceControl.mergeToGlobalTransaction(mTmpTransaction);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800674
675 final WindowStateAnimator winAnimator = w.mWinAnimator;
676
677 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
678 w.mContentChanged = false;
679
680 // Moved from updateWindowsAndWallpaperLocked().
681 if (w.mHasSurface) {
682 // Take care of the window being ready to display.
683 final boolean committed = winAnimator.commitFinishDrawingLocked();
684 if (isDefaultDisplay && committed) {
685 if (w.mAttrs.type == TYPE_DREAM) {
686 // HACK: When a dream is shown, it may at that point hide the lock screen.
687 // So we need to redo the layout to let the phone window manager make this
688 // happen.
689 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
690 if (DEBUG_LAYOUT_REPEATS) {
691 surfacePlacer.debugLayoutRepeats(
692 "dream and commitFinishDrawingLocked true",
693 pendingLayoutChanges);
694 }
695 }
696 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
697 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
698 "First draw done in potential wallpaper target " + w);
699 root.mWallpaperMayChange = true;
700 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
701 if (DEBUG_LAYOUT_REPEATS) {
702 surfacePlacer.debugLayoutRepeats(
703 "wallpaper and commitFinishDrawingLocked true",
704 pendingLayoutChanges);
705 }
706 }
707 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700708 final TaskStack stack = w.getStack();
Jorim Jaggia5e10572017-11-15 14:36:26 +0100709 if ((!winAnimator.isWaitingForOpening())
Robert Carr2117aaf2017-04-25 14:46:50 -0700710 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800711 // Updates the shown frame before we set up the surface. This is needed
712 // because the resizing could change the top-left position (in addition to
713 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
714 // position the surface.
715 //
716 // If an animation is being started, we can't call this method because the
717 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700718 // we do want to update the position if the window is animating. We make an exception
719 // for the bounds animating state, where an application may have been waiting
720 // for an exit animation to start, but instead enters PiP. We need to ensure
721 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800722 winAnimator.computeShownFrameLocked();
723 }
724 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
Jorim Jaggia5e10572017-11-15 14:36:26 +0100725
726 // Since setSurfaceBoundariesLocked applies the clipping, we need to apply the position
727 // to the surface of the window container as well. Use mTmpTransaction instead of
728 // mPendingTransaction to avoid committing any existing changes in there.
729 w.updateSurfacePosition(mTmpTransaction);
730 SurfaceControl.mergeToGlobalTransaction(mTmpTransaction);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800731 }
732
733 final AppWindowToken atoken = w.mAppToken;
734 if (atoken != null) {
735 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
736 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
737 mTmpUpdateAllDrawn.add(atoken);
738 }
739 }
740
741 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
742 && w.isDisplayedLw()) {
743 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
744 }
745
746 w.updateResizingWindowIfNeeded();
747 };
748
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800749 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800750 * Create new {@link DisplayContent} instance, add itself to the root window container and
751 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700752 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800753 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700754 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
755 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700756 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700757 DisplayContent(Display display, WindowManagerService service,
Robert Carrb1579c82017-09-05 14:54:47 -0700758 WallpaperController wallpaperController) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100759 super(service);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800760 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
761 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
762 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
763 + " new=" + display);
764 }
765
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700766 mDisplay = display;
767 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700768 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700769 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700770 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700771 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000772 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700773 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800774 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700775 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700776
Robert Carrb1579c82017-09-05 14:54:47 -0700777 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth);
778
779 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
780 .setSize(mSurfaceSize, mSurfaceSize)
781 .setOpaque(true);
782 mWindowingLayer = b.setName("Display Root").build();
783 mOverlayLayer = b.setName("Display Overlays").build();
784
Robert Carrf59b8dd2017-10-02 18:58:36 -0700785 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700786 .setLayerStack(mWindowingLayer, mDisplayId)
787 .show(mWindowingLayer)
788 .setLayer(mOverlayLayer, 1)
789 .setLayerStack(mOverlayLayer, mDisplayId)
790 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700791 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700792
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700793 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700794 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700795 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700796 super.addChild(mAboveAppWindowsContainers, null);
797 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800798
799 // Add itself as a child to the root container.
800 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700801
802 // TODO(b/62541591): evaluate whether this is the best spot to declare the
803 // {@link DisplayContent} ready for use.
804 mDisplayReady = true;
805 }
806
807 boolean isReady() {
808 // The display is ready when the system and the individual display are both ready.
809 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700810 }
811
812 int getDisplayId() {
813 return mDisplayId;
814 }
815
Wale Ogunwale02319a62016-09-26 15:21:22 -0700816 WindowToken getWindowToken(IBinder binder) {
817 return mTokenMap.get(binder);
818 }
819
820 AppWindowToken getAppWindowToken(IBinder binder) {
821 final WindowToken token = getWindowToken(binder);
822 if (token == null) {
823 return null;
824 }
825 return token.asAppWindowToken();
826 }
827
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700828 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700829 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
830 if (dc != null) {
831 // We currently don't support adding a window token to the display if the display
832 // already has the binder mapped to another token. If there is a use case for supporting
833 // this moving forward we will either need to merge the WindowTokens some how or have
834 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700835 throw new IllegalArgumentException("Can't map token=" + token + " to display="
836 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700837 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700838 if (binder == null) {
839 throw new IllegalArgumentException("Can't map token=" + token + " to display="
840 + getName() + " binder is null");
841 }
842 if (token == null) {
843 throw new IllegalArgumentException("Can't map null token to display="
844 + getName() + " binder=" + binder);
845 }
846
Wale Ogunwale02319a62016-09-26 15:21:22 -0700847 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700848
849 if (token.asAppWindowToken() == null) {
850 // Add non-app token to container hierarchy on the display. App tokens are added through
851 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700852 switch (token.windowType) {
853 case TYPE_WALLPAPER:
854 mBelowAppWindowsContainers.addChild(token);
855 break;
856 case TYPE_INPUT_METHOD:
857 case TYPE_INPUT_METHOD_DIALOG:
858 mImeWindowsContainers.addChild(token);
859 break;
860 default:
861 mAboveAppWindowsContainers.addChild(token);
862 break;
863 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700864 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700865 }
866
867 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700868 final WindowToken token = mTokenMap.remove(binder);
869 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800870 token.setExiting();
871 }
872 return token;
873 }
874
875 /** Changes the display the input window token is housed on to this one. */
876 void reParentWindowToken(WindowToken token) {
877 final DisplayContent prevDc = token.getDisplayContent();
878 if (prevDc == this) {
879 return;
880 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800881 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
882 && token.asAppWindowToken() == null) {
883 // Removed the token from the map, but made sure it's not an app token before removing
884 // from parent.
885 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700886 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800887
888 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700889 }
890
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700891 void removeAppToken(IBinder binder) {
892 final WindowToken token = removeWindowToken(binder);
893 if (token == null) {
894 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
895 return;
896 }
897
898 final AppWindowToken appToken = token.asAppWindowToken();
899
900 if (appToken == null) {
901 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
902 return;
903 }
904
905 appToken.onRemovedFromDisplay();
906 }
907
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700908 Display getDisplay() {
909 return mDisplay;
910 }
911
Craig Mautner59c00972012-07-30 12:10:24 -0700912 DisplayInfo getDisplayInfo() {
913 return mDisplayInfo;
914 }
915
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700916 DisplayMetrics getDisplayMetrics() {
917 return mDisplayMetrics;
918 }
919
Andrii Kulian8ee72852017-03-10 10:36:45 -0800920 int getRotation() {
921 return mRotation;
922 }
923
924 void setRotation(int newRotation) {
925 mRotation = newRotation;
926 }
927
928 int getLastOrientation() {
929 return mLastOrientation;
930 }
931
932 void setLastOrientation(int orientation) {
933 mLastOrientation = orientation;
934 }
935
936 boolean getAltOrientation() {
937 return mAltOrientation;
938 }
939
940 void setAltOrientation(boolean altOrientation) {
941 mAltOrientation = altOrientation;
942 }
943
944 int getLastWindowForcedOrientation() {
945 return mLastWindowForcedOrientation;
946 }
947
Andrii Kulian06d07d62017-03-14 11:11:47 -0700948 /**
949 * Update rotation of the display.
950 *
951 * Returns true if the rotation has been changed. In this case YOU MUST CALL
952 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
953 */
954 boolean updateRotationUnchecked(boolean inTransaction) {
955 if (mService.mDeferredRotationPauseCount > 0) {
956 // Rotation updates have been paused temporarily. Defer the update until
957 // updates have been resumed.
958 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
959 return false;
960 }
961
962 ScreenRotationAnimation screenRotationAnimation =
963 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
964 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
965 // Rotation updates cannot be performed while the previous rotation change
966 // animation is still in progress. Skip this update. We will try updating
967 // again after the animation is finished and the display is unfrozen.
968 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
969 return false;
970 }
971 if (mService.mDisplayFrozen) {
972 // Even if the screen rotation animation has finished (e.g. isAnimating
973 // returns false), there is still some time where we haven't yet unfrozen
974 // the display. We also need to abort rotation here.
975 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
976 "Deferring rotation, still finishing previous rotation");
977 return false;
978 }
979
980 if (!mService.mDisplayEnabled) {
981 // No point choosing a rotation if the display is not enabled.
982 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
983 return false;
984 }
985
986 final int oldRotation = mRotation;
987 final int lastOrientation = mLastOrientation;
988 final boolean oldAltOrientation = mAltOrientation;
989 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr0e007272017-10-02 13:00:55 -0700990 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
Robert Carr897215d2017-03-29 12:25:50 -0700991 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700992
Robert Carr0e007272017-10-02 13:00:55 -0700993 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700994 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
995 if (seamlessRotated != null) {
996 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
997 // to complete (that is, waiting for windows to redraw). It's tempting to check
998 // w.mSeamlessRotationCount but that could be incorrect in the case of
999 // window-removal.
1000 return false;
1001 }
Robert Carr0e007272017-10-02 13:00:55 -07001002
1003 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001004 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001005 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001006 mayRotateSeamlessly = false;
1007 }
1008 for (int i = 0; i < mService.mSessions.size(); i++) {
1009 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1010 mayRotateSeamlessly = false;
1011 break;
1012 }
1013 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001014 }
Robert Carr0e007272017-10-02 13:00:55 -07001015 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001016
1017 // TODO: Implement forced rotation changes.
1018 // Set mAltOrientation to indicate that the application is receiving
1019 // an orientation that has different metrics than it expected.
1020 // eg. Portrait instead of Landscape.
1021
1022 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
1023 lastOrientation, rotation);
1024
1025 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
1026 + ", got rotation " + rotation + " which has "
1027 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1028
1029 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1030 // No change.
1031 return false;
1032 }
1033
1034 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
1035 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1036 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1037
1038 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1039 mService.mWaitingForConfig = true;
1040 }
1041
1042 mRotation = rotation;
1043 mAltOrientation = altOrientation;
1044 if (isDefaultDisplay) {
1045 mService.mPolicy.setRotationLw(rotation);
1046 }
1047
1048 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1049 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1050 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1051 WINDOW_FREEZE_TIMEOUT_DURATION);
1052
1053 setLayoutNeeded();
1054 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001055 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001056
1057 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001058 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001059 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1060 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1061 mDisplayId);
1062 } else {
1063 // The screen rotation animation uses a screenshot to freeze the screen
1064 // while windows resize underneath.
1065 // When we are rotating seamlessly, we allow the elements to transition
1066 // to their rotated state independently and without a freeze required.
1067 screenRotationAnimation = null;
1068
1069 // We have to reset this in case a window was removed before it
1070 // finished seamless rotation.
1071 mService.mSeamlessRotationCount = 0;
1072 }
1073
1074 // We need to update our screen size information to match the new rotation. If the rotation
1075 // has actually changed then this method will return true and, according to the comment at
1076 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1077 // By updating the Display info here it will be available to
1078 // #computeScreenConfiguration() later.
1079 updateDisplayAndOrientation(getConfiguration().uiMode);
1080
1081 if (!inTransaction) {
1082 if (SHOW_TRANSACTIONS) {
1083 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1084 }
1085 mService.openSurfaceTransaction();
1086 }
1087 try {
1088 // NOTE: We disable the rotation in the emulator because
1089 // it doesn't support hardware OpenGL emulation yet.
1090 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1091 && screenRotationAnimation.hasScreenshot()) {
Robert Carrb1579c82017-09-05 14:54:47 -07001092 if (screenRotationAnimation.setRotationInTransaction(rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001093 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1094 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1095 mService.scheduleAnimationLocked();
1096 }
1097 }
1098
1099 if (rotateSeamlessly) {
1100 forAllWindows(w -> {
1101 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1102 }, true /* traverseTopToBottom */);
1103 }
1104
1105 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1106 } finally {
1107 if (!inTransaction) {
Adrian Roos111aff92017-09-27 18:11:46 +02001108 mService.closeSurfaceTransaction("setRotationUnchecked");
Andrii Kulian06d07d62017-03-14 11:11:47 -07001109 if (SHOW_LIGHT_TRANSACTIONS) {
1110 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1111 }
1112 }
1113 }
1114
1115 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001116 if (w.mHasSurface && !rotateSeamlessly) {
1117 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001118 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001119 mService.mRoot.mOrientationChangeComplete = false;
1120 w.mLastFreezeDuration = 0;
1121 }
1122 w.mReportOrientationChanged = true;
1123 }, true /* traverseTopToBottom */);
1124
1125 if (rotateSeamlessly) {
1126 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1127 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1128 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1129 }
1130
1131 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1132 final WindowManagerService.RotationWatcher rotationWatcher
1133 = mService.mRotationWatchers.get(i);
1134 if (rotationWatcher.mDisplayId == mDisplayId) {
1135 try {
1136 rotationWatcher.mWatcher.onRotationChanged(rotation);
1137 } catch (RemoteException e) {
1138 // Ignore
1139 }
1140 }
1141 }
1142
1143 // TODO (multi-display): Magnification is supported only for the default display.
1144 // Announce rotation only if we will not animate as we already have the
1145 // windows in final state. Otherwise, we make this call at the rotation end.
1146 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1147 && isDefaultDisplay) {
1148 mService.mAccessibilityController.onRotationChangedLocked(this);
1149 }
1150
1151 return true;
1152 }
1153
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001154 void configureDisplayPolicy() {
1155 mService.mPolicy.setInitialDisplaySize(getDisplay(),
1156 mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1157
1158 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
1159 }
1160
Andrii Kulian06d07d62017-03-14 11:11:47 -07001161 /**
1162 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1163 * changed.
1164 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1165 */
1166 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1167 // Use the effective "visual" dimensions based on current rotation
1168 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1169 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1170 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1171 int dw = realdw;
1172 int dh = realdh;
1173
1174 if (mAltOrientation) {
1175 if (realdw > realdh) {
1176 // Turn landscape into portrait.
1177 int maxw = (int)(realdh/1.3f);
1178 if (maxw < realdw) {
1179 dw = maxw;
1180 }
1181 } else {
1182 // Turn portrait into landscape.
1183 int maxh = (int)(realdw/1.3f);
1184 if (maxh < realdh) {
1185 dh = maxh;
1186 }
1187 }
1188 }
1189
1190 // Update application display metrics.
1191 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1192 mDisplayId);
1193 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1194 mDisplayId);
1195 mDisplayInfo.rotation = mRotation;
1196 mDisplayInfo.logicalWidth = dw;
1197 mDisplayInfo.logicalHeight = dh;
1198 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1199 mDisplayInfo.appWidth = appWidth;
1200 mDisplayInfo.appHeight = appHeight;
1201 if (isDefaultDisplay) {
1202 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1203 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1204 }
1205 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1206 if (mDisplayScalingDisabled) {
1207 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1208 } else {
1209 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1210 }
1211
1212 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1213 mDisplayInfo);
1214
1215 mBaseDisplayRect.set(0, 0, dw, dh);
1216
1217 if (isDefaultDisplay) {
1218 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1219 mCompatDisplayMetrics);
1220 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001221
1222 updateBounds();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001223 return mDisplayInfo;
1224 }
1225
1226 /**
1227 * Compute display configuration based on display properties and policy settings.
1228 * Do not call if mDisplayReady == false.
1229 */
1230 void computeScreenConfiguration(Configuration config) {
1231 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1232
1233 final int dw = displayInfo.logicalWidth;
1234 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001235 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1236 // TODO: Probably best to set this based on some setting in the display content object,
1237 // so the display can be configured for things like fullscreen.
1238 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001239
Andrii Kulian06d07d62017-03-14 11:11:47 -07001240 config.screenWidthDp =
1241 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1242 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1243 config.screenHeightDp =
1244 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1245 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001246
1247 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1248 final int leftInset = mTmpRect.left;
1249 final int topInset = mTmpRect.top;
1250 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001251 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1252 leftInset + displayInfo.appWidth /* right */,
1253 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001254 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1255 || displayInfo.rotation == Surface.ROTATION_270);
1256
1257 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1258 mDisplayMetrics.density, config);
1259
1260 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1261 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1262 ? Configuration.SCREENLAYOUT_ROUND_YES
1263 : Configuration.SCREENLAYOUT_ROUND_NO);
1264
1265 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1266 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1267 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1268 dh, mDisplayId);
1269 config.densityDpi = displayInfo.logicalDensityDpi;
1270
1271 config.colorMode =
1272 (displayInfo.isHdr()
1273 ? Configuration.COLOR_MODE_HDR_YES
1274 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001275 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001276 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1277 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1278
1279 // Update the configuration based on available input devices, lid switch,
1280 // and platform configuration.
1281 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1282 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1283 config.navigation = Configuration.NAVIGATION_NONAV;
1284
1285 int keyboardPresence = 0;
1286 int navigationPresence = 0;
1287 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1288 final int len = devices != null ? devices.length : 0;
1289 for (int i = 0; i < len; i++) {
1290 InputDevice device = devices[i];
1291 if (!device.isVirtual()) {
1292 final int sources = device.getSources();
1293 final int presenceFlag = device.isExternal() ?
1294 WindowManagerPolicy.PRESENCE_EXTERNAL :
1295 WindowManagerPolicy.PRESENCE_INTERNAL;
1296
1297 // TODO(multi-display): Configure on per-display basis.
1298 if (mService.mIsTouchDevice) {
1299 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1300 InputDevice.SOURCE_TOUCHSCREEN) {
1301 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1302 }
1303 } else {
1304 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1305 }
1306
1307 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1308 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1309 navigationPresence |= presenceFlag;
1310 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1311 && config.navigation == Configuration.NAVIGATION_NONAV) {
1312 config.navigation = Configuration.NAVIGATION_DPAD;
1313 navigationPresence |= presenceFlag;
1314 }
1315
1316 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1317 config.keyboard = Configuration.KEYBOARD_QWERTY;
1318 keyboardPresence |= presenceFlag;
1319 }
1320 }
1321 }
1322
1323 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1324 config.navigation = Configuration.NAVIGATION_DPAD;
1325 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1326 }
1327
1328 // Determine whether a hard keyboard is available and enabled.
1329 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1330 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1331 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1332 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1333 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1334 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1335 }
1336
1337 // Let the policy update hidden states.
1338 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1339 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1340 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1341 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1342 }
1343
1344 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1345 int displayId) {
1346 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1347 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1348 final int unrotDw, unrotDh;
1349 if (rotated) {
1350 unrotDw = dh;
1351 unrotDh = dw;
1352 } else {
1353 unrotDw = dw;
1354 unrotDh = dh;
1355 }
1356 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1357 displayId);
1358 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1359 displayId);
1360 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1361 displayId);
1362 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1363 displayId);
1364 return sw;
1365 }
1366
1367 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1368 DisplayMetrics dm, int dw, int dh, int displayId) {
1369 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1370 displayId);
1371 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1372 uiMode, displayId);
1373 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1374 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1375 if (curSize == 0 || size < curSize) {
1376 curSize = size;
1377 }
1378 return curSize;
1379 }
1380
1381 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1382 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1383
1384 // We need to determine the smallest width that will occur under normal
1385 // operation. To this, start with the base screen size and compute the
1386 // width under the different possible rotations. We need to un-rotate
1387 // the current screen dimensions before doing this.
1388 int unrotDw, unrotDh;
1389 if (rotated) {
1390 unrotDw = dh;
1391 unrotDh = dw;
1392 } else {
1393 unrotDw = dw;
1394 unrotDh = dh;
1395 }
1396 displayInfo.smallestNominalAppWidth = 1<<30;
1397 displayInfo.smallestNominalAppHeight = 1<<30;
1398 displayInfo.largestNominalAppWidth = 0;
1399 displayInfo.largestNominalAppHeight = 0;
1400 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1401 unrotDh);
1402 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1403 unrotDw);
1404 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1405 unrotDh);
1406 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1407 unrotDw);
1408 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1409 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1410 displayId);
1411 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1412 displayId);
1413 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1414 displayId);
1415 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1416 displayId);
1417 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1418 outConfig.screenLayout = sl;
1419 }
1420
1421 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1422 int uiMode, int displayId) {
1423 // Get the app screen size at this rotation.
1424 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1425 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1426
1427 // Compute the screen layout size class for this rotation.
1428 int longSize = w;
1429 int shortSize = h;
1430 if (longSize < shortSize) {
1431 int tmp = longSize;
1432 longSize = shortSize;
1433 shortSize = tmp;
1434 }
1435 longSize = (int)(longSize/density);
1436 shortSize = (int)(shortSize/density);
1437 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1438 }
1439
1440 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1441 int uiMode, int dw, int dh) {
1442 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1443 displayId);
1444 if (width < displayInfo.smallestNominalAppWidth) {
1445 displayInfo.smallestNominalAppWidth = width;
1446 }
1447 if (width > displayInfo.largestNominalAppWidth) {
1448 displayInfo.largestNominalAppWidth = width;
1449 }
1450 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1451 displayId);
1452 if (height < displayInfo.smallestNominalAppHeight) {
1453 displayInfo.smallestNominalAppHeight = height;
1454 }
1455 if (height > displayInfo.largestNominalAppHeight) {
1456 displayInfo.largestNominalAppHeight = height;
1457 }
1458 }
1459
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001460 DockedStackDividerController getDockedDividerController() {
1461 return mDividerControllerLocked;
1462 }
1463
Winson Chung655332c2016-10-31 13:14:28 -07001464 PinnedStackController getPinnedStackController() {
1465 return mPinnedStackControllerLocked;
1466 }
1467
Jeff Browna506a6e2013-06-04 00:02:38 -07001468 /**
1469 * Returns true if the specified UID has access to this display.
1470 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001471 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001472 return mDisplay.hasAccess(uid);
1473 }
1474
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001475 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001476 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001477 }
1478
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001479 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001480 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001481 }
1482
Wale Ogunwale61911492017-10-11 08:50:50 -07001483 /**
1484 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1485 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001486 TaskStack getSplitScreenPrimaryStack() {
1487 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001488 return (stack != null && stack.isVisible()) ? stack : null;
1489 }
1490
1491 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001492 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001493 * not visible.
1494 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001495 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1496 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001497 }
1498
1499 TaskStack getPinnedStack() {
1500 return mTaskStackContainers.getPinnedStack();
1501 }
1502
1503 private boolean hasPinnedStack() {
1504 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001505 }
1506
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001507 /**
1508 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1509 * Null is no compatible stack on the display.
1510 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001511 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001512 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1513 }
1514
1515 /**
1516 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1517 * activity type. Null is no compatible stack on the display.
1518 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001519 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001520 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001521 }
1522
Bryce Lee48f4b572017-04-10 10:54:15 -07001523 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001524 TaskStack getTopStack() {
1525 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001526 }
1527
Wale Ogunwale61911492017-10-11 08:50:50 -07001528 void onStackWindowingModeChanged(TaskStack stack) {
1529 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001530 }
1531
Andrii Kulian441e4492016-09-29 15:25:00 -07001532 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001533 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001534 super.onConfigurationChanged(newParentConfig);
1535
Andrii Kulian3a507b52016-09-19 18:14:12 -07001536 // The display size information is heavily dependent on the resources in the current
1537 // configuration, so we need to reconfigure it every time the configuration changes.
1538 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1539 mService.reconfigureDisplayLocked(this);
1540
Bryce Leef3c6a472017-11-14 14:53:06 -08001541 final DockedStackDividerController dividerController = getDockedDividerController();
1542
1543 if (dividerController != null) {
1544 getDockedDividerController().onConfigurationChanged();
1545 }
1546
1547 final PinnedStackController pinnedStackController = getPinnedStackController();
1548
1549 if (pinnedStackController != null) {
1550 getPinnedStackController().onConfigurationChanged();
1551 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001552 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001553
Andrii Kulian441e4492016-09-29 15:25:00 -07001554 /**
1555 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1556 * bounds were updated.
1557 */
1558 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001559 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1560 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001561 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001562 changedStackList.add(stack.mStackId);
1563 }
1564 }
Winson Chung32c566f2017-04-11 18:31:21 -07001565
1566 // If there was no pinned stack, we still need to notify the controller of the display info
1567 // update as a result of the config change. We do this here to consolidate the flow between
1568 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001569 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001570 mPinnedStackControllerLocked.onDisplayInfoChanged();
1571 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001572 }
1573
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001574 @Override
1575 boolean fillsParent() {
1576 return true;
1577 }
1578
1579 @Override
1580 boolean isVisible() {
1581 return true;
1582 }
1583
1584 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001585 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001586 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001587 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001588 }
1589
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001590 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001591 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1592 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1593 // target, or here in order if there isn't an IME target.
1594 if (traverseTopToBottom) {
1595 for (int i = mChildren.size() - 1; i >= 0; --i) {
1596 final DisplayChildWindowContainer child = mChildren.get(i);
1597 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1598 // In this case the Ime windows will be processed above their target so we skip
1599 // here.
1600 continue;
1601 }
1602 if (child.forAllWindows(callback, traverseTopToBottom)) {
1603 return true;
1604 }
1605 }
1606 } else {
1607 final int count = mChildren.size();
1608 for (int i = 0; i < count; i++) {
1609 final DisplayChildWindowContainer child = mChildren.get(i);
1610 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1611 // In this case the Ime windows will be processed above their target so we skip
1612 // here.
1613 continue;
1614 }
1615 if (child.forAllWindows(callback, traverseTopToBottom)) {
1616 return true;
1617 }
1618 }
1619 }
1620 return false;
1621 }
1622
1623 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1624 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1625 }
1626
Wale Ogunwale399c8692017-05-08 14:22:42 -07001627 @Override
1628 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001629 final WindowManagerPolicy policy = mService.mPolicy;
1630
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001631 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001632 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001633 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001634 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001635 // If the display is frozen, some activities may be in the middle of restarting, and
1636 // thus have removed their old window. If the window has the flag to hide the lock
1637 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1638 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001639 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001640 } else if (policy.isKeyguardLocked()) {
1641 // Use the last orientation the while the display is frozen with the keyguard
1642 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1643 // window. We don't want to check the show when locked window directly though as
1644 // things aren't stable while the display is frozen, for example the window could be
1645 // momentarily unavailable due to activity relaunch.
1646 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001647 + "return " + mLastOrientation);
1648 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001649 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001650 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001651 final int orientation = mAboveAppWindowsContainers.getOrientation();
1652 if (orientation != SCREEN_ORIENTATION_UNSET) {
1653 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001654 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001655 }
1656
Wale Ogunwale399c8692017-05-08 14:22:42 -07001657 // Top system windows are not requesting an orientation. Start searching from apps.
1658 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001659 }
1660
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001661 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001662 // Check if display metrics changed and update base values if needed.
1663 updateBaseDisplayMetricsIfNeeded();
1664
Craig Mautner722285e2012-09-07 13:55:58 -07001665 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001666 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001667
Wale Ogunwale61911492017-10-11 08:50:50 -07001668 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1669 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001670 }
Craig Mautner722285e2012-09-07 13:55:58 -07001671 }
1672
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001673 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001674 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1675 if (displayManagerInternal != null) {
1676 // Bootstrap the default logical display from the display manager.
1677 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1678 if (newDisplayInfo != null) {
1679 mDisplayInfo.copyFrom(newDisplayInfo);
1680 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001681 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001682
Andrii Kuliancd097992017-03-23 18:31:59 -07001683 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1684 mDisplayInfo.logicalDensityDpi);
1685 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1686 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1687 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001688 }
1689
Andrii Kuliancd097992017-03-23 18:31:59 -07001690 /**
1691 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1692 * values.
1693 */
1694 private void updateBaseDisplayMetricsIfNeeded() {
1695 // Get real display metrics without overrides from WM.
1696 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1697 final int orientation = mDisplayInfo.rotation;
1698 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1699 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1700 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1701 final int newDensity = mDisplayInfo.logicalDensityDpi;
1702
1703 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1704 || mInitialDisplayHeight != newHeight
1705 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1706
1707 if (displayMetricsChanged) {
1708 // Check if display size or density is forced.
1709 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1710 || mBaseDisplayHeight != mInitialDisplayHeight;
1711 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1712
1713 // If there is an override set for base values - use it, otherwise use new values.
1714 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1715 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1716 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1717
1718 // Real display metrics changed, so we should also update initial values.
1719 mInitialDisplayWidth = newWidth;
1720 mInitialDisplayHeight = newHeight;
1721 mInitialDisplayDensity = newDensity;
1722 mService.reconfigureDisplayLocked(this);
1723 }
1724 }
1725
Bryce Lee27cec322017-03-21 09:41:37 -07001726 /** Sets the maximum width the screen resolution can be */
1727 void setMaxUiWidth(int width) {
1728 if (DEBUG_DISPLAY) {
1729 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1730 }
1731
1732 mMaxUiWidth = width;
1733
1734 // Update existing metrics.
1735 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1736 }
1737
1738 /** Update base (override) display metrics. */
1739 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1740 mBaseDisplayWidth = baseWidth;
1741 mBaseDisplayHeight = baseHeight;
1742 mBaseDisplayDensity = baseDensity;
1743
1744 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1745 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1746 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1747 mBaseDisplayWidth = mMaxUiWidth;
1748
1749 if (DEBUG_DISPLAY) {
1750 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1751 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1752 + " on display:" + getDisplayId());
1753 }
1754 }
1755
1756 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08001757
1758 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07001759 }
1760
Wale Ogunwaledb506192017-12-08 10:57:32 -08001761 void getStableRect(Rect out) {
1762 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08001763 }
1764
Wale Ogunwale61911492017-10-11 08:50:50 -07001765 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001766 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1767 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001768
Wale Ogunwale61911492017-10-11 08:50:50 -07001769 final TaskStack stack = new TaskStack(mService, stackId, controller);
1770 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001771 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001772 }
1773
Andrii Kulian51c1b672017-04-07 18:39:32 -07001774 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001775 final DisplayContent prevDc = stack.getDisplayContent();
1776 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001777 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1778 + " which is not currently attached to any display");
1779 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001780 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001781 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1782 + " to its current displayId=" + mDisplayId);
1783 }
1784
Wale Ogunwale61911492017-10-11 08:50:50 -07001785 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001786 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001787 }
1788
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001789 @Override
1790 protected void addChild(DisplayChildWindowContainer child,
1791 Comparator<DisplayChildWindowContainer> comparator) {
1792 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1793 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001794
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001795 @Override
1796 protected void addChild(DisplayChildWindowContainer child, int index) {
1797 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1798 }
1799
1800 @Override
1801 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001802 // Only allow removal of direct children from this display if the display is in the process
1803 // of been removed.
1804 if (mRemovingDisplay) {
1805 super.removeChild(child);
1806 return;
1807 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001808 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001809 }
1810
Andrii Kuliand2765632016-12-12 22:26:34 -08001811 @Override
1812 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1813 // Children of the display are statically ordered, so the real intention here is to perform
1814 // the operation on the display and not the static direct children.
1815 getParent().positionChildAt(position, this, includingParents);
1816 }
1817
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001818 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001819 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1820 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001821 final int taskId = stack.taskIdFromPoint(x, y);
1822 if (taskId != -1) {
1823 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001824 }
1825 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001826 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001827 }
1828
Chong Zhang8e89b312015-09-09 15:09:30 -07001829 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001830 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001831 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001832 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001833 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001834 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001835 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07001836 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1837 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001838 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001839 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001840 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001841
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001842 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1843 if (mTmpTaskForResizePointSearchResult.searchDone) {
1844 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001845 }
1846 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001847 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001848 }
1849
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001850 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001851 // The provided task is the task on this display with focus, so if WindowManagerService's
1852 // focused app is not on this display, focusedTask will be null.
1853 if (focusedTask == null) {
1854 mTouchExcludeRegion.setEmpty();
1855 } else {
1856 mTouchExcludeRegion.set(mBaseDisplayRect);
1857 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1858 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07001859 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1860 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001861 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
1862 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08001863 }
1864 // If we removed the focused task above, add it back and only leave its
1865 // outside touch area in the exclusion. TapDectector is not interested in
1866 // any touch inside the focused task itself.
1867 if (!mTmpRect2.isEmpty()) {
1868 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1869 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001870 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001871 final WindowState inputMethod = mService.mInputMethodWindow;
1872 if (inputMethod != null && inputMethod.isVisibleLw()) {
1873 // If the input method is visible and the user is typing, we don't want these touch
1874 // events to be intercepted and used to change focus. This would likely cause a
1875 // disappearance of the input method.
1876 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001877 if (inputMethod.getDisplayId() == mDisplayId) {
1878 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1879 } else {
1880 // IME is on a different display, so we need to update its tap detector.
1881 // TODO(multidisplay): Remove when IME will always appear on same display.
1882 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1883 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001884 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001885 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1886 WindowState win = mTapExcludedWindows.get(i);
1887 win.getTouchableRegion(mTmpRegion);
1888 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1889 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001890 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07001891 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001892 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001893 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001894 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1895 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001896 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001897 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001898 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001899 }
1900
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001901 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001902 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001903 super.switchUser();
1904 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001905 }
1906
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001907 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001908 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1909 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001910 }
1911 }
1912
Wale Ogunwale10124582016-09-15 20:25:50 -07001913 @Override
1914 void removeIfPossible() {
1915 if (isAnimating()) {
1916 mDeferredRemoval = true;
1917 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001918 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001919 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001920 }
1921
Wale Ogunwale10124582016-09-15 20:25:50 -07001922 @Override
1923 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001924 mRemovingDisplay = true;
1925 try {
1926 super.removeImmediately();
1927 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07001928 if (mService.canDispatchPointerEvents()) {
1929 if (mTapDetector != null) {
1930 mService.unregisterPointerEventListener(mTapDetector);
1931 }
1932 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
1933 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1934 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001935 }
Robert Carrb1579c82017-09-05 14:54:47 -07001936 // The pending transaction won't be applied so we should
1937 // just clean up any surfaces pending destruction.
1938 onPendingTransactionApplied();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001939 } finally {
1940 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001941 }
Craig Mautner95da1082014-02-24 17:54:35 -08001942 }
1943
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001944 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001945 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001946 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001947 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1948
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001949 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001950 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001951 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001952 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001953 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001954 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001955 }
1956
Andrii Kulian0214ed92017-05-16 13:44:05 -07001957 /** @return 'true' if removal of this display content is deferred due to active animation. */
1958 boolean isRemovalDeferred() {
1959 return mDeferredRemoval;
1960 }
1961
Wale Ogunwale10124582016-09-15 20:25:50 -07001962 boolean animateForIme(float interpolatedValue, float animationTarget,
1963 float dividerAnimationTarget) {
1964 boolean updated = false;
1965
Wale Ogunwale61911492017-10-11 08:50:50 -07001966 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1967 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001968 if (stack == null || !stack.isAdjustedForIme()) {
1969 continue;
1970 }
1971
1972 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1973 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1974 updated = true;
1975 } else {
1976 mDividerControllerLocked.mLastAnimationProgress =
1977 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1978 mDividerControllerLocked.mLastDividerProgress =
1979 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1980 updated |= stack.updateAdjustForIme(
1981 mDividerControllerLocked.mLastAnimationProgress,
1982 mDividerControllerLocked.mLastDividerProgress,
1983 false /* force */);
1984 }
1985 if (interpolatedValue >= 1f) {
1986 stack.endImeAdjustAnimation();
1987 }
1988 }
1989
1990 return updated;
1991 }
1992
1993 boolean clearImeAdjustAnimation() {
1994 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07001995 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1996 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001997 if (stack != null && stack.isAdjustedForIme()) {
1998 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1999 changed = true;
2000 }
2001 }
2002 return changed;
2003 }
2004
2005 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002006 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2007 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002008 if (stack.isVisible() && stack.isAdjustedForIme()) {
2009 stack.beginImeAdjustAnimation();
2010 }
2011 }
2012 }
2013
2014 void adjustForImeIfNeeded() {
2015 final WindowState imeWin = mService.mInputMethodWindow;
2016 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2017 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002018 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002019 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2020 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2021 imeTargetStack.getDockSide() : DOCKED_INVALID;
2022 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2023 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2024 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002025 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002026 final boolean imeHeightChanged = imeVisible &&
2027 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2028
2029 // The divider could be adjusted for IME position, or be thinner than usual,
2030 // or both. There are three possible cases:
2031 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2032 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2033 // - If IME is not visible, divider is not moved and is normal width.
2034
2035 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002036 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2037 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002038 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002039 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2040 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002041 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2042 } else {
2043 stack.resetAdjustedForIme(false);
2044 }
2045 }
2046 mDividerControllerLocked.setAdjustedForIme(
2047 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2048 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002049 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2050 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002051 stack.resetAdjustedForIme(!dockVisible);
2052 }
2053 mDividerControllerLocked.setAdjustedForIme(
2054 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2055 }
Winson Chung655332c2016-10-31 13:14:28 -07002056 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002057 }
2058
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002059 void setInputMethodAnimLayerAdjustment(int adj) {
2060 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2061 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002062 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002063 }
2064
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002065 /**
2066 * If a window that has an animation specifying a colored background and the current wallpaper
2067 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2068 * suddenly disappear.
2069 */
2070 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002071 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2072 w -> w.mIsWallpaper && w.isVisibleNow());
2073
2074 if (visibleWallpaper != null) {
2075 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002076 }
2077 return winAnimator.mAnimLayer;
2078 }
2079
Wale Ogunwale10124582016-09-15 20:25:50 -07002080 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002081 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2082 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002083 stack.prepareFreezingTaskBounds();
2084 }
2085 }
2086
Wale Ogunwale94744212015-09-21 19:01:47 -07002087 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002088 getBounds(mTmpRect, newRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002089
2090 // Compute a transform matrix to undo the coordinate space transformation,
2091 // and present the window at the same physical position it previously occupied.
2092 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2093 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2094
2095 mTmpRectF.set(bounds);
2096 mTmpMatrix.mapRect(mTmpRectF);
2097 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002098 }
2099
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002100 static int deltaRotation(int oldRotation, int newRotation) {
2101 int delta = newRotation - oldRotation;
2102 if (delta < 0) delta += 4;
2103 return delta;
2104 }
2105
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002106 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002107 Matrix outMatrix) {
2108 // For rotations without Z-ordering we don't need the target rectangle's position.
2109 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2110 displayHeight, outMatrix);
2111 }
2112
2113 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2114 float displayWidth, float displayHeight, Matrix outMatrix) {
2115 switch (rotation) {
2116 case ROTATION_0:
2117 outMatrix.reset();
2118 break;
2119 case ROTATION_270:
2120 outMatrix.setRotate(270, 0, 0);
2121 outMatrix.postTranslate(0, displayHeight);
2122 outMatrix.postTranslate(rectTop, 0);
2123 break;
2124 case ROTATION_180:
2125 outMatrix.reset();
2126 break;
2127 case ROTATION_90:
2128 outMatrix.setRotate(90, 0, 0);
2129 outMatrix.postTranslate(displayWidth, 0);
2130 outMatrix.postTranslate(-rectTop, rectLeft);
2131 break;
2132 }
2133 }
2134
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002135 @CallSuper
2136 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002137 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002138 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002139 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002140 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002141 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2142 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002143 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002144 }
2145 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2146 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002147 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2148 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002149 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002150 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002151 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2152 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002153 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002154 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002155 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2156 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002157 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002158 }
2159 proto.write(DPI, mBaseDisplayDensity);
2160 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002161 proto.write(ROTATION, mRotation);
2162 final ScreenRotationAnimation screenRotationAnimation =
2163 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2164 if (screenRotationAnimation != null) {
2165 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2166 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002167 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002168 proto.end(token);
2169 }
2170
Craig Mautnera91f9e22012-09-14 16:22:08 -07002171 public void dump(String prefix, PrintWriter pw) {
2172 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2173 final String subPrefix = " " + prefix;
2174 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2175 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2176 pw.print("dpi");
2177 if (mInitialDisplayWidth != mBaseDisplayWidth
2178 || mInitialDisplayHeight != mBaseDisplayHeight
2179 || mInitialDisplayDensity != mBaseDisplayDensity) {
2180 pw.print(" base=");
2181 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2182 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2183 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002184 if (mDisplayScalingDisabled) {
2185 pw.println(" noscale");
2186 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002187 pw.print(" cur=");
2188 pw.print(mDisplayInfo.logicalWidth);
2189 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2190 pw.print(" app=");
2191 pw.print(mDisplayInfo.appWidth);
2192 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2193 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2194 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2195 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2196 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002197 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002198 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002199 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002200
Craig Mautnerdc548482014-02-05 13:35:24 -08002201 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002202 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002203 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2204 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002205 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002206 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002207
Craig Mautnerdc548482014-02-05 13:35:24 -08002208 pw.println();
2209 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002210 pw.println();
2211 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002212 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002213 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002214 pw.print(" Exiting #"); pw.print(i);
2215 pw.print(' '); pw.print(token);
2216 pw.println(':');
2217 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002218 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002219 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002220
Jorim Jaggi31f71702016-05-04 16:43:04 -07002221 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002222
2223 // Dump stack references
2224 final TaskStack homeStack = getHomeStack();
2225 if (homeStack != null) {
2226 pw.println(prefix + "homeStack=" + homeStack.getName());
2227 }
2228 final TaskStack pinnedStack = getPinnedStack();
2229 if (pinnedStack != null) {
2230 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2231 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002232 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002233 if (splitScreenPrimaryStack != null) {
2234 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2235 }
2236
2237 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002238 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002239 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002240 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002241
2242 if (mInputMethodAnimLayerAdjustment != 0) {
2243 pw.println(subPrefix
2244 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2245 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002246
2247 pw.println();
2248 mDisplayFrames.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002249 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002250
2251 @Override
2252 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002253 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002254 }
2255
2256 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002257 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002258 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002259
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002260 /** Returns true if the stack in the windowing mode is visible. */
2261 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002262 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002263 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002264 }
2265
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002266 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002267 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002268 final int x = (int) xf;
2269 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002270 final WindowState touchedWin = getWindow(w -> {
2271 final int flags = w.mAttrs.flags;
2272 if (!w.isVisibleLw()) {
2273 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002274 }
2275 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002276 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002277 }
2278
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002279 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002280 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002281 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002282 }
2283
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002284 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002285
2286 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002287 return mTmpRegion.contains(x, y) || touchFlags == 0;
2288 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002289
2290 return touchedWin;
2291 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002292
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002293 boolean canAddToastWindowForUid(int uid) {
2294 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002295 // Also if the app is focused adding more than one toast at
2296 // a time for better backwards compatibility.
2297 final WindowState focusedWindowForUid = getWindow(w ->
2298 w.mOwnerUid == uid && w.isFocused());
2299 if (focusedWindowForUid != null) {
2300 return true;
2301 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002302 final WindowState win = getWindow(w ->
2303 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2304 && !w.mWindowRemovalAllowed);
2305 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002306 }
2307
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002308 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002309 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2310 return;
2311 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002312
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002313 // Used to communicate the old focus to the callback method.
2314 mTmpWindow = oldFocus;
2315
2316 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002317 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002318
2319 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002320 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002321
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002322 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002323
2324 if (mTmpWindow == null) {
2325 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2326 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002327 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002328 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002329 }
2330
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002331 /** Updates the layer assignment of windows on this display. */
2332 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002333 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002334 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002335 if (setLayoutNeeded) {
2336 setLayoutNeeded();
2337 }
Robert Carrb1579c82017-09-05 14:54:47 -07002338
Robert Carrf59b8dd2017-10-02 18:58:36 -07002339 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002340 // the application of this transaction until the animation pass triggers
2341 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2342 // the hiding and showing of surfaces.
2343 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002344 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002345 }
2346
Wale Ogunwale1666e312016-12-16 11:27:18 -08002347 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2348 // moving containers or resizing them. Need to investigate the best way to have it automatically
2349 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002350 void layoutAndAssignWindowLayersIfNeeded() {
2351 mService.mWindowsChanged = true;
2352 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002353
2354 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2355 false /*updateInputWindows*/)) {
2356 assignWindowLayers(false /* setLayoutNeeded */);
2357 }
2358
2359 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2360 mService.mWindowPlacerLocked.performSurfacePlacement();
2361 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2362 }
2363
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002364 /** Returns true if a leaked surface was destroyed */
2365 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002366 // Used to indicate that a surface was leaked.
2367 mTmpWindow = null;
2368 forAllWindows(w -> {
2369 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002370 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002371 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002372 }
2373 if (!mService.mSessions.contains(wsa.mSession)) {
2374 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002375 + w + " surface=" + wsa.mSurfaceController
2376 + " token=" + w.mToken
2377 + " pid=" + w.mSession.mPid
2378 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002379 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002380 mService.mForceRemoves.add(w);
2381 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002382 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002383 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002384 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002385 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002386 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002387 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002388 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002389 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002390 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002391
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002392 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002393 }
2394
2395 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002396 * Determine and return the window that should be the IME target.
2397 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2398 * @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 +00002399 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002400 WindowState computeImeTarget(boolean updateImeTarget) {
2401 if (mService.mInputMethodWindow == null) {
2402 // There isn't an IME so there shouldn't be a target...That was easy!
2403 if (updateImeTarget) {
2404 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2405 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2406 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2407 }
2408 return null;
2409 }
2410
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002411 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2412 // same display. Or even when the current IME/target are not on the same screen as the next
2413 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002414 mUpdateImeTarget = updateImeTarget;
2415 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002416
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002417
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002418 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2419 // to be on top of it, but it is not -really- where input will go. So look down below
2420 // for a real window to target...
2421 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2422 final AppWindowToken token = target.mAppToken;
2423 if (token != null) {
2424 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2425 if (betterTarget != null) {
2426 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002427 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002428 }
2429 }
2430
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002431 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2432 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002433
2434 // Now, a special case -- if the last target's window is in the process of exiting, and is
2435 // above the new target, keep on the last target to avoid flicker. Consider for example a
2436 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2437 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2438 // scrim.
2439 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002440 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2441 && (target == null
2442 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002443 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002444 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002445 }
2446
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002447 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2448 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002449
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002450 if (target == null) {
2451 if (updateImeTarget) {
2452 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2453 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2454 + Debug.getCallers(4) : ""));
2455 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2456 }
2457
2458 return null;
2459 }
2460
2461 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002462 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2463 if (token != null) {
2464
2465 // Now some fun for dealing with window animations that modify the Z order. We need
2466 // to look at all windows below the current target that are in this app, finding the
2467 // highest visible one in layering.
2468 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002469 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002470 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002471 }
2472
2473 if (highestTarget != null) {
2474 final AppTransition appTransition = mService.mAppTransition;
2475 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2476 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2477 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002478 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002479
2480 if (appTransition.isTransitionSet()) {
2481 // If we are currently setting up for an animation, hold everything until we
2482 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002483 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2484 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002485 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002486 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002487 // If the window we are currently targeting is involved with an animation,
2488 // and it is on top of the next target we will be over, then hold off on
2489 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002490 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2491 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002492 }
2493 }
2494 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002495
2496 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2497 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2498 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002499 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002500 }
2501
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002502 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002503 }
2504
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002505 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2506 if (target == mService.mInputMethodTarget
2507 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2508 && mInputMethodAnimLayerAdjustment == layerAdj) {
2509 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002510 }
2511
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002512 mService.mInputMethodTarget = target;
2513 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2514 setInputMethodAnimLayerAdjustment(layerAdj);
2515 assignWindowLayers(false /* setLayoutNeeded */);
2516 }
2517
2518 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2519 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2520 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2521 }
2522
2523 // Used to indicate we have reached the first window in the range we are interested in.
2524 mTmpWindow = null;
2525
2526 // TODO: Figure-out a more efficient way to do this.
2527 final WindowState candidate = getWindow(w -> {
2528 if (w == top) {
2529 // Reached the first window in the range we are interested in.
2530 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002531 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002532 if (mTmpWindow == null) {
2533 return false;
2534 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002535
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002536 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2537 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002538 }
2539 // If we reached the bottom of the range of windows we are considering,
2540 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002541 if (w == bottom) {
2542 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002543 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002544 return false;
2545 });
2546
2547 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002548 }
2549
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002550 void setLayoutNeeded() {
2551 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2552 mLayoutNeeded = true;
2553 }
2554
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002555 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002556 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2557 mLayoutNeeded = false;
2558 }
2559
2560 boolean isLayoutNeeded() {
2561 return mLayoutNeeded;
2562 }
2563
Wale Ogunwale02319a62016-09-26 15:21:22 -07002564 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2565 if (mTokenMap.isEmpty()) {
2566 return;
2567 }
2568 pw.println(" Display #" + mDisplayId);
2569 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2570 while (it.hasNext()) {
2571 final WindowToken token = it.next();
2572 pw.print(" ");
2573 pw.print(token);
2574 if (dumpAll) {
2575 pw.println(':');
2576 token.dump(pw, " ");
2577 } else {
2578 pw.println();
2579 }
2580 }
2581 }
2582
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002583 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002584 final int[] index = new int[1];
2585 forAllWindows(w -> {
2586 final WindowStateAnimator wAnim = w.mWinAnimator;
2587 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2588 index[0] = index[0] + 1;
2589 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002590 }
2591
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002592 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002593 forAllWindows(w -> {
2594 w.mWinAnimator.enableSurfaceTrace(fd);
2595 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002596 }
2597
2598 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002599 forAllWindows(w -> {
2600 w.mWinAnimator.disableSurfaceTrace();
2601 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002602 }
2603
Jorim Jaggife762342016-10-13 14:33:27 +02002604 /**
2605 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2606 */
2607 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2608 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002609 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002610 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2611 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002612 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02002613 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002614 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002615 }
2616
Wale Ogunwale494009b82016-10-21 09:01:38 -07002617 boolean checkWaitingForWindows() {
2618
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002619 mHaveBootMsg = false;
2620 mHaveApp = false;
2621 mHaveWallpaper = false;
2622 mHaveKeyguard = true;
2623
2624 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002625 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2626 return true;
2627 }
2628 if (w.isDrawnLw()) {
2629 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002630 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002631 } else if (w.mAttrs.type == TYPE_APPLICATION
2632 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002633 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002634 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002635 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002636 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002637 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002638 }
2639 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002640 return false;
2641 });
2642
2643 if (visibleWindow != null) {
2644 // We have a visible window.
2645 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002646 }
2647
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002648 // if the wallpaper service is disabled on the device, we're never going to have
2649 // wallpaper, don't bother waiting for it
2650 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2651 com.android.internal.R.bool.config_enableWallpaperService)
2652 && !mService.mOnlyCore;
2653
Wale Ogunwale494009b82016-10-21 09:01:38 -07002654 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2655 "******** booted=" + mService.mSystemBooted
2656 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002657 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2658 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2659 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002660
2661 // If we are turning on the screen to show the boot message, don't do it until the boot
2662 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002663 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002664 return true;
2665 }
2666
2667 // If we are turning on the screen after the boot is completed normally, don't do so until
2668 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002669 if (mService.mSystemBooted
2670 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002671 return true;
2672 }
2673
2674 return false;
2675 }
2676
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002677 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002678 mTmpWindowAnimator = animator;
2679 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002680 }
2681
2682 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002683 resetAnimationBackgroundAnimator();
2684
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002685 // Used to indicate a detached wallpaper.
2686 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002687 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002688
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002689 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002690
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002691 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002692 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002693 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2694 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002695 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2696 }
2697 }
2698
Wale Ogunwale494009b82016-10-21 09:01:38 -07002699 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002700 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002701 if (imFocus == null) {
2702 return false;
2703 }
2704
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002705 if (DEBUG_INPUT_METHOD) {
2706 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2707 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2708 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002709 }
2710
Wale Ogunwale494009b82016-10-21 09:01:38 -07002711 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2712
2713 if (DEBUG_INPUT_METHOD) {
2714 Slog.i(TAG_WM, "IM target client: " + imeClient);
2715 if (imeClient != null) {
2716 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2717 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2718 }
2719 }
2720
2721 return imeClient != null && imeClient.asBinder() == client.asBinder();
2722 }
2723
2724 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002725 final WindowState win = getWindow(
2726 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2727 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002728 }
2729
2730 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002731 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002732 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002733 final int curValue = w.mSystemUiVisibility;
2734 final int diff = (curValue ^ visibility) & globalDiff;
2735 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002736 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002737 w.mSeq++;
2738 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002739 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002740 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2741 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002742 visibility, newValue, diff);
2743 }
2744 } catch (RemoteException e) {
2745 // so sorry
2746 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002747 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002748 }
2749
2750 void onWindowFreezeTimeout() {
2751 Slog.w(TAG_WM, "Window freeze timeout expired.");
2752 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002753
2754 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002755 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002756 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002757 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002758 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002759 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2760 - mService.mDisplayFreezeTime);
2761 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002762 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002763 mService.mWindowPlacerLocked.performSurfacePlacement();
2764 }
2765
2766 void waitForAllWindowsDrawn() {
2767 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002768 forAllWindows(w -> {
2769 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2770 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2771 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002772 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002773 w.mLastContentInsets.set(-1, -1, -1, -1);
2774 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002775 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002776 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002777 }
2778
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002779 // TODO: Super crazy long method that should be broken down...
2780 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2781
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002782 final int dw = mDisplayInfo.logicalWidth;
2783 final int dh = mDisplayInfo.logicalHeight;
2784 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2785
2786 mTmpUpdateAllDrawn.clear();
2787
2788 int repeats = 0;
2789 do {
2790 repeats++;
2791 if (repeats > 6) {
2792 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2793 clearLayoutNeeded();
2794 break;
2795 }
2796
2797 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2798 pendingLayoutChanges);
2799
Andrii Kulian839def92016-11-02 10:58:58 -07002800 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2801 // the wallpaper window jumping across displays.
2802 // Remove check for default display when there will be support for multiple wallpaper
2803 // targets (on different displays).
2804 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002805 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002806 }
2807
2808 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2809 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2810 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2811 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002812 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002813 }
2814 }
2815
2816 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2817 setLayoutNeeded();
2818 }
2819
2820 // FIRST LOOP: Perform a layout, if needed.
2821 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2822 performLayout(repeats == 1, false /* updateInputWindows */);
2823 } else {
2824 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2825 }
2826
2827 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2828 pendingLayoutChanges = 0;
2829
2830 if (isDefaultDisplay) {
2831 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002832 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002833 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2834 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2835 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2836 }
2837 } while (pendingLayoutChanges != 0);
2838
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002839 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002840
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002841 mTmpRecoveringMemory = recoveringMemory;
2842 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002843
2844 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002845 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2846 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2847 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002848 true /* inTraversal, must call performTraversalInTrans... below */);
2849
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002850 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2851 if (wallpaperVisible != mLastWallpaperVisible) {
2852 mLastWallpaperVisible = wallpaperVisible;
2853 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2854 }
2855
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002856 while (!mTmpUpdateAllDrawn.isEmpty()) {
2857 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2858 // See if any windows have been drawn, so they (and others associated with them)
2859 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002860 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002861 }
2862
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002863 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002864 }
2865
Bryce Leef3c6a472017-11-14 14:53:06 -08002866 private void updateBounds() {
2867 calculateBounds(mTmpBounds);
2868 setBounds(mTmpBounds);
2869 }
2870
2871 // Determines the current display bounds based on the current state
2872 private void calculateBounds(Rect out) {
2873 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
2874 final int orientation = mDisplayInfo.rotation;
2875 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2876 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
2877 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
2878 int width = mDisplayInfo.logicalWidth;
2879 int left = (physWidth - width) / 2;
2880 int height = mDisplayInfo.logicalHeight;
2881 int top = (physHeight - height) / 2;
2882 out.set(left, top, left + width, top + height);
2883 }
2884
2885 @Override
2886 public void getBounds(Rect out) {
2887 calculateBounds(out);
2888 }
2889
2890 private void getBounds(Rect out, int orientation) {
2891 getBounds(out);
2892
2893 // Rotate the Rect if needed.
2894 final int currentRotation = mDisplayInfo.rotation;
2895 final int rotationDelta = deltaRotation(currentRotation, orientation);
2896 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
2897 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
2898 mTmpRectF.set(out);
2899 mTmpMatrix.mapRect(mTmpRectF);
2900 mTmpRectF.round(out);
2901 }
2902 }
2903
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002904 void performLayout(boolean initial, boolean updateInputWindows) {
2905 if (!isLayoutNeeded()) {
2906 return;
2907 }
2908 clearLayoutNeeded();
2909
2910 final int dw = mDisplayInfo.logicalWidth;
2911 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002912 if (DEBUG_LAYOUT) {
2913 Slog.v(TAG, "-------------------------------------");
2914 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2915 }
2916
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002917 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
2918 // TODO: Not sure if we really need to set the rotation here since we are updating from the
2919 // display info above...
2920 mDisplayFrames.mRotation = mRotation;
2921 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002922 if (isDefaultDisplay) {
2923 // Not needed on non-default displays.
2924 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2925 mService.mScreenRect.set(0, 0, dw, dh);
2926 }
2927
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002928 int seq = mService.mLayoutSeq + 1;
2929 if (seq < 0) seq = 0;
2930 mService.mLayoutSeq = seq;
2931
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002932 // Used to indicate that we have processed the dream window and all additional windows are
2933 // behind it.
2934 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002935 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002936
2937 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002938 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002939
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002940 // Used to indicate that we have processed the dream window and all additional attached
2941 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002942 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002943 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002944
2945 // Now perform layout of attached windows, which usually depend on the position of the
2946 // window they are attached to. XXX does not deal with windows that are attached to windows
2947 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002948 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002949
2950 // Window frames may have changed. Tell the input dispatcher about it.
2951 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2952 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2953 if (updateInputWindows) {
2954 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2955 }
2956
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002957 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2958 }
2959
2960 /**
2961 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2962 * In portrait mode, it grabs the full screenshot.
2963 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002964 * @param config of the output bitmap
2965 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
2966 */
chaviwfbe47df2017-11-10 16:14:49 -08002967 Bitmap screenshotDisplay(Bitmap.Config config, boolean wallpaperOnly) {
2968 synchronized (mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02002969 if (!mService.mPolicy.isScreenOn()) {
chaviwfbe47df2017-11-10 16:14:49 -08002970 if (DEBUG_SCREENSHOT) {
2971 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002972 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002973 return null;
2974 }
2975
chaviwfbe47df2017-11-10 16:14:49 -08002976 if (wallpaperOnly && !shouldScreenshotWallpaper()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002977 return null;
2978 }
2979
chaviwfbe47df2017-11-10 16:14:49 -08002980 int dw = mDisplayInfo.logicalWidth;
2981 int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002982
chaviwfbe47df2017-11-10 16:14:49 -08002983 if (dw <= 0 || dh <= 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002984 return null;
2985 }
2986
chaviwfbe47df2017-11-10 16:14:49 -08002987 final Rect frame = new Rect(0, 0, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002988
2989 // The screenshot API does not apply the current screen rotation.
2990 int rot = mDisplay.getRotation();
2991
2992 if (rot == ROTATION_90 || rot == ROTATION_270) {
2993 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
2994 }
2995
chaviwfbe47df2017-11-10 16:14:49 -08002996 // SurfaceFlinger is not aware of orientation, so convert our logical
2997 // crop to SurfaceFlinger's portrait orientation.
2998 convertCropForSurfaceFlinger(frame, rot, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002999
3000 final ScreenRotationAnimation screenRotationAnimation =
3001 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3002 final boolean inRotation = screenRotationAnimation != null &&
3003 screenRotationAnimation.isAnimating();
chaviwfbe47df2017-11-10 16:14:49 -08003004 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003005
Robert Carrb1579c82017-09-05 14:54:47 -07003006 // TODO(b/68392460): We should screenshot Task controls directly
3007 // but it's difficult at the moment as the Task doesn't have the
3008 // correct size set.
chaviwfbe47df2017-11-10 16:14:49 -08003009 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, 0, 1, inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003010 if (bitmap == null) {
Robert Carrb1579c82017-09-05 14:54:47 -07003011 Slog.w(TAG_WM, "Failed to take screenshot");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003012 return null;
3013 }
chaviwfbe47df2017-11-10 16:14:49 -08003014
3015 // Create a copy of the screenshot that is immutable and backed in ashmem.
3016 // This greatly reduces the overhead of passing the bitmap between processes.
3017 final Bitmap ret = bitmap.createAshmemBitmap(config);
3018 bitmap.recycle();
3019 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003020 }
chaviwfbe47df2017-11-10 16:14:49 -08003021 }
3022
3023 private boolean shouldScreenshotWallpaper() {
3024 MutableBoolean screenshotReady = new MutableBoolean(false);
3025
3026 forAllWindows(w -> {
3027 if (!w.mIsWallpaper) {
3028 return false;
3029 }
3030
3031 // Found the wallpaper window
3032 final WindowStateAnimator winAnim = w.mWinAnimator;
3033
3034 if (winAnim.getShown() && winAnim.mLastAlpha > 0f) {
3035 screenshotReady.value = true;
3036 }
3037
3038 return true;
3039 }, true /* traverseTopToBottom */);
3040
3041 return screenshotReady.value;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003042 }
3043
3044 // TODO: Can this use createRotationMatrix()?
3045 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3046 if (rot == Surface.ROTATION_90) {
3047 final int tmp = crop.top;
3048 crop.top = dw - crop.right;
3049 crop.right = crop.bottom;
3050 crop.bottom = dw - crop.left;
3051 crop.left = tmp;
3052 } else if (rot == Surface.ROTATION_180) {
3053 int tmp = crop.top;
3054 crop.top = dh - crop.bottom;
3055 crop.bottom = dh - tmp;
3056 tmp = crop.right;
3057 crop.right = dw - crop.left;
3058 crop.left = dw - tmp;
3059 } else if (rot == Surface.ROTATION_270) {
3060 final int tmp = crop.top;
3061 crop.top = crop.left;
3062 crop.left = dh - crop.bottom;
3063 crop.bottom = crop.right;
3064 crop.right = dh - tmp;
3065 }
3066 }
3067
3068 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003069 // Used to indicate the layout is needed.
3070 mTmpWindow = null;
3071
3072 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003073 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003074 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003075 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003076 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003077 w.setDisplayLayoutNeeded();
3078 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003079 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003080
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003081 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003082 mService.mWindowPlacerLocked.performSurfacePlacement();
3083 }
3084 }
3085
Wale Ogunwale1666e312016-12-16 11:27:18 -08003086 void setExitingTokensHasVisible(boolean hasVisible) {
3087 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3088 mExitingTokens.get(i).hasVisible = hasVisible;
3089 }
3090
3091 // Initialize state of exiting applications.
3092 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3093 }
3094
3095 void removeExistingTokensIfPossible() {
3096 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3097 final WindowToken token = mExitingTokens.get(i);
3098 if (!token.hasVisible) {
3099 mExitingTokens.remove(i);
3100 }
3101 }
3102
3103 // Time to remove any exiting applications?
3104 mTaskStackContainers.removeExistingAppTokensIfPossible();
3105 }
3106
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003107 @Override
3108 void onDescendantOverrideConfigurationChanged() {
3109 setLayoutNeeded();
3110 mService.requestTraversal();
3111 }
3112
David Stevens9440dc82017-03-16 19:00:20 -07003113 boolean okToDisplay() {
3114 if (mDisplayId == DEFAULT_DISPLAY) {
3115 return !mService.mDisplayFrozen
3116 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3117 }
3118 return mDisplayInfo.state == Display.STATE_ON;
3119 }
3120
3121 boolean okToAnimate() {
3122 return okToDisplay() &&
3123 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3124 }
3125
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003126 static final class TaskForResizePointSearchResult {
3127 boolean searchDone;
3128 Task taskForResize;
3129
3130 void reset() {
3131 searchDone = false;
3132 taskForResize = null;
3133 }
3134 }
Robert Carr3b716242016-08-16 16:02:21 -07003135
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003136 private static final class ApplySurfaceChangesTransactionState {
3137 boolean displayHasContent;
3138 boolean obscured;
3139 boolean syswin;
3140 boolean focusDisplayed;
3141 float preferredRefreshRate;
3142 int preferredModeId;
3143
3144 void reset() {
3145 displayHasContent = false;
3146 obscured = false;
3147 syswin = false;
3148 focusDisplayed = false;
3149 preferredRefreshRate = 0;
3150 preferredModeId = 0;
3151 }
3152 }
3153
3154 private static final class ScreenshotApplicationState {
3155 WindowState appWin;
3156 int maxLayer;
3157 int minLayer;
3158 boolean screenshotReady;
3159
3160 void reset(boolean screenshotReady) {
3161 appWin = null;
3162 maxLayer = 0;
3163 minLayer = 0;
3164 this.screenshotReady = screenshotReady;
3165 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3166 }
3167 }
3168
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003169 /**
3170 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3171 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3172 * homogeneous children type which is currently required by sub-classes of
3173 * {@link WindowContainer} class.
3174 */
3175 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3176
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003177 DisplayChildWindowContainer(WindowManagerService service) {
3178 super(service);
3179 }
3180
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003181 @Override
3182 boolean fillsParent() {
3183 return true;
3184 }
3185
3186 @Override
3187 boolean isVisible() {
3188 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003189 }
3190 }
3191
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003192 /**
3193 * Window container class that contains all containers on this display relating to Apps.
3194 * I.e Activities.
3195 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003196 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003197 /**
3198 * A control placed at the appropriate level for transitions to occur.
3199 */
3200 SurfaceControl mAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003201
Wale Ogunwale61911492017-10-11 08:50:50 -07003202 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3203 // through the list to find them.
3204 private TaskStack mHomeStack = null;
3205 private TaskStack mPinnedStack = null;
3206 private TaskStack mSplitScreenPrimaryStack = null;
3207
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003208 TaskStackContainers(WindowManagerService service) {
3209 super(service);
3210 }
3211
Wale Ogunwale61911492017-10-11 08:50:50 -07003212 /**
3213 * Returns the topmost stack on the display that is compatible with the input windowing mode
3214 * and activity type. Null is no compatible stack on the display.
3215 */
3216 TaskStack getStack(int windowingMode, int activityType) {
3217 if (activityType == ACTIVITY_TYPE_HOME) {
3218 return mHomeStack;
3219 }
3220 if (windowingMode == WINDOWING_MODE_PINNED) {
3221 return mPinnedStack;
3222 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3223 return mSplitScreenPrimaryStack;
3224 }
3225 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3226 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003227 if (activityType == ACTIVITY_TYPE_UNDEFINED
3228 && windowingMode == stack.getWindowingMode()) {
3229 // Passing in undefined type means we want to match the topmost stack with the
3230 // windowing mode.
3231 return stack;
3232 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003233 if (stack.isCompatible(windowingMode, activityType)) {
3234 return stack;
3235 }
3236 }
3237 return null;
3238 }
3239
3240 @VisibleForTesting
3241 TaskStack getTopStack() {
3242 return mTaskStackContainers.getChildCount() > 0
3243 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3244 }
3245
3246 TaskStack getHomeStack() {
3247 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3248 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3249 }
3250 return mHomeStack;
3251 }
3252
3253 TaskStack getPinnedStack() {
3254 return mPinnedStack;
3255 }
3256
Matthew Ng64e77cf2017-10-31 14:01:31 -07003257 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003258 return mSplitScreenPrimaryStack;
3259 }
3260
Andrii Kulian839def92016-11-02 10:58:58 -07003261 /**
3262 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003263 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003264 */
3265 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003266 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003267 addChild(stack, onTop);
3268 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003269 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003270
Wale Ogunwale61911492017-10-11 08:50:50 -07003271 void onStackWindowingModeChanged(TaskStack stack) {
3272 removeStackReferenceIfNeeded(stack);
3273 addStackReferenceIfNeeded(stack);
3274 if (stack == mPinnedStack && getTopStack() != stack) {
3275 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3276 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3277 }
3278 }
3279
3280 private void addStackReferenceIfNeeded(TaskStack stack) {
3281 if (stack.isActivityTypeHome()) {
3282 if (mHomeStack != null) {
3283 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
3284 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
3285 }
3286 mHomeStack = stack;
3287 }
3288 final int windowingMode = stack.getWindowingMode();
3289 if (windowingMode == WINDOWING_MODE_PINNED) {
3290 if (mPinnedStack != null) {
3291 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3292 + mPinnedStack + " already exist on display=" + this
3293 + " stack=" + stack);
3294 }
3295 mPinnedStack = stack;
3296 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3297 if (mSplitScreenPrimaryStack != null) {
3298 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3299 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3300 + " already exist on display=" + this + " stack=" + stack);
3301 }
3302 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003303 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003304 }
3305 }
3306
3307 private void removeStackReferenceIfNeeded(TaskStack stack) {
3308 if (stack == mHomeStack) {
3309 mHomeStack = null;
3310 } else if (stack == mPinnedStack) {
3311 mPinnedStack = null;
3312 } else if (stack == mSplitScreenPrimaryStack) {
3313 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003314 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3315 mService.setDockedStackCreateStateLocked(
3316 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3317 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003318 }
Andrii Kulian839def92016-11-02 10:58:58 -07003319 }
3320
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003321 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003322 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3323 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003324 addChild(stack, addIndex);
3325 setLayoutNeeded();
3326 }
3327
Wale Ogunwale61911492017-10-11 08:50:50 -07003328 @Override
3329 protected void removeChild(TaskStack stack) {
3330 super.removeChild(stack);
3331 removeStackReferenceIfNeeded(stack);
3332 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003333
3334 @Override
3335 boolean isOnTop() {
3336 // Considered always on top
3337 return true;
3338 }
3339
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003340 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003341 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003342 if (child.getWindowConfiguration().isAlwaysOnTop()
3343 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003344 // This stack is always-on-top, override the default behavior.
3345 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3346
3347 // Moving to its current position, as we must call super but we don't want to
3348 // perform any meaningful action.
3349 final int currentPosition = mChildren.indexOf(child);
3350 super.positionChildAt(currentPosition, child, false /* includingParents */);
3351 return;
3352 }
3353
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003354 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3355 super.positionChildAt(targetPosition, child, includingParents);
3356
3357 setLayoutNeeded();
3358 }
3359
3360 /**
3361 * When stack is added or repositioned, find a proper position for it.
3362 * This will make sure that pinned stack always stays on top.
3363 * @param requestedPosition Position requested by caller.
3364 * @param stack Stack to be added or positioned.
3365 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3366 * @return The proper position for the stack.
3367 */
3368 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3369 final int topChildPosition = mChildren.size() - 1;
3370 boolean toTop = requestedPosition == POSITION_TOP;
3371 toTop |= adding ? requestedPosition >= topChildPosition + 1
3372 : requestedPosition >= topChildPosition;
3373 int targetPosition = requestedPosition;
3374
Wale Ogunwale61911492017-10-11 08:50:50 -07003375 if (toTop && stack.getWindowingMode() != WINDOWING_MODE_PINNED && hasPinnedStack()) {
Bryce Lee48f4b572017-04-10 10:54:15 -07003376 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003377 TaskStack topStack = mChildren.get(topChildPosition);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003378 if (topStack.getWindowingMode() != WINDOWING_MODE_PINNED) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003379 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3380 }
3381
3382 // So, stack is moved just below the pinned stack.
3383 // When we're adding a new stack the target is the current pinned stack position.
3384 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003385 // stack, because WindowContainer#positionAt() first removes element and then adds
3386 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003387 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3388 }
3389
3390 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003391 }
3392
3393 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003394 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3395 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003396 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003397 if (super.forAllWindows(callback, traverseTopToBottom)) {
3398 return true;
3399 }
3400 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3401 return true;
3402 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003403 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003404 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3405 return true;
3406 }
3407 if (super.forAllWindows(callback, traverseTopToBottom)) {
3408 return true;
3409 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003410 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003411 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003412 }
3413
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003414 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003415 boolean traverseTopToBottom) {
3416 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3417 // app tokens.
3418 // TODO: Investigate if we need to continue to do this or if we can just process them
3419 // in-order.
3420 if (traverseTopToBottom) {
3421 for (int i = mChildren.size() - 1; i >= 0; --i) {
3422 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3423 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003424 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3425 traverseTopToBottom)) {
3426 return true;
3427 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003428 }
3429 }
3430 } else {
3431 final int count = mChildren.size();
3432 for (int i = 0; i < count; ++i) {
3433 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3434 final int appTokensCount = appTokens.size();
3435 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003436 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3437 traverseTopToBottom)) {
3438 return true;
3439 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003440 }
3441 }
3442 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003443 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003444 }
3445
Wale Ogunwale1666e312016-12-16 11:27:18 -08003446 void setExitingTokensHasVisible(boolean hasVisible) {
3447 for (int i = mChildren.size() - 1; i >= 0; --i) {
3448 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3449 for (int j = appTokens.size() - 1; j >= 0; --j) {
3450 appTokens.get(j).hasVisible = hasVisible;
3451 }
3452 }
3453 }
3454
3455 void removeExistingAppTokensIfPossible() {
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 final AppWindowToken token = appTokens.get(j);
3460 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3461 && (!token.mIsExiting || token.isEmpty())) {
3462 // Make sure there is no animation running on this token, so any windows
3463 // associated with it will be removed as soon as their animations are
3464 // complete.
3465 token.mAppAnimator.clearAnimation();
3466 token.mAppAnimator.animating = false;
3467 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3468 "performLayout: App token exiting now removed" + token);
3469 token.removeIfPossible();
3470 }
3471 }
3472 }
3473 }
3474
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003475 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003476 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003477 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3478 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003479 // Apps and their containers are not allowed to specify an orientation while the
3480 // docked or freeform stack is visible...except for the home stack/task if the
3481 // docked stack is minimized and it actually set something.
3482 if (mHomeStack != null && mHomeStack.isVisible()
3483 && mDividerControllerLocked.isMinimizedDock()) {
3484 final int orientation = mHomeStack.getOrientation();
3485 if (orientation != SCREEN_ORIENTATION_UNSET) {
3486 return orientation;
3487 }
3488 }
3489 return SCREEN_ORIENTATION_UNSPECIFIED;
3490 }
3491
3492 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003493 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3494 PackageManager.FEATURE_AUTOMOTIVE);
3495 if (isCar) {
3496 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3497 // allow anything but the default orientation.
3498 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3499 "Forcing UNSPECIFIED orientation in car. Ignoring " + orientation);
3500 return SCREEN_ORIENTATION_UNSPECIFIED;
3501 }
3502
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003503 if (orientation != SCREEN_ORIENTATION_UNSET
3504 && orientation != SCREEN_ORIENTATION_BEHIND) {
3505 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3506 "App is requesting an orientation, return " + orientation);
3507 return orientation;
3508 }
3509
3510 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003511 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003512 // The next app has not been requested to be visible, so we keep the current orientation
3513 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003514 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003515 }
Robert Carrb1579c82017-09-05 14:54:47 -07003516
3517 @Override
3518 void assignChildLayers(SurfaceControl.Transaction t) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003519 int layer = 0;
Robert Carrb1579c82017-09-05 14:54:47 -07003520
3521 // We allow stacks to change visual order from the AM specified order due to
3522 // Z-boosting during animations. However we must take care to ensure TaskStacks
3523 // which are marked as alwaysOnTop remain that way.
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003524 for (int i = 0; i < mChildren.size(); i++) {
3525 final TaskStack s = mChildren.get(i);
3526 s.assignChildLayers();
3527 if (!s.needsZBoost() && !s.isAlwaysOnTop()) {
3528 s.assignLayer(t, layer++);
Robert Carrb1579c82017-09-05 14:54:47 -07003529 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003530 }
3531 for (int i = 0; i < mChildren.size(); i++) {
3532 final TaskStack s = mChildren.get(i);
3533 if (s.needsZBoost() && !s.isAlwaysOnTop()) {
3534 s.assignLayer(t, layer++);
Robert Carrb1579c82017-09-05 14:54:47 -07003535 }
3536 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003537 for (int i = 0; i < mChildren.size(); i++) {
3538 final TaskStack s = mChildren.get(i);
3539 if (s.isAlwaysOnTop()) {
3540 s.assignLayer(t, layer++);
3541 }
3542 }
3543
3544 // The appropriate place for App-Transitions to occur is right
3545 // above all other animations but still below things in the Picture-and-Picture
3546 // windowing mode.
3547 if (mAnimationLayer != null) {
3548 t.setLayer(mAnimationLayer, layer++);
3549 }
Robert Carrb1579c82017-09-05 14:54:47 -07003550 }
3551
3552 @Override
3553 void onParentSet() {
3554 super.onParentSet();
3555 if (getParent() != null) {
3556 mAnimationLayer = makeSurface().build();
3557 } else {
3558 mAnimationLayer.destroy();
3559 mAnimationLayer = null;
3560 }
3561 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003562 }
3563
Robert Carree4d4b92017-11-22 12:21:46 -08003564 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003565 AboveAppWindowContainers(String name, WindowManagerService service) {
3566 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08003567 }
3568
3569 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
3570 boolean needAssignIme = imeContainer != null
3571 && imeContainer.getSurfaceControl() != null;
3572 for (int j = 0; j < mChildren.size(); ++j) {
3573 final WindowToken wt = mChildren.get(j);
3574 wt.assignLayer(t, j);
3575 wt.assignChildLayers(t);
3576
3577 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
3578 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08003579
3580 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
3581 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003582 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08003583 needAssignIme = false;
3584 }
3585 }
3586 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003587 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08003588 }
3589 }
3590 }
3591
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003592 /**
3593 * Window container class that contains all containers on this display that are not related to
3594 * Apps. E.g. status bar.
3595 */
Robert Carree4d4b92017-11-22 12:21:46 -08003596 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07003597 /**
3598 * Compares two child window tokens returns -1 if the first is lesser than the second in
3599 * terms of z-order and 1 otherwise.
3600 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003601 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003602 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003603 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3604 token1.mOwnerCanManageAppTokens)
3605 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3606 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003607
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003608 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3609 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3610 return false;
3611 }
3612 final int req = w.mAttrs.screenOrientation;
3613 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3614 || req == SCREEN_ORIENTATION_UNSET) {
3615 return false;
3616 }
3617 return true;
3618 };
3619
Wale Ogunwale3a931692016-11-02 16:49:48 -07003620 private final String mName;
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003621 NonAppWindowContainers(String name, WindowManagerService service) {
3622 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003623 mName = name;
3624 }
3625
3626 void addChild(WindowToken token) {
3627 addChild(token, mWindowComparator);
3628 }
3629
3630 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003631 int getOrientation() {
3632 final WindowManagerPolicy policy = mService.mPolicy;
3633 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003634 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003635
3636 if (win != null) {
3637 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003638 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003639 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003640 if (mService.mKeyguardGoingAway) {
3641 // Keyguard can't affect the orientation if it is going away...
3642 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3643 return SCREEN_ORIENTATION_UNSET;
3644 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003645 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003646 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003647 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003648 }
3649
Andrii Kulian8ee72852017-03-10 10:36:45 -08003650 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003651
Jorim Jaggi75520d52017-08-24 17:23:19 +02003652 if (policy.isKeyguardShowingAndNotOccluded()
3653 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003654 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003655 }
3656
3657 return SCREEN_ORIENTATION_UNSET;
3658 }
3659
3660 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003661 String getName() {
3662 return mName;
3663 }
Robert Carr3b716242016-08-16 16:02:21 -07003664 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003665
Robert Carrb1579c82017-09-05 14:54:47 -07003666 SurfaceControl.Builder makeSurface(SurfaceSession s) {
3667 return mService.makeSurfaceBuilder(s)
3668 .setParent(mWindowingLayer);
3669 }
3670
3671 @Override
3672 SurfaceSession getSession() {
3673 return mSession;
3674 }
3675
3676 @Override
3677 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003678 SurfaceSession s = child != null ? child.getSession() : getSession();
3679 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07003680 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07003681
3682 if (child == null) {
3683 return b;
3684 }
3685
Robert Carree4d4b92017-11-22 12:21:46 -08003686 return b.setName(child.getName())
3687 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07003688 }
3689
3690 /**
3691 * The makeSurface variants are for use by the window-container
3692 * hierarchy. makeOverlay here is a function for various non windowing
3693 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
3694 * and other potpourii.
3695 */
3696 SurfaceControl.Builder makeOverlay() {
3697 return mService.makeSurfaceBuilder(mSession)
3698 .setParent(mOverlayLayer);
3699 }
3700
3701 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003702 applyMagnificationSpec(getPendingTransaction(), spec);
3703 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07003704 }
3705
3706 @Override
3707 void onParentSet() {
3708 // Since we are the top of the SurfaceControl hierarchy here
3709 // we create the root surfaces explicitly rather than chaining
3710 // up as the default implementation in onParentSet does. So we
3711 // explicitly do NOT call super here.
3712 }
3713
3714 @Override
3715 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07003716
3717 // These are layers as children of "mWindowingLayer"
3718 mBelowAppWindowsContainers.assignLayer(t, 0);
3719 mTaskStackContainers.assignLayer(t, 1);
3720 mAboveAppWindowsContainers.assignLayer(t, 2);
3721
3722 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08003723 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08003724
Robert Carree4d4b92017-11-22 12:21:46 -08003725 // In the case where we have an IME target that is not in split-screen
3726 // mode IME assignment is easy. We just need the IME to go directly above
3727 // the target. This way children of the target will naturally go above the IME
3728 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08003729 //
Robert Carree4d4b92017-11-22 12:21:46 -08003730 // In the case of split-screen windowing mode, we need to elevate the IME above the
3731 // docked divider while keeping the app itself below the docked divider, so instead
3732 // we use relative layering of the IME targets child windows, and place the
3733 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08003734 //
Robert Carree4d4b92017-11-22 12:21:46 -08003735 // In the case where we have no IME target we assign it where it's base layer would
3736 // place it in the AboveAppWindowContainers.
3737 if (imeTarget != null && !imeTarget.inSplitScreenWindowingMode()
3738 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003739 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07003740 // TODO: We need to use an extra level on the app surface to ensure
3741 // this is always above SurfaceView but always below attached window.
3742 1);
Robert Carree4d4b92017-11-22 12:21:46 -08003743 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07003744 }
3745
3746 // Above we have assigned layers to our children, now we ask them to assign
3747 // layers to their children.
3748 mBelowAppWindowsContainers.assignChildLayers(t);
3749 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08003750 mAboveAppWindowsContainers.assignChildLayers(t,
3751 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07003752 mImeWindowsContainers.assignChildLayers(t);
3753 }
3754
3755 /**
3756 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
3757 * that the IME target is one of the docked applications. We'd like the docked divider to be
3758 * above both of the applications, and we'd like the IME to be above the docked divider.
3759 * However we need child windows of the applications to be above the IME (Text drag handles).
3760 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
3761 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
3762 * with {@link #WindowState#assignLayer}
3763 */
3764 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
3765 t.setRelativeLayer(child.getSurfaceControl(), mImeWindowsContainers.getSurfaceControl(), 1);
3766 }
3767
3768 @Override
Jorim Jaggia5e10572017-11-15 14:36:26 +01003769 public void destroyAfterPendingTransaction(SurfaceControl surface) {
Robert Carrb1579c82017-09-05 14:54:47 -07003770 mPendingDestroyingSurfaces.add(surface);
3771 }
3772
3773 /**
3774 * Destroys any surfaces that have been put into the pending list with
Jorim Jaggia5e10572017-11-15 14:36:26 +01003775 * {@link #destroyAfterPendingTransaction}.
Robert Carrb1579c82017-09-05 14:54:47 -07003776 */
3777 void onPendingTransactionApplied() {
3778 for (int i = mPendingDestroyingSurfaces.size() - 1; i >= 0; i--) {
3779 mPendingDestroyingSurfaces.get(i).destroy();
3780 }
3781 mPendingDestroyingSurfaces.clear();
3782 }
Jorim Jaggi9af095b2017-12-12 17:18:57 +01003783
3784 @Override
3785 void prepareSurfaces() {
3786 final ScreenRotationAnimation screenRotationAnimation =
3787 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
3788 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
3789 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
3790 mPendingTransaction.setMatrix(mWindowingLayer,
3791 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
3792 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
3793 mPendingTransaction.setPosition(mWindowingLayer,
3794 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
3795 mPendingTransaction.setAlpha(mWindowingLayer,
3796 screenRotationAnimation.getEnterTransformation().getAlpha());
3797 }
3798 super.prepareSurfaces();
3799 }
Craig Mautner59c00972012-07-30 12:10:24 -07003800}