blob: e099a4f0c91c8ddb516c9f24bd0b8162b7a7b564 [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;
Tiger Huang86e6d072019-05-02 20:23:47 +080029import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_USER;
Wale Ogunwale687b4272017-07-27 02:56:23 -070030import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
31import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Jorim Jaggi4981f152019-03-26 18:58:45 +010032import static android.os.Trace.TRACE_TAG_WINDOW_MANAGER;
Adrian Roos506267d2019-06-18 16:13:53 +020033import static android.util.DisplayMetrics.DENSITY_DEFAULT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070034import static android.view.Display.DEFAULT_DISPLAY;
35import static android.view.Display.FLAG_PRIVATE;
Andrii Kuliandd989612019-02-21 12:13:28 -080036import static android.view.Display.FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS;
Tiger Huang04dc4cc2019-01-17 18:41:41 +080037import static android.view.Display.INVALID_DISPLAY;
chaviwff2e7d82018-11-02 11:11:27 -070038import static android.view.InsetsState.TYPE_IME;
Adrian Roos506267d2019-06-18 16:13:53 +020039import static android.view.InsetsState.TYPE_LEFT_GESTURES;
40import static android.view.InsetsState.TYPE_RIGHT_GESTURES;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070041import static android.view.Surface.ROTATION_0;
42import static android.view.Surface.ROTATION_180;
43import static android.view.Surface.ROTATION_270;
44import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070045import static android.view.View.GONE;
Adrian Roos506267d2019-06-18 16:13:53 +020046import static android.view.View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
47import static android.view.View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
Wale Ogunwale10124582016-09-15 20:25:50 -070048import static android.view.WindowManager.DOCKED_BOTTOM;
49import static android.view.WindowManager.DOCKED_INVALID;
50import static android.view.WindowManager.DOCKED_TOP;
Winson Chungc5fe7ff2019-02-19 14:49:25 -080051import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080052import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
53import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
54import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070055import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070056import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Tiger Huang04dc4cc2019-01-17 18:41:41 +080057import static android.view.WindowManager.LayoutParams.FLAG_SPLIT_TOUCH;
Winson Chungc5fe7ff2019-02-19 14:49:25 -080058import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070059import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
60import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070061import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070062import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070063import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070064import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Robert Carrf7a7ca82017-12-06 13:17:07 -080065import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
Wale Ogunwale494009b82016-10-21 09:01:38 -070066import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070067import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070068import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
69import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Adrian Roos5f2c9a12019-07-03 18:31:46 +020070import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR;
Wale Ogunwale494009b82016-10-21 09:01:38 -070071import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070072import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
73import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070074import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070075import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale3a256e62018-12-06 14:41:18 -080076import static android.view.WindowManager.TRANSIT_ACTIVITY_OPEN;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080077import static android.view.WindowManager.TRANSIT_KEYGUARD_UNOCCLUDE;
Wale Ogunwale3a256e62018-12-06 14:41:18 -080078import static android.view.WindowManager.TRANSIT_TASK_OPEN;
79import static android.view.WindowManager.TRANSIT_TASK_TO_FRONT;
Tiger Huang51c5a1d2018-12-11 20:24:51 +080080
Adrian Roose99bc052017-11-20 17:55:31 +010081import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
82import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
83import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
84import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080085import static com.android.server.wm.DisplayContentProto.ABOVE_APP_WINDOWS;
lumark588a3e82018-07-20 18:53:54 +080086import static com.android.server.wm.DisplayContentProto.APP_TRANSITION;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080087import static com.android.server.wm.DisplayContentProto.BELOW_APP_WINDOWS;
Louis Changa9350fe2019-04-25 17:14:20 +080088import static com.android.server.wm.DisplayContentProto.CHANGING_APPS;
89import static com.android.server.wm.DisplayContentProto.CLOSING_APPS;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080090import static com.android.server.wm.DisplayContentProto.DISPLAY_FRAMES;
91import static com.android.server.wm.DisplayContentProto.DISPLAY_INFO;
92import static com.android.server.wm.DisplayContentProto.DOCKED_STACK_DIVIDER_CONTROLLER;
93import static com.android.server.wm.DisplayContentProto.DPI;
Tiger Huang1e5b10a2018-07-30 20:19:51 +080094import static com.android.server.wm.DisplayContentProto.FOCUSED_APP;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080095import static com.android.server.wm.DisplayContentProto.ID;
96import static com.android.server.wm.DisplayContentProto.IME_WINDOWS;
Louis Changa9350fe2019-04-25 17:14:20 +080097import static com.android.server.wm.DisplayContentProto.OPENING_APPS;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080098import static com.android.server.wm.DisplayContentProto.PINNED_STACK_CONTROLLER;
99import static com.android.server.wm.DisplayContentProto.ROTATION;
100import static com.android.server.wm.DisplayContentProto.SCREEN_ROTATION_ANIMATION;
101import static com.android.server.wm.DisplayContentProto.STACKS;
102import static com.android.server.wm.DisplayContentProto.WINDOW_CONTAINER;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800103import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
lumark588a3e82018-07-20 18:53:54 +0800104import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_APP_TRANSITIONS;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700105import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -0700106import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -0700107import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
108import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700109import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700110import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700111import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +0200112import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700113import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700114import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -0700115import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800116import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700117import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700118import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700119import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700120import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800121import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700122import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Tiger Huang51c5a1d2018-12-11 20:24:51 +0800123import static com.android.server.wm.WindowManagerService.H.REPORT_FOCUS_CHANGE;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800124import static com.android.server.wm.WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800125import static com.android.server.wm.WindowManagerService.H.REPORT_LOSING_FOCUS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700126import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
127import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700128import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700129import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700130import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
131import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800132import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_PLACING_SURFACES;
133import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_REMOVING_FOCUS;
134import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_ASSIGN_LAYERS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700135import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700136import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700137import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700138import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700139import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700140import static com.android.server.wm.WindowManagerService.logSurface;
Adrian Roos5f2c9a12019-07-03 18:31:46 +0200141import static com.android.server.wm.WindowState.EXCLUSION_LEFT;
142import static com.android.server.wm.WindowState.EXCLUSION_RIGHT;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700143import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700144import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700145import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Adrian Roos2351d5f2019-07-03 15:35:07 +0200146import static com.android.server.wm.utils.RegionUtils.forEachRectReverse;
Adrian Roos4ffc8972019-02-07 20:45:11 +0100147import static com.android.server.wm.utils.RegionUtils.rectListToRegion;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700148
lumark588a3e82018-07-20 18:53:54 +0800149import android.animation.AnimationHandler;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700150import android.annotation.CallSuper;
Riddle Hsuf53da812018-08-15 22:00:27 +0800151import android.annotation.IntDef;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700152import android.annotation.NonNull;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200153import android.annotation.Nullable;
Dean Harding3e5a1522017-10-06 09:19:01 -0700154import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700155import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700156import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700157import android.graphics.Bitmap;
Issei Suzukia5dbf522019-02-01 17:58:15 +0100158import android.graphics.Insets;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700159import android.graphics.Matrix;
Tiger Huangd8ec9382019-04-18 14:35:09 -0700160import android.graphics.Point;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800161import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700162import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700163import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100164import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700165import android.hardware.display.DisplayManagerInternal;
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500166import android.metrics.LogMaker;
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800167import android.os.Binder;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700168import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700169import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700170import android.os.IBinder;
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800171import android.os.Process;
Adrian Roos4ffc8972019-02-07 20:45:11 +0100172import android.os.RemoteCallbackList;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700173import android.os.RemoteException;
174import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100175import android.os.Trace;
Riddle Hsuf53da812018-08-15 22:00:27 +0800176import android.os.UserHandle;
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800177import android.util.ArraySet;
Chong Zhang8e89b312015-09-09 15:09:30 -0700178import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700179import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700180import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700181import android.view.Display;
Adrian Roos1cf585052018-01-03 18:43:27 +0100182import android.view.DisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700183import android.view.DisplayInfo;
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800184import android.view.Gravity;
Adrian Roos4ffc8972019-02-07 20:45:11 +0100185import android.view.ISystemGestureExclusionListener;
Arthur Hungbe5ce212018-09-13 18:41:56 +0800186import android.view.InputChannel;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700187import android.view.InputDevice;
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800188import android.view.InputWindowHandle;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200189import android.view.InsetsState.InternalInsetType;
Robert Carrb1579c82017-09-05 14:54:47 -0700190import android.view.MagnificationSpec;
Evan Rosky966759f2019-01-15 10:33:58 -0800191import android.view.RemoteAnimationDefinition;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700192import android.view.Surface;
193import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100194import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700195import android.view.SurfaceSession;
Tiger Huang7c610aa2018-10-27 00:01:01 +0800196import android.view.View;
lumark588a3e82018-07-20 18:53:54 +0800197import android.view.WindowManager;
Evan Rosky39b6f232018-10-30 18:35:41 -0700198import android.view.WindowManagerPolicyConstants.PointerEventListener;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700199
Bryce Lee48f4b572017-04-10 10:54:15 -0700200import com.android.internal.annotations.VisibleForTesting;
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500201import com.android.internal.logging.MetricsLogger;
202import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800203import com.android.internal.util.ToBooleanFunction;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200204import com.android.internal.util.function.TriConsumer;
Winson Chungc5fe7ff2019-02-19 14:49:25 -0800205import com.android.internal.util.function.pooled.PooledConsumer;
206import com.android.internal.util.function.pooled.PooledLambda;
Winson Chung6ba767a2018-12-13 18:20:11 -0800207import com.android.server.AnimationThread;
Adrian Roose99bc052017-11-20 17:55:31 +0100208import com.android.server.policy.WindowManagerPolicy;
Issei Suzuki43190bd2018-08-20 17:28:41 +0200209import com.android.server.wm.utils.DisplayRotationUtil;
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100210import com.android.server.wm.utils.RotationCache;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100211import com.android.server.wm.utils.WmDisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700212
213import java.io.PrintWriter;
Riddle Hsuf53da812018-08-15 22:00:27 +0800214import java.lang.annotation.Retention;
215import java.lang.annotation.RetentionPolicy;
Craig Mautner59c00972012-07-30 12:10:24 -0700216import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700217import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700218import java.util.HashMap;
219import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700220import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700221import java.util.List;
Adrian Roos1cf585052018-01-03 18:43:27 +0100222import java.util.Objects;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800223import java.util.function.Consumer;
224import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700225
Craig Mautner59c00972012-07-30 12:10:24 -0700226/**
227 * Utility class for keeping track of the WindowStates and other pertinent contents of a
228 * particular Display.
Craig Mautner59c00972012-07-30 12:10:24 -0700229 */
Riddle Hsuad256a12018-07-18 16:11:30 +0800230class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer>
231 implements WindowManagerPolicy.DisplayContentInfo {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700232 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700233
Riddle Hsuf53da812018-08-15 22:00:27 +0800234 /** The default scaling mode that scales content automatically. */
235 static final int FORCE_SCALING_MODE_AUTO = 0;
236 /** For {@link #setForcedScalingMode} to apply flag {@link Display#FLAG_SCALING_DISABLED}. */
237 static final int FORCE_SCALING_MODE_DISABLED = 1;
238
239 @IntDef(prefix = { "FORCE_SCALING_MODE_" }, value = {
240 FORCE_SCALING_MODE_AUTO,
241 FORCE_SCALING_MODE_DISABLED
242 })
243 @Retention(RetentionPolicy.SOURCE)
244 @interface ForceScalingMode {}
245
Craig Mautner59c00972012-07-30 12:10:24 -0700246 /** Unique identifier of this stack. */
247 private final int mDisplayId;
248
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800249 // TODO: Remove once unification is complete.
250 ActivityDisplay mAcitvityDisplay;
251
Wale Ogunwale3a931692016-11-02 16:49:48 -0700252 /** The containers below are the only child containers the display can have. */
253 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800254 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700255 // Contains all non-app window containers that should be displayed above the app containers
256 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800257 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800258 new AboveAppWindowContainers("mAboveAppWindowsContainers", mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700259 // Contains all non-app window containers that should be displayed below the app containers
260 // (e.g. Wallpaper).
261 private final NonAppWindowContainers mBelowAppWindowsContainers =
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800262 new NonAppWindowContainers("mBelowAppWindowsContainers", mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700263 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
264 // 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 -0800265 // window containers together and move them in-sync if/when needed. We use a subclass of
266 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
Eugene Susla25a24952019-04-08 13:57:44 -0700267 private final NonAppWindowContainers mImeWindowsContainers =
268 new NonAppWindowContainers("mImeWindowsContainers", mWmService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700269
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000270 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800271 private WindowState mTmpWindow2;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800272 private boolean mTmpRecoveringMemory;
273 private boolean mUpdateImeTarget;
274 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700275 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700276
lumark588a3e82018-07-20 18:53:54 +0800277 final AppTransition mAppTransition;
278 final AppTransitionController mAppTransitionController;
279 boolean mSkipAppTransitionAnimation = false;
280
281 final ArraySet<AppWindowToken> mOpeningApps = new ArraySet<>();
282 final ArraySet<AppWindowToken> mClosingApps = new ArraySet<>();
Evan Rosky2289ba12018-11-19 18:28:18 -0800283 final ArraySet<AppWindowToken> mChangingApps = new ArraySet<>();
lumark588a3e82018-07-20 18:53:54 +0800284 final UnknownAppVisibilityController mUnknownAppVisibilityController;
285 BoundsAnimationController mBoundsAnimationController;
286
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500287 private MetricsLogger mMetricsLogger;
288
lumark588a3e82018-07-20 18:53:54 +0800289 /**
290 * List of clients without a transtiton animation that we notify once we are done
291 * transitioning since they won't be notified through the app window animator.
292 */
293 final List<IBinder> mNoAnimationNotifyOnTransitionFinished = new ArrayList<>();
294
Wale Ogunwale02319a62016-09-26 15:21:22 -0700295 // Mapping from a token IBinder to a WindowToken object on this display.
296 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
297
Andrii Kuliancd097992017-03-23 18:31:59 -0700298 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700299 int mInitialDisplayWidth = 0;
300 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700301 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700302
Adrian Roos1cf585052018-01-03 18:43:27 +0100303 DisplayCutout mInitialDisplayCutout;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100304 private final RotationCache<DisplayCutout, WmDisplayCutout> mDisplayCutoutCache
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100305 = new RotationCache<>(this::calculateDisplayCutoutForRotationUncached);
Adrian Roos1cf585052018-01-03 18:43:27 +0100306
Andrii Kuliancd097992017-03-23 18:31:59 -0700307 /**
308 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
309 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
310 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
311 */
Craig Mautner59c00972012-07-30 12:10:24 -0700312 int mBaseDisplayWidth = 0;
313 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700314 /**
315 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
316 * but can be set from Settings or via shell command "adb shell wm density".
317 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
318 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700319 int mBaseDisplayDensity = 0;
Riddle Hsuf53da812018-08-15 22:00:27 +0800320
321 /**
322 * Whether to disable display scaling. This can be set via shell command "adb shell wm scaling".
323 * @see WindowManagerService#setForcedDisplayScalingMode(int, int)
324 */
Jeff Brownd46747a2015-04-15 19:02:36 -0700325 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700326 private final DisplayInfo mDisplayInfo = new DisplayInfo();
327 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700328 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800329 private final DisplayPolicy mDisplayPolicy;
330 private DisplayRotation mDisplayRotation;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000331 DisplayFrames mDisplayFrames;
332
Adrian Roos4ffc8972019-02-07 20:45:11 +0100333 private final RemoteCallbackList<ISystemGestureExclusionListener>
334 mSystemGestureExclusionListeners = new RemoteCallbackList<>();
335 private final Region mSystemGestureExclusion = new Region();
Adrian Roos5f2c9a12019-07-03 18:31:46 +0200336 private boolean mSystemGestureExclusionWasRestricted = false;
337 private final Region mSystemGestureExclusionUnrestricted = new Region();
Adrian Roos506267d2019-06-18 16:13:53 +0200338 private int mSystemGestureExclusionLimit;
Adrian Roos4ffc8972019-02-07 20:45:11 +0100339
Andrii Kulian06d07d62017-03-14 11:11:47 -0700340 /**
341 * For default display it contains real metrics, empty for others.
342 * @see WindowManagerService#createWatermarkInTransaction()
343 */
344 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800345
346 /** @see #computeCompatSmallestWidth(boolean, int, int, int, DisplayCutout) */
Andrii Kulian06d07d62017-03-14 11:11:47 -0700347 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700348
Andrii Kulian06d07d62017-03-14 11:11:47 -0700349 /**
350 * Compat metrics computed based on {@link #mDisplayMetrics}.
351 * @see #updateDisplayAndOrientation(int)
352 */
353 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
354
355 /** The desired scaling factor for compatible apps. */
356 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700357
Andrii Kulian8ee72852017-03-10 10:36:45 -0800358 /**
359 * Current rotation of the display.
360 * Constants as per {@link android.view.Surface.Rotation}.
361 *
Robert Carrae606b42018-02-15 15:36:23 -0800362 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800363 */
364 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700365
Andrii Kulian8ee72852017-03-10 10:36:45 -0800366 /**
367 * Last applied orientation of the display.
368 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
369 *
Riddle Hsu4e611772018-10-31 18:58:28 +0800370 * @see #updateOrientationFromAppTokens()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800371 */
372 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700373
Andrii Kulian8ee72852017-03-10 10:36:45 -0800374 /**
Andrii Kulian8ee72852017-03-10 10:36:45 -0800375 * Orientation forced by some window. If there is no visible window that specifies orientation
376 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
377 *
378 * @see NonAppWindowContainers#getOrientation()
379 */
380 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700381
Andrii Kulian8ee72852017-03-10 10:36:45 -0800382 /**
383 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
384 * occluded.
385 *
386 * @see NonAppWindowContainers#getOrientation()
387 */
388 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
389
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700390 /**
Tiger Huang86e6d072019-05-02 20:23:47 +0800391 * The maximum aspect ratio (longerSide/shorterSide) that is treated as close-to-square. The
392 * orientation requests from apps would be ignored if the display is close-to-square.
393 */
394 @VisibleForTesting
395 final float mCloseToSquareMaxAspectRatio;
396
397 /**
398 * If this is true, we would not rotate the display for apps. The rotation would be either the
399 * sensor rotation or the user rotation, controlled by
400 * {@link WindowManagerPolicy.UserRotationMode}.
401 */
402 private boolean mIgnoreRotationForApps;
403
404 /**
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700405 * Keep track of wallpaper visibility to notify changes.
406 */
407 private boolean mLastWallpaperVisible = false;
408
Andrii Kulian06d07d62017-03-14 11:11:47 -0700409 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700410
Craig Mautner39834192012-09-02 07:47:24 -0700411 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700412 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700413 int pendingLayoutChanges;
Riddle Hsu654a6f92018-07-13 22:59:36 +0800414 int mDeferredRotationPauseCount;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800415
Vishnu Nairba183352018-11-21 11:16:49 -0800416 /**
417 * Used to gate application window layout until we have sent the complete configuration.
418 * TODO: There are still scenarios where we may be out of sync with the client. Ideally
419 * we want to replace this flag with a mechanism that will confirm the configuration
420 * applied by the client is the one expected by the system server.
421 */
422 boolean mWaitingForConfig;
423
Andrii Kulian839def92016-11-02 10:58:58 -0700424 // TODO(multi-display): remove some of the usages.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800425 @VisibleForTesting
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800426 boolean isDefaultDisplay;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800427
Andrii Kulianf0379de2018-03-14 16:24:07 -0700428 /**
429 * Flag indicating whether WindowManager should override info for this display in
430 * DisplayManager.
431 */
432 boolean mShouldOverrideDisplayConfiguration = true;
Craig Mautner39834192012-09-02 07:47:24 -0700433
Craig Mautnerdc548482014-02-05 13:35:24 -0800434 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700435 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800436
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700437 /** Detect user tapping outside of current focused task bounds .*/
Riddle Hsu2588ab02019-02-25 14:23:56 +0800438 @VisibleForTesting
439 final TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700440
Craig Mautner6601b7b2013-04-29 10:29:11 -0700441 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700442 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700443
Craig Mautner6601b7b2013-04-29 10:29:11 -0700444 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800445 private final Rect mTmpRect = new Rect();
446 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700447 private final RectF mTmpRectF = new RectF();
448 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800449 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700450
Bryce Leef3c6a472017-11-14 14:53:06 -0800451 /** Used for handing back size of display */
452 private final Rect mTmpBounds = new Rect();
453
Garfield Tan90b04282018-12-11 14:04:42 -0800454 private final Configuration mTmpConfiguration = new Configuration();
455
Craig Mautner95da1082014-02-24 17:54:35 -0800456 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700457 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800458
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700459 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700460 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700461
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800462 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800463 /** A collection of windows that provide tap exclude regions inside of them. */
464 final ArraySet<WindowState> mTapExcludeProvidingWindows = new ArraySet<>();
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800465
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000466 private boolean mHaveBootMsg = false;
467 private boolean mHaveApp = false;
468 private boolean mHaveWallpaper = false;
469 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700470
471 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
472
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700473 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
474 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000475 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
476 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700477
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700478 // True if this display is in the process of being removed. Used to determine if the removal of
479 // the display's direct children should be allowed.
480 private boolean mRemovingDisplay = false;
481
Bryce Leed1871262017-06-12 14:12:29 -0700482 // {@code false} if this display is in the processing of being created.
483 private boolean mDisplayReady = false;
484
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800485 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700486
wilsonshihc32538e2018-11-07 17:27:34 +0800487 boolean mWallpaperMayChange = false;
488
Robert Carrb1579c82017-09-05 14:54:47 -0700489 private final SurfaceSession mSession = new SurfaceSession();
490
491 /**
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800492 * Window that is currently interacting with the user. This window is responsible for receiving
493 * key events and pointer events from the user.
494 */
495 WindowState mCurrentFocus = null;
496
497 /**
498 * The last focused window that we've notified the client that the focus is changed.
499 */
500 WindowState mLastFocus = null;
501
502 /**
503 * Windows that have lost input focus and are waiting for the new focus window to be displayed
504 * before they are told about this.
505 */
506 ArrayList<WindowState> mLosingFocus = new ArrayList<>();
507
508 /**
509 * The foreground app of this display. Windows below this app cannot be the focused window. If
510 * the user taps on the area outside of the task of the focused app, we will notify AM about the
511 * new task the user wants to interact with.
512 */
513 AppWindowToken mFocusedApp = null;
514
515 /** Windows added since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
516 final ArrayList<WindowState> mWinAddedSinceNullFocus = new ArrayList<>();
517
518 /** Windows removed since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
519 final ArrayList<WindowState> mWinRemovedSinceNullFocus = new ArrayList<>();
520
521 /**
Robert Carrb1579c82017-09-05 14:54:47 -0700522 * We organize all top-level Surfaces in to the following layers.
523 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800524 * and omitted from Screen-Magnification, for example the strict mode flash or
525 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700526 * {@link #mWindowingLayer} contains everything else.
527 */
528 private SurfaceControl mOverlayLayer;
529
530 /**
531 * See {@link #mOverlayLayer}
532 */
533 private SurfaceControl mWindowingLayer;
534
535 /**
Adrian Roos5251b1d2018-03-23 18:57:43 +0100536 * Sequence number for the current layout pass.
537 */
538 int mLayoutSeq = 0;
539
Chavi Weingarten3a748552018-05-14 17:32:42 +0000540 /**
541 * Specifies the count to determine whether to defer updating the IME target until ready.
542 */
543 private int mDeferUpdateImeTargetCount;
544
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100545 /** Temporary float array to retrieve 3x3 matrix values. */
546 private final float[] mTmpFloats = new float[9];
547
Robert Carr24be9ab2018-04-30 17:54:53 -0700548 private MagnificationSpec mMagnificationSpec;
549
Arthur Hung95b38a92018-07-20 18:56:12 +0800550 private InputMonitor mInputMonitor;
551
Jorim Jaggif1292892018-09-10 11:58:13 +0200552 /** Caches the value whether told display manager that we have content. */
553 private boolean mLastHasContent;
554
Issei Suzuki43190bd2018-08-20 17:28:41 +0200555 private DisplayRotationUtil mRotationUtil = new DisplayRotationUtil();
556
lumark90120a82018-08-15 00:33:03 +0800557 /**
558 * The input method window for this display.
559 */
560 WindowState mInputMethodWindow;
561
lumarkff0ab692018-11-05 20:32:30 +0800562 /**
563 * This just indicates the window the input method is on top of, not
564 * necessarily the window its input is going to.
565 */
566 WindowState mInputMethodTarget;
567
568 /** If true hold off on modifying the animation layer of mInputMethodTarget */
569 boolean mInputMethodTargetWaitingAnim;
570
Arthur Hungbe5ce212018-09-13 18:41:56 +0800571 private final PointerEventDispatcher mPointerEventDispatcher;
572
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200573 private final InsetsStateController mInsetsStateController;
574
Tiger Huangd8ec9382019-04-18 14:35:09 -0700575 /** @see #getParentWindow() */
576 private WindowState mParentWindow;
577
578 private Point mLocationInParentWindow = new Point();
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800579 private SurfaceControl mParentSurfaceControl;
580 private InputWindowHandle mPortalWindowHandle;
581
Tiger Huang7c610aa2018-10-27 00:01:01 +0800582 // Last systemUiVisibility we received from status bar.
583 private int mLastStatusBarVisibility = 0;
584 // Last systemUiVisibility we dispatched to windows.
585 private int mLastDispatchedSystemUiVisibility = 0;
586
Tiger Huang43b8fc22019-04-26 11:49:29 +0800587 /** Corner radius that windows should have in order to match the display. */
588 private final float mWindowCornerRadius;
589
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800590 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
591 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800592 final AppWindowToken atoken = w.mAppToken;
593 if (winAnimator.mDrawState == READY_TO_SHOW) {
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +0200594 if (atoken == null || atoken.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800595 if (w.performShowLocked()) {
596 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
597 if (DEBUG_LAYOUT_REPEATS) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800598 mWmService.mWindowPlacerLocked.debugLayoutRepeats(
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800599 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
600 }
601 }
602 }
603 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800604 };
605
606 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
607 final WindowStateAnimator winAnimator = w.mWinAnimator;
608 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
609 return;
610 }
611
Jorim Jaggi8f520872018-08-14 17:00:20 +0200612 // If this window is animating, ensure the animation background is set.
613 final AnimationAdapter anim = w.mAppToken != null
614 ? w.mAppToken.getAnimation()
615 : w.getAnimation();
616 if (anim != null) {
617 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800618 if (color != 0) {
619 final TaskStack stack = w.getStack();
620 if (stack != null) {
621 stack.setAnimationBackground(winAnimator, color);
622 }
623 }
624 }
625 };
626
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800627 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
628 final int lostFocusUid = mTmpWindow.mOwnerUid;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800629 final Handler handler = mWmService.mH;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800630 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
631 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
632 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
633 w.mAttrs.hideTimeoutMilliseconds);
634 }
635 }
636 };
637
638 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800639 final AppWindowToken focusedApp = mFocusedApp;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800640 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
641 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
642
643 if (!w.canReceiveKeys()) {
644 return false;
645 }
646
647 final AppWindowToken wtoken = w.mAppToken;
648
649 // If this window's application has been removed, just skip it.
650 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
651 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
652 + (wtoken.removed ? "removed" : "sendingToBottom"));
653 return false;
654 }
655
656 if (focusedApp == null) {
657 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
658 + " using new focus @ " + w);
659 mTmpWindow = w;
660 return true;
661 }
662
663 if (!focusedApp.windowsAreFocusable()) {
664 // Current focused app windows aren't focusable...
665 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
666 + " focusable using new focus @ " + w);
667 mTmpWindow = w;
668 return true;
669 }
670
671 // Descend through all of the app tokens and find the first that either matches
672 // win.mAppToken (return win) or mFocusedApp (return null).
673 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
674 if (focusedApp.compareTo(wtoken) > 0) {
675 // App stack below focused app stack. No focus for you!!!
676 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
677 "findFocusedWindow: Reached focused app=" + focusedApp);
678 mTmpWindow = null;
679 return true;
680 }
681 }
682
683 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
684 mTmpWindow = w;
685 return true;
686 };
687
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800688 private final Consumer<WindowState> mPerformLayout = w -> {
689 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
690 // wasting time and funky changes while a window is animating away.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800691 final boolean gone = (mTmpWindow != null && mWmService.mPolicy.canBeHiddenByKeyguardLw(w))
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800692 || w.isGoneForLayoutLw();
693
694 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
695 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
696 + " mLayoutAttached=" + w.mLayoutAttached
Jorim Jaggi381cd722019-03-27 17:33:02 +0100697 + " config reported=" + w.isLastConfigReportedToClient());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800698 final AppWindowToken atoken = w.mAppToken;
699 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200700 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800701 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
702 + " parentHidden=" + w.isParentWindowHidden());
703 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200704 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800705 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
706 + " parentHidden=" + w.isParentWindowHidden());
707 }
708
709 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
710 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
711 // since that means "perform layout as normal, just don't display").
Jorim Jaggi91d382a2019-03-27 17:00:48 +0100712 if ((!gone || !w.mHaveFrame || w.mLayoutNeeded) && !w.mLayoutAttached) {
713 if (mTmpInitial) {
714 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800715 }
Jorim Jaggi91d382a2019-03-27 17:00:48 +0100716 if (w.mAttrs.type == TYPE_DREAM) {
717 // Don't layout windows behind a dream, so that if it does stuff like hide
718 // the status bar we won't get a bad transition when it goes away.
719 mTmpWindow = w;
720 }
721 w.mLayoutNeeded = false;
722 w.prelayout();
723 final boolean firstLayout = !w.isLaidOut();
724 getDisplayPolicy().layoutWindowLw(w, null, mDisplayFrames);
725 w.mLayoutSeq = mLayoutSeq;
726
727 // If this is the first layout, we need to initialize the last inset values as
728 // otherwise we'd immediately cause an unnecessary resize.
729 if (firstLayout) {
730 w.updateLastInsetValues();
731 }
732
733 if (w.mAppToken != null) {
734 w.mAppToken.layoutLetterbox(w);
735 }
736
737 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
738 + " mContainingFrame=" + w.getContainingFrame()
739 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800740 }
741 };
742
743 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
744 if (w.mLayoutAttached) {
745 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
746 + " mViewVisibility=" + w.mViewVisibility
747 + " mRelayoutCalled=" + w.mRelayoutCalled);
748 // If this view is GONE, then skip it -- keep the current frame, and let the caller
749 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
750 // windows, since that means "perform layout as normal, just don't display").
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800751 if (mTmpWindow != null && mWmService.mPolicy.canBeHiddenByKeyguardLw(w)) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800752 return;
753 }
754 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
755 || w.mLayoutNeeded) {
756 if (mTmpInitial) {
757 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700758 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800759 }
760 w.mLayoutNeeded = false;
761 w.prelayout();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800762 getDisplayPolicy().layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100763 w.mLayoutSeq = mLayoutSeq;
chaviw492139a2018-07-16 16:07:35 -0700764 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700765 + " mContainingFrame=" + w.getContainingFrame()
766 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800767 }
768 } else if (w.mAttrs.type == TYPE_DREAM) {
769 // Don't layout windows behind a dream, so that if it does stuff like hide the
770 // status bar we won't get a bad transition when it goes away.
771 mTmpWindow = mTmpWindow2;
772 }
773 };
774
775 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
776 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
777 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
778 return w.canBeImeTarget();
779 };
780
781 private final Consumer<WindowState> mApplyPostLayoutPolicy =
Tiger Huang7c610aa2018-10-27 00:01:01 +0800782 w -> getDisplayPolicy().applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
lumarkff0ab692018-11-05 20:32:30 +0800783 mInputMethodTarget);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800784
785 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800786 final WindowSurfacePlacer surfacePlacer = mWmService.mWindowPlacerLocked;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800787 final boolean obscuredChanged = w.mObscured !=
788 mTmpApplySurfaceChangesTransactionState.obscured;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800789 final RootWindowContainer root = mWmService.mRoot;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800790
791 // Update effect.
792 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
793 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
794 final boolean isDisplayed = w.isDisplayedLw();
795
796 if (isDisplayed && w.isObscuringDisplay()) {
797 // This window completely covers everything behind it, so we want to leave all
798 // of them as undimmed (for performance reasons).
799 root.mObscuringWindow = w;
800 mTmpApplySurfaceChangesTransactionState.obscured = true;
801 }
802
803 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
804 root.handleNotObscuredLocked(w,
805 mTmpApplySurfaceChangesTransactionState.obscured,
806 mTmpApplySurfaceChangesTransactionState.syswin);
807
808 if (w.mHasSurface && isDisplayed) {
809 final int type = w.mAttrs.type;
810 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
811 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
812 mTmpApplySurfaceChangesTransactionState.syswin = true;
813 }
814 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
815 && w.mAttrs.preferredRefreshRate != 0) {
816 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
817 = w.mAttrs.preferredRefreshRate;
818 }
Ady Abrahamf3e05312019-05-13 18:04:59 -0700819 final int preferredModeId = getDisplayPolicy().getRefreshRatePolicy()
820 .getPreferredModeId(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800821 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
Ady Abrahamf3e05312019-05-13 18:04:59 -0700822 && preferredModeId != 0) {
823 mTmpApplySurfaceChangesTransactionState.preferredModeId = preferredModeId;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800824 }
825 }
826 }
827
wilsonshihc32538e2018-11-07 17:27:34 +0800828 if (obscuredChanged && w.isVisibleLw() && mWallpaperController.isWallpaperTarget(w)) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800829 // This is the wallpaper target and its obscured state changed... make sure the
830 // current wallpaper's visibility has been updated accordingly.
831 mWallpaperController.updateWallpaperVisibility();
832 }
833
chaviw161ea3e2018-01-31 12:01:12 -0800834 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800835
836 final WindowStateAnimator winAnimator = w.mWinAnimator;
837
838 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
chaviw1454b392018-08-06 09:54:04 -0700839 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800840
841 // Moved from updateWindowsAndWallpaperLocked().
842 if (w.mHasSurface) {
843 // Take care of the window being ready to display.
844 final boolean committed = winAnimator.commitFinishDrawingLocked();
845 if (isDefaultDisplay && committed) {
846 if (w.mAttrs.type == TYPE_DREAM) {
847 // HACK: When a dream is shown, it may at that point hide the lock screen.
848 // So we need to redo the layout to let the phone window manager make this
849 // happen.
850 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
851 if (DEBUG_LAYOUT_REPEATS) {
852 surfacePlacer.debugLayoutRepeats(
853 "dream and commitFinishDrawingLocked true",
854 pendingLayoutChanges);
855 }
856 }
857 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
858 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
859 "First draw done in potential wallpaper target " + w);
wilsonshihc32538e2018-11-07 17:27:34 +0800860 mWallpaperMayChange = true;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800861 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
862 if (DEBUG_LAYOUT_REPEATS) {
863 surfacePlacer.debugLayoutRepeats(
864 "wallpaper and commitFinishDrawingLocked true",
865 pendingLayoutChanges);
866 }
867 }
868 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800869 }
870
871 final AppWindowToken atoken = w.mAppToken;
872 if (atoken != null) {
Adrian Roos23df3a32018-03-15 15:41:13 +0100873 atoken.updateLetterboxSurface(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800874 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
875 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
876 mTmpUpdateAllDrawn.add(atoken);
877 }
878 }
879
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800880 if (!mLosingFocus.isEmpty() && w.isFocused() && w.isDisplayedLw()) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800881 mWmService.mH.obtainMessage(REPORT_LOSING_FOCUS, this).sendToTarget();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800882 }
883
884 w.updateResizingWindowIfNeeded();
885 };
886
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800887 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800888 * Create new {@link DisplayContent} instance, add itself to the root window container and
889 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700890 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800891 * @param service You know.
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800892 * @param activityDisplay The ActivityDisplay for the display container.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700893 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700894 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800895 ActivityDisplay activityDisplay) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100896 super(service);
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800897 mAcitvityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800898 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
899 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
900 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
901 + " new=" + display);
902 }
903
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700904 mDisplay = display;
905 mDisplayId = display.getDisplayId();
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800906 mWallpaperController = new WallpaperController(mWmService, this);
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700907 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700908 display.getMetrics(mDisplayMetrics);
Adrian Roos506267d2019-06-18 16:13:53 +0200909 mSystemGestureExclusionLimit = mWmService.mSystemGestureExclusionLimitDp
910 * mDisplayMetrics.densityDpi / DENSITY_DEFAULT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700911 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100912 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo,
913 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700914 initializeDisplayBaseInfo();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700915
lumark588a3e82018-07-20 18:53:54 +0800916 mAppTransition = new AppTransition(service.mContext, service, this);
917 mAppTransition.registerListenerLocked(service.mActivityManagerAppTransitionNotifier);
918 mAppTransitionController = new AppTransitionController(service, this);
919 mUnknownAppVisibilityController = new UnknownAppVisibilityController(service, this);
920
921 AnimationHandler animationHandler = new AnimationHandler();
922 mBoundsAnimationController = new BoundsAnimationController(service.mContext,
Winson Chung6ba767a2018-12-13 18:20:11 -0800923 mAppTransition, AnimationThread.getHandler(), animationHandler);
lumark588a3e82018-07-20 18:53:54 +0800924
Riddle Hsu2588ab02019-02-25 14:23:56 +0800925 final InputChannel inputChannel = mWmService.mInputManager.monitorInput(
926 "PointerEventDispatcher" + mDisplayId, mDisplayId);
927 mPointerEventDispatcher = new PointerEventDispatcher(inputChannel);
928
929 // Tap Listeners are supported for:
930 // 1. All physical displays (multi-display).
931 // 2. VirtualDisplays on VR, AA (and everything else).
932 mTapDetector = new TaskTapPointerEventListener(mWmService, this);
933 registerPointerEventListener(mTapDetector);
934 registerPointerEventListener(mWmService.mMousePositionTracker);
Winson Chungc5fe7ff2019-02-19 14:49:25 -0800935 if (mWmService.mAtmService.getRecentTasks() != null) {
936 registerPointerEventListener(
937 mWmService.mAtmService.getRecentTasks().getInputListener());
938 }
Riddle Hsu2588ab02019-02-25 14:23:56 +0800939
Tiger Huang7c610aa2018-10-27 00:01:01 +0800940 mDisplayPolicy = new DisplayPolicy(service, this);
941 mDisplayRotation = new DisplayRotation(service, this);
Tiger Huang86e6d072019-05-02 20:23:47 +0800942 mCloseToSquareMaxAspectRatio = service.mContext.getResources().getFloat(
943 com.android.internal.R.dimen.config_closeToSquareDisplayMaxAspectRatio);
Tiger Huang7c610aa2018-10-27 00:01:01 +0800944 if (isDefaultDisplay) {
945 // The policy may be invoked right after here, so it requires the necessary default
946 // fields of this display content.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800947 mWmService.mPolicy.setDefaultDisplay(this);
Tiger Huang7c610aa2018-10-27 00:01:01 +0800948 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800949 if (mWmService.mDisplayReady) {
Tiger Huang7c610aa2018-10-27 00:01:01 +0800950 mDisplayPolicy.onConfigurationChanged();
951 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800952 if (mWmService.mSystemReady) {
Tiger Huang7c610aa2018-10-27 00:01:01 +0800953 mDisplayPolicy.systemReady();
954 }
Tiger Huang43b8fc22019-04-26 11:49:29 +0800955 mWindowCornerRadius = mDisplayPolicy.getWindowCornerRadius();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800956 mDividerControllerLocked = new DockedStackDividerController(service, this);
957 mPinnedStackControllerLocked = new PinnedStackController(service, this);
958
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000959 final SurfaceControl.Builder b = mWmService.makeSurfaceBuilder(mSession)
960 .setOpaque(true)
961 .setContainerLayer();
Robert Carrb1579c82017-09-05 14:54:47 -0700962 mWindowingLayer = b.setName("Display Root").build();
963 mOverlayLayer = b.setName("Display Overlays").build();
964
Robert Carrf59b8dd2017-10-02 18:58:36 -0700965 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700966 .setLayerStack(mWindowingLayer, mDisplayId)
967 .show(mWindowingLayer)
968 .setLayer(mOverlayLayer, 1)
969 .setLayerStack(mOverlayLayer, mDisplayId)
970 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700971 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700972
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700973 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700974 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700975 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700976 super.addChild(mAboveAppWindowsContainers, null);
977 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800978
979 // Add itself as a child to the root container.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800980 mWmService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700981
982 // TODO(b/62541591): evaluate whether this is the best spot to declare the
983 // {@link DisplayContent} ready for use.
984 mDisplayReady = true;
Arthur Hung95b38a92018-07-20 18:56:12 +0800985
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800986 mWmService.mAnimator.addDisplayLocked(mDisplayId);
Arthur Hung39134b22018-08-14 11:58:28 +0800987 mInputMonitor = new InputMonitor(service, mDisplayId);
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200988 mInsetsStateController = new InsetsStateController(this);
Bryce Leed1871262017-06-12 14:12:29 -0700989 }
990
991 boolean isReady() {
992 // The display is ready when the system and the individual display are both ready.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800993 return mWmService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700994 }
995
996 int getDisplayId() {
997 return mDisplayId;
998 }
999
Tiger Huang43b8fc22019-04-26 11:49:29 +08001000 float getWindowCornerRadius() {
1001 return mWindowCornerRadius;
1002 }
1003
Wale Ogunwale02319a62016-09-26 15:21:22 -07001004 WindowToken getWindowToken(IBinder binder) {
1005 return mTokenMap.get(binder);
1006 }
1007
1008 AppWindowToken getAppWindowToken(IBinder binder) {
1009 final WindowToken token = getWindowToken(binder);
1010 if (token == null) {
1011 return null;
1012 }
1013 return token.asAppWindowToken();
1014 }
1015
Wale Ogunwale2f569ed2017-05-08 09:15:49 -07001016 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001017 final DisplayContent dc = mWmService.mRoot.getWindowTokenDisplay(token);
Wale Ogunwale02319a62016-09-26 15:21:22 -07001018 if (dc != null) {
1019 // We currently don't support adding a window token to the display if the display
1020 // already has the binder mapped to another token. If there is a use case for supporting
1021 // this moving forward we will either need to merge the WindowTokens some how or have
1022 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -07001023 throw new IllegalArgumentException("Can't map token=" + token + " to display="
1024 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -07001025 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -07001026 if (binder == null) {
1027 throw new IllegalArgumentException("Can't map token=" + token + " to display="
1028 + getName() + " binder is null");
1029 }
1030 if (token == null) {
1031 throw new IllegalArgumentException("Can't map null token to display="
1032 + getName() + " binder=" + binder);
1033 }
1034
Wale Ogunwale02319a62016-09-26 15:21:22 -07001035 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001036
1037 if (token.asAppWindowToken() == null) {
1038 // Add non-app token to container hierarchy on the display. App tokens are added through
1039 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -07001040 switch (token.windowType) {
1041 case TYPE_WALLPAPER:
1042 mBelowAppWindowsContainers.addChild(token);
1043 break;
1044 case TYPE_INPUT_METHOD:
1045 case TYPE_INPUT_METHOD_DIALOG:
1046 mImeWindowsContainers.addChild(token);
1047 break;
1048 default:
1049 mAboveAppWindowsContainers.addChild(token);
1050 break;
1051 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001052 }
Wale Ogunwale02319a62016-09-26 15:21:22 -07001053 }
1054
1055 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001056 final WindowToken token = mTokenMap.remove(binder);
1057 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -08001058 token.setExiting();
1059 }
1060 return token;
1061 }
1062
1063 /** Changes the display the input window token is housed on to this one. */
1064 void reParentWindowToken(WindowToken token) {
1065 final DisplayContent prevDc = token.getDisplayContent();
1066 if (prevDc == this) {
1067 return;
1068 }
Riddle Hsu85bd04b2018-11-17 00:34:36 +08001069 if (prevDc != null) {
1070 if (prevDc.mTokenMap.remove(token.token) != null && token.asAppWindowToken() == null) {
1071 // Removed the token from the map, but made sure it's not an app token before
1072 // removing from parent.
1073 token.getParent().removeChild(token);
1074 }
1075 if (prevDc.mLastFocus == mCurrentFocus) {
1076 // The window has become the focus of this display, so it should not be notified
1077 // that it lost focus from the previous display.
1078 prevDc.mLastFocus = null;
1079 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001080 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -08001081
1082 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -07001083 }
1084
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001085 void removeAppToken(IBinder binder) {
1086 final WindowToken token = removeWindowToken(binder);
1087 if (token == null) {
1088 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
1089 return;
1090 }
1091
1092 final AppWindowToken appToken = token.asAppWindowToken();
1093
1094 if (appToken == null) {
1095 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
1096 return;
1097 }
1098
1099 appToken.onRemovedFromDisplay();
1100 }
1101
Riddle Hsuad256a12018-07-18 16:11:30 +08001102 @Override
1103 public Display getDisplay() {
Craig Mautnerb47bbc32012-08-22 17:41:48 -07001104 return mDisplay;
1105 }
1106
Craig Mautner59c00972012-07-30 12:10:24 -07001107 DisplayInfo getDisplayInfo() {
1108 return mDisplayInfo;
1109 }
1110
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001111 DisplayMetrics getDisplayMetrics() {
1112 return mDisplayMetrics;
1113 }
1114
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001115 DisplayPolicy getDisplayPolicy() {
1116 return mDisplayPolicy;
1117 }
1118
Riddle Hsuad256a12018-07-18 16:11:30 +08001119 @Override
1120 public DisplayRotation getDisplayRotation() {
1121 return mDisplayRotation;
1122 }
1123
Jorim Jaggif96c90a2018-09-26 16:55:15 +02001124 /**
1125 * Marks a window as providing insets for the rest of the windows in the system.
1126 *
1127 * @param type The type of inset this window provides.
1128 * @param win The window.
1129 * @param frameProvider Function to compute the frame, or {@code null} if the just the frame of
1130 * the window should be taken.
1131 */
1132 void setInsetProvider(@InternalInsetType int type, WindowState win,
1133 @Nullable TriConsumer<DisplayFrames, WindowState, Rect> frameProvider) {
1134 mInsetsStateController.getSourceProvider(type).setWindow(win, frameProvider);
1135 }
1136
1137 InsetsStateController getInsetsStateController() {
1138 return mInsetsStateController;
1139 }
1140
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001141 @VisibleForTesting
1142 void setDisplayRotation(DisplayRotation displayRotation) {
1143 mDisplayRotation = displayRotation;
1144 }
1145
Andrii Kulian8ee72852017-03-10 10:36:45 -08001146 int getRotation() {
1147 return mRotation;
1148 }
1149
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001150 @VisibleForTesting
Andrii Kulian8ee72852017-03-10 10:36:45 -08001151 void setRotation(int newRotation) {
1152 mRotation = newRotation;
Riddle Hsuad256a12018-07-18 16:11:30 +08001153 mDisplayRotation.setRotation(newRotation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001154 }
1155
1156 int getLastOrientation() {
1157 return mLastOrientation;
1158 }
1159
Andrii Kulian8ee72852017-03-10 10:36:45 -08001160 int getLastWindowForcedOrientation() {
1161 return mLastWindowForcedOrientation;
1162 }
1163
Evan Rosky966759f2019-01-15 10:33:58 -08001164 void registerRemoteAnimations(RemoteAnimationDefinition definition) {
1165 mAppTransitionController.registerRemoteAnimations(definition);
1166 }
1167
Andrii Kulian06d07d62017-03-14 11:11:47 -07001168 /**
Riddle Hsu654a6f92018-07-13 22:59:36 +08001169 * Temporarily pauses rotation changes until resumed.
1170 *
1171 * This can be used to prevent rotation changes from occurring while the user is
1172 * performing certain operations, such as drag and drop.
1173 *
1174 * This call nests and must be matched by an equal number of calls to
1175 * {@link #resumeRotationLocked}.
1176 */
1177 void pauseRotationLocked() {
1178 mDeferredRotationPauseCount++;
1179 }
1180
1181 /**
1182 * Resumes normal rotation changes after being paused.
1183 */
1184 void resumeRotationLocked() {
1185 if (mDeferredRotationPauseCount <= 0) {
1186 return;
1187 }
1188
1189 mDeferredRotationPauseCount--;
1190 if (mDeferredRotationPauseCount == 0) {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001191 updateRotationAndSendNewConfigIfNeeded();
Riddle Hsu654a6f92018-07-13 22:59:36 +08001192 }
1193 }
1194
1195 /**
Riddle Hsuad256a12018-07-18 16:11:30 +08001196 * If this is true we have updated our desired orientation, but not yet changed the real
1197 * orientation our applied our screen rotation animation. For example, because a previous
1198 * screen rotation was in progress.
1199 *
1200 * @return {@code true} if the there is an ongoing rotation change.
1201 */
1202 boolean rotationNeedsUpdate() {
1203 final int lastOrientation = getLastOrientation();
1204 final int oldRotation = getRotation();
Riddle Hsuad256a12018-07-18 16:11:30 +08001205
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001206 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Garfield Tan4bb83472019-01-16 14:37:04 -08001207 return oldRotation != rotation;
Riddle Hsuad256a12018-07-18 16:11:30 +08001208 }
1209
Wale Ogunwale3a256e62018-12-06 14:41:18 -08001210 /**
1211 * The display content may have configuration set from {@link #DisplayWindowSettings}. This
1212 * callback let the owner of container know there is existing configuration to prevent the
1213 * values from being replaced by the initializing {@link #ActivityDisplay}.
1214 */
1215 void initializeDisplayOverrideConfiguration() {
1216 if (mAcitvityDisplay != null) {
1217 mAcitvityDisplay.onInitializeOverrideConfiguration(getRequestedOverrideConfiguration());
1218 }
1219 }
1220
Riddle Hsu4e611772018-10-31 18:58:28 +08001221 /** Notify the configuration change of this display. */
1222 void sendNewConfiguration() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001223 mWmService.mH.obtainMessage(SEND_NEW_CONFIGURATION, this).sendToTarget();
Riddle Hsu4e611772018-10-31 18:58:28 +08001224 }
1225
Garfield Tan90b04282018-12-11 14:04:42 -08001226 @Override
1227 boolean onDescendantOrientationChanged(IBinder freezeDisplayToken,
1228 ConfigurationContainer requestingContainer) {
1229 final Configuration config = updateOrientationFromAppTokens(
1230 getRequestedOverrideConfiguration(), freezeDisplayToken, false);
Garfield Tan49dae102019-02-04 09:51:59 -08001231 // If display rotation class tells us that it doesn't consider app requested orientation,
1232 // this display won't rotate just because of an app changes its requested orientation. Thus
1233 // it indicates that this display chooses not to handle this request.
1234 final boolean handled = getDisplayRotation().respectAppRequestedOrientation();
Garfield Tan90b04282018-12-11 14:04:42 -08001235 if (config == null) {
1236 return handled;
1237 }
1238
1239 if (handled && requestingContainer instanceof ActivityRecord) {
1240 final ActivityRecord activityRecord = (ActivityRecord) requestingContainer;
1241 final boolean kept = mWmService.mAtmService.updateDisplayOverrideConfigurationLocked(
1242 config, activityRecord, false /* deferResume */, getDisplayId());
1243 activityRecord.frozenBeforeDestroy = true;
1244 if (!kept) {
1245 mWmService.mAtmService.mRootActivityContainer.resumeFocusedStacksTopActivities();
1246 }
1247 } else {
1248 // We have a new configuration to push so we need to update ATMS for now.
1249 // TODO: Clean up display configuration push between ATMS and WMS after unification.
1250 mWmService.mAtmService.updateDisplayOverrideConfigurationLocked(
1251 config, null /* starting */, false /* deferResume */, getDisplayId());
1252 }
1253 return handled;
1254 }
1255
Garfield Tan49dae102019-02-04 09:51:59 -08001256 @Override
1257 boolean handlesOrientationChangeFromDescendant() {
1258 return getDisplayRotation().respectAppRequestedOrientation();
1259 }
1260
Riddle Hsu4e611772018-10-31 18:58:28 +08001261 /**
1262 * Determine the new desired orientation of this display.
1263 *
1264 * The orientation is computed from non-application windows first. If none of the
1265 * non-application windows specify orientation, the orientation is computed from application
1266 * tokens.
1267 *
1268 * @return {@code true} if the orientation is changed.
1269 */
1270 boolean updateOrientationFromAppTokens() {
1271 return updateOrientationFromAppTokens(false /* forceUpdate */);
1272 }
1273
Garfield Tan90b04282018-12-11 14:04:42 -08001274 /**
1275 * Update orientation of the target display, returning a non-null new Configuration if it has
1276 * changed from the current orientation. If a non-null configuration is returned, someone must
1277 * call {@link WindowManagerService#setNewDisplayOverrideConfiguration(Configuration,
1278 * DisplayContent)} to tell the window manager it can unfreeze the screen. This will typically
1279 * be done by calling {@link WindowManagerService#sendNewConfiguration(int)}.
1280 */
1281 Configuration updateOrientationFromAppTokens(Configuration currentConfig,
1282 IBinder freezeDisplayToken, boolean forceUpdate) {
1283 if (!mDisplayReady) {
1284 return null;
1285 }
1286
1287 Configuration config = null;
1288 if (updateOrientationFromAppTokens(forceUpdate)) {
1289 // If we changed the orientation but mOrientationChangeComplete is already true,
1290 // we used seamless rotation, and we don't need to freeze the screen.
1291 if (freezeDisplayToken != null && !mWmService.mRoot.mOrientationChangeComplete) {
1292 final AppWindowToken atoken = getAppWindowToken(freezeDisplayToken);
1293 if (atoken != null) {
1294 atoken.startFreezingScreen();
1295 }
1296 }
1297 config = new Configuration();
1298 computeScreenConfiguration(config);
1299 } else if (currentConfig != null) {
1300 // No obvious action we need to take, but if our current state mismatches the
1301 // activity manager's, update it, disregarding font scale, which should remain set
1302 // to the value of the previous configuration.
1303 // Here we're calling Configuration#unset() instead of setToDefaults() because we
1304 // need to keep override configs clear of non-empty values (e.g. fontSize).
1305 mTmpConfiguration.unset();
1306 mTmpConfiguration.updateFrom(currentConfig);
1307 computeScreenConfiguration(mTmpConfiguration);
1308 if (currentConfig.diff(mTmpConfiguration) != 0) {
1309 mWaitingForConfig = true;
1310 setLayoutNeeded();
1311 int[] anim = new int[2];
1312 getDisplayPolicy().selectRotationAnimationLw(anim);
1313
1314 mWmService.startFreezingDisplayLocked(anim[0], anim[1], this);
1315 config = new Configuration(mTmpConfiguration);
1316 }
1317 }
1318
1319 return config;
1320 }
1321
1322
1323 private boolean updateOrientationFromAppTokens(boolean forceUpdate) {
Riddle Hsu4e611772018-10-31 18:58:28 +08001324 final int req = getOrientation();
1325 if (req != mLastOrientation || forceUpdate) {
1326 mLastOrientation = req;
1327 mDisplayRotation.setCurrentOrientation(req);
1328 return updateRotationUnchecked(forceUpdate);
1329 }
1330 return false;
1331 }
1332
Riddle Hsuad256a12018-07-18 16:11:30 +08001333 /**
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001334 * Update rotation of the display and send configuration if the rotation is changed.
1335 *
1336 * @return {@code true} if the rotation has been changed and the new config is sent.
1337 */
1338 boolean updateRotationAndSendNewConfigIfNeeded() {
1339 final boolean changed = updateRotationUnchecked(false /* forceUpdate */);
1340 if (changed) {
Riddle Hsu4e611772018-10-31 18:58:28 +08001341 sendNewConfiguration();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001342 }
1343 return changed;
1344 }
1345
1346 /**
Andrii Kulian06d07d62017-03-14 11:11:47 -07001347 * Update rotation of the display.
1348 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001349 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1350 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001351 */
Robert Carrae606b42018-02-15 15:36:23 -08001352 boolean updateRotationUnchecked() {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001353 return updateRotationUnchecked(false /* forceUpdate */);
1354 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001355
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001356 /**
Evan Roskye747c3e2018-10-30 20:06:41 -07001357 * Update rotation of the DisplayContent with an option to force the update. This updates
1358 * the container's perception of rotation and, depending on the top activities, will freeze
1359 * the screen or start seamless rotation. The display itself gets rotated in
1360 * {@link #applyRotationLocked} during {@link WindowManagerService#sendNewConfiguration}.
1361 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001362 * @param forceUpdate Force the rotation update. Sometimes in WM we might skip updating
1363 * orientation because we're waiting for some rotation to finish or display
1364 * to unfreeze, which results in configuration of the previously visible
1365 * activity being applied to a newly visible one. Forcing the rotation
1366 * update allows to workaround this issue.
1367 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
Evan Roskye747c3e2018-10-30 20:06:41 -07001368 * {@link WindowManagerService#sendNewConfiguration(int)} TO COMPLETE THE ROTATION AND
1369 * UNFREEZE THE SCREEN.
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001370 */
1371 boolean updateRotationUnchecked(boolean forceUpdate) {
1372 ScreenRotationAnimation screenRotationAnimation;
1373 if (!forceUpdate) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001374 if (mDeferredRotationPauseCount > 0) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001375 // Rotation updates have been paused temporarily. Defer the update until
1376 // updates have been resumed.
1377 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
1378 return false;
1379 }
1380
1381 screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001382 mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001383 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
1384 // Rotation updates cannot be performed while the previous rotation change
1385 // animation is still in progress. Skip this update. We will try updating
1386 // again after the animation is finished and the display is unfrozen.
1387 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
1388 return false;
1389 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001390 if (mWmService.mDisplayFrozen) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001391 // Even if the screen rotation animation has finished (e.g. isAnimating
1392 // returns false), there is still some time where we haven't yet unfrozen
1393 // the display. We also need to abort rotation here.
1394 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
1395 "Deferring rotation, still finishing previous rotation");
1396 return false;
1397 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001398 }
1399
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001400 if (!mWmService.mDisplayEnabled) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001401 // No point choosing a rotation if the display is not enabled.
1402 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
1403 return false;
1404 }
1405
1406 final int oldRotation = mRotation;
1407 final int lastOrientation = mLastOrientation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001408 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001409 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Computed rotation=" + rotation + " for display id="
1410 + mDisplayId + " based on lastOrientation=" + lastOrientation
1411 + " and oldRotation=" + oldRotation);
Tiger Huang7c610aa2018-10-27 00:01:01 +08001412 boolean mayRotateSeamlessly = mDisplayPolicy.shouldRotateSeamlessly(mDisplayRotation,
Riddle Hsuad256a12018-07-18 16:11:30 +08001413 oldRotation, rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001414
Robert Carr0e007272017-10-02 13:00:55 -07001415 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001416 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001417 if (seamlessRotated != null && !forceUpdate) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001418 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1419 // to complete (that is, waiting for windows to redraw). It's tempting to check
1420 // w.mSeamlessRotationCount but that could be incorrect in the case of
1421 // window-removal.
1422 return false;
1423 }
Robert Carr0e007272017-10-02 13:00:55 -07001424
1425 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001426 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001427 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001428 mayRotateSeamlessly = false;
1429 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001430 for (int i = 0; i < mWmService.mSessions.size(); i++) {
1431 if (mWmService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
Robert Carr0e007272017-10-02 13:00:55 -07001432 mayRotateSeamlessly = false;
1433 break;
1434 }
1435 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001436 }
Robert Carr0e007272017-10-02 13:00:55 -07001437 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001438
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001439 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1440 + " selected orientation " + lastOrientation
Garfield Tan49dae102019-02-04 09:51:59 -08001441 + ", got rotation " + rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001442
Garfield Tan4bb83472019-01-16 14:37:04 -08001443 if (oldRotation == rotation) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001444 // No change.
1445 return false;
1446 }
1447
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001448 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1449 + " rotation changed to " + rotation
Garfield Tan4bb83472019-01-16 14:37:04 -08001450 + " from " + oldRotation
1451 + ", lastOrientation=" + lastOrientation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001452
1453 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
Vishnu Nairba183352018-11-21 11:16:49 -08001454 mWaitingForConfig = true;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001455 }
1456
Evan Roskye747c3e2018-10-30 20:06:41 -07001457 mRotation = rotation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001458
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001459 mWmService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1460 mWmService.mH.sendNewMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
Riddle Hsu654a6f92018-07-13 22:59:36 +08001461 this, WINDOW_FREEZE_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001462
1463 setLayoutNeeded();
1464 final int[] anim = new int[2];
Tiger Huang7c610aa2018-10-27 00:01:01 +08001465 mDisplayPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001466
1467 if (!rotateSeamlessly) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001468 mWmService.startFreezingDisplayLocked(anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001469 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001470 } else {
1471 // The screen rotation animation uses a screenshot to freeze the screen
1472 // while windows resize underneath.
1473 // When we are rotating seamlessly, we allow the elements to transition
1474 // to their rotated state independently and without a freeze required.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001475 mWmService.startSeamlessRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001476 }
1477
Evan Roskye747c3e2018-10-30 20:06:41 -07001478 return true;
1479 }
1480
1481 /**
1482 * Applies the rotation transaction. This must be called after {@link #updateRotationUnchecked}
1483 * (if it returned {@code true}) to actually finish the rotation.
1484 *
1485 * @param oldRotation the rotation we are coming from.
1486 * @param rotation the rotation to apply.
1487 */
1488 void applyRotationLocked(final int oldRotation, final int rotation) {
1489 mDisplayRotation.setRotation(rotation);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001490 final boolean rotateSeamlessly = mWmService.isRotatingSeamlessly();
Evan Roskye747c3e2018-10-30 20:06:41 -07001491 ScreenRotationAnimation screenRotationAnimation = rotateSeamlessly
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001492 ? null : mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001493 // We need to update our screen size information to match the new rotation. If the rotation
1494 // has actually changed then this method will return true and, according to the comment at
1495 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1496 // By updating the Display info here it will be available to
1497 // #computeScreenConfiguration() later.
Tiger Huanga817b1f2019-05-09 20:04:17 +08001498 updateDisplayAndOrientation(getConfiguration().uiMode, null /* outConfig */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001499
Robert Carrae606b42018-02-15 15:36:23 -08001500 // NOTE: We disable the rotation in the emulator because
1501 // it doesn't support hardware OpenGL emulation yet.
1502 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1503 && screenRotationAnimation.hasScreenshot()) {
1504 if (screenRotationAnimation.setRotation(getPendingTransaction(), rotation,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001505 MAX_ANIMATION_DURATION, mWmService.getTransitionAnimationScaleLocked(),
Robert Carrae606b42018-02-15 15:36:23 -08001506 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001507 mWmService.scheduleAnimationLocked();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001508 }
1509 }
1510
Vishnu Nair83537a72018-07-19 21:27:48 -07001511 forAllWindows(w -> {
Vishnu Nairddd80742018-08-21 14:12:46 -07001512 w.seamlesslyRotateIfAllowed(getPendingTransaction(), oldRotation, rotation,
1513 rotateSeamlessly);
Vishnu Nair83537a72018-07-19 21:27:48 -07001514 }, true /* traverseTopToBottom */);
1515
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001516 mWmService.mDisplayManagerInternal.performTraversal(getPendingTransaction());
Robert Carrae606b42018-02-15 15:36:23 -08001517 scheduleAnimation();
1518
Andrii Kulian06d07d62017-03-14 11:11:47 -07001519 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001520 if (w.mHasSurface && !rotateSeamlessly) {
1521 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001522 w.setOrientationChanging(true);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001523 mWmService.mRoot.mOrientationChangeComplete = false;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001524 w.mLastFreezeDuration = 0;
1525 }
1526 w.mReportOrientationChanged = true;
1527 }, true /* traverseTopToBottom */);
1528
1529 if (rotateSeamlessly) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001530 mWmService.mH.sendNewMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
Riddle Hsu654a6f92018-07-13 22:59:36 +08001531 this, SEAMLESS_ROTATION_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001532 }
1533
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001534 for (int i = mWmService.mRotationWatchers.size() - 1; i >= 0; i--) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001535 final WindowManagerService.RotationWatcher rotationWatcher
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001536 = mWmService.mRotationWatchers.get(i);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001537 if (rotationWatcher.mDisplayId == mDisplayId) {
1538 try {
1539 rotationWatcher.mWatcher.onRotationChanged(rotation);
1540 } catch (RemoteException e) {
1541 // Ignore
1542 }
1543 }
1544 }
1545
Andrii Kulian06d07d62017-03-14 11:11:47 -07001546 // Announce rotation only if we will not animate as we already have the
1547 // windows in final state. Otherwise, we make this call at the rotation end.
Rhed Jao02655dc2018-10-30 20:44:52 +08001548 if (screenRotationAnimation == null && mWmService.mAccessibilityController != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001549 mWmService.mAccessibilityController.onRotationChangedLocked(this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001550 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001551 }
1552
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001553 void configureDisplayPolicy() {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001554 final int width = mBaseDisplayWidth;
1555 final int height = mBaseDisplayHeight;
1556 final int shortSize;
1557 final int longSize;
1558 if (width > height) {
1559 shortSize = height;
1560 longSize = width;
1561 } else {
1562 shortSize = width;
1563 longSize = height;
1564 }
1565
Adrian Roos506267d2019-06-18 16:13:53 +02001566 final int shortSizeDp = shortSize * DENSITY_DEFAULT / mBaseDisplayDensity;
1567 final int longSizeDp = longSize * DENSITY_DEFAULT / mBaseDisplayDensity;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001568
Winson Chung4723b4e2019-03-25 16:49:36 -07001569 mDisplayPolicy.updateConfigurationAndScreenSizeDependentBehaviors();
Tiger Huang3d2b8982019-01-29 22:56:48 +08001570 mDisplayRotation.configure(width, height, shortSizeDp, longSizeDp);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001571
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001572 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1573 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Tiger Huang86e6d072019-05-02 20:23:47 +08001574
1575 // Not much of use to rotate the display for apps since it's close to square.
1576 mIgnoreRotationForApps = isNonDecorDisplayCloseToSquare(Surface.ROTATION_0, width, height);
1577 }
1578
1579 private boolean isNonDecorDisplayCloseToSquare(int rotation, int width, int height) {
1580 final DisplayCutout displayCutout =
1581 calculateDisplayCutoutForRotation(rotation).getDisplayCutout();
1582 final int uiMode = mWmService.mPolicy.getUiMode();
1583 final int w = mDisplayPolicy.getNonDecorDisplayWidth(
1584 width, height, rotation, uiMode, displayCutout);
1585 final int h = mDisplayPolicy.getNonDecorDisplayHeight(
1586 width, height, rotation, uiMode, displayCutout);
1587 final float aspectRatio = Math.max(w, h) / (float) Math.min(w, h);
1588 return aspectRatio <= mCloseToSquareMaxAspectRatio;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001589 }
1590
Andrii Kulian06d07d62017-03-14 11:11:47 -07001591 /**
1592 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1593 * changed.
1594 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1595 */
Tiger Huanga817b1f2019-05-09 20:04:17 +08001596 private DisplayInfo updateDisplayAndOrientation(int uiMode, Configuration outConfig) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001597 // Use the effective "visual" dimensions based on current rotation
1598 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
Garfield Tan4bb83472019-01-16 14:37:04 -08001599 final int dw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1600 final int dh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001601
1602 // Update application display metrics.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001603 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(mRotation);
1604 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1605
Tiger Huang7c610aa2018-10-27 00:01:01 +08001606 final int appWidth = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1607 displayCutout);
1608 final int appHeight = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1609 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001610 mDisplayInfo.rotation = mRotation;
1611 mDisplayInfo.logicalWidth = dw;
1612 mDisplayInfo.logicalHeight = dh;
1613 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1614 mDisplayInfo.appWidth = appWidth;
1615 mDisplayInfo.appHeight = appHeight;
1616 if (isDefaultDisplay) {
1617 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1618 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1619 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001620 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001621 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1622 if (mDisplayScalingDisabled) {
1623 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1624 } else {
1625 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1626 }
1627
Tiger Huanga817b1f2019-05-09 20:04:17 +08001628 computeSizeRangesAndScreenLayout(mDisplayInfo, rotated, uiMode, dw, dh,
1629 mDisplayMetrics.density, outConfig);
1630
Andrii Kulianf0379de2018-03-14 16:24:07 -07001631 // We usually set the override info in DisplayManager so that we get consistent display
1632 // metrics values when displays are changing and don't send out new values until WM is aware
1633 // of them. However, we don't do this for displays that serve as containers for ActivityView
1634 // because we don't want letter-/pillar-boxing during resize.
1635 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1636 ? mDisplayInfo : null;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001637 mWmService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001638 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001639
1640 mBaseDisplayRect.set(0, 0, dw, dh);
1641
1642 if (isDefaultDisplay) {
1643 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1644 mCompatDisplayMetrics);
1645 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001646
Andrii Kulian06d07d62017-03-14 11:11:47 -07001647 return mDisplayInfo;
1648 }
1649
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001650 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001651 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1652 }
1653
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001654 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001655 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001656 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001657 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001658 }
Adrian Roos11c25582018-02-19 18:06:36 +01001659 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001660 return WmDisplayCutout.computeSafeInsets(
1661 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001662 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001663 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Issei Suzuki43190bd2018-08-20 17:28:41 +02001664 final Rect[] newBounds = mRotationUtil.getRotatedBounds(
1665 WmDisplayCutout.computeSafeInsets(
Jorim Jaggi60640512018-06-29 01:14:31 +02001666 cutout, mInitialDisplayWidth, mInitialDisplayHeight)
Issei Suzuki43190bd2018-08-20 17:28:41 +02001667 .getDisplayCutout().getBoundingRectsAll(),
1668 rotation, mInitialDisplayWidth, mInitialDisplayHeight);
1669 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(newBounds),
Adrian Roos24264212018-02-19 16:26:15 +01001670 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1671 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001672 }
1673
Andrii Kulian06d07d62017-03-14 11:11:47 -07001674 /**
1675 * Compute display configuration based on display properties and policy settings.
1676 * Do not call if mDisplayReady == false.
1677 */
1678 void computeScreenConfiguration(Configuration config) {
Tiger Huanga817b1f2019-05-09 20:04:17 +08001679 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode, config);
Evan Roskye747c3e2018-10-30 20:06:41 -07001680 calculateBounds(displayInfo, mTmpBounds);
1681 config.windowConfiguration.setBounds(mTmpBounds);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001682
1683 final int dw = displayInfo.logicalWidth;
1684 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001685 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Garfield Tane0846042018-07-26 13:42:04 -07001686 config.windowConfiguration.setWindowingMode(getWindowingMode());
Yunfan Chen7daa6ac2018-11-29 18:16:44 -08001687 config.windowConfiguration.setDisplayWindowingMode(getWindowingMode());
Evan Roskye747c3e2018-10-30 20:06:41 -07001688 config.windowConfiguration.setRotation(displayInfo.rotation);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001689
Adrian Roos11c25582018-02-19 18:06:36 +01001690 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001691 config.screenWidthDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001692 (int)(mDisplayPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1693 config.uiMode, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001694 config.screenHeightDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001695 (int)(mDisplayPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1696 config.uiMode, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001697
Tiger Huang7c610aa2018-10-27 00:01:01 +08001698 mDisplayPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001699 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001700 final int leftInset = mTmpRect.left;
1701 final int topInset = mTmpRect.top;
1702 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001703 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1704 leftInset + displayInfo.appWidth /* right */,
1705 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001706 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1707 || displayInfo.rotation == Surface.ROTATION_270);
1708
Andrii Kulian06d07d62017-03-14 11:11:47 -07001709 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1710 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1711 ? Configuration.SCREENLAYOUT_ROUND_YES
1712 : Configuration.SCREENLAYOUT_ROUND_NO);
1713
1714 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1715 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1716 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001717 dh, displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001718 config.densityDpi = displayInfo.logicalDensityDpi;
1719
1720 config.colorMode =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001721 ((displayInfo.isHdr() && mWmService.hasHdrSupport())
Andrii Kulian06d07d62017-03-14 11:11:47 -07001722 ? Configuration.COLOR_MODE_HDR_YES
1723 : Configuration.COLOR_MODE_HDR_NO)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001724 | (displayInfo.isWideColorGamut() && mWmService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001725 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1726 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1727
1728 // Update the configuration based on available input devices, lid switch,
1729 // and platform configuration.
1730 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1731 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1732 config.navigation = Configuration.NAVIGATION_NONAV;
1733
1734 int keyboardPresence = 0;
1735 int navigationPresence = 0;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001736 final InputDevice[] devices = mWmService.mInputManager.getInputDevices();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001737 final int len = devices != null ? devices.length : 0;
1738 for (int i = 0; i < len; i++) {
1739 InputDevice device = devices[i];
Arthur Hung82bbfc32018-11-29 20:24:51 +08001740 // Ignore virtual input device.
1741 if (device.isVirtual()) {
1742 continue;
1743 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001744
Arthur Hung82bbfc32018-11-29 20:24:51 +08001745 // Check if input device can dispatch events to current display.
1746 // If display type is virtual, will follow the default display.
1747 if (!mWmService.mInputManager.canDispatchToDisplay(device.getId(),
1748 displayInfo.type == Display.TYPE_VIRTUAL ? DEFAULT_DISPLAY : mDisplayId)) {
1749 continue;
1750 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001751
Arthur Hung82bbfc32018-11-29 20:24:51 +08001752 final int sources = device.getSources();
1753 final int presenceFlag = device.isExternal()
1754 ? WindowManagerPolicy.PRESENCE_EXTERNAL : WindowManagerPolicy.PRESENCE_INTERNAL;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001755
Arthur Hung82bbfc32018-11-29 20:24:51 +08001756 if (mWmService.mIsTouchDevice) {
1757 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) == InputDevice.SOURCE_TOUCHSCREEN) {
1758 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001759 }
Arthur Hung82bbfc32018-11-29 20:24:51 +08001760 } else {
1761 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1762 }
1763
1764 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1765 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1766 navigationPresence |= presenceFlag;
1767 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1768 && config.navigation == Configuration.NAVIGATION_NONAV) {
1769 config.navigation = Configuration.NAVIGATION_DPAD;
1770 navigationPresence |= presenceFlag;
1771 }
1772
1773 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1774 config.keyboard = Configuration.KEYBOARD_QWERTY;
1775 keyboardPresence |= presenceFlag;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001776 }
1777 }
1778
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001779 if (config.navigation == Configuration.NAVIGATION_NONAV && mWmService.mHasPermanentDpad) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001780 config.navigation = Configuration.NAVIGATION_DPAD;
1781 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1782 }
1783
1784 // Determine whether a hard keyboard is available and enabled.
1785 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1786 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001787 if (hardKeyboardAvailable != mWmService.mHardKeyboardAvailable) {
1788 mWmService.mHardKeyboardAvailable = hardKeyboardAvailable;
1789 mWmService.mH.removeMessages(REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1790 mWmService.mH.sendEmptyMessage(REPORT_HARD_KEYBOARD_STATUS_CHANGE);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001791 }
1792
Winson Chung4723b4e2019-03-25 16:49:36 -07001793 mDisplayPolicy.updateConfigurationAndScreenSizeDependentBehaviors();
Tiger Huang7c610aa2018-10-27 00:01:01 +08001794
Andrii Kulian06d07d62017-03-14 11:11:47 -07001795 // Let the policy update hidden states.
1796 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1797 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1798 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001799 mWmService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001800 }
1801
1802 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001803 DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001804 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1805 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1806 final int unrotDw, unrotDh;
1807 if (rotated) {
1808 unrotDw = dh;
1809 unrotDh = dw;
1810 } else {
1811 unrotDw = dw;
1812 unrotDh = dh;
1813 }
1814 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001815 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001816 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001817 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001818 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001819 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001820 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001821 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001822 return sw;
1823 }
1824
1825 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001826 DisplayMetrics dm, int dw, int dh, DisplayCutout displayCutout) {
1827 dm.noncompatWidthPixels = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1828 displayCutout);
1829 dm.noncompatHeightPixels = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode,
1830 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001831 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1832 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1833 if (curSize == 0 || size < curSize) {
1834 curSize = size;
1835 }
1836 return curSize;
1837 }
1838
Tiger Huang7c610aa2018-10-27 00:01:01 +08001839 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, boolean rotated,
1840 int uiMode, int dw, int dh, float density, Configuration outConfig) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001841
1842 // We need to determine the smallest width that will occur under normal
1843 // operation. To this, start with the base screen size and compute the
1844 // width under the different possible rotations. We need to un-rotate
1845 // the current screen dimensions before doing this.
1846 int unrotDw, unrotDh;
1847 if (rotated) {
1848 unrotDw = dh;
1849 unrotDh = dw;
1850 } else {
1851 unrotDw = dw;
1852 unrotDh = dh;
1853 }
1854 displayInfo.smallestNominalAppWidth = 1<<30;
1855 displayInfo.smallestNominalAppHeight = 1<<30;
1856 displayInfo.largestNominalAppWidth = 0;
1857 displayInfo.largestNominalAppHeight = 0;
Tiger Huang7c610aa2018-10-27 00:01:01 +08001858 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_0, uiMode, unrotDw, unrotDh);
1859 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_90, uiMode, unrotDh, unrotDw);
1860 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_180, uiMode, unrotDw, unrotDh);
1861 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_270, uiMode, unrotDh, unrotDw);
Tiger Huanga817b1f2019-05-09 20:04:17 +08001862
1863 if (outConfig == null) {
1864 return;
1865 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001866 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1867 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001868 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001869 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001870 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001871 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001872 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001873 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001874 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001875 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1876 outConfig.screenLayout = sl;
1877 }
1878
1879 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001880 int uiMode, DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001881 // Get the app screen size at this rotation.
Tiger Huang7c610aa2018-10-27 00:01:01 +08001882 int w = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayCutout);
1883 int h = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001884
1885 // Compute the screen layout size class for this rotation.
1886 int longSize = w;
1887 int shortSize = h;
1888 if (longSize < shortSize) {
1889 int tmp = longSize;
1890 longSize = shortSize;
1891 shortSize = tmp;
1892 }
1893 longSize = (int)(longSize/density);
1894 shortSize = (int)(shortSize/density);
1895 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1896 }
1897
Tiger Huang7c610aa2018-10-27 00:01:01 +08001898 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001899 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001900 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1901 rotation).getDisplayCutout();
Tiger Huang7c610aa2018-10-27 00:01:01 +08001902 final int width = mDisplayPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1903 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001904 if (width < displayInfo.smallestNominalAppWidth) {
1905 displayInfo.smallestNominalAppWidth = width;
1906 }
1907 if (width > displayInfo.largestNominalAppWidth) {
1908 displayInfo.largestNominalAppWidth = width;
1909 }
Tiger Huang7c610aa2018-10-27 00:01:01 +08001910 final int height = mDisplayPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1911 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001912 if (height < displayInfo.smallestNominalAppHeight) {
1913 displayInfo.smallestNominalAppHeight = height;
1914 }
1915 if (height > displayInfo.largestNominalAppHeight) {
1916 displayInfo.largestNominalAppHeight = height;
1917 }
1918 }
1919
Riddle Hsua4d6fa22018-08-11 00:50:39 +08001920 /**
1921 * Apps that use the compact menu panel (as controlled by the panelMenuIsCompact
1922 * theme attribute) on devices that feature a physical options menu key attempt to position
1923 * their menu panel window along the edge of the screen nearest the physical menu key.
1924 * This lowers the travel distance between invoking the menu panel and selecting
1925 * a menu option.
1926 *
1927 * This method helps control where that menu is placed. Its current implementation makes
1928 * assumptions about the menu key and its relationship to the screen based on whether
1929 * the device's natural orientation is portrait (width < height) or landscape.
1930 *
1931 * The menu key is assumed to be located along the bottom edge of natural-portrait
1932 * devices and along the right edge of natural-landscape devices. If these assumptions
1933 * do not hold for the target device, this method should be changed to reflect that.
1934 *
1935 * @return A {@link Gravity} value for placing the options menu window.
1936 */
1937 int getPreferredOptionsPanelGravity() {
1938 final int rotation = getRotation();
1939 if (mInitialDisplayWidth < mInitialDisplayHeight) {
1940 // On devices with a natural orientation of portrait.
1941 switch (rotation) {
1942 default:
1943 case Surface.ROTATION_0:
1944 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1945 case Surface.ROTATION_90:
1946 return Gravity.RIGHT | Gravity.BOTTOM;
1947 case Surface.ROTATION_180:
1948 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1949 case Surface.ROTATION_270:
1950 return Gravity.START | Gravity.BOTTOM;
1951 }
1952 }
1953
1954 // On devices with a natural orientation of landscape.
1955 switch (rotation) {
1956 default:
1957 case Surface.ROTATION_0:
1958 return Gravity.RIGHT | Gravity.BOTTOM;
1959 case Surface.ROTATION_90:
1960 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1961 case Surface.ROTATION_180:
1962 return Gravity.START | Gravity.BOTTOM;
1963 case Surface.ROTATION_270:
1964 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1965 }
1966 }
1967
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001968 DockedStackDividerController getDockedDividerController() {
1969 return mDividerControllerLocked;
1970 }
1971
Winson Chung655332c2016-10-31 13:14:28 -07001972 PinnedStackController getPinnedStackController() {
1973 return mPinnedStackControllerLocked;
1974 }
1975
Jeff Browna506a6e2013-06-04 00:02:38 -07001976 /**
1977 * Returns true if the specified UID has access to this display.
1978 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001979 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001980 return mDisplay.hasAccess(uid);
1981 }
1982
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001983 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001984 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001985 }
1986
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001987 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001988 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001989 }
1990
Wale Ogunwale61911492017-10-11 08:50:50 -07001991 /**
1992 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1993 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001994 TaskStack getSplitScreenPrimaryStack() {
1995 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001996 return (stack != null && stack.isVisible()) ? stack : null;
1997 }
1998
Robert Carr9785cf32018-04-25 15:06:07 -07001999 boolean hasSplitScreenPrimaryStack() {
2000 return getSplitScreenPrimaryStack() != null;
2001 }
2002
Wale Ogunwale61911492017-10-11 08:50:50 -07002003 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07002004 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07002005 * not visible.
2006 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07002007 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
2008 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002009 }
2010
2011 TaskStack getPinnedStack() {
2012 return mTaskStackContainers.getPinnedStack();
2013 }
2014
2015 private boolean hasPinnedStack() {
2016 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07002017 }
2018
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07002019 /**
2020 * Returns the topmost stack on the display that is compatible with the input windowing mode.
2021 * Null is no compatible stack on the display.
2022 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002023 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07002024 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
2025 }
2026
2027 /**
2028 * Returns the topmost stack on the display that is compatible with the input windowing mode and
2029 * activity type. Null is no compatible stack on the display.
2030 */
Wale Ogunwale68278562017-09-23 17:13:55 -07002031 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002032 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07002033 }
2034
Bryce Lee48f4b572017-04-10 10:54:15 -07002035 @VisibleForTesting
Kazuki Takise148d00a2018-05-31 15:32:19 +09002036 WindowList<TaskStack> getStacks() {
2037 return mTaskStackContainers.mChildren;
2038 }
2039
2040 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07002041 TaskStack getTopStack() {
2042 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07002043 }
2044
Winson Chunge2d72172018-01-25 17:46:20 +00002045 ArrayList<Task> getVisibleTasks() {
2046 return mTaskStackContainers.getVisibleTasks();
2047 }
2048
Wale Ogunwale61911492017-10-11 08:50:50 -07002049 void onStackWindowingModeChanged(TaskStack stack) {
2050 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07002051 }
2052
Andrii Kulian441e4492016-09-29 15:25:00 -07002053 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07002054 public void onConfigurationChanged(Configuration newParentConfig) {
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05002055 final int lastOrientation = getConfiguration().orientation;
Andrii Kulian441e4492016-09-29 15:25:00 -07002056 super.onConfigurationChanged(newParentConfig);
Tiger Huang7c610aa2018-10-27 00:01:01 +08002057 if (mDisplayPolicy != null) {
2058 mDisplayPolicy.onConfigurationChanged();
2059 }
Andrii Kulian441e4492016-09-29 15:25:00 -07002060
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05002061 if (lastOrientation != getConfiguration().orientation) {
2062 getMetricsLogger().write(
2063 new LogMaker(MetricsEvent.ACTION_PHONE_ORIENTATION_CHANGED)
2064 .setSubtype(getConfiguration().orientation)
2065 .addTaggedData(MetricsEvent.FIELD_DISPLAY_ID, getDisplayId()));
2066 }
2067
Evan Roskye747c3e2018-10-30 20:06:41 -07002068 // If there was no pinned stack, we still need to notify the controller of the display info
2069 // update as a result of the config change.
2070 if (mPinnedStackControllerLocked != null && !hasPinnedStack()) {
2071 mPinnedStackControllerLocked.onDisplayInfoChanged(getDisplayInfo());
2072 }
Evan Roskye747c3e2018-10-30 20:06:41 -07002073 }
2074
2075 /**
2076 * Updates the resources used by docked/pinned controllers. This needs to be called at the
2077 * beginning of a configuration update cascade since the metrics from these resources are used
2078 * for bounds calculations. Since ActivityDisplay initiates the configuration update, this
2079 * should be called from there instead of DisplayContent's onConfigurationChanged.
2080 */
2081 void preOnConfigurationChanged() {
Bryce Leef3c6a472017-11-14 14:53:06 -08002082 final DockedStackDividerController dividerController = getDockedDividerController();
2083
2084 if (dividerController != null) {
2085 getDockedDividerController().onConfigurationChanged();
2086 }
2087
2088 final PinnedStackController pinnedStackController = getPinnedStackController();
2089
2090 if (pinnedStackController != null) {
2091 getPinnedStackController().onConfigurationChanged();
2092 }
Andrii Kulian441e4492016-09-29 15:25:00 -07002093 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07002094
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07002095 @Override
2096 boolean fillsParent() {
2097 return true;
2098 }
2099
2100 @Override
2101 boolean isVisible() {
2102 return true;
2103 }
2104
2105 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002106 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002107 super.onAppTransitionDone();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002108 mWmService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002109 }
2110
Yunfan Chen7daa6ac2018-11-29 18:16:44 -08002111 @Override
2112 public void setWindowingMode(int windowingMode) {
2113 super.setWindowingMode(windowingMode);
2114 super.setDisplayWindowingMode(windowingMode);
2115 }
2116
2117 @Override
2118 void setDisplayWindowingMode(int windowingMode) {
2119 setWindowingMode(windowingMode);
2120 }
2121
Robert Carr9785cf32018-04-25 15:06:07 -07002122 /**
2123 * In split-screen mode we process the IME containers above the docked divider
2124 * rather than directly above their target.
2125 */
2126 private boolean skipTraverseChild(WindowContainer child) {
lumarkff0ab692018-11-05 20:32:30 +08002127 if (child == mImeWindowsContainers && mInputMethodTarget != null
Robert Carr9785cf32018-04-25 15:06:07 -07002128 && !hasSplitScreenPrimaryStack()) {
2129 return true;
2130 }
2131 return false;
2132 }
2133
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07002134 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002135 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
2136 // Special handling so we can process IME windows with #forAllImeWindows above their IME
2137 // target, or here in order if there isn't an IME target.
2138 if (traverseTopToBottom) {
2139 for (int i = mChildren.size() - 1; i >= 0; --i) {
2140 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07002141 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002142 continue;
2143 }
Robert Carr9785cf32018-04-25 15:06:07 -07002144
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002145 if (child.forAllWindows(callback, traverseTopToBottom)) {
2146 return true;
2147 }
2148 }
2149 } else {
2150 final int count = mChildren.size();
2151 for (int i = 0; i < count; i++) {
2152 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07002153 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002154 continue;
2155 }
Robert Carr9785cf32018-04-25 15:06:07 -07002156
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002157 if (child.forAllWindows(callback, traverseTopToBottom)) {
2158 return true;
2159 }
2160 }
2161 }
2162 return false;
2163 }
2164
2165 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
2166 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
2167 }
2168
Wale Ogunwale399c8692017-05-08 14:22:42 -07002169 @Override
2170 int getOrientation() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002171 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002172
Tiger Huang86e6d072019-05-02 20:23:47 +08002173 if (mIgnoreRotationForApps) {
2174 return SCREEN_ORIENTATION_USER;
2175 }
2176
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002177 if (mWmService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08002178 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07002179 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
2180 + " is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002181 // If the display is frozen, some activities may be in the middle of restarting, and
2182 // thus have removed their old window. If the window has the flag to hide the lock
2183 // screen, then the lock screen can re-appear and inflict its own orientation on us.
2184 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08002185 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002186 } else if (policy.isKeyguardLocked()) {
2187 // Use the last orientation the while the display is frozen with the keyguard
2188 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
2189 // window. We don't want to check the show when locked window directly though as
2190 // things aren't stable while the display is frozen, for example the window could be
2191 // momentarily unavailable due to activity relaunch.
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07002192 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
2193 + " is frozen while keyguard locked, return " + mLastOrientation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08002194 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07002195 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002196 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002197 final int orientation = mAboveAppWindowsContainers.getOrientation();
2198 if (orientation != SCREEN_ORIENTATION_UNSET) {
2199 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002200 }
Wale Ogunwale51362492016-09-08 17:49:17 -07002201 }
2202
Wale Ogunwale399c8692017-05-08 14:22:42 -07002203 // Top system windows are not requesting an orientation. Start searching from apps.
2204 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07002205 }
2206
Craig Mautner46ac6fa2013-08-01 10:06:34 -07002207 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07002208 // Check if display metrics changed and update base values if needed.
2209 updateBaseDisplayMetricsIfNeeded();
2210
Craig Mautner722285e2012-09-07 13:55:58 -07002211 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07002212 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07002213
Garfield Tan2f145f22018-11-01 15:27:03 -07002214 onDisplayChanged(this);
Craig Mautner722285e2012-09-07 13:55:58 -07002215 }
2216
Adrian Roos506267d2019-06-18 16:13:53 +02002217 @Override
2218 void onDisplayChanged(DisplayContent dc) {
2219 super.onDisplayChanged(dc);
2220 updateSystemGestureExclusionLimit();
2221 }
2222
2223 void updateSystemGestureExclusionLimit() {
2224 mSystemGestureExclusionLimit = mWmService.mSystemGestureExclusionLimitDp
2225 * mDisplayMetrics.densityDpi / DENSITY_DEFAULT;
2226 updateSystemGestureExclusion();
2227 }
2228
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002229 void initializeDisplayBaseInfo() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002230 final DisplayManagerInternal displayManagerInternal = mWmService.mDisplayManagerInternal;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002231 if (displayManagerInternal != null) {
2232 // Bootstrap the default logical display from the display manager.
2233 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
2234 if (newDisplayInfo != null) {
2235 mDisplayInfo.copyFrom(newDisplayInfo);
2236 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002237 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002238
Andrii Kuliancd097992017-03-23 18:31:59 -07002239 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
2240 mDisplayInfo.logicalDensityDpi);
2241 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
2242 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
2243 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002244 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002245 }
2246
Andrii Kuliancd097992017-03-23 18:31:59 -07002247 /**
2248 * If display metrics changed, overrides are not set and it's not just a rotation - update base
2249 * values.
2250 */
2251 private void updateBaseDisplayMetricsIfNeeded() {
2252 // Get real display metrics without overrides from WM.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002253 mWmService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
Andrii Kuliancd097992017-03-23 18:31:59 -07002254 final int orientation = mDisplayInfo.rotation;
2255 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2256 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
2257 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
2258 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002259 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07002260
2261 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
2262 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01002263 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
2264 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07002265
2266 if (displayMetricsChanged) {
2267 // Check if display size or density is forced.
2268 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
2269 || mBaseDisplayHeight != mInitialDisplayHeight;
2270 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
2271
2272 // If there is an override set for base values - use it, otherwise use new values.
2273 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
2274 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
2275 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
2276
2277 // Real display metrics changed, so we should also update initial values.
2278 mInitialDisplayWidth = newWidth;
2279 mInitialDisplayHeight = newHeight;
2280 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01002281 mInitialDisplayCutout = newCutout;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002282 mWmService.reconfigureDisplayLocked(this);
Andrii Kuliancd097992017-03-23 18:31:59 -07002283 }
2284 }
2285
Bryce Lee27cec322017-03-21 09:41:37 -07002286 /** Sets the maximum width the screen resolution can be */
2287 void setMaxUiWidth(int width) {
2288 if (DEBUG_DISPLAY) {
2289 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
2290 }
2291
2292 mMaxUiWidth = width;
2293
2294 // Update existing metrics.
2295 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
2296 }
2297
2298 /** Update base (override) display metrics. */
2299 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
2300 mBaseDisplayWidth = baseWidth;
2301 mBaseDisplayHeight = baseHeight;
2302 mBaseDisplayDensity = baseDensity;
2303
2304 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
2305 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
2306 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
2307 mBaseDisplayWidth = mMaxUiWidth;
2308
2309 if (DEBUG_DISPLAY) {
2310 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
2311 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
2312 + " on display:" + getDisplayId());
2313 }
2314 }
2315
2316 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08002317
2318 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07002319 }
2320
Riddle Hsuf53da812018-08-15 22:00:27 +08002321 /**
2322 * Forces this display to use the specified density.
2323 *
2324 * @param density The density in DPI to use. If the value equals to initial density, the setting
2325 * will be cleared.
2326 * @param userId The target user to apply. Only meaningful when this is default display. If the
2327 * user id is {@link UserHandle#USER_CURRENT}, it means to apply current settings
2328 * so only need to configure display.
2329 */
2330 void setForcedDensity(int density, int userId) {
2331 final boolean clear = density == mInitialDisplayDensity;
2332 final boolean updateCurrent = userId == UserHandle.USER_CURRENT;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002333 if (mWmService.mCurrentUserId == userId || updateCurrent) {
Riddle Hsuf53da812018-08-15 22:00:27 +08002334 mBaseDisplayDensity = density;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002335 mWmService.reconfigureDisplayLocked(this);
Riddle Hsuf53da812018-08-15 22:00:27 +08002336 }
2337 if (updateCurrent) {
2338 // We are applying existing settings so no need to save it again.
2339 return;
2340 }
2341
2342 if (density == mInitialDisplayDensity) {
2343 density = 0;
2344 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002345 mWmService.mDisplayWindowSettings.setForcedDensity(this, density, userId);
Riddle Hsuf53da812018-08-15 22:00:27 +08002346 }
2347
2348 /** @param mode {@link #FORCE_SCALING_MODE_AUTO} or {@link #FORCE_SCALING_MODE_DISABLED}. */
2349 void setForcedScalingMode(@ForceScalingMode int mode) {
2350 if (mode != FORCE_SCALING_MODE_DISABLED) {
2351 mode = FORCE_SCALING_MODE_AUTO;
2352 }
2353
2354 mDisplayScalingDisabled = (mode != FORCE_SCALING_MODE_AUTO);
2355 Slog.i(TAG_WM, "Using display scaling mode: " + (mDisplayScalingDisabled ? "off" : "auto"));
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002356 mWmService.reconfigureDisplayLocked(this);
Riddle Hsuf53da812018-08-15 22:00:27 +08002357
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002358 mWmService.mDisplayWindowSettings.setForcedScalingMode(this, mode);
Riddle Hsuf53da812018-08-15 22:00:27 +08002359 }
2360
2361 /** If the given width and height equal to initial size, the setting will be cleared. */
2362 void setForcedSize(int width, int height) {
2363 final boolean clear = mInitialDisplayWidth == width && mInitialDisplayHeight == height;
2364 if (!clear) {
2365 // Set some sort of reasonable bounds on the size of the display that we will try
2366 // to emulate.
2367 final int minSize = 200;
2368 final int maxScale = 2;
2369 width = Math.min(Math.max(width, minSize), mInitialDisplayWidth * maxScale);
2370 height = Math.min(Math.max(height, minSize), mInitialDisplayHeight * maxScale);
2371 }
2372
2373 Slog.i(TAG_WM, "Using new display size: " + width + "x" + height);
2374 updateBaseDisplayMetrics(width, height, mBaseDisplayDensity);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002375 mWmService.reconfigureDisplayLocked(this);
Riddle Hsuf53da812018-08-15 22:00:27 +08002376
2377 if (clear) {
2378 width = height = 0;
2379 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002380 mWmService.mDisplayWindowSettings.setForcedSize(this, width, height);
Riddle Hsuf53da812018-08-15 22:00:27 +08002381 }
2382
Wale Ogunwaledb506192017-12-08 10:57:32 -08002383 void getStableRect(Rect out) {
2384 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08002385 }
2386
Yunfan Chen279f5582018-12-12 15:24:50 -08002387 void setStackOnDisplay(int stackId, boolean onTop, TaskStack stack) {
2388 if (DEBUG_STACK) {
2389 Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId=" + mDisplayId);
2390 }
Andrii Kulian839def92016-11-02 10:58:58 -07002391
Wale Ogunwale61911492017-10-11 08:50:50 -07002392 mTaskStackContainers.addStackToDisplay(stack, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002393 }
2394
Andrii Kulian51c1b672017-04-07 18:39:32 -07002395 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002396 final DisplayContent prevDc = stack.getDisplayContent();
2397 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002398 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
2399 + " which is not currently attached to any display");
2400 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002401 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07002402 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
2403 + " to its current displayId=" + mDisplayId);
2404 }
2405
Wale Ogunwale61911492017-10-11 08:50:50 -07002406 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07002407 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08002408 }
2409
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002410 @Override
2411 protected void addChild(DisplayChildWindowContainer child,
2412 Comparator<DisplayChildWindowContainer> comparator) {
2413 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2414 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002415
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002416 @Override
2417 protected void addChild(DisplayChildWindowContainer child, int index) {
2418 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2419 }
2420
2421 @Override
2422 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002423 // Only allow removal of direct children from this display if the display is in the process
2424 // of been removed.
2425 if (mRemovingDisplay) {
2426 super.removeChild(child);
2427 return;
2428 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002429 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002430 }
2431
Andrii Kuliand2765632016-12-12 22:26:34 -08002432 @Override
2433 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
2434 // Children of the display are statically ordered, so the real intention here is to perform
2435 // the operation on the display and not the static direct children.
2436 getParent().positionChildAt(position, this, includingParents);
2437 }
2438
Riddle Hsu57831b52018-07-27 00:31:48 +08002439 void positionStackAt(int position, TaskStack child, boolean includingParents) {
2440 mTaskStackContainers.positionChildAt(position, child, includingParents);
Winson Chung59a47ded2018-01-25 17:46:06 +00002441 layoutAndAssignWindowLayersIfNeeded();
2442 }
2443
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002444 /**
Winson Chungc5fe7ff2019-02-19 14:49:25 -08002445 * Returns true if the input point is within an app window.
2446 */
2447 boolean pointWithinAppWindow(int x, int y) {
2448 final int[] targetWindowType = {-1};
2449 final Consumer fn = PooledLambda.obtainConsumer((w, nonArg) -> {
2450 if (targetWindowType[0] != -1) {
2451 return;
2452 }
2453
2454 if (w.isOnScreen() && w.isVisibleLw() && w.getFrameLw().contains(x, y)) {
2455 targetWindowType[0] = w.mAttrs.type;
2456 return;
2457 }
2458 }, PooledLambda.__(WindowState.class), mTmpRect);
2459 forAllWindows(fn, true /* traverseTopToBottom */);
2460 ((PooledConsumer) fn).recycle();
2461 return FIRST_APPLICATION_WINDOW <= targetWindowType[0]
2462 && targetWindowType[0] <= LAST_APPLICATION_WINDOW;
2463 }
2464
2465 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08002466 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07002467 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07002468 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07002469 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002470 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002471 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07002472 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2473 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002474 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002475 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002476 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002477
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002478 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
2479 if (mTmpTaskForResizePointSearchResult.searchDone) {
2480 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07002481 }
2482 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002483 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002484 }
2485
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002486 void updateTouchExcludeRegion() {
2487 final Task focusedTask = (mFocusedApp != null ? mFocusedApp.getTask() : null);
David Stevensee9e2772017-02-09 16:30:27 -08002488 if (focusedTask == null) {
2489 mTouchExcludeRegion.setEmpty();
2490 } else {
2491 mTouchExcludeRegion.set(mBaseDisplayRect);
2492 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
2493 mTmpRect2.setEmpty();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002494 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0;
2495 --stackNdx) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002496 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002497 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
2498 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08002499 }
2500 // If we removed the focused task above, add it back and only leave its
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002501 // outside touch area in the exclusion. TapDetector is not interested in
David Stevensee9e2772017-02-09 16:30:27 -08002502 // any touch inside the focused task itself.
2503 if (!mTmpRect2.isEmpty()) {
2504 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
2505 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08002506 }
lumark90120a82018-08-15 00:33:03 +08002507 if (mInputMethodWindow != null && mInputMethodWindow.isVisibleLw()) {
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002508 // If the input method is visible and the user is typing, we don't want these touch
2509 // events to be intercepted and used to change focus. This would likely cause a
2510 // disappearance of the input method.
lumark90120a82018-08-15 00:33:03 +08002511 mInputMethodWindow.getTouchableRegion(mTmpRegion);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002512 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002513 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002514 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002515 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002516 win.getTouchableRegion(mTmpRegion);
2517 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2518 }
Tiger Huang04dc4cc2019-01-17 18:41:41 +08002519 amendWindowTapExcludeRegion(mTouchExcludeRegion);
Andrii Kulian03c403d2016-11-11 11:14:12 -08002520 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002521 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002522 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002523 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002524 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2525 }
Riddle Hsu2588ab02019-02-25 14:23:56 +08002526 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner6601b7b2013-04-29 10:29:11 -07002527 }
2528
Tiger Huang04dc4cc2019-01-17 18:41:41 +08002529 /**
2530 * Union the region with all the tap exclude region provided by windows on this display.
2531 *
2532 * @param inOutRegion The region to be amended.
2533 */
2534 void amendWindowTapExcludeRegion(Region inOutRegion) {
2535 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2536 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2537 win.amendTapExcludeRegion(inOutRegion);
2538 }
2539 }
2540
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002541 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07002542 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002543 super.switchUser();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002544 mWmService.mWindowsChanged = true;
Winson Chungda20fec2019-04-10 12:19:59 -07002545 mDisplayPolicy.switchUser();
Craig Mautner858d8a62013-04-23 17:08:34 -07002546 }
2547
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07002548 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002549 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2550 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07002551 }
2552 }
2553
Wale Ogunwale10124582016-09-15 20:25:50 -07002554 @Override
2555 void removeIfPossible() {
2556 if (isAnimating()) {
2557 mDeferredRemoval = true;
2558 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002559 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002560 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002561 }
2562
Wale Ogunwale10124582016-09-15 20:25:50 -07002563 @Override
2564 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002565 mRemovingDisplay = true;
2566 try {
Jackal Guoc43a0a62019-04-23 09:15:14 +08002567 if (mParentWindow != null) {
2568 mParentWindow.removeEmbeddedDisplayContent(this);
2569 }
lumark588a3e82018-07-20 18:53:54 +08002570 // Clear all transitions & screen frozen states when removing display.
2571 mOpeningApps.clear();
2572 mClosingApps.clear();
Evan Rosky2289ba12018-11-19 18:28:18 -08002573 mChangingApps.clear();
lumark588a3e82018-07-20 18:53:54 +08002574 mUnknownAppVisibilityController.clear();
2575 mAppTransition.removeAppTransitionTimeoutCallbacks();
2576 handleAnimatingStoppedAndTransition();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002577 mWmService.stopFreezingDisplayLocked();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002578 super.removeImmediately();
2579 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Riddle Hsu2588ab02019-02-25 14:23:56 +08002580 mPointerEventDispatcher.dispose();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002581 mWmService.mAnimator.removeDisplayLocked(mDisplayId);
Louis Chang207f9112018-07-30 08:58:54 +08002582 mWindowingLayer.release();
2583 mOverlayLayer.release();
Jorim Jaggiae962e62018-12-27 17:23:48 +01002584 mInputMonitor.onDisplayRemoved();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002585 } finally {
Riddle Hsu4e611772018-10-31 18:58:28 +08002586 mDisplayReady = false;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002587 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002588 }
Robert Carr679ccb02018-08-08 15:32:35 -07002589
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002590 mWmService.mWindowPlacerLocked.requestTraversal();
Craig Mautner95da1082014-02-24 17:54:35 -08002591 }
2592
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002593 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002594 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002595 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002596 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2597
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002598 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002599 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002600 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002601 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002602 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002603 }
2604
Andrii Kulian0214ed92017-05-16 13:44:05 -07002605 /** @return 'true' if removal of this display content is deferred due to active animation. */
2606 boolean isRemovalDeferred() {
2607 return mDeferredRemoval;
2608 }
2609
Wale Ogunwale10124582016-09-15 20:25:50 -07002610 boolean animateForIme(float interpolatedValue, float animationTarget,
2611 float dividerAnimationTarget) {
2612 boolean updated = false;
2613
Wale Ogunwale61911492017-10-11 08:50:50 -07002614 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2615 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002616 if (stack == null || !stack.isAdjustedForIme()) {
2617 continue;
2618 }
2619
2620 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2621 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2622 updated = true;
2623 } else {
2624 mDividerControllerLocked.mLastAnimationProgress =
2625 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2626 mDividerControllerLocked.mLastDividerProgress =
2627 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2628 updated |= stack.updateAdjustForIme(
2629 mDividerControllerLocked.mLastAnimationProgress,
2630 mDividerControllerLocked.mLastDividerProgress,
2631 false /* force */);
2632 }
2633 if (interpolatedValue >= 1f) {
2634 stack.endImeAdjustAnimation();
2635 }
2636 }
2637
2638 return updated;
2639 }
2640
2641 boolean clearImeAdjustAnimation() {
2642 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002643 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2644 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002645 if (stack != null && stack.isAdjustedForIme()) {
2646 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2647 changed = true;
2648 }
2649 }
2650 return changed;
2651 }
2652
2653 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002654 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2655 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002656 if (stack.isVisible() && stack.isAdjustedForIme()) {
2657 stack.beginImeAdjustAnimation();
2658 }
2659 }
2660 }
2661
2662 void adjustForImeIfNeeded() {
lumark90120a82018-08-15 00:33:03 +08002663 final WindowState imeWin = mInputMethodWindow;
Wale Ogunwale10124582016-09-15 20:25:50 -07002664 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2665 && !mDividerControllerLocked.isImeHideRequested();
Riddle Hsuc6814252019-05-16 17:06:52 +08002666 final TaskStack dockedStack = getSplitScreenPrimaryStack();
2667 final boolean dockVisible = dockedStack != null;
2668 final Task topDockedTask = dockVisible ? dockedStack.getTopChild() : null;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002669 final TaskStack imeTargetStack = mWmService.getImeFocusStackLocked();
Wale Ogunwale10124582016-09-15 20:25:50 -07002670 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2671 imeTargetStack.getDockSide() : DOCKED_INVALID;
2672 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2673 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002674 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002675 final boolean imeHeightChanged = imeVisible &&
2676 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2677
Riddle Hsuc6814252019-05-16 17:06:52 +08002678 // This includes a case where the docked stack is unminimizing and IME is visible for the
2679 // bottom side stack. The condition prevents adjusting the override task bounds for IME to
2680 // the minimized docked stack bounds.
2681 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock()
2682 || (topDockedTask != null && imeOnBottom && !dockedStack.isAdjustedForIme()
2683 && dockedStack.getBounds().height() < topDockedTask.getBounds().height());
2684
Wale Ogunwale10124582016-09-15 20:25:50 -07002685 // The divider could be adjusted for IME position, or be thinner than usual,
2686 // or both. There are three possible cases:
2687 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2688 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2689 // - If IME is not visible, divider is not moved and is normal width.
2690
2691 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002692 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2693 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002694 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002695 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2696 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002697 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2698 } else {
2699 stack.resetAdjustedForIme(false);
2700 }
2701 }
2702 mDividerControllerLocked.setAdjustedForIme(
2703 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2704 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002705 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2706 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002707 stack.resetAdjustedForIme(!dockVisible);
2708 }
2709 mDividerControllerLocked.setAdjustedForIme(
2710 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2711 }
Winson Chung655332c2016-10-31 13:14:28 -07002712 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002713 }
2714
2715 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002716 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2717 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002718 stack.prepareFreezingTaskBounds();
2719 }
2720 }
2721
Wale Ogunwale94744212015-09-21 19:01:47 -07002722 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002723 getBounds(mTmpRect, newRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002724 rotateBounds(mTmpRect, oldRotation, newRotation, bounds);
2725 }
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002726
Evan Rosky39b6f232018-10-30 18:35:41 -07002727 void rotateBounds(Rect parentBounds, int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002728 // Compute a transform matrix to undo the coordinate space transformation,
2729 // and present the window at the same physical position it previously occupied.
2730 final int deltaRotation = deltaRotation(newRotation, oldRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002731 createRotationMatrix(
2732 deltaRotation, parentBounds.width(), parentBounds.height(), mTmpMatrix);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002733
2734 mTmpRectF.set(bounds);
2735 mTmpMatrix.mapRect(mTmpRectF);
2736 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002737 }
2738
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002739 static int deltaRotation(int oldRotation, int newRotation) {
2740 int delta = newRotation - oldRotation;
2741 if (delta < 0) delta += 4;
2742 return delta;
2743 }
2744
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002745 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002746 Matrix outMatrix) {
2747 // For rotations without Z-ordering we don't need the target rectangle's position.
2748 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2749 displayHeight, outMatrix);
2750 }
2751
2752 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2753 float displayWidth, float displayHeight, Matrix outMatrix) {
2754 switch (rotation) {
2755 case ROTATION_0:
2756 outMatrix.reset();
2757 break;
2758 case ROTATION_270:
2759 outMatrix.setRotate(270, 0, 0);
2760 outMatrix.postTranslate(0, displayHeight);
2761 outMatrix.postTranslate(rectTop, 0);
2762 break;
2763 case ROTATION_180:
2764 outMatrix.reset();
2765 break;
2766 case ROTATION_90:
2767 outMatrix.setRotate(90, 0, 0);
2768 outMatrix.postTranslate(displayWidth, 0);
2769 outMatrix.postTranslate(-rectTop, rectLeft);
2770 break;
2771 }
2772 }
2773
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002774 @CallSuper
2775 @Override
Nataniel Borges023ecb52019-01-16 14:15:43 -08002776 public void writeToProto(ProtoOutputStream proto, long fieldId,
2777 @WindowTraceLogLevel int logLevel) {
2778 // Critical log level logs only visible elements to mitigate performance overheard
2779 if (logLevel == WindowTraceLogLevel.CRITICAL && !isVisible()) {
2780 return;
2781 }
2782
Steven Timotiusaf03df62017-07-18 16:56:43 -07002783 final long token = proto.start(fieldId);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002784 super.writeToProto(proto, WINDOW_CONTAINER, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002785 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002786 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2787 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002788 stack.writeToProto(proto, STACKS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002789 }
2790 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2791 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002792 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2793 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002794 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002795 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002796 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2797 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002798 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002799 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002800 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2801 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002802 windowToken.writeToProto(proto, IME_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002803 }
2804 proto.write(DPI, mBaseDisplayDensity);
2805 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002806 proto.write(ROTATION, mRotation);
2807 final ScreenRotationAnimation screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002808 mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002809 if (screenRotationAnimation != null) {
2810 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2811 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002812 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
lumark588a3e82018-07-20 18:53:54 +08002813 mAppTransition.writeToProto(proto, APP_TRANSITION);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002814 if (mFocusedApp != null) {
2815 mFocusedApp.writeNameToProto(proto, FOCUSED_APP);
2816 }
Jorim Jaggi1112fed2019-04-15 13:32:14 +02002817 for (int i = mOpeningApps.size() - 1; i >= 0; i--) {
2818 mOpeningApps.valueAt(i).mActivityRecord.writeIdentifierToProto(proto, OPENING_APPS);
2819 }
2820 for (int i = mClosingApps.size() - 1; i >= 0; i--) {
2821 mClosingApps.valueAt(i).mActivityRecord.writeIdentifierToProto(proto, CLOSING_APPS);
2822 }
2823 for (int i = mChangingApps.size() - 1; i >= 0; i--) {
2824 mChangingApps.valueAt(i).mActivityRecord.writeIdentifierToProto(proto, CHANGING_APPS);
2825 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002826 proto.end(token);
2827 }
2828
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002829 @Override
2830 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2831 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002832 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2833 final String subPrefix = " " + prefix;
2834 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2835 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2836 pw.print("dpi");
2837 if (mInitialDisplayWidth != mBaseDisplayWidth
2838 || mInitialDisplayHeight != mBaseDisplayHeight
2839 || mInitialDisplayDensity != mBaseDisplayDensity) {
2840 pw.print(" base=");
2841 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2842 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2843 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002844 if (mDisplayScalingDisabled) {
2845 pw.println(" noscale");
2846 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002847 pw.print(" cur=");
2848 pw.print(mDisplayInfo.logicalWidth);
2849 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2850 pw.print(" app=");
2851 pw.print(mDisplayInfo.appWidth);
2852 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2853 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2854 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2855 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2856 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002857 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002858 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002859 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002860
Craig Mautnerdc548482014-02-05 13:35:24 -08002861 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002862 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002863 pw.print(prefix);
2864 pw.print("mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
Adrian Roos5251b1d2018-03-23 18:57:43 +01002865
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002866 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
2867 if (mLastFocus != mCurrentFocus) {
2868 pw.print(" mLastFocus="); pw.println(mLastFocus);
2869 }
2870 if (mLosingFocus.size() > 0) {
2871 pw.println();
2872 pw.println(" Windows losing focus:");
2873 for (int i = mLosingFocus.size() - 1; i >= 0; i--) {
2874 final WindowState w = mLosingFocus.get(i);
2875 pw.print(" Losing #"); pw.print(i); pw.print(' ');
2876 pw.print(w);
2877 if (dumpAll) {
2878 pw.println(":");
2879 w.dump(pw, " ", true);
2880 } else {
2881 pw.println();
2882 }
2883 }
2884 }
2885 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
Tiger Huang7c610aa2018-10-27 00:01:01 +08002886 if (mLastStatusBarVisibility != 0) {
2887 pw.print(" mLastStatusBarVisibility=0x");
2888 pw.println(Integer.toHexString(mLastStatusBarVisibility));
2889 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002890
Adrian Roos5251b1d2018-03-23 18:57:43 +01002891 pw.println();
wilsonshihc32538e2018-11-07 17:27:34 +08002892 mWallpaperController.dump(pw, " ");
2893
2894 pw.println();
Adrian Roos4ffc8972019-02-07 20:45:11 +01002895 pw.print("mSystemGestureExclusion=");
2896 if (mSystemGestureExclusionListeners.getRegisteredCallbackCount() > 0) {
2897 pw.println(mSystemGestureExclusion);
2898 } else {
2899 pw.println("<no lstnrs>");
2900 }
2901
2902 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002903 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002904 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2905 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002906 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002907 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002908
Craig Mautnerdc548482014-02-05 13:35:24 -08002909 pw.println();
2910 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002911 pw.println();
2912 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002913 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002914 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002915 pw.print(" Exiting #"); pw.print(i);
2916 pw.print(' '); pw.print(token);
2917 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002918 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002919 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002920 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002921
Jorim Jaggi31f71702016-05-04 16:43:04 -07002922 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002923
2924 // Dump stack references
2925 final TaskStack homeStack = getHomeStack();
2926 if (homeStack != null) {
2927 pw.println(prefix + "homeStack=" + homeStack.getName());
2928 }
2929 final TaskStack pinnedStack = getPinnedStack();
2930 if (pinnedStack != null) {
2931 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2932 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002933 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002934 if (splitScreenPrimaryStack != null) {
2935 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2936 }
2937
2938 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002939 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002940 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002941 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002942
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002943 pw.println();
2944 mDisplayFrames.dump(prefix, pw);
Arthur Hung95b38a92018-07-20 18:56:12 +08002945 pw.println();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08002946 mDisplayPolicy.dump(prefix, pw);
2947 pw.println();
Riddle Hsuad256a12018-07-18 16:11:30 +08002948 mDisplayRotation.dump(prefix, pw);
2949 pw.println();
Arthur Hung95b38a92018-07-20 18:56:12 +08002950 mInputMonitor.dump(pw, " ");
Jorim Jaggif96c90a2018-09-26 16:55:15 +02002951 pw.println();
2952 mInsetsStateController.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002953 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002954
2955 @Override
2956 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002957 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002958 }
2959
2960 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002961 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002962 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002963
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002964 /** Returns true if the stack in the windowing mode is visible. */
2965 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002966 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002967 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002968 }
2969
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002970 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002971 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002972 final int x = (int) xf;
2973 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002974 final WindowState touchedWin = getWindow(w -> {
2975 final int flags = w.mAttrs.flags;
2976 if (!w.isVisibleLw()) {
2977 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002978 }
2979 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002980 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002981 }
2982
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002983 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002984 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002985 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002986 }
2987
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002988 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002989
2990 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002991 return mTmpRegion.contains(x, y) || touchFlags == 0;
2992 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002993
2994 return touchedWin;
2995 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002996
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002997 boolean canAddToastWindowForUid(int uid) {
2998 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002999 // Also if the app is focused adding more than one toast at
3000 // a time for better backwards compatibility.
3001 final WindowState focusedWindowForUid = getWindow(w ->
3002 w.mOwnerUid == uid && w.isFocused());
3003 if (focusedWindowForUid != null) {
3004 return true;
3005 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003006 final WindowState win = getWindow(w ->
3007 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
3008 && !w.mWindowRemovalAllowed);
3009 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07003010 }
3011
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003012 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07003013 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
3014 return;
3015 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003016
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003017 // Used to communicate the old focus to the callback method.
3018 mTmpWindow = oldFocus;
3019
3020 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07003021 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07003022
Louis Changa9350fe2019-04-25 17:14:20 +08003023 /**
3024 * Looking for the focused window on this display if the top focused display hasn't been
3025 * found yet (topFocusedDisplayId is INVALID_DISPLAY) or per-display focused was allowed.
3026 *
3027 * @param topFocusedDisplayId Id of the top focused display.
3028 * @return The focused window or null if there isn't any or no need to seek.
3029 */
3030 WindowState findFocusedWindowIfNeeded(int topFocusedDisplayId) {
3031 return (mWmService.mPerDisplayFocusEnabled || topFocusedDisplayId == INVALID_DISPLAY)
3032 ? findFocusedWindow() : null;
Tiger Huang51c5a1d2018-12-11 20:24:51 +08003033 }
3034
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07003035 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003036 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07003037
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003038 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003039
3040 if (mTmpWindow == null) {
3041 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
3042 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07003043 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003044 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07003045 }
3046
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003047 /**
3048 * Update the focused window and make some adjustments if the focus has changed.
3049 *
3050 * @param mode Indicates the situation we are in. Possible modes are:
3051 * {@link WindowManagerService#UPDATE_FOCUS_NORMAL},
3052 * {@link WindowManagerService#UPDATE_FOCUS_PLACING_SURFACES},
3053 * {@link WindowManagerService#UPDATE_FOCUS_WILL_PLACE_SURFACES},
3054 * {@link WindowManagerService#UPDATE_FOCUS_REMOVING_FOCUS}
3055 * @param updateInputWindows Whether to sync the window information to the input module.
Louis Changa9350fe2019-04-25 17:14:20 +08003056 * @param topFocusedDisplayId Display id of current top focused display.
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003057 * @return {@code true} if the focused window has changed.
3058 */
Louis Changa9350fe2019-04-25 17:14:20 +08003059 boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows,
3060 int topFocusedDisplayId) {
3061 WindowState newFocus = findFocusedWindowIfNeeded(topFocusedDisplayId);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003062 if (mCurrentFocus == newFocus) {
3063 return false;
3064 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003065 boolean imWindowChanged = false;
Tiger Huang51c5a1d2018-12-11 20:24:51 +08003066 final WindowState imWindow = mInputMethodWindow;
3067 if (imWindow != null) {
3068 final WindowState prevTarget = mInputMethodTarget;
3069 final WindowState newTarget = computeImeTarget(true /* updateImeTarget*/);
3070 imWindowChanged = prevTarget != newTarget;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003071
Tiger Huang51c5a1d2018-12-11 20:24:51 +08003072 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS
3073 && mode != UPDATE_FOCUS_WILL_PLACE_SURFACES) {
3074 assignWindowLayers(false /* setLayoutNeeded */);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003075 }
3076 }
3077
3078 if (imWindowChanged) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003079 mWmService.mWindowsChanged = true;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003080 setLayoutNeeded();
Louis Changa9350fe2019-04-25 17:14:20 +08003081 newFocus = findFocusedWindowIfNeeded(topFocusedDisplayId);
Tiger Huang51c5a1d2018-12-11 20:24:51 +08003082 }
3083 if (mCurrentFocus != newFocus) {
3084 mWmService.mH.obtainMessage(REPORT_FOCUS_CHANGE, this).sendToTarget();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003085 }
3086
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003087 if (DEBUG_FOCUS_LIGHT || mWmService.localLOGV) Slog.v(TAG_WM, "Changing focus from "
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003088 + mCurrentFocus + " to " + newFocus + " displayId=" + getDisplayId()
3089 + " Callers=" + Debug.getCallers(4));
3090 final WindowState oldFocus = mCurrentFocus;
3091 mCurrentFocus = newFocus;
3092 mLosingFocus.remove(newFocus);
3093
3094 if (newFocus != null) {
3095 mWinAddedSinceNullFocus.clear();
3096 mWinRemovedSinceNullFocus.clear();
3097
3098 if (newFocus.canReceiveKeys()) {
3099 // Displaying a window implicitly causes dispatching to be unpaused.
3100 // This is to protect against bugs if someone pauses dispatching but
3101 // forgets to resume.
3102 newFocus.mToken.paused = false;
3103 }
3104 }
3105
Tiger Huang7c610aa2018-10-27 00:01:01 +08003106 int focusChanged = getDisplayPolicy().focusChangedLw(oldFocus, newFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003107
3108 if (imWindowChanged && oldFocus != mInputMethodWindow) {
3109 // Focus of the input method window changed. Perform layout if needed.
3110 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
3111 performLayout(true /*initial*/, updateInputWindows);
3112 focusChanged &= ~FINISH_LAYOUT_REDO_LAYOUT;
3113 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
3114 // Client will do the layout, but we need to assign layers
3115 // for handleNewWindowLocked() below.
3116 assignWindowLayers(false /* setLayoutNeeded */);
3117 }
3118 }
3119
3120 if ((focusChanged & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3121 // The change in focus caused us to need to do a layout. Okay.
3122 setLayoutNeeded();
3123 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
3124 performLayout(true /*initial*/, updateInputWindows);
3125 } else if (mode == UPDATE_FOCUS_REMOVING_FOCUS) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003126 mWmService.mRoot.performSurfacePlacement(false);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003127 }
3128 }
3129
3130 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
3131 // If we defer assigning layers, then the caller is responsible for doing this part.
3132 getInputMonitor().setInputFocusLw(newFocus, updateInputWindows);
3133 }
3134
3135 adjustForImeIfNeeded();
3136
3137 // We may need to schedule some toast windows to be removed. The toasts for an app that
3138 // does not have input focus are removed within a timeout to prevent apps to redress
3139 // other apps' UI.
3140 scheduleToastWindowsTimeoutIfNeededLocked(oldFocus, newFocus);
3141
3142 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
3143 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
3144 }
3145 return true;
3146 }
3147
3148 /**
3149 * Set the new focused app to this display.
3150 *
3151 * @param newFocus the new focused AppWindowToken.
3152 * @return true if the focused app is changed.
3153 */
3154 boolean setFocusedApp(AppWindowToken newFocus) {
3155 if (newFocus != null) {
3156 final DisplayContent appDisplay = newFocus.getDisplayContent();
3157 if (appDisplay != this) {
3158 throw new IllegalStateException(newFocus + " is not on " + getName()
3159 + " but " + ((appDisplay != null) ? appDisplay.getName() : "none"));
3160 }
3161 }
3162 if (mFocusedApp == newFocus) {
3163 return false;
3164 }
3165 mFocusedApp = newFocus;
3166 getInputMonitor().setFocusedAppLw(newFocus);
3167 updateTouchExcludeRegion();
3168 return true;
3169 }
3170
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003171 /** Updates the layer assignment of windows on this display. */
3172 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggi4981f152019-03-26 18:58:45 +01003173 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07003174 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003175 if (setLayoutNeeded) {
3176 setLayoutNeeded();
3177 }
Robert Carrb1579c82017-09-05 14:54:47 -07003178
Robert Carrf59b8dd2017-10-02 18:58:36 -07003179 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07003180 // the application of this transaction until the animation pass triggers
3181 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
3182 // the hiding and showing of surfaces.
3183 scheduleAnimation();
Jorim Jaggi4981f152019-03-26 18:58:45 +01003184 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003185 }
3186
Wale Ogunwale1666e312016-12-16 11:27:18 -08003187 // TODO: This should probably be called any time a visual change is made to the hierarchy like
3188 // moving containers or resizing them. Need to investigate the best way to have it automatically
3189 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003190 void layoutAndAssignWindowLayersIfNeeded() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003191 mWmService.mWindowsChanged = true;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003192 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003193
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003194 if (!mWmService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003195 false /*updateInputWindows*/)) {
3196 assignWindowLayers(false /* setLayoutNeeded */);
3197 }
3198
Arthur Hung95b38a92018-07-20 18:56:12 +08003199 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003200 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08003201 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003202 }
3203
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003204 /** Returns true if a leaked surface was destroyed */
3205 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003206 // Used to indicate that a surface was leaked.
3207 mTmpWindow = null;
3208 forAllWindows(w -> {
3209 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003210 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003211 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003212 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003213 if (!mWmService.mSessions.contains(wsa.mSession)) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003214 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003215 + w + " surface=" + wsa.mSurfaceController
3216 + " token=" + w.mToken
3217 + " pid=" + w.mSession.mPid
3218 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003219 wsa.destroySurface();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003220 mWmService.mForceRemoves.add(w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003221 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07003222 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003223 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003224 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02003225 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003226 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003227 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003228 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003229 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003230 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003231
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003232 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003233 }
3234
3235 /**
lumark90120a82018-08-15 00:33:03 +08003236 * Set input method window for the display.
3237 * @param win Set when window added or Null when destroyed.
3238 */
3239 void setInputMethodWindowLocked(WindowState win) {
3240 mInputMethodWindow = win;
lumark70865a82018-09-17 16:56:26 +08003241 // Update display configuration for IME process.
3242 if (mInputMethodWindow != null) {
3243 final int imePid = mInputMethodWindow.mSession.mPid;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003244 mWmService.mAtmInternal.onImeWindowSetOnDisplay(imePid,
lumark70865a82018-09-17 16:56:26 +08003245 mInputMethodWindow.getDisplayId());
3246 }
lumark90120a82018-08-15 00:33:03 +08003247 computeImeTarget(true /* updateImeTarget */);
Jorim Jaggif96c90a2018-09-26 16:55:15 +02003248 mInsetsStateController.getSourceProvider(TYPE_IME).setWindow(win,
3249 null /* frameProvider */);
lumark90120a82018-08-15 00:33:03 +08003250 }
3251
3252 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003253 * Determine and return the window that should be the IME target.
3254 * @param updateImeTarget If true the system IME target will be updated to match what we found.
3255 * @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 +00003256 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003257 WindowState computeImeTarget(boolean updateImeTarget) {
lumark90120a82018-08-15 00:33:03 +08003258 if (mInputMethodWindow == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003259 // There isn't an IME so there shouldn't be a target...That was easy!
3260 if (updateImeTarget) {
3261 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
lumarkff0ab692018-11-05 20:32:30 +08003262 + mInputMethodTarget + " to null since mInputMethodWindow is null");
3263 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003264 }
3265 return null;
3266 }
3267
lumarkff0ab692018-11-05 20:32:30 +08003268 final WindowState curTarget = mInputMethodTarget;
Chavi Weingarten3a748552018-05-14 17:32:42 +00003269 if (!canUpdateImeTarget()) {
3270 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
3271 return curTarget;
3272 }
3273
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003274 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
3275 // same display. Or even when the current IME/target are not on the same screen as the next
3276 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08003277 mUpdateImeTarget = updateImeTarget;
3278 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003279
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003280
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003281 // Yet more tricksyness! If this window is a "starting" window, we do actually want
3282 // to be on top of it, but it is not -really- where input will go. So look down below
3283 // for a real window to target...
3284 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
3285 final AppWindowToken token = target.mAppToken;
3286 if (token != null) {
3287 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
3288 if (betterTarget != null) {
3289 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003290 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003291 }
3292 }
3293
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003294 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
lumarkff0ab692018-11-05 20:32:30 +08003295 "Proposed new IME target: " + target + " for display: " + getDisplayId());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003296
chaviw1c13ad32018-06-12 16:44:23 -07003297 // Now, a special case -- if the last target's window is in the process of exiting, but
Tarandeep Singh2d0aca42019-03-28 14:08:58 -07003298 // not removed, keep on the last target to avoid IME flicker.
chaviw1c13ad32018-06-12 16:44:23 -07003299 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
Tarandeep Singh2d0aca42019-03-28 14:08:58 -07003300 && curTarget.isClosing()) {
3301 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Not changing target till current window is"
3302 + " closing and not removed");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003303 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003304 }
3305
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003306 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
3307 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003308
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003309 if (target == null) {
3310 if (updateImeTarget) {
3311 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
3312 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
3313 + Debug.getCallers(4) : ""));
lumarkff0ab692018-11-05 20:32:30 +08003314 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003315 }
3316
3317 return null;
3318 }
3319
3320 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003321 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
3322 if (token != null) {
3323
3324 // Now some fun for dealing with window animations that modify the Z order. We need
3325 // to look at all windows below the current target that are in this app, finding the
3326 // highest visible one in layering.
3327 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003328 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003329 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003330 }
3331
3332 if (highestTarget != null) {
lumark588a3e82018-07-20 18:53:54 +08003333 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, mAppTransition + " " + highestTarget
Jorim Jaggi35d328a2018-08-14 17:00:20 +02003334 + " animating=" + highestTarget.isAnimating());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003335
lumark588a3e82018-07-20 18:53:54 +08003336 if (mAppTransition.isTransitionSet()) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003337 // If we are currently setting up for an animation, hold everything until we
3338 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003339 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003340 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003341 }
3342 }
3343 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003344
3345 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
3346 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003347 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003348 }
3349
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003350 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003351 }
3352
lumarkff0ab692018-11-05 20:32:30 +08003353 /**
3354 * Calling {@link #computeImeTarget(boolean)} to update the input method target window in
3355 * the candidate app window token if needed.
3356 */
3357 void computeImeTargetIfNeeded(AppWindowToken candidate) {
3358 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == candidate) {
3359 computeImeTarget(true /* updateImeTarget */);
3360 }
3361 }
3362
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003363 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
lumarkff0ab692018-11-05 20:32:30 +08003364 if (target == mInputMethodTarget && mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003365 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003366 }
3367
lumarkff0ab692018-11-05 20:32:30 +08003368 mInputMethodTarget = target;
3369 mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003370 assignWindowLayers(false /* setLayoutNeeded */);
Tarandeep Singh215929b2019-01-11 18:24:37 -08003371 mInsetsStateController.onImeTargetChanged(target);
Tarandeep Singha6f35612019-01-11 19:50:46 -08003372 updateImeParent();
3373 }
3374
3375 private void updateImeParent() {
Jackal Guo818c3f12019-03-08 18:00:39 +08003376 // Force attaching IME to the display when magnifying, or it would be magnified with
3377 // target app together.
3378 final boolean shouldAttachToDisplay = (mMagnificationSpec != null);
3379 final SurfaceControl newParent =
3380 shouldAttachToDisplay ? mWindowingLayer : computeImeParent();
Tarandeep Singha6f35612019-01-11 19:50:46 -08003381 if (newParent != null) {
Tiger Huanged6794e2019-05-07 20:07:59 +08003382 getPendingTransaction().reparent(mImeWindowsContainers.mSurfaceControl, newParent);
Tarandeep Singha6f35612019-01-11 19:50:46 -08003383 scheduleAnimation();
3384 }
3385 }
3386
3387 /**
3388 * Computes the window the IME should be attached to.
3389 */
3390 @VisibleForTesting
3391 SurfaceControl computeImeParent() {
3392
3393 // Attach it to app if the target is part of an app and such app is covering the entire
3394 // screen. If it's not covering the entire screen the IME might extend beyond the apps
3395 // bounds.
Riddle Hsu6d6f67c2019-03-14 16:54:26 +08003396 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken != null
3397 && mInputMethodTarget.getWindowingMode() == WINDOWING_MODE_FULLSCREEN
3398 // An activity with override bounds should be letterboxed inside its parent bounds,
3399 // so it doesn't fill the screen.
3400 && mInputMethodTarget.mAppToken.matchParentBounds()) {
Tarandeep Singha6f35612019-01-11 19:50:46 -08003401 return mInputMethodTarget.mAppToken.getSurfaceControl();
3402 }
3403
3404 // Otherwise, we just attach it to the display.
3405 return mWindowingLayer;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003406 }
3407
3408 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
3409 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3410 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
3411 }
3412
3413 // Used to indicate we have reached the first window in the range we are interested in.
3414 mTmpWindow = null;
3415
3416 // TODO: Figure-out a more efficient way to do this.
3417 final WindowState candidate = getWindow(w -> {
3418 if (w == top) {
3419 // Reached the first window in the range we are interested in.
3420 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003421 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003422 if (mTmpWindow == null) {
3423 return false;
3424 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003425
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003426 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3427 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003428 }
3429 // If we reached the bottom of the range of windows we are considering,
3430 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003431 if (w == bottom) {
3432 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003433 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003434 return false;
3435 });
3436
3437 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003438 }
3439
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003440 void setLayoutNeeded() {
3441 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
3442 mLayoutNeeded = true;
3443 }
3444
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003445 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003446 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
3447 mLayoutNeeded = false;
3448 }
3449
3450 boolean isLayoutNeeded() {
3451 return mLayoutNeeded;
3452 }
3453
Wale Ogunwale02319a62016-09-26 15:21:22 -07003454 void dumpTokens(PrintWriter pw, boolean dumpAll) {
3455 if (mTokenMap.isEmpty()) {
3456 return;
3457 }
3458 pw.println(" Display #" + mDisplayId);
3459 final Iterator<WindowToken> it = mTokenMap.values().iterator();
3460 while (it.hasNext()) {
3461 final WindowToken token = it.next();
3462 pw.print(" ");
3463 pw.print(token);
3464 if (dumpAll) {
3465 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003466 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07003467 } else {
3468 pw.println();
3469 }
3470 }
lumark588a3e82018-07-20 18:53:54 +08003471
Evan Rosky2289ba12018-11-19 18:28:18 -08003472 if (!mOpeningApps.isEmpty() || !mClosingApps.isEmpty() || !mChangingApps.isEmpty()) {
lumark588a3e82018-07-20 18:53:54 +08003473 pw.println();
3474 if (mOpeningApps.size() > 0) {
3475 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
3476 }
3477 if (mClosingApps.size() > 0) {
3478 pw.print(" mClosingApps="); pw.println(mClosingApps);
3479 }
Evan Rosky2289ba12018-11-19 18:28:18 -08003480 if (mChangingApps.size() > 0) {
3481 pw.print(" mChangingApps="); pw.println(mChangingApps);
3482 }
lumark588a3e82018-07-20 18:53:54 +08003483 }
3484
3485 mUnknownAppVisibilityController.dump(pw, " ");
Wale Ogunwale02319a62016-09-26 15:21:22 -07003486 }
3487
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003488 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003489 final int[] index = new int[1];
3490 forAllWindows(w -> {
3491 final WindowStateAnimator wAnim = w.mWinAnimator;
3492 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
3493 index[0] = index[0] + 1;
3494 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003495 }
3496
Jorim Jaggife762342016-10-13 14:33:27 +02003497 /**
3498 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
3499 */
Issei Suzuki5609ccb2019-06-13 15:04:08 +02003500 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade,
3501 boolean subtle) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003502 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003503 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02003504 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
3505 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Issei Suzuki5609ccb2019-06-13 15:04:08 +02003506 w.startAnimation(policy.createHiddenByKeyguardExit(
3507 onWallpaper, goingToShade, subtle));
Jorim Jaggife762342016-10-13 14:33:27 +02003508 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003509 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02003510 }
3511
Wale Ogunwale494009b82016-10-21 09:01:38 -07003512 boolean checkWaitingForWindows() {
3513
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003514 mHaveBootMsg = false;
3515 mHaveApp = false;
3516 mHaveWallpaper = false;
3517 mHaveKeyguard = true;
3518
3519 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003520 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
3521 return true;
3522 }
3523 if (w.isDrawnLw()) {
3524 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003525 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003526 } else if (w.mAttrs.type == TYPE_APPLICATION
3527 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003528 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003529 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003530 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003531 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003532 mHaveKeyguard = mWmService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003533 }
3534 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003535 return false;
3536 });
3537
3538 if (visibleWindow != null) {
3539 // We have a visible window.
3540 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003541 }
3542
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003543 // if the wallpaper service is disabled on the device, we're never going to have
3544 // wallpaper, don't bother waiting for it
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003545 boolean wallpaperEnabled = mWmService.mContext.getResources().getBoolean(
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003546 com.android.internal.R.bool.config_enableWallpaperService)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003547 && mWmService.mContext.getResources().getBoolean(
Daichi Hironoc1432222018-03-29 13:06:07 +09003548 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003549 && !mWmService.mOnlyCore;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003550
Wale Ogunwale494009b82016-10-21 09:01:38 -07003551 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003552 "******** booted=" + mWmService.mSystemBooted
3553 + " msg=" + mWmService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003554 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
3555 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
3556 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003557
3558 // If we are turning on the screen to show the boot message, don't do it until the boot
3559 // message is actually displayed.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003560 if (!mWmService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003561 return true;
3562 }
3563
3564 // If we are turning on the screen after the boot is completed normally, don't do so until
3565 // we have the application and wallpaper.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003566 if (mWmService.mSystemBooted
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003567 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003568 return true;
3569 }
3570
3571 return false;
3572 }
3573
Jorim Jaggi8f520872018-08-14 17:00:20 +02003574 void updateWindowsForAnimator() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003575 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003576 }
3577
Jorim Jaggi8f520872018-08-14 17:00:20 +02003578 /**
3579 * Updates the {@link TaskStack#setAnimationBackground} for all windows.
3580 */
3581 void updateBackgroundForAnimator() {
Jorim Jaggi7156b652016-10-25 08:31:58 -07003582 resetAnimationBackgroundAnimator();
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003583 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003584 }
3585
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003586 boolean isInputMethodClientFocus(int uid, int pid) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003587 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003588 if (imFocus == null) {
3589 return false;
3590 }
3591
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003592 if (DEBUG_INPUT_METHOD) {
3593 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003594 Slog.i(TAG_WM, "Current focus: " + mCurrentFocus + " displayId=" + mDisplayId);
3595 Slog.i(TAG_WM, "Last focus: " + mLastFocus + " displayId=" + mDisplayId);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003596 }
3597
Wale Ogunwale494009b82016-10-21 09:01:38 -07003598 if (DEBUG_INPUT_METHOD) {
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003599 Slog.i(TAG_WM, "IM target uid/pid: " + imFocus.mSession.mUid
3600 + "/" + imFocus.mSession.mPid);
3601 Slog.i(TAG_WM, "Requesting client uid/pid: " + uid + "/" + pid);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003602 }
3603
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003604 return imFocus.mSession.mUid == uid && imFocus.mSession.mPid == pid;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003605 }
3606
3607 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003608 final WindowState win = getWindow(
3609 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
3610 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003611 }
3612
Tiger Huang7c610aa2018-10-27 00:01:01 +08003613 void statusBarVisibilityChanged(int visibility) {
3614 mLastStatusBarVisibility = visibility;
3615 visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(visibility);
3616 updateStatusBarVisibilityLocked(visibility);
3617 }
3618
3619 private boolean updateStatusBarVisibilityLocked(int visibility) {
3620 if (mLastDispatchedSystemUiVisibility == visibility) {
3621 return false;
3622 }
3623 final int globalDiff = (visibility ^ mLastDispatchedSystemUiVisibility)
3624 // We are only interested in differences of one of the
3625 // clearable flags...
3626 & View.SYSTEM_UI_CLEARABLE_FLAGS
3627 // ...if it has actually been cleared.
3628 & ~visibility;
3629
3630 mLastDispatchedSystemUiVisibility = visibility;
3631 if (isDefaultDisplay) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003632 mWmService.mInputManager.setSystemUiVisibility(visibility);
Tiger Huang7c610aa2018-10-27 00:01:01 +08003633 }
3634 updateSystemUiVisibility(visibility, globalDiff);
3635 return true;
3636 }
3637
Wale Ogunwale494009b82016-10-21 09:01:38 -07003638 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003639 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003640 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003641 final int curValue = w.mSystemUiVisibility;
3642 final int diff = (curValue ^ visibility) & globalDiff;
3643 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003644 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003645 w.mSeq++;
3646 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003647 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003648 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
3649 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07003650 visibility, newValue, diff);
3651 }
3652 } catch (RemoteException e) {
3653 // so sorry
3654 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003655 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003656 }
3657
Tiger Huang7c610aa2018-10-27 00:01:01 +08003658 void reevaluateStatusBarVisibility() {
3659 int visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(mLastStatusBarVisibility);
3660 if (updateStatusBarVisibilityLocked(visibility)) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003661 mWmService.mWindowPlacerLocked.requestTraversal();
Tiger Huang7c610aa2018-10-27 00:01:01 +08003662 }
3663 }
3664
Wale Ogunwale494009b82016-10-21 09:01:38 -07003665 void onWindowFreezeTimeout() {
3666 Slog.w(TAG_WM, "Window freeze timeout expired.");
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003667 mWmService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003668
3669 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07003670 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003671 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003672 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07003673 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003674 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003675 - mWmService.mDisplayFreezeTime);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003676 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003677 }, true /* traverseTopToBottom */);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003678 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003679 }
3680
3681 void waitForAllWindowsDrawn() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003682 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003683 forAllWindows(w -> {
3684 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
3685 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
3686 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003687 // Force add to mResizingWindows.
chaviw9c81e632018-07-31 11:17:52 -07003688 w.resetLastContentInsets();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003689 mWmService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003690 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003691 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003692 }
3693
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003694 // TODO: Super crazy long method that should be broken down...
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003695 void applySurfaceChangesTransaction(boolean recoveringMemory) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003696 final WindowSurfacePlacer surfacePlacer = mWmService.mWindowPlacerLocked;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003697
3698 mTmpUpdateAllDrawn.clear();
3699
3700 int repeats = 0;
3701 do {
3702 repeats++;
3703 if (repeats > 6) {
3704 Slog.w(TAG, "Animation repeat aborted after too many iterations");
3705 clearLayoutNeeded();
3706 break;
3707 }
3708
3709 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
3710 pendingLayoutChanges);
3711
wilsonshihc32538e2018-11-07 17:27:34 +08003712 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
3713 mWallpaperController.adjustWallpaperWindows();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003714 }
3715
Riddle Hsu654a6f92018-07-13 22:59:36 +08003716 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003717 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Riddle Hsu4e611772018-10-31 18:58:28 +08003718 if (updateOrientationFromAppTokens()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003719 setLayoutNeeded();
Riddle Hsu4e611772018-10-31 18:58:28 +08003720 sendNewConfiguration();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003721 }
3722 }
3723
3724 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3725 setLayoutNeeded();
3726 }
3727
3728 // FIRST LOOP: Perform a layout, if needed.
3729 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
3730 performLayout(repeats == 1, false /* updateInputWindows */);
3731 } else {
3732 Slog.w(TAG, "Layout repeat skipped after too many iterations");
3733 }
3734
3735 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
3736 pendingLayoutChanges = 0;
3737
Jorim Jaggi4981f152019-03-26 18:58:45 +01003738 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "applyPostLayoutPolicy");
3739 try {
3740 mDisplayPolicy.beginPostLayoutPolicyLw();
3741 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
3742 pendingLayoutChanges |= mDisplayPolicy.finishPostLayoutPolicyLw();
3743 } finally {
3744 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
3745 }
Tiger Huang7c610aa2018-10-27 00:01:01 +08003746 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
3747 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
Jorim Jaggif96c90a2018-09-26 16:55:15 +02003748 mInsetsStateController.onPostLayout();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003749 } while (pendingLayoutChanges != 0);
3750
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003751 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003752
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003753 mTmpRecoveringMemory = recoveringMemory;
Jorim Jaggi4981f152019-03-26 18:58:45 +01003754
3755 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "applyWindowSurfaceChanges");
3756 try {
3757 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
3758 } finally {
3759 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
3760 }
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01003761 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003762
Jorim Jaggif1292892018-09-10 11:58:13 +02003763 mLastHasContent = mTmpApplySurfaceChangesTransactionState.displayHasContent;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003764 mWmService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Jorim Jaggif1292892018-09-10 11:58:13 +02003765 mLastHasContent,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003766 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
3767 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003768 true /* inTraversal, must call performTraversalInTrans... below */);
3769
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003770 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
3771 if (wallpaperVisible != mLastWallpaperVisible) {
3772 mLastWallpaperVisible = wallpaperVisible;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003773 mWmService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003774 }
3775
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003776 while (!mTmpUpdateAllDrawn.isEmpty()) {
3777 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
3778 // See if any windows have been drawn, so they (and others associated with them)
3779 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07003780 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003781 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003782 }
3783
Bryce Leef3c6a472017-11-14 14:53:06 -08003784 private void updateBounds() {
Evan Rosky39b6f232018-10-30 18:35:41 -07003785 calculateBounds(mDisplayInfo, mTmpBounds);
Bryce Leef3c6a472017-11-14 14:53:06 -08003786 setBounds(mTmpBounds);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08003787 if (mPortalWindowHandle != null && mParentSurfaceControl != null) {
3788 mPortalWindowHandle.touchableRegion.getBounds(mTmpRect);
3789 if (!mTmpBounds.equals(mTmpRect)) {
3790 mPortalWindowHandle.touchableRegion.set(mTmpBounds);
Tiger Huanged6794e2019-05-07 20:07:59 +08003791 getPendingTransaction().setInputWindowInfo(
3792 mParentSurfaceControl, mPortalWindowHandle);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08003793 }
3794 }
Bryce Leef3c6a472017-11-14 14:53:06 -08003795 }
3796
3797 // Determines the current display bounds based on the current state
Evan Rosky39b6f232018-10-30 18:35:41 -07003798 private void calculateBounds(DisplayInfo displayInfo, Rect out) {
Bryce Leef3c6a472017-11-14 14:53:06 -08003799 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Evan Rosky39b6f232018-10-30 18:35:41 -07003800 final int rotation = displayInfo.rotation;
3801 boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Bryce Leef3c6a472017-11-14 14:53:06 -08003802 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
3803 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Evan Rosky39b6f232018-10-30 18:35:41 -07003804 int width = displayInfo.logicalWidth;
Bryce Leef3c6a472017-11-14 14:53:06 -08003805 int left = (physWidth - width) / 2;
Evan Rosky39b6f232018-10-30 18:35:41 -07003806 int height = displayInfo.logicalHeight;
Bryce Leef3c6a472017-11-14 14:53:06 -08003807 int top = (physHeight - height) / 2;
3808 out.set(left, top, left + width, top + height);
3809 }
3810
Bryce Leef3c6a472017-11-14 14:53:06 -08003811 private void getBounds(Rect out, int orientation) {
3812 getBounds(out);
3813
3814 // Rotate the Rect if needed.
3815 final int currentRotation = mDisplayInfo.rotation;
3816 final int rotationDelta = deltaRotation(currentRotation, orientation);
3817 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3818 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3819 mTmpRectF.set(out);
3820 mTmpMatrix.mapRect(mTmpRectF);
3821 mTmpRectF.round(out);
3822 }
3823 }
3824
Evan Rosky730f6e82018-12-03 17:40:11 -08003825 /** @returns the orientation of the display when it's rotation is ROTATION_0. */
3826 int getNaturalOrientation() {
3827 return mBaseDisplayWidth < mBaseDisplayHeight
3828 ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
3829 }
3830
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003831 void performLayout(boolean initial, boolean updateInputWindows) {
Jorim Jaggi4981f152019-03-26 18:58:45 +01003832 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "performLayout");
3833 try {
3834 performLayoutNoTrace(initial, updateInputWindows);
3835 } finally {
3836 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
3837 }
3838 }
3839
3840 private void performLayoutNoTrace(boolean initial, boolean updateInputWindows) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003841 if (!isLayoutNeeded()) {
3842 return;
3843 }
3844 clearLayoutNeeded();
3845
3846 final int dw = mDisplayInfo.logicalWidth;
3847 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003848 if (DEBUG_LAYOUT) {
3849 Slog.v(TAG, "-------------------------------------");
Jorim Jaggi4981f152019-03-26 18:58:45 +01003850 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw
3851 + " dh=" + dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003852 }
3853
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003854 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3855 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Jorim Jaggi4981f152019-03-26 18:58:45 +01003856 // TODO: Not sure if we really need to set the rotation here since we are updating from
3857 // the display info above...
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00003858 mDisplayFrames.mRotation = mRotation;
Tiger Huang7c610aa2018-10-27 00:01:01 +08003859 mDisplayPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003860
Adrian Roos5251b1d2018-03-23 18:57:43 +01003861 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003862 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003863 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003864
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003865 // Used to indicate that we have processed the dream window and all additional windows are
3866 // behind it.
3867 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003868 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003869
3870 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003871 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003872
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003873 // Used to indicate that we have processed the dream window and all additional attached
3874 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003875 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003876 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003877
3878 // Now perform layout of attached windows, which usually depend on the position of the
3879 // window they are attached to. XXX does not deal with windows that are attached to windows
3880 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003881 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003882
3883 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003884 mInputMonitor.layoutInputConsumers(dw, dh);
3885 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003886 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003887 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003888 }
3889
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003890 mWmService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003891 }
3892
3893 /**
3894 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3895 * In portrait mode, it grabs the full screenshot.
3896 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003897 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003898 */
chaviw0315a1a2018-03-05 15:28:35 -08003899 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003900 if (!mWmService.mPolicy.isScreenOn()) {
chaviw0315a1a2018-03-05 15:28:35 -08003901 if (DEBUG_SCREENSHOT) {
3902 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003903 }
chaviw0315a1a2018-03-05 15:28:35 -08003904 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003905 }
chaviwfbe47df2017-11-10 16:14:49 -08003906
chaviw0315a1a2018-03-05 15:28:35 -08003907 int dw = mDisplayInfo.logicalWidth;
3908 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003909
chaviw0315a1a2018-03-05 15:28:35 -08003910 if (dw <= 0 || dh <= 0) {
3911 return null;
3912 }
chaviwfbe47df2017-11-10 16:14:49 -08003913
chaviw0315a1a2018-03-05 15:28:35 -08003914 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003915
chaviw0315a1a2018-03-05 15:28:35 -08003916 // The screenshot API does not apply the current screen rotation.
3917 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003918
chaviw0315a1a2018-03-05 15:28:35 -08003919 if (rot == ROTATION_90 || rot == ROTATION_270) {
3920 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3921 }
chaviwfbe47df2017-11-10 16:14:49 -08003922
chaviw0315a1a2018-03-05 15:28:35 -08003923 // SurfaceFlinger is not aware of orientation, so convert our logical
3924 // crop to SurfaceFlinger's portrait orientation.
3925 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3926
3927 final ScreenRotationAnimation screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003928 mWmService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
chaviw0315a1a2018-03-05 15:28:35 -08003929 final boolean inRotation = screenRotationAnimation != null &&
3930 screenRotationAnimation.isAnimating();
3931 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3932
3933 // TODO(b/68392460): We should screenshot Task controls directly
3934 // but it's difficult at the moment as the Task doesn't have the
3935 // correct size set.
chaviw08520a02018-09-10 16:44:56 -07003936 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, inRotation, rot);
chaviw0315a1a2018-03-05 15:28:35 -08003937 if (bitmap == null) {
3938 Slog.w(TAG_WM, "Failed to take screenshot");
3939 return null;
3940 }
3941
3942 // Create a copy of the screenshot that is immutable and backed in ashmem.
3943 // This greatly reduces the overhead of passing the bitmap between processes.
3944 final Bitmap ret = bitmap.createAshmemBitmap(config);
3945 bitmap.recycle();
3946 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003947 }
3948
3949 // TODO: Can this use createRotationMatrix()?
3950 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3951 if (rot == Surface.ROTATION_90) {
3952 final int tmp = crop.top;
3953 crop.top = dw - crop.right;
3954 crop.right = crop.bottom;
3955 crop.bottom = dw - crop.left;
3956 crop.left = tmp;
3957 } else if (rot == Surface.ROTATION_180) {
3958 int tmp = crop.top;
3959 crop.top = dh - crop.bottom;
3960 crop.bottom = dh - tmp;
3961 tmp = crop.right;
3962 crop.right = dw - crop.left;
3963 crop.left = dw - tmp;
3964 } else if (rot == Surface.ROTATION_270) {
3965 final int tmp = crop.top;
3966 crop.top = crop.left;
3967 crop.left = dh - crop.bottom;
3968 crop.bottom = crop.right;
3969 crop.right = dh - tmp;
3970 }
3971 }
3972
3973 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003974 // Used to indicate the layout is needed.
3975 mTmpWindow = null;
3976
3977 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003978 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003979 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003980 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003981 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003982 w.setDisplayLayoutNeeded();
Vishnu Nairba183352018-11-21 11:16:49 -08003983 w.finishSeamlessRotation(true /* timeout */);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003984 mWmService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003985 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003986
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003987 if (mTmpWindow != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003988 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003989 }
3990 }
3991
Wale Ogunwale1666e312016-12-16 11:27:18 -08003992 void setExitingTokensHasVisible(boolean hasVisible) {
3993 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3994 mExitingTokens.get(i).hasVisible = hasVisible;
3995 }
3996
3997 // Initialize state of exiting applications.
3998 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3999 }
4000
4001 void removeExistingTokensIfPossible() {
4002 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
4003 final WindowToken token = mExitingTokens.get(i);
4004 if (!token.hasVisible) {
4005 mExitingTokens.remove(i);
4006 }
4007 }
4008
4009 // Time to remove any exiting applications?
4010 mTaskStackContainers.removeExistingAppTokensIfPossible();
4011 }
4012
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07004013 @Override
4014 void onDescendantOverrideConfigurationChanged() {
4015 setLayoutNeeded();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004016 mWmService.requestTraversal();
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07004017 }
4018
David Stevens9440dc82017-03-16 19:00:20 -07004019 boolean okToDisplay() {
4020 if (mDisplayId == DEFAULT_DISPLAY) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004021 return !mWmService.mDisplayFrozen
4022 && mWmService.mDisplayEnabled && mWmService.mPolicy.isScreenOn();
David Stevens9440dc82017-03-16 19:00:20 -07004023 }
4024 return mDisplayInfo.state == Display.STATE_ON;
4025 }
4026
4027 boolean okToAnimate() {
4028 return okToDisplay() &&
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004029 (mDisplayId != DEFAULT_DISPLAY || mWmService.mPolicy.okToAnimate());
David Stevens9440dc82017-03-16 19:00:20 -07004030 }
4031
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07004032 static final class TaskForResizePointSearchResult {
4033 boolean searchDone;
4034 Task taskForResize;
4035
4036 void reset() {
4037 searchDone = false;
4038 taskForResize = null;
4039 }
4040 }
Robert Carr3b716242016-08-16 16:02:21 -07004041
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004042 private static final class ApplySurfaceChangesTransactionState {
4043 boolean displayHasContent;
4044 boolean obscured;
4045 boolean syswin;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004046 float preferredRefreshRate;
4047 int preferredModeId;
4048
4049 void reset() {
4050 displayHasContent = false;
4051 obscured = false;
4052 syswin = false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004053 preferredRefreshRate = 0;
4054 preferredModeId = 0;
4055 }
4056 }
4057
4058 private static final class ScreenshotApplicationState {
4059 WindowState appWin;
4060 int maxLayer;
4061 int minLayer;
4062 boolean screenshotReady;
4063
4064 void reset(boolean screenshotReady) {
4065 appWin = null;
4066 maxLayer = 0;
4067 minLayer = 0;
4068 this.screenshotReady = screenshotReady;
4069 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
4070 }
4071 }
4072
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004073 /**
4074 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
4075 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
4076 * homogeneous children type which is currently required by sub-classes of
4077 * {@link WindowContainer} class.
4078 */
4079 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
4080
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004081 DisplayChildWindowContainer(WindowManagerService service) {
4082 super(service);
4083 }
4084
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004085 @Override
4086 boolean fillsParent() {
4087 return true;
4088 }
4089
4090 @Override
4091 boolean isVisible() {
4092 return true;
Robert Carr3b716242016-08-16 16:02:21 -07004093 }
4094 }
4095
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004096 /**
4097 * Window container class that contains all containers on this display relating to Apps.
4098 * I.e Activities.
4099 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07004100 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07004101 /**
4102 * A control placed at the appropriate level for transitions to occur.
4103 */
chaviw23ee71c2017-12-18 11:29:41 -08004104 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02004105 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02004106 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004107
Robert Carrf7a7ca82017-12-06 13:17:07 -08004108 /**
4109 * Given that the split-screen divider does not have an AppWindowToken, it
4110 * will have to live inside of a "NonAppWindowContainer", in particular
4111 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
4112 * it will need to be interleaved with some of our children, appearing on top of
4113 * both docked stacks but underneath any assistant stacks.
4114 *
4115 * To solve this problem we have this anchor control, which will always exist so
4116 * we can always assign it the correct value in our {@link #assignChildLayers}.
4117 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
4118 * assign the divider a layer relative to it. This way we prevent linking lifecycle
4119 * events between the two containers.
4120 */
4121 SurfaceControl mSplitScreenDividerAnchor = null;
4122
Wale Ogunwale61911492017-10-11 08:50:50 -07004123 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
4124 // through the list to find them.
4125 private TaskStack mHomeStack = null;
4126 private TaskStack mPinnedStack = null;
4127 private TaskStack mSplitScreenPrimaryStack = null;
4128
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004129 TaskStackContainers(WindowManagerService service) {
4130 super(service);
4131 }
4132
Wale Ogunwale61911492017-10-11 08:50:50 -07004133 /**
4134 * Returns the topmost stack on the display that is compatible with the input windowing mode
4135 * and activity type. Null is no compatible stack on the display.
4136 */
4137 TaskStack getStack(int windowingMode, int activityType) {
4138 if (activityType == ACTIVITY_TYPE_HOME) {
4139 return mHomeStack;
4140 }
4141 if (windowingMode == WINDOWING_MODE_PINNED) {
4142 return mPinnedStack;
4143 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4144 return mSplitScreenPrimaryStack;
4145 }
4146 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
4147 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004148 if (activityType == ACTIVITY_TYPE_UNDEFINED
4149 && windowingMode == stack.getWindowingMode()) {
4150 // Passing in undefined type means we want to match the topmost stack with the
4151 // windowing mode.
4152 return stack;
4153 }
Wale Ogunwale61911492017-10-11 08:50:50 -07004154 if (stack.isCompatible(windowingMode, activityType)) {
4155 return stack;
4156 }
4157 }
4158 return null;
4159 }
4160
4161 @VisibleForTesting
4162 TaskStack getTopStack() {
4163 return mTaskStackContainers.getChildCount() > 0
4164 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
4165 }
4166
4167 TaskStack getHomeStack() {
4168 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
4169 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
4170 }
4171 return mHomeStack;
4172 }
4173
4174 TaskStack getPinnedStack() {
4175 return mPinnedStack;
4176 }
4177
Matthew Ng64e77cf2017-10-31 14:01:31 -07004178 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07004179 return mSplitScreenPrimaryStack;
4180 }
4181
Winson Chunge2d72172018-01-25 17:46:20 +00004182 ArrayList<Task> getVisibleTasks() {
4183 final ArrayList<Task> visibleTasks = new ArrayList<>();
4184 forAllTasks(task -> {
4185 if (task.isVisible()) {
4186 visibleTasks.add(task);
4187 }
4188 });
4189 return visibleTasks;
4190 }
4191
Andrii Kulian839def92016-11-02 10:58:58 -07004192 /**
4193 * Adds the stack to this container.
Andrii Kulian839def92016-11-02 10:58:58 -07004194 */
4195 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07004196 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004197 addChild(stack, onTop);
4198 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07004199 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004200
Wale Ogunwale61911492017-10-11 08:50:50 -07004201 void onStackWindowingModeChanged(TaskStack stack) {
4202 removeStackReferenceIfNeeded(stack);
4203 addStackReferenceIfNeeded(stack);
4204 if (stack == mPinnedStack && getTopStack() != stack) {
4205 // Looks like this stack changed windowing mode to pinned. Move it to the top.
4206 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
4207 }
4208 }
4209
4210 private void addStackReferenceIfNeeded(TaskStack stack) {
4211 if (stack.isActivityTypeHome()) {
4212 if (mHomeStack != null) {
Louis Chang7d0037c2018-08-13 12:42:06 +08004213 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07004214 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang7d0037c2018-08-13 12:42:06 +08004215
Wale Ogunwale61911492017-10-11 08:50:50 -07004216 }
Louis Chang7d0037c2018-08-13 12:42:06 +08004217 mHomeStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07004218 }
4219 final int windowingMode = stack.getWindowingMode();
4220 if (windowingMode == WINDOWING_MODE_PINNED) {
4221 if (mPinnedStack != null) {
4222 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
4223 + mPinnedStack + " already exist on display=" + this
4224 + " stack=" + stack);
4225 }
4226 mPinnedStack = stack;
4227 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4228 if (mSplitScreenPrimaryStack != null) {
4229 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
4230 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
4231 + " already exist on display=" + this + " stack=" + stack);
4232 }
4233 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004234 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07004235 }
4236 }
4237
4238 private void removeStackReferenceIfNeeded(TaskStack stack) {
4239 if (stack == mHomeStack) {
4240 mHomeStack = null;
4241 } else if (stack == mPinnedStack) {
4242 mPinnedStack = null;
4243 } else if (stack == mSplitScreenPrimaryStack) {
4244 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004245 // Re-set the split-screen create mode whenever the split-screen stack is removed.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004246 mWmService.setDockedStackCreateStateLocked(
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004247 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
4248 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07004249 }
Andrii Kulian839def92016-11-02 10:58:58 -07004250 }
4251
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004252 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004253 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
4254 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004255 addChild(stack, addIndex);
4256 setLayoutNeeded();
4257 }
4258
Wale Ogunwale61911492017-10-11 08:50:50 -07004259 @Override
4260 protected void removeChild(TaskStack stack) {
4261 super.removeChild(stack);
4262 removeStackReferenceIfNeeded(stack);
4263 }
Bryce Lee00d586d2017-07-28 20:48:43 -07004264
4265 @Override
4266 boolean isOnTop() {
4267 // Considered always on top
4268 return true;
4269 }
4270
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004271 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08004272 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07004273 if (child.getWindowConfiguration().isAlwaysOnTop()
4274 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08004275 // This stack is always-on-top, override the default behavior.
4276 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
4277
4278 // Moving to its current position, as we must call super but we don't want to
4279 // perform any meaningful action.
4280 final int currentPosition = mChildren.indexOf(child);
4281 super.positionChildAt(currentPosition, child, false /* includingParents */);
4282 return;
4283 }
4284
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004285 final int targetPosition = findPositionForStack(position, child, false /* adding */);
4286 super.positionChildAt(targetPosition, child, includingParents);
4287
Riddle Hsu2f6e1742018-08-23 22:44:36 +08004288 if (includingParents) {
4289 // We still want to move the display of this stack container to top because even the
4290 // target position is adjusted to non-top, the intention of the condition is to have
4291 // higher z-order to gain focus (e.g. moving a task of a fullscreen stack to front
4292 // in a non-top display which is using picture-in-picture mode).
4293 final int topChildPosition = getChildCount() - 1;
4294 if (targetPosition < topChildPosition && position >= topChildPosition) {
4295 getParent().positionChildAt(POSITION_TOP, this /* child */,
4296 true /* includingParents */);
4297 }
4298 }
4299
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004300 setLayoutNeeded();
4301 }
4302
4303 /**
4304 * When stack is added or repositioned, find a proper position for it.
4305 * This will make sure that pinned stack always stays on top.
4306 * @param requestedPosition Position requested by caller.
4307 * @param stack Stack to be added or positioned.
4308 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
4309 * @return The proper position for the stack.
4310 */
4311 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004312 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09004313 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09004314 }
4315
Kazuki Takisef85197b2018-06-18 18:18:36 +09004316 final int topChildPosition = mChildren.size() - 1;
4317 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
4318 for (int i = topChildPosition; i >= 0; --i) {
4319 if (getStacks().get(i) != stack && !getStacks().get(i).isAlwaysOnTop()) {
4320 belowAlwaysOnTopPosition = i;
4321 break;
4322 }
4323 }
4324
4325 // The max possible position we can insert the stack at.
4326 int maxPosition = POSITION_TOP;
4327 // The min possible position we can insert the stack at.
4328 int minPosition = POSITION_BOTTOM;
4329
4330 if (stack.isAlwaysOnTop()) {
4331 if (hasPinnedStack()) {
4332 // Always-on-top stacks go below the pinned stack.
4333 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
4334 }
4335 // Always-on-top stacks need to be above all other stacks.
4336 minPosition = belowAlwaysOnTopPosition !=
4337 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
4338 } else {
4339 // Other stacks need to be below the always-on-top stacks.
4340 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08004341 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09004342 }
4343
4344 int targetPosition = requestedPosition;
4345 targetPosition = Math.min(targetPosition, maxPosition);
4346 targetPosition = Math.max(targetPosition, minPosition);
4347
4348 int prevPosition = getStacks().indexOf(stack);
4349 // The positions we calculated above (maxPosition, minPosition) do not take into
4350 // consideration the following edge cases.
4351 // 1) We need to adjust the position depending on the value "adding".
4352 // 2) When we are moving a stack to another position, we also need to adjust the
4353 // position depending on whether the stack is moving to a higher or lower position.
4354 if ((targetPosition != requestedPosition) &&
4355 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004356 targetPosition++;
4357 }
4358
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004359 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08004360 }
4361
4362 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004363 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
4364 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004365 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004366 if (super.forAllWindows(callback, traverseTopToBottom)) {
4367 return true;
4368 }
4369 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4370 return true;
4371 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004372 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004373 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4374 return true;
4375 }
4376 if (super.forAllWindows(callback, traverseTopToBottom)) {
4377 return true;
4378 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004379 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004380 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004381 }
4382
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004383 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004384 boolean traverseTopToBottom) {
4385 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
4386 // app tokens.
4387 // TODO: Investigate if we need to continue to do this or if we can just process them
4388 // in-order.
4389 if (traverseTopToBottom) {
4390 for (int i = mChildren.size() - 1; i >= 0; --i) {
4391 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4392 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004393 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4394 traverseTopToBottom)) {
4395 return true;
4396 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004397 }
4398 }
4399 } else {
4400 final int count = mChildren.size();
4401 for (int i = 0; i < count; ++i) {
4402 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4403 final int appTokensCount = appTokens.size();
4404 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004405 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4406 traverseTopToBottom)) {
4407 return true;
4408 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004409 }
4410 }
4411 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004412 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004413 }
4414
Wale Ogunwale1666e312016-12-16 11:27:18 -08004415 void setExitingTokensHasVisible(boolean hasVisible) {
4416 for (int i = mChildren.size() - 1; i >= 0; --i) {
4417 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4418 for (int j = appTokens.size() - 1; j >= 0; --j) {
4419 appTokens.get(j).hasVisible = hasVisible;
4420 }
4421 }
4422 }
4423
4424 void removeExistingAppTokensIfPossible() {
4425 for (int i = mChildren.size() - 1; i >= 0; --i) {
4426 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4427 for (int j = appTokens.size() - 1; j >= 0; --j) {
4428 final AppWindowToken token = appTokens.get(j);
lumark588a3e82018-07-20 18:53:54 +08004429 if (!token.hasVisible && !mClosingApps.contains(token)
Wale Ogunwale1666e312016-12-16 11:27:18 -08004430 && (!token.mIsExiting || token.isEmpty())) {
4431 // Make sure there is no animation running on this token, so any windows
4432 // associated with it will be removed as soon as their animations are
4433 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02004434 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004435 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
4436 "performLayout: App token exiting now removed" + token);
4437 token.removeIfPossible();
4438 }
4439 }
4440 }
4441 }
4442
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004443 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004444 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004445 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
4446 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004447 // Apps and their containers are not allowed to specify an orientation while the
Wale Ogunwaled0c225c2018-06-15 14:47:46 -07004448 // docked or freeform stack is visible...except for the home stack if the docked
4449 // stack is minimized and it actually set something and the bounds is different from
4450 // the display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004451 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004452 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004453 && !(mDividerControllerLocked.isHomeStackResizable()
4454 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004455 final int orientation = mHomeStack.getOrientation();
4456 if (orientation != SCREEN_ORIENTATION_UNSET) {
4457 return orientation;
4458 }
4459 }
4460 return SCREEN_ORIENTATION_UNSPECIFIED;
4461 }
4462
4463 final int orientation = super.getOrientation();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004464 boolean isCar = mWmService.mContext.getPackageManager().hasSystemFeature(
Dean Harding3e5a1522017-10-06 09:19:01 -07004465 PackageManager.FEATURE_AUTOMOTIVE);
4466 if (isCar) {
4467 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
4468 // allow anything but the default orientation.
4469 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004470 "Forcing UNSPECIFIED orientation in car for display id=" + mDisplayId
4471 + ". Ignoring " + orientation);
Dean Harding3e5a1522017-10-06 09:19:01 -07004472 return SCREEN_ORIENTATION_UNSPECIFIED;
4473 }
4474
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004475 if (orientation != SCREEN_ORIENTATION_UNSET
4476 && orientation != SCREEN_ORIENTATION_BEHIND) {
4477 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004478 "App is requesting an orientation, return " + orientation
4479 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004480 return orientation;
4481 }
4482
4483 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004484 "No app is requesting an orientation, return " + mLastOrientation
4485 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004486 // The next app has not been requested to be visible, so we keep the current orientation
4487 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08004488 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004489 }
Robert Carrb1579c82017-09-05 14:54:47 -07004490
4491 @Override
4492 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004493 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004494
Robert Carr2f8aa392018-01-31 14:46:51 -08004495 for (int i = 0; i < mChildren.size(); i++) {
4496 final TaskStack s = mChildren.get(i);
4497 s.assignChildLayers(t);
4498 }
4499 }
4500
4501 void assignStackOrdering(SurfaceControl.Transaction t) {
Jorim Jaggibe418292018-03-26 16:14:12 +02004502
Robert Carrf7a7ca82017-12-06 13:17:07 -08004503 final int HOME_STACK_STATE = 0;
4504 final int NORMAL_STACK_STATE = 1;
4505 final int ALWAYS_ON_TOP_STATE = 2;
4506
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004507 int layer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004508 int layerForAnimationLayer = 0;
Jorim Jaggibe418292018-03-26 16:14:12 +02004509 int layerForBoostedAnimationLayer = 0;
Jorim Jaggi391790622018-04-18 15:30:44 +02004510 int layerForHomeAnimationLayer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004511
Robert Carrf7a7ca82017-12-06 13:17:07 -08004512 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
4513 for (int i = 0; i < mChildren.size(); i++) {
4514 final TaskStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08004515 if (state == HOME_STACK_STATE && !s.isActivityTypeHome()) {
4516 continue;
4517 } else if (state == NORMAL_STACK_STATE && (s.isActivityTypeHome()
4518 || s.isAlwaysOnTop())) {
4519 continue;
4520 } else if (state == ALWAYS_ON_TOP_STATE && !s.isAlwaysOnTop()) {
4521 continue;
4522 }
4523 s.assignLayer(t, layer++);
4524 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
4525 t.setLayer(mSplitScreenDividerAnchor, layer++);
4526 }
Winson Chungd41f71d2018-03-16 15:26:07 -07004527 if ((s.isTaskAnimating() || s.isAppAnimating())
4528 && state != ALWAYS_ON_TOP_STATE) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004529 // Ensure the animation layer ends up above the
4530 // highest animating stack and no higher.
4531 layerForAnimationLayer = layer++;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004532 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004533 if (state != ALWAYS_ON_TOP_STATE) {
4534 layerForBoostedAnimationLayer = layer++;
4535 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08004536 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004537 if (state == HOME_STACK_STATE) {
4538 layerForHomeAnimationLayer = layer++;
4539 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004540 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004541 if (mAppAnimationLayer != null) {
4542 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004543 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004544 if (mBoostedAppAnimationLayer != null) {
4545 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
4546 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004547 if (mHomeAppAnimationLayer != null) {
4548 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
4549 }
Robert Carrb1579c82017-09-05 14:54:47 -07004550 }
4551
4552 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02004553 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
4554 switch (animationLayer) {
4555 case ANIMATION_LAYER_BOOSTED:
4556 return mBoostedAppAnimationLayer;
4557 case ANIMATION_LAYER_HOME:
4558 return mHomeAppAnimationLayer;
4559 case ANIMATION_LAYER_STANDARD:
4560 default:
4561 return mAppAnimationLayer;
4562 }
chaviw23ee71c2017-12-18 11:29:41 -08004563 }
4564
Robert Carrf7a7ca82017-12-06 13:17:07 -08004565 SurfaceControl getSplitScreenDividerAnchor() {
4566 return mSplitScreenDividerAnchor;
4567 }
4568
chaviw23ee71c2017-12-18 11:29:41 -08004569 @Override
Riddle Hsu3a4bb612019-01-31 00:02:22 +08004570 void onParentChanged() {
4571 super.onParentChanged();
Robert Carrb1579c82017-09-05 14:54:47 -07004572 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08004573 mAppAnimationLayer = makeChildSurface(null)
4574 .setName("animationLayer")
4575 .build();
Jorim Jaggibe418292018-03-26 16:14:12 +02004576 mBoostedAppAnimationLayer = makeChildSurface(null)
4577 .setName("boostedAnimationLayer")
4578 .build();
Jorim Jaggi391790622018-04-18 15:30:44 +02004579 mHomeAppAnimationLayer = makeChildSurface(null)
4580 .setName("homeAnimationLayer")
4581 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08004582 mSplitScreenDividerAnchor = makeChildSurface(null)
4583 .setName("splitScreenDividerAnchor")
4584 .build();
4585 getPendingTransaction()
4586 .show(mAppAnimationLayer)
Jorim Jaggibe418292018-03-26 16:14:12 +02004587 .show(mBoostedAppAnimationLayer)
Jorim Jaggi391790622018-04-18 15:30:44 +02004588 .show(mHomeAppAnimationLayer)
Robert Carrf7a7ca82017-12-06 13:17:07 -08004589 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08004590 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07004591 } else {
Robert Carr5ea304d2019-02-04 16:04:55 -08004592 mAppAnimationLayer.remove();
chaviw23ee71c2017-12-18 11:29:41 -08004593 mAppAnimationLayer = null;
Robert Carr5ea304d2019-02-04 16:04:55 -08004594 mBoostedAppAnimationLayer.remove();
Jorim Jaggibe418292018-03-26 16:14:12 +02004595 mBoostedAppAnimationLayer = null;
Robert Carr5ea304d2019-02-04 16:04:55 -08004596 mHomeAppAnimationLayer.remove();
Jorim Jaggi391790622018-04-18 15:30:44 +02004597 mHomeAppAnimationLayer = null;
Robert Carr5ea304d2019-02-04 16:04:55 -08004598 mSplitScreenDividerAnchor.remove();
Robert Carrf7a7ca82017-12-06 13:17:07 -08004599 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07004600 }
4601 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004602 }
4603
Robert Carree4d4b92017-11-22 12:21:46 -08004604 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004605 AboveAppWindowContainers(String name, WindowManagerService service) {
4606 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08004607 }
4608
Robert Carrb9506032018-02-13 13:54:00 -08004609 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07004610 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
4611 final SurfaceControl.Builder builder = super.makeChildSurface(child);
4612 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
4613 // To draw above the ColorFade layer during the screen off transition, the
4614 // rounded corner overlays need to be at the root of the surface hierarchy.
4615 // TODO: move the ColorLayer into the display overlay layer such that this is not
4616 // necessary anymore.
4617 builder.setParent(null);
4618 }
4619 return builder;
4620 }
4621
4622 @Override
Robert Carrb9506032018-02-13 13:54:00 -08004623 void assignChildLayers(SurfaceControl.Transaction t) {
4624 assignChildLayers(t, null /* imeContainer */);
4625 }
4626
Robert Carree4d4b92017-11-22 12:21:46 -08004627 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
4628 boolean needAssignIme = imeContainer != null
4629 && imeContainer.getSurfaceControl() != null;
4630 for (int j = 0; j < mChildren.size(); ++j) {
4631 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004632
4633 // See {@link mSplitScreenDividerAnchor}
4634 if (wt.windowType == TYPE_DOCK_DIVIDER) {
4635 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
4636 continue;
4637 }
Vishnu Nair83537a72018-07-19 21:27:48 -07004638 if (wt.mRoundedCornerOverlay) {
4639 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
4640 continue;
4641 }
Robert Carree4d4b92017-11-22 12:21:46 -08004642 wt.assignLayer(t, j);
4643 wt.assignChildLayers(t);
4644
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004645 int layer = mWmService.mPolicy.getWindowLayerFromTypeLw(
Robert Carree4d4b92017-11-22 12:21:46 -08004646 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08004647
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004648 if (needAssignIme && layer >= mWmService.mPolicy.getWindowLayerFromTypeLw(
Robert Carrb8e78fa2017-11-28 13:07:58 -08004649 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004650 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08004651 needAssignIme = false;
4652 }
4653 }
4654 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004655 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08004656 }
4657 }
4658 }
4659
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004660 /**
4661 * Window container class that contains all containers on this display that are not related to
4662 * Apps. E.g. status bar.
4663 */
Robert Carree4d4b92017-11-22 12:21:46 -08004664 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07004665 /**
4666 * Compares two child window tokens returns -1 if the first is lesser than the second in
4667 * terms of z-order and 1 otherwise.
4668 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004669 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07004670 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004671 mWmService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004672 token1.mOwnerCanManageAppTokens)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004673 < mWmService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004674 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004675
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004676 private final Predicate<WindowState> mGetOrientingWindow = w -> {
chaviw15ad49f2019-04-24 15:05:39 -07004677 if (!w.isVisibleLw() || !w.mLegacyPolicyVisibilityAfterAnim) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004678 return false;
4679 }
4680 final int req = w.mAttrs.screenOrientation;
4681 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
4682 || req == SCREEN_ORIENTATION_UNSET) {
4683 return false;
4684 }
4685 return true;
4686 };
4687
Wale Ogunwale3a931692016-11-02 16:49:48 -07004688 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08004689 private final Dimmer mDimmer = new Dimmer(this);
4690 private final Rect mTmpDimBoundsRect = new Rect();
4691
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004692 NonAppWindowContainers(String name, WindowManagerService service) {
4693 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004694 mName = name;
4695 }
4696
4697 void addChild(WindowToken token) {
4698 addChild(token, mWindowComparator);
4699 }
4700
4701 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004702 int getOrientation() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004703 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004704 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004705 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004706
4707 if (win != null) {
4708 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004709 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004710 mLastKeyguardForcedOrientation = req;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004711 if (mWmService.mKeyguardGoingAway) {
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07004712 // Keyguard can't affect the orientation if it is going away...
4713 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
4714 return SCREEN_ORIENTATION_UNSET;
4715 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004716 }
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004717 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req
4718 + " for display id=" + mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08004719 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004720 }
4721
Andrii Kulian8ee72852017-03-10 10:36:45 -08004722 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004723
Jorim Jaggi1c530592018-04-06 15:11:47 +02004724 // Only allow force setting the orientation when all unknown visibilities have been
4725 // resolved, as otherwise we just may be starting another occluding activity.
4726 final boolean isUnoccluding =
lumark588a3e82018-07-20 18:53:54 +08004727 mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
4728 && mUnknownAppVisibilityController.allResolved();
Jorim Jaggi1c530592018-04-06 15:11:47 +02004729 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004730 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004731 }
4732
4733 return SCREEN_ORIENTATION_UNSET;
4734 }
4735
4736 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07004737 String getName() {
4738 return mName;
4739 }
chaviwf29223e2018-01-31 13:23:51 -08004740
4741 @Override
4742 Dimmer getDimmer() {
4743 return mDimmer;
4744 }
4745
4746 @Override
4747 void prepareSurfaces() {
4748 mDimmer.resetDimStates();
4749 super.prepareSurfaces();
4750 getBounds(mTmpDimBoundsRect);
4751
4752 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
4753 scheduleAnimation();
4754 }
4755 }
Robert Carr3b716242016-08-16 16:02:21 -07004756 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01004757
Robert Carrb1579c82017-09-05 14:54:47 -07004758 SurfaceControl.Builder makeSurface(SurfaceSession s) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004759 return mWmService.makeSurfaceBuilder(s)
Robert Carrb1579c82017-09-05 14:54:47 -07004760 .setParent(mWindowingLayer);
4761 }
4762
4763 @Override
4764 SurfaceSession getSession() {
4765 return mSession;
4766 }
4767
4768 @Override
4769 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07004770 SurfaceSession s = child != null ? child.getSession() : getSession();
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +00004771 final SurfaceControl.Builder b = mWmService.makeSurfaceBuilder(s).setContainerLayer();
Robert Carrf59b8dd2017-10-02 18:58:36 -07004772 if (child == null) {
4773 return b;
4774 }
4775
Robert Carree4d4b92017-11-22 12:21:46 -08004776 return b.setName(child.getName())
4777 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004778 }
4779
4780 /**
4781 * The makeSurface variants are for use by the window-container
4782 * hierarchy. makeOverlay here is a function for various non windowing
4783 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
4784 * and other potpourii.
4785 */
4786 SurfaceControl.Builder makeOverlay() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004787 return mWmService.makeSurfaceBuilder(mSession)
Robert Carrb1579c82017-09-05 14:54:47 -07004788 .setParent(mOverlayLayer);
4789 }
4790
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004791 /**
4792 * Reparents the given surface to mOverlayLayer.
4793 */
4794 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
Robert Carr10584fa2019-01-14 15:55:19 -08004795 transaction.reparent(surface, mOverlayLayer);
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004796 }
4797
Robert Carrb1579c82017-09-05 14:54:47 -07004798 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07004799 if (spec.scale != 1.0) {
4800 mMagnificationSpec = spec;
4801 } else {
4802 mMagnificationSpec = null;
4803 }
Jackal Guo818c3f12019-03-08 18:00:39 +08004804 // Re-parent IME's SurfaceControl when MagnificationSpec changed.
4805 updateImeParent();
Robert Carr24be9ab2018-04-30 17:54:53 -07004806
Robert Carrf59b8dd2017-10-02 18:58:36 -07004807 applyMagnificationSpec(getPendingTransaction(), spec);
4808 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07004809 }
4810
Robert Carr24be9ab2018-04-30 17:54:53 -07004811 void reapplyMagnificationSpec() {
4812 if (mMagnificationSpec != null) {
4813 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4814 }
4815 }
4816
Robert Carrb1579c82017-09-05 14:54:47 -07004817 @Override
Riddle Hsu3a4bb612019-01-31 00:02:22 +08004818 void onParentChanged() {
Robert Carrb1579c82017-09-05 14:54:47 -07004819 // Since we are the top of the SurfaceControl hierarchy here
4820 // we create the root surfaces explicitly rather than chaining
Riddle Hsu3a4bb612019-01-31 00:02:22 +08004821 // up as the default implementation in onParentChanged does. So we
Robert Carrb1579c82017-09-05 14:54:47 -07004822 // explicitly do NOT call super here.
4823 }
4824
4825 @Override
4826 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004827
4828 // These are layers as children of "mWindowingLayer"
4829 mBelowAppWindowsContainers.assignLayer(t, 0);
4830 mTaskStackContainers.assignLayer(t, 1);
4831 mAboveAppWindowsContainers.assignLayer(t, 2);
4832
lumarkff0ab692018-11-05 20:32:30 +08004833 final WindowState imeTarget = mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004834 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004835
Robert Carree4d4b92017-11-22 12:21:46 -08004836 // In the case where we have an IME target that is not in split-screen
4837 // mode IME assignment is easy. We just need the IME to go directly above
4838 // the target. This way children of the target will naturally go above the IME
4839 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004840 //
Robert Carree4d4b92017-11-22 12:21:46 -08004841 // In the case of split-screen windowing mode, we need to elevate the IME above the
4842 // docked divider while keeping the app itself below the docked divider, so instead
4843 // we use relative layering of the IME targets child windows, and place the
4844 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004845 //
Robert Carr234b6332018-03-20 13:38:16 -07004846 // In the case the IME target is animating, the animation Z order may be different
4847 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4848 // IME target. In this case we just layer the IME over all transitions by placing it in the
4849 // above applications layer.
4850 //
Robert Carree4d4b92017-11-22 12:21:46 -08004851 // In the case where we have no IME target we assign it where it's base layer would
4852 // place it in the AboveAppWindowContainers.
Tarandeep Singh69ab1042019-07-30 13:30:03 -07004853 //
4854 // Keep IME window in mAboveAppWindowsContainers as long as app's starting window exists
4855 // so it get's layered above the starting window.
4856 if (imeTarget != null
4857 && !(imeTarget.mAppToken != null && imeTarget.mAppToken.hasStartingWindow())
4858 && (!(imeTarget.inSplitScreenWindowingMode() || imeTarget.mToken.isAppAnimating())
4859 && (imeTarget.getSurfaceControl() != null))) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004860 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004861 // TODO: We need to use an extra level on the app surface to ensure
4862 // this is always above SurfaceView but always below attached window.
4863 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004864 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004865 }
4866
4867 // Above we have assigned layers to our children, now we ask them to assign
4868 // layers to their children.
4869 mBelowAppWindowsContainers.assignChildLayers(t);
4870 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004871 mAboveAppWindowsContainers.assignChildLayers(t,
4872 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004873 mImeWindowsContainers.assignChildLayers(t);
4874 }
4875
4876 /**
4877 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4878 * that the IME target is one of the docked applications. We'd like the docked divider to be
4879 * above both of the applications, and we'd like the IME to be above the docked divider.
4880 * However we need child windows of the applications to be above the IME (Text drag handles).
4881 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4882 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
Tiger Huang7c610aa2018-10-27 00:01:01 +08004883 * with {@link WindowState#assignLayer}
Robert Carrb1579c82017-09-05 14:54:47 -07004884 */
4885 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004886 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004887 }
4888
4889 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004890 void prepareSurfaces() {
Jorim Jaggi4981f152019-03-26 18:58:45 +01004891 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "prepareSurfaces");
4892 try {
4893 final ScreenRotationAnimation screenRotationAnimation =
4894 mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Tiger Huanged6794e2019-05-07 20:07:59 +08004895 final Transaction transaction = getPendingTransaction();
Jorim Jaggi4981f152019-03-26 18:58:45 +01004896 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
4897 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
Tiger Huanged6794e2019-05-07 20:07:59 +08004898 transaction.setMatrix(mWindowingLayer,
Jorim Jaggi4981f152019-03-26 18:58:45 +01004899 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
4900 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
Tiger Huanged6794e2019-05-07 20:07:59 +08004901 transaction.setPosition(mWindowingLayer,
Jorim Jaggi4981f152019-03-26 18:58:45 +01004902 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
Tiger Huanged6794e2019-05-07 20:07:59 +08004903 transaction.setAlpha(mWindowingLayer,
Jorim Jaggi4981f152019-03-26 18:58:45 +01004904 screenRotationAnimation.getEnterTransformation().getAlpha());
4905 }
Chavi Weingartenb736e322018-02-23 00:27:54 +00004906
Jorim Jaggi4981f152019-03-26 18:58:45 +01004907 super.prepareSurfaces();
Tiger Huanged6794e2019-05-07 20:07:59 +08004908
4909 // TODO: Once we totally eliminate global transaction we will pass transaction in here
4910 // rather than merging to global.
4911 SurfaceControl.mergeToGlobalTransaction(transaction);
Jorim Jaggi4981f152019-03-26 18:58:45 +01004912 } finally {
4913 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
4914 }
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004915 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004916
Jorim Jaggibe418292018-03-26 16:14:12 +02004917 void assignStackOrdering() {
4918 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004919 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004920
4921 /**
4922 * Increment the deferral count to determine whether to update the IME target.
4923 */
4924 void deferUpdateImeTarget() {
4925 mDeferUpdateImeTargetCount++;
4926 }
4927
4928 /**
4929 * Decrement the deferral count to determine whether to update the IME target. If the count
4930 * reaches 0, a new ime target will get computed.
4931 */
4932 void continueUpdateImeTarget() {
4933 if (mDeferUpdateImeTargetCount == 0) {
4934 return;
4935 }
4936
4937 mDeferUpdateImeTargetCount--;
4938 if (mDeferUpdateImeTargetCount == 0) {
4939 computeImeTarget(true /* updateImeTarget */);
4940 }
4941 }
4942
4943 /**
4944 * @return Whether a new IME target should be computed.
4945 */
4946 private boolean canUpdateImeTarget() {
4947 return mDeferUpdateImeTargetCount == 0;
4948 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004949
4950 InputMonitor getInputMonitor() {
4951 return mInputMonitor;
4952 }
Jorim Jaggif1292892018-09-10 11:58:13 +02004953
4954 /**
4955 * @return Cached value whether we told display manager that we have content.
4956 */
4957 boolean getLastHasContent() {
4958 return mLastHasContent;
4959 }
Arthur Hungbe5ce212018-09-13 18:41:56 +08004960
4961 void registerPointerEventListener(@NonNull PointerEventListener listener) {
Riddle Hsu2588ab02019-02-25 14:23:56 +08004962 mPointerEventDispatcher.registerInputEventListener(listener);
Arthur Hungbe5ce212018-09-13 18:41:56 +08004963 }
4964
4965 void unregisterPointerEventListener(@NonNull PointerEventListener listener) {
Riddle Hsu2588ab02019-02-25 14:23:56 +08004966 mPointerEventDispatcher.unregisterInputEventListener(listener);
Arthur Hungbe5ce212018-09-13 18:41:56 +08004967 }
lumark588a3e82018-07-20 18:53:54 +08004968
4969 void prepareAppTransition(@WindowManager.TransitionType int transit,
Wale Ogunwale3a256e62018-12-06 14:41:18 -08004970 boolean alwaysKeepCurrent) {
4971 prepareAppTransition(transit, alwaysKeepCurrent, 0 /* flags */, false /* forceOverride */);
4972 }
4973
4974 void prepareAppTransition(@WindowManager.TransitionType int transit,
lumark588a3e82018-07-20 18:53:54 +08004975 boolean alwaysKeepCurrent, @WindowManager.TransitionFlags int flags,
4976 boolean forceOverride) {
4977 final boolean prepared = mAppTransition.prepareAppTransitionLocked(
4978 transit, alwaysKeepCurrent, flags, forceOverride);
4979 if (prepared && okToAnimate()) {
4980 mSkipAppTransitionAnimation = false;
4981 }
4982 }
4983
4984 void executeAppTransition() {
4985 if (mAppTransition.isTransitionSet()) {
4986 if (DEBUG_APP_TRANSITIONS) {
4987 Slog.w(TAG_WM, "Execute app transition: " + mAppTransition + ", displayId: "
4988 + mDisplayId + " Callers=" + Debug.getCallers(5));
4989 }
4990 mAppTransition.setReady();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004991 mWmService.mWindowPlacerLocked.requestTraversal();
lumark588a3e82018-07-20 18:53:54 +08004992 }
4993 }
4994
4995 /**
4996 * Update pendingLayoutChanges after app transition has finished.
4997 */
4998 void handleAnimatingStoppedAndTransition() {
4999 int changes = 0;
5000
5001 mAppTransition.setIdle();
5002
5003 for (int i = mNoAnimationNotifyOnTransitionFinished.size() - 1; i >= 0; i--) {
5004 final IBinder token = mNoAnimationNotifyOnTransitionFinished.get(i);
5005 mAppTransition.notifyAppTransitionFinishedLocked(token);
5006 }
5007 mNoAnimationNotifyOnTransitionFinished.clear();
5008
5009 mWallpaperController.hideDeferredWallpapersIfNeeded();
5010
5011 onAppTransitionDone();
5012
5013 changes |= FINISH_LAYOUT_REDO_LAYOUT;
5014 if (DEBUG_WALLPAPER_LIGHT) {
5015 Slog.v(TAG_WM, "Wallpaper layer changed: assigning layers + relayout");
5016 }
5017 computeImeTarget(true /* updateImeTarget */);
wilsonshihc32538e2018-11-07 17:27:34 +08005018 mWallpaperMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08005019 // Since the window list has been rebuilt, focus might have to be recomputed since the
5020 // actual order of windows might have changed again.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08005021 mWmService.mFocusMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08005022
5023 pendingLayoutChanges |= changes;
5024 }
Andrii Kulian15cfb422018-11-07 13:38:49 -08005025
Wale Ogunwale3a256e62018-12-06 14:41:18 -08005026 /** Check if pending app transition is for activity / task launch. */
5027 boolean isNextTransitionForward() {
5028 final int transit = mAppTransition.getAppTransition();
5029 return transit == TRANSIT_ACTIVITY_OPEN
5030 || transit == TRANSIT_TASK_OPEN
5031 || transit == TRANSIT_TASK_TO_FRONT;
5032 }
5033
Andrii Kulian15cfb422018-11-07 13:38:49 -08005034 /**
5035 * @see Display#FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS
5036 */
5037 boolean supportsSystemDecorations() {
Arthur Hung879a8ab2019-03-13 19:51:10 +08005038 return (mWmService.mDisplayWindowSettings.shouldShowSystemDecorsLocked(this)
Andrii Kuliandd989612019-02-21 12:13:28 -08005039 || (mDisplay.getFlags() & FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS) != 0
Chilun891aed22019-05-02 18:08:47 +08005040 || (mWmService.mForceDesktopModeOnExternalDisplays && !isUntrustedVirtualDisplay()))
Arthur Hung879a8ab2019-03-13 19:51:10 +08005041 // VR virtual display will be used to run and render 2D app within a VR experience.
5042 && mDisplayId != mWmService.mVr2dDisplayId;
Andrii Kulian15cfb422018-11-07 13:38:49 -08005043 }
chaviwff2e7d82018-11-02 11:11:27 -07005044
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005045 /**
Chilun891aed22019-05-02 18:08:47 +08005046 * @return {@code true} if the display is non-system created virtual display.
5047 */
5048 boolean isUntrustedVirtualDisplay() {
5049 return mDisplay.getType() == Display.TYPE_VIRTUAL
5050 && mDisplay.getOwnerUid() != Process.SYSTEM_UID;
5051 }
5052
5053 /**
chaviwff2e7d82018-11-02 11:11:27 -07005054 * Re-parent the DisplayContent's top surfaces, {@link #mWindowingLayer} and
Tiger Huangd8ec9382019-04-18 14:35:09 -07005055 * {@link #mOverlayLayer} to the specified SurfaceControl.
chaviwff2e7d82018-11-02 11:11:27 -07005056 *
Tiger Huangd8ec9382019-04-18 14:35:09 -07005057 * @param win The window which owns the SurfaceControl. This indicates the z-order of the
5058 * windows of this display against the windows on the parent display.
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005059 * @param sc The new SurfaceControl, where the DisplayContent's surfaces will be re-parented to.
chaviwff2e7d82018-11-02 11:11:27 -07005060 */
Tiger Huangd8ec9382019-04-18 14:35:09 -07005061 void reparentDisplayContent(WindowState win, SurfaceControl sc) {
5062 mParentWindow = win;
Jackal Guoc43a0a62019-04-23 09:15:14 +08005063 mParentWindow.addEmbeddedDisplayContent(this);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005064 mParentSurfaceControl = sc;
5065 if (mPortalWindowHandle == null) {
5066 mPortalWindowHandle = createPortalWindowHandle(sc.toString());
5067 }
Tiger Huanged6794e2019-05-07 20:07:59 +08005068 getPendingTransaction().setInputWindowInfo(sc, mPortalWindowHandle)
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005069 .reparent(mWindowingLayer, sc).reparent(mOverlayLayer, sc);
5070 }
5071
Tiger Huangd8ec9382019-04-18 14:35:09 -07005072 /**
5073 * Get the window which owns the surface that this DisplayContent is re-parented to.
5074 *
5075 * @return the parent window.
5076 */
5077 WindowState getParentWindow() {
5078 return mParentWindow;
5079 }
5080
5081 /**
5082 * Update the location of this display in the parent window. This enables windows in this
5083 * display to compute the global transformation matrix.
5084 *
5085 * @param win The parent window of this display.
5086 * @param x The x coordinate in the parent window.
5087 * @param y The y coordinate in the parent window.
5088 */
5089 void updateLocation(WindowState win, int x, int y) {
5090 if (mParentWindow != win) {
5091 throw new IllegalArgumentException(
5092 "The given window is not the parent window of this display.");
5093 }
Jackal Guoc43a0a62019-04-23 09:15:14 +08005094 if (!mLocationInParentWindow.equals(x, y)) {
5095 mLocationInParentWindow.set(x, y);
Tiger Huangd8ec9382019-04-18 14:35:09 -07005096 if (mWmService.mAccessibilityController != null) {
5097 mWmService.mAccessibilityController.onSomeWindowResizedOrMovedLocked();
5098 }
Jackal Guoc43a0a62019-04-23 09:15:14 +08005099 notifyLocationInParentDisplayChanged();
Tiger Huangd8ec9382019-04-18 14:35:09 -07005100 }
5101 }
5102
5103 Point getLocationInParentWindow() {
5104 return mLocationInParentWindow;
5105 }
5106
Jackal Guoc43a0a62019-04-23 09:15:14 +08005107 Point getLocationInParentDisplay() {
5108 final Point location = new Point();
5109 if (mParentWindow != null) {
5110 // LocationInParentWindow indicates the offset to (0,0) of window, but what we need is
5111 // the offset to (0,0) of display.
5112 DisplayContent dc = this;
5113 do {
5114 final WindowState displayParent = dc.getParentWindow();
5115 location.x += displayParent.getFrameLw().left
5116 + (dc.getLocationInParentWindow().x * displayParent.mGlobalScale + 0.5f);
5117 location.y += displayParent.getFrameLw().top
5118 + (dc.getLocationInParentWindow().y * displayParent.mGlobalScale + 0.5f);
5119 dc = displayParent.getDisplayContent();
5120 } while (dc != null && dc.getParentWindow() != null);
5121 }
5122 return location;
5123 }
5124
5125 void notifyLocationInParentDisplayChanged() {
5126 forAllWindows(w -> {
5127 w.updateLocationInParentDisplayIfNeeded();
5128 }, false /* traverseTopToBottom */);
5129 }
5130
Tarandeep Singha6f35612019-01-11 19:50:46 -08005131 @VisibleForTesting
5132 SurfaceControl getWindowingLayer() {
5133 return mWindowingLayer;
5134 }
5135
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005136 /**
Adrian Roos4ffc8972019-02-07 20:45:11 +01005137 * Updates the display's system gesture exclusion.
5138 *
5139 * @return true, if the exclusion changed.
5140 */
5141 boolean updateSystemGestureExclusion() {
5142 if (mSystemGestureExclusionListeners.getRegisteredCallbackCount() == 0) {
5143 // No one's interested anyways.
5144 return false;
5145 }
5146
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005147 final Region systemGestureExclusion = Region.obtain();
5148 mSystemGestureExclusionWasRestricted = calculateSystemGestureExclusion(
5149 systemGestureExclusion, mSystemGestureExclusionUnrestricted);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005150 try {
5151 if (mSystemGestureExclusion.equals(systemGestureExclusion)) {
5152 return false;
5153 }
5154 mSystemGestureExclusion.set(systemGestureExclusion);
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005155 final Region unrestrictedOrNull = mSystemGestureExclusionWasRestricted
5156 ? mSystemGestureExclusionUnrestricted : null;
Adrian Roos4ffc8972019-02-07 20:45:11 +01005157 for (int i = mSystemGestureExclusionListeners.beginBroadcast() - 1; i >= 0; --i) {
5158 try {
5159 mSystemGestureExclusionListeners.getBroadcastItem(i)
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005160 .onSystemGestureExclusionChanged(mDisplayId, systemGestureExclusion,
5161 unrestrictedOrNull);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005162 } catch (RemoteException e) {
5163 Slog.e(TAG, "Failed to notify SystemGestureExclusionListener", e);
5164 }
5165 }
5166 mSystemGestureExclusionListeners.finishBroadcast();
5167 return true;
5168 } finally {
5169 systemGestureExclusion.recycle();
5170 }
5171 }
5172
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005173 /**
5174 * Calculates the system gesture exclusion.
5175 *
5176 * @param outExclusion will be set to the gesture exclusion region
5177 * @param outExclusionUnrestricted will be set to the gesture exclusion region without
5178 * any restrictions applied.
5179 * @return whether any restrictions were applied, i.e. outExclusion and outExclusionUnrestricted
5180 * differ.
5181 */
Adrian Roos4ffc8972019-02-07 20:45:11 +01005182 @VisibleForTesting
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005183 boolean calculateSystemGestureExclusion(Region outExclusion, @Nullable
5184 Region outExclusionUnrestricted) {
5185 outExclusion.setEmpty();
5186 if (outExclusionUnrestricted != null) {
5187 outExclusionUnrestricted.setEmpty();
5188 }
Adrian Roos506267d2019-06-18 16:13:53 +02005189 final Region unhandled = Region.obtain();
5190 unhandled.set(0, 0, mDisplayFrames.mDisplayWidth, mDisplayFrames.mDisplayHeight);
5191
5192 final Rect leftEdge = mInsetsStateController.getSourceProvider(TYPE_LEFT_GESTURES)
5193 .getSource().getFrame();
5194 final Rect rightEdge = mInsetsStateController.getSourceProvider(TYPE_RIGHT_GESTURES)
5195 .getSource().getFrame();
5196
Adrian Roos4ffc8972019-02-07 20:45:11 +01005197 final Region touchableRegion = Region.obtain();
5198 final Region local = Region.obtain();
Adrian Roos506267d2019-06-18 16:13:53 +02005199 final int[] remainingLeftRight =
5200 {mSystemGestureExclusionLimit, mSystemGestureExclusionLimit};
Adrian Roos4ffc8972019-02-07 20:45:11 +01005201
Adrian Roosb1063792019-06-28 12:10:51 +02005202 // Traverse all windows top down to assemble the gesture exclusion rects.
Adrian Roos4ffc8972019-02-07 20:45:11 +01005203 // For each window, we only take the rects that fall within its touchable region.
5204 forAllWindows(w -> {
5205 if (w.cantReceiveTouchInput() || !w.isVisible()
Adrian Roos506267d2019-06-18 16:13:53 +02005206 || (w.getAttrs().flags & FLAG_NOT_TOUCHABLE) != 0
5207 || unhandled.isEmpty()) {
Adrian Roos4ffc8972019-02-07 20:45:11 +01005208 return;
5209 }
Adrian Roos4ffc8972019-02-07 20:45:11 +01005210
Adrian Roos506267d2019-06-18 16:13:53 +02005211 // Get the touchable region of the window, and intersect with where the screen is still
5212 // touchable, i.e. touchable regions on top are not covering it yet.
Adrian Roosb1063792019-06-28 12:10:51 +02005213 w.getEffectiveTouchableRegion(touchableRegion);
Adrian Roos506267d2019-06-18 16:13:53 +02005214 touchableRegion.op(unhandled, Op.INTERSECT);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005215
Adrian Roos019a52b2019-07-02 16:47:44 +02005216 if (w.isImplicitlyExcludingAllSystemGestures()) {
5217 local.set(touchableRegion);
5218 } else {
5219 rectListToRegion(w.getSystemGestureExclusion(), local);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005220
Adrian Roos019a52b2019-07-02 16:47:44 +02005221 // Transform to display coordinates
5222 local.scale(w.mGlobalScale);
5223 final Rect frame = w.getWindowFrames().mFrame;
5224 local.translate(frame.left, frame.top);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005225
Adrian Roos019a52b2019-07-02 16:47:44 +02005226 // A window can only exclude system gestures where it is actually touchable
5227 local.op(touchableRegion, Op.INTERSECT);
5228 }
Adrian Roos4ffc8972019-02-07 20:45:11 +01005229
Adrian Roos506267d2019-06-18 16:13:53 +02005230 // Apply restriction if necessary.
5231 if (needsGestureExclusionRestrictions(w, mLastDispatchedSystemUiVisibility)) {
5232
5233 // Processes the region along the left edge.
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005234 remainingLeftRight[0] = addToGlobalAndConsumeLimit(local, outExclusion, leftEdge,
5235 remainingLeftRight[0], w, EXCLUSION_LEFT);
Adrian Roos506267d2019-06-18 16:13:53 +02005236
5237 // Processes the region along the right edge.
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005238 remainingLeftRight[1] = addToGlobalAndConsumeLimit(local, outExclusion, rightEdge,
5239 remainingLeftRight[1], w, EXCLUSION_RIGHT);
Adrian Roos506267d2019-06-18 16:13:53 +02005240
5241 // Adds the middle (unrestricted area)
5242 final Region middle = Region.obtain(local);
5243 middle.op(leftEdge, Op.DIFFERENCE);
5244 middle.op(rightEdge, Op.DIFFERENCE);
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005245 outExclusion.op(middle, Op.UNION);
Adrian Roos506267d2019-06-18 16:13:53 +02005246 middle.recycle();
5247 } else {
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005248 boolean loggable = needsGestureExclusionRestrictions(w, 0 /* lastSysUiVis */);
5249 if (loggable) {
5250 addToGlobalAndConsumeLimit(local, outExclusion, leftEdge,
5251 Integer.MAX_VALUE, w, EXCLUSION_LEFT);
5252 addToGlobalAndConsumeLimit(local, outExclusion, rightEdge,
5253 Integer.MAX_VALUE, w, EXCLUSION_RIGHT);
5254 }
5255 outExclusion.op(local, Op.UNION);
5256 }
5257 if (outExclusionUnrestricted != null) {
5258 outExclusionUnrestricted.op(local, Op.UNION);
Adrian Roos506267d2019-06-18 16:13:53 +02005259 }
5260 unhandled.op(touchableRegion, Op.DIFFERENCE);
5261 }, true /* topToBottom */);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005262 local.recycle();
5263 touchableRegion.recycle();
Adrian Roos506267d2019-06-18 16:13:53 +02005264 unhandled.recycle();
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005265 return remainingLeftRight[0] < mSystemGestureExclusionLimit
5266 || remainingLeftRight[1] < mSystemGestureExclusionLimit;
Adrian Roos4ffc8972019-02-07 20:45:11 +01005267 }
5268
Adrian Roos506267d2019-06-18 16:13:53 +02005269 /**
5270 * @return Whether gesture exclusion area should be restricted from the window depending on the
5271 * current SystemUI visibility flags.
5272 */
5273 private static boolean needsGestureExclusionRestrictions(WindowState win, int sysUiVisibility) {
5274 final int type = win.mAttrs.type;
5275 final int stickyHideNavFlags =
5276 SYSTEM_UI_FLAG_HIDE_NAVIGATION | SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
5277 final boolean stickyHideNav =
5278 (sysUiVisibility & stickyHideNavFlags) == stickyHideNavFlags;
5279 return !stickyHideNav && type != TYPE_INPUT_METHOD && type != TYPE_STATUS_BAR
5280 && win.getActivityType() != ACTIVITY_TYPE_HOME;
5281 }
5282
5283 /**
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005284 * @return Whether gesture exclusion area should be logged for the given window
5285 */
5286 static boolean logsGestureExclusionRestrictions(WindowState win) {
5287 if (win.mWmService.mSystemGestureExclusionLogDebounceTimeoutMillis <= 0) {
5288 return false;
5289 }
5290 final WindowManager.LayoutParams attrs = win.getAttrs();
5291 final int type = attrs.type;
5292 return type != TYPE_WALLPAPER
5293 && type != TYPE_APPLICATION_STARTING
5294 && type != TYPE_NAVIGATION_BAR
5295 && (attrs.flags & FLAG_NOT_TOUCHABLE) == 0
5296 && needsGestureExclusionRestrictions(win, 0 /* sysUiVisibility */)
5297 && win.getDisplayContent().mDisplayPolicy.hasSideGestures();
5298 }
5299
5300 /**
Adrian Roos506267d2019-06-18 16:13:53 +02005301 * Adds a local gesture exclusion area to the global area while applying a limit per edge.
5302 *
5303 * @param local The gesture exclusion area to add.
5304 * @param global The destination.
5305 * @param edge Only processes the part in that region.
5306 * @param limit How much limit in pixels we have.
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005307 * @param win The WindowState that is being processed
5308 * @param side The side that is being processed, either {@link WindowState#EXCLUSION_LEFT} or
5309 * {@link WindowState#EXCLUSION_RIGHT}
5310 * @return How much of the limit is remaining.
Adrian Roos506267d2019-06-18 16:13:53 +02005311 */
5312 private static int addToGlobalAndConsumeLimit(Region local, Region global, Rect edge,
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005313 int limit, WindowState win, int side) {
Adrian Roos506267d2019-06-18 16:13:53 +02005314 final Region r = Region.obtain(local);
5315 r.op(edge, Op.INTERSECT);
5316
5317 final int[] remaining = {limit};
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005318 final int[] requestedExclusion = {0};
Adrian Roos2351d5f2019-07-03 15:35:07 +02005319 forEachRectReverse(r, rect -> {
Adrian Roos506267d2019-06-18 16:13:53 +02005320 if (remaining[0] <= 0) {
5321 return;
5322 }
5323 final int height = rect.height();
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005324 requestedExclusion[0] += height;
Adrian Roos506267d2019-06-18 16:13:53 +02005325 if (height > remaining[0]) {
Adrian Roos2351d5f2019-07-03 15:35:07 +02005326 rect.top = rect.bottom - remaining[0];
Adrian Roos506267d2019-06-18 16:13:53 +02005327 }
5328 remaining[0] -= height;
5329 global.op(rect, Op.UNION);
5330 });
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005331
5332 final int grantedExclusion = limit - remaining[0];
5333 win.setLastExclusionHeights(side, requestedExclusion[0], grantedExclusion);
5334
Adrian Roos506267d2019-06-18 16:13:53 +02005335 r.recycle();
5336 return remaining[0];
5337 }
5338
Adrian Roos4ffc8972019-02-07 20:45:11 +01005339 void registerSystemGestureExclusionListener(ISystemGestureExclusionListener listener) {
5340 mSystemGestureExclusionListeners.register(listener);
5341 final boolean changed;
5342 if (mSystemGestureExclusionListeners.getRegisteredCallbackCount() == 1) {
5343 changed = updateSystemGestureExclusion();
5344 } else {
5345 changed = false;
5346 }
5347
5348 if (!changed) {
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005349 final Region unrestrictedOrNull = mSystemGestureExclusionWasRestricted
5350 ? mSystemGestureExclusionUnrestricted : null;
Adrian Roos4ffc8972019-02-07 20:45:11 +01005351 // If updateSystemGestureExclusion changed the exclusion, it will already have
5352 // notified the listener. Otherwise, we'll do it here.
5353 try {
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005354 listener.onSystemGestureExclusionChanged(mDisplayId, mSystemGestureExclusion,
5355 unrestrictedOrNull);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005356 } catch (RemoteException e) {
5357 Slog.e(TAG, "Failed to notify SystemGestureExclusionListener during register", e);
5358 }
5359 }
5360 }
5361
5362 void unregisterSystemGestureExclusionListener(ISystemGestureExclusionListener listener) {
5363 mSystemGestureExclusionListeners.unregister(listener);
5364 }
5365
5366 /**
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005367 * Create a portal window handle for input. This window transports any touch to the display
5368 * indicated by {@link InputWindowHandle#portalToDisplayId} if the touch hits this window.
5369 *
5370 * @param name The name of the portal window handle.
5371 * @return the new portal window handle.
5372 */
5373 private InputWindowHandle createPortalWindowHandle(String name) {
5374 // Let surface flinger to set the display ID of this input window handle because we don't
5375 // know which display the parent surface control is on.
5376 final InputWindowHandle portalWindowHandle = new InputWindowHandle(
5377 null /* inputApplicationHandle */, null /* clientWindow */, INVALID_DISPLAY);
5378 portalWindowHandle.name = name;
5379 portalWindowHandle.token = new Binder();
5380 portalWindowHandle.layoutParamsFlags =
5381 FLAG_SPLIT_TOUCH | FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL;
5382 getBounds(mTmpBounds);
5383 portalWindowHandle.touchableRegion.set(mTmpBounds);
5384 portalWindowHandle.scaleFactor = 1f;
5385 portalWindowHandle.ownerPid = Process.myPid();
5386 portalWindowHandle.ownerUid = Process.myUid();
5387 portalWindowHandle.portalToDisplayId = mDisplayId;
5388 return portalWindowHandle;
chaviwff2e7d82018-11-02 11:11:27 -07005389 }
Issei Suzukia5dbf522019-02-01 17:58:15 +01005390
5391 /**
5392 * @see IWindowManager#setForwardedInsets
5393 */
5394 public void setForwardedInsets(Insets insets) {
5395 if (insets == null) {
5396 insets = Insets.NONE;
5397 }
5398 if (mDisplayPolicy.getForwardedInsets().equals(insets)) {
5399 return;
5400 }
5401 mDisplayPolicy.setForwardedInsets(insets);
5402 setLayoutNeeded();
5403 mWmService.mWindowPlacerLocked.requestTraversal();
5404 }
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05005405
5406 protected MetricsLogger getMetricsLogger() {
5407 if (mMetricsLogger == null) {
5408 mMetricsLogger = new MetricsLogger();
5409 }
5410 return mMetricsLogger;
5411 }
Craig Mautner59c00972012-07-30 12:10:24 -07005412}