blob: 7ae5d1a872de6054ab9c73779e632b2a673e7630 [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;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070022import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale687b4272017-07-27 02:56:23 -070023import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070024import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
25import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070026import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070027import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
28import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale687b4272017-07-27 02:56:23 -070029import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
30import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070031import static android.view.Display.DEFAULT_DISPLAY;
32import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070033import static android.view.Surface.ROTATION_0;
34import static android.view.Surface.ROTATION_180;
35import static android.view.Surface.ROTATION_270;
36import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070037import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070038import static android.view.WindowManager.DOCKED_BOTTOM;
39import static android.view.WindowManager.DOCKED_INVALID;
40import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080041import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
42import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
43import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070044import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070045import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070046import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
47import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070048import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070049import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070050import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070051import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Wale Ogunwale494009b82016-10-21 09:01:38 -070052import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070053import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070054import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
55import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070056import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070057import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
58import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070059import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070060import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070061import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070062import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
63import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070064import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Jorim Jaggi75520d52017-08-24 17:23:19 +020065import static com.android.server.wm.AppTransition.TRANSIT_KEYGUARD_UNOCCLUDE;
Wale Ogunwale1666e312016-12-16 11:27:18 -080066import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070067import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070069import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
70import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwalec69694a2016-10-18 13:51:15 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070076import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070077import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070078import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080079import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070080import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070081import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070082import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070083import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070084import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070085import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080086import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -070087import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070088import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
89import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070090import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070091import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -070092import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
93import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070094import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER;
95import static com.android.server.wm.WindowManagerService.TYPE_LAYER_OFFSET;
96import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -070097import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070098import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070099import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700100import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700101import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700102import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700103import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700104import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700105import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700106import static com.android.server.wm.proto.DisplayProto.ABOVE_APP_WINDOWS;
107import static com.android.server.wm.proto.DisplayProto.BELOW_APP_WINDOWS;
108import static com.android.server.wm.proto.DisplayProto.DISPLAY_INFO;
109import static com.android.server.wm.proto.DisplayProto.DOCKED_STACK_DIVIDER_CONTROLLER;
110import static com.android.server.wm.proto.DisplayProto.DPI;
111import static com.android.server.wm.proto.DisplayProto.ID;
112import static com.android.server.wm.proto.DisplayProto.IME_WINDOWS;
113import static com.android.server.wm.proto.DisplayProto.PINNED_STACK_CONTROLLER;
Steven Timotiusf2d68892017-08-28 17:00:01 -0700114import static com.android.server.wm.proto.DisplayProto.ROTATION;
115import static com.android.server.wm.proto.DisplayProto.SCREEN_ROTATION_ANIMATION;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700116import static com.android.server.wm.proto.DisplayProto.STACKS;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700117import static com.android.server.wm.proto.DisplayProto.WINDOW_CONTAINER;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700118
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700119import android.annotation.CallSuper;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700120import android.annotation.NonNull;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700121import android.app.ActivityManager.StackId;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700122import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700123import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700124import android.graphics.Bitmap;
Jorim Jaggi6a7a8592017-01-12 00:44:33 +0100125import android.graphics.GraphicBuffer;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700126import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800127import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700128import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700129import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100130import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700131import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700132import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700133import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700134import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700135import android.os.RemoteException;
136import android.os.SystemClock;
Chong Zhang8e89b312015-09-09 15:09:30 -0700137import android.util.DisplayMetrics;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800138import android.util.MutableBoolean;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700139import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700140import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700141import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700142import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700143import android.view.InputDevice;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700144import android.view.Surface;
145import android.view.SurfaceControl;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700146import android.view.WindowManagerPolicy;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700147
Bryce Lee48f4b572017-04-10 10:54:15 -0700148import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800149import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700150import com.android.internal.view.IInputMethodClient;
Craig Mautner59c00972012-07-30 12:10:24 -0700151
Robert Carr3b716242016-08-16 16:02:21 -0700152import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700153import java.io.PrintWriter;
154import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700155import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700156import java.util.HashMap;
157import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700158import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700159import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800160import java.util.function.Consumer;
161import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700162
Craig Mautner59c00972012-07-30 12:10:24 -0700163/**
164 * Utility class for keeping track of the WindowStates and other pertinent contents of a
165 * particular Display.
166 *
167 * IMPORTANT: No method from this class should ever be used without holding
168 * WindowManagerService.mWindowMap.
169 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700170class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700171 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700172
173 /** Unique identifier of this stack. */
174 private final int mDisplayId;
175
Wale Ogunwale3a931692016-11-02 16:49:48 -0700176 /** The containers below are the only child containers the display can have. */
177 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700178 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700179 // Contains all non-app window containers that should be displayed above the app containers
180 // (e.g. Status bar)
181 private final NonAppWindowContainers mAboveAppWindowsContainers =
182 new NonAppWindowContainers("mAboveAppWindowsContainers");
183 // Contains all non-app window containers that should be displayed below the app containers
184 // (e.g. Wallpaper).
185 private final NonAppWindowContainers mBelowAppWindowsContainers =
186 new NonAppWindowContainers("mBelowAppWindowsContainers");
187 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
188 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
189 // window containers together and move them in-sync if/when needed.
190 private final NonAppWindowContainers mImeWindowsContainers =
191 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700192
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000193 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800194 private WindowState mTmpWindow2;
195 private WindowAnimator mTmpWindowAnimator;
196 private boolean mTmpRecoveringMemory;
197 private boolean mUpdateImeTarget;
198 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700199 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700200
Wale Ogunwale02319a62016-09-26 15:21:22 -0700201 // Mapping from a token IBinder to a WindowToken object on this display.
202 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
203
Andrii Kuliancd097992017-03-23 18:31:59 -0700204 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700205 int mInitialDisplayWidth = 0;
206 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700207 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700208
209 /**
210 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
211 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
212 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
213 */
Craig Mautner59c00972012-07-30 12:10:24 -0700214 int mBaseDisplayWidth = 0;
215 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700216 /**
217 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
218 * but can be set from Settings or via shell command "adb shell wm density".
219 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
220 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700221 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700222 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700223 private final DisplayInfo mDisplayInfo = new DisplayInfo();
224 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700225 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Andrii Kulian06d07d62017-03-14 11:11:47 -0700226 /**
227 * For default display it contains real metrics, empty for others.
228 * @see WindowManagerService#createWatermarkInTransaction()
229 */
230 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
231 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
232 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700233
Andrii Kulian06d07d62017-03-14 11:11:47 -0700234 /**
235 * Compat metrics computed based on {@link #mDisplayMetrics}.
236 * @see #updateDisplayAndOrientation(int)
237 */
238 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
239
240 /** The desired scaling factor for compatible apps. */
241 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700242
Andrii Kulian8ee72852017-03-10 10:36:45 -0800243 /**
244 * Current rotation of the display.
245 * Constants as per {@link android.view.Surface.Rotation}.
246 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700247 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800248 */
249 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700250
Andrii Kulian8ee72852017-03-10 10:36:45 -0800251 /**
252 * Last applied orientation of the display.
253 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
254 *
255 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
256 */
257 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700258
Andrii Kulian8ee72852017-03-10 10:36:45 -0800259 /**
260 * Flag indicating that the application is receiving an orientation that has different metrics
261 * than it expected. E.g. Portrait instead of Landscape.
262 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700263 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800264 */
265 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700266
Andrii Kulian8ee72852017-03-10 10:36:45 -0800267 /**
268 * Orientation forced by some window. If there is no visible window that specifies orientation
269 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
270 *
271 * @see NonAppWindowContainers#getOrientation()
272 */
273 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700274
Andrii Kulian8ee72852017-03-10 10:36:45 -0800275 /**
276 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
277 * occluded.
278 *
279 * @see NonAppWindowContainers#getOrientation()
280 */
281 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
282
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700283 /**
284 * Keep track of wallpaper visibility to notify changes.
285 */
286 private boolean mLastWallpaperVisible = false;
287
Andrii Kulian06d07d62017-03-14 11:11:47 -0700288 private Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700289 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700290
Craig Mautner39834192012-09-02 07:47:24 -0700291 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700292 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700293 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700294 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800295 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700296
Craig Mautnerdc548482014-02-05 13:35:24 -0800297 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700298 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800299
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800300 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
301 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700302 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700303
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700304 /** Detect user tapping outside of current focused task bounds .*/
305 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700306
Craig Mautner6601b7b2013-04-29 10:29:11 -0700307 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700308 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700309
Craig Mautner6601b7b2013-04-29 10:29:11 -0700310 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800311 private final Rect mTmpRect = new Rect();
312 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700313 private final RectF mTmpRectF = new RectF();
314 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800315 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700316
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800317 WindowManagerService mService;
Craig Mautner9d808b12013-08-06 18:00:25 -0700318
Craig Mautner95da1082014-02-24 17:54:35 -0800319 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700320 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800321
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700322 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700323 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700324
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800325 DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700326
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800327 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
328
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000329 private boolean mHaveBootMsg = false;
330 private boolean mHaveApp = false;
331 private boolean mHaveWallpaper = false;
332 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700333
334 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
335
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700336 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
337 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000338 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
339 new ApplySurfaceChangesTransactionState();
340 private final ScreenshotApplicationState mScreenshotApplicationState =
341 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700342
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700343 // True if this display is in the process of being removed. Used to determine if the removal of
344 // the display's direct children should be allowed.
345 private boolean mRemovingDisplay = false;
346
Bryce Leed1871262017-06-12 14:12:29 -0700347 // {@code false} if this display is in the processing of being created.
348 private boolean mDisplayReady = false;
349
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700350 private final WindowLayersController mLayersController;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800351 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700352 int mInputMethodAnimLayerAdjustment;
353
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800354 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
355 WindowStateAnimator winAnimator = w.mWinAnimator;
356 if (winAnimator.hasSurface()) {
357 final boolean wasAnimating = winAnimator.mWasAnimating;
358 final boolean nowAnimating = winAnimator.stepAnimationLocked(
359 mTmpWindowAnimator.mCurrentTime);
360 winAnimator.mWasAnimating = nowAnimating;
361 mTmpWindowAnimator.orAnimating(nowAnimating);
362
363 if (DEBUG_WALLPAPER) Slog.v(TAG,
364 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
365
366 if (wasAnimating && !winAnimator.mAnimating
367 && mWallpaperController.isWallpaperTarget(w)) {
368 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
369 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
370 if (DEBUG_LAYOUT_REPEATS) {
371 mService.mWindowPlacerLocked.debugLayoutRepeats(
372 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
373 }
374 }
375 }
376
377 final AppWindowToken atoken = w.mAppToken;
378 if (winAnimator.mDrawState == READY_TO_SHOW) {
379 if (atoken == null || atoken.allDrawn) {
380 if (w.performShowLocked()) {
381 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
382 if (DEBUG_LAYOUT_REPEATS) {
383 mService.mWindowPlacerLocked.debugLayoutRepeats(
384 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
385 }
386 }
387 }
388 }
389 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
390 if (appAnimator != null && appAnimator.thumbnail != null) {
391 if (appAnimator.thumbnailTransactionSeq
392 != mTmpWindowAnimator.mAnimTransactionSequence) {
393 appAnimator.thumbnailTransactionSeq =
394 mTmpWindowAnimator.mAnimTransactionSequence;
395 appAnimator.thumbnailLayer = 0;
396 }
397 if (appAnimator.thumbnailLayer < winAnimator.mAnimLayer) {
398 appAnimator.thumbnailLayer = winAnimator.mAnimLayer;
399 }
400 }
401 };
402
403 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
404 final WindowStateAnimator winAnimator = w.mWinAnimator;
405 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
406 return;
407 }
408
409 final int flags = w.mAttrs.flags;
410
411 // If this window is animating, make a note that we have an animating window and take
412 // care of a request to run a detached wallpaper animation.
413 if (winAnimator.mAnimating) {
414 if (winAnimator.mAnimation != null) {
415 if ((flags & FLAG_SHOW_WALLPAPER) != 0
416 && winAnimator.mAnimation.getDetachWallpaper()) {
417 mTmpWindow = w;
418 }
419 final int color = winAnimator.mAnimation.getBackgroundColor();
420 if (color != 0) {
421 final TaskStack stack = w.getStack();
422 if (stack != null) {
423 stack.setAnimationBackground(winAnimator, color);
424 }
425 }
426 }
427 mTmpWindowAnimator.setAnimating(true);
428 }
429
430 // If this window's app token is running a detached wallpaper animation, make a note so
431 // we can ensure the wallpaper is displayed behind it.
432 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
433 if (appAnimator != null && appAnimator.animation != null
434 && appAnimator.animating) {
435 if ((flags & FLAG_SHOW_WALLPAPER) != 0
436 && appAnimator.animation.getDetachWallpaper()) {
437 mTmpWindow = w;
438 }
439
440 final int color = appAnimator.animation.getBackgroundColor();
441 if (color != 0) {
442 final TaskStack stack = w.getStack();
443 if (stack != null) {
444 stack.setAnimationBackground(winAnimator, color);
445 }
446 }
447 }
448 };
449
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800450 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
451 final int lostFocusUid = mTmpWindow.mOwnerUid;
452 final Handler handler = mService.mH;
453 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
454 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
455 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
456 w.mAttrs.hideTimeoutMilliseconds);
457 }
458 }
459 };
460
461 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
462 final AppWindowToken focusedApp = mService.mFocusedApp;
463 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
464 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
465
466 if (!w.canReceiveKeys()) {
467 return false;
468 }
469
470 final AppWindowToken wtoken = w.mAppToken;
471
472 // If this window's application has been removed, just skip it.
473 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
474 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
475 + (wtoken.removed ? "removed" : "sendingToBottom"));
476 return false;
477 }
478
479 if (focusedApp == null) {
480 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
481 + " using new focus @ " + w);
482 mTmpWindow = w;
483 return true;
484 }
485
486 if (!focusedApp.windowsAreFocusable()) {
487 // Current focused app windows aren't focusable...
488 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
489 + " focusable using new focus @ " + w);
490 mTmpWindow = w;
491 return true;
492 }
493
494 // Descend through all of the app tokens and find the first that either matches
495 // win.mAppToken (return win) or mFocusedApp (return null).
496 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
497 if (focusedApp.compareTo(wtoken) > 0) {
498 // App stack below focused app stack. No focus for you!!!
499 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
500 "findFocusedWindow: Reached focused app=" + focusedApp);
501 mTmpWindow = null;
502 return true;
503 }
504 }
505
506 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
507 mTmpWindow = w;
508 return true;
509 };
510
511 private final Consumer<WindowState> mPrepareWindowSurfaces =
512 w -> w.mWinAnimator.prepareSurfaceLocked(true);
513
514 private final Consumer<WindowState> mPerformLayout = w -> {
515 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
516 // wasting time and funky changes while a window is animating away.
517 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
518 || w.isGoneForLayoutLw();
519
520 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
521 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
522 + " mLayoutAttached=" + w.mLayoutAttached
523 + " screen changed=" + w.isConfigChanged());
524 final AppWindowToken atoken = w.mAppToken;
525 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
526 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
527 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
528 + " parentHidden=" + w.isParentWindowHidden());
529 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
530 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
531 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
532 + " parentHidden=" + w.isParentWindowHidden());
533 }
534
535 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
536 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
537 // since that means "perform layout as normal, just don't display").
538 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
539 || ((w.isConfigChanged() || w.setReportResizeHints())
540 && !w.isGoneForLayoutLw() &&
541 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
542 (w.mHasSurface && w.mAppToken != null &&
543 w.mAppToken.layoutConfigChanges)))) {
544 if (!w.mLayoutAttached) {
545 if (mTmpInitial) {
546 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
547 w.mContentChanged = false;
548 }
549 if (w.mAttrs.type == TYPE_DREAM) {
550 // Don't layout windows behind a dream, so that if it does stuff like hide
551 // the status bar we won't get a bad transition when it goes away.
552 mTmpWindow = w;
553 }
554 w.mLayoutNeeded = false;
555 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200556 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800557 mService.mPolicy.layoutWindowLw(w, null);
558 w.mLayoutSeq = mService.mLayoutSeq;
559
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200560 // If this is the first layout, we need to initialize the last inset values as
561 // otherwise we'd immediately cause an unnecessary resize.
562 if (firstLayout) {
563 w.updateLastInsetValues();
564 }
565
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800566 // Window frames may have changed. Update dim layer with the new bounds.
567 final Task task = w.getTask();
568 if (task != null) {
569 mDimLayerController.updateDimLayer(task);
570 }
571
572 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
573 + " mContainingFrame=" + w.mContainingFrame
574 + " mDisplayFrame=" + w.mDisplayFrame);
575 }
576 }
577 };
578
579 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
580 if (w.mLayoutAttached) {
581 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
582 + " mViewVisibility=" + w.mViewVisibility
583 + " mRelayoutCalled=" + w.mRelayoutCalled);
584 // If this view is GONE, then skip it -- keep the current frame, and let the caller
585 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
586 // windows, since that means "perform layout as normal, just don't display").
587 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
588 return;
589 }
590 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
591 || w.mLayoutNeeded) {
592 if (mTmpInitial) {
593 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
594 w.mContentChanged = false;
595 }
596 w.mLayoutNeeded = false;
597 w.prelayout();
598 mService.mPolicy.layoutWindowLw(w, w.getParentWindow());
599 w.mLayoutSeq = mService.mLayoutSeq;
600 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
601 + " mContainingFrame=" + w.mContainingFrame
602 + " mDisplayFrame=" + w.mDisplayFrame);
603 }
604 } else if (w.mAttrs.type == TYPE_DREAM) {
605 // Don't layout windows behind a dream, so that if it does stuff like hide the
606 // status bar we won't get a bad transition when it goes away.
607 mTmpWindow = mTmpWindow2;
608 }
609 };
610
611 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
612 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
613 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
614 return w.canBeImeTarget();
615 };
616
617 private final Consumer<WindowState> mApplyPostLayoutPolicy =
618 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
619 mService.mInputMethodTarget);
620
621 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
622 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
623 final boolean obscuredChanged = w.mObscured !=
624 mTmpApplySurfaceChangesTransactionState.obscured;
625 final RootWindowContainer root = mService.mRoot;
626 // Only used if default window
627 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
628
629 // Update effect.
630 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
631 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
632 final boolean isDisplayed = w.isDisplayedLw();
633
634 if (isDisplayed && w.isObscuringDisplay()) {
635 // This window completely covers everything behind it, so we want to leave all
636 // of them as undimmed (for performance reasons).
637 root.mObscuringWindow = w;
638 mTmpApplySurfaceChangesTransactionState.obscured = true;
639 }
640
641 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
642 root.handleNotObscuredLocked(w,
643 mTmpApplySurfaceChangesTransactionState.obscured,
644 mTmpApplySurfaceChangesTransactionState.syswin);
645
646 if (w.mHasSurface && isDisplayed) {
647 final int type = w.mAttrs.type;
648 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
649 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
650 mTmpApplySurfaceChangesTransactionState.syswin = true;
651 }
652 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
653 && w.mAttrs.preferredRefreshRate != 0) {
654 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
655 = w.mAttrs.preferredRefreshRate;
656 }
657 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
658 && w.mAttrs.preferredDisplayModeId != 0) {
659 mTmpApplySurfaceChangesTransactionState.preferredModeId
660 = w.mAttrs.preferredDisplayModeId;
661 }
662 }
663 }
664
665 w.applyDimLayerIfNeeded();
666
667 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
668 && mWallpaperController.isWallpaperTarget(w)) {
669 // This is the wallpaper target and its obscured state changed... make sure the
670 // current wallpaper's visibility has been updated accordingly.
671 mWallpaperController.updateWallpaperVisibility();
672 }
673
674 w.handleWindowMovedIfNeeded();
675
676 final WindowStateAnimator winAnimator = w.mWinAnimator;
677
678 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
679 w.mContentChanged = false;
680
681 // Moved from updateWindowsAndWallpaperLocked().
682 if (w.mHasSurface) {
683 // Take care of the window being ready to display.
684 final boolean committed = winAnimator.commitFinishDrawingLocked();
685 if (isDefaultDisplay && committed) {
686 if (w.mAttrs.type == TYPE_DREAM) {
687 // HACK: When a dream is shown, it may at that point hide the lock screen.
688 // So we need to redo the layout to let the phone window manager make this
689 // happen.
690 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
691 if (DEBUG_LAYOUT_REPEATS) {
692 surfacePlacer.debugLayoutRepeats(
693 "dream and commitFinishDrawingLocked true",
694 pendingLayoutChanges);
695 }
696 }
697 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
698 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
699 "First draw done in potential wallpaper target " + w);
700 root.mWallpaperMayChange = true;
701 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
702 if (DEBUG_LAYOUT_REPEATS) {
703 surfacePlacer.debugLayoutRepeats(
704 "wallpaper and commitFinishDrawingLocked true",
705 pendingLayoutChanges);
706 }
707 }
708 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700709 final TaskStack stack = w.getStack();
710 if ((!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening())
711 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800712 // Updates the shown frame before we set up the surface. This is needed
713 // because the resizing could change the top-left position (in addition to
714 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
715 // position the surface.
716 //
717 // If an animation is being started, we can't call this method because the
718 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700719 // we do want to update the position if the window is animating. We make an exception
720 // for the bounds animating state, where an application may have been waiting
721 // for an exit animation to start, but instead enters PiP. We need to ensure
722 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800723 winAnimator.computeShownFrameLocked();
724 }
725 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
726 }
727
728 final AppWindowToken atoken = w.mAppToken;
729 if (atoken != null) {
730 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
731 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
732 mTmpUpdateAllDrawn.add(atoken);
733 }
734 }
735
736 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
737 && w.isDisplayedLw()) {
738 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
739 }
740
741 w.updateResizingWindowIfNeeded();
742 };
743
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800744 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800745 * Create new {@link DisplayContent} instance, add itself to the root window container and
746 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700747 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800748 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700749 * @param layersController window layer controller used to assign layer to the windows on this
750 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700751 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
752 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700753 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700754 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700755 WindowLayersController layersController, WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800756 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
757 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
758 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
759 + " new=" + display);
760 }
761
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700762 mDisplay = display;
763 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700764 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700765 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700766 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700767 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700768 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700769 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700770 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800771 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700772 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800773 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700774
775 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700776 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700777 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700778 super.addChild(mAboveAppWindowsContainers, null);
779 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800780
781 // Add itself as a child to the root container.
782 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700783
784 // TODO(b/62541591): evaluate whether this is the best spot to declare the
785 // {@link DisplayContent} ready for use.
786 mDisplayReady = true;
787 }
788
789 boolean isReady() {
790 // The display is ready when the system and the individual display are both ready.
791 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700792 }
793
794 int getDisplayId() {
795 return mDisplayId;
796 }
797
Wale Ogunwale02319a62016-09-26 15:21:22 -0700798 WindowToken getWindowToken(IBinder binder) {
799 return mTokenMap.get(binder);
800 }
801
802 AppWindowToken getAppWindowToken(IBinder binder) {
803 final WindowToken token = getWindowToken(binder);
804 if (token == null) {
805 return null;
806 }
807 return token.asAppWindowToken();
808 }
809
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700810 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700811 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
812 if (dc != null) {
813 // We currently don't support adding a window token to the display if the display
814 // already has the binder mapped to another token. If there is a use case for supporting
815 // this moving forward we will either need to merge the WindowTokens some how or have
816 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700817 throw new IllegalArgumentException("Can't map token=" + token + " to display="
818 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700819 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700820 if (binder == null) {
821 throw new IllegalArgumentException("Can't map token=" + token + " to display="
822 + getName() + " binder is null");
823 }
824 if (token == null) {
825 throw new IllegalArgumentException("Can't map null token to display="
826 + getName() + " binder=" + binder);
827 }
828
Wale Ogunwale02319a62016-09-26 15:21:22 -0700829 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700830
831 if (token.asAppWindowToken() == null) {
832 // Add non-app token to container hierarchy on the display. App tokens are added through
833 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700834 switch (token.windowType) {
835 case TYPE_WALLPAPER:
836 mBelowAppWindowsContainers.addChild(token);
837 break;
838 case TYPE_INPUT_METHOD:
839 case TYPE_INPUT_METHOD_DIALOG:
840 mImeWindowsContainers.addChild(token);
841 break;
842 default:
843 mAboveAppWindowsContainers.addChild(token);
844 break;
845 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700846 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700847 }
848
849 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700850 final WindowToken token = mTokenMap.remove(binder);
851 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800852 token.setExiting();
853 }
854 return token;
855 }
856
857 /** Changes the display the input window token is housed on to this one. */
858 void reParentWindowToken(WindowToken token) {
859 final DisplayContent prevDc = token.getDisplayContent();
860 if (prevDc == this) {
861 return;
862 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800863 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
864 && token.asAppWindowToken() == null) {
865 // Removed the token from the map, but made sure it's not an app token before removing
866 // from parent.
867 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700868 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800869
870 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700871 }
872
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700873 void removeAppToken(IBinder binder) {
874 final WindowToken token = removeWindowToken(binder);
875 if (token == null) {
876 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
877 return;
878 }
879
880 final AppWindowToken appToken = token.asAppWindowToken();
881
882 if (appToken == null) {
883 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
884 return;
885 }
886
887 appToken.onRemovedFromDisplay();
888 }
889
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700890 Display getDisplay() {
891 return mDisplay;
892 }
893
Craig Mautner59c00972012-07-30 12:10:24 -0700894 DisplayInfo getDisplayInfo() {
895 return mDisplayInfo;
896 }
897
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700898 DisplayMetrics getDisplayMetrics() {
899 return mDisplayMetrics;
900 }
901
Andrii Kulian8ee72852017-03-10 10:36:45 -0800902 int getRotation() {
903 return mRotation;
904 }
905
906 void setRotation(int newRotation) {
907 mRotation = newRotation;
908 }
909
910 int getLastOrientation() {
911 return mLastOrientation;
912 }
913
914 void setLastOrientation(int orientation) {
915 mLastOrientation = orientation;
916 }
917
918 boolean getAltOrientation() {
919 return mAltOrientation;
920 }
921
922 void setAltOrientation(boolean altOrientation) {
923 mAltOrientation = altOrientation;
924 }
925
926 int getLastWindowForcedOrientation() {
927 return mLastWindowForcedOrientation;
928 }
929
Andrii Kulian06d07d62017-03-14 11:11:47 -0700930 /**
931 * Update rotation of the display.
932 *
933 * Returns true if the rotation has been changed. In this case YOU MUST CALL
934 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
935 */
936 boolean updateRotationUnchecked(boolean inTransaction) {
937 if (mService.mDeferredRotationPauseCount > 0) {
938 // Rotation updates have been paused temporarily. Defer the update until
939 // updates have been resumed.
940 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
941 return false;
942 }
943
944 ScreenRotationAnimation screenRotationAnimation =
945 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
946 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
947 // Rotation updates cannot be performed while the previous rotation change
948 // animation is still in progress. Skip this update. We will try updating
949 // again after the animation is finished and the display is unfrozen.
950 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
951 return false;
952 }
953 if (mService.mDisplayFrozen) {
954 // Even if the screen rotation animation has finished (e.g. isAnimating
955 // returns false), there is still some time where we haven't yet unfrozen
956 // the display. We also need to abort rotation here.
957 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
958 "Deferring rotation, still finishing previous rotation");
959 return false;
960 }
961
962 if (!mService.mDisplayEnabled) {
963 // No point choosing a rotation if the display is not enabled.
964 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
965 return false;
966 }
967
968 final int oldRotation = mRotation;
969 final int lastOrientation = mLastOrientation;
970 final boolean oldAltOrientation = mAltOrientation;
971 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr897215d2017-03-29 12:25:50 -0700972 final boolean rotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
973 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700974
Robert Carr897215d2017-03-29 12:25:50 -0700975 if (rotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700976 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
977 if (seamlessRotated != null) {
978 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
979 // to complete (that is, waiting for windows to redraw). It's tempting to check
980 // w.mSeamlessRotationCount but that could be incorrect in the case of
981 // window-removal.
982 return false;
983 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700984 }
985
986 // TODO: Implement forced rotation changes.
987 // Set mAltOrientation to indicate that the application is receiving
988 // an orientation that has different metrics than it expected.
989 // eg. Portrait instead of Landscape.
990
991 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
992 lastOrientation, rotation);
993
994 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
995 + ", got rotation " + rotation + " which has "
996 + (altOrientation ? "incompatible" : "compatible") + " metrics");
997
998 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
999 // No change.
1000 return false;
1001 }
1002
1003 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
1004 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1005 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1006
1007 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1008 mService.mWaitingForConfig = true;
1009 }
1010
1011 mRotation = rotation;
1012 mAltOrientation = altOrientation;
1013 if (isDefaultDisplay) {
1014 mService.mPolicy.setRotationLw(rotation);
1015 }
1016
1017 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1018 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1019 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1020 WINDOW_FREEZE_TIMEOUT_DURATION);
1021
1022 setLayoutNeeded();
1023 final int[] anim = new int[2];
1024 if (isDimming()) {
1025 anim[0] = anim[1] = 0;
1026 } else {
1027 mService.mPolicy.selectRotationAnimationLw(anim);
1028 }
1029
1030 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001031 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001032 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1033 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1034 mDisplayId);
1035 } else {
1036 // The screen rotation animation uses a screenshot to freeze the screen
1037 // while windows resize underneath.
1038 // When we are rotating seamlessly, we allow the elements to transition
1039 // to their rotated state independently and without a freeze required.
1040 screenRotationAnimation = null;
1041
1042 // We have to reset this in case a window was removed before it
1043 // finished seamless rotation.
1044 mService.mSeamlessRotationCount = 0;
1045 }
1046
1047 // We need to update our screen size information to match the new rotation. If the rotation
1048 // has actually changed then this method will return true and, according to the comment at
1049 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1050 // By updating the Display info here it will be available to
1051 // #computeScreenConfiguration() later.
1052 updateDisplayAndOrientation(getConfiguration().uiMode);
1053
1054 if (!inTransaction) {
1055 if (SHOW_TRANSACTIONS) {
1056 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1057 }
1058 mService.openSurfaceTransaction();
1059 }
1060 try {
1061 // NOTE: We disable the rotation in the emulator because
1062 // it doesn't support hardware OpenGL emulation yet.
1063 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1064 && screenRotationAnimation.hasScreenshot()) {
1065 if (screenRotationAnimation.setRotationInTransaction(
1066 rotation, mService.mFxSession,
1067 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1068 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1069 mService.scheduleAnimationLocked();
1070 }
1071 }
1072
1073 if (rotateSeamlessly) {
1074 forAllWindows(w -> {
1075 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1076 }, true /* traverseTopToBottom */);
1077 }
1078
1079 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1080 } finally {
1081 if (!inTransaction) {
1082 mService.closeSurfaceTransaction();
1083 if (SHOW_LIGHT_TRANSACTIONS) {
1084 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1085 }
1086 }
1087 }
1088
1089 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001090 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
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001461 /**
1462 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1463 * Null is no compatible stack on the display.
1464 */
1465 TaskStack getStack(int windowingMode) {
1466 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1467 }
1468
1469 /**
1470 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1471 * activity type. Null is no compatible stack on the display.
1472 */
1473 private TaskStack getStack(int windowingMode, int activityType) {
1474 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1475 final TaskStack stack = mTaskStackContainers.get(i);
1476 if (stack.isCompatible(windowingMode, activityType)) {
1477 return stack;
1478 }
1479 }
1480 return null;
1481 }
1482
Bryce Lee48f4b572017-04-10 10:54:15 -07001483 @VisibleForTesting
1484 int getStackCount() {
1485 return mTaskStackContainers.size();
1486 }
1487
1488 @VisibleForTesting
Wale Ogunwale98d62312017-07-12 09:24:56 -07001489 int getStackPosById(int stackId) {
Bryce Lee48f4b572017-04-10 10:54:15 -07001490 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1491 final TaskStack stack = mTaskStackContainers.get(i);
1492 if (stack.mStackId == stackId) {
1493 return i;
1494 }
1495 }
1496 return -1;
1497 }
1498
Andrii Kulian441e4492016-09-29 15:25:00 -07001499 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001500 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001501 super.onConfigurationChanged(newParentConfig);
1502
Andrii Kulian3a507b52016-09-19 18:14:12 -07001503 // The display size information is heavily dependent on the resources in the current
1504 // configuration, so we need to reconfigure it every time the configuration changes.
1505 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1506 mService.reconfigureDisplayLocked(this);
1507
1508 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -07001509 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -07001510 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001511
Andrii Kulian441e4492016-09-29 15:25:00 -07001512 /**
1513 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1514 * bounds were updated.
1515 */
1516 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001517 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1518 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001519 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001520 changedStackList.add(stack.mStackId);
1521 }
1522 }
Winson Chung32c566f2017-04-11 18:31:21 -07001523
1524 // If there was no pinned stack, we still need to notify the controller of the display info
1525 // update as a result of the config change. We do this here to consolidate the flow between
1526 // changes when there is and is not a stack.
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001527 if (getStack(WINDOWING_MODE_PINNED) == null) {
Winson Chung32c566f2017-04-11 18:31:21 -07001528 mPinnedStackControllerLocked.onDisplayInfoChanged();
1529 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001530 }
1531
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001532 @Override
1533 boolean fillsParent() {
1534 return true;
1535 }
1536
1537 @Override
1538 boolean isVisible() {
1539 return true;
1540 }
1541
1542 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001543 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001544 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001545 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001546 }
1547
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001548 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001549 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1550 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1551 // target, or here in order if there isn't an IME target.
1552 if (traverseTopToBottom) {
1553 for (int i = mChildren.size() - 1; i >= 0; --i) {
1554 final DisplayChildWindowContainer child = mChildren.get(i);
1555 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1556 // In this case the Ime windows will be processed above their target so we skip
1557 // here.
1558 continue;
1559 }
1560 if (child.forAllWindows(callback, traverseTopToBottom)) {
1561 return true;
1562 }
1563 }
1564 } else {
1565 final int count = mChildren.size();
1566 for (int i = 0; i < count; i++) {
1567 final DisplayChildWindowContainer child = mChildren.get(i);
1568 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1569 // In this case the Ime windows will be processed above their target so we skip
1570 // here.
1571 continue;
1572 }
1573 if (child.forAllWindows(callback, traverseTopToBottom)) {
1574 return true;
1575 }
1576 }
1577 }
1578 return false;
1579 }
1580
1581 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1582 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1583 }
1584
Wale Ogunwale399c8692017-05-08 14:22:42 -07001585 @Override
1586 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001587 final WindowManagerPolicy policy = mService.mPolicy;
1588
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001589 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001590 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001591 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001592 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001593 // If the display is frozen, some activities may be in the middle of restarting, and
1594 // thus have removed their old window. If the window has the flag to hide the lock
1595 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1596 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001597 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001598 } else if (policy.isKeyguardLocked()) {
1599 // Use the last orientation the while the display is frozen with the keyguard
1600 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1601 // window. We don't want to check the show when locked window directly though as
1602 // things aren't stable while the display is frozen, for example the window could be
1603 // momentarily unavailable due to activity relaunch.
1604 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001605 + "return " + mLastOrientation);
1606 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001607 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001608 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001609 final int orientation = mAboveAppWindowsContainers.getOrientation();
1610 if (orientation != SCREEN_ORIENTATION_UNSET) {
1611 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001612 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001613 }
1614
Wale Ogunwale399c8692017-05-08 14:22:42 -07001615 // Top system windows are not requesting an orientation. Start searching from apps.
1616 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001617 }
1618
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001619 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001620 // Check if display metrics changed and update base values if needed.
1621 updateBaseDisplayMetricsIfNeeded();
1622
Craig Mautner722285e2012-09-07 13:55:58 -07001623 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001624 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001625
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001626 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1627 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001628 }
Craig Mautner722285e2012-09-07 13:55:58 -07001629 }
1630
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001631 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001632 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1633 if (displayManagerInternal != null) {
1634 // Bootstrap the default logical display from the display manager.
1635 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1636 if (newDisplayInfo != null) {
1637 mDisplayInfo.copyFrom(newDisplayInfo);
1638 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001639 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001640
Andrii Kuliancd097992017-03-23 18:31:59 -07001641 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1642 mDisplayInfo.logicalDensityDpi);
1643 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1644 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1645 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001646 }
1647
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001648 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001649 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -08001650 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001651 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -08001652 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1653 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001654 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001655 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001656 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -08001657 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001658 out.set(left, top, left + width, top + height);
1659 }
1660
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001661 private void getLogicalDisplayRect(Rect out, int orientation) {
1662 getLogicalDisplayRect(out);
1663
1664 // Rotate the Rect if needed.
1665 final int currentRotation = mDisplayInfo.rotation;
1666 final int rotationDelta = deltaRotation(currentRotation, orientation);
1667 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
1668 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
1669 mTmpRectF.set(out);
1670 mTmpMatrix.mapRect(mTmpRectF);
1671 mTmpRectF.round(out);
1672 }
1673 }
1674
Andrii Kuliancd097992017-03-23 18:31:59 -07001675 /**
1676 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1677 * values.
1678 */
1679 private void updateBaseDisplayMetricsIfNeeded() {
1680 // Get real display metrics without overrides from WM.
1681 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1682 final int orientation = mDisplayInfo.rotation;
1683 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1684 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1685 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1686 final int newDensity = mDisplayInfo.logicalDensityDpi;
1687
1688 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1689 || mInitialDisplayHeight != newHeight
1690 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1691
1692 if (displayMetricsChanged) {
1693 // Check if display size or density is forced.
1694 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1695 || mBaseDisplayHeight != mInitialDisplayHeight;
1696 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1697
1698 // If there is an override set for base values - use it, otherwise use new values.
1699 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1700 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1701 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1702
1703 // Real display metrics changed, so we should also update initial values.
1704 mInitialDisplayWidth = newWidth;
1705 mInitialDisplayHeight = newHeight;
1706 mInitialDisplayDensity = newDensity;
1707 mService.reconfigureDisplayLocked(this);
1708 }
1709 }
1710
Bryce Lee27cec322017-03-21 09:41:37 -07001711 /** Sets the maximum width the screen resolution can be */
1712 void setMaxUiWidth(int width) {
1713 if (DEBUG_DISPLAY) {
1714 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1715 }
1716
1717 mMaxUiWidth = width;
1718
1719 // Update existing metrics.
1720 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1721 }
1722
1723 /** Update base (override) display metrics. */
1724 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1725 mBaseDisplayWidth = baseWidth;
1726 mBaseDisplayHeight = baseHeight;
1727 mBaseDisplayDensity = baseDensity;
1728
1729 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1730 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1731 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1732 mBaseDisplayWidth = mMaxUiWidth;
1733
1734 if (DEBUG_DISPLAY) {
1735 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1736 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1737 + " on display:" + getDisplayId());
1738 }
1739 }
1740
1741 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
1742 }
1743
Chong Zhangf66db432016-01-13 10:39:51 -08001744 void getContentRect(Rect out) {
1745 out.set(mContentRect);
1746 }
1747
Wale Ogunwale704a3c02017-09-18 15:30:52 -07001748 TaskStack addStackToDisplay(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001749 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1750 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001751
Wale Ogunwale1666e312016-12-16 11:27:18 -08001752 TaskStack stack = getStackById(stackId);
1753 if (stack != null) {
Wale Ogunwale704a3c02017-09-18 15:30:52 -07001754 // It's already attached to the display...clear mDeferRemoval, set controller, and move
1755 // stack to appropriate z-order on display as needed.
Wale Ogunwale1666e312016-12-16 11:27:18 -08001756 stack.mDeferRemoval = false;
Wale Ogunwale704a3c02017-09-18 15:30:52 -07001757 stack.setController(controller);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001758 // We're not moving the display to front when we're adding stacks, only when
1759 // requested to change the position of stack explicitly.
1760 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
1761 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -07001762 } else {
Wale Ogunwale704a3c02017-09-18 15:30:52 -07001763 stack = new TaskStack(mService, stackId, controller);
Andrii Kulian839def92016-11-02 10:58:58 -07001764 mTaskStackContainers.addStackToDisplay(stack, onTop);
1765 }
1766
Wale Ogunwale1666e312016-12-16 11:27:18 -08001767 if (stackId == DOCKED_STACK_ID) {
1768 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -07001769 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001770 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001771 }
1772
Andrii Kulian51c1b672017-04-07 18:39:32 -07001773 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001774 final DisplayContent prevDc = stack.getDisplayContent();
1775 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001776 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1777 + " which is not currently attached to any display");
1778 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001779 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001780 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1781 + " to its current displayId=" + mDisplayId);
1782 }
1783
Wale Ogunwale1666e312016-12-16 11:27:18 -08001784 prevDc.mTaskStackContainers.removeStackFromDisplay(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001785 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001786 }
1787
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001788 @Override
1789 protected void addChild(DisplayChildWindowContainer child,
1790 Comparator<DisplayChildWindowContainer> comparator) {
1791 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1792 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001793
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001794 @Override
1795 protected void addChild(DisplayChildWindowContainer child, int index) {
1796 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1797 }
1798
1799 @Override
1800 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001801 // Only allow removal of direct children from this display if the display is in the process
1802 // of been removed.
1803 if (mRemovingDisplay) {
1804 super.removeChild(child);
1805 return;
1806 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001807 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001808 }
1809
Andrii Kuliand2765632016-12-12 22:26:34 -08001810 @Override
1811 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1812 // Children of the display are statically ordered, so the real intention here is to perform
1813 // the operation on the display and not the static direct children.
1814 getParent().positionChildAt(position, this, includingParents);
1815 }
1816
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001817 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001818 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1819 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001820 final int taskId = stack.taskIdFromPoint(x, y);
1821 if (taskId != -1) {
1822 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001823 }
1824 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001825 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001826 }
1827
Chong Zhang8e89b312015-09-09 15:09:30 -07001828 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001829 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001830 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001831 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001832 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001833 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001834 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001835 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1836 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001837 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001838 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001839 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001840
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001841 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1842 if (mTmpTaskForResizePointSearchResult.searchDone) {
1843 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001844 }
1845 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001846 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001847 }
1848
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001849 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001850 // The provided task is the task on this display with focus, so if WindowManagerService's
1851 // focused app is not on this display, focusedTask will be null.
1852 if (focusedTask == null) {
1853 mTouchExcludeRegion.setEmpty();
1854 } else {
1855 mTouchExcludeRegion.set(mBaseDisplayRect);
1856 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1857 mTmpRect2.setEmpty();
1858 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1859 final TaskStack stack = mTaskStackContainers.get(stackNdx);
1860 stack.setTouchExcludeRegion(
1861 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
1862 }
1863 // If we removed the focused task above, add it back and only leave its
1864 // outside touch area in the exclusion. TapDectector is not interested in
1865 // any touch inside the focused task itself.
1866 if (!mTmpRect2.isEmpty()) {
1867 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1868 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001869 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001870 final WindowState inputMethod = mService.mInputMethodWindow;
1871 if (inputMethod != null && inputMethod.isVisibleLw()) {
1872 // If the input method is visible and the user is typing, we don't want these touch
1873 // events to be intercepted and used to change focus. This would likely cause a
1874 // disappearance of the input method.
1875 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001876 if (inputMethod.getDisplayId() == mDisplayId) {
1877 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1878 } else {
1879 // IME is on a different display, so we need to update its tap detector.
1880 // TODO(multidisplay): Remove when IME will always appear on same display.
1881 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1882 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001883 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001884 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1885 WindowState win = mTapExcludedWindows.get(i);
1886 win.getTouchableRegion(mTmpRegion);
1887 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1888 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001889 // TODO(multi-display): Support docked stacks on secondary displays.
1890 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001891 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001892 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001893 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1894 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001895 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001896 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001897 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001898 }
1899
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001900 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001901 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001902 super.switchUser();
1903 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001904 }
1905
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001906 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001907 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1908 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001909 }
1910 }
1911
1912 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001913 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001914 }
1915
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001916 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001917 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001918 }
1919
1920 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001921 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001922 }
1923
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001924 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001925 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001926 }
1927
Wale Ogunwale10124582016-09-15 20:25:50 -07001928 @Override
1929 void removeIfPossible() {
1930 if (isAnimating()) {
1931 mDeferredRemoval = true;
1932 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001933 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001934 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001935 }
1936
Wale Ogunwale10124582016-09-15 20:25:50 -07001937 @Override
1938 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001939 mRemovingDisplay = true;
1940 try {
1941 super.removeImmediately();
1942 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1943 mDimLayerController.close();
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07001944 if (mService.canDispatchPointerEvents()) {
1945 if (mTapDetector != null) {
1946 mService.unregisterPointerEventListener(mTapDetector);
1947 }
1948 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
1949 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1950 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001951 }
1952 } finally {
1953 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001954 }
Craig Mautner95da1082014-02-24 17:54:35 -08001955 }
1956
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001957 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001958 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001959 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001960 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1961
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001962 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001963 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001964 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001965 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001966 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001967 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001968 }
1969
Andrii Kulian0214ed92017-05-16 13:44:05 -07001970 /** @return 'true' if removal of this display content is deferred due to active animation. */
1971 boolean isRemovalDeferred() {
1972 return mDeferredRemoval;
1973 }
1974
Wale Ogunwale10124582016-09-15 20:25:50 -07001975 boolean animateForIme(float interpolatedValue, float animationTarget,
1976 float dividerAnimationTarget) {
1977 boolean updated = false;
1978
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001979 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1980 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001981 if (stack == null || !stack.isAdjustedForIme()) {
1982 continue;
1983 }
1984
1985 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1986 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1987 updated = true;
1988 } else {
1989 mDividerControllerLocked.mLastAnimationProgress =
1990 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1991 mDividerControllerLocked.mLastDividerProgress =
1992 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1993 updated |= stack.updateAdjustForIme(
1994 mDividerControllerLocked.mLastAnimationProgress,
1995 mDividerControllerLocked.mLastDividerProgress,
1996 false /* force */);
1997 }
1998 if (interpolatedValue >= 1f) {
1999 stack.endImeAdjustAnimation();
2000 }
2001 }
2002
2003 return updated;
2004 }
2005
2006 boolean clearImeAdjustAnimation() {
2007 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002008 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2009 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002010 if (stack != null && stack.isAdjustedForIme()) {
2011 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2012 changed = true;
2013 }
2014 }
2015 return changed;
2016 }
2017
2018 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002019 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2020 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002021 if (stack.isVisible() && stack.isAdjustedForIme()) {
2022 stack.beginImeAdjustAnimation();
2023 }
2024 }
2025 }
2026
2027 void adjustForImeIfNeeded() {
2028 final WindowState imeWin = mService.mInputMethodWindow;
2029 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2030 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale1666e312016-12-16 11:27:18 -08002031 final boolean dockVisible = isStackVisible(DOCKED_STACK_ID);
Wale Ogunwale10124582016-09-15 20:25:50 -07002032 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2033 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2034 imeTargetStack.getDockSide() : DOCKED_INVALID;
2035 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2036 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2037 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
2038 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
2039 final boolean imeHeightChanged = imeVisible &&
2040 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2041
2042 // The divider could be adjusted for IME position, or be thinner than usual,
2043 // or both. There are three possible cases:
2044 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2045 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2046 // - If IME is not visible, divider is not moved and is normal width.
2047
2048 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002049 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2050 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002051 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Winson Chungfef058d2017-04-25 14:43:10 -07002052 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom) &&
2053 StackId.isStackAffectedByDragResizing(stack.mStackId)) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002054 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2055 } else {
2056 stack.resetAdjustedForIme(false);
2057 }
2058 }
2059 mDividerControllerLocked.setAdjustedForIme(
2060 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2061 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002062 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
2063 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002064 stack.resetAdjustedForIme(!dockVisible);
2065 }
2066 mDividerControllerLocked.setAdjustedForIme(
2067 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2068 }
Winson Chung655332c2016-10-31 13:14:28 -07002069 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002070 }
2071
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002072 void setInputMethodAnimLayerAdjustment(int adj) {
2073 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
2074 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08002075 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002076 }
2077
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002078 /**
2079 * If a window that has an animation specifying a colored background and the current wallpaper
2080 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2081 * suddenly disappear.
2082 */
2083 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002084 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2085 w -> w.mIsWallpaper && w.isVisibleNow());
2086
2087 if (visibleWallpaper != null) {
2088 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002089 }
2090 return winAnimator.mAnimLayer;
2091 }
2092
Wale Ogunwale10124582016-09-15 20:25:50 -07002093 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002094 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2095 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002096 stack.prepareFreezingTaskBounds();
2097 }
2098 }
2099
Wale Ogunwale94744212015-09-21 19:01:47 -07002100 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002101 getLogicalDisplayRect(mTmpRect, newRotation);
2102
2103 // Compute a transform matrix to undo the coordinate space transformation,
2104 // and present the window at the same physical position it previously occupied.
2105 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2106 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2107
2108 mTmpRectF.set(bounds);
2109 mTmpMatrix.mapRect(mTmpRectF);
2110 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002111 }
2112
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002113 static int deltaRotation(int oldRotation, int newRotation) {
2114 int delta = newRotation - oldRotation;
2115 if (delta < 0) delta += 4;
2116 return delta;
2117 }
2118
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002119 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002120 Matrix outMatrix) {
2121 // For rotations without Z-ordering we don't need the target rectangle's position.
2122 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2123 displayHeight, outMatrix);
2124 }
2125
2126 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2127 float displayWidth, float displayHeight, Matrix outMatrix) {
2128 switch (rotation) {
2129 case ROTATION_0:
2130 outMatrix.reset();
2131 break;
2132 case ROTATION_270:
2133 outMatrix.setRotate(270, 0, 0);
2134 outMatrix.postTranslate(0, displayHeight);
2135 outMatrix.postTranslate(rectTop, 0);
2136 break;
2137 case ROTATION_180:
2138 outMatrix.reset();
2139 break;
2140 case ROTATION_90:
2141 outMatrix.setRotate(90, 0, 0);
2142 outMatrix.postTranslate(displayWidth, 0);
2143 outMatrix.postTranslate(-rectTop, rectLeft);
2144 break;
2145 }
2146 }
2147
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002148 @CallSuper
2149 @Override
2150 public void writeToProto(ProtoOutputStream proto, long fieldId) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002151 final long token = proto.start(fieldId);
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002152 super.writeToProto(proto, WINDOW_CONTAINER);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002153 proto.write(ID, mDisplayId);
2154 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2155 final TaskStack stack = mTaskStackContainers.get(stackNdx);
2156 stack.writeToProto(proto, STACKS);
2157 }
2158 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2159 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
2160 for (int i = mAboveAppWindowsContainers.size() - 1; i >= 0; --i) {
2161 final WindowToken windowToken = mAboveAppWindowsContainers.get(i);
2162 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS);
2163 }
2164 for (int i = mBelowAppWindowsContainers.size() - 1; i >= 0; --i) {
2165 final WindowToken windowToken = mBelowAppWindowsContainers.get(i);
2166 windowToken.writeToProto(proto, BELOW_APP_WINDOWS);
2167 }
2168 for (int i = mImeWindowsContainers.size() - 1; i >= 0; --i) {
2169 final WindowToken windowToken = mImeWindowsContainers.get(i);
2170 windowToken.writeToProto(proto, IME_WINDOWS);
2171 }
2172 proto.write(DPI, mBaseDisplayDensity);
2173 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002174 proto.write(ROTATION, mRotation);
2175 final ScreenRotationAnimation screenRotationAnimation =
2176 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2177 if (screenRotationAnimation != null) {
2178 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2179 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002180 proto.end(token);
2181 }
2182
Craig Mautnera91f9e22012-09-14 16:22:08 -07002183 public void dump(String prefix, PrintWriter pw) {
2184 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2185 final String subPrefix = " " + prefix;
2186 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2187 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2188 pw.print("dpi");
2189 if (mInitialDisplayWidth != mBaseDisplayWidth
2190 || mInitialDisplayHeight != mBaseDisplayHeight
2191 || mInitialDisplayDensity != mBaseDisplayDensity) {
2192 pw.print(" base=");
2193 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2194 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2195 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002196 if (mDisplayScalingDisabled) {
2197 pw.println(" noscale");
2198 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002199 pw.print(" cur=");
2200 pw.print(mDisplayInfo.logicalWidth);
2201 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2202 pw.print(" app=");
2203 pw.print(mDisplayInfo.appWidth);
2204 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2205 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2206 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2207 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2208 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002209 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002210 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002211 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002212
Craig Mautnerdc548482014-02-05 13:35:24 -08002213 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002214 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002215 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
2216 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002217 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002218 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002219
Craig Mautnerdc548482014-02-05 13:35:24 -08002220 pw.println();
2221 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002222 pw.println();
2223 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002224 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002225 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002226 pw.print(" Exiting #"); pw.print(i);
2227 pw.print(' '); pw.print(token);
2228 pw.println(':');
2229 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002230 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002231 }
Craig Mautner59c00972012-07-30 12:10:24 -07002232 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002233 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07002234 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002235 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002236 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002237 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002238
2239 if (mInputMethodAnimLayerAdjustment != 0) {
2240 pw.println(subPrefix
2241 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
2242 }
Craig Mautner59c00972012-07-30 12:10:24 -07002243 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002244
2245 @Override
2246 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002247 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002248 }
2249
2250 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002251 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002252 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002253
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002254 /** Checks if stack with provided id is visible on this display. */
2255 boolean isStackVisible(int stackId) {
2256 final TaskStack stack = getStackById(stackId);
2257 return (stack != null && stack.isVisible());
2258 }
2259
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002260 /**
2261 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
2262 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07002263 TaskStack getDockedStackLocked() {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07002264 final TaskStack stack = getStack(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07002265 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002266 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002267
2268 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002269 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02002270 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002271 */
Jorim Jaggife762342016-10-13 14:33:27 +02002272 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07002273 return getStack(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08002274 }
2275
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002276 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002277 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002278 final int x = (int) xf;
2279 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002280 final WindowState touchedWin = getWindow(w -> {
2281 final int flags = w.mAttrs.flags;
2282 if (!w.isVisibleLw()) {
2283 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002284 }
2285 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002286 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002287 }
2288
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002289 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002290 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002291 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002292 }
2293
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002294 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002295
2296 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002297 return mTmpRegion.contains(x, y) || touchFlags == 0;
2298 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002299
2300 return touchedWin;
2301 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002302
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002303 boolean canAddToastWindowForUid(int uid) {
2304 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002305 // Also if the app is focused adding more than one toast at
2306 // a time for better backwards compatibility.
2307 final WindowState focusedWindowForUid = getWindow(w ->
2308 w.mOwnerUid == uid && w.isFocused());
2309 if (focusedWindowForUid != null) {
2310 return true;
2311 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002312 final WindowState win = getWindow(w ->
2313 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2314 && !w.mWindowRemovalAllowed);
2315 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002316 }
2317
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002318 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002319 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2320 return;
2321 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002322
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002323 // Used to communicate the old focus to the callback method.
2324 mTmpWindow = oldFocus;
2325
2326 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002327 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002328
2329 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002330 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002331
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002332 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002333
2334 if (mTmpWindow == null) {
2335 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2336 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002337 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002338 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002339 }
2340
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002341 /** Updates the layer assignment of windows on this display. */
2342 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08002343 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002344 if (setLayoutNeeded) {
2345 setLayoutNeeded();
2346 }
2347 }
2348
Wale Ogunwale1666e312016-12-16 11:27:18 -08002349 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2350 // moving containers or resizing them. Need to investigate the best way to have it automatically
2351 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002352 void layoutAndAssignWindowLayersIfNeeded() {
2353 mService.mWindowsChanged = true;
2354 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002355
2356 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2357 false /*updateInputWindows*/)) {
2358 assignWindowLayers(false /* setLayoutNeeded */);
2359 }
2360
2361 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2362 mService.mWindowPlacerLocked.performSurfacePlacement();
2363 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2364 }
2365
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002366 /** Returns true if a leaked surface was destroyed */
2367 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002368 // Used to indicate that a surface was leaked.
2369 mTmpWindow = null;
2370 forAllWindows(w -> {
2371 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002372 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002373 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002374 }
2375 if (!mService.mSessions.contains(wsa.mSession)) {
2376 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002377 + w + " surface=" + wsa.mSurfaceController
2378 + " token=" + w.mToken
2379 + " pid=" + w.mSession.mPid
2380 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002381 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002382 mService.mForceRemoves.add(w);
2383 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002384 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002385 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002386 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002387 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002388 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002389 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002390 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002391 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002392 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002393
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002394 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002395 }
2396
2397 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002398 * Determine and return the window that should be the IME target.
2399 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2400 * @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 +00002401 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002402 WindowState computeImeTarget(boolean updateImeTarget) {
2403 if (mService.mInputMethodWindow == null) {
2404 // There isn't an IME so there shouldn't be a target...That was easy!
2405 if (updateImeTarget) {
2406 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2407 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
2408 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2409 }
2410 return null;
2411 }
2412
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002413 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2414 // same display. Or even when the current IME/target are not on the same screen as the next
2415 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002416 mUpdateImeTarget = updateImeTarget;
2417 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002418
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002419
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002420 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2421 // to be on top of it, but it is not -really- where input will go. So look down below
2422 // for a real window to target...
2423 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2424 final AppWindowToken token = target.mAppToken;
2425 if (token != null) {
2426 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2427 if (betterTarget != null) {
2428 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002429 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002430 }
2431 }
2432
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002433 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2434 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002435
2436 // Now, a special case -- if the last target's window is in the process of exiting, and is
2437 // above the new target, keep on the last target to avoid flicker. Consider for example a
2438 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2439 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2440 // scrim.
2441 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002442 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2443 && (target == null
2444 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002445 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002446 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002447 }
2448
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002449 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2450 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002451
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002452 if (target == null) {
2453 if (updateImeTarget) {
2454 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2455 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2456 + Debug.getCallers(4) : ""));
2457 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
2458 }
2459
2460 return null;
2461 }
2462
2463 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002464 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2465 if (token != null) {
2466
2467 // Now some fun for dealing with window animations that modify the Z order. We need
2468 // to look at all windows below the current target that are in this app, finding the
2469 // highest visible one in layering.
2470 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002471 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002472 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002473 }
2474
2475 if (highestTarget != null) {
2476 final AppTransition appTransition = mService.mAppTransition;
2477 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2478 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2479 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002480 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002481
2482 if (appTransition.isTransitionSet()) {
2483 // If we are currently setting up for an animation, hold everything until we
2484 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002485 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2486 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002487 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002488 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002489 // If the window we are currently targeting is involved with an animation,
2490 // and it is on top of the next target we will be over, then hold off on
2491 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002492 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
2493 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002494 }
2495 }
2496 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002497
2498 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2499 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
2500 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08002501 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002502 }
2503
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002504 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002505 }
2506
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002507 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
2508 if (target == mService.mInputMethodTarget
2509 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
2510 && mInputMethodAnimLayerAdjustment == layerAdj) {
2511 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002512 }
2513
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002514 mService.mInputMethodTarget = target;
2515 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
2516 setInputMethodAnimLayerAdjustment(layerAdj);
2517 assignWindowLayers(false /* setLayoutNeeded */);
2518 }
2519
2520 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2521 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2522 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2523 }
2524
2525 // Used to indicate we have reached the first window in the range we are interested in.
2526 mTmpWindow = null;
2527
2528 // TODO: Figure-out a more efficient way to do this.
2529 final WindowState candidate = getWindow(w -> {
2530 if (w == top) {
2531 // Reached the first window in the range we are interested in.
2532 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002533 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002534 if (mTmpWindow == null) {
2535 return false;
2536 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002537
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002538 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2539 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002540 }
2541 // If we reached the bottom of the range of windows we are considering,
2542 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002543 if (w == bottom) {
2544 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002545 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002546 return false;
2547 });
2548
2549 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002550 }
2551
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002552 void setLayoutNeeded() {
2553 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2554 mLayoutNeeded = true;
2555 }
2556
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002557 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002558 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2559 mLayoutNeeded = false;
2560 }
2561
2562 boolean isLayoutNeeded() {
2563 return mLayoutNeeded;
2564 }
2565
Wale Ogunwale02319a62016-09-26 15:21:22 -07002566 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2567 if (mTokenMap.isEmpty()) {
2568 return;
2569 }
2570 pw.println(" Display #" + mDisplayId);
2571 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2572 while (it.hasNext()) {
2573 final WindowToken token = it.next();
2574 pw.print(" ");
2575 pw.print(token);
2576 if (dumpAll) {
2577 pw.println(':');
2578 token.dump(pw, " ");
2579 } else {
2580 pw.println();
2581 }
2582 }
2583 }
2584
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002585 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002586 final int[] index = new int[1];
2587 forAllWindows(w -> {
2588 final WindowStateAnimator wAnim = w.mWinAnimator;
2589 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2590 index[0] = index[0] + 1;
2591 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002592 }
2593
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002594 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002595 forAllWindows(w -> {
2596 w.mWinAnimator.enableSurfaceTrace(fd);
2597 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002598 }
2599
2600 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002601 forAllWindows(w -> {
2602 w.mWinAnimator.disableSurfaceTrace();
2603 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002604 }
2605
Jorim Jaggife762342016-10-13 14:33:27 +02002606 /**
2607 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2608 */
2609 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2610 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002611 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002612 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2613 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002614 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02002615 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2616 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002617 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002618 }
2619
Wale Ogunwale494009b82016-10-21 09:01:38 -07002620 boolean checkWaitingForWindows() {
2621
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002622 mHaveBootMsg = false;
2623 mHaveApp = false;
2624 mHaveWallpaper = false;
2625 mHaveKeyguard = true;
2626
2627 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002628 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2629 return true;
2630 }
2631 if (w.isDrawnLw()) {
2632 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002633 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002634 } else if (w.mAttrs.type == TYPE_APPLICATION
2635 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002636 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002637 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002638 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002639 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002640 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002641 }
2642 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002643 return false;
2644 });
2645
2646 if (visibleWindow != null) {
2647 // We have a visible window.
2648 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002649 }
2650
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002651 // if the wallpaper service is disabled on the device, we're never going to have
2652 // wallpaper, don't bother waiting for it
2653 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2654 com.android.internal.R.bool.config_enableWallpaperService)
2655 && !mService.mOnlyCore;
2656
Wale Ogunwale494009b82016-10-21 09:01:38 -07002657 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2658 "******** booted=" + mService.mSystemBooted
2659 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002660 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2661 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2662 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002663
2664 // If we are turning on the screen to show the boot message, don't do it until the boot
2665 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002666 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002667 return true;
2668 }
2669
2670 // If we are turning on the screen after the boot is completed normally, don't do so until
2671 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002672 if (mService.mSystemBooted
2673 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002674 return true;
2675 }
2676
2677 return false;
2678 }
2679
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002680 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002681 mTmpWindowAnimator = animator;
2682 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002683 }
2684
2685 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002686 resetAnimationBackgroundAnimator();
2687
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002688 // Used to indicate a detached wallpaper.
2689 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002690 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002691
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002692 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002693
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002694 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002695 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002696 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2697 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002698 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2699 }
2700 }
2701
2702 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002703 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002704 }
2705
Wale Ogunwale494009b82016-10-21 09:01:38 -07002706 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002707 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002708 if (imFocus == null) {
2709 return false;
2710 }
2711
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002712 if (DEBUG_INPUT_METHOD) {
2713 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2714 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2715 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002716 }
2717
Wale Ogunwale494009b82016-10-21 09:01:38 -07002718 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2719
2720 if (DEBUG_INPUT_METHOD) {
2721 Slog.i(TAG_WM, "IM target client: " + imeClient);
2722 if (imeClient != null) {
2723 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2724 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2725 }
2726 }
2727
2728 return imeClient != null && imeClient.asBinder() == client.asBinder();
2729 }
2730
2731 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002732 final WindowState win = getWindow(
2733 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2734 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002735 }
2736
2737 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002738 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002739 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002740 final int curValue = w.mSystemUiVisibility;
2741 final int diff = (curValue ^ visibility) & globalDiff;
2742 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002743 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002744 w.mSeq++;
2745 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002746 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002747 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2748 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002749 visibility, newValue, diff);
2750 }
2751 } catch (RemoteException e) {
2752 // so sorry
2753 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002754 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002755 }
2756
2757 void onWindowFreezeTimeout() {
2758 Slog.w(TAG_WM, "Window freeze timeout expired.");
2759 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002760
2761 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002762 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002763 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002764 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002765 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002766 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2767 - mService.mDisplayFreezeTime);
2768 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002769 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002770 mService.mWindowPlacerLocked.performSurfacePlacement();
2771 }
2772
2773 void waitForAllWindowsDrawn() {
2774 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002775 forAllWindows(w -> {
2776 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2777 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2778 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002779 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002780 w.mLastContentInsets.set(-1, -1, -1, -1);
2781 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002782 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002783 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002784 }
2785
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002786 // TODO: Super crazy long method that should be broken down...
2787 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2788
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002789 final int dw = mDisplayInfo.logicalWidth;
2790 final int dh = mDisplayInfo.logicalHeight;
2791 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2792
2793 mTmpUpdateAllDrawn.clear();
2794
2795 int repeats = 0;
2796 do {
2797 repeats++;
2798 if (repeats > 6) {
2799 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2800 clearLayoutNeeded();
2801 break;
2802 }
2803
2804 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2805 pendingLayoutChanges);
2806
Andrii Kulian839def92016-11-02 10:58:58 -07002807 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2808 // the wallpaper window jumping across displays.
2809 // Remove check for default display when there will be support for multiple wallpaper
2810 // targets (on different displays).
2811 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002812 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002813 }
2814
2815 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2816 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2817 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2818 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002819 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002820 }
2821 }
2822
2823 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2824 setLayoutNeeded();
2825 }
2826
2827 // FIRST LOOP: Perform a layout, if needed.
2828 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2829 performLayout(repeats == 1, false /* updateInputWindows */);
2830 } else {
2831 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2832 }
2833
2834 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2835 pendingLayoutChanges = 0;
2836
2837 if (isDefaultDisplay) {
2838 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002839 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002840 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2841 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2842 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2843 }
2844 } while (pendingLayoutChanges != 0);
2845
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002846 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002847 resetDimming();
2848
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002849 mTmpRecoveringMemory = recoveringMemory;
2850 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002851
2852 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002853 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2854 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2855 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002856 true /* inTraversal, must call performTraversalInTrans... below */);
2857
2858 stopDimmingIfNeeded();
2859
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002860 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2861 if (wallpaperVisible != mLastWallpaperVisible) {
2862 mLastWallpaperVisible = wallpaperVisible;
2863 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2864 }
2865
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002866 while (!mTmpUpdateAllDrawn.isEmpty()) {
2867 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2868 // See if any windows have been drawn, so they (and others associated with them)
2869 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002870 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002871 }
2872
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002873 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002874 }
2875
2876 void performLayout(boolean initial, boolean updateInputWindows) {
2877 if (!isLayoutNeeded()) {
2878 return;
2879 }
2880 clearLayoutNeeded();
2881
2882 final int dw = mDisplayInfo.logicalWidth;
2883 final int dh = mDisplayInfo.logicalHeight;
2884
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002885 if (DEBUG_LAYOUT) {
2886 Slog.v(TAG, "-------------------------------------");
2887 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2888 }
2889
Andrii Kulian8ee72852017-03-10 10:36:45 -08002890 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mRotation,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002891 getConfiguration().uiMode);
2892 if (isDefaultDisplay) {
2893 // Not needed on non-default displays.
2894 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2895 mService.mScreenRect.set(0, 0, dw, dh);
2896 }
2897
2898 mService.mPolicy.getContentRectLw(mContentRect);
2899
2900 int seq = mService.mLayoutSeq + 1;
2901 if (seq < 0) seq = 0;
2902 mService.mLayoutSeq = seq;
2903
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002904 // Used to indicate that we have processed the dream window and all additional windows are
2905 // behind it.
2906 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002907 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002908
2909 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002910 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002911
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002912 // Used to indicate that we have processed the dream window and all additional attached
2913 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002914 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002915 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002916
2917 // Now perform layout of attached windows, which usually depend on the position of the
2918 // window they are attached to. XXX does not deal with windows that are attached to windows
2919 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002920 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002921
2922 // Window frames may have changed. Tell the input dispatcher about it.
2923 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2924 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2925 if (updateInputWindows) {
2926 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2927 }
2928
2929 mService.mPolicy.finishLayoutLw();
2930 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2931 }
2932
2933 /**
2934 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2935 * In portrait mode, it grabs the full screenshot.
2936 *
2937 * @param width the width of the target bitmap
2938 * @param height the height of the target bitmap
2939 * @param includeFullDisplay true if the screen should not be cropped before capture
2940 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2941 * @param config of the output bitmap
2942 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002943 * @param includeDecor whether to include window decors, like the status or navigation bar
2944 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002945 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002946 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002947 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002948 boolean wallpaperOnly, boolean includeDecor) {
2949 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2950 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002951 if (bitmap == null) {
2952 return null;
2953 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002954
2955 if (DEBUG_SCREENSHOT) {
2956 // TEST IF IT's ALL BLACK
2957 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2958 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2959 bitmap.getHeight());
2960 boolean allBlack = true;
2961 final int firstColor = buffer[0];
2962 for (int i = 0; i < buffer.length; i++) {
2963 if (buffer[i] != firstColor) {
2964 allBlack = false;
2965 break;
2966 }
2967 }
2968 if (allBlack) {
2969 final WindowState appWin = mScreenshotApplicationState.appWin;
2970 final int maxLayer = mScreenshotApplicationState.maxLayer;
2971 final int minLayer = mScreenshotApplicationState.minLayer;
2972 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2973 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2974 (appWin != null ?
2975 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2976 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2977 }
2978 }
2979
2980 // Create a copy of the screenshot that is immutable and backed in ashmem.
2981 // This greatly reduces the overhead of passing the bitmap between processes.
2982 Bitmap ret = bitmap.createAshmemBitmap(config);
2983 bitmap.recycle();
2984 return ret;
2985 }
2986
2987 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2988 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2989 boolean includeDecor) {
2990 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2991 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2992 }
2993
2994 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2995 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2996 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002997 int dw = mDisplayInfo.logicalWidth;
2998 int dh = mDisplayInfo.logicalHeight;
2999 if (dw == 0 || dh == 0) {
3000 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3001 + ": returning null. logical widthxheight=" + dw + "x" + dh);
3002 return null;
3003 }
3004
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003005 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003006
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003007 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003008 final Rect frame = new Rect();
3009 final Rect stackBounds = new Rect();
3010
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003011 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003012 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08003013 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003014 synchronized(mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02003015 if (!mService.mPolicy.isScreenOn()) {
3016 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Attempted to take screenshot while display"
3017 + " was off.");
3018 return null;
3019 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003020 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003021 mScreenshotApplicationState.appWin = null;
3022 forAllWindows(w -> {
3023 if (!w.mHasSurface) {
3024 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003025 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003026 if (w.mLayer >= aboveAppLayer) {
3027 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003028 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003029 if (wallpaperOnly && !w.mIsWallpaper) {
3030 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003031 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003032 if (w.mIsImWindow) {
Jorim Jaggieb59d6e2017-05-04 13:54:22 +02003033 return false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003034 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003035 // If this is the wallpaper layer and we're only looking for the wallpaper layer
3036 // then the target window state is this one.
3037 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003038 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003039 }
3040
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003041 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003042 // We have not ran across the target window yet, so it is probably behind
3043 // the wallpaper. This can happen when the keyguard is up and all windows
3044 // are moved behind the wallpaper. We don't want to include the wallpaper
3045 // layer in the screenshot as it will cover-up the layer of the target
3046 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003047 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003048 }
3049 // Fall through. The target window is in front of the wallpaper. For this
3050 // case we want to include the wallpaper layer in the screenshot because
3051 // the target window might have some transparent areas.
3052 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003053 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003054 // This app window is of no interest if it is not associated with the
3055 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003056 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003057 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003058 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003059 }
3060
3061 // Include this window.
3062
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003063 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003064 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003065 if (mScreenshotApplicationState.maxLayer < layer) {
3066 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003067 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003068 if (mScreenshotApplicationState.minLayer > layer) {
3069 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003070 }
3071
3072 // Don't include wallpaper in bounds calculation
Jorim Jaggi70f59482017-05-04 14:05:59 +02003073 if (!w.mIsWallpaper && !mutableIncludeFullDisplay.value) {
3074 if (includeDecor) {
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003075 final Task task = w.getTask();
3076 if (task != null) {
3077 task.getBounds(frame);
3078 } else {
Jorim Jaggi30d64f32017-04-07 16:33:17 +02003079
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +02003080 // No task bounds? Too bad! Ain't no screenshot then.
3081 return true;
3082 }
Jorim Jaggi70f59482017-05-04 14:05:59 +02003083 } else {
3084 final Rect wf = w.mFrame;
3085 final Rect cr = w.mContentInsets;
3086 int left = wf.left + cr.left;
3087 int top = wf.top + cr.top;
3088 int right = wf.right - cr.right;
3089 int bottom = wf.bottom - cr.bottom;
3090 frame.union(left, top, right, bottom);
3091 w.getVisibleBounds(stackBounds);
3092 if (!Rect.intersects(frame, stackBounds)) {
3093 // Set frame empty if there's no intersection.
3094 frame.setEmpty();
3095 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003096 }
3097 }
3098
3099 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003100 (w.mAppToken != null && w.mAppToken.token == appToken)
3101 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi406386a82017-09-15 14:17:40 +02003102 if (foundTargetWs && winAnim.getShown() && winAnim.mLastAlpha > 0f) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003103 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003104 }
3105
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003106 if (w.isObscuringDisplay()){
3107 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003108 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003109 return false;
3110 }, true /* traverseTopToBottom */);
3111
3112 final WindowState appWin = mScreenshotApplicationState.appWin;
3113 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
3114 final int maxLayer = mScreenshotApplicationState.maxLayer;
3115 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003116
3117 if (appToken != null && appWin == null) {
3118 // Can't find a window to snapshot.
3119 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
3120 "Screenshot: Couldn't find a surface matching " + appToken);
3121 return null;
3122 }
3123
3124 if (!screenshotReady) {
3125 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3126 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3127 appWin.mWinAnimator.mDrawState)));
3128 return null;
3129 }
3130
3131 // Screenshot is ready to be taken. Everything from here below will continue
3132 // through the bottom of the loop and return a value. We only stay in the loop
3133 // because we don't want to release the mWindowMap lock until the screenshot is
3134 // taken.
3135
3136 if (maxLayer == 0) {
3137 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3138 + ": returning null maxLayer=" + maxLayer);
3139 return null;
3140 }
3141
Jorim Jaggi02886a82016-12-06 09:10:06 -08003142 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003143 // Constrain frame to the screen size.
3144 if (!frame.intersect(0, 0, dw, dh)) {
3145 frame.setEmpty();
3146 }
3147 } else {
3148 // Caller just wants entire display.
3149 frame.set(0, 0, dw, dh);
3150 }
3151 if (frame.isEmpty()) {
3152 return null;
3153 }
3154
3155 if (width < 0) {
3156 width = (int) (frame.width() * frameScale);
3157 }
3158 if (height < 0) {
3159 height = (int) (frame.height() * frameScale);
3160 }
3161
3162 // Tell surface flinger what part of the image to crop. Take the top
3163 // right part of the application, and crop the larger dimension to fit.
3164 Rect crop = new Rect(frame);
3165 if (width / (float) frame.width() < height / (float) frame.height()) {
3166 int cropWidth = (int)((float)width / (float)height * frame.height());
3167 crop.right = crop.left + cropWidth;
3168 } else {
3169 int cropHeight = (int)((float)height / (float)width * frame.width());
3170 crop.bottom = crop.top + cropHeight;
3171 }
3172
3173 // The screenshot API does not apply the current screen rotation.
3174 int rot = mDisplay.getRotation();
3175
3176 if (rot == ROTATION_90 || rot == ROTATION_270) {
3177 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3178 }
3179
3180 // Surfaceflinger is not aware of orientation, so convert our logical
3181 // crop to surfaceflinger's portrait orientation.
3182 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3183
3184 if (DEBUG_SCREENSHOT) {
3185 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3186 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003187 forAllWindows(w -> {
3188 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
3189 Slog.i(TAG_WM, w + ": " + w.mLayer
3190 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003191 + " surfaceLayer=" + ((controller == null)
3192 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003193 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003194 }
3195
3196 final ScreenRotationAnimation screenRotationAnimation =
3197 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3198 final boolean inRotation = screenRotationAnimation != null &&
3199 screenRotationAnimation.isAnimating();
3200 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3201 "Taking screenshot while rotating");
3202
3203 // We force pending transactions to flush before taking
3204 // the screenshot by pushing an empty synchronous transaction.
3205 SurfaceControl.openTransaction();
3206 SurfaceControl.closeTransactionSync();
3207
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003208 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003209 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003210 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003211 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3212 + ") to layer " + maxLayer);
3213 return null;
3214 }
3215 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003216 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003217 }
3218
3219 // TODO: Can this use createRotationMatrix()?
3220 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3221 if (rot == Surface.ROTATION_90) {
3222 final int tmp = crop.top;
3223 crop.top = dw - crop.right;
3224 crop.right = crop.bottom;
3225 crop.bottom = dw - crop.left;
3226 crop.left = tmp;
3227 } else if (rot == Surface.ROTATION_180) {
3228 int tmp = crop.top;
3229 crop.top = dh - crop.bottom;
3230 crop.bottom = dh - tmp;
3231 tmp = crop.right;
3232 crop.right = dw - crop.left;
3233 crop.left = dw - tmp;
3234 } else if (rot == Surface.ROTATION_270) {
3235 final int tmp = crop.top;
3236 crop.top = crop.left;
3237 crop.left = dh - crop.bottom;
3238 crop.bottom = crop.right;
3239 crop.right = dh - tmp;
3240 }
3241 }
3242
3243 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003244 // Used to indicate the layout is needed.
3245 mTmpWindow = null;
3246
3247 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003248 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003249 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003250 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003251 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003252 w.setDisplayLayoutNeeded();
3253 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003254 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003255
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003256 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003257 mService.mWindowPlacerLocked.performSurfacePlacement();
3258 }
3259 }
3260
Wale Ogunwale1666e312016-12-16 11:27:18 -08003261 void setExitingTokensHasVisible(boolean hasVisible) {
3262 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3263 mExitingTokens.get(i).hasVisible = hasVisible;
3264 }
3265
3266 // Initialize state of exiting applications.
3267 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3268 }
3269
3270 void removeExistingTokensIfPossible() {
3271 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3272 final WindowToken token = mExitingTokens.get(i);
3273 if (!token.hasVisible) {
3274 mExitingTokens.remove(i);
3275 }
3276 }
3277
3278 // Time to remove any exiting applications?
3279 mTaskStackContainers.removeExistingAppTokensIfPossible();
3280 }
3281
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003282 @Override
3283 void onDescendantOverrideConfigurationChanged() {
3284 setLayoutNeeded();
3285 mService.requestTraversal();
3286 }
3287
David Stevens9440dc82017-03-16 19:00:20 -07003288 boolean okToDisplay() {
3289 if (mDisplayId == DEFAULT_DISPLAY) {
3290 return !mService.mDisplayFrozen
3291 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3292 }
3293 return mDisplayInfo.state == Display.STATE_ON;
3294 }
3295
3296 boolean okToAnimate() {
3297 return okToDisplay() &&
3298 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3299 }
3300
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003301 static final class TaskForResizePointSearchResult {
3302 boolean searchDone;
3303 Task taskForResize;
3304
3305 void reset() {
3306 searchDone = false;
3307 taskForResize = null;
3308 }
3309 }
Robert Carr3b716242016-08-16 16:02:21 -07003310
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003311 private static final class ApplySurfaceChangesTransactionState {
3312 boolean displayHasContent;
3313 boolean obscured;
3314 boolean syswin;
3315 boolean focusDisplayed;
3316 float preferredRefreshRate;
3317 int preferredModeId;
3318
3319 void reset() {
3320 displayHasContent = false;
3321 obscured = false;
3322 syswin = false;
3323 focusDisplayed = false;
3324 preferredRefreshRate = 0;
3325 preferredModeId = 0;
3326 }
3327 }
3328
3329 private static final class ScreenshotApplicationState {
3330 WindowState appWin;
3331 int maxLayer;
3332 int minLayer;
3333 boolean screenshotReady;
3334
3335 void reset(boolean screenshotReady) {
3336 appWin = null;
3337 maxLayer = 0;
3338 minLayer = 0;
3339 this.screenshotReady = screenshotReady;
3340 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3341 }
3342 }
3343
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003344 /**
3345 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3346 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3347 * homogeneous children type which is currently required by sub-classes of
3348 * {@link WindowContainer} class.
3349 */
3350 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3351
3352 int size() {
3353 return mChildren.size();
3354 }
3355
3356 E get(int index) {
3357 return mChildren.get(index);
3358 }
3359
3360 @Override
3361 boolean fillsParent() {
3362 return true;
3363 }
3364
3365 @Override
3366 boolean isVisible() {
3367 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003368 }
3369 }
3370
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003371 /**
3372 * Window container class that contains all containers on this display relating to Apps.
3373 * I.e Activities.
3374 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003375 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003376
Andrii Kulian839def92016-11-02 10:58:58 -07003377 /**
3378 * Adds the stack to this container.
3379 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3380 */
3381 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003382 if (stack.mStackId == HOME_STACK_ID) {
3383 if (mHomeStack != null) {
3384 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3385 }
3386 mHomeStack = stack;
3387 }
3388 addChild(stack, onTop);
3389 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003390 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003391
Andrii Kulian839def92016-11-02 10:58:58 -07003392 /** Removes the stack from its container and prepare for changing the parent. */
3393 void removeStackFromDisplay(TaskStack stack) {
3394 removeChild(stack);
3395 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07003396 }
3397
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003398 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003399 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3400 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003401 addChild(stack, addIndex);
3402 setLayoutNeeded();
3403 }
3404
Bryce Lee00d586d2017-07-28 20:48:43 -07003405
3406 @Override
3407 boolean isOnTop() {
3408 // Considered always on top
3409 return true;
3410 }
3411
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003412 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003413 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003414 if (child.getWindowConfiguration().isAlwaysOnTop()
3415 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003416 // This stack is always-on-top, override the default behavior.
3417 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3418
3419 // Moving to its current position, as we must call super but we don't want to
3420 // perform any meaningful action.
3421 final int currentPosition = mChildren.indexOf(child);
3422 super.positionChildAt(currentPosition, child, false /* includingParents */);
3423 return;
3424 }
3425
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003426 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3427 super.positionChildAt(targetPosition, child, includingParents);
3428
3429 setLayoutNeeded();
3430 }
3431
3432 /**
3433 * When stack is added or repositioned, find a proper position for it.
3434 * This will make sure that pinned stack always stays on top.
3435 * @param requestedPosition Position requested by caller.
3436 * @param stack Stack to be added or positioned.
3437 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3438 * @return The proper position for the stack.
3439 */
3440 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3441 final int topChildPosition = mChildren.size() - 1;
3442 boolean toTop = requestedPosition == POSITION_TOP;
3443 toTop |= adding ? requestedPosition >= topChildPosition + 1
3444 : requestedPosition >= topChildPosition;
3445 int targetPosition = requestedPosition;
3446
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003447 if (toTop && stack.getWindowingMode() != WINDOWING_MODE_PINNED
3448 && getStack(WINDOWING_MODE_PINNED) != null) {
Bryce Lee48f4b572017-04-10 10:54:15 -07003449 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003450 TaskStack topStack = mChildren.get(topChildPosition);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003451 if (topStack.getWindowingMode() != WINDOWING_MODE_PINNED) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003452 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3453 }
3454
3455 // So, stack is moved just below the pinned stack.
3456 // When we're adding a new stack the target is the current pinned stack position.
3457 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003458 // stack, because WindowContainer#positionAt() first removes element and then adds
3459 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003460 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3461 }
3462
3463 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003464 }
3465
3466 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003467 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3468 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003469 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003470 if (super.forAllWindows(callback, traverseTopToBottom)) {
3471 return true;
3472 }
3473 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3474 return true;
3475 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003476 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003477 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3478 return true;
3479 }
3480 if (super.forAllWindows(callback, traverseTopToBottom)) {
3481 return true;
3482 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003483 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003484 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003485 }
3486
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003487 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003488 boolean traverseTopToBottom) {
3489 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3490 // app tokens.
3491 // TODO: Investigate if we need to continue to do this or if we can just process them
3492 // in-order.
3493 if (traverseTopToBottom) {
3494 for (int i = mChildren.size() - 1; i >= 0; --i) {
3495 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3496 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003497 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3498 traverseTopToBottom)) {
3499 return true;
3500 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003501 }
3502 }
3503 } else {
3504 final int count = mChildren.size();
3505 for (int i = 0; i < count; ++i) {
3506 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3507 final int appTokensCount = appTokens.size();
3508 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003509 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3510 traverseTopToBottom)) {
3511 return true;
3512 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003513 }
3514 }
3515 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003516 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003517 }
3518
Wale Ogunwale1666e312016-12-16 11:27:18 -08003519 void setExitingTokensHasVisible(boolean hasVisible) {
3520 for (int i = mChildren.size() - 1; i >= 0; --i) {
3521 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3522 for (int j = appTokens.size() - 1; j >= 0; --j) {
3523 appTokens.get(j).hasVisible = hasVisible;
3524 }
3525 }
3526 }
3527
3528 void removeExistingAppTokensIfPossible() {
3529 for (int i = mChildren.size() - 1; i >= 0; --i) {
3530 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3531 for (int j = appTokens.size() - 1; j >= 0; --j) {
3532 final AppWindowToken token = appTokens.get(j);
3533 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3534 && (!token.mIsExiting || token.isEmpty())) {
3535 // Make sure there is no animation running on this token, so any windows
3536 // associated with it will be removed as soon as their animations are
3537 // complete.
3538 token.mAppAnimator.clearAnimation();
3539 token.mAppAnimator.animating = false;
3540 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3541 "performLayout: App token exiting now removed" + token);
3542 token.removeIfPossible();
3543 }
3544 }
3545 }
3546 }
3547
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003548 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003549 int getOrientation() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08003550 if (isStackVisible(DOCKED_STACK_ID) || isStackVisible(FREEFORM_WORKSPACE_STACK_ID)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003551 // Apps and their containers are not allowed to specify an orientation while the
3552 // docked or freeform stack is visible...except for the home stack/task if the
3553 // docked stack is minimized and it actually set something.
3554 if (mHomeStack != null && mHomeStack.isVisible()
3555 && mDividerControllerLocked.isMinimizedDock()) {
3556 final int orientation = mHomeStack.getOrientation();
3557 if (orientation != SCREEN_ORIENTATION_UNSET) {
3558 return orientation;
3559 }
3560 }
3561 return SCREEN_ORIENTATION_UNSPECIFIED;
3562 }
3563
3564 final int orientation = super.getOrientation();
3565 if (orientation != SCREEN_ORIENTATION_UNSET
3566 && orientation != SCREEN_ORIENTATION_BEHIND) {
3567 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3568 "App is requesting an orientation, return " + orientation);
3569 return orientation;
3570 }
3571
3572 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003573 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003574 // The next app has not been requested to be visible, so we keep the current orientation
3575 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003576 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003577 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003578 }
3579
3580 /**
3581 * Window container class that contains all containers on this display that are not related to
3582 * Apps. E.g. status bar.
3583 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003584 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3585 /**
3586 * Compares two child window tokens returns -1 if the first is lesser than the second in
3587 * terms of z-order and 1 otherwise.
3588 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003589 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003590 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003591 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3592 token1.mOwnerCanManageAppTokens)
3593 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3594 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003595
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003596 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3597 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3598 return false;
3599 }
3600 final int req = w.mAttrs.screenOrientation;
3601 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3602 || req == SCREEN_ORIENTATION_UNSET) {
3603 return false;
3604 }
3605 return true;
3606 };
3607
Wale Ogunwale3a931692016-11-02 16:49:48 -07003608 private final String mName;
3609 NonAppWindowContainers(String name) {
3610 mName = name;
3611 }
3612
3613 void addChild(WindowToken token) {
3614 addChild(token, mWindowComparator);
3615 }
3616
3617 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003618 int getOrientation() {
3619 final WindowManagerPolicy policy = mService.mPolicy;
3620 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003621 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003622
3623 if (win != null) {
3624 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003625 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003626 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003627 if (mService.mKeyguardGoingAway) {
3628 // Keyguard can't affect the orientation if it is going away...
3629 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3630 return SCREEN_ORIENTATION_UNSET;
3631 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003632 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003633 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003634 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003635 }
3636
Andrii Kulian8ee72852017-03-10 10:36:45 -08003637 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003638
Jorim Jaggi75520d52017-08-24 17:23:19 +02003639 if (policy.isKeyguardShowingAndNotOccluded()
3640 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003641 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003642 }
3643
3644 return SCREEN_ORIENTATION_UNSET;
3645 }
3646
3647 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003648 String getName() {
3649 return mName;
3650 }
Robert Carr3b716242016-08-16 16:02:21 -07003651 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003652
3653 /**
3654 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
3655 */
3656 @FunctionalInterface
3657 private interface Screenshoter<E> {
3658 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
3659 boolean useIdentityTransform, int rotation);
3660 }
Craig Mautner59c00972012-07-30 12:10:24 -07003661}