blob: fe34834a8bed251eed432b9e774089152f66d3a5 [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 Ogunwale61911492017-10-11 08:50:50 -070019import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070020import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070021import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwale687b4272017-07-27 02:56:23 -070022import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070023import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
24import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070025import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070026import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
27import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale687b4272017-07-27 02:56:23 -070028import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
29import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070030import static android.view.Display.DEFAULT_DISPLAY;
31import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070032import static android.view.Surface.ROTATION_0;
33import static android.view.Surface.ROTATION_180;
34import static android.view.Surface.ROTATION_270;
35import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070036import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070037import static android.view.WindowManager.DOCKED_BOTTOM;
38import static android.view.WindowManager.DOCKED_INVALID;
39import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080040import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
41import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
42import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070043import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070044import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070045import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
46import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070047import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070048import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070049import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070050import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Wale Ogunwale494009b82016-10-21 09:01:38 -070051import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070052import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070053import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
54import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070055import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070056import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
57import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070058import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070059import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070060import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070061import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
62import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070063import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Jorim Jaggi75520d52017-08-24 17:23:19 +020064import static com.android.server.wm.AppTransition.TRANSIT_KEYGUARD_UNOCCLUDE;
Wale Ogunwale1666e312016-12-16 11:27:18 -080065import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070066import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070067import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
69import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070070import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwalec69694a2016-10-18 13:51:15 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070076import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070077import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080078import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070079import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070080import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070081import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070082import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070083import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070084import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080085import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -070086import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070087import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
88import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070089import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070090import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -070091import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
92import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070093import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER;
94import static com.android.server.wm.WindowManagerService.TYPE_LAYER_OFFSET;
95import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -070096import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070097import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070098import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070099import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700100import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700101import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700102import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700103import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700104import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700105import static com.android.server.wm.proto.DisplayProto.ABOVE_APP_WINDOWS;
106import static com.android.server.wm.proto.DisplayProto.BELOW_APP_WINDOWS;
107import static com.android.server.wm.proto.DisplayProto.DISPLAY_INFO;
108import static com.android.server.wm.proto.DisplayProto.DOCKED_STACK_DIVIDER_CONTROLLER;
109import static com.android.server.wm.proto.DisplayProto.DPI;
110import static com.android.server.wm.proto.DisplayProto.ID;
111import static com.android.server.wm.proto.DisplayProto.IME_WINDOWS;
112import static com.android.server.wm.proto.DisplayProto.PINNED_STACK_CONTROLLER;
Steven Timotiusf2d68892017-08-28 17:00:01 -0700113import static com.android.server.wm.proto.DisplayProto.ROTATION;
114import static com.android.server.wm.proto.DisplayProto.SCREEN_ROTATION_ANIMATION;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700115import static com.android.server.wm.proto.DisplayProto.STACKS;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700116import static com.android.server.wm.proto.DisplayProto.WINDOW_CONTAINER;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700117
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700118import android.annotation.CallSuper;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700119import android.annotation.NonNull;
Dean Harding3e5a1522017-10-06 09:19:01 -0700120import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700121import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700122import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700123import android.graphics.Bitmap;
Jorim Jaggi6a7a8592017-01-12 00:44:33 +0100124import android.graphics.GraphicBuffer;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700125import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800126import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700127import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700128import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100129import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700130import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700131import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700132import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700133import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700134import android.os.RemoteException;
135import android.os.SystemClock;
Chong Zhang8e89b312015-09-09 15:09:30 -0700136import android.util.DisplayMetrics;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800137import android.util.MutableBoolean;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700138import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700139import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700140import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700141import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700142import android.view.InputDevice;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700143import android.view.Surface;
144import android.view.SurfaceControl;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700145import android.view.WindowManagerPolicy;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700146
Bryce Lee48f4b572017-04-10 10:54:15 -0700147import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800148import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700149import com.android.internal.view.IInputMethodClient;
Craig Mautner59c00972012-07-30 12:10:24 -0700150
Robert Carr3b716242016-08-16 16:02:21 -0700151import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700152import java.io.PrintWriter;
153import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700154import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700155import java.util.HashMap;
156import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700157import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700158import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800159import java.util.function.Consumer;
160import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700161
Craig Mautner59c00972012-07-30 12:10:24 -0700162/**
163 * Utility class for keeping track of the WindowStates and other pertinent contents of a
164 * particular Display.
165 *
166 * IMPORTANT: No method from this class should ever be used without holding
167 * WindowManagerService.mWindowMap.
168 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700169class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700170 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700171
172 /** Unique identifier of this stack. */
173 private final int mDisplayId;
174
Wale Ogunwale3a931692016-11-02 16:49:48 -0700175 /** The containers below are the only child containers the display can have. */
176 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700177 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700178 // Contains all non-app window containers that should be displayed above the app containers
179 // (e.g. Status bar)
180 private final NonAppWindowContainers mAboveAppWindowsContainers =
181 new NonAppWindowContainers("mAboveAppWindowsContainers");
182 // Contains all non-app window containers that should be displayed below the app containers
183 // (e.g. Wallpaper).
184 private final NonAppWindowContainers mBelowAppWindowsContainers =
185 new NonAppWindowContainers("mBelowAppWindowsContainers");
186 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
187 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
188 // window containers together and move them in-sync if/when needed.
189 private final NonAppWindowContainers mImeWindowsContainers =
190 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700191
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000192 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800193 private WindowState mTmpWindow2;
194 private WindowAnimator mTmpWindowAnimator;
195 private boolean mTmpRecoveringMemory;
196 private boolean mUpdateImeTarget;
197 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700198 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700199
Wale Ogunwale02319a62016-09-26 15:21:22 -0700200 // Mapping from a token IBinder to a WindowToken object on this display.
201 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
202
Andrii Kuliancd097992017-03-23 18:31:59 -0700203 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700204 int mInitialDisplayWidth = 0;
205 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700206 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700207
208 /**
209 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
210 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
211 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
212 */
Craig Mautner59c00972012-07-30 12:10:24 -0700213 int mBaseDisplayWidth = 0;
214 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700215 /**
216 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
217 * but can be set from Settings or via shell command "adb shell wm density".
218 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
219 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700220 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700221 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700222 private final DisplayInfo mDisplayInfo = new DisplayInfo();
223 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700224 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Andrii Kulian06d07d62017-03-14 11:11:47 -0700225 /**
226 * For default display it contains real metrics, empty for others.
227 * @see WindowManagerService#createWatermarkInTransaction()
228 */
229 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
230 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
231 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700232
Andrii Kulian06d07d62017-03-14 11:11:47 -0700233 /**
234 * Compat metrics computed based on {@link #mDisplayMetrics}.
235 * @see #updateDisplayAndOrientation(int)
236 */
237 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
238
239 /** The desired scaling factor for compatible apps. */
240 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700241
Andrii Kulian8ee72852017-03-10 10:36:45 -0800242 /**
243 * Current rotation of the display.
244 * Constants as per {@link android.view.Surface.Rotation}.
245 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700246 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800247 */
248 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700249
Andrii Kulian8ee72852017-03-10 10:36:45 -0800250 /**
251 * Last applied orientation of the display.
252 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
253 *
254 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
255 */
256 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700257
Andrii Kulian8ee72852017-03-10 10:36:45 -0800258 /**
259 * Flag indicating that the application is receiving an orientation that has different metrics
260 * than it expected. E.g. Portrait instead of Landscape.
261 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700262 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800263 */
264 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700265
Andrii Kulian8ee72852017-03-10 10:36:45 -0800266 /**
267 * Orientation forced by some window. If there is no visible window that specifies orientation
268 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
269 *
270 * @see NonAppWindowContainers#getOrientation()
271 */
272 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700273
Andrii Kulian8ee72852017-03-10 10:36:45 -0800274 /**
275 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
276 * occluded.
277 *
278 * @see NonAppWindowContainers#getOrientation()
279 */
280 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
281
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700282 /**
283 * Keep track of wallpaper visibility to notify changes.
284 */
285 private boolean mLastWallpaperVisible = false;
286
Andrii Kulian06d07d62017-03-14 11:11:47 -0700287 private Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700288 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700289
Craig Mautner39834192012-09-02 07:47:24 -0700290 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700291 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700292 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700293 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800294 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700295
Craig Mautnerdc548482014-02-05 13:35:24 -0800296 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700297 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800298
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700299 /** Detect user tapping outside of current focused task bounds .*/
300 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700301
Craig Mautner6601b7b2013-04-29 10:29:11 -0700302 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700303 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700304
Craig Mautner6601b7b2013-04-29 10:29:11 -0700305 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800306 private final Rect mTmpRect = new Rect();
307 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700308 private final RectF mTmpRectF = new RectF();
309 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800310 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700311
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800312 WindowManagerService mService;
Craig Mautner9d808b12013-08-06 18:00:25 -0700313
Craig Mautner95da1082014-02-24 17:54:35 -0800314 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700315 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800316
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700317 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700318 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700319
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800320 DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700321
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800322 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
323
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000324 private boolean mHaveBootMsg = false;
325 private boolean mHaveApp = false;
326 private boolean mHaveWallpaper = false;
327 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700328
329 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
330
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700331 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
332 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000333 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
334 new ApplySurfaceChangesTransactionState();
335 private final ScreenshotApplicationState mScreenshotApplicationState =
336 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700337
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700338 // True if this display is in the process of being removed. Used to determine if the removal of
339 // the display's direct children should be allowed.
340 private boolean mRemovingDisplay = false;
341
Bryce Leed1871262017-06-12 14:12:29 -0700342 // {@code false} if this display is in the processing of being created.
343 private boolean mDisplayReady = false;
344
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700345 private final WindowLayersController mLayersController;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800346 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700347 int mInputMethodAnimLayerAdjustment;
348
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800349 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
350 WindowStateAnimator winAnimator = w.mWinAnimator;
351 if (winAnimator.hasSurface()) {
352 final boolean wasAnimating = winAnimator.mWasAnimating;
353 final boolean nowAnimating = winAnimator.stepAnimationLocked(
354 mTmpWindowAnimator.mCurrentTime);
355 winAnimator.mWasAnimating = nowAnimating;
356 mTmpWindowAnimator.orAnimating(nowAnimating);
357
358 if (DEBUG_WALLPAPER) Slog.v(TAG,
359 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
360
361 if (wasAnimating && !winAnimator.mAnimating
362 && mWallpaperController.isWallpaperTarget(w)) {
363 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
364 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
365 if (DEBUG_LAYOUT_REPEATS) {
366 mService.mWindowPlacerLocked.debugLayoutRepeats(
367 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
368 }
369 }
370 }
371
372 final AppWindowToken atoken = w.mAppToken;
373 if (winAnimator.mDrawState == READY_TO_SHOW) {
374 if (atoken == null || atoken.allDrawn) {
375 if (w.performShowLocked()) {
376 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
377 if (DEBUG_LAYOUT_REPEATS) {
378 mService.mWindowPlacerLocked.debugLayoutRepeats(
379 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
380 }
381 }
382 }
383 }
384 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
385 if (appAnimator != null && appAnimator.thumbnail != null) {
386 if (appAnimator.thumbnailTransactionSeq
387 != mTmpWindowAnimator.mAnimTransactionSequence) {
388 appAnimator.thumbnailTransactionSeq =
389 mTmpWindowAnimator.mAnimTransactionSequence;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800390 }
391 }
392 };
393
394 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
395 final WindowStateAnimator winAnimator = w.mWinAnimator;
396 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
397 return;
398 }
399
400 final int flags = w.mAttrs.flags;
401
402 // If this window is animating, make a note that we have an animating window and take
403 // care of a request to run a detached wallpaper animation.
404 if (winAnimator.mAnimating) {
405 if (winAnimator.mAnimation != null) {
406 if ((flags & FLAG_SHOW_WALLPAPER) != 0
407 && winAnimator.mAnimation.getDetachWallpaper()) {
408 mTmpWindow = w;
409 }
410 final int color = winAnimator.mAnimation.getBackgroundColor();
411 if (color != 0) {
412 final TaskStack stack = w.getStack();
413 if (stack != null) {
414 stack.setAnimationBackground(winAnimator, color);
415 }
416 }
417 }
418 mTmpWindowAnimator.setAnimating(true);
419 }
420
421 // If this window's app token is running a detached wallpaper animation, make a note so
422 // we can ensure the wallpaper is displayed behind it.
423 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
424 if (appAnimator != null && appAnimator.animation != null
425 && appAnimator.animating) {
426 if ((flags & FLAG_SHOW_WALLPAPER) != 0
427 && appAnimator.animation.getDetachWallpaper()) {
428 mTmpWindow = w;
429 }
430
431 final int color = appAnimator.animation.getBackgroundColor();
432 if (color != 0) {
433 final TaskStack stack = w.getStack();
434 if (stack != null) {
435 stack.setAnimationBackground(winAnimator, color);
436 }
437 }
438 }
439 };
440
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800441 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
442 final int lostFocusUid = mTmpWindow.mOwnerUid;
443 final Handler handler = mService.mH;
444 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
445 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
446 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
447 w.mAttrs.hideTimeoutMilliseconds);
448 }
449 }
450 };
451
452 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
453 final AppWindowToken focusedApp = mService.mFocusedApp;
454 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
455 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
456
457 if (!w.canReceiveKeys()) {
458 return false;
459 }
460
461 final AppWindowToken wtoken = w.mAppToken;
462
463 // If this window's application has been removed, just skip it.
464 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
465 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
466 + (wtoken.removed ? "removed" : "sendingToBottom"));
467 return false;
468 }
469
470 if (focusedApp == null) {
471 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
472 + " using new focus @ " + w);
473 mTmpWindow = w;
474 return true;
475 }
476
477 if (!focusedApp.windowsAreFocusable()) {
478 // Current focused app windows aren't focusable...
479 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
480 + " focusable using new focus @ " + w);
481 mTmpWindow = w;
482 return true;
483 }
484
485 // Descend through all of the app tokens and find the first that either matches
486 // win.mAppToken (return win) or mFocusedApp (return null).
487 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
488 if (focusedApp.compareTo(wtoken) > 0) {
489 // App stack below focused app stack. No focus for you!!!
490 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
491 "findFocusedWindow: Reached focused app=" + focusedApp);
492 mTmpWindow = null;
493 return true;
494 }
495 }
496
497 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
498 mTmpWindow = w;
499 return true;
500 };
501
502 private final Consumer<WindowState> mPrepareWindowSurfaces =
503 w -> w.mWinAnimator.prepareSurfaceLocked(true);
504
505 private final Consumer<WindowState> mPerformLayout = w -> {
506 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
507 // wasting time and funky changes while a window is animating away.
508 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
509 || w.isGoneForLayoutLw();
510
511 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
512 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
513 + " mLayoutAttached=" + w.mLayoutAttached
514 + " screen changed=" + w.isConfigChanged());
515 final AppWindowToken atoken = w.mAppToken;
516 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
517 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
518 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
519 + " parentHidden=" + w.isParentWindowHidden());
520 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
521 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
522 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
523 + " parentHidden=" + w.isParentWindowHidden());
524 }
525
526 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
527 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
528 // since that means "perform layout as normal, just don't display").
529 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
530 || ((w.isConfigChanged() || w.setReportResizeHints())
531 && !w.isGoneForLayoutLw() &&
532 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
533 (w.mHasSurface && w.mAppToken != null &&
534 w.mAppToken.layoutConfigChanges)))) {
535 if (!w.mLayoutAttached) {
536 if (mTmpInitial) {
537 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
538 w.mContentChanged = false;
539 }
540 if (w.mAttrs.type == TYPE_DREAM) {
541 // Don't layout windows behind a dream, so that if it does stuff like hide
542 // the status bar we won't get a bad transition when it goes away.
543 mTmpWindow = w;
544 }
545 w.mLayoutNeeded = false;
546 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200547 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800548 mService.mPolicy.layoutWindowLw(w, null);
549 w.mLayoutSeq = mService.mLayoutSeq;
550
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200551 // If this is the first layout, we need to initialize the last inset values as
552 // otherwise we'd immediately cause an unnecessary resize.
553 if (firstLayout) {
554 w.updateLastInsetValues();
555 }
556
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800557 // Window frames may have changed. Update dim layer with the new bounds.
558 final Task task = w.getTask();
559 if (task != null) {
560 mDimLayerController.updateDimLayer(task);
561 }
562
563 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
564 + " mContainingFrame=" + w.mContainingFrame
565 + " mDisplayFrame=" + w.mDisplayFrame);
566 }
567 }
568 };
569
570 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
571 if (w.mLayoutAttached) {
572 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
573 + " mViewVisibility=" + w.mViewVisibility
574 + " mRelayoutCalled=" + w.mRelayoutCalled);
575 // If this view is GONE, then skip it -- keep the current frame, and let the caller
576 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
577 // windows, since that means "perform layout as normal, just don't display").
578 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
579 return;
580 }
581 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
582 || w.mLayoutNeeded) {
583 if (mTmpInitial) {
584 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
585 w.mContentChanged = false;
586 }
587 w.mLayoutNeeded = false;
588 w.prelayout();
589 mService.mPolicy.layoutWindowLw(w, w.getParentWindow());
590 w.mLayoutSeq = mService.mLayoutSeq;
591 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
592 + " mContainingFrame=" + w.mContainingFrame
593 + " mDisplayFrame=" + w.mDisplayFrame);
594 }
595 } else if (w.mAttrs.type == TYPE_DREAM) {
596 // Don't layout windows behind a dream, so that if it does stuff like hide the
597 // status bar we won't get a bad transition when it goes away.
598 mTmpWindow = mTmpWindow2;
599 }
600 };
601
602 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
603 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
604 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
605 return w.canBeImeTarget();
606 };
607
608 private final Consumer<WindowState> mApplyPostLayoutPolicy =
609 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
610 mService.mInputMethodTarget);
611
612 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
613 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
614 final boolean obscuredChanged = w.mObscured !=
615 mTmpApplySurfaceChangesTransactionState.obscured;
616 final RootWindowContainer root = mService.mRoot;
617 // Only used if default window
618 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
619
620 // Update effect.
621 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
622 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
623 final boolean isDisplayed = w.isDisplayedLw();
624
625 if (isDisplayed && w.isObscuringDisplay()) {
626 // This window completely covers everything behind it, so we want to leave all
627 // of them as undimmed (for performance reasons).
628 root.mObscuringWindow = w;
629 mTmpApplySurfaceChangesTransactionState.obscured = true;
630 }
631
632 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
633 root.handleNotObscuredLocked(w,
634 mTmpApplySurfaceChangesTransactionState.obscured,
635 mTmpApplySurfaceChangesTransactionState.syswin);
636
637 if (w.mHasSurface && isDisplayed) {
638 final int type = w.mAttrs.type;
639 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
640 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
641 mTmpApplySurfaceChangesTransactionState.syswin = true;
642 }
643 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
644 && w.mAttrs.preferredRefreshRate != 0) {
645 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
646 = w.mAttrs.preferredRefreshRate;
647 }
648 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
649 && w.mAttrs.preferredDisplayModeId != 0) {
650 mTmpApplySurfaceChangesTransactionState.preferredModeId
651 = w.mAttrs.preferredDisplayModeId;
652 }
653 }
654 }
655
656 w.applyDimLayerIfNeeded();
657
658 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
659 && mWallpaperController.isWallpaperTarget(w)) {
660 // This is the wallpaper target and its obscured state changed... make sure the
661 // current wallpaper's visibility has been updated accordingly.
662 mWallpaperController.updateWallpaperVisibility();
663 }
664
665 w.handleWindowMovedIfNeeded();
666
667 final WindowStateAnimator winAnimator = w.mWinAnimator;
668
669 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
670 w.mContentChanged = false;
671
672 // Moved from updateWindowsAndWallpaperLocked().
673 if (w.mHasSurface) {
674 // Take care of the window being ready to display.
675 final boolean committed = winAnimator.commitFinishDrawingLocked();
676 if (isDefaultDisplay && committed) {
677 if (w.mAttrs.type == TYPE_DREAM) {
678 // HACK: When a dream is shown, it may at that point hide the lock screen.
679 // So we need to redo the layout to let the phone window manager make this
680 // happen.
681 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
682 if (DEBUG_LAYOUT_REPEATS) {
683 surfacePlacer.debugLayoutRepeats(
684 "dream and commitFinishDrawingLocked true",
685 pendingLayoutChanges);
686 }
687 }
688 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
689 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
690 "First draw done in potential wallpaper target " + w);
691 root.mWallpaperMayChange = true;
692 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
693 if (DEBUG_LAYOUT_REPEATS) {
694 surfacePlacer.debugLayoutRepeats(
695 "wallpaper and commitFinishDrawingLocked true",
696 pendingLayoutChanges);
697 }
698 }
699 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700700 final TaskStack stack = w.getStack();
701 if ((!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening())
702 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800703 // Updates the shown frame before we set up the surface. This is needed
704 // because the resizing could change the top-left position (in addition to
705 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
706 // position the surface.
707 //
708 // If an animation is being started, we can't call this method because the
709 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700710 // we do want to update the position if the window is animating. We make an exception
711 // for the bounds animating state, where an application may have been waiting
712 // for an exit animation to start, but instead enters PiP. We need to ensure
713 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800714 winAnimator.computeShownFrameLocked();
715 }
716 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
717 }
718
719 final AppWindowToken atoken = w.mAppToken;
720 if (atoken != null) {
721 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
722 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
723 mTmpUpdateAllDrawn.add(atoken);
724 }
725 }
726
727 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
728 && w.isDisplayedLw()) {
729 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
730 }
731
732 w.updateResizingWindowIfNeeded();
733 };
734
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800735 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800736 * Create new {@link DisplayContent} instance, add itself to the root window container and
737 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700738 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800739 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700740 * @param layersController window layer controller used to assign layer to the windows on this
741 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700742 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
743 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700744 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700745 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700746 WindowLayersController layersController, WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800747 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
748 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
749 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
750 + " new=" + display);
751 }
752
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700753 mDisplay = display;
754 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700755 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700756 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700757 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700758 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700759 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700760 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700761 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800762 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700763 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800764 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700765
766 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700767 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700768 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700769 super.addChild(mAboveAppWindowsContainers, null);
770 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800771
772 // Add itself as a child to the root container.
773 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700774
775 // TODO(b/62541591): evaluate whether this is the best spot to declare the
776 // {@link DisplayContent} ready for use.
777 mDisplayReady = true;
778 }
779
780 boolean isReady() {
781 // The display is ready when the system and the individual display are both ready.
782 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700783 }
784
785 int getDisplayId() {
786 return mDisplayId;
787 }
788
Wale Ogunwale02319a62016-09-26 15:21:22 -0700789 WindowToken getWindowToken(IBinder binder) {
790 return mTokenMap.get(binder);
791 }
792
793 AppWindowToken getAppWindowToken(IBinder binder) {
794 final WindowToken token = getWindowToken(binder);
795 if (token == null) {
796 return null;
797 }
798 return token.asAppWindowToken();
799 }
800
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700801 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700802 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
803 if (dc != null) {
804 // We currently don't support adding a window token to the display if the display
805 // already has the binder mapped to another token. If there is a use case for supporting
806 // this moving forward we will either need to merge the WindowTokens some how or have
807 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700808 throw new IllegalArgumentException("Can't map token=" + token + " to display="
809 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700810 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700811 if (binder == null) {
812 throw new IllegalArgumentException("Can't map token=" + token + " to display="
813 + getName() + " binder is null");
814 }
815 if (token == null) {
816 throw new IllegalArgumentException("Can't map null token to display="
817 + getName() + " binder=" + binder);
818 }
819
Wale Ogunwale02319a62016-09-26 15:21:22 -0700820 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700821
822 if (token.asAppWindowToken() == null) {
823 // Add non-app token to container hierarchy on the display. App tokens are added through
824 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700825 switch (token.windowType) {
826 case TYPE_WALLPAPER:
827 mBelowAppWindowsContainers.addChild(token);
828 break;
829 case TYPE_INPUT_METHOD:
830 case TYPE_INPUT_METHOD_DIALOG:
831 mImeWindowsContainers.addChild(token);
832 break;
833 default:
834 mAboveAppWindowsContainers.addChild(token);
835 break;
836 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700837 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700838 }
839
840 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700841 final WindowToken token = mTokenMap.remove(binder);
842 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800843 token.setExiting();
844 }
845 return token;
846 }
847
848 /** Changes the display the input window token is housed on to this one. */
849 void reParentWindowToken(WindowToken token) {
850 final DisplayContent prevDc = token.getDisplayContent();
851 if (prevDc == this) {
852 return;
853 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800854 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
855 && token.asAppWindowToken() == null) {
856 // Removed the token from the map, but made sure it's not an app token before removing
857 // from parent.
858 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700859 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800860
861 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700862 }
863
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700864 void removeAppToken(IBinder binder) {
865 final WindowToken token = removeWindowToken(binder);
866 if (token == null) {
867 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
868 return;
869 }
870
871 final AppWindowToken appToken = token.asAppWindowToken();
872
873 if (appToken == null) {
874 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
875 return;
876 }
877
878 appToken.onRemovedFromDisplay();
879 }
880
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700881 Display getDisplay() {
882 return mDisplay;
883 }
884
Craig Mautner59c00972012-07-30 12:10:24 -0700885 DisplayInfo getDisplayInfo() {
886 return mDisplayInfo;
887 }
888
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700889 DisplayMetrics getDisplayMetrics() {
890 return mDisplayMetrics;
891 }
892
Andrii Kulian8ee72852017-03-10 10:36:45 -0800893 int getRotation() {
894 return mRotation;
895 }
896
897 void setRotation(int newRotation) {
898 mRotation = newRotation;
899 }
900
901 int getLastOrientation() {
902 return mLastOrientation;
903 }
904
905 void setLastOrientation(int orientation) {
906 mLastOrientation = orientation;
907 }
908
909 boolean getAltOrientation() {
910 return mAltOrientation;
911 }
912
913 void setAltOrientation(boolean altOrientation) {
914 mAltOrientation = altOrientation;
915 }
916
917 int getLastWindowForcedOrientation() {
918 return mLastWindowForcedOrientation;
919 }
920
Andrii Kulian06d07d62017-03-14 11:11:47 -0700921 /**
922 * Update rotation of the display.
923 *
924 * Returns true if the rotation has been changed. In this case YOU MUST CALL
925 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
926 */
927 boolean updateRotationUnchecked(boolean inTransaction) {
928 if (mService.mDeferredRotationPauseCount > 0) {
929 // Rotation updates have been paused temporarily. Defer the update until
930 // updates have been resumed.
931 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
932 return false;
933 }
934
935 ScreenRotationAnimation screenRotationAnimation =
936 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
937 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
938 // Rotation updates cannot be performed while the previous rotation change
939 // animation is still in progress. Skip this update. We will try updating
940 // again after the animation is finished and the display is unfrozen.
941 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
942 return false;
943 }
944 if (mService.mDisplayFrozen) {
945 // Even if the screen rotation animation has finished (e.g. isAnimating
946 // returns false), there is still some time where we haven't yet unfrozen
947 // the display. We also need to abort rotation here.
948 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
949 "Deferring rotation, still finishing previous rotation");
950 return false;
951 }
952
953 if (!mService.mDisplayEnabled) {
954 // No point choosing a rotation if the display is not enabled.
955 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
956 return false;
957 }
958
959 final int oldRotation = mRotation;
960 final int lastOrientation = mLastOrientation;
961 final boolean oldAltOrientation = mAltOrientation;
962 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr0e007272017-10-02 13:00:55 -0700963 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
Robert Carr897215d2017-03-29 12:25:50 -0700964 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700965
Robert Carr0e007272017-10-02 13:00:55 -0700966 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700967 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
968 if (seamlessRotated != null) {
969 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
970 // to complete (that is, waiting for windows to redraw). It's tempting to check
971 // w.mSeamlessRotationCount but that could be incorrect in the case of
972 // window-removal.
973 return false;
974 }
Robert Carr0e007272017-10-02 13:00:55 -0700975
976 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700977 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -0700978 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -0700979 mayRotateSeamlessly = false;
980 }
981 for (int i = 0; i < mService.mSessions.size(); i++) {
982 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
983 mayRotateSeamlessly = false;
984 break;
985 }
986 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700987 }
Robert Carr0e007272017-10-02 13:00:55 -0700988 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700989
990 // TODO: Implement forced rotation changes.
991 // Set mAltOrientation to indicate that the application is receiving
992 // an orientation that has different metrics than it expected.
993 // eg. Portrait instead of Landscape.
994
995 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
996 lastOrientation, rotation);
997
998 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
999 + ", got rotation " + rotation + " which has "
1000 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1001
1002 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1003 // No change.
1004 return false;
1005 }
1006
1007 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
1008 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1009 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1010
1011 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1012 mService.mWaitingForConfig = true;
1013 }
1014
1015 mRotation = rotation;
1016 mAltOrientation = altOrientation;
1017 if (isDefaultDisplay) {
1018 mService.mPolicy.setRotationLw(rotation);
1019 }
1020
1021 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1022 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1023 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1024 WINDOW_FREEZE_TIMEOUT_DURATION);
1025
1026 setLayoutNeeded();
1027 final int[] anim = new int[2];
1028 if (isDimming()) {
1029 anim[0] = anim[1] = 0;
1030 } else {
1031 mService.mPolicy.selectRotationAnimationLw(anim);
1032 }
1033
1034 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001035 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001036 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1037 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1038 mDisplayId);
1039 } else {
1040 // The screen rotation animation uses a screenshot to freeze the screen
1041 // while windows resize underneath.
1042 // When we are rotating seamlessly, we allow the elements to transition
1043 // to their rotated state independently and without a freeze required.
1044 screenRotationAnimation = null;
1045
1046 // We have to reset this in case a window was removed before it
1047 // finished seamless rotation.
1048 mService.mSeamlessRotationCount = 0;
1049 }
1050
1051 // We need to update our screen size information to match the new rotation. If the rotation
1052 // has actually changed then this method will return true and, according to the comment at
1053 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1054 // By updating the Display info here it will be available to
1055 // #computeScreenConfiguration() later.
1056 updateDisplayAndOrientation(getConfiguration().uiMode);
1057
1058 if (!inTransaction) {
1059 if (SHOW_TRANSACTIONS) {
1060 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1061 }
1062 mService.openSurfaceTransaction();
1063 }
1064 try {
1065 // NOTE: We disable the rotation in the emulator because
1066 // it doesn't support hardware OpenGL emulation yet.
1067 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1068 && screenRotationAnimation.hasScreenshot()) {
1069 if (screenRotationAnimation.setRotationInTransaction(
1070 rotation, mService.mFxSession,
1071 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1072 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1073 mService.scheduleAnimationLocked();
1074 }
1075 }
1076
1077 if (rotateSeamlessly) {
1078 forAllWindows(w -> {
1079 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1080 }, true /* traverseTopToBottom */);
1081 }
1082
1083 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1084 } finally {
1085 if (!inTransaction) {
1086 mService.closeSurfaceTransaction();
1087 if (SHOW_LIGHT_TRANSACTIONS) {
1088 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1089 }
1090 }
1091 }
1092
1093 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001094 if (w.mHasSurface && !rotateSeamlessly) {
1095 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001096 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001097 mService.mRoot.mOrientationChangeComplete = false;
1098 w.mLastFreezeDuration = 0;
1099 }
1100 w.mReportOrientationChanged = true;
1101 }, true /* traverseTopToBottom */);
1102
1103 if (rotateSeamlessly) {
1104 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1105 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1106 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1107 }
1108
1109 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1110 final WindowManagerService.RotationWatcher rotationWatcher
1111 = mService.mRotationWatchers.get(i);
1112 if (rotationWatcher.mDisplayId == mDisplayId) {
1113 try {
1114 rotationWatcher.mWatcher.onRotationChanged(rotation);
1115 } catch (RemoteException e) {
1116 // Ignore
1117 }
1118 }
1119 }
1120
1121 // TODO (multi-display): Magnification is supported only for the default display.
1122 // Announce rotation only if we will not animate as we already have the
1123 // windows in final state. Otherwise, we make this call at the rotation end.
1124 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1125 && isDefaultDisplay) {
1126 mService.mAccessibilityController.onRotationChangedLocked(this);
1127 }
1128
1129 return true;
1130 }
1131
1132 /**
1133 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1134 * changed.
1135 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1136 */
1137 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1138 // Use the effective "visual" dimensions based on current rotation
1139 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1140 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1141 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1142 int dw = realdw;
1143 int dh = realdh;
1144
1145 if (mAltOrientation) {
1146 if (realdw > realdh) {
1147 // Turn landscape into portrait.
1148 int maxw = (int)(realdh/1.3f);
1149 if (maxw < realdw) {
1150 dw = maxw;
1151 }
1152 } else {
1153 // Turn portrait into landscape.
1154 int maxh = (int)(realdw/1.3f);
1155 if (maxh < realdh) {
1156 dh = maxh;
1157 }
1158 }
1159 }
1160
1161 // Update application display metrics.
1162 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1163 mDisplayId);
1164 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1165 mDisplayId);
1166 mDisplayInfo.rotation = mRotation;
1167 mDisplayInfo.logicalWidth = dw;
1168 mDisplayInfo.logicalHeight = dh;
1169 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1170 mDisplayInfo.appWidth = appWidth;
1171 mDisplayInfo.appHeight = appHeight;
1172 if (isDefaultDisplay) {
1173 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1174 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1175 }
1176 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1177 if (mDisplayScalingDisabled) {
1178 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1179 } else {
1180 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1181 }
1182
1183 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1184 mDisplayInfo);
1185
1186 mBaseDisplayRect.set(0, 0, dw, dh);
1187
1188 if (isDefaultDisplay) {
1189 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1190 mCompatDisplayMetrics);
1191 }
1192 return mDisplayInfo;
1193 }
1194
1195 /**
1196 * Compute display configuration based on display properties and policy settings.
1197 * Do not call if mDisplayReady == false.
1198 */
1199 void computeScreenConfiguration(Configuration config) {
1200 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1201
1202 final int dw = displayInfo.logicalWidth;
1203 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001204 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1205 // TODO: Probably best to set this based on some setting in the display content object,
1206 // so the display can be configured for things like fullscreen.
1207 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001208
Andrii Kulian06d07d62017-03-14 11:11:47 -07001209 config.screenWidthDp =
1210 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1211 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1212 config.screenHeightDp =
1213 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1214 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001215
1216 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1217 final int leftInset = mTmpRect.left;
1218 final int topInset = mTmpRect.top;
1219 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001220 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1221 leftInset + displayInfo.appWidth /* right */,
1222 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001223 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1224 || displayInfo.rotation == Surface.ROTATION_270);
1225
1226 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1227 mDisplayMetrics.density, config);
1228
1229 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1230 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1231 ? Configuration.SCREENLAYOUT_ROUND_YES
1232 : Configuration.SCREENLAYOUT_ROUND_NO);
1233
1234 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1235 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1236 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1237 dh, mDisplayId);
1238 config.densityDpi = displayInfo.logicalDensityDpi;
1239
1240 config.colorMode =
1241 (displayInfo.isHdr()
1242 ? Configuration.COLOR_MODE_HDR_YES
1243 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001244 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001245 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1246 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1247
1248 // Update the configuration based on available input devices, lid switch,
1249 // and platform configuration.
1250 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1251 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1252 config.navigation = Configuration.NAVIGATION_NONAV;
1253
1254 int keyboardPresence = 0;
1255 int navigationPresence = 0;
1256 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1257 final int len = devices != null ? devices.length : 0;
1258 for (int i = 0; i < len; i++) {
1259 InputDevice device = devices[i];
1260 if (!device.isVirtual()) {
1261 final int sources = device.getSources();
1262 final int presenceFlag = device.isExternal() ?
1263 WindowManagerPolicy.PRESENCE_EXTERNAL :
1264 WindowManagerPolicy.PRESENCE_INTERNAL;
1265
1266 // TODO(multi-display): Configure on per-display basis.
1267 if (mService.mIsTouchDevice) {
1268 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1269 InputDevice.SOURCE_TOUCHSCREEN) {
1270 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1271 }
1272 } else {
1273 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1274 }
1275
1276 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1277 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1278 navigationPresence |= presenceFlag;
1279 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1280 && config.navigation == Configuration.NAVIGATION_NONAV) {
1281 config.navigation = Configuration.NAVIGATION_DPAD;
1282 navigationPresence |= presenceFlag;
1283 }
1284
1285 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1286 config.keyboard = Configuration.KEYBOARD_QWERTY;
1287 keyboardPresence |= presenceFlag;
1288 }
1289 }
1290 }
1291
1292 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1293 config.navigation = Configuration.NAVIGATION_DPAD;
1294 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1295 }
1296
1297 // Determine whether a hard keyboard is available and enabled.
1298 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1299 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1300 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1301 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1302 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1303 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1304 }
1305
1306 // Let the policy update hidden states.
1307 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1308 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1309 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1310 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1311 }
1312
1313 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1314 int displayId) {
1315 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1316 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1317 final int unrotDw, unrotDh;
1318 if (rotated) {
1319 unrotDw = dh;
1320 unrotDh = dw;
1321 } else {
1322 unrotDw = dw;
1323 unrotDh = dh;
1324 }
1325 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1326 displayId);
1327 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1328 displayId);
1329 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1330 displayId);
1331 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1332 displayId);
1333 return sw;
1334 }
1335
1336 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1337 DisplayMetrics dm, int dw, int dh, int displayId) {
1338 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1339 displayId);
1340 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1341 uiMode, displayId);
1342 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1343 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1344 if (curSize == 0 || size < curSize) {
1345 curSize = size;
1346 }
1347 return curSize;
1348 }
1349
1350 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1351 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1352
1353 // We need to determine the smallest width that will occur under normal
1354 // operation. To this, start with the base screen size and compute the
1355 // width under the different possible rotations. We need to un-rotate
1356 // the current screen dimensions before doing this.
1357 int unrotDw, unrotDh;
1358 if (rotated) {
1359 unrotDw = dh;
1360 unrotDh = dw;
1361 } else {
1362 unrotDw = dw;
1363 unrotDh = dh;
1364 }
1365 displayInfo.smallestNominalAppWidth = 1<<30;
1366 displayInfo.smallestNominalAppHeight = 1<<30;
1367 displayInfo.largestNominalAppWidth = 0;
1368 displayInfo.largestNominalAppHeight = 0;
1369 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1370 unrotDh);
1371 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1372 unrotDw);
1373 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1374 unrotDh);
1375 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1376 unrotDw);
1377 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1378 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1379 displayId);
1380 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1381 displayId);
1382 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1383 displayId);
1384 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1385 displayId);
1386 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1387 outConfig.screenLayout = sl;
1388 }
1389
1390 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1391 int uiMode, int displayId) {
1392 // Get the app screen size at this rotation.
1393 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1394 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1395
1396 // Compute the screen layout size class for this rotation.
1397 int longSize = w;
1398 int shortSize = h;
1399 if (longSize < shortSize) {
1400 int tmp = longSize;
1401 longSize = shortSize;
1402 shortSize = tmp;
1403 }
1404 longSize = (int)(longSize/density);
1405 shortSize = (int)(shortSize/density);
1406 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1407 }
1408
1409 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1410 int uiMode, int dw, int dh) {
1411 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1412 displayId);
1413 if (width < displayInfo.smallestNominalAppWidth) {
1414 displayInfo.smallestNominalAppWidth = width;
1415 }
1416 if (width > displayInfo.largestNominalAppWidth) {
1417 displayInfo.largestNominalAppWidth = width;
1418 }
1419 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1420 displayId);
1421 if (height < displayInfo.smallestNominalAppHeight) {
1422 displayInfo.smallestNominalAppHeight = height;
1423 }
1424 if (height > displayInfo.largestNominalAppHeight) {
1425 displayInfo.largestNominalAppHeight = height;
1426 }
1427 }
1428
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001429 DockedStackDividerController getDockedDividerController() {
1430 return mDividerControllerLocked;
1431 }
1432
Winson Chung655332c2016-10-31 13:14:28 -07001433 PinnedStackController getPinnedStackController() {
1434 return mPinnedStackControllerLocked;
1435 }
1436
Jeff Browna506a6e2013-06-04 00:02:38 -07001437 /**
1438 * Returns true if the specified UID has access to this display.
1439 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001440 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001441 return mDisplay.hasAccess(uid);
1442 }
1443
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001444 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001445 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001446 }
1447
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001448 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001449 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001450 }
1451
Wale Ogunwale61911492017-10-11 08:50:50 -07001452 /**
1453 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1454 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001455 TaskStack getSplitScreenPrimaryStack() {
1456 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001457 return (stack != null && stack.isVisible()) ? stack : null;
1458 }
1459
1460 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001461 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001462 * not visible.
1463 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001464 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1465 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001466 }
1467
1468 TaskStack getPinnedStack() {
1469 return mTaskStackContainers.getPinnedStack();
1470 }
1471
1472 private boolean hasPinnedStack() {
1473 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001474 }
1475
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001476 /**
1477 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1478 * Null is no compatible stack on the display.
1479 */
1480 TaskStack getStack(int windowingMode) {
1481 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1482 }
1483
1484 /**
1485 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1486 * activity type. Null is no compatible stack on the display.
1487 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001488 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001489 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001490 }
1491
Bryce Lee48f4b572017-04-10 10:54:15 -07001492 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001493 TaskStack getTopStack() {
1494 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001495 }
1496
Wale Ogunwale61911492017-10-11 08:50:50 -07001497 void onStackWindowingModeChanged(TaskStack stack) {
1498 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001499 }
1500
Andrii Kulian441e4492016-09-29 15:25:00 -07001501 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001502 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001503 super.onConfigurationChanged(newParentConfig);
1504
Andrii Kulian3a507b52016-09-19 18:14:12 -07001505 // The display size information is heavily dependent on the resources in the current
1506 // configuration, so we need to reconfigure it every time the configuration changes.
1507 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1508 mService.reconfigureDisplayLocked(this);
1509
1510 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001511 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001512 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001513
Andrii Kulian441e4492016-09-29 15:25:00 -07001514 /**
1515 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1516 * bounds were updated.
1517 */
1518 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001519 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1520 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001521 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001522 changedStackList.add(stack.mStackId);
1523 }
1524 }
Winson Chung32c566f2017-04-11 18:31:21 -07001525
1526 // If there was no pinned stack, we still need to notify the controller of the display info
1527 // update as a result of the config change. We do this here to consolidate the flow between
1528 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001529 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001530 mPinnedStackControllerLocked.onDisplayInfoChanged();
1531 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001532 }
1533
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001534 @Override
1535 boolean fillsParent() {
1536 return true;
1537 }
1538
1539 @Override
1540 boolean isVisible() {
1541 return true;
1542 }
1543
1544 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001545 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001546 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001547 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001548 }
1549
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001550 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001551 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1552 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1553 // target, or here in order if there isn't an IME target.
1554 if (traverseTopToBottom) {
1555 for (int i = mChildren.size() - 1; i >= 0; --i) {
1556 final DisplayChildWindowContainer child = mChildren.get(i);
1557 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1558 // In this case the Ime windows will be processed above their target so we skip
1559 // here.
1560 continue;
1561 }
1562 if (child.forAllWindows(callback, traverseTopToBottom)) {
1563 return true;
1564 }
1565 }
1566 } else {
1567 final int count = mChildren.size();
1568 for (int i = 0; i < count; i++) {
1569 final DisplayChildWindowContainer child = mChildren.get(i);
1570 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1571 // In this case the Ime windows will be processed above their target so we skip
1572 // here.
1573 continue;
1574 }
1575 if (child.forAllWindows(callback, traverseTopToBottom)) {
1576 return true;
1577 }
1578 }
1579 }
1580 return false;
1581 }
1582
1583 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1584 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1585 }
1586
Wale Ogunwale399c8692017-05-08 14:22:42 -07001587 @Override
1588 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001589 final WindowManagerPolicy policy = mService.mPolicy;
1590
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001591 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001592 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001593 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001594 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001595 // If the display is frozen, some activities may be in the middle of restarting, and
1596 // thus have removed their old window. If the window has the flag to hide the lock
1597 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1598 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001599 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001600 } else if (policy.isKeyguardLocked()) {
1601 // Use the last orientation the while the display is frozen with the keyguard
1602 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1603 // window. We don't want to check the show when locked window directly though as
1604 // things aren't stable while the display is frozen, for example the window could be
1605 // momentarily unavailable due to activity relaunch.
1606 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001607 + "return " + mLastOrientation);
1608 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001609 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001610 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001611 final int orientation = mAboveAppWindowsContainers.getOrientation();
1612 if (orientation != SCREEN_ORIENTATION_UNSET) {
1613 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001614 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001615 }
1616
Wale Ogunwale399c8692017-05-08 14:22:42 -07001617 // Top system windows are not requesting an orientation. Start searching from apps.
1618 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001619 }
1620
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001621 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001622 // Check if display metrics changed and update base values if needed.
1623 updateBaseDisplayMetricsIfNeeded();
1624
Craig Mautner722285e2012-09-07 13:55:58 -07001625 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001626 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001627
Wale Ogunwale61911492017-10-11 08:50:50 -07001628 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1629 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001630 }
Craig Mautner722285e2012-09-07 13:55:58 -07001631 }
1632
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001633 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001634 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1635 if (displayManagerInternal != null) {
1636 // Bootstrap the default logical display from the display manager.
1637 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1638 if (newDisplayInfo != null) {
1639 mDisplayInfo.copyFrom(newDisplayInfo);
1640 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001641 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001642
Andrii Kuliancd097992017-03-23 18:31:59 -07001643 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1644 mDisplayInfo.logicalDensityDpi);
1645 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1646 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1647 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001648 }
1649
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001650 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001651 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001652 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001653 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001654 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1655 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001656 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001657 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001658 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001659 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001660 out.set(left, top, left + width, top + height);
1661 }
1662
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001663 private void getLogicalDisplayRect(Rect out, int orientation) {
1664 getLogicalDisplayRect(out);
1665
1666 // Rotate the Rect if needed.
1667 final int currentRotation = mDisplayInfo.rotation;
1668 final int rotationDelta = deltaRotation(currentRotation, orientation);
1669 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1670 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1671 mTmpRectF.set(out);
1672 mTmpMatrix.mapRect(mTmpRectF);
1673 mTmpRectF.round(out);
1674 }
1675 }
1676
Andrii Kuliancd097992017-03-23 18:31:59 -07001677 /**
1678 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1679 * values.
1680 */
1681 private void updateBaseDisplayMetricsIfNeeded() {
1682 // Get real display metrics without overrides from WM.
1683 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1684 final int orientation = mDisplayInfo.rotation;
1685 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1686 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1687 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1688 final int newDensity = mDisplayInfo.logicalDensityDpi;
1689
1690 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1691 || mInitialDisplayHeight != newHeight
1692 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1693
1694 if (displayMetricsChanged) {
1695 // Check if display size or density is forced.
1696 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1697 || mBaseDisplayHeight != mInitialDisplayHeight;
1698 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1699
1700 // If there is an override set for base values - use it, otherwise use new values.
1701 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1702 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1703 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1704
1705 // Real display metrics changed, so we should also update initial values.
1706 mInitialDisplayWidth = newWidth;
1707 mInitialDisplayHeight = newHeight;
1708 mInitialDisplayDensity = newDensity;
1709 mService.reconfigureDisplayLocked(this);
1710 }
1711 }
1712
Bryce Lee27cec322017-03-21 09:41:37 -07001713 /** Sets the maximum width the screen resolution can be */
1714 void setMaxUiWidth(int width) {
1715 if (DEBUG_DISPLAY) {
1716 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1717 }
1718
1719 mMaxUiWidth = width;
1720
1721 // Update existing metrics.
1722 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1723 }
1724
1725 /** Update base (override) display metrics. */
1726 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1727 mBaseDisplayWidth = baseWidth;
1728 mBaseDisplayHeight = baseHeight;
1729 mBaseDisplayDensity = baseDensity;
1730
1731 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1732 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1733 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1734 mBaseDisplayWidth = mMaxUiWidth;
1735
1736 if (DEBUG_DISPLAY) {
1737 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1738 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1739 + " on display:" + getDisplayId());
1740 }
1741 }
1742
1743 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1744 }
1745
Chong Zhangf66db432016-01-13 10:39:51 -08001746 void getContentRect(Rect out) {
1747 out.set(mContentRect);
1748 }
1749
Wale Ogunwale61911492017-10-11 08:50:50 -07001750 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001751 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1752 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001753
Wale Ogunwale61911492017-10-11 08:50:50 -07001754 final TaskStack stack = new TaskStack(mService, stackId, controller);
1755 mTaskStackContainers.addStackToDisplay(stack, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07001756
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001757 if (stack.inSplitScreenPrimaryWindowingMode()) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001758 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -07001759 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001760 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001761 }
1762
Andrii Kulian51c1b672017-04-07 18:39:32 -07001763 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001764 final DisplayContent prevDc = stack.getDisplayContent();
1765 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001766 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1767 + " which is not currently attached to any display");
1768 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001769 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001770 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1771 + " to its current displayId=" + mDisplayId);
1772 }
1773
Wale Ogunwale61911492017-10-11 08:50:50 -07001774 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001775 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001776 }
1777
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001778 @Override
1779 protected void addChild(DisplayChildWindowContainer child,
1780 Comparator<DisplayChildWindowContainer> comparator) {
1781 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1782 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001783
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001784 @Override
1785 protected void addChild(DisplayChildWindowContainer child, int index) {
1786 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1787 }
1788
1789 @Override
1790 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001791 // Only allow removal of direct children from this display if the display is in the process
1792 // of been removed.
1793 if (mRemovingDisplay) {
1794 super.removeChild(child);
1795 return;
1796 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001797 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001798 }
1799
Andrii Kuliand2765632016-12-12 22:26:34 -08001800 @Override
1801 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1802 // Children of the display are statically ordered, so the real intention here is to perform
1803 // the operation on the display and not the static direct children.
1804 getParent().positionChildAt(position, this, includingParents);
1805 }
1806
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001807 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001808 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1809 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001810 final int taskId = stack.taskIdFromPoint(x, y);
1811 if (taskId != -1) {
1812 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001813 }
1814 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001815 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001816 }
1817
Chong Zhang8e89b312015-09-09 15:09:30 -07001818 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001819 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001820 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001821 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001822 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001823 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001824 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07001825 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1826 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001827 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001828 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001829 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001830
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001831 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1832 if (mTmpTaskForResizePointSearchResult.searchDone) {
1833 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001834 }
1835 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001836 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001837 }
1838
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001839 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001840 // The provided task is the task on this display with focus, so if WindowManagerService's
1841 // focused app is not on this display, focusedTask will be null.
1842 if (focusedTask == null) {
1843 mTouchExcludeRegion.setEmpty();
1844 } else {
1845 mTouchExcludeRegion.set(mBaseDisplayRect);
1846 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1847 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07001848 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1849 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
David Stevensee9e2772017-02-09 16:30:27 -08001850 stack.setTouchExcludeRegion(
1851 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
1852 }
1853 // If we removed the focused task above, add it back and only leave its
1854 // outside touch area in the exclusion. TapDectector is not interested in
1855 // any touch inside the focused task itself.
1856 if (!mTmpRect2.isEmpty()) {
1857 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1858 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001859 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001860 final WindowState inputMethod = mService.mInputMethodWindow;
1861 if (inputMethod != null && inputMethod.isVisibleLw()) {
1862 // If the input method is visible and the user is typing, we don't want these touch
1863 // events to be intercepted and used to change focus. This would likely cause a
1864 // disappearance of the input method.
1865 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001866 if (inputMethod.getDisplayId() == mDisplayId) {
1867 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1868 } else {
1869 // IME is on a different display, so we need to update its tap detector.
1870 // TODO(multidisplay): Remove when IME will always appear on same display.
1871 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1872 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001873 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001874 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1875 WindowState win = mTapExcludedWindows.get(i);
1876 win.getTouchableRegion(mTmpRegion);
1877 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1878 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001879 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07001880 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001881 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001882 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001883 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1884 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001885 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001886 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001887 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001888 }
1889
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001890 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001891 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001892 super.switchUser();
1893 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001894 }
1895
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001896 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001897 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1898 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001899 }
1900 }
1901
1902 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001903 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001904 }
1905
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001906 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001907 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001908 }
1909
1910 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001911 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001912 }
1913
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001914 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001915 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001916 }
1917
Wale Ogunwale10124582016-09-15 20:25:50 -07001918 @Override
1919 void removeIfPossible() {
1920 if (isAnimating()) {
1921 mDeferredRemoval = true;
1922 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001923 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001924 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001925 }
1926
Wale Ogunwale10124582016-09-15 20:25:50 -07001927 @Override
1928 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001929 mRemovingDisplay = true;
1930 try {
1931 super.removeImmediately();
1932 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1933 mDimLayerController.close();
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07001934 if (mService.canDispatchPointerEvents()) {
1935 if (mTapDetector != null) {
1936 mService.unregisterPointerEventListener(mTapDetector);
1937 }
1938 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
1939 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1940 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001941 }
1942 } finally {
1943 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001944 }
Craig Mautner95da1082014-02-24 17:54:35 -08001945 }
1946
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001947 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001948 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001949 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001950 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1951
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001952 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001953 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001954 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001955 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001956 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001957 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001958 }
1959
Andrii Kulian0214ed92017-05-16 13:44:05 -07001960 /** @return 'true' if removal of this display content is deferred due to active animation. */
1961 boolean isRemovalDeferred() {
1962 return mDeferredRemoval;
1963 }
1964
Wale Ogunwale10124582016-09-15 20:25:50 -07001965 boolean animateForIme(float interpolatedValue, float animationTarget,
1966 float dividerAnimationTarget) {
1967 boolean updated = false;
1968
Wale Ogunwale61911492017-10-11 08:50:50 -07001969 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1970 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001971 if (stack == null || !stack.isAdjustedForIme()) {
1972 continue;
1973 }
1974
1975 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1976 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1977 updated = true;
1978 } else {
1979 mDividerControllerLocked.mLastAnimationProgress =
1980 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1981 mDividerControllerLocked.mLastDividerProgress =
1982 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1983 updated |= stack.updateAdjustForIme(
1984 mDividerControllerLocked.mLastAnimationProgress,
1985 mDividerControllerLocked.mLastDividerProgress,
1986 false /* force */);
1987 }
1988 if (interpolatedValue >= 1f) {
1989 stack.endImeAdjustAnimation();
1990 }
1991 }
1992
1993 return updated;
1994 }
1995
1996 boolean clearImeAdjustAnimation() {
1997 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07001998 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1999 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002000 if (stack != null && stack.isAdjustedForIme()) {
2001 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2002 changed = true;
2003 }
2004 }
2005 return changed;
2006 }
2007
2008 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002009 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2010 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002011 if (stack.isVisible() && stack.isAdjustedForIme()) {
2012 stack.beginImeAdjustAnimation();
2013 }
2014 }
2015 }
2016
2017 void adjustForImeIfNeeded() {
2018 final WindowState imeWin = mService.mInputMethodWindow;
2019 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2020 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002021 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002022 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2023 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2024 imeTargetStack.getDockSide() : DOCKED_INVALID;
2025 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2026 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2027 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
2028 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
2029 final boolean imeHeightChanged = imeVisible &&
2030 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2031
2032 // The divider could be adjusted for IME position, or be thinner than usual,
2033 // or both. There are three possible cases:
2034 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2035 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2036 // - If IME is not visible, divider is not moved and is normal width.
2037
2038 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002039 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2040 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002041 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002042 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2043 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002044 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2045 } else {
2046 stack.resetAdjustedForIme(false);
2047 }
2048 }
2049 mDividerControllerLocked.setAdjustedForIme(
2050 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2051 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002052 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2053 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002054 stack.resetAdjustedForIme(!dockVisible);
2055 }
2056 mDividerControllerLocked.setAdjustedForIme(
2057 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2058 }
Winson Chung655332c2016-10-31 13:14:28 -07002059 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002060 }
2061
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002062 void setInputMethodAnimLayerAdjustment(int adj) {
2063 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2064 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002065 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002066 }
2067
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002068 /**
2069 * If a window that has an animation specifying a colored background and the current wallpaper
2070 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2071 * suddenly disappear.
2072 */
2073 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002074 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2075 w -> w.mIsWallpaper && w.isVisibleNow());
2076
2077 if (visibleWallpaper != null) {
2078 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002079 }
2080 return winAnimator.mAnimLayer;
2081 }
2082
Wale Ogunwale10124582016-09-15 20:25:50 -07002083 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002084 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2085 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002086 stack.prepareFreezingTaskBounds();
2087 }
2088 }
2089
Wale Ogunwale94744212015-09-21 19:01:47 -07002090 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002091 getLogicalDisplayRect(mTmpRect, newRotation);
2092
2093 // Compute a transform matrix to undo the coordinate space transformation,
2094 // and present the window at the same physical position it previously occupied.
2095 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2096 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2097
2098 mTmpRectF.set(bounds);
2099 mTmpMatrix.mapRect(mTmpRectF);
2100 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002101 }
2102
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002103 static int deltaRotation(int oldRotation, int newRotation) {
2104 int delta = newRotation - oldRotation;
2105 if (delta < 0) delta += 4;
2106 return delta;
2107 }
2108
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002109 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002110 Matrix outMatrix) {
2111 // For rotations without Z-ordering we don't need the target rectangle's position.
2112 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2113 displayHeight, outMatrix);
2114 }
2115
2116 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2117 float displayWidth, float displayHeight, Matrix outMatrix) {
2118 switch (rotation) {
2119 case ROTATION_0:
2120 outMatrix.reset();
2121 break;
2122 case ROTATION_270:
2123 outMatrix.setRotate(270, 0, 0);
2124 outMatrix.postTranslate(0, displayHeight);
2125 outMatrix.postTranslate(rectTop, 0);
2126 break;
2127 case ROTATION_180:
2128 outMatrix.reset();
2129 break;
2130 case ROTATION_90:
2131 outMatrix.setRotate(90, 0, 0);
2132 outMatrix.postTranslate(displayWidth, 0);
2133 outMatrix.postTranslate(-rectTop, rectLeft);
2134 break;
2135 }
2136 }
2137
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002138 @CallSuper
2139 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002140 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002141 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002142 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002143 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002144 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2145 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002146 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002147 }
2148 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2149 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002150 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2151 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002152 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002153 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002154 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2155 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002156 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002157 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002158 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2159 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002160 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002161 }
2162 proto.write(DPI, mBaseDisplayDensity);
2163 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002164 proto.write(ROTATION, mRotation);
2165 final ScreenRotationAnimation screenRotationAnimation =
2166 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2167 if (screenRotationAnimation != null) {
2168 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2169 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002170 proto.end(token);
2171 }
2172
Craig Mautnera91f9e22012-09-14 16:22:08 -07002173 public void dump(String prefix, PrintWriter pw) {
2174 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2175 final String subPrefix = " " + prefix;
2176 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2177 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2178 pw.print("dpi");
2179 if (mInitialDisplayWidth != mBaseDisplayWidth
2180 || mInitialDisplayHeight != mBaseDisplayHeight
2181 || mInitialDisplayDensity != mBaseDisplayDensity) {
2182 pw.print(" base=");
2183 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2184 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2185 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002186 if (mDisplayScalingDisabled) {
2187 pw.println(" noscale");
2188 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002189 pw.print(" cur=");
2190 pw.print(mDisplayInfo.logicalWidth);
2191 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2192 pw.print(" app=");
2193 pw.print(mDisplayInfo.appWidth);
2194 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2195 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2196 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2197 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2198 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002199 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002200 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002201 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002202
Craig Mautnerdc548482014-02-05 13:35:24 -08002203 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002204 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002205 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2206 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002207 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002208 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002209
Craig Mautnerdc548482014-02-05 13:35:24 -08002210 pw.println();
2211 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002212 pw.println();
2213 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002214 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002215 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002216 pw.print(" Exiting #"); pw.print(i);
2217 pw.print(' '); pw.print(token);
2218 pw.println(':');
2219 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002220 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002221 }
Craig Mautner59c00972012-07-30 12:10:24 -07002222 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002223 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002224 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002225
2226 // Dump stack references
2227 final TaskStack homeStack = getHomeStack();
2228 if (homeStack != null) {
2229 pw.println(prefix + "homeStack=" + homeStack.getName());
2230 }
2231 final TaskStack pinnedStack = getPinnedStack();
2232 if (pinnedStack != null) {
2233 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2234 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002235 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002236 if (splitScreenPrimaryStack != null) {
2237 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2238 }
2239
2240 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002241 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002242 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002243 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002244
2245 if (mInputMethodAnimLayerAdjustment != 0) {
2246 pw.println(subPrefix
2247 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2248 }
Craig Mautner59c00972012-07-30 12:10:24 -07002249 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002250
2251 @Override
2252 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002253 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002254 }
2255
2256 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002257 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002258 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002259
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002260 /** Returns true if the stack in the windowing mode is visible. */
2261 boolean isStackVisible(int windowingMode) {
2262 final TaskStack stack = getStack(windowingMode);
2263 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002264 }
2265
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002266 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002267 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002268 final int x = (int) xf;
2269 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002270 final WindowState touchedWin = getWindow(w -> {
2271 final int flags = w.mAttrs.flags;
2272 if (!w.isVisibleLw()) {
2273 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002274 }
2275 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002276 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002277 }
2278
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002279 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002280 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002281 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002282 }
2283
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002284 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002285
2286 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002287 return mTmpRegion.contains(x, y) || touchFlags == 0;
2288 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002289
2290 return touchedWin;
2291 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002292
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002293 boolean canAddToastWindowForUid(int uid) {
2294 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002295 // Also if the app is focused adding more than one toast at
2296 // a time for better backwards compatibility.
2297 final WindowState focusedWindowForUid = getWindow(w ->
2298 w.mOwnerUid == uid && w.isFocused());
2299 if (focusedWindowForUid != null) {
2300 return true;
2301 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002302 final WindowState win = getWindow(w ->
2303 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2304 && !w.mWindowRemovalAllowed);
2305 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002306 }
2307
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002308 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002309 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2310 return;
2311 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002312
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002313 // Used to communicate the old focus to the callback method.
2314 mTmpWindow = oldFocus;
2315
2316 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002317 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002318
2319 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002320 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002321
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002322 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002323
2324 if (mTmpWindow == null) {
2325 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2326 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002327 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002328 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002329 }
2330
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002331 /** Updates the layer assignment of windows on this display. */
2332 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002333 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002334 if (setLayoutNeeded) {
2335 setLayoutNeeded();
2336 }
2337 }
2338
Wale Ogunwale1666e312016-12-16 11:27:18 -08002339 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2340 // moving containers or resizing them. Need to investigate the best way to have it automatically
2341 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002342 void layoutAndAssignWindowLayersIfNeeded() {
2343 mService.mWindowsChanged = true;
2344 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002345
2346 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2347 false /*updateInputWindows*/)) {
2348 assignWindowLayers(false /* setLayoutNeeded */);
2349 }
2350
2351 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2352 mService.mWindowPlacerLocked.performSurfacePlacement();
2353 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2354 }
2355
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002356 /** Returns true if a leaked surface was destroyed */
2357 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002358 // Used to indicate that a surface was leaked.
2359 mTmpWindow = null;
2360 forAllWindows(w -> {
2361 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002362 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002363 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002364 }
2365 if (!mService.mSessions.contains(wsa.mSession)) {
2366 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002367 + w + " surface=" + wsa.mSurfaceController
2368 + " token=" + w.mToken
2369 + " pid=" + w.mSession.mPid
2370 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002371 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002372 mService.mForceRemoves.add(w);
2373 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002374 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002375 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002376 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002377 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002378 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002379 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002380 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002381 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002382 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002383
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002384 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002385 }
2386
2387 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002388 * Determine and return the window that should be the IME target.
2389 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2390 * @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 +00002391 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002392 WindowState computeImeTarget(boolean updateImeTarget) {
2393 if (mService.mInputMethodWindow == null) {
2394 // There isn't an IME so there shouldn't be a target...That was easy!
2395 if (updateImeTarget) {
2396 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2397 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2398 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2399 }
2400 return null;
2401 }
2402
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002403 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2404 // same display. Or even when the current IME/target are not on the same screen as the next
2405 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002406 mUpdateImeTarget = updateImeTarget;
2407 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002408
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002409
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002410 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2411 // to be on top of it, but it is not -really- where input will go. So look down below
2412 // for a real window to target...
2413 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2414 final AppWindowToken token = target.mAppToken;
2415 if (token != null) {
2416 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2417 if (betterTarget != null) {
2418 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002419 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002420 }
2421 }
2422
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002423 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2424 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002425
2426 // Now, a special case -- if the last target's window is in the process of exiting, and is
2427 // above the new target, keep on the last target to avoid flicker. Consider for example a
2428 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2429 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2430 // scrim.
2431 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002432 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2433 && (target == null
2434 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002435 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002436 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002437 }
2438
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002439 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2440 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002441
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002442 if (target == null) {
2443 if (updateImeTarget) {
2444 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2445 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2446 + Debug.getCallers(4) : ""));
2447 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2448 }
2449
2450 return null;
2451 }
2452
2453 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002454 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2455 if (token != null) {
2456
2457 // Now some fun for dealing with window animations that modify the Z order. We need
2458 // to look at all windows below the current target that are in this app, finding the
2459 // highest visible one in layering.
2460 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002461 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002462 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002463 }
2464
2465 if (highestTarget != null) {
2466 final AppTransition appTransition = mService.mAppTransition;
2467 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2468 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2469 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002470 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002471
2472 if (appTransition.isTransitionSet()) {
2473 // If we are currently setting up for an animation, hold everything until we
2474 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002475 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2476 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002477 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002478 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002479 // If the window we are currently targeting is involved with an animation,
2480 // and it is on top of the next target we will be over, then hold off on
2481 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002482 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2483 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002484 }
2485 }
2486 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002487
2488 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2489 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2490 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002491 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002492 }
2493
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002494 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002495 }
2496
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002497 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2498 if (target == mService.mInputMethodTarget
2499 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2500 && mInputMethodAnimLayerAdjustment == layerAdj) {
2501 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002502 }
2503
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002504 mService.mInputMethodTarget = target;
2505 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2506 setInputMethodAnimLayerAdjustment(layerAdj);
2507 assignWindowLayers(false /* setLayoutNeeded */);
2508 }
2509
2510 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2511 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2512 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2513 }
2514
2515 // Used to indicate we have reached the first window in the range we are interested in.
2516 mTmpWindow = null;
2517
2518 // TODO: Figure-out a more efficient way to do this.
2519 final WindowState candidate = getWindow(w -> {
2520 if (w == top) {
2521 // Reached the first window in the range we are interested in.
2522 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002523 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002524 if (mTmpWindow == null) {
2525 return false;
2526 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002527
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002528 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2529 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002530 }
2531 // If we reached the bottom of the range of windows we are considering,
2532 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002533 if (w == bottom) {
2534 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002535 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002536 return false;
2537 });
2538
2539 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002540 }
2541
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002542 void setLayoutNeeded() {
2543 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2544 mLayoutNeeded = true;
2545 }
2546
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002547 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002548 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2549 mLayoutNeeded = false;
2550 }
2551
2552 boolean isLayoutNeeded() {
2553 return mLayoutNeeded;
2554 }
2555
Wale Ogunwale02319a62016-09-26 15:21:22 -07002556 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2557 if (mTokenMap.isEmpty()) {
2558 return;
2559 }
2560 pw.println(" Display #" + mDisplayId);
2561 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2562 while (it.hasNext()) {
2563 final WindowToken token = it.next();
2564 pw.print(" ");
2565 pw.print(token);
2566 if (dumpAll) {
2567 pw.println(':');
2568 token.dump(pw, " ");
2569 } else {
2570 pw.println();
2571 }
2572 }
2573 }
2574
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002575 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002576 final int[] index = new int[1];
2577 forAllWindows(w -> {
2578 final WindowStateAnimator wAnim = w.mWinAnimator;
2579 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2580 index[0] = index[0] + 1;
2581 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002582 }
2583
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002584 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002585 forAllWindows(w -> {
2586 w.mWinAnimator.enableSurfaceTrace(fd);
2587 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002588 }
2589
2590 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002591 forAllWindows(w -> {
2592 w.mWinAnimator.disableSurfaceTrace();
2593 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002594 }
2595
Jorim Jaggife762342016-10-13 14:33:27 +02002596 /**
2597 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2598 */
2599 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2600 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002601 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002602 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2603 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002604 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002605 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2606 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002607 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002608 }
2609
Wale Ogunwale494009b82016-10-21 09:01:38 -07002610 boolean checkWaitingForWindows() {
2611
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002612 mHaveBootMsg = false;
2613 mHaveApp = false;
2614 mHaveWallpaper = false;
2615 mHaveKeyguard = true;
2616
2617 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002618 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2619 return true;
2620 }
2621 if (w.isDrawnLw()) {
2622 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002623 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002624 } else if (w.mAttrs.type == TYPE_APPLICATION
2625 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002626 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002627 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002628 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002629 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002630 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002631 }
2632 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002633 return false;
2634 });
2635
2636 if (visibleWindow != null) {
2637 // We have a visible window.
2638 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002639 }
2640
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002641 // if the wallpaper service is disabled on the device, we're never going to have
2642 // wallpaper, don't bother waiting for it
2643 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2644 com.android.internal.R.bool.config_enableWallpaperService)
2645 && !mService.mOnlyCore;
2646
Wale Ogunwale494009b82016-10-21 09:01:38 -07002647 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2648 "******** booted=" + mService.mSystemBooted
2649 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002650 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2651 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2652 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002653
2654 // If we are turning on the screen to show the boot message, don't do it until the boot
2655 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002656 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002657 return true;
2658 }
2659
2660 // If we are turning on the screen after the boot is completed normally, don't do so until
2661 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002662 if (mService.mSystemBooted
2663 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002664 return true;
2665 }
2666
2667 return false;
2668 }
2669
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002670 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002671 mTmpWindowAnimator = animator;
2672 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002673 }
2674
2675 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002676 resetAnimationBackgroundAnimator();
2677
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002678 // Used to indicate a detached wallpaper.
2679 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002680 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002681
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002682 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002683
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002684 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002685 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002686 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2687 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002688 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2689 }
2690 }
2691
2692 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002693 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002694 }
2695
Wale Ogunwale494009b82016-10-21 09:01:38 -07002696 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002697 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002698 if (imFocus == null) {
2699 return false;
2700 }
2701
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002702 if (DEBUG_INPUT_METHOD) {
2703 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2704 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2705 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002706 }
2707
Wale Ogunwale494009b82016-10-21 09:01:38 -07002708 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2709
2710 if (DEBUG_INPUT_METHOD) {
2711 Slog.i(TAG_WM, "IM target client: " + imeClient);
2712 if (imeClient != null) {
2713 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2714 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2715 }
2716 }
2717
2718 return imeClient != null && imeClient.asBinder() == client.asBinder();
2719 }
2720
2721 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002722 final WindowState win = getWindow(
2723 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2724 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002725 }
2726
2727 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002728 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002729 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002730 final int curValue = w.mSystemUiVisibility;
2731 final int diff = (curValue ^ visibility) & globalDiff;
2732 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002733 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002734 w.mSeq++;
2735 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002736 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002737 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2738 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002739 visibility, newValue, diff);
2740 }
2741 } catch (RemoteException e) {
2742 // so sorry
2743 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002744 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002745 }
2746
2747 void onWindowFreezeTimeout() {
2748 Slog.w(TAG_WM, "Window freeze timeout expired.");
2749 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002750
2751 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002752 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002753 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002754 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002755 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002756 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2757 - mService.mDisplayFreezeTime);
2758 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002759 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002760 mService.mWindowPlacerLocked.performSurfacePlacement();
2761 }
2762
2763 void waitForAllWindowsDrawn() {
2764 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002765 forAllWindows(w -> {
2766 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2767 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2768 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002769 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002770 w.mLastContentInsets.set(-1, -1, -1, -1);
2771 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002772 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002773 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002774 }
2775
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002776 // TODO: Super crazy long method that should be broken down...
2777 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2778
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002779 final int dw = mDisplayInfo.logicalWidth;
2780 final int dh = mDisplayInfo.logicalHeight;
2781 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2782
2783 mTmpUpdateAllDrawn.clear();
2784
2785 int repeats = 0;
2786 do {
2787 repeats++;
2788 if (repeats > 6) {
2789 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2790 clearLayoutNeeded();
2791 break;
2792 }
2793
2794 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2795 pendingLayoutChanges);
2796
Andrii Kulian839def92016-11-02 10:58:58 -07002797 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2798 // the wallpaper window jumping across displays.
2799 // Remove check for default display when there will be support for multiple wallpaper
2800 // targets (on different displays).
2801 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002802 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002803 }
2804
2805 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2806 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2807 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2808 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002809 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002810 }
2811 }
2812
2813 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2814 setLayoutNeeded();
2815 }
2816
2817 // FIRST LOOP: Perform a layout, if needed.
2818 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2819 performLayout(repeats == 1, false /* updateInputWindows */);
2820 } else {
2821 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2822 }
2823
2824 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2825 pendingLayoutChanges = 0;
2826
2827 if (isDefaultDisplay) {
2828 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002829 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002830 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2831 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2832 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2833 }
2834 } while (pendingLayoutChanges != 0);
2835
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002836 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002837 resetDimming();
2838
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002839 mTmpRecoveringMemory = recoveringMemory;
2840 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002841
2842 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002843 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2844 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2845 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002846 true /* inTraversal, must call performTraversalInTrans... below */);
2847
2848 stopDimmingIfNeeded();
2849
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002850 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2851 if (wallpaperVisible != mLastWallpaperVisible) {
2852 mLastWallpaperVisible = wallpaperVisible;
2853 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2854 }
2855
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002856 while (!mTmpUpdateAllDrawn.isEmpty()) {
2857 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2858 // See if any windows have been drawn, so they (and others associated with them)
2859 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002860 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002861 }
2862
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002863 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002864 }
2865
2866 void performLayout(boolean initial, boolean updateInputWindows) {
2867 if (!isLayoutNeeded()) {
2868 return;
2869 }
2870 clearLayoutNeeded();
2871
2872 final int dw = mDisplayInfo.logicalWidth;
2873 final int dh = mDisplayInfo.logicalHeight;
2874
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002875 if (DEBUG_LAYOUT) {
2876 Slog.v(TAG, "-------------------------------------");
2877 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2878 }
2879
Andrii Kulian8ee72852017-03-10 10:36:45 -08002880 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002881 getConfiguration().uiMode);
2882 if (isDefaultDisplay) {
2883 // Not needed on non-default displays.
2884 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2885 mService.mScreenRect.set(0, 0, dw, dh);
2886 }
2887
2888 mService.mPolicy.getContentRectLw(mContentRect);
2889
2890 int seq = mService.mLayoutSeq + 1;
2891 if (seq < 0) seq = 0;
2892 mService.mLayoutSeq = seq;
2893
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002894 // Used to indicate that we have processed the dream window and all additional windows are
2895 // behind it.
2896 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002897 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002898
2899 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002900 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002901
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002902 // Used to indicate that we have processed the dream window and all additional attached
2903 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002904 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002905 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002906
2907 // Now perform layout of attached windows, which usually depend on the position of the
2908 // window they are attached to. XXX does not deal with windows that are attached to windows
2909 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002910 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002911
2912 // Window frames may have changed. Tell the input dispatcher about it.
2913 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2914 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2915 if (updateInputWindows) {
2916 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2917 }
2918
2919 mService.mPolicy.finishLayoutLw();
2920 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2921 }
2922
2923 /**
2924 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2925 * In portrait mode, it grabs the full screenshot.
2926 *
2927 * @param width the width of the target bitmap
2928 * @param height the height of the target bitmap
2929 * @param includeFullDisplay true if the screen should not be cropped before capture
2930 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2931 * @param config of the output bitmap
2932 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002933 * @param includeDecor whether to include window decors, like the status or navigation bar
2934 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002935 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002936 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002937 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002938 boolean wallpaperOnly, boolean includeDecor) {
2939 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2940 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002941 if (bitmap == null) {
2942 return null;
2943 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002944
2945 if (DEBUG_SCREENSHOT) {
2946 // TEST IF IT's ALL BLACK
2947 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2948 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2949 bitmap.getHeight());
2950 boolean allBlack = true;
2951 final int firstColor = buffer[0];
2952 for (int i = 0; i < buffer.length; i++) {
2953 if (buffer[i] != firstColor) {
2954 allBlack = false;
2955 break;
2956 }
2957 }
2958 if (allBlack) {
2959 final WindowState appWin = mScreenshotApplicationState.appWin;
2960 final int maxLayer = mScreenshotApplicationState.maxLayer;
2961 final int minLayer = mScreenshotApplicationState.minLayer;
2962 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2963 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2964 (appWin != null ?
2965 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2966 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2967 }
2968 }
2969
2970 // Create a copy of the screenshot that is immutable and backed in ashmem.
2971 // This greatly reduces the overhead of passing the bitmap between processes.
2972 Bitmap ret = bitmap.createAshmemBitmap(config);
2973 bitmap.recycle();
2974 return ret;
2975 }
2976
2977 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2978 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2979 boolean includeDecor) {
2980 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2981 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2982 }
2983
2984 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2985 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2986 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002987 int dw = mDisplayInfo.logicalWidth;
2988 int dh = mDisplayInfo.logicalHeight;
2989 if (dw == 0 || dh == 0) {
2990 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2991 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2992 return null;
2993 }
2994
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002995 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002996
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002997 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002998 final Rect frame = new Rect();
2999 final Rect stackBounds = new Rect();
3000
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003001 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003002 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08003003 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003004 synchronized(mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02003005 if (!mService.mPolicy.isScreenOn()) {
3006 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Attempted to take screenshot while display"
3007 + " was off.");
3008 return null;
3009 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003010 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003011 mScreenshotApplicationState.appWin = null;
3012 forAllWindows(w -> {
3013 if (!w.mHasSurface) {
3014 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003015 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003016 if (w.mLayer >= aboveAppLayer) {
3017 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003018 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003019 if (wallpaperOnly && !w.mIsWallpaper) {
3020 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003021 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003022 if (w.mIsImWindow) {
Jorim Jaggieb59d6e2017-05-04 13:54:22 +02003023 return false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003024 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003025 // If this is the wallpaper layer and we're only looking for the wallpaper layer
3026 // then the target window state is this one.
3027 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003028 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003029 }
3030
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003031 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003032 // We have not ran across the target window yet, so it is probably behind
3033 // the wallpaper. This can happen when the keyguard is up and all windows
3034 // are moved behind the wallpaper. We don't want to include the wallpaper
3035 // layer in the screenshot as it will cover-up the layer of the target
3036 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003037 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003038 }
3039 // Fall through. The target window is in front of the wallpaper. For this
3040 // case we want to include the wallpaper layer in the screenshot because
3041 // the target window might have some transparent areas.
3042 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003043 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003044 // This app window is of no interest if it is not associated with the
3045 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003046 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003047 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003048 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003049 }
3050
3051 // Include this window.
3052
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003053 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003054 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003055 if (mScreenshotApplicationState.maxLayer < layer) {
3056 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003057 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003058 if (mScreenshotApplicationState.minLayer > layer) {
3059 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003060 }
3061
3062 // Don't include wallpaper in bounds calculation
Jorim Jaggi70f59482017-05-04 14:05:59 +02003063 if (!w.mIsWallpaper && !mutableIncludeFullDisplay.value) {
3064 if (includeDecor) {
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003065 final Task task = w.getTask();
3066 if (task != null) {
3067 task.getBounds(frame);
3068 } else {
Jorim Jaggi30d64f32017-04-07 16:33:17 +02003069
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003070 // No task bounds? Too bad! Ain't no screenshot then.
3071 return true;
3072 }
Jorim Jaggi70f59482017-05-04 14:05:59 +02003073 } else {
3074 final Rect wf = w.mFrame;
3075 final Rect cr = w.mContentInsets;
3076 int left = wf.left + cr.left;
3077 int top = wf.top + cr.top;
3078 int right = wf.right - cr.right;
3079 int bottom = wf.bottom - cr.bottom;
3080 frame.union(left, top, right, bottom);
3081 w.getVisibleBounds(stackBounds);
3082 if (!Rect.intersects(frame, stackBounds)) {
3083 // Set frame empty if there's no intersection.
3084 frame.setEmpty();
3085 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003086 }
3087 }
3088
3089 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003090 (w.mAppToken != null && w.mAppToken.token == appToken)
3091 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi406386a82017-09-15 14:17:40 +02003092 if (foundTargetWs && winAnim.getShown() && winAnim.mLastAlpha > 0f) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003093 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003094 }
3095
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003096 if (w.isObscuringDisplay()){
3097 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003098 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003099 return false;
3100 }, true /* traverseTopToBottom */);
3101
3102 final WindowState appWin = mScreenshotApplicationState.appWin;
3103 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
3104 final int maxLayer = mScreenshotApplicationState.maxLayer;
3105 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003106
3107 if (appToken != null && appWin == null) {
3108 // Can't find a window to snapshot.
3109 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
3110 "Screenshot: Couldn't find a surface matching " + appToken);
3111 return null;
3112 }
3113
3114 if (!screenshotReady) {
3115 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3116 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3117 appWin.mWinAnimator.mDrawState)));
3118 return null;
3119 }
3120
3121 // Screenshot is ready to be taken. Everything from here below will continue
3122 // through the bottom of the loop and return a value. We only stay in the loop
3123 // because we don't want to release the mWindowMap lock until the screenshot is
3124 // taken.
3125
3126 if (maxLayer == 0) {
3127 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3128 + ": returning null maxLayer=" + maxLayer);
3129 return null;
3130 }
3131
Jorim Jaggi02886a82016-12-06 09:10:06 -08003132 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003133 // Constrain frame to the screen size.
3134 if (!frame.intersect(0, 0, dw, dh)) {
3135 frame.setEmpty();
3136 }
3137 } else {
3138 // Caller just wants entire display.
3139 frame.set(0, 0, dw, dh);
3140 }
3141 if (frame.isEmpty()) {
3142 return null;
3143 }
3144
3145 if (width < 0) {
3146 width = (int) (frame.width() * frameScale);
3147 }
3148 if (height < 0) {
3149 height = (int) (frame.height() * frameScale);
3150 }
3151
3152 // Tell surface flinger what part of the image to crop. Take the top
3153 // right part of the application, and crop the larger dimension to fit.
3154 Rect crop = new Rect(frame);
3155 if (width / (float) frame.width() < height / (float) frame.height()) {
3156 int cropWidth = (int)((float)width / (float)height * frame.height());
3157 crop.right = crop.left + cropWidth;
3158 } else {
3159 int cropHeight = (int)((float)height / (float)width * frame.width());
3160 crop.bottom = crop.top + cropHeight;
3161 }
3162
3163 // The screenshot API does not apply the current screen rotation.
3164 int rot = mDisplay.getRotation();
3165
3166 if (rot == ROTATION_90 || rot == ROTATION_270) {
3167 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3168 }
3169
3170 // Surfaceflinger is not aware of orientation, so convert our logical
3171 // crop to surfaceflinger's portrait orientation.
3172 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3173
3174 if (DEBUG_SCREENSHOT) {
3175 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3176 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003177 forAllWindows(w -> {
3178 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3179 Slog.i(TAG_WM, w + ": " + w.mLayer
3180 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003181 + " surfaceLayer=" + ((controller == null)
3182 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003183 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003184 }
3185
3186 final ScreenRotationAnimation screenRotationAnimation =
3187 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3188 final boolean inRotation = screenRotationAnimation != null &&
3189 screenRotationAnimation.isAnimating();
3190 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3191 "Taking screenshot while rotating");
3192
3193 // We force pending transactions to flush before taking
3194 // the screenshot by pushing an empty synchronous transaction.
3195 SurfaceControl.openTransaction();
3196 SurfaceControl.closeTransactionSync();
3197
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003198 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003199 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003200 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003201 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3202 + ") to layer " + maxLayer);
3203 return null;
3204 }
3205 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003206 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003207 }
3208
3209 // TODO: Can this use createRotationMatrix()?
3210 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3211 if (rot == Surface.ROTATION_90) {
3212 final int tmp = crop.top;
3213 crop.top = dw - crop.right;
3214 crop.right = crop.bottom;
3215 crop.bottom = dw - crop.left;
3216 crop.left = tmp;
3217 } else if (rot == Surface.ROTATION_180) {
3218 int tmp = crop.top;
3219 crop.top = dh - crop.bottom;
3220 crop.bottom = dh - tmp;
3221 tmp = crop.right;
3222 crop.right = dw - crop.left;
3223 crop.left = dw - tmp;
3224 } else if (rot == Surface.ROTATION_270) {
3225 final int tmp = crop.top;
3226 crop.top = crop.left;
3227 crop.left = dh - crop.bottom;
3228 crop.bottom = crop.right;
3229 crop.right = dh - tmp;
3230 }
3231 }
3232
3233 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003234 // Used to indicate the layout is needed.
3235 mTmpWindow = null;
3236
3237 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003238 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003239 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003240 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003241 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003242 w.setDisplayLayoutNeeded();
3243 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003244 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003245
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003246 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003247 mService.mWindowPlacerLocked.performSurfacePlacement();
3248 }
3249 }
3250
Wale Ogunwale1666e312016-12-16 11:27:18 -08003251 void setExitingTokensHasVisible(boolean hasVisible) {
3252 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3253 mExitingTokens.get(i).hasVisible = hasVisible;
3254 }
3255
3256 // Initialize state of exiting applications.
3257 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3258 }
3259
3260 void removeExistingTokensIfPossible() {
3261 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3262 final WindowToken token = mExitingTokens.get(i);
3263 if (!token.hasVisible) {
3264 mExitingTokens.remove(i);
3265 }
3266 }
3267
3268 // Time to remove any exiting applications?
3269 mTaskStackContainers.removeExistingAppTokensIfPossible();
3270 }
3271
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003272 @Override
3273 void onDescendantOverrideConfigurationChanged() {
3274 setLayoutNeeded();
3275 mService.requestTraversal();
3276 }
3277
David Stevens9440dc82017-03-16 19:00:20 -07003278 boolean okToDisplay() {
3279 if (mDisplayId == DEFAULT_DISPLAY) {
3280 return !mService.mDisplayFrozen
3281 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3282 }
3283 return mDisplayInfo.state == Display.STATE_ON;
3284 }
3285
3286 boolean okToAnimate() {
3287 return okToDisplay() &&
3288 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3289 }
3290
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003291 static final class TaskForResizePointSearchResult {
3292 boolean searchDone;
3293 Task taskForResize;
3294
3295 void reset() {
3296 searchDone = false;
3297 taskForResize = null;
3298 }
3299 }
Robert Carr3b716242016-08-16 16:02:21 -07003300
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003301 private static final class ApplySurfaceChangesTransactionState {
3302 boolean displayHasContent;
3303 boolean obscured;
3304 boolean syswin;
3305 boolean focusDisplayed;
3306 float preferredRefreshRate;
3307 int preferredModeId;
3308
3309 void reset() {
3310 displayHasContent = false;
3311 obscured = false;
3312 syswin = false;
3313 focusDisplayed = false;
3314 preferredRefreshRate = 0;
3315 preferredModeId = 0;
3316 }
3317 }
3318
3319 private static final class ScreenshotApplicationState {
3320 WindowState appWin;
3321 int maxLayer;
3322 int minLayer;
3323 boolean screenshotReady;
3324
3325 void reset(boolean screenshotReady) {
3326 appWin = null;
3327 maxLayer = 0;
3328 minLayer = 0;
3329 this.screenshotReady = screenshotReady;
3330 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3331 }
3332 }
3333
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003334 /**
3335 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3336 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3337 * homogeneous children type which is currently required by sub-classes of
3338 * {@link WindowContainer} class.
3339 */
3340 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3341
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003342 @Override
3343 boolean fillsParent() {
3344 return true;
3345 }
3346
3347 @Override
3348 boolean isVisible() {
3349 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003350 }
3351 }
3352
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003353 /**
3354 * Window container class that contains all containers on this display relating to Apps.
3355 * I.e Activities.
3356 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003357 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003358
Wale Ogunwale61911492017-10-11 08:50:50 -07003359 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3360 // through the list to find them.
3361 private TaskStack mHomeStack = null;
3362 private TaskStack mPinnedStack = null;
3363 private TaskStack mSplitScreenPrimaryStack = null;
3364
3365 /**
3366 * Returns the topmost stack on the display that is compatible with the input windowing mode
3367 * and activity type. Null is no compatible stack on the display.
3368 */
3369 TaskStack getStack(int windowingMode, int activityType) {
3370 if (activityType == ACTIVITY_TYPE_HOME) {
3371 return mHomeStack;
3372 }
3373 if (windowingMode == WINDOWING_MODE_PINNED) {
3374 return mPinnedStack;
3375 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3376 return mSplitScreenPrimaryStack;
3377 }
3378 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3379 final TaskStack stack = mTaskStackContainers.getChildAt(i);
3380 if (stack.isCompatible(windowingMode, activityType)) {
3381 return stack;
3382 }
3383 }
3384 return null;
3385 }
3386
3387 @VisibleForTesting
3388 TaskStack getTopStack() {
3389 return mTaskStackContainers.getChildCount() > 0
3390 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3391 }
3392
3393 TaskStack getHomeStack() {
3394 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3395 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3396 }
3397 return mHomeStack;
3398 }
3399
3400 TaskStack getPinnedStack() {
3401 return mPinnedStack;
3402 }
3403
Matthew Ng64e77cf2017-10-31 14:01:31 -07003404 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003405 return mSplitScreenPrimaryStack;
3406 }
3407
Andrii Kulian839def92016-11-02 10:58:58 -07003408 /**
3409 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003410 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003411 */
3412 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003413 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003414 addChild(stack, onTop);
3415 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003416 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003417
Wale Ogunwale61911492017-10-11 08:50:50 -07003418 void onStackWindowingModeChanged(TaskStack stack) {
3419 removeStackReferenceIfNeeded(stack);
3420 addStackReferenceIfNeeded(stack);
3421 if (stack == mPinnedStack && getTopStack() != stack) {
3422 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3423 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3424 }
3425 }
3426
3427 private void addStackReferenceIfNeeded(TaskStack stack) {
3428 if (stack.isActivityTypeHome()) {
3429 if (mHomeStack != null) {
3430 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
3431 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
3432 }
3433 mHomeStack = stack;
3434 }
3435 final int windowingMode = stack.getWindowingMode();
3436 if (windowingMode == WINDOWING_MODE_PINNED) {
3437 if (mPinnedStack != null) {
3438 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3439 + mPinnedStack + " already exist on display=" + this
3440 + " stack=" + stack);
3441 }
3442 mPinnedStack = stack;
3443 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3444 if (mSplitScreenPrimaryStack != null) {
3445 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3446 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3447 + " already exist on display=" + this + " stack=" + stack);
3448 }
3449 mSplitScreenPrimaryStack = stack;
3450 }
3451 }
3452
3453 private void removeStackReferenceIfNeeded(TaskStack stack) {
3454 if (stack == mHomeStack) {
3455 mHomeStack = null;
3456 } else if (stack == mPinnedStack) {
3457 mPinnedStack = null;
3458 } else if (stack == mSplitScreenPrimaryStack) {
3459 mSplitScreenPrimaryStack = null;
3460 }
Andrii Kulian839def92016-11-02 10:58:58 -07003461 }
3462
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003463 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003464 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3465 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003466 addChild(stack, addIndex);
3467 setLayoutNeeded();
3468 }
3469
Wale Ogunwale61911492017-10-11 08:50:50 -07003470 @Override
3471 protected void removeChild(TaskStack stack) {
3472 super.removeChild(stack);
3473 removeStackReferenceIfNeeded(stack);
3474 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003475
3476 @Override
3477 boolean isOnTop() {
3478 // Considered always on top
3479 return true;
3480 }
3481
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003482 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003483 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003484 if (child.getWindowConfiguration().isAlwaysOnTop()
3485 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003486 // This stack is always-on-top, override the default behavior.
3487 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3488
3489 // Moving to its current position, as we must call super but we don't want to
3490 // perform any meaningful action.
3491 final int currentPosition = mChildren.indexOf(child);
3492 super.positionChildAt(currentPosition, child, false /* includingParents */);
3493 return;
3494 }
3495
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003496 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3497 super.positionChildAt(targetPosition, child, includingParents);
3498
3499 setLayoutNeeded();
3500 }
3501
3502 /**
3503 * When stack is added or repositioned, find a proper position for it.
3504 * This will make sure that pinned stack always stays on top.
3505 * @param requestedPosition Position requested by caller.
3506 * @param stack Stack to be added or positioned.
3507 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3508 * @return The proper position for the stack.
3509 */
3510 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3511 final int topChildPosition = mChildren.size() - 1;
3512 boolean toTop = requestedPosition == POSITION_TOP;
3513 toTop |= adding ? requestedPosition >= topChildPosition + 1
3514 : requestedPosition >= topChildPosition;
3515 int targetPosition = requestedPosition;
3516
Wale Ogunwale61911492017-10-11 08:50:50 -07003517 if (toTop && stack.getWindowingMode() != WINDOWING_MODE_PINNED && hasPinnedStack()) {
Bryce Lee48f4b572017-04-10 10:54:15 -07003518 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003519 TaskStack topStack = mChildren.get(topChildPosition);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003520 if (topStack.getWindowingMode() != WINDOWING_MODE_PINNED) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003521 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3522 }
3523
3524 // So, stack is moved just below the pinned stack.
3525 // When we're adding a new stack the target is the current pinned stack position.
3526 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003527 // stack, because WindowContainer#positionAt() first removes element and then adds
3528 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003529 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3530 }
3531
3532 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003533 }
3534
3535 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003536 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3537 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003538 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003539 if (super.forAllWindows(callback, traverseTopToBottom)) {
3540 return true;
3541 }
3542 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3543 return true;
3544 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003545 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003546 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3547 return true;
3548 }
3549 if (super.forAllWindows(callback, traverseTopToBottom)) {
3550 return true;
3551 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003552 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003553 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003554 }
3555
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003556 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003557 boolean traverseTopToBottom) {
3558 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3559 // app tokens.
3560 // TODO: Investigate if we need to continue to do this or if we can just process them
3561 // in-order.
3562 if (traverseTopToBottom) {
3563 for (int i = mChildren.size() - 1; i >= 0; --i) {
3564 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3565 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003566 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3567 traverseTopToBottom)) {
3568 return true;
3569 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003570 }
3571 }
3572 } else {
3573 final int count = mChildren.size();
3574 for (int i = 0; i < count; ++i) {
3575 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3576 final int appTokensCount = appTokens.size();
3577 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003578 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3579 traverseTopToBottom)) {
3580 return true;
3581 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003582 }
3583 }
3584 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003585 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003586 }
3587
Wale Ogunwale1666e312016-12-16 11:27:18 -08003588 void setExitingTokensHasVisible(boolean hasVisible) {
3589 for (int i = mChildren.size() - 1; i >= 0; --i) {
3590 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3591 for (int j = appTokens.size() - 1; j >= 0; --j) {
3592 appTokens.get(j).hasVisible = hasVisible;
3593 }
3594 }
3595 }
3596
3597 void removeExistingAppTokensIfPossible() {
3598 for (int i = mChildren.size() - 1; i >= 0; --i) {
3599 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3600 for (int j = appTokens.size() - 1; j >= 0; --j) {
3601 final AppWindowToken token = appTokens.get(j);
3602 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3603 && (!token.mIsExiting || token.isEmpty())) {
3604 // Make sure there is no animation running on this token, so any windows
3605 // associated with it will be removed as soon as their animations are
3606 // complete.
3607 token.mAppAnimator.clearAnimation();
3608 token.mAppAnimator.animating = false;
3609 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3610 "performLayout: App token exiting now removed" + token);
3611 token.removeIfPossible();
3612 }
3613 }
3614 }
3615 }
3616
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003617 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003618 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003619 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3620 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003621 // Apps and their containers are not allowed to specify an orientation while the
3622 // docked or freeform stack is visible...except for the home stack/task if the
3623 // docked stack is minimized and it actually set something.
3624 if (mHomeStack != null && mHomeStack.isVisible()
3625 && mDividerControllerLocked.isMinimizedDock()) {
3626 final int orientation = mHomeStack.getOrientation();
3627 if (orientation != SCREEN_ORIENTATION_UNSET) {
3628 return orientation;
3629 }
3630 }
3631 return SCREEN_ORIENTATION_UNSPECIFIED;
3632 }
3633
3634 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003635 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3636 PackageManager.FEATURE_AUTOMOTIVE);
3637 if (isCar) {
3638 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3639 // allow anything but the default orientation.
3640 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3641 "Forcing UNSPECIFIED orientation in car. Ignoring " + orientation);
3642 return SCREEN_ORIENTATION_UNSPECIFIED;
3643 }
3644
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003645 if (orientation != SCREEN_ORIENTATION_UNSET
3646 && orientation != SCREEN_ORIENTATION_BEHIND) {
3647 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3648 "App is requesting an orientation, return " + orientation);
3649 return orientation;
3650 }
3651
3652 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003653 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003654 // The next app has not been requested to be visible, so we keep the current orientation
3655 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003656 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003657 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003658 }
3659
3660 /**
3661 * Window container class that contains all containers on this display that are not related to
3662 * Apps. E.g. status bar.
3663 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003664 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3665 /**
3666 * Compares two child window tokens returns -1 if the first is lesser than the second in
3667 * terms of z-order and 1 otherwise.
3668 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003669 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003670 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003671 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3672 token1.mOwnerCanManageAppTokens)
3673 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3674 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003675
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003676 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3677 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3678 return false;
3679 }
3680 final int req = w.mAttrs.screenOrientation;
3681 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3682 || req == SCREEN_ORIENTATION_UNSET) {
3683 return false;
3684 }
3685 return true;
3686 };
3687
Wale Ogunwale3a931692016-11-02 16:49:48 -07003688 private final String mName;
3689 NonAppWindowContainers(String name) {
3690 mName = name;
3691 }
3692
3693 void addChild(WindowToken token) {
3694 addChild(token, mWindowComparator);
3695 }
3696
3697 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003698 int getOrientation() {
3699 final WindowManagerPolicy policy = mService.mPolicy;
3700 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003701 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003702
3703 if (win != null) {
3704 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003705 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003706 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003707 if (mService.mKeyguardGoingAway) {
3708 // Keyguard can't affect the orientation if it is going away...
3709 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3710 return SCREEN_ORIENTATION_UNSET;
3711 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003712 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003713 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003714 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003715 }
3716
Andrii Kulian8ee72852017-03-10 10:36:45 -08003717 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003718
Jorim Jaggi75520d52017-08-24 17:23:19 +02003719 if (policy.isKeyguardShowingAndNotOccluded()
3720 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003721 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003722 }
3723
3724 return SCREEN_ORIENTATION_UNSET;
3725 }
3726
3727 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003728 String getName() {
3729 return mName;
3730 }
Robert Carr3b716242016-08-16 16:02:21 -07003731 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003732
3733 /**
3734 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3735 */
3736 @FunctionalInterface
3737 private interface Screenshoter<E> {
3738 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3739 boolean useIdentityTransform, int rotation);
3740 }
Craig Mautner59c00972012-07-30 12:10:24 -07003741}