blob: bec72f5686df764d501ffeebec0592af57fa8350 [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;
Tarandeep Singh215929b2019-01-11 18:24:37 -080023import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070024import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
25import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070026import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070027import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
28import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale687b4272017-07-27 02:56:23 -070029import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
30import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070031import static android.view.Display.DEFAULT_DISPLAY;
32import static android.view.Display.FLAG_PRIVATE;
Andrii Kuliandd989612019-02-21 12:13:28 -080033import static android.view.Display.FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS;
Tiger Huang04dc4cc2019-01-17 18:41:41 +080034import static android.view.Display.INVALID_DISPLAY;
chaviwff2e7d82018-11-02 11:11:27 -070035import static android.view.InsetsState.TYPE_IME;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070036import static android.view.Surface.ROTATION_0;
37import static android.view.Surface.ROTATION_180;
38import static android.view.Surface.ROTATION_270;
39import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070040import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070041import static android.view.WindowManager.DOCKED_BOTTOM;
42import static android.view.WindowManager.DOCKED_INVALID;
43import static android.view.WindowManager.DOCKED_TOP;
Winson Chungc5fe7ff2019-02-19 14:49:25 -080044import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080045import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
46import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
47import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070048import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070049import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Tiger Huang04dc4cc2019-01-17 18:41:41 +080050import static android.view.WindowManager.LayoutParams.FLAG_SPLIT_TOUCH;
Winson Chungc5fe7ff2019-02-19 14:49:25 -080051import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070052import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
53import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070054import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070055import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070056import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070057import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Robert Carrf7a7ca82017-12-06 13:17:07 -080058import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
Wale Ogunwale494009b82016-10-21 09:01:38 -070059import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070060import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070061import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
62import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070063import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070064import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
65import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070066import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070067import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale3a256e62018-12-06 14:41:18 -080068import static android.view.WindowManager.TRANSIT_ACTIVITY_OPEN;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080069import static android.view.WindowManager.TRANSIT_KEYGUARD_UNOCCLUDE;
Wale Ogunwale3a256e62018-12-06 14:41:18 -080070import static android.view.WindowManager.TRANSIT_TASK_OPEN;
71import static android.view.WindowManager.TRANSIT_TASK_TO_FRONT;
Tiger Huang51c5a1d2018-12-11 20:24:51 +080072
Adrian Roose99bc052017-11-20 17:55:31 +010073import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
74import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
75import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
76import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080077import static com.android.server.wm.DisplayContentProto.ABOVE_APP_WINDOWS;
lumark588a3e82018-07-20 18:53:54 +080078import static com.android.server.wm.DisplayContentProto.APP_TRANSITION;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080079import static com.android.server.wm.DisplayContentProto.BELOW_APP_WINDOWS;
80import static com.android.server.wm.DisplayContentProto.DISPLAY_FRAMES;
81import static com.android.server.wm.DisplayContentProto.DISPLAY_INFO;
82import static com.android.server.wm.DisplayContentProto.DOCKED_STACK_DIVIDER_CONTROLLER;
83import static com.android.server.wm.DisplayContentProto.DPI;
Tiger Huang1e5b10a2018-07-30 20:19:51 +080084import static com.android.server.wm.DisplayContentProto.FOCUSED_APP;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080085import static com.android.server.wm.DisplayContentProto.ID;
86import static com.android.server.wm.DisplayContentProto.IME_WINDOWS;
87import static com.android.server.wm.DisplayContentProto.PINNED_STACK_CONTROLLER;
88import static com.android.server.wm.DisplayContentProto.ROTATION;
89import static com.android.server.wm.DisplayContentProto.SCREEN_ROTATION_ANIMATION;
90import static com.android.server.wm.DisplayContentProto.STACKS;
91import static com.android.server.wm.DisplayContentProto.WINDOW_CONTAINER;
Wale Ogunwale1666e312016-12-16 11:27:18 -080092import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
lumark588a3e82018-07-20 18:53:54 +080093import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_APP_TRANSITIONS;
Wale Ogunwale494009b82016-10-21 09:01:38 -070094import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070095import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070096import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
97import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070098import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070099import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700100import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +0200101import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700102import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700103import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -0700104import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800105import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700106import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700107import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700108import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700109import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800110import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700111import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Tiger Huang51c5a1d2018-12-11 20:24:51 +0800112import static com.android.server.wm.WindowManagerService.H.REPORT_FOCUS_CHANGE;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800113import static com.android.server.wm.WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800114import static com.android.server.wm.WindowManagerService.H.REPORT_LOSING_FOCUS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700115import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
116import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700117import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700118import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700119import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
120import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800121import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_PLACING_SURFACES;
122import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_REMOVING_FOCUS;
123import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_ASSIGN_LAYERS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700124import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700125import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700126import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700127import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700128import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700129import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700130import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700131import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700132import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Adrian Roos4ffc8972019-02-07 20:45:11 +0100133import static com.android.server.wm.utils.RegionUtils.rectListToRegion;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700134
lumark588a3e82018-07-20 18:53:54 +0800135import android.animation.AnimationHandler;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700136import android.annotation.CallSuper;
Riddle Hsuf53da812018-08-15 22:00:27 +0800137import android.annotation.IntDef;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700138import android.annotation.NonNull;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200139import android.annotation.Nullable;
Dean Harding3e5a1522017-10-06 09:19:01 -0700140import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700141import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700142import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700143import android.graphics.Bitmap;
Issei Suzukia5dbf522019-02-01 17:58:15 +0100144import android.graphics.Insets;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700145import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800146import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700147import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700148import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100149import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700150import android.hardware.display.DisplayManagerInternal;
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500151import android.metrics.LogMaker;
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800152import android.os.Binder;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700153import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700154import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700155import android.os.IBinder;
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800156import android.os.Process;
Adrian Roos4ffc8972019-02-07 20:45:11 +0100157import android.os.RemoteCallbackList;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700158import android.os.RemoteException;
159import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100160import android.os.Trace;
Riddle Hsuf53da812018-08-15 22:00:27 +0800161import android.os.UserHandle;
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800162import android.util.ArraySet;
Chong Zhang8e89b312015-09-09 15:09:30 -0700163import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700164import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700165import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700166import android.view.Display;
Adrian Roos1cf585052018-01-03 18:43:27 +0100167import android.view.DisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700168import android.view.DisplayInfo;
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800169import android.view.Gravity;
Adrian Roos4ffc8972019-02-07 20:45:11 +0100170import android.view.ISystemGestureExclusionListener;
Arthur Hungbe5ce212018-09-13 18:41:56 +0800171import android.view.InputChannel;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700172import android.view.InputDevice;
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800173import android.view.InputWindowHandle;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200174import android.view.InsetsState.InternalInsetType;
Robert Carrb1579c82017-09-05 14:54:47 -0700175import android.view.MagnificationSpec;
Evan Rosky966759f2019-01-15 10:33:58 -0800176import android.view.RemoteAnimationDefinition;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700177import android.view.Surface;
178import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100179import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700180import android.view.SurfaceSession;
Tiger Huang7c610aa2018-10-27 00:01:01 +0800181import android.view.View;
lumark588a3e82018-07-20 18:53:54 +0800182import android.view.WindowManager;
Evan Rosky39b6f232018-10-30 18:35:41 -0700183import android.view.WindowManagerPolicyConstants.PointerEventListener;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700184
Bryce Lee48f4b572017-04-10 10:54:15 -0700185import com.android.internal.annotations.VisibleForTesting;
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500186import com.android.internal.logging.MetricsLogger;
187import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800188import com.android.internal.util.ToBooleanFunction;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200189import com.android.internal.util.function.TriConsumer;
Winson Chungc5fe7ff2019-02-19 14:49:25 -0800190import com.android.internal.util.function.pooled.PooledConsumer;
191import com.android.internal.util.function.pooled.PooledLambda;
Winson Chung6ba767a2018-12-13 18:20:11 -0800192import com.android.server.AnimationThread;
Adrian Roose99bc052017-11-20 17:55:31 +0100193import com.android.server.policy.WindowManagerPolicy;
Issei Suzuki43190bd2018-08-20 17:28:41 +0200194import com.android.server.wm.utils.DisplayRotationUtil;
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100195import com.android.server.wm.utils.RotationCache;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100196import com.android.server.wm.utils.WmDisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700197
198import java.io.PrintWriter;
Riddle Hsuf53da812018-08-15 22:00:27 +0800199import java.lang.annotation.Retention;
200import java.lang.annotation.RetentionPolicy;
Craig Mautner59c00972012-07-30 12:10:24 -0700201import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700202import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700203import java.util.HashMap;
204import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700205import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700206import java.util.List;
Adrian Roos1cf585052018-01-03 18:43:27 +0100207import java.util.Objects;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800208import java.util.function.Consumer;
209import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700210
Craig Mautner59c00972012-07-30 12:10:24 -0700211/**
212 * Utility class for keeping track of the WindowStates and other pertinent contents of a
213 * particular Display.
Craig Mautner59c00972012-07-30 12:10:24 -0700214 */
Riddle Hsuad256a12018-07-18 16:11:30 +0800215class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer>
216 implements WindowManagerPolicy.DisplayContentInfo {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700217 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700218
Riddle Hsuf53da812018-08-15 22:00:27 +0800219 /** The default scaling mode that scales content automatically. */
220 static final int FORCE_SCALING_MODE_AUTO = 0;
221 /** For {@link #setForcedScalingMode} to apply flag {@link Display#FLAG_SCALING_DISABLED}. */
222 static final int FORCE_SCALING_MODE_DISABLED = 1;
223
224 @IntDef(prefix = { "FORCE_SCALING_MODE_" }, value = {
225 FORCE_SCALING_MODE_AUTO,
226 FORCE_SCALING_MODE_DISABLED
227 })
228 @Retention(RetentionPolicy.SOURCE)
229 @interface ForceScalingMode {}
230
Craig Mautner59c00972012-07-30 12:10:24 -0700231 /** Unique identifier of this stack. */
232 private final int mDisplayId;
233
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800234 // TODO: Remove once unification is complete.
235 ActivityDisplay mAcitvityDisplay;
236
Wale Ogunwale3a931692016-11-02 16:49:48 -0700237 /** The containers below are the only child containers the display can have. */
238 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800239 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700240 // Contains all non-app window containers that should be displayed above the app containers
241 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800242 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800243 new AboveAppWindowContainers("mAboveAppWindowsContainers", mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700244 // Contains all non-app window containers that should be displayed below the app containers
245 // (e.g. Wallpaper).
246 private final NonAppWindowContainers mBelowAppWindowsContainers =
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800247 new NonAppWindowContainers("mBelowAppWindowsContainers", mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700248 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
249 // 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 -0800250 // window containers together and move them in-sync if/when needed. We use a subclass of
251 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
252 private final NonMagnifiableWindowContainers mImeWindowsContainers =
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800253 new NonMagnifiableWindowContainers("mImeWindowsContainers", mWmService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700254
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000255 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800256 private WindowState mTmpWindow2;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800257 private boolean mTmpRecoveringMemory;
258 private boolean mUpdateImeTarget;
259 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700260 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700261
lumark588a3e82018-07-20 18:53:54 +0800262 final AppTransition mAppTransition;
263 final AppTransitionController mAppTransitionController;
264 boolean mSkipAppTransitionAnimation = false;
265
266 final ArraySet<AppWindowToken> mOpeningApps = new ArraySet<>();
267 final ArraySet<AppWindowToken> mClosingApps = new ArraySet<>();
Evan Rosky2289ba12018-11-19 18:28:18 -0800268 final ArraySet<AppWindowToken> mChangingApps = new ArraySet<>();
lumark588a3e82018-07-20 18:53:54 +0800269 final UnknownAppVisibilityController mUnknownAppVisibilityController;
270 BoundsAnimationController mBoundsAnimationController;
271
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500272 private MetricsLogger mMetricsLogger;
273
lumark588a3e82018-07-20 18:53:54 +0800274 /**
275 * List of clients without a transtiton animation that we notify once we are done
276 * transitioning since they won't be notified through the app window animator.
277 */
278 final List<IBinder> mNoAnimationNotifyOnTransitionFinished = new ArrayList<>();
279
Wale Ogunwale02319a62016-09-26 15:21:22 -0700280 // Mapping from a token IBinder to a WindowToken object on this display.
281 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
282
Andrii Kuliancd097992017-03-23 18:31:59 -0700283 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700284 int mInitialDisplayWidth = 0;
285 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700286 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700287
Adrian Roos1cf585052018-01-03 18:43:27 +0100288 DisplayCutout mInitialDisplayCutout;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100289 private final RotationCache<DisplayCutout, WmDisplayCutout> mDisplayCutoutCache
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100290 = new RotationCache<>(this::calculateDisplayCutoutForRotationUncached);
Adrian Roos1cf585052018-01-03 18:43:27 +0100291
Andrii Kuliancd097992017-03-23 18:31:59 -0700292 /**
293 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
294 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
295 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
296 */
Craig Mautner59c00972012-07-30 12:10:24 -0700297 int mBaseDisplayWidth = 0;
298 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700299 /**
300 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
301 * but can be set from Settings or via shell command "adb shell wm density".
302 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
303 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700304 int mBaseDisplayDensity = 0;
Riddle Hsuf53da812018-08-15 22:00:27 +0800305
306 /**
307 * Whether to disable display scaling. This can be set via shell command "adb shell wm scaling".
308 * @see WindowManagerService#setForcedDisplayScalingMode(int, int)
309 */
Jeff Brownd46747a2015-04-15 19:02:36 -0700310 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700311 private final DisplayInfo mDisplayInfo = new DisplayInfo();
312 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700313 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800314 private final DisplayPolicy mDisplayPolicy;
315 private DisplayRotation mDisplayRotation;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000316 DisplayFrames mDisplayFrames;
317
Adrian Roos4ffc8972019-02-07 20:45:11 +0100318 private final RemoteCallbackList<ISystemGestureExclusionListener>
319 mSystemGestureExclusionListeners = new RemoteCallbackList<>();
320 private final Region mSystemGestureExclusion = new Region();
321
Andrii Kulian06d07d62017-03-14 11:11:47 -0700322 /**
323 * For default display it contains real metrics, empty for others.
324 * @see WindowManagerService#createWatermarkInTransaction()
325 */
326 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800327
328 /** @see #computeCompatSmallestWidth(boolean, int, int, int, DisplayCutout) */
Andrii Kulian06d07d62017-03-14 11:11:47 -0700329 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700330
Andrii Kulian06d07d62017-03-14 11:11:47 -0700331 /**
332 * Compat metrics computed based on {@link #mDisplayMetrics}.
333 * @see #updateDisplayAndOrientation(int)
334 */
335 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
336
337 /** The desired scaling factor for compatible apps. */
338 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700339
Andrii Kulian8ee72852017-03-10 10:36:45 -0800340 /**
341 * Current rotation of the display.
342 * Constants as per {@link android.view.Surface.Rotation}.
343 *
Robert Carrae606b42018-02-15 15:36:23 -0800344 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800345 */
346 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700347
Andrii Kulian8ee72852017-03-10 10:36:45 -0800348 /**
349 * Last applied orientation of the display.
350 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
351 *
Riddle Hsu4e611772018-10-31 18:58:28 +0800352 * @see #updateOrientationFromAppTokens()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800353 */
354 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700355
Andrii Kulian8ee72852017-03-10 10:36:45 -0800356 /**
Andrii Kulian8ee72852017-03-10 10:36:45 -0800357 * Orientation forced by some window. If there is no visible window that specifies orientation
358 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
359 *
360 * @see NonAppWindowContainers#getOrientation()
361 */
362 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700363
Andrii Kulian8ee72852017-03-10 10:36:45 -0800364 /**
365 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
366 * occluded.
367 *
368 * @see NonAppWindowContainers#getOrientation()
369 */
370 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
371
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700372 /**
373 * Keep track of wallpaper visibility to notify changes.
374 */
375 private boolean mLastWallpaperVisible = false;
376
Andrii Kulian06d07d62017-03-14 11:11:47 -0700377 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700378
Craig Mautner39834192012-09-02 07:47:24 -0700379 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700380 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700381 int pendingLayoutChanges;
Riddle Hsu654a6f92018-07-13 22:59:36 +0800382 int mDeferredRotationPauseCount;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800383
Vishnu Nairba183352018-11-21 11:16:49 -0800384 /**
385 * Used to gate application window layout until we have sent the complete configuration.
386 * TODO: There are still scenarios where we may be out of sync with the client. Ideally
387 * we want to replace this flag with a mechanism that will confirm the configuration
388 * applied by the client is the one expected by the system server.
389 */
390 boolean mWaitingForConfig;
391
Andrii Kulian839def92016-11-02 10:58:58 -0700392 // TODO(multi-display): remove some of the usages.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800393 @VisibleForTesting
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800394 boolean isDefaultDisplay;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800395
Andrii Kulianf0379de2018-03-14 16:24:07 -0700396 /**
397 * Flag indicating whether WindowManager should override info for this display in
398 * DisplayManager.
399 */
400 boolean mShouldOverrideDisplayConfiguration = true;
Craig Mautner39834192012-09-02 07:47:24 -0700401
Craig Mautnerdc548482014-02-05 13:35:24 -0800402 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700403 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800404
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700405 /** Detect user tapping outside of current focused task bounds .*/
Riddle Hsu2588ab02019-02-25 14:23:56 +0800406 @VisibleForTesting
407 final TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700408
Craig Mautner6601b7b2013-04-29 10:29:11 -0700409 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700410 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700411
Craig Mautner6601b7b2013-04-29 10:29:11 -0700412 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800413 private final Rect mTmpRect = new Rect();
414 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700415 private final RectF mTmpRectF = new RectF();
416 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800417 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700418
Bryce Leef3c6a472017-11-14 14:53:06 -0800419 /** Used for handing back size of display */
420 private final Rect mTmpBounds = new Rect();
421
Garfield Tan90b04282018-12-11 14:04:42 -0800422 private final Configuration mTmpConfiguration = new Configuration();
423
Craig Mautner95da1082014-02-24 17:54:35 -0800424 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700425 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800426
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700427 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700428 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700429
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800430 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800431 /** A collection of windows that provide tap exclude regions inside of them. */
432 final ArraySet<WindowState> mTapExcludeProvidingWindows = new ArraySet<>();
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800433
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000434 private boolean mHaveBootMsg = false;
435 private boolean mHaveApp = false;
436 private boolean mHaveWallpaper = false;
437 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700438
439 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
440
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700441 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
442 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000443 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
444 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700445
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700446 // True if this display is in the process of being removed. Used to determine if the removal of
447 // the display's direct children should be allowed.
448 private boolean mRemovingDisplay = false;
449
Bryce Leed1871262017-06-12 14:12:29 -0700450 // {@code false} if this display is in the processing of being created.
451 private boolean mDisplayReady = false;
452
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800453 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700454
wilsonshihc32538e2018-11-07 17:27:34 +0800455 boolean mWallpaperMayChange = false;
456
Robert Carrb1579c82017-09-05 14:54:47 -0700457 private final SurfaceSession mSession = new SurfaceSession();
458
459 /**
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800460 * Window that is currently interacting with the user. This window is responsible for receiving
461 * key events and pointer events from the user.
462 */
463 WindowState mCurrentFocus = null;
464
465 /**
466 * The last focused window that we've notified the client that the focus is changed.
467 */
468 WindowState mLastFocus = null;
469
470 /**
471 * Windows that have lost input focus and are waiting for the new focus window to be displayed
472 * before they are told about this.
473 */
474 ArrayList<WindowState> mLosingFocus = new ArrayList<>();
475
476 /**
477 * The foreground app of this display. Windows below this app cannot be the focused window. If
478 * the user taps on the area outside of the task of the focused app, we will notify AM about the
479 * new task the user wants to interact with.
480 */
481 AppWindowToken mFocusedApp = null;
482
483 /** Windows added since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
484 final ArrayList<WindowState> mWinAddedSinceNullFocus = new ArrayList<>();
485
486 /** Windows removed since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
487 final ArrayList<WindowState> mWinRemovedSinceNullFocus = new ArrayList<>();
488
489 /**
Robert Carrb1579c82017-09-05 14:54:47 -0700490 * We organize all top-level Surfaces in to the following layers.
491 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800492 * and omitted from Screen-Magnification, for example the strict mode flash or
493 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700494 * {@link #mWindowingLayer} contains everything else.
495 */
496 private SurfaceControl mOverlayLayer;
497
498 /**
499 * See {@link #mOverlayLayer}
500 */
501 private SurfaceControl mWindowingLayer;
502
503 /**
Adrian Roos5251b1d2018-03-23 18:57:43 +0100504 * Sequence number for the current layout pass.
505 */
506 int mLayoutSeq = 0;
507
Chavi Weingarten3a748552018-05-14 17:32:42 +0000508 /**
509 * Specifies the count to determine whether to defer updating the IME target until ready.
510 */
511 private int mDeferUpdateImeTargetCount;
512
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100513 /** Temporary float array to retrieve 3x3 matrix values. */
514 private final float[] mTmpFloats = new float[9];
515
Robert Carr24be9ab2018-04-30 17:54:53 -0700516 private MagnificationSpec mMagnificationSpec;
517
Arthur Hung95b38a92018-07-20 18:56:12 +0800518 private InputMonitor mInputMonitor;
519
Jorim Jaggif1292892018-09-10 11:58:13 +0200520 /** Caches the value whether told display manager that we have content. */
521 private boolean mLastHasContent;
522
Issei Suzuki43190bd2018-08-20 17:28:41 +0200523 private DisplayRotationUtil mRotationUtil = new DisplayRotationUtil();
524
lumark90120a82018-08-15 00:33:03 +0800525 /**
526 * The input method window for this display.
527 */
528 WindowState mInputMethodWindow;
529
lumarkff0ab692018-11-05 20:32:30 +0800530 /**
531 * This just indicates the window the input method is on top of, not
532 * necessarily the window its input is going to.
533 */
534 WindowState mInputMethodTarget;
535
536 /** If true hold off on modifying the animation layer of mInputMethodTarget */
537 boolean mInputMethodTargetWaitingAnim;
538
Arthur Hungbe5ce212018-09-13 18:41:56 +0800539 private final PointerEventDispatcher mPointerEventDispatcher;
540
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200541 private final InsetsStateController mInsetsStateController;
542
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800543 private SurfaceControl mParentSurfaceControl;
544 private InputWindowHandle mPortalWindowHandle;
545
Tiger Huang7c610aa2018-10-27 00:01:01 +0800546 // Last systemUiVisibility we received from status bar.
547 private int mLastStatusBarVisibility = 0;
548 // Last systemUiVisibility we dispatched to windows.
549 private int mLastDispatchedSystemUiVisibility = 0;
550
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800551 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
552 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800553 final AppWindowToken atoken = w.mAppToken;
554 if (winAnimator.mDrawState == READY_TO_SHOW) {
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +0200555 if (atoken == null || atoken.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800556 if (w.performShowLocked()) {
557 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
558 if (DEBUG_LAYOUT_REPEATS) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800559 mWmService.mWindowPlacerLocked.debugLayoutRepeats(
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800560 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
561 }
562 }
563 }
564 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800565 };
566
567 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
568 final WindowStateAnimator winAnimator = w.mWinAnimator;
569 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
570 return;
571 }
572
Jorim Jaggi8f520872018-08-14 17:00:20 +0200573 // If this window is animating, ensure the animation background is set.
574 final AnimationAdapter anim = w.mAppToken != null
575 ? w.mAppToken.getAnimation()
576 : w.getAnimation();
577 if (anim != null) {
578 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800579 if (color != 0) {
580 final TaskStack stack = w.getStack();
581 if (stack != null) {
582 stack.setAnimationBackground(winAnimator, color);
583 }
584 }
585 }
586 };
587
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800588 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
589 final int lostFocusUid = mTmpWindow.mOwnerUid;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800590 final Handler handler = mWmService.mH;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800591 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
592 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
593 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
594 w.mAttrs.hideTimeoutMilliseconds);
595 }
596 }
597 };
598
599 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800600 final AppWindowToken focusedApp = mFocusedApp;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800601 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
602 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
603
604 if (!w.canReceiveKeys()) {
605 return false;
606 }
607
608 final AppWindowToken wtoken = w.mAppToken;
609
610 // If this window's application has been removed, just skip it.
611 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
612 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
613 + (wtoken.removed ? "removed" : "sendingToBottom"));
614 return false;
615 }
616
617 if (focusedApp == null) {
618 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
619 + " using new focus @ " + w);
620 mTmpWindow = w;
621 return true;
622 }
623
624 if (!focusedApp.windowsAreFocusable()) {
625 // Current focused app windows aren't focusable...
626 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
627 + " focusable using new focus @ " + w);
628 mTmpWindow = w;
629 return true;
630 }
631
632 // Descend through all of the app tokens and find the first that either matches
633 // win.mAppToken (return win) or mFocusedApp (return null).
634 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
635 if (focusedApp.compareTo(wtoken) > 0) {
636 // App stack below focused app stack. No focus for you!!!
637 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
638 "findFocusedWindow: Reached focused app=" + focusedApp);
639 mTmpWindow = null;
640 return true;
641 }
642 }
643
644 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
645 mTmpWindow = w;
646 return true;
647 };
648
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800649 private final Consumer<WindowState> mPerformLayout = w -> {
650 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
651 // wasting time and funky changes while a window is animating away.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800652 final boolean gone = (mTmpWindow != null && mWmService.mPolicy.canBeHiddenByKeyguardLw(w))
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800653 || w.isGoneForLayoutLw();
654
655 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
656 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
657 + " mLayoutAttached=" + w.mLayoutAttached
658 + " screen changed=" + w.isConfigChanged());
659 final AppWindowToken atoken = w.mAppToken;
660 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200661 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800662 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
663 + " parentHidden=" + w.isParentWindowHidden());
664 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200665 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800666 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
667 + " parentHidden=" + w.isParentWindowHidden());
668 }
669
670 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
671 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
672 // since that means "perform layout as normal, just don't display").
673 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
674 || ((w.isConfigChanged() || w.setReportResizeHints())
675 && !w.isGoneForLayoutLw() &&
676 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
677 (w.mHasSurface && w.mAppToken != null &&
678 w.mAppToken.layoutConfigChanges)))) {
679 if (!w.mLayoutAttached) {
680 if (mTmpInitial) {
681 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700682 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800683 }
684 if (w.mAttrs.type == TYPE_DREAM) {
685 // Don't layout windows behind a dream, so that if it does stuff like hide
686 // the status bar we won't get a bad transition when it goes away.
687 mTmpWindow = w;
688 }
689 w.mLayoutNeeded = false;
690 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200691 final boolean firstLayout = !w.isLaidOut();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800692 getDisplayPolicy().layoutWindowLw(w, null, mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100693 w.mLayoutSeq = mLayoutSeq;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800694
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200695 // If this is the first layout, we need to initialize the last inset values as
696 // otherwise we'd immediately cause an unnecessary resize.
697 if (firstLayout) {
698 w.updateLastInsetValues();
699 }
700
Adrian Roos23df3a32018-03-15 15:41:13 +0100701 if (w.mAppToken != null) {
702 w.mAppToken.layoutLetterbox(w);
703 }
704
chaviw492139a2018-07-16 16:07:35 -0700705 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700706 + " mContainingFrame=" + w.getContainingFrame()
707 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800708 }
709 }
710 };
711
712 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
713 if (w.mLayoutAttached) {
714 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
715 + " mViewVisibility=" + w.mViewVisibility
716 + " mRelayoutCalled=" + w.mRelayoutCalled);
717 // If this view is GONE, then skip it -- keep the current frame, and let the caller
718 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
719 // windows, since that means "perform layout as normal, just don't display").
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800720 if (mTmpWindow != null && mWmService.mPolicy.canBeHiddenByKeyguardLw(w)) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800721 return;
722 }
723 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
724 || w.mLayoutNeeded) {
725 if (mTmpInitial) {
726 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700727 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800728 }
729 w.mLayoutNeeded = false;
730 w.prelayout();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800731 getDisplayPolicy().layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100732 w.mLayoutSeq = mLayoutSeq;
chaviw492139a2018-07-16 16:07:35 -0700733 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700734 + " mContainingFrame=" + w.getContainingFrame()
735 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800736 }
737 } else if (w.mAttrs.type == TYPE_DREAM) {
738 // Don't layout windows behind a dream, so that if it does stuff like hide the
739 // status bar we won't get a bad transition when it goes away.
740 mTmpWindow = mTmpWindow2;
741 }
742 };
743
744 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
745 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
746 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
747 return w.canBeImeTarget();
748 };
749
750 private final Consumer<WindowState> mApplyPostLayoutPolicy =
Tiger Huang7c610aa2018-10-27 00:01:01 +0800751 w -> getDisplayPolicy().applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
lumarkff0ab692018-11-05 20:32:30 +0800752 mInputMethodTarget);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800753
754 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800755 final WindowSurfacePlacer surfacePlacer = mWmService.mWindowPlacerLocked;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800756 final boolean obscuredChanged = w.mObscured !=
757 mTmpApplySurfaceChangesTransactionState.obscured;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800758 final RootWindowContainer root = mWmService.mRoot;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800759
760 // Update effect.
761 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
762 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
763 final boolean isDisplayed = w.isDisplayedLw();
764
765 if (isDisplayed && w.isObscuringDisplay()) {
766 // This window completely covers everything behind it, so we want to leave all
767 // of them as undimmed (for performance reasons).
768 root.mObscuringWindow = w;
769 mTmpApplySurfaceChangesTransactionState.obscured = true;
770 }
771
772 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
773 root.handleNotObscuredLocked(w,
774 mTmpApplySurfaceChangesTransactionState.obscured,
775 mTmpApplySurfaceChangesTransactionState.syswin);
776
777 if (w.mHasSurface && isDisplayed) {
778 final int type = w.mAttrs.type;
779 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
780 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
781 mTmpApplySurfaceChangesTransactionState.syswin = true;
782 }
783 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
784 && w.mAttrs.preferredRefreshRate != 0) {
785 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
786 = w.mAttrs.preferredRefreshRate;
787 }
788 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
789 && w.mAttrs.preferredDisplayModeId != 0) {
790 mTmpApplySurfaceChangesTransactionState.preferredModeId
791 = w.mAttrs.preferredDisplayModeId;
792 }
793 }
794 }
795
wilsonshihc32538e2018-11-07 17:27:34 +0800796 if (obscuredChanged && w.isVisibleLw() && mWallpaperController.isWallpaperTarget(w)) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800797 // This is the wallpaper target and its obscured state changed... make sure the
798 // current wallpaper's visibility has been updated accordingly.
799 mWallpaperController.updateWallpaperVisibility();
800 }
801
chaviw161ea3e2018-01-31 12:01:12 -0800802 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800803
804 final WindowStateAnimator winAnimator = w.mWinAnimator;
805
806 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
chaviw1454b392018-08-06 09:54:04 -0700807 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800808
809 // Moved from updateWindowsAndWallpaperLocked().
810 if (w.mHasSurface) {
811 // Take care of the window being ready to display.
812 final boolean committed = winAnimator.commitFinishDrawingLocked();
813 if (isDefaultDisplay && committed) {
814 if (w.mAttrs.type == TYPE_DREAM) {
815 // HACK: When a dream is shown, it may at that point hide the lock screen.
816 // So we need to redo the layout to let the phone window manager make this
817 // happen.
818 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
819 if (DEBUG_LAYOUT_REPEATS) {
820 surfacePlacer.debugLayoutRepeats(
821 "dream and commitFinishDrawingLocked true",
822 pendingLayoutChanges);
823 }
824 }
825 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
826 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
827 "First draw done in potential wallpaper target " + w);
wilsonshihc32538e2018-11-07 17:27:34 +0800828 mWallpaperMayChange = true;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800829 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
830 if (DEBUG_LAYOUT_REPEATS) {
831 surfacePlacer.debugLayoutRepeats(
832 "wallpaper and commitFinishDrawingLocked true",
833 pendingLayoutChanges);
834 }
835 }
836 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800837 }
838
839 final AppWindowToken atoken = w.mAppToken;
840 if (atoken != null) {
Adrian Roos23df3a32018-03-15 15:41:13 +0100841 atoken.updateLetterboxSurface(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800842 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
843 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
844 mTmpUpdateAllDrawn.add(atoken);
845 }
846 }
847
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800848 if (!mLosingFocus.isEmpty() && w.isFocused() && w.isDisplayedLw()) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800849 mWmService.mH.obtainMessage(REPORT_LOSING_FOCUS, this).sendToTarget();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800850 }
851
852 w.updateResizingWindowIfNeeded();
853 };
854
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800855 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800856 * Create new {@link DisplayContent} instance, add itself to the root window container and
857 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700858 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800859 * @param service You know.
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800860 * @param activityDisplay The ActivityDisplay for the display container.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700861 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700862 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800863 ActivityDisplay activityDisplay) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100864 super(service);
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800865 mAcitvityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800866 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
867 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
868 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
869 + " new=" + display);
870 }
871
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700872 mDisplay = display;
873 mDisplayId = display.getDisplayId();
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800874 mWallpaperController = new WallpaperController(mWmService, this);
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700875 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700876 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700877 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100878 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo,
879 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700880 initializeDisplayBaseInfo();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700881
lumark588a3e82018-07-20 18:53:54 +0800882 mAppTransition = new AppTransition(service.mContext, service, this);
883 mAppTransition.registerListenerLocked(service.mActivityManagerAppTransitionNotifier);
884 mAppTransitionController = new AppTransitionController(service, this);
885 mUnknownAppVisibilityController = new UnknownAppVisibilityController(service, this);
886
887 AnimationHandler animationHandler = new AnimationHandler();
888 mBoundsAnimationController = new BoundsAnimationController(service.mContext,
Winson Chung6ba767a2018-12-13 18:20:11 -0800889 mAppTransition, AnimationThread.getHandler(), animationHandler);
lumark588a3e82018-07-20 18:53:54 +0800890
Riddle Hsu2588ab02019-02-25 14:23:56 +0800891 final InputChannel inputChannel = mWmService.mInputManager.monitorInput(
892 "PointerEventDispatcher" + mDisplayId, mDisplayId);
893 mPointerEventDispatcher = new PointerEventDispatcher(inputChannel);
894
895 // Tap Listeners are supported for:
896 // 1. All physical displays (multi-display).
897 // 2. VirtualDisplays on VR, AA (and everything else).
898 mTapDetector = new TaskTapPointerEventListener(mWmService, this);
899 registerPointerEventListener(mTapDetector);
900 registerPointerEventListener(mWmService.mMousePositionTracker);
Winson Chungc5fe7ff2019-02-19 14:49:25 -0800901 if (mWmService.mAtmService.getRecentTasks() != null) {
902 registerPointerEventListener(
903 mWmService.mAtmService.getRecentTasks().getInputListener());
904 }
Riddle Hsu2588ab02019-02-25 14:23:56 +0800905
Tiger Huang7c610aa2018-10-27 00:01:01 +0800906 mDisplayPolicy = new DisplayPolicy(service, this);
907 mDisplayRotation = new DisplayRotation(service, this);
908 if (isDefaultDisplay) {
909 // The policy may be invoked right after here, so it requires the necessary default
910 // fields of this display content.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800911 mWmService.mPolicy.setDefaultDisplay(this);
Tiger Huang7c610aa2018-10-27 00:01:01 +0800912 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800913 if (mWmService.mDisplayReady) {
Tiger Huang7c610aa2018-10-27 00:01:01 +0800914 mDisplayPolicy.onConfigurationChanged();
915 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800916 if (mWmService.mSystemReady) {
Tiger Huang7c610aa2018-10-27 00:01:01 +0800917 mDisplayPolicy.systemReady();
918 }
919 mDividerControllerLocked = new DockedStackDividerController(service, this);
920 mPinnedStackControllerLocked = new PinnedStackController(service, this);
921
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000922 final SurfaceControl.Builder b = mWmService.makeSurfaceBuilder(mSession)
923 .setOpaque(true)
924 .setContainerLayer();
Robert Carrb1579c82017-09-05 14:54:47 -0700925 mWindowingLayer = b.setName("Display Root").build();
926 mOverlayLayer = b.setName("Display Overlays").build();
927
Robert Carrf59b8dd2017-10-02 18:58:36 -0700928 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700929 .setLayerStack(mWindowingLayer, mDisplayId)
930 .show(mWindowingLayer)
931 .setLayer(mOverlayLayer, 1)
932 .setLayerStack(mOverlayLayer, mDisplayId)
933 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700934 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700935
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700936 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700937 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700938 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700939 super.addChild(mAboveAppWindowsContainers, null);
940 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800941
942 // Add itself as a child to the root container.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800943 mWmService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700944
945 // TODO(b/62541591): evaluate whether this is the best spot to declare the
946 // {@link DisplayContent} ready for use.
947 mDisplayReady = true;
Arthur Hung95b38a92018-07-20 18:56:12 +0800948
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800949 mWmService.mAnimator.addDisplayLocked(mDisplayId);
Arthur Hung39134b22018-08-14 11:58:28 +0800950 mInputMonitor = new InputMonitor(service, mDisplayId);
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200951 mInsetsStateController = new InsetsStateController(this);
Bryce Leed1871262017-06-12 14:12:29 -0700952 }
953
954 boolean isReady() {
955 // The display is ready when the system and the individual display are both ready.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800956 return mWmService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700957 }
958
959 int getDisplayId() {
960 return mDisplayId;
961 }
962
Wale Ogunwale02319a62016-09-26 15:21:22 -0700963 WindowToken getWindowToken(IBinder binder) {
964 return mTokenMap.get(binder);
965 }
966
967 AppWindowToken getAppWindowToken(IBinder binder) {
968 final WindowToken token = getWindowToken(binder);
969 if (token == null) {
970 return null;
971 }
972 return token.asAppWindowToken();
973 }
974
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700975 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800976 final DisplayContent dc = mWmService.mRoot.getWindowTokenDisplay(token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700977 if (dc != null) {
978 // We currently don't support adding a window token to the display if the display
979 // already has the binder mapped to another token. If there is a use case for supporting
980 // this moving forward we will either need to merge the WindowTokens some how or have
981 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700982 throw new IllegalArgumentException("Can't map token=" + token + " to display="
983 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700984 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700985 if (binder == null) {
986 throw new IllegalArgumentException("Can't map token=" + token + " to display="
987 + getName() + " binder is null");
988 }
989 if (token == null) {
990 throw new IllegalArgumentException("Can't map null token to display="
991 + getName() + " binder=" + binder);
992 }
993
Wale Ogunwale02319a62016-09-26 15:21:22 -0700994 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700995
996 if (token.asAppWindowToken() == null) {
997 // Add non-app token to container hierarchy on the display. App tokens are added through
998 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700999 switch (token.windowType) {
1000 case TYPE_WALLPAPER:
1001 mBelowAppWindowsContainers.addChild(token);
1002 break;
1003 case TYPE_INPUT_METHOD:
1004 case TYPE_INPUT_METHOD_DIALOG:
1005 mImeWindowsContainers.addChild(token);
1006 break;
1007 default:
1008 mAboveAppWindowsContainers.addChild(token);
1009 break;
1010 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001011 }
Wale Ogunwale02319a62016-09-26 15:21:22 -07001012 }
1013
1014 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001015 final WindowToken token = mTokenMap.remove(binder);
1016 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -08001017 token.setExiting();
1018 }
1019 return token;
1020 }
1021
1022 /** Changes the display the input window token is housed on to this one. */
1023 void reParentWindowToken(WindowToken token) {
1024 final DisplayContent prevDc = token.getDisplayContent();
1025 if (prevDc == this) {
1026 return;
1027 }
Riddle Hsu85bd04b2018-11-17 00:34:36 +08001028 if (prevDc != null) {
1029 if (prevDc.mTokenMap.remove(token.token) != null && token.asAppWindowToken() == null) {
1030 // Removed the token from the map, but made sure it's not an app token before
1031 // removing from parent.
1032 token.getParent().removeChild(token);
1033 }
1034 if (prevDc.mLastFocus == mCurrentFocus) {
1035 // The window has become the focus of this display, so it should not be notified
1036 // that it lost focus from the previous display.
1037 prevDc.mLastFocus = null;
1038 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001039 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -08001040
1041 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -07001042 }
1043
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001044 void removeAppToken(IBinder binder) {
1045 final WindowToken token = removeWindowToken(binder);
1046 if (token == null) {
1047 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
1048 return;
1049 }
1050
1051 final AppWindowToken appToken = token.asAppWindowToken();
1052
1053 if (appToken == null) {
1054 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
1055 return;
1056 }
1057
1058 appToken.onRemovedFromDisplay();
1059 }
1060
Riddle Hsuad256a12018-07-18 16:11:30 +08001061 @Override
1062 public Display getDisplay() {
Craig Mautnerb47bbc32012-08-22 17:41:48 -07001063 return mDisplay;
1064 }
1065
Craig Mautner59c00972012-07-30 12:10:24 -07001066 DisplayInfo getDisplayInfo() {
1067 return mDisplayInfo;
1068 }
1069
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001070 DisplayMetrics getDisplayMetrics() {
1071 return mDisplayMetrics;
1072 }
1073
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001074 DisplayPolicy getDisplayPolicy() {
1075 return mDisplayPolicy;
1076 }
1077
Riddle Hsuad256a12018-07-18 16:11:30 +08001078 @Override
1079 public DisplayRotation getDisplayRotation() {
1080 return mDisplayRotation;
1081 }
1082
Jorim Jaggif96c90a2018-09-26 16:55:15 +02001083 /**
1084 * Marks a window as providing insets for the rest of the windows in the system.
1085 *
1086 * @param type The type of inset this window provides.
1087 * @param win The window.
1088 * @param frameProvider Function to compute the frame, or {@code null} if the just the frame of
1089 * the window should be taken.
1090 */
1091 void setInsetProvider(@InternalInsetType int type, WindowState win,
1092 @Nullable TriConsumer<DisplayFrames, WindowState, Rect> frameProvider) {
1093 mInsetsStateController.getSourceProvider(type).setWindow(win, frameProvider);
1094 }
1095
1096 InsetsStateController getInsetsStateController() {
1097 return mInsetsStateController;
1098 }
1099
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001100 @VisibleForTesting
1101 void setDisplayRotation(DisplayRotation displayRotation) {
1102 mDisplayRotation = displayRotation;
1103 }
1104
Andrii Kulian8ee72852017-03-10 10:36:45 -08001105 int getRotation() {
1106 return mRotation;
1107 }
1108
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001109 @VisibleForTesting
Andrii Kulian8ee72852017-03-10 10:36:45 -08001110 void setRotation(int newRotation) {
1111 mRotation = newRotation;
Riddle Hsuad256a12018-07-18 16:11:30 +08001112 mDisplayRotation.setRotation(newRotation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001113 }
1114
1115 int getLastOrientation() {
1116 return mLastOrientation;
1117 }
1118
Andrii Kulian8ee72852017-03-10 10:36:45 -08001119 int getLastWindowForcedOrientation() {
1120 return mLastWindowForcedOrientation;
1121 }
1122
Evan Rosky966759f2019-01-15 10:33:58 -08001123 void registerRemoteAnimations(RemoteAnimationDefinition definition) {
1124 mAppTransitionController.registerRemoteAnimations(definition);
1125 }
1126
Andrii Kulian06d07d62017-03-14 11:11:47 -07001127 /**
Riddle Hsu654a6f92018-07-13 22:59:36 +08001128 * Temporarily pauses rotation changes until resumed.
1129 *
1130 * This can be used to prevent rotation changes from occurring while the user is
1131 * performing certain operations, such as drag and drop.
1132 *
1133 * This call nests and must be matched by an equal number of calls to
1134 * {@link #resumeRotationLocked}.
1135 */
1136 void pauseRotationLocked() {
1137 mDeferredRotationPauseCount++;
1138 }
1139
1140 /**
1141 * Resumes normal rotation changes after being paused.
1142 */
1143 void resumeRotationLocked() {
1144 if (mDeferredRotationPauseCount <= 0) {
1145 return;
1146 }
1147
1148 mDeferredRotationPauseCount--;
1149 if (mDeferredRotationPauseCount == 0) {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001150 updateRotationAndSendNewConfigIfNeeded();
Riddle Hsu654a6f92018-07-13 22:59:36 +08001151 }
1152 }
1153
1154 /**
Riddle Hsuad256a12018-07-18 16:11:30 +08001155 * If this is true we have updated our desired orientation, but not yet changed the real
1156 * orientation our applied our screen rotation animation. For example, because a previous
1157 * screen rotation was in progress.
1158 *
1159 * @return {@code true} if the there is an ongoing rotation change.
1160 */
1161 boolean rotationNeedsUpdate() {
1162 final int lastOrientation = getLastOrientation();
1163 final int oldRotation = getRotation();
Riddle Hsuad256a12018-07-18 16:11:30 +08001164
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001165 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Garfield Tan4bb83472019-01-16 14:37:04 -08001166 return oldRotation != rotation;
Riddle Hsuad256a12018-07-18 16:11:30 +08001167 }
1168
Wale Ogunwale3a256e62018-12-06 14:41:18 -08001169 /**
1170 * The display content may have configuration set from {@link #DisplayWindowSettings}. This
1171 * callback let the owner of container know there is existing configuration to prevent the
1172 * values from being replaced by the initializing {@link #ActivityDisplay}.
1173 */
1174 void initializeDisplayOverrideConfiguration() {
1175 if (mAcitvityDisplay != null) {
1176 mAcitvityDisplay.onInitializeOverrideConfiguration(getRequestedOverrideConfiguration());
1177 }
1178 }
1179
Riddle Hsu4e611772018-10-31 18:58:28 +08001180 /** Notify the configuration change of this display. */
1181 void sendNewConfiguration() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001182 mWmService.mH.obtainMessage(SEND_NEW_CONFIGURATION, this).sendToTarget();
Riddle Hsu4e611772018-10-31 18:58:28 +08001183 }
1184
Garfield Tan90b04282018-12-11 14:04:42 -08001185 @Override
1186 boolean onDescendantOrientationChanged(IBinder freezeDisplayToken,
1187 ConfigurationContainer requestingContainer) {
1188 final Configuration config = updateOrientationFromAppTokens(
1189 getRequestedOverrideConfiguration(), freezeDisplayToken, false);
Garfield Tan49dae102019-02-04 09:51:59 -08001190 // If display rotation class tells us that it doesn't consider app requested orientation,
1191 // this display won't rotate just because of an app changes its requested orientation. Thus
1192 // it indicates that this display chooses not to handle this request.
1193 final boolean handled = getDisplayRotation().respectAppRequestedOrientation();
Garfield Tan90b04282018-12-11 14:04:42 -08001194 if (config == null) {
1195 return handled;
1196 }
1197
1198 if (handled && requestingContainer instanceof ActivityRecord) {
1199 final ActivityRecord activityRecord = (ActivityRecord) requestingContainer;
1200 final boolean kept = mWmService.mAtmService.updateDisplayOverrideConfigurationLocked(
1201 config, activityRecord, false /* deferResume */, getDisplayId());
1202 activityRecord.frozenBeforeDestroy = true;
1203 if (!kept) {
1204 mWmService.mAtmService.mRootActivityContainer.resumeFocusedStacksTopActivities();
1205 }
1206 } else {
1207 // We have a new configuration to push so we need to update ATMS for now.
1208 // TODO: Clean up display configuration push between ATMS and WMS after unification.
1209 mWmService.mAtmService.updateDisplayOverrideConfigurationLocked(
1210 config, null /* starting */, false /* deferResume */, getDisplayId());
1211 }
1212 return handled;
1213 }
1214
Garfield Tan49dae102019-02-04 09:51:59 -08001215 @Override
1216 boolean handlesOrientationChangeFromDescendant() {
1217 return getDisplayRotation().respectAppRequestedOrientation();
1218 }
1219
Riddle Hsu4e611772018-10-31 18:58:28 +08001220 /**
1221 * Determine the new desired orientation of this display.
1222 *
1223 * The orientation is computed from non-application windows first. If none of the
1224 * non-application windows specify orientation, the orientation is computed from application
1225 * tokens.
1226 *
1227 * @return {@code true} if the orientation is changed.
1228 */
1229 boolean updateOrientationFromAppTokens() {
1230 return updateOrientationFromAppTokens(false /* forceUpdate */);
1231 }
1232
Garfield Tan90b04282018-12-11 14:04:42 -08001233 /**
1234 * Update orientation of the target display, returning a non-null new Configuration if it has
1235 * changed from the current orientation. If a non-null configuration is returned, someone must
1236 * call {@link WindowManagerService#setNewDisplayOverrideConfiguration(Configuration,
1237 * DisplayContent)} to tell the window manager it can unfreeze the screen. This will typically
1238 * be done by calling {@link WindowManagerService#sendNewConfiguration(int)}.
1239 */
1240 Configuration updateOrientationFromAppTokens(Configuration currentConfig,
1241 IBinder freezeDisplayToken, boolean forceUpdate) {
1242 if (!mDisplayReady) {
1243 return null;
1244 }
1245
1246 Configuration config = null;
1247 if (updateOrientationFromAppTokens(forceUpdate)) {
1248 // If we changed the orientation but mOrientationChangeComplete is already true,
1249 // we used seamless rotation, and we don't need to freeze the screen.
1250 if (freezeDisplayToken != null && !mWmService.mRoot.mOrientationChangeComplete) {
1251 final AppWindowToken atoken = getAppWindowToken(freezeDisplayToken);
1252 if (atoken != null) {
1253 atoken.startFreezingScreen();
1254 }
1255 }
1256 config = new Configuration();
1257 computeScreenConfiguration(config);
1258 } else if (currentConfig != null) {
1259 // No obvious action we need to take, but if our current state mismatches the
1260 // activity manager's, update it, disregarding font scale, which should remain set
1261 // to the value of the previous configuration.
1262 // Here we're calling Configuration#unset() instead of setToDefaults() because we
1263 // need to keep override configs clear of non-empty values (e.g. fontSize).
1264 mTmpConfiguration.unset();
1265 mTmpConfiguration.updateFrom(currentConfig);
1266 computeScreenConfiguration(mTmpConfiguration);
1267 if (currentConfig.diff(mTmpConfiguration) != 0) {
1268 mWaitingForConfig = true;
1269 setLayoutNeeded();
1270 int[] anim = new int[2];
1271 getDisplayPolicy().selectRotationAnimationLw(anim);
1272
1273 mWmService.startFreezingDisplayLocked(anim[0], anim[1], this);
1274 config = new Configuration(mTmpConfiguration);
1275 }
1276 }
1277
1278 return config;
1279 }
1280
1281
1282 private boolean updateOrientationFromAppTokens(boolean forceUpdate) {
Riddle Hsu4e611772018-10-31 18:58:28 +08001283 final int req = getOrientation();
1284 if (req != mLastOrientation || forceUpdate) {
1285 mLastOrientation = req;
1286 mDisplayRotation.setCurrentOrientation(req);
1287 return updateRotationUnchecked(forceUpdate);
1288 }
1289 return false;
1290 }
1291
Riddle Hsuad256a12018-07-18 16:11:30 +08001292 /**
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001293 * Update rotation of the display and send configuration if the rotation is changed.
1294 *
1295 * @return {@code true} if the rotation has been changed and the new config is sent.
1296 */
1297 boolean updateRotationAndSendNewConfigIfNeeded() {
1298 final boolean changed = updateRotationUnchecked(false /* forceUpdate */);
1299 if (changed) {
Riddle Hsu4e611772018-10-31 18:58:28 +08001300 sendNewConfiguration();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001301 }
1302 return changed;
1303 }
1304
1305 /**
Andrii Kulian06d07d62017-03-14 11:11:47 -07001306 * Update rotation of the display.
1307 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001308 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1309 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001310 */
Robert Carrae606b42018-02-15 15:36:23 -08001311 boolean updateRotationUnchecked() {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001312 return updateRotationUnchecked(false /* forceUpdate */);
1313 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001314
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001315 /**
Evan Roskye747c3e2018-10-30 20:06:41 -07001316 * Update rotation of the DisplayContent with an option to force the update. This updates
1317 * the container's perception of rotation and, depending on the top activities, will freeze
1318 * the screen or start seamless rotation. The display itself gets rotated in
1319 * {@link #applyRotationLocked} during {@link WindowManagerService#sendNewConfiguration}.
1320 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001321 * @param forceUpdate Force the rotation update. Sometimes in WM we might skip updating
1322 * orientation because we're waiting for some rotation to finish or display
1323 * to unfreeze, which results in configuration of the previously visible
1324 * activity being applied to a newly visible one. Forcing the rotation
1325 * update allows to workaround this issue.
1326 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
Evan Roskye747c3e2018-10-30 20:06:41 -07001327 * {@link WindowManagerService#sendNewConfiguration(int)} TO COMPLETE THE ROTATION AND
1328 * UNFREEZE THE SCREEN.
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001329 */
1330 boolean updateRotationUnchecked(boolean forceUpdate) {
1331 ScreenRotationAnimation screenRotationAnimation;
1332 if (!forceUpdate) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001333 if (mDeferredRotationPauseCount > 0) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001334 // Rotation updates have been paused temporarily. Defer the update until
1335 // updates have been resumed.
1336 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
1337 return false;
1338 }
1339
1340 screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001341 mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001342 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
1343 // Rotation updates cannot be performed while the previous rotation change
1344 // animation is still in progress. Skip this update. We will try updating
1345 // again after the animation is finished and the display is unfrozen.
1346 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
1347 return false;
1348 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001349 if (mWmService.mDisplayFrozen) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001350 // Even if the screen rotation animation has finished (e.g. isAnimating
1351 // returns false), there is still some time where we haven't yet unfrozen
1352 // the display. We also need to abort rotation here.
1353 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
1354 "Deferring rotation, still finishing previous rotation");
1355 return false;
1356 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001357 }
1358
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001359 if (!mWmService.mDisplayEnabled) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001360 // No point choosing a rotation if the display is not enabled.
1361 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
1362 return false;
1363 }
1364
1365 final int oldRotation = mRotation;
1366 final int lastOrientation = mLastOrientation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001367 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001368 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Computed rotation=" + rotation + " for display id="
1369 + mDisplayId + " based on lastOrientation=" + lastOrientation
1370 + " and oldRotation=" + oldRotation);
Tiger Huang7c610aa2018-10-27 00:01:01 +08001371 boolean mayRotateSeamlessly = mDisplayPolicy.shouldRotateSeamlessly(mDisplayRotation,
Riddle Hsuad256a12018-07-18 16:11:30 +08001372 oldRotation, rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001373
Robert Carr0e007272017-10-02 13:00:55 -07001374 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001375 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001376 if (seamlessRotated != null && !forceUpdate) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001377 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1378 // to complete (that is, waiting for windows to redraw). It's tempting to check
1379 // w.mSeamlessRotationCount but that could be incorrect in the case of
1380 // window-removal.
1381 return false;
1382 }
Robert Carr0e007272017-10-02 13:00:55 -07001383
1384 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001385 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001386 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001387 mayRotateSeamlessly = false;
1388 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001389 for (int i = 0; i < mWmService.mSessions.size(); i++) {
1390 if (mWmService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
Robert Carr0e007272017-10-02 13:00:55 -07001391 mayRotateSeamlessly = false;
1392 break;
1393 }
1394 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001395 }
Robert Carr0e007272017-10-02 13:00:55 -07001396 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001397
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001398 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1399 + " selected orientation " + lastOrientation
Garfield Tan49dae102019-02-04 09:51:59 -08001400 + ", got rotation " + rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001401
Garfield Tan4bb83472019-01-16 14:37:04 -08001402 if (oldRotation == rotation) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001403 // No change.
1404 return false;
1405 }
1406
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001407 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1408 + " rotation changed to " + rotation
Garfield Tan4bb83472019-01-16 14:37:04 -08001409 + " from " + oldRotation
1410 + ", lastOrientation=" + lastOrientation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001411
1412 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
Vishnu Nairba183352018-11-21 11:16:49 -08001413 mWaitingForConfig = true;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001414 }
1415
Evan Roskye747c3e2018-10-30 20:06:41 -07001416 mRotation = rotation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001417
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001418 mWmService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1419 mWmService.mH.sendNewMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
Riddle Hsu654a6f92018-07-13 22:59:36 +08001420 this, WINDOW_FREEZE_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001421
1422 setLayoutNeeded();
1423 final int[] anim = new int[2];
Tiger Huang7c610aa2018-10-27 00:01:01 +08001424 mDisplayPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001425
1426 if (!rotateSeamlessly) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001427 mWmService.startFreezingDisplayLocked(anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001428 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001429 } else {
1430 // The screen rotation animation uses a screenshot to freeze the screen
1431 // while windows resize underneath.
1432 // When we are rotating seamlessly, we allow the elements to transition
1433 // to their rotated state independently and without a freeze required.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001434 mWmService.startSeamlessRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001435 }
1436
Evan Roskye747c3e2018-10-30 20:06:41 -07001437 return true;
1438 }
1439
1440 /**
1441 * Applies the rotation transaction. This must be called after {@link #updateRotationUnchecked}
1442 * (if it returned {@code true}) to actually finish the rotation.
1443 *
1444 * @param oldRotation the rotation we are coming from.
1445 * @param rotation the rotation to apply.
1446 */
1447 void applyRotationLocked(final int oldRotation, final int rotation) {
1448 mDisplayRotation.setRotation(rotation);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001449 final boolean rotateSeamlessly = mWmService.isRotatingSeamlessly();
Evan Roskye747c3e2018-10-30 20:06:41 -07001450 ScreenRotationAnimation screenRotationAnimation = rotateSeamlessly
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001451 ? null : mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001452 // We need to update our screen size information to match the new rotation. If the rotation
1453 // has actually changed then this method will return true and, according to the comment at
1454 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1455 // By updating the Display info here it will be available to
1456 // #computeScreenConfiguration() later.
1457 updateDisplayAndOrientation(getConfiguration().uiMode);
1458
Robert Carrae606b42018-02-15 15:36:23 -08001459 // NOTE: We disable the rotation in the emulator because
1460 // it doesn't support hardware OpenGL emulation yet.
1461 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1462 && screenRotationAnimation.hasScreenshot()) {
1463 if (screenRotationAnimation.setRotation(getPendingTransaction(), rotation,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001464 MAX_ANIMATION_DURATION, mWmService.getTransitionAnimationScaleLocked(),
Robert Carrae606b42018-02-15 15:36:23 -08001465 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001466 mWmService.scheduleAnimationLocked();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001467 }
1468 }
1469
Vishnu Nair83537a72018-07-19 21:27:48 -07001470 forAllWindows(w -> {
Vishnu Nairddd80742018-08-21 14:12:46 -07001471 w.seamlesslyRotateIfAllowed(getPendingTransaction(), oldRotation, rotation,
1472 rotateSeamlessly);
Vishnu Nair83537a72018-07-19 21:27:48 -07001473 }, true /* traverseTopToBottom */);
1474
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001475 mWmService.mDisplayManagerInternal.performTraversal(getPendingTransaction());
Robert Carrae606b42018-02-15 15:36:23 -08001476 scheduleAnimation();
1477
Andrii Kulian06d07d62017-03-14 11:11:47 -07001478 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001479 if (w.mHasSurface && !rotateSeamlessly) {
1480 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001481 w.setOrientationChanging(true);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001482 mWmService.mRoot.mOrientationChangeComplete = false;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001483 w.mLastFreezeDuration = 0;
1484 }
1485 w.mReportOrientationChanged = true;
1486 }, true /* traverseTopToBottom */);
1487
1488 if (rotateSeamlessly) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001489 mWmService.mH.sendNewMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
Riddle Hsu654a6f92018-07-13 22:59:36 +08001490 this, SEAMLESS_ROTATION_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001491 }
1492
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001493 for (int i = mWmService.mRotationWatchers.size() - 1; i >= 0; i--) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001494 final WindowManagerService.RotationWatcher rotationWatcher
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001495 = mWmService.mRotationWatchers.get(i);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001496 if (rotationWatcher.mDisplayId == mDisplayId) {
1497 try {
1498 rotationWatcher.mWatcher.onRotationChanged(rotation);
1499 } catch (RemoteException e) {
1500 // Ignore
1501 }
1502 }
1503 }
1504
Andrii Kulian06d07d62017-03-14 11:11:47 -07001505 // Announce rotation only if we will not animate as we already have the
1506 // windows in final state. Otherwise, we make this call at the rotation end.
Rhed Jao02655dc2018-10-30 20:44:52 +08001507 if (screenRotationAnimation == null && mWmService.mAccessibilityController != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001508 mWmService.mAccessibilityController.onRotationChangedLocked(this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001509 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001510 }
1511
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001512 void configureDisplayPolicy() {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001513 final int width = mBaseDisplayWidth;
1514 final int height = mBaseDisplayHeight;
1515 final int shortSize;
1516 final int longSize;
1517 if (width > height) {
1518 shortSize = height;
1519 longSize = width;
1520 } else {
1521 shortSize = width;
1522 longSize = height;
1523 }
1524
1525 final int shortSizeDp = shortSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1526 final int longSizeDp = longSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1527
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001528 mDisplayPolicy.configure(width, height, shortSizeDp);
Tiger Huang3d2b8982019-01-29 22:56:48 +08001529 mDisplayRotation.configure(width, height, shortSizeDp, longSizeDp);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001530
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001531 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1532 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001533 }
1534
Andrii Kulian06d07d62017-03-14 11:11:47 -07001535 /**
1536 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1537 * changed.
1538 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1539 */
1540 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1541 // Use the effective "visual" dimensions based on current rotation
1542 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
Garfield Tan4bb83472019-01-16 14:37:04 -08001543 final int dw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1544 final int dh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001545
1546 // Update application display metrics.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001547 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(mRotation);
1548 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1549
Tiger Huang7c610aa2018-10-27 00:01:01 +08001550 final int appWidth = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1551 displayCutout);
1552 final int appHeight = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1553 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001554 mDisplayInfo.rotation = mRotation;
1555 mDisplayInfo.logicalWidth = dw;
1556 mDisplayInfo.logicalHeight = dh;
1557 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1558 mDisplayInfo.appWidth = appWidth;
1559 mDisplayInfo.appHeight = appHeight;
1560 if (isDefaultDisplay) {
1561 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1562 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1563 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001564 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001565 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1566 if (mDisplayScalingDisabled) {
1567 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1568 } else {
1569 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1570 }
1571
Andrii Kulianf0379de2018-03-14 16:24:07 -07001572 // We usually set the override info in DisplayManager so that we get consistent display
1573 // metrics values when displays are changing and don't send out new values until WM is aware
1574 // of them. However, we don't do this for displays that serve as containers for ActivityView
1575 // because we don't want letter-/pillar-boxing during resize.
1576 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1577 ? mDisplayInfo : null;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001578 mWmService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001579 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001580
1581 mBaseDisplayRect.set(0, 0, dw, dh);
1582
1583 if (isDefaultDisplay) {
1584 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1585 mCompatDisplayMetrics);
1586 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001587
Andrii Kulian06d07d62017-03-14 11:11:47 -07001588 return mDisplayInfo;
1589 }
1590
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001591 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001592 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1593 }
1594
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001595 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001596 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001597 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001598 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001599 }
Adrian Roos11c25582018-02-19 18:06:36 +01001600 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001601 return WmDisplayCutout.computeSafeInsets(
1602 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001603 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001604 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Issei Suzuki43190bd2018-08-20 17:28:41 +02001605 final Rect[] newBounds = mRotationUtil.getRotatedBounds(
1606 WmDisplayCutout.computeSafeInsets(
Jorim Jaggi60640512018-06-29 01:14:31 +02001607 cutout, mInitialDisplayWidth, mInitialDisplayHeight)
Issei Suzuki43190bd2018-08-20 17:28:41 +02001608 .getDisplayCutout().getBoundingRectsAll(),
1609 rotation, mInitialDisplayWidth, mInitialDisplayHeight);
1610 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(newBounds),
Adrian Roos24264212018-02-19 16:26:15 +01001611 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1612 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001613 }
1614
Andrii Kulian06d07d62017-03-14 11:11:47 -07001615 /**
1616 * Compute display configuration based on display properties and policy settings.
1617 * Do not call if mDisplayReady == false.
1618 */
1619 void computeScreenConfiguration(Configuration config) {
1620 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
Evan Roskye747c3e2018-10-30 20:06:41 -07001621 calculateBounds(displayInfo, mTmpBounds);
1622 config.windowConfiguration.setBounds(mTmpBounds);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001623
1624 final int dw = displayInfo.logicalWidth;
1625 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001626 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Garfield Tane0846042018-07-26 13:42:04 -07001627 config.windowConfiguration.setWindowingMode(getWindowingMode());
Yunfan Chen7daa6ac2018-11-29 18:16:44 -08001628 config.windowConfiguration.setDisplayWindowingMode(getWindowingMode());
Evan Roskye747c3e2018-10-30 20:06:41 -07001629 config.windowConfiguration.setRotation(displayInfo.rotation);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001630
Adrian Roos11c25582018-02-19 18:06:36 +01001631 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001632 config.screenWidthDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001633 (int)(mDisplayPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1634 config.uiMode, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001635 config.screenHeightDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001636 (int)(mDisplayPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1637 config.uiMode, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001638
Tiger Huang7c610aa2018-10-27 00:01:01 +08001639 mDisplayPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001640 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001641 final int leftInset = mTmpRect.left;
1642 final int topInset = mTmpRect.top;
1643 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001644 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1645 leftInset + displayInfo.appWidth /* right */,
1646 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001647 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1648 || displayInfo.rotation == Surface.ROTATION_270);
1649
Tiger Huang7c610aa2018-10-27 00:01:01 +08001650 computeSizeRangesAndScreenLayout(displayInfo, rotated, config.uiMode, dw, dh, density,
1651 config);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001652
1653 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1654 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1655 ? Configuration.SCREENLAYOUT_ROUND_YES
1656 : Configuration.SCREENLAYOUT_ROUND_NO);
1657
1658 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1659 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1660 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001661 dh, displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001662 config.densityDpi = displayInfo.logicalDensityDpi;
1663
1664 config.colorMode =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001665 ((displayInfo.isHdr() && mWmService.hasHdrSupport())
Andrii Kulian06d07d62017-03-14 11:11:47 -07001666 ? Configuration.COLOR_MODE_HDR_YES
1667 : Configuration.COLOR_MODE_HDR_NO)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001668 | (displayInfo.isWideColorGamut() && mWmService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001669 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1670 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1671
1672 // Update the configuration based on available input devices, lid switch,
1673 // and platform configuration.
1674 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1675 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1676 config.navigation = Configuration.NAVIGATION_NONAV;
1677
1678 int keyboardPresence = 0;
1679 int navigationPresence = 0;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001680 final InputDevice[] devices = mWmService.mInputManager.getInputDevices();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001681 final int len = devices != null ? devices.length : 0;
1682 for (int i = 0; i < len; i++) {
1683 InputDevice device = devices[i];
Arthur Hung82bbfc32018-11-29 20:24:51 +08001684 // Ignore virtual input device.
1685 if (device.isVirtual()) {
1686 continue;
1687 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001688
Arthur Hung82bbfc32018-11-29 20:24:51 +08001689 // Check if input device can dispatch events to current display.
1690 // If display type is virtual, will follow the default display.
1691 if (!mWmService.mInputManager.canDispatchToDisplay(device.getId(),
1692 displayInfo.type == Display.TYPE_VIRTUAL ? DEFAULT_DISPLAY : mDisplayId)) {
1693 continue;
1694 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001695
Arthur Hung82bbfc32018-11-29 20:24:51 +08001696 final int sources = device.getSources();
1697 final int presenceFlag = device.isExternal()
1698 ? WindowManagerPolicy.PRESENCE_EXTERNAL : WindowManagerPolicy.PRESENCE_INTERNAL;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001699
Arthur Hung82bbfc32018-11-29 20:24:51 +08001700 if (mWmService.mIsTouchDevice) {
1701 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) == InputDevice.SOURCE_TOUCHSCREEN) {
1702 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001703 }
Arthur Hung82bbfc32018-11-29 20:24:51 +08001704 } else {
1705 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1706 }
1707
1708 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1709 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1710 navigationPresence |= presenceFlag;
1711 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1712 && config.navigation == Configuration.NAVIGATION_NONAV) {
1713 config.navigation = Configuration.NAVIGATION_DPAD;
1714 navigationPresence |= presenceFlag;
1715 }
1716
1717 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1718 config.keyboard = Configuration.KEYBOARD_QWERTY;
1719 keyboardPresence |= presenceFlag;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001720 }
1721 }
1722
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001723 if (config.navigation == Configuration.NAVIGATION_NONAV && mWmService.mHasPermanentDpad) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001724 config.navigation = Configuration.NAVIGATION_DPAD;
1725 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1726 }
1727
1728 // Determine whether a hard keyboard is available and enabled.
1729 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1730 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001731 if (hardKeyboardAvailable != mWmService.mHardKeyboardAvailable) {
1732 mWmService.mHardKeyboardAvailable = hardKeyboardAvailable;
1733 mWmService.mH.removeMessages(REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1734 mWmService.mH.sendEmptyMessage(REPORT_HARD_KEYBOARD_STATUS_CHANGE);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001735 }
1736
Tiger Huang7c610aa2018-10-27 00:01:01 +08001737 mDisplayPolicy.updateConfigurationDependentBehaviors();
1738
Andrii Kulian06d07d62017-03-14 11:11:47 -07001739 // Let the policy update hidden states.
1740 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1741 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1742 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001743 mWmService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001744 }
1745
1746 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001747 DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001748 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1749 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1750 final int unrotDw, unrotDh;
1751 if (rotated) {
1752 unrotDw = dh;
1753 unrotDh = dw;
1754 } else {
1755 unrotDw = dw;
1756 unrotDh = dh;
1757 }
1758 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001759 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001760 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001761 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001762 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001763 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001764 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001765 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001766 return sw;
1767 }
1768
1769 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001770 DisplayMetrics dm, int dw, int dh, DisplayCutout displayCutout) {
1771 dm.noncompatWidthPixels = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1772 displayCutout);
1773 dm.noncompatHeightPixels = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode,
1774 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001775 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1776 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1777 if (curSize == 0 || size < curSize) {
1778 curSize = size;
1779 }
1780 return curSize;
1781 }
1782
Tiger Huang7c610aa2018-10-27 00:01:01 +08001783 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, boolean rotated,
1784 int uiMode, int dw, int dh, float density, Configuration outConfig) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001785
1786 // We need to determine the smallest width that will occur under normal
1787 // operation. To this, start with the base screen size and compute the
1788 // width under the different possible rotations. We need to un-rotate
1789 // the current screen dimensions before doing this.
1790 int unrotDw, unrotDh;
1791 if (rotated) {
1792 unrotDw = dh;
1793 unrotDh = dw;
1794 } else {
1795 unrotDw = dw;
1796 unrotDh = dh;
1797 }
1798 displayInfo.smallestNominalAppWidth = 1<<30;
1799 displayInfo.smallestNominalAppHeight = 1<<30;
1800 displayInfo.largestNominalAppWidth = 0;
1801 displayInfo.largestNominalAppHeight = 0;
Tiger Huang7c610aa2018-10-27 00:01:01 +08001802 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_0, uiMode, unrotDw, unrotDh);
1803 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_90, uiMode, unrotDh, unrotDw);
1804 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_180, uiMode, unrotDw, unrotDh);
1805 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_270, uiMode, unrotDh, unrotDw);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001806 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1807 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001808 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001809 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001810 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001811 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001812 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001813 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001814 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001815 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1816 outConfig.screenLayout = sl;
1817 }
1818
1819 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001820 int uiMode, DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001821 // Get the app screen size at this rotation.
Tiger Huang7c610aa2018-10-27 00:01:01 +08001822 int w = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayCutout);
1823 int h = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001824
1825 // Compute the screen layout size class for this rotation.
1826 int longSize = w;
1827 int shortSize = h;
1828 if (longSize < shortSize) {
1829 int tmp = longSize;
1830 longSize = shortSize;
1831 shortSize = tmp;
1832 }
1833 longSize = (int)(longSize/density);
1834 shortSize = (int)(shortSize/density);
1835 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1836 }
1837
Tiger Huang7c610aa2018-10-27 00:01:01 +08001838 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001839 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001840 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1841 rotation).getDisplayCutout();
Tiger Huang7c610aa2018-10-27 00:01:01 +08001842 final int width = mDisplayPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1843 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001844 if (width < displayInfo.smallestNominalAppWidth) {
1845 displayInfo.smallestNominalAppWidth = width;
1846 }
1847 if (width > displayInfo.largestNominalAppWidth) {
1848 displayInfo.largestNominalAppWidth = width;
1849 }
Tiger Huang7c610aa2018-10-27 00:01:01 +08001850 final int height = mDisplayPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1851 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001852 if (height < displayInfo.smallestNominalAppHeight) {
1853 displayInfo.smallestNominalAppHeight = height;
1854 }
1855 if (height > displayInfo.largestNominalAppHeight) {
1856 displayInfo.largestNominalAppHeight = height;
1857 }
1858 }
1859
Riddle Hsua4d6fa22018-08-11 00:50:39 +08001860 /**
1861 * Apps that use the compact menu panel (as controlled by the panelMenuIsCompact
1862 * theme attribute) on devices that feature a physical options menu key attempt to position
1863 * their menu panel window along the edge of the screen nearest the physical menu key.
1864 * This lowers the travel distance between invoking the menu panel and selecting
1865 * a menu option.
1866 *
1867 * This method helps control where that menu is placed. Its current implementation makes
1868 * assumptions about the menu key and its relationship to the screen based on whether
1869 * the device's natural orientation is portrait (width < height) or landscape.
1870 *
1871 * The menu key is assumed to be located along the bottom edge of natural-portrait
1872 * devices and along the right edge of natural-landscape devices. If these assumptions
1873 * do not hold for the target device, this method should be changed to reflect that.
1874 *
1875 * @return A {@link Gravity} value for placing the options menu window.
1876 */
1877 int getPreferredOptionsPanelGravity() {
1878 final int rotation = getRotation();
1879 if (mInitialDisplayWidth < mInitialDisplayHeight) {
1880 // On devices with a natural orientation of portrait.
1881 switch (rotation) {
1882 default:
1883 case Surface.ROTATION_0:
1884 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1885 case Surface.ROTATION_90:
1886 return Gravity.RIGHT | Gravity.BOTTOM;
1887 case Surface.ROTATION_180:
1888 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1889 case Surface.ROTATION_270:
1890 return Gravity.START | Gravity.BOTTOM;
1891 }
1892 }
1893
1894 // On devices with a natural orientation of landscape.
1895 switch (rotation) {
1896 default:
1897 case Surface.ROTATION_0:
1898 return Gravity.RIGHT | Gravity.BOTTOM;
1899 case Surface.ROTATION_90:
1900 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1901 case Surface.ROTATION_180:
1902 return Gravity.START | Gravity.BOTTOM;
1903 case Surface.ROTATION_270:
1904 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1905 }
1906 }
1907
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001908 DockedStackDividerController getDockedDividerController() {
1909 return mDividerControllerLocked;
1910 }
1911
Winson Chung655332c2016-10-31 13:14:28 -07001912 PinnedStackController getPinnedStackController() {
1913 return mPinnedStackControllerLocked;
1914 }
1915
Jeff Browna506a6e2013-06-04 00:02:38 -07001916 /**
1917 * Returns true if the specified UID has access to this display.
1918 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001919 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001920 return mDisplay.hasAccess(uid);
1921 }
1922
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001923 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001924 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001925 }
1926
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001927 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001928 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001929 }
1930
Wale Ogunwale61911492017-10-11 08:50:50 -07001931 /**
1932 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1933 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001934 TaskStack getSplitScreenPrimaryStack() {
1935 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001936 return (stack != null && stack.isVisible()) ? stack : null;
1937 }
1938
Robert Carr9785cf32018-04-25 15:06:07 -07001939 boolean hasSplitScreenPrimaryStack() {
1940 return getSplitScreenPrimaryStack() != null;
1941 }
1942
Wale Ogunwale61911492017-10-11 08:50:50 -07001943 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001944 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001945 * not visible.
1946 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001947 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1948 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001949 }
1950
1951 TaskStack getPinnedStack() {
1952 return mTaskStackContainers.getPinnedStack();
1953 }
1954
1955 private boolean hasPinnedStack() {
1956 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001957 }
1958
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001959 /**
1960 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1961 * Null is no compatible stack on the display.
1962 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001963 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001964 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1965 }
1966
1967 /**
1968 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1969 * activity type. Null is no compatible stack on the display.
1970 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001971 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001972 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001973 }
1974
Bryce Lee48f4b572017-04-10 10:54:15 -07001975 @VisibleForTesting
Kazuki Takise148d00a2018-05-31 15:32:19 +09001976 WindowList<TaskStack> getStacks() {
1977 return mTaskStackContainers.mChildren;
1978 }
1979
1980 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001981 TaskStack getTopStack() {
1982 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001983 }
1984
Winson Chunge2d72172018-01-25 17:46:20 +00001985 ArrayList<Task> getVisibleTasks() {
1986 return mTaskStackContainers.getVisibleTasks();
1987 }
1988
Wale Ogunwale61911492017-10-11 08:50:50 -07001989 void onStackWindowingModeChanged(TaskStack stack) {
1990 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001991 }
1992
Andrii Kulian441e4492016-09-29 15:25:00 -07001993 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001994 public void onConfigurationChanged(Configuration newParentConfig) {
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05001995 final int lastOrientation = getConfiguration().orientation;
Andrii Kulian441e4492016-09-29 15:25:00 -07001996 super.onConfigurationChanged(newParentConfig);
Tiger Huang7c610aa2018-10-27 00:01:01 +08001997 if (mDisplayPolicy != null) {
1998 mDisplayPolicy.onConfigurationChanged();
1999 }
Andrii Kulian441e4492016-09-29 15:25:00 -07002000
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05002001 if (lastOrientation != getConfiguration().orientation) {
2002 getMetricsLogger().write(
2003 new LogMaker(MetricsEvent.ACTION_PHONE_ORIENTATION_CHANGED)
2004 .setSubtype(getConfiguration().orientation)
2005 .addTaggedData(MetricsEvent.FIELD_DISPLAY_ID, getDisplayId()));
2006 }
2007
Evan Roskye747c3e2018-10-30 20:06:41 -07002008 // If there was no pinned stack, we still need to notify the controller of the display info
2009 // update as a result of the config change.
2010 if (mPinnedStackControllerLocked != null && !hasPinnedStack()) {
2011 mPinnedStackControllerLocked.onDisplayInfoChanged(getDisplayInfo());
2012 }
Evan Roskye747c3e2018-10-30 20:06:41 -07002013 }
2014
2015 /**
2016 * Updates the resources used by docked/pinned controllers. This needs to be called at the
2017 * beginning of a configuration update cascade since the metrics from these resources are used
2018 * for bounds calculations. Since ActivityDisplay initiates the configuration update, this
2019 * should be called from there instead of DisplayContent's onConfigurationChanged.
2020 */
2021 void preOnConfigurationChanged() {
Bryce Leef3c6a472017-11-14 14:53:06 -08002022 final DockedStackDividerController dividerController = getDockedDividerController();
2023
2024 if (dividerController != null) {
2025 getDockedDividerController().onConfigurationChanged();
2026 }
2027
2028 final PinnedStackController pinnedStackController = getPinnedStackController();
2029
2030 if (pinnedStackController != null) {
2031 getPinnedStackController().onConfigurationChanged();
2032 }
Andrii Kulian441e4492016-09-29 15:25:00 -07002033 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07002034
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07002035 @Override
2036 boolean fillsParent() {
2037 return true;
2038 }
2039
2040 @Override
2041 boolean isVisible() {
2042 return true;
2043 }
2044
2045 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002046 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002047 super.onAppTransitionDone();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002048 mWmService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002049 }
2050
Yunfan Chen7daa6ac2018-11-29 18:16:44 -08002051 @Override
2052 public void setWindowingMode(int windowingMode) {
2053 super.setWindowingMode(windowingMode);
2054 super.setDisplayWindowingMode(windowingMode);
2055 }
2056
2057 @Override
2058 void setDisplayWindowingMode(int windowingMode) {
2059 setWindowingMode(windowingMode);
2060 }
2061
Robert Carr9785cf32018-04-25 15:06:07 -07002062 /**
2063 * In split-screen mode we process the IME containers above the docked divider
2064 * rather than directly above their target.
2065 */
2066 private boolean skipTraverseChild(WindowContainer child) {
lumarkff0ab692018-11-05 20:32:30 +08002067 if (child == mImeWindowsContainers && mInputMethodTarget != null
Robert Carr9785cf32018-04-25 15:06:07 -07002068 && !hasSplitScreenPrimaryStack()) {
2069 return true;
2070 }
2071 return false;
2072 }
2073
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07002074 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002075 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
2076 // Special handling so we can process IME windows with #forAllImeWindows above their IME
2077 // target, or here in order if there isn't an IME target.
2078 if (traverseTopToBottom) {
2079 for (int i = mChildren.size() - 1; i >= 0; --i) {
2080 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07002081 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002082 continue;
2083 }
Robert Carr9785cf32018-04-25 15:06:07 -07002084
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002085 if (child.forAllWindows(callback, traverseTopToBottom)) {
2086 return true;
2087 }
2088 }
2089 } else {
2090 final int count = mChildren.size();
2091 for (int i = 0; i < count; i++) {
2092 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07002093 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002094 continue;
2095 }
Robert Carr9785cf32018-04-25 15:06:07 -07002096
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002097 if (child.forAllWindows(callback, traverseTopToBottom)) {
2098 return true;
2099 }
2100 }
2101 }
2102 return false;
2103 }
2104
2105 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
2106 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
2107 }
2108
Wale Ogunwale399c8692017-05-08 14:22:42 -07002109 @Override
2110 int getOrientation() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002111 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002112
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002113 if (mWmService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08002114 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07002115 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
2116 + " is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002117 // If the display is frozen, some activities may be in the middle of restarting, and
2118 // thus have removed their old window. If the window has the flag to hide the lock
2119 // screen, then the lock screen can re-appear and inflict its own orientation on us.
2120 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08002121 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002122 } else if (policy.isKeyguardLocked()) {
2123 // Use the last orientation the while the display is frozen with the keyguard
2124 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
2125 // window. We don't want to check the show when locked window directly though as
2126 // things aren't stable while the display is frozen, for example the window could be
2127 // momentarily unavailable due to activity relaunch.
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07002128 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
2129 + " is frozen while keyguard locked, return " + mLastOrientation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08002130 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07002131 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002132 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002133 final int orientation = mAboveAppWindowsContainers.getOrientation();
2134 if (orientation != SCREEN_ORIENTATION_UNSET) {
2135 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002136 }
Wale Ogunwale51362492016-09-08 17:49:17 -07002137 }
2138
Wale Ogunwale399c8692017-05-08 14:22:42 -07002139 // Top system windows are not requesting an orientation. Start searching from apps.
2140 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07002141 }
2142
Craig Mautner46ac6fa2013-08-01 10:06:34 -07002143 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07002144 // Check if display metrics changed and update base values if needed.
2145 updateBaseDisplayMetricsIfNeeded();
2146
Craig Mautner722285e2012-09-07 13:55:58 -07002147 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07002148 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07002149
Garfield Tan2f145f22018-11-01 15:27:03 -07002150 onDisplayChanged(this);
Craig Mautner722285e2012-09-07 13:55:58 -07002151 }
2152
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002153 void initializeDisplayBaseInfo() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002154 final DisplayManagerInternal displayManagerInternal = mWmService.mDisplayManagerInternal;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002155 if (displayManagerInternal != null) {
2156 // Bootstrap the default logical display from the display manager.
2157 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
2158 if (newDisplayInfo != null) {
2159 mDisplayInfo.copyFrom(newDisplayInfo);
2160 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002161 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002162
Andrii Kuliancd097992017-03-23 18:31:59 -07002163 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
2164 mDisplayInfo.logicalDensityDpi);
2165 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
2166 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
2167 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002168 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002169 }
2170
Andrii Kuliancd097992017-03-23 18:31:59 -07002171 /**
2172 * If display metrics changed, overrides are not set and it's not just a rotation - update base
2173 * values.
2174 */
2175 private void updateBaseDisplayMetricsIfNeeded() {
2176 // Get real display metrics without overrides from WM.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002177 mWmService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
Andrii Kuliancd097992017-03-23 18:31:59 -07002178 final int orientation = mDisplayInfo.rotation;
2179 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2180 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
2181 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
2182 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002183 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07002184
2185 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
2186 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01002187 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
2188 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07002189
2190 if (displayMetricsChanged) {
2191 // Check if display size or density is forced.
2192 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
2193 || mBaseDisplayHeight != mInitialDisplayHeight;
2194 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
2195
2196 // If there is an override set for base values - use it, otherwise use new values.
2197 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
2198 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
2199 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
2200
2201 // Real display metrics changed, so we should also update initial values.
2202 mInitialDisplayWidth = newWidth;
2203 mInitialDisplayHeight = newHeight;
2204 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01002205 mInitialDisplayCutout = newCutout;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002206 mWmService.reconfigureDisplayLocked(this);
Andrii Kuliancd097992017-03-23 18:31:59 -07002207 }
2208 }
2209
Bryce Lee27cec322017-03-21 09:41:37 -07002210 /** Sets the maximum width the screen resolution can be */
2211 void setMaxUiWidth(int width) {
2212 if (DEBUG_DISPLAY) {
2213 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
2214 }
2215
2216 mMaxUiWidth = width;
2217
2218 // Update existing metrics.
2219 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
2220 }
2221
2222 /** Update base (override) display metrics. */
2223 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
2224 mBaseDisplayWidth = baseWidth;
2225 mBaseDisplayHeight = baseHeight;
2226 mBaseDisplayDensity = baseDensity;
2227
2228 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
2229 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
2230 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
2231 mBaseDisplayWidth = mMaxUiWidth;
2232
2233 if (DEBUG_DISPLAY) {
2234 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
2235 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
2236 + " on display:" + getDisplayId());
2237 }
2238 }
2239
2240 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08002241
2242 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07002243 }
2244
Riddle Hsuf53da812018-08-15 22:00:27 +08002245 /**
2246 * Forces this display to use the specified density.
2247 *
2248 * @param density The density in DPI to use. If the value equals to initial density, the setting
2249 * will be cleared.
2250 * @param userId The target user to apply. Only meaningful when this is default display. If the
2251 * user id is {@link UserHandle#USER_CURRENT}, it means to apply current settings
2252 * so only need to configure display.
2253 */
2254 void setForcedDensity(int density, int userId) {
2255 final boolean clear = density == mInitialDisplayDensity;
2256 final boolean updateCurrent = userId == UserHandle.USER_CURRENT;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002257 if (mWmService.mCurrentUserId == userId || updateCurrent) {
Riddle Hsuf53da812018-08-15 22:00:27 +08002258 mBaseDisplayDensity = density;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002259 mWmService.reconfigureDisplayLocked(this);
Riddle Hsuf53da812018-08-15 22:00:27 +08002260 }
2261 if (updateCurrent) {
2262 // We are applying existing settings so no need to save it again.
2263 return;
2264 }
2265
2266 if (density == mInitialDisplayDensity) {
2267 density = 0;
2268 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002269 mWmService.mDisplayWindowSettings.setForcedDensity(this, density, userId);
Riddle Hsuf53da812018-08-15 22:00:27 +08002270 }
2271
2272 /** @param mode {@link #FORCE_SCALING_MODE_AUTO} or {@link #FORCE_SCALING_MODE_DISABLED}. */
2273 void setForcedScalingMode(@ForceScalingMode int mode) {
2274 if (mode != FORCE_SCALING_MODE_DISABLED) {
2275 mode = FORCE_SCALING_MODE_AUTO;
2276 }
2277
2278 mDisplayScalingDisabled = (mode != FORCE_SCALING_MODE_AUTO);
2279 Slog.i(TAG_WM, "Using display scaling mode: " + (mDisplayScalingDisabled ? "off" : "auto"));
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002280 mWmService.reconfigureDisplayLocked(this);
Riddle Hsuf53da812018-08-15 22:00:27 +08002281
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002282 mWmService.mDisplayWindowSettings.setForcedScalingMode(this, mode);
Riddle Hsuf53da812018-08-15 22:00:27 +08002283 }
2284
2285 /** If the given width and height equal to initial size, the setting will be cleared. */
2286 void setForcedSize(int width, int height) {
2287 final boolean clear = mInitialDisplayWidth == width && mInitialDisplayHeight == height;
2288 if (!clear) {
2289 // Set some sort of reasonable bounds on the size of the display that we will try
2290 // to emulate.
2291 final int minSize = 200;
2292 final int maxScale = 2;
2293 width = Math.min(Math.max(width, minSize), mInitialDisplayWidth * maxScale);
2294 height = Math.min(Math.max(height, minSize), mInitialDisplayHeight * maxScale);
2295 }
2296
2297 Slog.i(TAG_WM, "Using new display size: " + width + "x" + height);
2298 updateBaseDisplayMetrics(width, height, mBaseDisplayDensity);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002299 mWmService.reconfigureDisplayLocked(this);
Riddle Hsuf53da812018-08-15 22:00:27 +08002300
2301 if (clear) {
2302 width = height = 0;
2303 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002304 mWmService.mDisplayWindowSettings.setForcedSize(this, width, height);
Riddle Hsuf53da812018-08-15 22:00:27 +08002305 }
2306
Wale Ogunwaledb506192017-12-08 10:57:32 -08002307 void getStableRect(Rect out) {
2308 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08002309 }
2310
Yunfan Chen279f5582018-12-12 15:24:50 -08002311 void setStackOnDisplay(int stackId, boolean onTop, TaskStack stack) {
2312 if (DEBUG_STACK) {
2313 Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId=" + mDisplayId);
2314 }
Andrii Kulian839def92016-11-02 10:58:58 -07002315
Wale Ogunwale61911492017-10-11 08:50:50 -07002316 mTaskStackContainers.addStackToDisplay(stack, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002317 }
2318
Andrii Kulian51c1b672017-04-07 18:39:32 -07002319 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002320 final DisplayContent prevDc = stack.getDisplayContent();
2321 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002322 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
2323 + " which is not currently attached to any display");
2324 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002325 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07002326 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
2327 + " to its current displayId=" + mDisplayId);
2328 }
2329
lumark588a3e82018-07-20 18:53:54 +08002330 // Clean up all pending transitions when stack reparent to another display.
2331 stack.forAllAppWindows(AppWindowToken::removeFromPendingTransition);
2332
Wale Ogunwale61911492017-10-11 08:50:50 -07002333 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07002334 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08002335 }
2336
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002337 @Override
2338 protected void addChild(DisplayChildWindowContainer child,
2339 Comparator<DisplayChildWindowContainer> comparator) {
2340 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2341 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002342
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002343 @Override
2344 protected void addChild(DisplayChildWindowContainer child, int index) {
2345 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2346 }
2347
2348 @Override
2349 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002350 // Only allow removal of direct children from this display if the display is in the process
2351 // of been removed.
2352 if (mRemovingDisplay) {
2353 super.removeChild(child);
2354 return;
2355 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002356 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002357 }
2358
Andrii Kuliand2765632016-12-12 22:26:34 -08002359 @Override
2360 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
2361 // Children of the display are statically ordered, so the real intention here is to perform
2362 // the operation on the display and not the static direct children.
2363 getParent().positionChildAt(position, this, includingParents);
2364 }
2365
Riddle Hsu57831b52018-07-27 00:31:48 +08002366 void positionStackAt(int position, TaskStack child, boolean includingParents) {
2367 mTaskStackContainers.positionChildAt(position, child, includingParents);
Winson Chung59a47ded2018-01-25 17:46:06 +00002368 layoutAndAssignWindowLayersIfNeeded();
2369 }
2370
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002371 /**
2372 * Used to obtain task ID when user taps on coordinate (x, y) in this display, and outside
2373 * current task in focus.
2374 *
2375 * This returns the task ID of the foremost task at (x, y) if the task is not home. Otherwise it
2376 * returns -1.
2377 *
2378 * @param x horizontal coordinate of the tap position
2379 * @param y vertical coordinate of the tap position
2380 * @return the task ID if a non-home task is found; -1 if not
2381 */
2382 int taskForTapOutside(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002383 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2384 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Garfield Tanf2e0a9e2018-10-16 09:42:02 -07002385 if (stack.isActivityTypeHome() && !stack.inMultiWindowMode()) {
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002386 // We skip not only home stack, but also everything behind home because user can't
Garfield Tanf2e0a9e2018-10-16 09:42:02 -07002387 // see them when home stack is isn't in multi-window mode.
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002388 break;
2389 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002390 final int taskId = stack.taskIdFromPoint(x, y);
2391 if (taskId != -1) {
2392 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07002393 }
2394 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002395 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07002396 }
2397
Chong Zhang8e89b312015-09-09 15:09:30 -07002398 /**
Winson Chungc5fe7ff2019-02-19 14:49:25 -08002399 * Returns true if the input point is within an app window.
2400 */
2401 boolean pointWithinAppWindow(int x, int y) {
2402 final int[] targetWindowType = {-1};
2403 final Consumer fn = PooledLambda.obtainConsumer((w, nonArg) -> {
2404 if (targetWindowType[0] != -1) {
2405 return;
2406 }
2407
2408 if (w.isOnScreen() && w.isVisibleLw() && w.getFrameLw().contains(x, y)) {
2409 targetWindowType[0] = w.mAttrs.type;
2410 return;
2411 }
2412 }, PooledLambda.__(WindowState.class), mTmpRect);
2413 forAllWindows(fn, true /* traverseTopToBottom */);
2414 ((PooledConsumer) fn).recycle();
2415 return FIRST_APPLICATION_WINDOW <= targetWindowType[0]
2416 && targetWindowType[0] <= LAST_APPLICATION_WINDOW;
2417 }
2418
2419 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08002420 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07002421 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07002422 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07002423 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002424 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002425 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07002426 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2427 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002428 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002429 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002430 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002431
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002432 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
2433 if (mTmpTaskForResizePointSearchResult.searchDone) {
2434 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07002435 }
2436 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002437 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002438 }
2439
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002440 void updateTouchExcludeRegion() {
2441 final Task focusedTask = (mFocusedApp != null ? mFocusedApp.getTask() : null);
David Stevensee9e2772017-02-09 16:30:27 -08002442 if (focusedTask == null) {
2443 mTouchExcludeRegion.setEmpty();
2444 } else {
2445 mTouchExcludeRegion.set(mBaseDisplayRect);
2446 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
2447 mTmpRect2.setEmpty();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002448 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0;
2449 --stackNdx) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002450 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002451 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
2452 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08002453 }
2454 // If we removed the focused task above, add it back and only leave its
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002455 // outside touch area in the exclusion. TapDetector is not interested in
David Stevensee9e2772017-02-09 16:30:27 -08002456 // any touch inside the focused task itself.
2457 if (!mTmpRect2.isEmpty()) {
2458 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
2459 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08002460 }
lumark90120a82018-08-15 00:33:03 +08002461 if (mInputMethodWindow != null && mInputMethodWindow.isVisibleLw()) {
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002462 // If the input method is visible and the user is typing, we don't want these touch
2463 // events to be intercepted and used to change focus. This would likely cause a
2464 // disappearance of the input method.
lumark90120a82018-08-15 00:33:03 +08002465 mInputMethodWindow.getTouchableRegion(mTmpRegion);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002466 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002467 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002468 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002469 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002470 win.getTouchableRegion(mTmpRegion);
2471 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2472 }
Tiger Huang04dc4cc2019-01-17 18:41:41 +08002473 amendWindowTapExcludeRegion(mTouchExcludeRegion);
Andrii Kulian03c403d2016-11-11 11:14:12 -08002474 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002475 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002476 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002477 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002478 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2479 }
Riddle Hsu2588ab02019-02-25 14:23:56 +08002480 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner6601b7b2013-04-29 10:29:11 -07002481 }
2482
Tiger Huang04dc4cc2019-01-17 18:41:41 +08002483 /**
2484 * Union the region with all the tap exclude region provided by windows on this display.
2485 *
2486 * @param inOutRegion The region to be amended.
2487 */
2488 void amendWindowTapExcludeRegion(Region inOutRegion) {
2489 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2490 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2491 win.amendTapExcludeRegion(inOutRegion);
2492 }
2493 }
2494
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002495 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07002496 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002497 super.switchUser();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002498 mWmService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07002499 }
2500
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07002501 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002502 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2503 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07002504 }
2505 }
2506
Wale Ogunwale10124582016-09-15 20:25:50 -07002507 @Override
2508 void removeIfPossible() {
2509 if (isAnimating()) {
2510 mDeferredRemoval = true;
2511 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002512 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002513 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002514 }
2515
Wale Ogunwale10124582016-09-15 20:25:50 -07002516 @Override
2517 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002518 mRemovingDisplay = true;
2519 try {
lumark588a3e82018-07-20 18:53:54 +08002520 // Clear all transitions & screen frozen states when removing display.
2521 mOpeningApps.clear();
2522 mClosingApps.clear();
Evan Rosky2289ba12018-11-19 18:28:18 -08002523 mChangingApps.clear();
lumark588a3e82018-07-20 18:53:54 +08002524 mUnknownAppVisibilityController.clear();
2525 mAppTransition.removeAppTransitionTimeoutCallbacks();
2526 handleAnimatingStoppedAndTransition();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002527 mWmService.stopFreezingDisplayLocked();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002528 super.removeImmediately();
2529 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Riddle Hsu2588ab02019-02-25 14:23:56 +08002530 mPointerEventDispatcher.dispose();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002531 mWmService.mAnimator.removeDisplayLocked(mDisplayId);
Louis Chang207f9112018-07-30 08:58:54 +08002532 mWindowingLayer.release();
2533 mOverlayLayer.release();
Jorim Jaggiae962e62018-12-27 17:23:48 +01002534 mInputMonitor.onDisplayRemoved();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002535 } finally {
Riddle Hsu4e611772018-10-31 18:58:28 +08002536 mDisplayReady = false;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002537 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002538 }
Robert Carr679ccb02018-08-08 15:32:35 -07002539
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002540 mWmService.mWindowPlacerLocked.requestTraversal();
Craig Mautner95da1082014-02-24 17:54:35 -08002541 }
2542
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002543 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002544 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002545 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002546 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2547
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002548 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002549 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002550 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002551 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002552 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002553 }
2554
Andrii Kulian0214ed92017-05-16 13:44:05 -07002555 /** @return 'true' if removal of this display content is deferred due to active animation. */
2556 boolean isRemovalDeferred() {
2557 return mDeferredRemoval;
2558 }
2559
Wale Ogunwale10124582016-09-15 20:25:50 -07002560 boolean animateForIme(float interpolatedValue, float animationTarget,
2561 float dividerAnimationTarget) {
2562 boolean updated = false;
2563
Wale Ogunwale61911492017-10-11 08:50:50 -07002564 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2565 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002566 if (stack == null || !stack.isAdjustedForIme()) {
2567 continue;
2568 }
2569
2570 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2571 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2572 updated = true;
2573 } else {
2574 mDividerControllerLocked.mLastAnimationProgress =
2575 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2576 mDividerControllerLocked.mLastDividerProgress =
2577 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2578 updated |= stack.updateAdjustForIme(
2579 mDividerControllerLocked.mLastAnimationProgress,
2580 mDividerControllerLocked.mLastDividerProgress,
2581 false /* force */);
2582 }
2583 if (interpolatedValue >= 1f) {
2584 stack.endImeAdjustAnimation();
2585 }
2586 }
2587
2588 return updated;
2589 }
2590
2591 boolean clearImeAdjustAnimation() {
2592 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002593 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2594 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002595 if (stack != null && stack.isAdjustedForIme()) {
2596 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2597 changed = true;
2598 }
2599 }
2600 return changed;
2601 }
2602
2603 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002604 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2605 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002606 if (stack.isVisible() && stack.isAdjustedForIme()) {
2607 stack.beginImeAdjustAnimation();
2608 }
2609 }
2610 }
2611
2612 void adjustForImeIfNeeded() {
lumark90120a82018-08-15 00:33:03 +08002613 final WindowState imeWin = mInputMethodWindow;
Wale Ogunwale10124582016-09-15 20:25:50 -07002614 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2615 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002616 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002617 final TaskStack imeTargetStack = mWmService.getImeFocusStackLocked();
Wale Ogunwale10124582016-09-15 20:25:50 -07002618 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2619 imeTargetStack.getDockSide() : DOCKED_INVALID;
2620 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2621 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2622 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002623 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002624 final boolean imeHeightChanged = imeVisible &&
2625 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2626
2627 // The divider could be adjusted for IME position, or be thinner than usual,
2628 // or both. There are three possible cases:
2629 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2630 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2631 // - If IME is not visible, divider is not moved and is normal width.
2632
2633 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002634 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2635 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002636 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002637 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2638 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002639 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2640 } else {
2641 stack.resetAdjustedForIme(false);
2642 }
2643 }
2644 mDividerControllerLocked.setAdjustedForIme(
2645 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2646 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002647 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2648 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002649 stack.resetAdjustedForIme(!dockVisible);
2650 }
2651 mDividerControllerLocked.setAdjustedForIme(
2652 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2653 }
Winson Chung655332c2016-10-31 13:14:28 -07002654 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002655 }
2656
2657 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002658 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2659 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002660 stack.prepareFreezingTaskBounds();
2661 }
2662 }
2663
Wale Ogunwale94744212015-09-21 19:01:47 -07002664 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002665 getBounds(mTmpRect, newRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002666 rotateBounds(mTmpRect, oldRotation, newRotation, bounds);
2667 }
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002668
Evan Rosky39b6f232018-10-30 18:35:41 -07002669 void rotateBounds(Rect parentBounds, int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002670 // Compute a transform matrix to undo the coordinate space transformation,
2671 // and present the window at the same physical position it previously occupied.
2672 final int deltaRotation = deltaRotation(newRotation, oldRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002673 createRotationMatrix(
2674 deltaRotation, parentBounds.width(), parentBounds.height(), mTmpMatrix);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002675
2676 mTmpRectF.set(bounds);
2677 mTmpMatrix.mapRect(mTmpRectF);
2678 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002679 }
2680
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002681 static int deltaRotation(int oldRotation, int newRotation) {
2682 int delta = newRotation - oldRotation;
2683 if (delta < 0) delta += 4;
2684 return delta;
2685 }
2686
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002687 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002688 Matrix outMatrix) {
2689 // For rotations without Z-ordering we don't need the target rectangle's position.
2690 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2691 displayHeight, outMatrix);
2692 }
2693
2694 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2695 float displayWidth, float displayHeight, Matrix outMatrix) {
2696 switch (rotation) {
2697 case ROTATION_0:
2698 outMatrix.reset();
2699 break;
2700 case ROTATION_270:
2701 outMatrix.setRotate(270, 0, 0);
2702 outMatrix.postTranslate(0, displayHeight);
2703 outMatrix.postTranslate(rectTop, 0);
2704 break;
2705 case ROTATION_180:
2706 outMatrix.reset();
2707 break;
2708 case ROTATION_90:
2709 outMatrix.setRotate(90, 0, 0);
2710 outMatrix.postTranslate(displayWidth, 0);
2711 outMatrix.postTranslate(-rectTop, rectLeft);
2712 break;
2713 }
2714 }
2715
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002716 @CallSuper
2717 @Override
Nataniel Borges023ecb52019-01-16 14:15:43 -08002718 public void writeToProto(ProtoOutputStream proto, long fieldId,
2719 @WindowTraceLogLevel int logLevel) {
2720 // Critical log level logs only visible elements to mitigate performance overheard
2721 if (logLevel == WindowTraceLogLevel.CRITICAL && !isVisible()) {
2722 return;
2723 }
2724
Steven Timotiusaf03df62017-07-18 16:56:43 -07002725 final long token = proto.start(fieldId);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002726 super.writeToProto(proto, WINDOW_CONTAINER, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002727 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002728 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2729 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002730 stack.writeToProto(proto, STACKS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002731 }
2732 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2733 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002734 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2735 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002736 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002737 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002738 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2739 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002740 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002741 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002742 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2743 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002744 windowToken.writeToProto(proto, IME_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002745 }
2746 proto.write(DPI, mBaseDisplayDensity);
2747 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002748 proto.write(ROTATION, mRotation);
2749 final ScreenRotationAnimation screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002750 mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002751 if (screenRotationAnimation != null) {
2752 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2753 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002754 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
lumark588a3e82018-07-20 18:53:54 +08002755 mAppTransition.writeToProto(proto, APP_TRANSITION);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002756 if (mFocusedApp != null) {
2757 mFocusedApp.writeNameToProto(proto, FOCUSED_APP);
2758 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002759 proto.end(token);
2760 }
2761
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002762 @Override
2763 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2764 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002765 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2766 final String subPrefix = " " + prefix;
2767 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2768 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2769 pw.print("dpi");
2770 if (mInitialDisplayWidth != mBaseDisplayWidth
2771 || mInitialDisplayHeight != mBaseDisplayHeight
2772 || mInitialDisplayDensity != mBaseDisplayDensity) {
2773 pw.print(" base=");
2774 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2775 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2776 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002777 if (mDisplayScalingDisabled) {
2778 pw.println(" noscale");
2779 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002780 pw.print(" cur=");
2781 pw.print(mDisplayInfo.logicalWidth);
2782 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2783 pw.print(" app=");
2784 pw.print(mDisplayInfo.appWidth);
2785 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2786 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2787 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2788 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2789 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002790 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002791 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002792 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002793
Craig Mautnerdc548482014-02-05 13:35:24 -08002794 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002795 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002796 pw.print(prefix);
2797 pw.print("mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
Adrian Roos5251b1d2018-03-23 18:57:43 +01002798
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002799 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
2800 if (mLastFocus != mCurrentFocus) {
2801 pw.print(" mLastFocus="); pw.println(mLastFocus);
2802 }
2803 if (mLosingFocus.size() > 0) {
2804 pw.println();
2805 pw.println(" Windows losing focus:");
2806 for (int i = mLosingFocus.size() - 1; i >= 0; i--) {
2807 final WindowState w = mLosingFocus.get(i);
2808 pw.print(" Losing #"); pw.print(i); pw.print(' ');
2809 pw.print(w);
2810 if (dumpAll) {
2811 pw.println(":");
2812 w.dump(pw, " ", true);
2813 } else {
2814 pw.println();
2815 }
2816 }
2817 }
2818 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
Tiger Huang7c610aa2018-10-27 00:01:01 +08002819 if (mLastStatusBarVisibility != 0) {
2820 pw.print(" mLastStatusBarVisibility=0x");
2821 pw.println(Integer.toHexString(mLastStatusBarVisibility));
2822 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002823
Adrian Roos5251b1d2018-03-23 18:57:43 +01002824 pw.println();
wilsonshihc32538e2018-11-07 17:27:34 +08002825 mWallpaperController.dump(pw, " ");
2826
2827 pw.println();
Adrian Roos4ffc8972019-02-07 20:45:11 +01002828 pw.print("mSystemGestureExclusion=");
2829 if (mSystemGestureExclusionListeners.getRegisteredCallbackCount() > 0) {
2830 pw.println(mSystemGestureExclusion);
2831 } else {
2832 pw.println("<no lstnrs>");
2833 }
2834
2835 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002836 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002837 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2838 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002839 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002840 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002841
Craig Mautnerdc548482014-02-05 13:35:24 -08002842 pw.println();
2843 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002844 pw.println();
2845 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002846 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002847 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002848 pw.print(" Exiting #"); pw.print(i);
2849 pw.print(' '); pw.print(token);
2850 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002851 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002852 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002853 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002854
Jorim Jaggi31f71702016-05-04 16:43:04 -07002855 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002856
2857 // Dump stack references
2858 final TaskStack homeStack = getHomeStack();
2859 if (homeStack != null) {
2860 pw.println(prefix + "homeStack=" + homeStack.getName());
2861 }
2862 final TaskStack pinnedStack = getPinnedStack();
2863 if (pinnedStack != null) {
2864 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2865 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002866 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002867 if (splitScreenPrimaryStack != null) {
2868 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2869 }
2870
2871 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002872 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002873 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002874 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002875
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002876 pw.println();
2877 mDisplayFrames.dump(prefix, pw);
Arthur Hung95b38a92018-07-20 18:56:12 +08002878 pw.println();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08002879 mDisplayPolicy.dump(prefix, pw);
2880 pw.println();
Riddle Hsuad256a12018-07-18 16:11:30 +08002881 mDisplayRotation.dump(prefix, pw);
2882 pw.println();
Arthur Hung95b38a92018-07-20 18:56:12 +08002883 mInputMonitor.dump(pw, " ");
Jorim Jaggif96c90a2018-09-26 16:55:15 +02002884 pw.println();
2885 mInsetsStateController.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002886 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002887
2888 @Override
2889 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002890 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002891 }
2892
2893 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002894 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002895 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002896
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002897 /** Returns true if the stack in the windowing mode is visible. */
2898 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002899 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002900 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002901 }
2902
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002903 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002904 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002905 final int x = (int) xf;
2906 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002907 final WindowState touchedWin = getWindow(w -> {
2908 final int flags = w.mAttrs.flags;
2909 if (!w.isVisibleLw()) {
2910 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002911 }
2912 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002913 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002914 }
2915
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002916 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002917 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002918 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002919 }
2920
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002921 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002922
2923 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002924 return mTmpRegion.contains(x, y) || touchFlags == 0;
2925 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002926
2927 return touchedWin;
2928 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002929
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002930 boolean canAddToastWindowForUid(int uid) {
2931 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002932 // Also if the app is focused adding more than one toast at
2933 // a time for better backwards compatibility.
2934 final WindowState focusedWindowForUid = getWindow(w ->
2935 w.mOwnerUid == uid && w.isFocused());
2936 if (focusedWindowForUid != null) {
2937 return true;
2938 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002939 final WindowState win = getWindow(w ->
2940 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2941 && !w.mWindowRemovalAllowed);
2942 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002943 }
2944
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002945 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002946 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2947 return;
2948 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002949
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002950 // Used to communicate the old focus to the callback method.
2951 mTmpWindow = oldFocus;
2952
2953 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002954 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002955
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002956 WindowState findFocusedWindowIfNeeded() {
2957 return (mWmService.mPerDisplayFocusEnabled
2958 || mWmService.mRoot.mTopFocusedAppByProcess.isEmpty()) ? findFocusedWindow() : null;
2959 }
2960
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002961 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002962 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002963
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002964 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002965
2966 if (mTmpWindow == null) {
2967 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2968 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002969 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002970 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002971 }
2972
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002973 /**
2974 * Update the focused window and make some adjustments if the focus has changed.
2975 *
2976 * @param mode Indicates the situation we are in. Possible modes are:
2977 * {@link WindowManagerService#UPDATE_FOCUS_NORMAL},
2978 * {@link WindowManagerService#UPDATE_FOCUS_PLACING_SURFACES},
2979 * {@link WindowManagerService#UPDATE_FOCUS_WILL_PLACE_SURFACES},
2980 * {@link WindowManagerService#UPDATE_FOCUS_REMOVING_FOCUS}
2981 * @param updateInputWindows Whether to sync the window information to the input module.
2982 * @return {@code true} if the focused window has changed.
2983 */
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002984 boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows) {
2985 WindowState newFocus = findFocusedWindowIfNeeded();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002986 if (mCurrentFocus == newFocus) {
2987 return false;
2988 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002989 boolean imWindowChanged = false;
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002990 final WindowState imWindow = mInputMethodWindow;
2991 if (imWindow != null) {
2992 final WindowState prevTarget = mInputMethodTarget;
2993 final WindowState newTarget = computeImeTarget(true /* updateImeTarget*/);
2994 imWindowChanged = prevTarget != newTarget;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002995
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002996 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS
2997 && mode != UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2998 assignWindowLayers(false /* setLayoutNeeded */);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002999 }
3000 }
3001
3002 if (imWindowChanged) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003003 mWmService.mWindowsChanged = true;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003004 setLayoutNeeded();
Tiger Huang51c5a1d2018-12-11 20:24:51 +08003005 newFocus = findFocusedWindowIfNeeded();
3006 }
3007 if (mCurrentFocus != newFocus) {
3008 mWmService.mH.obtainMessage(REPORT_FOCUS_CHANGE, this).sendToTarget();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003009 }
3010
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003011 if (DEBUG_FOCUS_LIGHT || mWmService.localLOGV) Slog.v(TAG_WM, "Changing focus from "
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003012 + mCurrentFocus + " to " + newFocus + " displayId=" + getDisplayId()
3013 + " Callers=" + Debug.getCallers(4));
3014 final WindowState oldFocus = mCurrentFocus;
3015 mCurrentFocus = newFocus;
3016 mLosingFocus.remove(newFocus);
3017
3018 if (newFocus != null) {
3019 mWinAddedSinceNullFocus.clear();
3020 mWinRemovedSinceNullFocus.clear();
3021
3022 if (newFocus.canReceiveKeys()) {
3023 // Displaying a window implicitly causes dispatching to be unpaused.
3024 // This is to protect against bugs if someone pauses dispatching but
3025 // forgets to resume.
3026 newFocus.mToken.paused = false;
3027 }
3028 }
3029
Tiger Huang7c610aa2018-10-27 00:01:01 +08003030 int focusChanged = getDisplayPolicy().focusChangedLw(oldFocus, newFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003031
3032 if (imWindowChanged && oldFocus != mInputMethodWindow) {
3033 // Focus of the input method window changed. Perform layout if needed.
3034 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
3035 performLayout(true /*initial*/, updateInputWindows);
3036 focusChanged &= ~FINISH_LAYOUT_REDO_LAYOUT;
3037 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
3038 // Client will do the layout, but we need to assign layers
3039 // for handleNewWindowLocked() below.
3040 assignWindowLayers(false /* setLayoutNeeded */);
3041 }
3042 }
3043
3044 if ((focusChanged & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3045 // The change in focus caused us to need to do a layout. Okay.
3046 setLayoutNeeded();
3047 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
3048 performLayout(true /*initial*/, updateInputWindows);
3049 } else if (mode == UPDATE_FOCUS_REMOVING_FOCUS) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003050 mWmService.mRoot.performSurfacePlacement(false);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003051 }
3052 }
3053
3054 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
3055 // If we defer assigning layers, then the caller is responsible for doing this part.
3056 getInputMonitor().setInputFocusLw(newFocus, updateInputWindows);
3057 }
3058
3059 adjustForImeIfNeeded();
3060
3061 // We may need to schedule some toast windows to be removed. The toasts for an app that
3062 // does not have input focus are removed within a timeout to prevent apps to redress
3063 // other apps' UI.
3064 scheduleToastWindowsTimeoutIfNeededLocked(oldFocus, newFocus);
3065
3066 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
3067 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
3068 }
3069 return true;
3070 }
3071
3072 /**
3073 * Set the new focused app to this display.
3074 *
3075 * @param newFocus the new focused AppWindowToken.
3076 * @return true if the focused app is changed.
3077 */
3078 boolean setFocusedApp(AppWindowToken newFocus) {
3079 if (newFocus != null) {
3080 final DisplayContent appDisplay = newFocus.getDisplayContent();
3081 if (appDisplay != this) {
3082 throw new IllegalStateException(newFocus + " is not on " + getName()
3083 + " but " + ((appDisplay != null) ? appDisplay.getName() : "none"));
3084 }
3085 }
3086 if (mFocusedApp == newFocus) {
3087 return false;
3088 }
3089 mFocusedApp = newFocus;
3090 getInputMonitor().setFocusedAppLw(newFocus);
3091 updateTouchExcludeRegion();
3092 return true;
3093 }
3094
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003095 /** Updates the layer assignment of windows on this display. */
3096 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003097 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07003098 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003099 if (setLayoutNeeded) {
3100 setLayoutNeeded();
3101 }
Robert Carrb1579c82017-09-05 14:54:47 -07003102
Robert Carrf59b8dd2017-10-02 18:58:36 -07003103 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07003104 // the application of this transaction until the animation pass triggers
3105 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
3106 // the hiding and showing of surfaces.
3107 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003108 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003109 }
3110
Wale Ogunwale1666e312016-12-16 11:27:18 -08003111 // TODO: This should probably be called any time a visual change is made to the hierarchy like
3112 // moving containers or resizing them. Need to investigate the best way to have it automatically
3113 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003114 void layoutAndAssignWindowLayersIfNeeded() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003115 mWmService.mWindowsChanged = true;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003116 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003117
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003118 if (!mWmService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003119 false /*updateInputWindows*/)) {
3120 assignWindowLayers(false /* setLayoutNeeded */);
3121 }
3122
Arthur Hung95b38a92018-07-20 18:56:12 +08003123 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003124 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08003125 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003126 }
3127
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003128 /** Returns true if a leaked surface was destroyed */
3129 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003130 // Used to indicate that a surface was leaked.
3131 mTmpWindow = null;
3132 forAllWindows(w -> {
3133 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003134 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003135 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003136 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003137 if (!mWmService.mSessions.contains(wsa.mSession)) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003138 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003139 + w + " surface=" + wsa.mSurfaceController
3140 + " token=" + w.mToken
3141 + " pid=" + w.mSession.mPid
3142 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003143 wsa.destroySurface();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003144 mWmService.mForceRemoves.add(w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003145 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07003146 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003147 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003148 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02003149 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003150 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003151 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003152 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003153 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003154 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003155
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003156 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003157 }
3158
3159 /**
lumark90120a82018-08-15 00:33:03 +08003160 * Set input method window for the display.
3161 * @param win Set when window added or Null when destroyed.
3162 */
3163 void setInputMethodWindowLocked(WindowState win) {
3164 mInputMethodWindow = win;
lumark70865a82018-09-17 16:56:26 +08003165 // Update display configuration for IME process.
3166 if (mInputMethodWindow != null) {
3167 final int imePid = mInputMethodWindow.mSession.mPid;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003168 mWmService.mAtmInternal.onImeWindowSetOnDisplay(imePid,
lumark70865a82018-09-17 16:56:26 +08003169 mInputMethodWindow.getDisplayId());
3170 }
lumark90120a82018-08-15 00:33:03 +08003171 computeImeTarget(true /* updateImeTarget */);
Jorim Jaggif96c90a2018-09-26 16:55:15 +02003172 mInsetsStateController.getSourceProvider(TYPE_IME).setWindow(win,
3173 null /* frameProvider */);
lumark90120a82018-08-15 00:33:03 +08003174 }
3175
3176 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003177 * Determine and return the window that should be the IME target.
3178 * @param updateImeTarget If true the system IME target will be updated to match what we found.
3179 * @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 +00003180 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003181 WindowState computeImeTarget(boolean updateImeTarget) {
lumark90120a82018-08-15 00:33:03 +08003182 if (mInputMethodWindow == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003183 // There isn't an IME so there shouldn't be a target...That was easy!
3184 if (updateImeTarget) {
3185 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
lumarkff0ab692018-11-05 20:32:30 +08003186 + mInputMethodTarget + " to null since mInputMethodWindow is null");
3187 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003188 }
3189 return null;
3190 }
3191
lumarkff0ab692018-11-05 20:32:30 +08003192 final WindowState curTarget = mInputMethodTarget;
Chavi Weingarten3a748552018-05-14 17:32:42 +00003193 if (!canUpdateImeTarget()) {
3194 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
3195 return curTarget;
3196 }
3197
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003198 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
3199 // same display. Or even when the current IME/target are not on the same screen as the next
3200 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08003201 mUpdateImeTarget = updateImeTarget;
3202 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003203
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003204
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003205 // Yet more tricksyness! If this window is a "starting" window, we do actually want
3206 // to be on top of it, but it is not -really- where input will go. So look down below
3207 // for a real window to target...
3208 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
3209 final AppWindowToken token = target.mAppToken;
3210 if (token != null) {
3211 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
3212 if (betterTarget != null) {
3213 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003214 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003215 }
3216 }
3217
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003218 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
lumarkff0ab692018-11-05 20:32:30 +08003219 "Proposed new IME target: " + target + " for display: " + getDisplayId());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003220
chaviw1c13ad32018-06-12 16:44:23 -07003221 // Now, a special case -- if the last target's window is in the process of exiting, but
3222 // not removed, and the new target is home, keep on the last target to avoid flicker.
3223 // Home is a special case since its above other stacks in the ordering list, but layed
3224 // out below the others.
3225 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
3226 && curTarget.isClosing() && (target == null || target.isActivityTypeHome())) {
chaviw4ad54912018-05-30 11:05:44 -07003227 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "New target is home while current target is "
Chavi Weingarten3a748552018-05-14 17:32:42 +00003228 + "closing, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003229 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003230 }
3231
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003232 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
3233 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003234
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003235 if (target == null) {
3236 if (updateImeTarget) {
3237 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
3238 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
3239 + Debug.getCallers(4) : ""));
lumarkff0ab692018-11-05 20:32:30 +08003240 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003241 }
3242
3243 return null;
3244 }
3245
3246 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003247 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
3248 if (token != null) {
3249
3250 // Now some fun for dealing with window animations that modify the Z order. We need
3251 // to look at all windows below the current target that are in this app, finding the
3252 // highest visible one in layering.
3253 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003254 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003255 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003256 }
3257
3258 if (highestTarget != null) {
lumark588a3e82018-07-20 18:53:54 +08003259 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, mAppTransition + " " + highestTarget
Jorim Jaggi35d328a2018-08-14 17:00:20 +02003260 + " animating=" + highestTarget.isAnimating());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003261
lumark588a3e82018-07-20 18:53:54 +08003262 if (mAppTransition.isTransitionSet()) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003263 // If we are currently setting up for an animation, hold everything until we
3264 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003265 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003266 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003267 }
3268 }
3269 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003270
3271 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
3272 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003273 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003274 }
3275
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003276 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003277 }
3278
lumarkff0ab692018-11-05 20:32:30 +08003279 /**
3280 * Calling {@link #computeImeTarget(boolean)} to update the input method target window in
3281 * the candidate app window token if needed.
3282 */
3283 void computeImeTargetIfNeeded(AppWindowToken candidate) {
3284 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == candidate) {
3285 computeImeTarget(true /* updateImeTarget */);
3286 }
3287 }
3288
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003289 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
lumarkff0ab692018-11-05 20:32:30 +08003290 if (target == mInputMethodTarget && mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003291 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003292 }
3293
lumarkff0ab692018-11-05 20:32:30 +08003294 mInputMethodTarget = target;
3295 mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003296 assignWindowLayers(false /* setLayoutNeeded */);
Tarandeep Singh215929b2019-01-11 18:24:37 -08003297 mInsetsStateController.onImeTargetChanged(target);
Tarandeep Singha6f35612019-01-11 19:50:46 -08003298 updateImeParent();
3299 }
3300
3301 private void updateImeParent() {
Jackal Guo818c3f12019-03-08 18:00:39 +08003302 // Force attaching IME to the display when magnifying, or it would be magnified with
3303 // target app together.
3304 final boolean shouldAttachToDisplay = (mMagnificationSpec != null);
3305 final SurfaceControl newParent =
3306 shouldAttachToDisplay ? mWindowingLayer : computeImeParent();
Tarandeep Singha6f35612019-01-11 19:50:46 -08003307 if (newParent != null) {
3308 mPendingTransaction.reparent(mImeWindowsContainers.mSurfaceControl, newParent);
3309 scheduleAnimation();
3310 }
3311 }
3312
3313 /**
3314 * Computes the window the IME should be attached to.
3315 */
3316 @VisibleForTesting
3317 SurfaceControl computeImeParent() {
3318
3319 // Attach it to app if the target is part of an app and such app is covering the entire
3320 // screen. If it's not covering the entire screen the IME might extend beyond the apps
3321 // bounds.
Riddle Hsu6d6f67c2019-03-14 16:54:26 +08003322 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken != null
3323 && mInputMethodTarget.getWindowingMode() == WINDOWING_MODE_FULLSCREEN
3324 // An activity with override bounds should be letterboxed inside its parent bounds,
3325 // so it doesn't fill the screen.
3326 && mInputMethodTarget.mAppToken.matchParentBounds()) {
Tarandeep Singha6f35612019-01-11 19:50:46 -08003327 return mInputMethodTarget.mAppToken.getSurfaceControl();
3328 }
3329
3330 // Otherwise, we just attach it to the display.
3331 return mWindowingLayer;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003332 }
3333
3334 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
3335 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3336 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
3337 }
3338
3339 // Used to indicate we have reached the first window in the range we are interested in.
3340 mTmpWindow = null;
3341
3342 // TODO: Figure-out a more efficient way to do this.
3343 final WindowState candidate = getWindow(w -> {
3344 if (w == top) {
3345 // Reached the first window in the range we are interested in.
3346 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003347 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003348 if (mTmpWindow == null) {
3349 return false;
3350 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003351
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003352 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3353 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003354 }
3355 // If we reached the bottom of the range of windows we are considering,
3356 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003357 if (w == bottom) {
3358 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003359 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003360 return false;
3361 });
3362
3363 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003364 }
3365
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003366 void setLayoutNeeded() {
3367 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
3368 mLayoutNeeded = true;
3369 }
3370
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003371 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003372 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
3373 mLayoutNeeded = false;
3374 }
3375
3376 boolean isLayoutNeeded() {
3377 return mLayoutNeeded;
3378 }
3379
Wale Ogunwale02319a62016-09-26 15:21:22 -07003380 void dumpTokens(PrintWriter pw, boolean dumpAll) {
3381 if (mTokenMap.isEmpty()) {
3382 return;
3383 }
3384 pw.println(" Display #" + mDisplayId);
3385 final Iterator<WindowToken> it = mTokenMap.values().iterator();
3386 while (it.hasNext()) {
3387 final WindowToken token = it.next();
3388 pw.print(" ");
3389 pw.print(token);
3390 if (dumpAll) {
3391 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003392 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07003393 } else {
3394 pw.println();
3395 }
3396 }
lumark588a3e82018-07-20 18:53:54 +08003397
Evan Rosky2289ba12018-11-19 18:28:18 -08003398 if (!mOpeningApps.isEmpty() || !mClosingApps.isEmpty() || !mChangingApps.isEmpty()) {
lumark588a3e82018-07-20 18:53:54 +08003399 pw.println();
3400 if (mOpeningApps.size() > 0) {
3401 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
3402 }
3403 if (mClosingApps.size() > 0) {
3404 pw.print(" mClosingApps="); pw.println(mClosingApps);
3405 }
Evan Rosky2289ba12018-11-19 18:28:18 -08003406 if (mChangingApps.size() > 0) {
3407 pw.print(" mChangingApps="); pw.println(mChangingApps);
3408 }
lumark588a3e82018-07-20 18:53:54 +08003409 }
3410
3411 mUnknownAppVisibilityController.dump(pw, " ");
Wale Ogunwale02319a62016-09-26 15:21:22 -07003412 }
3413
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003414 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003415 final int[] index = new int[1];
3416 forAllWindows(w -> {
3417 final WindowStateAnimator wAnim = w.mWinAnimator;
3418 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
3419 index[0] = index[0] + 1;
3420 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003421 }
3422
Jorim Jaggife762342016-10-13 14:33:27 +02003423 /**
3424 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
3425 */
3426 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003427 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003428 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02003429 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
3430 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01003431 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02003432 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003433 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02003434 }
3435
Wale Ogunwale494009b82016-10-21 09:01:38 -07003436 boolean checkWaitingForWindows() {
3437
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003438 mHaveBootMsg = false;
3439 mHaveApp = false;
3440 mHaveWallpaper = false;
3441 mHaveKeyguard = true;
3442
3443 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003444 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
3445 return true;
3446 }
3447 if (w.isDrawnLw()) {
3448 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003449 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003450 } else if (w.mAttrs.type == TYPE_APPLICATION
3451 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003452 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003453 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003454 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003455 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003456 mHaveKeyguard = mWmService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003457 }
3458 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003459 return false;
3460 });
3461
3462 if (visibleWindow != null) {
3463 // We have a visible window.
3464 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003465 }
3466
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003467 // if the wallpaper service is disabled on the device, we're never going to have
3468 // wallpaper, don't bother waiting for it
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003469 boolean wallpaperEnabled = mWmService.mContext.getResources().getBoolean(
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003470 com.android.internal.R.bool.config_enableWallpaperService)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003471 && mWmService.mContext.getResources().getBoolean(
Daichi Hironoc1432222018-03-29 13:06:07 +09003472 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003473 && !mWmService.mOnlyCore;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003474
Wale Ogunwale494009b82016-10-21 09:01:38 -07003475 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003476 "******** booted=" + mWmService.mSystemBooted
3477 + " msg=" + mWmService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003478 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
3479 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
3480 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003481
3482 // If we are turning on the screen to show the boot message, don't do it until the boot
3483 // message is actually displayed.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003484 if (!mWmService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003485 return true;
3486 }
3487
3488 // If we are turning on the screen after the boot is completed normally, don't do so until
3489 // we have the application and wallpaper.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003490 if (mWmService.mSystemBooted
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003491 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003492 return true;
3493 }
3494
3495 return false;
3496 }
3497
Jorim Jaggi8f520872018-08-14 17:00:20 +02003498 void updateWindowsForAnimator() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003499 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003500 }
3501
Jorim Jaggi8f520872018-08-14 17:00:20 +02003502 /**
3503 * Updates the {@link TaskStack#setAnimationBackground} for all windows.
3504 */
3505 void updateBackgroundForAnimator() {
Jorim Jaggi7156b652016-10-25 08:31:58 -07003506 resetAnimationBackgroundAnimator();
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003507 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003508 }
3509
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003510 boolean isInputMethodClientFocus(int uid, int pid) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003511 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003512 if (imFocus == null) {
3513 return false;
3514 }
3515
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003516 if (DEBUG_INPUT_METHOD) {
3517 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003518 Slog.i(TAG_WM, "Current focus: " + mCurrentFocus + " displayId=" + mDisplayId);
3519 Slog.i(TAG_WM, "Last focus: " + mLastFocus + " displayId=" + mDisplayId);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003520 }
3521
Wale Ogunwale494009b82016-10-21 09:01:38 -07003522 if (DEBUG_INPUT_METHOD) {
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003523 Slog.i(TAG_WM, "IM target uid/pid: " + imFocus.mSession.mUid
3524 + "/" + imFocus.mSession.mPid);
3525 Slog.i(TAG_WM, "Requesting client uid/pid: " + uid + "/" + pid);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003526 }
3527
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003528 return imFocus.mSession.mUid == uid && imFocus.mSession.mPid == pid;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003529 }
3530
3531 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003532 final WindowState win = getWindow(
3533 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
3534 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003535 }
3536
Tiger Huang7c610aa2018-10-27 00:01:01 +08003537 void statusBarVisibilityChanged(int visibility) {
3538 mLastStatusBarVisibility = visibility;
3539 visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(visibility);
3540 updateStatusBarVisibilityLocked(visibility);
3541 }
3542
3543 private boolean updateStatusBarVisibilityLocked(int visibility) {
3544 if (mLastDispatchedSystemUiVisibility == visibility) {
3545 return false;
3546 }
3547 final int globalDiff = (visibility ^ mLastDispatchedSystemUiVisibility)
3548 // We are only interested in differences of one of the
3549 // clearable flags...
3550 & View.SYSTEM_UI_CLEARABLE_FLAGS
3551 // ...if it has actually been cleared.
3552 & ~visibility;
3553
3554 mLastDispatchedSystemUiVisibility = visibility;
3555 if (isDefaultDisplay) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003556 mWmService.mInputManager.setSystemUiVisibility(visibility);
Tiger Huang7c610aa2018-10-27 00:01:01 +08003557 }
3558 updateSystemUiVisibility(visibility, globalDiff);
3559 return true;
3560 }
3561
Wale Ogunwale494009b82016-10-21 09:01:38 -07003562 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003563 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003564 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003565 final int curValue = w.mSystemUiVisibility;
3566 final int diff = (curValue ^ visibility) & globalDiff;
3567 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003568 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003569 w.mSeq++;
3570 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003571 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003572 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
3573 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07003574 visibility, newValue, diff);
3575 }
3576 } catch (RemoteException e) {
3577 // so sorry
3578 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003579 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003580 }
3581
Tiger Huang7c610aa2018-10-27 00:01:01 +08003582 void reevaluateStatusBarVisibility() {
3583 int visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(mLastStatusBarVisibility);
3584 if (updateStatusBarVisibilityLocked(visibility)) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003585 mWmService.mWindowPlacerLocked.requestTraversal();
Tiger Huang7c610aa2018-10-27 00:01:01 +08003586 }
3587 }
3588
Wale Ogunwale494009b82016-10-21 09:01:38 -07003589 void onWindowFreezeTimeout() {
3590 Slog.w(TAG_WM, "Window freeze timeout expired.");
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003591 mWmService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003592
3593 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07003594 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003595 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003596 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07003597 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003598 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003599 - mWmService.mDisplayFreezeTime);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003600 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003601 }, true /* traverseTopToBottom */);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003602 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003603 }
3604
3605 void waitForAllWindowsDrawn() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003606 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003607 forAllWindows(w -> {
3608 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
3609 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
3610 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003611 // Force add to mResizingWindows.
chaviw9c81e632018-07-31 11:17:52 -07003612 w.resetLastContentInsets();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003613 mWmService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003614 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003615 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003616 }
3617
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003618 // TODO: Super crazy long method that should be broken down...
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003619 void applySurfaceChangesTransaction(boolean recoveringMemory) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003620 final WindowSurfacePlacer surfacePlacer = mWmService.mWindowPlacerLocked;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003621
3622 mTmpUpdateAllDrawn.clear();
3623
3624 int repeats = 0;
3625 do {
3626 repeats++;
3627 if (repeats > 6) {
3628 Slog.w(TAG, "Animation repeat aborted after too many iterations");
3629 clearLayoutNeeded();
3630 break;
3631 }
3632
3633 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
3634 pendingLayoutChanges);
3635
wilsonshihc32538e2018-11-07 17:27:34 +08003636 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
3637 mWallpaperController.adjustWallpaperWindows();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003638 }
3639
Riddle Hsu654a6f92018-07-13 22:59:36 +08003640 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003641 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Riddle Hsu4e611772018-10-31 18:58:28 +08003642 if (updateOrientationFromAppTokens()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003643 setLayoutNeeded();
Riddle Hsu4e611772018-10-31 18:58:28 +08003644 sendNewConfiguration();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003645 }
3646 }
3647
3648 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3649 setLayoutNeeded();
3650 }
3651
3652 // FIRST LOOP: Perform a layout, if needed.
3653 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
3654 performLayout(repeats == 1, false /* updateInputWindows */);
3655 } else {
3656 Slog.w(TAG, "Layout repeat skipped after too many iterations");
3657 }
3658
3659 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
3660 pendingLayoutChanges = 0;
3661
Tiger Huang7c610aa2018-10-27 00:01:01 +08003662 mDisplayPolicy.beginPostLayoutPolicyLw();
3663 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
3664 pendingLayoutChanges |= mDisplayPolicy.finishPostLayoutPolicyLw();
3665 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
3666 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
Jorim Jaggif96c90a2018-09-26 16:55:15 +02003667 mInsetsStateController.onPostLayout();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003668 } while (pendingLayoutChanges != 0);
3669
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003670 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003671
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003672 mTmpRecoveringMemory = recoveringMemory;
3673 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01003674 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003675
Jorim Jaggif1292892018-09-10 11:58:13 +02003676 mLastHasContent = mTmpApplySurfaceChangesTransactionState.displayHasContent;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003677 mWmService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Jorim Jaggif1292892018-09-10 11:58:13 +02003678 mLastHasContent,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003679 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
3680 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003681 true /* inTraversal, must call performTraversalInTrans... below */);
3682
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003683 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
3684 if (wallpaperVisible != mLastWallpaperVisible) {
3685 mLastWallpaperVisible = wallpaperVisible;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003686 mWmService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003687 }
3688
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003689 while (!mTmpUpdateAllDrawn.isEmpty()) {
3690 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
3691 // See if any windows have been drawn, so they (and others associated with them)
3692 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07003693 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003694 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003695 }
3696
Bryce Leef3c6a472017-11-14 14:53:06 -08003697 private void updateBounds() {
Evan Rosky39b6f232018-10-30 18:35:41 -07003698 calculateBounds(mDisplayInfo, mTmpBounds);
Bryce Leef3c6a472017-11-14 14:53:06 -08003699 setBounds(mTmpBounds);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08003700 if (mPortalWindowHandle != null && mParentSurfaceControl != null) {
3701 mPortalWindowHandle.touchableRegion.getBounds(mTmpRect);
3702 if (!mTmpBounds.equals(mTmpRect)) {
3703 mPortalWindowHandle.touchableRegion.set(mTmpBounds);
3704 mPendingTransaction.setInputWindowInfo(mParentSurfaceControl, mPortalWindowHandle);
3705 }
3706 }
Bryce Leef3c6a472017-11-14 14:53:06 -08003707 }
3708
3709 // Determines the current display bounds based on the current state
Evan Rosky39b6f232018-10-30 18:35:41 -07003710 private void calculateBounds(DisplayInfo displayInfo, Rect out) {
Bryce Leef3c6a472017-11-14 14:53:06 -08003711 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Evan Rosky39b6f232018-10-30 18:35:41 -07003712 final int rotation = displayInfo.rotation;
3713 boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Bryce Leef3c6a472017-11-14 14:53:06 -08003714 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
3715 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Evan Rosky39b6f232018-10-30 18:35:41 -07003716 int width = displayInfo.logicalWidth;
Bryce Leef3c6a472017-11-14 14:53:06 -08003717 int left = (physWidth - width) / 2;
Evan Rosky39b6f232018-10-30 18:35:41 -07003718 int height = displayInfo.logicalHeight;
Bryce Leef3c6a472017-11-14 14:53:06 -08003719 int top = (physHeight - height) / 2;
3720 out.set(left, top, left + width, top + height);
3721 }
3722
3723 @Override
3724 public void getBounds(Rect out) {
Evan Rosky39b6f232018-10-30 18:35:41 -07003725 calculateBounds(mDisplayInfo, out);
Bryce Leef3c6a472017-11-14 14:53:06 -08003726 }
3727
3728 private void getBounds(Rect out, int orientation) {
3729 getBounds(out);
3730
3731 // Rotate the Rect if needed.
3732 final int currentRotation = mDisplayInfo.rotation;
3733 final int rotationDelta = deltaRotation(currentRotation, orientation);
3734 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3735 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3736 mTmpRectF.set(out);
3737 mTmpMatrix.mapRect(mTmpRectF);
3738 mTmpRectF.round(out);
3739 }
3740 }
3741
Evan Rosky730f6e82018-12-03 17:40:11 -08003742 /** @returns the orientation of the display when it's rotation is ROTATION_0. */
3743 int getNaturalOrientation() {
3744 return mBaseDisplayWidth < mBaseDisplayHeight
3745 ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
3746 }
3747
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003748 void performLayout(boolean initial, boolean updateInputWindows) {
3749 if (!isLayoutNeeded()) {
3750 return;
3751 }
3752 clearLayoutNeeded();
3753
3754 final int dw = mDisplayInfo.logicalWidth;
3755 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003756 if (DEBUG_LAYOUT) {
3757 Slog.v(TAG, "-------------------------------------");
3758 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
3759 }
3760
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003761 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3762 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00003763 // TODO: Not sure if we really need to set the rotation here since we are updating from the
3764 // display info above...
3765 mDisplayFrames.mRotation = mRotation;
Tiger Huang7c610aa2018-10-27 00:01:01 +08003766 mDisplayPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003767
Adrian Roos5251b1d2018-03-23 18:57:43 +01003768 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003769 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003770 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003771
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003772 // Used to indicate that we have processed the dream window and all additional windows are
3773 // behind it.
3774 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003775 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003776
3777 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003778 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003779
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003780 // Used to indicate that we have processed the dream window and all additional attached
3781 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003782 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003783 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003784
3785 // Now perform layout of attached windows, which usually depend on the position of the
3786 // window they are attached to. XXX does not deal with windows that are attached to windows
3787 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003788 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003789
3790 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003791 mInputMonitor.layoutInputConsumers(dw, dh);
3792 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003793 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003794 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003795 }
3796
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003797 mWmService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003798 }
3799
3800 /**
3801 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3802 * In portrait mode, it grabs the full screenshot.
3803 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003804 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003805 */
chaviw0315a1a2018-03-05 15:28:35 -08003806 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003807 if (!mWmService.mPolicy.isScreenOn()) {
chaviw0315a1a2018-03-05 15:28:35 -08003808 if (DEBUG_SCREENSHOT) {
3809 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003810 }
chaviw0315a1a2018-03-05 15:28:35 -08003811 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003812 }
chaviwfbe47df2017-11-10 16:14:49 -08003813
chaviw0315a1a2018-03-05 15:28:35 -08003814 int dw = mDisplayInfo.logicalWidth;
3815 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003816
chaviw0315a1a2018-03-05 15:28:35 -08003817 if (dw <= 0 || dh <= 0) {
3818 return null;
3819 }
chaviwfbe47df2017-11-10 16:14:49 -08003820
chaviw0315a1a2018-03-05 15:28:35 -08003821 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003822
chaviw0315a1a2018-03-05 15:28:35 -08003823 // The screenshot API does not apply the current screen rotation.
3824 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003825
chaviw0315a1a2018-03-05 15:28:35 -08003826 if (rot == ROTATION_90 || rot == ROTATION_270) {
3827 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3828 }
chaviwfbe47df2017-11-10 16:14:49 -08003829
chaviw0315a1a2018-03-05 15:28:35 -08003830 // SurfaceFlinger is not aware of orientation, so convert our logical
3831 // crop to SurfaceFlinger's portrait orientation.
3832 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3833
3834 final ScreenRotationAnimation screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003835 mWmService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
chaviw0315a1a2018-03-05 15:28:35 -08003836 final boolean inRotation = screenRotationAnimation != null &&
3837 screenRotationAnimation.isAnimating();
3838 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3839
3840 // TODO(b/68392460): We should screenshot Task controls directly
3841 // but it's difficult at the moment as the Task doesn't have the
3842 // correct size set.
chaviw08520a02018-09-10 16:44:56 -07003843 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, inRotation, rot);
chaviw0315a1a2018-03-05 15:28:35 -08003844 if (bitmap == null) {
3845 Slog.w(TAG_WM, "Failed to take screenshot");
3846 return null;
3847 }
3848
3849 // Create a copy of the screenshot that is immutable and backed in ashmem.
3850 // This greatly reduces the overhead of passing the bitmap between processes.
3851 final Bitmap ret = bitmap.createAshmemBitmap(config);
3852 bitmap.recycle();
3853 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003854 }
3855
3856 // TODO: Can this use createRotationMatrix()?
3857 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3858 if (rot == Surface.ROTATION_90) {
3859 final int tmp = crop.top;
3860 crop.top = dw - crop.right;
3861 crop.right = crop.bottom;
3862 crop.bottom = dw - crop.left;
3863 crop.left = tmp;
3864 } else if (rot == Surface.ROTATION_180) {
3865 int tmp = crop.top;
3866 crop.top = dh - crop.bottom;
3867 crop.bottom = dh - tmp;
3868 tmp = crop.right;
3869 crop.right = dw - crop.left;
3870 crop.left = dw - tmp;
3871 } else if (rot == Surface.ROTATION_270) {
3872 final int tmp = crop.top;
3873 crop.top = crop.left;
3874 crop.left = dh - crop.bottom;
3875 crop.bottom = crop.right;
3876 crop.right = dh - tmp;
3877 }
3878 }
3879
3880 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003881 // Used to indicate the layout is needed.
3882 mTmpWindow = null;
3883
3884 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003885 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003886 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003887 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003888 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003889 w.setDisplayLayoutNeeded();
Vishnu Nairba183352018-11-21 11:16:49 -08003890 w.finishSeamlessRotation(true /* timeout */);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003891 mWmService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003892 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003893
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003894 if (mTmpWindow != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003895 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003896 }
3897 }
3898
Wale Ogunwale1666e312016-12-16 11:27:18 -08003899 void setExitingTokensHasVisible(boolean hasVisible) {
3900 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3901 mExitingTokens.get(i).hasVisible = hasVisible;
3902 }
3903
3904 // Initialize state of exiting applications.
3905 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3906 }
3907
3908 void removeExistingTokensIfPossible() {
3909 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3910 final WindowToken token = mExitingTokens.get(i);
3911 if (!token.hasVisible) {
3912 mExitingTokens.remove(i);
3913 }
3914 }
3915
3916 // Time to remove any exiting applications?
3917 mTaskStackContainers.removeExistingAppTokensIfPossible();
3918 }
3919
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003920 @Override
3921 void onDescendantOverrideConfigurationChanged() {
3922 setLayoutNeeded();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003923 mWmService.requestTraversal();
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003924 }
3925
David Stevens9440dc82017-03-16 19:00:20 -07003926 boolean okToDisplay() {
3927 if (mDisplayId == DEFAULT_DISPLAY) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003928 return !mWmService.mDisplayFrozen
3929 && mWmService.mDisplayEnabled && mWmService.mPolicy.isScreenOn();
David Stevens9440dc82017-03-16 19:00:20 -07003930 }
3931 return mDisplayInfo.state == Display.STATE_ON;
3932 }
3933
3934 boolean okToAnimate() {
3935 return okToDisplay() &&
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003936 (mDisplayId != DEFAULT_DISPLAY || mWmService.mPolicy.okToAnimate());
David Stevens9440dc82017-03-16 19:00:20 -07003937 }
3938
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003939 static final class TaskForResizePointSearchResult {
3940 boolean searchDone;
3941 Task taskForResize;
3942
3943 void reset() {
3944 searchDone = false;
3945 taskForResize = null;
3946 }
3947 }
Robert Carr3b716242016-08-16 16:02:21 -07003948
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003949 private static final class ApplySurfaceChangesTransactionState {
3950 boolean displayHasContent;
3951 boolean obscured;
3952 boolean syswin;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003953 float preferredRefreshRate;
3954 int preferredModeId;
3955
3956 void reset() {
3957 displayHasContent = false;
3958 obscured = false;
3959 syswin = false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003960 preferredRefreshRate = 0;
3961 preferredModeId = 0;
3962 }
3963 }
3964
3965 private static final class ScreenshotApplicationState {
3966 WindowState appWin;
3967 int maxLayer;
3968 int minLayer;
3969 boolean screenshotReady;
3970
3971 void reset(boolean screenshotReady) {
3972 appWin = null;
3973 maxLayer = 0;
3974 minLayer = 0;
3975 this.screenshotReady = screenshotReady;
3976 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3977 }
3978 }
3979
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003980 /**
3981 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3982 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3983 * homogeneous children type which is currently required by sub-classes of
3984 * {@link WindowContainer} class.
3985 */
3986 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3987
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003988 DisplayChildWindowContainer(WindowManagerService service) {
3989 super(service);
3990 }
3991
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003992 @Override
3993 boolean fillsParent() {
3994 return true;
3995 }
3996
3997 @Override
3998 boolean isVisible() {
3999 return true;
Robert Carr3b716242016-08-16 16:02:21 -07004000 }
4001 }
4002
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004003 /**
4004 * Window container class that contains all containers on this display relating to Apps.
4005 * I.e Activities.
4006 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07004007 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07004008 /**
4009 * A control placed at the appropriate level for transitions to occur.
4010 */
chaviw23ee71c2017-12-18 11:29:41 -08004011 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02004012 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02004013 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004014
Robert Carrf7a7ca82017-12-06 13:17:07 -08004015 /**
4016 * Given that the split-screen divider does not have an AppWindowToken, it
4017 * will have to live inside of a "NonAppWindowContainer", in particular
4018 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
4019 * it will need to be interleaved with some of our children, appearing on top of
4020 * both docked stacks but underneath any assistant stacks.
4021 *
4022 * To solve this problem we have this anchor control, which will always exist so
4023 * we can always assign it the correct value in our {@link #assignChildLayers}.
4024 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
4025 * assign the divider a layer relative to it. This way we prevent linking lifecycle
4026 * events between the two containers.
4027 */
4028 SurfaceControl mSplitScreenDividerAnchor = null;
4029
Wale Ogunwale61911492017-10-11 08:50:50 -07004030 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
4031 // through the list to find them.
4032 private TaskStack mHomeStack = null;
4033 private TaskStack mPinnedStack = null;
4034 private TaskStack mSplitScreenPrimaryStack = null;
4035
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004036 TaskStackContainers(WindowManagerService service) {
4037 super(service);
4038 }
4039
Wale Ogunwale61911492017-10-11 08:50:50 -07004040 /**
4041 * Returns the topmost stack on the display that is compatible with the input windowing mode
4042 * and activity type. Null is no compatible stack on the display.
4043 */
4044 TaskStack getStack(int windowingMode, int activityType) {
4045 if (activityType == ACTIVITY_TYPE_HOME) {
4046 return mHomeStack;
4047 }
4048 if (windowingMode == WINDOWING_MODE_PINNED) {
4049 return mPinnedStack;
4050 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4051 return mSplitScreenPrimaryStack;
4052 }
4053 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
4054 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004055 if (activityType == ACTIVITY_TYPE_UNDEFINED
4056 && windowingMode == stack.getWindowingMode()) {
4057 // Passing in undefined type means we want to match the topmost stack with the
4058 // windowing mode.
4059 return stack;
4060 }
Wale Ogunwale61911492017-10-11 08:50:50 -07004061 if (stack.isCompatible(windowingMode, activityType)) {
4062 return stack;
4063 }
4064 }
4065 return null;
4066 }
4067
4068 @VisibleForTesting
4069 TaskStack getTopStack() {
4070 return mTaskStackContainers.getChildCount() > 0
4071 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
4072 }
4073
4074 TaskStack getHomeStack() {
4075 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
4076 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
4077 }
4078 return mHomeStack;
4079 }
4080
4081 TaskStack getPinnedStack() {
4082 return mPinnedStack;
4083 }
4084
Matthew Ng64e77cf2017-10-31 14:01:31 -07004085 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07004086 return mSplitScreenPrimaryStack;
4087 }
4088
Winson Chunge2d72172018-01-25 17:46:20 +00004089 ArrayList<Task> getVisibleTasks() {
4090 final ArrayList<Task> visibleTasks = new ArrayList<>();
4091 forAllTasks(task -> {
4092 if (task.isVisible()) {
4093 visibleTasks.add(task);
4094 }
4095 });
4096 return visibleTasks;
4097 }
4098
Andrii Kulian839def92016-11-02 10:58:58 -07004099 /**
4100 * Adds the stack to this container.
Andrii Kulian839def92016-11-02 10:58:58 -07004101 */
4102 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07004103 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004104 addChild(stack, onTop);
4105 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07004106 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004107
Wale Ogunwale61911492017-10-11 08:50:50 -07004108 void onStackWindowingModeChanged(TaskStack stack) {
4109 removeStackReferenceIfNeeded(stack);
4110 addStackReferenceIfNeeded(stack);
4111 if (stack == mPinnedStack && getTopStack() != stack) {
4112 // Looks like this stack changed windowing mode to pinned. Move it to the top.
4113 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
4114 }
4115 }
4116
4117 private void addStackReferenceIfNeeded(TaskStack stack) {
4118 if (stack.isActivityTypeHome()) {
4119 if (mHomeStack != null) {
Louis Chang7d0037c2018-08-13 12:42:06 +08004120 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07004121 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang7d0037c2018-08-13 12:42:06 +08004122
Wale Ogunwale61911492017-10-11 08:50:50 -07004123 }
Louis Chang7d0037c2018-08-13 12:42:06 +08004124 mHomeStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07004125 }
4126 final int windowingMode = stack.getWindowingMode();
4127 if (windowingMode == WINDOWING_MODE_PINNED) {
4128 if (mPinnedStack != null) {
4129 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
4130 + mPinnedStack + " already exist on display=" + this
4131 + " stack=" + stack);
4132 }
4133 mPinnedStack = stack;
4134 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4135 if (mSplitScreenPrimaryStack != null) {
4136 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
4137 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
4138 + " already exist on display=" + this + " stack=" + stack);
4139 }
4140 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004141 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07004142 }
4143 }
4144
4145 private void removeStackReferenceIfNeeded(TaskStack stack) {
4146 if (stack == mHomeStack) {
4147 mHomeStack = null;
4148 } else if (stack == mPinnedStack) {
4149 mPinnedStack = null;
4150 } else if (stack == mSplitScreenPrimaryStack) {
4151 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004152 // Re-set the split-screen create mode whenever the split-screen stack is removed.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004153 mWmService.setDockedStackCreateStateLocked(
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004154 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
4155 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07004156 }
Andrii Kulian839def92016-11-02 10:58:58 -07004157 }
4158
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004159 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004160 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
4161 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004162 addChild(stack, addIndex);
4163 setLayoutNeeded();
4164 }
4165
Wale Ogunwale61911492017-10-11 08:50:50 -07004166 @Override
4167 protected void removeChild(TaskStack stack) {
4168 super.removeChild(stack);
4169 removeStackReferenceIfNeeded(stack);
4170 }
Bryce Lee00d586d2017-07-28 20:48:43 -07004171
4172 @Override
4173 boolean isOnTop() {
4174 // Considered always on top
4175 return true;
4176 }
4177
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004178 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08004179 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07004180 if (child.getWindowConfiguration().isAlwaysOnTop()
4181 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08004182 // This stack is always-on-top, override the default behavior.
4183 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
4184
4185 // Moving to its current position, as we must call super but we don't want to
4186 // perform any meaningful action.
4187 final int currentPosition = mChildren.indexOf(child);
4188 super.positionChildAt(currentPosition, child, false /* includingParents */);
4189 return;
4190 }
4191
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004192 final int targetPosition = findPositionForStack(position, child, false /* adding */);
4193 super.positionChildAt(targetPosition, child, includingParents);
4194
Riddle Hsu2f6e1742018-08-23 22:44:36 +08004195 if (includingParents) {
4196 // We still want to move the display of this stack container to top because even the
4197 // target position is adjusted to non-top, the intention of the condition is to have
4198 // higher z-order to gain focus (e.g. moving a task of a fullscreen stack to front
4199 // in a non-top display which is using picture-in-picture mode).
4200 final int topChildPosition = getChildCount() - 1;
4201 if (targetPosition < topChildPosition && position >= topChildPosition) {
4202 getParent().positionChildAt(POSITION_TOP, this /* child */,
4203 true /* includingParents */);
4204 }
4205 }
4206
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004207 setLayoutNeeded();
4208 }
4209
4210 /**
4211 * When stack is added or repositioned, find a proper position for it.
4212 * This will make sure that pinned stack always stays on top.
4213 * @param requestedPosition Position requested by caller.
4214 * @param stack Stack to be added or positioned.
4215 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
4216 * @return The proper position for the stack.
4217 */
4218 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004219 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09004220 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09004221 }
4222
Kazuki Takisef85197b2018-06-18 18:18:36 +09004223 final int topChildPosition = mChildren.size() - 1;
4224 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
4225 for (int i = topChildPosition; i >= 0; --i) {
4226 if (getStacks().get(i) != stack && !getStacks().get(i).isAlwaysOnTop()) {
4227 belowAlwaysOnTopPosition = i;
4228 break;
4229 }
4230 }
4231
4232 // The max possible position we can insert the stack at.
4233 int maxPosition = POSITION_TOP;
4234 // The min possible position we can insert the stack at.
4235 int minPosition = POSITION_BOTTOM;
4236
4237 if (stack.isAlwaysOnTop()) {
4238 if (hasPinnedStack()) {
4239 // Always-on-top stacks go below the pinned stack.
4240 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
4241 }
4242 // Always-on-top stacks need to be above all other stacks.
4243 minPosition = belowAlwaysOnTopPosition !=
4244 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
4245 } else {
4246 // Other stacks need to be below the always-on-top stacks.
4247 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08004248 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09004249 }
4250
4251 int targetPosition = requestedPosition;
4252 targetPosition = Math.min(targetPosition, maxPosition);
4253 targetPosition = Math.max(targetPosition, minPosition);
4254
4255 int prevPosition = getStacks().indexOf(stack);
4256 // The positions we calculated above (maxPosition, minPosition) do not take into
4257 // consideration the following edge cases.
4258 // 1) We need to adjust the position depending on the value "adding".
4259 // 2) When we are moving a stack to another position, we also need to adjust the
4260 // position depending on whether the stack is moving to a higher or lower position.
4261 if ((targetPosition != requestedPosition) &&
4262 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004263 targetPosition++;
4264 }
4265
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004266 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08004267 }
4268
4269 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004270 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
4271 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004272 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004273 if (super.forAllWindows(callback, traverseTopToBottom)) {
4274 return true;
4275 }
4276 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4277 return true;
4278 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004279 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004280 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4281 return true;
4282 }
4283 if (super.forAllWindows(callback, traverseTopToBottom)) {
4284 return true;
4285 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004286 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004287 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004288 }
4289
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004290 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004291 boolean traverseTopToBottom) {
4292 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
4293 // app tokens.
4294 // TODO: Investigate if we need to continue to do this or if we can just process them
4295 // in-order.
4296 if (traverseTopToBottom) {
4297 for (int i = mChildren.size() - 1; i >= 0; --i) {
4298 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4299 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004300 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4301 traverseTopToBottom)) {
4302 return true;
4303 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004304 }
4305 }
4306 } else {
4307 final int count = mChildren.size();
4308 for (int i = 0; i < count; ++i) {
4309 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4310 final int appTokensCount = appTokens.size();
4311 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004312 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4313 traverseTopToBottom)) {
4314 return true;
4315 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004316 }
4317 }
4318 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004319 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004320 }
4321
Wale Ogunwale1666e312016-12-16 11:27:18 -08004322 void setExitingTokensHasVisible(boolean hasVisible) {
4323 for (int i = mChildren.size() - 1; i >= 0; --i) {
4324 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4325 for (int j = appTokens.size() - 1; j >= 0; --j) {
4326 appTokens.get(j).hasVisible = hasVisible;
4327 }
4328 }
4329 }
4330
4331 void removeExistingAppTokensIfPossible() {
4332 for (int i = mChildren.size() - 1; i >= 0; --i) {
4333 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4334 for (int j = appTokens.size() - 1; j >= 0; --j) {
4335 final AppWindowToken token = appTokens.get(j);
lumark588a3e82018-07-20 18:53:54 +08004336 if (!token.hasVisible && !mClosingApps.contains(token)
Wale Ogunwale1666e312016-12-16 11:27:18 -08004337 && (!token.mIsExiting || token.isEmpty())) {
4338 // Make sure there is no animation running on this token, so any windows
4339 // associated with it will be removed as soon as their animations are
4340 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02004341 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004342 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
4343 "performLayout: App token exiting now removed" + token);
4344 token.removeIfPossible();
4345 }
4346 }
4347 }
4348 }
4349
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004350 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004351 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004352 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
4353 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004354 // Apps and their containers are not allowed to specify an orientation while the
Wale Ogunwaled0c225c2018-06-15 14:47:46 -07004355 // docked or freeform stack is visible...except for the home stack if the docked
4356 // stack is minimized and it actually set something and the bounds is different from
4357 // the display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004358 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004359 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004360 && !(mDividerControllerLocked.isHomeStackResizable()
4361 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004362 final int orientation = mHomeStack.getOrientation();
4363 if (orientation != SCREEN_ORIENTATION_UNSET) {
4364 return orientation;
4365 }
4366 }
4367 return SCREEN_ORIENTATION_UNSPECIFIED;
4368 }
4369
4370 final int orientation = super.getOrientation();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004371 boolean isCar = mWmService.mContext.getPackageManager().hasSystemFeature(
Dean Harding3e5a1522017-10-06 09:19:01 -07004372 PackageManager.FEATURE_AUTOMOTIVE);
4373 if (isCar) {
4374 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
4375 // allow anything but the default orientation.
4376 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004377 "Forcing UNSPECIFIED orientation in car for display id=" + mDisplayId
4378 + ". Ignoring " + orientation);
Dean Harding3e5a1522017-10-06 09:19:01 -07004379 return SCREEN_ORIENTATION_UNSPECIFIED;
4380 }
4381
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004382 if (orientation != SCREEN_ORIENTATION_UNSET
4383 && orientation != SCREEN_ORIENTATION_BEHIND) {
4384 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004385 "App is requesting an orientation, return " + orientation
4386 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004387 return orientation;
4388 }
4389
4390 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004391 "No app is requesting an orientation, return " + mLastOrientation
4392 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004393 // The next app has not been requested to be visible, so we keep the current orientation
4394 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08004395 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004396 }
Robert Carrb1579c82017-09-05 14:54:47 -07004397
4398 @Override
4399 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004400 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004401
Robert Carr2f8aa392018-01-31 14:46:51 -08004402 for (int i = 0; i < mChildren.size(); i++) {
4403 final TaskStack s = mChildren.get(i);
4404 s.assignChildLayers(t);
4405 }
4406 }
4407
4408 void assignStackOrdering(SurfaceControl.Transaction t) {
Jorim Jaggibe418292018-03-26 16:14:12 +02004409
Robert Carrf7a7ca82017-12-06 13:17:07 -08004410 final int HOME_STACK_STATE = 0;
4411 final int NORMAL_STACK_STATE = 1;
4412 final int ALWAYS_ON_TOP_STATE = 2;
4413
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004414 int layer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004415 int layerForAnimationLayer = 0;
Jorim Jaggibe418292018-03-26 16:14:12 +02004416 int layerForBoostedAnimationLayer = 0;
Jorim Jaggi391790622018-04-18 15:30:44 +02004417 int layerForHomeAnimationLayer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004418
Robert Carrf7a7ca82017-12-06 13:17:07 -08004419 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
4420 for (int i = 0; i < mChildren.size(); i++) {
4421 final TaskStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08004422 if (state == HOME_STACK_STATE && !s.isActivityTypeHome()) {
4423 continue;
4424 } else if (state == NORMAL_STACK_STATE && (s.isActivityTypeHome()
4425 || s.isAlwaysOnTop())) {
4426 continue;
4427 } else if (state == ALWAYS_ON_TOP_STATE && !s.isAlwaysOnTop()) {
4428 continue;
4429 }
4430 s.assignLayer(t, layer++);
4431 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
4432 t.setLayer(mSplitScreenDividerAnchor, layer++);
4433 }
Winson Chungd41f71d2018-03-16 15:26:07 -07004434 if ((s.isTaskAnimating() || s.isAppAnimating())
4435 && state != ALWAYS_ON_TOP_STATE) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004436 // Ensure the animation layer ends up above the
4437 // highest animating stack and no higher.
4438 layerForAnimationLayer = layer++;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004439 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004440 if (state != ALWAYS_ON_TOP_STATE) {
4441 layerForBoostedAnimationLayer = layer++;
4442 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08004443 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004444 if (state == HOME_STACK_STATE) {
4445 layerForHomeAnimationLayer = layer++;
4446 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004447 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004448 if (mAppAnimationLayer != null) {
4449 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004450 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004451 if (mBoostedAppAnimationLayer != null) {
4452 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
4453 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004454 if (mHomeAppAnimationLayer != null) {
4455 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
4456 }
Robert Carrb1579c82017-09-05 14:54:47 -07004457 }
4458
4459 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02004460 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
4461 switch (animationLayer) {
4462 case ANIMATION_LAYER_BOOSTED:
4463 return mBoostedAppAnimationLayer;
4464 case ANIMATION_LAYER_HOME:
4465 return mHomeAppAnimationLayer;
4466 case ANIMATION_LAYER_STANDARD:
4467 default:
4468 return mAppAnimationLayer;
4469 }
chaviw23ee71c2017-12-18 11:29:41 -08004470 }
4471
Robert Carrf7a7ca82017-12-06 13:17:07 -08004472 SurfaceControl getSplitScreenDividerAnchor() {
4473 return mSplitScreenDividerAnchor;
4474 }
4475
chaviw23ee71c2017-12-18 11:29:41 -08004476 @Override
Riddle Hsu3a4bb612019-01-31 00:02:22 +08004477 void onParentChanged() {
4478 super.onParentChanged();
Robert Carrb1579c82017-09-05 14:54:47 -07004479 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08004480 mAppAnimationLayer = makeChildSurface(null)
4481 .setName("animationLayer")
4482 .build();
Jorim Jaggibe418292018-03-26 16:14:12 +02004483 mBoostedAppAnimationLayer = makeChildSurface(null)
4484 .setName("boostedAnimationLayer")
4485 .build();
Jorim Jaggi391790622018-04-18 15:30:44 +02004486 mHomeAppAnimationLayer = makeChildSurface(null)
4487 .setName("homeAnimationLayer")
4488 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08004489 mSplitScreenDividerAnchor = makeChildSurface(null)
4490 .setName("splitScreenDividerAnchor")
4491 .build();
4492 getPendingTransaction()
4493 .show(mAppAnimationLayer)
Jorim Jaggibe418292018-03-26 16:14:12 +02004494 .show(mBoostedAppAnimationLayer)
Jorim Jaggi391790622018-04-18 15:30:44 +02004495 .show(mHomeAppAnimationLayer)
Robert Carrf7a7ca82017-12-06 13:17:07 -08004496 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08004497 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07004498 } else {
Robert Carr5ea304d2019-02-04 16:04:55 -08004499 mAppAnimationLayer.remove();
chaviw23ee71c2017-12-18 11:29:41 -08004500 mAppAnimationLayer = null;
Robert Carr5ea304d2019-02-04 16:04:55 -08004501 mBoostedAppAnimationLayer.remove();
Jorim Jaggibe418292018-03-26 16:14:12 +02004502 mBoostedAppAnimationLayer = null;
Robert Carr5ea304d2019-02-04 16:04:55 -08004503 mHomeAppAnimationLayer.remove();
Jorim Jaggi391790622018-04-18 15:30:44 +02004504 mHomeAppAnimationLayer = null;
Robert Carr5ea304d2019-02-04 16:04:55 -08004505 mSplitScreenDividerAnchor.remove();
Robert Carrf7a7ca82017-12-06 13:17:07 -08004506 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07004507 }
4508 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004509 }
4510
Robert Carree4d4b92017-11-22 12:21:46 -08004511 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004512 AboveAppWindowContainers(String name, WindowManagerService service) {
4513 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08004514 }
4515
Robert Carrb9506032018-02-13 13:54:00 -08004516 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07004517 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
4518 final SurfaceControl.Builder builder = super.makeChildSurface(child);
4519 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
4520 // To draw above the ColorFade layer during the screen off transition, the
4521 // rounded corner overlays need to be at the root of the surface hierarchy.
4522 // TODO: move the ColorLayer into the display overlay layer such that this is not
4523 // necessary anymore.
4524 builder.setParent(null);
4525 }
4526 return builder;
4527 }
4528
4529 @Override
Robert Carrb9506032018-02-13 13:54:00 -08004530 void assignChildLayers(SurfaceControl.Transaction t) {
4531 assignChildLayers(t, null /* imeContainer */);
4532 }
4533
Robert Carree4d4b92017-11-22 12:21:46 -08004534 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
4535 boolean needAssignIme = imeContainer != null
4536 && imeContainer.getSurfaceControl() != null;
4537 for (int j = 0; j < mChildren.size(); ++j) {
4538 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004539
4540 // See {@link mSplitScreenDividerAnchor}
4541 if (wt.windowType == TYPE_DOCK_DIVIDER) {
4542 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
4543 continue;
4544 }
Vishnu Nair83537a72018-07-19 21:27:48 -07004545 if (wt.mRoundedCornerOverlay) {
4546 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
4547 continue;
4548 }
Robert Carree4d4b92017-11-22 12:21:46 -08004549 wt.assignLayer(t, j);
4550 wt.assignChildLayers(t);
4551
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004552 int layer = mWmService.mPolicy.getWindowLayerFromTypeLw(
Robert Carree4d4b92017-11-22 12:21:46 -08004553 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08004554
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004555 if (needAssignIme && layer >= mWmService.mPolicy.getWindowLayerFromTypeLw(
Robert Carrb8e78fa2017-11-28 13:07:58 -08004556 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004557 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08004558 needAssignIme = false;
4559 }
4560 }
4561 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004562 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08004563 }
4564 }
4565 }
4566
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004567 /**
4568 * Window container class that contains all containers on this display that are not related to
4569 * Apps. E.g. status bar.
4570 */
Robert Carree4d4b92017-11-22 12:21:46 -08004571 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07004572 /**
4573 * Compares two child window tokens returns -1 if the first is lesser than the second in
4574 * terms of z-order and 1 otherwise.
4575 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004576 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07004577 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004578 mWmService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004579 token1.mOwnerCanManageAppTokens)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004580 < mWmService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004581 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004582
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004583 private final Predicate<WindowState> mGetOrientingWindow = w -> {
4584 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
4585 return false;
4586 }
4587 final int req = w.mAttrs.screenOrientation;
4588 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
4589 || req == SCREEN_ORIENTATION_UNSET) {
4590 return false;
4591 }
4592 return true;
4593 };
4594
Wale Ogunwale3a931692016-11-02 16:49:48 -07004595 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08004596 private final Dimmer mDimmer = new Dimmer(this);
4597 private final Rect mTmpDimBoundsRect = new Rect();
4598
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004599 NonAppWindowContainers(String name, WindowManagerService service) {
4600 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004601 mName = name;
4602 }
4603
4604 void addChild(WindowToken token) {
4605 addChild(token, mWindowComparator);
4606 }
4607
4608 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004609 int getOrientation() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004610 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004611 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004612 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004613
4614 if (win != null) {
4615 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004616 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004617 mLastKeyguardForcedOrientation = req;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004618 if (mWmService.mKeyguardGoingAway) {
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07004619 // Keyguard can't affect the orientation if it is going away...
4620 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
4621 return SCREEN_ORIENTATION_UNSET;
4622 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004623 }
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004624 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req
4625 + " for display id=" + mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08004626 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004627 }
4628
Andrii Kulian8ee72852017-03-10 10:36:45 -08004629 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004630
Jorim Jaggi1c530592018-04-06 15:11:47 +02004631 // Only allow force setting the orientation when all unknown visibilities have been
4632 // resolved, as otherwise we just may be starting another occluding activity.
4633 final boolean isUnoccluding =
lumark588a3e82018-07-20 18:53:54 +08004634 mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
4635 && mUnknownAppVisibilityController.allResolved();
Jorim Jaggi1c530592018-04-06 15:11:47 +02004636 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004637 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004638 }
4639
4640 return SCREEN_ORIENTATION_UNSET;
4641 }
4642
4643 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07004644 String getName() {
4645 return mName;
4646 }
chaviwf29223e2018-01-31 13:23:51 -08004647
4648 @Override
4649 Dimmer getDimmer() {
4650 return mDimmer;
4651 }
4652
4653 @Override
4654 void prepareSurfaces() {
4655 mDimmer.resetDimStates();
4656 super.prepareSurfaces();
4657 getBounds(mTmpDimBoundsRect);
4658
4659 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
4660 scheduleAnimation();
4661 }
4662 }
Robert Carr3b716242016-08-16 16:02:21 -07004663 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01004664
Robert Carr9034d962018-01-04 18:27:42 -08004665 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
4666 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
4667 super(name, service);
4668 }
4669
4670 @Override
4671 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
4672 }
4673 };
4674
Robert Carrb1579c82017-09-05 14:54:47 -07004675 SurfaceControl.Builder makeSurface(SurfaceSession s) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004676 return mWmService.makeSurfaceBuilder(s)
Robert Carrb1579c82017-09-05 14:54:47 -07004677 .setParent(mWindowingLayer);
4678 }
4679
4680 @Override
4681 SurfaceSession getSession() {
4682 return mSession;
4683 }
4684
4685 @Override
4686 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07004687 SurfaceSession s = child != null ? child.getSession() : getSession();
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +00004688 final SurfaceControl.Builder b = mWmService.makeSurfaceBuilder(s).setContainerLayer();
Robert Carrf59b8dd2017-10-02 18:58:36 -07004689 if (child == null) {
4690 return b;
4691 }
4692
Robert Carree4d4b92017-11-22 12:21:46 -08004693 return b.setName(child.getName())
4694 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004695 }
4696
4697 /**
4698 * The makeSurface variants are for use by the window-container
4699 * hierarchy. makeOverlay here is a function for various non windowing
4700 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
4701 * and other potpourii.
4702 */
4703 SurfaceControl.Builder makeOverlay() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004704 return mWmService.makeSurfaceBuilder(mSession)
Robert Carrb1579c82017-09-05 14:54:47 -07004705 .setParent(mOverlayLayer);
4706 }
4707
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004708 /**
4709 * Reparents the given surface to mOverlayLayer.
4710 */
4711 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
Robert Carr10584fa2019-01-14 15:55:19 -08004712 transaction.reparent(surface, mOverlayLayer);
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004713 }
4714
Robert Carrb1579c82017-09-05 14:54:47 -07004715 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07004716 if (spec.scale != 1.0) {
4717 mMagnificationSpec = spec;
4718 } else {
4719 mMagnificationSpec = null;
4720 }
Jackal Guo818c3f12019-03-08 18:00:39 +08004721 // Re-parent IME's SurfaceControl when MagnificationSpec changed.
4722 updateImeParent();
Robert Carr24be9ab2018-04-30 17:54:53 -07004723
Robert Carrf59b8dd2017-10-02 18:58:36 -07004724 applyMagnificationSpec(getPendingTransaction(), spec);
4725 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07004726 }
4727
Robert Carr24be9ab2018-04-30 17:54:53 -07004728 void reapplyMagnificationSpec() {
4729 if (mMagnificationSpec != null) {
4730 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4731 }
4732 }
4733
Robert Carrb1579c82017-09-05 14:54:47 -07004734 @Override
Riddle Hsu3a4bb612019-01-31 00:02:22 +08004735 void onParentChanged() {
Robert Carrb1579c82017-09-05 14:54:47 -07004736 // Since we are the top of the SurfaceControl hierarchy here
4737 // we create the root surfaces explicitly rather than chaining
Riddle Hsu3a4bb612019-01-31 00:02:22 +08004738 // up as the default implementation in onParentChanged does. So we
Robert Carrb1579c82017-09-05 14:54:47 -07004739 // explicitly do NOT call super here.
4740 }
4741
4742 @Override
4743 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004744
4745 // These are layers as children of "mWindowingLayer"
4746 mBelowAppWindowsContainers.assignLayer(t, 0);
4747 mTaskStackContainers.assignLayer(t, 1);
4748 mAboveAppWindowsContainers.assignLayer(t, 2);
4749
lumarkff0ab692018-11-05 20:32:30 +08004750 final WindowState imeTarget = mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004751 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004752
Robert Carree4d4b92017-11-22 12:21:46 -08004753 // In the case where we have an IME target that is not in split-screen
4754 // mode IME assignment is easy. We just need the IME to go directly above
4755 // the target. This way children of the target will naturally go above the IME
4756 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004757 //
Robert Carree4d4b92017-11-22 12:21:46 -08004758 // In the case of split-screen windowing mode, we need to elevate the IME above the
4759 // docked divider while keeping the app itself below the docked divider, so instead
4760 // we use relative layering of the IME targets child windows, and place the
4761 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004762 //
Robert Carr234b6332018-03-20 13:38:16 -07004763 // In the case the IME target is animating, the animation Z order may be different
4764 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4765 // IME target. In this case we just layer the IME over all transitions by placing it in the
4766 // above applications layer.
4767 //
Robert Carree4d4b92017-11-22 12:21:46 -08004768 // In the case where we have no IME target we assign it where it's base layer would
4769 // place it in the AboveAppWindowContainers.
Robert Carr234b6332018-03-20 13:38:16 -07004770 if (imeTarget != null && !(imeTarget.inSplitScreenWindowingMode()
4771 || imeTarget.mToken.isAppAnimating())
Robert Carree4d4b92017-11-22 12:21:46 -08004772 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004773 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004774 // TODO: We need to use an extra level on the app surface to ensure
4775 // this is always above SurfaceView but always below attached window.
4776 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004777 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004778 }
4779
4780 // Above we have assigned layers to our children, now we ask them to assign
4781 // layers to their children.
4782 mBelowAppWindowsContainers.assignChildLayers(t);
4783 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004784 mAboveAppWindowsContainers.assignChildLayers(t,
4785 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004786 mImeWindowsContainers.assignChildLayers(t);
4787 }
4788
4789 /**
4790 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4791 * that the IME target is one of the docked applications. We'd like the docked divider to be
4792 * above both of the applications, and we'd like the IME to be above the docked divider.
4793 * However we need child windows of the applications to be above the IME (Text drag handles).
4794 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4795 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
Tiger Huang7c610aa2018-10-27 00:01:01 +08004796 * with {@link WindowState#assignLayer}
Robert Carrb1579c82017-09-05 14:54:47 -07004797 */
4798 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004799 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004800 }
4801
4802 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004803 void prepareSurfaces() {
4804 final ScreenRotationAnimation screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004805 mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004806 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
4807 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
4808 mPendingTransaction.setMatrix(mWindowingLayer,
4809 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
4810 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
4811 mPendingTransaction.setPosition(mWindowingLayer,
4812 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
4813 mPendingTransaction.setAlpha(mWindowingLayer,
4814 screenRotationAnimation.getEnterTransformation().getAlpha());
4815 }
Chavi Weingartenb736e322018-02-23 00:27:54 +00004816
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004817 super.prepareSurfaces();
4818 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004819
Jorim Jaggibe418292018-03-26 16:14:12 +02004820 void assignStackOrdering() {
4821 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004822 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004823
4824 /**
4825 * Increment the deferral count to determine whether to update the IME target.
4826 */
4827 void deferUpdateImeTarget() {
4828 mDeferUpdateImeTargetCount++;
4829 }
4830
4831 /**
4832 * Decrement the deferral count to determine whether to update the IME target. If the count
4833 * reaches 0, a new ime target will get computed.
4834 */
4835 void continueUpdateImeTarget() {
4836 if (mDeferUpdateImeTargetCount == 0) {
4837 return;
4838 }
4839
4840 mDeferUpdateImeTargetCount--;
4841 if (mDeferUpdateImeTargetCount == 0) {
4842 computeImeTarget(true /* updateImeTarget */);
4843 }
4844 }
4845
4846 /**
4847 * @return Whether a new IME target should be computed.
4848 */
4849 private boolean canUpdateImeTarget() {
4850 return mDeferUpdateImeTargetCount == 0;
4851 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004852
4853 InputMonitor getInputMonitor() {
4854 return mInputMonitor;
4855 }
Jorim Jaggif1292892018-09-10 11:58:13 +02004856
4857 /**
4858 * @return Cached value whether we told display manager that we have content.
4859 */
4860 boolean getLastHasContent() {
4861 return mLastHasContent;
4862 }
Arthur Hungbe5ce212018-09-13 18:41:56 +08004863
4864 void registerPointerEventListener(@NonNull PointerEventListener listener) {
Riddle Hsu2588ab02019-02-25 14:23:56 +08004865 mPointerEventDispatcher.registerInputEventListener(listener);
Arthur Hungbe5ce212018-09-13 18:41:56 +08004866 }
4867
4868 void unregisterPointerEventListener(@NonNull PointerEventListener listener) {
Riddle Hsu2588ab02019-02-25 14:23:56 +08004869 mPointerEventDispatcher.unregisterInputEventListener(listener);
Arthur Hungbe5ce212018-09-13 18:41:56 +08004870 }
lumark588a3e82018-07-20 18:53:54 +08004871
4872 void prepareAppTransition(@WindowManager.TransitionType int transit,
Wale Ogunwale3a256e62018-12-06 14:41:18 -08004873 boolean alwaysKeepCurrent) {
4874 prepareAppTransition(transit, alwaysKeepCurrent, 0 /* flags */, false /* forceOverride */);
4875 }
4876
4877 void prepareAppTransition(@WindowManager.TransitionType int transit,
lumark588a3e82018-07-20 18:53:54 +08004878 boolean alwaysKeepCurrent, @WindowManager.TransitionFlags int flags,
4879 boolean forceOverride) {
4880 final boolean prepared = mAppTransition.prepareAppTransitionLocked(
4881 transit, alwaysKeepCurrent, flags, forceOverride);
4882 if (prepared && okToAnimate()) {
4883 mSkipAppTransitionAnimation = false;
4884 }
4885 }
4886
4887 void executeAppTransition() {
4888 if (mAppTransition.isTransitionSet()) {
4889 if (DEBUG_APP_TRANSITIONS) {
4890 Slog.w(TAG_WM, "Execute app transition: " + mAppTransition + ", displayId: "
4891 + mDisplayId + " Callers=" + Debug.getCallers(5));
4892 }
4893 mAppTransition.setReady();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004894 mWmService.mWindowPlacerLocked.requestTraversal();
lumark588a3e82018-07-20 18:53:54 +08004895 }
4896 }
4897
4898 /**
4899 * Update pendingLayoutChanges after app transition has finished.
4900 */
4901 void handleAnimatingStoppedAndTransition() {
4902 int changes = 0;
4903
4904 mAppTransition.setIdle();
4905
4906 for (int i = mNoAnimationNotifyOnTransitionFinished.size() - 1; i >= 0; i--) {
4907 final IBinder token = mNoAnimationNotifyOnTransitionFinished.get(i);
4908 mAppTransition.notifyAppTransitionFinishedLocked(token);
4909 }
4910 mNoAnimationNotifyOnTransitionFinished.clear();
4911
4912 mWallpaperController.hideDeferredWallpapersIfNeeded();
4913
4914 onAppTransitionDone();
4915
4916 changes |= FINISH_LAYOUT_REDO_LAYOUT;
4917 if (DEBUG_WALLPAPER_LIGHT) {
4918 Slog.v(TAG_WM, "Wallpaper layer changed: assigning layers + relayout");
4919 }
4920 computeImeTarget(true /* updateImeTarget */);
wilsonshihc32538e2018-11-07 17:27:34 +08004921 mWallpaperMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08004922 // Since the window list has been rebuilt, focus might have to be recomputed since the
4923 // actual order of windows might have changed again.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004924 mWmService.mFocusMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08004925
4926 pendingLayoutChanges |= changes;
4927 }
Andrii Kulian15cfb422018-11-07 13:38:49 -08004928
Wale Ogunwale3a256e62018-12-06 14:41:18 -08004929 /** Check if pending app transition is for activity / task launch. */
4930 boolean isNextTransitionForward() {
4931 final int transit = mAppTransition.getAppTransition();
4932 return transit == TRANSIT_ACTIVITY_OPEN
4933 || transit == TRANSIT_TASK_OPEN
4934 || transit == TRANSIT_TASK_TO_FRONT;
4935 }
4936
Andrii Kulian15cfb422018-11-07 13:38:49 -08004937 /**
4938 * @see Display#FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS
4939 */
4940 boolean supportsSystemDecorations() {
Arthur Hung879a8ab2019-03-13 19:51:10 +08004941 return (mWmService.mDisplayWindowSettings.shouldShowSystemDecorsLocked(this)
Andrii Kuliandd989612019-02-21 12:13:28 -08004942 || (mDisplay.getFlags() & FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS) != 0
Arthur Hung879a8ab2019-03-13 19:51:10 +08004943 || mWmService.mForceDesktopModeOnExternalDisplays)
4944 // VR virtual display will be used to run and render 2D app within a VR experience.
4945 && mDisplayId != mWmService.mVr2dDisplayId;
Andrii Kulian15cfb422018-11-07 13:38:49 -08004946 }
chaviwff2e7d82018-11-02 11:11:27 -07004947
Tiger Huang04dc4cc2019-01-17 18:41:41 +08004948 /**
chaviwff2e7d82018-11-02 11:11:27 -07004949 * Re-parent the DisplayContent's top surfaces, {@link #mWindowingLayer} and
4950 * {@link #mOverlayLayer} to the specified surfaceControl.
4951 *
Tiger Huang04dc4cc2019-01-17 18:41:41 +08004952 * @param sc The new SurfaceControl, where the DisplayContent's surfaces will be re-parented to.
chaviwff2e7d82018-11-02 11:11:27 -07004953 */
Robert Carr10584fa2019-01-14 15:55:19 -08004954 void reparentDisplayContent(SurfaceControl sc) {
Tiger Huang04dc4cc2019-01-17 18:41:41 +08004955 mParentSurfaceControl = sc;
4956 if (mPortalWindowHandle == null) {
4957 mPortalWindowHandle = createPortalWindowHandle(sc.toString());
4958 }
4959 mPendingTransaction.setInputWindowInfo(sc, mPortalWindowHandle)
4960 .reparent(mWindowingLayer, sc).reparent(mOverlayLayer, sc);
4961 }
4962
Tarandeep Singha6f35612019-01-11 19:50:46 -08004963 @VisibleForTesting
4964 SurfaceControl getWindowingLayer() {
4965 return mWindowingLayer;
4966 }
4967
Tiger Huang04dc4cc2019-01-17 18:41:41 +08004968 /**
Adrian Roos4ffc8972019-02-07 20:45:11 +01004969 * Updates the display's system gesture exclusion.
4970 *
4971 * @return true, if the exclusion changed.
4972 */
4973 boolean updateSystemGestureExclusion() {
4974 if (mSystemGestureExclusionListeners.getRegisteredCallbackCount() == 0) {
4975 // No one's interested anyways.
4976 return false;
4977 }
4978
4979 final Region systemGestureExclusion = calculateSystemGestureExclusion();
4980 try {
4981 if (mSystemGestureExclusion.equals(systemGestureExclusion)) {
4982 return false;
4983 }
4984 mSystemGestureExclusion.set(systemGestureExclusion);
4985 for (int i = mSystemGestureExclusionListeners.beginBroadcast() - 1; i >= 0; --i) {
4986 try {
4987 mSystemGestureExclusionListeners.getBroadcastItem(i)
4988 .onSystemGestureExclusionChanged(mDisplayId, systemGestureExclusion);
4989 } catch (RemoteException e) {
4990 Slog.e(TAG, "Failed to notify SystemGestureExclusionListener", e);
4991 }
4992 }
4993 mSystemGestureExclusionListeners.finishBroadcast();
4994 return true;
4995 } finally {
4996 systemGestureExclusion.recycle();
4997 }
4998 }
4999
5000 @VisibleForTesting
5001 Region calculateSystemGestureExclusion() {
5002 final Region global = Region.obtain();
5003 final Region touchableRegion = Region.obtain();
5004 final Region local = Region.obtain();
5005
5006 // Traverse all windows bottom up to assemble the gesture exclusion rects.
5007 // For each window, we only take the rects that fall within its touchable region.
5008 forAllWindows(w -> {
5009 if (w.cantReceiveTouchInput() || !w.isVisible()
5010 || (w.getAttrs().flags & FLAG_NOT_TOUCHABLE) != 0) {
5011 return;
5012 }
5013 final boolean modal =
5014 (w.mAttrs.flags & (FLAG_NOT_TOUCH_MODAL | FLAG_NOT_FOCUSABLE)) == 0;
5015
5016 // Only keep the exclusion zones from the windows behind where the current window
5017 // isn't touchable.
5018 w.getTouchableRegion(touchableRegion);
5019 global.op(touchableRegion, Op.DIFFERENCE);
5020
5021 rectListToRegion(w.getSystemGestureExclusion(), local);
5022
5023 // Transform to display coordinates
5024 local.scale(w.mGlobalScale);
5025 final Rect frame = w.getWindowFrames().mFrame;
5026 local.translate(frame.left, frame.top);
5027
5028 // A window can only exclude system gestures where it is actually touchable
5029 local.op(touchableRegion, Op.INTERSECT);
5030
5031 global.op(local, Op.UNION);
5032 }, false /* topToBottom */);
5033 local.recycle();
5034 touchableRegion.recycle();
5035 return global;
5036 }
5037
5038 void registerSystemGestureExclusionListener(ISystemGestureExclusionListener listener) {
5039 mSystemGestureExclusionListeners.register(listener);
5040 final boolean changed;
5041 if (mSystemGestureExclusionListeners.getRegisteredCallbackCount() == 1) {
5042 changed = updateSystemGestureExclusion();
5043 } else {
5044 changed = false;
5045 }
5046
5047 if (!changed) {
5048 // If updateSystemGestureExclusion changed the exclusion, it will already have
5049 // notified the listener. Otherwise, we'll do it here.
5050 try {
5051 listener.onSystemGestureExclusionChanged(mDisplayId, mSystemGestureExclusion);
5052 } catch (RemoteException e) {
5053 Slog.e(TAG, "Failed to notify SystemGestureExclusionListener during register", e);
5054 }
5055 }
5056 }
5057
5058 void unregisterSystemGestureExclusionListener(ISystemGestureExclusionListener listener) {
5059 mSystemGestureExclusionListeners.unregister(listener);
5060 }
5061
5062 /**
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005063 * Create a portal window handle for input. This window transports any touch to the display
5064 * indicated by {@link InputWindowHandle#portalToDisplayId} if the touch hits this window.
5065 *
5066 * @param name The name of the portal window handle.
5067 * @return the new portal window handle.
5068 */
5069 private InputWindowHandle createPortalWindowHandle(String name) {
5070 // Let surface flinger to set the display ID of this input window handle because we don't
5071 // know which display the parent surface control is on.
5072 final InputWindowHandle portalWindowHandle = new InputWindowHandle(
5073 null /* inputApplicationHandle */, null /* clientWindow */, INVALID_DISPLAY);
5074 portalWindowHandle.name = name;
5075 portalWindowHandle.token = new Binder();
5076 portalWindowHandle.layoutParamsFlags =
5077 FLAG_SPLIT_TOUCH | FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL;
5078 getBounds(mTmpBounds);
5079 portalWindowHandle.touchableRegion.set(mTmpBounds);
5080 portalWindowHandle.scaleFactor = 1f;
5081 portalWindowHandle.ownerPid = Process.myPid();
5082 portalWindowHandle.ownerUid = Process.myUid();
5083 portalWindowHandle.portalToDisplayId = mDisplayId;
5084 return portalWindowHandle;
chaviwff2e7d82018-11-02 11:11:27 -07005085 }
Issei Suzukia5dbf522019-02-01 17:58:15 +01005086
5087 /**
5088 * @see IWindowManager#setForwardedInsets
5089 */
5090 public void setForwardedInsets(Insets insets) {
5091 if (insets == null) {
5092 insets = Insets.NONE;
5093 }
5094 if (mDisplayPolicy.getForwardedInsets().equals(insets)) {
5095 return;
5096 }
5097 mDisplayPolicy.setForwardedInsets(insets);
5098 setLayoutNeeded();
5099 mWmService.mWindowPlacerLocked.requestTraversal();
5100 }
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05005101
5102 protected MetricsLogger getMetricsLogger() {
5103 if (mMetricsLogger == null) {
5104 mMetricsLogger = new MetricsLogger();
5105 }
5106 return mMetricsLogger;
5107 }
Craig Mautner59c00972012-07-30 12:10:24 -07005108}