blob: 2cc2a0e70ebaa02fa007ed948ff03b5920961e41 [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 Ogunwale30e441d2017-11-09 08:28:45 -080019import static android.app.ActivityManager.SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT;
Wale Ogunwale61911492017-10-11 08:50:50 -070020import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070021import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070022import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
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;
Adrian Roose99bc052017-11-20 17:55:31 +010061import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
62import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
63import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
64import static com.android.server.policy.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 Ogunwale2b06bfc2016-09-28 14:17:05 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070076import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070077import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080078import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070079import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070080import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070081import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070082import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070083import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070084import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080085import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -070086import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070087import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
88import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070089import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070090import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -070091import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
92import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070093import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -070094import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070095import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070096import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070097import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070098import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070099import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700100import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700101import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700102import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700103import static com.android.server.wm.proto.DisplayProto.ABOVE_APP_WINDOWS;
104import static com.android.server.wm.proto.DisplayProto.BELOW_APP_WINDOWS;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000105import static com.android.server.wm.proto.DisplayProto.DISPLAY_FRAMES;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700106import static com.android.server.wm.proto.DisplayProto.DISPLAY_INFO;
107import static com.android.server.wm.proto.DisplayProto.DOCKED_STACK_DIVIDER_CONTROLLER;
108import static com.android.server.wm.proto.DisplayProto.DPI;
109import static com.android.server.wm.proto.DisplayProto.ID;
110import static com.android.server.wm.proto.DisplayProto.IME_WINDOWS;
111import static com.android.server.wm.proto.DisplayProto.PINNED_STACK_CONTROLLER;
Steven Timotiusf2d68892017-08-28 17:00:01 -0700112import static com.android.server.wm.proto.DisplayProto.ROTATION;
113import static com.android.server.wm.proto.DisplayProto.SCREEN_ROTATION_ANIMATION;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700114import static com.android.server.wm.proto.DisplayProto.STACKS;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700115import static com.android.server.wm.proto.DisplayProto.WINDOW_CONTAINER;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700116
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700117import android.annotation.CallSuper;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700118import android.annotation.NonNull;
Dean Harding3e5a1522017-10-06 09:19:01 -0700119import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700120import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700121import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700122import android.graphics.Bitmap;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700123import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800124import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700125import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700126import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100127import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700128import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700129import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700130import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700131import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700132import android.os.RemoteException;
133import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100134import android.os.Trace;
Chong Zhang8e89b312015-09-09 15:09:30 -0700135import android.util.DisplayMetrics;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800136import android.util.MutableBoolean;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700137import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700138import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700139import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700140import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700141import android.view.InputDevice;
Robert Carrb1579c82017-09-05 14:54:47 -0700142import android.view.MagnificationSpec;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700143import android.view.Surface;
144import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100145import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700146import android.view.SurfaceSession;
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;
Adrian Roose99bc052017-11-20 17:55:31 +0100151import com.android.server.policy.WindowManagerPolicy;
Craig Mautner59c00972012-07-30 12:10:24 -0700152
Robert Carr3b716242016-08-16 16:02:21 -0700153import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700154import java.io.PrintWriter;
155import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700156import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700157import java.util.HashMap;
158import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700159import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700160import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800161import java.util.function.Consumer;
162import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700163
Craig Mautner59c00972012-07-30 12:10:24 -0700164/**
165 * Utility class for keeping track of the WindowStates and other pertinent contents of a
166 * particular Display.
167 *
168 * IMPORTANT: No method from this class should ever be used without holding
169 * WindowManagerService.mWindowMap.
170 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700171class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700172 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700173
174 /** Unique identifier of this stack. */
175 private final int mDisplayId;
176
Wale Ogunwale3a931692016-11-02 16:49:48 -0700177 /** The containers below are the only child containers the display can have. */
178 // Contains all window containers that are related to apps (Activities)
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100179 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700180 // Contains all non-app window containers that should be displayed above the app containers
181 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800182 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100183 new AboveAppWindowContainers("mAboveAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700184 // Contains all non-app window containers that should be displayed below the app containers
185 // (e.g. Wallpaper).
186 private final NonAppWindowContainers mBelowAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100187 new NonAppWindowContainers("mBelowAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700188 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
189 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
Robert Carr9034d962018-01-04 18:27:42 -0800190 // window containers together and move them in-sync if/when needed. We use a subclass of
191 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
192 private final NonMagnifiableWindowContainers mImeWindowsContainers =
193 new NonMagnifiableWindowContainers("mImeWindowsContainers", mService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700194
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000195 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800196 private WindowState mTmpWindow2;
197 private WindowAnimator mTmpWindowAnimator;
198 private boolean mTmpRecoveringMemory;
199 private boolean mUpdateImeTarget;
200 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700201 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700202
Wale Ogunwale02319a62016-09-26 15:21:22 -0700203 // Mapping from a token IBinder to a WindowToken object on this display.
204 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
205
Andrii Kuliancd097992017-03-23 18:31:59 -0700206 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700207 int mInitialDisplayWidth = 0;
208 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700209 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700210
211 /**
212 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
213 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
214 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
215 */
Craig Mautner59c00972012-07-30 12:10:24 -0700216 int mBaseDisplayWidth = 0;
217 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700218 /**
219 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
220 * but can be set from Settings or via shell command "adb shell wm density".
221 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
222 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700223 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700224 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700225 private final DisplayInfo mDisplayInfo = new DisplayInfo();
226 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700227 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000228 DisplayFrames mDisplayFrames;
229
Andrii Kulian06d07d62017-03-14 11:11:47 -0700230 /**
231 * For default display it contains real metrics, empty for others.
232 * @see WindowManagerService#createWatermarkInTransaction()
233 */
234 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
235 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
236 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700237
Andrii Kulian06d07d62017-03-14 11:11:47 -0700238 /**
239 * Compat metrics computed based on {@link #mDisplayMetrics}.
240 * @see #updateDisplayAndOrientation(int)
241 */
242 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
243
244 /** The desired scaling factor for compatible apps. */
245 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700246
Andrii Kulian8ee72852017-03-10 10:36:45 -0800247 /**
248 * Current rotation of the display.
249 * Constants as per {@link android.view.Surface.Rotation}.
250 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700251 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800252 */
253 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700254
Andrii Kulian8ee72852017-03-10 10:36:45 -0800255 /**
256 * Last applied orientation of the display.
257 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
258 *
259 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
260 */
261 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700262
Andrii Kulian8ee72852017-03-10 10:36:45 -0800263 /**
264 * Flag indicating that the application is receiving an orientation that has different metrics
265 * than it expected. E.g. Portrait instead of Landscape.
266 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700267 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800268 */
269 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700270
Andrii Kulian8ee72852017-03-10 10:36:45 -0800271 /**
272 * Orientation forced by some window. If there is no visible window that specifies orientation
273 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
274 *
275 * @see NonAppWindowContainers#getOrientation()
276 */
277 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700278
Andrii Kulian8ee72852017-03-10 10:36:45 -0800279 /**
280 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
281 * occluded.
282 *
283 * @see NonAppWindowContainers#getOrientation()
284 */
285 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
286
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700287 /**
288 * Keep track of wallpaper visibility to notify changes.
289 */
290 private boolean mLastWallpaperVisible = false;
291
Andrii Kulian06d07d62017-03-14 11:11:47 -0700292 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700293
Craig Mautner39834192012-09-02 07:47:24 -0700294 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700295 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700296 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700297 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800298 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700299
Craig Mautnerdc548482014-02-05 13:35:24 -0800300 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700301 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800302
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700303 /** Detect user tapping outside of current focused task bounds .*/
304 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700305
Craig Mautner6601b7b2013-04-29 10:29:11 -0700306 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700307 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700308
Craig Mautner6601b7b2013-04-29 10:29:11 -0700309 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800310 private final Rect mTmpRect = new Rect();
311 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700312 private final RectF mTmpRectF = new RectF();
313 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800314 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700315
Bryce Leef3c6a472017-11-14 14:53:06 -0800316 /** Used for handing back size of display */
317 private final Rect mTmpBounds = new Rect();
318
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
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800325 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
326
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000327 private boolean mHaveBootMsg = false;
328 private boolean mHaveApp = false;
329 private boolean mHaveWallpaper = false;
330 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700331
332 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
333
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700334 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
335 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000336 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
337 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700338
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700339 // True if this display is in the process of being removed. Used to determine if the removal of
340 // the display's direct children should be allowed.
341 private boolean mRemovingDisplay = false;
342
Bryce Leed1871262017-06-12 14:12:29 -0700343 // {@code false} if this display is in the processing of being created.
344 private boolean mDisplayReady = false;
345
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800346 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700347
Robert Carrb1579c82017-09-05 14:54:47 -0700348 private final SurfaceSession mSession = new SurfaceSession();
349
350 /**
351 * We organize all top-level Surfaces in to the following layers.
352 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800353 * and omitted from Screen-Magnification, for example the strict mode flash or
354 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700355 * {@link #mWindowingLayer} contains everything else.
356 */
357 private SurfaceControl mOverlayLayer;
358
359 /**
360 * See {@link #mOverlayLayer}
361 */
362 private SurfaceControl mWindowingLayer;
363
364 /**
365 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
366 * <p>
367 * For these surfaces currently we use a surface based on the larger of width or height so we
368 * don't have to resize when rotating the display.
369 */
370 private int mSurfaceSize;
371
372 /**
373 * A list of surfaces to be destroyed after {@link #mPendingTransaction} is applied.
374 */
375 private final ArrayList<SurfaceControl> mPendingDestroyingSurfaces = new ArrayList<>();
376
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100377 /** Temporary float array to retrieve 3x3 matrix values. */
378 private final float[] mTmpFloats = new float[9];
379
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800380 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
381 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800382 final AppWindowToken atoken = w.mAppToken;
383 if (winAnimator.mDrawState == READY_TO_SHOW) {
384 if (atoken == null || atoken.allDrawn) {
385 if (w.performShowLocked()) {
386 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
387 if (DEBUG_LAYOUT_REPEATS) {
388 mService.mWindowPlacerLocked.debugLayoutRepeats(
389 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
390 }
391 }
392 }
393 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800394 };
395
396 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
397 final WindowStateAnimator winAnimator = w.mWinAnimator;
398 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
399 return;
400 }
401
402 final int flags = w.mAttrs.flags;
403
404 // If this window is animating, make a note that we have an animating window and take
405 // care of a request to run a detached wallpaper animation.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100406 if (winAnimator.isAnimationSet()) {
407 final AnimationAdapter anim = w.getAnimation();
408 if (anim != null) {
409 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && anim.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800410 mTmpWindow = w;
411 }
Jorim Jaggia5e10572017-11-15 14:36:26 +0100412 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800413 if (color != 0) {
414 final TaskStack stack = w.getStack();
415 if (stack != null) {
416 stack.setAnimationBackground(winAnimator, color);
417 }
418 }
419 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800420 }
421
422 // If this window's app token is running a detached wallpaper animation, make a note so
423 // we can ensure the wallpaper is displayed behind it.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200424 final AppWindowToken atoken = winAnimator.mWin.mAppToken;
425 final AnimationAdapter animation = atoken != null ? atoken.getAnimation() : null;
426 if (animation != null) {
427 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && animation.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800428 mTmpWindow = w;
429 }
430
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200431 final int color = animation.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800432 if (color != 0) {
433 final TaskStack stack = w.getStack();
434 if (stack != null) {
435 stack.setAnimationBackground(winAnimator, color);
436 }
437 }
438 }
439 };
440
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800441 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
442 final int lostFocusUid = mTmpWindow.mOwnerUid;
443 final Handler handler = mService.mH;
444 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
445 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
446 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
447 w.mAttrs.hideTimeoutMilliseconds);
448 }
449 }
450 };
451
452 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
453 final AppWindowToken focusedApp = mService.mFocusedApp;
454 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
455 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
456
457 if (!w.canReceiveKeys()) {
458 return false;
459 }
460
461 final AppWindowToken wtoken = w.mAppToken;
462
463 // If this window's application has been removed, just skip it.
464 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
465 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
466 + (wtoken.removed ? "removed" : "sendingToBottom"));
467 return false;
468 }
469
470 if (focusedApp == null) {
471 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
472 + " using new focus @ " + w);
473 mTmpWindow = w;
474 return true;
475 }
476
477 if (!focusedApp.windowsAreFocusable()) {
478 // Current focused app windows aren't focusable...
479 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
480 + " focusable using new focus @ " + w);
481 mTmpWindow = w;
482 return true;
483 }
484
485 // Descend through all of the app tokens and find the first that either matches
486 // win.mAppToken (return win) or mFocusedApp (return null).
487 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
488 if (focusedApp.compareTo(wtoken) > 0) {
489 // App stack below focused app stack. No focus for you!!!
490 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
491 "findFocusedWindow: Reached focused app=" + focusedApp);
492 mTmpWindow = null;
493 return true;
494 }
495 }
496
497 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
498 mTmpWindow = w;
499 return true;
500 };
501
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800502 private final Consumer<WindowState> mPerformLayout = w -> {
503 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
504 // wasting time and funky changes while a window is animating away.
505 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
506 || w.isGoneForLayoutLw();
507
508 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
509 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
510 + " mLayoutAttached=" + w.mLayoutAttached
511 + " screen changed=" + w.isConfigChanged());
512 final AppWindowToken atoken = w.mAppToken;
513 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200514 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800515 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
516 + " parentHidden=" + w.isParentWindowHidden());
517 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200518 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800519 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
520 + " parentHidden=" + w.isParentWindowHidden());
521 }
522
523 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
524 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
525 // since that means "perform layout as normal, just don't display").
526 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
527 || ((w.isConfigChanged() || w.setReportResizeHints())
528 && !w.isGoneForLayoutLw() &&
529 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
530 (w.mHasSurface && w.mAppToken != null &&
531 w.mAppToken.layoutConfigChanges)))) {
532 if (!w.mLayoutAttached) {
533 if (mTmpInitial) {
534 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
535 w.mContentChanged = false;
536 }
537 if (w.mAttrs.type == TYPE_DREAM) {
538 // Don't layout windows behind a dream, so that if it does stuff like hide
539 // the status bar we won't get a bad transition when it goes away.
540 mTmpWindow = w;
541 }
542 w.mLayoutNeeded = false;
543 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200544 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000545 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800546 w.mLayoutSeq = mService.mLayoutSeq;
547
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200548 // If this is the first layout, we need to initialize the last inset values as
549 // otherwise we'd immediately cause an unnecessary resize.
550 if (firstLayout) {
551 w.updateLastInsetValues();
552 }
553
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800554 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
555 + " mContainingFrame=" + w.mContainingFrame
556 + " mDisplayFrame=" + w.mDisplayFrame);
557 }
558 }
559 };
560
561 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
562 if (w.mLayoutAttached) {
563 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
564 + " mViewVisibility=" + w.mViewVisibility
565 + " mRelayoutCalled=" + w.mRelayoutCalled);
566 // If this view is GONE, then skip it -- keep the current frame, and let the caller
567 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
568 // windows, since that means "perform layout as normal, just don't display").
569 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
570 return;
571 }
572 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
573 || w.mLayoutNeeded) {
574 if (mTmpInitial) {
575 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
576 w.mContentChanged = false;
577 }
578 w.mLayoutNeeded = false;
579 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000580 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800581 w.mLayoutSeq = mService.mLayoutSeq;
582 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
583 + " mContainingFrame=" + w.mContainingFrame
584 + " mDisplayFrame=" + w.mDisplayFrame);
585 }
586 } else if (w.mAttrs.type == TYPE_DREAM) {
587 // Don't layout windows behind a dream, so that if it does stuff like hide the
588 // status bar we won't get a bad transition when it goes away.
589 mTmpWindow = mTmpWindow2;
590 }
591 };
592
593 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
594 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
595 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
596 return w.canBeImeTarget();
597 };
598
599 private final Consumer<WindowState> mApplyPostLayoutPolicy =
600 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
601 mService.mInputMethodTarget);
602
603 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
604 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
605 final boolean obscuredChanged = w.mObscured !=
606 mTmpApplySurfaceChangesTransactionState.obscured;
607 final RootWindowContainer root = mService.mRoot;
608 // Only used if default window
609 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
610
611 // Update effect.
612 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
613 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
614 final boolean isDisplayed = w.isDisplayedLw();
615
616 if (isDisplayed && w.isObscuringDisplay()) {
617 // This window completely covers everything behind it, so we want to leave all
618 // of them as undimmed (for performance reasons).
619 root.mObscuringWindow = w;
620 mTmpApplySurfaceChangesTransactionState.obscured = true;
621 }
622
623 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
624 root.handleNotObscuredLocked(w,
625 mTmpApplySurfaceChangesTransactionState.obscured,
626 mTmpApplySurfaceChangesTransactionState.syswin);
627
628 if (w.mHasSurface && isDisplayed) {
629 final int type = w.mAttrs.type;
630 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
631 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
632 mTmpApplySurfaceChangesTransactionState.syswin = true;
633 }
634 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
635 && w.mAttrs.preferredRefreshRate != 0) {
636 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
637 = w.mAttrs.preferredRefreshRate;
638 }
639 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
640 && w.mAttrs.preferredDisplayModeId != 0) {
641 mTmpApplySurfaceChangesTransactionState.preferredModeId
642 = w.mAttrs.preferredDisplayModeId;
643 }
644 }
645 }
646
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800647 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
648 && mWallpaperController.isWallpaperTarget(w)) {
649 // This is the wallpaper target and its obscured state changed... make sure the
650 // current wallpaper's visibility has been updated accordingly.
651 mWallpaperController.updateWallpaperVisibility();
652 }
653
Jorim Jaggi6b0f8462018-01-05 17:23:47 +0100654 w.handleWindowMovedIfNeeded(mPendingTransaction);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800655
656 final WindowStateAnimator winAnimator = w.mWinAnimator;
657
658 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
659 w.mContentChanged = false;
660
661 // Moved from updateWindowsAndWallpaperLocked().
662 if (w.mHasSurface) {
663 // Take care of the window being ready to display.
664 final boolean committed = winAnimator.commitFinishDrawingLocked();
665 if (isDefaultDisplay && committed) {
666 if (w.mAttrs.type == TYPE_DREAM) {
667 // HACK: When a dream is shown, it may at that point hide the lock screen.
668 // So we need to redo the layout to let the phone window manager make this
669 // happen.
670 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
671 if (DEBUG_LAYOUT_REPEATS) {
672 surfacePlacer.debugLayoutRepeats(
673 "dream and commitFinishDrawingLocked true",
674 pendingLayoutChanges);
675 }
676 }
677 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
678 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
679 "First draw done in potential wallpaper target " + w);
680 root.mWallpaperMayChange = true;
681 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
682 if (DEBUG_LAYOUT_REPEATS) {
683 surfacePlacer.debugLayoutRepeats(
684 "wallpaper and commitFinishDrawingLocked true",
685 pendingLayoutChanges);
686 }
687 }
688 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800689 }
690
691 final AppWindowToken atoken = w.mAppToken;
692 if (atoken != null) {
693 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
694 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
695 mTmpUpdateAllDrawn.add(atoken);
696 }
697 }
698
699 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
700 && w.isDisplayedLw()) {
701 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
702 }
703
704 w.updateResizingWindowIfNeeded();
705 };
706
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800707 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800708 * Create new {@link DisplayContent} instance, add itself to the root window container and
709 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700710 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800711 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700712 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
713 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700714 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700715 DisplayContent(Display display, WindowManagerService service,
Robert Carrb1579c82017-09-05 14:54:47 -0700716 WallpaperController wallpaperController) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100717 super(service);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800718 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
719 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
720 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
721 + " new=" + display);
722 }
723
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700724 mDisplay = display;
725 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700726 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700727 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700728 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700729 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000730 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700731 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800732 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700733 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700734
Robert Carrb1579c82017-09-05 14:54:47 -0700735 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth);
736
737 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
738 .setSize(mSurfaceSize, mSurfaceSize)
739 .setOpaque(true);
740 mWindowingLayer = b.setName("Display Root").build();
741 mOverlayLayer = b.setName("Display Overlays").build();
742
Robert Carrf59b8dd2017-10-02 18:58:36 -0700743 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700744 .setLayerStack(mWindowingLayer, mDisplayId)
745 .show(mWindowingLayer)
746 .setLayer(mOverlayLayer, 1)
747 .setLayerStack(mOverlayLayer, mDisplayId)
748 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700749 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700750
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700751 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700752 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700753 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700754 super.addChild(mAboveAppWindowsContainers, null);
755 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800756
757 // Add itself as a child to the root container.
758 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700759
760 // TODO(b/62541591): evaluate whether this is the best spot to declare the
761 // {@link DisplayContent} ready for use.
762 mDisplayReady = true;
763 }
764
765 boolean isReady() {
766 // The display is ready when the system and the individual display are both ready.
767 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700768 }
769
770 int getDisplayId() {
771 return mDisplayId;
772 }
773
Wale Ogunwale02319a62016-09-26 15:21:22 -0700774 WindowToken getWindowToken(IBinder binder) {
775 return mTokenMap.get(binder);
776 }
777
778 AppWindowToken getAppWindowToken(IBinder binder) {
779 final WindowToken token = getWindowToken(binder);
780 if (token == null) {
781 return null;
782 }
783 return token.asAppWindowToken();
784 }
785
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700786 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700787 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
788 if (dc != null) {
789 // We currently don't support adding a window token to the display if the display
790 // already has the binder mapped to another token. If there is a use case for supporting
791 // this moving forward we will either need to merge the WindowTokens some how or have
792 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700793 throw new IllegalArgumentException("Can't map token=" + token + " to display="
794 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700795 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700796 if (binder == null) {
797 throw new IllegalArgumentException("Can't map token=" + token + " to display="
798 + getName() + " binder is null");
799 }
800 if (token == null) {
801 throw new IllegalArgumentException("Can't map null token to display="
802 + getName() + " binder=" + binder);
803 }
804
Wale Ogunwale02319a62016-09-26 15:21:22 -0700805 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700806
807 if (token.asAppWindowToken() == null) {
808 // Add non-app token to container hierarchy on the display. App tokens are added through
809 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700810 switch (token.windowType) {
811 case TYPE_WALLPAPER:
812 mBelowAppWindowsContainers.addChild(token);
813 break;
814 case TYPE_INPUT_METHOD:
815 case TYPE_INPUT_METHOD_DIALOG:
816 mImeWindowsContainers.addChild(token);
817 break;
818 default:
819 mAboveAppWindowsContainers.addChild(token);
820 break;
821 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700822 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700823 }
824
825 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700826 final WindowToken token = mTokenMap.remove(binder);
827 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800828 token.setExiting();
829 }
830 return token;
831 }
832
833 /** Changes the display the input window token is housed on to this one. */
834 void reParentWindowToken(WindowToken token) {
835 final DisplayContent prevDc = token.getDisplayContent();
836 if (prevDc == this) {
837 return;
838 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800839 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
840 && token.asAppWindowToken() == null) {
841 // Removed the token from the map, but made sure it's not an app token before removing
842 // from parent.
843 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700844 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800845
846 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700847 }
848
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700849 void removeAppToken(IBinder binder) {
850 final WindowToken token = removeWindowToken(binder);
851 if (token == null) {
852 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
853 return;
854 }
855
856 final AppWindowToken appToken = token.asAppWindowToken();
857
858 if (appToken == null) {
859 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
860 return;
861 }
862
863 appToken.onRemovedFromDisplay();
864 }
865
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700866 Display getDisplay() {
867 return mDisplay;
868 }
869
Craig Mautner59c00972012-07-30 12:10:24 -0700870 DisplayInfo getDisplayInfo() {
871 return mDisplayInfo;
872 }
873
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700874 DisplayMetrics getDisplayMetrics() {
875 return mDisplayMetrics;
876 }
877
Andrii Kulian8ee72852017-03-10 10:36:45 -0800878 int getRotation() {
879 return mRotation;
880 }
881
882 void setRotation(int newRotation) {
883 mRotation = newRotation;
884 }
885
886 int getLastOrientation() {
887 return mLastOrientation;
888 }
889
890 void setLastOrientation(int orientation) {
891 mLastOrientation = orientation;
892 }
893
894 boolean getAltOrientation() {
895 return mAltOrientation;
896 }
897
898 void setAltOrientation(boolean altOrientation) {
899 mAltOrientation = altOrientation;
900 }
901
902 int getLastWindowForcedOrientation() {
903 return mLastWindowForcedOrientation;
904 }
905
Andrii Kulian06d07d62017-03-14 11:11:47 -0700906 /**
907 * Update rotation of the display.
908 *
909 * Returns true if the rotation has been changed. In this case YOU MUST CALL
910 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
911 */
912 boolean updateRotationUnchecked(boolean inTransaction) {
913 if (mService.mDeferredRotationPauseCount > 0) {
914 // Rotation updates have been paused temporarily. Defer the update until
915 // updates have been resumed.
916 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
917 return false;
918 }
919
920 ScreenRotationAnimation screenRotationAnimation =
921 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
922 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
923 // Rotation updates cannot be performed while the previous rotation change
924 // animation is still in progress. Skip this update. We will try updating
925 // again after the animation is finished and the display is unfrozen.
926 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
927 return false;
928 }
929 if (mService.mDisplayFrozen) {
930 // Even if the screen rotation animation has finished (e.g. isAnimating
931 // returns false), there is still some time where we haven't yet unfrozen
932 // the display. We also need to abort rotation here.
933 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
934 "Deferring rotation, still finishing previous rotation");
935 return false;
936 }
937
938 if (!mService.mDisplayEnabled) {
939 // No point choosing a rotation if the display is not enabled.
940 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
941 return false;
942 }
943
944 final int oldRotation = mRotation;
945 final int lastOrientation = mLastOrientation;
946 final boolean oldAltOrientation = mAltOrientation;
947 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr0e007272017-10-02 13:00:55 -0700948 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
Robert Carr897215d2017-03-29 12:25:50 -0700949 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700950
Robert Carr0e007272017-10-02 13:00:55 -0700951 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700952 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
953 if (seamlessRotated != null) {
954 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
955 // to complete (that is, waiting for windows to redraw). It's tempting to check
956 // w.mSeamlessRotationCount but that could be incorrect in the case of
957 // window-removal.
958 return false;
959 }
Robert Carr0e007272017-10-02 13:00:55 -0700960
961 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700962 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -0700963 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -0700964 mayRotateSeamlessly = false;
965 }
966 for (int i = 0; i < mService.mSessions.size(); i++) {
967 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
968 mayRotateSeamlessly = false;
969 break;
970 }
971 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700972 }
Robert Carr0e007272017-10-02 13:00:55 -0700973 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700974
975 // TODO: Implement forced rotation changes.
976 // Set mAltOrientation to indicate that the application is receiving
977 // an orientation that has different metrics than it expected.
978 // eg. Portrait instead of Landscape.
979
980 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
981 lastOrientation, rotation);
982
983 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
984 + ", got rotation " + rotation + " which has "
985 + (altOrientation ? "incompatible" : "compatible") + " metrics");
986
987 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
988 // No change.
989 return false;
990 }
991
992 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
993 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
994 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
995
996 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
997 mService.mWaitingForConfig = true;
998 }
999
1000 mRotation = rotation;
1001 mAltOrientation = altOrientation;
1002 if (isDefaultDisplay) {
1003 mService.mPolicy.setRotationLw(rotation);
1004 }
1005
1006 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1007 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1008 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1009 WINDOW_FREEZE_TIMEOUT_DURATION);
1010
1011 setLayoutNeeded();
1012 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001013 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001014
1015 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001016 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001017 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1018 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1019 mDisplayId);
1020 } else {
1021 // The screen rotation animation uses a screenshot to freeze the screen
1022 // while windows resize underneath.
1023 // When we are rotating seamlessly, we allow the elements to transition
1024 // to their rotated state independently and without a freeze required.
1025 screenRotationAnimation = null;
1026
1027 // We have to reset this in case a window was removed before it
1028 // finished seamless rotation.
1029 mService.mSeamlessRotationCount = 0;
1030 }
1031
1032 // We need to update our screen size information to match the new rotation. If the rotation
1033 // has actually changed then this method will return true and, according to the comment at
1034 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1035 // By updating the Display info here it will be available to
1036 // #computeScreenConfiguration() later.
1037 updateDisplayAndOrientation(getConfiguration().uiMode);
1038
1039 if (!inTransaction) {
1040 if (SHOW_TRANSACTIONS) {
1041 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1042 }
1043 mService.openSurfaceTransaction();
1044 }
1045 try {
1046 // NOTE: We disable the rotation in the emulator because
1047 // it doesn't support hardware OpenGL emulation yet.
1048 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1049 && screenRotationAnimation.hasScreenshot()) {
Robert Carrb1579c82017-09-05 14:54:47 -07001050 if (screenRotationAnimation.setRotationInTransaction(rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001051 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1052 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1053 mService.scheduleAnimationLocked();
1054 }
1055 }
1056
1057 if (rotateSeamlessly) {
1058 forAllWindows(w -> {
1059 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1060 }, true /* traverseTopToBottom */);
1061 }
1062
1063 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1064 } finally {
1065 if (!inTransaction) {
Adrian Roos111aff92017-09-27 18:11:46 +02001066 mService.closeSurfaceTransaction("setRotationUnchecked");
Andrii Kulian06d07d62017-03-14 11:11:47 -07001067 if (SHOW_LIGHT_TRANSACTIONS) {
1068 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1069 }
1070 }
1071 }
1072
1073 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001074 if (w.mHasSurface && !rotateSeamlessly) {
1075 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001076 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001077 mService.mRoot.mOrientationChangeComplete = false;
1078 w.mLastFreezeDuration = 0;
1079 }
1080 w.mReportOrientationChanged = true;
1081 }, true /* traverseTopToBottom */);
1082
1083 if (rotateSeamlessly) {
1084 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1085 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1086 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1087 }
1088
1089 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1090 final WindowManagerService.RotationWatcher rotationWatcher
1091 = mService.mRotationWatchers.get(i);
1092 if (rotationWatcher.mDisplayId == mDisplayId) {
1093 try {
1094 rotationWatcher.mWatcher.onRotationChanged(rotation);
1095 } catch (RemoteException e) {
1096 // Ignore
1097 }
1098 }
1099 }
1100
1101 // TODO (multi-display): Magnification is supported only for the default display.
1102 // Announce rotation only if we will not animate as we already have the
1103 // windows in final state. Otherwise, we make this call at the rotation end.
1104 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1105 && isDefaultDisplay) {
1106 mService.mAccessibilityController.onRotationChangedLocked(this);
1107 }
1108
1109 return true;
1110 }
1111
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001112 void configureDisplayPolicy() {
1113 mService.mPolicy.setInitialDisplaySize(getDisplay(),
1114 mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1115
1116 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
1117 }
1118
Andrii Kulian06d07d62017-03-14 11:11:47 -07001119 /**
1120 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1121 * changed.
1122 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1123 */
1124 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1125 // Use the effective "visual" dimensions based on current rotation
1126 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1127 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1128 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1129 int dw = realdw;
1130 int dh = realdh;
1131
1132 if (mAltOrientation) {
1133 if (realdw > realdh) {
1134 // Turn landscape into portrait.
1135 int maxw = (int)(realdh/1.3f);
1136 if (maxw < realdw) {
1137 dw = maxw;
1138 }
1139 } else {
1140 // Turn portrait into landscape.
1141 int maxh = (int)(realdw/1.3f);
1142 if (maxh < realdh) {
1143 dh = maxh;
1144 }
1145 }
1146 }
1147
1148 // Update application display metrics.
1149 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1150 mDisplayId);
1151 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1152 mDisplayId);
1153 mDisplayInfo.rotation = mRotation;
1154 mDisplayInfo.logicalWidth = dw;
1155 mDisplayInfo.logicalHeight = dh;
1156 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1157 mDisplayInfo.appWidth = appWidth;
1158 mDisplayInfo.appHeight = appHeight;
1159 if (isDefaultDisplay) {
1160 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1161 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1162 }
1163 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1164 if (mDisplayScalingDisabled) {
1165 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1166 } else {
1167 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1168 }
1169
1170 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1171 mDisplayInfo);
1172
1173 mBaseDisplayRect.set(0, 0, dw, dh);
1174
1175 if (isDefaultDisplay) {
1176 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1177 mCompatDisplayMetrics);
1178 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001179
1180 updateBounds();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001181 return mDisplayInfo;
1182 }
1183
1184 /**
1185 * Compute display configuration based on display properties and policy settings.
1186 * Do not call if mDisplayReady == false.
1187 */
1188 void computeScreenConfiguration(Configuration config) {
1189 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1190
1191 final int dw = displayInfo.logicalWidth;
1192 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001193 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1194 // TODO: Probably best to set this based on some setting in the display content object,
1195 // so the display can be configured for things like fullscreen.
1196 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001197
Andrii Kulian06d07d62017-03-14 11:11:47 -07001198 config.screenWidthDp =
1199 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1200 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1201 config.screenHeightDp =
1202 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1203 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001204
1205 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1206 final int leftInset = mTmpRect.left;
1207 final int topInset = mTmpRect.top;
1208 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001209 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1210 leftInset + displayInfo.appWidth /* right */,
1211 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001212 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1213 || displayInfo.rotation == Surface.ROTATION_270);
1214
1215 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1216 mDisplayMetrics.density, config);
1217
1218 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1219 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1220 ? Configuration.SCREENLAYOUT_ROUND_YES
1221 : Configuration.SCREENLAYOUT_ROUND_NO);
1222
1223 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1224 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1225 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1226 dh, mDisplayId);
1227 config.densityDpi = displayInfo.logicalDensityDpi;
1228
1229 config.colorMode =
1230 (displayInfo.isHdr()
1231 ? Configuration.COLOR_MODE_HDR_YES
1232 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001233 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001234 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1235 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1236
1237 // Update the configuration based on available input devices, lid switch,
1238 // and platform configuration.
1239 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1240 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1241 config.navigation = Configuration.NAVIGATION_NONAV;
1242
1243 int keyboardPresence = 0;
1244 int navigationPresence = 0;
1245 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1246 final int len = devices != null ? devices.length : 0;
1247 for (int i = 0; i < len; i++) {
1248 InputDevice device = devices[i];
1249 if (!device.isVirtual()) {
1250 final int sources = device.getSources();
1251 final int presenceFlag = device.isExternal() ?
1252 WindowManagerPolicy.PRESENCE_EXTERNAL :
1253 WindowManagerPolicy.PRESENCE_INTERNAL;
1254
1255 // TODO(multi-display): Configure on per-display basis.
1256 if (mService.mIsTouchDevice) {
1257 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1258 InputDevice.SOURCE_TOUCHSCREEN) {
1259 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1260 }
1261 } else {
1262 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1263 }
1264
1265 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1266 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1267 navigationPresence |= presenceFlag;
1268 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1269 && config.navigation == Configuration.NAVIGATION_NONAV) {
1270 config.navigation = Configuration.NAVIGATION_DPAD;
1271 navigationPresence |= presenceFlag;
1272 }
1273
1274 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1275 config.keyboard = Configuration.KEYBOARD_QWERTY;
1276 keyboardPresence |= presenceFlag;
1277 }
1278 }
1279 }
1280
1281 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1282 config.navigation = Configuration.NAVIGATION_DPAD;
1283 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1284 }
1285
1286 // Determine whether a hard keyboard is available and enabled.
1287 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1288 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1289 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1290 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1291 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1292 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1293 }
1294
1295 // Let the policy update hidden states.
1296 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1297 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1298 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1299 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1300 }
1301
1302 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1303 int displayId) {
1304 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1305 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1306 final int unrotDw, unrotDh;
1307 if (rotated) {
1308 unrotDw = dh;
1309 unrotDh = dw;
1310 } else {
1311 unrotDw = dw;
1312 unrotDh = dh;
1313 }
1314 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1315 displayId);
1316 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1317 displayId);
1318 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1319 displayId);
1320 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1321 displayId);
1322 return sw;
1323 }
1324
1325 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1326 DisplayMetrics dm, int dw, int dh, int displayId) {
1327 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1328 displayId);
1329 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1330 uiMode, displayId);
1331 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1332 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1333 if (curSize == 0 || size < curSize) {
1334 curSize = size;
1335 }
1336 return curSize;
1337 }
1338
1339 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1340 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1341
1342 // We need to determine the smallest width that will occur under normal
1343 // operation. To this, start with the base screen size and compute the
1344 // width under the different possible rotations. We need to un-rotate
1345 // the current screen dimensions before doing this.
1346 int unrotDw, unrotDh;
1347 if (rotated) {
1348 unrotDw = dh;
1349 unrotDh = dw;
1350 } else {
1351 unrotDw = dw;
1352 unrotDh = dh;
1353 }
1354 displayInfo.smallestNominalAppWidth = 1<<30;
1355 displayInfo.smallestNominalAppHeight = 1<<30;
1356 displayInfo.largestNominalAppWidth = 0;
1357 displayInfo.largestNominalAppHeight = 0;
1358 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1359 unrotDh);
1360 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1361 unrotDw);
1362 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1363 unrotDh);
1364 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1365 unrotDw);
1366 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1367 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1368 displayId);
1369 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1370 displayId);
1371 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1372 displayId);
1373 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1374 displayId);
1375 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1376 outConfig.screenLayout = sl;
1377 }
1378
1379 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1380 int uiMode, int displayId) {
1381 // Get the app screen size at this rotation.
1382 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1383 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1384
1385 // Compute the screen layout size class for this rotation.
1386 int longSize = w;
1387 int shortSize = h;
1388 if (longSize < shortSize) {
1389 int tmp = longSize;
1390 longSize = shortSize;
1391 shortSize = tmp;
1392 }
1393 longSize = (int)(longSize/density);
1394 shortSize = (int)(shortSize/density);
1395 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1396 }
1397
1398 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1399 int uiMode, int dw, int dh) {
1400 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1401 displayId);
1402 if (width < displayInfo.smallestNominalAppWidth) {
1403 displayInfo.smallestNominalAppWidth = width;
1404 }
1405 if (width > displayInfo.largestNominalAppWidth) {
1406 displayInfo.largestNominalAppWidth = width;
1407 }
1408 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1409 displayId);
1410 if (height < displayInfo.smallestNominalAppHeight) {
1411 displayInfo.smallestNominalAppHeight = height;
1412 }
1413 if (height > displayInfo.largestNominalAppHeight) {
1414 displayInfo.largestNominalAppHeight = height;
1415 }
1416 }
1417
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001418 DockedStackDividerController getDockedDividerController() {
1419 return mDividerControllerLocked;
1420 }
1421
Winson Chung655332c2016-10-31 13:14:28 -07001422 PinnedStackController getPinnedStackController() {
1423 return mPinnedStackControllerLocked;
1424 }
1425
Jeff Browna506a6e2013-06-04 00:02:38 -07001426 /**
1427 * Returns true if the specified UID has access to this display.
1428 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001429 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001430 return mDisplay.hasAccess(uid);
1431 }
1432
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001433 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001434 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001435 }
1436
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001437 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001438 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001439 }
1440
Wale Ogunwale61911492017-10-11 08:50:50 -07001441 /**
1442 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1443 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001444 TaskStack getSplitScreenPrimaryStack() {
1445 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001446 return (stack != null && stack.isVisible()) ? stack : null;
1447 }
1448
1449 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001450 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001451 * not visible.
1452 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001453 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1454 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001455 }
1456
1457 TaskStack getPinnedStack() {
1458 return mTaskStackContainers.getPinnedStack();
1459 }
1460
1461 private boolean hasPinnedStack() {
1462 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001463 }
1464
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001465 /**
1466 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1467 * Null is no compatible stack on the display.
1468 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001469 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001470 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1471 }
1472
1473 /**
1474 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1475 * activity type. Null is no compatible stack on the display.
1476 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001477 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001478 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001479 }
1480
Bryce Lee48f4b572017-04-10 10:54:15 -07001481 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001482 TaskStack getTopStack() {
1483 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001484 }
1485
Wale Ogunwale61911492017-10-11 08:50:50 -07001486 void onStackWindowingModeChanged(TaskStack stack) {
1487 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001488 }
1489
Andrii Kulian441e4492016-09-29 15:25:00 -07001490 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001491 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001492 super.onConfigurationChanged(newParentConfig);
1493
Andrii Kulian3a507b52016-09-19 18:14:12 -07001494 // The display size information is heavily dependent on the resources in the current
1495 // configuration, so we need to reconfigure it every time the configuration changes.
1496 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1497 mService.reconfigureDisplayLocked(this);
1498
Bryce Leef3c6a472017-11-14 14:53:06 -08001499 final DockedStackDividerController dividerController = getDockedDividerController();
1500
1501 if (dividerController != null) {
1502 getDockedDividerController().onConfigurationChanged();
1503 }
1504
1505 final PinnedStackController pinnedStackController = getPinnedStackController();
1506
1507 if (pinnedStackController != null) {
1508 getPinnedStackController().onConfigurationChanged();
1509 }
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 Ogunwale61911492017-10-11 08:50:50 -07001517 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1518 final TaskStack stack = mTaskStackContainers.getChildAt(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 Ogunwale61911492017-10-11 08:50:50 -07001527 if (!hasPinnedStack()) {
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 Ogunwale61911492017-10-11 08:50:50 -07001626 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1627 mTaskStackContainers.getChildAt(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
Andrii Kuliancd097992017-03-23 18:31:59 -07001648 /**
1649 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1650 * values.
1651 */
1652 private void updateBaseDisplayMetricsIfNeeded() {
1653 // Get real display metrics without overrides from WM.
1654 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1655 final int orientation = mDisplayInfo.rotation;
1656 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1657 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1658 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1659 final int newDensity = mDisplayInfo.logicalDensityDpi;
1660
1661 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1662 || mInitialDisplayHeight != newHeight
1663 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1664
1665 if (displayMetricsChanged) {
1666 // Check if display size or density is forced.
1667 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1668 || mBaseDisplayHeight != mInitialDisplayHeight;
1669 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1670
1671 // If there is an override set for base values - use it, otherwise use new values.
1672 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1673 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1674 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1675
1676 // Real display metrics changed, so we should also update initial values.
1677 mInitialDisplayWidth = newWidth;
1678 mInitialDisplayHeight = newHeight;
1679 mInitialDisplayDensity = newDensity;
1680 mService.reconfigureDisplayLocked(this);
1681 }
1682 }
1683
Bryce Lee27cec322017-03-21 09:41:37 -07001684 /** Sets the maximum width the screen resolution can be */
1685 void setMaxUiWidth(int width) {
1686 if (DEBUG_DISPLAY) {
1687 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1688 }
1689
1690 mMaxUiWidth = width;
1691
1692 // Update existing metrics.
1693 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1694 }
1695
1696 /** Update base (override) display metrics. */
1697 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1698 mBaseDisplayWidth = baseWidth;
1699 mBaseDisplayHeight = baseHeight;
1700 mBaseDisplayDensity = baseDensity;
1701
1702 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1703 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1704 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1705 mBaseDisplayWidth = mMaxUiWidth;
1706
1707 if (DEBUG_DISPLAY) {
1708 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1709 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1710 + " on display:" + getDisplayId());
1711 }
1712 }
1713
1714 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08001715
1716 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07001717 }
1718
Wale Ogunwaledb506192017-12-08 10:57:32 -08001719 void getStableRect(Rect out) {
1720 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08001721 }
1722
Wale Ogunwale61911492017-10-11 08:50:50 -07001723 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001724 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1725 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001726
Wale Ogunwale61911492017-10-11 08:50:50 -07001727 final TaskStack stack = new TaskStack(mService, stackId, controller);
1728 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001729 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001730 }
1731
Andrii Kulian51c1b672017-04-07 18:39:32 -07001732 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001733 final DisplayContent prevDc = stack.getDisplayContent();
1734 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001735 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1736 + " which is not currently attached to any display");
1737 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001738 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001739 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1740 + " to its current displayId=" + mDisplayId);
1741 }
1742
Wale Ogunwale61911492017-10-11 08:50:50 -07001743 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001744 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001745 }
1746
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001747 @Override
1748 protected void addChild(DisplayChildWindowContainer child,
1749 Comparator<DisplayChildWindowContainer> comparator) {
1750 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1751 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001752
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001753 @Override
1754 protected void addChild(DisplayChildWindowContainer child, int index) {
1755 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1756 }
1757
1758 @Override
1759 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001760 // Only allow removal of direct children from this display if the display is in the process
1761 // of been removed.
1762 if (mRemovingDisplay) {
1763 super.removeChild(child);
1764 return;
1765 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001766 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001767 }
1768
Andrii Kuliand2765632016-12-12 22:26:34 -08001769 @Override
1770 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1771 // Children of the display are statically ordered, so the real intention here is to perform
1772 // the operation on the display and not the static direct children.
1773 getParent().positionChildAt(position, this, includingParents);
1774 }
1775
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001776 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001777 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1778 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001779 final int taskId = stack.taskIdFromPoint(x, y);
1780 if (taskId != -1) {
1781 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001782 }
1783 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001784 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001785 }
1786
Chong Zhang8e89b312015-09-09 15:09:30 -07001787 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001788 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001789 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001790 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001791 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001792 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001793 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07001794 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1795 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001796 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001797 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001798 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001799
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001800 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1801 if (mTmpTaskForResizePointSearchResult.searchDone) {
1802 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001803 }
1804 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001805 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001806 }
1807
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001808 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001809 // The provided task is the task on this display with focus, so if WindowManagerService's
1810 // focused app is not on this display, focusedTask will be null.
1811 if (focusedTask == null) {
1812 mTouchExcludeRegion.setEmpty();
1813 } else {
1814 mTouchExcludeRegion.set(mBaseDisplayRect);
1815 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1816 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07001817 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1818 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001819 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
1820 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08001821 }
1822 // If we removed the focused task above, add it back and only leave its
1823 // outside touch area in the exclusion. TapDectector is not interested in
1824 // any touch inside the focused task itself.
1825 if (!mTmpRect2.isEmpty()) {
1826 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1827 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001828 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001829 final WindowState inputMethod = mService.mInputMethodWindow;
1830 if (inputMethod != null && inputMethod.isVisibleLw()) {
1831 // If the input method is visible and the user is typing, we don't want these touch
1832 // events to be intercepted and used to change focus. This would likely cause a
1833 // disappearance of the input method.
1834 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001835 if (inputMethod.getDisplayId() == mDisplayId) {
1836 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1837 } else {
1838 // IME is on a different display, so we need to update its tap detector.
1839 // TODO(multidisplay): Remove when IME will always appear on same display.
1840 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1841 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001842 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001843 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1844 WindowState win = mTapExcludedWindows.get(i);
1845 win.getTouchableRegion(mTmpRegion);
1846 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1847 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001848 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07001849 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001850 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001851 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001852 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1853 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001854 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001855 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001856 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001857 }
1858
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001859 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001860 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001861 super.switchUser();
1862 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001863 }
1864
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001865 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001866 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1867 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001868 }
1869 }
1870
Wale Ogunwale10124582016-09-15 20:25:50 -07001871 @Override
1872 void removeIfPossible() {
1873 if (isAnimating()) {
1874 mDeferredRemoval = true;
1875 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001876 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001877 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001878 }
1879
Wale Ogunwale10124582016-09-15 20:25:50 -07001880 @Override
1881 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001882 mRemovingDisplay = true;
1883 try {
1884 super.removeImmediately();
1885 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07001886 if (mService.canDispatchPointerEvents()) {
1887 if (mTapDetector != null) {
1888 mService.unregisterPointerEventListener(mTapDetector);
1889 }
1890 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
1891 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1892 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001893 }
Jorim Jaggia3844032018-01-03 15:54:43 +01001894 mService.mAnimator.removeDisplayLocked(mDisplayId);
1895
Robert Carrb1579c82017-09-05 14:54:47 -07001896 // The pending transaction won't be applied so we should
1897 // just clean up any surfaces pending destruction.
1898 onPendingTransactionApplied();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001899 } finally {
1900 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001901 }
Craig Mautner95da1082014-02-24 17:54:35 -08001902 }
1903
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001904 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001905 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001906 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001907 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1908
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001909 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001910 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001911 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001912 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001913 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001914 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001915 }
1916
Andrii Kulian0214ed92017-05-16 13:44:05 -07001917 /** @return 'true' if removal of this display content is deferred due to active animation. */
1918 boolean isRemovalDeferred() {
1919 return mDeferredRemoval;
1920 }
1921
Wale Ogunwale10124582016-09-15 20:25:50 -07001922 boolean animateForIme(float interpolatedValue, float animationTarget,
1923 float dividerAnimationTarget) {
1924 boolean updated = false;
1925
Wale Ogunwale61911492017-10-11 08:50:50 -07001926 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1927 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001928 if (stack == null || !stack.isAdjustedForIme()) {
1929 continue;
1930 }
1931
1932 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1933 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1934 updated = true;
1935 } else {
1936 mDividerControllerLocked.mLastAnimationProgress =
1937 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1938 mDividerControllerLocked.mLastDividerProgress =
1939 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1940 updated |= stack.updateAdjustForIme(
1941 mDividerControllerLocked.mLastAnimationProgress,
1942 mDividerControllerLocked.mLastDividerProgress,
1943 false /* force */);
1944 }
1945 if (interpolatedValue >= 1f) {
1946 stack.endImeAdjustAnimation();
1947 }
1948 }
1949
1950 return updated;
1951 }
1952
1953 boolean clearImeAdjustAnimation() {
1954 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07001955 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1956 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001957 if (stack != null && stack.isAdjustedForIme()) {
1958 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1959 changed = true;
1960 }
1961 }
1962 return changed;
1963 }
1964
1965 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001966 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1967 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001968 if (stack.isVisible() && stack.isAdjustedForIme()) {
1969 stack.beginImeAdjustAnimation();
1970 }
1971 }
1972 }
1973
1974 void adjustForImeIfNeeded() {
1975 final WindowState imeWin = mService.mInputMethodWindow;
1976 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
1977 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001978 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07001979 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
1980 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
1981 imeTargetStack.getDockSide() : DOCKED_INVALID;
1982 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
1983 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
1984 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001985 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07001986 final boolean imeHeightChanged = imeVisible &&
1987 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
1988
1989 // The divider could be adjusted for IME position, or be thinner than usual,
1990 // or both. There are three possible cases:
1991 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
1992 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
1993 // - If IME is not visible, divider is not moved and is normal width.
1994
1995 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001996 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1997 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001998 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07001999 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2000 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002001 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2002 } else {
2003 stack.resetAdjustedForIme(false);
2004 }
2005 }
2006 mDividerControllerLocked.setAdjustedForIme(
2007 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2008 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002009 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2010 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002011 stack.resetAdjustedForIme(!dockVisible);
2012 }
2013 mDividerControllerLocked.setAdjustedForIme(
2014 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2015 }
Winson Chung655332c2016-10-31 13:14:28 -07002016 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002017 }
2018
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002019 /**
2020 * If a window that has an animation specifying a colored background and the current wallpaper
2021 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2022 * suddenly disappear.
2023 */
2024 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002025 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2026 w -> w.mIsWallpaper && w.isVisibleNow());
2027
2028 if (visibleWallpaper != null) {
2029 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002030 }
2031 return winAnimator.mAnimLayer;
2032 }
2033
Wale Ogunwale10124582016-09-15 20:25:50 -07002034 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002035 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2036 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002037 stack.prepareFreezingTaskBounds();
2038 }
2039 }
2040
Wale Ogunwale94744212015-09-21 19:01:47 -07002041 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002042 getBounds(mTmpRect, newRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002043
2044 // Compute a transform matrix to undo the coordinate space transformation,
2045 // and present the window at the same physical position it previously occupied.
2046 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2047 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2048
2049 mTmpRectF.set(bounds);
2050 mTmpMatrix.mapRect(mTmpRectF);
2051 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002052 }
2053
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002054 static int deltaRotation(int oldRotation, int newRotation) {
2055 int delta = newRotation - oldRotation;
2056 if (delta < 0) delta += 4;
2057 return delta;
2058 }
2059
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002060 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002061 Matrix outMatrix) {
2062 // For rotations without Z-ordering we don't need the target rectangle's position.
2063 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2064 displayHeight, outMatrix);
2065 }
2066
2067 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2068 float displayWidth, float displayHeight, Matrix outMatrix) {
2069 switch (rotation) {
2070 case ROTATION_0:
2071 outMatrix.reset();
2072 break;
2073 case ROTATION_270:
2074 outMatrix.setRotate(270, 0, 0);
2075 outMatrix.postTranslate(0, displayHeight);
2076 outMatrix.postTranslate(rectTop, 0);
2077 break;
2078 case ROTATION_180:
2079 outMatrix.reset();
2080 break;
2081 case ROTATION_90:
2082 outMatrix.setRotate(90, 0, 0);
2083 outMatrix.postTranslate(displayWidth, 0);
2084 outMatrix.postTranslate(-rectTop, rectLeft);
2085 break;
2086 }
2087 }
2088
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002089 @CallSuper
2090 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002091 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002092 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002093 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002094 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002095 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2096 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002097 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002098 }
2099 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2100 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002101 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2102 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002103 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002104 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002105 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2106 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002107 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002108 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002109 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2110 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002111 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002112 }
2113 proto.write(DPI, mBaseDisplayDensity);
2114 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002115 proto.write(ROTATION, mRotation);
2116 final ScreenRotationAnimation screenRotationAnimation =
2117 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2118 if (screenRotationAnimation != null) {
2119 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2120 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002121 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002122 proto.end(token);
2123 }
2124
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002125 @Override
2126 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2127 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002128 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2129 final String subPrefix = " " + prefix;
2130 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2131 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2132 pw.print("dpi");
2133 if (mInitialDisplayWidth != mBaseDisplayWidth
2134 || mInitialDisplayHeight != mBaseDisplayHeight
2135 || mInitialDisplayDensity != mBaseDisplayDensity) {
2136 pw.print(" base=");
2137 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2138 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2139 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002140 if (mDisplayScalingDisabled) {
2141 pw.println(" noscale");
2142 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002143 pw.print(" cur=");
2144 pw.print(mDisplayInfo.logicalWidth);
2145 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2146 pw.print(" app=");
2147 pw.print(mDisplayInfo.appWidth);
2148 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2149 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2150 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2151 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2152 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002153 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002154 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002155 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002156
Craig Mautnerdc548482014-02-05 13:35:24 -08002157 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002158 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002159 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2160 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002161 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002162 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002163
Craig Mautnerdc548482014-02-05 13:35:24 -08002164 pw.println();
2165 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002166 pw.println();
2167 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002168 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002169 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002170 pw.print(" Exiting #"); pw.print(i);
2171 pw.print(' '); pw.print(token);
2172 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002173 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002174 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002175 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002176
Jorim Jaggi31f71702016-05-04 16:43:04 -07002177 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002178
2179 // Dump stack references
2180 final TaskStack homeStack = getHomeStack();
2181 if (homeStack != null) {
2182 pw.println(prefix + "homeStack=" + homeStack.getName());
2183 }
2184 final TaskStack pinnedStack = getPinnedStack();
2185 if (pinnedStack != null) {
2186 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2187 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002188 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002189 if (splitScreenPrimaryStack != null) {
2190 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2191 }
2192
2193 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002194 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002195 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002196 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002197
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002198 pw.println();
2199 mDisplayFrames.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002200 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002201
2202 @Override
2203 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002204 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002205 }
2206
2207 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002208 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002209 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002210
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002211 /** Returns true if the stack in the windowing mode is visible. */
2212 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002213 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002214 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002215 }
2216
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002217 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002218 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002219 final int x = (int) xf;
2220 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002221 final WindowState touchedWin = getWindow(w -> {
2222 final int flags = w.mAttrs.flags;
2223 if (!w.isVisibleLw()) {
2224 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002225 }
2226 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002227 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002228 }
2229
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002230 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002231 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002232 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002233 }
2234
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002235 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002236
2237 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002238 return mTmpRegion.contains(x, y) || touchFlags == 0;
2239 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002240
2241 return touchedWin;
2242 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002243
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002244 boolean canAddToastWindowForUid(int uid) {
2245 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002246 // Also if the app is focused adding more than one toast at
2247 // a time for better backwards compatibility.
2248 final WindowState focusedWindowForUid = getWindow(w ->
2249 w.mOwnerUid == uid && w.isFocused());
2250 if (focusedWindowForUid != null) {
2251 return true;
2252 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002253 final WindowState win = getWindow(w ->
2254 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2255 && !w.mWindowRemovalAllowed);
2256 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002257 }
2258
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002259 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002260 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2261 return;
2262 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002263
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002264 // Used to communicate the old focus to the callback method.
2265 mTmpWindow = oldFocus;
2266
2267 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002268 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002269
2270 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002271 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002272
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002273 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002274
2275 if (mTmpWindow == null) {
2276 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2277 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002278 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002279 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002280 }
2281
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002282 /** Updates the layer assignment of windows on this display. */
2283 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002284 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002285 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002286 if (setLayoutNeeded) {
2287 setLayoutNeeded();
2288 }
Robert Carrb1579c82017-09-05 14:54:47 -07002289
Robert Carrf59b8dd2017-10-02 18:58:36 -07002290 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002291 // the application of this transaction until the animation pass triggers
2292 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2293 // the hiding and showing of surfaces.
2294 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002295 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002296 }
2297
Wale Ogunwale1666e312016-12-16 11:27:18 -08002298 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2299 // moving containers or resizing them. Need to investigate the best way to have it automatically
2300 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002301 void layoutAndAssignWindowLayersIfNeeded() {
2302 mService.mWindowsChanged = true;
2303 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002304
2305 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2306 false /*updateInputWindows*/)) {
2307 assignWindowLayers(false /* setLayoutNeeded */);
2308 }
2309
2310 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2311 mService.mWindowPlacerLocked.performSurfacePlacement();
2312 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2313 }
2314
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002315 /** Returns true if a leaked surface was destroyed */
2316 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002317 // Used to indicate that a surface was leaked.
2318 mTmpWindow = null;
2319 forAllWindows(w -> {
2320 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002321 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002322 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002323 }
2324 if (!mService.mSessions.contains(wsa.mSession)) {
2325 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002326 + w + " surface=" + wsa.mSurfaceController
2327 + " token=" + w.mToken
2328 + " pid=" + w.mSession.mPid
2329 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002330 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002331 mService.mForceRemoves.add(w);
2332 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002333 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002334 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002335 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002336 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002337 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002338 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002339 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002340 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002341 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002342
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002343 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002344 }
2345
2346 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002347 * Determine and return the window that should be the IME target.
2348 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2349 * @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 +00002350 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002351 WindowState computeImeTarget(boolean updateImeTarget) {
2352 if (mService.mInputMethodWindow == null) {
2353 // There isn't an IME so there shouldn't be a target...That was easy!
2354 if (updateImeTarget) {
2355 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2356 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002357 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002358 }
2359 return null;
2360 }
2361
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002362 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2363 // same display. Or even when the current IME/target are not on the same screen as the next
2364 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002365 mUpdateImeTarget = updateImeTarget;
2366 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002367
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002368
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002369 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2370 // to be on top of it, but it is not -really- where input will go. So look down below
2371 // for a real window to target...
2372 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2373 final AppWindowToken token = target.mAppToken;
2374 if (token != null) {
2375 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2376 if (betterTarget != null) {
2377 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002378 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002379 }
2380 }
2381
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002382 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2383 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002384
2385 // Now, a special case -- if the last target's window is in the process of exiting, and is
2386 // above the new target, keep on the last target to avoid flicker. Consider for example a
2387 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2388 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2389 // scrim.
2390 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002391 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2392 && (target == null
2393 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002394 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002395 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002396 }
2397
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002398 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2399 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002400
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002401 if (target == null) {
2402 if (updateImeTarget) {
2403 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2404 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2405 + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002406 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002407 }
2408
2409 return null;
2410 }
2411
2412 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002413 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2414 if (token != null) {
2415
2416 // Now some fun for dealing with window animations that modify the Z order. We need
2417 // to look at all windows below the current target that are in this app, finding the
2418 // highest visible one in layering.
2419 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002420 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002421 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002422 }
2423
2424 if (highestTarget != null) {
2425 final AppTransition appTransition = mService.mAppTransition;
2426 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2427 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2428 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002429 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002430
2431 if (appTransition.isTransitionSet()) {
2432 // If we are currently setting up for an animation, hold everything until we
2433 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002434 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002435 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002436 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002437 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002438 // If the window we are currently targeting is involved with an animation,
2439 // and it is on top of the next target we will be over, then hold off on
2440 // moving until that is done.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002441 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002442 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002443 }
2444 }
2445 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002446
2447 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2448 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002449 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002450 }
2451
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002452 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002453 }
2454
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002455 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002456 if (target == mService.mInputMethodTarget
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002457 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002458 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002459 }
2460
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002461 mService.mInputMethodTarget = target;
2462 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002463 assignWindowLayers(false /* setLayoutNeeded */);
2464 }
2465
2466 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2467 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2468 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2469 }
2470
2471 // Used to indicate we have reached the first window in the range we are interested in.
2472 mTmpWindow = null;
2473
2474 // TODO: Figure-out a more efficient way to do this.
2475 final WindowState candidate = getWindow(w -> {
2476 if (w == top) {
2477 // Reached the first window in the range we are interested in.
2478 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002479 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002480 if (mTmpWindow == null) {
2481 return false;
2482 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002483
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002484 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2485 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002486 }
2487 // If we reached the bottom of the range of windows we are considering,
2488 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002489 if (w == bottom) {
2490 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002491 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002492 return false;
2493 });
2494
2495 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002496 }
2497
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002498 void setLayoutNeeded() {
2499 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2500 mLayoutNeeded = true;
2501 }
2502
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002503 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002504 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2505 mLayoutNeeded = false;
2506 }
2507
2508 boolean isLayoutNeeded() {
2509 return mLayoutNeeded;
2510 }
2511
Wale Ogunwale02319a62016-09-26 15:21:22 -07002512 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2513 if (mTokenMap.isEmpty()) {
2514 return;
2515 }
2516 pw.println(" Display #" + mDisplayId);
2517 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2518 while (it.hasNext()) {
2519 final WindowToken token = it.next();
2520 pw.print(" ");
2521 pw.print(token);
2522 if (dumpAll) {
2523 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002524 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07002525 } else {
2526 pw.println();
2527 }
2528 }
2529 }
2530
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002531 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002532 final int[] index = new int[1];
2533 forAllWindows(w -> {
2534 final WindowStateAnimator wAnim = w.mWinAnimator;
2535 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2536 index[0] = index[0] + 1;
2537 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002538 }
2539
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002540 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002541 forAllWindows(w -> {
2542 w.mWinAnimator.enableSurfaceTrace(fd);
2543 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002544 }
2545
2546 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002547 forAllWindows(w -> {
2548 w.mWinAnimator.disableSurfaceTrace();
2549 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002550 }
2551
Jorim Jaggife762342016-10-13 14:33:27 +02002552 /**
2553 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2554 */
2555 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2556 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002557 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002558 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2559 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002560 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02002561 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002562 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002563 }
2564
Wale Ogunwale494009b82016-10-21 09:01:38 -07002565 boolean checkWaitingForWindows() {
2566
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002567 mHaveBootMsg = false;
2568 mHaveApp = false;
2569 mHaveWallpaper = false;
2570 mHaveKeyguard = true;
2571
2572 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002573 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2574 return true;
2575 }
2576 if (w.isDrawnLw()) {
2577 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002578 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002579 } else if (w.mAttrs.type == TYPE_APPLICATION
2580 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002581 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002582 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002583 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002584 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002585 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002586 }
2587 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002588 return false;
2589 });
2590
2591 if (visibleWindow != null) {
2592 // We have a visible window.
2593 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002594 }
2595
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002596 // if the wallpaper service is disabled on the device, we're never going to have
2597 // wallpaper, don't bother waiting for it
2598 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2599 com.android.internal.R.bool.config_enableWallpaperService)
2600 && !mService.mOnlyCore;
2601
Wale Ogunwale494009b82016-10-21 09:01:38 -07002602 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2603 "******** booted=" + mService.mSystemBooted
2604 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002605 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2606 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2607 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002608
2609 // If we are turning on the screen to show the boot message, don't do it until the boot
2610 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002611 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002612 return true;
2613 }
2614
2615 // If we are turning on the screen after the boot is completed normally, don't do so until
2616 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002617 if (mService.mSystemBooted
2618 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002619 return true;
2620 }
2621
2622 return false;
2623 }
2624
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002625 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002626 mTmpWindowAnimator = animator;
2627 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002628 }
2629
2630 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002631 resetAnimationBackgroundAnimator();
2632
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002633 // Used to indicate a detached wallpaper.
2634 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002635 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002636
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002637 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002638
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002639 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002640 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002641 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2642 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002643 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2644 }
2645 }
2646
Wale Ogunwale494009b82016-10-21 09:01:38 -07002647 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002648 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002649 if (imFocus == null) {
2650 return false;
2651 }
2652
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002653 if (DEBUG_INPUT_METHOD) {
2654 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2655 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2656 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002657 }
2658
Wale Ogunwale494009b82016-10-21 09:01:38 -07002659 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2660
2661 if (DEBUG_INPUT_METHOD) {
2662 Slog.i(TAG_WM, "IM target client: " + imeClient);
2663 if (imeClient != null) {
2664 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2665 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2666 }
2667 }
2668
2669 return imeClient != null && imeClient.asBinder() == client.asBinder();
2670 }
2671
2672 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002673 final WindowState win = getWindow(
2674 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2675 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002676 }
2677
2678 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002679 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002680 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002681 final int curValue = w.mSystemUiVisibility;
2682 final int diff = (curValue ^ visibility) & globalDiff;
2683 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002684 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002685 w.mSeq++;
2686 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002687 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002688 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2689 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002690 visibility, newValue, diff);
2691 }
2692 } catch (RemoteException e) {
2693 // so sorry
2694 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002695 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002696 }
2697
2698 void onWindowFreezeTimeout() {
2699 Slog.w(TAG_WM, "Window freeze timeout expired.");
2700 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002701
2702 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002703 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002704 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002705 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002706 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002707 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2708 - mService.mDisplayFreezeTime);
2709 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002710 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002711 mService.mWindowPlacerLocked.performSurfacePlacement();
2712 }
2713
2714 void waitForAllWindowsDrawn() {
2715 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002716 forAllWindows(w -> {
2717 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2718 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2719 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002720 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002721 w.mLastContentInsets.set(-1, -1, -1, -1);
2722 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002723 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002724 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002725 }
2726
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002727 // TODO: Super crazy long method that should be broken down...
2728 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2729
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002730 final int dw = mDisplayInfo.logicalWidth;
2731 final int dh = mDisplayInfo.logicalHeight;
2732 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2733
2734 mTmpUpdateAllDrawn.clear();
2735
2736 int repeats = 0;
2737 do {
2738 repeats++;
2739 if (repeats > 6) {
2740 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2741 clearLayoutNeeded();
2742 break;
2743 }
2744
2745 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2746 pendingLayoutChanges);
2747
Andrii Kulian839def92016-11-02 10:58:58 -07002748 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2749 // the wallpaper window jumping across displays.
2750 // Remove check for default display when there will be support for multiple wallpaper
2751 // targets (on different displays).
2752 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002753 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002754 }
2755
2756 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2757 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2758 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2759 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002760 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002761 }
2762 }
2763
2764 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2765 setLayoutNeeded();
2766 }
2767
2768 // FIRST LOOP: Perform a layout, if needed.
2769 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2770 performLayout(repeats == 1, false /* updateInputWindows */);
2771 } else {
2772 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2773 }
2774
2775 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2776 pendingLayoutChanges = 0;
2777
2778 if (isDefaultDisplay) {
2779 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002780 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002781 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2782 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2783 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2784 }
2785 } while (pendingLayoutChanges != 0);
2786
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002787 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002788
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002789 mTmpRecoveringMemory = recoveringMemory;
2790 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01002791 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002792
2793 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002794 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2795 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2796 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002797 true /* inTraversal, must call performTraversalInTrans... below */);
2798
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002799 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2800 if (wallpaperVisible != mLastWallpaperVisible) {
2801 mLastWallpaperVisible = wallpaperVisible;
2802 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2803 }
2804
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002805 while (!mTmpUpdateAllDrawn.isEmpty()) {
2806 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2807 // See if any windows have been drawn, so they (and others associated with them)
2808 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002809 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002810 }
2811
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002812 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002813 }
2814
Bryce Leef3c6a472017-11-14 14:53:06 -08002815 private void updateBounds() {
2816 calculateBounds(mTmpBounds);
2817 setBounds(mTmpBounds);
2818 }
2819
2820 // Determines the current display bounds based on the current state
2821 private void calculateBounds(Rect out) {
2822 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
2823 final int orientation = mDisplayInfo.rotation;
2824 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2825 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
2826 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
2827 int width = mDisplayInfo.logicalWidth;
2828 int left = (physWidth - width) / 2;
2829 int height = mDisplayInfo.logicalHeight;
2830 int top = (physHeight - height) / 2;
2831 out.set(left, top, left + width, top + height);
2832 }
2833
2834 @Override
2835 public void getBounds(Rect out) {
2836 calculateBounds(out);
2837 }
2838
2839 private void getBounds(Rect out, int orientation) {
2840 getBounds(out);
2841
2842 // Rotate the Rect if needed.
2843 final int currentRotation = mDisplayInfo.rotation;
2844 final int rotationDelta = deltaRotation(currentRotation, orientation);
2845 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
2846 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
2847 mTmpRectF.set(out);
2848 mTmpMatrix.mapRect(mTmpRectF);
2849 mTmpRectF.round(out);
2850 }
2851 }
2852
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002853 void performLayout(boolean initial, boolean updateInputWindows) {
2854 if (!isLayoutNeeded()) {
2855 return;
2856 }
2857 clearLayoutNeeded();
2858
2859 final int dw = mDisplayInfo.logicalWidth;
2860 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002861 if (DEBUG_LAYOUT) {
2862 Slog.v(TAG, "-------------------------------------");
2863 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2864 }
2865
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002866 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
2867 // TODO: Not sure if we really need to set the rotation here since we are updating from the
2868 // display info above...
2869 mDisplayFrames.mRotation = mRotation;
2870 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002871 if (isDefaultDisplay) {
2872 // Not needed on non-default displays.
2873 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2874 mService.mScreenRect.set(0, 0, dw, dh);
2875 }
2876
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002877 int seq = mService.mLayoutSeq + 1;
2878 if (seq < 0) seq = 0;
2879 mService.mLayoutSeq = seq;
2880
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002881 // Used to indicate that we have processed the dream window and all additional windows are
2882 // behind it.
2883 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002884 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002885
2886 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002887 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002888
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002889 // Used to indicate that we have processed the dream window and all additional attached
2890 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002891 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002892 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002893
2894 // Now perform layout of attached windows, which usually depend on the position of the
2895 // window they are attached to. XXX does not deal with windows that are attached to windows
2896 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002897 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002898
2899 // Window frames may have changed. Tell the input dispatcher about it.
2900 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2901 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2902 if (updateInputWindows) {
2903 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2904 }
2905
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002906 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2907 }
2908
2909 /**
2910 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2911 * In portrait mode, it grabs the full screenshot.
2912 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002913 * @param config of the output bitmap
2914 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
2915 */
chaviwfbe47df2017-11-10 16:14:49 -08002916 Bitmap screenshotDisplay(Bitmap.Config config, boolean wallpaperOnly) {
2917 synchronized (mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02002918 if (!mService.mPolicy.isScreenOn()) {
chaviwfbe47df2017-11-10 16:14:49 -08002919 if (DEBUG_SCREENSHOT) {
2920 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002921 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002922 return null;
2923 }
2924
chaviwfbe47df2017-11-10 16:14:49 -08002925 if (wallpaperOnly && !shouldScreenshotWallpaper()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002926 return null;
2927 }
2928
chaviwfbe47df2017-11-10 16:14:49 -08002929 int dw = mDisplayInfo.logicalWidth;
2930 int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002931
chaviwfbe47df2017-11-10 16:14:49 -08002932 if (dw <= 0 || dh <= 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002933 return null;
2934 }
2935
chaviwfbe47df2017-11-10 16:14:49 -08002936 final Rect frame = new Rect(0, 0, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002937
2938 // The screenshot API does not apply the current screen rotation.
2939 int rot = mDisplay.getRotation();
2940
2941 if (rot == ROTATION_90 || rot == ROTATION_270) {
2942 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
2943 }
2944
chaviwfbe47df2017-11-10 16:14:49 -08002945 // SurfaceFlinger is not aware of orientation, so convert our logical
2946 // crop to SurfaceFlinger's portrait orientation.
2947 convertCropForSurfaceFlinger(frame, rot, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002948
2949 final ScreenRotationAnimation screenRotationAnimation =
2950 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
2951 final boolean inRotation = screenRotationAnimation != null &&
2952 screenRotationAnimation.isAnimating();
chaviwfbe47df2017-11-10 16:14:49 -08002953 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002954
Robert Carrb1579c82017-09-05 14:54:47 -07002955 // TODO(b/68392460): We should screenshot Task controls directly
2956 // but it's difficult at the moment as the Task doesn't have the
2957 // correct size set.
chaviwfbe47df2017-11-10 16:14:49 -08002958 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, 0, 1, inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002959 if (bitmap == null) {
Robert Carrb1579c82017-09-05 14:54:47 -07002960 Slog.w(TAG_WM, "Failed to take screenshot");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002961 return null;
2962 }
chaviwfbe47df2017-11-10 16:14:49 -08002963
2964 // Create a copy of the screenshot that is immutable and backed in ashmem.
2965 // This greatly reduces the overhead of passing the bitmap between processes.
2966 final Bitmap ret = bitmap.createAshmemBitmap(config);
2967 bitmap.recycle();
2968 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002969 }
chaviwfbe47df2017-11-10 16:14:49 -08002970 }
2971
2972 private boolean shouldScreenshotWallpaper() {
2973 MutableBoolean screenshotReady = new MutableBoolean(false);
2974
2975 forAllWindows(w -> {
2976 if (!w.mIsWallpaper) {
2977 return false;
2978 }
2979
2980 // Found the wallpaper window
2981 final WindowStateAnimator winAnim = w.mWinAnimator;
2982
2983 if (winAnim.getShown() && winAnim.mLastAlpha > 0f) {
2984 screenshotReady.value = true;
2985 }
2986
2987 return true;
2988 }, true /* traverseTopToBottom */);
2989
2990 return screenshotReady.value;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002991 }
2992
2993 // TODO: Can this use createRotationMatrix()?
2994 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
2995 if (rot == Surface.ROTATION_90) {
2996 final int tmp = crop.top;
2997 crop.top = dw - crop.right;
2998 crop.right = crop.bottom;
2999 crop.bottom = dw - crop.left;
3000 crop.left = tmp;
3001 } else if (rot == Surface.ROTATION_180) {
3002 int tmp = crop.top;
3003 crop.top = dh - crop.bottom;
3004 crop.bottom = dh - tmp;
3005 tmp = crop.right;
3006 crop.right = dw - crop.left;
3007 crop.left = dw - tmp;
3008 } else if (rot == Surface.ROTATION_270) {
3009 final int tmp = crop.top;
3010 crop.top = crop.left;
3011 crop.left = dh - crop.bottom;
3012 crop.bottom = crop.right;
3013 crop.right = dh - tmp;
3014 }
3015 }
3016
3017 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003018 // Used to indicate the layout is needed.
3019 mTmpWindow = null;
3020
3021 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003022 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003023 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003024 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003025 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003026 w.setDisplayLayoutNeeded();
3027 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003028 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003029
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003030 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003031 mService.mWindowPlacerLocked.performSurfacePlacement();
3032 }
3033 }
3034
Wale Ogunwale1666e312016-12-16 11:27:18 -08003035 void setExitingTokensHasVisible(boolean hasVisible) {
3036 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3037 mExitingTokens.get(i).hasVisible = hasVisible;
3038 }
3039
3040 // Initialize state of exiting applications.
3041 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3042 }
3043
3044 void removeExistingTokensIfPossible() {
3045 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3046 final WindowToken token = mExitingTokens.get(i);
3047 if (!token.hasVisible) {
3048 mExitingTokens.remove(i);
3049 }
3050 }
3051
3052 // Time to remove any exiting applications?
3053 mTaskStackContainers.removeExistingAppTokensIfPossible();
3054 }
3055
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003056 @Override
3057 void onDescendantOverrideConfigurationChanged() {
3058 setLayoutNeeded();
3059 mService.requestTraversal();
3060 }
3061
David Stevens9440dc82017-03-16 19:00:20 -07003062 boolean okToDisplay() {
3063 if (mDisplayId == DEFAULT_DISPLAY) {
3064 return !mService.mDisplayFrozen
3065 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3066 }
3067 return mDisplayInfo.state == Display.STATE_ON;
3068 }
3069
3070 boolean okToAnimate() {
3071 return okToDisplay() &&
3072 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3073 }
3074
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003075 static final class TaskForResizePointSearchResult {
3076 boolean searchDone;
3077 Task taskForResize;
3078
3079 void reset() {
3080 searchDone = false;
3081 taskForResize = null;
3082 }
3083 }
Robert Carr3b716242016-08-16 16:02:21 -07003084
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003085 private static final class ApplySurfaceChangesTransactionState {
3086 boolean displayHasContent;
3087 boolean obscured;
3088 boolean syswin;
3089 boolean focusDisplayed;
3090 float preferredRefreshRate;
3091 int preferredModeId;
3092
3093 void reset() {
3094 displayHasContent = false;
3095 obscured = false;
3096 syswin = false;
3097 focusDisplayed = false;
3098 preferredRefreshRate = 0;
3099 preferredModeId = 0;
3100 }
3101 }
3102
3103 private static final class ScreenshotApplicationState {
3104 WindowState appWin;
3105 int maxLayer;
3106 int minLayer;
3107 boolean screenshotReady;
3108
3109 void reset(boolean screenshotReady) {
3110 appWin = null;
3111 maxLayer = 0;
3112 minLayer = 0;
3113 this.screenshotReady = screenshotReady;
3114 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3115 }
3116 }
3117
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003118 /**
3119 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3120 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3121 * homogeneous children type which is currently required by sub-classes of
3122 * {@link WindowContainer} class.
3123 */
3124 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3125
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003126 DisplayChildWindowContainer(WindowManagerService service) {
3127 super(service);
3128 }
3129
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003130 @Override
3131 boolean fillsParent() {
3132 return true;
3133 }
3134
3135 @Override
3136 boolean isVisible() {
3137 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003138 }
3139 }
3140
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003141 /**
3142 * Window container class that contains all containers on this display relating to Apps.
3143 * I.e Activities.
3144 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003145 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003146 /**
3147 * A control placed at the appropriate level for transitions to occur.
3148 */
chaviw23ee71c2017-12-18 11:29:41 -08003149 SurfaceControl mAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003150
Wale Ogunwale61911492017-10-11 08:50:50 -07003151 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3152 // through the list to find them.
3153 private TaskStack mHomeStack = null;
3154 private TaskStack mPinnedStack = null;
3155 private TaskStack mSplitScreenPrimaryStack = null;
3156
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003157 TaskStackContainers(WindowManagerService service) {
3158 super(service);
3159 }
3160
Wale Ogunwale61911492017-10-11 08:50:50 -07003161 /**
3162 * Returns the topmost stack on the display that is compatible with the input windowing mode
3163 * and activity type. Null is no compatible stack on the display.
3164 */
3165 TaskStack getStack(int windowingMode, int activityType) {
3166 if (activityType == ACTIVITY_TYPE_HOME) {
3167 return mHomeStack;
3168 }
3169 if (windowingMode == WINDOWING_MODE_PINNED) {
3170 return mPinnedStack;
3171 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3172 return mSplitScreenPrimaryStack;
3173 }
3174 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3175 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003176 if (activityType == ACTIVITY_TYPE_UNDEFINED
3177 && windowingMode == stack.getWindowingMode()) {
3178 // Passing in undefined type means we want to match the topmost stack with the
3179 // windowing mode.
3180 return stack;
3181 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003182 if (stack.isCompatible(windowingMode, activityType)) {
3183 return stack;
3184 }
3185 }
3186 return null;
3187 }
3188
3189 @VisibleForTesting
3190 TaskStack getTopStack() {
3191 return mTaskStackContainers.getChildCount() > 0
3192 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3193 }
3194
3195 TaskStack getHomeStack() {
3196 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3197 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3198 }
3199 return mHomeStack;
3200 }
3201
3202 TaskStack getPinnedStack() {
3203 return mPinnedStack;
3204 }
3205
Matthew Ng64e77cf2017-10-31 14:01:31 -07003206 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003207 return mSplitScreenPrimaryStack;
3208 }
3209
Andrii Kulian839def92016-11-02 10:58:58 -07003210 /**
3211 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003212 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003213 */
3214 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003215 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003216 addChild(stack, onTop);
3217 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003218 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003219
Wale Ogunwale61911492017-10-11 08:50:50 -07003220 void onStackWindowingModeChanged(TaskStack stack) {
3221 removeStackReferenceIfNeeded(stack);
3222 addStackReferenceIfNeeded(stack);
3223 if (stack == mPinnedStack && getTopStack() != stack) {
3224 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3225 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3226 }
3227 }
3228
3229 private void addStackReferenceIfNeeded(TaskStack stack) {
3230 if (stack.isActivityTypeHome()) {
3231 if (mHomeStack != null) {
3232 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
3233 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
3234 }
3235 mHomeStack = stack;
3236 }
3237 final int windowingMode = stack.getWindowingMode();
3238 if (windowingMode == WINDOWING_MODE_PINNED) {
3239 if (mPinnedStack != null) {
3240 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3241 + mPinnedStack + " already exist on display=" + this
3242 + " stack=" + stack);
3243 }
3244 mPinnedStack = stack;
3245 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3246 if (mSplitScreenPrimaryStack != null) {
3247 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3248 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3249 + " already exist on display=" + this + " stack=" + stack);
3250 }
3251 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003252 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003253 }
3254 }
3255
3256 private void removeStackReferenceIfNeeded(TaskStack stack) {
3257 if (stack == mHomeStack) {
3258 mHomeStack = null;
3259 } else if (stack == mPinnedStack) {
3260 mPinnedStack = null;
3261 } else if (stack == mSplitScreenPrimaryStack) {
3262 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003263 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3264 mService.setDockedStackCreateStateLocked(
3265 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3266 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003267 }
Andrii Kulian839def92016-11-02 10:58:58 -07003268 }
3269
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003270 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003271 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3272 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003273 addChild(stack, addIndex);
3274 setLayoutNeeded();
3275 }
3276
Wale Ogunwale61911492017-10-11 08:50:50 -07003277 @Override
3278 protected void removeChild(TaskStack stack) {
3279 super.removeChild(stack);
3280 removeStackReferenceIfNeeded(stack);
3281 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003282
3283 @Override
3284 boolean isOnTop() {
3285 // Considered always on top
3286 return true;
3287 }
3288
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003289 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003290 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003291 if (child.getWindowConfiguration().isAlwaysOnTop()
3292 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003293 // This stack is always-on-top, override the default behavior.
3294 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3295
3296 // Moving to its current position, as we must call super but we don't want to
3297 // perform any meaningful action.
3298 final int currentPosition = mChildren.indexOf(child);
3299 super.positionChildAt(currentPosition, child, false /* includingParents */);
3300 return;
3301 }
3302
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003303 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3304 super.positionChildAt(targetPosition, child, includingParents);
3305
3306 setLayoutNeeded();
3307 }
3308
3309 /**
3310 * When stack is added or repositioned, find a proper position for it.
3311 * This will make sure that pinned stack always stays on top.
3312 * @param requestedPosition Position requested by caller.
3313 * @param stack Stack to be added or positioned.
3314 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3315 * @return The proper position for the stack.
3316 */
3317 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3318 final int topChildPosition = mChildren.size() - 1;
3319 boolean toTop = requestedPosition == POSITION_TOP;
3320 toTop |= adding ? requestedPosition >= topChildPosition + 1
3321 : requestedPosition >= topChildPosition;
3322 int targetPosition = requestedPosition;
3323
Wale Ogunwale61911492017-10-11 08:50:50 -07003324 if (toTop && stack.getWindowingMode() != WINDOWING_MODE_PINNED && hasPinnedStack()) {
Bryce Lee48f4b572017-04-10 10:54:15 -07003325 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003326 TaskStack topStack = mChildren.get(topChildPosition);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003327 if (topStack.getWindowingMode() != WINDOWING_MODE_PINNED) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003328 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3329 }
3330
3331 // So, stack is moved just below the pinned stack.
3332 // When we're adding a new stack the target is the current pinned stack position.
3333 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003334 // stack, because WindowContainer#positionAt() first removes element and then adds
3335 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003336 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3337 }
3338
3339 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003340 }
3341
3342 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003343 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3344 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003345 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003346 if (super.forAllWindows(callback, traverseTopToBottom)) {
3347 return true;
3348 }
3349 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3350 return true;
3351 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003352 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003353 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3354 return true;
3355 }
3356 if (super.forAllWindows(callback, traverseTopToBottom)) {
3357 return true;
3358 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003359 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003360 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003361 }
3362
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003363 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003364 boolean traverseTopToBottom) {
3365 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3366 // app tokens.
3367 // TODO: Investigate if we need to continue to do this or if we can just process them
3368 // in-order.
3369 if (traverseTopToBottom) {
3370 for (int i = mChildren.size() - 1; i >= 0; --i) {
3371 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3372 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003373 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3374 traverseTopToBottom)) {
3375 return true;
3376 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003377 }
3378 }
3379 } else {
3380 final int count = mChildren.size();
3381 for (int i = 0; i < count; ++i) {
3382 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3383 final int appTokensCount = appTokens.size();
3384 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003385 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3386 traverseTopToBottom)) {
3387 return true;
3388 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003389 }
3390 }
3391 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003392 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003393 }
3394
Wale Ogunwale1666e312016-12-16 11:27:18 -08003395 void setExitingTokensHasVisible(boolean hasVisible) {
3396 for (int i = mChildren.size() - 1; i >= 0; --i) {
3397 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3398 for (int j = appTokens.size() - 1; j >= 0; --j) {
3399 appTokens.get(j).hasVisible = hasVisible;
3400 }
3401 }
3402 }
3403
3404 void removeExistingAppTokensIfPossible() {
3405 for (int i = mChildren.size() - 1; i >= 0; --i) {
3406 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3407 for (int j = appTokens.size() - 1; j >= 0; --j) {
3408 final AppWindowToken token = appTokens.get(j);
3409 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3410 && (!token.mIsExiting || token.isEmpty())) {
3411 // Make sure there is no animation running on this token, so any windows
3412 // associated with it will be removed as soon as their animations are
3413 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003414 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003415 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3416 "performLayout: App token exiting now removed" + token);
3417 token.removeIfPossible();
3418 }
3419 }
3420 }
3421 }
3422
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003423 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003424 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003425 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3426 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003427 // Apps and their containers are not allowed to specify an orientation while the
3428 // docked or freeform stack is visible...except for the home stack/task if the
3429 // docked stack is minimized and it actually set something.
3430 if (mHomeStack != null && mHomeStack.isVisible()
3431 && mDividerControllerLocked.isMinimizedDock()) {
3432 final int orientation = mHomeStack.getOrientation();
3433 if (orientation != SCREEN_ORIENTATION_UNSET) {
3434 return orientation;
3435 }
3436 }
3437 return SCREEN_ORIENTATION_UNSPECIFIED;
3438 }
3439
3440 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003441 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3442 PackageManager.FEATURE_AUTOMOTIVE);
3443 if (isCar) {
3444 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3445 // allow anything but the default orientation.
3446 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3447 "Forcing UNSPECIFIED orientation in car. Ignoring " + orientation);
3448 return SCREEN_ORIENTATION_UNSPECIFIED;
3449 }
3450
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003451 if (orientation != SCREEN_ORIENTATION_UNSET
3452 && orientation != SCREEN_ORIENTATION_BEHIND) {
3453 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3454 "App is requesting an orientation, return " + orientation);
3455 return orientation;
3456 }
3457
3458 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003459 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003460 // The next app has not been requested to be visible, so we keep the current orientation
3461 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003462 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003463 }
Robert Carrb1579c82017-09-05 14:54:47 -07003464
3465 @Override
3466 void assignChildLayers(SurfaceControl.Transaction t) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003467 int layer = 0;
Rob Carr6693e062018-01-09 17:10:00 +00003468
3469 // We allow stacks to change visual order from the AM specified order due to
3470 // Z-boosting during animations. However we must take care to ensure TaskStacks
3471 // which are marked as alwaysOnTop remain that way.
3472 for (int i = 0; i < mChildren.size(); i++) {
3473 final TaskStack s = mChildren.get(i);
3474 s.assignChildLayers();
3475 if (!s.needsZBoost() && !s.isAlwaysOnTop()) {
3476 s.assignLayer(t, layer++);
Robert Carrb1579c82017-09-05 14:54:47 -07003477 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003478 }
3479 for (int i = 0; i < mChildren.size(); i++) {
3480 final TaskStack s = mChildren.get(i);
Rob Carr6693e062018-01-09 17:10:00 +00003481 if (s.needsZBoost() && !s.isAlwaysOnTop()) {
3482 s.assignLayer(t, layer++);
3483 }
3484 }
3485 for (int i = 0; i < mChildren.size(); i++) {
3486 final TaskStack s = mChildren.get(i);
3487 if (s.isAlwaysOnTop()) {
3488 s.assignLayer(t, layer++);
3489 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003490 }
3491
Rob Carr6693e062018-01-09 17:10:00 +00003492 // The appropriate place for App-Transitions to occur is right
3493 // above all other animations but still below things in the Picture-and-Picture
3494 // windowing mode.
3495 if (mAppAnimationLayer != null) {
3496 t.setLayer(mAppAnimationLayer, layer++);
3497 }
Robert Carrb1579c82017-09-05 14:54:47 -07003498 }
3499
3500 @Override
chaviw23ee71c2017-12-18 11:29:41 -08003501 SurfaceControl getAppAnimationLayer() {
3502 return mAppAnimationLayer;
3503 }
3504
3505 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07003506 void onParentSet() {
3507 super.onParentSet();
3508 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08003509 mAppAnimationLayer = makeChildSurface(null)
3510 .setName("animationLayer")
3511 .build();
3512 getPendingTransaction().show(mAppAnimationLayer);
3513 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07003514 } else {
chaviw23ee71c2017-12-18 11:29:41 -08003515 mAppAnimationLayer.destroy();
3516 mAppAnimationLayer = null;
Robert Carrb1579c82017-09-05 14:54:47 -07003517 }
3518 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003519 }
3520
Robert Carree4d4b92017-11-22 12:21:46 -08003521 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003522 AboveAppWindowContainers(String name, WindowManagerService service) {
3523 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08003524 }
3525
3526 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
3527 boolean needAssignIme = imeContainer != null
3528 && imeContainer.getSurfaceControl() != null;
3529 for (int j = 0; j < mChildren.size(); ++j) {
3530 final WindowToken wt = mChildren.get(j);
3531 wt.assignLayer(t, j);
3532 wt.assignChildLayers(t);
3533
3534 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
3535 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08003536
3537 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
3538 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003539 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08003540 needAssignIme = false;
3541 }
3542 }
3543 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003544 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08003545 }
3546 }
3547 }
3548
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003549 /**
3550 * Window container class that contains all containers on this display that are not related to
3551 * Apps. E.g. status bar.
3552 */
Robert Carree4d4b92017-11-22 12:21:46 -08003553 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07003554 /**
3555 * Compares two child window tokens returns -1 if the first is lesser than the second in
3556 * terms of z-order and 1 otherwise.
3557 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003558 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003559 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003560 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3561 token1.mOwnerCanManageAppTokens)
3562 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3563 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003564
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003565 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3566 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3567 return false;
3568 }
3569 final int req = w.mAttrs.screenOrientation;
3570 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3571 || req == SCREEN_ORIENTATION_UNSET) {
3572 return false;
3573 }
3574 return true;
3575 };
3576
Wale Ogunwale3a931692016-11-02 16:49:48 -07003577 private final String mName;
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003578 NonAppWindowContainers(String name, WindowManagerService service) {
3579 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003580 mName = name;
3581 }
3582
3583 void addChild(WindowToken token) {
3584 addChild(token, mWindowComparator);
3585 }
3586
3587 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003588 int getOrientation() {
3589 final WindowManagerPolicy policy = mService.mPolicy;
3590 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003591 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003592
3593 if (win != null) {
3594 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003595 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003596 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003597 if (mService.mKeyguardGoingAway) {
3598 // Keyguard can't affect the orientation if it is going away...
3599 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3600 return SCREEN_ORIENTATION_UNSET;
3601 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003602 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003603 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003604 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003605 }
3606
Andrii Kulian8ee72852017-03-10 10:36:45 -08003607 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003608
Jorim Jaggi75520d52017-08-24 17:23:19 +02003609 if (policy.isKeyguardShowingAndNotOccluded()
3610 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003611 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003612 }
3613
3614 return SCREEN_ORIENTATION_UNSET;
3615 }
3616
3617 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003618 String getName() {
3619 return mName;
3620 }
Robert Carr3b716242016-08-16 16:02:21 -07003621 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003622
Robert Carr9034d962018-01-04 18:27:42 -08003623 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
3624 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
3625 super(name, service);
3626 }
3627
3628 @Override
3629 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
3630 }
3631 };
3632
Robert Carrb1579c82017-09-05 14:54:47 -07003633 SurfaceControl.Builder makeSurface(SurfaceSession s) {
3634 return mService.makeSurfaceBuilder(s)
3635 .setParent(mWindowingLayer);
3636 }
3637
3638 @Override
3639 SurfaceSession getSession() {
3640 return mSession;
3641 }
3642
3643 @Override
3644 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003645 SurfaceSession s = child != null ? child.getSession() : getSession();
3646 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07003647 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07003648
3649 if (child == null) {
3650 return b;
3651 }
3652
Robert Carree4d4b92017-11-22 12:21:46 -08003653 return b.setName(child.getName())
3654 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07003655 }
3656
3657 /**
3658 * The makeSurface variants are for use by the window-container
3659 * hierarchy. makeOverlay here is a function for various non windowing
3660 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
3661 * and other potpourii.
3662 */
3663 SurfaceControl.Builder makeOverlay() {
3664 return mService.makeSurfaceBuilder(mSession)
3665 .setParent(mOverlayLayer);
3666 }
3667
3668 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003669 applyMagnificationSpec(getPendingTransaction(), spec);
3670 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07003671 }
3672
3673 @Override
3674 void onParentSet() {
3675 // Since we are the top of the SurfaceControl hierarchy here
3676 // we create the root surfaces explicitly rather than chaining
3677 // up as the default implementation in onParentSet does. So we
3678 // explicitly do NOT call super here.
3679 }
3680
3681 @Override
3682 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07003683
3684 // These are layers as children of "mWindowingLayer"
3685 mBelowAppWindowsContainers.assignLayer(t, 0);
3686 mTaskStackContainers.assignLayer(t, 1);
3687 mAboveAppWindowsContainers.assignLayer(t, 2);
3688
3689 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08003690 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08003691
Robert Carree4d4b92017-11-22 12:21:46 -08003692 // In the case where we have an IME target that is not in split-screen
3693 // mode IME assignment is easy. We just need the IME to go directly above
3694 // the target. This way children of the target will naturally go above the IME
3695 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08003696 //
Robert Carree4d4b92017-11-22 12:21:46 -08003697 // In the case of split-screen windowing mode, we need to elevate the IME above the
3698 // docked divider while keeping the app itself below the docked divider, so instead
3699 // we use relative layering of the IME targets child windows, and place the
3700 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08003701 //
Robert Carree4d4b92017-11-22 12:21:46 -08003702 // In the case where we have no IME target we assign it where it's base layer would
3703 // place it in the AboveAppWindowContainers.
3704 if (imeTarget != null && !imeTarget.inSplitScreenWindowingMode()
3705 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003706 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07003707 // TODO: We need to use an extra level on the app surface to ensure
3708 // this is always above SurfaceView but always below attached window.
3709 1);
Robert Carree4d4b92017-11-22 12:21:46 -08003710 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07003711 }
3712
3713 // Above we have assigned layers to our children, now we ask them to assign
3714 // layers to their children.
3715 mBelowAppWindowsContainers.assignChildLayers(t);
3716 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08003717 mAboveAppWindowsContainers.assignChildLayers(t,
3718 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07003719 mImeWindowsContainers.assignChildLayers(t);
3720 }
3721
3722 /**
3723 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
3724 * that the IME target is one of the docked applications. We'd like the docked divider to be
3725 * above both of the applications, and we'd like the IME to be above the docked divider.
3726 * However we need child windows of the applications to be above the IME (Text drag handles).
3727 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
3728 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
3729 * with {@link #WindowState#assignLayer}
3730 */
3731 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
3732 t.setRelativeLayer(child.getSurfaceControl(), mImeWindowsContainers.getSurfaceControl(), 1);
3733 }
3734
3735 @Override
Jorim Jaggia5e10572017-11-15 14:36:26 +01003736 public void destroyAfterPendingTransaction(SurfaceControl surface) {
Robert Carrb1579c82017-09-05 14:54:47 -07003737 mPendingDestroyingSurfaces.add(surface);
3738 }
3739
3740 /**
3741 * Destroys any surfaces that have been put into the pending list with
Jorim Jaggia5e10572017-11-15 14:36:26 +01003742 * {@link #destroyAfterPendingTransaction}.
Robert Carrb1579c82017-09-05 14:54:47 -07003743 */
3744 void onPendingTransactionApplied() {
3745 for (int i = mPendingDestroyingSurfaces.size() - 1; i >= 0; i--) {
3746 mPendingDestroyingSurfaces.get(i).destroy();
3747 }
3748 mPendingDestroyingSurfaces.clear();
3749 }
Jorim Jaggi9af095b2017-12-12 17:18:57 +01003750
3751 @Override
3752 void prepareSurfaces() {
3753 final ScreenRotationAnimation screenRotationAnimation =
3754 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
3755 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
3756 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
3757 mPendingTransaction.setMatrix(mWindowingLayer,
3758 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
3759 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
3760 mPendingTransaction.setPosition(mWindowingLayer,
3761 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
3762 mPendingTransaction.setAlpha(mWindowingLayer,
3763 screenRotationAnimation.getEnterTransformation().getAlpha());
3764 }
3765 super.prepareSurfaces();
3766 }
Craig Mautner59c00972012-07-30 12:10:24 -07003767}