blob: 07ba517900383e7e770f7808daec9c5ae84b5dbe [file] [log] [blame]
Craig Mautner59c00972012-07-30 12:10:24 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
Wale Ogunwale3797c222015-10-27 14:21:58 -070019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
Wale Ogunwale51362492016-09-08 17:49:17 -070020import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
Wale Ogunwale3797c222015-10-27 14:21:58 -070021import static android.app.ActivityManager.StackId.HOME_STACK_ID;
22import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwale687b4272017-07-27 02:56:23 -070023import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070024import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070025import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
26import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale687b4272017-07-27 02:56:23 -070027import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
28import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070029import static android.view.Display.DEFAULT_DISPLAY;
30import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070031import static android.view.Surface.ROTATION_0;
32import static android.view.Surface.ROTATION_180;
33import static android.view.Surface.ROTATION_270;
34import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070035import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070036import static android.view.WindowManager.DOCKED_BOTTOM;
37import static android.view.WindowManager.DOCKED_INVALID;
38import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080039import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
40import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
41import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070042import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070043import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070044import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
45import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070046import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070047import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070048import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070049import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Wale Ogunwale494009b82016-10-21 09:01:38 -070050import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070051import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070052import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
53import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070054import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070055import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
56import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070057import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070058import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070059import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070060import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
61import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070062import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwale1666e312016-12-16 11:27:18 -080063import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070064import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070065import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070066import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
67import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwalec69694a2016-10-18 13:51:15 -070069import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070070import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080076import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070077import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070078import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070079import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070080import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070081import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070082import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080083import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -070084import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070085import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
86import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070087import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070088import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -070089import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
90import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070091import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER;
92import static com.android.server.wm.WindowManagerService.TYPE_LAYER_OFFSET;
93import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -070094import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070095import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070096import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070097import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070098import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070099import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700100import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700101import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700102import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700103import static com.android.server.wm.proto.DisplayProto.ABOVE_APP_WINDOWS;
104import static com.android.server.wm.proto.DisplayProto.BELOW_APP_WINDOWS;
105import static com.android.server.wm.proto.DisplayProto.DISPLAY_INFO;
106import static com.android.server.wm.proto.DisplayProto.DOCKED_STACK_DIVIDER_CONTROLLER;
107import static com.android.server.wm.proto.DisplayProto.DPI;
108import static com.android.server.wm.proto.DisplayProto.ID;
109import static com.android.server.wm.proto.DisplayProto.IME_WINDOWS;
110import static com.android.server.wm.proto.DisplayProto.PINNED_STACK_CONTROLLER;
Steven Timotiusf2d68892017-08-28 17:00:01 -0700111import static com.android.server.wm.proto.DisplayProto.ROTATION;
112import static com.android.server.wm.proto.DisplayProto.SCREEN_ROTATION_ANIMATION;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700113import static com.android.server.wm.proto.DisplayProto.STACKS;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700114
Andrii Kulian3a507b52016-09-19 18:14:12 -0700115import android.annotation.NonNull;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700116import android.app.ActivityManager.StackId;
Wale Ogunwale687b4272017-07-27 02:56:23 -0700117import android.app.WindowConfiguration;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700118import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700119import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700120import android.graphics.Bitmap;
Jorim Jaggi6a7a8592017-01-12 00:44:33 +0100121import android.graphics.GraphicBuffer;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700122import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800123import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700124import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700125import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100126import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700127import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700128import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700129import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700130import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700131import android.os.RemoteException;
132import android.os.SystemClock;
Chong Zhang8e89b312015-09-09 15:09:30 -0700133import android.util.DisplayMetrics;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800134import android.util.MutableBoolean;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700135import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700136import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700137import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700138import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700139import android.view.InputDevice;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700140import android.view.Surface;
141import android.view.SurfaceControl;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700142import android.view.WindowManagerPolicy;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700143
Bryce Lee48f4b572017-04-10 10:54:15 -0700144import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800145import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700146import com.android.internal.view.IInputMethodClient;
Craig Mautner59c00972012-07-30 12:10:24 -0700147
Robert Carr3b716242016-08-16 16:02:21 -0700148import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700149import java.io.PrintWriter;
150import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700151import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700152import java.util.HashMap;
153import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700154import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700155import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800156import java.util.function.Consumer;
157import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700158
Craig Mautner59c00972012-07-30 12:10:24 -0700159/**
160 * Utility class for keeping track of the WindowStates and other pertinent contents of a
161 * particular Display.
162 *
163 * IMPORTANT: No method from this class should ever be used without holding
164 * WindowManagerService.mWindowMap.
165 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700166class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700167 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700168
169 /** Unique identifier of this stack. */
170 private final int mDisplayId;
171
Wale Ogunwale3a931692016-11-02 16:49:48 -0700172 /** The containers below are the only child containers the display can have. */
173 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700174 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700175 // Contains all non-app window containers that should be displayed above the app containers
176 // (e.g. Status bar)
177 private final NonAppWindowContainers mAboveAppWindowsContainers =
178 new NonAppWindowContainers("mAboveAppWindowsContainers");
179 // Contains all non-app window containers that should be displayed below the app containers
180 // (e.g. Wallpaper).
181 private final NonAppWindowContainers mBelowAppWindowsContainers =
182 new NonAppWindowContainers("mBelowAppWindowsContainers");
183 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
184 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
185 // window containers together and move them in-sync if/when needed.
186 private final NonAppWindowContainers mImeWindowsContainers =
187 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700188
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000189 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800190 private WindowState mTmpWindow2;
191 private WindowAnimator mTmpWindowAnimator;
192 private boolean mTmpRecoveringMemory;
193 private boolean mUpdateImeTarget;
194 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700195 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700196
Wale Ogunwale02319a62016-09-26 15:21:22 -0700197 // Mapping from a token IBinder to a WindowToken object on this display.
198 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
199
Andrii Kuliancd097992017-03-23 18:31:59 -0700200 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700201 int mInitialDisplayWidth = 0;
202 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700203 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700204
205 /**
206 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
207 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
208 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
209 */
Craig Mautner59c00972012-07-30 12:10:24 -0700210 int mBaseDisplayWidth = 0;
211 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700212 /**
213 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
214 * but can be set from Settings or via shell command "adb shell wm density".
215 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
216 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700217 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700218 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700219 private final DisplayInfo mDisplayInfo = new DisplayInfo();
220 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700221 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Andrii Kulian06d07d62017-03-14 11:11:47 -0700222 /**
223 * For default display it contains real metrics, empty for others.
224 * @see WindowManagerService#createWatermarkInTransaction()
225 */
226 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
227 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
228 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700229
Andrii Kulian06d07d62017-03-14 11:11:47 -0700230 /**
231 * Compat metrics computed based on {@link #mDisplayMetrics}.
232 * @see #updateDisplayAndOrientation(int)
233 */
234 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
235
236 /** The desired scaling factor for compatible apps. */
237 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700238
Andrii Kulian8ee72852017-03-10 10:36:45 -0800239 /**
240 * Current rotation of the display.
241 * Constants as per {@link android.view.Surface.Rotation}.
242 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700243 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800244 */
245 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700246
Andrii Kulian8ee72852017-03-10 10:36:45 -0800247 /**
248 * Last applied orientation of the display.
249 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
250 *
251 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
252 */
253 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700254
Andrii Kulian8ee72852017-03-10 10:36:45 -0800255 /**
256 * Flag indicating that the application is receiving an orientation that has different metrics
257 * than it expected. E.g. Portrait instead of Landscape.
258 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700259 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800260 */
261 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700262
Andrii Kulian8ee72852017-03-10 10:36:45 -0800263 /**
264 * Orientation forced by some window. If there is no visible window that specifies orientation
265 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
266 *
267 * @see NonAppWindowContainers#getOrientation()
268 */
269 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700270
Andrii Kulian8ee72852017-03-10 10:36:45 -0800271 /**
272 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
273 * occluded.
274 *
275 * @see NonAppWindowContainers#getOrientation()
276 */
277 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
278
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700279 /**
280 * Keep track of wallpaper visibility to notify changes.
281 */
282 private boolean mLastWallpaperVisible = false;
283
Andrii Kulian06d07d62017-03-14 11:11:47 -0700284 private Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700285 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700286
Craig Mautner39834192012-09-02 07:47:24 -0700287 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700288 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700289 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700290 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800291 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700292
Craig Mautnerdc548482014-02-05 13:35:24 -0800293 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700294 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800295
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800296 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
297 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700298 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700299
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700300 /** Detect user tapping outside of current focused task bounds .*/
301 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700302
Craig Mautner6601b7b2013-04-29 10:29:11 -0700303 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700304 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700305
Craig Mautner6601b7b2013-04-29 10:29:11 -0700306 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800307 private final Rect mTmpRect = new Rect();
308 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700309 private final RectF mTmpRectF = new RectF();
310 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800311 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700312
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800313 WindowManagerService mService;
Craig Mautner9d808b12013-08-06 18:00:25 -0700314
Craig Mautner95da1082014-02-24 17:54:35 -0800315 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700316 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800317
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700318 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700319 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700320
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800321 DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700322
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800323 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
324
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000325 private boolean mHaveBootMsg = false;
326 private boolean mHaveApp = false;
327 private boolean mHaveWallpaper = false;
328 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700329
330 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
331
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700332 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
333 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000334 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
335 new ApplySurfaceChangesTransactionState();
336 private final ScreenshotApplicationState mScreenshotApplicationState =
337 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700338
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700339 // True if this display is in the process of being removed. Used to determine if the removal of
340 // the display's direct children should be allowed.
341 private boolean mRemovingDisplay = false;
342
Bryce Leed1871262017-06-12 14:12:29 -0700343 // {@code false} if this display is in the processing of being created.
344 private boolean mDisplayReady = false;
345
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700346 private final WindowLayersController mLayersController;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800347 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700348 int mInputMethodAnimLayerAdjustment;
349
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800350 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
351 WindowStateAnimator winAnimator = w.mWinAnimator;
352 if (winAnimator.hasSurface()) {
353 final boolean wasAnimating = winAnimator.mWasAnimating;
354 final boolean nowAnimating = winAnimator.stepAnimationLocked(
355 mTmpWindowAnimator.mCurrentTime);
356 winAnimator.mWasAnimating = nowAnimating;
357 mTmpWindowAnimator.orAnimating(nowAnimating);
358
359 if (DEBUG_WALLPAPER) Slog.v(TAG,
360 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
361
362 if (wasAnimating && !winAnimator.mAnimating
363 && mWallpaperController.isWallpaperTarget(w)) {
364 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
365 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
366 if (DEBUG_LAYOUT_REPEATS) {
367 mService.mWindowPlacerLocked.debugLayoutRepeats(
368 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
369 }
370 }
371 }
372
373 final AppWindowToken atoken = w.mAppToken;
374 if (winAnimator.mDrawState == READY_TO_SHOW) {
375 if (atoken == null || atoken.allDrawn) {
376 if (w.performShowLocked()) {
377 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
378 if (DEBUG_LAYOUT_REPEATS) {
379 mService.mWindowPlacerLocked.debugLayoutRepeats(
380 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
381 }
382 }
383 }
384 }
385 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
386 if (appAnimator != null && appAnimator.thumbnail != null) {
387 if (appAnimator.thumbnailTransactionSeq
388 != mTmpWindowAnimator.mAnimTransactionSequence) {
389 appAnimator.thumbnailTransactionSeq =
390 mTmpWindowAnimator.mAnimTransactionSequence;
391 appAnimator.thumbnailLayer = 0;
392 }
393 if (appAnimator.thumbnailLayer < winAnimator.mAnimLayer) {
394 appAnimator.thumbnailLayer = winAnimator.mAnimLayer;
395 }
396 }
397 };
398
399 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
400 final WindowStateAnimator winAnimator = w.mWinAnimator;
401 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
402 return;
403 }
404
405 final int flags = w.mAttrs.flags;
406
407 // If this window is animating, make a note that we have an animating window and take
408 // care of a request to run a detached wallpaper animation.
409 if (winAnimator.mAnimating) {
410 if (winAnimator.mAnimation != null) {
411 if ((flags & FLAG_SHOW_WALLPAPER) != 0
412 && winAnimator.mAnimation.getDetachWallpaper()) {
413 mTmpWindow = w;
414 }
415 final int color = winAnimator.mAnimation.getBackgroundColor();
416 if (color != 0) {
417 final TaskStack stack = w.getStack();
418 if (stack != null) {
419 stack.setAnimationBackground(winAnimator, color);
420 }
421 }
422 }
423 mTmpWindowAnimator.setAnimating(true);
424 }
425
426 // If this window's app token is running a detached wallpaper animation, make a note so
427 // we can ensure the wallpaper is displayed behind it.
428 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
429 if (appAnimator != null && appAnimator.animation != null
430 && appAnimator.animating) {
431 if ((flags & FLAG_SHOW_WALLPAPER) != 0
432 && appAnimator.animation.getDetachWallpaper()) {
433 mTmpWindow = w;
434 }
435
436 final int color = appAnimator.animation.getBackgroundColor();
437 if (color != 0) {
438 final TaskStack stack = w.getStack();
439 if (stack != null) {
440 stack.setAnimationBackground(winAnimator, color);
441 }
442 }
443 }
444 };
445
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800446 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
447 final int lostFocusUid = mTmpWindow.mOwnerUid;
448 final Handler handler = mService.mH;
449 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
450 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
451 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
452 w.mAttrs.hideTimeoutMilliseconds);
453 }
454 }
455 };
456
457 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
458 final AppWindowToken focusedApp = mService.mFocusedApp;
459 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
460 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
461
462 if (!w.canReceiveKeys()) {
463 return false;
464 }
465
466 final AppWindowToken wtoken = w.mAppToken;
467
468 // If this window's application has been removed, just skip it.
469 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
470 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
471 + (wtoken.removed ? "removed" : "sendingToBottom"));
472 return false;
473 }
474
475 if (focusedApp == null) {
476 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
477 + " using new focus @ " + w);
478 mTmpWindow = w;
479 return true;
480 }
481
482 if (!focusedApp.windowsAreFocusable()) {
483 // Current focused app windows aren't focusable...
484 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
485 + " focusable using new focus @ " + w);
486 mTmpWindow = w;
487 return true;
488 }
489
490 // Descend through all of the app tokens and find the first that either matches
491 // win.mAppToken (return win) or mFocusedApp (return null).
492 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
493 if (focusedApp.compareTo(wtoken) > 0) {
494 // App stack below focused app stack. No focus for you!!!
495 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
496 "findFocusedWindow: Reached focused app=" + focusedApp);
497 mTmpWindow = null;
498 return true;
499 }
500 }
501
502 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
503 mTmpWindow = w;
504 return true;
505 };
506
507 private final Consumer<WindowState> mPrepareWindowSurfaces =
508 w -> w.mWinAnimator.prepareSurfaceLocked(true);
509
510 private final Consumer<WindowState> mPerformLayout = w -> {
511 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
512 // wasting time and funky changes while a window is animating away.
513 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
514 || w.isGoneForLayoutLw();
515
516 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
517 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
518 + " mLayoutAttached=" + w.mLayoutAttached
519 + " screen changed=" + w.isConfigChanged());
520 final AppWindowToken atoken = w.mAppToken;
521 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
522 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
523 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
524 + " parentHidden=" + w.isParentWindowHidden());
525 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
526 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
527 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
528 + " parentHidden=" + w.isParentWindowHidden());
529 }
530
531 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
532 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
533 // since that means "perform layout as normal, just don't display").
534 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
535 || ((w.isConfigChanged() || w.setReportResizeHints())
536 && !w.isGoneForLayoutLw() &&
537 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
538 (w.mHasSurface && w.mAppToken != null &&
539 w.mAppToken.layoutConfigChanges)))) {
540 if (!w.mLayoutAttached) {
541 if (mTmpInitial) {
542 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
543 w.mContentChanged = false;
544 }
545 if (w.mAttrs.type == TYPE_DREAM) {
546 // Don't layout windows behind a dream, so that if it does stuff like hide
547 // the status bar we won't get a bad transition when it goes away.
548 mTmpWindow = w;
549 }
550 w.mLayoutNeeded = false;
551 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200552 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800553 mService.mPolicy.layoutWindowLw(w, null);
554 w.mLayoutSeq = mService.mLayoutSeq;
555
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200556 // If this is the first layout, we need to initialize the last inset values as
557 // otherwise we'd immediately cause an unnecessary resize.
558 if (firstLayout) {
559 w.updateLastInsetValues();
560 }
561
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800562 // Window frames may have changed. Update dim layer with the new bounds.
563 final Task task = w.getTask();
564 if (task != null) {
565 mDimLayerController.updateDimLayer(task);
566 }
567
568 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
569 + " mContainingFrame=" + w.mContainingFrame
570 + " mDisplayFrame=" + w.mDisplayFrame);
571 }
572 }
573 };
574
575 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
576 if (w.mLayoutAttached) {
577 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
578 + " mViewVisibility=" + w.mViewVisibility
579 + " mRelayoutCalled=" + w.mRelayoutCalled);
580 // If this view is GONE, then skip it -- keep the current frame, and let the caller
581 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
582 // windows, since that means "perform layout as normal, just don't display").
583 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
584 return;
585 }
586 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
587 || w.mLayoutNeeded) {
588 if (mTmpInitial) {
589 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
590 w.mContentChanged = false;
591 }
592 w.mLayoutNeeded = false;
593 w.prelayout();
594 mService.mPolicy.layoutWindowLw(w, w.getParentWindow());
595 w.mLayoutSeq = mService.mLayoutSeq;
596 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
597 + " mContainingFrame=" + w.mContainingFrame
598 + " mDisplayFrame=" + w.mDisplayFrame);
599 }
600 } else if (w.mAttrs.type == TYPE_DREAM) {
601 // Don't layout windows behind a dream, so that if it does stuff like hide the
602 // status bar we won't get a bad transition when it goes away.
603 mTmpWindow = mTmpWindow2;
604 }
605 };
606
607 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
608 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
609 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
610 return w.canBeImeTarget();
611 };
612
613 private final Consumer<WindowState> mApplyPostLayoutPolicy =
614 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
615 mService.mInputMethodTarget);
616
617 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
618 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
619 final boolean obscuredChanged = w.mObscured !=
620 mTmpApplySurfaceChangesTransactionState.obscured;
621 final RootWindowContainer root = mService.mRoot;
622 // Only used if default window
623 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
624
625 // Update effect.
626 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
627 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
628 final boolean isDisplayed = w.isDisplayedLw();
629
630 if (isDisplayed && w.isObscuringDisplay()) {
631 // This window completely covers everything behind it, so we want to leave all
632 // of them as undimmed (for performance reasons).
633 root.mObscuringWindow = w;
634 mTmpApplySurfaceChangesTransactionState.obscured = true;
635 }
636
637 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
638 root.handleNotObscuredLocked(w,
639 mTmpApplySurfaceChangesTransactionState.obscured,
640 mTmpApplySurfaceChangesTransactionState.syswin);
641
642 if (w.mHasSurface && isDisplayed) {
643 final int type = w.mAttrs.type;
644 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
645 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
646 mTmpApplySurfaceChangesTransactionState.syswin = true;
647 }
648 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
649 && w.mAttrs.preferredRefreshRate != 0) {
650 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
651 = w.mAttrs.preferredRefreshRate;
652 }
653 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
654 && w.mAttrs.preferredDisplayModeId != 0) {
655 mTmpApplySurfaceChangesTransactionState.preferredModeId
656 = w.mAttrs.preferredDisplayModeId;
657 }
658 }
659 }
660
661 w.applyDimLayerIfNeeded();
662
663 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
664 && mWallpaperController.isWallpaperTarget(w)) {
665 // This is the wallpaper target and its obscured state changed... make sure the
666 // current wallpaper's visibility has been updated accordingly.
667 mWallpaperController.updateWallpaperVisibility();
668 }
669
670 w.handleWindowMovedIfNeeded();
671
672 final WindowStateAnimator winAnimator = w.mWinAnimator;
673
674 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
675 w.mContentChanged = false;
676
677 // Moved from updateWindowsAndWallpaperLocked().
678 if (w.mHasSurface) {
679 // Take care of the window being ready to display.
680 final boolean committed = winAnimator.commitFinishDrawingLocked();
681 if (isDefaultDisplay && committed) {
682 if (w.mAttrs.type == TYPE_DREAM) {
683 // HACK: When a dream is shown, it may at that point hide the lock screen.
684 // So we need to redo the layout to let the phone window manager make this
685 // happen.
686 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
687 if (DEBUG_LAYOUT_REPEATS) {
688 surfacePlacer.debugLayoutRepeats(
689 "dream and commitFinishDrawingLocked true",
690 pendingLayoutChanges);
691 }
692 }
693 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
694 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
695 "First draw done in potential wallpaper target " + w);
696 root.mWallpaperMayChange = true;
697 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
698 if (DEBUG_LAYOUT_REPEATS) {
699 surfacePlacer.debugLayoutRepeats(
700 "wallpaper and commitFinishDrawingLocked true",
701 pendingLayoutChanges);
702 }
703 }
704 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700705 final TaskStack stack = w.getStack();
706 if ((!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening())
707 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800708 // Updates the shown frame before we set up the surface. This is needed
709 // because the resizing could change the top-left position (in addition to
710 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
711 // position the surface.
712 //
713 // If an animation is being started, we can't call this method because the
714 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700715 // we do want to update the position if the window is animating. We make an exception
716 // for the bounds animating state, where an application may have been waiting
717 // for an exit animation to start, but instead enters PiP. We need to ensure
718 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800719 winAnimator.computeShownFrameLocked();
720 }
721 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
722 }
723
724 final AppWindowToken atoken = w.mAppToken;
725 if (atoken != null) {
726 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
727 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
728 mTmpUpdateAllDrawn.add(atoken);
729 }
730 }
731
732 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
733 && w.isDisplayedLw()) {
734 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
735 }
736
737 w.updateResizingWindowIfNeeded();
738 };
739
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800740 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800741 * Create new {@link DisplayContent} instance, add itself to the root window container and
742 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700743 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800744 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700745 * @param layersController window layer controller used to assign layer to the windows on this
746 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700747 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
748 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700749 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700750 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700751 WindowLayersController layersController, WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800752 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
753 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
754 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
755 + " new=" + display);
756 }
757
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700758 mDisplay = display;
759 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700760 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700761 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700762 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700763 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700764 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700765 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700766 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800767 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700768 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800769 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700770
771 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700772 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700773 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700774 super.addChild(mAboveAppWindowsContainers, null);
775 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800776
777 // Add itself as a child to the root container.
778 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700779
780 // TODO(b/62541591): evaluate whether this is the best spot to declare the
781 // {@link DisplayContent} ready for use.
782 mDisplayReady = true;
783 }
784
785 boolean isReady() {
786 // The display is ready when the system and the individual display are both ready.
787 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700788 }
789
790 int getDisplayId() {
791 return mDisplayId;
792 }
793
Wale Ogunwale02319a62016-09-26 15:21:22 -0700794 WindowToken getWindowToken(IBinder binder) {
795 return mTokenMap.get(binder);
796 }
797
798 AppWindowToken getAppWindowToken(IBinder binder) {
799 final WindowToken token = getWindowToken(binder);
800 if (token == null) {
801 return null;
802 }
803 return token.asAppWindowToken();
804 }
805
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700806 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700807 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
808 if (dc != null) {
809 // We currently don't support adding a window token to the display if the display
810 // already has the binder mapped to another token. If there is a use case for supporting
811 // this moving forward we will either need to merge the WindowTokens some how or have
812 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700813 throw new IllegalArgumentException("Can't map token=" + token + " to display="
814 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700815 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700816 if (binder == null) {
817 throw new IllegalArgumentException("Can't map token=" + token + " to display="
818 + getName() + " binder is null");
819 }
820 if (token == null) {
821 throw new IllegalArgumentException("Can't map null token to display="
822 + getName() + " binder=" + binder);
823 }
824
Wale Ogunwale02319a62016-09-26 15:21:22 -0700825 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700826
827 if (token.asAppWindowToken() == null) {
828 // Add non-app token to container hierarchy on the display. App tokens are added through
829 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700830 switch (token.windowType) {
831 case TYPE_WALLPAPER:
832 mBelowAppWindowsContainers.addChild(token);
833 break;
834 case TYPE_INPUT_METHOD:
835 case TYPE_INPUT_METHOD_DIALOG:
836 mImeWindowsContainers.addChild(token);
837 break;
838 default:
839 mAboveAppWindowsContainers.addChild(token);
840 break;
841 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700842 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700843 }
844
845 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700846 final WindowToken token = mTokenMap.remove(binder);
847 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800848 token.setExiting();
849 }
850 return token;
851 }
852
853 /** Changes the display the input window token is housed on to this one. */
854 void reParentWindowToken(WindowToken token) {
855 final DisplayContent prevDc = token.getDisplayContent();
856 if (prevDc == this) {
857 return;
858 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800859 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
860 && token.asAppWindowToken() == null) {
861 // Removed the token from the map, but made sure it's not an app token before removing
862 // from parent.
863 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700864 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800865
866 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700867 }
868
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700869 void removeAppToken(IBinder binder) {
870 final WindowToken token = removeWindowToken(binder);
871 if (token == null) {
872 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
873 return;
874 }
875
876 final AppWindowToken appToken = token.asAppWindowToken();
877
878 if (appToken == null) {
879 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
880 return;
881 }
882
883 appToken.onRemovedFromDisplay();
884 }
885
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700886 Display getDisplay() {
887 return mDisplay;
888 }
889
Craig Mautner59c00972012-07-30 12:10:24 -0700890 DisplayInfo getDisplayInfo() {
891 return mDisplayInfo;
892 }
893
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700894 DisplayMetrics getDisplayMetrics() {
895 return mDisplayMetrics;
896 }
897
Andrii Kulian8ee72852017-03-10 10:36:45 -0800898 int getRotation() {
899 return mRotation;
900 }
901
902 void setRotation(int newRotation) {
903 mRotation = newRotation;
904 }
905
906 int getLastOrientation() {
907 return mLastOrientation;
908 }
909
910 void setLastOrientation(int orientation) {
911 mLastOrientation = orientation;
912 }
913
914 boolean getAltOrientation() {
915 return mAltOrientation;
916 }
917
918 void setAltOrientation(boolean altOrientation) {
919 mAltOrientation = altOrientation;
920 }
921
922 int getLastWindowForcedOrientation() {
923 return mLastWindowForcedOrientation;
924 }
925
Andrii Kulian06d07d62017-03-14 11:11:47 -0700926 /**
927 * Update rotation of the display.
928 *
929 * Returns true if the rotation has been changed. In this case YOU MUST CALL
930 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
931 */
932 boolean updateRotationUnchecked(boolean inTransaction) {
933 if (mService.mDeferredRotationPauseCount > 0) {
934 // Rotation updates have been paused temporarily. Defer the update until
935 // updates have been resumed.
936 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
937 return false;
938 }
939
940 ScreenRotationAnimation screenRotationAnimation =
941 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
942 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
943 // Rotation updates cannot be performed while the previous rotation change
944 // animation is still in progress. Skip this update. We will try updating
945 // again after the animation is finished and the display is unfrozen.
946 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
947 return false;
948 }
949 if (mService.mDisplayFrozen) {
950 // Even if the screen rotation animation has finished (e.g. isAnimating
951 // returns false), there is still some time where we haven't yet unfrozen
952 // the display. We also need to abort rotation here.
953 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
954 "Deferring rotation, still finishing previous rotation");
955 return false;
956 }
957
958 if (!mService.mDisplayEnabled) {
959 // No point choosing a rotation if the display is not enabled.
960 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
961 return false;
962 }
963
964 final int oldRotation = mRotation;
965 final int lastOrientation = mLastOrientation;
966 final boolean oldAltOrientation = mAltOrientation;
967 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr897215d2017-03-29 12:25:50 -0700968 final boolean rotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
969 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700970
Robert Carr897215d2017-03-29 12:25:50 -0700971 if (rotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700972 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
973 if (seamlessRotated != null) {
974 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
975 // to complete (that is, waiting for windows to redraw). It's tempting to check
976 // w.mSeamlessRotationCount but that could be incorrect in the case of
977 // window-removal.
978 return false;
979 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700980 }
981
982 // TODO: Implement forced rotation changes.
983 // Set mAltOrientation to indicate that the application is receiving
984 // an orientation that has different metrics than it expected.
985 // eg. Portrait instead of Landscape.
986
987 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
988 lastOrientation, rotation);
989
990 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
991 + ", got rotation " + rotation + " which has "
992 + (altOrientation ? "incompatible" : "compatible") + " metrics");
993
994 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
995 // No change.
996 return false;
997 }
998
999 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
1000 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1001 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1002
1003 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1004 mService.mWaitingForConfig = true;
1005 }
1006
1007 mRotation = rotation;
1008 mAltOrientation = altOrientation;
1009 if (isDefaultDisplay) {
1010 mService.mPolicy.setRotationLw(rotation);
1011 }
1012
1013 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1014 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1015 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1016 WINDOW_FREEZE_TIMEOUT_DURATION);
1017
1018 setLayoutNeeded();
1019 final int[] anim = new int[2];
1020 if (isDimming()) {
1021 anim[0] = anim[1] = 0;
1022 } else {
1023 mService.mPolicy.selectRotationAnimationLw(anim);
1024 }
1025
1026 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001027 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001028 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1029 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1030 mDisplayId);
1031 } else {
1032 // The screen rotation animation uses a screenshot to freeze the screen
1033 // while windows resize underneath.
1034 // When we are rotating seamlessly, we allow the elements to transition
1035 // to their rotated state independently and without a freeze required.
1036 screenRotationAnimation = null;
1037
1038 // We have to reset this in case a window was removed before it
1039 // finished seamless rotation.
1040 mService.mSeamlessRotationCount = 0;
1041 }
1042
1043 // We need to update our screen size information to match the new rotation. If the rotation
1044 // has actually changed then this method will return true and, according to the comment at
1045 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1046 // By updating the Display info here it will be available to
1047 // #computeScreenConfiguration() later.
1048 updateDisplayAndOrientation(getConfiguration().uiMode);
1049
1050 if (!inTransaction) {
1051 if (SHOW_TRANSACTIONS) {
1052 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1053 }
1054 mService.openSurfaceTransaction();
1055 }
1056 try {
1057 // NOTE: We disable the rotation in the emulator because
1058 // it doesn't support hardware OpenGL emulation yet.
1059 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1060 && screenRotationAnimation.hasScreenshot()) {
1061 if (screenRotationAnimation.setRotationInTransaction(
1062 rotation, mService.mFxSession,
1063 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1064 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1065 mService.scheduleAnimationLocked();
1066 }
1067 }
1068
1069 if (rotateSeamlessly) {
1070 forAllWindows(w -> {
1071 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1072 }, true /* traverseTopToBottom */);
1073 }
1074
1075 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1076 } finally {
1077 if (!inTransaction) {
1078 mService.closeSurfaceTransaction();
1079 if (SHOW_LIGHT_TRANSACTIONS) {
1080 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1081 }
1082 }
1083 }
1084
1085 forAllWindows(w -> {
1086 // Discard surface after orientation change, these can't be reused.
1087 if (w.mAppToken != null) {
1088 w.mAppToken.destroySavedSurfaces();
1089 }
1090 if (w.mHasSurface && !rotateSeamlessly) {
1091 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001092 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001093 mService.mRoot.mOrientationChangeComplete = false;
1094 w.mLastFreezeDuration = 0;
1095 }
1096 w.mReportOrientationChanged = true;
1097 }, true /* traverseTopToBottom */);
1098
1099 if (rotateSeamlessly) {
1100 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1101 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1102 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1103 }
1104
1105 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1106 final WindowManagerService.RotationWatcher rotationWatcher
1107 = mService.mRotationWatchers.get(i);
1108 if (rotationWatcher.mDisplayId == mDisplayId) {
1109 try {
1110 rotationWatcher.mWatcher.onRotationChanged(rotation);
1111 } catch (RemoteException e) {
1112 // Ignore
1113 }
1114 }
1115 }
1116
1117 // TODO (multi-display): Magnification is supported only for the default display.
1118 // Announce rotation only if we will not animate as we already have the
1119 // windows in final state. Otherwise, we make this call at the rotation end.
1120 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1121 && isDefaultDisplay) {
1122 mService.mAccessibilityController.onRotationChangedLocked(this);
1123 }
1124
1125 return true;
1126 }
1127
1128 /**
1129 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1130 * changed.
1131 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1132 */
1133 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1134 // Use the effective "visual" dimensions based on current rotation
1135 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1136 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1137 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1138 int dw = realdw;
1139 int dh = realdh;
1140
1141 if (mAltOrientation) {
1142 if (realdw > realdh) {
1143 // Turn landscape into portrait.
1144 int maxw = (int)(realdh/1.3f);
1145 if (maxw < realdw) {
1146 dw = maxw;
1147 }
1148 } else {
1149 // Turn portrait into landscape.
1150 int maxh = (int)(realdw/1.3f);
1151 if (maxh < realdh) {
1152 dh = maxh;
1153 }
1154 }
1155 }
1156
1157 // Update application display metrics.
1158 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1159 mDisplayId);
1160 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1161 mDisplayId);
1162 mDisplayInfo.rotation = mRotation;
1163 mDisplayInfo.logicalWidth = dw;
1164 mDisplayInfo.logicalHeight = dh;
1165 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1166 mDisplayInfo.appWidth = appWidth;
1167 mDisplayInfo.appHeight = appHeight;
1168 if (isDefaultDisplay) {
1169 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1170 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1171 }
1172 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1173 if (mDisplayScalingDisabled) {
1174 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1175 } else {
1176 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1177 }
1178
1179 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1180 mDisplayInfo);
1181
1182 mBaseDisplayRect.set(0, 0, dw, dh);
1183
1184 if (isDefaultDisplay) {
1185 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1186 mCompatDisplayMetrics);
1187 }
1188 return mDisplayInfo;
1189 }
1190
1191 /**
1192 * Compute display configuration based on display properties and policy settings.
1193 * Do not call if mDisplayReady == false.
1194 */
1195 void computeScreenConfiguration(Configuration config) {
1196 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1197
1198 final int dw = displayInfo.logicalWidth;
1199 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001200 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1201 // TODO: Probably best to set this based on some setting in the display content object,
1202 // so the display can be configured for things like fullscreen.
1203 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001204
Andrii Kulian06d07d62017-03-14 11:11:47 -07001205 config.screenWidthDp =
1206 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1207 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1208 config.screenHeightDp =
1209 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1210 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001211
1212 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1213 final int leftInset = mTmpRect.left;
1214 final int topInset = mTmpRect.top;
1215 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001216 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1217 leftInset + displayInfo.appWidth /* right */,
1218 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001219 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1220 || displayInfo.rotation == Surface.ROTATION_270);
1221
1222 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1223 mDisplayMetrics.density, config);
1224
1225 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1226 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1227 ? Configuration.SCREENLAYOUT_ROUND_YES
1228 : Configuration.SCREENLAYOUT_ROUND_NO);
1229
1230 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1231 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1232 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1233 dh, mDisplayId);
1234 config.densityDpi = displayInfo.logicalDensityDpi;
1235
1236 config.colorMode =
1237 (displayInfo.isHdr()
1238 ? Configuration.COLOR_MODE_HDR_YES
1239 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001240 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001241 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1242 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1243
1244 // Update the configuration based on available input devices, lid switch,
1245 // and platform configuration.
1246 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1247 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1248 config.navigation = Configuration.NAVIGATION_NONAV;
1249
1250 int keyboardPresence = 0;
1251 int navigationPresence = 0;
1252 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1253 final int len = devices != null ? devices.length : 0;
1254 for (int i = 0; i < len; i++) {
1255 InputDevice device = devices[i];
1256 if (!device.isVirtual()) {
1257 final int sources = device.getSources();
1258 final int presenceFlag = device.isExternal() ?
1259 WindowManagerPolicy.PRESENCE_EXTERNAL :
1260 WindowManagerPolicy.PRESENCE_INTERNAL;
1261
1262 // TODO(multi-display): Configure on per-display basis.
1263 if (mService.mIsTouchDevice) {
1264 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1265 InputDevice.SOURCE_TOUCHSCREEN) {
1266 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1267 }
1268 } else {
1269 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1270 }
1271
1272 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1273 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1274 navigationPresence |= presenceFlag;
1275 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1276 && config.navigation == Configuration.NAVIGATION_NONAV) {
1277 config.navigation = Configuration.NAVIGATION_DPAD;
1278 navigationPresence |= presenceFlag;
1279 }
1280
1281 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1282 config.keyboard = Configuration.KEYBOARD_QWERTY;
1283 keyboardPresence |= presenceFlag;
1284 }
1285 }
1286 }
1287
1288 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1289 config.navigation = Configuration.NAVIGATION_DPAD;
1290 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1291 }
1292
1293 // Determine whether a hard keyboard is available and enabled.
1294 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1295 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1296 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1297 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1298 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1299 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1300 }
1301
1302 // Let the policy update hidden states.
1303 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1304 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1305 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1306 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1307 }
1308
1309 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1310 int displayId) {
1311 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1312 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1313 final int unrotDw, unrotDh;
1314 if (rotated) {
1315 unrotDw = dh;
1316 unrotDh = dw;
1317 } else {
1318 unrotDw = dw;
1319 unrotDh = dh;
1320 }
1321 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1322 displayId);
1323 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1324 displayId);
1325 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1326 displayId);
1327 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1328 displayId);
1329 return sw;
1330 }
1331
1332 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1333 DisplayMetrics dm, int dw, int dh, int displayId) {
1334 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1335 displayId);
1336 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1337 uiMode, displayId);
1338 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1339 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1340 if (curSize == 0 || size < curSize) {
1341 curSize = size;
1342 }
1343 return curSize;
1344 }
1345
1346 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1347 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1348
1349 // We need to determine the smallest width that will occur under normal
1350 // operation. To this, start with the base screen size and compute the
1351 // width under the different possible rotations. We need to un-rotate
1352 // the current screen dimensions before doing this.
1353 int unrotDw, unrotDh;
1354 if (rotated) {
1355 unrotDw = dh;
1356 unrotDh = dw;
1357 } else {
1358 unrotDw = dw;
1359 unrotDh = dh;
1360 }
1361 displayInfo.smallestNominalAppWidth = 1<<30;
1362 displayInfo.smallestNominalAppHeight = 1<<30;
1363 displayInfo.largestNominalAppWidth = 0;
1364 displayInfo.largestNominalAppHeight = 0;
1365 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1366 unrotDh);
1367 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1368 unrotDw);
1369 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1370 unrotDh);
1371 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1372 unrotDw);
1373 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1374 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1375 displayId);
1376 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1377 displayId);
1378 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1379 displayId);
1380 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1381 displayId);
1382 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1383 outConfig.screenLayout = sl;
1384 }
1385
1386 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1387 int uiMode, int displayId) {
1388 // Get the app screen size at this rotation.
1389 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1390 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1391
1392 // Compute the screen layout size class for this rotation.
1393 int longSize = w;
1394 int shortSize = h;
1395 if (longSize < shortSize) {
1396 int tmp = longSize;
1397 longSize = shortSize;
1398 shortSize = tmp;
1399 }
1400 longSize = (int)(longSize/density);
1401 shortSize = (int)(shortSize/density);
1402 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1403 }
1404
1405 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1406 int uiMode, int dw, int dh) {
1407 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1408 displayId);
1409 if (width < displayInfo.smallestNominalAppWidth) {
1410 displayInfo.smallestNominalAppWidth = width;
1411 }
1412 if (width > displayInfo.largestNominalAppWidth) {
1413 displayInfo.largestNominalAppWidth = width;
1414 }
1415 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1416 displayId);
1417 if (height < displayInfo.smallestNominalAppHeight) {
1418 displayInfo.smallestNominalAppHeight = height;
1419 }
1420 if (height > displayInfo.largestNominalAppHeight) {
1421 displayInfo.largestNominalAppHeight = height;
1422 }
1423 }
1424
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001425 DockedStackDividerController getDockedDividerController() {
1426 return mDividerControllerLocked;
1427 }
1428
Winson Chung655332c2016-10-31 13:14:28 -07001429 PinnedStackController getPinnedStackController() {
1430 return mPinnedStackControllerLocked;
1431 }
1432
Jeff Browna506a6e2013-06-04 00:02:38 -07001433 /**
1434 * Returns true if the specified UID has access to this display.
1435 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001436 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001437 return mDisplay.hasAccess(uid);
1438 }
1439
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001440 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001441 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001442 }
1443
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001444 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001445 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -08001446 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -08001447 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001448 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001449 }
1450
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001451 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001452 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1453 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001454 if (stack.mStackId == stackId) {
1455 return stack;
1456 }
1457 }
1458 return null;
1459 }
1460
Bryce Lee48f4b572017-04-10 10:54:15 -07001461 @VisibleForTesting
1462 int getStackCount() {
1463 return mTaskStackContainers.size();
1464 }
1465
1466 @VisibleForTesting
Wale Ogunwale98d62312017-07-12 09:24:56 -07001467 int getStackPosById(int stackId) {
Bryce Lee48f4b572017-04-10 10:54:15 -07001468 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1469 final TaskStack stack = mTaskStackContainers.get(i);
1470 if (stack.mStackId == stackId) {
1471 return i;
1472 }
1473 }
1474 return -1;
1475 }
1476
Andrii Kulian441e4492016-09-29 15:25:00 -07001477 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001478 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001479 super.onConfigurationChanged(newParentConfig);
1480
Andrii Kulian3a507b52016-09-19 18:14:12 -07001481 // The display size information is heavily dependent on the resources in the current
1482 // configuration, so we need to reconfigure it every time the configuration changes.
1483 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1484 mService.reconfigureDisplayLocked(this);
1485
1486 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001487 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001488 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001489
Andrii Kulian441e4492016-09-29 15:25:00 -07001490 /**
1491 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1492 * bounds were updated.
1493 */
1494 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001495 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1496 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001497 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001498 changedStackList.add(stack.mStackId);
1499 }
1500 }
Winson Chung32c566f2017-04-11 18:31:21 -07001501
1502 // If there was no pinned stack, we still need to notify the controller of the display info
1503 // update as a result of the config change. We do this here to consolidate the flow between
1504 // changes when there is and is not a stack.
1505 if (getStackById(PINNED_STACK_ID) == null) {
1506 mPinnedStackControllerLocked.onDisplayInfoChanged();
1507 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001508 }
1509
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001510 @Override
1511 boolean fillsParent() {
1512 return true;
1513 }
1514
1515 @Override
1516 boolean isVisible() {
1517 return true;
1518 }
1519
1520 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001521 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001522 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001523 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001524 }
1525
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001526 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001527 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1528 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1529 // target, or here in order if there isn't an IME target.
1530 if (traverseTopToBottom) {
1531 for (int i = mChildren.size() - 1; i >= 0; --i) {
1532 final DisplayChildWindowContainer child = mChildren.get(i);
1533 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1534 // In this case the Ime windows will be processed above their target so we skip
1535 // here.
1536 continue;
1537 }
1538 if (child.forAllWindows(callback, traverseTopToBottom)) {
1539 return true;
1540 }
1541 }
1542 } else {
1543 final int count = mChildren.size();
1544 for (int i = 0; i < count; i++) {
1545 final DisplayChildWindowContainer child = mChildren.get(i);
1546 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1547 // In this case the Ime windows will be processed above their target so we skip
1548 // here.
1549 continue;
1550 }
1551 if (child.forAllWindows(callback, traverseTopToBottom)) {
1552 return true;
1553 }
1554 }
1555 }
1556 return false;
1557 }
1558
1559 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1560 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1561 }
1562
Wale Ogunwale399c8692017-05-08 14:22:42 -07001563 @Override
1564 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001565 final WindowManagerPolicy policy = mService.mPolicy;
1566
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001567 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001568 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001569 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001570 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001571 // If the display is frozen, some activities may be in the middle of restarting, and
1572 // thus have removed their old window. If the window has the flag to hide the lock
1573 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1574 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001575 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001576 } else if (policy.isKeyguardLocked()) {
1577 // Use the last orientation the while the display is frozen with the keyguard
1578 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1579 // window. We don't want to check the show when locked window directly though as
1580 // things aren't stable while the display is frozen, for example the window could be
1581 // momentarily unavailable due to activity relaunch.
1582 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001583 + "return " + mLastOrientation);
1584 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001585 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001586 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001587 final int orientation = mAboveAppWindowsContainers.getOrientation();
1588 if (orientation != SCREEN_ORIENTATION_UNSET) {
1589 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001590 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001591 }
1592
Wale Ogunwale399c8692017-05-08 14:22:42 -07001593 // Top system windows are not requesting an orientation. Start searching from apps.
1594 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001595 }
1596
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001597 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001598 // Check if display metrics changed and update base values if needed.
1599 updateBaseDisplayMetricsIfNeeded();
1600
Craig Mautner722285e2012-09-07 13:55:58 -07001601 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001602 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001603
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001604 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1605 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001606 }
Craig Mautner722285e2012-09-07 13:55:58 -07001607 }
1608
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001609 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001610 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1611 if (displayManagerInternal != null) {
1612 // Bootstrap the default logical display from the display manager.
1613 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1614 if (newDisplayInfo != null) {
1615 mDisplayInfo.copyFrom(newDisplayInfo);
1616 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001617 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001618
Andrii Kuliancd097992017-03-23 18:31:59 -07001619 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1620 mDisplayInfo.logicalDensityDpi);
1621 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1622 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1623 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001624 }
1625
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001626 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001627 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001628 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001629 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001630 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1631 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001632 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001633 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001634 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001635 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001636 out.set(left, top, left + width, top + height);
1637 }
1638
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001639 private void getLogicalDisplayRect(Rect out, int orientation) {
1640 getLogicalDisplayRect(out);
1641
1642 // Rotate the Rect if needed.
1643 final int currentRotation = mDisplayInfo.rotation;
1644 final int rotationDelta = deltaRotation(currentRotation, orientation);
1645 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1646 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1647 mTmpRectF.set(out);
1648 mTmpMatrix.mapRect(mTmpRectF);
1649 mTmpRectF.round(out);
1650 }
1651 }
1652
Andrii Kuliancd097992017-03-23 18:31:59 -07001653 /**
1654 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1655 * values.
1656 */
1657 private void updateBaseDisplayMetricsIfNeeded() {
1658 // Get real display metrics without overrides from WM.
1659 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1660 final int orientation = mDisplayInfo.rotation;
1661 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1662 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1663 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1664 final int newDensity = mDisplayInfo.logicalDensityDpi;
1665
1666 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1667 || mInitialDisplayHeight != newHeight
1668 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1669
1670 if (displayMetricsChanged) {
1671 // Check if display size or density is forced.
1672 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1673 || mBaseDisplayHeight != mInitialDisplayHeight;
1674 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1675
1676 // If there is an override set for base values - use it, otherwise use new values.
1677 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1678 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1679 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1680
1681 // Real display metrics changed, so we should also update initial values.
1682 mInitialDisplayWidth = newWidth;
1683 mInitialDisplayHeight = newHeight;
1684 mInitialDisplayDensity = newDensity;
1685 mService.reconfigureDisplayLocked(this);
1686 }
1687 }
1688
Bryce Lee27cec322017-03-21 09:41:37 -07001689 /** Sets the maximum width the screen resolution can be */
1690 void setMaxUiWidth(int width) {
1691 if (DEBUG_DISPLAY) {
1692 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1693 }
1694
1695 mMaxUiWidth = width;
1696
1697 // Update existing metrics.
1698 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1699 }
1700
1701 /** Update base (override) display metrics. */
1702 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1703 mBaseDisplayWidth = baseWidth;
1704 mBaseDisplayHeight = baseHeight;
1705 mBaseDisplayDensity = baseDensity;
1706
1707 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1708 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1709 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1710 mBaseDisplayWidth = mMaxUiWidth;
1711
1712 if (DEBUG_DISPLAY) {
1713 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1714 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1715 + " on display:" + getDisplayId());
1716 }
1717 }
1718
1719 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1720 }
1721
Chong Zhangf66db432016-01-13 10:39:51 -08001722 void getContentRect(Rect out) {
1723 out.set(mContentRect);
1724 }
1725
Wale Ogunwale1666e312016-12-16 11:27:18 -08001726 TaskStack addStackToDisplay(int stackId, boolean onTop) {
1727 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1728 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001729
Wale Ogunwale1666e312016-12-16 11:27:18 -08001730 TaskStack stack = getStackById(stackId);
1731 if (stack != null) {
1732 // It's already attached to the display...clear mDeferRemoval and move stack to
1733 // appropriate z-order on display as needed.
1734 stack.mDeferRemoval = false;
1735 // We're not moving the display to front when we're adding stacks, only when
1736 // requested to change the position of stack explicitly.
1737 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
1738 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -07001739 } else {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001740 stack = new TaskStack(mService, stackId);
Andrii Kulian839def92016-11-02 10:58:58 -07001741 mTaskStackContainers.addStackToDisplay(stack, onTop);
1742 }
1743
Wale Ogunwale1666e312016-12-16 11:27:18 -08001744 if (stackId == DOCKED_STACK_ID) {
1745 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -07001746 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001747 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001748 }
1749
Andrii Kulian51c1b672017-04-07 18:39:32 -07001750 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001751 final DisplayContent prevDc = stack.getDisplayContent();
1752 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001753 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1754 + " which is not currently attached to any display");
1755 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001756 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001757 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1758 + " to its current displayId=" + mDisplayId);
1759 }
1760
Wale Ogunwale1666e312016-12-16 11:27:18 -08001761 prevDc.mTaskStackContainers.removeStackFromDisplay(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001762 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001763 }
1764
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001765 @Override
1766 protected void addChild(DisplayChildWindowContainer child,
1767 Comparator<DisplayChildWindowContainer> comparator) {
1768 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1769 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001770
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001771 @Override
1772 protected void addChild(DisplayChildWindowContainer child, int index) {
1773 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1774 }
1775
1776 @Override
1777 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001778 // Only allow removal of direct children from this display if the display is in the process
1779 // of been removed.
1780 if (mRemovingDisplay) {
1781 super.removeChild(child);
1782 return;
1783 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001784 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001785 }
1786
Andrii Kuliand2765632016-12-12 22:26:34 -08001787 @Override
1788 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1789 // Children of the display are statically ordered, so the real intention here is to perform
1790 // the operation on the display and not the static direct children.
1791 getParent().positionChildAt(position, this, includingParents);
1792 }
1793
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001794 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001795 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1796 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001797 final int taskId = stack.taskIdFromPoint(x, y);
1798 if (taskId != -1) {
1799 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001800 }
1801 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001802 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001803 }
1804
Chong Zhang8e89b312015-09-09 15:09:30 -07001805 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001806 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001807 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001808 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001809 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001810 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001811 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001812 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1813 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001814 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001815 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001816 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001817
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001818 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1819 if (mTmpTaskForResizePointSearchResult.searchDone) {
1820 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001821 }
1822 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001823 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001824 }
1825
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001826 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001827 // The provided task is the task on this display with focus, so if WindowManagerService's
1828 // focused app is not on this display, focusedTask will be null.
1829 if (focusedTask == null) {
1830 mTouchExcludeRegion.setEmpty();
1831 } else {
1832 mTouchExcludeRegion.set(mBaseDisplayRect);
1833 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1834 mTmpRect2.setEmpty();
1835 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1836 final TaskStack stack = mTaskStackContainers.get(stackNdx);
1837 stack.setTouchExcludeRegion(
1838 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
1839 }
1840 // If we removed the focused task above, add it back and only leave its
1841 // outside touch area in the exclusion. TapDectector is not interested in
1842 // any touch inside the focused task itself.
1843 if (!mTmpRect2.isEmpty()) {
1844 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1845 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001846 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001847 final WindowState inputMethod = mService.mInputMethodWindow;
1848 if (inputMethod != null && inputMethod.isVisibleLw()) {
1849 // If the input method is visible and the user is typing, we don't want these touch
1850 // events to be intercepted and used to change focus. This would likely cause a
1851 // disappearance of the input method.
1852 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001853 if (inputMethod.getDisplayId() == mDisplayId) {
1854 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1855 } else {
1856 // IME is on a different display, so we need to update its tap detector.
1857 // TODO(multidisplay): Remove when IME will always appear on same display.
1858 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1859 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001860 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001861 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1862 WindowState win = mTapExcludedWindows.get(i);
1863 win.getTouchableRegion(mTmpRegion);
1864 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1865 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001866 // TODO(multi-display): Support docked stacks on secondary displays.
1867 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001868 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001869 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001870 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1871 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001872 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001873 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001874 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001875 }
1876
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001877 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001878 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001879 super.switchUser();
1880 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001881 }
1882
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001883 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001884 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1885 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001886 }
1887 }
1888
1889 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001890 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001891 }
1892
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001893 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001894 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001895 }
1896
1897 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001898 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001899 }
1900
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001901 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001902 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001903 }
1904
Wale Ogunwale10124582016-09-15 20:25:50 -07001905 @Override
1906 void removeIfPossible() {
1907 if (isAnimating()) {
1908 mDeferredRemoval = true;
1909 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001910 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001911 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001912 }
1913
Wale Ogunwale10124582016-09-15 20:25:50 -07001914 @Override
1915 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001916 mRemovingDisplay = true;
1917 try {
1918 super.removeImmediately();
1919 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1920 mDimLayerController.close();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001921 if (mDisplayId == DEFAULT_DISPLAY && mService.canDispatchPointerEvents()) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001922 mService.unregisterPointerEventListener(mTapDetector);
1923 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1924 }
1925 } finally {
1926 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001927 }
Craig Mautner95da1082014-02-24 17:54:35 -08001928 }
1929
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001930 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001931 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001932 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001933 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1934
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001935 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001936 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001937 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001938 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001939 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001940 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001941 }
1942
Andrii Kulian0214ed92017-05-16 13:44:05 -07001943 /** @return 'true' if removal of this display content is deferred due to active animation. */
1944 boolean isRemovalDeferred() {
1945 return mDeferredRemoval;
1946 }
1947
Wale Ogunwale10124582016-09-15 20:25:50 -07001948 boolean animateForIme(float interpolatedValue, float animationTarget,
1949 float dividerAnimationTarget) {
1950 boolean updated = false;
1951
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001952 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1953 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001954 if (stack == null || !stack.isAdjustedForIme()) {
1955 continue;
1956 }
1957
1958 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1959 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1960 updated = true;
1961 } else {
1962 mDividerControllerLocked.mLastAnimationProgress =
1963 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1964 mDividerControllerLocked.mLastDividerProgress =
1965 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1966 updated |= stack.updateAdjustForIme(
1967 mDividerControllerLocked.mLastAnimationProgress,
1968 mDividerControllerLocked.mLastDividerProgress,
1969 false /* force */);
1970 }
1971 if (interpolatedValue >= 1f) {
1972 stack.endImeAdjustAnimation();
1973 }
1974 }
1975
1976 return updated;
1977 }
1978
1979 boolean clearImeAdjustAnimation() {
1980 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001981 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1982 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001983 if (stack != null && stack.isAdjustedForIme()) {
1984 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1985 changed = true;
1986 }
1987 }
1988 return changed;
1989 }
1990
1991 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001992 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1993 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001994 if (stack.isVisible() && stack.isAdjustedForIme()) {
1995 stack.beginImeAdjustAnimation();
1996 }
1997 }
1998 }
1999
2000 void adjustForImeIfNeeded() {
2001 final WindowState imeWin = mService.mInputMethodWindow;
2002 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2003 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002004 final boolean dockVisible = isStackVisible(DOCKED_STACK_ID);
Wale Ogunwale10124582016-09-15 20:25:50 -07002005 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2006 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2007 imeTargetStack.getDockSide() : DOCKED_INVALID;
2008 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2009 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2010 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
2011 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
2012 final boolean imeHeightChanged = imeVisible &&
2013 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2014
2015 // The divider could be adjusted for IME position, or be thinner than usual,
2016 // or both. There are three possible cases:
2017 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2018 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2019 // - If IME is not visible, divider is not moved and is normal width.
2020
2021 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002022 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2023 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002024 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Winson Chungfef058d2017-04-25 14:43:10 -07002025 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom) &&
2026 StackId.isStackAffectedByDragResizing(stack.mStackId)) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002027 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2028 } else {
2029 stack.resetAdjustedForIme(false);
2030 }
2031 }
2032 mDividerControllerLocked.setAdjustedForIme(
2033 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2034 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002035 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2036 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002037 stack.resetAdjustedForIme(!dockVisible);
2038 }
2039 mDividerControllerLocked.setAdjustedForIme(
2040 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2041 }
Winson Chung655332c2016-10-31 13:14:28 -07002042 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002043 }
2044
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002045 void setInputMethodAnimLayerAdjustment(int adj) {
2046 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2047 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002048 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002049 }
2050
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002051 /**
2052 * If a window that has an animation specifying a colored background and the current wallpaper
2053 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2054 * suddenly disappear.
2055 */
2056 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002057 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2058 w -> w.mIsWallpaper && w.isVisibleNow());
2059
2060 if (visibleWallpaper != null) {
2061 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002062 }
2063 return winAnimator.mAnimLayer;
2064 }
2065
Wale Ogunwale10124582016-09-15 20:25:50 -07002066 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002067 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2068 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002069 stack.prepareFreezingTaskBounds();
2070 }
2071 }
2072
Wale Ogunwale94744212015-09-21 19:01:47 -07002073 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002074 getLogicalDisplayRect(mTmpRect, newRotation);
2075
2076 // Compute a transform matrix to undo the coordinate space transformation,
2077 // and present the window at the same physical position it previously occupied.
2078 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2079 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2080
2081 mTmpRectF.set(bounds);
2082 mTmpMatrix.mapRect(mTmpRectF);
2083 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002084 }
2085
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002086 static int deltaRotation(int oldRotation, int newRotation) {
2087 int delta = newRotation - oldRotation;
2088 if (delta < 0) delta += 4;
2089 return delta;
2090 }
2091
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002092 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002093 Matrix outMatrix) {
2094 // For rotations without Z-ordering we don't need the target rectangle's position.
2095 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2096 displayHeight, outMatrix);
2097 }
2098
2099 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2100 float displayWidth, float displayHeight, Matrix outMatrix) {
2101 switch (rotation) {
2102 case ROTATION_0:
2103 outMatrix.reset();
2104 break;
2105 case ROTATION_270:
2106 outMatrix.setRotate(270, 0, 0);
2107 outMatrix.postTranslate(0, displayHeight);
2108 outMatrix.postTranslate(rectTop, 0);
2109 break;
2110 case ROTATION_180:
2111 outMatrix.reset();
2112 break;
2113 case ROTATION_90:
2114 outMatrix.setRotate(90, 0, 0);
2115 outMatrix.postTranslate(displayWidth, 0);
2116 outMatrix.postTranslate(-rectTop, rectLeft);
2117 break;
2118 }
2119 }
2120
Steven Timotiusaf03df62017-07-18 16:56:43 -07002121 void writeToProto(ProtoOutputStream proto, long fieldId) {
2122 final long token = proto.start(fieldId);
2123 proto.write(ID, mDisplayId);
2124 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2125 final TaskStack stack = mTaskStackContainers.get(stackNdx);
2126 stack.writeToProto(proto, STACKS);
2127 }
2128 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2129 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
2130 for (int i = mAboveAppWindowsContainers.size() - 1; i >= 0; --i) {
2131 final WindowToken windowToken = mAboveAppWindowsContainers.get(i);
2132 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS);
2133 }
2134 for (int i = mBelowAppWindowsContainers.size() - 1; i >= 0; --i) {
2135 final WindowToken windowToken = mBelowAppWindowsContainers.get(i);
2136 windowToken.writeToProto(proto, BELOW_APP_WINDOWS);
2137 }
2138 for (int i = mImeWindowsContainers.size() - 1; i >= 0; --i) {
2139 final WindowToken windowToken = mImeWindowsContainers.get(i);
2140 windowToken.writeToProto(proto, IME_WINDOWS);
2141 }
2142 proto.write(DPI, mBaseDisplayDensity);
2143 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002144 proto.write(ROTATION, mRotation);
2145 final ScreenRotationAnimation screenRotationAnimation =
2146 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2147 if (screenRotationAnimation != null) {
2148 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2149 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002150 proto.end(token);
2151 }
2152
Craig Mautnera91f9e22012-09-14 16:22:08 -07002153 public void dump(String prefix, PrintWriter pw) {
2154 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2155 final String subPrefix = " " + prefix;
2156 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2157 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2158 pw.print("dpi");
2159 if (mInitialDisplayWidth != mBaseDisplayWidth
2160 || mInitialDisplayHeight != mBaseDisplayHeight
2161 || mInitialDisplayDensity != mBaseDisplayDensity) {
2162 pw.print(" base=");
2163 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2164 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2165 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002166 if (mDisplayScalingDisabled) {
2167 pw.println(" noscale");
2168 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002169 pw.print(" cur=");
2170 pw.print(mDisplayInfo.logicalWidth);
2171 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2172 pw.print(" app=");
2173 pw.print(mDisplayInfo.appWidth);
2174 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2175 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2176 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2177 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2178 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002179 pw.println(subPrefix + "deferred=" + mDeferredRemoval
2180 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002181
Craig Mautnerdc548482014-02-05 13:35:24 -08002182 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002183 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002184 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2185 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002186 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002187 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002188
Craig Mautnerdc548482014-02-05 13:35:24 -08002189 pw.println();
2190 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002191 pw.println();
2192 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002193 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002194 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002195 pw.print(" Exiting #"); pw.print(i);
2196 pw.print(' '); pw.print(token);
2197 pw.println(':');
2198 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002199 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002200 }
Craig Mautner59c00972012-07-30 12:10:24 -07002201 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002202 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002203 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002204 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002205 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002206 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002207
2208 if (mInputMethodAnimLayerAdjustment != 0) {
2209 pw.println(subPrefix
2210 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2211 }
Craig Mautner59c00972012-07-30 12:10:24 -07002212 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002213
2214 @Override
2215 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002216 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002217 }
2218
2219 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002220 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002221 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002222
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002223 /** Checks if stack with provided id is visible on this display. */
2224 boolean isStackVisible(int stackId) {
2225 final TaskStack stack = getStackById(stackId);
2226 return (stack != null && stack.isVisible());
2227 }
2228
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002229 /**
2230 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
2231 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07002232 TaskStack getDockedStackLocked() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002233 final TaskStack stack = getStackById(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07002234 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002235 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002236
2237 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002238 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02002239 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002240 */
Jorim Jaggife762342016-10-13 14:33:27 +02002241 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002242 return getStackById(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002243 }
2244
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002245 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002246 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002247 final int x = (int) xf;
2248 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002249 final WindowState touchedWin = getWindow(w -> {
2250 final int flags = w.mAttrs.flags;
2251 if (!w.isVisibleLw()) {
2252 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002253 }
2254 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002255 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002256 }
2257
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002258 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002259 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002260 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002261 }
2262
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002263 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002264
2265 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002266 return mTmpRegion.contains(x, y) || touchFlags == 0;
2267 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002268
2269 return touchedWin;
2270 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002271
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002272 boolean canAddToastWindowForUid(int uid) {
2273 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002274 // Also if the app is focused adding more than one toast at
2275 // a time for better backwards compatibility.
2276 final WindowState focusedWindowForUid = getWindow(w ->
2277 w.mOwnerUid == uid && w.isFocused());
2278 if (focusedWindowForUid != null) {
2279 return true;
2280 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002281 final WindowState win = getWindow(w ->
2282 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2283 && !w.mWindowRemovalAllowed);
2284 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002285 }
2286
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002287 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002288 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2289 return;
2290 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002291
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002292 // Used to communicate the old focus to the callback method.
2293 mTmpWindow = oldFocus;
2294
2295 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002296 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002297
2298 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002299 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002300
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002301 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002302
2303 if (mTmpWindow == null) {
2304 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2305 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002306 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002307 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002308 }
2309
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002310 /** Updates the layer assignment of windows on this display. */
2311 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002312 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002313 if (setLayoutNeeded) {
2314 setLayoutNeeded();
2315 }
2316 }
2317
Wale Ogunwale1666e312016-12-16 11:27:18 -08002318 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2319 // moving containers or resizing them. Need to investigate the best way to have it automatically
2320 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002321 void layoutAndAssignWindowLayersIfNeeded() {
2322 mService.mWindowsChanged = true;
2323 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002324
2325 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2326 false /*updateInputWindows*/)) {
2327 assignWindowLayers(false /* setLayoutNeeded */);
2328 }
2329
2330 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2331 mService.mWindowPlacerLocked.performSurfacePlacement();
2332 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2333 }
2334
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002335 /** Returns true if a leaked surface was destroyed */
2336 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002337 // Used to indicate that a surface was leaked.
2338 mTmpWindow = null;
2339 forAllWindows(w -> {
2340 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002341 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002342 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002343 }
2344 if (!mService.mSessions.contains(wsa.mSession)) {
2345 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002346 + w + " surface=" + wsa.mSurfaceController
2347 + " token=" + w.mToken
2348 + " pid=" + w.mSession.mPid
2349 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002350 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002351 mService.mForceRemoves.add(w);
2352 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002353 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002354 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002355 + w + " surface=" + wsa.mSurfaceController
2356 + " token=" + w.mAppToken
2357 + " saved=" + w.hasSavedSurface());
2358 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002359 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002360 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002361 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002362 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002363
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002364 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002365 }
2366
2367 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002368 * Determine and return the window that should be the IME target.
2369 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2370 * @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 +00002371 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002372 WindowState computeImeTarget(boolean updateImeTarget) {
2373 if (mService.mInputMethodWindow == null) {
2374 // There isn't an IME so there shouldn't be a target...That was easy!
2375 if (updateImeTarget) {
2376 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2377 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2378 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2379 }
2380 return null;
2381 }
2382
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002383 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2384 // same display. Or even when the current IME/target are not on the same screen as the next
2385 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002386 mUpdateImeTarget = updateImeTarget;
2387 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002388
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002389
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002390 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2391 // to be on top of it, but it is not -really- where input will go. So look down below
2392 // for a real window to target...
2393 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2394 final AppWindowToken token = target.mAppToken;
2395 if (token != null) {
2396 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2397 if (betterTarget != null) {
2398 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002399 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002400 }
2401 }
2402
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002403 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2404 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002405
2406 // Now, a special case -- if the last target's window is in the process of exiting, and is
2407 // above the new target, keep on the last target to avoid flicker. Consider for example a
2408 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2409 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2410 // scrim.
2411 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002412 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2413 && (target == null
2414 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002415 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002416 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002417 }
2418
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002419 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2420 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002421
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002422 if (target == null) {
2423 if (updateImeTarget) {
2424 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2425 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2426 + Debug.getCallers(4) : ""));
2427 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2428 }
2429
2430 return null;
2431 }
2432
2433 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002434 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2435 if (token != null) {
2436
2437 // Now some fun for dealing with window animations that modify the Z order. We need
2438 // to look at all windows below the current target that are in this app, finding the
2439 // highest visible one in layering.
2440 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002441 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002442 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002443 }
2444
2445 if (highestTarget != null) {
2446 final AppTransition appTransition = mService.mAppTransition;
2447 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2448 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2449 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002450 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002451
2452 if (appTransition.isTransitionSet()) {
2453 // If we are currently setting up for an animation, hold everything until we
2454 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002455 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2456 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002457 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002458 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002459 // If the window we are currently targeting is involved with an animation,
2460 // and it is on top of the next target we will be over, then hold off on
2461 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002462 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2463 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002464 }
2465 }
2466 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002467
2468 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2469 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2470 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002471 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002472 }
2473
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002474 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002475 }
2476
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002477 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2478 if (target == mService.mInputMethodTarget
2479 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2480 && mInputMethodAnimLayerAdjustment == layerAdj) {
2481 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002482 }
2483
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002484 mService.mInputMethodTarget = target;
2485 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2486 setInputMethodAnimLayerAdjustment(layerAdj);
2487 assignWindowLayers(false /* setLayoutNeeded */);
2488 }
2489
2490 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2491 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2492 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2493 }
2494
2495 // Used to indicate we have reached the first window in the range we are interested in.
2496 mTmpWindow = null;
2497
2498 // TODO: Figure-out a more efficient way to do this.
2499 final WindowState candidate = getWindow(w -> {
2500 if (w == top) {
2501 // Reached the first window in the range we are interested in.
2502 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002503 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002504 if (mTmpWindow == null) {
2505 return false;
2506 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002507
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002508 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2509 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002510 }
2511 // If we reached the bottom of the range of windows we are considering,
2512 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002513 if (w == bottom) {
2514 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002515 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002516 return false;
2517 });
2518
2519 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002520 }
2521
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002522 void setLayoutNeeded() {
2523 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2524 mLayoutNeeded = true;
2525 }
2526
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002527 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002528 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2529 mLayoutNeeded = false;
2530 }
2531
2532 boolean isLayoutNeeded() {
2533 return mLayoutNeeded;
2534 }
2535
Wale Ogunwale02319a62016-09-26 15:21:22 -07002536 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2537 if (mTokenMap.isEmpty()) {
2538 return;
2539 }
2540 pw.println(" Display #" + mDisplayId);
2541 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2542 while (it.hasNext()) {
2543 final WindowToken token = it.next();
2544 pw.print(" ");
2545 pw.print(token);
2546 if (dumpAll) {
2547 pw.println(':');
2548 token.dump(pw, " ");
2549 } else {
2550 pw.println();
2551 }
2552 }
2553 }
2554
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002555 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002556 final int[] index = new int[1];
2557 forAllWindows(w -> {
2558 final WindowStateAnimator wAnim = w.mWinAnimator;
2559 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2560 index[0] = index[0] + 1;
2561 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002562 }
2563
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002564 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002565 forAllWindows(w -> {
2566 w.mWinAnimator.enableSurfaceTrace(fd);
2567 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002568 }
2569
2570 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002571 forAllWindows(w -> {
2572 w.mWinAnimator.disableSurfaceTrace();
2573 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002574 }
2575
Jorim Jaggife762342016-10-13 14:33:27 +02002576 /**
2577 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2578 */
2579 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2580 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002581 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002582 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2583 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002584 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002585 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2586 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002587 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002588 }
2589
Wale Ogunwale494009b82016-10-21 09:01:38 -07002590 boolean checkWaitingForWindows() {
2591
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002592 mHaveBootMsg = false;
2593 mHaveApp = false;
2594 mHaveWallpaper = false;
2595 mHaveKeyguard = true;
2596
2597 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002598 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2599 return true;
2600 }
2601 if (w.isDrawnLw()) {
2602 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002603 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002604 } else if (w.mAttrs.type == TYPE_APPLICATION
2605 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002606 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002607 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002608 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002609 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002610 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002611 }
2612 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002613 return false;
2614 });
2615
2616 if (visibleWindow != null) {
2617 // We have a visible window.
2618 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002619 }
2620
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002621 // if the wallpaper service is disabled on the device, we're never going to have
2622 // wallpaper, don't bother waiting for it
2623 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2624 com.android.internal.R.bool.config_enableWallpaperService)
2625 && !mService.mOnlyCore;
2626
Wale Ogunwale494009b82016-10-21 09:01:38 -07002627 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2628 "******** booted=" + mService.mSystemBooted
2629 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002630 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2631 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2632 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002633
2634 // If we are turning on the screen to show the boot message, don't do it until the boot
2635 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002636 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002637 return true;
2638 }
2639
2640 // If we are turning on the screen after the boot is completed normally, don't do so until
2641 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002642 if (mService.mSystemBooted
2643 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002644 return true;
2645 }
2646
2647 return false;
2648 }
2649
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002650 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002651 mTmpWindowAnimator = animator;
2652 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002653 }
2654
2655 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002656 resetAnimationBackgroundAnimator();
2657
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002658 // Used to indicate a detached wallpaper.
2659 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002660 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002661
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002662 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002663
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002664 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002665 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002666 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2667 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002668 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2669 }
2670 }
2671
2672 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002673 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002674 }
2675
Wale Ogunwale494009b82016-10-21 09:01:38 -07002676 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002677 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002678 if (imFocus == null) {
2679 return false;
2680 }
2681
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002682 if (DEBUG_INPUT_METHOD) {
2683 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2684 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2685 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002686 }
2687
Wale Ogunwale494009b82016-10-21 09:01:38 -07002688 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2689
2690 if (DEBUG_INPUT_METHOD) {
2691 Slog.i(TAG_WM, "IM target client: " + imeClient);
2692 if (imeClient != null) {
2693 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2694 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2695 }
2696 }
2697
2698 return imeClient != null && imeClient.asBinder() == client.asBinder();
2699 }
2700
2701 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002702 final WindowState win = getWindow(
2703 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2704 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002705 }
2706
2707 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002708 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002709 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002710 final int curValue = w.mSystemUiVisibility;
2711 final int diff = (curValue ^ visibility) & globalDiff;
2712 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002713 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002714 w.mSeq++;
2715 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002716 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002717 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2718 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002719 visibility, newValue, diff);
2720 }
2721 } catch (RemoteException e) {
2722 // so sorry
2723 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002724 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002725 }
2726
2727 void onWindowFreezeTimeout() {
2728 Slog.w(TAG_WM, "Window freeze timeout expired.");
2729 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002730
2731 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002732 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002733 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002734 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002735 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002736 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2737 - mService.mDisplayFreezeTime);
2738 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002739 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002740 mService.mWindowPlacerLocked.performSurfacePlacement();
2741 }
2742
2743 void waitForAllWindowsDrawn() {
2744 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002745 forAllWindows(w -> {
2746 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2747 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2748 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002749 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002750 w.mLastContentInsets.set(-1, -1, -1, -1);
2751 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002752 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002753 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002754 }
2755
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002756 // TODO: Super crazy long method that should be broken down...
2757 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2758
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002759 final int dw = mDisplayInfo.logicalWidth;
2760 final int dh = mDisplayInfo.logicalHeight;
2761 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2762
2763 mTmpUpdateAllDrawn.clear();
2764
2765 int repeats = 0;
2766 do {
2767 repeats++;
2768 if (repeats > 6) {
2769 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2770 clearLayoutNeeded();
2771 break;
2772 }
2773
2774 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2775 pendingLayoutChanges);
2776
Andrii Kulian839def92016-11-02 10:58:58 -07002777 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2778 // the wallpaper window jumping across displays.
2779 // Remove check for default display when there will be support for multiple wallpaper
2780 // targets (on different displays).
2781 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002782 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002783 }
2784
2785 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2786 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2787 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2788 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002789 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002790 }
2791 }
2792
2793 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2794 setLayoutNeeded();
2795 }
2796
2797 // FIRST LOOP: Perform a layout, if needed.
2798 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2799 performLayout(repeats == 1, false /* updateInputWindows */);
2800 } else {
2801 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2802 }
2803
2804 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2805 pendingLayoutChanges = 0;
2806
2807 if (isDefaultDisplay) {
2808 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002809 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002810 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2811 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2812 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2813 }
2814 } while (pendingLayoutChanges != 0);
2815
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002816 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002817 resetDimming();
2818
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002819 mTmpRecoveringMemory = recoveringMemory;
2820 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002821
2822 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002823 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2824 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2825 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002826 true /* inTraversal, must call performTraversalInTrans... below */);
2827
2828 stopDimmingIfNeeded();
2829
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002830 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2831 if (wallpaperVisible != mLastWallpaperVisible) {
2832 mLastWallpaperVisible = wallpaperVisible;
2833 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2834 }
2835
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002836 while (!mTmpUpdateAllDrawn.isEmpty()) {
2837 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2838 // See if any windows have been drawn, so they (and others associated with them)
2839 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002840 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002841 }
2842
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002843 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002844 }
2845
2846 void performLayout(boolean initial, boolean updateInputWindows) {
2847 if (!isLayoutNeeded()) {
2848 return;
2849 }
2850 clearLayoutNeeded();
2851
2852 final int dw = mDisplayInfo.logicalWidth;
2853 final int dh = mDisplayInfo.logicalHeight;
2854
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002855 if (DEBUG_LAYOUT) {
2856 Slog.v(TAG, "-------------------------------------");
2857 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2858 }
2859
Andrii Kulian8ee72852017-03-10 10:36:45 -08002860 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002861 getConfiguration().uiMode);
2862 if (isDefaultDisplay) {
2863 // Not needed on non-default displays.
2864 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2865 mService.mScreenRect.set(0, 0, dw, dh);
2866 }
2867
2868 mService.mPolicy.getContentRectLw(mContentRect);
2869
2870 int seq = mService.mLayoutSeq + 1;
2871 if (seq < 0) seq = 0;
2872 mService.mLayoutSeq = seq;
2873
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002874 // Used to indicate that we have processed the dream window and all additional windows are
2875 // behind it.
2876 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002877 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002878
2879 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002880 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002881
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002882 // Used to indicate that we have processed the dream window and all additional attached
2883 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002884 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002885 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002886
2887 // Now perform layout of attached windows, which usually depend on the position of the
2888 // window they are attached to. XXX does not deal with windows that are attached to windows
2889 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002890 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002891
2892 // Window frames may have changed. Tell the input dispatcher about it.
2893 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2894 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2895 if (updateInputWindows) {
2896 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2897 }
2898
2899 mService.mPolicy.finishLayoutLw();
2900 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2901 }
2902
2903 /**
2904 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2905 * In portrait mode, it grabs the full screenshot.
2906 *
2907 * @param width the width of the target bitmap
2908 * @param height the height of the target bitmap
2909 * @param includeFullDisplay true if the screen should not be cropped before capture
2910 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2911 * @param config of the output bitmap
2912 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002913 * @param includeDecor whether to include window decors, like the status or navigation bar
2914 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002915 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002916 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002917 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002918 boolean wallpaperOnly, boolean includeDecor) {
2919 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2920 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002921 if (bitmap == null) {
2922 return null;
2923 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002924
2925 if (DEBUG_SCREENSHOT) {
2926 // TEST IF IT's ALL BLACK
2927 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2928 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2929 bitmap.getHeight());
2930 boolean allBlack = true;
2931 final int firstColor = buffer[0];
2932 for (int i = 0; i < buffer.length; i++) {
2933 if (buffer[i] != firstColor) {
2934 allBlack = false;
2935 break;
2936 }
2937 }
2938 if (allBlack) {
2939 final WindowState appWin = mScreenshotApplicationState.appWin;
2940 final int maxLayer = mScreenshotApplicationState.maxLayer;
2941 final int minLayer = mScreenshotApplicationState.minLayer;
2942 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2943 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2944 (appWin != null ?
2945 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2946 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2947 }
2948 }
2949
2950 // Create a copy of the screenshot that is immutable and backed in ashmem.
2951 // This greatly reduces the overhead of passing the bitmap between processes.
2952 Bitmap ret = bitmap.createAshmemBitmap(config);
2953 bitmap.recycle();
2954 return ret;
2955 }
2956
2957 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2958 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2959 boolean includeDecor) {
2960 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2961 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2962 }
2963
2964 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2965 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2966 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002967 int dw = mDisplayInfo.logicalWidth;
2968 int dh = mDisplayInfo.logicalHeight;
2969 if (dw == 0 || dh == 0) {
2970 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2971 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2972 return null;
2973 }
2974
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002975 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002976
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002977 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002978 final Rect frame = new Rect();
2979 final Rect stackBounds = new Rect();
2980
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08002981 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002982 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08002983 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002984 synchronized(mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02002985 if (!mService.mPolicy.isScreenOn()) {
2986 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Attempted to take screenshot while display"
2987 + " was off.");
2988 return null;
2989 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002990 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002991 mScreenshotApplicationState.appWin = null;
2992 forAllWindows(w -> {
2993 if (!w.mHasSurface) {
2994 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002995 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002996 if (w.mLayer >= aboveAppLayer) {
2997 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002998 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002999 if (wallpaperOnly && !w.mIsWallpaper) {
3000 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003001 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003002 if (w.mIsImWindow) {
Jorim Jaggieb59d6e2017-05-04 13:54:22 +02003003 return false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003004 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003005 // If this is the wallpaper layer and we're only looking for the wallpaper layer
3006 // then the target window state is this one.
3007 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003008 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003009 }
3010
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003011 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003012 // We have not ran across the target window yet, so it is probably behind
3013 // the wallpaper. This can happen when the keyguard is up and all windows
3014 // are moved behind the wallpaper. We don't want to include the wallpaper
3015 // layer in the screenshot as it will cover-up the layer of the target
3016 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003017 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003018 }
3019 // Fall through. The target window is in front of the wallpaper. For this
3020 // case we want to include the wallpaper layer in the screenshot because
3021 // the target window might have some transparent areas.
3022 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003023 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003024 // This app window is of no interest if it is not associated with the
3025 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003026 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003027 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003028 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003029 }
3030
3031 // Include this window.
3032
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003033 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003034 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003035 if (mScreenshotApplicationState.maxLayer < layer) {
3036 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003037 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003038 if (mScreenshotApplicationState.minLayer > layer) {
3039 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003040 }
3041
3042 // Don't include wallpaper in bounds calculation
Jorim Jaggi70f59482017-05-04 14:05:59 +02003043 if (!w.mIsWallpaper && !mutableIncludeFullDisplay.value) {
3044 if (includeDecor) {
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003045 final Task task = w.getTask();
3046 if (task != null) {
3047 task.getBounds(frame);
3048 } else {
Jorim Jaggi30d64f32017-04-07 16:33:17 +02003049
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003050 // No task bounds? Too bad! Ain't no screenshot then.
3051 return true;
3052 }
Jorim Jaggi70f59482017-05-04 14:05:59 +02003053 } else {
3054 final Rect wf = w.mFrame;
3055 final Rect cr = w.mContentInsets;
3056 int left = wf.left + cr.left;
3057 int top = wf.top + cr.top;
3058 int right = wf.right - cr.right;
3059 int bottom = wf.bottom - cr.bottom;
3060 frame.union(left, top, right, bottom);
3061 w.getVisibleBounds(stackBounds);
3062 if (!Rect.intersects(frame, stackBounds)) {
3063 // Set frame empty if there's no intersection.
3064 frame.setEmpty();
3065 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003066 }
3067 }
3068
3069 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003070 (w.mAppToken != null && w.mAppToken.token == appToken)
3071 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi52e85da2017-01-24 16:21:39 +01003072 if (foundTargetWs && winAnim.getShown()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003073 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003074 }
3075
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003076 if (w.isObscuringDisplay()){
3077 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003078 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003079 return false;
3080 }, true /* traverseTopToBottom */);
3081
3082 final WindowState appWin = mScreenshotApplicationState.appWin;
3083 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
3084 final int maxLayer = mScreenshotApplicationState.maxLayer;
3085 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003086
3087 if (appToken != null && appWin == null) {
3088 // Can't find a window to snapshot.
3089 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
3090 "Screenshot: Couldn't find a surface matching " + appToken);
3091 return null;
3092 }
3093
3094 if (!screenshotReady) {
3095 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3096 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3097 appWin.mWinAnimator.mDrawState)));
3098 return null;
3099 }
3100
3101 // Screenshot is ready to be taken. Everything from here below will continue
3102 // through the bottom of the loop and return a value. We only stay in the loop
3103 // because we don't want to release the mWindowMap lock until the screenshot is
3104 // taken.
3105
3106 if (maxLayer == 0) {
3107 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3108 + ": returning null maxLayer=" + maxLayer);
3109 return null;
3110 }
3111
Jorim Jaggi02886a82016-12-06 09:10:06 -08003112 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003113 // Constrain frame to the screen size.
3114 if (!frame.intersect(0, 0, dw, dh)) {
3115 frame.setEmpty();
3116 }
3117 } else {
3118 // Caller just wants entire display.
3119 frame.set(0, 0, dw, dh);
3120 }
3121 if (frame.isEmpty()) {
3122 return null;
3123 }
3124
3125 if (width < 0) {
3126 width = (int) (frame.width() * frameScale);
3127 }
3128 if (height < 0) {
3129 height = (int) (frame.height() * frameScale);
3130 }
3131
3132 // Tell surface flinger what part of the image to crop. Take the top
3133 // right part of the application, and crop the larger dimension to fit.
3134 Rect crop = new Rect(frame);
3135 if (width / (float) frame.width() < height / (float) frame.height()) {
3136 int cropWidth = (int)((float)width / (float)height * frame.height());
3137 crop.right = crop.left + cropWidth;
3138 } else {
3139 int cropHeight = (int)((float)height / (float)width * frame.width());
3140 crop.bottom = crop.top + cropHeight;
3141 }
3142
3143 // The screenshot API does not apply the current screen rotation.
3144 int rot = mDisplay.getRotation();
3145
3146 if (rot == ROTATION_90 || rot == ROTATION_270) {
3147 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3148 }
3149
3150 // Surfaceflinger is not aware of orientation, so convert our logical
3151 // crop to surfaceflinger's portrait orientation.
3152 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3153
3154 if (DEBUG_SCREENSHOT) {
3155 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3156 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003157 forAllWindows(w -> {
3158 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3159 Slog.i(TAG_WM, w + ": " + w.mLayer
3160 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003161 + " surfaceLayer=" + ((controller == null)
3162 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003163 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003164 }
3165
3166 final ScreenRotationAnimation screenRotationAnimation =
3167 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3168 final boolean inRotation = screenRotationAnimation != null &&
3169 screenRotationAnimation.isAnimating();
3170 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3171 "Taking screenshot while rotating");
3172
3173 // We force pending transactions to flush before taking
3174 // the screenshot by pushing an empty synchronous transaction.
3175 SurfaceControl.openTransaction();
3176 SurfaceControl.closeTransactionSync();
3177
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003178 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003179 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003180 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003181 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3182 + ") to layer " + maxLayer);
3183 return null;
3184 }
3185 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003186 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003187 }
3188
3189 // TODO: Can this use createRotationMatrix()?
3190 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3191 if (rot == Surface.ROTATION_90) {
3192 final int tmp = crop.top;
3193 crop.top = dw - crop.right;
3194 crop.right = crop.bottom;
3195 crop.bottom = dw - crop.left;
3196 crop.left = tmp;
3197 } else if (rot == Surface.ROTATION_180) {
3198 int tmp = crop.top;
3199 crop.top = dh - crop.bottom;
3200 crop.bottom = dh - tmp;
3201 tmp = crop.right;
3202 crop.right = dw - crop.left;
3203 crop.left = dw - tmp;
3204 } else if (rot == Surface.ROTATION_270) {
3205 final int tmp = crop.top;
3206 crop.top = crop.left;
3207 crop.left = dh - crop.bottom;
3208 crop.bottom = crop.right;
3209 crop.right = dh - tmp;
3210 }
3211 }
3212
3213 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003214 // Used to indicate the layout is needed.
3215 mTmpWindow = null;
3216
3217 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003218 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003219 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003220 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003221 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003222 w.setDisplayLayoutNeeded();
3223 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003224 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003225
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003226 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003227 mService.mWindowPlacerLocked.performSurfacePlacement();
3228 }
3229 }
3230
Wale Ogunwale1666e312016-12-16 11:27:18 -08003231 void setExitingTokensHasVisible(boolean hasVisible) {
3232 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3233 mExitingTokens.get(i).hasVisible = hasVisible;
3234 }
3235
3236 // Initialize state of exiting applications.
3237 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3238 }
3239
3240 void removeExistingTokensIfPossible() {
3241 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3242 final WindowToken token = mExitingTokens.get(i);
3243 if (!token.hasVisible) {
3244 mExitingTokens.remove(i);
3245 }
3246 }
3247
3248 // Time to remove any exiting applications?
3249 mTaskStackContainers.removeExistingAppTokensIfPossible();
3250 }
3251
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003252 @Override
3253 void onDescendantOverrideConfigurationChanged() {
3254 setLayoutNeeded();
3255 mService.requestTraversal();
3256 }
3257
David Stevens9440dc82017-03-16 19:00:20 -07003258 boolean okToDisplay() {
3259 if (mDisplayId == DEFAULT_DISPLAY) {
3260 return !mService.mDisplayFrozen
3261 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3262 }
3263 return mDisplayInfo.state == Display.STATE_ON;
3264 }
3265
3266 boolean okToAnimate() {
3267 return okToDisplay() &&
3268 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3269 }
3270
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003271 static final class TaskForResizePointSearchResult {
3272 boolean searchDone;
3273 Task taskForResize;
3274
3275 void reset() {
3276 searchDone = false;
3277 taskForResize = null;
3278 }
3279 }
Robert Carr3b716242016-08-16 16:02:21 -07003280
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003281 private static final class ApplySurfaceChangesTransactionState {
3282 boolean displayHasContent;
3283 boolean obscured;
3284 boolean syswin;
3285 boolean focusDisplayed;
3286 float preferredRefreshRate;
3287 int preferredModeId;
3288
3289 void reset() {
3290 displayHasContent = false;
3291 obscured = false;
3292 syswin = false;
3293 focusDisplayed = false;
3294 preferredRefreshRate = 0;
3295 preferredModeId = 0;
3296 }
3297 }
3298
3299 private static final class ScreenshotApplicationState {
3300 WindowState appWin;
3301 int maxLayer;
3302 int minLayer;
3303 boolean screenshotReady;
3304
3305 void reset(boolean screenshotReady) {
3306 appWin = null;
3307 maxLayer = 0;
3308 minLayer = 0;
3309 this.screenshotReady = screenshotReady;
3310 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3311 }
3312 }
3313
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003314 /**
3315 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3316 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3317 * homogeneous children type which is currently required by sub-classes of
3318 * {@link WindowContainer} class.
3319 */
3320 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3321
3322 int size() {
3323 return mChildren.size();
3324 }
3325
3326 E get(int index) {
3327 return mChildren.get(index);
3328 }
3329
3330 @Override
3331 boolean fillsParent() {
3332 return true;
3333 }
3334
3335 @Override
3336 boolean isVisible() {
3337 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003338 }
3339 }
3340
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003341 /**
3342 * Window container class that contains all containers on this display relating to Apps.
3343 * I.e Activities.
3344 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003345 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003346
Andrii Kulian839def92016-11-02 10:58:58 -07003347 /**
3348 * Adds the stack to this container.
3349 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3350 */
3351 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003352 if (stack.mStackId == HOME_STACK_ID) {
3353 if (mHomeStack != null) {
3354 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3355 }
3356 mHomeStack = stack;
3357 }
3358 addChild(stack, onTop);
3359 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003360 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003361
Andrii Kulian839def92016-11-02 10:58:58 -07003362 /** Removes the stack from its container and prepare for changing the parent. */
3363 void removeStackFromDisplay(TaskStack stack) {
3364 removeChild(stack);
3365 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07003366 }
3367
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003368 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003369 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3370 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003371 addChild(stack, addIndex);
3372 setLayoutNeeded();
3373 }
3374
Bryce Lee00d586d2017-07-28 20:48:43 -07003375
3376 @Override
3377 boolean isOnTop() {
3378 // Considered always on top
3379 return true;
3380 }
3381
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003382 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003383 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003384 if (child.getWindowConfiguration().isAlwaysOnTop()
3385 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003386 // This stack is always-on-top, override the default behavior.
3387 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3388
3389 // Moving to its current position, as we must call super but we don't want to
3390 // perform any meaningful action.
3391 final int currentPosition = mChildren.indexOf(child);
3392 super.positionChildAt(currentPosition, child, false /* includingParents */);
3393 return;
3394 }
3395
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003396 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3397 super.positionChildAt(targetPosition, child, includingParents);
3398
3399 setLayoutNeeded();
3400 }
3401
3402 /**
3403 * When stack is added or repositioned, find a proper position for it.
3404 * This will make sure that pinned stack always stays on top.
3405 * @param requestedPosition Position requested by caller.
3406 * @param stack Stack to be added or positioned.
3407 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3408 * @return The proper position for the stack.
3409 */
3410 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3411 final int topChildPosition = mChildren.size() - 1;
3412 boolean toTop = requestedPosition == POSITION_TOP;
3413 toTop |= adding ? requestedPosition >= topChildPosition + 1
3414 : requestedPosition >= topChildPosition;
3415 int targetPosition = requestedPosition;
3416
Bryce Lee48f4b572017-04-10 10:54:15 -07003417 if (toTop && stack.mStackId != PINNED_STACK_ID
3418 && getStackById(PINNED_STACK_ID) != null) {
3419 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003420 TaskStack topStack = mChildren.get(topChildPosition);
3421 if (topStack.mStackId != PINNED_STACK_ID) {
3422 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3423 }
3424
3425 // So, stack is moved just below the pinned stack.
3426 // When we're adding a new stack the target is the current pinned stack position.
3427 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003428 // stack, because WindowContainer#positionAt() first removes element and then adds
3429 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003430 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3431 }
3432
3433 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003434 }
3435
3436 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003437 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3438 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003439 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003440 if (super.forAllWindows(callback, traverseTopToBottom)) {
3441 return true;
3442 }
3443 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3444 return true;
3445 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003446 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003447 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3448 return true;
3449 }
3450 if (super.forAllWindows(callback, traverseTopToBottom)) {
3451 return true;
3452 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003453 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003454 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003455 }
3456
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003457 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003458 boolean traverseTopToBottom) {
3459 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3460 // app tokens.
3461 // TODO: Investigate if we need to continue to do this or if we can just process them
3462 // in-order.
3463 if (traverseTopToBottom) {
3464 for (int i = mChildren.size() - 1; i >= 0; --i) {
3465 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3466 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003467 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3468 traverseTopToBottom)) {
3469 return true;
3470 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003471 }
3472 }
3473 } else {
3474 final int count = mChildren.size();
3475 for (int i = 0; i < count; ++i) {
3476 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3477 final int appTokensCount = appTokens.size();
3478 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003479 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3480 traverseTopToBottom)) {
3481 return true;
3482 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003483 }
3484 }
3485 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003486 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003487 }
3488
Wale Ogunwale1666e312016-12-16 11:27:18 -08003489 void setExitingTokensHasVisible(boolean hasVisible) {
3490 for (int i = mChildren.size() - 1; i >= 0; --i) {
3491 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3492 for (int j = appTokens.size() - 1; j >= 0; --j) {
3493 appTokens.get(j).hasVisible = hasVisible;
3494 }
3495 }
3496 }
3497
3498 void removeExistingAppTokensIfPossible() {
3499 for (int i = mChildren.size() - 1; i >= 0; --i) {
3500 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3501 for (int j = appTokens.size() - 1; j >= 0; --j) {
3502 final AppWindowToken token = appTokens.get(j);
3503 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3504 && (!token.mIsExiting || token.isEmpty())) {
3505 // Make sure there is no animation running on this token, so any windows
3506 // associated with it will be removed as soon as their animations are
3507 // complete.
3508 token.mAppAnimator.clearAnimation();
3509 token.mAppAnimator.animating = false;
3510 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3511 "performLayout: App token exiting now removed" + token);
3512 token.removeIfPossible();
3513 }
3514 }
3515 }
3516 }
3517
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003518 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003519 int getOrientation() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003520 if (isStackVisible(DOCKED_STACK_ID) || isStackVisible(FREEFORM_WORKSPACE_STACK_ID)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003521 // Apps and their containers are not allowed to specify an orientation while the
3522 // docked or freeform stack is visible...except for the home stack/task if the
3523 // docked stack is minimized and it actually set something.
3524 if (mHomeStack != null && mHomeStack.isVisible()
3525 && mDividerControllerLocked.isMinimizedDock()) {
3526 final int orientation = mHomeStack.getOrientation();
3527 if (orientation != SCREEN_ORIENTATION_UNSET) {
3528 return orientation;
3529 }
3530 }
3531 return SCREEN_ORIENTATION_UNSPECIFIED;
3532 }
3533
3534 final int orientation = super.getOrientation();
3535 if (orientation != SCREEN_ORIENTATION_UNSET
3536 && orientation != SCREEN_ORIENTATION_BEHIND) {
3537 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3538 "App is requesting an orientation, return " + orientation);
3539 return orientation;
3540 }
3541
3542 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003543 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003544 // The next app has not been requested to be visible, so we keep the current orientation
3545 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003546 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003547 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003548 }
3549
3550 /**
3551 * Window container class that contains all containers on this display that are not related to
3552 * Apps. E.g. status bar.
3553 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003554 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3555 /**
3556 * Compares two child window tokens returns -1 if the first is lesser than the second in
3557 * terms of z-order and 1 otherwise.
3558 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003559 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003560 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003561 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3562 token1.mOwnerCanManageAppTokens)
3563 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3564 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003565
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003566 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3567 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3568 return false;
3569 }
3570 final int req = w.mAttrs.screenOrientation;
3571 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3572 || req == SCREEN_ORIENTATION_UNSET) {
3573 return false;
3574 }
3575 return true;
3576 };
3577
Wale Ogunwale3a931692016-11-02 16:49:48 -07003578 private final String mName;
3579 NonAppWindowContainers(String name) {
3580 mName = name;
3581 }
3582
3583 void addChild(WindowToken token) {
3584 addChild(token, mWindowComparator);
3585 }
3586
3587 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003588 int getOrientation() {
3589 final WindowManagerPolicy policy = mService.mPolicy;
3590 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003591 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003592
3593 if (win != null) {
3594 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003595 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003596 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003597 if (mService.mKeyguardGoingAway) {
3598 // Keyguard can't affect the orientation if it is going away...
3599 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3600 return SCREEN_ORIENTATION_UNSET;
3601 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003602 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003603 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003604 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003605 }
3606
Andrii Kulian8ee72852017-03-10 10:36:45 -08003607 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003608
3609 if (policy.isKeyguardShowingAndNotOccluded()) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003610 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003611 }
3612
3613 return SCREEN_ORIENTATION_UNSET;
3614 }
3615
3616 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003617 String getName() {
3618 return mName;
3619 }
Robert Carr3b716242016-08-16 16:02:21 -07003620 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003621
3622 /**
3623 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3624 */
3625 @FunctionalInterface
3626 private interface Screenshoter<E> {
3627 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3628 boolean useIdentityTransform, int rotation);
3629 }
Craig Mautner59c00972012-07-30 12:10:24 -07003630}