blob: b01d67d84f957b0152467c07972d8e676d112f08 [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 Ogunwale65ebd952018-04-25 15:41:44 -070019import static android.app.ActivityTaskManager.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 Ogunwaleb62139d2017-09-20 15:37:35 -070023import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
24import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070025import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070026import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
27import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale687b4272017-07-27 02:56:23 -070028import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
29import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070030import static android.view.Display.DEFAULT_DISPLAY;
31import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070032import static android.view.Surface.ROTATION_0;
33import static android.view.Surface.ROTATION_180;
34import static android.view.Surface.ROTATION_270;
35import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070036import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070037import static android.view.WindowManager.DOCKED_BOTTOM;
38import static android.view.WindowManager.DOCKED_INVALID;
39import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080040import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
41import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
42import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070043import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070044import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070045import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
46import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070047import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070048import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070049import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070050import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Robert Carrf7a7ca82017-12-06 13:17:07 -080051import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
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;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080061import static android.view.WindowManager.TRANSIT_KEYGUARD_UNOCCLUDE;
62
Adrian Roose99bc052017-11-20 17:55:31 +010063import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
64import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
65import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
66import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080067import static com.android.server.wm.DisplayContentProto.ABOVE_APP_WINDOWS;
lumark588a3e82018-07-20 18:53:54 +080068import static com.android.server.wm.DisplayContentProto.APP_TRANSITION;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080069import static com.android.server.wm.DisplayContentProto.BELOW_APP_WINDOWS;
70import static com.android.server.wm.DisplayContentProto.DISPLAY_FRAMES;
71import static com.android.server.wm.DisplayContentProto.DISPLAY_INFO;
72import static com.android.server.wm.DisplayContentProto.DOCKED_STACK_DIVIDER_CONTROLLER;
73import static com.android.server.wm.DisplayContentProto.DPI;
Tiger Huang1e5b10a2018-07-30 20:19:51 +080074import static com.android.server.wm.DisplayContentProto.FOCUSED_APP;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080075import static com.android.server.wm.DisplayContentProto.ID;
76import static com.android.server.wm.DisplayContentProto.IME_WINDOWS;
77import static com.android.server.wm.DisplayContentProto.PINNED_STACK_CONTROLLER;
78import static com.android.server.wm.DisplayContentProto.ROTATION;
79import static com.android.server.wm.DisplayContentProto.SCREEN_ROTATION_ANIMATION;
80import static com.android.server.wm.DisplayContentProto.STACKS;
Louis Chang7501e332018-08-20 13:08:39 +080081import static com.android.server.wm.DisplayContentProto.SURFACE_SIZE;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080082import static com.android.server.wm.DisplayContentProto.WINDOW_CONTAINER;
Wale Ogunwale1666e312016-12-16 11:27:18 -080083import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
lumark588a3e82018-07-20 18:53:54 +080084import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_APP_TRANSITIONS;
Wale Ogunwale494009b82016-10-21 09:01:38 -070085import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070086import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070087import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
88import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070089import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070090import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070091import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020092import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070093import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070094import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070095import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080096import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070097import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070098import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070099import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700100import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800101import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700102import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800103import static com.android.server.wm.WindowManagerService.H.REPORT_FOCUS_CHANGE;
104import static com.android.server.wm.WindowManagerService.H.REPORT_LOSING_FOCUS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700105import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
106import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700107import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700108import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700109import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
110import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800111import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_PLACING_SURFACES;
112import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_REMOVING_FOCUS;
113import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_ASSIGN_LAYERS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700114import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700115import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700116import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700117import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700118import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700119import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700120import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700121import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700122import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700123
lumark588a3e82018-07-20 18:53:54 +0800124import android.animation.AnimationHandler;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700125import android.annotation.CallSuper;
Riddle Hsuf53da812018-08-15 22:00:27 +0800126import android.annotation.IntDef;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700127import android.annotation.NonNull;
Dean Harding3e5a1522017-10-06 09:19:01 -0700128import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700129import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700130import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700131import android.graphics.Bitmap;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700132import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800133import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700134import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700135import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100136import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700137import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700138import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700139import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700140import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700141import android.os.RemoteException;
142import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100143import android.os.Trace;
Riddle Hsuf53da812018-08-15 22:00:27 +0800144import android.os.UserHandle;
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800145import android.util.ArraySet;
Chong Zhang8e89b312015-09-09 15:09:30 -0700146import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700147import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700148import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700149import android.view.Display;
Adrian Roos1cf585052018-01-03 18:43:27 +0100150import android.view.DisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700151import android.view.DisplayInfo;
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800152import android.view.Gravity;
Arthur Hungbe5ce212018-09-13 18:41:56 +0800153import android.view.InputChannel;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700154import android.view.InputDevice;
Robert Carrb1579c82017-09-05 14:54:47 -0700155import android.view.MagnificationSpec;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700156import android.view.Surface;
157import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100158import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700159import android.view.SurfaceSession;
lumark588a3e82018-07-20 18:53:54 +0800160import android.view.WindowManager;
Evan Rosky39b6f232018-10-30 18:35:41 -0700161import android.view.WindowManagerPolicyConstants.PointerEventListener;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700162
Bryce Lee48f4b572017-04-10 10:54:15 -0700163import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800164import com.android.internal.util.ToBooleanFunction;
Adrian Roose99bc052017-11-20 17:55:31 +0100165import com.android.server.policy.WindowManagerPolicy;
Issei Suzuki43190bd2018-08-20 17:28:41 +0200166import com.android.server.wm.utils.DisplayRotationUtil;
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100167import com.android.server.wm.utils.RotationCache;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100168import com.android.server.wm.utils.WmDisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700169
170import java.io.PrintWriter;
Riddle Hsuf53da812018-08-15 22:00:27 +0800171import java.lang.annotation.Retention;
172import java.lang.annotation.RetentionPolicy;
Craig Mautner59c00972012-07-30 12:10:24 -0700173import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700174import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700175import java.util.HashMap;
176import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700177import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700178import java.util.List;
Adrian Roos1cf585052018-01-03 18:43:27 +0100179import java.util.Objects;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800180import java.util.function.Consumer;
181import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700182
Craig Mautner59c00972012-07-30 12:10:24 -0700183/**
184 * Utility class for keeping track of the WindowStates and other pertinent contents of a
185 * particular Display.
186 *
187 * IMPORTANT: No method from this class should ever be used without holding
188 * WindowManagerService.mWindowMap.
189 */
Riddle Hsuad256a12018-07-18 16:11:30 +0800190class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer>
191 implements WindowManagerPolicy.DisplayContentInfo {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700192 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700193
Riddle Hsuf53da812018-08-15 22:00:27 +0800194 /** The default scaling mode that scales content automatically. */
195 static final int FORCE_SCALING_MODE_AUTO = 0;
196 /** For {@link #setForcedScalingMode} to apply flag {@link Display#FLAG_SCALING_DISABLED}. */
197 static final int FORCE_SCALING_MODE_DISABLED = 1;
198
199 @IntDef(prefix = { "FORCE_SCALING_MODE_" }, value = {
200 FORCE_SCALING_MODE_AUTO,
201 FORCE_SCALING_MODE_DISABLED
202 })
203 @Retention(RetentionPolicy.SOURCE)
204 @interface ForceScalingMode {}
205
Craig Mautner59c00972012-07-30 12:10:24 -0700206 /** Unique identifier of this stack. */
207 private final int mDisplayId;
208
Wale Ogunwale3a931692016-11-02 16:49:48 -0700209 /** The containers below are the only child containers the display can have. */
210 // Contains all window containers that are related to apps (Activities)
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100211 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700212 // Contains all non-app window containers that should be displayed above the app containers
213 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800214 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100215 new AboveAppWindowContainers("mAboveAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700216 // Contains all non-app window containers that should be displayed below the app containers
217 // (e.g. Wallpaper).
218 private final NonAppWindowContainers mBelowAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100219 new NonAppWindowContainers("mBelowAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700220 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
221 // 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 -0800222 // window containers together and move them in-sync if/when needed. We use a subclass of
223 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
224 private final NonMagnifiableWindowContainers mImeWindowsContainers =
225 new NonMagnifiableWindowContainers("mImeWindowsContainers", mService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700226
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000227 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800228 private WindowState mTmpWindow2;
229 private WindowAnimator mTmpWindowAnimator;
230 private boolean mTmpRecoveringMemory;
231 private boolean mUpdateImeTarget;
232 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700233 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700234
lumark588a3e82018-07-20 18:53:54 +0800235 final AppTransition mAppTransition;
236 final AppTransitionController mAppTransitionController;
237 boolean mSkipAppTransitionAnimation = false;
238
239 final ArraySet<AppWindowToken> mOpeningApps = new ArraySet<>();
240 final ArraySet<AppWindowToken> mClosingApps = new ArraySet<>();
241 final UnknownAppVisibilityController mUnknownAppVisibilityController;
242 BoundsAnimationController mBoundsAnimationController;
243
244 /**
245 * List of clients without a transtiton animation that we notify once we are done
246 * transitioning since they won't be notified through the app window animator.
247 */
248 final List<IBinder> mNoAnimationNotifyOnTransitionFinished = new ArrayList<>();
249
Wale Ogunwale02319a62016-09-26 15:21:22 -0700250 // Mapping from a token IBinder to a WindowToken object on this display.
251 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
252
Andrii Kuliancd097992017-03-23 18:31:59 -0700253 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700254 int mInitialDisplayWidth = 0;
255 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700256 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700257
Adrian Roos1cf585052018-01-03 18:43:27 +0100258 DisplayCutout mInitialDisplayCutout;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100259 private final RotationCache<DisplayCutout, WmDisplayCutout> mDisplayCutoutCache
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100260 = new RotationCache<>(this::calculateDisplayCutoutForRotationUncached);
Adrian Roos1cf585052018-01-03 18:43:27 +0100261
Andrii Kuliancd097992017-03-23 18:31:59 -0700262 /**
263 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
264 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
265 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
266 */
Craig Mautner59c00972012-07-30 12:10:24 -0700267 int mBaseDisplayWidth = 0;
268 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700269 /**
270 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
271 * but can be set from Settings or via shell command "adb shell wm density".
272 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
273 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700274 int mBaseDisplayDensity = 0;
Riddle Hsuf53da812018-08-15 22:00:27 +0800275
276 /**
277 * Whether to disable display scaling. This can be set via shell command "adb shell wm scaling".
278 * @see WindowManagerService#setForcedDisplayScalingMode(int, int)
279 */
Jeff Brownd46747a2015-04-15 19:02:36 -0700280 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700281 private final DisplayInfo mDisplayInfo = new DisplayInfo();
282 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700283 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800284 private final DisplayPolicy mDisplayPolicy;
285 private DisplayRotation mDisplayRotation;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000286 DisplayFrames mDisplayFrames;
287
Andrii Kulian06d07d62017-03-14 11:11:47 -0700288 /**
289 * For default display it contains real metrics, empty for others.
290 * @see WindowManagerService#createWatermarkInTransaction()
291 */
292 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
293 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
294 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700295
Andrii Kulian06d07d62017-03-14 11:11:47 -0700296 /**
297 * Compat metrics computed based on {@link #mDisplayMetrics}.
298 * @see #updateDisplayAndOrientation(int)
299 */
300 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
301
302 /** The desired scaling factor for compatible apps. */
303 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700304
Andrii Kulian8ee72852017-03-10 10:36:45 -0800305 /**
306 * Current rotation of the display.
307 * Constants as per {@link android.view.Surface.Rotation}.
308 *
Robert Carrae606b42018-02-15 15:36:23 -0800309 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800310 */
311 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700312
Andrii Kulian8ee72852017-03-10 10:36:45 -0800313 /**
314 * Last applied orientation of the display.
315 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
316 *
Riddle Hsu4e611772018-10-31 18:58:28 +0800317 * @see #updateOrientationFromAppTokens()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800318 */
319 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700320
Andrii Kulian8ee72852017-03-10 10:36:45 -0800321 /**
322 * Flag indicating that the application is receiving an orientation that has different metrics
323 * than it expected. E.g. Portrait instead of Landscape.
324 *
Robert Carrae606b42018-02-15 15:36:23 -0800325 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800326 */
327 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700328
Andrii Kulian8ee72852017-03-10 10:36:45 -0800329 /**
330 * Orientation forced by some window. If there is no visible window that specifies orientation
331 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
332 *
333 * @see NonAppWindowContainers#getOrientation()
334 */
335 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700336
Andrii Kulian8ee72852017-03-10 10:36:45 -0800337 /**
338 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
339 * occluded.
340 *
341 * @see NonAppWindowContainers#getOrientation()
342 */
343 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
344
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700345 /**
346 * Keep track of wallpaper visibility to notify changes.
347 */
348 private boolean mLastWallpaperVisible = false;
349
Andrii Kulian06d07d62017-03-14 11:11:47 -0700350 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700351
Craig Mautner39834192012-09-02 07:47:24 -0700352 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700353 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700354 int pendingLayoutChanges;
Riddle Hsu654a6f92018-07-13 22:59:36 +0800355 int mDeferredRotationPauseCount;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800356
Andrii Kulian839def92016-11-02 10:58:58 -0700357 // TODO(multi-display): remove some of the usages.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800358 @VisibleForTesting
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800359 boolean isDefaultDisplay;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800360
Andrii Kulianf0379de2018-03-14 16:24:07 -0700361 /**
362 * Flag indicating whether WindowManager should override info for this display in
363 * DisplayManager.
364 */
365 boolean mShouldOverrideDisplayConfiguration = true;
Craig Mautner39834192012-09-02 07:47:24 -0700366
Craig Mautnerdc548482014-02-05 13:35:24 -0800367 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700368 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800369
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700370 /** Detect user tapping outside of current focused task bounds .*/
371 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700372
Craig Mautner6601b7b2013-04-29 10:29:11 -0700373 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700374 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700375
Craig Mautner6601b7b2013-04-29 10:29:11 -0700376 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800377 private final Rect mTmpRect = new Rect();
378 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700379 private final RectF mTmpRectF = new RectF();
380 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800381 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700382
Issei Suzuki43190bd2018-08-20 17:28:41 +0200383
Bryce Leef3c6a472017-11-14 14:53:06 -0800384 /** Used for handing back size of display */
385 private final Rect mTmpBounds = new Rect();
386
Craig Mautner95da1082014-02-24 17:54:35 -0800387 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700388 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800389
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700390 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700391 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700392
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800393 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800394 /** A collection of windows that provide tap exclude regions inside of them. */
395 final ArraySet<WindowState> mTapExcludeProvidingWindows = new ArraySet<>();
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800396
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000397 private boolean mHaveBootMsg = false;
398 private boolean mHaveApp = false;
399 private boolean mHaveWallpaper = false;
400 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700401
402 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
403
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700404 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
405 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000406 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
407 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700408
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700409 // True if this display is in the process of being removed. Used to determine if the removal of
410 // the display's direct children should be allowed.
411 private boolean mRemovingDisplay = false;
412
Bryce Leed1871262017-06-12 14:12:29 -0700413 // {@code false} if this display is in the processing of being created.
414 private boolean mDisplayReady = false;
415
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800416 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700417
Robert Carrb1579c82017-09-05 14:54:47 -0700418 private final SurfaceSession mSession = new SurfaceSession();
419
420 /**
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800421 * Window that is currently interacting with the user. This window is responsible for receiving
422 * key events and pointer events from the user.
423 */
424 WindowState mCurrentFocus = null;
425
426 /**
427 * The last focused window that we've notified the client that the focus is changed.
428 */
429 WindowState mLastFocus = null;
430
431 /**
432 * Windows that have lost input focus and are waiting for the new focus window to be displayed
433 * before they are told about this.
434 */
435 ArrayList<WindowState> mLosingFocus = new ArrayList<>();
436
437 /**
438 * The foreground app of this display. Windows below this app cannot be the focused window. If
439 * the user taps on the area outside of the task of the focused app, we will notify AM about the
440 * new task the user wants to interact with.
441 */
442 AppWindowToken mFocusedApp = null;
443
444 /** Windows added since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
445 final ArrayList<WindowState> mWinAddedSinceNullFocus = new ArrayList<>();
446
447 /** Windows removed since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
448 final ArrayList<WindowState> mWinRemovedSinceNullFocus = new ArrayList<>();
449
450 /**
Robert Carrb1579c82017-09-05 14:54:47 -0700451 * We organize all top-level Surfaces in to the following layers.
452 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800453 * and omitted from Screen-Magnification, for example the strict mode flash or
454 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700455 * {@link #mWindowingLayer} contains everything else.
456 */
457 private SurfaceControl mOverlayLayer;
458
459 /**
460 * See {@link #mOverlayLayer}
461 */
462 private SurfaceControl mWindowingLayer;
463
464 /**
465 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
466 * <p>
467 * For these surfaces currently we use a surface based on the larger of width or height so we
468 * don't have to resize when rotating the display.
469 */
470 private int mSurfaceSize;
471
Adrian Roos5251b1d2018-03-23 18:57:43 +0100472 /**
473 * Sequence number for the current layout pass.
474 */
475 int mLayoutSeq = 0;
476
Chavi Weingarten3a748552018-05-14 17:32:42 +0000477 /**
478 * Specifies the count to determine whether to defer updating the IME target until ready.
479 */
480 private int mDeferUpdateImeTargetCount;
481
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100482 /** Temporary float array to retrieve 3x3 matrix values. */
483 private final float[] mTmpFloats = new float[9];
484
Robert Carr24be9ab2018-04-30 17:54:53 -0700485 private MagnificationSpec mMagnificationSpec;
486
Arthur Hung95b38a92018-07-20 18:56:12 +0800487 private InputMonitor mInputMonitor;
488
Jorim Jaggif1292892018-09-10 11:58:13 +0200489 /** Caches the value whether told display manager that we have content. */
490 private boolean mLastHasContent;
491
Issei Suzuki43190bd2018-08-20 17:28:41 +0200492 private DisplayRotationUtil mRotationUtil = new DisplayRotationUtil();
493
lumark90120a82018-08-15 00:33:03 +0800494 /**
495 * The input method window for this display.
496 */
497 WindowState mInputMethodWindow;
498
Arthur Hungbe5ce212018-09-13 18:41:56 +0800499 private final PointerEventDispatcher mPointerEventDispatcher;
500
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800501 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
502 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800503 final AppWindowToken atoken = w.mAppToken;
504 if (winAnimator.mDrawState == READY_TO_SHOW) {
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +0200505 if (atoken == null || atoken.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800506 if (w.performShowLocked()) {
507 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
508 if (DEBUG_LAYOUT_REPEATS) {
509 mService.mWindowPlacerLocked.debugLayoutRepeats(
510 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
511 }
512 }
513 }
514 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800515 };
516
517 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
518 final WindowStateAnimator winAnimator = w.mWinAnimator;
519 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
520 return;
521 }
522
Jorim Jaggi8f520872018-08-14 17:00:20 +0200523 // If this window is animating, ensure the animation background is set.
524 final AnimationAdapter anim = w.mAppToken != null
525 ? w.mAppToken.getAnimation()
526 : w.getAnimation();
527 if (anim != null) {
528 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800529 if (color != 0) {
530 final TaskStack stack = w.getStack();
531 if (stack != null) {
532 stack.setAnimationBackground(winAnimator, color);
533 }
534 }
535 }
536 };
537
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800538 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
539 final int lostFocusUid = mTmpWindow.mOwnerUid;
540 final Handler handler = mService.mH;
541 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
542 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
543 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
544 w.mAttrs.hideTimeoutMilliseconds);
545 }
546 }
547 };
548
549 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800550 final AppWindowToken focusedApp = mFocusedApp;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800551 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
552 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
553
554 if (!w.canReceiveKeys()) {
555 return false;
556 }
557
558 final AppWindowToken wtoken = w.mAppToken;
559
560 // If this window's application has been removed, just skip it.
561 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
562 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
563 + (wtoken.removed ? "removed" : "sendingToBottom"));
564 return false;
565 }
566
567 if (focusedApp == null) {
568 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
569 + " using new focus @ " + w);
570 mTmpWindow = w;
571 return true;
572 }
573
574 if (!focusedApp.windowsAreFocusable()) {
575 // Current focused app windows aren't focusable...
576 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
577 + " focusable using new focus @ " + w);
578 mTmpWindow = w;
579 return true;
580 }
581
582 // Descend through all of the app tokens and find the first that either matches
583 // win.mAppToken (return win) or mFocusedApp (return null).
584 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
585 if (focusedApp.compareTo(wtoken) > 0) {
586 // App stack below focused app stack. No focus for you!!!
587 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
588 "findFocusedWindow: Reached focused app=" + focusedApp);
589 mTmpWindow = null;
590 return true;
591 }
592 }
593
594 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
595 mTmpWindow = w;
596 return true;
597 };
598
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800599 private final Consumer<WindowState> mPerformLayout = w -> {
600 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
601 // wasting time and funky changes while a window is animating away.
602 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
603 || w.isGoneForLayoutLw();
604
605 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
606 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
607 + " mLayoutAttached=" + w.mLayoutAttached
608 + " screen changed=" + w.isConfigChanged());
609 final AppWindowToken atoken = w.mAppToken;
610 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200611 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800612 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
613 + " parentHidden=" + w.isParentWindowHidden());
614 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200615 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800616 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
617 + " parentHidden=" + w.isParentWindowHidden());
618 }
619
620 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
621 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
622 // since that means "perform layout as normal, just don't display").
623 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
624 || ((w.isConfigChanged() || w.setReportResizeHints())
625 && !w.isGoneForLayoutLw() &&
626 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
627 (w.mHasSurface && w.mAppToken != null &&
628 w.mAppToken.layoutConfigChanges)))) {
629 if (!w.mLayoutAttached) {
630 if (mTmpInitial) {
631 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700632 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800633 }
634 if (w.mAttrs.type == TYPE_DREAM) {
635 // Don't layout windows behind a dream, so that if it does stuff like hide
636 // the status bar we won't get a bad transition when it goes away.
637 mTmpWindow = w;
638 }
639 w.mLayoutNeeded = false;
640 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200641 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000642 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100643 w.mLayoutSeq = mLayoutSeq;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800644
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200645 // If this is the first layout, we need to initialize the last inset values as
646 // otherwise we'd immediately cause an unnecessary resize.
647 if (firstLayout) {
648 w.updateLastInsetValues();
649 }
650
Adrian Roos23df3a32018-03-15 15:41:13 +0100651 if (w.mAppToken != null) {
652 w.mAppToken.layoutLetterbox(w);
653 }
654
chaviw492139a2018-07-16 16:07:35 -0700655 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700656 + " mContainingFrame=" + w.getContainingFrame()
657 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800658 }
659 }
660 };
661
662 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
663 if (w.mLayoutAttached) {
664 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
665 + " mViewVisibility=" + w.mViewVisibility
666 + " mRelayoutCalled=" + w.mRelayoutCalled);
667 // If this view is GONE, then skip it -- keep the current frame, and let the caller
668 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
669 // windows, since that means "perform layout as normal, just don't display").
670 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
671 return;
672 }
673 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
674 || w.mLayoutNeeded) {
675 if (mTmpInitial) {
676 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700677 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800678 }
679 w.mLayoutNeeded = false;
680 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000681 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100682 w.mLayoutSeq = mLayoutSeq;
chaviw492139a2018-07-16 16:07:35 -0700683 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700684 + " mContainingFrame=" + w.getContainingFrame()
685 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800686 }
687 } else if (w.mAttrs.type == TYPE_DREAM) {
688 // Don't layout windows behind a dream, so that if it does stuff like hide the
689 // status bar we won't get a bad transition when it goes away.
690 mTmpWindow = mTmpWindow2;
691 }
692 };
693
694 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
695 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
696 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
697 return w.canBeImeTarget();
698 };
699
700 private final Consumer<WindowState> mApplyPostLayoutPolicy =
701 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
702 mService.mInputMethodTarget);
703
704 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
705 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
706 final boolean obscuredChanged = w.mObscured !=
707 mTmpApplySurfaceChangesTransactionState.obscured;
708 final RootWindowContainer root = mService.mRoot;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800709
710 // Update effect.
711 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
712 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
713 final boolean isDisplayed = w.isDisplayedLw();
714
715 if (isDisplayed && w.isObscuringDisplay()) {
716 // This window completely covers everything behind it, so we want to leave all
717 // of them as undimmed (for performance reasons).
718 root.mObscuringWindow = w;
719 mTmpApplySurfaceChangesTransactionState.obscured = true;
720 }
721
722 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
723 root.handleNotObscuredLocked(w,
724 mTmpApplySurfaceChangesTransactionState.obscured,
725 mTmpApplySurfaceChangesTransactionState.syswin);
726
727 if (w.mHasSurface && isDisplayed) {
728 final int type = w.mAttrs.type;
729 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
730 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
731 mTmpApplySurfaceChangesTransactionState.syswin = true;
732 }
733 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
734 && w.mAttrs.preferredRefreshRate != 0) {
735 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
736 = w.mAttrs.preferredRefreshRate;
737 }
738 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
739 && w.mAttrs.preferredDisplayModeId != 0) {
740 mTmpApplySurfaceChangesTransactionState.preferredModeId
741 = w.mAttrs.preferredDisplayModeId;
742 }
743 }
744 }
745
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800746 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
747 && mWallpaperController.isWallpaperTarget(w)) {
748 // This is the wallpaper target and its obscured state changed... make sure the
749 // current wallpaper's visibility has been updated accordingly.
750 mWallpaperController.updateWallpaperVisibility();
751 }
752
chaviw161ea3e2018-01-31 12:01:12 -0800753 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800754
755 final WindowStateAnimator winAnimator = w.mWinAnimator;
756
757 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
chaviw1454b392018-08-06 09:54:04 -0700758 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800759
760 // Moved from updateWindowsAndWallpaperLocked().
761 if (w.mHasSurface) {
762 // Take care of the window being ready to display.
763 final boolean committed = winAnimator.commitFinishDrawingLocked();
764 if (isDefaultDisplay && committed) {
765 if (w.mAttrs.type == TYPE_DREAM) {
766 // HACK: When a dream is shown, it may at that point hide the lock screen.
767 // So we need to redo the layout to let the phone window manager make this
768 // happen.
769 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
770 if (DEBUG_LAYOUT_REPEATS) {
771 surfacePlacer.debugLayoutRepeats(
772 "dream and commitFinishDrawingLocked true",
773 pendingLayoutChanges);
774 }
775 }
776 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
777 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
778 "First draw done in potential wallpaper target " + w);
779 root.mWallpaperMayChange = true;
780 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
781 if (DEBUG_LAYOUT_REPEATS) {
782 surfacePlacer.debugLayoutRepeats(
783 "wallpaper and commitFinishDrawingLocked true",
784 pendingLayoutChanges);
785 }
786 }
787 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800788 }
789
790 final AppWindowToken atoken = w.mAppToken;
791 if (atoken != null) {
Adrian Roos23df3a32018-03-15 15:41:13 +0100792 atoken.updateLetterboxSurface(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800793 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
794 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
795 mTmpUpdateAllDrawn.add(atoken);
796 }
797 }
798
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800799 if (!mLosingFocus.isEmpty() && w.isFocused() && w.isDisplayedLw()) {
800 mService.mH.obtainMessage(REPORT_LOSING_FOCUS, this).sendToTarget();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800801 }
802
803 w.updateResizingWindowIfNeeded();
804 };
805
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800806 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800807 * Create new {@link DisplayContent} instance, add itself to the root window container and
808 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700809 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800810 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700811 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
812 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700813 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700814 DisplayContent(Display display, WindowManagerService service,
Bryce Leef19cbe22018-02-02 15:09:21 -0800815 WallpaperController wallpaperController, DisplayWindowController controller) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100816 super(service);
Bryce Leef19cbe22018-02-02 15:09:21 -0800817 setController(controller);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800818 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
819 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
820 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
821 + " new=" + display);
822 }
823
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700824 mDisplay = display;
825 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700826 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700827 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700828 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700829 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100830 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo,
831 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700832 initializeDisplayBaseInfo();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800833 mDisplayPolicy = new DisplayPolicy(service);
834 mDisplayRotation = new DisplayRotation(service, this);
835 if (isDefaultDisplay) {
836 // The policy may be invoked right after here, so it requires the necessary default
837 // fields of this display content.
838 mService.mPolicy.setDefaultDisplay(this);
839 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800840 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700841 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700842
lumark588a3e82018-07-20 18:53:54 +0800843 mAppTransition = new AppTransition(service.mContext, service, this);
844 mAppTransition.registerListenerLocked(service.mActivityManagerAppTransitionNotifier);
845 mAppTransitionController = new AppTransitionController(service, this);
846 mUnknownAppVisibilityController = new UnknownAppVisibilityController(service, this);
847
848 AnimationHandler animationHandler = new AnimationHandler();
849 mBoundsAnimationController = new BoundsAnimationController(service.mContext,
850 mAppTransition, SurfaceAnimationThread.getHandler(), animationHandler);
851
Robert Carr74a66a22018-02-23 12:17:51 -0800852 // We use this as our arbitrary surface size for buffer-less parents
853 // that don't impose cropping on their children. It may need to be larger
854 // than the display size because fullscreen windows can be shifted offscreen
855 // due to surfaceInsets. 2 times the largest display dimension feels like an
856 // appropriately arbitrary number. Eventually we would like to give SurfaceFlinger
857 // layers the ability to match their parent sizes and be able to skip
858 // such arbitrary size settings.
859 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth) * 2;
Robert Carrb1579c82017-09-05 14:54:47 -0700860
861 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
862 .setSize(mSurfaceSize, mSurfaceSize)
863 .setOpaque(true);
864 mWindowingLayer = b.setName("Display Root").build();
865 mOverlayLayer = b.setName("Display Overlays").build();
866
Robert Carrf59b8dd2017-10-02 18:58:36 -0700867 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700868 .setLayerStack(mWindowingLayer, mDisplayId)
869 .show(mWindowingLayer)
870 .setLayer(mOverlayLayer, 1)
871 .setLayerStack(mOverlayLayer, mDisplayId)
872 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700873 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700874
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700875 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700876 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700877 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700878 super.addChild(mAboveAppWindowsContainers, null);
879 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800880
881 // Add itself as a child to the root container.
882 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700883
884 // TODO(b/62541591): evaluate whether this is the best spot to declare the
885 // {@link DisplayContent} ready for use.
886 mDisplayReady = true;
Arthur Hung95b38a92018-07-20 18:56:12 +0800887
Riddle Hsuf53da812018-08-15 22:00:27 +0800888 mService.mAnimator.addDisplayLocked(mDisplayId);
Arthur Hung39134b22018-08-14 11:58:28 +0800889 mInputMonitor = new InputMonitor(service, mDisplayId);
Arthur Hungbe5ce212018-09-13 18:41:56 +0800890
891 if (mService.mInputManager != null) {
892 final InputChannel inputChannel = mService.mInputManager.monitorInput("Display "
893 + mDisplayId, mDisplayId);
894 mPointerEventDispatcher = inputChannel != null
895 ? new PointerEventDispatcher(inputChannel) : null;
896 } else {
897 mPointerEventDispatcher = null;
898 }
Bryce Leed1871262017-06-12 14:12:29 -0700899 }
900
901 boolean isReady() {
902 // The display is ready when the system and the individual display are both ready.
903 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700904 }
905
906 int getDisplayId() {
907 return mDisplayId;
908 }
909
Wale Ogunwale02319a62016-09-26 15:21:22 -0700910 WindowToken getWindowToken(IBinder binder) {
911 return mTokenMap.get(binder);
912 }
913
914 AppWindowToken getAppWindowToken(IBinder binder) {
915 final WindowToken token = getWindowToken(binder);
916 if (token == null) {
917 return null;
918 }
919 return token.asAppWindowToken();
920 }
921
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700922 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700923 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
924 if (dc != null) {
925 // We currently don't support adding a window token to the display if the display
926 // already has the binder mapped to another token. If there is a use case for supporting
927 // this moving forward we will either need to merge the WindowTokens some how or have
928 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700929 throw new IllegalArgumentException("Can't map token=" + token + " to display="
930 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700931 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700932 if (binder == null) {
933 throw new IllegalArgumentException("Can't map token=" + token + " to display="
934 + getName() + " binder is null");
935 }
936 if (token == null) {
937 throw new IllegalArgumentException("Can't map null token to display="
938 + getName() + " binder=" + binder);
939 }
940
Wale Ogunwale02319a62016-09-26 15:21:22 -0700941 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700942
943 if (token.asAppWindowToken() == null) {
944 // Add non-app token to container hierarchy on the display. App tokens are added through
945 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700946 switch (token.windowType) {
947 case TYPE_WALLPAPER:
948 mBelowAppWindowsContainers.addChild(token);
949 break;
950 case TYPE_INPUT_METHOD:
951 case TYPE_INPUT_METHOD_DIALOG:
952 mImeWindowsContainers.addChild(token);
953 break;
954 default:
955 mAboveAppWindowsContainers.addChild(token);
956 break;
957 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700958 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700959 }
960
961 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700962 final WindowToken token = mTokenMap.remove(binder);
963 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800964 token.setExiting();
965 }
966 return token;
967 }
968
969 /** Changes the display the input window token is housed on to this one. */
970 void reParentWindowToken(WindowToken token) {
971 final DisplayContent prevDc = token.getDisplayContent();
972 if (prevDc == this) {
973 return;
974 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800975 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
976 && token.asAppWindowToken() == null) {
977 // Removed the token from the map, but made sure it's not an app token before removing
978 // from parent.
979 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700980 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800981
982 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700983 }
984
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700985 void removeAppToken(IBinder binder) {
986 final WindowToken token = removeWindowToken(binder);
987 if (token == null) {
988 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
989 return;
990 }
991
992 final AppWindowToken appToken = token.asAppWindowToken();
993
994 if (appToken == null) {
995 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
996 return;
997 }
998
999 appToken.onRemovedFromDisplay();
1000 }
1001
Riddle Hsuad256a12018-07-18 16:11:30 +08001002 @Override
Riddle Hsu86cb7de2018-08-13 23:29:58 +08001003 DisplayWindowController getController() {
1004 return (DisplayWindowController) super.getController();
1005 }
1006
1007 @Override
Riddle Hsuad256a12018-07-18 16:11:30 +08001008 public Display getDisplay() {
Craig Mautnerb47bbc32012-08-22 17:41:48 -07001009 return mDisplay;
1010 }
1011
Craig Mautner59c00972012-07-30 12:10:24 -07001012 DisplayInfo getDisplayInfo() {
1013 return mDisplayInfo;
1014 }
1015
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001016 DisplayMetrics getDisplayMetrics() {
1017 return mDisplayMetrics;
1018 }
1019
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001020 DisplayPolicy getDisplayPolicy() {
1021 return mDisplayPolicy;
1022 }
1023
Riddle Hsuad256a12018-07-18 16:11:30 +08001024 @Override
1025 public DisplayRotation getDisplayRotation() {
1026 return mDisplayRotation;
1027 }
1028
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001029 @VisibleForTesting
1030 void setDisplayRotation(DisplayRotation displayRotation) {
1031 mDisplayRotation = displayRotation;
1032 }
1033
Andrii Kulian8ee72852017-03-10 10:36:45 -08001034 int getRotation() {
1035 return mRotation;
1036 }
1037
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001038 @VisibleForTesting
Andrii Kulian8ee72852017-03-10 10:36:45 -08001039 void setRotation(int newRotation) {
1040 mRotation = newRotation;
Riddle Hsuad256a12018-07-18 16:11:30 +08001041 mDisplayRotation.setRotation(newRotation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001042 }
1043
1044 int getLastOrientation() {
1045 return mLastOrientation;
1046 }
1047
Andrii Kulian8ee72852017-03-10 10:36:45 -08001048 boolean getAltOrientation() {
1049 return mAltOrientation;
1050 }
1051
Andrii Kulian8ee72852017-03-10 10:36:45 -08001052 int getLastWindowForcedOrientation() {
1053 return mLastWindowForcedOrientation;
1054 }
1055
Andrii Kulian06d07d62017-03-14 11:11:47 -07001056 /**
Riddle Hsu654a6f92018-07-13 22:59:36 +08001057 * Temporarily pauses rotation changes until resumed.
1058 *
1059 * This can be used to prevent rotation changes from occurring while the user is
1060 * performing certain operations, such as drag and drop.
1061 *
1062 * This call nests and must be matched by an equal number of calls to
1063 * {@link #resumeRotationLocked}.
1064 */
1065 void pauseRotationLocked() {
1066 mDeferredRotationPauseCount++;
1067 }
1068
1069 /**
1070 * Resumes normal rotation changes after being paused.
1071 */
1072 void resumeRotationLocked() {
1073 if (mDeferredRotationPauseCount <= 0) {
1074 return;
1075 }
1076
1077 mDeferredRotationPauseCount--;
1078 if (mDeferredRotationPauseCount == 0) {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001079 updateRotationAndSendNewConfigIfNeeded();
Riddle Hsu654a6f92018-07-13 22:59:36 +08001080 }
1081 }
1082
1083 /**
Riddle Hsuad256a12018-07-18 16:11:30 +08001084 * If this is true we have updated our desired orientation, but not yet changed the real
1085 * orientation our applied our screen rotation animation. For example, because a previous
1086 * screen rotation was in progress.
1087 *
1088 * @return {@code true} if the there is an ongoing rotation change.
1089 */
1090 boolean rotationNeedsUpdate() {
1091 final int lastOrientation = getLastOrientation();
1092 final int oldRotation = getRotation();
1093 final boolean oldAltOrientation = getAltOrientation();
1094
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001095 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Riddle Hsuad256a12018-07-18 16:11:30 +08001096 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
1097 lastOrientation, rotation);
1098 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1099 return false;
1100 }
1101 return true;
1102 }
1103
Riddle Hsu4e611772018-10-31 18:58:28 +08001104 /** Notify the configuration change of this display. */
1105 void sendNewConfiguration() {
1106 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, this).sendToTarget();
1107 }
1108
1109 /**
1110 * Determine the new desired orientation of this display.
1111 *
1112 * The orientation is computed from non-application windows first. If none of the
1113 * non-application windows specify orientation, the orientation is computed from application
1114 * tokens.
1115 *
1116 * @return {@code true} if the orientation is changed.
1117 */
1118 boolean updateOrientationFromAppTokens() {
1119 return updateOrientationFromAppTokens(false /* forceUpdate */);
1120 }
1121
1122 boolean updateOrientationFromAppTokens(boolean forceUpdate) {
1123 final int req = getOrientation();
1124 if (req != mLastOrientation || forceUpdate) {
1125 mLastOrientation = req;
1126 mDisplayRotation.setCurrentOrientation(req);
1127 return updateRotationUnchecked(forceUpdate);
1128 }
1129 return false;
1130 }
1131
Riddle Hsuad256a12018-07-18 16:11:30 +08001132 /**
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001133 * Update rotation of the display and send configuration if the rotation is changed.
1134 *
1135 * @return {@code true} if the rotation has been changed and the new config is sent.
1136 */
1137 boolean updateRotationAndSendNewConfigIfNeeded() {
1138 final boolean changed = updateRotationUnchecked(false /* forceUpdate */);
1139 if (changed) {
Riddle Hsu4e611772018-10-31 18:58:28 +08001140 sendNewConfiguration();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001141 }
1142 return changed;
1143 }
1144
1145 /**
Andrii Kulian06d07d62017-03-14 11:11:47 -07001146 * Update rotation of the display.
1147 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001148 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1149 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001150 */
Robert Carrae606b42018-02-15 15:36:23 -08001151 boolean updateRotationUnchecked() {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001152 return updateRotationUnchecked(false /* forceUpdate */);
1153 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001154
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001155 /**
Evan Roskye747c3e2018-10-30 20:06:41 -07001156 * Update rotation of the DisplayContent with an option to force the update. This updates
1157 * the container's perception of rotation and, depending on the top activities, will freeze
1158 * the screen or start seamless rotation. The display itself gets rotated in
1159 * {@link #applyRotationLocked} during {@link WindowManagerService#sendNewConfiguration}.
1160 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001161 * @param forceUpdate Force the rotation update. Sometimes in WM we might skip updating
1162 * orientation because we're waiting for some rotation to finish or display
1163 * to unfreeze, which results in configuration of the previously visible
1164 * activity being applied to a newly visible one. Forcing the rotation
1165 * update allows to workaround this issue.
1166 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
Evan Roskye747c3e2018-10-30 20:06:41 -07001167 * {@link WindowManagerService#sendNewConfiguration(int)} TO COMPLETE THE ROTATION AND
1168 * UNFREEZE THE SCREEN.
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001169 */
1170 boolean updateRotationUnchecked(boolean forceUpdate) {
1171 ScreenRotationAnimation screenRotationAnimation;
1172 if (!forceUpdate) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001173 if (mDeferredRotationPauseCount > 0) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001174 // Rotation updates have been paused temporarily. Defer the update until
1175 // updates have been resumed.
1176 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
1177 return false;
1178 }
1179
1180 screenRotationAnimation =
1181 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
1182 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
1183 // Rotation updates cannot be performed while the previous rotation change
1184 // animation is still in progress. Skip this update. We will try updating
1185 // again after the animation is finished and the display is unfrozen.
1186 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
1187 return false;
1188 }
1189 if (mService.mDisplayFrozen) {
1190 // Even if the screen rotation animation has finished (e.g. isAnimating
1191 // returns false), there is still some time where we haven't yet unfrozen
1192 // the display. We also need to abort rotation here.
1193 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
1194 "Deferring rotation, still finishing previous rotation");
1195 return false;
1196 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001197 }
1198
1199 if (!mService.mDisplayEnabled) {
1200 // No point choosing a rotation if the display is not enabled.
1201 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
1202 return false;
1203 }
1204
1205 final int oldRotation = mRotation;
1206 final int lastOrientation = mLastOrientation;
1207 final boolean oldAltOrientation = mAltOrientation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001208 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001209 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Computed rotation=" + rotation + " for display id="
1210 + mDisplayId + " based on lastOrientation=" + lastOrientation
1211 + " and oldRotation=" + oldRotation);
Riddle Hsuad256a12018-07-18 16:11:30 +08001212 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(mDisplayRotation,
1213 oldRotation, rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001214
Robert Carr0e007272017-10-02 13:00:55 -07001215 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001216 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001217 if (seamlessRotated != null && !forceUpdate) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001218 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1219 // to complete (that is, waiting for windows to redraw). It's tempting to check
1220 // w.mSeamlessRotationCount but that could be incorrect in the case of
1221 // window-removal.
1222 return false;
1223 }
Robert Carr0e007272017-10-02 13:00:55 -07001224
1225 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001226 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001227 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001228 mayRotateSeamlessly = false;
1229 }
1230 for (int i = 0; i < mService.mSessions.size(); i++) {
1231 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1232 mayRotateSeamlessly = false;
1233 break;
1234 }
1235 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001236 }
Robert Carr0e007272017-10-02 13:00:55 -07001237 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001238
1239 // TODO: Implement forced rotation changes.
1240 // Set mAltOrientation to indicate that the application is receiving
1241 // an orientation that has different metrics than it expected.
1242 // eg. Portrait instead of Landscape.
1243
Riddle Hsuad256a12018-07-18 16:11:30 +08001244 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
Andrii Kulian06d07d62017-03-14 11:11:47 -07001245 lastOrientation, rotation);
1246
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001247 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1248 + " selected orientation " + lastOrientation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001249 + ", got rotation " + rotation + " which has "
1250 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1251
1252 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1253 // No change.
1254 return false;
1255 }
1256
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001257 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1258 + " rotation changed to " + rotation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001259 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1260 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1261
1262 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1263 mService.mWaitingForConfig = true;
1264 }
1265
Evan Roskye747c3e2018-10-30 20:06:41 -07001266 mRotation = rotation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001267 mAltOrientation = altOrientation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001268
1269 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
Riddle Hsu654a6f92018-07-13 22:59:36 +08001270 mService.mH.sendNewMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1271 this, WINDOW_FREEZE_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001272
1273 setLayoutNeeded();
1274 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001275 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001276
1277 if (!rotateSeamlessly) {
Robert Carrae606b42018-02-15 15:36:23 -08001278 mService.startFreezingDisplayLocked(anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001279 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001280 } else {
1281 // The screen rotation animation uses a screenshot to freeze the screen
1282 // while windows resize underneath.
1283 // When we are rotating seamlessly, we allow the elements to transition
1284 // to their rotated state independently and without a freeze required.
Robert Carrefc75702018-02-16 11:46:16 -08001285 mService.startSeamlessRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001286 }
1287
Evan Roskye747c3e2018-10-30 20:06:41 -07001288 return true;
1289 }
1290
1291 /**
1292 * Applies the rotation transaction. This must be called after {@link #updateRotationUnchecked}
1293 * (if it returned {@code true}) to actually finish the rotation.
1294 *
1295 * @param oldRotation the rotation we are coming from.
1296 * @param rotation the rotation to apply.
1297 */
1298 void applyRotationLocked(final int oldRotation, final int rotation) {
1299 mDisplayRotation.setRotation(rotation);
1300 final boolean rotateSeamlessly = mService.isRotatingSeamlessly();
1301 ScreenRotationAnimation screenRotationAnimation = rotateSeamlessly
1302 ? null : mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001303 // We need to update our screen size information to match the new rotation. If the rotation
1304 // has actually changed then this method will return true and, according to the comment at
1305 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1306 // By updating the Display info here it will be available to
1307 // #computeScreenConfiguration() later.
1308 updateDisplayAndOrientation(getConfiguration().uiMode);
1309
Robert Carrae606b42018-02-15 15:36:23 -08001310 // NOTE: We disable the rotation in the emulator because
1311 // it doesn't support hardware OpenGL emulation yet.
1312 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1313 && screenRotationAnimation.hasScreenshot()) {
1314 if (screenRotationAnimation.setRotation(getPendingTransaction(), rotation,
1315 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1316 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1317 mService.scheduleAnimationLocked();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001318 }
1319 }
1320
Vishnu Nair83537a72018-07-19 21:27:48 -07001321 forAllWindows(w -> {
Vishnu Nairddd80742018-08-21 14:12:46 -07001322 w.seamlesslyRotateIfAllowed(getPendingTransaction(), oldRotation, rotation,
1323 rotateSeamlessly);
Vishnu Nair83537a72018-07-19 21:27:48 -07001324 }, true /* traverseTopToBottom */);
1325
Robert Carrae606b42018-02-15 15:36:23 -08001326 mService.mDisplayManagerInternal.performTraversal(getPendingTransaction());
1327 scheduleAnimation();
1328
Andrii Kulian06d07d62017-03-14 11:11:47 -07001329 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001330 if (w.mHasSurface && !rotateSeamlessly) {
1331 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001332 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001333 mService.mRoot.mOrientationChangeComplete = false;
1334 w.mLastFreezeDuration = 0;
1335 }
1336 w.mReportOrientationChanged = true;
1337 }, true /* traverseTopToBottom */);
1338
1339 if (rotateSeamlessly) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001340 mService.mH.sendNewMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1341 this, SEAMLESS_ROTATION_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001342 }
1343
1344 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1345 final WindowManagerService.RotationWatcher rotationWatcher
1346 = mService.mRotationWatchers.get(i);
1347 if (rotationWatcher.mDisplayId == mDisplayId) {
1348 try {
1349 rotationWatcher.mWatcher.onRotationChanged(rotation);
1350 } catch (RemoteException e) {
1351 // Ignore
1352 }
1353 }
1354 }
1355
1356 // TODO (multi-display): Magnification is supported only for the default display.
1357 // Announce rotation only if we will not animate as we already have the
1358 // windows in final state. Otherwise, we make this call at the rotation end.
1359 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1360 && isDefaultDisplay) {
1361 mService.mAccessibilityController.onRotationChangedLocked(this);
1362 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001363 }
1364
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001365 void configureDisplayPolicy() {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001366 final int width = mBaseDisplayWidth;
1367 final int height = mBaseDisplayHeight;
1368 final int shortSize;
1369 final int longSize;
1370 if (width > height) {
1371 shortSize = height;
1372 longSize = width;
1373 } else {
1374 shortSize = width;
1375 longSize = height;
1376 }
1377
1378 final int shortSizeDp = shortSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1379 final int longSizeDp = longSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1380
1381 mDisplayRotation.configure(width, height, shortSizeDp, longSizeDp);
1382 mDisplayPolicy.configure(width, height, shortSizeDp);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001383
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001384 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1385 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Arthur Hungbe5ce212018-09-13 18:41:56 +08001386
1387 // Tap Listeners are supported for:
1388 // 1. All physical displays (multi-display).
1389 // 2. VirtualDisplays on VR, AA (and everything else).
1390 if (mPointerEventDispatcher != null && mTapDetector == null) {
1391 if (DEBUG_DISPLAY) {
1392 Slog.d(TAG,
1393 "Registering PointerEventListener for DisplayId: " + mDisplayId);
1394 }
1395 mTapDetector = new TaskTapPointerEventListener(mService, this);
1396 registerPointerEventListener(mTapDetector);
1397 registerPointerEventListener(mService.mMousePositionTracker);
1398 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001399 }
1400
Andrii Kulian06d07d62017-03-14 11:11:47 -07001401 /**
1402 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1403 * changed.
1404 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1405 */
1406 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1407 // Use the effective "visual" dimensions based on current rotation
1408 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1409 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1410 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1411 int dw = realdw;
1412 int dh = realdh;
1413
1414 if (mAltOrientation) {
1415 if (realdw > realdh) {
1416 // Turn landscape into portrait.
1417 int maxw = (int)(realdh/1.3f);
1418 if (maxw < realdw) {
1419 dw = maxw;
1420 }
1421 } else {
1422 // Turn portrait into landscape.
1423 int maxh = (int)(realdw/1.3f);
1424 if (maxh < realdh) {
1425 dh = maxh;
1426 }
1427 }
1428 }
1429
1430 // Update application display metrics.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001431 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(mRotation);
1432 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1433
Andrii Kulian06d07d62017-03-14 11:11:47 -07001434 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001435 mDisplayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001436 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001437 mDisplayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001438 mDisplayInfo.rotation = mRotation;
1439 mDisplayInfo.logicalWidth = dw;
1440 mDisplayInfo.logicalHeight = dh;
1441 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1442 mDisplayInfo.appWidth = appWidth;
1443 mDisplayInfo.appHeight = appHeight;
1444 if (isDefaultDisplay) {
1445 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1446 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1447 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001448 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001449 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1450 if (mDisplayScalingDisabled) {
1451 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1452 } else {
1453 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1454 }
1455
Andrii Kulianf0379de2018-03-14 16:24:07 -07001456 // We usually set the override info in DisplayManager so that we get consistent display
1457 // metrics values when displays are changing and don't send out new values until WM is aware
1458 // of them. However, we don't do this for displays that serve as containers for ActivityView
1459 // because we don't want letter-/pillar-boxing during resize.
1460 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1461 ? mDisplayInfo : null;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001462 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001463 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001464
1465 mBaseDisplayRect.set(0, 0, dw, dh);
1466
1467 if (isDefaultDisplay) {
1468 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1469 mCompatDisplayMetrics);
1470 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001471
Andrii Kulian06d07d62017-03-14 11:11:47 -07001472 return mDisplayInfo;
1473 }
1474
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001475 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001476 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1477 }
1478
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001479 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001480 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001481 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001482 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001483 }
Adrian Roos11c25582018-02-19 18:06:36 +01001484 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001485 return WmDisplayCutout.computeSafeInsets(
1486 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001487 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001488 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Issei Suzuki43190bd2018-08-20 17:28:41 +02001489 final Rect[] newBounds = mRotationUtil.getRotatedBounds(
1490 WmDisplayCutout.computeSafeInsets(
Jorim Jaggi60640512018-06-29 01:14:31 +02001491 cutout, mInitialDisplayWidth, mInitialDisplayHeight)
Issei Suzuki43190bd2018-08-20 17:28:41 +02001492 .getDisplayCutout().getBoundingRectsAll(),
1493 rotation, mInitialDisplayWidth, mInitialDisplayHeight);
1494 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(newBounds),
Adrian Roos24264212018-02-19 16:26:15 +01001495 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1496 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001497 }
1498
Andrii Kulian06d07d62017-03-14 11:11:47 -07001499 /**
1500 * Compute display configuration based on display properties and policy settings.
1501 * Do not call if mDisplayReady == false.
1502 */
1503 void computeScreenConfiguration(Configuration config) {
1504 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
Evan Roskye747c3e2018-10-30 20:06:41 -07001505 calculateBounds(displayInfo, mTmpBounds);
1506 config.windowConfiguration.setBounds(mTmpBounds);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001507
1508 final int dw = displayInfo.logicalWidth;
1509 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001510 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Garfield Tane0846042018-07-26 13:42:04 -07001511 config.windowConfiguration.setWindowingMode(getWindowingMode());
Evan Roskye747c3e2018-10-30 20:06:41 -07001512 config.windowConfiguration.setRotation(displayInfo.rotation);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001513
Adrian Roos11c25582018-02-19 18:06:36 +01001514 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001515 config.screenWidthDp =
1516 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001517 config.uiMode, mDisplayId, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001518 config.screenHeightDp =
1519 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001520 config.uiMode, mDisplayId, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001521
Adrian Roos11c25582018-02-19 18:06:36 +01001522 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
1523 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001524 final int leftInset = mTmpRect.left;
1525 final int topInset = mTmpRect.top;
1526 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001527 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1528 leftInset + displayInfo.appWidth /* right */,
1529 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001530 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1531 || displayInfo.rotation == Surface.ROTATION_270);
1532
1533 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001534 density, config);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001535
1536 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1537 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1538 ? Configuration.SCREENLAYOUT_ROUND_YES
1539 : Configuration.SCREENLAYOUT_ROUND_NO);
1540
1541 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1542 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1543 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
Evan Rosky39b6f232018-10-30 18:35:41 -07001544 dh, displayInfo.displayCutout, mDisplayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001545 config.densityDpi = displayInfo.logicalDensityDpi;
1546
1547 config.colorMode =
Chia-I Wuf739bad2018-05-09 12:36:57 -07001548 ((displayInfo.isHdr() && mService.hasHdrSupport())
Andrii Kulian06d07d62017-03-14 11:11:47 -07001549 ? Configuration.COLOR_MODE_HDR_YES
1550 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001551 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001552 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1553 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1554
1555 // Update the configuration based on available input devices, lid switch,
1556 // and platform configuration.
1557 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1558 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1559 config.navigation = Configuration.NAVIGATION_NONAV;
1560
1561 int keyboardPresence = 0;
1562 int navigationPresence = 0;
1563 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1564 final int len = devices != null ? devices.length : 0;
1565 for (int i = 0; i < len; i++) {
1566 InputDevice device = devices[i];
1567 if (!device.isVirtual()) {
1568 final int sources = device.getSources();
1569 final int presenceFlag = device.isExternal() ?
1570 WindowManagerPolicy.PRESENCE_EXTERNAL :
1571 WindowManagerPolicy.PRESENCE_INTERNAL;
1572
1573 // TODO(multi-display): Configure on per-display basis.
1574 if (mService.mIsTouchDevice) {
1575 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1576 InputDevice.SOURCE_TOUCHSCREEN) {
1577 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1578 }
1579 } else {
1580 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1581 }
1582
1583 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1584 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1585 navigationPresence |= presenceFlag;
1586 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1587 && config.navigation == Configuration.NAVIGATION_NONAV) {
1588 config.navigation = Configuration.NAVIGATION_DPAD;
1589 navigationPresence |= presenceFlag;
1590 }
1591
1592 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1593 config.keyboard = Configuration.KEYBOARD_QWERTY;
1594 keyboardPresence |= presenceFlag;
1595 }
1596 }
1597 }
1598
1599 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1600 config.navigation = Configuration.NAVIGATION_DPAD;
1601 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1602 }
1603
1604 // Determine whether a hard keyboard is available and enabled.
1605 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1606 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1607 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1608 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1609 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1610 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1611 }
1612
1613 // Let the policy update hidden states.
1614 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1615 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1616 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1617 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1618 }
1619
1620 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
Evan Rosky39b6f232018-10-30 18:35:41 -07001621 DisplayCutout displayCutout, int displayId) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001622 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1623 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1624 final int unrotDw, unrotDh;
1625 if (rotated) {
1626 unrotDw = dh;
1627 unrotDh = dw;
1628 } else {
1629 unrotDw = dw;
1630 unrotDh = dh;
1631 }
1632 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
Evan Rosky39b6f232018-10-30 18:35:41 -07001633 displayCutout, displayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001634 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
Evan Rosky39b6f232018-10-30 18:35:41 -07001635 displayCutout, displayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001636 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
Evan Rosky39b6f232018-10-30 18:35:41 -07001637 displayCutout, displayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001638 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
Evan Rosky39b6f232018-10-30 18:35:41 -07001639 displayCutout, displayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001640 return sw;
1641 }
1642
1643 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
Evan Rosky39b6f232018-10-30 18:35:41 -07001644 DisplayMetrics dm, int dw, int dh, DisplayCutout displayCutout, int displayId) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001645 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
Evan Rosky39b6f232018-10-30 18:35:41 -07001646 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001647 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
Evan Rosky39b6f232018-10-30 18:35:41 -07001648 uiMode, displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001649 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1650 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1651 if (curSize == 0 || size < curSize) {
1652 curSize = size;
1653 }
1654 return curSize;
1655 }
1656
1657 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1658 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1659
1660 // We need to determine the smallest width that will occur under normal
1661 // operation. To this, start with the base screen size and compute the
1662 // width under the different possible rotations. We need to un-rotate
1663 // the current screen dimensions before doing this.
1664 int unrotDw, unrotDh;
1665 if (rotated) {
1666 unrotDw = dh;
1667 unrotDh = dw;
1668 } else {
1669 unrotDw = dw;
1670 unrotDh = dh;
1671 }
1672 displayInfo.smallestNominalAppWidth = 1<<30;
1673 displayInfo.smallestNominalAppHeight = 1<<30;
1674 displayInfo.largestNominalAppWidth = 0;
1675 displayInfo.largestNominalAppHeight = 0;
1676 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1677 unrotDh);
1678 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1679 unrotDw);
1680 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1681 unrotDh);
1682 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1683 unrotDw);
1684 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1685 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
Evan Rosky39b6f232018-10-30 18:35:41 -07001686 displayInfo.displayCutout, displayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001687 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
Evan Rosky39b6f232018-10-30 18:35:41 -07001688 displayInfo.displayCutout, displayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001689 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
Evan Rosky39b6f232018-10-30 18:35:41 -07001690 displayInfo.displayCutout, displayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001691 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
Evan Rosky39b6f232018-10-30 18:35:41 -07001692 displayInfo.displayCutout, displayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001693 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1694 outConfig.screenLayout = sl;
1695 }
1696
1697 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
Evan Rosky39b6f232018-10-30 18:35:41 -07001698 int uiMode, DisplayCutout displayCutout, int displayId) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001699 // Get the app screen size at this rotation.
Adrian Roos11c25582018-02-19 18:06:36 +01001700 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId,
Evan Rosky39b6f232018-10-30 18:35:41 -07001701 displayCutout);
Adrian Roos11c25582018-02-19 18:06:36 +01001702 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId,
Evan Rosky39b6f232018-10-30 18:35:41 -07001703 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001704
1705 // Compute the screen layout size class for this rotation.
1706 int longSize = w;
1707 int shortSize = h;
1708 if (longSize < shortSize) {
1709 int tmp = longSize;
1710 longSize = shortSize;
1711 shortSize = tmp;
1712 }
1713 longSize = (int)(longSize/density);
1714 shortSize = (int)(shortSize/density);
1715 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1716 }
1717
1718 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1719 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001720 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1721 rotation).getDisplayCutout();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001722 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001723 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001724 if (width < displayInfo.smallestNominalAppWidth) {
1725 displayInfo.smallestNominalAppWidth = width;
1726 }
1727 if (width > displayInfo.largestNominalAppWidth) {
1728 displayInfo.largestNominalAppWidth = width;
1729 }
1730 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001731 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001732 if (height < displayInfo.smallestNominalAppHeight) {
1733 displayInfo.smallestNominalAppHeight = height;
1734 }
1735 if (height > displayInfo.largestNominalAppHeight) {
1736 displayInfo.largestNominalAppHeight = height;
1737 }
1738 }
1739
Riddle Hsua4d6fa22018-08-11 00:50:39 +08001740 /**
1741 * Apps that use the compact menu panel (as controlled by the panelMenuIsCompact
1742 * theme attribute) on devices that feature a physical options menu key attempt to position
1743 * their menu panel window along the edge of the screen nearest the physical menu key.
1744 * This lowers the travel distance between invoking the menu panel and selecting
1745 * a menu option.
1746 *
1747 * This method helps control where that menu is placed. Its current implementation makes
1748 * assumptions about the menu key and its relationship to the screen based on whether
1749 * the device's natural orientation is portrait (width < height) or landscape.
1750 *
1751 * The menu key is assumed to be located along the bottom edge of natural-portrait
1752 * devices and along the right edge of natural-landscape devices. If these assumptions
1753 * do not hold for the target device, this method should be changed to reflect that.
1754 *
1755 * @return A {@link Gravity} value for placing the options menu window.
1756 */
1757 int getPreferredOptionsPanelGravity() {
1758 final int rotation = getRotation();
1759 if (mInitialDisplayWidth < mInitialDisplayHeight) {
1760 // On devices with a natural orientation of portrait.
1761 switch (rotation) {
1762 default:
1763 case Surface.ROTATION_0:
1764 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1765 case Surface.ROTATION_90:
1766 return Gravity.RIGHT | Gravity.BOTTOM;
1767 case Surface.ROTATION_180:
1768 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1769 case Surface.ROTATION_270:
1770 return Gravity.START | Gravity.BOTTOM;
1771 }
1772 }
1773
1774 // On devices with a natural orientation of landscape.
1775 switch (rotation) {
1776 default:
1777 case Surface.ROTATION_0:
1778 return Gravity.RIGHT | Gravity.BOTTOM;
1779 case Surface.ROTATION_90:
1780 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1781 case Surface.ROTATION_180:
1782 return Gravity.START | Gravity.BOTTOM;
1783 case Surface.ROTATION_270:
1784 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1785 }
1786 }
1787
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001788 DockedStackDividerController getDockedDividerController() {
1789 return mDividerControllerLocked;
1790 }
1791
Winson Chung655332c2016-10-31 13:14:28 -07001792 PinnedStackController getPinnedStackController() {
1793 return mPinnedStackControllerLocked;
1794 }
1795
Jeff Browna506a6e2013-06-04 00:02:38 -07001796 /**
1797 * Returns true if the specified UID has access to this display.
1798 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001799 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001800 return mDisplay.hasAccess(uid);
1801 }
1802
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001803 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001804 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001805 }
1806
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001807 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001808 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001809 }
1810
Wale Ogunwale61911492017-10-11 08:50:50 -07001811 /**
1812 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1813 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001814 TaskStack getSplitScreenPrimaryStack() {
1815 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001816 return (stack != null && stack.isVisible()) ? stack : null;
1817 }
1818
Robert Carr9785cf32018-04-25 15:06:07 -07001819 boolean hasSplitScreenPrimaryStack() {
1820 return getSplitScreenPrimaryStack() != null;
1821 }
1822
Wale Ogunwale61911492017-10-11 08:50:50 -07001823 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001824 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001825 * not visible.
1826 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001827 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1828 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001829 }
1830
1831 TaskStack getPinnedStack() {
1832 return mTaskStackContainers.getPinnedStack();
1833 }
1834
1835 private boolean hasPinnedStack() {
1836 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001837 }
1838
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001839 /**
1840 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1841 * Null is no compatible stack on the display.
1842 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001843 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001844 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1845 }
1846
1847 /**
1848 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1849 * activity type. Null is no compatible stack on the display.
1850 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001851 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001852 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001853 }
1854
Bryce Lee48f4b572017-04-10 10:54:15 -07001855 @VisibleForTesting
Kazuki Takise148d00a2018-05-31 15:32:19 +09001856 WindowList<TaskStack> getStacks() {
1857 return mTaskStackContainers.mChildren;
1858 }
1859
1860 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001861 TaskStack getTopStack() {
1862 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001863 }
1864
Winson Chunge2d72172018-01-25 17:46:20 +00001865 ArrayList<Task> getVisibleTasks() {
1866 return mTaskStackContainers.getVisibleTasks();
1867 }
1868
Wale Ogunwale61911492017-10-11 08:50:50 -07001869 void onStackWindowingModeChanged(TaskStack stack) {
1870 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001871 }
1872
Andrii Kulian441e4492016-09-29 15:25:00 -07001873 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001874 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001875 super.onConfigurationChanged(newParentConfig);
1876
Evan Roskye747c3e2018-10-30 20:06:41 -07001877 // If there was no pinned stack, we still need to notify the controller of the display info
1878 // update as a result of the config change.
1879 if (mPinnedStackControllerLocked != null && !hasPinnedStack()) {
1880 mPinnedStackControllerLocked.onDisplayInfoChanged(getDisplayInfo());
1881 }
1882
Andrii Kulian3a507b52016-09-19 18:14:12 -07001883 // The display size information is heavily dependent on the resources in the current
1884 // configuration, so we need to reconfigure it every time the configuration changes.
Riddle Hsuf53da812018-08-15 22:00:27 +08001885 // See {@link #configureDisplayPolicy}...sigh...
Andrii Kulian3a507b52016-09-19 18:14:12 -07001886 mService.reconfigureDisplayLocked(this);
1887
Evan Roskye747c3e2018-10-30 20:06:41 -07001888 }
1889
1890 /**
1891 * Updates the resources used by docked/pinned controllers. This needs to be called at the
1892 * beginning of a configuration update cascade since the metrics from these resources are used
1893 * for bounds calculations. Since ActivityDisplay initiates the configuration update, this
1894 * should be called from there instead of DisplayContent's onConfigurationChanged.
1895 */
1896 void preOnConfigurationChanged() {
Bryce Leef3c6a472017-11-14 14:53:06 -08001897 final DockedStackDividerController dividerController = getDockedDividerController();
1898
1899 if (dividerController != null) {
1900 getDockedDividerController().onConfigurationChanged();
1901 }
1902
1903 final PinnedStackController pinnedStackController = getPinnedStackController();
1904
1905 if (pinnedStackController != null) {
1906 getPinnedStackController().onConfigurationChanged();
1907 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001908 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001909
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001910 @Override
1911 boolean fillsParent() {
1912 return true;
1913 }
1914
1915 @Override
1916 boolean isVisible() {
1917 return true;
1918 }
1919
1920 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001921 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001922 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001923 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001924 }
1925
Robert Carr9785cf32018-04-25 15:06:07 -07001926 /**
1927 * In split-screen mode we process the IME containers above the docked divider
1928 * rather than directly above their target.
1929 */
1930 private boolean skipTraverseChild(WindowContainer child) {
1931 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null
1932 && !hasSplitScreenPrimaryStack()) {
1933 return true;
1934 }
1935 return false;
1936 }
1937
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001938 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001939 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1940 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1941 // target, or here in order if there isn't an IME target.
1942 if (traverseTopToBottom) {
1943 for (int i = mChildren.size() - 1; i >= 0; --i) {
1944 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001945 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001946 continue;
1947 }
Robert Carr9785cf32018-04-25 15:06:07 -07001948
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001949 if (child.forAllWindows(callback, traverseTopToBottom)) {
1950 return true;
1951 }
1952 }
1953 } else {
1954 final int count = mChildren.size();
1955 for (int i = 0; i < count; i++) {
1956 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001957 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001958 continue;
1959 }
Robert Carr9785cf32018-04-25 15:06:07 -07001960
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001961 if (child.forAllWindows(callback, traverseTopToBottom)) {
1962 return true;
1963 }
1964 }
1965 }
1966 return false;
1967 }
1968
1969 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1970 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1971 }
1972
Wale Ogunwale399c8692017-05-08 14:22:42 -07001973 @Override
1974 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001975 final WindowManagerPolicy policy = mService.mPolicy;
1976
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001977 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001978 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001979 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1980 + " is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001981 // If the display is frozen, some activities may be in the middle of restarting, and
1982 // thus have removed their old window. If the window has the flag to hide the lock
1983 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1984 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001985 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001986 } else if (policy.isKeyguardLocked()) {
1987 // Use the last orientation the while the display is frozen with the keyguard
1988 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1989 // window. We don't want to check the show when locked window directly though as
1990 // things aren't stable while the display is frozen, for example the window could be
1991 // momentarily unavailable due to activity relaunch.
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001992 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1993 + " is frozen while keyguard locked, return " + mLastOrientation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001994 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001995 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001996 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001997 final int orientation = mAboveAppWindowsContainers.getOrientation();
1998 if (orientation != SCREEN_ORIENTATION_UNSET) {
1999 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002000 }
Wale Ogunwale51362492016-09-08 17:49:17 -07002001 }
2002
Wale Ogunwale399c8692017-05-08 14:22:42 -07002003 // Top system windows are not requesting an orientation. Start searching from apps.
2004 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07002005 }
2006
Craig Mautner46ac6fa2013-08-01 10:06:34 -07002007 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07002008 // Check if display metrics changed and update base values if needed.
2009 updateBaseDisplayMetricsIfNeeded();
2010
Craig Mautner722285e2012-09-07 13:55:58 -07002011 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07002012 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07002013
Wale Ogunwale61911492017-10-11 08:50:50 -07002014 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2015 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002016 }
Craig Mautner722285e2012-09-07 13:55:58 -07002017 }
2018
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002019 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002020 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
2021 if (displayManagerInternal != null) {
2022 // Bootstrap the default logical display from the display manager.
2023 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
2024 if (newDisplayInfo != null) {
2025 mDisplayInfo.copyFrom(newDisplayInfo);
2026 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002027 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002028
Andrii Kuliancd097992017-03-23 18:31:59 -07002029 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
2030 mDisplayInfo.logicalDensityDpi);
2031 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
2032 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
2033 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002034 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002035 }
2036
Andrii Kuliancd097992017-03-23 18:31:59 -07002037 /**
2038 * If display metrics changed, overrides are not set and it's not just a rotation - update base
2039 * values.
2040 */
2041 private void updateBaseDisplayMetricsIfNeeded() {
2042 // Get real display metrics without overrides from WM.
2043 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
2044 final int orientation = mDisplayInfo.rotation;
2045 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2046 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
2047 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
2048 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002049 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07002050
2051 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
2052 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01002053 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
2054 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07002055
2056 if (displayMetricsChanged) {
2057 // Check if display size or density is forced.
2058 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
2059 || mBaseDisplayHeight != mInitialDisplayHeight;
2060 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
2061
2062 // If there is an override set for base values - use it, otherwise use new values.
2063 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
2064 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
2065 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
2066
2067 // Real display metrics changed, so we should also update initial values.
2068 mInitialDisplayWidth = newWidth;
2069 mInitialDisplayHeight = newHeight;
2070 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01002071 mInitialDisplayCutout = newCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07002072 mService.reconfigureDisplayLocked(this);
2073 }
2074 }
2075
Bryce Lee27cec322017-03-21 09:41:37 -07002076 /** Sets the maximum width the screen resolution can be */
2077 void setMaxUiWidth(int width) {
2078 if (DEBUG_DISPLAY) {
2079 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
2080 }
2081
2082 mMaxUiWidth = width;
2083
2084 // Update existing metrics.
2085 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
2086 }
2087
2088 /** Update base (override) display metrics. */
2089 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
2090 mBaseDisplayWidth = baseWidth;
2091 mBaseDisplayHeight = baseHeight;
2092 mBaseDisplayDensity = baseDensity;
2093
2094 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
2095 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
2096 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
2097 mBaseDisplayWidth = mMaxUiWidth;
2098
2099 if (DEBUG_DISPLAY) {
2100 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
2101 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
2102 + " on display:" + getDisplayId());
2103 }
2104 }
2105
2106 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08002107
2108 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07002109 }
2110
Riddle Hsuf53da812018-08-15 22:00:27 +08002111 /**
2112 * Forces this display to use the specified density.
2113 *
2114 * @param density The density in DPI to use. If the value equals to initial density, the setting
2115 * will be cleared.
2116 * @param userId The target user to apply. Only meaningful when this is default display. If the
2117 * user id is {@link UserHandle#USER_CURRENT}, it means to apply current settings
2118 * so only need to configure display.
2119 */
2120 void setForcedDensity(int density, int userId) {
2121 final boolean clear = density == mInitialDisplayDensity;
2122 final boolean updateCurrent = userId == UserHandle.USER_CURRENT;
2123 if (mService.mCurrentUserId == userId || updateCurrent) {
2124 mBaseDisplayDensity = density;
2125 mService.reconfigureDisplayLocked(this);
2126 }
2127 if (updateCurrent) {
2128 // We are applying existing settings so no need to save it again.
2129 return;
2130 }
2131
2132 if (density == mInitialDisplayDensity) {
2133 density = 0;
2134 }
Chilun8753ad32018-10-09 15:56:45 +08002135 mService.mDisplayWindowSettings.setForcedDensity(this, density, userId);
Riddle Hsuf53da812018-08-15 22:00:27 +08002136 }
2137
2138 /** @param mode {@link #FORCE_SCALING_MODE_AUTO} or {@link #FORCE_SCALING_MODE_DISABLED}. */
2139 void setForcedScalingMode(@ForceScalingMode int mode) {
2140 if (mode != FORCE_SCALING_MODE_DISABLED) {
2141 mode = FORCE_SCALING_MODE_AUTO;
2142 }
2143
2144 mDisplayScalingDisabled = (mode != FORCE_SCALING_MODE_AUTO);
2145 Slog.i(TAG_WM, "Using display scaling mode: " + (mDisplayScalingDisabled ? "off" : "auto"));
2146 mService.reconfigureDisplayLocked(this);
2147
Chilun8753ad32018-10-09 15:56:45 +08002148 mService.mDisplayWindowSettings.setForcedScalingMode(this, mode);
Riddle Hsuf53da812018-08-15 22:00:27 +08002149 }
2150
2151 /** If the given width and height equal to initial size, the setting will be cleared. */
2152 void setForcedSize(int width, int height) {
2153 final boolean clear = mInitialDisplayWidth == width && mInitialDisplayHeight == height;
2154 if (!clear) {
2155 // Set some sort of reasonable bounds on the size of the display that we will try
2156 // to emulate.
2157 final int minSize = 200;
2158 final int maxScale = 2;
2159 width = Math.min(Math.max(width, minSize), mInitialDisplayWidth * maxScale);
2160 height = Math.min(Math.max(height, minSize), mInitialDisplayHeight * maxScale);
2161 }
2162
2163 Slog.i(TAG_WM, "Using new display size: " + width + "x" + height);
2164 updateBaseDisplayMetrics(width, height, mBaseDisplayDensity);
2165 mService.reconfigureDisplayLocked(this);
2166
2167 if (clear) {
2168 width = height = 0;
2169 }
Chilun8753ad32018-10-09 15:56:45 +08002170 mService.mDisplayWindowSettings.setForcedSize(this, width, height);
Riddle Hsuf53da812018-08-15 22:00:27 +08002171 }
2172
Wale Ogunwaledb506192017-12-08 10:57:32 -08002173 void getStableRect(Rect out) {
2174 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08002175 }
2176
Wale Ogunwale61911492017-10-11 08:50:50 -07002177 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002178 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
2179 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002180
Wale Ogunwale61911492017-10-11 08:50:50 -07002181 final TaskStack stack = new TaskStack(mService, stackId, controller);
2182 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002183 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07002184 }
2185
Andrii Kulian51c1b672017-04-07 18:39:32 -07002186 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002187 final DisplayContent prevDc = stack.getDisplayContent();
2188 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002189 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
2190 + " which is not currently attached to any display");
2191 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002192 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07002193 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
2194 + " to its current displayId=" + mDisplayId);
2195 }
2196
lumark588a3e82018-07-20 18:53:54 +08002197 // Clean up all pending transitions when stack reparent to another display.
2198 stack.forAllAppWindows(AppWindowToken::removeFromPendingTransition);
2199
Wale Ogunwale61911492017-10-11 08:50:50 -07002200 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07002201 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08002202 }
2203
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002204 @Override
2205 protected void addChild(DisplayChildWindowContainer child,
2206 Comparator<DisplayChildWindowContainer> comparator) {
2207 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2208 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002209
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002210 @Override
2211 protected void addChild(DisplayChildWindowContainer child, int index) {
2212 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2213 }
2214
2215 @Override
2216 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002217 // Only allow removal of direct children from this display if the display is in the process
2218 // of been removed.
2219 if (mRemovingDisplay) {
2220 super.removeChild(child);
2221 return;
2222 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002223 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002224 }
2225
Andrii Kuliand2765632016-12-12 22:26:34 -08002226 @Override
2227 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
2228 // Children of the display are statically ordered, so the real intention here is to perform
2229 // the operation on the display and not the static direct children.
2230 getParent().positionChildAt(position, this, includingParents);
2231 }
2232
Riddle Hsu57831b52018-07-27 00:31:48 +08002233 void positionStackAt(int position, TaskStack child, boolean includingParents) {
2234 mTaskStackContainers.positionChildAt(position, child, includingParents);
Winson Chung59a47ded2018-01-25 17:46:06 +00002235 layoutAndAssignWindowLayersIfNeeded();
2236 }
2237
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002238 /**
2239 * Used to obtain task ID when user taps on coordinate (x, y) in this display, and outside
2240 * current task in focus.
2241 *
2242 * This returns the task ID of the foremost task at (x, y) if the task is not home. Otherwise it
2243 * returns -1.
2244 *
2245 * @param x horizontal coordinate of the tap position
2246 * @param y vertical coordinate of the tap position
2247 * @return the task ID if a non-home task is found; -1 if not
2248 */
2249 int taskForTapOutside(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002250 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2251 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Garfield Tanf2e0a9e2018-10-16 09:42:02 -07002252 if (stack.isActivityTypeHome() && !stack.inMultiWindowMode()) {
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002253 // We skip not only home stack, but also everything behind home because user can't
Garfield Tanf2e0a9e2018-10-16 09:42:02 -07002254 // see them when home stack is isn't in multi-window mode.
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002255 break;
2256 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002257 final int taskId = stack.taskIdFromPoint(x, y);
2258 if (taskId != -1) {
2259 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07002260 }
2261 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002262 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07002263 }
2264
Chong Zhang8e89b312015-09-09 15:09:30 -07002265 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08002266 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07002267 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07002268 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07002269 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002270 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002271 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07002272 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2273 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002274 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002275 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002276 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002277
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002278 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
2279 if (mTmpTaskForResizePointSearchResult.searchDone) {
2280 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07002281 }
2282 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002283 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002284 }
2285
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002286 void updateTouchExcludeRegion() {
2287 final Task focusedTask = (mFocusedApp != null ? mFocusedApp.getTask() : null);
David Stevensee9e2772017-02-09 16:30:27 -08002288 if (focusedTask == null) {
2289 mTouchExcludeRegion.setEmpty();
2290 } else {
2291 mTouchExcludeRegion.set(mBaseDisplayRect);
2292 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
2293 mTmpRect2.setEmpty();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002294 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0;
2295 --stackNdx) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002296 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002297 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
2298 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08002299 }
2300 // If we removed the focused task above, add it back and only leave its
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002301 // outside touch area in the exclusion. TapDetector is not interested in
David Stevensee9e2772017-02-09 16:30:27 -08002302 // any touch inside the focused task itself.
2303 if (!mTmpRect2.isEmpty()) {
2304 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
2305 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08002306 }
lumark90120a82018-08-15 00:33:03 +08002307 if (mInputMethodWindow != null && mInputMethodWindow.isVisibleLw()) {
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002308 // If the input method is visible and the user is typing, we don't want these touch
2309 // events to be intercepted and used to change focus. This would likely cause a
2310 // disappearance of the input method.
lumark90120a82018-08-15 00:33:03 +08002311 mInputMethodWindow.getTouchableRegion(mTmpRegion);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002312 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002313 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002314 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002315 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002316 win.getTouchableRegion(mTmpRegion);
2317 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2318 }
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002319 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2320 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2321 win.amendTapExcludeRegion(mTouchExcludeRegion);
2322 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08002323 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002324 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002325 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002326 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002327 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2328 }
Craig Mautner1bef3892015-02-17 15:09:47 -08002329 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07002330 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08002331 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07002332 }
2333
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002334 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07002335 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002336 super.switchUser();
2337 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07002338 }
2339
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07002340 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002341 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2342 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07002343 }
2344 }
2345
Wale Ogunwale10124582016-09-15 20:25:50 -07002346 @Override
2347 void removeIfPossible() {
2348 if (isAnimating()) {
2349 mDeferredRemoval = true;
2350 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002351 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002352 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002353 }
2354
Wale Ogunwale10124582016-09-15 20:25:50 -07002355 @Override
2356 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002357 mRemovingDisplay = true;
2358 try {
lumark588a3e82018-07-20 18:53:54 +08002359 // Clear all transitions & screen frozen states when removing display.
2360 mOpeningApps.clear();
2361 mClosingApps.clear();
2362 mUnknownAppVisibilityController.clear();
2363 mAppTransition.removeAppTransitionTimeoutCallbacks();
2364 handleAnimatingStoppedAndTransition();
2365 mService.stopFreezingDisplayLocked();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002366 super.removeImmediately();
2367 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Arthur Hungbe5ce212018-09-13 18:41:56 +08002368 if (mPointerEventDispatcher != null && mTapDetector != null) {
2369 unregisterPointerEventListener(mTapDetector);
2370 unregisterPointerEventListener(mService.mMousePositionTracker);
2371 mTapDetector = null;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002372 }
Jorim Jaggia3844032018-01-03 15:54:43 +01002373 mService.mAnimator.removeDisplayLocked(mDisplayId);
Louis Chang207f9112018-07-30 08:58:54 +08002374 mWindowingLayer.release();
2375 mOverlayLayer.release();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002376 } finally {
Riddle Hsu4e611772018-10-31 18:58:28 +08002377 mDisplayReady = false;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002378 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002379 }
Bryce Leef19cbe22018-02-02 15:09:21 -08002380
Arthur Hung39134b22018-08-14 11:58:28 +08002381 mInputMonitor.onRemoved();
Riddle Hsuf53da812018-08-15 22:00:27 +08002382 mService.mWindowPlacerLocked.requestTraversal();
Craig Mautner95da1082014-02-24 17:54:35 -08002383 }
2384
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002385 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002386 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002387 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002388 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2389
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002390 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002391 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002392 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002393 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002394 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002395 }
2396
Andrii Kulian0214ed92017-05-16 13:44:05 -07002397 /** @return 'true' if removal of this display content is deferred due to active animation. */
2398 boolean isRemovalDeferred() {
2399 return mDeferredRemoval;
2400 }
2401
Wale Ogunwale10124582016-09-15 20:25:50 -07002402 boolean animateForIme(float interpolatedValue, float animationTarget,
2403 float dividerAnimationTarget) {
2404 boolean updated = false;
2405
Wale Ogunwale61911492017-10-11 08:50:50 -07002406 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2407 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002408 if (stack == null || !stack.isAdjustedForIme()) {
2409 continue;
2410 }
2411
2412 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2413 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2414 updated = true;
2415 } else {
2416 mDividerControllerLocked.mLastAnimationProgress =
2417 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2418 mDividerControllerLocked.mLastDividerProgress =
2419 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2420 updated |= stack.updateAdjustForIme(
2421 mDividerControllerLocked.mLastAnimationProgress,
2422 mDividerControllerLocked.mLastDividerProgress,
2423 false /* force */);
2424 }
2425 if (interpolatedValue >= 1f) {
2426 stack.endImeAdjustAnimation();
2427 }
2428 }
2429
2430 return updated;
2431 }
2432
2433 boolean clearImeAdjustAnimation() {
2434 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002435 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2436 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002437 if (stack != null && stack.isAdjustedForIme()) {
2438 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2439 changed = true;
2440 }
2441 }
2442 return changed;
2443 }
2444
2445 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002446 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2447 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002448 if (stack.isVisible() && stack.isAdjustedForIme()) {
2449 stack.beginImeAdjustAnimation();
2450 }
2451 }
2452 }
2453
2454 void adjustForImeIfNeeded() {
lumark90120a82018-08-15 00:33:03 +08002455 final WindowState imeWin = mInputMethodWindow;
Wale Ogunwale10124582016-09-15 20:25:50 -07002456 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2457 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002458 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002459 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2460 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2461 imeTargetStack.getDockSide() : DOCKED_INVALID;
2462 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2463 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2464 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002465 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002466 final boolean imeHeightChanged = imeVisible &&
2467 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2468
2469 // The divider could be adjusted for IME position, or be thinner than usual,
2470 // or both. There are three possible cases:
2471 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2472 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2473 // - If IME is not visible, divider is not moved and is normal width.
2474
2475 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002476 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2477 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002478 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002479 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2480 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002481 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2482 } else {
2483 stack.resetAdjustedForIme(false);
2484 }
2485 }
2486 mDividerControllerLocked.setAdjustedForIme(
2487 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2488 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002489 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2490 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002491 stack.resetAdjustedForIme(!dockVisible);
2492 }
2493 mDividerControllerLocked.setAdjustedForIme(
2494 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2495 }
Winson Chung655332c2016-10-31 13:14:28 -07002496 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002497 }
2498
2499 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002500 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2501 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002502 stack.prepareFreezingTaskBounds();
2503 }
2504 }
2505
Wale Ogunwale94744212015-09-21 19:01:47 -07002506 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002507 getBounds(mTmpRect, newRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002508 rotateBounds(mTmpRect, oldRotation, newRotation, bounds);
2509 }
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002510
Evan Rosky39b6f232018-10-30 18:35:41 -07002511 void rotateBounds(Rect parentBounds, int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002512 // Compute a transform matrix to undo the coordinate space transformation,
2513 // and present the window at the same physical position it previously occupied.
2514 final int deltaRotation = deltaRotation(newRotation, oldRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002515 createRotationMatrix(
2516 deltaRotation, parentBounds.width(), parentBounds.height(), mTmpMatrix);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002517
2518 mTmpRectF.set(bounds);
2519 mTmpMatrix.mapRect(mTmpRectF);
2520 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002521 }
2522
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002523 static int deltaRotation(int oldRotation, int newRotation) {
2524 int delta = newRotation - oldRotation;
2525 if (delta < 0) delta += 4;
2526 return delta;
2527 }
2528
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002529 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002530 Matrix outMatrix) {
2531 // For rotations without Z-ordering we don't need the target rectangle's position.
2532 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2533 displayHeight, outMatrix);
2534 }
2535
2536 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2537 float displayWidth, float displayHeight, Matrix outMatrix) {
2538 switch (rotation) {
2539 case ROTATION_0:
2540 outMatrix.reset();
2541 break;
2542 case ROTATION_270:
2543 outMatrix.setRotate(270, 0, 0);
2544 outMatrix.postTranslate(0, displayHeight);
2545 outMatrix.postTranslate(rectTop, 0);
2546 break;
2547 case ROTATION_180:
2548 outMatrix.reset();
2549 break;
2550 case ROTATION_90:
2551 outMatrix.setRotate(90, 0, 0);
2552 outMatrix.postTranslate(displayWidth, 0);
2553 outMatrix.postTranslate(-rectTop, rectLeft);
2554 break;
2555 }
2556 }
2557
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002558 @CallSuper
2559 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002560 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002561 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002562 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002563 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002564 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2565 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002566 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002567 }
2568 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2569 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002570 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2571 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002572 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002573 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002574 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2575 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002576 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002577 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002578 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2579 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002580 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002581 }
2582 proto.write(DPI, mBaseDisplayDensity);
2583 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002584 proto.write(ROTATION, mRotation);
2585 final ScreenRotationAnimation screenRotationAnimation =
2586 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2587 if (screenRotationAnimation != null) {
2588 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2589 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002590 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
lumark588a3e82018-07-20 18:53:54 +08002591 mAppTransition.writeToProto(proto, APP_TRANSITION);
Louis Chang7501e332018-08-20 13:08:39 +08002592 proto.write(SURFACE_SIZE, mSurfaceSize);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002593 if (mFocusedApp != null) {
2594 mFocusedApp.writeNameToProto(proto, FOCUSED_APP);
2595 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002596 proto.end(token);
2597 }
2598
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002599 @Override
2600 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2601 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002602 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2603 final String subPrefix = " " + prefix;
2604 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2605 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2606 pw.print("dpi");
2607 if (mInitialDisplayWidth != mBaseDisplayWidth
2608 || mInitialDisplayHeight != mBaseDisplayHeight
2609 || mInitialDisplayDensity != mBaseDisplayDensity) {
2610 pw.print(" base=");
2611 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2612 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2613 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002614 if (mDisplayScalingDisabled) {
2615 pw.println(" noscale");
2616 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002617 pw.print(" cur=");
2618 pw.print(mDisplayInfo.logicalWidth);
2619 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2620 pw.print(" app=");
2621 pw.print(mDisplayInfo.appWidth);
2622 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2623 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2624 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2625 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2626 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002627 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002628 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002629 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002630
Craig Mautnerdc548482014-02-05 13:35:24 -08002631 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002632 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002633 pw.print(prefix);
2634 pw.print("mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
Adrian Roos5251b1d2018-03-23 18:57:43 +01002635
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002636 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
2637 if (mLastFocus != mCurrentFocus) {
2638 pw.print(" mLastFocus="); pw.println(mLastFocus);
2639 }
2640 if (mLosingFocus.size() > 0) {
2641 pw.println();
2642 pw.println(" Windows losing focus:");
2643 for (int i = mLosingFocus.size() - 1; i >= 0; i--) {
2644 final WindowState w = mLosingFocus.get(i);
2645 pw.print(" Losing #"); pw.print(i); pw.print(' ');
2646 pw.print(w);
2647 if (dumpAll) {
2648 pw.println(":");
2649 w.dump(pw, " ", true);
2650 } else {
2651 pw.println();
2652 }
2653 }
2654 }
2655 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
2656
Adrian Roos5251b1d2018-03-23 18:57:43 +01002657 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002658 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002659 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2660 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002661 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002662 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002663
Craig Mautnerdc548482014-02-05 13:35:24 -08002664 pw.println();
2665 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002666 pw.println();
2667 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002668 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002669 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002670 pw.print(" Exiting #"); pw.print(i);
2671 pw.print(' '); pw.print(token);
2672 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002673 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002674 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002675 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002676
Jorim Jaggi31f71702016-05-04 16:43:04 -07002677 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002678
2679 // Dump stack references
2680 final TaskStack homeStack = getHomeStack();
2681 if (homeStack != null) {
2682 pw.println(prefix + "homeStack=" + homeStack.getName());
2683 }
2684 final TaskStack pinnedStack = getPinnedStack();
2685 if (pinnedStack != null) {
2686 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2687 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002688 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002689 if (splitScreenPrimaryStack != null) {
2690 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2691 }
2692
2693 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002694 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002695 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002696 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002697
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002698 pw.println();
2699 mDisplayFrames.dump(prefix, pw);
Arthur Hung95b38a92018-07-20 18:56:12 +08002700 pw.println();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08002701 mDisplayPolicy.dump(prefix, pw);
2702 pw.println();
Riddle Hsuad256a12018-07-18 16:11:30 +08002703 mDisplayRotation.dump(prefix, pw);
2704 pw.println();
Arthur Hung95b38a92018-07-20 18:56:12 +08002705 mInputMonitor.dump(pw, " ");
Craig Mautner59c00972012-07-30 12:10:24 -07002706 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002707
2708 @Override
2709 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002710 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002711 }
2712
2713 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002714 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002715 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002716
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002717 /** Returns true if the stack in the windowing mode is visible. */
2718 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002719 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002720 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002721 }
2722
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002723 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002724 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002725 final int x = (int) xf;
2726 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002727 final WindowState touchedWin = getWindow(w -> {
2728 final int flags = w.mAttrs.flags;
2729 if (!w.isVisibleLw()) {
2730 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002731 }
2732 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002733 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002734 }
2735
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002736 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002737 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002738 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002739 }
2740
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002741 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002742
2743 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002744 return mTmpRegion.contains(x, y) || touchFlags == 0;
2745 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002746
2747 return touchedWin;
2748 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002749
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002750 boolean canAddToastWindowForUid(int uid) {
2751 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002752 // Also if the app is focused adding more than one toast at
2753 // a time for better backwards compatibility.
2754 final WindowState focusedWindowForUid = getWindow(w ->
2755 w.mOwnerUid == uid && w.isFocused());
2756 if (focusedWindowForUid != null) {
2757 return true;
2758 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002759 final WindowState win = getWindow(w ->
2760 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2761 && !w.mWindowRemovalAllowed);
2762 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002763 }
2764
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002765 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002766 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2767 return;
2768 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002769
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002770 // Used to communicate the old focus to the callback method.
2771 mTmpWindow = oldFocus;
2772
2773 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002774 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002775
2776 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002777 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002778
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002779 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002780
2781 if (mTmpWindow == null) {
2782 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2783 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002784 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002785 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002786 }
2787
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002788
2789 /**
2790 * Update the focused window and make some adjustments if the focus has changed.
2791 *
2792 * @param mode Indicates the situation we are in. Possible modes are:
2793 * {@link WindowManagerService#UPDATE_FOCUS_NORMAL},
2794 * {@link WindowManagerService#UPDATE_FOCUS_PLACING_SURFACES},
2795 * {@link WindowManagerService#UPDATE_FOCUS_WILL_PLACE_SURFACES},
2796 * {@link WindowManagerService#UPDATE_FOCUS_REMOVING_FOCUS}
2797 * @param updateInputWindows Whether to sync the window information to the input module.
2798 * @return {@code true} if the focused window has changed.
2799 */
2800 boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows, boolean focusFound) {
2801 final WindowState newFocus = findFocusedWindow();
2802 if (mCurrentFocus == newFocus) {
2803 return false;
2804 }
2805 mService.mH.obtainMessage(REPORT_FOCUS_CHANGE, this).sendToTarget();
2806 boolean imWindowChanged = false;
2807 // TODO (b/111080190): Multi-Session IME
2808 if (!focusFound) {
2809 final WindowState imWindow = mInputMethodWindow;
2810 if (imWindow != null) {
2811 final WindowState prevTarget = mService.mInputMethodTarget;
2812
2813 final WindowState newTarget = computeImeTarget(true /* updateImeTarget*/);
2814 imWindowChanged = prevTarget != newTarget;
2815
2816 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS
2817 && mode != UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2818 assignWindowLayers(false /* setLayoutNeeded */);
2819 }
2820 }
2821 }
2822
2823 if (imWindowChanged) {
2824 mService.mWindowsChanged = true;
2825 setLayoutNeeded();
2826 }
2827
2828 if (DEBUG_FOCUS_LIGHT || mService.localLOGV) Slog.v(TAG_WM, "Changing focus from "
2829 + mCurrentFocus + " to " + newFocus + " displayId=" + getDisplayId()
2830 + " Callers=" + Debug.getCallers(4));
2831 final WindowState oldFocus = mCurrentFocus;
2832 mCurrentFocus = newFocus;
2833 mLosingFocus.remove(newFocus);
2834
2835 if (newFocus != null) {
2836 mWinAddedSinceNullFocus.clear();
2837 mWinRemovedSinceNullFocus.clear();
2838
2839 if (newFocus.canReceiveKeys()) {
2840 // Displaying a window implicitly causes dispatching to be unpaused.
2841 // This is to protect against bugs if someone pauses dispatching but
2842 // forgets to resume.
2843 newFocus.mToken.paused = false;
2844 }
2845 }
2846
2847 // System UI is only shown on the default display.
2848 int focusChanged = isDefaultDisplay
2849 ? mService.mPolicy.focusChangedLw(oldFocus, newFocus) : 0;
2850
2851 if (imWindowChanged && oldFocus != mInputMethodWindow) {
2852 // Focus of the input method window changed. Perform layout if needed.
2853 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2854 performLayout(true /*initial*/, updateInputWindows);
2855 focusChanged &= ~FINISH_LAYOUT_REDO_LAYOUT;
2856 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2857 // Client will do the layout, but we need to assign layers
2858 // for handleNewWindowLocked() below.
2859 assignWindowLayers(false /* setLayoutNeeded */);
2860 }
2861 }
2862
2863 if ((focusChanged & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2864 // The change in focus caused us to need to do a layout. Okay.
2865 setLayoutNeeded();
2866 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2867 performLayout(true /*initial*/, updateInputWindows);
2868 } else if (mode == UPDATE_FOCUS_REMOVING_FOCUS) {
2869 mService.mRoot.performSurfacePlacement(false);
2870 }
2871 }
2872
2873 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
2874 // If we defer assigning layers, then the caller is responsible for doing this part.
2875 getInputMonitor().setInputFocusLw(newFocus, updateInputWindows);
2876 }
2877
2878 adjustForImeIfNeeded();
2879
2880 // We may need to schedule some toast windows to be removed. The toasts for an app that
2881 // does not have input focus are removed within a timeout to prevent apps to redress
2882 // other apps' UI.
2883 scheduleToastWindowsTimeoutIfNeededLocked(oldFocus, newFocus);
2884
2885 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2886 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
2887 }
2888 return true;
2889 }
2890
2891 /**
2892 * Set the new focused app to this display.
2893 *
2894 * @param newFocus the new focused AppWindowToken.
2895 * @return true if the focused app is changed.
2896 */
2897 boolean setFocusedApp(AppWindowToken newFocus) {
2898 if (newFocus != null) {
2899 final DisplayContent appDisplay = newFocus.getDisplayContent();
2900 if (appDisplay != this) {
2901 throw new IllegalStateException(newFocus + " is not on " + getName()
2902 + " but " + ((appDisplay != null) ? appDisplay.getName() : "none"));
2903 }
2904 }
2905 if (mFocusedApp == newFocus) {
2906 return false;
2907 }
2908 mFocusedApp = newFocus;
2909 getInputMonitor().setFocusedAppLw(newFocus);
2910 updateTouchExcludeRegion();
2911 return true;
2912 }
2913
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002914 /** Updates the layer assignment of windows on this display. */
2915 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002916 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002917 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002918 if (setLayoutNeeded) {
2919 setLayoutNeeded();
2920 }
Robert Carrb1579c82017-09-05 14:54:47 -07002921
Robert Carrf59b8dd2017-10-02 18:58:36 -07002922 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002923 // the application of this transaction until the animation pass triggers
2924 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2925 // the hiding and showing of surfaces.
2926 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002927 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002928 }
2929
Wale Ogunwale1666e312016-12-16 11:27:18 -08002930 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2931 // moving containers or resizing them. Need to investigate the best way to have it automatically
2932 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002933 void layoutAndAssignWindowLayersIfNeeded() {
2934 mService.mWindowsChanged = true;
2935 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002936
2937 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2938 false /*updateInputWindows*/)) {
2939 assignWindowLayers(false /* setLayoutNeeded */);
2940 }
2941
Arthur Hung95b38a92018-07-20 18:56:12 +08002942 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002943 mService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08002944 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002945 }
2946
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002947 /** Returns true if a leaked surface was destroyed */
2948 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002949 // Used to indicate that a surface was leaked.
2950 mTmpWindow = null;
2951 forAllWindows(w -> {
2952 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002953 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002954 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002955 }
2956 if (!mService.mSessions.contains(wsa.mSession)) {
2957 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002958 + w + " surface=" + wsa.mSurfaceController
2959 + " token=" + w.mToken
2960 + " pid=" + w.mSession.mPid
2961 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002962 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002963 mService.mForceRemoves.add(w);
2964 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002965 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002966 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002967 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002968 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002969 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002970 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002971 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002972 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002973 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002974
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002975 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002976 }
2977
2978 /**
lumark90120a82018-08-15 00:33:03 +08002979 * Set input method window for the display.
2980 * @param win Set when window added or Null when destroyed.
2981 */
2982 void setInputMethodWindowLocked(WindowState win) {
2983 mInputMethodWindow = win;
lumark70865a82018-09-17 16:56:26 +08002984 // Update display configuration for IME process.
2985 if (mInputMethodWindow != null) {
2986 final int imePid = mInputMethodWindow.mSession.mPid;
2987 mService.mAtmInternal.onImeWindowSetOnDisplay(imePid,
2988 mInputMethodWindow.getDisplayId());
2989 }
lumark90120a82018-08-15 00:33:03 +08002990 computeImeTarget(true /* updateImeTarget */);
2991 }
2992
2993 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002994 * Determine and return the window that should be the IME target.
2995 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2996 * @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 +00002997 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002998 WindowState computeImeTarget(boolean updateImeTarget) {
lumark90120a82018-08-15 00:33:03 +08002999 if (mInputMethodWindow == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003000 // There isn't an IME so there shouldn't be a target...That was easy!
3001 if (updateImeTarget) {
3002 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
3003 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003004 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003005 }
3006 return null;
3007 }
3008
Chavi Weingarten3a748552018-05-14 17:32:42 +00003009 final WindowState curTarget = mService.mInputMethodTarget;
3010 if (!canUpdateImeTarget()) {
3011 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
3012 return curTarget;
3013 }
3014
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003015 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
3016 // same display. Or even when the current IME/target are not on the same screen as the next
3017 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08003018 mUpdateImeTarget = updateImeTarget;
3019 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003020
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003021
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003022 // Yet more tricksyness! If this window is a "starting" window, we do actually want
3023 // to be on top of it, but it is not -really- where input will go. So look down below
3024 // for a real window to target...
3025 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
3026 final AppWindowToken token = target.mAppToken;
3027 if (token != null) {
3028 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
3029 if (betterTarget != null) {
3030 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003031 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003032 }
3033 }
3034
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003035 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
3036 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003037
chaviw1c13ad32018-06-12 16:44:23 -07003038 // Now, a special case -- if the last target's window is in the process of exiting, but
3039 // not removed, and the new target is home, keep on the last target to avoid flicker.
3040 // Home is a special case since its above other stacks in the ordering list, but layed
3041 // out below the others.
3042 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
3043 && curTarget.isClosing() && (target == null || target.isActivityTypeHome())) {
chaviw4ad54912018-05-30 11:05:44 -07003044 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "New target is home while current target is "
Chavi Weingarten3a748552018-05-14 17:32:42 +00003045 + "closing, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003046 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003047 }
3048
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003049 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
3050 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003051
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003052 if (target == null) {
3053 if (updateImeTarget) {
3054 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
3055 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
3056 + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003057 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003058 }
3059
3060 return null;
3061 }
3062
3063 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003064 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
3065 if (token != null) {
3066
3067 // Now some fun for dealing with window animations that modify the Z order. We need
3068 // to look at all windows below the current target that are in this app, finding the
3069 // highest visible one in layering.
3070 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003071 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003072 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003073 }
3074
3075 if (highestTarget != null) {
lumark588a3e82018-07-20 18:53:54 +08003076 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, mAppTransition + " " + highestTarget
Jorim Jaggi35d328a2018-08-14 17:00:20 +02003077 + " animating=" + highestTarget.isAnimating());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003078
lumark588a3e82018-07-20 18:53:54 +08003079 if (mAppTransition.isTransitionSet()) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003080 // If we are currently setting up for an animation, hold everything until we
3081 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003082 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003083 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003084 }
3085 }
3086 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003087
3088 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
3089 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003090 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003091 }
3092
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003093 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003094 }
3095
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003096 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003097 if (target == mService.mInputMethodTarget
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003098 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003099 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003100 }
3101
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003102 mService.mInputMethodTarget = target;
3103 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003104 assignWindowLayers(false /* setLayoutNeeded */);
3105 }
3106
3107 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
3108 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3109 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
3110 }
3111
3112 // Used to indicate we have reached the first window in the range we are interested in.
3113 mTmpWindow = null;
3114
3115 // TODO: Figure-out a more efficient way to do this.
3116 final WindowState candidate = getWindow(w -> {
3117 if (w == top) {
3118 // Reached the first window in the range we are interested in.
3119 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003120 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003121 if (mTmpWindow == null) {
3122 return false;
3123 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003124
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003125 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3126 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003127 }
3128 // If we reached the bottom of the range of windows we are considering,
3129 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003130 if (w == bottom) {
3131 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003132 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003133 return false;
3134 });
3135
3136 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003137 }
3138
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003139 void setLayoutNeeded() {
3140 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
3141 mLayoutNeeded = true;
3142 }
3143
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003144 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003145 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
3146 mLayoutNeeded = false;
3147 }
3148
3149 boolean isLayoutNeeded() {
3150 return mLayoutNeeded;
3151 }
3152
Wale Ogunwale02319a62016-09-26 15:21:22 -07003153 void dumpTokens(PrintWriter pw, boolean dumpAll) {
3154 if (mTokenMap.isEmpty()) {
3155 return;
3156 }
3157 pw.println(" Display #" + mDisplayId);
3158 final Iterator<WindowToken> it = mTokenMap.values().iterator();
3159 while (it.hasNext()) {
3160 final WindowToken token = it.next();
3161 pw.print(" ");
3162 pw.print(token);
3163 if (dumpAll) {
3164 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003165 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07003166 } else {
3167 pw.println();
3168 }
3169 }
lumark588a3e82018-07-20 18:53:54 +08003170
3171 if (!mOpeningApps.isEmpty() || !mClosingApps.isEmpty()) {
3172 pw.println();
3173 if (mOpeningApps.size() > 0) {
3174 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
3175 }
3176 if (mClosingApps.size() > 0) {
3177 pw.print(" mClosingApps="); pw.println(mClosingApps);
3178 }
3179 }
3180
3181 mUnknownAppVisibilityController.dump(pw, " ");
Wale Ogunwale02319a62016-09-26 15:21:22 -07003182 }
3183
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003184 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003185 final int[] index = new int[1];
3186 forAllWindows(w -> {
3187 final WindowStateAnimator wAnim = w.mWinAnimator;
3188 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
3189 index[0] = index[0] + 1;
3190 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003191 }
3192
Jorim Jaggife762342016-10-13 14:33:27 +02003193 /**
3194 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
3195 */
3196 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
3197 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003198 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02003199 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
3200 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01003201 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02003202 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003203 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02003204 }
3205
Wale Ogunwale494009b82016-10-21 09:01:38 -07003206 boolean checkWaitingForWindows() {
3207
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003208 mHaveBootMsg = false;
3209 mHaveApp = false;
3210 mHaveWallpaper = false;
3211 mHaveKeyguard = true;
3212
3213 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003214 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
3215 return true;
3216 }
3217 if (w.isDrawnLw()) {
3218 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003219 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003220 } else if (w.mAttrs.type == TYPE_APPLICATION
3221 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003222 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003223 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003224 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003225 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003226 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003227 }
3228 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003229 return false;
3230 });
3231
3232 if (visibleWindow != null) {
3233 // We have a visible window.
3234 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003235 }
3236
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003237 // if the wallpaper service is disabled on the device, we're never going to have
3238 // wallpaper, don't bother waiting for it
3239 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
3240 com.android.internal.R.bool.config_enableWallpaperService)
Daichi Hironoc1432222018-03-29 13:06:07 +09003241 && mService.mContext.getResources().getBoolean(
3242 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003243 && !mService.mOnlyCore;
3244
Wale Ogunwale494009b82016-10-21 09:01:38 -07003245 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
3246 "******** booted=" + mService.mSystemBooted
3247 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003248 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
3249 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
3250 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003251
3252 // If we are turning on the screen to show the boot message, don't do it until the boot
3253 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003254 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003255 return true;
3256 }
3257
3258 // If we are turning on the screen after the boot is completed normally, don't do so until
3259 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003260 if (mService.mSystemBooted
3261 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003262 return true;
3263 }
3264
3265 return false;
3266 }
3267
Jorim Jaggi8f520872018-08-14 17:00:20 +02003268 void updateWindowsForAnimator() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003269 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003270 }
3271
Jorim Jaggi8f520872018-08-14 17:00:20 +02003272 /**
3273 * Updates the {@link TaskStack#setAnimationBackground} for all windows.
3274 */
3275 void updateBackgroundForAnimator() {
Jorim Jaggi7156b652016-10-25 08:31:58 -07003276 resetAnimationBackgroundAnimator();
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003277 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003278 }
3279
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003280 boolean isInputMethodClientFocus(int uid, int pid) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003281 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003282 if (imFocus == null) {
3283 return false;
3284 }
3285
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003286 if (DEBUG_INPUT_METHOD) {
3287 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003288 Slog.i(TAG_WM, "Current focus: " + mCurrentFocus + " displayId=" + mDisplayId);
3289 Slog.i(TAG_WM, "Last focus: " + mLastFocus + " displayId=" + mDisplayId);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003290 }
3291
Wale Ogunwale494009b82016-10-21 09:01:38 -07003292 if (DEBUG_INPUT_METHOD) {
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003293 Slog.i(TAG_WM, "IM target uid/pid: " + imFocus.mSession.mUid
3294 + "/" + imFocus.mSession.mPid);
3295 Slog.i(TAG_WM, "Requesting client uid/pid: " + uid + "/" + pid);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003296 }
3297
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003298 return imFocus.mSession.mUid == uid && imFocus.mSession.mPid == pid;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003299 }
3300
3301 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003302 final WindowState win = getWindow(
3303 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
3304 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003305 }
3306
3307 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003308 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003309 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003310 final int curValue = w.mSystemUiVisibility;
3311 final int diff = (curValue ^ visibility) & globalDiff;
3312 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003313 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003314 w.mSeq++;
3315 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003316 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003317 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
3318 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07003319 visibility, newValue, diff);
3320 }
3321 } catch (RemoteException e) {
3322 // so sorry
3323 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003324 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003325 }
3326
3327 void onWindowFreezeTimeout() {
3328 Slog.w(TAG_WM, "Window freeze timeout expired.");
3329 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003330
3331 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07003332 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003333 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003334 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07003335 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003336 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
3337 - mService.mDisplayFreezeTime);
3338 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003339 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003340 mService.mWindowPlacerLocked.performSurfacePlacement();
3341 }
3342
3343 void waitForAllWindowsDrawn() {
3344 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003345 forAllWindows(w -> {
3346 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
3347 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
3348 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003349 // Force add to mResizingWindows.
chaviw9c81e632018-07-31 11:17:52 -07003350 w.resetLastContentInsets();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003351 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003352 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003353 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003354 }
3355
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003356 // TODO: Super crazy long method that should be broken down...
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003357 void applySurfaceChangesTransaction(boolean recoveringMemory) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003358
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003359 final int dw = mDisplayInfo.logicalWidth;
3360 final int dh = mDisplayInfo.logicalHeight;
3361 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
3362
3363 mTmpUpdateAllDrawn.clear();
3364
3365 int repeats = 0;
3366 do {
3367 repeats++;
3368 if (repeats > 6) {
3369 Slog.w(TAG, "Animation repeat aborted after too many iterations");
3370 clearLayoutNeeded();
3371 break;
3372 }
3373
3374 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
3375 pendingLayoutChanges);
3376
Andrii Kulian839def92016-11-02 10:58:58 -07003377 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
3378 // the wallpaper window jumping across displays.
3379 // Remove check for default display when there will be support for multiple wallpaper
3380 // targets (on different displays).
3381 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003382 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003383 }
3384
Riddle Hsu654a6f92018-07-13 22:59:36 +08003385 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003386 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Riddle Hsu4e611772018-10-31 18:58:28 +08003387 if (updateOrientationFromAppTokens()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003388 setLayoutNeeded();
Riddle Hsu4e611772018-10-31 18:58:28 +08003389 sendNewConfiguration();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003390 }
3391 }
3392
3393 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3394 setLayoutNeeded();
3395 }
3396
3397 // FIRST LOOP: Perform a layout, if needed.
3398 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
3399 performLayout(repeats == 1, false /* updateInputWindows */);
3400 } else {
3401 Slog.w(TAG, "Layout repeat skipped after too many iterations");
3402 }
3403
3404 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
3405 pendingLayoutChanges = 0;
3406
3407 if (isDefaultDisplay) {
3408 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003409 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003410 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
3411 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
3412 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
3413 }
3414 } while (pendingLayoutChanges != 0);
3415
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003416 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003417
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003418 mTmpRecoveringMemory = recoveringMemory;
3419 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01003420 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003421
Jorim Jaggif1292892018-09-10 11:58:13 +02003422 mLastHasContent = mTmpApplySurfaceChangesTransactionState.displayHasContent;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003423 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Jorim Jaggif1292892018-09-10 11:58:13 +02003424 mLastHasContent,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003425 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
3426 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003427 true /* inTraversal, must call performTraversalInTrans... below */);
3428
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003429 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
3430 if (wallpaperVisible != mLastWallpaperVisible) {
3431 mLastWallpaperVisible = wallpaperVisible;
3432 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
3433 }
3434
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003435 while (!mTmpUpdateAllDrawn.isEmpty()) {
3436 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
3437 // See if any windows have been drawn, so they (and others associated with them)
3438 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07003439 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003440 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003441 }
3442
Bryce Leef3c6a472017-11-14 14:53:06 -08003443 private void updateBounds() {
Evan Rosky39b6f232018-10-30 18:35:41 -07003444 calculateBounds(mDisplayInfo, mTmpBounds);
Bryce Leef3c6a472017-11-14 14:53:06 -08003445 setBounds(mTmpBounds);
3446 }
3447
3448 // Determines the current display bounds based on the current state
Evan Rosky39b6f232018-10-30 18:35:41 -07003449 private void calculateBounds(DisplayInfo displayInfo, Rect out) {
Bryce Leef3c6a472017-11-14 14:53:06 -08003450 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Evan Rosky39b6f232018-10-30 18:35:41 -07003451 final int rotation = displayInfo.rotation;
3452 boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Bryce Leef3c6a472017-11-14 14:53:06 -08003453 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
3454 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Evan Rosky39b6f232018-10-30 18:35:41 -07003455 int width = displayInfo.logicalWidth;
Bryce Leef3c6a472017-11-14 14:53:06 -08003456 int left = (physWidth - width) / 2;
Evan Rosky39b6f232018-10-30 18:35:41 -07003457 int height = displayInfo.logicalHeight;
Bryce Leef3c6a472017-11-14 14:53:06 -08003458 int top = (physHeight - height) / 2;
3459 out.set(left, top, left + width, top + height);
3460 }
3461
3462 @Override
3463 public void getBounds(Rect out) {
Evan Rosky39b6f232018-10-30 18:35:41 -07003464 calculateBounds(mDisplayInfo, out);
Bryce Leef3c6a472017-11-14 14:53:06 -08003465 }
3466
3467 private void getBounds(Rect out, int orientation) {
3468 getBounds(out);
3469
3470 // Rotate the Rect if needed.
3471 final int currentRotation = mDisplayInfo.rotation;
3472 final int rotationDelta = deltaRotation(currentRotation, orientation);
3473 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3474 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3475 mTmpRectF.set(out);
3476 mTmpMatrix.mapRect(mTmpRectF);
3477 mTmpRectF.round(out);
3478 }
3479 }
3480
Louis Chang7501e332018-08-20 13:08:39 +08003481 int getSurfaceSize() {
3482 return mSurfaceSize;
3483 }
3484
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003485 void performLayout(boolean initial, boolean updateInputWindows) {
3486 if (!isLayoutNeeded()) {
3487 return;
3488 }
3489 clearLayoutNeeded();
3490
3491 final int dw = mDisplayInfo.logicalWidth;
3492 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003493 if (DEBUG_LAYOUT) {
3494 Slog.v(TAG, "-------------------------------------");
3495 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
3496 }
3497
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003498 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3499 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00003500 // TODO: Not sure if we really need to set the rotation here since we are updating from the
3501 // display info above...
3502 mDisplayFrames.mRotation = mRotation;
3503 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003504 if (isDefaultDisplay) {
3505 // Not needed on non-default displays.
3506 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
3507 mService.mScreenRect.set(0, 0, dw, dh);
3508 }
3509
Adrian Roos5251b1d2018-03-23 18:57:43 +01003510 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003511 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003512 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003513
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003514 // Used to indicate that we have processed the dream window and all additional windows are
3515 // behind it.
3516 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003517 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003518
3519 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003520 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003521
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003522 // Used to indicate that we have processed the dream window and all additional attached
3523 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003524 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003525 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003526
3527 // Now perform layout of attached windows, which usually depend on the position of the
3528 // window they are attached to. XXX does not deal with windows that are attached to windows
3529 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003530 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003531
3532 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003533 mInputMonitor.layoutInputConsumers(dw, dh);
3534 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003535 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003536 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003537 }
3538
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003539 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
3540 }
3541
3542 /**
3543 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3544 * In portrait mode, it grabs the full screenshot.
3545 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003546 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003547 */
chaviw0315a1a2018-03-05 15:28:35 -08003548 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
3549 if (!mService.mPolicy.isScreenOn()) {
3550 if (DEBUG_SCREENSHOT) {
3551 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003552 }
chaviw0315a1a2018-03-05 15:28:35 -08003553 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003554 }
chaviwfbe47df2017-11-10 16:14:49 -08003555
chaviw0315a1a2018-03-05 15:28:35 -08003556 int dw = mDisplayInfo.logicalWidth;
3557 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003558
chaviw0315a1a2018-03-05 15:28:35 -08003559 if (dw <= 0 || dh <= 0) {
3560 return null;
3561 }
chaviwfbe47df2017-11-10 16:14:49 -08003562
chaviw0315a1a2018-03-05 15:28:35 -08003563 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003564
chaviw0315a1a2018-03-05 15:28:35 -08003565 // The screenshot API does not apply the current screen rotation.
3566 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003567
chaviw0315a1a2018-03-05 15:28:35 -08003568 if (rot == ROTATION_90 || rot == ROTATION_270) {
3569 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3570 }
chaviwfbe47df2017-11-10 16:14:49 -08003571
chaviw0315a1a2018-03-05 15:28:35 -08003572 // SurfaceFlinger is not aware of orientation, so convert our logical
3573 // crop to SurfaceFlinger's portrait orientation.
3574 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3575
3576 final ScreenRotationAnimation screenRotationAnimation =
3577 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3578 final boolean inRotation = screenRotationAnimation != null &&
3579 screenRotationAnimation.isAnimating();
3580 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3581
3582 // TODO(b/68392460): We should screenshot Task controls directly
3583 // but it's difficult at the moment as the Task doesn't have the
3584 // correct size set.
chaviw08520a02018-09-10 16:44:56 -07003585 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, inRotation, rot);
chaviw0315a1a2018-03-05 15:28:35 -08003586 if (bitmap == null) {
3587 Slog.w(TAG_WM, "Failed to take screenshot");
3588 return null;
3589 }
3590
3591 // Create a copy of the screenshot that is immutable and backed in ashmem.
3592 // This greatly reduces the overhead of passing the bitmap between processes.
3593 final Bitmap ret = bitmap.createAshmemBitmap(config);
3594 bitmap.recycle();
3595 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003596 }
3597
3598 // TODO: Can this use createRotationMatrix()?
3599 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3600 if (rot == Surface.ROTATION_90) {
3601 final int tmp = crop.top;
3602 crop.top = dw - crop.right;
3603 crop.right = crop.bottom;
3604 crop.bottom = dw - crop.left;
3605 crop.left = tmp;
3606 } else if (rot == Surface.ROTATION_180) {
3607 int tmp = crop.top;
3608 crop.top = dh - crop.bottom;
3609 crop.bottom = dh - tmp;
3610 tmp = crop.right;
3611 crop.right = dw - crop.left;
3612 crop.left = dw - tmp;
3613 } else if (rot == Surface.ROTATION_270) {
3614 final int tmp = crop.top;
3615 crop.top = crop.left;
3616 crop.left = dh - crop.bottom;
3617 crop.bottom = crop.right;
3618 crop.right = dh - tmp;
3619 }
3620 }
3621
3622 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003623 // Used to indicate the layout is needed.
3624 mTmpWindow = null;
3625
3626 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003627 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003628 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003629 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003630 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003631 w.setDisplayLayoutNeeded();
3632 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003633 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003634
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003635 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003636 mService.mWindowPlacerLocked.performSurfacePlacement();
3637 }
3638 }
3639
Wale Ogunwale1666e312016-12-16 11:27:18 -08003640 void setExitingTokensHasVisible(boolean hasVisible) {
3641 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3642 mExitingTokens.get(i).hasVisible = hasVisible;
3643 }
3644
3645 // Initialize state of exiting applications.
3646 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3647 }
3648
3649 void removeExistingTokensIfPossible() {
3650 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3651 final WindowToken token = mExitingTokens.get(i);
3652 if (!token.hasVisible) {
3653 mExitingTokens.remove(i);
3654 }
3655 }
3656
3657 // Time to remove any exiting applications?
3658 mTaskStackContainers.removeExistingAppTokensIfPossible();
3659 }
3660
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003661 @Override
3662 void onDescendantOverrideConfigurationChanged() {
3663 setLayoutNeeded();
3664 mService.requestTraversal();
3665 }
3666
David Stevens9440dc82017-03-16 19:00:20 -07003667 boolean okToDisplay() {
3668 if (mDisplayId == DEFAULT_DISPLAY) {
3669 return !mService.mDisplayFrozen
3670 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3671 }
3672 return mDisplayInfo.state == Display.STATE_ON;
3673 }
3674
3675 boolean okToAnimate() {
3676 return okToDisplay() &&
3677 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3678 }
3679
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003680 static final class TaskForResizePointSearchResult {
3681 boolean searchDone;
3682 Task taskForResize;
3683
3684 void reset() {
3685 searchDone = false;
3686 taskForResize = null;
3687 }
3688 }
Robert Carr3b716242016-08-16 16:02:21 -07003689
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003690 private static final class ApplySurfaceChangesTransactionState {
3691 boolean displayHasContent;
3692 boolean obscured;
3693 boolean syswin;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003694 float preferredRefreshRate;
3695 int preferredModeId;
3696
3697 void reset() {
3698 displayHasContent = false;
3699 obscured = false;
3700 syswin = false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003701 preferredRefreshRate = 0;
3702 preferredModeId = 0;
3703 }
3704 }
3705
3706 private static final class ScreenshotApplicationState {
3707 WindowState appWin;
3708 int maxLayer;
3709 int minLayer;
3710 boolean screenshotReady;
3711
3712 void reset(boolean screenshotReady) {
3713 appWin = null;
3714 maxLayer = 0;
3715 minLayer = 0;
3716 this.screenshotReady = screenshotReady;
3717 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3718 }
3719 }
3720
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003721 /**
3722 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3723 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3724 * homogeneous children type which is currently required by sub-classes of
3725 * {@link WindowContainer} class.
3726 */
3727 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3728
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003729 DisplayChildWindowContainer(WindowManagerService service) {
3730 super(service);
3731 }
3732
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003733 @Override
3734 boolean fillsParent() {
3735 return true;
3736 }
3737
3738 @Override
3739 boolean isVisible() {
3740 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003741 }
3742 }
3743
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003744 /**
3745 * Window container class that contains all containers on this display relating to Apps.
3746 * I.e Activities.
3747 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003748 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003749 /**
3750 * A control placed at the appropriate level for transitions to occur.
3751 */
chaviw23ee71c2017-12-18 11:29:41 -08003752 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003753 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003754 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003755
Robert Carrf7a7ca82017-12-06 13:17:07 -08003756 /**
3757 * Given that the split-screen divider does not have an AppWindowToken, it
3758 * will have to live inside of a "NonAppWindowContainer", in particular
3759 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
3760 * it will need to be interleaved with some of our children, appearing on top of
3761 * both docked stacks but underneath any assistant stacks.
3762 *
3763 * To solve this problem we have this anchor control, which will always exist so
3764 * we can always assign it the correct value in our {@link #assignChildLayers}.
3765 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
3766 * assign the divider a layer relative to it. This way we prevent linking lifecycle
3767 * events between the two containers.
3768 */
3769 SurfaceControl mSplitScreenDividerAnchor = null;
3770
Wale Ogunwale61911492017-10-11 08:50:50 -07003771 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3772 // through the list to find them.
3773 private TaskStack mHomeStack = null;
3774 private TaskStack mPinnedStack = null;
3775 private TaskStack mSplitScreenPrimaryStack = null;
3776
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003777 TaskStackContainers(WindowManagerService service) {
3778 super(service);
3779 }
3780
Wale Ogunwale61911492017-10-11 08:50:50 -07003781 /**
3782 * Returns the topmost stack on the display that is compatible with the input windowing mode
3783 * and activity type. Null is no compatible stack on the display.
3784 */
3785 TaskStack getStack(int windowingMode, int activityType) {
3786 if (activityType == ACTIVITY_TYPE_HOME) {
3787 return mHomeStack;
3788 }
3789 if (windowingMode == WINDOWING_MODE_PINNED) {
3790 return mPinnedStack;
3791 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3792 return mSplitScreenPrimaryStack;
3793 }
3794 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3795 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003796 if (activityType == ACTIVITY_TYPE_UNDEFINED
3797 && windowingMode == stack.getWindowingMode()) {
3798 // Passing in undefined type means we want to match the topmost stack with the
3799 // windowing mode.
3800 return stack;
3801 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003802 if (stack.isCompatible(windowingMode, activityType)) {
3803 return stack;
3804 }
3805 }
3806 return null;
3807 }
3808
3809 @VisibleForTesting
3810 TaskStack getTopStack() {
3811 return mTaskStackContainers.getChildCount() > 0
3812 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3813 }
3814
3815 TaskStack getHomeStack() {
3816 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3817 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3818 }
3819 return mHomeStack;
3820 }
3821
3822 TaskStack getPinnedStack() {
3823 return mPinnedStack;
3824 }
3825
Matthew Ng64e77cf2017-10-31 14:01:31 -07003826 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003827 return mSplitScreenPrimaryStack;
3828 }
3829
Winson Chunge2d72172018-01-25 17:46:20 +00003830 ArrayList<Task> getVisibleTasks() {
3831 final ArrayList<Task> visibleTasks = new ArrayList<>();
3832 forAllTasks(task -> {
3833 if (task.isVisible()) {
3834 visibleTasks.add(task);
3835 }
3836 });
3837 return visibleTasks;
3838 }
3839
Andrii Kulian839def92016-11-02 10:58:58 -07003840 /**
3841 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003842 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003843 */
3844 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003845 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003846 addChild(stack, onTop);
3847 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003848 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003849
Wale Ogunwale61911492017-10-11 08:50:50 -07003850 void onStackWindowingModeChanged(TaskStack stack) {
3851 removeStackReferenceIfNeeded(stack);
3852 addStackReferenceIfNeeded(stack);
3853 if (stack == mPinnedStack && getTopStack() != stack) {
3854 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3855 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3856 }
3857 }
3858
3859 private void addStackReferenceIfNeeded(TaskStack stack) {
3860 if (stack.isActivityTypeHome()) {
3861 if (mHomeStack != null) {
Louis Chang7d0037c2018-08-13 12:42:06 +08003862 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07003863 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang7d0037c2018-08-13 12:42:06 +08003864
Wale Ogunwale61911492017-10-11 08:50:50 -07003865 }
Louis Chang7d0037c2018-08-13 12:42:06 +08003866 mHomeStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07003867 }
3868 final int windowingMode = stack.getWindowingMode();
3869 if (windowingMode == WINDOWING_MODE_PINNED) {
3870 if (mPinnedStack != null) {
3871 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3872 + mPinnedStack + " already exist on display=" + this
3873 + " stack=" + stack);
3874 }
3875 mPinnedStack = stack;
3876 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3877 if (mSplitScreenPrimaryStack != null) {
3878 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3879 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3880 + " already exist on display=" + this + " stack=" + stack);
3881 }
3882 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003883 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003884 }
3885 }
3886
3887 private void removeStackReferenceIfNeeded(TaskStack stack) {
3888 if (stack == mHomeStack) {
3889 mHomeStack = null;
3890 } else if (stack == mPinnedStack) {
3891 mPinnedStack = null;
3892 } else if (stack == mSplitScreenPrimaryStack) {
3893 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003894 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3895 mService.setDockedStackCreateStateLocked(
3896 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3897 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003898 }
Andrii Kulian839def92016-11-02 10:58:58 -07003899 }
3900
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003901 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003902 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3903 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003904 addChild(stack, addIndex);
3905 setLayoutNeeded();
3906 }
3907
Wale Ogunwale61911492017-10-11 08:50:50 -07003908 @Override
3909 protected void removeChild(TaskStack stack) {
3910 super.removeChild(stack);
3911 removeStackReferenceIfNeeded(stack);
3912 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003913
3914 @Override
3915 boolean isOnTop() {
3916 // Considered always on top
3917 return true;
3918 }
3919
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003920 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003921 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003922 if (child.getWindowConfiguration().isAlwaysOnTop()
3923 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003924 // This stack is always-on-top, override the default behavior.
3925 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3926
3927 // Moving to its current position, as we must call super but we don't want to
3928 // perform any meaningful action.
3929 final int currentPosition = mChildren.indexOf(child);
3930 super.positionChildAt(currentPosition, child, false /* includingParents */);
3931 return;
3932 }
3933
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003934 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3935 super.positionChildAt(targetPosition, child, includingParents);
3936
Riddle Hsu2f6e1742018-08-23 22:44:36 +08003937 if (includingParents) {
3938 // We still want to move the display of this stack container to top because even the
3939 // target position is adjusted to non-top, the intention of the condition is to have
3940 // higher z-order to gain focus (e.g. moving a task of a fullscreen stack to front
3941 // in a non-top display which is using picture-in-picture mode).
3942 final int topChildPosition = getChildCount() - 1;
3943 if (targetPosition < topChildPosition && position >= topChildPosition) {
3944 getParent().positionChildAt(POSITION_TOP, this /* child */,
3945 true /* includingParents */);
3946 }
3947 }
3948
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003949 setLayoutNeeded();
3950 }
3951
3952 /**
3953 * When stack is added or repositioned, find a proper position for it.
3954 * This will make sure that pinned stack always stays on top.
3955 * @param requestedPosition Position requested by caller.
3956 * @param stack Stack to be added or positioned.
3957 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3958 * @return The proper position for the stack.
3959 */
3960 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09003961 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09003962 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09003963 }
3964
Kazuki Takisef85197b2018-06-18 18:18:36 +09003965 final int topChildPosition = mChildren.size() - 1;
3966 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
3967 for (int i = topChildPosition; i >= 0; --i) {
3968 if (getStacks().get(i) != stack && !getStacks().get(i).isAlwaysOnTop()) {
3969 belowAlwaysOnTopPosition = i;
3970 break;
3971 }
3972 }
3973
3974 // The max possible position we can insert the stack at.
3975 int maxPosition = POSITION_TOP;
3976 // The min possible position we can insert the stack at.
3977 int minPosition = POSITION_BOTTOM;
3978
3979 if (stack.isAlwaysOnTop()) {
3980 if (hasPinnedStack()) {
3981 // Always-on-top stacks go below the pinned stack.
3982 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
3983 }
3984 // Always-on-top stacks need to be above all other stacks.
3985 minPosition = belowAlwaysOnTopPosition !=
3986 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
3987 } else {
3988 // Other stacks need to be below the always-on-top stacks.
3989 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08003990 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09003991 }
3992
3993 int targetPosition = requestedPosition;
3994 targetPosition = Math.min(targetPosition, maxPosition);
3995 targetPosition = Math.max(targetPosition, minPosition);
3996
3997 int prevPosition = getStacks().indexOf(stack);
3998 // The positions we calculated above (maxPosition, minPosition) do not take into
3999 // consideration the following edge cases.
4000 // 1) We need to adjust the position depending on the value "adding".
4001 // 2) When we are moving a stack to another position, we also need to adjust the
4002 // position depending on whether the stack is moving to a higher or lower position.
4003 if ((targetPosition != requestedPosition) &&
4004 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004005 targetPosition++;
4006 }
4007
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004008 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08004009 }
4010
4011 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004012 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
4013 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004014 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004015 if (super.forAllWindows(callback, traverseTopToBottom)) {
4016 return true;
4017 }
4018 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4019 return true;
4020 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004021 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004022 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4023 return true;
4024 }
4025 if (super.forAllWindows(callback, traverseTopToBottom)) {
4026 return true;
4027 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004028 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004029 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004030 }
4031
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004032 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004033 boolean traverseTopToBottom) {
4034 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
4035 // app tokens.
4036 // TODO: Investigate if we need to continue to do this or if we can just process them
4037 // in-order.
4038 if (traverseTopToBottom) {
4039 for (int i = mChildren.size() - 1; i >= 0; --i) {
4040 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4041 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004042 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4043 traverseTopToBottom)) {
4044 return true;
4045 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004046 }
4047 }
4048 } else {
4049 final int count = mChildren.size();
4050 for (int i = 0; i < count; ++i) {
4051 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4052 final int appTokensCount = appTokens.size();
4053 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004054 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4055 traverseTopToBottom)) {
4056 return true;
4057 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004058 }
4059 }
4060 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004061 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004062 }
4063
Wale Ogunwale1666e312016-12-16 11:27:18 -08004064 void setExitingTokensHasVisible(boolean hasVisible) {
4065 for (int i = mChildren.size() - 1; i >= 0; --i) {
4066 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4067 for (int j = appTokens.size() - 1; j >= 0; --j) {
4068 appTokens.get(j).hasVisible = hasVisible;
4069 }
4070 }
4071 }
4072
4073 void removeExistingAppTokensIfPossible() {
4074 for (int i = mChildren.size() - 1; i >= 0; --i) {
4075 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4076 for (int j = appTokens.size() - 1; j >= 0; --j) {
4077 final AppWindowToken token = appTokens.get(j);
lumark588a3e82018-07-20 18:53:54 +08004078 if (!token.hasVisible && !mClosingApps.contains(token)
Wale Ogunwale1666e312016-12-16 11:27:18 -08004079 && (!token.mIsExiting || token.isEmpty())) {
4080 // Make sure there is no animation running on this token, so any windows
4081 // associated with it will be removed as soon as their animations are
4082 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02004083 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004084 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
4085 "performLayout: App token exiting now removed" + token);
4086 token.removeIfPossible();
4087 }
4088 }
4089 }
4090 }
4091
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004092 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004093 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004094 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
4095 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004096 // Apps and their containers are not allowed to specify an orientation while the
Wale Ogunwaled0c225c2018-06-15 14:47:46 -07004097 // docked or freeform stack is visible...except for the home stack if the docked
4098 // stack is minimized and it actually set something and the bounds is different from
4099 // the display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004100 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004101 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004102 && !(mDividerControllerLocked.isHomeStackResizable()
4103 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004104 final int orientation = mHomeStack.getOrientation();
4105 if (orientation != SCREEN_ORIENTATION_UNSET) {
4106 return orientation;
4107 }
4108 }
4109 return SCREEN_ORIENTATION_UNSPECIFIED;
4110 }
4111
4112 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07004113 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
4114 PackageManager.FEATURE_AUTOMOTIVE);
4115 if (isCar) {
4116 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
4117 // allow anything but the default orientation.
4118 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004119 "Forcing UNSPECIFIED orientation in car for display id=" + mDisplayId
4120 + ". Ignoring " + orientation);
Dean Harding3e5a1522017-10-06 09:19:01 -07004121 return SCREEN_ORIENTATION_UNSPECIFIED;
4122 }
4123
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004124 if (orientation != SCREEN_ORIENTATION_UNSET
4125 && orientation != SCREEN_ORIENTATION_BEHIND) {
4126 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004127 "App is requesting an orientation, return " + orientation
4128 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004129 return orientation;
4130 }
4131
4132 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004133 "No app is requesting an orientation, return " + mLastOrientation
4134 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004135 // The next app has not been requested to be visible, so we keep the current orientation
4136 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08004137 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004138 }
Robert Carrb1579c82017-09-05 14:54:47 -07004139
4140 @Override
4141 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004142 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004143
Robert Carr2f8aa392018-01-31 14:46:51 -08004144 for (int i = 0; i < mChildren.size(); i++) {
4145 final TaskStack s = mChildren.get(i);
4146 s.assignChildLayers(t);
4147 }
4148 }
4149
4150 void assignStackOrdering(SurfaceControl.Transaction t) {
Jorim Jaggibe418292018-03-26 16:14:12 +02004151
Robert Carrf7a7ca82017-12-06 13:17:07 -08004152 final int HOME_STACK_STATE = 0;
4153 final int NORMAL_STACK_STATE = 1;
4154 final int ALWAYS_ON_TOP_STATE = 2;
4155
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004156 int layer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004157 int layerForAnimationLayer = 0;
Jorim Jaggibe418292018-03-26 16:14:12 +02004158 int layerForBoostedAnimationLayer = 0;
Jorim Jaggi391790622018-04-18 15:30:44 +02004159 int layerForHomeAnimationLayer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004160
Robert Carrf7a7ca82017-12-06 13:17:07 -08004161 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
4162 for (int i = 0; i < mChildren.size(); i++) {
4163 final TaskStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08004164 if (state == HOME_STACK_STATE && !s.isActivityTypeHome()) {
4165 continue;
4166 } else if (state == NORMAL_STACK_STATE && (s.isActivityTypeHome()
4167 || s.isAlwaysOnTop())) {
4168 continue;
4169 } else if (state == ALWAYS_ON_TOP_STATE && !s.isAlwaysOnTop()) {
4170 continue;
4171 }
4172 s.assignLayer(t, layer++);
4173 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
4174 t.setLayer(mSplitScreenDividerAnchor, layer++);
4175 }
Winson Chungd41f71d2018-03-16 15:26:07 -07004176 if ((s.isTaskAnimating() || s.isAppAnimating())
4177 && state != ALWAYS_ON_TOP_STATE) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004178 // Ensure the animation layer ends up above the
4179 // highest animating stack and no higher.
4180 layerForAnimationLayer = layer++;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004181 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004182 if (state != ALWAYS_ON_TOP_STATE) {
4183 layerForBoostedAnimationLayer = layer++;
4184 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08004185 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004186 if (state == HOME_STACK_STATE) {
4187 layerForHomeAnimationLayer = layer++;
4188 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004189 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004190 if (mAppAnimationLayer != null) {
4191 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004192 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004193 if (mBoostedAppAnimationLayer != null) {
4194 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
4195 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004196 if (mHomeAppAnimationLayer != null) {
4197 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
4198 }
Robert Carrb1579c82017-09-05 14:54:47 -07004199 }
4200
4201 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02004202 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
4203 switch (animationLayer) {
4204 case ANIMATION_LAYER_BOOSTED:
4205 return mBoostedAppAnimationLayer;
4206 case ANIMATION_LAYER_HOME:
4207 return mHomeAppAnimationLayer;
4208 case ANIMATION_LAYER_STANDARD:
4209 default:
4210 return mAppAnimationLayer;
4211 }
chaviw23ee71c2017-12-18 11:29:41 -08004212 }
4213
Robert Carrf7a7ca82017-12-06 13:17:07 -08004214 SurfaceControl getSplitScreenDividerAnchor() {
4215 return mSplitScreenDividerAnchor;
4216 }
4217
chaviw23ee71c2017-12-18 11:29:41 -08004218 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07004219 void onParentSet() {
4220 super.onParentSet();
4221 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08004222 mAppAnimationLayer = makeChildSurface(null)
4223 .setName("animationLayer")
4224 .build();
Jorim Jaggibe418292018-03-26 16:14:12 +02004225 mBoostedAppAnimationLayer = makeChildSurface(null)
4226 .setName("boostedAnimationLayer")
4227 .build();
Jorim Jaggi391790622018-04-18 15:30:44 +02004228 mHomeAppAnimationLayer = makeChildSurface(null)
4229 .setName("homeAnimationLayer")
4230 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08004231 mSplitScreenDividerAnchor = makeChildSurface(null)
4232 .setName("splitScreenDividerAnchor")
4233 .build();
4234 getPendingTransaction()
4235 .show(mAppAnimationLayer)
Jorim Jaggibe418292018-03-26 16:14:12 +02004236 .show(mBoostedAppAnimationLayer)
Jorim Jaggi391790622018-04-18 15:30:44 +02004237 .show(mHomeAppAnimationLayer)
Robert Carrf7a7ca82017-12-06 13:17:07 -08004238 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08004239 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07004240 } else {
chaviw23ee71c2017-12-18 11:29:41 -08004241 mAppAnimationLayer.destroy();
4242 mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02004243 mBoostedAppAnimationLayer.destroy();
4244 mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02004245 mHomeAppAnimationLayer.destroy();
4246 mHomeAppAnimationLayer = null;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004247 mSplitScreenDividerAnchor.destroy();
4248 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07004249 }
4250 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004251 }
4252
Robert Carree4d4b92017-11-22 12:21:46 -08004253 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004254 AboveAppWindowContainers(String name, WindowManagerService service) {
4255 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08004256 }
4257
Robert Carrb9506032018-02-13 13:54:00 -08004258 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07004259 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
4260 final SurfaceControl.Builder builder = super.makeChildSurface(child);
4261 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
4262 // To draw above the ColorFade layer during the screen off transition, the
4263 // rounded corner overlays need to be at the root of the surface hierarchy.
4264 // TODO: move the ColorLayer into the display overlay layer such that this is not
4265 // necessary anymore.
4266 builder.setParent(null);
4267 }
4268 return builder;
4269 }
4270
4271 @Override
Robert Carrb9506032018-02-13 13:54:00 -08004272 void assignChildLayers(SurfaceControl.Transaction t) {
4273 assignChildLayers(t, null /* imeContainer */);
4274 }
4275
Robert Carree4d4b92017-11-22 12:21:46 -08004276 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
4277 boolean needAssignIme = imeContainer != null
4278 && imeContainer.getSurfaceControl() != null;
4279 for (int j = 0; j < mChildren.size(); ++j) {
4280 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004281
4282 // See {@link mSplitScreenDividerAnchor}
4283 if (wt.windowType == TYPE_DOCK_DIVIDER) {
4284 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
4285 continue;
4286 }
Vishnu Nair83537a72018-07-19 21:27:48 -07004287 if (wt.mRoundedCornerOverlay) {
4288 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
4289 continue;
4290 }
Robert Carree4d4b92017-11-22 12:21:46 -08004291 wt.assignLayer(t, j);
4292 wt.assignChildLayers(t);
4293
4294 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
4295 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08004296
4297 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
4298 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004299 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08004300 needAssignIme = false;
4301 }
4302 }
4303 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004304 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08004305 }
4306 }
4307 }
4308
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004309 /**
4310 * Window container class that contains all containers on this display that are not related to
4311 * Apps. E.g. status bar.
4312 */
Robert Carree4d4b92017-11-22 12:21:46 -08004313 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07004314 /**
4315 * Compares two child window tokens returns -1 if the first is lesser than the second in
4316 * terms of z-order and 1 otherwise.
4317 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004318 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07004319 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004320 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
4321 token1.mOwnerCanManageAppTokens)
4322 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
4323 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004324
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004325 private final Predicate<WindowState> mGetOrientingWindow = w -> {
4326 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
4327 return false;
4328 }
4329 final int req = w.mAttrs.screenOrientation;
4330 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
4331 || req == SCREEN_ORIENTATION_UNSET) {
4332 return false;
4333 }
4334 return true;
4335 };
4336
Wale Ogunwale3a931692016-11-02 16:49:48 -07004337 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08004338 private final Dimmer mDimmer = new Dimmer(this);
4339 private final Rect mTmpDimBoundsRect = new Rect();
4340
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004341 NonAppWindowContainers(String name, WindowManagerService service) {
4342 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004343 mName = name;
4344 }
4345
4346 void addChild(WindowToken token) {
4347 addChild(token, mWindowComparator);
4348 }
4349
4350 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004351 int getOrientation() {
4352 final WindowManagerPolicy policy = mService.mPolicy;
4353 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004354 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004355
4356 if (win != null) {
4357 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004358 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004359 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07004360 if (mService.mKeyguardGoingAway) {
4361 // Keyguard can't affect the orientation if it is going away...
4362 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
4363 return SCREEN_ORIENTATION_UNSET;
4364 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004365 }
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004366 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req
4367 + " for display id=" + mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08004368 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004369 }
4370
Andrii Kulian8ee72852017-03-10 10:36:45 -08004371 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004372
Jorim Jaggi1c530592018-04-06 15:11:47 +02004373 // Only allow force setting the orientation when all unknown visibilities have been
4374 // resolved, as otherwise we just may be starting another occluding activity.
4375 final boolean isUnoccluding =
lumark588a3e82018-07-20 18:53:54 +08004376 mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
4377 && mUnknownAppVisibilityController.allResolved();
Jorim Jaggi1c530592018-04-06 15:11:47 +02004378 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004379 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004380 }
4381
4382 return SCREEN_ORIENTATION_UNSET;
4383 }
4384
4385 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07004386 String getName() {
4387 return mName;
4388 }
chaviwf29223e2018-01-31 13:23:51 -08004389
4390 @Override
4391 Dimmer getDimmer() {
4392 return mDimmer;
4393 }
4394
4395 @Override
4396 void prepareSurfaces() {
4397 mDimmer.resetDimStates();
4398 super.prepareSurfaces();
4399 getBounds(mTmpDimBoundsRect);
4400
4401 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
4402 scheduleAnimation();
4403 }
4404 }
Robert Carr3b716242016-08-16 16:02:21 -07004405 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01004406
Robert Carr9034d962018-01-04 18:27:42 -08004407 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
4408 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
4409 super(name, service);
4410 }
4411
4412 @Override
4413 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
4414 }
4415 };
4416
Robert Carrb1579c82017-09-05 14:54:47 -07004417 SurfaceControl.Builder makeSurface(SurfaceSession s) {
4418 return mService.makeSurfaceBuilder(s)
4419 .setParent(mWindowingLayer);
4420 }
4421
4422 @Override
4423 SurfaceSession getSession() {
4424 return mSession;
4425 }
4426
4427 @Override
4428 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07004429 SurfaceSession s = child != null ? child.getSession() : getSession();
4430 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07004431 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07004432
4433 if (child == null) {
4434 return b;
4435 }
4436
Robert Carree4d4b92017-11-22 12:21:46 -08004437 return b.setName(child.getName())
4438 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004439 }
4440
4441 /**
4442 * The makeSurface variants are for use by the window-container
4443 * hierarchy. makeOverlay here is a function for various non windowing
4444 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
4445 * and other potpourii.
4446 */
4447 SurfaceControl.Builder makeOverlay() {
4448 return mService.makeSurfaceBuilder(mSession)
4449 .setParent(mOverlayLayer);
4450 }
4451
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004452 /**
4453 * Reparents the given surface to mOverlayLayer.
4454 */
4455 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
4456 transaction.reparent(surface, mOverlayLayer.getHandle());
4457 }
4458
Robert Carrb1579c82017-09-05 14:54:47 -07004459 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07004460 if (spec.scale != 1.0) {
4461 mMagnificationSpec = spec;
4462 } else {
4463 mMagnificationSpec = null;
4464 }
4465
Robert Carrf59b8dd2017-10-02 18:58:36 -07004466 applyMagnificationSpec(getPendingTransaction(), spec);
4467 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07004468 }
4469
Robert Carr24be9ab2018-04-30 17:54:53 -07004470 void reapplyMagnificationSpec() {
4471 if (mMagnificationSpec != null) {
4472 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4473 }
4474 }
4475
Robert Carrb1579c82017-09-05 14:54:47 -07004476 @Override
4477 void onParentSet() {
4478 // Since we are the top of the SurfaceControl hierarchy here
4479 // we create the root surfaces explicitly rather than chaining
4480 // up as the default implementation in onParentSet does. So we
4481 // explicitly do NOT call super here.
4482 }
4483
4484 @Override
4485 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004486
4487 // These are layers as children of "mWindowingLayer"
4488 mBelowAppWindowsContainers.assignLayer(t, 0);
4489 mTaskStackContainers.assignLayer(t, 1);
4490 mAboveAppWindowsContainers.assignLayer(t, 2);
4491
4492 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004493 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004494
Robert Carree4d4b92017-11-22 12:21:46 -08004495 // In the case where we have an IME target that is not in split-screen
4496 // mode IME assignment is easy. We just need the IME to go directly above
4497 // the target. This way children of the target will naturally go above the IME
4498 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004499 //
Robert Carree4d4b92017-11-22 12:21:46 -08004500 // In the case of split-screen windowing mode, we need to elevate the IME above the
4501 // docked divider while keeping the app itself below the docked divider, so instead
4502 // we use relative layering of the IME targets child windows, and place the
4503 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004504 //
Robert Carr234b6332018-03-20 13:38:16 -07004505 // In the case the IME target is animating, the animation Z order may be different
4506 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4507 // IME target. In this case we just layer the IME over all transitions by placing it in the
4508 // above applications layer.
4509 //
Robert Carree4d4b92017-11-22 12:21:46 -08004510 // In the case where we have no IME target we assign it where it's base layer would
4511 // place it in the AboveAppWindowContainers.
Robert Carr234b6332018-03-20 13:38:16 -07004512 if (imeTarget != null && !(imeTarget.inSplitScreenWindowingMode()
4513 || imeTarget.mToken.isAppAnimating())
Robert Carree4d4b92017-11-22 12:21:46 -08004514 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004515 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004516 // TODO: We need to use an extra level on the app surface to ensure
4517 // this is always above SurfaceView but always below attached window.
4518 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004519 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004520 }
4521
4522 // Above we have assigned layers to our children, now we ask them to assign
4523 // layers to their children.
4524 mBelowAppWindowsContainers.assignChildLayers(t);
4525 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004526 mAboveAppWindowsContainers.assignChildLayers(t,
4527 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004528 mImeWindowsContainers.assignChildLayers(t);
4529 }
4530
4531 /**
4532 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4533 * that the IME target is one of the docked applications. We'd like the docked divider to be
4534 * above both of the applications, and we'd like the IME to be above the docked divider.
4535 * However we need child windows of the applications to be above the IME (Text drag handles).
4536 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4537 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
4538 * with {@link #WindowState#assignLayer}
4539 */
4540 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004541 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004542 }
4543
4544 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004545 void prepareSurfaces() {
4546 final ScreenRotationAnimation screenRotationAnimation =
4547 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
4548 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
4549 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
4550 mPendingTransaction.setMatrix(mWindowingLayer,
4551 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
4552 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
4553 mPendingTransaction.setPosition(mWindowingLayer,
4554 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
4555 mPendingTransaction.setAlpha(mWindowingLayer,
4556 screenRotationAnimation.getEnterTransformation().getAlpha());
4557 }
Chavi Weingartenb736e322018-02-23 00:27:54 +00004558
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004559 super.prepareSurfaces();
4560 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004561
Jorim Jaggibe418292018-03-26 16:14:12 +02004562 void assignStackOrdering() {
4563 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004564 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004565
4566 /**
4567 * Increment the deferral count to determine whether to update the IME target.
4568 */
4569 void deferUpdateImeTarget() {
4570 mDeferUpdateImeTargetCount++;
4571 }
4572
4573 /**
4574 * Decrement the deferral count to determine whether to update the IME target. If the count
4575 * reaches 0, a new ime target will get computed.
4576 */
4577 void continueUpdateImeTarget() {
4578 if (mDeferUpdateImeTargetCount == 0) {
4579 return;
4580 }
4581
4582 mDeferUpdateImeTargetCount--;
4583 if (mDeferUpdateImeTargetCount == 0) {
4584 computeImeTarget(true /* updateImeTarget */);
4585 }
4586 }
4587
4588 /**
4589 * @return Whether a new IME target should be computed.
4590 */
4591 private boolean canUpdateImeTarget() {
4592 return mDeferUpdateImeTargetCount == 0;
4593 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004594
4595 InputMonitor getInputMonitor() {
4596 return mInputMonitor;
4597 }
Jorim Jaggif1292892018-09-10 11:58:13 +02004598
4599 /**
4600 * @return Cached value whether we told display manager that we have content.
4601 */
4602 boolean getLastHasContent() {
4603 return mLastHasContent;
4604 }
Arthur Hungbe5ce212018-09-13 18:41:56 +08004605
4606 void registerPointerEventListener(@NonNull PointerEventListener listener) {
4607 if (mPointerEventDispatcher != null) {
4608 mPointerEventDispatcher.registerInputEventListener(listener);
4609 }
4610 }
4611
4612 void unregisterPointerEventListener(@NonNull PointerEventListener listener) {
4613 if (mPointerEventDispatcher != null) {
4614 mPointerEventDispatcher.unregisterInputEventListener(listener);
4615 }
4616 }
lumark588a3e82018-07-20 18:53:54 +08004617
4618 void prepareAppTransition(@WindowManager.TransitionType int transit,
4619 boolean alwaysKeepCurrent, @WindowManager.TransitionFlags int flags,
4620 boolean forceOverride) {
4621 final boolean prepared = mAppTransition.prepareAppTransitionLocked(
4622 transit, alwaysKeepCurrent, flags, forceOverride);
4623 if (prepared && okToAnimate()) {
4624 mSkipAppTransitionAnimation = false;
4625 }
4626 }
4627
4628 void executeAppTransition() {
4629 if (mAppTransition.isTransitionSet()) {
4630 if (DEBUG_APP_TRANSITIONS) {
4631 Slog.w(TAG_WM, "Execute app transition: " + mAppTransition + ", displayId: "
4632 + mDisplayId + " Callers=" + Debug.getCallers(5));
4633 }
4634 mAppTransition.setReady();
4635 mService.mWindowPlacerLocked.requestTraversal();
4636 }
4637 }
4638
4639 /**
4640 * Update pendingLayoutChanges after app transition has finished.
4641 */
4642 void handleAnimatingStoppedAndTransition() {
4643 int changes = 0;
4644
4645 mAppTransition.setIdle();
4646
4647 for (int i = mNoAnimationNotifyOnTransitionFinished.size() - 1; i >= 0; i--) {
4648 final IBinder token = mNoAnimationNotifyOnTransitionFinished.get(i);
4649 mAppTransition.notifyAppTransitionFinishedLocked(token);
4650 }
4651 mNoAnimationNotifyOnTransitionFinished.clear();
4652
4653 mWallpaperController.hideDeferredWallpapersIfNeeded();
4654
4655 onAppTransitionDone();
4656
4657 changes |= FINISH_LAYOUT_REDO_LAYOUT;
4658 if (DEBUG_WALLPAPER_LIGHT) {
4659 Slog.v(TAG_WM, "Wallpaper layer changed: assigning layers + relayout");
4660 }
4661 computeImeTarget(true /* updateImeTarget */);
4662 mService.mRoot.mWallpaperMayChange = true;
4663 // Since the window list has been rebuilt, focus might have to be recomputed since the
4664 // actual order of windows might have changed again.
4665 mService.mFocusMayChange = true;
4666
4667 pendingLayoutChanges |= changes;
4668 }
Andrii Kulian15cfb422018-11-07 13:38:49 -08004669
4670 /**
4671 * @see Display#FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS
4672 */
4673 boolean supportsSystemDecorations() {
4674 // TODO(b/114338689): Read the setting from DisplaySettings.
4675 return mDisplay.supportsSystemDecorations()
4676 // TODO (b/111363427): Remove this and set the new FLAG_SHOULD_SHOW_LAUNCHER flag
4677 // (b/114338689) whenever vr 2d display id is set.
4678 || mDisplayId == mService.mVr2dDisplayId
4679 || mService.mForceDesktopModeOnExternalDisplays;
4680 }
Craig Mautner59c00972012-07-30 12:10:24 -07004681}