blob: f05cf2a532bc9fb0fd17db8b5aa97a330215859e [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
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800384 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
385 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800386 final AppWindowToken atoken = w.mAppToken;
387 if (winAnimator.mDrawState == READY_TO_SHOW) {
388 if (atoken == null || atoken.allDrawn) {
389 if (w.performShowLocked()) {
390 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
391 if (DEBUG_LAYOUT_REPEATS) {
392 mService.mWindowPlacerLocked.debugLayoutRepeats(
393 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
394 }
395 }
396 }
397 }
398 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
399 if (appAnimator != null && appAnimator.thumbnail != null) {
400 if (appAnimator.thumbnailTransactionSeq
401 != mTmpWindowAnimator.mAnimTransactionSequence) {
402 appAnimator.thumbnailTransactionSeq =
403 mTmpWindowAnimator.mAnimTransactionSequence;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800404 }
405 }
406 };
407
408 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
409 final WindowStateAnimator winAnimator = w.mWinAnimator;
410 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
411 return;
412 }
413
414 final int flags = w.mAttrs.flags;
415
416 // If this window is animating, make a note that we have an animating window and take
417 // care of a request to run a detached wallpaper animation.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100418 if (winAnimator.isAnimationSet()) {
419 final AnimationAdapter anim = w.getAnimation();
420 if (anim != null) {
421 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && anim.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800422 mTmpWindow = w;
423 }
Jorim Jaggia5e10572017-11-15 14:36:26 +0100424 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800425 if (color != 0) {
426 final TaskStack stack = w.getStack();
427 if (stack != null) {
428 stack.setAnimationBackground(winAnimator, color);
429 }
430 }
431 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800432 }
433
434 // If this window's app token is running a detached wallpaper animation, make a note so
435 // we can ensure the wallpaper is displayed behind it.
436 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
437 if (appAnimator != null && appAnimator.animation != null
438 && appAnimator.animating) {
439 if ((flags & FLAG_SHOW_WALLPAPER) != 0
440 && appAnimator.animation.getDetachWallpaper()) {
441 mTmpWindow = w;
442 }
443
444 final int color = appAnimator.animation.getBackgroundColor();
445 if (color != 0) {
446 final TaskStack stack = w.getStack();
447 if (stack != null) {
448 stack.setAnimationBackground(winAnimator, color);
449 }
450 }
451 }
452 };
453
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800454 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
455 final int lostFocusUid = mTmpWindow.mOwnerUid;
456 final Handler handler = mService.mH;
457 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
458 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
459 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
460 w.mAttrs.hideTimeoutMilliseconds);
461 }
462 }
463 };
464
465 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
466 final AppWindowToken focusedApp = mService.mFocusedApp;
467 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
468 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
469
470 if (!w.canReceiveKeys()) {
471 return false;
472 }
473
474 final AppWindowToken wtoken = w.mAppToken;
475
476 // If this window's application has been removed, just skip it.
477 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
478 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
479 + (wtoken.removed ? "removed" : "sendingToBottom"));
480 return false;
481 }
482
483 if (focusedApp == null) {
484 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
485 + " using new focus @ " + w);
486 mTmpWindow = w;
487 return true;
488 }
489
490 if (!focusedApp.windowsAreFocusable()) {
491 // Current focused app windows aren't focusable...
492 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
493 + " focusable using new focus @ " + w);
494 mTmpWindow = w;
495 return true;
496 }
497
498 // Descend through all of the app tokens and find the first that either matches
499 // win.mAppToken (return win) or mFocusedApp (return null).
500 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
501 if (focusedApp.compareTo(wtoken) > 0) {
502 // App stack below focused app stack. No focus for you!!!
503 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
504 "findFocusedWindow: Reached focused app=" + focusedApp);
505 mTmpWindow = null;
506 return true;
507 }
508 }
509
510 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
511 mTmpWindow = w;
512 return true;
513 };
514
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800515 private final Consumer<WindowState> mPerformLayout = w -> {
516 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
517 // wasting time and funky changes while a window is animating away.
518 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
519 || w.isGoneForLayoutLw();
520
521 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
522 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
523 + " mLayoutAttached=" + w.mLayoutAttached
524 + " screen changed=" + w.isConfigChanged());
525 final AppWindowToken atoken = w.mAppToken;
526 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
527 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
528 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
529 + " parentHidden=" + w.isParentWindowHidden());
530 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
531 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
532 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
533 + " parentHidden=" + w.isParentWindowHidden());
534 }
535
536 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
537 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
538 // since that means "perform layout as normal, just don't display").
539 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
540 || ((w.isConfigChanged() || w.setReportResizeHints())
541 && !w.isGoneForLayoutLw() &&
542 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
543 (w.mHasSurface && w.mAppToken != null &&
544 w.mAppToken.layoutConfigChanges)))) {
545 if (!w.mLayoutAttached) {
546 if (mTmpInitial) {
547 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
548 w.mContentChanged = false;
549 }
550 if (w.mAttrs.type == TYPE_DREAM) {
551 // Don't layout windows behind a dream, so that if it does stuff like hide
552 // the status bar we won't get a bad transition when it goes away.
553 mTmpWindow = w;
554 }
555 w.mLayoutNeeded = false;
556 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200557 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000558 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800559 w.mLayoutSeq = mService.mLayoutSeq;
560
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200561 // If this is the first layout, we need to initialize the last inset values as
562 // otherwise we'd immediately cause an unnecessary resize.
563 if (firstLayout) {
564 w.updateLastInsetValues();
565 }
566
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800567 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
568 + " mContainingFrame=" + w.mContainingFrame
569 + " mDisplayFrame=" + w.mDisplayFrame);
570 }
571 }
572 };
573
574 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
575 if (w.mLayoutAttached) {
576 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
577 + " mViewVisibility=" + w.mViewVisibility
578 + " mRelayoutCalled=" + w.mRelayoutCalled);
579 // If this view is GONE, then skip it -- keep the current frame, and let the caller
580 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
581 // windows, since that means "perform layout as normal, just don't display").
582 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
583 return;
584 }
585 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
586 || w.mLayoutNeeded) {
587 if (mTmpInitial) {
588 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
589 w.mContentChanged = false;
590 }
591 w.mLayoutNeeded = false;
592 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000593 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800594 w.mLayoutSeq = mService.mLayoutSeq;
595 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
596 + " mContainingFrame=" + w.mContainingFrame
597 + " mDisplayFrame=" + w.mDisplayFrame);
598 }
599 } else if (w.mAttrs.type == TYPE_DREAM) {
600 // Don't layout windows behind a dream, so that if it does stuff like hide the
601 // status bar we won't get a bad transition when it goes away.
602 mTmpWindow = mTmpWindow2;
603 }
604 };
605
606 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
607 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
608 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
609 return w.canBeImeTarget();
610 };
611
612 private final Consumer<WindowState> mApplyPostLayoutPolicy =
613 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
614 mService.mInputMethodTarget);
615
616 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
617 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
618 final boolean obscuredChanged = w.mObscured !=
619 mTmpApplySurfaceChangesTransactionState.obscured;
620 final RootWindowContainer root = mService.mRoot;
621 // Only used if default window
622 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
623
624 // Update effect.
625 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
626 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
627 final boolean isDisplayed = w.isDisplayedLw();
628
629 if (isDisplayed && w.isObscuringDisplay()) {
630 // This window completely covers everything behind it, so we want to leave all
631 // of them as undimmed (for performance reasons).
632 root.mObscuringWindow = w;
633 mTmpApplySurfaceChangesTransactionState.obscured = true;
634 }
635
636 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
637 root.handleNotObscuredLocked(w,
638 mTmpApplySurfaceChangesTransactionState.obscured,
639 mTmpApplySurfaceChangesTransactionState.syswin);
640
641 if (w.mHasSurface && isDisplayed) {
642 final int type = w.mAttrs.type;
643 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
644 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
645 mTmpApplySurfaceChangesTransactionState.syswin = true;
646 }
647 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
648 && w.mAttrs.preferredRefreshRate != 0) {
649 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
650 = w.mAttrs.preferredRefreshRate;
651 }
652 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
653 && w.mAttrs.preferredDisplayModeId != 0) {
654 mTmpApplySurfaceChangesTransactionState.preferredModeId
655 = w.mAttrs.preferredDisplayModeId;
656 }
657 }
658 }
659
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800660 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
661 && mWallpaperController.isWallpaperTarget(w)) {
662 // This is the wallpaper target and its obscured state changed... make sure the
663 // current wallpaper's visibility has been updated accordingly.
664 mWallpaperController.updateWallpaperVisibility();
665 }
666
Jorim Jaggia5e10572017-11-15 14:36:26 +0100667 // Use mTmpTransaction instead of mPendingTransaction because we don't want to commit
668 // other changes in mPendingTransaction at this point.
669 w.handleWindowMovedIfNeeded(mTmpTransaction);
670 SurfaceControl.mergeToGlobalTransaction(mTmpTransaction);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800671
672 final WindowStateAnimator winAnimator = w.mWinAnimator;
673
674 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
675 w.mContentChanged = false;
676
677 // Moved from updateWindowsAndWallpaperLocked().
678 if (w.mHasSurface) {
679 // Take care of the window being ready to display.
680 final boolean committed = winAnimator.commitFinishDrawingLocked();
681 if (isDefaultDisplay && committed) {
682 if (w.mAttrs.type == TYPE_DREAM) {
683 // HACK: When a dream is shown, it may at that point hide the lock screen.
684 // So we need to redo the layout to let the phone window manager make this
685 // happen.
686 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
687 if (DEBUG_LAYOUT_REPEATS) {
688 surfacePlacer.debugLayoutRepeats(
689 "dream and commitFinishDrawingLocked true",
690 pendingLayoutChanges);
691 }
692 }
693 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
694 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
695 "First draw done in potential wallpaper target " + w);
696 root.mWallpaperMayChange = true;
697 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
698 if (DEBUG_LAYOUT_REPEATS) {
699 surfacePlacer.debugLayoutRepeats(
700 "wallpaper and commitFinishDrawingLocked true",
701 pendingLayoutChanges);
702 }
703 }
704 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700705 final TaskStack stack = w.getStack();
Jorim Jaggia5e10572017-11-15 14:36:26 +0100706 if ((!winAnimator.isWaitingForOpening())
Robert Carr2117aaf2017-04-25 14:46:50 -0700707 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800708 // Updates the shown frame before we set up the surface. This is needed
709 // because the resizing could change the top-left position (in addition to
710 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
711 // position the surface.
712 //
713 // If an animation is being started, we can't call this method because the
714 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700715 // we do want to update the position if the window is animating. We make an exception
716 // for the bounds animating state, where an application may have been waiting
717 // for an exit animation to start, but instead enters PiP. We need to ensure
718 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800719 winAnimator.computeShownFrameLocked();
720 }
721 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
Jorim Jaggia5e10572017-11-15 14:36:26 +0100722
723 // Since setSurfaceBoundariesLocked applies the clipping, we need to apply the position
724 // to the surface of the window container as well. Use mTmpTransaction instead of
725 // mPendingTransaction to avoid committing any existing changes in there.
726 w.updateSurfacePosition(mTmpTransaction);
727 SurfaceControl.mergeToGlobalTransaction(mTmpTransaction);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800728 }
729
730 final AppWindowToken atoken = w.mAppToken;
731 if (atoken != null) {
732 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
733 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
734 mTmpUpdateAllDrawn.add(atoken);
735 }
736 }
737
738 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
739 && w.isDisplayedLw()) {
740 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
741 }
742
743 w.updateResizingWindowIfNeeded();
744 };
745
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800746 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800747 * Create new {@link DisplayContent} instance, add itself to the root window container and
748 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700749 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800750 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700751 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
752 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700753 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700754 DisplayContent(Display display, WindowManagerService service,
Robert Carrb1579c82017-09-05 14:54:47 -0700755 WallpaperController wallpaperController) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100756 super(service);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800757 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
758 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
759 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
760 + " new=" + display);
761 }
762
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700763 mDisplay = display;
764 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700765 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700766 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700767 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700768 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000769 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700770 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800771 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700772 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700773
Robert Carrb1579c82017-09-05 14:54:47 -0700774 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth);
775
776 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
777 .setSize(mSurfaceSize, mSurfaceSize)
778 .setOpaque(true);
779 mWindowingLayer = b.setName("Display Root").build();
780 mOverlayLayer = b.setName("Display Overlays").build();
781
Robert Carrf59b8dd2017-10-02 18:58:36 -0700782 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700783 .setLayerStack(mWindowingLayer, mDisplayId)
784 .show(mWindowingLayer)
785 .setLayer(mOverlayLayer, 1)
786 .setLayerStack(mOverlayLayer, mDisplayId)
787 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700788 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700789
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700790 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700791 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700792 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700793 super.addChild(mAboveAppWindowsContainers, null);
794 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800795
796 // Add itself as a child to the root container.
797 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700798
799 // TODO(b/62541591): evaluate whether this is the best spot to declare the
800 // {@link DisplayContent} ready for use.
801 mDisplayReady = true;
802 }
803
804 boolean isReady() {
805 // The display is ready when the system and the individual display are both ready.
806 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700807 }
808
809 int getDisplayId() {
810 return mDisplayId;
811 }
812
Wale Ogunwale02319a62016-09-26 15:21:22 -0700813 WindowToken getWindowToken(IBinder binder) {
814 return mTokenMap.get(binder);
815 }
816
817 AppWindowToken getAppWindowToken(IBinder binder) {
818 final WindowToken token = getWindowToken(binder);
819 if (token == null) {
820 return null;
821 }
822 return token.asAppWindowToken();
823 }
824
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700825 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700826 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
827 if (dc != null) {
828 // We currently don't support adding a window token to the display if the display
829 // already has the binder mapped to another token. If there is a use case for supporting
830 // this moving forward we will either need to merge the WindowTokens some how or have
831 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700832 throw new IllegalArgumentException("Can't map token=" + token + " to display="
833 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700834 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700835 if (binder == null) {
836 throw new IllegalArgumentException("Can't map token=" + token + " to display="
837 + getName() + " binder is null");
838 }
839 if (token == null) {
840 throw new IllegalArgumentException("Can't map null token to display="
841 + getName() + " binder=" + binder);
842 }
843
Wale Ogunwale02319a62016-09-26 15:21:22 -0700844 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700845
846 if (token.asAppWindowToken() == null) {
847 // Add non-app token to container hierarchy on the display. App tokens are added through
848 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700849 switch (token.windowType) {
850 case TYPE_WALLPAPER:
851 mBelowAppWindowsContainers.addChild(token);
852 break;
853 case TYPE_INPUT_METHOD:
854 case TYPE_INPUT_METHOD_DIALOG:
855 mImeWindowsContainers.addChild(token);
856 break;
857 default:
858 mAboveAppWindowsContainers.addChild(token);
859 break;
860 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700861 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700862 }
863
864 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700865 final WindowToken token = mTokenMap.remove(binder);
866 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800867 token.setExiting();
868 }
869 return token;
870 }
871
872 /** Changes the display the input window token is housed on to this one. */
873 void reParentWindowToken(WindowToken token) {
874 final DisplayContent prevDc = token.getDisplayContent();
875 if (prevDc == this) {
876 return;
877 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800878 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
879 && token.asAppWindowToken() == null) {
880 // Removed the token from the map, but made sure it's not an app token before removing
881 // from parent.
882 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700883 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800884
885 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700886 }
887
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700888 void removeAppToken(IBinder binder) {
889 final WindowToken token = removeWindowToken(binder);
890 if (token == null) {
891 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
892 return;
893 }
894
895 final AppWindowToken appToken = token.asAppWindowToken();
896
897 if (appToken == null) {
898 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
899 return;
900 }
901
902 appToken.onRemovedFromDisplay();
903 }
904
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700905 Display getDisplay() {
906 return mDisplay;
907 }
908
Craig Mautner59c00972012-07-30 12:10:24 -0700909 DisplayInfo getDisplayInfo() {
910 return mDisplayInfo;
911 }
912
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700913 DisplayMetrics getDisplayMetrics() {
914 return mDisplayMetrics;
915 }
916
Andrii Kulian8ee72852017-03-10 10:36:45 -0800917 int getRotation() {
918 return mRotation;
919 }
920
921 void setRotation(int newRotation) {
922 mRotation = newRotation;
923 }
924
925 int getLastOrientation() {
926 return mLastOrientation;
927 }
928
929 void setLastOrientation(int orientation) {
930 mLastOrientation = orientation;
931 }
932
933 boolean getAltOrientation() {
934 return mAltOrientation;
935 }
936
937 void setAltOrientation(boolean altOrientation) {
938 mAltOrientation = altOrientation;
939 }
940
941 int getLastWindowForcedOrientation() {
942 return mLastWindowForcedOrientation;
943 }
944
Andrii Kulian06d07d62017-03-14 11:11:47 -0700945 /**
946 * Update rotation of the display.
947 *
948 * Returns true if the rotation has been changed. In this case YOU MUST CALL
949 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
950 */
951 boolean updateRotationUnchecked(boolean inTransaction) {
952 if (mService.mDeferredRotationPauseCount > 0) {
953 // Rotation updates have been paused temporarily. Defer the update until
954 // updates have been resumed.
955 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
956 return false;
957 }
958
959 ScreenRotationAnimation screenRotationAnimation =
960 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
961 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
962 // Rotation updates cannot be performed while the previous rotation change
963 // animation is still in progress. Skip this update. We will try updating
964 // again after the animation is finished and the display is unfrozen.
965 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
966 return false;
967 }
968 if (mService.mDisplayFrozen) {
969 // Even if the screen rotation animation has finished (e.g. isAnimating
970 // returns false), there is still some time where we haven't yet unfrozen
971 // the display. We also need to abort rotation here.
972 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
973 "Deferring rotation, still finishing previous rotation");
974 return false;
975 }
976
977 if (!mService.mDisplayEnabled) {
978 // No point choosing a rotation if the display is not enabled.
979 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
980 return false;
981 }
982
983 final int oldRotation = mRotation;
984 final int lastOrientation = mLastOrientation;
985 final boolean oldAltOrientation = mAltOrientation;
986 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr0e007272017-10-02 13:00:55 -0700987 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
Robert Carr897215d2017-03-29 12:25:50 -0700988 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700989
Robert Carr0e007272017-10-02 13:00:55 -0700990 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700991 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
992 if (seamlessRotated != null) {
993 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
994 // to complete (that is, waiting for windows to redraw). It's tempting to check
995 // w.mSeamlessRotationCount but that could be incorrect in the case of
996 // window-removal.
997 return false;
998 }
Robert Carr0e007272017-10-02 13:00:55 -0700999
1000 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001001 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001002 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001003 mayRotateSeamlessly = false;
1004 }
1005 for (int i = 0; i < mService.mSessions.size(); i++) {
1006 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1007 mayRotateSeamlessly = false;
1008 break;
1009 }
1010 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001011 }
Robert Carr0e007272017-10-02 13:00:55 -07001012 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001013
1014 // TODO: Implement forced rotation changes.
1015 // Set mAltOrientation to indicate that the application is receiving
1016 // an orientation that has different metrics than it expected.
1017 // eg. Portrait instead of Landscape.
1018
1019 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
1020 lastOrientation, rotation);
1021
1022 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
1023 + ", got rotation " + rotation + " which has "
1024 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1025
1026 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1027 // No change.
1028 return false;
1029 }
1030
1031 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
1032 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1033 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1034
1035 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1036 mService.mWaitingForConfig = true;
1037 }
1038
1039 mRotation = rotation;
1040 mAltOrientation = altOrientation;
1041 if (isDefaultDisplay) {
1042 mService.mPolicy.setRotationLw(rotation);
1043 }
1044
1045 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1046 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1047 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1048 WINDOW_FREEZE_TIMEOUT_DURATION);
1049
1050 setLayoutNeeded();
1051 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001052 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001053
1054 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001055 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001056 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1057 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1058 mDisplayId);
1059 } else {
1060 // The screen rotation animation uses a screenshot to freeze the screen
1061 // while windows resize underneath.
1062 // When we are rotating seamlessly, we allow the elements to transition
1063 // to their rotated state independently and without a freeze required.
1064 screenRotationAnimation = null;
1065
1066 // We have to reset this in case a window was removed before it
1067 // finished seamless rotation.
1068 mService.mSeamlessRotationCount = 0;
1069 }
1070
1071 // We need to update our screen size information to match the new rotation. If the rotation
1072 // has actually changed then this method will return true and, according to the comment at
1073 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1074 // By updating the Display info here it will be available to
1075 // #computeScreenConfiguration() later.
1076 updateDisplayAndOrientation(getConfiguration().uiMode);
1077
1078 if (!inTransaction) {
1079 if (SHOW_TRANSACTIONS) {
1080 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1081 }
1082 mService.openSurfaceTransaction();
1083 }
1084 try {
1085 // NOTE: We disable the rotation in the emulator because
1086 // it doesn't support hardware OpenGL emulation yet.
1087 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1088 && screenRotationAnimation.hasScreenshot()) {
Robert Carrb1579c82017-09-05 14:54:47 -07001089 if (screenRotationAnimation.setRotationInTransaction(rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001090 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1091 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1092 mService.scheduleAnimationLocked();
1093 }
1094 }
1095
1096 if (rotateSeamlessly) {
1097 forAllWindows(w -> {
1098 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1099 }, true /* traverseTopToBottom */);
1100 }
1101
1102 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1103 } finally {
1104 if (!inTransaction) {
Adrian Roos111aff92017-09-27 18:11:46 +02001105 mService.closeSurfaceTransaction("setRotationUnchecked");
Andrii Kulian06d07d62017-03-14 11:11:47 -07001106 if (SHOW_LIGHT_TRANSACTIONS) {
1107 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1108 }
1109 }
1110 }
1111
1112 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001113 if (w.mHasSurface && !rotateSeamlessly) {
1114 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001115 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001116 mService.mRoot.mOrientationChangeComplete = false;
1117 w.mLastFreezeDuration = 0;
1118 }
1119 w.mReportOrientationChanged = true;
1120 }, true /* traverseTopToBottom */);
1121
1122 if (rotateSeamlessly) {
1123 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1124 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1125 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1126 }
1127
1128 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1129 final WindowManagerService.RotationWatcher rotationWatcher
1130 = mService.mRotationWatchers.get(i);
1131 if (rotationWatcher.mDisplayId == mDisplayId) {
1132 try {
1133 rotationWatcher.mWatcher.onRotationChanged(rotation);
1134 } catch (RemoteException e) {
1135 // Ignore
1136 }
1137 }
1138 }
1139
1140 // TODO (multi-display): Magnification is supported only for the default display.
1141 // Announce rotation only if we will not animate as we already have the
1142 // windows in final state. Otherwise, we make this call at the rotation end.
1143 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1144 && isDefaultDisplay) {
1145 mService.mAccessibilityController.onRotationChangedLocked(this);
1146 }
1147
1148 return true;
1149 }
1150
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001151 void configureDisplayPolicy() {
1152 mService.mPolicy.setInitialDisplaySize(getDisplay(),
1153 mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1154
1155 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
1156 }
1157
Andrii Kulian06d07d62017-03-14 11:11:47 -07001158 /**
1159 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1160 * changed.
1161 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1162 */
1163 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1164 // Use the effective "visual" dimensions based on current rotation
1165 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1166 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1167 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1168 int dw = realdw;
1169 int dh = realdh;
1170
1171 if (mAltOrientation) {
1172 if (realdw > realdh) {
1173 // Turn landscape into portrait.
1174 int maxw = (int)(realdh/1.3f);
1175 if (maxw < realdw) {
1176 dw = maxw;
1177 }
1178 } else {
1179 // Turn portrait into landscape.
1180 int maxh = (int)(realdw/1.3f);
1181 if (maxh < realdh) {
1182 dh = maxh;
1183 }
1184 }
1185 }
1186
1187 // Update application display metrics.
1188 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1189 mDisplayId);
1190 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1191 mDisplayId);
1192 mDisplayInfo.rotation = mRotation;
1193 mDisplayInfo.logicalWidth = dw;
1194 mDisplayInfo.logicalHeight = dh;
1195 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1196 mDisplayInfo.appWidth = appWidth;
1197 mDisplayInfo.appHeight = appHeight;
1198 if (isDefaultDisplay) {
1199 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1200 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1201 }
1202 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1203 if (mDisplayScalingDisabled) {
1204 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1205 } else {
1206 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1207 }
1208
1209 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1210 mDisplayInfo);
1211
1212 mBaseDisplayRect.set(0, 0, dw, dh);
1213
1214 if (isDefaultDisplay) {
1215 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1216 mCompatDisplayMetrics);
1217 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001218
1219 updateBounds();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001220 return mDisplayInfo;
1221 }
1222
1223 /**
1224 * Compute display configuration based on display properties and policy settings.
1225 * Do not call if mDisplayReady == false.
1226 */
1227 void computeScreenConfiguration(Configuration config) {
1228 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1229
1230 final int dw = displayInfo.logicalWidth;
1231 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001232 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1233 // TODO: Probably best to set this based on some setting in the display content object,
1234 // so the display can be configured for things like fullscreen.
1235 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001236
Andrii Kulian06d07d62017-03-14 11:11:47 -07001237 config.screenWidthDp =
1238 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1239 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1240 config.screenHeightDp =
1241 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1242 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001243
1244 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1245 final int leftInset = mTmpRect.left;
1246 final int topInset = mTmpRect.top;
1247 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001248 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1249 leftInset + displayInfo.appWidth /* right */,
1250 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001251 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1252 || displayInfo.rotation == Surface.ROTATION_270);
1253
1254 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1255 mDisplayMetrics.density, config);
1256
1257 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1258 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1259 ? Configuration.SCREENLAYOUT_ROUND_YES
1260 : Configuration.SCREENLAYOUT_ROUND_NO);
1261
1262 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1263 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1264 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1265 dh, mDisplayId);
1266 config.densityDpi = displayInfo.logicalDensityDpi;
1267
1268 config.colorMode =
1269 (displayInfo.isHdr()
1270 ? Configuration.COLOR_MODE_HDR_YES
1271 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001272 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001273 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1274 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1275
1276 // Update the configuration based on available input devices, lid switch,
1277 // and platform configuration.
1278 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1279 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1280 config.navigation = Configuration.NAVIGATION_NONAV;
1281
1282 int keyboardPresence = 0;
1283 int navigationPresence = 0;
1284 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1285 final int len = devices != null ? devices.length : 0;
1286 for (int i = 0; i < len; i++) {
1287 InputDevice device = devices[i];
1288 if (!device.isVirtual()) {
1289 final int sources = device.getSources();
1290 final int presenceFlag = device.isExternal() ?
1291 WindowManagerPolicy.PRESENCE_EXTERNAL :
1292 WindowManagerPolicy.PRESENCE_INTERNAL;
1293
1294 // TODO(multi-display): Configure on per-display basis.
1295 if (mService.mIsTouchDevice) {
1296 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1297 InputDevice.SOURCE_TOUCHSCREEN) {
1298 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1299 }
1300 } else {
1301 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1302 }
1303
1304 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1305 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1306 navigationPresence |= presenceFlag;
1307 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1308 && config.navigation == Configuration.NAVIGATION_NONAV) {
1309 config.navigation = Configuration.NAVIGATION_DPAD;
1310 navigationPresence |= presenceFlag;
1311 }
1312
1313 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1314 config.keyboard = Configuration.KEYBOARD_QWERTY;
1315 keyboardPresence |= presenceFlag;
1316 }
1317 }
1318 }
1319
1320 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1321 config.navigation = Configuration.NAVIGATION_DPAD;
1322 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1323 }
1324
1325 // Determine whether a hard keyboard is available and enabled.
1326 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1327 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1328 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1329 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1330 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1331 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1332 }
1333
1334 // Let the policy update hidden states.
1335 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1336 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1337 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1338 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1339 }
1340
1341 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1342 int displayId) {
1343 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1344 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1345 final int unrotDw, unrotDh;
1346 if (rotated) {
1347 unrotDw = dh;
1348 unrotDh = dw;
1349 } else {
1350 unrotDw = dw;
1351 unrotDh = dh;
1352 }
1353 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1354 displayId);
1355 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1356 displayId);
1357 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1358 displayId);
1359 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1360 displayId);
1361 return sw;
1362 }
1363
1364 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1365 DisplayMetrics dm, int dw, int dh, int displayId) {
1366 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1367 displayId);
1368 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1369 uiMode, displayId);
1370 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1371 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1372 if (curSize == 0 || size < curSize) {
1373 curSize = size;
1374 }
1375 return curSize;
1376 }
1377
1378 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1379 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1380
1381 // We need to determine the smallest width that will occur under normal
1382 // operation. To this, start with the base screen size and compute the
1383 // width under the different possible rotations. We need to un-rotate
1384 // the current screen dimensions before doing this.
1385 int unrotDw, unrotDh;
1386 if (rotated) {
1387 unrotDw = dh;
1388 unrotDh = dw;
1389 } else {
1390 unrotDw = dw;
1391 unrotDh = dh;
1392 }
1393 displayInfo.smallestNominalAppWidth = 1<<30;
1394 displayInfo.smallestNominalAppHeight = 1<<30;
1395 displayInfo.largestNominalAppWidth = 0;
1396 displayInfo.largestNominalAppHeight = 0;
1397 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1398 unrotDh);
1399 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1400 unrotDw);
1401 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1402 unrotDh);
1403 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1404 unrotDw);
1405 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1406 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1407 displayId);
1408 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1409 displayId);
1410 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1411 displayId);
1412 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1413 displayId);
1414 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1415 outConfig.screenLayout = sl;
1416 }
1417
1418 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1419 int uiMode, int displayId) {
1420 // Get the app screen size at this rotation.
1421 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1422 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1423
1424 // Compute the screen layout size class for this rotation.
1425 int longSize = w;
1426 int shortSize = h;
1427 if (longSize < shortSize) {
1428 int tmp = longSize;
1429 longSize = shortSize;
1430 shortSize = tmp;
1431 }
1432 longSize = (int)(longSize/density);
1433 shortSize = (int)(shortSize/density);
1434 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1435 }
1436
1437 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1438 int uiMode, int dw, int dh) {
1439 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1440 displayId);
1441 if (width < displayInfo.smallestNominalAppWidth) {
1442 displayInfo.smallestNominalAppWidth = width;
1443 }
1444 if (width > displayInfo.largestNominalAppWidth) {
1445 displayInfo.largestNominalAppWidth = width;
1446 }
1447 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1448 displayId);
1449 if (height < displayInfo.smallestNominalAppHeight) {
1450 displayInfo.smallestNominalAppHeight = height;
1451 }
1452 if (height > displayInfo.largestNominalAppHeight) {
1453 displayInfo.largestNominalAppHeight = height;
1454 }
1455 }
1456
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001457 DockedStackDividerController getDockedDividerController() {
1458 return mDividerControllerLocked;
1459 }
1460
Winson Chung655332c2016-10-31 13:14:28 -07001461 PinnedStackController getPinnedStackController() {
1462 return mPinnedStackControllerLocked;
1463 }
1464
Jeff Browna506a6e2013-06-04 00:02:38 -07001465 /**
1466 * Returns true if the specified UID has access to this display.
1467 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001468 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001469 return mDisplay.hasAccess(uid);
1470 }
1471
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001472 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001473 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001474 }
1475
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001476 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001477 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001478 }
1479
Wale Ogunwale61911492017-10-11 08:50:50 -07001480 /**
1481 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1482 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001483 TaskStack getSplitScreenPrimaryStack() {
1484 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001485 return (stack != null && stack.isVisible()) ? stack : null;
1486 }
1487
1488 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001489 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001490 * not visible.
1491 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001492 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1493 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001494 }
1495
1496 TaskStack getPinnedStack() {
1497 return mTaskStackContainers.getPinnedStack();
1498 }
1499
1500 private boolean hasPinnedStack() {
1501 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001502 }
1503
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001504 /**
1505 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1506 * Null is no compatible stack on the display.
1507 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001508 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001509 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1510 }
1511
1512 /**
1513 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1514 * activity type. Null is no compatible stack on the display.
1515 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001516 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001517 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001518 }
1519
Bryce Lee48f4b572017-04-10 10:54:15 -07001520 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001521 TaskStack getTopStack() {
1522 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001523 }
1524
Wale Ogunwale61911492017-10-11 08:50:50 -07001525 void onStackWindowingModeChanged(TaskStack stack) {
1526 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001527 }
1528
Andrii Kulian441e4492016-09-29 15:25:00 -07001529 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001530 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001531 super.onConfigurationChanged(newParentConfig);
1532
Andrii Kulian3a507b52016-09-19 18:14:12 -07001533 // The display size information is heavily dependent on the resources in the current
1534 // configuration, so we need to reconfigure it every time the configuration changes.
1535 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1536 mService.reconfigureDisplayLocked(this);
1537
Bryce Leef3c6a472017-11-14 14:53:06 -08001538 final DockedStackDividerController dividerController = getDockedDividerController();
1539
1540 if (dividerController != null) {
1541 getDockedDividerController().onConfigurationChanged();
1542 }
1543
1544 final PinnedStackController pinnedStackController = getPinnedStackController();
1545
1546 if (pinnedStackController != null) {
1547 getPinnedStackController().onConfigurationChanged();
1548 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001549 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001550
Andrii Kulian441e4492016-09-29 15:25:00 -07001551 /**
1552 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1553 * bounds were updated.
1554 */
1555 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001556 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1557 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001558 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001559 changedStackList.add(stack.mStackId);
1560 }
1561 }
Winson Chung32c566f2017-04-11 18:31:21 -07001562
1563 // If there was no pinned stack, we still need to notify the controller of the display info
1564 // update as a result of the config change. We do this here to consolidate the flow between
1565 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001566 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001567 mPinnedStackControllerLocked.onDisplayInfoChanged();
1568 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001569 }
1570
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001571 @Override
1572 boolean fillsParent() {
1573 return true;
1574 }
1575
1576 @Override
1577 boolean isVisible() {
1578 return true;
1579 }
1580
1581 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001582 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001583 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001584 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001585 }
1586
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001587 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001588 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1589 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1590 // target, or here in order if there isn't an IME target.
1591 if (traverseTopToBottom) {
1592 for (int i = mChildren.size() - 1; i >= 0; --i) {
1593 final DisplayChildWindowContainer child = mChildren.get(i);
1594 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1595 // In this case the Ime windows will be processed above their target so we skip
1596 // here.
1597 continue;
1598 }
1599 if (child.forAllWindows(callback, traverseTopToBottom)) {
1600 return true;
1601 }
1602 }
1603 } else {
1604 final int count = mChildren.size();
1605 for (int i = 0; i < count; i++) {
1606 final DisplayChildWindowContainer child = mChildren.get(i);
1607 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1608 // In this case the Ime windows will be processed above their target so we skip
1609 // here.
1610 continue;
1611 }
1612 if (child.forAllWindows(callback, traverseTopToBottom)) {
1613 return true;
1614 }
1615 }
1616 }
1617 return false;
1618 }
1619
1620 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1621 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1622 }
1623
Wale Ogunwale399c8692017-05-08 14:22:42 -07001624 @Override
1625 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001626 final WindowManagerPolicy policy = mService.mPolicy;
1627
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001628 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001629 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001630 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001631 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001632 // If the display is frozen, some activities may be in the middle of restarting, and
1633 // thus have removed their old window. If the window has the flag to hide the lock
1634 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1635 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001636 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001637 } else if (policy.isKeyguardLocked()) {
1638 // Use the last orientation the while the display is frozen with the keyguard
1639 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1640 // window. We don't want to check the show when locked window directly though as
1641 // things aren't stable while the display is frozen, for example the window could be
1642 // momentarily unavailable due to activity relaunch.
1643 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001644 + "return " + mLastOrientation);
1645 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001646 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001647 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001648 final int orientation = mAboveAppWindowsContainers.getOrientation();
1649 if (orientation != SCREEN_ORIENTATION_UNSET) {
1650 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001651 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001652 }
1653
Wale Ogunwale399c8692017-05-08 14:22:42 -07001654 // Top system windows are not requesting an orientation. Start searching from apps.
1655 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001656 }
1657
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001658 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001659 // Check if display metrics changed and update base values if needed.
1660 updateBaseDisplayMetricsIfNeeded();
1661
Craig Mautner722285e2012-09-07 13:55:58 -07001662 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001663 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001664
Wale Ogunwale61911492017-10-11 08:50:50 -07001665 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1666 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001667 }
Craig Mautner722285e2012-09-07 13:55:58 -07001668 }
1669
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001670 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001671 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1672 if (displayManagerInternal != null) {
1673 // Bootstrap the default logical display from the display manager.
1674 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1675 if (newDisplayInfo != null) {
1676 mDisplayInfo.copyFrom(newDisplayInfo);
1677 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001678 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001679
Andrii Kuliancd097992017-03-23 18:31:59 -07001680 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1681 mDisplayInfo.logicalDensityDpi);
1682 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1683 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1684 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001685 }
1686
Andrii Kuliancd097992017-03-23 18:31:59 -07001687 /**
1688 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1689 * values.
1690 */
1691 private void updateBaseDisplayMetricsIfNeeded() {
1692 // Get real display metrics without overrides from WM.
1693 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1694 final int orientation = mDisplayInfo.rotation;
1695 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1696 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1697 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1698 final int newDensity = mDisplayInfo.logicalDensityDpi;
1699
1700 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1701 || mInitialDisplayHeight != newHeight
1702 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1703
1704 if (displayMetricsChanged) {
1705 // Check if display size or density is forced.
1706 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1707 || mBaseDisplayHeight != mInitialDisplayHeight;
1708 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1709
1710 // If there is an override set for base values - use it, otherwise use new values.
1711 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1712 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1713 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1714
1715 // Real display metrics changed, so we should also update initial values.
1716 mInitialDisplayWidth = newWidth;
1717 mInitialDisplayHeight = newHeight;
1718 mInitialDisplayDensity = newDensity;
1719 mService.reconfigureDisplayLocked(this);
1720 }
1721 }
1722
Bryce Lee27cec322017-03-21 09:41:37 -07001723 /** Sets the maximum width the screen resolution can be */
1724 void setMaxUiWidth(int width) {
1725 if (DEBUG_DISPLAY) {
1726 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1727 }
1728
1729 mMaxUiWidth = width;
1730
1731 // Update existing metrics.
1732 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1733 }
1734
1735 /** Update base (override) display metrics. */
1736 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1737 mBaseDisplayWidth = baseWidth;
1738 mBaseDisplayHeight = baseHeight;
1739 mBaseDisplayDensity = baseDensity;
1740
1741 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1742 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1743 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1744 mBaseDisplayWidth = mMaxUiWidth;
1745
1746 if (DEBUG_DISPLAY) {
1747 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1748 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1749 + " on display:" + getDisplayId());
1750 }
1751 }
1752
1753 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08001754
1755 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07001756 }
1757
Wale Ogunwaledb506192017-12-08 10:57:32 -08001758 void getStableRect(Rect out) {
1759 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08001760 }
1761
Wale Ogunwale61911492017-10-11 08:50:50 -07001762 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001763 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1764 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001765
Wale Ogunwale61911492017-10-11 08:50:50 -07001766 final TaskStack stack = new TaskStack(mService, stackId, controller);
1767 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001768 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001769 }
1770
Andrii Kulian51c1b672017-04-07 18:39:32 -07001771 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001772 final DisplayContent prevDc = stack.getDisplayContent();
1773 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001774 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1775 + " which is not currently attached to any display");
1776 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001777 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001778 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1779 + " to its current displayId=" + mDisplayId);
1780 }
1781
Wale Ogunwale61911492017-10-11 08:50:50 -07001782 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001783 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001784 }
1785
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001786 @Override
1787 protected void addChild(DisplayChildWindowContainer child,
1788 Comparator<DisplayChildWindowContainer> comparator) {
1789 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1790 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001791
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001792 @Override
1793 protected void addChild(DisplayChildWindowContainer child, int index) {
1794 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1795 }
1796
1797 @Override
1798 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001799 // Only allow removal of direct children from this display if the display is in the process
1800 // of been removed.
1801 if (mRemovingDisplay) {
1802 super.removeChild(child);
1803 return;
1804 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001805 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001806 }
1807
Andrii Kuliand2765632016-12-12 22:26:34 -08001808 @Override
1809 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1810 // Children of the display are statically ordered, so the real intention here is to perform
1811 // the operation on the display and not the static direct children.
1812 getParent().positionChildAt(position, this, includingParents);
1813 }
1814
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001815 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001816 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1817 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001818 final int taskId = stack.taskIdFromPoint(x, y);
1819 if (taskId != -1) {
1820 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001821 }
1822 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001823 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001824 }
1825
Chong Zhang8e89b312015-09-09 15:09:30 -07001826 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001827 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001828 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001829 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001830 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001831 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001832 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07001833 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1834 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001835 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001836 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001837 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001838
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001839 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1840 if (mTmpTaskForResizePointSearchResult.searchDone) {
1841 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001842 }
1843 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001844 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001845 }
1846
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001847 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001848 // The provided task is the task on this display with focus, so if WindowManagerService's
1849 // focused app is not on this display, focusedTask will be null.
1850 if (focusedTask == null) {
1851 mTouchExcludeRegion.setEmpty();
1852 } else {
1853 mTouchExcludeRegion.set(mBaseDisplayRect);
1854 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1855 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07001856 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1857 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001858 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
1859 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08001860 }
1861 // If we removed the focused task above, add it back and only leave its
1862 // outside touch area in the exclusion. TapDectector is not interested in
1863 // any touch inside the focused task itself.
1864 if (!mTmpRect2.isEmpty()) {
1865 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1866 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001867 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001868 final WindowState inputMethod = mService.mInputMethodWindow;
1869 if (inputMethod != null && inputMethod.isVisibleLw()) {
1870 // If the input method is visible and the user is typing, we don't want these touch
1871 // events to be intercepted and used to change focus. This would likely cause a
1872 // disappearance of the input method.
1873 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001874 if (inputMethod.getDisplayId() == mDisplayId) {
1875 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1876 } else {
1877 // IME is on a different display, so we need to update its tap detector.
1878 // TODO(multidisplay): Remove when IME will always appear on same display.
1879 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1880 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001881 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001882 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1883 WindowState win = mTapExcludedWindows.get(i);
1884 win.getTouchableRegion(mTmpRegion);
1885 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1886 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001887 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07001888 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001889 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001890 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001891 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1892 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001893 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001894 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001895 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001896 }
1897
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001898 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001899 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001900 super.switchUser();
1901 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001902 }
1903
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001904 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001905 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1906 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001907 }
1908 }
1909
Wale Ogunwale10124582016-09-15 20:25:50 -07001910 @Override
1911 void removeIfPossible() {
1912 if (isAnimating()) {
1913 mDeferredRemoval = true;
1914 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001915 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001916 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001917 }
1918
Wale Ogunwale10124582016-09-15 20:25:50 -07001919 @Override
1920 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001921 mRemovingDisplay = true;
1922 try {
1923 super.removeImmediately();
1924 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07001925 if (mService.canDispatchPointerEvents()) {
1926 if (mTapDetector != null) {
1927 mService.unregisterPointerEventListener(mTapDetector);
1928 }
1929 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
1930 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1931 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001932 }
Robert Carrb1579c82017-09-05 14:54:47 -07001933 // The pending transaction won't be applied so we should
1934 // just clean up any surfaces pending destruction.
1935 onPendingTransactionApplied();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001936 } finally {
1937 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001938 }
Craig Mautner95da1082014-02-24 17:54:35 -08001939 }
1940
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001941 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001942 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001943 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001944 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1945
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001946 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001947 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001948 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001949 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001950 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001951 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001952 }
1953
Andrii Kulian0214ed92017-05-16 13:44:05 -07001954 /** @return 'true' if removal of this display content is deferred due to active animation. */
1955 boolean isRemovalDeferred() {
1956 return mDeferredRemoval;
1957 }
1958
Wale Ogunwale10124582016-09-15 20:25:50 -07001959 boolean animateForIme(float interpolatedValue, float animationTarget,
1960 float dividerAnimationTarget) {
1961 boolean updated = false;
1962
Wale Ogunwale61911492017-10-11 08:50:50 -07001963 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1964 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001965 if (stack == null || !stack.isAdjustedForIme()) {
1966 continue;
1967 }
1968
1969 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1970 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1971 updated = true;
1972 } else {
1973 mDividerControllerLocked.mLastAnimationProgress =
1974 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1975 mDividerControllerLocked.mLastDividerProgress =
1976 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1977 updated |= stack.updateAdjustForIme(
1978 mDividerControllerLocked.mLastAnimationProgress,
1979 mDividerControllerLocked.mLastDividerProgress,
1980 false /* force */);
1981 }
1982 if (interpolatedValue >= 1f) {
1983 stack.endImeAdjustAnimation();
1984 }
1985 }
1986
1987 return updated;
1988 }
1989
1990 boolean clearImeAdjustAnimation() {
1991 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07001992 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1993 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001994 if (stack != null && stack.isAdjustedForIme()) {
1995 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1996 changed = true;
1997 }
1998 }
1999 return changed;
2000 }
2001
2002 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002003 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2004 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002005 if (stack.isVisible() && stack.isAdjustedForIme()) {
2006 stack.beginImeAdjustAnimation();
2007 }
2008 }
2009 }
2010
2011 void adjustForImeIfNeeded() {
2012 final WindowState imeWin = mService.mInputMethodWindow;
2013 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2014 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002015 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002016 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2017 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2018 imeTargetStack.getDockSide() : DOCKED_INVALID;
2019 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2020 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2021 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002022 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002023 final boolean imeHeightChanged = imeVisible &&
2024 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2025
2026 // The divider could be adjusted for IME position, or be thinner than usual,
2027 // or both. There are three possible cases:
2028 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2029 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2030 // - If IME is not visible, divider is not moved and is normal width.
2031
2032 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002033 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2034 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002035 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002036 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2037 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002038 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2039 } else {
2040 stack.resetAdjustedForIme(false);
2041 }
2042 }
2043 mDividerControllerLocked.setAdjustedForIme(
2044 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2045 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002046 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2047 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002048 stack.resetAdjustedForIme(!dockVisible);
2049 }
2050 mDividerControllerLocked.setAdjustedForIme(
2051 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2052 }
Winson Chung655332c2016-10-31 13:14:28 -07002053 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002054 }
2055
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002056 void setInputMethodAnimLayerAdjustment(int adj) {
2057 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2058 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002059 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002060 }
2061
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002062 /**
2063 * If a window that has an animation specifying a colored background and the current wallpaper
2064 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2065 * suddenly disappear.
2066 */
2067 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002068 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2069 w -> w.mIsWallpaper && w.isVisibleNow());
2070
2071 if (visibleWallpaper != null) {
2072 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002073 }
2074 return winAnimator.mAnimLayer;
2075 }
2076
Wale Ogunwale10124582016-09-15 20:25:50 -07002077 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002078 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2079 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002080 stack.prepareFreezingTaskBounds();
2081 }
2082 }
2083
Wale Ogunwale94744212015-09-21 19:01:47 -07002084 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002085 getBounds(mTmpRect, newRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002086
2087 // Compute a transform matrix to undo the coordinate space transformation,
2088 // and present the window at the same physical position it previously occupied.
2089 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2090 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2091
2092 mTmpRectF.set(bounds);
2093 mTmpMatrix.mapRect(mTmpRectF);
2094 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002095 }
2096
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002097 static int deltaRotation(int oldRotation, int newRotation) {
2098 int delta = newRotation - oldRotation;
2099 if (delta < 0) delta += 4;
2100 return delta;
2101 }
2102
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002103 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002104 Matrix outMatrix) {
2105 // For rotations without Z-ordering we don't need the target rectangle's position.
2106 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2107 displayHeight, outMatrix);
2108 }
2109
2110 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2111 float displayWidth, float displayHeight, Matrix outMatrix) {
2112 switch (rotation) {
2113 case ROTATION_0:
2114 outMatrix.reset();
2115 break;
2116 case ROTATION_270:
2117 outMatrix.setRotate(270, 0, 0);
2118 outMatrix.postTranslate(0, displayHeight);
2119 outMatrix.postTranslate(rectTop, 0);
2120 break;
2121 case ROTATION_180:
2122 outMatrix.reset();
2123 break;
2124 case ROTATION_90:
2125 outMatrix.setRotate(90, 0, 0);
2126 outMatrix.postTranslate(displayWidth, 0);
2127 outMatrix.postTranslate(-rectTop, rectLeft);
2128 break;
2129 }
2130 }
2131
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002132 @CallSuper
2133 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002134 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002135 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002136 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002137 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002138 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2139 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002140 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002141 }
2142 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2143 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002144 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2145 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002146 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002147 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002148 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2149 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002150 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002151 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002152 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2153 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002154 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002155 }
2156 proto.write(DPI, mBaseDisplayDensity);
2157 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002158 proto.write(ROTATION, mRotation);
2159 final ScreenRotationAnimation screenRotationAnimation =
2160 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2161 if (screenRotationAnimation != null) {
2162 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2163 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002164 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002165 proto.end(token);
2166 }
2167
Craig Mautnera91f9e22012-09-14 16:22:08 -07002168 public void dump(String prefix, PrintWriter pw) {
2169 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2170 final String subPrefix = " " + prefix;
2171 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2172 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2173 pw.print("dpi");
2174 if (mInitialDisplayWidth != mBaseDisplayWidth
2175 || mInitialDisplayHeight != mBaseDisplayHeight
2176 || mInitialDisplayDensity != mBaseDisplayDensity) {
2177 pw.print(" base=");
2178 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2179 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2180 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002181 if (mDisplayScalingDisabled) {
2182 pw.println(" noscale");
2183 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002184 pw.print(" cur=");
2185 pw.print(mDisplayInfo.logicalWidth);
2186 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2187 pw.print(" app=");
2188 pw.print(mDisplayInfo.appWidth);
2189 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2190 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2191 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2192 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2193 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002194 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002195 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002196 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002197
Craig Mautnerdc548482014-02-05 13:35:24 -08002198 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002199 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002200 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2201 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002202 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002203 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002204
Craig Mautnerdc548482014-02-05 13:35:24 -08002205 pw.println();
2206 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002207 pw.println();
2208 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002209 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002210 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002211 pw.print(" Exiting #"); pw.print(i);
2212 pw.print(' '); pw.print(token);
2213 pw.println(':');
2214 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002215 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002216 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002217
Jorim Jaggi31f71702016-05-04 16:43:04 -07002218 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002219
2220 // Dump stack references
2221 final TaskStack homeStack = getHomeStack();
2222 if (homeStack != null) {
2223 pw.println(prefix + "homeStack=" + homeStack.getName());
2224 }
2225 final TaskStack pinnedStack = getPinnedStack();
2226 if (pinnedStack != null) {
2227 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2228 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002229 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002230 if (splitScreenPrimaryStack != null) {
2231 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2232 }
2233
2234 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002235 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002236 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002237 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002238
2239 if (mInputMethodAnimLayerAdjustment != 0) {
2240 pw.println(subPrefix
2241 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2242 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002243
2244 pw.println();
2245 mDisplayFrames.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002246 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002247
2248 @Override
2249 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002250 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002251 }
2252
2253 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002254 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002255 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002256
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002257 /** Returns true if the stack in the windowing mode is visible. */
2258 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002259 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002260 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002261 }
2262
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002263 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002264 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002265 final int x = (int) xf;
2266 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002267 final WindowState touchedWin = getWindow(w -> {
2268 final int flags = w.mAttrs.flags;
2269 if (!w.isVisibleLw()) {
2270 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002271 }
2272 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002273 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002274 }
2275
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002276 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002277 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002278 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002279 }
2280
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002281 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002282
2283 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002284 return mTmpRegion.contains(x, y) || touchFlags == 0;
2285 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002286
2287 return touchedWin;
2288 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002289
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002290 boolean canAddToastWindowForUid(int uid) {
2291 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002292 // Also if the app is focused adding more than one toast at
2293 // a time for better backwards compatibility.
2294 final WindowState focusedWindowForUid = getWindow(w ->
2295 w.mOwnerUid == uid && w.isFocused());
2296 if (focusedWindowForUid != null) {
2297 return true;
2298 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002299 final WindowState win = getWindow(w ->
2300 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2301 && !w.mWindowRemovalAllowed);
2302 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002303 }
2304
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002305 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002306 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2307 return;
2308 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002309
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002310 // Used to communicate the old focus to the callback method.
2311 mTmpWindow = oldFocus;
2312
2313 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002314 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002315
2316 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002317 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002318
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002319 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002320
2321 if (mTmpWindow == null) {
2322 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2323 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002324 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002325 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002326 }
2327
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002328 /** Updates the layer assignment of windows on this display. */
2329 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002330 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002331 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002332 if (setLayoutNeeded) {
2333 setLayoutNeeded();
2334 }
Robert Carrb1579c82017-09-05 14:54:47 -07002335
Robert Carrf59b8dd2017-10-02 18:58:36 -07002336 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002337 // the application of this transaction until the animation pass triggers
2338 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2339 // the hiding and showing of surfaces.
2340 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002341 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002342 }
2343
Wale Ogunwale1666e312016-12-16 11:27:18 -08002344 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2345 // moving containers or resizing them. Need to investigate the best way to have it automatically
2346 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002347 void layoutAndAssignWindowLayersIfNeeded() {
2348 mService.mWindowsChanged = true;
2349 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002350
2351 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2352 false /*updateInputWindows*/)) {
2353 assignWindowLayers(false /* setLayoutNeeded */);
2354 }
2355
2356 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2357 mService.mWindowPlacerLocked.performSurfacePlacement();
2358 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2359 }
2360
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002361 /** Returns true if a leaked surface was destroyed */
2362 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002363 // Used to indicate that a surface was leaked.
2364 mTmpWindow = null;
2365 forAllWindows(w -> {
2366 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002367 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002368 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002369 }
2370 if (!mService.mSessions.contains(wsa.mSession)) {
2371 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002372 + w + " surface=" + wsa.mSurfaceController
2373 + " token=" + w.mToken
2374 + " pid=" + w.mSession.mPid
2375 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002376 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002377 mService.mForceRemoves.add(w);
2378 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002379 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002380 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002381 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002382 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002383 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002384 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002385 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002386 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002387 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002388
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002389 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002390 }
2391
2392 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002393 * Determine and return the window that should be the IME target.
2394 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2395 * @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 +00002396 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002397 WindowState computeImeTarget(boolean updateImeTarget) {
2398 if (mService.mInputMethodWindow == null) {
2399 // There isn't an IME so there shouldn't be a target...That was easy!
2400 if (updateImeTarget) {
2401 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2402 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2403 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2404 }
2405 return null;
2406 }
2407
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002408 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2409 // same display. Or even when the current IME/target are not on the same screen as the next
2410 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002411 mUpdateImeTarget = updateImeTarget;
2412 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002413
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002414
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002415 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2416 // to be on top of it, but it is not -really- where input will go. So look down below
2417 // for a real window to target...
2418 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2419 final AppWindowToken token = target.mAppToken;
2420 if (token != null) {
2421 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2422 if (betterTarget != null) {
2423 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002424 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002425 }
2426 }
2427
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002428 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2429 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002430
2431 // Now, a special case -- if the last target's window is in the process of exiting, and is
2432 // above the new target, keep on the last target to avoid flicker. Consider for example a
2433 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2434 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2435 // scrim.
2436 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002437 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2438 && (target == null
2439 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002440 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002441 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002442 }
2443
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002444 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2445 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002446
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002447 if (target == null) {
2448 if (updateImeTarget) {
2449 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2450 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2451 + Debug.getCallers(4) : ""));
2452 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2453 }
2454
2455 return null;
2456 }
2457
2458 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002459 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2460 if (token != null) {
2461
2462 // Now some fun for dealing with window animations that modify the Z order. We need
2463 // to look at all windows below the current target that are in this app, finding the
2464 // highest visible one in layering.
2465 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002466 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002467 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002468 }
2469
2470 if (highestTarget != null) {
2471 final AppTransition appTransition = mService.mAppTransition;
2472 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2473 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2474 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002475 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002476
2477 if (appTransition.isTransitionSet()) {
2478 // If we are currently setting up for an animation, hold everything until we
2479 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002480 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2481 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002482 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002483 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002484 // If the window we are currently targeting is involved with an animation,
2485 // and it is on top of the next target we will be over, then hold off on
2486 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002487 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2488 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002489 }
2490 }
2491 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002492
2493 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2494 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2495 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002496 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002497 }
2498
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002499 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002500 }
2501
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002502 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2503 if (target == mService.mInputMethodTarget
2504 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2505 && mInputMethodAnimLayerAdjustment == layerAdj) {
2506 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002507 }
2508
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002509 mService.mInputMethodTarget = target;
2510 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2511 setInputMethodAnimLayerAdjustment(layerAdj);
2512 assignWindowLayers(false /* setLayoutNeeded */);
2513 }
2514
2515 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2516 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2517 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2518 }
2519
2520 // Used to indicate we have reached the first window in the range we are interested in.
2521 mTmpWindow = null;
2522
2523 // TODO: Figure-out a more efficient way to do this.
2524 final WindowState candidate = getWindow(w -> {
2525 if (w == top) {
2526 // Reached the first window in the range we are interested in.
2527 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002528 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002529 if (mTmpWindow == null) {
2530 return false;
2531 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002532
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002533 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2534 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002535 }
2536 // If we reached the bottom of the range of windows we are considering,
2537 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002538 if (w == bottom) {
2539 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002540 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002541 return false;
2542 });
2543
2544 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002545 }
2546
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002547 void setLayoutNeeded() {
2548 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2549 mLayoutNeeded = true;
2550 }
2551
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002552 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002553 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2554 mLayoutNeeded = false;
2555 }
2556
2557 boolean isLayoutNeeded() {
2558 return mLayoutNeeded;
2559 }
2560
Wale Ogunwale02319a62016-09-26 15:21:22 -07002561 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2562 if (mTokenMap.isEmpty()) {
2563 return;
2564 }
2565 pw.println(" Display #" + mDisplayId);
2566 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2567 while (it.hasNext()) {
2568 final WindowToken token = it.next();
2569 pw.print(" ");
2570 pw.print(token);
2571 if (dumpAll) {
2572 pw.println(':');
2573 token.dump(pw, " ");
2574 } else {
2575 pw.println();
2576 }
2577 }
2578 }
2579
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002580 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002581 final int[] index = new int[1];
2582 forAllWindows(w -> {
2583 final WindowStateAnimator wAnim = w.mWinAnimator;
2584 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2585 index[0] = index[0] + 1;
2586 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002587 }
2588
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002589 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002590 forAllWindows(w -> {
2591 w.mWinAnimator.enableSurfaceTrace(fd);
2592 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002593 }
2594
2595 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002596 forAllWindows(w -> {
2597 w.mWinAnimator.disableSurfaceTrace();
2598 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002599 }
2600
Jorim Jaggife762342016-10-13 14:33:27 +02002601 /**
2602 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2603 */
2604 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2605 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002606 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002607 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2608 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002609 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02002610 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002611 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002612 }
2613
Wale Ogunwale494009b82016-10-21 09:01:38 -07002614 boolean checkWaitingForWindows() {
2615
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002616 mHaveBootMsg = false;
2617 mHaveApp = false;
2618 mHaveWallpaper = false;
2619 mHaveKeyguard = true;
2620
2621 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002622 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2623 return true;
2624 }
2625 if (w.isDrawnLw()) {
2626 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002627 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002628 } else if (w.mAttrs.type == TYPE_APPLICATION
2629 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002630 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002631 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002632 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002633 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002634 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002635 }
2636 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002637 return false;
2638 });
2639
2640 if (visibleWindow != null) {
2641 // We have a visible window.
2642 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002643 }
2644
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002645 // if the wallpaper service is disabled on the device, we're never going to have
2646 // wallpaper, don't bother waiting for it
2647 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2648 com.android.internal.R.bool.config_enableWallpaperService)
2649 && !mService.mOnlyCore;
2650
Wale Ogunwale494009b82016-10-21 09:01:38 -07002651 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2652 "******** booted=" + mService.mSystemBooted
2653 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002654 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2655 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2656 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002657
2658 // If we are turning on the screen to show the boot message, don't do it until the boot
2659 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002660 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002661 return true;
2662 }
2663
2664 // If we are turning on the screen after the boot is completed normally, don't do so until
2665 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002666 if (mService.mSystemBooted
2667 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002668 return true;
2669 }
2670
2671 return false;
2672 }
2673
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002674 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002675 mTmpWindowAnimator = animator;
2676 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002677 }
2678
2679 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002680 resetAnimationBackgroundAnimator();
2681
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002682 // Used to indicate a detached wallpaper.
2683 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002684 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002685
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002686 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002687
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002688 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002689 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002690 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2691 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002692 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2693 }
2694 }
2695
Wale Ogunwale494009b82016-10-21 09:01:38 -07002696 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002697 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002698 if (imFocus == null) {
2699 return false;
2700 }
2701
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002702 if (DEBUG_INPUT_METHOD) {
2703 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2704 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2705 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002706 }
2707
Wale Ogunwale494009b82016-10-21 09:01:38 -07002708 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2709
2710 if (DEBUG_INPUT_METHOD) {
2711 Slog.i(TAG_WM, "IM target client: " + imeClient);
2712 if (imeClient != null) {
2713 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2714 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2715 }
2716 }
2717
2718 return imeClient != null && imeClient.asBinder() == client.asBinder();
2719 }
2720
2721 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002722 final WindowState win = getWindow(
2723 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2724 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002725 }
2726
2727 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002728 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002729 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002730 final int curValue = w.mSystemUiVisibility;
2731 final int diff = (curValue ^ visibility) & globalDiff;
2732 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002733 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002734 w.mSeq++;
2735 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002736 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002737 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2738 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002739 visibility, newValue, diff);
2740 }
2741 } catch (RemoteException e) {
2742 // so sorry
2743 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002744 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002745 }
2746
2747 void onWindowFreezeTimeout() {
2748 Slog.w(TAG_WM, "Window freeze timeout expired.");
2749 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002750
2751 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002752 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002753 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002754 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002755 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002756 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2757 - mService.mDisplayFreezeTime);
2758 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002759 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002760 mService.mWindowPlacerLocked.performSurfacePlacement();
2761 }
2762
2763 void waitForAllWindowsDrawn() {
2764 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002765 forAllWindows(w -> {
2766 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2767 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2768 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002769 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002770 w.mLastContentInsets.set(-1, -1, -1, -1);
2771 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002772 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002773 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002774 }
2775
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002776 // TODO: Super crazy long method that should be broken down...
2777 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2778
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002779 final int dw = mDisplayInfo.logicalWidth;
2780 final int dh = mDisplayInfo.logicalHeight;
2781 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2782
2783 mTmpUpdateAllDrawn.clear();
2784
2785 int repeats = 0;
2786 do {
2787 repeats++;
2788 if (repeats > 6) {
2789 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2790 clearLayoutNeeded();
2791 break;
2792 }
2793
2794 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2795 pendingLayoutChanges);
2796
Andrii Kulian839def92016-11-02 10:58:58 -07002797 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2798 // the wallpaper window jumping across displays.
2799 // Remove check for default display when there will be support for multiple wallpaper
2800 // targets (on different displays).
2801 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002802 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002803 }
2804
2805 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2806 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2807 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2808 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002809 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002810 }
2811 }
2812
2813 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2814 setLayoutNeeded();
2815 }
2816
2817 // FIRST LOOP: Perform a layout, if needed.
2818 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2819 performLayout(repeats == 1, false /* updateInputWindows */);
2820 } else {
2821 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2822 }
2823
2824 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2825 pendingLayoutChanges = 0;
2826
2827 if (isDefaultDisplay) {
2828 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002829 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002830 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2831 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2832 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2833 }
2834 } while (pendingLayoutChanges != 0);
2835
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002836 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002837
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002838 mTmpRecoveringMemory = recoveringMemory;
2839 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002840
2841 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002842 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2843 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2844 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002845 true /* inTraversal, must call performTraversalInTrans... below */);
2846
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002847 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2848 if (wallpaperVisible != mLastWallpaperVisible) {
2849 mLastWallpaperVisible = wallpaperVisible;
2850 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2851 }
2852
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002853 while (!mTmpUpdateAllDrawn.isEmpty()) {
2854 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2855 // See if any windows have been drawn, so they (and others associated with them)
2856 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002857 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002858 }
2859
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002860 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002861 }
2862
Bryce Leef3c6a472017-11-14 14:53:06 -08002863 private void updateBounds() {
2864 calculateBounds(mTmpBounds);
2865 setBounds(mTmpBounds);
2866 }
2867
2868 // Determines the current display bounds based on the current state
2869 private void calculateBounds(Rect out) {
2870 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
2871 final int orientation = mDisplayInfo.rotation;
2872 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2873 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
2874 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
2875 int width = mDisplayInfo.logicalWidth;
2876 int left = (physWidth - width) / 2;
2877 int height = mDisplayInfo.logicalHeight;
2878 int top = (physHeight - height) / 2;
2879 out.set(left, top, left + width, top + height);
2880 }
2881
2882 @Override
2883 public void getBounds(Rect out) {
2884 calculateBounds(out);
2885 }
2886
2887 private void getBounds(Rect out, int orientation) {
2888 getBounds(out);
2889
2890 // Rotate the Rect if needed.
2891 final int currentRotation = mDisplayInfo.rotation;
2892 final int rotationDelta = deltaRotation(currentRotation, orientation);
2893 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
2894 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
2895 mTmpRectF.set(out);
2896 mTmpMatrix.mapRect(mTmpRectF);
2897 mTmpRectF.round(out);
2898 }
2899 }
2900
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002901 void performLayout(boolean initial, boolean updateInputWindows) {
2902 if (!isLayoutNeeded()) {
2903 return;
2904 }
2905 clearLayoutNeeded();
2906
2907 final int dw = mDisplayInfo.logicalWidth;
2908 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002909 if (DEBUG_LAYOUT) {
2910 Slog.v(TAG, "-------------------------------------");
2911 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2912 }
2913
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002914 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
2915 // TODO: Not sure if we really need to set the rotation here since we are updating from the
2916 // display info above...
2917 mDisplayFrames.mRotation = mRotation;
2918 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002919 if (isDefaultDisplay) {
2920 // Not needed on non-default displays.
2921 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2922 mService.mScreenRect.set(0, 0, dw, dh);
2923 }
2924
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002925 int seq = mService.mLayoutSeq + 1;
2926 if (seq < 0) seq = 0;
2927 mService.mLayoutSeq = seq;
2928
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002929 // Used to indicate that we have processed the dream window and all additional windows are
2930 // behind it.
2931 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002932 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002933
2934 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002935 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002936
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002937 // Used to indicate that we have processed the dream window and all additional attached
2938 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002939 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002940 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002941
2942 // Now perform layout of attached windows, which usually depend on the position of the
2943 // window they are attached to. XXX does not deal with windows that are attached to windows
2944 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002945 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002946
2947 // Window frames may have changed. Tell the input dispatcher about it.
2948 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2949 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2950 if (updateInputWindows) {
2951 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2952 }
2953
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002954 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2955 }
2956
2957 /**
2958 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2959 * In portrait mode, it grabs the full screenshot.
2960 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002961 * @param config of the output bitmap
2962 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
2963 */
chaviwfbe47df2017-11-10 16:14:49 -08002964 Bitmap screenshotDisplay(Bitmap.Config config, boolean wallpaperOnly) {
2965 synchronized (mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02002966 if (!mService.mPolicy.isScreenOn()) {
chaviwfbe47df2017-11-10 16:14:49 -08002967 if (DEBUG_SCREENSHOT) {
2968 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002969 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002970 return null;
2971 }
2972
chaviwfbe47df2017-11-10 16:14:49 -08002973 if (wallpaperOnly && !shouldScreenshotWallpaper()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002974 return null;
2975 }
2976
chaviwfbe47df2017-11-10 16:14:49 -08002977 int dw = mDisplayInfo.logicalWidth;
2978 int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002979
chaviwfbe47df2017-11-10 16:14:49 -08002980 if (dw <= 0 || dh <= 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002981 return null;
2982 }
2983
chaviwfbe47df2017-11-10 16:14:49 -08002984 final Rect frame = new Rect(0, 0, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002985
2986 // The screenshot API does not apply the current screen rotation.
2987 int rot = mDisplay.getRotation();
2988
2989 if (rot == ROTATION_90 || rot == ROTATION_270) {
2990 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
2991 }
2992
chaviwfbe47df2017-11-10 16:14:49 -08002993 // SurfaceFlinger is not aware of orientation, so convert our logical
2994 // crop to SurfaceFlinger's portrait orientation.
2995 convertCropForSurfaceFlinger(frame, rot, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002996
2997 final ScreenRotationAnimation screenRotationAnimation =
2998 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
2999 final boolean inRotation = screenRotationAnimation != null &&
3000 screenRotationAnimation.isAnimating();
chaviwfbe47df2017-11-10 16:14:49 -08003001 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003002
Robert Carrb1579c82017-09-05 14:54:47 -07003003 // TODO(b/68392460): We should screenshot Task controls directly
3004 // but it's difficult at the moment as the Task doesn't have the
3005 // correct size set.
chaviwfbe47df2017-11-10 16:14:49 -08003006 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, 0, 1, inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003007 if (bitmap == null) {
Robert Carrb1579c82017-09-05 14:54:47 -07003008 Slog.w(TAG_WM, "Failed to take screenshot");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003009 return null;
3010 }
chaviwfbe47df2017-11-10 16:14:49 -08003011
3012 // Create a copy of the screenshot that is immutable and backed in ashmem.
3013 // This greatly reduces the overhead of passing the bitmap between processes.
3014 final Bitmap ret = bitmap.createAshmemBitmap(config);
3015 bitmap.recycle();
3016 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003017 }
chaviwfbe47df2017-11-10 16:14:49 -08003018 }
3019
3020 private boolean shouldScreenshotWallpaper() {
3021 MutableBoolean screenshotReady = new MutableBoolean(false);
3022
3023 forAllWindows(w -> {
3024 if (!w.mIsWallpaper) {
3025 return false;
3026 }
3027
3028 // Found the wallpaper window
3029 final WindowStateAnimator winAnim = w.mWinAnimator;
3030
3031 if (winAnim.getShown() && winAnim.mLastAlpha > 0f) {
3032 screenshotReady.value = true;
3033 }
3034
3035 return true;
3036 }, true /* traverseTopToBottom */);
3037
3038 return screenshotReady.value;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003039 }
3040
3041 // TODO: Can this use createRotationMatrix()?
3042 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3043 if (rot == Surface.ROTATION_90) {
3044 final int tmp = crop.top;
3045 crop.top = dw - crop.right;
3046 crop.right = crop.bottom;
3047 crop.bottom = dw - crop.left;
3048 crop.left = tmp;
3049 } else if (rot == Surface.ROTATION_180) {
3050 int tmp = crop.top;
3051 crop.top = dh - crop.bottom;
3052 crop.bottom = dh - tmp;
3053 tmp = crop.right;
3054 crop.right = dw - crop.left;
3055 crop.left = dw - tmp;
3056 } else if (rot == Surface.ROTATION_270) {
3057 final int tmp = crop.top;
3058 crop.top = crop.left;
3059 crop.left = dh - crop.bottom;
3060 crop.bottom = crop.right;
3061 crop.right = dh - tmp;
3062 }
3063 }
3064
3065 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003066 // Used to indicate the layout is needed.
3067 mTmpWindow = null;
3068
3069 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003070 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003071 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003072 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003073 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003074 w.setDisplayLayoutNeeded();
3075 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003076 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003077
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003078 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003079 mService.mWindowPlacerLocked.performSurfacePlacement();
3080 }
3081 }
3082
Wale Ogunwale1666e312016-12-16 11:27:18 -08003083 void setExitingTokensHasVisible(boolean hasVisible) {
3084 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3085 mExitingTokens.get(i).hasVisible = hasVisible;
3086 }
3087
3088 // Initialize state of exiting applications.
3089 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3090 }
3091
3092 void removeExistingTokensIfPossible() {
3093 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3094 final WindowToken token = mExitingTokens.get(i);
3095 if (!token.hasVisible) {
3096 mExitingTokens.remove(i);
3097 }
3098 }
3099
3100 // Time to remove any exiting applications?
3101 mTaskStackContainers.removeExistingAppTokensIfPossible();
3102 }
3103
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003104 @Override
3105 void onDescendantOverrideConfigurationChanged() {
3106 setLayoutNeeded();
3107 mService.requestTraversal();
3108 }
3109
David Stevens9440dc82017-03-16 19:00:20 -07003110 boolean okToDisplay() {
3111 if (mDisplayId == DEFAULT_DISPLAY) {
3112 return !mService.mDisplayFrozen
3113 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3114 }
3115 return mDisplayInfo.state == Display.STATE_ON;
3116 }
3117
3118 boolean okToAnimate() {
3119 return okToDisplay() &&
3120 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3121 }
3122
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003123 static final class TaskForResizePointSearchResult {
3124 boolean searchDone;
3125 Task taskForResize;
3126
3127 void reset() {
3128 searchDone = false;
3129 taskForResize = null;
3130 }
3131 }
Robert Carr3b716242016-08-16 16:02:21 -07003132
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003133 private static final class ApplySurfaceChangesTransactionState {
3134 boolean displayHasContent;
3135 boolean obscured;
3136 boolean syswin;
3137 boolean focusDisplayed;
3138 float preferredRefreshRate;
3139 int preferredModeId;
3140
3141 void reset() {
3142 displayHasContent = false;
3143 obscured = false;
3144 syswin = false;
3145 focusDisplayed = false;
3146 preferredRefreshRate = 0;
3147 preferredModeId = 0;
3148 }
3149 }
3150
3151 private static final class ScreenshotApplicationState {
3152 WindowState appWin;
3153 int maxLayer;
3154 int minLayer;
3155 boolean screenshotReady;
3156
3157 void reset(boolean screenshotReady) {
3158 appWin = null;
3159 maxLayer = 0;
3160 minLayer = 0;
3161 this.screenshotReady = screenshotReady;
3162 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3163 }
3164 }
3165
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003166 /**
3167 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3168 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3169 * homogeneous children type which is currently required by sub-classes of
3170 * {@link WindowContainer} class.
3171 */
3172 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3173
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003174 DisplayChildWindowContainer(WindowManagerService service) {
3175 super(service);
3176 }
3177
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003178 @Override
3179 boolean fillsParent() {
3180 return true;
3181 }
3182
3183 @Override
3184 boolean isVisible() {
3185 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003186 }
3187 }
3188
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003189 /**
3190 * Window container class that contains all containers on this display relating to Apps.
3191 * I.e Activities.
3192 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003193 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003194 /**
3195 * A control placed at the appropriate level for transitions to occur.
3196 */
3197 SurfaceControl mAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003198
Wale Ogunwale61911492017-10-11 08:50:50 -07003199 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3200 // through the list to find them.
3201 private TaskStack mHomeStack = null;
3202 private TaskStack mPinnedStack = null;
3203 private TaskStack mSplitScreenPrimaryStack = null;
3204
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003205 TaskStackContainers(WindowManagerService service) {
3206 super(service);
3207 }
3208
Wale Ogunwale61911492017-10-11 08:50:50 -07003209 /**
3210 * Returns the topmost stack on the display that is compatible with the input windowing mode
3211 * and activity type. Null is no compatible stack on the display.
3212 */
3213 TaskStack getStack(int windowingMode, int activityType) {
3214 if (activityType == ACTIVITY_TYPE_HOME) {
3215 return mHomeStack;
3216 }
3217 if (windowingMode == WINDOWING_MODE_PINNED) {
3218 return mPinnedStack;
3219 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3220 return mSplitScreenPrimaryStack;
3221 }
3222 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3223 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003224 if (activityType == ACTIVITY_TYPE_UNDEFINED
3225 && windowingMode == stack.getWindowingMode()) {
3226 // Passing in undefined type means we want to match the topmost stack with the
3227 // windowing mode.
3228 return stack;
3229 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003230 if (stack.isCompatible(windowingMode, activityType)) {
3231 return stack;
3232 }
3233 }
3234 return null;
3235 }
3236
3237 @VisibleForTesting
3238 TaskStack getTopStack() {
3239 return mTaskStackContainers.getChildCount() > 0
3240 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3241 }
3242
3243 TaskStack getHomeStack() {
3244 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3245 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3246 }
3247 return mHomeStack;
3248 }
3249
3250 TaskStack getPinnedStack() {
3251 return mPinnedStack;
3252 }
3253
Matthew Ng64e77cf2017-10-31 14:01:31 -07003254 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003255 return mSplitScreenPrimaryStack;
3256 }
3257
Andrii Kulian839def92016-11-02 10:58:58 -07003258 /**
3259 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003260 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003261 */
3262 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003263 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003264 addChild(stack, onTop);
3265 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003266 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003267
Wale Ogunwale61911492017-10-11 08:50:50 -07003268 void onStackWindowingModeChanged(TaskStack stack) {
3269 removeStackReferenceIfNeeded(stack);
3270 addStackReferenceIfNeeded(stack);
3271 if (stack == mPinnedStack && getTopStack() != stack) {
3272 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3273 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3274 }
3275 }
3276
3277 private void addStackReferenceIfNeeded(TaskStack stack) {
3278 if (stack.isActivityTypeHome()) {
3279 if (mHomeStack != null) {
3280 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
3281 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
3282 }
3283 mHomeStack = stack;
3284 }
3285 final int windowingMode = stack.getWindowingMode();
3286 if (windowingMode == WINDOWING_MODE_PINNED) {
3287 if (mPinnedStack != null) {
3288 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3289 + mPinnedStack + " already exist on display=" + this
3290 + " stack=" + stack);
3291 }
3292 mPinnedStack = stack;
3293 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3294 if (mSplitScreenPrimaryStack != null) {
3295 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3296 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3297 + " already exist on display=" + this + " stack=" + stack);
3298 }
3299 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003300 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003301 }
3302 }
3303
3304 private void removeStackReferenceIfNeeded(TaskStack stack) {
3305 if (stack == mHomeStack) {
3306 mHomeStack = null;
3307 } else if (stack == mPinnedStack) {
3308 mPinnedStack = null;
3309 } else if (stack == mSplitScreenPrimaryStack) {
3310 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003311 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3312 mService.setDockedStackCreateStateLocked(
3313 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3314 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003315 }
Andrii Kulian839def92016-11-02 10:58:58 -07003316 }
3317
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003318 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003319 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3320 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003321 addChild(stack, addIndex);
3322 setLayoutNeeded();
3323 }
3324
Wale Ogunwale61911492017-10-11 08:50:50 -07003325 @Override
3326 protected void removeChild(TaskStack stack) {
3327 super.removeChild(stack);
3328 removeStackReferenceIfNeeded(stack);
3329 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003330
3331 @Override
3332 boolean isOnTop() {
3333 // Considered always on top
3334 return true;
3335 }
3336
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003337 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003338 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003339 if (child.getWindowConfiguration().isAlwaysOnTop()
3340 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003341 // This stack is always-on-top, override the default behavior.
3342 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3343
3344 // Moving to its current position, as we must call super but we don't want to
3345 // perform any meaningful action.
3346 final int currentPosition = mChildren.indexOf(child);
3347 super.positionChildAt(currentPosition, child, false /* includingParents */);
3348 return;
3349 }
3350
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003351 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3352 super.positionChildAt(targetPosition, child, includingParents);
3353
3354 setLayoutNeeded();
3355 }
3356
3357 /**
3358 * When stack is added or repositioned, find a proper position for it.
3359 * This will make sure that pinned stack always stays on top.
3360 * @param requestedPosition Position requested by caller.
3361 * @param stack Stack to be added or positioned.
3362 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3363 * @return The proper position for the stack.
3364 */
3365 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3366 final int topChildPosition = mChildren.size() - 1;
3367 boolean toTop = requestedPosition == POSITION_TOP;
3368 toTop |= adding ? requestedPosition >= topChildPosition + 1
3369 : requestedPosition >= topChildPosition;
3370 int targetPosition = requestedPosition;
3371
Wale Ogunwale61911492017-10-11 08:50:50 -07003372 if (toTop && stack.getWindowingMode() != WINDOWING_MODE_PINNED && hasPinnedStack()) {
Bryce Lee48f4b572017-04-10 10:54:15 -07003373 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003374 TaskStack topStack = mChildren.get(topChildPosition);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003375 if (topStack.getWindowingMode() != WINDOWING_MODE_PINNED) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003376 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3377 }
3378
3379 // So, stack is moved just below the pinned stack.
3380 // When we're adding a new stack the target is the current pinned stack position.
3381 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003382 // stack, because WindowContainer#positionAt() first removes element and then adds
3383 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003384 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3385 }
3386
3387 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003388 }
3389
3390 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003391 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3392 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003393 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003394 if (super.forAllWindows(callback, traverseTopToBottom)) {
3395 return true;
3396 }
3397 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3398 return true;
3399 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003400 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003401 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3402 return true;
3403 }
3404 if (super.forAllWindows(callback, traverseTopToBottom)) {
3405 return true;
3406 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003407 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003408 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003409 }
3410
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003411 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003412 boolean traverseTopToBottom) {
3413 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3414 // app tokens.
3415 // TODO: Investigate if we need to continue to do this or if we can just process them
3416 // in-order.
3417 if (traverseTopToBottom) {
3418 for (int i = mChildren.size() - 1; i >= 0; --i) {
3419 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3420 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003421 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3422 traverseTopToBottom)) {
3423 return true;
3424 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003425 }
3426 }
3427 } else {
3428 final int count = mChildren.size();
3429 for (int i = 0; i < count; ++i) {
3430 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3431 final int appTokensCount = appTokens.size();
3432 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003433 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3434 traverseTopToBottom)) {
3435 return true;
3436 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003437 }
3438 }
3439 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003440 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003441 }
3442
Wale Ogunwale1666e312016-12-16 11:27:18 -08003443 void setExitingTokensHasVisible(boolean hasVisible) {
3444 for (int i = mChildren.size() - 1; i >= 0; --i) {
3445 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3446 for (int j = appTokens.size() - 1; j >= 0; --j) {
3447 appTokens.get(j).hasVisible = hasVisible;
3448 }
3449 }
3450 }
3451
3452 void removeExistingAppTokensIfPossible() {
3453 for (int i = mChildren.size() - 1; i >= 0; --i) {
3454 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3455 for (int j = appTokens.size() - 1; j >= 0; --j) {
3456 final AppWindowToken token = appTokens.get(j);
3457 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3458 && (!token.mIsExiting || token.isEmpty())) {
3459 // Make sure there is no animation running on this token, so any windows
3460 // associated with it will be removed as soon as their animations are
3461 // complete.
3462 token.mAppAnimator.clearAnimation();
3463 token.mAppAnimator.animating = false;
3464 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3465 "performLayout: App token exiting now removed" + token);
3466 token.removeIfPossible();
3467 }
3468 }
3469 }
3470 }
3471
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003472 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003473 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003474 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3475 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003476 // Apps and their containers are not allowed to specify an orientation while the
3477 // docked or freeform stack is visible...except for the home stack/task if the
3478 // docked stack is minimized and it actually set something.
3479 if (mHomeStack != null && mHomeStack.isVisible()
3480 && mDividerControllerLocked.isMinimizedDock()) {
3481 final int orientation = mHomeStack.getOrientation();
3482 if (orientation != SCREEN_ORIENTATION_UNSET) {
3483 return orientation;
3484 }
3485 }
3486 return SCREEN_ORIENTATION_UNSPECIFIED;
3487 }
3488
3489 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003490 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3491 PackageManager.FEATURE_AUTOMOTIVE);
3492 if (isCar) {
3493 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3494 // allow anything but the default orientation.
3495 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3496 "Forcing UNSPECIFIED orientation in car. Ignoring " + orientation);
3497 return SCREEN_ORIENTATION_UNSPECIFIED;
3498 }
3499
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003500 if (orientation != SCREEN_ORIENTATION_UNSET
3501 && orientation != SCREEN_ORIENTATION_BEHIND) {
3502 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3503 "App is requesting an orientation, return " + orientation);
3504 return orientation;
3505 }
3506
3507 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003508 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003509 // The next app has not been requested to be visible, so we keep the current orientation
3510 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003511 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003512 }
Robert Carrb1579c82017-09-05 14:54:47 -07003513
3514 @Override
3515 void assignChildLayers(SurfaceControl.Transaction t) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003516 int layer = 0;
Robert Carrb1579c82017-09-05 14:54:47 -07003517
3518 // We allow stacks to change visual order from the AM specified order due to
3519 // Z-boosting during animations. However we must take care to ensure TaskStacks
3520 // which are marked as alwaysOnTop remain that way.
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003521 for (int i = 0; i < mChildren.size(); i++) {
3522 final TaskStack s = mChildren.get(i);
3523 s.assignChildLayers();
3524 if (!s.needsZBoost() && !s.isAlwaysOnTop()) {
3525 s.assignLayer(t, layer++);
Robert Carrb1579c82017-09-05 14:54:47 -07003526 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003527 }
3528 for (int i = 0; i < mChildren.size(); i++) {
3529 final TaskStack s = mChildren.get(i);
3530 if (s.needsZBoost() && !s.isAlwaysOnTop()) {
3531 s.assignLayer(t, layer++);
Robert Carrb1579c82017-09-05 14:54:47 -07003532 }
3533 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003534 for (int i = 0; i < mChildren.size(); i++) {
3535 final TaskStack s = mChildren.get(i);
3536 if (s.isAlwaysOnTop()) {
3537 s.assignLayer(t, layer++);
3538 }
3539 }
3540
3541 // The appropriate place for App-Transitions to occur is right
3542 // above all other animations but still below things in the Picture-and-Picture
3543 // windowing mode.
3544 if (mAnimationLayer != null) {
3545 t.setLayer(mAnimationLayer, layer++);
3546 }
Robert Carrb1579c82017-09-05 14:54:47 -07003547 }
3548
3549 @Override
3550 void onParentSet() {
3551 super.onParentSet();
3552 if (getParent() != null) {
3553 mAnimationLayer = makeSurface().build();
3554 } else {
3555 mAnimationLayer.destroy();
3556 mAnimationLayer = null;
3557 }
3558 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003559 }
3560
Robert Carree4d4b92017-11-22 12:21:46 -08003561 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003562 AboveAppWindowContainers(String name, WindowManagerService service) {
3563 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08003564 }
3565
3566 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
3567 boolean needAssignIme = imeContainer != null
3568 && imeContainer.getSurfaceControl() != null;
3569 for (int j = 0; j < mChildren.size(); ++j) {
3570 final WindowToken wt = mChildren.get(j);
3571 wt.assignLayer(t, j);
3572 wt.assignChildLayers(t);
3573
3574 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
3575 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08003576
3577 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
3578 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003579 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08003580 needAssignIme = false;
3581 }
3582 }
3583 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003584 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08003585 }
3586 }
3587 }
3588
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003589 /**
3590 * Window container class that contains all containers on this display that are not related to
3591 * Apps. E.g. status bar.
3592 */
Robert Carree4d4b92017-11-22 12:21:46 -08003593 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07003594 /**
3595 * Compares two child window tokens returns -1 if the first is lesser than the second in
3596 * terms of z-order and 1 otherwise.
3597 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003598 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003599 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003600 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3601 token1.mOwnerCanManageAppTokens)
3602 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3603 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003604
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003605 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3606 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3607 return false;
3608 }
3609 final int req = w.mAttrs.screenOrientation;
3610 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3611 || req == SCREEN_ORIENTATION_UNSET) {
3612 return false;
3613 }
3614 return true;
3615 };
3616
Wale Ogunwale3a931692016-11-02 16:49:48 -07003617 private final String mName;
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003618 NonAppWindowContainers(String name, WindowManagerService service) {
3619 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003620 mName = name;
3621 }
3622
3623 void addChild(WindowToken token) {
3624 addChild(token, mWindowComparator);
3625 }
3626
3627 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003628 int getOrientation() {
3629 final WindowManagerPolicy policy = mService.mPolicy;
3630 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003631 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003632
3633 if (win != null) {
3634 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003635 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003636 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003637 if (mService.mKeyguardGoingAway) {
3638 // Keyguard can't affect the orientation if it is going away...
3639 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3640 return SCREEN_ORIENTATION_UNSET;
3641 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003642 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003643 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003644 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003645 }
3646
Andrii Kulian8ee72852017-03-10 10:36:45 -08003647 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003648
Jorim Jaggi75520d52017-08-24 17:23:19 +02003649 if (policy.isKeyguardShowingAndNotOccluded()
3650 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003651 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003652 }
3653
3654 return SCREEN_ORIENTATION_UNSET;
3655 }
3656
3657 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003658 String getName() {
3659 return mName;
3660 }
Robert Carr3b716242016-08-16 16:02:21 -07003661 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003662
Robert Carrb1579c82017-09-05 14:54:47 -07003663 SurfaceControl.Builder makeSurface(SurfaceSession s) {
3664 return mService.makeSurfaceBuilder(s)
3665 .setParent(mWindowingLayer);
3666 }
3667
3668 @Override
3669 SurfaceSession getSession() {
3670 return mSession;
3671 }
3672
3673 @Override
3674 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003675 SurfaceSession s = child != null ? child.getSession() : getSession();
3676 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07003677 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07003678
3679 if (child == null) {
3680 return b;
3681 }
3682
Robert Carree4d4b92017-11-22 12:21:46 -08003683 return b.setName(child.getName())
3684 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07003685 }
3686
3687 /**
3688 * The makeSurface variants are for use by the window-container
3689 * hierarchy. makeOverlay here is a function for various non windowing
3690 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
3691 * and other potpourii.
3692 */
3693 SurfaceControl.Builder makeOverlay() {
3694 return mService.makeSurfaceBuilder(mSession)
3695 .setParent(mOverlayLayer);
3696 }
3697
3698 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003699 applyMagnificationSpec(getPendingTransaction(), spec);
3700 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07003701 }
3702
3703 @Override
3704 void onParentSet() {
3705 // Since we are the top of the SurfaceControl hierarchy here
3706 // we create the root surfaces explicitly rather than chaining
3707 // up as the default implementation in onParentSet does. So we
3708 // explicitly do NOT call super here.
3709 }
3710
3711 @Override
3712 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07003713
3714 // These are layers as children of "mWindowingLayer"
3715 mBelowAppWindowsContainers.assignLayer(t, 0);
3716 mTaskStackContainers.assignLayer(t, 1);
3717 mAboveAppWindowsContainers.assignLayer(t, 2);
3718
3719 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08003720 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08003721
Robert Carree4d4b92017-11-22 12:21:46 -08003722 // In the case where we have an IME target that is not in split-screen
3723 // mode IME assignment is easy. We just need the IME to go directly above
3724 // the target. This way children of the target will naturally go above the IME
3725 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08003726 //
Robert Carree4d4b92017-11-22 12:21:46 -08003727 // In the case of split-screen windowing mode, we need to elevate the IME above the
3728 // docked divider while keeping the app itself below the docked divider, so instead
3729 // we use relative layering of the IME targets child windows, and place the
3730 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08003731 //
Robert Carree4d4b92017-11-22 12:21:46 -08003732 // In the case where we have no IME target we assign it where it's base layer would
3733 // place it in the AboveAppWindowContainers.
3734 if (imeTarget != null && !imeTarget.inSplitScreenWindowingMode()
3735 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003736 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07003737 // TODO: We need to use an extra level on the app surface to ensure
3738 // this is always above SurfaceView but always below attached window.
3739 1);
Robert Carree4d4b92017-11-22 12:21:46 -08003740 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07003741 }
3742
3743 // Above we have assigned layers to our children, now we ask them to assign
3744 // layers to their children.
3745 mBelowAppWindowsContainers.assignChildLayers(t);
3746 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08003747 mAboveAppWindowsContainers.assignChildLayers(t,
3748 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07003749 mImeWindowsContainers.assignChildLayers(t);
3750 }
3751
3752 /**
3753 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
3754 * that the IME target is one of the docked applications. We'd like the docked divider to be
3755 * above both of the applications, and we'd like the IME to be above the docked divider.
3756 * However we need child windows of the applications to be above the IME (Text drag handles).
3757 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
3758 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
3759 * with {@link #WindowState#assignLayer}
3760 */
3761 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
3762 t.setRelativeLayer(child.getSurfaceControl(), mImeWindowsContainers.getSurfaceControl(), 1);
3763 }
3764
3765 @Override
Jorim Jaggia5e10572017-11-15 14:36:26 +01003766 public void destroyAfterPendingTransaction(SurfaceControl surface) {
Robert Carrb1579c82017-09-05 14:54:47 -07003767 mPendingDestroyingSurfaces.add(surface);
3768 }
3769
3770 /**
3771 * Destroys any surfaces that have been put into the pending list with
Jorim Jaggia5e10572017-11-15 14:36:26 +01003772 * {@link #destroyAfterPendingTransaction}.
Robert Carrb1579c82017-09-05 14:54:47 -07003773 */
3774 void onPendingTransactionApplied() {
3775 for (int i = mPendingDestroyingSurfaces.size() - 1; i >= 0; i--) {
3776 mPendingDestroyingSurfaces.get(i).destroy();
3777 }
3778 mPendingDestroyingSurfaces.clear();
3779 }
Craig Mautner59c00972012-07-30 12:10:24 -07003780}