blob: 036e803ef9756d99dd73fe7f0e12657a9d82f8d9 [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 Ogunwaleb62139d2017-09-20 15:37:35 -070019import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070020import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwale687b4272017-07-27 02:56:23 -070021import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070022import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
23import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070024import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070025import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
26import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale687b4272017-07-27 02:56:23 -070027import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
28import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070029import static android.view.Display.DEFAULT_DISPLAY;
30import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070031import static android.view.Surface.ROTATION_0;
32import static android.view.Surface.ROTATION_180;
33import static android.view.Surface.ROTATION_270;
34import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070035import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070036import static android.view.WindowManager.DOCKED_BOTTOM;
37import static android.view.WindowManager.DOCKED_INVALID;
38import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080039import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
40import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
41import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070042import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070043import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070044import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
45import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070046import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070047import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070048import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070049import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Wale Ogunwale494009b82016-10-21 09:01:38 -070050import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070051import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070052import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
53import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070054import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070055import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
56import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070057import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070058import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070059import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070060import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
61import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070062import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Jorim Jaggi75520d52017-08-24 17:23:19 +020063import static com.android.server.wm.AppTransition.TRANSIT_KEYGUARD_UNOCCLUDE;
Wale Ogunwale1666e312016-12-16 11:27:18 -080064import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070065import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070066import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070067import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
68import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070069import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwalec69694a2016-10-18 13:51:15 -070070import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070076import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080077import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070078import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070079import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070080import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070081import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070082import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070083import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080084import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -070085import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070086import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
87import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070088import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070089import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -070090import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
91import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070092import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER;
93import static com.android.server.wm.WindowManagerService.TYPE_LAYER_OFFSET;
94import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -070095import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070096import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070097import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070098import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070099import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700100import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700101import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700102import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700103import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700104import static com.android.server.wm.proto.DisplayProto.ABOVE_APP_WINDOWS;
105import static com.android.server.wm.proto.DisplayProto.BELOW_APP_WINDOWS;
106import static com.android.server.wm.proto.DisplayProto.DISPLAY_INFO;
107import static com.android.server.wm.proto.DisplayProto.DOCKED_STACK_DIVIDER_CONTROLLER;
108import static com.android.server.wm.proto.DisplayProto.DPI;
109import static com.android.server.wm.proto.DisplayProto.ID;
110import static com.android.server.wm.proto.DisplayProto.IME_WINDOWS;
111import static com.android.server.wm.proto.DisplayProto.PINNED_STACK_CONTROLLER;
Steven Timotiusf2d68892017-08-28 17:00:01 -0700112import static com.android.server.wm.proto.DisplayProto.ROTATION;
113import static com.android.server.wm.proto.DisplayProto.SCREEN_ROTATION_ANIMATION;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700114import static com.android.server.wm.proto.DisplayProto.STACKS;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700115import static com.android.server.wm.proto.DisplayProto.WINDOW_CONTAINER;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700116
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700117import android.annotation.CallSuper;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700118import android.annotation.NonNull;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700119import android.app.ActivityManager.StackId;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700120import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700121import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700122import android.graphics.Bitmap;
Jorim Jaggi6a7a8592017-01-12 00:44:33 +0100123import android.graphics.GraphicBuffer;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700124import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800125import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700126import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700127import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100128import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700129import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700130import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700131import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700132import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700133import android.os.RemoteException;
134import android.os.SystemClock;
Chong Zhang8e89b312015-09-09 15:09:30 -0700135import android.util.DisplayMetrics;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800136import android.util.MutableBoolean;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700137import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700138import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700139import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700140import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700141import android.view.InputDevice;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700142import android.view.Surface;
143import android.view.SurfaceControl;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700144import android.view.WindowManagerPolicy;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700145
Bryce Lee48f4b572017-04-10 10:54:15 -0700146import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800147import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700148import com.android.internal.view.IInputMethodClient;
Craig Mautner59c00972012-07-30 12:10:24 -0700149
Robert Carr3b716242016-08-16 16:02:21 -0700150import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700151import java.io.PrintWriter;
152import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700153import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700154import java.util.HashMap;
155import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700156import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700157import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800158import java.util.function.Consumer;
159import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700160
Craig Mautner59c00972012-07-30 12:10:24 -0700161/**
162 * Utility class for keeping track of the WindowStates and other pertinent contents of a
163 * particular Display.
164 *
165 * IMPORTANT: No method from this class should ever be used without holding
166 * WindowManagerService.mWindowMap.
167 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700168class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700169 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700170
171 /** Unique identifier of this stack. */
172 private final int mDisplayId;
173
Wale Ogunwale3a931692016-11-02 16:49:48 -0700174 /** The containers below are the only child containers the display can have. */
175 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700176 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700177 // Contains all non-app window containers that should be displayed above the app containers
178 // (e.g. Status bar)
179 private final NonAppWindowContainers mAboveAppWindowsContainers =
180 new NonAppWindowContainers("mAboveAppWindowsContainers");
181 // Contains all non-app window containers that should be displayed below the app containers
182 // (e.g. Wallpaper).
183 private final NonAppWindowContainers mBelowAppWindowsContainers =
184 new NonAppWindowContainers("mBelowAppWindowsContainers");
185 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
186 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
187 // window containers together and move them in-sync if/when needed.
188 private final NonAppWindowContainers mImeWindowsContainers =
189 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700190
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000191 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800192 private WindowState mTmpWindow2;
193 private WindowAnimator mTmpWindowAnimator;
194 private boolean mTmpRecoveringMemory;
195 private boolean mUpdateImeTarget;
196 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700197 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700198
Wale Ogunwale02319a62016-09-26 15:21:22 -0700199 // Mapping from a token IBinder to a WindowToken object on this display.
200 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
201
Andrii Kuliancd097992017-03-23 18:31:59 -0700202 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700203 int mInitialDisplayWidth = 0;
204 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700205 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700206
207 /**
208 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
209 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
210 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
211 */
Craig Mautner59c00972012-07-30 12:10:24 -0700212 int mBaseDisplayWidth = 0;
213 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700214 /**
215 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
216 * but can be set from Settings or via shell command "adb shell wm density".
217 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
218 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700219 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700220 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700221 private final DisplayInfo mDisplayInfo = new DisplayInfo();
222 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700223 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Andrii Kulian06d07d62017-03-14 11:11:47 -0700224 /**
225 * For default display it contains real metrics, empty for others.
226 * @see WindowManagerService#createWatermarkInTransaction()
227 */
228 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
229 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
230 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700231
Andrii Kulian06d07d62017-03-14 11:11:47 -0700232 /**
233 * Compat metrics computed based on {@link #mDisplayMetrics}.
234 * @see #updateDisplayAndOrientation(int)
235 */
236 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
237
238 /** The desired scaling factor for compatible apps. */
239 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700240
Andrii Kulian8ee72852017-03-10 10:36:45 -0800241 /**
242 * Current rotation of the display.
243 * Constants as per {@link android.view.Surface.Rotation}.
244 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700245 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800246 */
247 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700248
Andrii Kulian8ee72852017-03-10 10:36:45 -0800249 /**
250 * Last applied orientation of the display.
251 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
252 *
253 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
254 */
255 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700256
Andrii Kulian8ee72852017-03-10 10:36:45 -0800257 /**
258 * Flag indicating that the application is receiving an orientation that has different metrics
259 * than it expected. E.g. Portrait instead of Landscape.
260 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700261 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800262 */
263 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700264
Andrii Kulian8ee72852017-03-10 10:36:45 -0800265 /**
266 * Orientation forced by some window. If there is no visible window that specifies orientation
267 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
268 *
269 * @see NonAppWindowContainers#getOrientation()
270 */
271 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700272
Andrii Kulian8ee72852017-03-10 10:36:45 -0800273 /**
274 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
275 * occluded.
276 *
277 * @see NonAppWindowContainers#getOrientation()
278 */
279 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
280
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700281 /**
282 * Keep track of wallpaper visibility to notify changes.
283 */
284 private boolean mLastWallpaperVisible = false;
285
Andrii Kulian06d07d62017-03-14 11:11:47 -0700286 private Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700287 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700288
Craig Mautner39834192012-09-02 07:47:24 -0700289 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700290 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700291 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700292 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800293 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700294
Craig Mautnerdc548482014-02-05 13:35:24 -0800295 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700296 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800297
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800298 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
299 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700300 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700301
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700302 /** Detect user tapping outside of current focused task bounds .*/
303 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700304
Craig Mautner6601b7b2013-04-29 10:29:11 -0700305 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700306 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700307
Craig Mautner6601b7b2013-04-29 10:29:11 -0700308 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800309 private final Rect mTmpRect = new Rect();
310 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700311 private final RectF mTmpRectF = new RectF();
312 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800313 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700314
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800315 WindowManagerService mService;
Craig Mautner9d808b12013-08-06 18:00:25 -0700316
Craig Mautner95da1082014-02-24 17:54:35 -0800317 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700318 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800319
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700320 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700321 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700322
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800323 DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700324
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800325 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
326
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000327 private boolean mHaveBootMsg = false;
328 private boolean mHaveApp = false;
329 private boolean mHaveWallpaper = false;
330 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700331
332 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
333
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700334 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
335 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000336 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
337 new ApplySurfaceChangesTransactionState();
338 private final ScreenshotApplicationState mScreenshotApplicationState =
339 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700340
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700341 // True if this display is in the process of being removed. Used to determine if the removal of
342 // the display's direct children should be allowed.
343 private boolean mRemovingDisplay = false;
344
Bryce Leed1871262017-06-12 14:12:29 -0700345 // {@code false} if this display is in the processing of being created.
346 private boolean mDisplayReady = false;
347
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700348 private final WindowLayersController mLayersController;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800349 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700350 int mInputMethodAnimLayerAdjustment;
351
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800352 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
353 WindowStateAnimator winAnimator = w.mWinAnimator;
354 if (winAnimator.hasSurface()) {
355 final boolean wasAnimating = winAnimator.mWasAnimating;
356 final boolean nowAnimating = winAnimator.stepAnimationLocked(
357 mTmpWindowAnimator.mCurrentTime);
358 winAnimator.mWasAnimating = nowAnimating;
359 mTmpWindowAnimator.orAnimating(nowAnimating);
360
361 if (DEBUG_WALLPAPER) Slog.v(TAG,
362 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
363
364 if (wasAnimating && !winAnimator.mAnimating
365 && mWallpaperController.isWallpaperTarget(w)) {
366 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
367 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
368 if (DEBUG_LAYOUT_REPEATS) {
369 mService.mWindowPlacerLocked.debugLayoutRepeats(
370 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
371 }
372 }
373 }
374
375 final AppWindowToken atoken = w.mAppToken;
376 if (winAnimator.mDrawState == READY_TO_SHOW) {
377 if (atoken == null || atoken.allDrawn) {
378 if (w.performShowLocked()) {
379 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
380 if (DEBUG_LAYOUT_REPEATS) {
381 mService.mWindowPlacerLocked.debugLayoutRepeats(
382 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
383 }
384 }
385 }
386 }
387 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
388 if (appAnimator != null && appAnimator.thumbnail != null) {
389 if (appAnimator.thumbnailTransactionSeq
390 != mTmpWindowAnimator.mAnimTransactionSequence) {
391 appAnimator.thumbnailTransactionSeq =
392 mTmpWindowAnimator.mAnimTransactionSequence;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800393 }
394 }
395 };
396
397 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
398 final WindowStateAnimator winAnimator = w.mWinAnimator;
399 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
400 return;
401 }
402
403 final int flags = w.mAttrs.flags;
404
405 // If this window is animating, make a note that we have an animating window and take
406 // care of a request to run a detached wallpaper animation.
407 if (winAnimator.mAnimating) {
408 if (winAnimator.mAnimation != null) {
409 if ((flags & FLAG_SHOW_WALLPAPER) != 0
410 && winAnimator.mAnimation.getDetachWallpaper()) {
411 mTmpWindow = w;
412 }
413 final int color = winAnimator.mAnimation.getBackgroundColor();
414 if (color != 0) {
415 final TaskStack stack = w.getStack();
416 if (stack != null) {
417 stack.setAnimationBackground(winAnimator, color);
418 }
419 }
420 }
421 mTmpWindowAnimator.setAnimating(true);
422 }
423
424 // If this window's app token is running a detached wallpaper animation, make a note so
425 // we can ensure the wallpaper is displayed behind it.
426 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
427 if (appAnimator != null && appAnimator.animation != null
428 && appAnimator.animating) {
429 if ((flags & FLAG_SHOW_WALLPAPER) != 0
430 && appAnimator.animation.getDetachWallpaper()) {
431 mTmpWindow = w;
432 }
433
434 final int color = appAnimator.animation.getBackgroundColor();
435 if (color != 0) {
436 final TaskStack stack = w.getStack();
437 if (stack != null) {
438 stack.setAnimationBackground(winAnimator, color);
439 }
440 }
441 }
442 };
443
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800444 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
445 final int lostFocusUid = mTmpWindow.mOwnerUid;
446 final Handler handler = mService.mH;
447 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
448 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
449 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
450 w.mAttrs.hideTimeoutMilliseconds);
451 }
452 }
453 };
454
455 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
456 final AppWindowToken focusedApp = mService.mFocusedApp;
457 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
458 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
459
460 if (!w.canReceiveKeys()) {
461 return false;
462 }
463
464 final AppWindowToken wtoken = w.mAppToken;
465
466 // If this window's application has been removed, just skip it.
467 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
468 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
469 + (wtoken.removed ? "removed" : "sendingToBottom"));
470 return false;
471 }
472
473 if (focusedApp == null) {
474 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
475 + " using new focus @ " + w);
476 mTmpWindow = w;
477 return true;
478 }
479
480 if (!focusedApp.windowsAreFocusable()) {
481 // Current focused app windows aren't focusable...
482 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
483 + " focusable using new focus @ " + w);
484 mTmpWindow = w;
485 return true;
486 }
487
488 // Descend through all of the app tokens and find the first that either matches
489 // win.mAppToken (return win) or mFocusedApp (return null).
490 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
491 if (focusedApp.compareTo(wtoken) > 0) {
492 // App stack below focused app stack. No focus for you!!!
493 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
494 "findFocusedWindow: Reached focused app=" + focusedApp);
495 mTmpWindow = null;
496 return true;
497 }
498 }
499
500 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
501 mTmpWindow = w;
502 return true;
503 };
504
505 private final Consumer<WindowState> mPrepareWindowSurfaces =
506 w -> w.mWinAnimator.prepareSurfaceLocked(true);
507
508 private final Consumer<WindowState> mPerformLayout = w -> {
509 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
510 // wasting time and funky changes while a window is animating away.
511 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
512 || w.isGoneForLayoutLw();
513
514 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
515 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
516 + " mLayoutAttached=" + w.mLayoutAttached
517 + " screen changed=" + w.isConfigChanged());
518 final AppWindowToken atoken = w.mAppToken;
519 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
520 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
521 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
522 + " parentHidden=" + w.isParentWindowHidden());
523 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
524 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
525 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
526 + " parentHidden=" + w.isParentWindowHidden());
527 }
528
529 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
530 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
531 // since that means "perform layout as normal, just don't display").
532 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
533 || ((w.isConfigChanged() || w.setReportResizeHints())
534 && !w.isGoneForLayoutLw() &&
535 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
536 (w.mHasSurface && w.mAppToken != null &&
537 w.mAppToken.layoutConfigChanges)))) {
538 if (!w.mLayoutAttached) {
539 if (mTmpInitial) {
540 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
541 w.mContentChanged = false;
542 }
543 if (w.mAttrs.type == TYPE_DREAM) {
544 // Don't layout windows behind a dream, so that if it does stuff like hide
545 // the status bar we won't get a bad transition when it goes away.
546 mTmpWindow = w;
547 }
548 w.mLayoutNeeded = false;
549 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200550 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800551 mService.mPolicy.layoutWindowLw(w, null);
552 w.mLayoutSeq = mService.mLayoutSeq;
553
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200554 // If this is the first layout, we need to initialize the last inset values as
555 // otherwise we'd immediately cause an unnecessary resize.
556 if (firstLayout) {
557 w.updateLastInsetValues();
558 }
559
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800560 // Window frames may have changed. Update dim layer with the new bounds.
561 final Task task = w.getTask();
562 if (task != null) {
563 mDimLayerController.updateDimLayer(task);
564 }
565
566 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
567 + " mContainingFrame=" + w.mContainingFrame
568 + " mDisplayFrame=" + w.mDisplayFrame);
569 }
570 }
571 };
572
573 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
574 if (w.mLayoutAttached) {
575 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
576 + " mViewVisibility=" + w.mViewVisibility
577 + " mRelayoutCalled=" + w.mRelayoutCalled);
578 // If this view is GONE, then skip it -- keep the current frame, and let the caller
579 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
580 // windows, since that means "perform layout as normal, just don't display").
581 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
582 return;
583 }
584 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
585 || w.mLayoutNeeded) {
586 if (mTmpInitial) {
587 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
588 w.mContentChanged = false;
589 }
590 w.mLayoutNeeded = false;
591 w.prelayout();
592 mService.mPolicy.layoutWindowLw(w, w.getParentWindow());
593 w.mLayoutSeq = mService.mLayoutSeq;
594 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
595 + " mContainingFrame=" + w.mContainingFrame
596 + " mDisplayFrame=" + w.mDisplayFrame);
597 }
598 } else if (w.mAttrs.type == TYPE_DREAM) {
599 // Don't layout windows behind a dream, so that if it does stuff like hide the
600 // status bar we won't get a bad transition when it goes away.
601 mTmpWindow = mTmpWindow2;
602 }
603 };
604
605 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
606 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
607 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
608 return w.canBeImeTarget();
609 };
610
611 private final Consumer<WindowState> mApplyPostLayoutPolicy =
612 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
613 mService.mInputMethodTarget);
614
615 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
616 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
617 final boolean obscuredChanged = w.mObscured !=
618 mTmpApplySurfaceChangesTransactionState.obscured;
619 final RootWindowContainer root = mService.mRoot;
620 // Only used if default window
621 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
622
623 // Update effect.
624 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
625 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
626 final boolean isDisplayed = w.isDisplayedLw();
627
628 if (isDisplayed && w.isObscuringDisplay()) {
629 // This window completely covers everything behind it, so we want to leave all
630 // of them as undimmed (for performance reasons).
631 root.mObscuringWindow = w;
632 mTmpApplySurfaceChangesTransactionState.obscured = true;
633 }
634
635 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
636 root.handleNotObscuredLocked(w,
637 mTmpApplySurfaceChangesTransactionState.obscured,
638 mTmpApplySurfaceChangesTransactionState.syswin);
639
640 if (w.mHasSurface && isDisplayed) {
641 final int type = w.mAttrs.type;
642 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
643 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
644 mTmpApplySurfaceChangesTransactionState.syswin = true;
645 }
646 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
647 && w.mAttrs.preferredRefreshRate != 0) {
648 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
649 = w.mAttrs.preferredRefreshRate;
650 }
651 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
652 && w.mAttrs.preferredDisplayModeId != 0) {
653 mTmpApplySurfaceChangesTransactionState.preferredModeId
654 = w.mAttrs.preferredDisplayModeId;
655 }
656 }
657 }
658
659 w.applyDimLayerIfNeeded();
660
661 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
662 && mWallpaperController.isWallpaperTarget(w)) {
663 // This is the wallpaper target and its obscured state changed... make sure the
664 // current wallpaper's visibility has been updated accordingly.
665 mWallpaperController.updateWallpaperVisibility();
666 }
667
668 w.handleWindowMovedIfNeeded();
669
670 final WindowStateAnimator winAnimator = w.mWinAnimator;
671
672 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
673 w.mContentChanged = false;
674
675 // Moved from updateWindowsAndWallpaperLocked().
676 if (w.mHasSurface) {
677 // Take care of the window being ready to display.
678 final boolean committed = winAnimator.commitFinishDrawingLocked();
679 if (isDefaultDisplay && committed) {
680 if (w.mAttrs.type == TYPE_DREAM) {
681 // HACK: When a dream is shown, it may at that point hide the lock screen.
682 // So we need to redo the layout to let the phone window manager make this
683 // happen.
684 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
685 if (DEBUG_LAYOUT_REPEATS) {
686 surfacePlacer.debugLayoutRepeats(
687 "dream and commitFinishDrawingLocked true",
688 pendingLayoutChanges);
689 }
690 }
691 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
692 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
693 "First draw done in potential wallpaper target " + w);
694 root.mWallpaperMayChange = true;
695 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
696 if (DEBUG_LAYOUT_REPEATS) {
697 surfacePlacer.debugLayoutRepeats(
698 "wallpaper and commitFinishDrawingLocked true",
699 pendingLayoutChanges);
700 }
701 }
702 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700703 final TaskStack stack = w.getStack();
704 if ((!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening())
705 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800706 // Updates the shown frame before we set up the surface. This is needed
707 // because the resizing could change the top-left position (in addition to
708 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
709 // position the surface.
710 //
711 // If an animation is being started, we can't call this method because the
712 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700713 // we do want to update the position if the window is animating. We make an exception
714 // for the bounds animating state, where an application may have been waiting
715 // for an exit animation to start, but instead enters PiP. We need to ensure
716 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800717 winAnimator.computeShownFrameLocked();
718 }
719 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
720 }
721
722 final AppWindowToken atoken = w.mAppToken;
723 if (atoken != null) {
724 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
725 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
726 mTmpUpdateAllDrawn.add(atoken);
727 }
728 }
729
730 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
731 && w.isDisplayedLw()) {
732 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
733 }
734
735 w.updateResizingWindowIfNeeded();
736 };
737
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800738 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800739 * Create new {@link DisplayContent} instance, add itself to the root window container and
740 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700741 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800742 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700743 * @param layersController window layer controller used to assign layer to the windows on this
744 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700745 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
746 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700747 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700748 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700749 WindowLayersController layersController, WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800750 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
751 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
752 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
753 + " new=" + display);
754 }
755
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700756 mDisplay = display;
757 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700758 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700759 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700760 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700761 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700762 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700763 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700764 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800765 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700766 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800767 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700768
769 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700770 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700771 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700772 super.addChild(mAboveAppWindowsContainers, null);
773 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800774
775 // Add itself as a child to the root container.
776 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700777
778 // TODO(b/62541591): evaluate whether this is the best spot to declare the
779 // {@link DisplayContent} ready for use.
780 mDisplayReady = true;
781 }
782
783 boolean isReady() {
784 // The display is ready when the system and the individual display are both ready.
785 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700786 }
787
788 int getDisplayId() {
789 return mDisplayId;
790 }
791
Wale Ogunwale02319a62016-09-26 15:21:22 -0700792 WindowToken getWindowToken(IBinder binder) {
793 return mTokenMap.get(binder);
794 }
795
796 AppWindowToken getAppWindowToken(IBinder binder) {
797 final WindowToken token = getWindowToken(binder);
798 if (token == null) {
799 return null;
800 }
801 return token.asAppWindowToken();
802 }
803
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700804 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700805 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
806 if (dc != null) {
807 // We currently don't support adding a window token to the display if the display
808 // already has the binder mapped to another token. If there is a use case for supporting
809 // this moving forward we will either need to merge the WindowTokens some how or have
810 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700811 throw new IllegalArgumentException("Can't map token=" + token + " to display="
812 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700813 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700814 if (binder == null) {
815 throw new IllegalArgumentException("Can't map token=" + token + " to display="
816 + getName() + " binder is null");
817 }
818 if (token == null) {
819 throw new IllegalArgumentException("Can't map null token to display="
820 + getName() + " binder=" + binder);
821 }
822
Wale Ogunwale02319a62016-09-26 15:21:22 -0700823 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700824
825 if (token.asAppWindowToken() == null) {
826 // Add non-app token to container hierarchy on the display. App tokens are added through
827 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700828 switch (token.windowType) {
829 case TYPE_WALLPAPER:
830 mBelowAppWindowsContainers.addChild(token);
831 break;
832 case TYPE_INPUT_METHOD:
833 case TYPE_INPUT_METHOD_DIALOG:
834 mImeWindowsContainers.addChild(token);
835 break;
836 default:
837 mAboveAppWindowsContainers.addChild(token);
838 break;
839 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700840 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700841 }
842
843 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700844 final WindowToken token = mTokenMap.remove(binder);
845 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800846 token.setExiting();
847 }
848 return token;
849 }
850
851 /** Changes the display the input window token is housed on to this one. */
852 void reParentWindowToken(WindowToken token) {
853 final DisplayContent prevDc = token.getDisplayContent();
854 if (prevDc == this) {
855 return;
856 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800857 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
858 && token.asAppWindowToken() == null) {
859 // Removed the token from the map, but made sure it's not an app token before removing
860 // from parent.
861 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700862 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800863
864 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700865 }
866
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700867 void removeAppToken(IBinder binder) {
868 final WindowToken token = removeWindowToken(binder);
869 if (token == null) {
870 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
871 return;
872 }
873
874 final AppWindowToken appToken = token.asAppWindowToken();
875
876 if (appToken == null) {
877 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
878 return;
879 }
880
881 appToken.onRemovedFromDisplay();
882 }
883
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700884 Display getDisplay() {
885 return mDisplay;
886 }
887
Craig Mautner59c00972012-07-30 12:10:24 -0700888 DisplayInfo getDisplayInfo() {
889 return mDisplayInfo;
890 }
891
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700892 DisplayMetrics getDisplayMetrics() {
893 return mDisplayMetrics;
894 }
895
Andrii Kulian8ee72852017-03-10 10:36:45 -0800896 int getRotation() {
897 return mRotation;
898 }
899
900 void setRotation(int newRotation) {
901 mRotation = newRotation;
902 }
903
904 int getLastOrientation() {
905 return mLastOrientation;
906 }
907
908 void setLastOrientation(int orientation) {
909 mLastOrientation = orientation;
910 }
911
912 boolean getAltOrientation() {
913 return mAltOrientation;
914 }
915
916 void setAltOrientation(boolean altOrientation) {
917 mAltOrientation = altOrientation;
918 }
919
920 int getLastWindowForcedOrientation() {
921 return mLastWindowForcedOrientation;
922 }
923
Andrii Kulian06d07d62017-03-14 11:11:47 -0700924 /**
925 * Update rotation of the display.
926 *
927 * Returns true if the rotation has been changed. In this case YOU MUST CALL
928 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
929 */
930 boolean updateRotationUnchecked(boolean inTransaction) {
931 if (mService.mDeferredRotationPauseCount > 0) {
932 // Rotation updates have been paused temporarily. Defer the update until
933 // updates have been resumed.
934 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
935 return false;
936 }
937
938 ScreenRotationAnimation screenRotationAnimation =
939 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
940 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
941 // Rotation updates cannot be performed while the previous rotation change
942 // animation is still in progress. Skip this update. We will try updating
943 // again after the animation is finished and the display is unfrozen.
944 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
945 return false;
946 }
947 if (mService.mDisplayFrozen) {
948 // Even if the screen rotation animation has finished (e.g. isAnimating
949 // returns false), there is still some time where we haven't yet unfrozen
950 // the display. We also need to abort rotation here.
951 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
952 "Deferring rotation, still finishing previous rotation");
953 return false;
954 }
955
956 if (!mService.mDisplayEnabled) {
957 // No point choosing a rotation if the display is not enabled.
958 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
959 return false;
960 }
961
962 final int oldRotation = mRotation;
963 final int lastOrientation = mLastOrientation;
964 final boolean oldAltOrientation = mAltOrientation;
965 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr0e007272017-10-02 13:00:55 -0700966 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
Robert Carr897215d2017-03-29 12:25:50 -0700967 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700968
Robert Carr0e007272017-10-02 13:00:55 -0700969 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700970 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
971 if (seamlessRotated != null) {
972 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
973 // to complete (that is, waiting for windows to redraw). It's tempting to check
974 // w.mSeamlessRotationCount but that could be incorrect in the case of
975 // window-removal.
976 return false;
977 }
Robert Carr0e007272017-10-02 13:00:55 -0700978
979 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700980 // windows we unfortunately can not seamlessly rotate.
981 if (getStack(WINDOWING_MODE_PINNED) != null) {
Robert Carr0e007272017-10-02 13:00:55 -0700982 mayRotateSeamlessly = false;
983 }
984 for (int i = 0; i < mService.mSessions.size(); i++) {
985 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
986 mayRotateSeamlessly = false;
987 break;
988 }
989 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700990 }
Robert Carr0e007272017-10-02 13:00:55 -0700991 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700992
993 // TODO: Implement forced rotation changes.
994 // Set mAltOrientation to indicate that the application is receiving
995 // an orientation that has different metrics than it expected.
996 // eg. Portrait instead of Landscape.
997
998 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
999 lastOrientation, rotation);
1000
1001 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
1002 + ", got rotation " + rotation + " which has "
1003 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1004
1005 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1006 // No change.
1007 return false;
1008 }
1009
1010 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
1011 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1012 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1013
1014 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1015 mService.mWaitingForConfig = true;
1016 }
1017
1018 mRotation = rotation;
1019 mAltOrientation = altOrientation;
1020 if (isDefaultDisplay) {
1021 mService.mPolicy.setRotationLw(rotation);
1022 }
1023
1024 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1025 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1026 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1027 WINDOW_FREEZE_TIMEOUT_DURATION);
1028
1029 setLayoutNeeded();
1030 final int[] anim = new int[2];
1031 if (isDimming()) {
1032 anim[0] = anim[1] = 0;
1033 } else {
1034 mService.mPolicy.selectRotationAnimationLw(anim);
1035 }
1036
1037 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001038 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001039 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1040 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1041 mDisplayId);
1042 } else {
1043 // The screen rotation animation uses a screenshot to freeze the screen
1044 // while windows resize underneath.
1045 // When we are rotating seamlessly, we allow the elements to transition
1046 // to their rotated state independently and without a freeze required.
1047 screenRotationAnimation = null;
1048
1049 // We have to reset this in case a window was removed before it
1050 // finished seamless rotation.
1051 mService.mSeamlessRotationCount = 0;
1052 }
1053
1054 // We need to update our screen size information to match the new rotation. If the rotation
1055 // has actually changed then this method will return true and, according to the comment at
1056 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1057 // By updating the Display info here it will be available to
1058 // #computeScreenConfiguration() later.
1059 updateDisplayAndOrientation(getConfiguration().uiMode);
1060
1061 if (!inTransaction) {
1062 if (SHOW_TRANSACTIONS) {
1063 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1064 }
1065 mService.openSurfaceTransaction();
1066 }
1067 try {
1068 // NOTE: We disable the rotation in the emulator because
1069 // it doesn't support hardware OpenGL emulation yet.
1070 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1071 && screenRotationAnimation.hasScreenshot()) {
1072 if (screenRotationAnimation.setRotationInTransaction(
1073 rotation, mService.mFxSession,
1074 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1075 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1076 mService.scheduleAnimationLocked();
1077 }
1078 }
1079
1080 if (rotateSeamlessly) {
1081 forAllWindows(w -> {
1082 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1083 }, true /* traverseTopToBottom */);
1084 }
1085
1086 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1087 } finally {
1088 if (!inTransaction) {
1089 mService.closeSurfaceTransaction();
1090 if (SHOW_LIGHT_TRANSACTIONS) {
1091 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1092 }
1093 }
1094 }
1095
1096 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001097 if (w.mHasSurface && !rotateSeamlessly) {
1098 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001099 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001100 mService.mRoot.mOrientationChangeComplete = false;
1101 w.mLastFreezeDuration = 0;
1102 }
1103 w.mReportOrientationChanged = true;
1104 }, true /* traverseTopToBottom */);
1105
1106 if (rotateSeamlessly) {
1107 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1108 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1109 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1110 }
1111
1112 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1113 final WindowManagerService.RotationWatcher rotationWatcher
1114 = mService.mRotationWatchers.get(i);
1115 if (rotationWatcher.mDisplayId == mDisplayId) {
1116 try {
1117 rotationWatcher.mWatcher.onRotationChanged(rotation);
1118 } catch (RemoteException e) {
1119 // Ignore
1120 }
1121 }
1122 }
1123
1124 // TODO (multi-display): Magnification is supported only for the default display.
1125 // Announce rotation only if we will not animate as we already have the
1126 // windows in final state. Otherwise, we make this call at the rotation end.
1127 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1128 && isDefaultDisplay) {
1129 mService.mAccessibilityController.onRotationChangedLocked(this);
1130 }
1131
1132 return true;
1133 }
1134
1135 /**
1136 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1137 * changed.
1138 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1139 */
1140 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1141 // Use the effective "visual" dimensions based on current rotation
1142 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1143 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1144 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1145 int dw = realdw;
1146 int dh = realdh;
1147
1148 if (mAltOrientation) {
1149 if (realdw > realdh) {
1150 // Turn landscape into portrait.
1151 int maxw = (int)(realdh/1.3f);
1152 if (maxw < realdw) {
1153 dw = maxw;
1154 }
1155 } else {
1156 // Turn portrait into landscape.
1157 int maxh = (int)(realdw/1.3f);
1158 if (maxh < realdh) {
1159 dh = maxh;
1160 }
1161 }
1162 }
1163
1164 // Update application display metrics.
1165 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1166 mDisplayId);
1167 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1168 mDisplayId);
1169 mDisplayInfo.rotation = mRotation;
1170 mDisplayInfo.logicalWidth = dw;
1171 mDisplayInfo.logicalHeight = dh;
1172 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1173 mDisplayInfo.appWidth = appWidth;
1174 mDisplayInfo.appHeight = appHeight;
1175 if (isDefaultDisplay) {
1176 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1177 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1178 }
1179 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1180 if (mDisplayScalingDisabled) {
1181 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1182 } else {
1183 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1184 }
1185
1186 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1187 mDisplayInfo);
1188
1189 mBaseDisplayRect.set(0, 0, dw, dh);
1190
1191 if (isDefaultDisplay) {
1192 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1193 mCompatDisplayMetrics);
1194 }
1195 return mDisplayInfo;
1196 }
1197
1198 /**
1199 * Compute display configuration based on display properties and policy settings.
1200 * Do not call if mDisplayReady == false.
1201 */
1202 void computeScreenConfiguration(Configuration config) {
1203 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1204
1205 final int dw = displayInfo.logicalWidth;
1206 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001207 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1208 // TODO: Probably best to set this based on some setting in the display content object,
1209 // so the display can be configured for things like fullscreen.
1210 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001211
Andrii Kulian06d07d62017-03-14 11:11:47 -07001212 config.screenWidthDp =
1213 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1214 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1215 config.screenHeightDp =
1216 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1217 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001218
1219 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1220 final int leftInset = mTmpRect.left;
1221 final int topInset = mTmpRect.top;
1222 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001223 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1224 leftInset + displayInfo.appWidth /* right */,
1225 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001226 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1227 || displayInfo.rotation == Surface.ROTATION_270);
1228
1229 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1230 mDisplayMetrics.density, config);
1231
1232 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1233 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1234 ? Configuration.SCREENLAYOUT_ROUND_YES
1235 : Configuration.SCREENLAYOUT_ROUND_NO);
1236
1237 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1238 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1239 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1240 dh, mDisplayId);
1241 config.densityDpi = displayInfo.logicalDensityDpi;
1242
1243 config.colorMode =
1244 (displayInfo.isHdr()
1245 ? Configuration.COLOR_MODE_HDR_YES
1246 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001247 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001248 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1249 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1250
1251 // Update the configuration based on available input devices, lid switch,
1252 // and platform configuration.
1253 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1254 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1255 config.navigation = Configuration.NAVIGATION_NONAV;
1256
1257 int keyboardPresence = 0;
1258 int navigationPresence = 0;
1259 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1260 final int len = devices != null ? devices.length : 0;
1261 for (int i = 0; i < len; i++) {
1262 InputDevice device = devices[i];
1263 if (!device.isVirtual()) {
1264 final int sources = device.getSources();
1265 final int presenceFlag = device.isExternal() ?
1266 WindowManagerPolicy.PRESENCE_EXTERNAL :
1267 WindowManagerPolicy.PRESENCE_INTERNAL;
1268
1269 // TODO(multi-display): Configure on per-display basis.
1270 if (mService.mIsTouchDevice) {
1271 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1272 InputDevice.SOURCE_TOUCHSCREEN) {
1273 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1274 }
1275 } else {
1276 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1277 }
1278
1279 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1280 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1281 navigationPresence |= presenceFlag;
1282 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1283 && config.navigation == Configuration.NAVIGATION_NONAV) {
1284 config.navigation = Configuration.NAVIGATION_DPAD;
1285 navigationPresence |= presenceFlag;
1286 }
1287
1288 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1289 config.keyboard = Configuration.KEYBOARD_QWERTY;
1290 keyboardPresence |= presenceFlag;
1291 }
1292 }
1293 }
1294
1295 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1296 config.navigation = Configuration.NAVIGATION_DPAD;
1297 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1298 }
1299
1300 // Determine whether a hard keyboard is available and enabled.
1301 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1302 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1303 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1304 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1305 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1306 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1307 }
1308
1309 // Let the policy update hidden states.
1310 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1311 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1312 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1313 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1314 }
1315
1316 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1317 int displayId) {
1318 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1319 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1320 final int unrotDw, unrotDh;
1321 if (rotated) {
1322 unrotDw = dh;
1323 unrotDh = dw;
1324 } else {
1325 unrotDw = dw;
1326 unrotDh = dh;
1327 }
1328 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1329 displayId);
1330 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1331 displayId);
1332 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1333 displayId);
1334 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1335 displayId);
1336 return sw;
1337 }
1338
1339 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1340 DisplayMetrics dm, int dw, int dh, int displayId) {
1341 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1342 displayId);
1343 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1344 uiMode, displayId);
1345 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1346 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1347 if (curSize == 0 || size < curSize) {
1348 curSize = size;
1349 }
1350 return curSize;
1351 }
1352
1353 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1354 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1355
1356 // We need to determine the smallest width that will occur under normal
1357 // operation. To this, start with the base screen size and compute the
1358 // width under the different possible rotations. We need to un-rotate
1359 // the current screen dimensions before doing this.
1360 int unrotDw, unrotDh;
1361 if (rotated) {
1362 unrotDw = dh;
1363 unrotDh = dw;
1364 } else {
1365 unrotDw = dw;
1366 unrotDh = dh;
1367 }
1368 displayInfo.smallestNominalAppWidth = 1<<30;
1369 displayInfo.smallestNominalAppHeight = 1<<30;
1370 displayInfo.largestNominalAppWidth = 0;
1371 displayInfo.largestNominalAppHeight = 0;
1372 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1373 unrotDh);
1374 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1375 unrotDw);
1376 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1377 unrotDh);
1378 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1379 unrotDw);
1380 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1381 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1382 displayId);
1383 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1384 displayId);
1385 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1386 displayId);
1387 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1388 displayId);
1389 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1390 outConfig.screenLayout = sl;
1391 }
1392
1393 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1394 int uiMode, int displayId) {
1395 // Get the app screen size at this rotation.
1396 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1397 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1398
1399 // Compute the screen layout size class for this rotation.
1400 int longSize = w;
1401 int shortSize = h;
1402 if (longSize < shortSize) {
1403 int tmp = longSize;
1404 longSize = shortSize;
1405 shortSize = tmp;
1406 }
1407 longSize = (int)(longSize/density);
1408 shortSize = (int)(shortSize/density);
1409 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1410 }
1411
1412 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1413 int uiMode, int dw, int dh) {
1414 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1415 displayId);
1416 if (width < displayInfo.smallestNominalAppWidth) {
1417 displayInfo.smallestNominalAppWidth = width;
1418 }
1419 if (width > displayInfo.largestNominalAppWidth) {
1420 displayInfo.largestNominalAppWidth = width;
1421 }
1422 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1423 displayId);
1424 if (height < displayInfo.smallestNominalAppHeight) {
1425 displayInfo.smallestNominalAppHeight = height;
1426 }
1427 if (height > displayInfo.largestNominalAppHeight) {
1428 displayInfo.largestNominalAppHeight = height;
1429 }
1430 }
1431
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001432 DockedStackDividerController getDockedDividerController() {
1433 return mDividerControllerLocked;
1434 }
1435
Winson Chung655332c2016-10-31 13:14:28 -07001436 PinnedStackController getPinnedStackController() {
1437 return mPinnedStackControllerLocked;
1438 }
1439
Jeff Browna506a6e2013-06-04 00:02:38 -07001440 /**
1441 * Returns true if the specified UID has access to this display.
1442 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001443 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001444 return mDisplay.hasAccess(uid);
1445 }
1446
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001447 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001448 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001449 }
1450
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001451 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001452 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001453 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -08001454 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001455 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001456 }
1457
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001458 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001459 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1460 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001461 if (stack.mStackId == stackId) {
1462 return stack;
1463 }
1464 }
1465 return null;
1466 }
1467
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001468 /**
1469 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1470 * Null is no compatible stack on the display.
1471 */
1472 TaskStack getStack(int windowingMode) {
1473 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1474 }
1475
1476 /**
1477 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1478 * activity type. Null is no compatible stack on the display.
1479 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001480 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001481 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1482 final TaskStack stack = mTaskStackContainers.get(i);
1483 if (stack.isCompatible(windowingMode, activityType)) {
1484 return stack;
1485 }
1486 }
1487 return null;
1488 }
1489
Bryce Lee48f4b572017-04-10 10:54:15 -07001490 @VisibleForTesting
1491 int getStackCount() {
1492 return mTaskStackContainers.size();
1493 }
1494
1495 @VisibleForTesting
Wale Ogunwale68278562017-09-23 17:13:55 -07001496 int getStackPosition(int windowingMode, int activityType) {
Bryce Lee48f4b572017-04-10 10:54:15 -07001497 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1498 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale68278562017-09-23 17:13:55 -07001499 if (stack.isCompatible(windowingMode, activityType)) {
Bryce Lee48f4b572017-04-10 10:54:15 -07001500 return i;
1501 }
1502 }
1503 return -1;
1504 }
1505
Andrii Kulian441e4492016-09-29 15:25:00 -07001506 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001507 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001508 super.onConfigurationChanged(newParentConfig);
1509
Andrii Kulian3a507b52016-09-19 18:14:12 -07001510 // The display size information is heavily dependent on the resources in the current
1511 // configuration, so we need to reconfigure it every time the configuration changes.
1512 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1513 mService.reconfigureDisplayLocked(this);
1514
1515 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001516 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001517 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001518
Andrii Kulian441e4492016-09-29 15:25:00 -07001519 /**
1520 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1521 * bounds were updated.
1522 */
1523 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001524 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1525 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001526 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001527 changedStackList.add(stack.mStackId);
1528 }
1529 }
Winson Chung32c566f2017-04-11 18:31:21 -07001530
1531 // If there was no pinned stack, we still need to notify the controller of the display info
1532 // update as a result of the config change. We do this here to consolidate the flow between
1533 // changes when there is and is not a stack.
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001534 if (getStack(WINDOWING_MODE_PINNED) == null) {
Winson Chung32c566f2017-04-11 18:31:21 -07001535 mPinnedStackControllerLocked.onDisplayInfoChanged();
1536 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001537 }
1538
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001539 @Override
1540 boolean fillsParent() {
1541 return true;
1542 }
1543
1544 @Override
1545 boolean isVisible() {
1546 return true;
1547 }
1548
1549 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001550 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001551 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001552 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001553 }
1554
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001555 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001556 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1557 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1558 // target, or here in order if there isn't an IME target.
1559 if (traverseTopToBottom) {
1560 for (int i = mChildren.size() - 1; i >= 0; --i) {
1561 final DisplayChildWindowContainer child = mChildren.get(i);
1562 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1563 // In this case the Ime windows will be processed above their target so we skip
1564 // here.
1565 continue;
1566 }
1567 if (child.forAllWindows(callback, traverseTopToBottom)) {
1568 return true;
1569 }
1570 }
1571 } else {
1572 final int count = mChildren.size();
1573 for (int i = 0; i < count; i++) {
1574 final DisplayChildWindowContainer child = mChildren.get(i);
1575 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1576 // In this case the Ime windows will be processed above their target so we skip
1577 // here.
1578 continue;
1579 }
1580 if (child.forAllWindows(callback, traverseTopToBottom)) {
1581 return true;
1582 }
1583 }
1584 }
1585 return false;
1586 }
1587
1588 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1589 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1590 }
1591
Wale Ogunwale399c8692017-05-08 14:22:42 -07001592 @Override
1593 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001594 final WindowManagerPolicy policy = mService.mPolicy;
1595
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001596 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001597 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001598 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001599 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001600 // If the display is frozen, some activities may be in the middle of restarting, and
1601 // thus have removed their old window. If the window has the flag to hide the lock
1602 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1603 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001604 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001605 } else if (policy.isKeyguardLocked()) {
1606 // Use the last orientation the while the display is frozen with the keyguard
1607 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1608 // window. We don't want to check the show when locked window directly though as
1609 // things aren't stable while the display is frozen, for example the window could be
1610 // momentarily unavailable due to activity relaunch.
1611 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001612 + "return " + mLastOrientation);
1613 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001614 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001615 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001616 final int orientation = mAboveAppWindowsContainers.getOrientation();
1617 if (orientation != SCREEN_ORIENTATION_UNSET) {
1618 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001619 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001620 }
1621
Wale Ogunwale399c8692017-05-08 14:22:42 -07001622 // Top system windows are not requesting an orientation. Start searching from apps.
1623 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001624 }
1625
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001626 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001627 // Check if display metrics changed and update base values if needed.
1628 updateBaseDisplayMetricsIfNeeded();
1629
Craig Mautner722285e2012-09-07 13:55:58 -07001630 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001631 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001632
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001633 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1634 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001635 }
Craig Mautner722285e2012-09-07 13:55:58 -07001636 }
1637
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001638 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001639 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1640 if (displayManagerInternal != null) {
1641 // Bootstrap the default logical display from the display manager.
1642 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1643 if (newDisplayInfo != null) {
1644 mDisplayInfo.copyFrom(newDisplayInfo);
1645 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001646 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001647
Andrii Kuliancd097992017-03-23 18:31:59 -07001648 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1649 mDisplayInfo.logicalDensityDpi);
1650 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1651 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1652 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001653 }
1654
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001655 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001656 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001657 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001658 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001659 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1660 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001661 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001662 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001663 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001664 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001665 out.set(left, top, left + width, top + height);
1666 }
1667
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001668 private void getLogicalDisplayRect(Rect out, int orientation) {
1669 getLogicalDisplayRect(out);
1670
1671 // Rotate the Rect if needed.
1672 final int currentRotation = mDisplayInfo.rotation;
1673 final int rotationDelta = deltaRotation(currentRotation, orientation);
1674 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1675 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1676 mTmpRectF.set(out);
1677 mTmpMatrix.mapRect(mTmpRectF);
1678 mTmpRectF.round(out);
1679 }
1680 }
1681
Andrii Kuliancd097992017-03-23 18:31:59 -07001682 /**
1683 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1684 * values.
1685 */
1686 private void updateBaseDisplayMetricsIfNeeded() {
1687 // Get real display metrics without overrides from WM.
1688 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1689 final int orientation = mDisplayInfo.rotation;
1690 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1691 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1692 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1693 final int newDensity = mDisplayInfo.logicalDensityDpi;
1694
1695 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1696 || mInitialDisplayHeight != newHeight
1697 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1698
1699 if (displayMetricsChanged) {
1700 // Check if display size or density is forced.
1701 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1702 || mBaseDisplayHeight != mInitialDisplayHeight;
1703 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1704
1705 // If there is an override set for base values - use it, otherwise use new values.
1706 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1707 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1708 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1709
1710 // Real display metrics changed, so we should also update initial values.
1711 mInitialDisplayWidth = newWidth;
1712 mInitialDisplayHeight = newHeight;
1713 mInitialDisplayDensity = newDensity;
1714 mService.reconfigureDisplayLocked(this);
1715 }
1716 }
1717
Bryce Lee27cec322017-03-21 09:41:37 -07001718 /** Sets the maximum width the screen resolution can be */
1719 void setMaxUiWidth(int width) {
1720 if (DEBUG_DISPLAY) {
1721 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1722 }
1723
1724 mMaxUiWidth = width;
1725
1726 // Update existing metrics.
1727 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1728 }
1729
1730 /** Update base (override) display metrics. */
1731 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1732 mBaseDisplayWidth = baseWidth;
1733 mBaseDisplayHeight = baseHeight;
1734 mBaseDisplayDensity = baseDensity;
1735
1736 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1737 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1738 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1739 mBaseDisplayWidth = mMaxUiWidth;
1740
1741 if (DEBUG_DISPLAY) {
1742 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1743 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1744 + " on display:" + getDisplayId());
1745 }
1746 }
1747
1748 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1749 }
1750
Chong Zhangf66db432016-01-13 10:39:51 -08001751 void getContentRect(Rect out) {
1752 out.set(mContentRect);
1753 }
1754
Wale Ogunwale704a3c02017-09-18 15:30:52 -07001755 TaskStack addStackToDisplay(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001756 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1757 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001758
Wale Ogunwale1666e312016-12-16 11:27:18 -08001759 TaskStack stack = getStackById(stackId);
1760 if (stack != null) {
Wale Ogunwale704a3c02017-09-18 15:30:52 -07001761 // It's already attached to the display...clear mDeferRemoval, set controller, and move
1762 // stack to appropriate z-order on display as needed.
Wale Ogunwale1666e312016-12-16 11:27:18 -08001763 stack.mDeferRemoval = false;
Wale Ogunwale704a3c02017-09-18 15:30:52 -07001764 stack.setController(controller);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001765 // We're not moving the display to front when we're adding stacks, only when
1766 // requested to change the position of stack explicitly.
1767 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
1768 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -07001769 } else {
Wale Ogunwale704a3c02017-09-18 15:30:52 -07001770 stack = new TaskStack(mService, stackId, controller);
Andrii Kulian839def92016-11-02 10:58:58 -07001771 mTaskStackContainers.addStackToDisplay(stack, onTop);
1772 }
1773
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001774 if (stack.inSplitScreenPrimaryWindowingMode()) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001775 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -07001776 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001777 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001778 }
1779
Andrii Kulian51c1b672017-04-07 18:39:32 -07001780 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001781 final DisplayContent prevDc = stack.getDisplayContent();
1782 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001783 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1784 + " which is not currently attached to any display");
1785 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001786 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001787 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1788 + " to its current displayId=" + mDisplayId);
1789 }
1790
Wale Ogunwale1666e312016-12-16 11:27:18 -08001791 prevDc.mTaskStackContainers.removeStackFromDisplay(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001792 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001793 }
1794
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001795 @Override
1796 protected void addChild(DisplayChildWindowContainer child,
1797 Comparator<DisplayChildWindowContainer> comparator) {
1798 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1799 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001800
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001801 @Override
1802 protected void addChild(DisplayChildWindowContainer child, int index) {
1803 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1804 }
1805
1806 @Override
1807 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001808 // Only allow removal of direct children from this display if the display is in the process
1809 // of been removed.
1810 if (mRemovingDisplay) {
1811 super.removeChild(child);
1812 return;
1813 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001814 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001815 }
1816
Andrii Kuliand2765632016-12-12 22:26:34 -08001817 @Override
1818 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1819 // Children of the display are statically ordered, so the real intention here is to perform
1820 // the operation on the display and not the static direct children.
1821 getParent().positionChildAt(position, this, includingParents);
1822 }
1823
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001824 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001825 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1826 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001827 final int taskId = stack.taskIdFromPoint(x, y);
1828 if (taskId != -1) {
1829 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001830 }
1831 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001832 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001833 }
1834
Chong Zhang8e89b312015-09-09 15:09:30 -07001835 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001836 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001837 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001838 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001839 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001840 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001841 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001842 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1843 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001844 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001845 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001846 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001847
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001848 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1849 if (mTmpTaskForResizePointSearchResult.searchDone) {
1850 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001851 }
1852 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001853 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001854 }
1855
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001856 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001857 // The provided task is the task on this display with focus, so if WindowManagerService's
1858 // focused app is not on this display, focusedTask will be null.
1859 if (focusedTask == null) {
1860 mTouchExcludeRegion.setEmpty();
1861 } else {
1862 mTouchExcludeRegion.set(mBaseDisplayRect);
1863 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1864 mTmpRect2.setEmpty();
1865 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1866 final TaskStack stack = mTaskStackContainers.get(stackNdx);
1867 stack.setTouchExcludeRegion(
1868 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
1869 }
1870 // If we removed the focused task above, add it back and only leave its
1871 // outside touch area in the exclusion. TapDectector is not interested in
1872 // any touch inside the focused task itself.
1873 if (!mTmpRect2.isEmpty()) {
1874 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1875 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001876 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001877 final WindowState inputMethod = mService.mInputMethodWindow;
1878 if (inputMethod != null && inputMethod.isVisibleLw()) {
1879 // If the input method is visible and the user is typing, we don't want these touch
1880 // events to be intercepted and used to change focus. This would likely cause a
1881 // disappearance of the input method.
1882 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001883 if (inputMethod.getDisplayId() == mDisplayId) {
1884 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1885 } else {
1886 // IME is on a different display, so we need to update its tap detector.
1887 // TODO(multidisplay): Remove when IME will always appear on same display.
1888 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1889 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001890 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001891 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1892 WindowState win = mTapExcludedWindows.get(i);
1893 win.getTouchableRegion(mTmpRegion);
1894 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1895 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001896 // TODO(multi-display): Support docked stacks on secondary displays.
1897 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001898 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001899 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001900 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1901 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001902 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001903 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001904 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001905 }
1906
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001907 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001908 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001909 super.switchUser();
1910 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001911 }
1912
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001913 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001914 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1915 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001916 }
1917 }
1918
1919 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001920 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001921 }
1922
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001923 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001924 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001925 }
1926
1927 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001928 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001929 }
1930
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001931 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001932 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001933 }
1934
Wale Ogunwale10124582016-09-15 20:25:50 -07001935 @Override
1936 void removeIfPossible() {
1937 if (isAnimating()) {
1938 mDeferredRemoval = true;
1939 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001940 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001941 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001942 }
1943
Wale Ogunwale10124582016-09-15 20:25:50 -07001944 @Override
1945 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001946 mRemovingDisplay = true;
1947 try {
1948 super.removeImmediately();
1949 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1950 mDimLayerController.close();
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07001951 if (mService.canDispatchPointerEvents()) {
1952 if (mTapDetector != null) {
1953 mService.unregisterPointerEventListener(mTapDetector);
1954 }
1955 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
1956 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1957 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001958 }
1959 } finally {
1960 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001961 }
Craig Mautner95da1082014-02-24 17:54:35 -08001962 }
1963
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001964 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001965 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001966 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001967 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1968
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001969 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001970 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001971 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001972 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001973 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001974 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001975 }
1976
Andrii Kulian0214ed92017-05-16 13:44:05 -07001977 /** @return 'true' if removal of this display content is deferred due to active animation. */
1978 boolean isRemovalDeferred() {
1979 return mDeferredRemoval;
1980 }
1981
Wale Ogunwale10124582016-09-15 20:25:50 -07001982 boolean animateForIme(float interpolatedValue, float animationTarget,
1983 float dividerAnimationTarget) {
1984 boolean updated = false;
1985
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001986 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1987 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001988 if (stack == null || !stack.isAdjustedForIme()) {
1989 continue;
1990 }
1991
1992 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1993 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1994 updated = true;
1995 } else {
1996 mDividerControllerLocked.mLastAnimationProgress =
1997 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1998 mDividerControllerLocked.mLastDividerProgress =
1999 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2000 updated |= stack.updateAdjustForIme(
2001 mDividerControllerLocked.mLastAnimationProgress,
2002 mDividerControllerLocked.mLastDividerProgress,
2003 false /* force */);
2004 }
2005 if (interpolatedValue >= 1f) {
2006 stack.endImeAdjustAnimation();
2007 }
2008 }
2009
2010 return updated;
2011 }
2012
2013 boolean clearImeAdjustAnimation() {
2014 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002015 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2016 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002017 if (stack != null && stack.isAdjustedForIme()) {
2018 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2019 changed = true;
2020 }
2021 }
2022 return changed;
2023 }
2024
2025 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002026 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2027 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002028 if (stack.isVisible() && stack.isAdjustedForIme()) {
2029 stack.beginImeAdjustAnimation();
2030 }
2031 }
2032 }
2033
2034 void adjustForImeIfNeeded() {
2035 final WindowState imeWin = mService.mInputMethodWindow;
2036 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2037 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002038 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002039 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2040 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2041 imeTargetStack.getDockSide() : DOCKED_INVALID;
2042 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2043 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2044 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
2045 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
2046 final boolean imeHeightChanged = imeVisible &&
2047 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2048
2049 // The divider could be adjusted for IME position, or be thinner than usual,
2050 // or both. There are three possible cases:
2051 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2052 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2053 // - If IME is not visible, divider is not moved and is normal width.
2054
2055 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002056 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2057 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002058 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002059 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2060 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002061 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2062 } else {
2063 stack.resetAdjustedForIme(false);
2064 }
2065 }
2066 mDividerControllerLocked.setAdjustedForIme(
2067 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2068 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002069 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2070 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002071 stack.resetAdjustedForIme(!dockVisible);
2072 }
2073 mDividerControllerLocked.setAdjustedForIme(
2074 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2075 }
Winson Chung655332c2016-10-31 13:14:28 -07002076 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002077 }
2078
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002079 void setInputMethodAnimLayerAdjustment(int adj) {
2080 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2081 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002082 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002083 }
2084
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002085 /**
2086 * If a window that has an animation specifying a colored background and the current wallpaper
2087 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2088 * suddenly disappear.
2089 */
2090 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002091 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2092 w -> w.mIsWallpaper && w.isVisibleNow());
2093
2094 if (visibleWallpaper != null) {
2095 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002096 }
2097 return winAnimator.mAnimLayer;
2098 }
2099
Wale Ogunwale10124582016-09-15 20:25:50 -07002100 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002101 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2102 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002103 stack.prepareFreezingTaskBounds();
2104 }
2105 }
2106
Wale Ogunwale94744212015-09-21 19:01:47 -07002107 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002108 getLogicalDisplayRect(mTmpRect, newRotation);
2109
2110 // Compute a transform matrix to undo the coordinate space transformation,
2111 // and present the window at the same physical position it previously occupied.
2112 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2113 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2114
2115 mTmpRectF.set(bounds);
2116 mTmpMatrix.mapRect(mTmpRectF);
2117 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002118 }
2119
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002120 static int deltaRotation(int oldRotation, int newRotation) {
2121 int delta = newRotation - oldRotation;
2122 if (delta < 0) delta += 4;
2123 return delta;
2124 }
2125
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002126 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002127 Matrix outMatrix) {
2128 // For rotations without Z-ordering we don't need the target rectangle's position.
2129 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2130 displayHeight, outMatrix);
2131 }
2132
2133 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2134 float displayWidth, float displayHeight, Matrix outMatrix) {
2135 switch (rotation) {
2136 case ROTATION_0:
2137 outMatrix.reset();
2138 break;
2139 case ROTATION_270:
2140 outMatrix.setRotate(270, 0, 0);
2141 outMatrix.postTranslate(0, displayHeight);
2142 outMatrix.postTranslate(rectTop, 0);
2143 break;
2144 case ROTATION_180:
2145 outMatrix.reset();
2146 break;
2147 case ROTATION_90:
2148 outMatrix.setRotate(90, 0, 0);
2149 outMatrix.postTranslate(displayWidth, 0);
2150 outMatrix.postTranslate(-rectTop, rectLeft);
2151 break;
2152 }
2153 }
2154
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002155 @CallSuper
2156 @Override
2157 public void writeToProto(ProtoOutputStream proto, long fieldId) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002158 final long token = proto.start(fieldId);
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002159 super.writeToProto(proto, WINDOW_CONTAINER);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002160 proto.write(ID, mDisplayId);
2161 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2162 final TaskStack stack = mTaskStackContainers.get(stackNdx);
2163 stack.writeToProto(proto, STACKS);
2164 }
2165 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2166 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
2167 for (int i = mAboveAppWindowsContainers.size() - 1; i >= 0; --i) {
2168 final WindowToken windowToken = mAboveAppWindowsContainers.get(i);
2169 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS);
2170 }
2171 for (int i = mBelowAppWindowsContainers.size() - 1; i >= 0; --i) {
2172 final WindowToken windowToken = mBelowAppWindowsContainers.get(i);
2173 windowToken.writeToProto(proto, BELOW_APP_WINDOWS);
2174 }
2175 for (int i = mImeWindowsContainers.size() - 1; i >= 0; --i) {
2176 final WindowToken windowToken = mImeWindowsContainers.get(i);
2177 windowToken.writeToProto(proto, IME_WINDOWS);
2178 }
2179 proto.write(DPI, mBaseDisplayDensity);
2180 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002181 proto.write(ROTATION, mRotation);
2182 final ScreenRotationAnimation screenRotationAnimation =
2183 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2184 if (screenRotationAnimation != null) {
2185 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2186 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002187 proto.end(token);
2188 }
2189
Craig Mautnera91f9e22012-09-14 16:22:08 -07002190 public void dump(String prefix, PrintWriter pw) {
2191 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2192 final String subPrefix = " " + prefix;
2193 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2194 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2195 pw.print("dpi");
2196 if (mInitialDisplayWidth != mBaseDisplayWidth
2197 || mInitialDisplayHeight != mBaseDisplayHeight
2198 || mInitialDisplayDensity != mBaseDisplayDensity) {
2199 pw.print(" base=");
2200 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2201 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2202 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002203 if (mDisplayScalingDisabled) {
2204 pw.println(" noscale");
2205 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002206 pw.print(" cur=");
2207 pw.print(mDisplayInfo.logicalWidth);
2208 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2209 pw.print(" app=");
2210 pw.print(mDisplayInfo.appWidth);
2211 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2212 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2213 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2214 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2215 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002216 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002217 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002218 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002219
Craig Mautnerdc548482014-02-05 13:35:24 -08002220 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002221 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002222 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2223 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002224 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002225 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002226
Craig Mautnerdc548482014-02-05 13:35:24 -08002227 pw.println();
2228 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002229 pw.println();
2230 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002231 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002232 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002233 pw.print(" Exiting #"); pw.print(i);
2234 pw.print(' '); pw.print(token);
2235 pw.println(':');
2236 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002237 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002238 }
Craig Mautner59c00972012-07-30 12:10:24 -07002239 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002240 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002241 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002242 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002243 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002244 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002245
2246 if (mInputMethodAnimLayerAdjustment != 0) {
2247 pw.println(subPrefix
2248 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2249 }
Craig Mautner59c00972012-07-30 12:10:24 -07002250 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002251
2252 @Override
2253 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002254 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002255 }
2256
2257 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002258 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002259 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002260
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002261 /** Returns true if the stack in the windowing mode is visible. */
2262 boolean isStackVisible(int windowingMode) {
2263 final TaskStack stack = getStack(windowingMode);
2264 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002265 }
2266
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002267 /**
2268 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
2269 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07002270 TaskStack getDockedStackLocked() {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07002271 final TaskStack stack = getStack(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07002272 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002273 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002274
2275 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002276 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02002277 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002278 */
Jorim Jaggife762342016-10-13 14:33:27 +02002279 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07002280 return getStack(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002281 }
2282
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002283 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002284 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002285 final int x = (int) xf;
2286 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002287 final WindowState touchedWin = getWindow(w -> {
2288 final int flags = w.mAttrs.flags;
2289 if (!w.isVisibleLw()) {
2290 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002291 }
2292 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002293 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002294 }
2295
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002296 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002297 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002298 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002299 }
2300
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002301 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002302
2303 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002304 return mTmpRegion.contains(x, y) || touchFlags == 0;
2305 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002306
2307 return touchedWin;
2308 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002309
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002310 boolean canAddToastWindowForUid(int uid) {
2311 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002312 // Also if the app is focused adding more than one toast at
2313 // a time for better backwards compatibility.
2314 final WindowState focusedWindowForUid = getWindow(w ->
2315 w.mOwnerUid == uid && w.isFocused());
2316 if (focusedWindowForUid != null) {
2317 return true;
2318 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002319 final WindowState win = getWindow(w ->
2320 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2321 && !w.mWindowRemovalAllowed);
2322 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002323 }
2324
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002325 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002326 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2327 return;
2328 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002329
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002330 // Used to communicate the old focus to the callback method.
2331 mTmpWindow = oldFocus;
2332
2333 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002334 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002335
2336 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002337 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002338
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002339 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002340
2341 if (mTmpWindow == null) {
2342 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2343 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002344 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002345 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002346 }
2347
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002348 /** Updates the layer assignment of windows on this display. */
2349 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002350 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002351 if (setLayoutNeeded) {
2352 setLayoutNeeded();
2353 }
2354 }
2355
Wale Ogunwale1666e312016-12-16 11:27:18 -08002356 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2357 // moving containers or resizing them. Need to investigate the best way to have it automatically
2358 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002359 void layoutAndAssignWindowLayersIfNeeded() {
2360 mService.mWindowsChanged = true;
2361 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002362
2363 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2364 false /*updateInputWindows*/)) {
2365 assignWindowLayers(false /* setLayoutNeeded */);
2366 }
2367
2368 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2369 mService.mWindowPlacerLocked.performSurfacePlacement();
2370 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2371 }
2372
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002373 /** Returns true if a leaked surface was destroyed */
2374 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002375 // Used to indicate that a surface was leaked.
2376 mTmpWindow = null;
2377 forAllWindows(w -> {
2378 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002379 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002380 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002381 }
2382 if (!mService.mSessions.contains(wsa.mSession)) {
2383 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002384 + w + " surface=" + wsa.mSurfaceController
2385 + " token=" + w.mToken
2386 + " pid=" + w.mSession.mPid
2387 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002388 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002389 mService.mForceRemoves.add(w);
2390 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002391 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002392 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002393 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002394 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002395 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002396 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002397 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002398 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002399 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002400
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002401 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002402 }
2403
2404 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002405 * Determine and return the window that should be the IME target.
2406 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2407 * @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 +00002408 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002409 WindowState computeImeTarget(boolean updateImeTarget) {
2410 if (mService.mInputMethodWindow == null) {
2411 // There isn't an IME so there shouldn't be a target...That was easy!
2412 if (updateImeTarget) {
2413 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2414 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2415 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2416 }
2417 return null;
2418 }
2419
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002420 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2421 // same display. Or even when the current IME/target are not on the same screen as the next
2422 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002423 mUpdateImeTarget = updateImeTarget;
2424 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002425
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002426
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002427 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2428 // to be on top of it, but it is not -really- where input will go. So look down below
2429 // for a real window to target...
2430 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2431 final AppWindowToken token = target.mAppToken;
2432 if (token != null) {
2433 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2434 if (betterTarget != null) {
2435 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002436 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002437 }
2438 }
2439
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002440 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2441 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002442
2443 // Now, a special case -- if the last target's window is in the process of exiting, and is
2444 // above the new target, keep on the last target to avoid flicker. Consider for example a
2445 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2446 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2447 // scrim.
2448 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002449 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2450 && (target == null
2451 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002452 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002453 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002454 }
2455
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002456 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2457 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002458
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002459 if (target == null) {
2460 if (updateImeTarget) {
2461 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2462 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2463 + Debug.getCallers(4) : ""));
2464 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2465 }
2466
2467 return null;
2468 }
2469
2470 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002471 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2472 if (token != null) {
2473
2474 // Now some fun for dealing with window animations that modify the Z order. We need
2475 // to look at all windows below the current target that are in this app, finding the
2476 // highest visible one in layering.
2477 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002478 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002479 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002480 }
2481
2482 if (highestTarget != null) {
2483 final AppTransition appTransition = mService.mAppTransition;
2484 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2485 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2486 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002487 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002488
2489 if (appTransition.isTransitionSet()) {
2490 // If we are currently setting up for an animation, hold everything until we
2491 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002492 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2493 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002494 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002495 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002496 // If the window we are currently targeting is involved with an animation,
2497 // and it is on top of the next target we will be over, then hold off on
2498 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002499 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2500 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002501 }
2502 }
2503 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002504
2505 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2506 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2507 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002508 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002509 }
2510
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002511 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002512 }
2513
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002514 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2515 if (target == mService.mInputMethodTarget
2516 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2517 && mInputMethodAnimLayerAdjustment == layerAdj) {
2518 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002519 }
2520
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002521 mService.mInputMethodTarget = target;
2522 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2523 setInputMethodAnimLayerAdjustment(layerAdj);
2524 assignWindowLayers(false /* setLayoutNeeded */);
2525 }
2526
2527 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2528 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2529 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2530 }
2531
2532 // Used to indicate we have reached the first window in the range we are interested in.
2533 mTmpWindow = null;
2534
2535 // TODO: Figure-out a more efficient way to do this.
2536 final WindowState candidate = getWindow(w -> {
2537 if (w == top) {
2538 // Reached the first window in the range we are interested in.
2539 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002540 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002541 if (mTmpWindow == null) {
2542 return false;
2543 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002544
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002545 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2546 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002547 }
2548 // If we reached the bottom of the range of windows we are considering,
2549 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002550 if (w == bottom) {
2551 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002552 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002553 return false;
2554 });
2555
2556 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002557 }
2558
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002559 void setLayoutNeeded() {
2560 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2561 mLayoutNeeded = true;
2562 }
2563
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002564 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002565 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2566 mLayoutNeeded = false;
2567 }
2568
2569 boolean isLayoutNeeded() {
2570 return mLayoutNeeded;
2571 }
2572
Wale Ogunwale02319a62016-09-26 15:21:22 -07002573 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2574 if (mTokenMap.isEmpty()) {
2575 return;
2576 }
2577 pw.println(" Display #" + mDisplayId);
2578 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2579 while (it.hasNext()) {
2580 final WindowToken token = it.next();
2581 pw.print(" ");
2582 pw.print(token);
2583 if (dumpAll) {
2584 pw.println(':');
2585 token.dump(pw, " ");
2586 } else {
2587 pw.println();
2588 }
2589 }
2590 }
2591
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002592 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002593 final int[] index = new int[1];
2594 forAllWindows(w -> {
2595 final WindowStateAnimator wAnim = w.mWinAnimator;
2596 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2597 index[0] = index[0] + 1;
2598 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002599 }
2600
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002601 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002602 forAllWindows(w -> {
2603 w.mWinAnimator.enableSurfaceTrace(fd);
2604 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002605 }
2606
2607 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002608 forAllWindows(w -> {
2609 w.mWinAnimator.disableSurfaceTrace();
2610 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002611 }
2612
Jorim Jaggife762342016-10-13 14:33:27 +02002613 /**
2614 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2615 */
2616 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2617 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002618 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002619 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2620 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002621 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002622 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2623 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002624 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002625 }
2626
Wale Ogunwale494009b82016-10-21 09:01:38 -07002627 boolean checkWaitingForWindows() {
2628
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002629 mHaveBootMsg = false;
2630 mHaveApp = false;
2631 mHaveWallpaper = false;
2632 mHaveKeyguard = true;
2633
2634 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002635 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2636 return true;
2637 }
2638 if (w.isDrawnLw()) {
2639 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002640 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002641 } else if (w.mAttrs.type == TYPE_APPLICATION
2642 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002643 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002644 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002645 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002646 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002647 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002648 }
2649 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002650 return false;
2651 });
2652
2653 if (visibleWindow != null) {
2654 // We have a visible window.
2655 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002656 }
2657
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002658 // if the wallpaper service is disabled on the device, we're never going to have
2659 // wallpaper, don't bother waiting for it
2660 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2661 com.android.internal.R.bool.config_enableWallpaperService)
2662 && !mService.mOnlyCore;
2663
Wale Ogunwale494009b82016-10-21 09:01:38 -07002664 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2665 "******** booted=" + mService.mSystemBooted
2666 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002667 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2668 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2669 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002670
2671 // If we are turning on the screen to show the boot message, don't do it until the boot
2672 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002673 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002674 return true;
2675 }
2676
2677 // If we are turning on the screen after the boot is completed normally, don't do so until
2678 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002679 if (mService.mSystemBooted
2680 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002681 return true;
2682 }
2683
2684 return false;
2685 }
2686
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002687 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002688 mTmpWindowAnimator = animator;
2689 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002690 }
2691
2692 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002693 resetAnimationBackgroundAnimator();
2694
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002695 // Used to indicate a detached wallpaper.
2696 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002697 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002698
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002699 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002700
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002701 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002702 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002703 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2704 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002705 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2706 }
2707 }
2708
2709 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002710 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002711 }
2712
Wale Ogunwale494009b82016-10-21 09:01:38 -07002713 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002714 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002715 if (imFocus == null) {
2716 return false;
2717 }
2718
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002719 if (DEBUG_INPUT_METHOD) {
2720 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2721 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2722 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002723 }
2724
Wale Ogunwale494009b82016-10-21 09:01:38 -07002725 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2726
2727 if (DEBUG_INPUT_METHOD) {
2728 Slog.i(TAG_WM, "IM target client: " + imeClient);
2729 if (imeClient != null) {
2730 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2731 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2732 }
2733 }
2734
2735 return imeClient != null && imeClient.asBinder() == client.asBinder();
2736 }
2737
2738 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002739 final WindowState win = getWindow(
2740 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2741 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002742 }
2743
2744 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002745 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002746 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002747 final int curValue = w.mSystemUiVisibility;
2748 final int diff = (curValue ^ visibility) & globalDiff;
2749 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002750 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002751 w.mSeq++;
2752 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002753 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002754 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2755 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002756 visibility, newValue, diff);
2757 }
2758 } catch (RemoteException e) {
2759 // so sorry
2760 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002761 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002762 }
2763
2764 void onWindowFreezeTimeout() {
2765 Slog.w(TAG_WM, "Window freeze timeout expired.");
2766 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002767
2768 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002769 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002770 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002771 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002772 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002773 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2774 - mService.mDisplayFreezeTime);
2775 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002776 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002777 mService.mWindowPlacerLocked.performSurfacePlacement();
2778 }
2779
2780 void waitForAllWindowsDrawn() {
2781 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002782 forAllWindows(w -> {
2783 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2784 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2785 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002786 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002787 w.mLastContentInsets.set(-1, -1, -1, -1);
2788 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002789 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002790 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002791 }
2792
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002793 // TODO: Super crazy long method that should be broken down...
2794 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2795
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002796 final int dw = mDisplayInfo.logicalWidth;
2797 final int dh = mDisplayInfo.logicalHeight;
2798 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2799
2800 mTmpUpdateAllDrawn.clear();
2801
2802 int repeats = 0;
2803 do {
2804 repeats++;
2805 if (repeats > 6) {
2806 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2807 clearLayoutNeeded();
2808 break;
2809 }
2810
2811 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2812 pendingLayoutChanges);
2813
Andrii Kulian839def92016-11-02 10:58:58 -07002814 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2815 // the wallpaper window jumping across displays.
2816 // Remove check for default display when there will be support for multiple wallpaper
2817 // targets (on different displays).
2818 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002819 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002820 }
2821
2822 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2823 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2824 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2825 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002826 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002827 }
2828 }
2829
2830 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2831 setLayoutNeeded();
2832 }
2833
2834 // FIRST LOOP: Perform a layout, if needed.
2835 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2836 performLayout(repeats == 1, false /* updateInputWindows */);
2837 } else {
2838 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2839 }
2840
2841 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2842 pendingLayoutChanges = 0;
2843
2844 if (isDefaultDisplay) {
2845 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002846 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002847 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2848 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2849 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2850 }
2851 } while (pendingLayoutChanges != 0);
2852
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002853 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002854 resetDimming();
2855
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002856 mTmpRecoveringMemory = recoveringMemory;
2857 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002858
2859 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002860 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2861 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2862 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002863 true /* inTraversal, must call performTraversalInTrans... below */);
2864
2865 stopDimmingIfNeeded();
2866
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002867 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2868 if (wallpaperVisible != mLastWallpaperVisible) {
2869 mLastWallpaperVisible = wallpaperVisible;
2870 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2871 }
2872
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002873 while (!mTmpUpdateAllDrawn.isEmpty()) {
2874 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2875 // See if any windows have been drawn, so they (and others associated with them)
2876 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002877 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002878 }
2879
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002880 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002881 }
2882
2883 void performLayout(boolean initial, boolean updateInputWindows) {
2884 if (!isLayoutNeeded()) {
2885 return;
2886 }
2887 clearLayoutNeeded();
2888
2889 final int dw = mDisplayInfo.logicalWidth;
2890 final int dh = mDisplayInfo.logicalHeight;
2891
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002892 if (DEBUG_LAYOUT) {
2893 Slog.v(TAG, "-------------------------------------");
2894 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2895 }
2896
Andrii Kulian8ee72852017-03-10 10:36:45 -08002897 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002898 getConfiguration().uiMode);
2899 if (isDefaultDisplay) {
2900 // Not needed on non-default displays.
2901 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2902 mService.mScreenRect.set(0, 0, dw, dh);
2903 }
2904
2905 mService.mPolicy.getContentRectLw(mContentRect);
2906
2907 int seq = mService.mLayoutSeq + 1;
2908 if (seq < 0) seq = 0;
2909 mService.mLayoutSeq = seq;
2910
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002911 // Used to indicate that we have processed the dream window and all additional windows are
2912 // behind it.
2913 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002914 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002915
2916 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002917 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002918
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002919 // Used to indicate that we have processed the dream window and all additional attached
2920 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002921 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002922 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002923
2924 // Now perform layout of attached windows, which usually depend on the position of the
2925 // window they are attached to. XXX does not deal with windows that are attached to windows
2926 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002927 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002928
2929 // Window frames may have changed. Tell the input dispatcher about it.
2930 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2931 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2932 if (updateInputWindows) {
2933 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2934 }
2935
2936 mService.mPolicy.finishLayoutLw();
2937 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2938 }
2939
2940 /**
2941 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2942 * In portrait mode, it grabs the full screenshot.
2943 *
2944 * @param width the width of the target bitmap
2945 * @param height the height of the target bitmap
2946 * @param includeFullDisplay true if the screen should not be cropped before capture
2947 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2948 * @param config of the output bitmap
2949 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002950 * @param includeDecor whether to include window decors, like the status or navigation bar
2951 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002952 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002953 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002954 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002955 boolean wallpaperOnly, boolean includeDecor) {
2956 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2957 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002958 if (bitmap == null) {
2959 return null;
2960 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002961
2962 if (DEBUG_SCREENSHOT) {
2963 // TEST IF IT's ALL BLACK
2964 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2965 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2966 bitmap.getHeight());
2967 boolean allBlack = true;
2968 final int firstColor = buffer[0];
2969 for (int i = 0; i < buffer.length; i++) {
2970 if (buffer[i] != firstColor) {
2971 allBlack = false;
2972 break;
2973 }
2974 }
2975 if (allBlack) {
2976 final WindowState appWin = mScreenshotApplicationState.appWin;
2977 final int maxLayer = mScreenshotApplicationState.maxLayer;
2978 final int minLayer = mScreenshotApplicationState.minLayer;
2979 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2980 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2981 (appWin != null ?
2982 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2983 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2984 }
2985 }
2986
2987 // Create a copy of the screenshot that is immutable and backed in ashmem.
2988 // This greatly reduces the overhead of passing the bitmap between processes.
2989 Bitmap ret = bitmap.createAshmemBitmap(config);
2990 bitmap.recycle();
2991 return ret;
2992 }
2993
2994 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2995 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2996 boolean includeDecor) {
2997 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2998 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2999 }
3000
3001 private <E> E screenshotApplications(IBinder appToken, int width, int height,
3002 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
3003 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003004 int dw = mDisplayInfo.logicalWidth;
3005 int dh = mDisplayInfo.logicalHeight;
3006 if (dw == 0 || dh == 0) {
3007 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3008 + ": returning null. logical widthxheight=" + dw + "x" + dh);
3009 return null;
3010 }
3011
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003012 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003013
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003014 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003015 final Rect frame = new Rect();
3016 final Rect stackBounds = new Rect();
3017
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003018 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003019 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08003020 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003021 synchronized(mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02003022 if (!mService.mPolicy.isScreenOn()) {
3023 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Attempted to take screenshot while display"
3024 + " was off.");
3025 return null;
3026 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003027 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003028 mScreenshotApplicationState.appWin = null;
3029 forAllWindows(w -> {
3030 if (!w.mHasSurface) {
3031 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003032 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003033 if (w.mLayer >= aboveAppLayer) {
3034 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003035 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003036 if (wallpaperOnly && !w.mIsWallpaper) {
3037 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003038 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003039 if (w.mIsImWindow) {
Jorim Jaggieb59d6e2017-05-04 13:54:22 +02003040 return false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003041 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003042 // If this is the wallpaper layer and we're only looking for the wallpaper layer
3043 // then the target window state is this one.
3044 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003045 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003046 }
3047
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003048 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003049 // We have not ran across the target window yet, so it is probably behind
3050 // the wallpaper. This can happen when the keyguard is up and all windows
3051 // are moved behind the wallpaper. We don't want to include the wallpaper
3052 // layer in the screenshot as it will cover-up the layer of the target
3053 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003054 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003055 }
3056 // Fall through. The target window is in front of the wallpaper. For this
3057 // case we want to include the wallpaper layer in the screenshot because
3058 // the target window might have some transparent areas.
3059 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003060 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003061 // This app window is of no interest if it is not associated with the
3062 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003063 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003064 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003065 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003066 }
3067
3068 // Include this window.
3069
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003070 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003071 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003072 if (mScreenshotApplicationState.maxLayer < layer) {
3073 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003074 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003075 if (mScreenshotApplicationState.minLayer > layer) {
3076 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003077 }
3078
3079 // Don't include wallpaper in bounds calculation
Jorim Jaggi70f59482017-05-04 14:05:59 +02003080 if (!w.mIsWallpaper && !mutableIncludeFullDisplay.value) {
3081 if (includeDecor) {
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003082 final Task task = w.getTask();
3083 if (task != null) {
3084 task.getBounds(frame);
3085 } else {
Jorim Jaggi30d64f32017-04-07 16:33:17 +02003086
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003087 // No task bounds? Too bad! Ain't no screenshot then.
3088 return true;
3089 }
Jorim Jaggi70f59482017-05-04 14:05:59 +02003090 } else {
3091 final Rect wf = w.mFrame;
3092 final Rect cr = w.mContentInsets;
3093 int left = wf.left + cr.left;
3094 int top = wf.top + cr.top;
3095 int right = wf.right - cr.right;
3096 int bottom = wf.bottom - cr.bottom;
3097 frame.union(left, top, right, bottom);
3098 w.getVisibleBounds(stackBounds);
3099 if (!Rect.intersects(frame, stackBounds)) {
3100 // Set frame empty if there's no intersection.
3101 frame.setEmpty();
3102 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003103 }
3104 }
3105
3106 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003107 (w.mAppToken != null && w.mAppToken.token == appToken)
3108 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi406386a82017-09-15 14:17:40 +02003109 if (foundTargetWs && winAnim.getShown() && winAnim.mLastAlpha > 0f) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003110 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003111 }
3112
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003113 if (w.isObscuringDisplay()){
3114 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003115 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003116 return false;
3117 }, true /* traverseTopToBottom */);
3118
3119 final WindowState appWin = mScreenshotApplicationState.appWin;
3120 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
3121 final int maxLayer = mScreenshotApplicationState.maxLayer;
3122 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003123
3124 if (appToken != null && appWin == null) {
3125 // Can't find a window to snapshot.
3126 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
3127 "Screenshot: Couldn't find a surface matching " + appToken);
3128 return null;
3129 }
3130
3131 if (!screenshotReady) {
3132 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3133 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3134 appWin.mWinAnimator.mDrawState)));
3135 return null;
3136 }
3137
3138 // Screenshot is ready to be taken. Everything from here below will continue
3139 // through the bottom of the loop and return a value. We only stay in the loop
3140 // because we don't want to release the mWindowMap lock until the screenshot is
3141 // taken.
3142
3143 if (maxLayer == 0) {
3144 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3145 + ": returning null maxLayer=" + maxLayer);
3146 return null;
3147 }
3148
Jorim Jaggi02886a82016-12-06 09:10:06 -08003149 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003150 // Constrain frame to the screen size.
3151 if (!frame.intersect(0, 0, dw, dh)) {
3152 frame.setEmpty();
3153 }
3154 } else {
3155 // Caller just wants entire display.
3156 frame.set(0, 0, dw, dh);
3157 }
3158 if (frame.isEmpty()) {
3159 return null;
3160 }
3161
3162 if (width < 0) {
3163 width = (int) (frame.width() * frameScale);
3164 }
3165 if (height < 0) {
3166 height = (int) (frame.height() * frameScale);
3167 }
3168
3169 // Tell surface flinger what part of the image to crop. Take the top
3170 // right part of the application, and crop the larger dimension to fit.
3171 Rect crop = new Rect(frame);
3172 if (width / (float) frame.width() < height / (float) frame.height()) {
3173 int cropWidth = (int)((float)width / (float)height * frame.height());
3174 crop.right = crop.left + cropWidth;
3175 } else {
3176 int cropHeight = (int)((float)height / (float)width * frame.width());
3177 crop.bottom = crop.top + cropHeight;
3178 }
3179
3180 // The screenshot API does not apply the current screen rotation.
3181 int rot = mDisplay.getRotation();
3182
3183 if (rot == ROTATION_90 || rot == ROTATION_270) {
3184 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3185 }
3186
3187 // Surfaceflinger is not aware of orientation, so convert our logical
3188 // crop to surfaceflinger's portrait orientation.
3189 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3190
3191 if (DEBUG_SCREENSHOT) {
3192 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3193 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003194 forAllWindows(w -> {
3195 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3196 Slog.i(TAG_WM, w + ": " + w.mLayer
3197 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003198 + " surfaceLayer=" + ((controller == null)
3199 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003200 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003201 }
3202
3203 final ScreenRotationAnimation screenRotationAnimation =
3204 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3205 final boolean inRotation = screenRotationAnimation != null &&
3206 screenRotationAnimation.isAnimating();
3207 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3208 "Taking screenshot while rotating");
3209
3210 // We force pending transactions to flush before taking
3211 // the screenshot by pushing an empty synchronous transaction.
3212 SurfaceControl.openTransaction();
3213 SurfaceControl.closeTransactionSync();
3214
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003215 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003216 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003217 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003218 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3219 + ") to layer " + maxLayer);
3220 return null;
3221 }
3222 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003223 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003224 }
3225
3226 // TODO: Can this use createRotationMatrix()?
3227 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3228 if (rot == Surface.ROTATION_90) {
3229 final int tmp = crop.top;
3230 crop.top = dw - crop.right;
3231 crop.right = crop.bottom;
3232 crop.bottom = dw - crop.left;
3233 crop.left = tmp;
3234 } else if (rot == Surface.ROTATION_180) {
3235 int tmp = crop.top;
3236 crop.top = dh - crop.bottom;
3237 crop.bottom = dh - tmp;
3238 tmp = crop.right;
3239 crop.right = dw - crop.left;
3240 crop.left = dw - tmp;
3241 } else if (rot == Surface.ROTATION_270) {
3242 final int tmp = crop.top;
3243 crop.top = crop.left;
3244 crop.left = dh - crop.bottom;
3245 crop.bottom = crop.right;
3246 crop.right = dh - tmp;
3247 }
3248 }
3249
3250 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003251 // Used to indicate the layout is needed.
3252 mTmpWindow = null;
3253
3254 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003255 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003256 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003257 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003258 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003259 w.setDisplayLayoutNeeded();
3260 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003261 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003262
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003263 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003264 mService.mWindowPlacerLocked.performSurfacePlacement();
3265 }
3266 }
3267
Wale Ogunwale1666e312016-12-16 11:27:18 -08003268 void setExitingTokensHasVisible(boolean hasVisible) {
3269 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3270 mExitingTokens.get(i).hasVisible = hasVisible;
3271 }
3272
3273 // Initialize state of exiting applications.
3274 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3275 }
3276
3277 void removeExistingTokensIfPossible() {
3278 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3279 final WindowToken token = mExitingTokens.get(i);
3280 if (!token.hasVisible) {
3281 mExitingTokens.remove(i);
3282 }
3283 }
3284
3285 // Time to remove any exiting applications?
3286 mTaskStackContainers.removeExistingAppTokensIfPossible();
3287 }
3288
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003289 @Override
3290 void onDescendantOverrideConfigurationChanged() {
3291 setLayoutNeeded();
3292 mService.requestTraversal();
3293 }
3294
David Stevens9440dc82017-03-16 19:00:20 -07003295 boolean okToDisplay() {
3296 if (mDisplayId == DEFAULT_DISPLAY) {
3297 return !mService.mDisplayFrozen
3298 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3299 }
3300 return mDisplayInfo.state == Display.STATE_ON;
3301 }
3302
3303 boolean okToAnimate() {
3304 return okToDisplay() &&
3305 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3306 }
3307
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003308 static final class TaskForResizePointSearchResult {
3309 boolean searchDone;
3310 Task taskForResize;
3311
3312 void reset() {
3313 searchDone = false;
3314 taskForResize = null;
3315 }
3316 }
Robert Carr3b716242016-08-16 16:02:21 -07003317
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003318 private static final class ApplySurfaceChangesTransactionState {
3319 boolean displayHasContent;
3320 boolean obscured;
3321 boolean syswin;
3322 boolean focusDisplayed;
3323 float preferredRefreshRate;
3324 int preferredModeId;
3325
3326 void reset() {
3327 displayHasContent = false;
3328 obscured = false;
3329 syswin = false;
3330 focusDisplayed = false;
3331 preferredRefreshRate = 0;
3332 preferredModeId = 0;
3333 }
3334 }
3335
3336 private static final class ScreenshotApplicationState {
3337 WindowState appWin;
3338 int maxLayer;
3339 int minLayer;
3340 boolean screenshotReady;
3341
3342 void reset(boolean screenshotReady) {
3343 appWin = null;
3344 maxLayer = 0;
3345 minLayer = 0;
3346 this.screenshotReady = screenshotReady;
3347 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3348 }
3349 }
3350
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003351 /**
3352 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3353 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3354 * homogeneous children type which is currently required by sub-classes of
3355 * {@link WindowContainer} class.
3356 */
3357 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3358
3359 int size() {
3360 return mChildren.size();
3361 }
3362
3363 E get(int index) {
3364 return mChildren.get(index);
3365 }
3366
3367 @Override
3368 boolean fillsParent() {
3369 return true;
3370 }
3371
3372 @Override
3373 boolean isVisible() {
3374 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003375 }
3376 }
3377
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003378 /**
3379 * Window container class that contains all containers on this display relating to Apps.
3380 * I.e Activities.
3381 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003382 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003383
Andrii Kulian839def92016-11-02 10:58:58 -07003384 /**
3385 * Adds the stack to this container.
3386 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3387 */
3388 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale68278562017-09-23 17:13:55 -07003389 if (stack.isActivityTypeHome()) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003390 if (mHomeStack != null) {
3391 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3392 }
3393 mHomeStack = stack;
3394 }
3395 addChild(stack, onTop);
3396 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003397 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003398
Andrii Kulian839def92016-11-02 10:58:58 -07003399 /** Removes the stack from its container and prepare for changing the parent. */
3400 void removeStackFromDisplay(TaskStack stack) {
3401 removeChild(stack);
3402 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07003403 }
3404
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003405 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003406 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3407 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003408 addChild(stack, addIndex);
3409 setLayoutNeeded();
3410 }
3411
Bryce Lee00d586d2017-07-28 20:48:43 -07003412
3413 @Override
3414 boolean isOnTop() {
3415 // Considered always on top
3416 return true;
3417 }
3418
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003419 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003420 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003421 if (child.getWindowConfiguration().isAlwaysOnTop()
3422 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003423 // This stack is always-on-top, override the default behavior.
3424 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3425
3426 // Moving to its current position, as we must call super but we don't want to
3427 // perform any meaningful action.
3428 final int currentPosition = mChildren.indexOf(child);
3429 super.positionChildAt(currentPosition, child, false /* includingParents */);
3430 return;
3431 }
3432
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003433 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3434 super.positionChildAt(targetPosition, child, includingParents);
3435
3436 setLayoutNeeded();
3437 }
3438
3439 /**
3440 * When stack is added or repositioned, find a proper position for it.
3441 * This will make sure that pinned stack always stays on top.
3442 * @param requestedPosition Position requested by caller.
3443 * @param stack Stack to be added or positioned.
3444 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3445 * @return The proper position for the stack.
3446 */
3447 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3448 final int topChildPosition = mChildren.size() - 1;
3449 boolean toTop = requestedPosition == POSITION_TOP;
3450 toTop |= adding ? requestedPosition >= topChildPosition + 1
3451 : requestedPosition >= topChildPosition;
3452 int targetPosition = requestedPosition;
3453
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003454 if (toTop && stack.getWindowingMode() != WINDOWING_MODE_PINNED
3455 && getStack(WINDOWING_MODE_PINNED) != null) {
Bryce Lee48f4b572017-04-10 10:54:15 -07003456 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003457 TaskStack topStack = mChildren.get(topChildPosition);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003458 if (topStack.getWindowingMode() != WINDOWING_MODE_PINNED) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003459 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3460 }
3461
3462 // So, stack is moved just below the pinned stack.
3463 // When we're adding a new stack the target is the current pinned stack position.
3464 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003465 // stack, because WindowContainer#positionAt() first removes element and then adds
3466 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003467 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3468 }
3469
3470 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003471 }
3472
3473 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003474 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3475 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003476 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003477 if (super.forAllWindows(callback, traverseTopToBottom)) {
3478 return true;
3479 }
3480 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3481 return true;
3482 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003483 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003484 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3485 return true;
3486 }
3487 if (super.forAllWindows(callback, traverseTopToBottom)) {
3488 return true;
3489 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003490 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003491 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003492 }
3493
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003494 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003495 boolean traverseTopToBottom) {
3496 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3497 // app tokens.
3498 // TODO: Investigate if we need to continue to do this or if we can just process them
3499 // in-order.
3500 if (traverseTopToBottom) {
3501 for (int i = mChildren.size() - 1; i >= 0; --i) {
3502 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3503 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003504 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3505 traverseTopToBottom)) {
3506 return true;
3507 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003508 }
3509 }
3510 } else {
3511 final int count = mChildren.size();
3512 for (int i = 0; i < count; ++i) {
3513 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3514 final int appTokensCount = appTokens.size();
3515 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003516 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3517 traverseTopToBottom)) {
3518 return true;
3519 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003520 }
3521 }
3522 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003523 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003524 }
3525
Wale Ogunwale1666e312016-12-16 11:27:18 -08003526 void setExitingTokensHasVisible(boolean hasVisible) {
3527 for (int i = mChildren.size() - 1; i >= 0; --i) {
3528 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3529 for (int j = appTokens.size() - 1; j >= 0; --j) {
3530 appTokens.get(j).hasVisible = hasVisible;
3531 }
3532 }
3533 }
3534
3535 void removeExistingAppTokensIfPossible() {
3536 for (int i = mChildren.size() - 1; i >= 0; --i) {
3537 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3538 for (int j = appTokens.size() - 1; j >= 0; --j) {
3539 final AppWindowToken token = appTokens.get(j);
3540 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3541 && (!token.mIsExiting || token.isEmpty())) {
3542 // Make sure there is no animation running on this token, so any windows
3543 // associated with it will be removed as soon as their animations are
3544 // complete.
3545 token.mAppAnimator.clearAnimation();
3546 token.mAppAnimator.animating = false;
3547 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3548 "performLayout: App token exiting now removed" + token);
3549 token.removeIfPossible();
3550 }
3551 }
3552 }
3553 }
3554
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003555 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003556 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003557 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3558 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003559 // Apps and their containers are not allowed to specify an orientation while the
3560 // docked or freeform stack is visible...except for the home stack/task if the
3561 // docked stack is minimized and it actually set something.
3562 if (mHomeStack != null && mHomeStack.isVisible()
3563 && mDividerControllerLocked.isMinimizedDock()) {
3564 final int orientation = mHomeStack.getOrientation();
3565 if (orientation != SCREEN_ORIENTATION_UNSET) {
3566 return orientation;
3567 }
3568 }
3569 return SCREEN_ORIENTATION_UNSPECIFIED;
3570 }
3571
3572 final int orientation = super.getOrientation();
3573 if (orientation != SCREEN_ORIENTATION_UNSET
3574 && orientation != SCREEN_ORIENTATION_BEHIND) {
3575 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3576 "App is requesting an orientation, return " + orientation);
3577 return orientation;
3578 }
3579
3580 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003581 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003582 // The next app has not been requested to be visible, so we keep the current orientation
3583 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003584 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003585 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003586 }
3587
3588 /**
3589 * Window container class that contains all containers on this display that are not related to
3590 * Apps. E.g. status bar.
3591 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003592 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3593 /**
3594 * Compares two child window tokens returns -1 if the first is lesser than the second in
3595 * terms of z-order and 1 otherwise.
3596 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003597 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003598 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003599 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3600 token1.mOwnerCanManageAppTokens)
3601 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3602 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003603
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003604 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3605 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3606 return false;
3607 }
3608 final int req = w.mAttrs.screenOrientation;
3609 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3610 || req == SCREEN_ORIENTATION_UNSET) {
3611 return false;
3612 }
3613 return true;
3614 };
3615
Wale Ogunwale3a931692016-11-02 16:49:48 -07003616 private final String mName;
3617 NonAppWindowContainers(String name) {
3618 mName = name;
3619 }
3620
3621 void addChild(WindowToken token) {
3622 addChild(token, mWindowComparator);
3623 }
3624
3625 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003626 int getOrientation() {
3627 final WindowManagerPolicy policy = mService.mPolicy;
3628 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003629 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003630
3631 if (win != null) {
3632 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003633 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003634 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003635 if (mService.mKeyguardGoingAway) {
3636 // Keyguard can't affect the orientation if it is going away...
3637 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3638 return SCREEN_ORIENTATION_UNSET;
3639 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003640 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003641 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003642 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003643 }
3644
Andrii Kulian8ee72852017-03-10 10:36:45 -08003645 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003646
Jorim Jaggi75520d52017-08-24 17:23:19 +02003647 if (policy.isKeyguardShowingAndNotOccluded()
3648 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003649 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003650 }
3651
3652 return SCREEN_ORIENTATION_UNSET;
3653 }
3654
3655 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003656 String getName() {
3657 return mName;
3658 }
Robert Carr3b716242016-08-16 16:02:21 -07003659 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003660
3661 /**
3662 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3663 */
3664 @FunctionalInterface
3665 private interface Screenshoter<E> {
3666 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3667 boolean useIdentityTransform, int rotation);
3668 }
Craig Mautner59c00972012-07-30 12:10:24 -07003669}