blob: 0b65b2287bdd6e499291b9c9d3b41fea2b4a2c8d [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 Ogunwale3797c222015-10-27 14:21:58 -070019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
Wale Ogunwale51362492016-09-08 17:49:17 -070020import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
Wale Ogunwale01bb9d42017-10-04 12:54:23 -070021import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070022import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
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;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070061import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070062import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
63import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070064import static android.view.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;
108import static com.android.server.wm.proto.DisplayProto.DISPLAY_INFO;
109import static com.android.server.wm.proto.DisplayProto.DOCKED_STACK_DIVIDER_CONTROLLER;
110import static com.android.server.wm.proto.DisplayProto.DPI;
111import static com.android.server.wm.proto.DisplayProto.ID;
112import static com.android.server.wm.proto.DisplayProto.IME_WINDOWS;
113import static com.android.server.wm.proto.DisplayProto.PINNED_STACK_CONTROLLER;
Steven Timotiusf2d68892017-08-28 17:00:01 -0700114import static com.android.server.wm.proto.DisplayProto.ROTATION;
115import static com.android.server.wm.proto.DisplayProto.SCREEN_ROTATION_ANIMATION;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700116import static com.android.server.wm.proto.DisplayProto.STACKS;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700117import static com.android.server.wm.proto.DisplayProto.WINDOW_CONTAINER;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700118
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700119import android.annotation.CallSuper;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700120import android.annotation.NonNull;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700121import android.app.ActivityManager.StackId;
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;
Chong Zhang8e89b312015-09-09 15:09:30 -0700138import android.util.DisplayMetrics;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800139import android.util.MutableBoolean;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700140import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700141import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700142import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700143import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700144import android.view.InputDevice;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700145import android.view.Surface;
146import android.view.SurfaceControl;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700147import android.view.WindowManagerPolicy;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700148
Bryce Lee48f4b572017-04-10 10:54:15 -0700149import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800150import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700151import com.android.internal.view.IInputMethodClient;
Craig Mautner59c00972012-07-30 12:10:24 -0700152
Robert Carr3b716242016-08-16 16:02:21 -0700153import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700154import java.io.PrintWriter;
155import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700156import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700157import java.util.HashMap;
158import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700159import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700160import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800161import java.util.function.Consumer;
162import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700163
Craig Mautner59c00972012-07-30 12:10:24 -0700164/**
165 * Utility class for keeping track of the WindowStates and other pertinent contents of a
166 * particular Display.
167 *
168 * IMPORTANT: No method from this class should ever be used without holding
169 * WindowManagerService.mWindowMap.
170 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700171class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700172 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700173
174 /** Unique identifier of this stack. */
175 private final int mDisplayId;
176
Wale Ogunwale3a931692016-11-02 16:49:48 -0700177 /** The containers below are the only child containers the display can have. */
178 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700179 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700180 // Contains all non-app window containers that should be displayed above the app containers
181 // (e.g. Status bar)
182 private final NonAppWindowContainers mAboveAppWindowsContainers =
183 new NonAppWindowContainers("mAboveAppWindowsContainers");
184 // Contains all non-app window containers that should be displayed below the app containers
185 // (e.g. Wallpaper).
186 private final NonAppWindowContainers mBelowAppWindowsContainers =
187 new NonAppWindowContainers("mBelowAppWindowsContainers");
188 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
189 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
190 // window containers together and move them in-sync if/when needed.
191 private final NonAppWindowContainers mImeWindowsContainers =
192 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700193
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000194 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800195 private WindowState mTmpWindow2;
196 private WindowAnimator mTmpWindowAnimator;
197 private boolean mTmpRecoveringMemory;
198 private boolean mUpdateImeTarget;
199 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700200 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700201
Wale Ogunwale02319a62016-09-26 15:21:22 -0700202 // Mapping from a token IBinder to a WindowToken object on this display.
203 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
204
Andrii Kuliancd097992017-03-23 18:31:59 -0700205 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700206 int mInitialDisplayWidth = 0;
207 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700208 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700209
210 /**
211 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
212 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
213 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
214 */
Craig Mautner59c00972012-07-30 12:10:24 -0700215 int mBaseDisplayWidth = 0;
216 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700217 /**
218 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
219 * but can be set from Settings or via shell command "adb shell wm density".
220 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
221 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700222 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700223 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700224 private final DisplayInfo mDisplayInfo = new DisplayInfo();
225 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700226 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Andrii Kulian06d07d62017-03-14 11:11:47 -0700227 /**
228 * For default display it contains real metrics, empty for others.
229 * @see WindowManagerService#createWatermarkInTransaction()
230 */
231 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
232 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
233 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700234
Andrii Kulian06d07d62017-03-14 11:11:47 -0700235 /**
236 * Compat metrics computed based on {@link #mDisplayMetrics}.
237 * @see #updateDisplayAndOrientation(int)
238 */
239 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
240
241 /** The desired scaling factor for compatible apps. */
242 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700243
Andrii Kulian8ee72852017-03-10 10:36:45 -0800244 /**
245 * Current rotation of the display.
246 * Constants as per {@link android.view.Surface.Rotation}.
247 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700248 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800249 */
250 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700251
Andrii Kulian8ee72852017-03-10 10:36:45 -0800252 /**
253 * Last applied orientation of the display.
254 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
255 *
256 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
257 */
258 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700259
Andrii Kulian8ee72852017-03-10 10:36:45 -0800260 /**
261 * Flag indicating that the application is receiving an orientation that has different metrics
262 * than it expected. E.g. Portrait instead of Landscape.
263 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700264 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800265 */
266 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700267
Andrii Kulian8ee72852017-03-10 10:36:45 -0800268 /**
269 * Orientation forced by some window. If there is no visible window that specifies orientation
270 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
271 *
272 * @see NonAppWindowContainers#getOrientation()
273 */
274 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700275
Andrii Kulian8ee72852017-03-10 10:36:45 -0800276 /**
277 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
278 * occluded.
279 *
280 * @see NonAppWindowContainers#getOrientation()
281 */
282 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
283
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700284 /**
285 * Keep track of wallpaper visibility to notify changes.
286 */
287 private boolean mLastWallpaperVisible = false;
288
Andrii Kulian06d07d62017-03-14 11:11:47 -0700289 private Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700290 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700291
Craig Mautner39834192012-09-02 07:47:24 -0700292 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700293 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700294 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700295 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800296 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700297
Craig Mautnerdc548482014-02-05 13:35:24 -0800298 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700299 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800300
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800301 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
302 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700303 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700304
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700305 /** Detect user tapping outside of current focused task bounds .*/
306 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700307
Craig Mautner6601b7b2013-04-29 10:29:11 -0700308 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700309 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700310
Craig Mautner6601b7b2013-04-29 10:29:11 -0700311 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800312 private final Rect mTmpRect = new Rect();
313 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700314 private final RectF mTmpRectF = new RectF();
315 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800316 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700317
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800318 WindowManagerService mService;
Craig Mautner9d808b12013-08-06 18:00:25 -0700319
Craig Mautner95da1082014-02-24 17:54:35 -0800320 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700321 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800322
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700323 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700324 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700325
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800326 DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -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();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700343
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700344 // True if this display is in the process of being removed. Used to determine if the removal of
345 // the display's direct children should be allowed.
346 private boolean mRemovingDisplay = false;
347
Bryce Leed1871262017-06-12 14:12:29 -0700348 // {@code false} if this display is in the processing of being created.
349 private boolean mDisplayReady = false;
350
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700351 private final WindowLayersController mLayersController;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800352 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700353 int mInputMethodAnimLayerAdjustment;
354
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800355 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
356 WindowStateAnimator winAnimator = w.mWinAnimator;
357 if (winAnimator.hasSurface()) {
358 final boolean wasAnimating = winAnimator.mWasAnimating;
359 final boolean nowAnimating = winAnimator.stepAnimationLocked(
360 mTmpWindowAnimator.mCurrentTime);
361 winAnimator.mWasAnimating = nowAnimating;
362 mTmpWindowAnimator.orAnimating(nowAnimating);
363
364 if (DEBUG_WALLPAPER) Slog.v(TAG,
365 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
366
367 if (wasAnimating && !winAnimator.mAnimating
368 && mWallpaperController.isWallpaperTarget(w)) {
369 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
370 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
371 if (DEBUG_LAYOUT_REPEATS) {
372 mService.mWindowPlacerLocked.debugLayoutRepeats(
373 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
374 }
375 }
376 }
377
378 final AppWindowToken atoken = w.mAppToken;
379 if (winAnimator.mDrawState == READY_TO_SHOW) {
380 if (atoken == null || atoken.allDrawn) {
381 if (w.performShowLocked()) {
382 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
383 if (DEBUG_LAYOUT_REPEATS) {
384 mService.mWindowPlacerLocked.debugLayoutRepeats(
385 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
386 }
387 }
388 }
389 }
390 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
391 if (appAnimator != null && appAnimator.thumbnail != null) {
392 if (appAnimator.thumbnailTransactionSeq
393 != mTmpWindowAnimator.mAnimTransactionSequence) {
394 appAnimator.thumbnailTransactionSeq =
395 mTmpWindowAnimator.mAnimTransactionSequence;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800396 }
397 }
398 };
399
400 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
401 final WindowStateAnimator winAnimator = w.mWinAnimator;
402 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
403 return;
404 }
405
406 final int flags = w.mAttrs.flags;
407
408 // If this window is animating, make a note that we have an animating window and take
409 // care of a request to run a detached wallpaper animation.
410 if (winAnimator.mAnimating) {
411 if (winAnimator.mAnimation != null) {
412 if ((flags & FLAG_SHOW_WALLPAPER) != 0
413 && winAnimator.mAnimation.getDetachWallpaper()) {
414 mTmpWindow = w;
415 }
416 final int color = winAnimator.mAnimation.getBackgroundColor();
417 if (color != 0) {
418 final TaskStack stack = w.getStack();
419 if (stack != null) {
420 stack.setAnimationBackground(winAnimator, color);
421 }
422 }
423 }
424 mTmpWindowAnimator.setAnimating(true);
425 }
426
427 // If this window's app token is running a detached wallpaper animation, make a note so
428 // we can ensure the wallpaper is displayed behind it.
429 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
430 if (appAnimator != null && appAnimator.animation != null
431 && appAnimator.animating) {
432 if ((flags & FLAG_SHOW_WALLPAPER) != 0
433 && appAnimator.animation.getDetachWallpaper()) {
434 mTmpWindow = w;
435 }
436
437 final int color = appAnimator.animation.getBackgroundColor();
438 if (color != 0) {
439 final TaskStack stack = w.getStack();
440 if (stack != null) {
441 stack.setAnimationBackground(winAnimator, color);
442 }
443 }
444 }
445 };
446
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800447 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
448 final int lostFocusUid = mTmpWindow.mOwnerUid;
449 final Handler handler = mService.mH;
450 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
451 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
452 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
453 w.mAttrs.hideTimeoutMilliseconds);
454 }
455 }
456 };
457
458 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
459 final AppWindowToken focusedApp = mService.mFocusedApp;
460 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
461 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
462
463 if (!w.canReceiveKeys()) {
464 return false;
465 }
466
467 final AppWindowToken wtoken = w.mAppToken;
468
469 // If this window's application has been removed, just skip it.
470 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
471 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
472 + (wtoken.removed ? "removed" : "sendingToBottom"));
473 return false;
474 }
475
476 if (focusedApp == null) {
477 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
478 + " using new focus @ " + w);
479 mTmpWindow = w;
480 return true;
481 }
482
483 if (!focusedApp.windowsAreFocusable()) {
484 // Current focused app windows aren't focusable...
485 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
486 + " focusable using new focus @ " + w);
487 mTmpWindow = w;
488 return true;
489 }
490
491 // Descend through all of the app tokens and find the first that either matches
492 // win.mAppToken (return win) or mFocusedApp (return null).
493 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
494 if (focusedApp.compareTo(wtoken) > 0) {
495 // App stack below focused app stack. No focus for you!!!
496 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
497 "findFocusedWindow: Reached focused app=" + focusedApp);
498 mTmpWindow = null;
499 return true;
500 }
501 }
502
503 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
504 mTmpWindow = w;
505 return true;
506 };
507
508 private final Consumer<WindowState> mPrepareWindowSurfaces =
509 w -> w.mWinAnimator.prepareSurfaceLocked(true);
510
511 private final Consumer<WindowState> mPerformLayout = w -> {
512 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
513 // wasting time and funky changes while a window is animating away.
514 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
515 || w.isGoneForLayoutLw();
516
517 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
518 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
519 + " mLayoutAttached=" + w.mLayoutAttached
520 + " screen changed=" + w.isConfigChanged());
521 final AppWindowToken atoken = w.mAppToken;
522 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
523 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
524 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
525 + " parentHidden=" + w.isParentWindowHidden());
526 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
527 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
528 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
529 + " parentHidden=" + w.isParentWindowHidden());
530 }
531
532 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
533 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
534 // since that means "perform layout as normal, just don't display").
535 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
536 || ((w.isConfigChanged() || w.setReportResizeHints())
537 && !w.isGoneForLayoutLw() &&
538 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
539 (w.mHasSurface && w.mAppToken != null &&
540 w.mAppToken.layoutConfigChanges)))) {
541 if (!w.mLayoutAttached) {
542 if (mTmpInitial) {
543 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
544 w.mContentChanged = false;
545 }
546 if (w.mAttrs.type == TYPE_DREAM) {
547 // Don't layout windows behind a dream, so that if it does stuff like hide
548 // the status bar we won't get a bad transition when it goes away.
549 mTmpWindow = w;
550 }
551 w.mLayoutNeeded = false;
552 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200553 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800554 mService.mPolicy.layoutWindowLw(w, null);
555 w.mLayoutSeq = mService.mLayoutSeq;
556
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200557 // If this is the first layout, we need to initialize the last inset values as
558 // otherwise we'd immediately cause an unnecessary resize.
559 if (firstLayout) {
560 w.updateLastInsetValues();
561 }
562
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800563 // Window frames may have changed. Update dim layer with the new bounds.
564 final Task task = w.getTask();
565 if (task != null) {
566 mDimLayerController.updateDimLayer(task);
567 }
568
569 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
570 + " mContainingFrame=" + w.mContainingFrame
571 + " mDisplayFrame=" + w.mDisplayFrame);
572 }
573 }
574 };
575
576 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
577 if (w.mLayoutAttached) {
578 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
579 + " mViewVisibility=" + w.mViewVisibility
580 + " mRelayoutCalled=" + w.mRelayoutCalled);
581 // If this view is GONE, then skip it -- keep the current frame, and let the caller
582 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
583 // windows, since that means "perform layout as normal, just don't display").
584 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
585 return;
586 }
587 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
588 || w.mLayoutNeeded) {
589 if (mTmpInitial) {
590 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
591 w.mContentChanged = false;
592 }
593 w.mLayoutNeeded = false;
594 w.prelayout();
595 mService.mPolicy.layoutWindowLw(w, w.getParentWindow());
596 w.mLayoutSeq = mService.mLayoutSeq;
597 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
598 + " mContainingFrame=" + w.mContainingFrame
599 + " mDisplayFrame=" + w.mDisplayFrame);
600 }
601 } else if (w.mAttrs.type == TYPE_DREAM) {
602 // Don't layout windows behind a dream, so that if it does stuff like hide the
603 // status bar we won't get a bad transition when it goes away.
604 mTmpWindow = mTmpWindow2;
605 }
606 };
607
608 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
609 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
610 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
611 return w.canBeImeTarget();
612 };
613
614 private final Consumer<WindowState> mApplyPostLayoutPolicy =
615 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
616 mService.mInputMethodTarget);
617
618 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
619 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
620 final boolean obscuredChanged = w.mObscured !=
621 mTmpApplySurfaceChangesTransactionState.obscured;
622 final RootWindowContainer root = mService.mRoot;
623 // Only used if default window
624 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
625
626 // Update effect.
627 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
628 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
629 final boolean isDisplayed = w.isDisplayedLw();
630
631 if (isDisplayed && w.isObscuringDisplay()) {
632 // This window completely covers everything behind it, so we want to leave all
633 // of them as undimmed (for performance reasons).
634 root.mObscuringWindow = w;
635 mTmpApplySurfaceChangesTransactionState.obscured = true;
636 }
637
638 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
639 root.handleNotObscuredLocked(w,
640 mTmpApplySurfaceChangesTransactionState.obscured,
641 mTmpApplySurfaceChangesTransactionState.syswin);
642
643 if (w.mHasSurface && isDisplayed) {
644 final int type = w.mAttrs.type;
645 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
646 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
647 mTmpApplySurfaceChangesTransactionState.syswin = true;
648 }
649 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
650 && w.mAttrs.preferredRefreshRate != 0) {
651 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
652 = w.mAttrs.preferredRefreshRate;
653 }
654 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
655 && w.mAttrs.preferredDisplayModeId != 0) {
656 mTmpApplySurfaceChangesTransactionState.preferredModeId
657 = w.mAttrs.preferredDisplayModeId;
658 }
659 }
660 }
661
662 w.applyDimLayerIfNeeded();
663
664 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
665 && mWallpaperController.isWallpaperTarget(w)) {
666 // This is the wallpaper target and its obscured state changed... make sure the
667 // current wallpaper's visibility has been updated accordingly.
668 mWallpaperController.updateWallpaperVisibility();
669 }
670
671 w.handleWindowMovedIfNeeded();
672
673 final WindowStateAnimator winAnimator = w.mWinAnimator;
674
675 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
676 w.mContentChanged = false;
677
678 // Moved from updateWindowsAndWallpaperLocked().
679 if (w.mHasSurface) {
680 // Take care of the window being ready to display.
681 final boolean committed = winAnimator.commitFinishDrawingLocked();
682 if (isDefaultDisplay && committed) {
683 if (w.mAttrs.type == TYPE_DREAM) {
684 // HACK: When a dream is shown, it may at that point hide the lock screen.
685 // So we need to redo the layout to let the phone window manager make this
686 // happen.
687 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
688 if (DEBUG_LAYOUT_REPEATS) {
689 surfacePlacer.debugLayoutRepeats(
690 "dream and commitFinishDrawingLocked true",
691 pendingLayoutChanges);
692 }
693 }
694 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
695 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
696 "First draw done in potential wallpaper target " + w);
697 root.mWallpaperMayChange = true;
698 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
699 if (DEBUG_LAYOUT_REPEATS) {
700 surfacePlacer.debugLayoutRepeats(
701 "wallpaper and commitFinishDrawingLocked true",
702 pendingLayoutChanges);
703 }
704 }
705 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700706 final TaskStack stack = w.getStack();
707 if ((!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening())
708 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800709 // Updates the shown frame before we set up the surface. This is needed
710 // because the resizing could change the top-left position (in addition to
711 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
712 // position the surface.
713 //
714 // If an animation is being started, we can't call this method because the
715 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700716 // we do want to update the position if the window is animating. We make an exception
717 // for the bounds animating state, where an application may have been waiting
718 // for an exit animation to start, but instead enters PiP. We need to ensure
719 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800720 winAnimator.computeShownFrameLocked();
721 }
722 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
723 }
724
725 final AppWindowToken atoken = w.mAppToken;
726 if (atoken != null) {
727 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
728 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
729 mTmpUpdateAllDrawn.add(atoken);
730 }
731 }
732
733 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
734 && w.isDisplayedLw()) {
735 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
736 }
737
738 w.updateResizingWindowIfNeeded();
739 };
740
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800741 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800742 * Create new {@link DisplayContent} instance, add itself to the root window container and
743 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700744 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800745 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700746 * @param layersController window layer controller used to assign layer to the windows on this
747 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700748 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
749 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700750 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700751 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700752 WindowLayersController layersController, WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800753 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
754 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
755 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
756 + " new=" + display);
757 }
758
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700759 mDisplay = display;
760 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700761 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700762 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700763 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700764 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700765 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700766 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700767 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800768 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700769 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800770 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700771
772 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700773 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700774 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700775 super.addChild(mAboveAppWindowsContainers, null);
776 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800777
778 // Add itself as a child to the root container.
779 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700780
781 // TODO(b/62541591): evaluate whether this is the best spot to declare the
782 // {@link DisplayContent} ready for use.
783 mDisplayReady = true;
784 }
785
786 boolean isReady() {
787 // The display is ready when the system and the individual display are both ready.
788 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700789 }
790
791 int getDisplayId() {
792 return mDisplayId;
793 }
794
Wale Ogunwale02319a62016-09-26 15:21:22 -0700795 WindowToken getWindowToken(IBinder binder) {
796 return mTokenMap.get(binder);
797 }
798
799 AppWindowToken getAppWindowToken(IBinder binder) {
800 final WindowToken token = getWindowToken(binder);
801 if (token == null) {
802 return null;
803 }
804 return token.asAppWindowToken();
805 }
806
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700807 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700808 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
809 if (dc != null) {
810 // We currently don't support adding a window token to the display if the display
811 // already has the binder mapped to another token. If there is a use case for supporting
812 // this moving forward we will either need to merge the WindowTokens some how or have
813 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700814 throw new IllegalArgumentException("Can't map token=" + token + " to display="
815 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700816 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700817 if (binder == null) {
818 throw new IllegalArgumentException("Can't map token=" + token + " to display="
819 + getName() + " binder is null");
820 }
821 if (token == null) {
822 throw new IllegalArgumentException("Can't map null token to display="
823 + getName() + " binder=" + binder);
824 }
825
Wale Ogunwale02319a62016-09-26 15:21:22 -0700826 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700827
828 if (token.asAppWindowToken() == null) {
829 // Add non-app token to container hierarchy on the display. App tokens are added through
830 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700831 switch (token.windowType) {
832 case TYPE_WALLPAPER:
833 mBelowAppWindowsContainers.addChild(token);
834 break;
835 case TYPE_INPUT_METHOD:
836 case TYPE_INPUT_METHOD_DIALOG:
837 mImeWindowsContainers.addChild(token);
838 break;
839 default:
840 mAboveAppWindowsContainers.addChild(token);
841 break;
842 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700843 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700844 }
845
846 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700847 final WindowToken token = mTokenMap.remove(binder);
848 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800849 token.setExiting();
850 }
851 return token;
852 }
853
854 /** Changes the display the input window token is housed on to this one. */
855 void reParentWindowToken(WindowToken token) {
856 final DisplayContent prevDc = token.getDisplayContent();
857 if (prevDc == this) {
858 return;
859 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800860 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
861 && token.asAppWindowToken() == null) {
862 // Removed the token from the map, but made sure it's not an app token before removing
863 // from parent.
864 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700865 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800866
867 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700868 }
869
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700870 void removeAppToken(IBinder binder) {
871 final WindowToken token = removeWindowToken(binder);
872 if (token == null) {
873 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
874 return;
875 }
876
877 final AppWindowToken appToken = token.asAppWindowToken();
878
879 if (appToken == null) {
880 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
881 return;
882 }
883
884 appToken.onRemovedFromDisplay();
885 }
886
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700887 Display getDisplay() {
888 return mDisplay;
889 }
890
Craig Mautner59c00972012-07-30 12:10:24 -0700891 DisplayInfo getDisplayInfo() {
892 return mDisplayInfo;
893 }
894
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700895 DisplayMetrics getDisplayMetrics() {
896 return mDisplayMetrics;
897 }
898
Andrii Kulian8ee72852017-03-10 10:36:45 -0800899 int getRotation() {
900 return mRotation;
901 }
902
903 void setRotation(int newRotation) {
904 mRotation = newRotation;
905 }
906
907 int getLastOrientation() {
908 return mLastOrientation;
909 }
910
911 void setLastOrientation(int orientation) {
912 mLastOrientation = orientation;
913 }
914
915 boolean getAltOrientation() {
916 return mAltOrientation;
917 }
918
919 void setAltOrientation(boolean altOrientation) {
920 mAltOrientation = altOrientation;
921 }
922
923 int getLastWindowForcedOrientation() {
924 return mLastWindowForcedOrientation;
925 }
926
Andrii Kulian06d07d62017-03-14 11:11:47 -0700927 /**
928 * Update rotation of the display.
929 *
930 * Returns true if the rotation has been changed. In this case YOU MUST CALL
931 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
932 */
933 boolean updateRotationUnchecked(boolean inTransaction) {
934 if (mService.mDeferredRotationPauseCount > 0) {
935 // Rotation updates have been paused temporarily. Defer the update until
936 // updates have been resumed.
937 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
938 return false;
939 }
940
941 ScreenRotationAnimation screenRotationAnimation =
942 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
943 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
944 // Rotation updates cannot be performed while the previous rotation change
945 // animation is still in progress. Skip this update. We will try updating
946 // again after the animation is finished and the display is unfrozen.
947 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
948 return false;
949 }
950 if (mService.mDisplayFrozen) {
951 // Even if the screen rotation animation has finished (e.g. isAnimating
952 // returns false), there is still some time where we haven't yet unfrozen
953 // the display. We also need to abort rotation here.
954 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
955 "Deferring rotation, still finishing previous rotation");
956 return false;
957 }
958
959 if (!mService.mDisplayEnabled) {
960 // No point choosing a rotation if the display is not enabled.
961 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
962 return false;
963 }
964
965 final int oldRotation = mRotation;
966 final int lastOrientation = mLastOrientation;
967 final boolean oldAltOrientation = mAltOrientation;
968 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr0e007272017-10-02 13:00:55 -0700969 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
Robert Carr897215d2017-03-29 12:25:50 -0700970 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700971
Robert Carr0e007272017-10-02 13:00:55 -0700972 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700973 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
974 if (seamlessRotated != null) {
975 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
976 // to complete (that is, waiting for windows to redraw). It's tempting to check
977 // w.mSeamlessRotationCount but that could be incorrect in the case of
978 // window-removal.
979 return false;
980 }
Robert Carr0e007272017-10-02 13:00:55 -0700981
982 // In the presence of the PINNED stack or System Alert
983 // windows we unforuntately can not seamlessly rotate.
984 if (getStackById(PINNED_STACK_ID) != null) {
985 mayRotateSeamlessly = false;
986 }
987 for (int i = 0; i < mService.mSessions.size(); i++) {
988 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
989 mayRotateSeamlessly = false;
990 break;
991 }
992 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700993 }
Robert Carr0e007272017-10-02 13:00:55 -0700994 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700995
996 // TODO: Implement forced rotation changes.
997 // Set mAltOrientation to indicate that the application is receiving
998 // an orientation that has different metrics than it expected.
999 // eg. Portrait instead of Landscape.
1000
1001 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
1002 lastOrientation, rotation);
1003
1004 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
1005 + ", got rotation " + rotation + " which has "
1006 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1007
1008 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1009 // No change.
1010 return false;
1011 }
1012
1013 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
1014 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1015 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1016
1017 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1018 mService.mWaitingForConfig = true;
1019 }
1020
1021 mRotation = rotation;
1022 mAltOrientation = altOrientation;
1023 if (isDefaultDisplay) {
1024 mService.mPolicy.setRotationLw(rotation);
1025 }
1026
1027 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1028 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1029 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1030 WINDOW_FREEZE_TIMEOUT_DURATION);
1031
1032 setLayoutNeeded();
1033 final int[] anim = new int[2];
1034 if (isDimming()) {
1035 anim[0] = anim[1] = 0;
1036 } else {
1037 mService.mPolicy.selectRotationAnimationLw(anim);
1038 }
1039
1040 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001041 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001042 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1043 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1044 mDisplayId);
1045 } else {
1046 // The screen rotation animation uses a screenshot to freeze the screen
1047 // while windows resize underneath.
1048 // When we are rotating seamlessly, we allow the elements to transition
1049 // to their rotated state independently and without a freeze required.
1050 screenRotationAnimation = null;
1051
1052 // We have to reset this in case a window was removed before it
1053 // finished seamless rotation.
1054 mService.mSeamlessRotationCount = 0;
1055 }
1056
1057 // We need to update our screen size information to match the new rotation. If the rotation
1058 // has actually changed then this method will return true and, according to the comment at
1059 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1060 // By updating the Display info here it will be available to
1061 // #computeScreenConfiguration() later.
1062 updateDisplayAndOrientation(getConfiguration().uiMode);
1063
1064 if (!inTransaction) {
1065 if (SHOW_TRANSACTIONS) {
1066 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1067 }
1068 mService.openSurfaceTransaction();
1069 }
1070 try {
1071 // NOTE: We disable the rotation in the emulator because
1072 // it doesn't support hardware OpenGL emulation yet.
1073 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1074 && screenRotationAnimation.hasScreenshot()) {
1075 if (screenRotationAnimation.setRotationInTransaction(
1076 rotation, mService.mFxSession,
1077 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1078 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1079 mService.scheduleAnimationLocked();
1080 }
1081 }
1082
1083 if (rotateSeamlessly) {
1084 forAllWindows(w -> {
1085 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1086 }, true /* traverseTopToBottom */);
1087 }
1088
1089 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1090 } finally {
1091 if (!inTransaction) {
1092 mService.closeSurfaceTransaction();
1093 if (SHOW_LIGHT_TRANSACTIONS) {
1094 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1095 }
1096 }
1097 }
1098
1099 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001100 if (w.mHasSurface && !rotateSeamlessly) {
1101 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001102 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001103 mService.mRoot.mOrientationChangeComplete = false;
1104 w.mLastFreezeDuration = 0;
1105 }
1106 w.mReportOrientationChanged = true;
1107 }, true /* traverseTopToBottom */);
1108
1109 if (rotateSeamlessly) {
1110 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1111 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1112 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1113 }
1114
1115 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1116 final WindowManagerService.RotationWatcher rotationWatcher
1117 = mService.mRotationWatchers.get(i);
1118 if (rotationWatcher.mDisplayId == mDisplayId) {
1119 try {
1120 rotationWatcher.mWatcher.onRotationChanged(rotation);
1121 } catch (RemoteException e) {
1122 // Ignore
1123 }
1124 }
1125 }
1126
1127 // TODO (multi-display): Magnification is supported only for the default display.
1128 // Announce rotation only if we will not animate as we already have the
1129 // windows in final state. Otherwise, we make this call at the rotation end.
1130 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1131 && isDefaultDisplay) {
1132 mService.mAccessibilityController.onRotationChangedLocked(this);
1133 }
1134
1135 return true;
1136 }
1137
1138 /**
1139 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1140 * changed.
1141 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1142 */
1143 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1144 // Use the effective "visual" dimensions based on current rotation
1145 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1146 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1147 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1148 int dw = realdw;
1149 int dh = realdh;
1150
1151 if (mAltOrientation) {
1152 if (realdw > realdh) {
1153 // Turn landscape into portrait.
1154 int maxw = (int)(realdh/1.3f);
1155 if (maxw < realdw) {
1156 dw = maxw;
1157 }
1158 } else {
1159 // Turn portrait into landscape.
1160 int maxh = (int)(realdw/1.3f);
1161 if (maxh < realdh) {
1162 dh = maxh;
1163 }
1164 }
1165 }
1166
1167 // Update application display metrics.
1168 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1169 mDisplayId);
1170 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1171 mDisplayId);
1172 mDisplayInfo.rotation = mRotation;
1173 mDisplayInfo.logicalWidth = dw;
1174 mDisplayInfo.logicalHeight = dh;
1175 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1176 mDisplayInfo.appWidth = appWidth;
1177 mDisplayInfo.appHeight = appHeight;
1178 if (isDefaultDisplay) {
1179 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1180 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1181 }
1182 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1183 if (mDisplayScalingDisabled) {
1184 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1185 } else {
1186 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1187 }
1188
1189 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1190 mDisplayInfo);
1191
1192 mBaseDisplayRect.set(0, 0, dw, dh);
1193
1194 if (isDefaultDisplay) {
1195 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1196 mCompatDisplayMetrics);
1197 }
1198 return mDisplayInfo;
1199 }
1200
1201 /**
1202 * Compute display configuration based on display properties and policy settings.
1203 * Do not call if mDisplayReady == false.
1204 */
1205 void computeScreenConfiguration(Configuration config) {
1206 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1207
1208 final int dw = displayInfo.logicalWidth;
1209 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001210 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1211 // TODO: Probably best to set this based on some setting in the display content object,
1212 // so the display can be configured for things like fullscreen.
1213 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001214
Andrii Kulian06d07d62017-03-14 11:11:47 -07001215 config.screenWidthDp =
1216 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1217 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1218 config.screenHeightDp =
1219 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1220 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001221
1222 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1223 final int leftInset = mTmpRect.left;
1224 final int topInset = mTmpRect.top;
1225 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001226 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1227 leftInset + displayInfo.appWidth /* right */,
1228 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001229 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1230 || displayInfo.rotation == Surface.ROTATION_270);
1231
1232 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1233 mDisplayMetrics.density, config);
1234
1235 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1236 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1237 ? Configuration.SCREENLAYOUT_ROUND_YES
1238 : Configuration.SCREENLAYOUT_ROUND_NO);
1239
1240 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1241 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1242 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1243 dh, mDisplayId);
1244 config.densityDpi = displayInfo.logicalDensityDpi;
1245
1246 config.colorMode =
1247 (displayInfo.isHdr()
1248 ? Configuration.COLOR_MODE_HDR_YES
1249 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001250 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001251 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1252 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1253
1254 // Update the configuration based on available input devices, lid switch,
1255 // and platform configuration.
1256 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1257 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1258 config.navigation = Configuration.NAVIGATION_NONAV;
1259
1260 int keyboardPresence = 0;
1261 int navigationPresence = 0;
1262 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1263 final int len = devices != null ? devices.length : 0;
1264 for (int i = 0; i < len; i++) {
1265 InputDevice device = devices[i];
1266 if (!device.isVirtual()) {
1267 final int sources = device.getSources();
1268 final int presenceFlag = device.isExternal() ?
1269 WindowManagerPolicy.PRESENCE_EXTERNAL :
1270 WindowManagerPolicy.PRESENCE_INTERNAL;
1271
1272 // TODO(multi-display): Configure on per-display basis.
1273 if (mService.mIsTouchDevice) {
1274 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1275 InputDevice.SOURCE_TOUCHSCREEN) {
1276 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1277 }
1278 } else {
1279 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1280 }
1281
1282 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1283 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1284 navigationPresence |= presenceFlag;
1285 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1286 && config.navigation == Configuration.NAVIGATION_NONAV) {
1287 config.navigation = Configuration.NAVIGATION_DPAD;
1288 navigationPresence |= presenceFlag;
1289 }
1290
1291 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1292 config.keyboard = Configuration.KEYBOARD_QWERTY;
1293 keyboardPresence |= presenceFlag;
1294 }
1295 }
1296 }
1297
1298 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1299 config.navigation = Configuration.NAVIGATION_DPAD;
1300 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1301 }
1302
1303 // Determine whether a hard keyboard is available and enabled.
1304 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1305 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1306 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1307 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1308 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1309 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1310 }
1311
1312 // Let the policy update hidden states.
1313 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1314 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1315 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1316 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1317 }
1318
1319 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1320 int displayId) {
1321 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1322 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1323 final int unrotDw, unrotDh;
1324 if (rotated) {
1325 unrotDw = dh;
1326 unrotDh = dw;
1327 } else {
1328 unrotDw = dw;
1329 unrotDh = dh;
1330 }
1331 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1332 displayId);
1333 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1334 displayId);
1335 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1336 displayId);
1337 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1338 displayId);
1339 return sw;
1340 }
1341
1342 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1343 DisplayMetrics dm, int dw, int dh, int displayId) {
1344 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1345 displayId);
1346 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1347 uiMode, displayId);
1348 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1349 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1350 if (curSize == 0 || size < curSize) {
1351 curSize = size;
1352 }
1353 return curSize;
1354 }
1355
1356 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1357 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1358
1359 // We need to determine the smallest width that will occur under normal
1360 // operation. To this, start with the base screen size and compute the
1361 // width under the different possible rotations. We need to un-rotate
1362 // the current screen dimensions before doing this.
1363 int unrotDw, unrotDh;
1364 if (rotated) {
1365 unrotDw = dh;
1366 unrotDh = dw;
1367 } else {
1368 unrotDw = dw;
1369 unrotDh = dh;
1370 }
1371 displayInfo.smallestNominalAppWidth = 1<<30;
1372 displayInfo.smallestNominalAppHeight = 1<<30;
1373 displayInfo.largestNominalAppWidth = 0;
1374 displayInfo.largestNominalAppHeight = 0;
1375 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1376 unrotDh);
1377 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1378 unrotDw);
1379 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1380 unrotDh);
1381 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1382 unrotDw);
1383 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1384 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1385 displayId);
1386 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1387 displayId);
1388 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1389 displayId);
1390 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1391 displayId);
1392 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1393 outConfig.screenLayout = sl;
1394 }
1395
1396 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1397 int uiMode, int displayId) {
1398 // Get the app screen size at this rotation.
1399 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1400 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1401
1402 // Compute the screen layout size class for this rotation.
1403 int longSize = w;
1404 int shortSize = h;
1405 if (longSize < shortSize) {
1406 int tmp = longSize;
1407 longSize = shortSize;
1408 shortSize = tmp;
1409 }
1410 longSize = (int)(longSize/density);
1411 shortSize = (int)(shortSize/density);
1412 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1413 }
1414
1415 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1416 int uiMode, int dw, int dh) {
1417 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1418 displayId);
1419 if (width < displayInfo.smallestNominalAppWidth) {
1420 displayInfo.smallestNominalAppWidth = width;
1421 }
1422 if (width > displayInfo.largestNominalAppWidth) {
1423 displayInfo.largestNominalAppWidth = width;
1424 }
1425 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1426 displayId);
1427 if (height < displayInfo.smallestNominalAppHeight) {
1428 displayInfo.smallestNominalAppHeight = height;
1429 }
1430 if (height > displayInfo.largestNominalAppHeight) {
1431 displayInfo.largestNominalAppHeight = height;
1432 }
1433 }
1434
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001435 DockedStackDividerController getDockedDividerController() {
1436 return mDividerControllerLocked;
1437 }
1438
Winson Chung655332c2016-10-31 13:14:28 -07001439 PinnedStackController getPinnedStackController() {
1440 return mPinnedStackControllerLocked;
1441 }
1442
Jeff Browna506a6e2013-06-04 00:02:38 -07001443 /**
1444 * Returns true if the specified UID has access to this display.
1445 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001446 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001447 return mDisplay.hasAccess(uid);
1448 }
1449
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001450 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001451 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001452 }
1453
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001454 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001455 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001456 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -08001457 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001458 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001459 }
1460
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001461 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001462 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1463 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001464 if (stack.mStackId == stackId) {
1465 return stack;
1466 }
1467 }
1468 return null;
1469 }
1470
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001471 /**
1472 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1473 * Null is no compatible stack on the display.
1474 */
1475 TaskStack getStack(int windowingMode) {
1476 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1477 }
1478
1479 /**
1480 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1481 * activity type. Null is no compatible stack on the display.
1482 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001483 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001484 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1485 final TaskStack stack = mTaskStackContainers.get(i);
1486 if (stack.isCompatible(windowingMode, activityType)) {
1487 return stack;
1488 }
1489 }
1490 return null;
1491 }
1492
Bryce Lee48f4b572017-04-10 10:54:15 -07001493 @VisibleForTesting
1494 int getStackCount() {
1495 return mTaskStackContainers.size();
1496 }
1497
1498 @VisibleForTesting
Wale Ogunwale68278562017-09-23 17:13:55 -07001499 int getStackPosition(int windowingMode, int activityType) {
Bryce Lee48f4b572017-04-10 10:54:15 -07001500 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1501 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale68278562017-09-23 17:13:55 -07001502 if (stack.isCompatible(windowingMode, activityType)) {
Bryce Lee48f4b572017-04-10 10:54:15 -07001503 return i;
1504 }
1505 }
1506 return -1;
1507 }
1508
Andrii Kulian441e4492016-09-29 15:25:00 -07001509 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001510 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001511 super.onConfigurationChanged(newParentConfig);
1512
Andrii Kulian3a507b52016-09-19 18:14:12 -07001513 // The display size information is heavily dependent on the resources in the current
1514 // configuration, so we need to reconfigure it every time the configuration changes.
1515 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1516 mService.reconfigureDisplayLocked(this);
1517
1518 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001519 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001520 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001521
Andrii Kulian441e4492016-09-29 15:25:00 -07001522 /**
1523 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1524 * bounds were updated.
1525 */
1526 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001527 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1528 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001529 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001530 changedStackList.add(stack.mStackId);
1531 }
1532 }
Winson Chung32c566f2017-04-11 18:31:21 -07001533
1534 // If there was no pinned stack, we still need to notify the controller of the display info
1535 // update as a result of the config change. We do this here to consolidate the flow between
1536 // changes when there is and is not a stack.
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001537 if (getStack(WINDOWING_MODE_PINNED) == null) {
Winson Chung32c566f2017-04-11 18:31:21 -07001538 mPinnedStackControllerLocked.onDisplayInfoChanged();
1539 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001540 }
1541
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001542 @Override
1543 boolean fillsParent() {
1544 return true;
1545 }
1546
1547 @Override
1548 boolean isVisible() {
1549 return true;
1550 }
1551
1552 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001553 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001554 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001555 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001556 }
1557
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001558 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001559 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1560 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1561 // target, or here in order if there isn't an IME target.
1562 if (traverseTopToBottom) {
1563 for (int i = mChildren.size() - 1; i >= 0; --i) {
1564 final DisplayChildWindowContainer child = mChildren.get(i);
1565 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1566 // In this case the Ime windows will be processed above their target so we skip
1567 // here.
1568 continue;
1569 }
1570 if (child.forAllWindows(callback, traverseTopToBottom)) {
1571 return true;
1572 }
1573 }
1574 } else {
1575 final int count = mChildren.size();
1576 for (int i = 0; i < count; i++) {
1577 final DisplayChildWindowContainer child = mChildren.get(i);
1578 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1579 // In this case the Ime windows will be processed above their target so we skip
1580 // here.
1581 continue;
1582 }
1583 if (child.forAllWindows(callback, traverseTopToBottom)) {
1584 return true;
1585 }
1586 }
1587 }
1588 return false;
1589 }
1590
1591 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1592 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1593 }
1594
Wale Ogunwale399c8692017-05-08 14:22:42 -07001595 @Override
1596 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001597 final WindowManagerPolicy policy = mService.mPolicy;
1598
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001599 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001600 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001601 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001602 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001603 // If the display is frozen, some activities may be in the middle of restarting, and
1604 // thus have removed their old window. If the window has the flag to hide the lock
1605 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1606 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001607 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001608 } else if (policy.isKeyguardLocked()) {
1609 // Use the last orientation the while the display is frozen with the keyguard
1610 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1611 // window. We don't want to check the show when locked window directly though as
1612 // things aren't stable while the display is frozen, for example the window could be
1613 // momentarily unavailable due to activity relaunch.
1614 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001615 + "return " + mLastOrientation);
1616 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001617 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001618 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001619 final int orientation = mAboveAppWindowsContainers.getOrientation();
1620 if (orientation != SCREEN_ORIENTATION_UNSET) {
1621 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001622 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001623 }
1624
Wale Ogunwale399c8692017-05-08 14:22:42 -07001625 // Top system windows are not requesting an orientation. Start searching from apps.
1626 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001627 }
1628
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001629 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001630 // Check if display metrics changed and update base values if needed.
1631 updateBaseDisplayMetricsIfNeeded();
1632
Craig Mautner722285e2012-09-07 13:55:58 -07001633 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001634 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001635
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001636 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1637 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001638 }
Craig Mautner722285e2012-09-07 13:55:58 -07001639 }
1640
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001641 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001642 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1643 if (displayManagerInternal != null) {
1644 // Bootstrap the default logical display from the display manager.
1645 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1646 if (newDisplayInfo != null) {
1647 mDisplayInfo.copyFrom(newDisplayInfo);
1648 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001649 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001650
Andrii Kuliancd097992017-03-23 18:31:59 -07001651 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1652 mDisplayInfo.logicalDensityDpi);
1653 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1654 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1655 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001656 }
1657
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001658 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001659 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001660 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001661 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001662 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1663 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001664 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001665 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001666 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001667 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001668 out.set(left, top, left + width, top + height);
1669 }
1670
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001671 private void getLogicalDisplayRect(Rect out, int orientation) {
1672 getLogicalDisplayRect(out);
1673
1674 // Rotate the Rect if needed.
1675 final int currentRotation = mDisplayInfo.rotation;
1676 final int rotationDelta = deltaRotation(currentRotation, orientation);
1677 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1678 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1679 mTmpRectF.set(out);
1680 mTmpMatrix.mapRect(mTmpRectF);
1681 mTmpRectF.round(out);
1682 }
1683 }
1684
Andrii Kuliancd097992017-03-23 18:31:59 -07001685 /**
1686 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1687 * values.
1688 */
1689 private void updateBaseDisplayMetricsIfNeeded() {
1690 // Get real display metrics without overrides from WM.
1691 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1692 final int orientation = mDisplayInfo.rotation;
1693 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1694 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1695 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1696 final int newDensity = mDisplayInfo.logicalDensityDpi;
1697
1698 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1699 || mInitialDisplayHeight != newHeight
1700 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1701
1702 if (displayMetricsChanged) {
1703 // Check if display size or density is forced.
1704 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1705 || mBaseDisplayHeight != mInitialDisplayHeight;
1706 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1707
1708 // If there is an override set for base values - use it, otherwise use new values.
1709 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1710 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1711 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1712
1713 // Real display metrics changed, so we should also update initial values.
1714 mInitialDisplayWidth = newWidth;
1715 mInitialDisplayHeight = newHeight;
1716 mInitialDisplayDensity = newDensity;
1717 mService.reconfigureDisplayLocked(this);
1718 }
1719 }
1720
Bryce Lee27cec322017-03-21 09:41:37 -07001721 /** Sets the maximum width the screen resolution can be */
1722 void setMaxUiWidth(int width) {
1723 if (DEBUG_DISPLAY) {
1724 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1725 }
1726
1727 mMaxUiWidth = width;
1728
1729 // Update existing metrics.
1730 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1731 }
1732
1733 /** Update base (override) display metrics. */
1734 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1735 mBaseDisplayWidth = baseWidth;
1736 mBaseDisplayHeight = baseHeight;
1737 mBaseDisplayDensity = baseDensity;
1738
1739 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1740 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1741 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1742 mBaseDisplayWidth = mMaxUiWidth;
1743
1744 if (DEBUG_DISPLAY) {
1745 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1746 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1747 + " on display:" + getDisplayId());
1748 }
1749 }
1750
1751 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1752 }
1753
Chong Zhangf66db432016-01-13 10:39:51 -08001754 void getContentRect(Rect out) {
1755 out.set(mContentRect);
1756 }
1757
Wale Ogunwale704a3c02017-09-18 15:30:52 -07001758 TaskStack addStackToDisplay(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001759 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1760 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001761
Wale Ogunwale1666e312016-12-16 11:27:18 -08001762 TaskStack stack = getStackById(stackId);
1763 if (stack != null) {
Wale Ogunwale704a3c02017-09-18 15:30:52 -07001764 // It's already attached to the display...clear mDeferRemoval, set controller, and move
1765 // stack to appropriate z-order on display as needed.
Wale Ogunwale1666e312016-12-16 11:27:18 -08001766 stack.mDeferRemoval = false;
Wale Ogunwale704a3c02017-09-18 15:30:52 -07001767 stack.setController(controller);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001768 // We're not moving the display to front when we're adding stacks, only when
1769 // requested to change the position of stack explicitly.
1770 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
1771 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -07001772 } else {
Wale Ogunwale704a3c02017-09-18 15:30:52 -07001773 stack = new TaskStack(mService, stackId, controller);
Andrii Kulian839def92016-11-02 10:58:58 -07001774 mTaskStackContainers.addStackToDisplay(stack, onTop);
1775 }
1776
Wale Ogunwale1666e312016-12-16 11:27:18 -08001777 if (stackId == DOCKED_STACK_ID) {
1778 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -07001779 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001780 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001781 }
1782
Andrii Kulian51c1b672017-04-07 18:39:32 -07001783 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001784 final DisplayContent prevDc = stack.getDisplayContent();
1785 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001786 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1787 + " which is not currently attached to any display");
1788 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001789 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001790 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1791 + " to its current displayId=" + mDisplayId);
1792 }
1793
Wale Ogunwale1666e312016-12-16 11:27:18 -08001794 prevDc.mTaskStackContainers.removeStackFromDisplay(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001795 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001796 }
1797
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001798 @Override
1799 protected void addChild(DisplayChildWindowContainer child,
1800 Comparator<DisplayChildWindowContainer> comparator) {
1801 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1802 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001803
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001804 @Override
1805 protected void addChild(DisplayChildWindowContainer child, int index) {
1806 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1807 }
1808
1809 @Override
1810 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001811 // Only allow removal of direct children from this display if the display is in the process
1812 // of been removed.
1813 if (mRemovingDisplay) {
1814 super.removeChild(child);
1815 return;
1816 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001817 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001818 }
1819
Andrii Kuliand2765632016-12-12 22:26:34 -08001820 @Override
1821 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1822 // Children of the display are statically ordered, so the real intention here is to perform
1823 // the operation on the display and not the static direct children.
1824 getParent().positionChildAt(position, this, includingParents);
1825 }
1826
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001827 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001828 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1829 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001830 final int taskId = stack.taskIdFromPoint(x, y);
1831 if (taskId != -1) {
1832 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001833 }
1834 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001835 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001836 }
1837
Chong Zhang8e89b312015-09-09 15:09:30 -07001838 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001839 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001840 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001841 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001842 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001843 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001844 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001845 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1846 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001847 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001848 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001849 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001850
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001851 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1852 if (mTmpTaskForResizePointSearchResult.searchDone) {
1853 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001854 }
1855 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001856 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001857 }
1858
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001859 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001860 // The provided task is the task on this display with focus, so if WindowManagerService's
1861 // focused app is not on this display, focusedTask will be null.
1862 if (focusedTask == null) {
1863 mTouchExcludeRegion.setEmpty();
1864 } else {
1865 mTouchExcludeRegion.set(mBaseDisplayRect);
1866 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1867 mTmpRect2.setEmpty();
1868 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1869 final TaskStack stack = mTaskStackContainers.get(stackNdx);
1870 stack.setTouchExcludeRegion(
1871 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
1872 }
1873 // If we removed the focused task above, add it back and only leave its
1874 // outside touch area in the exclusion. TapDectector is not interested in
1875 // any touch inside the focused task itself.
1876 if (!mTmpRect2.isEmpty()) {
1877 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1878 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001879 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001880 final WindowState inputMethod = mService.mInputMethodWindow;
1881 if (inputMethod != null && inputMethod.isVisibleLw()) {
1882 // If the input method is visible and the user is typing, we don't want these touch
1883 // events to be intercepted and used to change focus. This would likely cause a
1884 // disappearance of the input method.
1885 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001886 if (inputMethod.getDisplayId() == mDisplayId) {
1887 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1888 } else {
1889 // IME is on a different display, so we need to update its tap detector.
1890 // TODO(multidisplay): Remove when IME will always appear on same display.
1891 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1892 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001893 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001894 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1895 WindowState win = mTapExcludedWindows.get(i);
1896 win.getTouchableRegion(mTmpRegion);
1897 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1898 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001899 // TODO(multi-display): Support docked stacks on secondary displays.
1900 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001901 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001902 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001903 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1904 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001905 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001906 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001907 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001908 }
1909
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001910 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001911 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001912 super.switchUser();
1913 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001914 }
1915
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001916 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001917 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1918 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001919 }
1920 }
1921
1922 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001923 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001924 }
1925
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001926 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001927 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001928 }
1929
1930 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001931 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001932 }
1933
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001934 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001935 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001936 }
1937
Wale Ogunwale10124582016-09-15 20:25:50 -07001938 @Override
1939 void removeIfPossible() {
1940 if (isAnimating()) {
1941 mDeferredRemoval = true;
1942 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001943 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001944 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001945 }
1946
Wale Ogunwale10124582016-09-15 20:25:50 -07001947 @Override
1948 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001949 mRemovingDisplay = true;
1950 try {
1951 super.removeImmediately();
1952 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1953 mDimLayerController.close();
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07001954 if (mService.canDispatchPointerEvents()) {
1955 if (mTapDetector != null) {
1956 mService.unregisterPointerEventListener(mTapDetector);
1957 }
1958 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
1959 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1960 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001961 }
1962 } finally {
1963 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001964 }
Craig Mautner95da1082014-02-24 17:54:35 -08001965 }
1966
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001967 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001968 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001969 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001970 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1971
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001972 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001973 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001974 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001975 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001976 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001977 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001978 }
1979
Andrii Kulian0214ed92017-05-16 13:44:05 -07001980 /** @return 'true' if removal of this display content is deferred due to active animation. */
1981 boolean isRemovalDeferred() {
1982 return mDeferredRemoval;
1983 }
1984
Wale Ogunwale10124582016-09-15 20:25:50 -07001985 boolean animateForIme(float interpolatedValue, float animationTarget,
1986 float dividerAnimationTarget) {
1987 boolean updated = false;
1988
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001989 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1990 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001991 if (stack == null || !stack.isAdjustedForIme()) {
1992 continue;
1993 }
1994
1995 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1996 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1997 updated = true;
1998 } else {
1999 mDividerControllerLocked.mLastAnimationProgress =
2000 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2001 mDividerControllerLocked.mLastDividerProgress =
2002 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2003 updated |= stack.updateAdjustForIme(
2004 mDividerControllerLocked.mLastAnimationProgress,
2005 mDividerControllerLocked.mLastDividerProgress,
2006 false /* force */);
2007 }
2008 if (interpolatedValue >= 1f) {
2009 stack.endImeAdjustAnimation();
2010 }
2011 }
2012
2013 return updated;
2014 }
2015
2016 boolean clearImeAdjustAnimation() {
2017 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002018 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2019 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002020 if (stack != null && stack.isAdjustedForIme()) {
2021 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2022 changed = true;
2023 }
2024 }
2025 return changed;
2026 }
2027
2028 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002029 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2030 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002031 if (stack.isVisible() && stack.isAdjustedForIme()) {
2032 stack.beginImeAdjustAnimation();
2033 }
2034 }
2035 }
2036
2037 void adjustForImeIfNeeded() {
2038 final WindowState imeWin = mService.mInputMethodWindow;
2039 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2040 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002041 final boolean dockVisible = isStackVisible(DOCKED_STACK_ID);
Wale Ogunwale10124582016-09-15 20:25:50 -07002042 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2043 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2044 imeTargetStack.getDockSide() : DOCKED_INVALID;
2045 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2046 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2047 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
2048 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
2049 final boolean imeHeightChanged = imeVisible &&
2050 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2051
2052 // The divider could be adjusted for IME position, or be thinner than usual,
2053 // or both. There are three possible cases:
2054 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2055 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2056 // - If IME is not visible, divider is not moved and is normal width.
2057
2058 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002059 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2060 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002061 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002062 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2063 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002064 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2065 } else {
2066 stack.resetAdjustedForIme(false);
2067 }
2068 }
2069 mDividerControllerLocked.setAdjustedForIme(
2070 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2071 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002072 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2073 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002074 stack.resetAdjustedForIme(!dockVisible);
2075 }
2076 mDividerControllerLocked.setAdjustedForIme(
2077 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2078 }
Winson Chung655332c2016-10-31 13:14:28 -07002079 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002080 }
2081
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002082 void setInputMethodAnimLayerAdjustment(int adj) {
2083 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2084 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002085 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002086 }
2087
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002088 /**
2089 * If a window that has an animation specifying a colored background and the current wallpaper
2090 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2091 * suddenly disappear.
2092 */
2093 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002094 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2095 w -> w.mIsWallpaper && w.isVisibleNow());
2096
2097 if (visibleWallpaper != null) {
2098 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002099 }
2100 return winAnimator.mAnimLayer;
2101 }
2102
Wale Ogunwale10124582016-09-15 20:25:50 -07002103 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002104 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2105 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002106 stack.prepareFreezingTaskBounds();
2107 }
2108 }
2109
Wale Ogunwale94744212015-09-21 19:01:47 -07002110 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002111 getLogicalDisplayRect(mTmpRect, newRotation);
2112
2113 // Compute a transform matrix to undo the coordinate space transformation,
2114 // and present the window at the same physical position it previously occupied.
2115 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2116 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2117
2118 mTmpRectF.set(bounds);
2119 mTmpMatrix.mapRect(mTmpRectF);
2120 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002121 }
2122
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002123 static int deltaRotation(int oldRotation, int newRotation) {
2124 int delta = newRotation - oldRotation;
2125 if (delta < 0) delta += 4;
2126 return delta;
2127 }
2128
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002129 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002130 Matrix outMatrix) {
2131 // For rotations without Z-ordering we don't need the target rectangle's position.
2132 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2133 displayHeight, outMatrix);
2134 }
2135
2136 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2137 float displayWidth, float displayHeight, Matrix outMatrix) {
2138 switch (rotation) {
2139 case ROTATION_0:
2140 outMatrix.reset();
2141 break;
2142 case ROTATION_270:
2143 outMatrix.setRotate(270, 0, 0);
2144 outMatrix.postTranslate(0, displayHeight);
2145 outMatrix.postTranslate(rectTop, 0);
2146 break;
2147 case ROTATION_180:
2148 outMatrix.reset();
2149 break;
2150 case ROTATION_90:
2151 outMatrix.setRotate(90, 0, 0);
2152 outMatrix.postTranslate(displayWidth, 0);
2153 outMatrix.postTranslate(-rectTop, rectLeft);
2154 break;
2155 }
2156 }
2157
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002158 @CallSuper
2159 @Override
2160 public void writeToProto(ProtoOutputStream proto, long fieldId) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002161 final long token = proto.start(fieldId);
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002162 super.writeToProto(proto, WINDOW_CONTAINER);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002163 proto.write(ID, mDisplayId);
2164 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2165 final TaskStack stack = mTaskStackContainers.get(stackNdx);
2166 stack.writeToProto(proto, STACKS);
2167 }
2168 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2169 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
2170 for (int i = mAboveAppWindowsContainers.size() - 1; i >= 0; --i) {
2171 final WindowToken windowToken = mAboveAppWindowsContainers.get(i);
2172 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS);
2173 }
2174 for (int i = mBelowAppWindowsContainers.size() - 1; i >= 0; --i) {
2175 final WindowToken windowToken = mBelowAppWindowsContainers.get(i);
2176 windowToken.writeToProto(proto, BELOW_APP_WINDOWS);
2177 }
2178 for (int i = mImeWindowsContainers.size() - 1; i >= 0; --i) {
2179 final WindowToken windowToken = mImeWindowsContainers.get(i);
2180 windowToken.writeToProto(proto, IME_WINDOWS);
2181 }
2182 proto.write(DPI, mBaseDisplayDensity);
2183 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002184 proto.write(ROTATION, mRotation);
2185 final ScreenRotationAnimation screenRotationAnimation =
2186 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2187 if (screenRotationAnimation != null) {
2188 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2189 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002190 proto.end(token);
2191 }
2192
Craig Mautnera91f9e22012-09-14 16:22:08 -07002193 public void dump(String prefix, PrintWriter pw) {
2194 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2195 final String subPrefix = " " + prefix;
2196 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2197 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2198 pw.print("dpi");
2199 if (mInitialDisplayWidth != mBaseDisplayWidth
2200 || mInitialDisplayHeight != mBaseDisplayHeight
2201 || mInitialDisplayDensity != mBaseDisplayDensity) {
2202 pw.print(" base=");
2203 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2204 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2205 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002206 if (mDisplayScalingDisabled) {
2207 pw.println(" noscale");
2208 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002209 pw.print(" cur=");
2210 pw.print(mDisplayInfo.logicalWidth);
2211 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2212 pw.print(" app=");
2213 pw.print(mDisplayInfo.appWidth);
2214 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2215 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2216 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2217 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2218 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002219 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002220 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002221 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002222
Craig Mautnerdc548482014-02-05 13:35:24 -08002223 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002224 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002225 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2226 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002227 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002228 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002229
Craig Mautnerdc548482014-02-05 13:35:24 -08002230 pw.println();
2231 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002232 pw.println();
2233 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002234 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002235 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002236 pw.print(" Exiting #"); pw.print(i);
2237 pw.print(' '); pw.print(token);
2238 pw.println(':');
2239 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002240 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002241 }
Craig Mautner59c00972012-07-30 12:10:24 -07002242 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002243 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002244 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002245 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002246 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002247 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002248
2249 if (mInputMethodAnimLayerAdjustment != 0) {
2250 pw.println(subPrefix
2251 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2252 }
Craig Mautner59c00972012-07-30 12:10:24 -07002253 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002254
2255 @Override
2256 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002257 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002258 }
2259
2260 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002261 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002262 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002263
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002264 /** Checks if stack with provided id is visible on this display. */
2265 boolean isStackVisible(int stackId) {
2266 final TaskStack stack = getStackById(stackId);
2267 return (stack != null && stack.isVisible());
2268 }
2269
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002270 /**
2271 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
2272 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07002273 TaskStack getDockedStackLocked() {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07002274 final TaskStack stack = getStack(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07002275 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002276 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002277
2278 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002279 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02002280 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002281 */
Jorim Jaggife762342016-10-13 14:33:27 +02002282 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07002283 return getStack(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002284 }
2285
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002286 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002287 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002288 final int x = (int) xf;
2289 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002290 final WindowState touchedWin = getWindow(w -> {
2291 final int flags = w.mAttrs.flags;
2292 if (!w.isVisibleLw()) {
2293 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002294 }
2295 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002296 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002297 }
2298
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002299 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002300 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002301 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002302 }
2303
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002304 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002305
2306 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002307 return mTmpRegion.contains(x, y) || touchFlags == 0;
2308 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002309
2310 return touchedWin;
2311 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002312
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002313 boolean canAddToastWindowForUid(int uid) {
2314 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002315 // Also if the app is focused adding more than one toast at
2316 // a time for better backwards compatibility.
2317 final WindowState focusedWindowForUid = getWindow(w ->
2318 w.mOwnerUid == uid && w.isFocused());
2319 if (focusedWindowForUid != null) {
2320 return true;
2321 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002322 final WindowState win = getWindow(w ->
2323 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2324 && !w.mWindowRemovalAllowed);
2325 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002326 }
2327
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002328 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002329 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2330 return;
2331 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002332
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002333 // Used to communicate the old focus to the callback method.
2334 mTmpWindow = oldFocus;
2335
2336 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002337 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002338
2339 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002340 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002341
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002342 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002343
2344 if (mTmpWindow == null) {
2345 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2346 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002347 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002348 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002349 }
2350
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002351 /** Updates the layer assignment of windows on this display. */
2352 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002353 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002354 if (setLayoutNeeded) {
2355 setLayoutNeeded();
2356 }
2357 }
2358
Wale Ogunwale1666e312016-12-16 11:27:18 -08002359 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2360 // moving containers or resizing them. Need to investigate the best way to have it automatically
2361 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002362 void layoutAndAssignWindowLayersIfNeeded() {
2363 mService.mWindowsChanged = true;
2364 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002365
2366 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2367 false /*updateInputWindows*/)) {
2368 assignWindowLayers(false /* setLayoutNeeded */);
2369 }
2370
2371 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2372 mService.mWindowPlacerLocked.performSurfacePlacement();
2373 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2374 }
2375
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002376 /** Returns true if a leaked surface was destroyed */
2377 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002378 // Used to indicate that a surface was leaked.
2379 mTmpWindow = null;
2380 forAllWindows(w -> {
2381 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002382 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002383 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002384 }
2385 if (!mService.mSessions.contains(wsa.mSession)) {
2386 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002387 + w + " surface=" + wsa.mSurfaceController
2388 + " token=" + w.mToken
2389 + " pid=" + w.mSession.mPid
2390 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002391 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002392 mService.mForceRemoves.add(w);
2393 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002394 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002395 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002396 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002397 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002398 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002399 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002400 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002401 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002402 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002403
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002404 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002405 }
2406
2407 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002408 * Determine and return the window that should be the IME target.
2409 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2410 * @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 +00002411 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002412 WindowState computeImeTarget(boolean updateImeTarget) {
2413 if (mService.mInputMethodWindow == null) {
2414 // There isn't an IME so there shouldn't be a target...That was easy!
2415 if (updateImeTarget) {
2416 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2417 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2418 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2419 }
2420 return null;
2421 }
2422
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002423 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2424 // same display. Or even when the current IME/target are not on the same screen as the next
2425 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002426 mUpdateImeTarget = updateImeTarget;
2427 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002428
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002429
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002430 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2431 // to be on top of it, but it is not -really- where input will go. So look down below
2432 // for a real window to target...
2433 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2434 final AppWindowToken token = target.mAppToken;
2435 if (token != null) {
2436 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2437 if (betterTarget != null) {
2438 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002439 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002440 }
2441 }
2442
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002443 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2444 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002445
2446 // Now, a special case -- if the last target's window is in the process of exiting, and is
2447 // above the new target, keep on the last target to avoid flicker. Consider for example a
2448 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2449 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2450 // scrim.
2451 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002452 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2453 && (target == null
2454 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002455 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002456 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002457 }
2458
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002459 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2460 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002461
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002462 if (target == null) {
2463 if (updateImeTarget) {
2464 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2465 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2466 + Debug.getCallers(4) : ""));
2467 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2468 }
2469
2470 return null;
2471 }
2472
2473 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002474 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2475 if (token != null) {
2476
2477 // Now some fun for dealing with window animations that modify the Z order. We need
2478 // to look at all windows below the current target that are in this app, finding the
2479 // highest visible one in layering.
2480 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002481 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002482 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002483 }
2484
2485 if (highestTarget != null) {
2486 final AppTransition appTransition = mService.mAppTransition;
2487 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2488 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2489 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002490 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002491
2492 if (appTransition.isTransitionSet()) {
2493 // If we are currently setting up for an animation, hold everything until we
2494 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002495 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2496 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002497 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002498 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002499 // If the window we are currently targeting is involved with an animation,
2500 // and it is on top of the next target we will be over, then hold off on
2501 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002502 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2503 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002504 }
2505 }
2506 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002507
2508 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2509 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2510 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002511 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002512 }
2513
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002514 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002515 }
2516
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002517 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2518 if (target == mService.mInputMethodTarget
2519 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2520 && mInputMethodAnimLayerAdjustment == layerAdj) {
2521 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002522 }
2523
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002524 mService.mInputMethodTarget = target;
2525 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2526 setInputMethodAnimLayerAdjustment(layerAdj);
2527 assignWindowLayers(false /* setLayoutNeeded */);
2528 }
2529
2530 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2531 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2532 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2533 }
2534
2535 // Used to indicate we have reached the first window in the range we are interested in.
2536 mTmpWindow = null;
2537
2538 // TODO: Figure-out a more efficient way to do this.
2539 final WindowState candidate = getWindow(w -> {
2540 if (w == top) {
2541 // Reached the first window in the range we are interested in.
2542 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002543 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002544 if (mTmpWindow == null) {
2545 return false;
2546 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002547
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002548 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2549 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002550 }
2551 // If we reached the bottom of the range of windows we are considering,
2552 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002553 if (w == bottom) {
2554 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002555 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002556 return false;
2557 });
2558
2559 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002560 }
2561
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002562 void setLayoutNeeded() {
2563 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2564 mLayoutNeeded = true;
2565 }
2566
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002567 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002568 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2569 mLayoutNeeded = false;
2570 }
2571
2572 boolean isLayoutNeeded() {
2573 return mLayoutNeeded;
2574 }
2575
Wale Ogunwale02319a62016-09-26 15:21:22 -07002576 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2577 if (mTokenMap.isEmpty()) {
2578 return;
2579 }
2580 pw.println(" Display #" + mDisplayId);
2581 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2582 while (it.hasNext()) {
2583 final WindowToken token = it.next();
2584 pw.print(" ");
2585 pw.print(token);
2586 if (dumpAll) {
2587 pw.println(':');
2588 token.dump(pw, " ");
2589 } else {
2590 pw.println();
2591 }
2592 }
2593 }
2594
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002595 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002596 final int[] index = new int[1];
2597 forAllWindows(w -> {
2598 final WindowStateAnimator wAnim = w.mWinAnimator;
2599 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2600 index[0] = index[0] + 1;
2601 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002602 }
2603
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002604 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002605 forAllWindows(w -> {
2606 w.mWinAnimator.enableSurfaceTrace(fd);
2607 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002608 }
2609
2610 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002611 forAllWindows(w -> {
2612 w.mWinAnimator.disableSurfaceTrace();
2613 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002614 }
2615
Jorim Jaggife762342016-10-13 14:33:27 +02002616 /**
2617 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2618 */
2619 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2620 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002621 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002622 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2623 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002624 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002625 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2626 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002627 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002628 }
2629
Wale Ogunwale494009b82016-10-21 09:01:38 -07002630 boolean checkWaitingForWindows() {
2631
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002632 mHaveBootMsg = false;
2633 mHaveApp = false;
2634 mHaveWallpaper = false;
2635 mHaveKeyguard = true;
2636
2637 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002638 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2639 return true;
2640 }
2641 if (w.isDrawnLw()) {
2642 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002643 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002644 } else if (w.mAttrs.type == TYPE_APPLICATION
2645 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002646 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002647 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002648 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002649 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002650 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002651 }
2652 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002653 return false;
2654 });
2655
2656 if (visibleWindow != null) {
2657 // We have a visible window.
2658 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002659 }
2660
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002661 // if the wallpaper service is disabled on the device, we're never going to have
2662 // wallpaper, don't bother waiting for it
2663 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2664 com.android.internal.R.bool.config_enableWallpaperService)
2665 && !mService.mOnlyCore;
2666
Wale Ogunwale494009b82016-10-21 09:01:38 -07002667 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2668 "******** booted=" + mService.mSystemBooted
2669 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002670 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2671 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2672 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002673
2674 // If we are turning on the screen to show the boot message, don't do it until the boot
2675 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002676 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002677 return true;
2678 }
2679
2680 // If we are turning on the screen after the boot is completed normally, don't do so until
2681 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002682 if (mService.mSystemBooted
2683 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002684 return true;
2685 }
2686
2687 return false;
2688 }
2689
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002690 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002691 mTmpWindowAnimator = animator;
2692 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002693 }
2694
2695 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002696 resetAnimationBackgroundAnimator();
2697
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002698 // Used to indicate a detached wallpaper.
2699 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002700 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002701
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002702 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002703
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002704 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002705 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002706 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2707 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002708 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2709 }
2710 }
2711
2712 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002713 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002714 }
2715
Wale Ogunwale494009b82016-10-21 09:01:38 -07002716 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002717 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002718 if (imFocus == null) {
2719 return false;
2720 }
2721
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002722 if (DEBUG_INPUT_METHOD) {
2723 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2724 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2725 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002726 }
2727
Wale Ogunwale494009b82016-10-21 09:01:38 -07002728 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2729
2730 if (DEBUG_INPUT_METHOD) {
2731 Slog.i(TAG_WM, "IM target client: " + imeClient);
2732 if (imeClient != null) {
2733 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2734 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2735 }
2736 }
2737
2738 return imeClient != null && imeClient.asBinder() == client.asBinder();
2739 }
2740
2741 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002742 final WindowState win = getWindow(
2743 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2744 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002745 }
2746
2747 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002748 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002749 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002750 final int curValue = w.mSystemUiVisibility;
2751 final int diff = (curValue ^ visibility) & globalDiff;
2752 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002753 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002754 w.mSeq++;
2755 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002756 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002757 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2758 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002759 visibility, newValue, diff);
2760 }
2761 } catch (RemoteException e) {
2762 // so sorry
2763 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002764 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002765 }
2766
2767 void onWindowFreezeTimeout() {
2768 Slog.w(TAG_WM, "Window freeze timeout expired.");
2769 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002770
2771 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002772 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002773 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002774 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002775 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002776 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2777 - mService.mDisplayFreezeTime);
2778 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002779 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002780 mService.mWindowPlacerLocked.performSurfacePlacement();
2781 }
2782
2783 void waitForAllWindowsDrawn() {
2784 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002785 forAllWindows(w -> {
2786 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2787 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2788 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002789 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002790 w.mLastContentInsets.set(-1, -1, -1, -1);
2791 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002792 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002793 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002794 }
2795
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002796 // TODO: Super crazy long method that should be broken down...
2797 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2798
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002799 final int dw = mDisplayInfo.logicalWidth;
2800 final int dh = mDisplayInfo.logicalHeight;
2801 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2802
2803 mTmpUpdateAllDrawn.clear();
2804
2805 int repeats = 0;
2806 do {
2807 repeats++;
2808 if (repeats > 6) {
2809 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2810 clearLayoutNeeded();
2811 break;
2812 }
2813
2814 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2815 pendingLayoutChanges);
2816
Andrii Kulian839def92016-11-02 10:58:58 -07002817 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2818 // the wallpaper window jumping across displays.
2819 // Remove check for default display when there will be support for multiple wallpaper
2820 // targets (on different displays).
2821 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002822 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002823 }
2824
2825 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2826 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2827 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2828 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002829 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002830 }
2831 }
2832
2833 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2834 setLayoutNeeded();
2835 }
2836
2837 // FIRST LOOP: Perform a layout, if needed.
2838 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2839 performLayout(repeats == 1, false /* updateInputWindows */);
2840 } else {
2841 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2842 }
2843
2844 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2845 pendingLayoutChanges = 0;
2846
2847 if (isDefaultDisplay) {
2848 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002849 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002850 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2851 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2852 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2853 }
2854 } while (pendingLayoutChanges != 0);
2855
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002856 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002857 resetDimming();
2858
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002859 mTmpRecoveringMemory = recoveringMemory;
2860 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002861
2862 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002863 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2864 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2865 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002866 true /* inTraversal, must call performTraversalInTrans... below */);
2867
2868 stopDimmingIfNeeded();
2869
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002870 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2871 if (wallpaperVisible != mLastWallpaperVisible) {
2872 mLastWallpaperVisible = wallpaperVisible;
2873 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2874 }
2875
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002876 while (!mTmpUpdateAllDrawn.isEmpty()) {
2877 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2878 // See if any windows have been drawn, so they (and others associated with them)
2879 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002880 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002881 }
2882
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002883 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002884 }
2885
2886 void performLayout(boolean initial, boolean updateInputWindows) {
2887 if (!isLayoutNeeded()) {
2888 return;
2889 }
2890 clearLayoutNeeded();
2891
2892 final int dw = mDisplayInfo.logicalWidth;
2893 final int dh = mDisplayInfo.logicalHeight;
2894
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002895 if (DEBUG_LAYOUT) {
2896 Slog.v(TAG, "-------------------------------------");
2897 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2898 }
2899
Andrii Kulian8ee72852017-03-10 10:36:45 -08002900 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002901 getConfiguration().uiMode);
2902 if (isDefaultDisplay) {
2903 // Not needed on non-default displays.
2904 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2905 mService.mScreenRect.set(0, 0, dw, dh);
2906 }
2907
2908 mService.mPolicy.getContentRectLw(mContentRect);
2909
2910 int seq = mService.mLayoutSeq + 1;
2911 if (seq < 0) seq = 0;
2912 mService.mLayoutSeq = seq;
2913
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002914 // Used to indicate that we have processed the dream window and all additional windows are
2915 // behind it.
2916 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002917 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002918
2919 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002920 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002921
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002922 // Used to indicate that we have processed the dream window and all additional attached
2923 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002924 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002925 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002926
2927 // Now perform layout of attached windows, which usually depend on the position of the
2928 // window they are attached to. XXX does not deal with windows that are attached to windows
2929 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002930 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002931
2932 // Window frames may have changed. Tell the input dispatcher about it.
2933 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2934 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2935 if (updateInputWindows) {
2936 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2937 }
2938
2939 mService.mPolicy.finishLayoutLw();
2940 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2941 }
2942
2943 /**
2944 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2945 * In portrait mode, it grabs the full screenshot.
2946 *
2947 * @param width the width of the target bitmap
2948 * @param height the height of the target bitmap
2949 * @param includeFullDisplay true if the screen should not be cropped before capture
2950 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2951 * @param config of the output bitmap
2952 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002953 * @param includeDecor whether to include window decors, like the status or navigation bar
2954 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002955 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002956 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002957 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002958 boolean wallpaperOnly, boolean includeDecor) {
2959 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2960 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002961 if (bitmap == null) {
2962 return null;
2963 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002964
2965 if (DEBUG_SCREENSHOT) {
2966 // TEST IF IT's ALL BLACK
2967 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2968 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2969 bitmap.getHeight());
2970 boolean allBlack = true;
2971 final int firstColor = buffer[0];
2972 for (int i = 0; i < buffer.length; i++) {
2973 if (buffer[i] != firstColor) {
2974 allBlack = false;
2975 break;
2976 }
2977 }
2978 if (allBlack) {
2979 final WindowState appWin = mScreenshotApplicationState.appWin;
2980 final int maxLayer = mScreenshotApplicationState.maxLayer;
2981 final int minLayer = mScreenshotApplicationState.minLayer;
2982 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2983 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2984 (appWin != null ?
2985 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2986 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2987 }
2988 }
2989
2990 // Create a copy of the screenshot that is immutable and backed in ashmem.
2991 // This greatly reduces the overhead of passing the bitmap between processes.
2992 Bitmap ret = bitmap.createAshmemBitmap(config);
2993 bitmap.recycle();
2994 return ret;
2995 }
2996
2997 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2998 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2999 boolean includeDecor) {
3000 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
3001 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
3002 }
3003
3004 private <E> E screenshotApplications(IBinder appToken, int width, int height,
3005 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
3006 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003007 int dw = mDisplayInfo.logicalWidth;
3008 int dh = mDisplayInfo.logicalHeight;
3009 if (dw == 0 || dh == 0) {
3010 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3011 + ": returning null. logical widthxheight=" + dw + "x" + dh);
3012 return null;
3013 }
3014
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003015 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003016
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003017 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003018 final Rect frame = new Rect();
3019 final Rect stackBounds = new Rect();
3020
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003021 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003022 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08003023 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003024 synchronized(mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02003025 if (!mService.mPolicy.isScreenOn()) {
3026 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Attempted to take screenshot while display"
3027 + " was off.");
3028 return null;
3029 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003030 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003031 mScreenshotApplicationState.appWin = null;
3032 forAllWindows(w -> {
3033 if (!w.mHasSurface) {
3034 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003035 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003036 if (w.mLayer >= aboveAppLayer) {
3037 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003038 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003039 if (wallpaperOnly && !w.mIsWallpaper) {
3040 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003041 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003042 if (w.mIsImWindow) {
Jorim Jaggieb59d6e2017-05-04 13:54:22 +02003043 return false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003044 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003045 // If this is the wallpaper layer and we're only looking for the wallpaper layer
3046 // then the target window state is this one.
3047 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003048 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003049 }
3050
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003051 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003052 // We have not ran across the target window yet, so it is probably behind
3053 // the wallpaper. This can happen when the keyguard is up and all windows
3054 // are moved behind the wallpaper. We don't want to include the wallpaper
3055 // layer in the screenshot as it will cover-up the layer of the target
3056 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003057 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003058 }
3059 // Fall through. The target window is in front of the wallpaper. For this
3060 // case we want to include the wallpaper layer in the screenshot because
3061 // the target window might have some transparent areas.
3062 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003063 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003064 // This app window is of no interest if it is not associated with the
3065 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003066 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003067 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003068 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003069 }
3070
3071 // Include this window.
3072
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003073 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003074 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003075 if (mScreenshotApplicationState.maxLayer < layer) {
3076 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003077 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003078 if (mScreenshotApplicationState.minLayer > layer) {
3079 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003080 }
3081
3082 // Don't include wallpaper in bounds calculation
Jorim Jaggi70f59482017-05-04 14:05:59 +02003083 if (!w.mIsWallpaper && !mutableIncludeFullDisplay.value) {
3084 if (includeDecor) {
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003085 final Task task = w.getTask();
3086 if (task != null) {
3087 task.getBounds(frame);
3088 } else {
Jorim Jaggi30d64f32017-04-07 16:33:17 +02003089
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003090 // No task bounds? Too bad! Ain't no screenshot then.
3091 return true;
3092 }
Jorim Jaggi70f59482017-05-04 14:05:59 +02003093 } else {
3094 final Rect wf = w.mFrame;
3095 final Rect cr = w.mContentInsets;
3096 int left = wf.left + cr.left;
3097 int top = wf.top + cr.top;
3098 int right = wf.right - cr.right;
3099 int bottom = wf.bottom - cr.bottom;
3100 frame.union(left, top, right, bottom);
3101 w.getVisibleBounds(stackBounds);
3102 if (!Rect.intersects(frame, stackBounds)) {
3103 // Set frame empty if there's no intersection.
3104 frame.setEmpty();
3105 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003106 }
3107 }
3108
3109 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003110 (w.mAppToken != null && w.mAppToken.token == appToken)
3111 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi406386a82017-09-15 14:17:40 +02003112 if (foundTargetWs && winAnim.getShown() && winAnim.mLastAlpha > 0f) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003113 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003114 }
3115
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003116 if (w.isObscuringDisplay()){
3117 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003118 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003119 return false;
3120 }, true /* traverseTopToBottom */);
3121
3122 final WindowState appWin = mScreenshotApplicationState.appWin;
3123 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
3124 final int maxLayer = mScreenshotApplicationState.maxLayer;
3125 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003126
3127 if (appToken != null && appWin == null) {
3128 // Can't find a window to snapshot.
3129 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
3130 "Screenshot: Couldn't find a surface matching " + appToken);
3131 return null;
3132 }
3133
3134 if (!screenshotReady) {
3135 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3136 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3137 appWin.mWinAnimator.mDrawState)));
3138 return null;
3139 }
3140
3141 // Screenshot is ready to be taken. Everything from here below will continue
3142 // through the bottom of the loop and return a value. We only stay in the loop
3143 // because we don't want to release the mWindowMap lock until the screenshot is
3144 // taken.
3145
3146 if (maxLayer == 0) {
3147 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3148 + ": returning null maxLayer=" + maxLayer);
3149 return null;
3150 }
3151
Jorim Jaggi02886a82016-12-06 09:10:06 -08003152 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003153 // Constrain frame to the screen size.
3154 if (!frame.intersect(0, 0, dw, dh)) {
3155 frame.setEmpty();
3156 }
3157 } else {
3158 // Caller just wants entire display.
3159 frame.set(0, 0, dw, dh);
3160 }
3161 if (frame.isEmpty()) {
3162 return null;
3163 }
3164
3165 if (width < 0) {
3166 width = (int) (frame.width() * frameScale);
3167 }
3168 if (height < 0) {
3169 height = (int) (frame.height() * frameScale);
3170 }
3171
3172 // Tell surface flinger what part of the image to crop. Take the top
3173 // right part of the application, and crop the larger dimension to fit.
3174 Rect crop = new Rect(frame);
3175 if (width / (float) frame.width() < height / (float) frame.height()) {
3176 int cropWidth = (int)((float)width / (float)height * frame.height());
3177 crop.right = crop.left + cropWidth;
3178 } else {
3179 int cropHeight = (int)((float)height / (float)width * frame.width());
3180 crop.bottom = crop.top + cropHeight;
3181 }
3182
3183 // The screenshot API does not apply the current screen rotation.
3184 int rot = mDisplay.getRotation();
3185
3186 if (rot == ROTATION_90 || rot == ROTATION_270) {
3187 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3188 }
3189
3190 // Surfaceflinger is not aware of orientation, so convert our logical
3191 // crop to surfaceflinger's portrait orientation.
3192 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3193
3194 if (DEBUG_SCREENSHOT) {
3195 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3196 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003197 forAllWindows(w -> {
3198 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3199 Slog.i(TAG_WM, w + ": " + w.mLayer
3200 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003201 + " surfaceLayer=" + ((controller == null)
3202 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003203 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003204 }
3205
3206 final ScreenRotationAnimation screenRotationAnimation =
3207 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3208 final boolean inRotation = screenRotationAnimation != null &&
3209 screenRotationAnimation.isAnimating();
3210 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3211 "Taking screenshot while rotating");
3212
3213 // We force pending transactions to flush before taking
3214 // the screenshot by pushing an empty synchronous transaction.
3215 SurfaceControl.openTransaction();
3216 SurfaceControl.closeTransactionSync();
3217
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003218 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003219 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003220 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003221 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3222 + ") to layer " + maxLayer);
3223 return null;
3224 }
3225 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003226 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003227 }
3228
3229 // TODO: Can this use createRotationMatrix()?
3230 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3231 if (rot == Surface.ROTATION_90) {
3232 final int tmp = crop.top;
3233 crop.top = dw - crop.right;
3234 crop.right = crop.bottom;
3235 crop.bottom = dw - crop.left;
3236 crop.left = tmp;
3237 } else if (rot == Surface.ROTATION_180) {
3238 int tmp = crop.top;
3239 crop.top = dh - crop.bottom;
3240 crop.bottom = dh - tmp;
3241 tmp = crop.right;
3242 crop.right = dw - crop.left;
3243 crop.left = dw - tmp;
3244 } else if (rot == Surface.ROTATION_270) {
3245 final int tmp = crop.top;
3246 crop.top = crop.left;
3247 crop.left = dh - crop.bottom;
3248 crop.bottom = crop.right;
3249 crop.right = dh - tmp;
3250 }
3251 }
3252
3253 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003254 // Used to indicate the layout is needed.
3255 mTmpWindow = null;
3256
3257 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003258 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003259 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003260 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003261 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003262 w.setDisplayLayoutNeeded();
3263 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003264 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003265
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003266 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003267 mService.mWindowPlacerLocked.performSurfacePlacement();
3268 }
3269 }
3270
Wale Ogunwale1666e312016-12-16 11:27:18 -08003271 void setExitingTokensHasVisible(boolean hasVisible) {
3272 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3273 mExitingTokens.get(i).hasVisible = hasVisible;
3274 }
3275
3276 // Initialize state of exiting applications.
3277 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3278 }
3279
3280 void removeExistingTokensIfPossible() {
3281 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3282 final WindowToken token = mExitingTokens.get(i);
3283 if (!token.hasVisible) {
3284 mExitingTokens.remove(i);
3285 }
3286 }
3287
3288 // Time to remove any exiting applications?
3289 mTaskStackContainers.removeExistingAppTokensIfPossible();
3290 }
3291
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003292 @Override
3293 void onDescendantOverrideConfigurationChanged() {
3294 setLayoutNeeded();
3295 mService.requestTraversal();
3296 }
3297
David Stevens9440dc82017-03-16 19:00:20 -07003298 boolean okToDisplay() {
3299 if (mDisplayId == DEFAULT_DISPLAY) {
3300 return !mService.mDisplayFrozen
3301 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3302 }
3303 return mDisplayInfo.state == Display.STATE_ON;
3304 }
3305
3306 boolean okToAnimate() {
3307 return okToDisplay() &&
3308 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3309 }
3310
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003311 static final class TaskForResizePointSearchResult {
3312 boolean searchDone;
3313 Task taskForResize;
3314
3315 void reset() {
3316 searchDone = false;
3317 taskForResize = null;
3318 }
3319 }
Robert Carr3b716242016-08-16 16:02:21 -07003320
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003321 private static final class ApplySurfaceChangesTransactionState {
3322 boolean displayHasContent;
3323 boolean obscured;
3324 boolean syswin;
3325 boolean focusDisplayed;
3326 float preferredRefreshRate;
3327 int preferredModeId;
3328
3329 void reset() {
3330 displayHasContent = false;
3331 obscured = false;
3332 syswin = false;
3333 focusDisplayed = false;
3334 preferredRefreshRate = 0;
3335 preferredModeId = 0;
3336 }
3337 }
3338
3339 private static final class ScreenshotApplicationState {
3340 WindowState appWin;
3341 int maxLayer;
3342 int minLayer;
3343 boolean screenshotReady;
3344
3345 void reset(boolean screenshotReady) {
3346 appWin = null;
3347 maxLayer = 0;
3348 minLayer = 0;
3349 this.screenshotReady = screenshotReady;
3350 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3351 }
3352 }
3353
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003354 /**
3355 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3356 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3357 * homogeneous children type which is currently required by sub-classes of
3358 * {@link WindowContainer} class.
3359 */
3360 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3361
3362 int size() {
3363 return mChildren.size();
3364 }
3365
3366 E get(int index) {
3367 return mChildren.get(index);
3368 }
3369
3370 @Override
3371 boolean fillsParent() {
3372 return true;
3373 }
3374
3375 @Override
3376 boolean isVisible() {
3377 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003378 }
3379 }
3380
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003381 /**
3382 * Window container class that contains all containers on this display relating to Apps.
3383 * I.e Activities.
3384 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003385 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003386
Andrii Kulian839def92016-11-02 10:58:58 -07003387 /**
3388 * Adds the stack to this container.
3389 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3390 */
3391 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale68278562017-09-23 17:13:55 -07003392 if (stack.isActivityTypeHome()) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003393 if (mHomeStack != null) {
3394 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3395 }
3396 mHomeStack = stack;
3397 }
3398 addChild(stack, onTop);
3399 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003400 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003401
Andrii Kulian839def92016-11-02 10:58:58 -07003402 /** Removes the stack from its container and prepare for changing the parent. */
3403 void removeStackFromDisplay(TaskStack stack) {
3404 removeChild(stack);
3405 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07003406 }
3407
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003408 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003409 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3410 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003411 addChild(stack, addIndex);
3412 setLayoutNeeded();
3413 }
3414
Bryce Lee00d586d2017-07-28 20:48:43 -07003415
3416 @Override
3417 boolean isOnTop() {
3418 // Considered always on top
3419 return true;
3420 }
3421
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003422 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003423 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003424 if (child.getWindowConfiguration().isAlwaysOnTop()
3425 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003426 // This stack is always-on-top, override the default behavior.
3427 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3428
3429 // Moving to its current position, as we must call super but we don't want to
3430 // perform any meaningful action.
3431 final int currentPosition = mChildren.indexOf(child);
3432 super.positionChildAt(currentPosition, child, false /* includingParents */);
3433 return;
3434 }
3435
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003436 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3437 super.positionChildAt(targetPosition, child, includingParents);
3438
3439 setLayoutNeeded();
3440 }
3441
3442 /**
3443 * When stack is added or repositioned, find a proper position for it.
3444 * This will make sure that pinned stack always stays on top.
3445 * @param requestedPosition Position requested by caller.
3446 * @param stack Stack to be added or positioned.
3447 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3448 * @return The proper position for the stack.
3449 */
3450 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3451 final int topChildPosition = mChildren.size() - 1;
3452 boolean toTop = requestedPosition == POSITION_TOP;
3453 toTop |= adding ? requestedPosition >= topChildPosition + 1
3454 : requestedPosition >= topChildPosition;
3455 int targetPosition = requestedPosition;
3456
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003457 if (toTop && stack.getWindowingMode() != WINDOWING_MODE_PINNED
3458 && getStack(WINDOWING_MODE_PINNED) != null) {
Bryce Lee48f4b572017-04-10 10:54:15 -07003459 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003460 TaskStack topStack = mChildren.get(topChildPosition);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003461 if (topStack.getWindowingMode() != WINDOWING_MODE_PINNED) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003462 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3463 }
3464
3465 // So, stack is moved just below the pinned stack.
3466 // When we're adding a new stack the target is the current pinned stack position.
3467 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003468 // stack, because WindowContainer#positionAt() first removes element and then adds
3469 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003470 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3471 }
3472
3473 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003474 }
3475
3476 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003477 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3478 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003479 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003480 if (super.forAllWindows(callback, traverseTopToBottom)) {
3481 return true;
3482 }
3483 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3484 return true;
3485 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003486 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003487 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3488 return true;
3489 }
3490 if (super.forAllWindows(callback, traverseTopToBottom)) {
3491 return true;
3492 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003493 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003494 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003495 }
3496
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003497 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003498 boolean traverseTopToBottom) {
3499 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3500 // app tokens.
3501 // TODO: Investigate if we need to continue to do this or if we can just process them
3502 // in-order.
3503 if (traverseTopToBottom) {
3504 for (int i = mChildren.size() - 1; i >= 0; --i) {
3505 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3506 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003507 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3508 traverseTopToBottom)) {
3509 return true;
3510 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003511 }
3512 }
3513 } else {
3514 final int count = mChildren.size();
3515 for (int i = 0; i < count; ++i) {
3516 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3517 final int appTokensCount = appTokens.size();
3518 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003519 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3520 traverseTopToBottom)) {
3521 return true;
3522 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003523 }
3524 }
3525 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003526 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003527 }
3528
Wale Ogunwale1666e312016-12-16 11:27:18 -08003529 void setExitingTokensHasVisible(boolean hasVisible) {
3530 for (int i = mChildren.size() - 1; i >= 0; --i) {
3531 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3532 for (int j = appTokens.size() - 1; j >= 0; --j) {
3533 appTokens.get(j).hasVisible = hasVisible;
3534 }
3535 }
3536 }
3537
3538 void removeExistingAppTokensIfPossible() {
3539 for (int i = mChildren.size() - 1; i >= 0; --i) {
3540 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3541 for (int j = appTokens.size() - 1; j >= 0; --j) {
3542 final AppWindowToken token = appTokens.get(j);
3543 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3544 && (!token.mIsExiting || token.isEmpty())) {
3545 // Make sure there is no animation running on this token, so any windows
3546 // associated with it will be removed as soon as their animations are
3547 // complete.
3548 token.mAppAnimator.clearAnimation();
3549 token.mAppAnimator.animating = false;
3550 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3551 "performLayout: App token exiting now removed" + token);
3552 token.removeIfPossible();
3553 }
3554 }
3555 }
3556 }
3557
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003558 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003559 int getOrientation() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003560 if (isStackVisible(DOCKED_STACK_ID) || isStackVisible(FREEFORM_WORKSPACE_STACK_ID)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003561 // Apps and their containers are not allowed to specify an orientation while the
3562 // docked or freeform stack is visible...except for the home stack/task if the
3563 // docked stack is minimized and it actually set something.
3564 if (mHomeStack != null && mHomeStack.isVisible()
3565 && mDividerControllerLocked.isMinimizedDock()) {
3566 final int orientation = mHomeStack.getOrientation();
3567 if (orientation != SCREEN_ORIENTATION_UNSET) {
3568 return orientation;
3569 }
3570 }
3571 return SCREEN_ORIENTATION_UNSPECIFIED;
3572 }
3573
3574 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003575 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3576 PackageManager.FEATURE_AUTOMOTIVE);
3577 if (isCar) {
3578 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3579 // allow anything but the default orientation.
3580 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3581 "Forcing UNSPECIFIED orientation in car. Ignoring " + orientation);
3582 return SCREEN_ORIENTATION_UNSPECIFIED;
3583 }
3584
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003585 if (orientation != SCREEN_ORIENTATION_UNSET
3586 && orientation != SCREEN_ORIENTATION_BEHIND) {
3587 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3588 "App is requesting an orientation, return " + orientation);
3589 return orientation;
3590 }
3591
3592 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003593 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003594 // The next app has not been requested to be visible, so we keep the current orientation
3595 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003596 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003597 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003598 }
3599
3600 /**
3601 * Window container class that contains all containers on this display that are not related to
3602 * Apps. E.g. status bar.
3603 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003604 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3605 /**
3606 * Compares two child window tokens returns -1 if the first is lesser than the second in
3607 * terms of z-order and 1 otherwise.
3608 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003609 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003610 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003611 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3612 token1.mOwnerCanManageAppTokens)
3613 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3614 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003615
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003616 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3617 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3618 return false;
3619 }
3620 final int req = w.mAttrs.screenOrientation;
3621 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3622 || req == SCREEN_ORIENTATION_UNSET) {
3623 return false;
3624 }
3625 return true;
3626 };
3627
Wale Ogunwale3a931692016-11-02 16:49:48 -07003628 private final String mName;
3629 NonAppWindowContainers(String name) {
3630 mName = name;
3631 }
3632
3633 void addChild(WindowToken token) {
3634 addChild(token, mWindowComparator);
3635 }
3636
3637 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003638 int getOrientation() {
3639 final WindowManagerPolicy policy = mService.mPolicy;
3640 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003641 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003642
3643 if (win != null) {
3644 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003645 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003646 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003647 if (mService.mKeyguardGoingAway) {
3648 // Keyguard can't affect the orientation if it is going away...
3649 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3650 return SCREEN_ORIENTATION_UNSET;
3651 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003652 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003653 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003654 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003655 }
3656
Andrii Kulian8ee72852017-03-10 10:36:45 -08003657 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003658
Jorim Jaggi75520d52017-08-24 17:23:19 +02003659 if (policy.isKeyguardShowingAndNotOccluded()
3660 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003661 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003662 }
3663
3664 return SCREEN_ORIENTATION_UNSET;
3665 }
3666
3667 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003668 String getName() {
3669 return mName;
3670 }
Robert Carr3b716242016-08-16 16:02:21 -07003671 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003672
3673 /**
3674 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3675 */
3676 @FunctionalInterface
3677 private interface Screenshoter<E> {
3678 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3679 boolean useIdentityTransform, int rotation);
3680 }
Craig Mautner59c00972012-07-30 12:10:24 -07003681}