blob: 3e7fea2ac203cef8545b89ab18899ee91c6cbb0c [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 }
lumark280cb212019-09-09 09:47:22 +08001075 if (token.hasChild(prevDc.mLastFocus)) {
1076 // If the reparent window token contains previous display's last focus window, means
1077 // it will end up to gain window focus on the target display, so it should not be
1078 // notified that it lost focus from the previous display.
Riddle Hsu85bd04b2018-11-17 00:34:36 +08001079 prevDc.mLastFocus = null;
1080 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001081 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -08001082
1083 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -07001084 }
1085
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001086 void removeAppToken(IBinder binder) {
1087 final WindowToken token = removeWindowToken(binder);
1088 if (token == null) {
1089 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
1090 return;
1091 }
1092
1093 final AppWindowToken appToken = token.asAppWindowToken();
1094
1095 if (appToken == null) {
1096 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
1097 return;
1098 }
1099
1100 appToken.onRemovedFromDisplay();
1101 }
1102
Riddle Hsuad256a12018-07-18 16:11:30 +08001103 @Override
1104 public Display getDisplay() {
Craig Mautnerb47bbc32012-08-22 17:41:48 -07001105 return mDisplay;
1106 }
1107
Craig Mautner59c00972012-07-30 12:10:24 -07001108 DisplayInfo getDisplayInfo() {
1109 return mDisplayInfo;
1110 }
1111
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001112 DisplayMetrics getDisplayMetrics() {
1113 return mDisplayMetrics;
1114 }
1115
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001116 DisplayPolicy getDisplayPolicy() {
1117 return mDisplayPolicy;
1118 }
1119
Riddle Hsuad256a12018-07-18 16:11:30 +08001120 @Override
1121 public DisplayRotation getDisplayRotation() {
1122 return mDisplayRotation;
1123 }
1124
Jorim Jaggif96c90a2018-09-26 16:55:15 +02001125 /**
1126 * Marks a window as providing insets for the rest of the windows in the system.
1127 *
1128 * @param type The type of inset this window provides.
1129 * @param win The window.
1130 * @param frameProvider Function to compute the frame, or {@code null} if the just the frame of
1131 * the window should be taken.
1132 */
1133 void setInsetProvider(@InternalInsetType int type, WindowState win,
1134 @Nullable TriConsumer<DisplayFrames, WindowState, Rect> frameProvider) {
1135 mInsetsStateController.getSourceProvider(type).setWindow(win, frameProvider);
1136 }
1137
1138 InsetsStateController getInsetsStateController() {
1139 return mInsetsStateController;
1140 }
1141
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001142 @VisibleForTesting
1143 void setDisplayRotation(DisplayRotation displayRotation) {
1144 mDisplayRotation = displayRotation;
1145 }
1146
Andrii Kulian8ee72852017-03-10 10:36:45 -08001147 int getRotation() {
1148 return mRotation;
1149 }
1150
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001151 @VisibleForTesting
Andrii Kulian8ee72852017-03-10 10:36:45 -08001152 void setRotation(int newRotation) {
1153 mRotation = newRotation;
Riddle Hsuad256a12018-07-18 16:11:30 +08001154 mDisplayRotation.setRotation(newRotation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001155 }
1156
1157 int getLastOrientation() {
1158 return mLastOrientation;
1159 }
1160
Andrii Kulian8ee72852017-03-10 10:36:45 -08001161 int getLastWindowForcedOrientation() {
1162 return mLastWindowForcedOrientation;
1163 }
1164
Evan Rosky966759f2019-01-15 10:33:58 -08001165 void registerRemoteAnimations(RemoteAnimationDefinition definition) {
1166 mAppTransitionController.registerRemoteAnimations(definition);
1167 }
1168
Andrii Kulian06d07d62017-03-14 11:11:47 -07001169 /**
Riddle Hsu654a6f92018-07-13 22:59:36 +08001170 * Temporarily pauses rotation changes until resumed.
1171 *
1172 * This can be used to prevent rotation changes from occurring while the user is
1173 * performing certain operations, such as drag and drop.
1174 *
1175 * This call nests and must be matched by an equal number of calls to
1176 * {@link #resumeRotationLocked}.
1177 */
1178 void pauseRotationLocked() {
1179 mDeferredRotationPauseCount++;
1180 }
1181
1182 /**
1183 * Resumes normal rotation changes after being paused.
1184 */
1185 void resumeRotationLocked() {
1186 if (mDeferredRotationPauseCount <= 0) {
1187 return;
1188 }
1189
1190 mDeferredRotationPauseCount--;
1191 if (mDeferredRotationPauseCount == 0) {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001192 updateRotationAndSendNewConfigIfNeeded();
Riddle Hsu654a6f92018-07-13 22:59:36 +08001193 }
1194 }
1195
1196 /**
Riddle Hsuad256a12018-07-18 16:11:30 +08001197 * If this is true we have updated our desired orientation, but not yet changed the real
1198 * orientation our applied our screen rotation animation. For example, because a previous
1199 * screen rotation was in progress.
1200 *
1201 * @return {@code true} if the there is an ongoing rotation change.
1202 */
1203 boolean rotationNeedsUpdate() {
1204 final int lastOrientation = getLastOrientation();
1205 final int oldRotation = getRotation();
Riddle Hsuad256a12018-07-18 16:11:30 +08001206
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001207 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Garfield Tan4bb83472019-01-16 14:37:04 -08001208 return oldRotation != rotation;
Riddle Hsuad256a12018-07-18 16:11:30 +08001209 }
1210
Wale Ogunwale3a256e62018-12-06 14:41:18 -08001211 /**
1212 * The display content may have configuration set from {@link #DisplayWindowSettings}. This
1213 * callback let the owner of container know there is existing configuration to prevent the
1214 * values from being replaced by the initializing {@link #ActivityDisplay}.
1215 */
1216 void initializeDisplayOverrideConfiguration() {
1217 if (mAcitvityDisplay != null) {
1218 mAcitvityDisplay.onInitializeOverrideConfiguration(getRequestedOverrideConfiguration());
1219 }
1220 }
1221
Riddle Hsu4e611772018-10-31 18:58:28 +08001222 /** Notify the configuration change of this display. */
1223 void sendNewConfiguration() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001224 mWmService.mH.obtainMessage(SEND_NEW_CONFIGURATION, this).sendToTarget();
Riddle Hsu4e611772018-10-31 18:58:28 +08001225 }
1226
Garfield Tan90b04282018-12-11 14:04:42 -08001227 @Override
1228 boolean onDescendantOrientationChanged(IBinder freezeDisplayToken,
1229 ConfigurationContainer requestingContainer) {
1230 final Configuration config = updateOrientationFromAppTokens(
1231 getRequestedOverrideConfiguration(), freezeDisplayToken, false);
Garfield Tan49dae102019-02-04 09:51:59 -08001232 // If display rotation class tells us that it doesn't consider app requested orientation,
1233 // this display won't rotate just because of an app changes its requested orientation. Thus
1234 // it indicates that this display chooses not to handle this request.
1235 final boolean handled = getDisplayRotation().respectAppRequestedOrientation();
Garfield Tan90b04282018-12-11 14:04:42 -08001236 if (config == null) {
1237 return handled;
1238 }
1239
1240 if (handled && requestingContainer instanceof ActivityRecord) {
1241 final ActivityRecord activityRecord = (ActivityRecord) requestingContainer;
1242 final boolean kept = mWmService.mAtmService.updateDisplayOverrideConfigurationLocked(
1243 config, activityRecord, false /* deferResume */, getDisplayId());
1244 activityRecord.frozenBeforeDestroy = true;
1245 if (!kept) {
1246 mWmService.mAtmService.mRootActivityContainer.resumeFocusedStacksTopActivities();
1247 }
1248 } else {
1249 // We have a new configuration to push so we need to update ATMS for now.
1250 // TODO: Clean up display configuration push between ATMS and WMS after unification.
1251 mWmService.mAtmService.updateDisplayOverrideConfigurationLocked(
1252 config, null /* starting */, false /* deferResume */, getDisplayId());
1253 }
1254 return handled;
1255 }
1256
Garfield Tan49dae102019-02-04 09:51:59 -08001257 @Override
1258 boolean handlesOrientationChangeFromDescendant() {
1259 return getDisplayRotation().respectAppRequestedOrientation();
1260 }
1261
Riddle Hsu4e611772018-10-31 18:58:28 +08001262 /**
1263 * Determine the new desired orientation of this display.
1264 *
1265 * The orientation is computed from non-application windows first. If none of the
1266 * non-application windows specify orientation, the orientation is computed from application
1267 * tokens.
1268 *
1269 * @return {@code true} if the orientation is changed.
1270 */
1271 boolean updateOrientationFromAppTokens() {
1272 return updateOrientationFromAppTokens(false /* forceUpdate */);
1273 }
1274
Garfield Tan90b04282018-12-11 14:04:42 -08001275 /**
1276 * Update orientation of the target display, returning a non-null new Configuration if it has
1277 * changed from the current orientation. If a non-null configuration is returned, someone must
1278 * call {@link WindowManagerService#setNewDisplayOverrideConfiguration(Configuration,
1279 * DisplayContent)} to tell the window manager it can unfreeze the screen. This will typically
1280 * be done by calling {@link WindowManagerService#sendNewConfiguration(int)}.
1281 */
1282 Configuration updateOrientationFromAppTokens(Configuration currentConfig,
1283 IBinder freezeDisplayToken, boolean forceUpdate) {
1284 if (!mDisplayReady) {
1285 return null;
1286 }
1287
1288 Configuration config = null;
1289 if (updateOrientationFromAppTokens(forceUpdate)) {
1290 // If we changed the orientation but mOrientationChangeComplete is already true,
1291 // we used seamless rotation, and we don't need to freeze the screen.
1292 if (freezeDisplayToken != null && !mWmService.mRoot.mOrientationChangeComplete) {
1293 final AppWindowToken atoken = getAppWindowToken(freezeDisplayToken);
1294 if (atoken != null) {
1295 atoken.startFreezingScreen();
1296 }
1297 }
1298 config = new Configuration();
1299 computeScreenConfiguration(config);
1300 } else if (currentConfig != null) {
1301 // No obvious action we need to take, but if our current state mismatches the
1302 // activity manager's, update it, disregarding font scale, which should remain set
1303 // to the value of the previous configuration.
1304 // Here we're calling Configuration#unset() instead of setToDefaults() because we
1305 // need to keep override configs clear of non-empty values (e.g. fontSize).
1306 mTmpConfiguration.unset();
1307 mTmpConfiguration.updateFrom(currentConfig);
1308 computeScreenConfiguration(mTmpConfiguration);
1309 if (currentConfig.diff(mTmpConfiguration) != 0) {
1310 mWaitingForConfig = true;
1311 setLayoutNeeded();
1312 int[] anim = new int[2];
1313 getDisplayPolicy().selectRotationAnimationLw(anim);
1314
1315 mWmService.startFreezingDisplayLocked(anim[0], anim[1], this);
1316 config = new Configuration(mTmpConfiguration);
1317 }
1318 }
1319
1320 return config;
1321 }
1322
1323
1324 private boolean updateOrientationFromAppTokens(boolean forceUpdate) {
Riddle Hsu4e611772018-10-31 18:58:28 +08001325 final int req = getOrientation();
1326 if (req != mLastOrientation || forceUpdate) {
1327 mLastOrientation = req;
1328 mDisplayRotation.setCurrentOrientation(req);
1329 return updateRotationUnchecked(forceUpdate);
1330 }
1331 return false;
1332 }
1333
Riddle Hsuad256a12018-07-18 16:11:30 +08001334 /**
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001335 * Update rotation of the display and send configuration if the rotation is changed.
1336 *
1337 * @return {@code true} if the rotation has been changed and the new config is sent.
1338 */
1339 boolean updateRotationAndSendNewConfigIfNeeded() {
1340 final boolean changed = updateRotationUnchecked(false /* forceUpdate */);
1341 if (changed) {
Riddle Hsu4e611772018-10-31 18:58:28 +08001342 sendNewConfiguration();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001343 }
1344 return changed;
1345 }
1346
1347 /**
Andrii Kulian06d07d62017-03-14 11:11:47 -07001348 * Update rotation of the display.
1349 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001350 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1351 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001352 */
Robert Carrae606b42018-02-15 15:36:23 -08001353 boolean updateRotationUnchecked() {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001354 return updateRotationUnchecked(false /* forceUpdate */);
1355 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001356
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001357 /**
Evan Roskye747c3e2018-10-30 20:06:41 -07001358 * Update rotation of the DisplayContent with an option to force the update. This updates
1359 * the container's perception of rotation and, depending on the top activities, will freeze
1360 * the screen or start seamless rotation. The display itself gets rotated in
1361 * {@link #applyRotationLocked} during {@link WindowManagerService#sendNewConfiguration}.
1362 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001363 * @param forceUpdate Force the rotation update. Sometimes in WM we might skip updating
1364 * orientation because we're waiting for some rotation to finish or display
1365 * to unfreeze, which results in configuration of the previously visible
1366 * activity being applied to a newly visible one. Forcing the rotation
1367 * update allows to workaround this issue.
1368 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
Evan Roskye747c3e2018-10-30 20:06:41 -07001369 * {@link WindowManagerService#sendNewConfiguration(int)} TO COMPLETE THE ROTATION AND
1370 * UNFREEZE THE SCREEN.
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001371 */
1372 boolean updateRotationUnchecked(boolean forceUpdate) {
1373 ScreenRotationAnimation screenRotationAnimation;
1374 if (!forceUpdate) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001375 if (mDeferredRotationPauseCount > 0) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001376 // Rotation updates have been paused temporarily. Defer the update until
1377 // updates have been resumed.
1378 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
1379 return false;
1380 }
1381
1382 screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001383 mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001384 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
1385 // Rotation updates cannot be performed while the previous rotation change
1386 // animation is still in progress. Skip this update. We will try updating
1387 // again after the animation is finished and the display is unfrozen.
1388 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
1389 return false;
1390 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001391 if (mWmService.mDisplayFrozen) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001392 // Even if the screen rotation animation has finished (e.g. isAnimating
1393 // returns false), there is still some time where we haven't yet unfrozen
1394 // the display. We also need to abort rotation here.
1395 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
1396 "Deferring rotation, still finishing previous rotation");
1397 return false;
1398 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001399 }
1400
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001401 if (!mWmService.mDisplayEnabled) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001402 // No point choosing a rotation if the display is not enabled.
1403 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
1404 return false;
1405 }
1406
1407 final int oldRotation = mRotation;
1408 final int lastOrientation = mLastOrientation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001409 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001410 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Computed rotation=" + rotation + " for display id="
1411 + mDisplayId + " based on lastOrientation=" + lastOrientation
1412 + " and oldRotation=" + oldRotation);
Tiger Huang7c610aa2018-10-27 00:01:01 +08001413 boolean mayRotateSeamlessly = mDisplayPolicy.shouldRotateSeamlessly(mDisplayRotation,
Riddle Hsuad256a12018-07-18 16:11:30 +08001414 oldRotation, rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001415
Robert Carr0e007272017-10-02 13:00:55 -07001416 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001417 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001418 if (seamlessRotated != null && !forceUpdate) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001419 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1420 // to complete (that is, waiting for windows to redraw). It's tempting to check
1421 // w.mSeamlessRotationCount but that could be incorrect in the case of
1422 // window-removal.
1423 return false;
1424 }
Robert Carr0e007272017-10-02 13:00:55 -07001425
1426 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001427 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001428 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001429 mayRotateSeamlessly = false;
1430 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001431 for (int i = 0; i < mWmService.mSessions.size(); i++) {
1432 if (mWmService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
Robert Carr0e007272017-10-02 13:00:55 -07001433 mayRotateSeamlessly = false;
1434 break;
1435 }
1436 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001437 }
Robert Carr0e007272017-10-02 13:00:55 -07001438 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001439
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001440 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1441 + " selected orientation " + lastOrientation
Garfield Tan49dae102019-02-04 09:51:59 -08001442 + ", got rotation " + rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001443
Garfield Tan4bb83472019-01-16 14:37:04 -08001444 if (oldRotation == rotation) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001445 // No change.
1446 return false;
1447 }
1448
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001449 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1450 + " rotation changed to " + rotation
Garfield Tan4bb83472019-01-16 14:37:04 -08001451 + " from " + oldRotation
1452 + ", lastOrientation=" + lastOrientation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001453
1454 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
Vishnu Nairba183352018-11-21 11:16:49 -08001455 mWaitingForConfig = true;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001456 }
1457
Evan Roskye747c3e2018-10-30 20:06:41 -07001458 mRotation = rotation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001459
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001460 mWmService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1461 mWmService.mH.sendNewMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
Riddle Hsu654a6f92018-07-13 22:59:36 +08001462 this, WINDOW_FREEZE_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001463
1464 setLayoutNeeded();
1465 final int[] anim = new int[2];
Tiger Huang7c610aa2018-10-27 00:01:01 +08001466 mDisplayPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001467
1468 if (!rotateSeamlessly) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001469 mWmService.startFreezingDisplayLocked(anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001470 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001471 } else {
1472 // The screen rotation animation uses a screenshot to freeze the screen
1473 // while windows resize underneath.
1474 // When we are rotating seamlessly, we allow the elements to transition
1475 // to their rotated state independently and without a freeze required.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001476 mWmService.startSeamlessRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001477 }
1478
Evan Roskye747c3e2018-10-30 20:06:41 -07001479 return true;
1480 }
1481
1482 /**
1483 * Applies the rotation transaction. This must be called after {@link #updateRotationUnchecked}
1484 * (if it returned {@code true}) to actually finish the rotation.
1485 *
1486 * @param oldRotation the rotation we are coming from.
1487 * @param rotation the rotation to apply.
1488 */
1489 void applyRotationLocked(final int oldRotation, final int rotation) {
1490 mDisplayRotation.setRotation(rotation);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001491 final boolean rotateSeamlessly = mWmService.isRotatingSeamlessly();
Evan Roskye747c3e2018-10-30 20:06:41 -07001492 ScreenRotationAnimation screenRotationAnimation = rotateSeamlessly
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001493 ? null : mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001494 // We need to update our screen size information to match the new rotation. If the rotation
1495 // has actually changed then this method will return true and, according to the comment at
1496 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1497 // By updating the Display info here it will be available to
1498 // #computeScreenConfiguration() later.
Tiger Huanga817b1f2019-05-09 20:04:17 +08001499 updateDisplayAndOrientation(getConfiguration().uiMode, null /* outConfig */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001500
Robert Carrae606b42018-02-15 15:36:23 -08001501 // NOTE: We disable the rotation in the emulator because
1502 // it doesn't support hardware OpenGL emulation yet.
1503 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1504 && screenRotationAnimation.hasScreenshot()) {
1505 if (screenRotationAnimation.setRotation(getPendingTransaction(), rotation,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001506 MAX_ANIMATION_DURATION, mWmService.getTransitionAnimationScaleLocked(),
Robert Carrae606b42018-02-15 15:36:23 -08001507 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001508 mWmService.scheduleAnimationLocked();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001509 }
1510 }
1511
Vishnu Nair83537a72018-07-19 21:27:48 -07001512 forAllWindows(w -> {
Vishnu Nairddd80742018-08-21 14:12:46 -07001513 w.seamlesslyRotateIfAllowed(getPendingTransaction(), oldRotation, rotation,
1514 rotateSeamlessly);
Vishnu Nair83537a72018-07-19 21:27:48 -07001515 }, true /* traverseTopToBottom */);
1516
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001517 mWmService.mDisplayManagerInternal.performTraversal(getPendingTransaction());
Robert Carrae606b42018-02-15 15:36:23 -08001518 scheduleAnimation();
1519
Andrii Kulian06d07d62017-03-14 11:11:47 -07001520 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001521 if (w.mHasSurface && !rotateSeamlessly) {
1522 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001523 w.setOrientationChanging(true);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001524 mWmService.mRoot.mOrientationChangeComplete = false;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001525 w.mLastFreezeDuration = 0;
1526 }
1527 w.mReportOrientationChanged = true;
1528 }, true /* traverseTopToBottom */);
1529
1530 if (rotateSeamlessly) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001531 mWmService.mH.sendNewMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
Riddle Hsu654a6f92018-07-13 22:59:36 +08001532 this, SEAMLESS_ROTATION_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001533 }
1534
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001535 for (int i = mWmService.mRotationWatchers.size() - 1; i >= 0; i--) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001536 final WindowManagerService.RotationWatcher rotationWatcher
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001537 = mWmService.mRotationWatchers.get(i);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001538 if (rotationWatcher.mDisplayId == mDisplayId) {
1539 try {
1540 rotationWatcher.mWatcher.onRotationChanged(rotation);
1541 } catch (RemoteException e) {
1542 // Ignore
1543 }
1544 }
1545 }
1546
Andrii Kulian06d07d62017-03-14 11:11:47 -07001547 // Announce rotation only if we will not animate as we already have the
1548 // windows in final state. Otherwise, we make this call at the rotation end.
Rhed Jao02655dc2018-10-30 20:44:52 +08001549 if (screenRotationAnimation == null && mWmService.mAccessibilityController != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001550 mWmService.mAccessibilityController.onRotationChangedLocked(this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001551 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001552 }
1553
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001554 void configureDisplayPolicy() {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001555 final int width = mBaseDisplayWidth;
1556 final int height = mBaseDisplayHeight;
1557 final int shortSize;
1558 final int longSize;
1559 if (width > height) {
1560 shortSize = height;
1561 longSize = width;
1562 } else {
1563 shortSize = width;
1564 longSize = height;
1565 }
1566
Adrian Roos506267d2019-06-18 16:13:53 +02001567 final int shortSizeDp = shortSize * DENSITY_DEFAULT / mBaseDisplayDensity;
1568 final int longSizeDp = longSize * DENSITY_DEFAULT / mBaseDisplayDensity;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001569
Winson Chung4723b4e2019-03-25 16:49:36 -07001570 mDisplayPolicy.updateConfigurationAndScreenSizeDependentBehaviors();
Tiger Huang3d2b8982019-01-29 22:56:48 +08001571 mDisplayRotation.configure(width, height, shortSizeDp, longSizeDp);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001572
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001573 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1574 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Tiger Huang86e6d072019-05-02 20:23:47 +08001575
1576 // Not much of use to rotate the display for apps since it's close to square.
1577 mIgnoreRotationForApps = isNonDecorDisplayCloseToSquare(Surface.ROTATION_0, width, height);
1578 }
1579
1580 private boolean isNonDecorDisplayCloseToSquare(int rotation, int width, int height) {
1581 final DisplayCutout displayCutout =
1582 calculateDisplayCutoutForRotation(rotation).getDisplayCutout();
1583 final int uiMode = mWmService.mPolicy.getUiMode();
1584 final int w = mDisplayPolicy.getNonDecorDisplayWidth(
1585 width, height, rotation, uiMode, displayCutout);
1586 final int h = mDisplayPolicy.getNonDecorDisplayHeight(
1587 width, height, rotation, uiMode, displayCutout);
1588 final float aspectRatio = Math.max(w, h) / (float) Math.min(w, h);
1589 return aspectRatio <= mCloseToSquareMaxAspectRatio;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001590 }
1591
Andrii Kulian06d07d62017-03-14 11:11:47 -07001592 /**
1593 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1594 * changed.
1595 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1596 */
Tiger Huanga817b1f2019-05-09 20:04:17 +08001597 private DisplayInfo updateDisplayAndOrientation(int uiMode, Configuration outConfig) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001598 // Use the effective "visual" dimensions based on current rotation
1599 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
Garfield Tan4bb83472019-01-16 14:37:04 -08001600 final int dw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1601 final int dh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001602
1603 // Update application display metrics.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001604 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(mRotation);
1605 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1606
Tiger Huang7c610aa2018-10-27 00:01:01 +08001607 final int appWidth = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1608 displayCutout);
1609 final int appHeight = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1610 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001611 mDisplayInfo.rotation = mRotation;
1612 mDisplayInfo.logicalWidth = dw;
1613 mDisplayInfo.logicalHeight = dh;
1614 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1615 mDisplayInfo.appWidth = appWidth;
1616 mDisplayInfo.appHeight = appHeight;
1617 if (isDefaultDisplay) {
1618 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1619 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1620 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001621 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001622 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1623 if (mDisplayScalingDisabled) {
1624 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1625 } else {
1626 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1627 }
1628
Tiger Huanga817b1f2019-05-09 20:04:17 +08001629 computeSizeRangesAndScreenLayout(mDisplayInfo, rotated, uiMode, dw, dh,
1630 mDisplayMetrics.density, outConfig);
1631
Andrii Kulianf0379de2018-03-14 16:24:07 -07001632 // We usually set the override info in DisplayManager so that we get consistent display
1633 // metrics values when displays are changing and don't send out new values until WM is aware
1634 // of them. However, we don't do this for displays that serve as containers for ActivityView
1635 // because we don't want letter-/pillar-boxing during resize.
1636 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1637 ? mDisplayInfo : null;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001638 mWmService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001639 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001640
1641 mBaseDisplayRect.set(0, 0, dw, dh);
1642
1643 if (isDefaultDisplay) {
1644 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1645 mCompatDisplayMetrics);
1646 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001647
Andrii Kulian06d07d62017-03-14 11:11:47 -07001648 return mDisplayInfo;
1649 }
1650
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001651 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001652 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1653 }
1654
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001655 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001656 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001657 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001658 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001659 }
Adrian Roos11c25582018-02-19 18:06:36 +01001660 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001661 return WmDisplayCutout.computeSafeInsets(
1662 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001663 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001664 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Issei Suzuki43190bd2018-08-20 17:28:41 +02001665 final Rect[] newBounds = mRotationUtil.getRotatedBounds(
1666 WmDisplayCutout.computeSafeInsets(
Jorim Jaggi60640512018-06-29 01:14:31 +02001667 cutout, mInitialDisplayWidth, mInitialDisplayHeight)
Issei Suzuki43190bd2018-08-20 17:28:41 +02001668 .getDisplayCutout().getBoundingRectsAll(),
1669 rotation, mInitialDisplayWidth, mInitialDisplayHeight);
1670 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(newBounds),
Adrian Roos24264212018-02-19 16:26:15 +01001671 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1672 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001673 }
1674
Andrii Kulian06d07d62017-03-14 11:11:47 -07001675 /**
1676 * Compute display configuration based on display properties and policy settings.
1677 * Do not call if mDisplayReady == false.
1678 */
1679 void computeScreenConfiguration(Configuration config) {
Tiger Huanga817b1f2019-05-09 20:04:17 +08001680 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode, config);
Evan Roskye747c3e2018-10-30 20:06:41 -07001681 calculateBounds(displayInfo, mTmpBounds);
1682 config.windowConfiguration.setBounds(mTmpBounds);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001683
1684 final int dw = displayInfo.logicalWidth;
1685 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001686 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Garfield Tane0846042018-07-26 13:42:04 -07001687 config.windowConfiguration.setWindowingMode(getWindowingMode());
Yunfan Chen7daa6ac2018-11-29 18:16:44 -08001688 config.windowConfiguration.setDisplayWindowingMode(getWindowingMode());
Evan Roskye747c3e2018-10-30 20:06:41 -07001689 config.windowConfiguration.setRotation(displayInfo.rotation);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001690
Adrian Roos11c25582018-02-19 18:06:36 +01001691 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001692 config.screenWidthDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001693 (int)(mDisplayPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1694 config.uiMode, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001695 config.screenHeightDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001696 (int)(mDisplayPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1697 config.uiMode, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001698
Tiger Huang7c610aa2018-10-27 00:01:01 +08001699 mDisplayPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001700 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001701 final int leftInset = mTmpRect.left;
1702 final int topInset = mTmpRect.top;
1703 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001704 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1705 leftInset + displayInfo.appWidth /* right */,
1706 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001707 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1708 || displayInfo.rotation == Surface.ROTATION_270);
1709
Andrii Kulian06d07d62017-03-14 11:11:47 -07001710 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1711 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1712 ? Configuration.SCREENLAYOUT_ROUND_YES
1713 : Configuration.SCREENLAYOUT_ROUND_NO);
1714
1715 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1716 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1717 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001718 dh, displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001719 config.densityDpi = displayInfo.logicalDensityDpi;
1720
1721 config.colorMode =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001722 ((displayInfo.isHdr() && mWmService.hasHdrSupport())
Andrii Kulian06d07d62017-03-14 11:11:47 -07001723 ? Configuration.COLOR_MODE_HDR_YES
1724 : Configuration.COLOR_MODE_HDR_NO)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001725 | (displayInfo.isWideColorGamut() && mWmService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001726 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1727 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1728
1729 // Update the configuration based on available input devices, lid switch,
1730 // and platform configuration.
1731 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1732 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1733 config.navigation = Configuration.NAVIGATION_NONAV;
1734
1735 int keyboardPresence = 0;
1736 int navigationPresence = 0;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001737 final InputDevice[] devices = mWmService.mInputManager.getInputDevices();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001738 final int len = devices != null ? devices.length : 0;
1739 for (int i = 0; i < len; i++) {
1740 InputDevice device = devices[i];
Arthur Hung82bbfc32018-11-29 20:24:51 +08001741 // Ignore virtual input device.
1742 if (device.isVirtual()) {
1743 continue;
1744 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001745
Arthur Hung82bbfc32018-11-29 20:24:51 +08001746 // Check if input device can dispatch events to current display.
1747 // If display type is virtual, will follow the default display.
1748 if (!mWmService.mInputManager.canDispatchToDisplay(device.getId(),
1749 displayInfo.type == Display.TYPE_VIRTUAL ? DEFAULT_DISPLAY : mDisplayId)) {
1750 continue;
1751 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001752
Arthur Hung82bbfc32018-11-29 20:24:51 +08001753 final int sources = device.getSources();
1754 final int presenceFlag = device.isExternal()
1755 ? WindowManagerPolicy.PRESENCE_EXTERNAL : WindowManagerPolicy.PRESENCE_INTERNAL;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001756
Arthur Hung82bbfc32018-11-29 20:24:51 +08001757 if (mWmService.mIsTouchDevice) {
1758 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) == InputDevice.SOURCE_TOUCHSCREEN) {
1759 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001760 }
Arthur Hung82bbfc32018-11-29 20:24:51 +08001761 } else {
1762 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1763 }
1764
1765 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1766 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1767 navigationPresence |= presenceFlag;
1768 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1769 && config.navigation == Configuration.NAVIGATION_NONAV) {
1770 config.navigation = Configuration.NAVIGATION_DPAD;
1771 navigationPresence |= presenceFlag;
1772 }
1773
1774 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1775 config.keyboard = Configuration.KEYBOARD_QWERTY;
1776 keyboardPresence |= presenceFlag;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001777 }
1778 }
1779
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001780 if (config.navigation == Configuration.NAVIGATION_NONAV && mWmService.mHasPermanentDpad) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001781 config.navigation = Configuration.NAVIGATION_DPAD;
1782 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1783 }
1784
1785 // Determine whether a hard keyboard is available and enabled.
1786 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1787 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001788 if (hardKeyboardAvailable != mWmService.mHardKeyboardAvailable) {
1789 mWmService.mHardKeyboardAvailable = hardKeyboardAvailable;
1790 mWmService.mH.removeMessages(REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1791 mWmService.mH.sendEmptyMessage(REPORT_HARD_KEYBOARD_STATUS_CHANGE);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001792 }
1793
Winson Chung4723b4e2019-03-25 16:49:36 -07001794 mDisplayPolicy.updateConfigurationAndScreenSizeDependentBehaviors();
Tiger Huang7c610aa2018-10-27 00:01:01 +08001795
Andrii Kulian06d07d62017-03-14 11:11:47 -07001796 // Let the policy update hidden states.
1797 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1798 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1799 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001800 mWmService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001801 }
1802
1803 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001804 DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001805 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1806 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1807 final int unrotDw, unrotDh;
1808 if (rotated) {
1809 unrotDw = dh;
1810 unrotDh = dw;
1811 } else {
1812 unrotDw = dw;
1813 unrotDh = dh;
1814 }
1815 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001816 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001817 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001818 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001819 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001820 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001821 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001822 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001823 return sw;
1824 }
1825
1826 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001827 DisplayMetrics dm, int dw, int dh, DisplayCutout displayCutout) {
1828 dm.noncompatWidthPixels = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1829 displayCutout);
1830 dm.noncompatHeightPixels = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode,
1831 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001832 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1833 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1834 if (curSize == 0 || size < curSize) {
1835 curSize = size;
1836 }
1837 return curSize;
1838 }
1839
Tiger Huang7c610aa2018-10-27 00:01:01 +08001840 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, boolean rotated,
1841 int uiMode, int dw, int dh, float density, Configuration outConfig) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001842
1843 // We need to determine the smallest width that will occur under normal
1844 // operation. To this, start with the base screen size and compute the
1845 // width under the different possible rotations. We need to un-rotate
1846 // the current screen dimensions before doing this.
1847 int unrotDw, unrotDh;
1848 if (rotated) {
1849 unrotDw = dh;
1850 unrotDh = dw;
1851 } else {
1852 unrotDw = dw;
1853 unrotDh = dh;
1854 }
1855 displayInfo.smallestNominalAppWidth = 1<<30;
1856 displayInfo.smallestNominalAppHeight = 1<<30;
1857 displayInfo.largestNominalAppWidth = 0;
1858 displayInfo.largestNominalAppHeight = 0;
Tiger Huang7c610aa2018-10-27 00:01:01 +08001859 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_0, uiMode, unrotDw, unrotDh);
1860 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_90, uiMode, unrotDh, unrotDw);
1861 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_180, uiMode, unrotDw, unrotDh);
1862 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_270, uiMode, unrotDh, unrotDw);
Tiger Huanga817b1f2019-05-09 20:04:17 +08001863
1864 if (outConfig == null) {
1865 return;
1866 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001867 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1868 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001869 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001870 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001871 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001872 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001873 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001874 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001875 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001876 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1877 outConfig.screenLayout = sl;
1878 }
1879
1880 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001881 int uiMode, DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001882 // Get the app screen size at this rotation.
Tiger Huang7c610aa2018-10-27 00:01:01 +08001883 int w = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayCutout);
1884 int h = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001885
1886 // Compute the screen layout size class for this rotation.
1887 int longSize = w;
1888 int shortSize = h;
1889 if (longSize < shortSize) {
1890 int tmp = longSize;
1891 longSize = shortSize;
1892 shortSize = tmp;
1893 }
1894 longSize = (int)(longSize/density);
1895 shortSize = (int)(shortSize/density);
1896 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1897 }
1898
Tiger Huang7c610aa2018-10-27 00:01:01 +08001899 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001900 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001901 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1902 rotation).getDisplayCutout();
Tiger Huang7c610aa2018-10-27 00:01:01 +08001903 final int width = mDisplayPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1904 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001905 if (width < displayInfo.smallestNominalAppWidth) {
1906 displayInfo.smallestNominalAppWidth = width;
1907 }
1908 if (width > displayInfo.largestNominalAppWidth) {
1909 displayInfo.largestNominalAppWidth = width;
1910 }
Tiger Huang7c610aa2018-10-27 00:01:01 +08001911 final int height = mDisplayPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1912 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001913 if (height < displayInfo.smallestNominalAppHeight) {
1914 displayInfo.smallestNominalAppHeight = height;
1915 }
1916 if (height > displayInfo.largestNominalAppHeight) {
1917 displayInfo.largestNominalAppHeight = height;
1918 }
1919 }
1920
Riddle Hsua4d6fa22018-08-11 00:50:39 +08001921 /**
1922 * Apps that use the compact menu panel (as controlled by the panelMenuIsCompact
1923 * theme attribute) on devices that feature a physical options menu key attempt to position
1924 * their menu panel window along the edge of the screen nearest the physical menu key.
1925 * This lowers the travel distance between invoking the menu panel and selecting
1926 * a menu option.
1927 *
1928 * This method helps control where that menu is placed. Its current implementation makes
1929 * assumptions about the menu key and its relationship to the screen based on whether
1930 * the device's natural orientation is portrait (width < height) or landscape.
1931 *
1932 * The menu key is assumed to be located along the bottom edge of natural-portrait
1933 * devices and along the right edge of natural-landscape devices. If these assumptions
1934 * do not hold for the target device, this method should be changed to reflect that.
1935 *
1936 * @return A {@link Gravity} value for placing the options menu window.
1937 */
1938 int getPreferredOptionsPanelGravity() {
1939 final int rotation = getRotation();
1940 if (mInitialDisplayWidth < mInitialDisplayHeight) {
1941 // On devices with a natural orientation of portrait.
1942 switch (rotation) {
1943 default:
1944 case Surface.ROTATION_0:
1945 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1946 case Surface.ROTATION_90:
1947 return Gravity.RIGHT | Gravity.BOTTOM;
1948 case Surface.ROTATION_180:
1949 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1950 case Surface.ROTATION_270:
1951 return Gravity.START | Gravity.BOTTOM;
1952 }
1953 }
1954
1955 // On devices with a natural orientation of landscape.
1956 switch (rotation) {
1957 default:
1958 case Surface.ROTATION_0:
1959 return Gravity.RIGHT | Gravity.BOTTOM;
1960 case Surface.ROTATION_90:
1961 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1962 case Surface.ROTATION_180:
1963 return Gravity.START | Gravity.BOTTOM;
1964 case Surface.ROTATION_270:
1965 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1966 }
1967 }
1968
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001969 DockedStackDividerController getDockedDividerController() {
1970 return mDividerControllerLocked;
1971 }
1972
Winson Chung655332c2016-10-31 13:14:28 -07001973 PinnedStackController getPinnedStackController() {
1974 return mPinnedStackControllerLocked;
1975 }
1976
Jeff Browna506a6e2013-06-04 00:02:38 -07001977 /**
1978 * Returns true if the specified UID has access to this display.
1979 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001980 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001981 return mDisplay.hasAccess(uid);
1982 }
1983
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001984 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001985 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001986 }
1987
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001988 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001989 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001990 }
1991
Wale Ogunwale61911492017-10-11 08:50:50 -07001992 /**
1993 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1994 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001995 TaskStack getSplitScreenPrimaryStack() {
1996 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001997 return (stack != null && stack.isVisible()) ? stack : null;
1998 }
1999
Robert Carr9785cf32018-04-25 15:06:07 -07002000 boolean hasSplitScreenPrimaryStack() {
2001 return getSplitScreenPrimaryStack() != null;
2002 }
2003
Wale Ogunwale61911492017-10-11 08:50:50 -07002004 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07002005 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07002006 * not visible.
2007 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07002008 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
2009 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002010 }
2011
2012 TaskStack getPinnedStack() {
2013 return mTaskStackContainers.getPinnedStack();
2014 }
2015
2016 private boolean hasPinnedStack() {
2017 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07002018 }
2019
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07002020 /**
2021 * Returns the topmost stack on the display that is compatible with the input windowing mode.
2022 * Null is no compatible stack on the display.
2023 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002024 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07002025 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
2026 }
2027
2028 /**
2029 * Returns the topmost stack on the display that is compatible with the input windowing mode and
2030 * activity type. Null is no compatible stack on the display.
2031 */
Wale Ogunwale68278562017-09-23 17:13:55 -07002032 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002033 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07002034 }
2035
Bryce Lee48f4b572017-04-10 10:54:15 -07002036 @VisibleForTesting
Kazuki Takise148d00a2018-05-31 15:32:19 +09002037 WindowList<TaskStack> getStacks() {
2038 return mTaskStackContainers.mChildren;
2039 }
2040
2041 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07002042 TaskStack getTopStack() {
2043 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07002044 }
2045
Winson Chunge2d72172018-01-25 17:46:20 +00002046 ArrayList<Task> getVisibleTasks() {
2047 return mTaskStackContainers.getVisibleTasks();
2048 }
2049
Wale Ogunwale61911492017-10-11 08:50:50 -07002050 void onStackWindowingModeChanged(TaskStack stack) {
2051 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07002052 }
2053
Andrii Kulian441e4492016-09-29 15:25:00 -07002054 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07002055 public void onConfigurationChanged(Configuration newParentConfig) {
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05002056 final int lastOrientation = getConfiguration().orientation;
Andrii Kulian441e4492016-09-29 15:25:00 -07002057 super.onConfigurationChanged(newParentConfig);
Tiger Huang7c610aa2018-10-27 00:01:01 +08002058 if (mDisplayPolicy != null) {
2059 mDisplayPolicy.onConfigurationChanged();
2060 }
Andrii Kulian441e4492016-09-29 15:25:00 -07002061
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05002062 if (lastOrientation != getConfiguration().orientation) {
2063 getMetricsLogger().write(
2064 new LogMaker(MetricsEvent.ACTION_PHONE_ORIENTATION_CHANGED)
2065 .setSubtype(getConfiguration().orientation)
2066 .addTaggedData(MetricsEvent.FIELD_DISPLAY_ID, getDisplayId()));
2067 }
2068
Evan Roskye747c3e2018-10-30 20:06:41 -07002069 // If there was no pinned stack, we still need to notify the controller of the display info
2070 // update as a result of the config change.
2071 if (mPinnedStackControllerLocked != null && !hasPinnedStack()) {
2072 mPinnedStackControllerLocked.onDisplayInfoChanged(getDisplayInfo());
2073 }
Evan Roskye747c3e2018-10-30 20:06:41 -07002074 }
2075
2076 /**
2077 * Updates the resources used by docked/pinned controllers. This needs to be called at the
2078 * beginning of a configuration update cascade since the metrics from these resources are used
2079 * for bounds calculations. Since ActivityDisplay initiates the configuration update, this
2080 * should be called from there instead of DisplayContent's onConfigurationChanged.
2081 */
2082 void preOnConfigurationChanged() {
Bryce Leef3c6a472017-11-14 14:53:06 -08002083 final DockedStackDividerController dividerController = getDockedDividerController();
2084
2085 if (dividerController != null) {
2086 getDockedDividerController().onConfigurationChanged();
2087 }
2088
2089 final PinnedStackController pinnedStackController = getPinnedStackController();
2090
2091 if (pinnedStackController != null) {
2092 getPinnedStackController().onConfigurationChanged();
2093 }
Andrii Kulian441e4492016-09-29 15:25:00 -07002094 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07002095
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07002096 @Override
2097 boolean fillsParent() {
2098 return true;
2099 }
2100
2101 @Override
2102 boolean isVisible() {
2103 return true;
2104 }
2105
2106 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002107 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002108 super.onAppTransitionDone();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002109 mWmService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002110 }
2111
Yunfan Chen7daa6ac2018-11-29 18:16:44 -08002112 @Override
2113 public void setWindowingMode(int windowingMode) {
2114 super.setWindowingMode(windowingMode);
2115 super.setDisplayWindowingMode(windowingMode);
2116 }
2117
2118 @Override
2119 void setDisplayWindowingMode(int windowingMode) {
2120 setWindowingMode(windowingMode);
2121 }
2122
Robert Carr9785cf32018-04-25 15:06:07 -07002123 /**
2124 * In split-screen mode we process the IME containers above the docked divider
2125 * rather than directly above their target.
2126 */
2127 private boolean skipTraverseChild(WindowContainer child) {
lumarkff0ab692018-11-05 20:32:30 +08002128 if (child == mImeWindowsContainers && mInputMethodTarget != null
Robert Carr9785cf32018-04-25 15:06:07 -07002129 && !hasSplitScreenPrimaryStack()) {
2130 return true;
2131 }
2132 return false;
2133 }
2134
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07002135 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002136 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
2137 // Special handling so we can process IME windows with #forAllImeWindows above their IME
2138 // target, or here in order if there isn't an IME target.
2139 if (traverseTopToBottom) {
2140 for (int i = mChildren.size() - 1; i >= 0; --i) {
2141 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07002142 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002143 continue;
2144 }
Robert Carr9785cf32018-04-25 15:06:07 -07002145
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002146 if (child.forAllWindows(callback, traverseTopToBottom)) {
2147 return true;
2148 }
2149 }
2150 } else {
2151 final int count = mChildren.size();
2152 for (int i = 0; i < count; i++) {
2153 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07002154 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002155 continue;
2156 }
Robert Carr9785cf32018-04-25 15:06:07 -07002157
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002158 if (child.forAllWindows(callback, traverseTopToBottom)) {
2159 return true;
2160 }
2161 }
2162 }
2163 return false;
2164 }
2165
2166 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
2167 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
2168 }
2169
Wale Ogunwale399c8692017-05-08 14:22:42 -07002170 @Override
2171 int getOrientation() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002172 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002173
Tiger Huang86e6d072019-05-02 20:23:47 +08002174 if (mIgnoreRotationForApps) {
2175 return SCREEN_ORIENTATION_USER;
2176 }
2177
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002178 if (mWmService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08002179 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07002180 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
2181 + " is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002182 // If the display is frozen, some activities may be in the middle of restarting, and
2183 // thus have removed their old window. If the window has the flag to hide the lock
2184 // screen, then the lock screen can re-appear and inflict its own orientation on us.
2185 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08002186 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002187 } else if (policy.isKeyguardLocked()) {
2188 // Use the last orientation the while the display is frozen with the keyguard
2189 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
2190 // window. We don't want to check the show when locked window directly though as
2191 // things aren't stable while the display is frozen, for example the window could be
2192 // momentarily unavailable due to activity relaunch.
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07002193 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
2194 + " is frozen while keyguard locked, return " + mLastOrientation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08002195 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07002196 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002197 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002198 final int orientation = mAboveAppWindowsContainers.getOrientation();
2199 if (orientation != SCREEN_ORIENTATION_UNSET) {
2200 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002201 }
Wale Ogunwale51362492016-09-08 17:49:17 -07002202 }
2203
Wale Ogunwale399c8692017-05-08 14:22:42 -07002204 // Top system windows are not requesting an orientation. Start searching from apps.
2205 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07002206 }
2207
Craig Mautner46ac6fa2013-08-01 10:06:34 -07002208 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07002209 // Check if display metrics changed and update base values if needed.
2210 updateBaseDisplayMetricsIfNeeded();
2211
Craig Mautner722285e2012-09-07 13:55:58 -07002212 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07002213 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07002214
Garfield Tan2f145f22018-11-01 15:27:03 -07002215 onDisplayChanged(this);
Craig Mautner722285e2012-09-07 13:55:58 -07002216 }
2217
Adrian Roos506267d2019-06-18 16:13:53 +02002218 @Override
2219 void onDisplayChanged(DisplayContent dc) {
2220 super.onDisplayChanged(dc);
2221 updateSystemGestureExclusionLimit();
2222 }
2223
2224 void updateSystemGestureExclusionLimit() {
2225 mSystemGestureExclusionLimit = mWmService.mSystemGestureExclusionLimitDp
2226 * mDisplayMetrics.densityDpi / DENSITY_DEFAULT;
2227 updateSystemGestureExclusion();
2228 }
2229
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002230 void initializeDisplayBaseInfo() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002231 final DisplayManagerInternal displayManagerInternal = mWmService.mDisplayManagerInternal;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002232 if (displayManagerInternal != null) {
2233 // Bootstrap the default logical display from the display manager.
2234 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
2235 if (newDisplayInfo != null) {
2236 mDisplayInfo.copyFrom(newDisplayInfo);
2237 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002238 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002239
Andrii Kuliancd097992017-03-23 18:31:59 -07002240 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
2241 mDisplayInfo.logicalDensityDpi);
2242 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
2243 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
2244 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002245 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002246 }
2247
Andrii Kuliancd097992017-03-23 18:31:59 -07002248 /**
2249 * If display metrics changed, overrides are not set and it's not just a rotation - update base
2250 * values.
2251 */
2252 private void updateBaseDisplayMetricsIfNeeded() {
2253 // Get real display metrics without overrides from WM.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002254 mWmService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
Andrii Kuliancd097992017-03-23 18:31:59 -07002255 final int orientation = mDisplayInfo.rotation;
2256 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2257 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
2258 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
2259 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002260 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07002261
2262 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
2263 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01002264 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
2265 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07002266
2267 if (displayMetricsChanged) {
2268 // Check if display size or density is forced.
2269 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
2270 || mBaseDisplayHeight != mInitialDisplayHeight;
2271 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
2272
2273 // If there is an override set for base values - use it, otherwise use new values.
2274 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
2275 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
2276 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
2277
2278 // Real display metrics changed, so we should also update initial values.
2279 mInitialDisplayWidth = newWidth;
2280 mInitialDisplayHeight = newHeight;
2281 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01002282 mInitialDisplayCutout = newCutout;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002283 mWmService.reconfigureDisplayLocked(this);
Andrii Kuliancd097992017-03-23 18:31:59 -07002284 }
2285 }
2286
Bryce Lee27cec322017-03-21 09:41:37 -07002287 /** Sets the maximum width the screen resolution can be */
2288 void setMaxUiWidth(int width) {
2289 if (DEBUG_DISPLAY) {
2290 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
2291 }
2292
2293 mMaxUiWidth = width;
2294
2295 // Update existing metrics.
2296 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
2297 }
2298
2299 /** Update base (override) display metrics. */
2300 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
2301 mBaseDisplayWidth = baseWidth;
2302 mBaseDisplayHeight = baseHeight;
2303 mBaseDisplayDensity = baseDensity;
2304
2305 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
2306 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
2307 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
2308 mBaseDisplayWidth = mMaxUiWidth;
2309
2310 if (DEBUG_DISPLAY) {
2311 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
2312 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
2313 + " on display:" + getDisplayId());
2314 }
2315 }
2316
2317 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08002318
2319 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07002320 }
2321
Riddle Hsuf53da812018-08-15 22:00:27 +08002322 /**
2323 * Forces this display to use the specified density.
2324 *
2325 * @param density The density in DPI to use. If the value equals to initial density, the setting
2326 * will be cleared.
2327 * @param userId The target user to apply. Only meaningful when this is default display. If the
2328 * user id is {@link UserHandle#USER_CURRENT}, it means to apply current settings
2329 * so only need to configure display.
2330 */
2331 void setForcedDensity(int density, int userId) {
2332 final boolean clear = density == mInitialDisplayDensity;
2333 final boolean updateCurrent = userId == UserHandle.USER_CURRENT;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002334 if (mWmService.mCurrentUserId == userId || updateCurrent) {
Riddle Hsuf53da812018-08-15 22:00:27 +08002335 mBaseDisplayDensity = density;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002336 mWmService.reconfigureDisplayLocked(this);
Riddle Hsuf53da812018-08-15 22:00:27 +08002337 }
2338 if (updateCurrent) {
2339 // We are applying existing settings so no need to save it again.
2340 return;
2341 }
2342
2343 if (density == mInitialDisplayDensity) {
2344 density = 0;
2345 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002346 mWmService.mDisplayWindowSettings.setForcedDensity(this, density, userId);
Riddle Hsuf53da812018-08-15 22:00:27 +08002347 }
2348
2349 /** @param mode {@link #FORCE_SCALING_MODE_AUTO} or {@link #FORCE_SCALING_MODE_DISABLED}. */
2350 void setForcedScalingMode(@ForceScalingMode int mode) {
2351 if (mode != FORCE_SCALING_MODE_DISABLED) {
2352 mode = FORCE_SCALING_MODE_AUTO;
2353 }
2354
2355 mDisplayScalingDisabled = (mode != FORCE_SCALING_MODE_AUTO);
2356 Slog.i(TAG_WM, "Using display scaling mode: " + (mDisplayScalingDisabled ? "off" : "auto"));
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002357 mWmService.reconfigureDisplayLocked(this);
Riddle Hsuf53da812018-08-15 22:00:27 +08002358
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002359 mWmService.mDisplayWindowSettings.setForcedScalingMode(this, mode);
Riddle Hsuf53da812018-08-15 22:00:27 +08002360 }
2361
2362 /** If the given width and height equal to initial size, the setting will be cleared. */
2363 void setForcedSize(int width, int height) {
2364 final boolean clear = mInitialDisplayWidth == width && mInitialDisplayHeight == height;
2365 if (!clear) {
2366 // Set some sort of reasonable bounds on the size of the display that we will try
2367 // to emulate.
2368 final int minSize = 200;
2369 final int maxScale = 2;
2370 width = Math.min(Math.max(width, minSize), mInitialDisplayWidth * maxScale);
2371 height = Math.min(Math.max(height, minSize), mInitialDisplayHeight * maxScale);
2372 }
2373
2374 Slog.i(TAG_WM, "Using new display size: " + width + "x" + height);
2375 updateBaseDisplayMetrics(width, height, mBaseDisplayDensity);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002376 mWmService.reconfigureDisplayLocked(this);
Riddle Hsuf53da812018-08-15 22:00:27 +08002377
2378 if (clear) {
2379 width = height = 0;
2380 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002381 mWmService.mDisplayWindowSettings.setForcedSize(this, width, height);
Riddle Hsuf53da812018-08-15 22:00:27 +08002382 }
2383
Wale Ogunwaledb506192017-12-08 10:57:32 -08002384 void getStableRect(Rect out) {
2385 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08002386 }
2387
Yunfan Chen279f5582018-12-12 15:24:50 -08002388 void setStackOnDisplay(int stackId, boolean onTop, TaskStack stack) {
2389 if (DEBUG_STACK) {
2390 Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId=" + mDisplayId);
2391 }
Andrii Kulian839def92016-11-02 10:58:58 -07002392
Wale Ogunwale61911492017-10-11 08:50:50 -07002393 mTaskStackContainers.addStackToDisplay(stack, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002394 }
2395
Andrii Kulian51c1b672017-04-07 18:39:32 -07002396 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002397 final DisplayContent prevDc = stack.getDisplayContent();
2398 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002399 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
2400 + " which is not currently attached to any display");
2401 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002402 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07002403 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
2404 + " to its current displayId=" + mDisplayId);
2405 }
2406
Wale Ogunwale61911492017-10-11 08:50:50 -07002407 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07002408 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08002409 }
2410
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002411 @Override
2412 protected void addChild(DisplayChildWindowContainer child,
2413 Comparator<DisplayChildWindowContainer> comparator) {
2414 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2415 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002416
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002417 @Override
2418 protected void addChild(DisplayChildWindowContainer child, int index) {
2419 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2420 }
2421
2422 @Override
2423 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002424 // Only allow removal of direct children from this display if the display is in the process
2425 // of been removed.
2426 if (mRemovingDisplay) {
2427 super.removeChild(child);
2428 return;
2429 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002430 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002431 }
2432
Andrii Kuliand2765632016-12-12 22:26:34 -08002433 @Override
2434 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
2435 // Children of the display are statically ordered, so the real intention here is to perform
2436 // the operation on the display and not the static direct children.
2437 getParent().positionChildAt(position, this, includingParents);
2438 }
2439
Riddle Hsu57831b52018-07-27 00:31:48 +08002440 void positionStackAt(int position, TaskStack child, boolean includingParents) {
2441 mTaskStackContainers.positionChildAt(position, child, includingParents);
Winson Chung59a47ded2018-01-25 17:46:06 +00002442 layoutAndAssignWindowLayersIfNeeded();
2443 }
2444
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002445 /**
Winson Chungc5fe7ff2019-02-19 14:49:25 -08002446 * Returns true if the input point is within an app window.
2447 */
2448 boolean pointWithinAppWindow(int x, int y) {
2449 final int[] targetWindowType = {-1};
2450 final Consumer fn = PooledLambda.obtainConsumer((w, nonArg) -> {
2451 if (targetWindowType[0] != -1) {
2452 return;
2453 }
2454
2455 if (w.isOnScreen() && w.isVisibleLw() && w.getFrameLw().contains(x, y)) {
2456 targetWindowType[0] = w.mAttrs.type;
2457 return;
2458 }
2459 }, PooledLambda.__(WindowState.class), mTmpRect);
2460 forAllWindows(fn, true /* traverseTopToBottom */);
2461 ((PooledConsumer) fn).recycle();
2462 return FIRST_APPLICATION_WINDOW <= targetWindowType[0]
2463 && targetWindowType[0] <= LAST_APPLICATION_WINDOW;
2464 }
2465
2466 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08002467 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07002468 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07002469 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07002470 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002471 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002472 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07002473 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2474 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002475 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002476 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002477 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002478
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002479 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
2480 if (mTmpTaskForResizePointSearchResult.searchDone) {
2481 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07002482 }
2483 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002484 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002485 }
2486
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002487 void updateTouchExcludeRegion() {
2488 final Task focusedTask = (mFocusedApp != null ? mFocusedApp.getTask() : null);
David Stevensee9e2772017-02-09 16:30:27 -08002489 if (focusedTask == null) {
2490 mTouchExcludeRegion.setEmpty();
2491 } else {
2492 mTouchExcludeRegion.set(mBaseDisplayRect);
2493 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
2494 mTmpRect2.setEmpty();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002495 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0;
2496 --stackNdx) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002497 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002498 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
2499 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08002500 }
2501 // If we removed the focused task above, add it back and only leave its
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002502 // outside touch area in the exclusion. TapDetector is not interested in
David Stevensee9e2772017-02-09 16:30:27 -08002503 // any touch inside the focused task itself.
2504 if (!mTmpRect2.isEmpty()) {
2505 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
2506 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08002507 }
lumark90120a82018-08-15 00:33:03 +08002508 if (mInputMethodWindow != null && mInputMethodWindow.isVisibleLw()) {
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002509 // If the input method is visible and the user is typing, we don't want these touch
2510 // events to be intercepted and used to change focus. This would likely cause a
2511 // disappearance of the input method.
lumark90120a82018-08-15 00:33:03 +08002512 mInputMethodWindow.getTouchableRegion(mTmpRegion);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002513 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002514 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002515 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002516 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002517 win.getTouchableRegion(mTmpRegion);
2518 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2519 }
Tiger Huang04dc4cc2019-01-17 18:41:41 +08002520 amendWindowTapExcludeRegion(mTouchExcludeRegion);
Andrii Kulian03c403d2016-11-11 11:14:12 -08002521 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002522 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002523 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002524 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002525 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2526 }
Riddle Hsu2588ab02019-02-25 14:23:56 +08002527 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner6601b7b2013-04-29 10:29:11 -07002528 }
2529
Tiger Huang04dc4cc2019-01-17 18:41:41 +08002530 /**
2531 * Union the region with all the tap exclude region provided by windows on this display.
2532 *
2533 * @param inOutRegion The region to be amended.
2534 */
2535 void amendWindowTapExcludeRegion(Region inOutRegion) {
2536 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2537 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2538 win.amendTapExcludeRegion(inOutRegion);
2539 }
2540 }
2541
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002542 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07002543 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002544 super.switchUser();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002545 mWmService.mWindowsChanged = true;
Winson Chungda20fec2019-04-10 12:19:59 -07002546 mDisplayPolicy.switchUser();
Craig Mautner858d8a62013-04-23 17:08:34 -07002547 }
2548
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07002549 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002550 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2551 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07002552 }
2553 }
2554
Wale Ogunwale10124582016-09-15 20:25:50 -07002555 @Override
2556 void removeIfPossible() {
2557 if (isAnimating()) {
2558 mDeferredRemoval = true;
2559 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002560 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002561 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002562 }
2563
Wale Ogunwale10124582016-09-15 20:25:50 -07002564 @Override
2565 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002566 mRemovingDisplay = true;
2567 try {
Jackal Guoc43a0a62019-04-23 09:15:14 +08002568 if (mParentWindow != null) {
2569 mParentWindow.removeEmbeddedDisplayContent(this);
2570 }
lumark588a3e82018-07-20 18:53:54 +08002571 // Clear all transitions & screen frozen states when removing display.
2572 mOpeningApps.clear();
2573 mClosingApps.clear();
Evan Rosky2289ba12018-11-19 18:28:18 -08002574 mChangingApps.clear();
lumark588a3e82018-07-20 18:53:54 +08002575 mUnknownAppVisibilityController.clear();
2576 mAppTransition.removeAppTransitionTimeoutCallbacks();
2577 handleAnimatingStoppedAndTransition();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002578 mWmService.stopFreezingDisplayLocked();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002579 super.removeImmediately();
2580 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Riddle Hsu2588ab02019-02-25 14:23:56 +08002581 mPointerEventDispatcher.dispose();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002582 mWmService.mAnimator.removeDisplayLocked(mDisplayId);
Louis Chang207f9112018-07-30 08:58:54 +08002583 mWindowingLayer.release();
2584 mOverlayLayer.release();
Jorim Jaggiae962e62018-12-27 17:23:48 +01002585 mInputMonitor.onDisplayRemoved();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002586 } finally {
Riddle Hsu4e611772018-10-31 18:58:28 +08002587 mDisplayReady = false;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002588 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002589 }
Robert Carr679ccb02018-08-08 15:32:35 -07002590
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002591 mWmService.mWindowPlacerLocked.requestTraversal();
Craig Mautner95da1082014-02-24 17:54:35 -08002592 }
2593
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002594 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002595 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002596 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002597 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2598
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002599 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002600 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002601 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002602 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002603 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002604 }
2605
Andrii Kulian0214ed92017-05-16 13:44:05 -07002606 /** @return 'true' if removal of this display content is deferred due to active animation. */
2607 boolean isRemovalDeferred() {
2608 return mDeferredRemoval;
2609 }
2610
Wale Ogunwale10124582016-09-15 20:25:50 -07002611 boolean animateForIme(float interpolatedValue, float animationTarget,
2612 float dividerAnimationTarget) {
2613 boolean updated = false;
2614
Wale Ogunwale61911492017-10-11 08:50:50 -07002615 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2616 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002617 if (stack == null || !stack.isAdjustedForIme()) {
2618 continue;
2619 }
2620
2621 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2622 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2623 updated = true;
2624 } else {
2625 mDividerControllerLocked.mLastAnimationProgress =
2626 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2627 mDividerControllerLocked.mLastDividerProgress =
2628 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2629 updated |= stack.updateAdjustForIme(
2630 mDividerControllerLocked.mLastAnimationProgress,
2631 mDividerControllerLocked.mLastDividerProgress,
2632 false /* force */);
2633 }
2634 if (interpolatedValue >= 1f) {
2635 stack.endImeAdjustAnimation();
2636 }
2637 }
2638
2639 return updated;
2640 }
2641
2642 boolean clearImeAdjustAnimation() {
2643 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002644 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2645 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002646 if (stack != null && stack.isAdjustedForIme()) {
2647 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2648 changed = true;
2649 }
2650 }
2651 return changed;
2652 }
2653
2654 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002655 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2656 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002657 if (stack.isVisible() && stack.isAdjustedForIme()) {
2658 stack.beginImeAdjustAnimation();
2659 }
2660 }
2661 }
2662
2663 void adjustForImeIfNeeded() {
lumark90120a82018-08-15 00:33:03 +08002664 final WindowState imeWin = mInputMethodWindow;
Wale Ogunwale10124582016-09-15 20:25:50 -07002665 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2666 && !mDividerControllerLocked.isImeHideRequested();
Riddle Hsuc6814252019-05-16 17:06:52 +08002667 final TaskStack dockedStack = getSplitScreenPrimaryStack();
2668 final boolean dockVisible = dockedStack != null;
2669 final Task topDockedTask = dockVisible ? dockedStack.getTopChild() : null;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002670 final TaskStack imeTargetStack = mWmService.getImeFocusStackLocked();
Wale Ogunwale10124582016-09-15 20:25:50 -07002671 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2672 imeTargetStack.getDockSide() : DOCKED_INVALID;
2673 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2674 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002675 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002676 final boolean imeHeightChanged = imeVisible &&
2677 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2678
Riddle Hsuc6814252019-05-16 17:06:52 +08002679 // This includes a case where the docked stack is unminimizing and IME is visible for the
2680 // bottom side stack. The condition prevents adjusting the override task bounds for IME to
2681 // the minimized docked stack bounds.
2682 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock()
2683 || (topDockedTask != null && imeOnBottom && !dockedStack.isAdjustedForIme()
2684 && dockedStack.getBounds().height() < topDockedTask.getBounds().height());
2685
Wale Ogunwale10124582016-09-15 20:25:50 -07002686 // The divider could be adjusted for IME position, or be thinner than usual,
2687 // or both. There are three possible cases:
2688 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2689 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2690 // - If IME is not visible, divider is not moved and is normal width.
2691
2692 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002693 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2694 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002695 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002696 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2697 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002698 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2699 } else {
2700 stack.resetAdjustedForIme(false);
2701 }
2702 }
2703 mDividerControllerLocked.setAdjustedForIme(
2704 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2705 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002706 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2707 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002708 stack.resetAdjustedForIme(!dockVisible);
2709 }
2710 mDividerControllerLocked.setAdjustedForIme(
2711 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2712 }
Winson Chung655332c2016-10-31 13:14:28 -07002713 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002714 }
2715
2716 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002717 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2718 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002719 stack.prepareFreezingTaskBounds();
2720 }
2721 }
2722
Wale Ogunwale94744212015-09-21 19:01:47 -07002723 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002724 getBounds(mTmpRect, newRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002725 rotateBounds(mTmpRect, oldRotation, newRotation, bounds);
2726 }
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002727
Evan Rosky39b6f232018-10-30 18:35:41 -07002728 void rotateBounds(Rect parentBounds, int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002729 // Compute a transform matrix to undo the coordinate space transformation,
2730 // and present the window at the same physical position it previously occupied.
2731 final int deltaRotation = deltaRotation(newRotation, oldRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002732 createRotationMatrix(
2733 deltaRotation, parentBounds.width(), parentBounds.height(), mTmpMatrix);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002734
2735 mTmpRectF.set(bounds);
2736 mTmpMatrix.mapRect(mTmpRectF);
2737 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002738 }
2739
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002740 static int deltaRotation(int oldRotation, int newRotation) {
2741 int delta = newRotation - oldRotation;
2742 if (delta < 0) delta += 4;
2743 return delta;
2744 }
2745
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002746 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002747 Matrix outMatrix) {
2748 // For rotations without Z-ordering we don't need the target rectangle's position.
2749 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2750 displayHeight, outMatrix);
2751 }
2752
2753 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2754 float displayWidth, float displayHeight, Matrix outMatrix) {
2755 switch (rotation) {
2756 case ROTATION_0:
2757 outMatrix.reset();
2758 break;
2759 case ROTATION_270:
2760 outMatrix.setRotate(270, 0, 0);
2761 outMatrix.postTranslate(0, displayHeight);
2762 outMatrix.postTranslate(rectTop, 0);
2763 break;
2764 case ROTATION_180:
2765 outMatrix.reset();
2766 break;
2767 case ROTATION_90:
2768 outMatrix.setRotate(90, 0, 0);
2769 outMatrix.postTranslate(displayWidth, 0);
2770 outMatrix.postTranslate(-rectTop, rectLeft);
2771 break;
2772 }
2773 }
2774
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002775 @CallSuper
2776 @Override
Nataniel Borges023ecb52019-01-16 14:15:43 -08002777 public void writeToProto(ProtoOutputStream proto, long fieldId,
2778 @WindowTraceLogLevel int logLevel) {
2779 // Critical log level logs only visible elements to mitigate performance overheard
2780 if (logLevel == WindowTraceLogLevel.CRITICAL && !isVisible()) {
2781 return;
2782 }
2783
Steven Timotiusaf03df62017-07-18 16:56:43 -07002784 final long token = proto.start(fieldId);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002785 super.writeToProto(proto, WINDOW_CONTAINER, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002786 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002787 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2788 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002789 stack.writeToProto(proto, STACKS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002790 }
2791 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2792 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002793 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2794 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002795 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002796 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002797 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2798 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002799 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002800 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002801 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2802 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002803 windowToken.writeToProto(proto, IME_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002804 }
2805 proto.write(DPI, mBaseDisplayDensity);
2806 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002807 proto.write(ROTATION, mRotation);
2808 final ScreenRotationAnimation screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002809 mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002810 if (screenRotationAnimation != null) {
2811 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2812 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002813 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
lumark588a3e82018-07-20 18:53:54 +08002814 mAppTransition.writeToProto(proto, APP_TRANSITION);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002815 if (mFocusedApp != null) {
2816 mFocusedApp.writeNameToProto(proto, FOCUSED_APP);
2817 }
Jorim Jaggi1112fed2019-04-15 13:32:14 +02002818 for (int i = mOpeningApps.size() - 1; i >= 0; i--) {
2819 mOpeningApps.valueAt(i).mActivityRecord.writeIdentifierToProto(proto, OPENING_APPS);
2820 }
2821 for (int i = mClosingApps.size() - 1; i >= 0; i--) {
2822 mClosingApps.valueAt(i).mActivityRecord.writeIdentifierToProto(proto, CLOSING_APPS);
2823 }
2824 for (int i = mChangingApps.size() - 1; i >= 0; i--) {
2825 mChangingApps.valueAt(i).mActivityRecord.writeIdentifierToProto(proto, CHANGING_APPS);
2826 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002827 proto.end(token);
2828 }
2829
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002830 @Override
2831 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2832 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002833 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2834 final String subPrefix = " " + prefix;
2835 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2836 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2837 pw.print("dpi");
2838 if (mInitialDisplayWidth != mBaseDisplayWidth
2839 || mInitialDisplayHeight != mBaseDisplayHeight
2840 || mInitialDisplayDensity != mBaseDisplayDensity) {
2841 pw.print(" base=");
2842 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2843 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2844 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002845 if (mDisplayScalingDisabled) {
2846 pw.println(" noscale");
2847 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002848 pw.print(" cur=");
2849 pw.print(mDisplayInfo.logicalWidth);
2850 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2851 pw.print(" app=");
2852 pw.print(mDisplayInfo.appWidth);
2853 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2854 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2855 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2856 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2857 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002858 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002859 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002860 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002861
Craig Mautnerdc548482014-02-05 13:35:24 -08002862 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002863 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002864 pw.print(prefix);
2865 pw.print("mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
Adrian Roos5251b1d2018-03-23 18:57:43 +01002866
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002867 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
2868 if (mLastFocus != mCurrentFocus) {
2869 pw.print(" mLastFocus="); pw.println(mLastFocus);
2870 }
2871 if (mLosingFocus.size() > 0) {
2872 pw.println();
2873 pw.println(" Windows losing focus:");
2874 for (int i = mLosingFocus.size() - 1; i >= 0; i--) {
2875 final WindowState w = mLosingFocus.get(i);
2876 pw.print(" Losing #"); pw.print(i); pw.print(' ');
2877 pw.print(w);
2878 if (dumpAll) {
2879 pw.println(":");
2880 w.dump(pw, " ", true);
2881 } else {
2882 pw.println();
2883 }
2884 }
2885 }
2886 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
Tiger Huang7c610aa2018-10-27 00:01:01 +08002887 if (mLastStatusBarVisibility != 0) {
2888 pw.print(" mLastStatusBarVisibility=0x");
2889 pw.println(Integer.toHexString(mLastStatusBarVisibility));
2890 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002891
Adrian Roos5251b1d2018-03-23 18:57:43 +01002892 pw.println();
wilsonshihc32538e2018-11-07 17:27:34 +08002893 mWallpaperController.dump(pw, " ");
2894
2895 pw.println();
Adrian Roos4ffc8972019-02-07 20:45:11 +01002896 pw.print("mSystemGestureExclusion=");
2897 if (mSystemGestureExclusionListeners.getRegisteredCallbackCount() > 0) {
2898 pw.println(mSystemGestureExclusion);
2899 } else {
2900 pw.println("<no lstnrs>");
2901 }
2902
2903 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002904 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002905 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2906 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002907 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002908 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002909
Craig Mautnerdc548482014-02-05 13:35:24 -08002910 pw.println();
2911 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002912 pw.println();
2913 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002914 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002915 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002916 pw.print(" Exiting #"); pw.print(i);
2917 pw.print(' '); pw.print(token);
2918 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002919 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002920 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002921 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002922
Jorim Jaggi31f71702016-05-04 16:43:04 -07002923 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002924
2925 // Dump stack references
2926 final TaskStack homeStack = getHomeStack();
2927 if (homeStack != null) {
2928 pw.println(prefix + "homeStack=" + homeStack.getName());
2929 }
2930 final TaskStack pinnedStack = getPinnedStack();
2931 if (pinnedStack != null) {
2932 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2933 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002934 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002935 if (splitScreenPrimaryStack != null) {
2936 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2937 }
2938
2939 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002940 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002941 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002942 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002943
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002944 pw.println();
2945 mDisplayFrames.dump(prefix, pw);
Arthur Hung95b38a92018-07-20 18:56:12 +08002946 pw.println();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08002947 mDisplayPolicy.dump(prefix, pw);
2948 pw.println();
Riddle Hsuad256a12018-07-18 16:11:30 +08002949 mDisplayRotation.dump(prefix, pw);
2950 pw.println();
Arthur Hung95b38a92018-07-20 18:56:12 +08002951 mInputMonitor.dump(pw, " ");
Jorim Jaggif96c90a2018-09-26 16:55:15 +02002952 pw.println();
2953 mInsetsStateController.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002954 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002955
2956 @Override
2957 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002958 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002959 }
2960
2961 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002962 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002963 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002964
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002965 /** Returns true if the stack in the windowing mode is visible. */
2966 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002967 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002968 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002969 }
2970
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002971 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002972 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002973 final int x = (int) xf;
2974 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002975 final WindowState touchedWin = getWindow(w -> {
2976 final int flags = w.mAttrs.flags;
2977 if (!w.isVisibleLw()) {
2978 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002979 }
2980 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002981 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002982 }
2983
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002984 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002985 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002986 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002987 }
2988
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002989 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002990
2991 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002992 return mTmpRegion.contains(x, y) || touchFlags == 0;
2993 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002994
2995 return touchedWin;
2996 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002997
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002998 boolean canAddToastWindowForUid(int uid) {
2999 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08003000 // Also if the app is focused adding more than one toast at
3001 // a time for better backwards compatibility.
3002 final WindowState focusedWindowForUid = getWindow(w ->
3003 w.mOwnerUid == uid && w.isFocused());
3004 if (focusedWindowForUid != null) {
3005 return true;
3006 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003007 final WindowState win = getWindow(w ->
3008 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
3009 && !w.mWindowRemovalAllowed);
3010 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07003011 }
3012
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003013 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07003014 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
3015 return;
3016 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003017
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003018 // Used to communicate the old focus to the callback method.
3019 mTmpWindow = oldFocus;
3020
3021 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07003022 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07003023
Louis Changa9350fe2019-04-25 17:14:20 +08003024 /**
3025 * Looking for the focused window on this display if the top focused display hasn't been
3026 * found yet (topFocusedDisplayId is INVALID_DISPLAY) or per-display focused was allowed.
3027 *
3028 * @param topFocusedDisplayId Id of the top focused display.
3029 * @return The focused window or null if there isn't any or no need to seek.
3030 */
3031 WindowState findFocusedWindowIfNeeded(int topFocusedDisplayId) {
3032 return (mWmService.mPerDisplayFocusEnabled || topFocusedDisplayId == INVALID_DISPLAY)
3033 ? findFocusedWindow() : null;
Tiger Huang51c5a1d2018-12-11 20:24:51 +08003034 }
3035
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07003036 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003037 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07003038
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003039 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003040
3041 if (mTmpWindow == null) {
3042 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
3043 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07003044 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003045 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07003046 }
3047
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003048 /**
3049 * Update the focused window and make some adjustments if the focus has changed.
3050 *
3051 * @param mode Indicates the situation we are in. Possible modes are:
3052 * {@link WindowManagerService#UPDATE_FOCUS_NORMAL},
3053 * {@link WindowManagerService#UPDATE_FOCUS_PLACING_SURFACES},
3054 * {@link WindowManagerService#UPDATE_FOCUS_WILL_PLACE_SURFACES},
3055 * {@link WindowManagerService#UPDATE_FOCUS_REMOVING_FOCUS}
3056 * @param updateInputWindows Whether to sync the window information to the input module.
Louis Changa9350fe2019-04-25 17:14:20 +08003057 * @param topFocusedDisplayId Display id of current top focused display.
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003058 * @return {@code true} if the focused window has changed.
3059 */
Louis Changa9350fe2019-04-25 17:14:20 +08003060 boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows,
3061 int topFocusedDisplayId) {
3062 WindowState newFocus = findFocusedWindowIfNeeded(topFocusedDisplayId);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003063 if (mCurrentFocus == newFocus) {
3064 return false;
3065 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003066 boolean imWindowChanged = false;
Tiger Huang51c5a1d2018-12-11 20:24:51 +08003067 final WindowState imWindow = mInputMethodWindow;
3068 if (imWindow != null) {
3069 final WindowState prevTarget = mInputMethodTarget;
3070 final WindowState newTarget = computeImeTarget(true /* updateImeTarget*/);
3071 imWindowChanged = prevTarget != newTarget;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003072
Tiger Huang51c5a1d2018-12-11 20:24:51 +08003073 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS
3074 && mode != UPDATE_FOCUS_WILL_PLACE_SURFACES) {
3075 assignWindowLayers(false /* setLayoutNeeded */);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003076 }
3077 }
3078
3079 if (imWindowChanged) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003080 mWmService.mWindowsChanged = true;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003081 setLayoutNeeded();
Louis Changa9350fe2019-04-25 17:14:20 +08003082 newFocus = findFocusedWindowIfNeeded(topFocusedDisplayId);
Tiger Huang51c5a1d2018-12-11 20:24:51 +08003083 }
3084 if (mCurrentFocus != newFocus) {
3085 mWmService.mH.obtainMessage(REPORT_FOCUS_CHANGE, this).sendToTarget();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003086 }
3087
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003088 if (DEBUG_FOCUS_LIGHT || mWmService.localLOGV) Slog.v(TAG_WM, "Changing focus from "
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003089 + mCurrentFocus + " to " + newFocus + " displayId=" + getDisplayId()
3090 + " Callers=" + Debug.getCallers(4));
3091 final WindowState oldFocus = mCurrentFocus;
3092 mCurrentFocus = newFocus;
3093 mLosingFocus.remove(newFocus);
3094
3095 if (newFocus != null) {
3096 mWinAddedSinceNullFocus.clear();
3097 mWinRemovedSinceNullFocus.clear();
3098
3099 if (newFocus.canReceiveKeys()) {
3100 // Displaying a window implicitly causes dispatching to be unpaused.
3101 // This is to protect against bugs if someone pauses dispatching but
3102 // forgets to resume.
3103 newFocus.mToken.paused = false;
3104 }
3105 }
3106
Tiger Huang7c610aa2018-10-27 00:01:01 +08003107 int focusChanged = getDisplayPolicy().focusChangedLw(oldFocus, newFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003108
3109 if (imWindowChanged && oldFocus != mInputMethodWindow) {
3110 // Focus of the input method window changed. Perform layout if needed.
3111 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
3112 performLayout(true /*initial*/, updateInputWindows);
3113 focusChanged &= ~FINISH_LAYOUT_REDO_LAYOUT;
3114 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
3115 // Client will do the layout, but we need to assign layers
3116 // for handleNewWindowLocked() below.
3117 assignWindowLayers(false /* setLayoutNeeded */);
3118 }
3119 }
3120
3121 if ((focusChanged & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3122 // The change in focus caused us to need to do a layout. Okay.
3123 setLayoutNeeded();
3124 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
3125 performLayout(true /*initial*/, updateInputWindows);
3126 } else if (mode == UPDATE_FOCUS_REMOVING_FOCUS) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003127 mWmService.mRoot.performSurfacePlacement(false);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003128 }
3129 }
3130
3131 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
3132 // If we defer assigning layers, then the caller is responsible for doing this part.
3133 getInputMonitor().setInputFocusLw(newFocus, updateInputWindows);
3134 }
3135
3136 adjustForImeIfNeeded();
3137
3138 // We may need to schedule some toast windows to be removed. The toasts for an app that
3139 // does not have input focus are removed within a timeout to prevent apps to redress
3140 // other apps' UI.
3141 scheduleToastWindowsTimeoutIfNeededLocked(oldFocus, newFocus);
3142
3143 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
3144 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
3145 }
3146 return true;
3147 }
3148
3149 /**
3150 * Set the new focused app to this display.
3151 *
3152 * @param newFocus the new focused AppWindowToken.
3153 * @return true if the focused app is changed.
3154 */
3155 boolean setFocusedApp(AppWindowToken newFocus) {
3156 if (newFocus != null) {
3157 final DisplayContent appDisplay = newFocus.getDisplayContent();
3158 if (appDisplay != this) {
3159 throw new IllegalStateException(newFocus + " is not on " + getName()
3160 + " but " + ((appDisplay != null) ? appDisplay.getName() : "none"));
3161 }
3162 }
3163 if (mFocusedApp == newFocus) {
3164 return false;
3165 }
3166 mFocusedApp = newFocus;
3167 getInputMonitor().setFocusedAppLw(newFocus);
3168 updateTouchExcludeRegion();
3169 return true;
3170 }
3171
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003172 /** Updates the layer assignment of windows on this display. */
3173 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggi4981f152019-03-26 18:58:45 +01003174 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07003175 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003176 if (setLayoutNeeded) {
3177 setLayoutNeeded();
3178 }
Robert Carrb1579c82017-09-05 14:54:47 -07003179
Robert Carrf59b8dd2017-10-02 18:58:36 -07003180 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07003181 // the application of this transaction until the animation pass triggers
3182 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
3183 // the hiding and showing of surfaces.
3184 scheduleAnimation();
Jorim Jaggi4981f152019-03-26 18:58:45 +01003185 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003186 }
3187
Wale Ogunwale1666e312016-12-16 11:27:18 -08003188 // TODO: This should probably be called any time a visual change is made to the hierarchy like
3189 // moving containers or resizing them. Need to investigate the best way to have it automatically
3190 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003191 void layoutAndAssignWindowLayersIfNeeded() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003192 mWmService.mWindowsChanged = true;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003193 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003194
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003195 if (!mWmService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003196 false /*updateInputWindows*/)) {
3197 assignWindowLayers(false /* setLayoutNeeded */);
3198 }
3199
Arthur Hung95b38a92018-07-20 18:56:12 +08003200 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003201 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08003202 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003203 }
3204
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003205 /** Returns true if a leaked surface was destroyed */
3206 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003207 // Used to indicate that a surface was leaked.
3208 mTmpWindow = null;
3209 forAllWindows(w -> {
3210 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003211 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003212 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003213 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003214 if (!mWmService.mSessions.contains(wsa.mSession)) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003215 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003216 + w + " surface=" + wsa.mSurfaceController
3217 + " token=" + w.mToken
3218 + " pid=" + w.mSession.mPid
3219 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003220 wsa.destroySurface();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003221 mWmService.mForceRemoves.add(w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003222 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07003223 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003224 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003225 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02003226 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003227 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003228 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003229 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003230 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003231 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003232
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003233 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003234 }
3235
3236 /**
lumark90120a82018-08-15 00:33:03 +08003237 * Set input method window for the display.
3238 * @param win Set when window added or Null when destroyed.
3239 */
3240 void setInputMethodWindowLocked(WindowState win) {
3241 mInputMethodWindow = win;
lumark70865a82018-09-17 16:56:26 +08003242 // Update display configuration for IME process.
3243 if (mInputMethodWindow != null) {
3244 final int imePid = mInputMethodWindow.mSession.mPid;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003245 mWmService.mAtmInternal.onImeWindowSetOnDisplay(imePid,
lumark70865a82018-09-17 16:56:26 +08003246 mInputMethodWindow.getDisplayId());
3247 }
lumark90120a82018-08-15 00:33:03 +08003248 computeImeTarget(true /* updateImeTarget */);
Jorim Jaggif96c90a2018-09-26 16:55:15 +02003249 mInsetsStateController.getSourceProvider(TYPE_IME).setWindow(win,
3250 null /* frameProvider */);
lumark90120a82018-08-15 00:33:03 +08003251 }
3252
3253 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003254 * Determine and return the window that should be the IME target.
3255 * @param updateImeTarget If true the system IME target will be updated to match what we found.
3256 * @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 +00003257 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003258 WindowState computeImeTarget(boolean updateImeTarget) {
lumark90120a82018-08-15 00:33:03 +08003259 if (mInputMethodWindow == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003260 // There isn't an IME so there shouldn't be a target...That was easy!
3261 if (updateImeTarget) {
3262 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
lumarkff0ab692018-11-05 20:32:30 +08003263 + mInputMethodTarget + " to null since mInputMethodWindow is null");
3264 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003265 }
3266 return null;
3267 }
3268
lumarkff0ab692018-11-05 20:32:30 +08003269 final WindowState curTarget = mInputMethodTarget;
Chavi Weingarten3a748552018-05-14 17:32:42 +00003270 if (!canUpdateImeTarget()) {
3271 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
3272 return curTarget;
3273 }
3274
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003275 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
3276 // same display. Or even when the current IME/target are not on the same screen as the next
3277 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08003278 mUpdateImeTarget = updateImeTarget;
3279 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003280
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003281
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003282 // Yet more tricksyness! If this window is a "starting" window, we do actually want
3283 // to be on top of it, but it is not -really- where input will go. So look down below
3284 // for a real window to target...
3285 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
3286 final AppWindowToken token = target.mAppToken;
3287 if (token != null) {
3288 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
3289 if (betterTarget != null) {
3290 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003291 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003292 }
3293 }
3294
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003295 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
lumarkff0ab692018-11-05 20:32:30 +08003296 "Proposed new IME target: " + target + " for display: " + getDisplayId());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003297
chaviw1c13ad32018-06-12 16:44:23 -07003298 // Now, a special case -- if the last target's window is in the process of exiting, but
Tarandeep Singh2d0aca42019-03-28 14:08:58 -07003299 // not removed, keep on the last target to avoid IME flicker.
chaviw1c13ad32018-06-12 16:44:23 -07003300 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
Tarandeep Singh2d0aca42019-03-28 14:08:58 -07003301 && curTarget.isClosing()) {
3302 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Not changing target till current window is"
3303 + " closing and not removed");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003304 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003305 }
3306
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003307 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
3308 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003309
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003310 if (target == null) {
3311 if (updateImeTarget) {
3312 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
3313 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
3314 + Debug.getCallers(4) : ""));
lumarkff0ab692018-11-05 20:32:30 +08003315 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003316 }
3317
3318 return null;
3319 }
3320
3321 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003322 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
3323 if (token != null) {
3324
3325 // Now some fun for dealing with window animations that modify the Z order. We need
3326 // to look at all windows below the current target that are in this app, finding the
3327 // highest visible one in layering.
3328 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003329 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003330 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003331 }
3332
3333 if (highestTarget != null) {
lumark588a3e82018-07-20 18:53:54 +08003334 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, mAppTransition + " " + highestTarget
Jorim Jaggi35d328a2018-08-14 17:00:20 +02003335 + " animating=" + highestTarget.isAnimating());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003336
lumark588a3e82018-07-20 18:53:54 +08003337 if (mAppTransition.isTransitionSet()) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003338 // If we are currently setting up for an animation, hold everything until we
3339 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003340 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003341 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003342 }
3343 }
3344 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003345
3346 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
3347 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003348 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003349 }
3350
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003351 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003352 }
3353
lumarkff0ab692018-11-05 20:32:30 +08003354 /**
3355 * Calling {@link #computeImeTarget(boolean)} to update the input method target window in
3356 * the candidate app window token if needed.
3357 */
3358 void computeImeTargetIfNeeded(AppWindowToken candidate) {
3359 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == candidate) {
3360 computeImeTarget(true /* updateImeTarget */);
3361 }
3362 }
3363
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003364 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
lumarkff0ab692018-11-05 20:32:30 +08003365 if (target == mInputMethodTarget && mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003366 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003367 }
3368
lumarkff0ab692018-11-05 20:32:30 +08003369 mInputMethodTarget = target;
3370 mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003371 assignWindowLayers(false /* setLayoutNeeded */);
Tarandeep Singh215929b2019-01-11 18:24:37 -08003372 mInsetsStateController.onImeTargetChanged(target);
Tarandeep Singha6f35612019-01-11 19:50:46 -08003373 updateImeParent();
3374 }
3375
3376 private void updateImeParent() {
Jackal Guo818c3f12019-03-08 18:00:39 +08003377 // Force attaching IME to the display when magnifying, or it would be magnified with
3378 // target app together.
3379 final boolean shouldAttachToDisplay = (mMagnificationSpec != null);
3380 final SurfaceControl newParent =
3381 shouldAttachToDisplay ? mWindowingLayer : computeImeParent();
Tarandeep Singha6f35612019-01-11 19:50:46 -08003382 if (newParent != null) {
Tiger Huanged6794e2019-05-07 20:07:59 +08003383 getPendingTransaction().reparent(mImeWindowsContainers.mSurfaceControl, newParent);
Tarandeep Singha6f35612019-01-11 19:50:46 -08003384 scheduleAnimation();
3385 }
3386 }
3387
3388 /**
3389 * Computes the window the IME should be attached to.
3390 */
3391 @VisibleForTesting
3392 SurfaceControl computeImeParent() {
3393
3394 // Attach it to app if the target is part of an app and such app is covering the entire
3395 // screen. If it's not covering the entire screen the IME might extend beyond the apps
3396 // bounds.
Riddle Hsu6d6f67c2019-03-14 16:54:26 +08003397 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken != null
3398 && mInputMethodTarget.getWindowingMode() == WINDOWING_MODE_FULLSCREEN
3399 // An activity with override bounds should be letterboxed inside its parent bounds,
3400 // so it doesn't fill the screen.
3401 && mInputMethodTarget.mAppToken.matchParentBounds()) {
Tarandeep Singha6f35612019-01-11 19:50:46 -08003402 return mInputMethodTarget.mAppToken.getSurfaceControl();
3403 }
3404
3405 // Otherwise, we just attach it to the display.
3406 return mWindowingLayer;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003407 }
3408
3409 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
3410 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3411 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
3412 }
3413
3414 // Used to indicate we have reached the first window in the range we are interested in.
3415 mTmpWindow = null;
3416
3417 // TODO: Figure-out a more efficient way to do this.
3418 final WindowState candidate = getWindow(w -> {
3419 if (w == top) {
3420 // Reached the first window in the range we are interested in.
3421 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003422 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003423 if (mTmpWindow == null) {
3424 return false;
3425 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003426
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003427 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3428 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003429 }
3430 // If we reached the bottom of the range of windows we are considering,
3431 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003432 if (w == bottom) {
3433 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003434 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003435 return false;
3436 });
3437
3438 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003439 }
3440
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003441 void setLayoutNeeded() {
3442 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
3443 mLayoutNeeded = true;
3444 }
3445
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003446 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003447 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
3448 mLayoutNeeded = false;
3449 }
3450
3451 boolean isLayoutNeeded() {
3452 return mLayoutNeeded;
3453 }
3454
Wale Ogunwale02319a62016-09-26 15:21:22 -07003455 void dumpTokens(PrintWriter pw, boolean dumpAll) {
3456 if (mTokenMap.isEmpty()) {
3457 return;
3458 }
3459 pw.println(" Display #" + mDisplayId);
3460 final Iterator<WindowToken> it = mTokenMap.values().iterator();
3461 while (it.hasNext()) {
3462 final WindowToken token = it.next();
3463 pw.print(" ");
3464 pw.print(token);
3465 if (dumpAll) {
3466 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003467 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07003468 } else {
3469 pw.println();
3470 }
3471 }
lumark588a3e82018-07-20 18:53:54 +08003472
Evan Rosky2289ba12018-11-19 18:28:18 -08003473 if (!mOpeningApps.isEmpty() || !mClosingApps.isEmpty() || !mChangingApps.isEmpty()) {
lumark588a3e82018-07-20 18:53:54 +08003474 pw.println();
3475 if (mOpeningApps.size() > 0) {
3476 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
3477 }
3478 if (mClosingApps.size() > 0) {
3479 pw.print(" mClosingApps="); pw.println(mClosingApps);
3480 }
Evan Rosky2289ba12018-11-19 18:28:18 -08003481 if (mChangingApps.size() > 0) {
3482 pw.print(" mChangingApps="); pw.println(mChangingApps);
3483 }
lumark588a3e82018-07-20 18:53:54 +08003484 }
3485
3486 mUnknownAppVisibilityController.dump(pw, " ");
Wale Ogunwale02319a62016-09-26 15:21:22 -07003487 }
3488
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003489 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003490 final int[] index = new int[1];
3491 forAllWindows(w -> {
3492 final WindowStateAnimator wAnim = w.mWinAnimator;
3493 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
3494 index[0] = index[0] + 1;
3495 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003496 }
3497
Jorim Jaggife762342016-10-13 14:33:27 +02003498 /**
3499 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
3500 */
Issei Suzuki5609ccb2019-06-13 15:04:08 +02003501 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade,
3502 boolean subtle) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003503 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003504 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02003505 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
3506 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Issei Suzuki5609ccb2019-06-13 15:04:08 +02003507 w.startAnimation(policy.createHiddenByKeyguardExit(
3508 onWallpaper, goingToShade, subtle));
Jorim Jaggife762342016-10-13 14:33:27 +02003509 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003510 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02003511 }
3512
Wale Ogunwale494009b82016-10-21 09:01:38 -07003513 boolean checkWaitingForWindows() {
3514
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003515 mHaveBootMsg = false;
3516 mHaveApp = false;
3517 mHaveWallpaper = false;
3518 mHaveKeyguard = true;
3519
3520 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003521 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
3522 return true;
3523 }
3524 if (w.isDrawnLw()) {
3525 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003526 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003527 } else if (w.mAttrs.type == TYPE_APPLICATION
3528 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003529 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003530 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003531 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003532 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003533 mHaveKeyguard = mWmService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003534 }
3535 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003536 return false;
3537 });
3538
3539 if (visibleWindow != null) {
3540 // We have a visible window.
3541 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003542 }
3543
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003544 // if the wallpaper service is disabled on the device, we're never going to have
3545 // wallpaper, don't bother waiting for it
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003546 boolean wallpaperEnabled = mWmService.mContext.getResources().getBoolean(
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003547 com.android.internal.R.bool.config_enableWallpaperService)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003548 && mWmService.mContext.getResources().getBoolean(
Daichi Hironoc1432222018-03-29 13:06:07 +09003549 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003550 && !mWmService.mOnlyCore;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003551
Wale Ogunwale494009b82016-10-21 09:01:38 -07003552 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003553 "******** booted=" + mWmService.mSystemBooted
3554 + " msg=" + mWmService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003555 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
3556 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
3557 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003558
3559 // If we are turning on the screen to show the boot message, don't do it until the boot
3560 // message is actually displayed.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003561 if (!mWmService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003562 return true;
3563 }
3564
3565 // If we are turning on the screen after the boot is completed normally, don't do so until
3566 // we have the application and wallpaper.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003567 if (mWmService.mSystemBooted
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003568 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003569 return true;
3570 }
3571
3572 return false;
3573 }
3574
Jorim Jaggi8f520872018-08-14 17:00:20 +02003575 void updateWindowsForAnimator() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003576 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003577 }
3578
Jorim Jaggi8f520872018-08-14 17:00:20 +02003579 /**
3580 * Updates the {@link TaskStack#setAnimationBackground} for all windows.
3581 */
3582 void updateBackgroundForAnimator() {
Jorim Jaggi7156b652016-10-25 08:31:58 -07003583 resetAnimationBackgroundAnimator();
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003584 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003585 }
3586
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003587 boolean isInputMethodClientFocus(int uid, int pid) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003588 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003589 if (imFocus == null) {
3590 return false;
3591 }
3592
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003593 if (DEBUG_INPUT_METHOD) {
3594 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003595 Slog.i(TAG_WM, "Current focus: " + mCurrentFocus + " displayId=" + mDisplayId);
3596 Slog.i(TAG_WM, "Last focus: " + mLastFocus + " displayId=" + mDisplayId);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003597 }
3598
Wale Ogunwale494009b82016-10-21 09:01:38 -07003599 if (DEBUG_INPUT_METHOD) {
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003600 Slog.i(TAG_WM, "IM target uid/pid: " + imFocus.mSession.mUid
3601 + "/" + imFocus.mSession.mPid);
3602 Slog.i(TAG_WM, "Requesting client uid/pid: " + uid + "/" + pid);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003603 }
3604
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003605 return imFocus.mSession.mUid == uid && imFocus.mSession.mPid == pid;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003606 }
3607
3608 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003609 final WindowState win = getWindow(
3610 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
3611 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003612 }
3613
Tiger Huang7c610aa2018-10-27 00:01:01 +08003614 void statusBarVisibilityChanged(int visibility) {
3615 mLastStatusBarVisibility = visibility;
3616 visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(visibility);
3617 updateStatusBarVisibilityLocked(visibility);
3618 }
3619
3620 private boolean updateStatusBarVisibilityLocked(int visibility) {
3621 if (mLastDispatchedSystemUiVisibility == visibility) {
3622 return false;
3623 }
3624 final int globalDiff = (visibility ^ mLastDispatchedSystemUiVisibility)
3625 // We are only interested in differences of one of the
3626 // clearable flags...
3627 & View.SYSTEM_UI_CLEARABLE_FLAGS
3628 // ...if it has actually been cleared.
3629 & ~visibility;
3630
3631 mLastDispatchedSystemUiVisibility = visibility;
3632 if (isDefaultDisplay) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003633 mWmService.mInputManager.setSystemUiVisibility(visibility);
Tiger Huang7c610aa2018-10-27 00:01:01 +08003634 }
3635 updateSystemUiVisibility(visibility, globalDiff);
3636 return true;
3637 }
3638
Wale Ogunwale494009b82016-10-21 09:01:38 -07003639 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003640 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003641 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003642 final int curValue = w.mSystemUiVisibility;
3643 final int diff = (curValue ^ visibility) & globalDiff;
3644 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003645 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003646 w.mSeq++;
3647 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003648 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003649 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
3650 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07003651 visibility, newValue, diff);
3652 }
3653 } catch (RemoteException e) {
3654 // so sorry
3655 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003656 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003657 }
3658
Tiger Huang7c610aa2018-10-27 00:01:01 +08003659 void reevaluateStatusBarVisibility() {
3660 int visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(mLastStatusBarVisibility);
3661 if (updateStatusBarVisibilityLocked(visibility)) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003662 mWmService.mWindowPlacerLocked.requestTraversal();
Tiger Huang7c610aa2018-10-27 00:01:01 +08003663 }
3664 }
3665
Wale Ogunwale494009b82016-10-21 09:01:38 -07003666 void onWindowFreezeTimeout() {
3667 Slog.w(TAG_WM, "Window freeze timeout expired.");
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003668 mWmService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003669
3670 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07003671 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003672 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003673 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07003674 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003675 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003676 - mWmService.mDisplayFreezeTime);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003677 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003678 }, true /* traverseTopToBottom */);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003679 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003680 }
3681
3682 void waitForAllWindowsDrawn() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003683 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003684 forAllWindows(w -> {
3685 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
3686 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
3687 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003688 // Force add to mResizingWindows.
chaviw9c81e632018-07-31 11:17:52 -07003689 w.resetLastContentInsets();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003690 mWmService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003691 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003692 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003693 }
3694
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003695 // TODO: Super crazy long method that should be broken down...
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003696 void applySurfaceChangesTransaction(boolean recoveringMemory) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003697 final WindowSurfacePlacer surfacePlacer = mWmService.mWindowPlacerLocked;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003698
3699 mTmpUpdateAllDrawn.clear();
3700
3701 int repeats = 0;
3702 do {
3703 repeats++;
3704 if (repeats > 6) {
3705 Slog.w(TAG, "Animation repeat aborted after too many iterations");
3706 clearLayoutNeeded();
3707 break;
3708 }
3709
3710 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
3711 pendingLayoutChanges);
3712
wilsonshihc32538e2018-11-07 17:27:34 +08003713 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
3714 mWallpaperController.adjustWallpaperWindows();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003715 }
3716
Riddle Hsu654a6f92018-07-13 22:59:36 +08003717 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003718 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Riddle Hsu4e611772018-10-31 18:58:28 +08003719 if (updateOrientationFromAppTokens()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003720 setLayoutNeeded();
Riddle Hsu4e611772018-10-31 18:58:28 +08003721 sendNewConfiguration();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003722 }
3723 }
3724
3725 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3726 setLayoutNeeded();
3727 }
3728
3729 // FIRST LOOP: Perform a layout, if needed.
3730 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
3731 performLayout(repeats == 1, false /* updateInputWindows */);
3732 } else {
3733 Slog.w(TAG, "Layout repeat skipped after too many iterations");
3734 }
3735
3736 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
3737 pendingLayoutChanges = 0;
3738
Jorim Jaggi4981f152019-03-26 18:58:45 +01003739 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "applyPostLayoutPolicy");
3740 try {
3741 mDisplayPolicy.beginPostLayoutPolicyLw();
3742 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
3743 pendingLayoutChanges |= mDisplayPolicy.finishPostLayoutPolicyLw();
3744 } finally {
3745 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
3746 }
Tiger Huang7c610aa2018-10-27 00:01:01 +08003747 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
3748 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
Jorim Jaggif96c90a2018-09-26 16:55:15 +02003749 mInsetsStateController.onPostLayout();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003750 } while (pendingLayoutChanges != 0);
3751
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003752 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003753
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003754 mTmpRecoveringMemory = recoveringMemory;
Jorim Jaggi4981f152019-03-26 18:58:45 +01003755
3756 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "applyWindowSurfaceChanges");
3757 try {
3758 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
3759 } finally {
3760 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
3761 }
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01003762 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003763
Jorim Jaggif1292892018-09-10 11:58:13 +02003764 mLastHasContent = mTmpApplySurfaceChangesTransactionState.displayHasContent;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003765 mWmService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Jorim Jaggif1292892018-09-10 11:58:13 +02003766 mLastHasContent,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003767 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
3768 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003769 true /* inTraversal, must call performTraversalInTrans... below */);
3770
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003771 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
3772 if (wallpaperVisible != mLastWallpaperVisible) {
3773 mLastWallpaperVisible = wallpaperVisible;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003774 mWmService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003775 }
3776
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003777 while (!mTmpUpdateAllDrawn.isEmpty()) {
3778 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
3779 // See if any windows have been drawn, so they (and others associated with them)
3780 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07003781 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003782 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003783 }
3784
Bryce Leef3c6a472017-11-14 14:53:06 -08003785 private void updateBounds() {
Evan Rosky39b6f232018-10-30 18:35:41 -07003786 calculateBounds(mDisplayInfo, mTmpBounds);
Bryce Leef3c6a472017-11-14 14:53:06 -08003787 setBounds(mTmpBounds);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08003788 if (mPortalWindowHandle != null && mParentSurfaceControl != null) {
3789 mPortalWindowHandle.touchableRegion.getBounds(mTmpRect);
3790 if (!mTmpBounds.equals(mTmpRect)) {
3791 mPortalWindowHandle.touchableRegion.set(mTmpBounds);
Tiger Huanged6794e2019-05-07 20:07:59 +08003792 getPendingTransaction().setInputWindowInfo(
3793 mParentSurfaceControl, mPortalWindowHandle);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08003794 }
3795 }
Bryce Leef3c6a472017-11-14 14:53:06 -08003796 }
3797
3798 // Determines the current display bounds based on the current state
Evan Rosky39b6f232018-10-30 18:35:41 -07003799 private void calculateBounds(DisplayInfo displayInfo, Rect out) {
Bryce Leef3c6a472017-11-14 14:53:06 -08003800 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Evan Rosky39b6f232018-10-30 18:35:41 -07003801 final int rotation = displayInfo.rotation;
3802 boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Bryce Leef3c6a472017-11-14 14:53:06 -08003803 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
3804 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Evan Rosky39b6f232018-10-30 18:35:41 -07003805 int width = displayInfo.logicalWidth;
Bryce Leef3c6a472017-11-14 14:53:06 -08003806 int left = (physWidth - width) / 2;
Evan Rosky39b6f232018-10-30 18:35:41 -07003807 int height = displayInfo.logicalHeight;
Bryce Leef3c6a472017-11-14 14:53:06 -08003808 int top = (physHeight - height) / 2;
3809 out.set(left, top, left + width, top + height);
3810 }
3811
Bryce Leef3c6a472017-11-14 14:53:06 -08003812 private void getBounds(Rect out, int orientation) {
3813 getBounds(out);
3814
3815 // Rotate the Rect if needed.
3816 final int currentRotation = mDisplayInfo.rotation;
3817 final int rotationDelta = deltaRotation(currentRotation, orientation);
3818 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3819 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3820 mTmpRectF.set(out);
3821 mTmpMatrix.mapRect(mTmpRectF);
3822 mTmpRectF.round(out);
3823 }
3824 }
3825
Evan Rosky730f6e82018-12-03 17:40:11 -08003826 /** @returns the orientation of the display when it's rotation is ROTATION_0. */
3827 int getNaturalOrientation() {
3828 return mBaseDisplayWidth < mBaseDisplayHeight
3829 ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
3830 }
3831
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003832 void performLayout(boolean initial, boolean updateInputWindows) {
Jorim Jaggi4981f152019-03-26 18:58:45 +01003833 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "performLayout");
3834 try {
3835 performLayoutNoTrace(initial, updateInputWindows);
3836 } finally {
3837 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
3838 }
3839 }
3840
3841 private void performLayoutNoTrace(boolean initial, boolean updateInputWindows) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003842 if (!isLayoutNeeded()) {
3843 return;
3844 }
3845 clearLayoutNeeded();
3846
3847 final int dw = mDisplayInfo.logicalWidth;
3848 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003849 if (DEBUG_LAYOUT) {
3850 Slog.v(TAG, "-------------------------------------");
Jorim Jaggi4981f152019-03-26 18:58:45 +01003851 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw
3852 + " dh=" + dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003853 }
3854
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003855 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3856 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Jorim Jaggi4981f152019-03-26 18:58:45 +01003857 // TODO: Not sure if we really need to set the rotation here since we are updating from
3858 // the display info above...
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00003859 mDisplayFrames.mRotation = mRotation;
Tiger Huang7c610aa2018-10-27 00:01:01 +08003860 mDisplayPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003861
Adrian Roos5251b1d2018-03-23 18:57:43 +01003862 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003863 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003864 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003865
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003866 // Used to indicate that we have processed the dream window and all additional windows are
3867 // behind it.
3868 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003869 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003870
3871 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003872 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003873
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003874 // Used to indicate that we have processed the dream window and all additional attached
3875 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003876 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003877 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003878
3879 // Now perform layout of attached windows, which usually depend on the position of the
3880 // window they are attached to. XXX does not deal with windows that are attached to windows
3881 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003882 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003883
3884 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003885 mInputMonitor.layoutInputConsumers(dw, dh);
3886 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003887 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003888 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003889 }
3890
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003891 mWmService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003892 }
3893
3894 /**
3895 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3896 * In portrait mode, it grabs the full screenshot.
3897 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003898 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003899 */
chaviw0315a1a2018-03-05 15:28:35 -08003900 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003901 if (!mWmService.mPolicy.isScreenOn()) {
chaviw0315a1a2018-03-05 15:28:35 -08003902 if (DEBUG_SCREENSHOT) {
3903 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003904 }
chaviw0315a1a2018-03-05 15:28:35 -08003905 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003906 }
chaviwfbe47df2017-11-10 16:14:49 -08003907
chaviw0315a1a2018-03-05 15:28:35 -08003908 int dw = mDisplayInfo.logicalWidth;
3909 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003910
chaviw0315a1a2018-03-05 15:28:35 -08003911 if (dw <= 0 || dh <= 0) {
3912 return null;
3913 }
chaviwfbe47df2017-11-10 16:14:49 -08003914
chaviw0315a1a2018-03-05 15:28:35 -08003915 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003916
chaviw0315a1a2018-03-05 15:28:35 -08003917 // The screenshot API does not apply the current screen rotation.
3918 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003919
chaviw0315a1a2018-03-05 15:28:35 -08003920 if (rot == ROTATION_90 || rot == ROTATION_270) {
3921 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3922 }
chaviwfbe47df2017-11-10 16:14:49 -08003923
chaviw0315a1a2018-03-05 15:28:35 -08003924 // SurfaceFlinger is not aware of orientation, so convert our logical
3925 // crop to SurfaceFlinger's portrait orientation.
3926 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3927
3928 final ScreenRotationAnimation screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003929 mWmService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
chaviw0315a1a2018-03-05 15:28:35 -08003930 final boolean inRotation = screenRotationAnimation != null &&
3931 screenRotationAnimation.isAnimating();
3932 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3933
3934 // TODO(b/68392460): We should screenshot Task controls directly
3935 // but it's difficult at the moment as the Task doesn't have the
3936 // correct size set.
chaviw08520a02018-09-10 16:44:56 -07003937 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, inRotation, rot);
chaviw0315a1a2018-03-05 15:28:35 -08003938 if (bitmap == null) {
3939 Slog.w(TAG_WM, "Failed to take screenshot");
3940 return null;
3941 }
3942
3943 // Create a copy of the screenshot that is immutable and backed in ashmem.
3944 // This greatly reduces the overhead of passing the bitmap between processes.
3945 final Bitmap ret = bitmap.createAshmemBitmap(config);
3946 bitmap.recycle();
3947 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003948 }
3949
3950 // TODO: Can this use createRotationMatrix()?
3951 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3952 if (rot == Surface.ROTATION_90) {
3953 final int tmp = crop.top;
3954 crop.top = dw - crop.right;
3955 crop.right = crop.bottom;
3956 crop.bottom = dw - crop.left;
3957 crop.left = tmp;
3958 } else if (rot == Surface.ROTATION_180) {
3959 int tmp = crop.top;
3960 crop.top = dh - crop.bottom;
3961 crop.bottom = dh - tmp;
3962 tmp = crop.right;
3963 crop.right = dw - crop.left;
3964 crop.left = dw - tmp;
3965 } else if (rot == Surface.ROTATION_270) {
3966 final int tmp = crop.top;
3967 crop.top = crop.left;
3968 crop.left = dh - crop.bottom;
3969 crop.bottom = crop.right;
3970 crop.right = dh - tmp;
3971 }
3972 }
3973
3974 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003975 // Used to indicate the layout is needed.
3976 mTmpWindow = null;
3977
3978 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003979 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003980 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003981 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003982 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003983 w.setDisplayLayoutNeeded();
Vishnu Nairba183352018-11-21 11:16:49 -08003984 w.finishSeamlessRotation(true /* timeout */);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003985 mWmService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003986 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003987
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003988 if (mTmpWindow != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003989 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003990 }
3991 }
3992
Wale Ogunwale1666e312016-12-16 11:27:18 -08003993 void setExitingTokensHasVisible(boolean hasVisible) {
3994 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3995 mExitingTokens.get(i).hasVisible = hasVisible;
3996 }
3997
3998 // Initialize state of exiting applications.
3999 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
4000 }
4001
4002 void removeExistingTokensIfPossible() {
4003 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
4004 final WindowToken token = mExitingTokens.get(i);
4005 if (!token.hasVisible) {
4006 mExitingTokens.remove(i);
4007 }
4008 }
4009
4010 // Time to remove any exiting applications?
4011 mTaskStackContainers.removeExistingAppTokensIfPossible();
4012 }
4013
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07004014 @Override
4015 void onDescendantOverrideConfigurationChanged() {
4016 setLayoutNeeded();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004017 mWmService.requestTraversal();
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07004018 }
4019
David Stevens9440dc82017-03-16 19:00:20 -07004020 boolean okToDisplay() {
4021 if (mDisplayId == DEFAULT_DISPLAY) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004022 return !mWmService.mDisplayFrozen
4023 && mWmService.mDisplayEnabled && mWmService.mPolicy.isScreenOn();
David Stevens9440dc82017-03-16 19:00:20 -07004024 }
4025 return mDisplayInfo.state == Display.STATE_ON;
4026 }
4027
4028 boolean okToAnimate() {
4029 return okToDisplay() &&
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004030 (mDisplayId != DEFAULT_DISPLAY || mWmService.mPolicy.okToAnimate());
David Stevens9440dc82017-03-16 19:00:20 -07004031 }
4032
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07004033 static final class TaskForResizePointSearchResult {
4034 boolean searchDone;
4035 Task taskForResize;
4036
4037 void reset() {
4038 searchDone = false;
4039 taskForResize = null;
4040 }
4041 }
Robert Carr3b716242016-08-16 16:02:21 -07004042
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004043 private static final class ApplySurfaceChangesTransactionState {
4044 boolean displayHasContent;
4045 boolean obscured;
4046 boolean syswin;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004047 float preferredRefreshRate;
4048 int preferredModeId;
4049
4050 void reset() {
4051 displayHasContent = false;
4052 obscured = false;
4053 syswin = false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004054 preferredRefreshRate = 0;
4055 preferredModeId = 0;
4056 }
4057 }
4058
4059 private static final class ScreenshotApplicationState {
4060 WindowState appWin;
4061 int maxLayer;
4062 int minLayer;
4063 boolean screenshotReady;
4064
4065 void reset(boolean screenshotReady) {
4066 appWin = null;
4067 maxLayer = 0;
4068 minLayer = 0;
4069 this.screenshotReady = screenshotReady;
4070 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
4071 }
4072 }
4073
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004074 /**
4075 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
4076 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
4077 * homogeneous children type which is currently required by sub-classes of
4078 * {@link WindowContainer} class.
4079 */
4080 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
4081
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004082 DisplayChildWindowContainer(WindowManagerService service) {
4083 super(service);
4084 }
4085
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004086 @Override
4087 boolean fillsParent() {
4088 return true;
4089 }
4090
4091 @Override
4092 boolean isVisible() {
4093 return true;
Robert Carr3b716242016-08-16 16:02:21 -07004094 }
4095 }
4096
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004097 /**
4098 * Window container class that contains all containers on this display relating to Apps.
4099 * I.e Activities.
4100 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07004101 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07004102 /**
4103 * A control placed at the appropriate level for transitions to occur.
4104 */
chaviw23ee71c2017-12-18 11:29:41 -08004105 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02004106 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02004107 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004108
Robert Carrf7a7ca82017-12-06 13:17:07 -08004109 /**
4110 * Given that the split-screen divider does not have an AppWindowToken, it
4111 * will have to live inside of a "NonAppWindowContainer", in particular
4112 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
4113 * it will need to be interleaved with some of our children, appearing on top of
4114 * both docked stacks but underneath any assistant stacks.
4115 *
4116 * To solve this problem we have this anchor control, which will always exist so
4117 * we can always assign it the correct value in our {@link #assignChildLayers}.
4118 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
4119 * assign the divider a layer relative to it. This way we prevent linking lifecycle
4120 * events between the two containers.
4121 */
4122 SurfaceControl mSplitScreenDividerAnchor = null;
4123
Wale Ogunwale61911492017-10-11 08:50:50 -07004124 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
4125 // through the list to find them.
4126 private TaskStack mHomeStack = null;
4127 private TaskStack mPinnedStack = null;
4128 private TaskStack mSplitScreenPrimaryStack = null;
4129
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004130 TaskStackContainers(WindowManagerService service) {
4131 super(service);
4132 }
4133
Wale Ogunwale61911492017-10-11 08:50:50 -07004134 /**
4135 * Returns the topmost stack on the display that is compatible with the input windowing mode
4136 * and activity type. Null is no compatible stack on the display.
4137 */
4138 TaskStack getStack(int windowingMode, int activityType) {
4139 if (activityType == ACTIVITY_TYPE_HOME) {
4140 return mHomeStack;
4141 }
4142 if (windowingMode == WINDOWING_MODE_PINNED) {
4143 return mPinnedStack;
4144 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4145 return mSplitScreenPrimaryStack;
4146 }
4147 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
4148 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004149 if (activityType == ACTIVITY_TYPE_UNDEFINED
4150 && windowingMode == stack.getWindowingMode()) {
4151 // Passing in undefined type means we want to match the topmost stack with the
4152 // windowing mode.
4153 return stack;
4154 }
Wale Ogunwale61911492017-10-11 08:50:50 -07004155 if (stack.isCompatible(windowingMode, activityType)) {
4156 return stack;
4157 }
4158 }
4159 return null;
4160 }
4161
4162 @VisibleForTesting
4163 TaskStack getTopStack() {
4164 return mTaskStackContainers.getChildCount() > 0
4165 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
4166 }
4167
4168 TaskStack getHomeStack() {
4169 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
4170 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
4171 }
4172 return mHomeStack;
4173 }
4174
4175 TaskStack getPinnedStack() {
4176 return mPinnedStack;
4177 }
4178
Matthew Ng64e77cf2017-10-31 14:01:31 -07004179 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07004180 return mSplitScreenPrimaryStack;
4181 }
4182
Winson Chunge2d72172018-01-25 17:46:20 +00004183 ArrayList<Task> getVisibleTasks() {
4184 final ArrayList<Task> visibleTasks = new ArrayList<>();
4185 forAllTasks(task -> {
4186 if (task.isVisible()) {
4187 visibleTasks.add(task);
4188 }
4189 });
4190 return visibleTasks;
4191 }
4192
Andrii Kulian839def92016-11-02 10:58:58 -07004193 /**
4194 * Adds the stack to this container.
Andrii Kulian839def92016-11-02 10:58:58 -07004195 */
4196 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07004197 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004198 addChild(stack, onTop);
4199 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07004200 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004201
Wale Ogunwale61911492017-10-11 08:50:50 -07004202 void onStackWindowingModeChanged(TaskStack stack) {
4203 removeStackReferenceIfNeeded(stack);
4204 addStackReferenceIfNeeded(stack);
4205 if (stack == mPinnedStack && getTopStack() != stack) {
4206 // Looks like this stack changed windowing mode to pinned. Move it to the top.
4207 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
4208 }
4209 }
4210
4211 private void addStackReferenceIfNeeded(TaskStack stack) {
4212 if (stack.isActivityTypeHome()) {
4213 if (mHomeStack != null) {
Louis Chang7d0037c2018-08-13 12:42:06 +08004214 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07004215 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang7d0037c2018-08-13 12:42:06 +08004216
Wale Ogunwale61911492017-10-11 08:50:50 -07004217 }
Louis Chang7d0037c2018-08-13 12:42:06 +08004218 mHomeStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07004219 }
4220 final int windowingMode = stack.getWindowingMode();
4221 if (windowingMode == WINDOWING_MODE_PINNED) {
4222 if (mPinnedStack != null) {
4223 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
4224 + mPinnedStack + " already exist on display=" + this
4225 + " stack=" + stack);
4226 }
4227 mPinnedStack = stack;
4228 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4229 if (mSplitScreenPrimaryStack != null) {
4230 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
4231 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
4232 + " already exist on display=" + this + " stack=" + stack);
4233 }
4234 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004235 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07004236 }
4237 }
4238
4239 private void removeStackReferenceIfNeeded(TaskStack stack) {
4240 if (stack == mHomeStack) {
4241 mHomeStack = null;
4242 } else if (stack == mPinnedStack) {
4243 mPinnedStack = null;
4244 } else if (stack == mSplitScreenPrimaryStack) {
4245 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004246 // Re-set the split-screen create mode whenever the split-screen stack is removed.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004247 mWmService.setDockedStackCreateStateLocked(
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004248 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
4249 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07004250 }
Andrii Kulian839def92016-11-02 10:58:58 -07004251 }
4252
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004253 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004254 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
4255 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004256 addChild(stack, addIndex);
4257 setLayoutNeeded();
4258 }
4259
Wale Ogunwale61911492017-10-11 08:50:50 -07004260 @Override
4261 protected void removeChild(TaskStack stack) {
4262 super.removeChild(stack);
4263 removeStackReferenceIfNeeded(stack);
4264 }
Bryce Lee00d586d2017-07-28 20:48:43 -07004265
4266 @Override
4267 boolean isOnTop() {
4268 // Considered always on top
4269 return true;
4270 }
4271
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004272 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08004273 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07004274 if (child.getWindowConfiguration().isAlwaysOnTop()
4275 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08004276 // This stack is always-on-top, override the default behavior.
4277 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
4278
4279 // Moving to its current position, as we must call super but we don't want to
4280 // perform any meaningful action.
4281 final int currentPosition = mChildren.indexOf(child);
4282 super.positionChildAt(currentPosition, child, false /* includingParents */);
4283 return;
4284 }
4285
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004286 final int targetPosition = findPositionForStack(position, child, false /* adding */);
4287 super.positionChildAt(targetPosition, child, includingParents);
4288
Riddle Hsu2f6e1742018-08-23 22:44:36 +08004289 if (includingParents) {
4290 // We still want to move the display of this stack container to top because even the
4291 // target position is adjusted to non-top, the intention of the condition is to have
4292 // higher z-order to gain focus (e.g. moving a task of a fullscreen stack to front
4293 // in a non-top display which is using picture-in-picture mode).
4294 final int topChildPosition = getChildCount() - 1;
4295 if (targetPosition < topChildPosition && position >= topChildPosition) {
4296 getParent().positionChildAt(POSITION_TOP, this /* child */,
4297 true /* includingParents */);
4298 }
4299 }
4300
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004301 setLayoutNeeded();
4302 }
4303
4304 /**
4305 * When stack is added or repositioned, find a proper position for it.
4306 * This will make sure that pinned stack always stays on top.
4307 * @param requestedPosition Position requested by caller.
4308 * @param stack Stack to be added or positioned.
4309 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
4310 * @return The proper position for the stack.
4311 */
4312 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004313 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09004314 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09004315 }
4316
Kazuki Takisef85197b2018-06-18 18:18:36 +09004317 final int topChildPosition = mChildren.size() - 1;
4318 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
4319 for (int i = topChildPosition; i >= 0; --i) {
4320 if (getStacks().get(i) != stack && !getStacks().get(i).isAlwaysOnTop()) {
4321 belowAlwaysOnTopPosition = i;
4322 break;
4323 }
4324 }
4325
4326 // The max possible position we can insert the stack at.
4327 int maxPosition = POSITION_TOP;
4328 // The min possible position we can insert the stack at.
4329 int minPosition = POSITION_BOTTOM;
4330
4331 if (stack.isAlwaysOnTop()) {
4332 if (hasPinnedStack()) {
4333 // Always-on-top stacks go below the pinned stack.
4334 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
4335 }
4336 // Always-on-top stacks need to be above all other stacks.
4337 minPosition = belowAlwaysOnTopPosition !=
4338 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
4339 } else {
4340 // Other stacks need to be below the always-on-top stacks.
4341 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08004342 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09004343 }
4344
4345 int targetPosition = requestedPosition;
4346 targetPosition = Math.min(targetPosition, maxPosition);
4347 targetPosition = Math.max(targetPosition, minPosition);
4348
4349 int prevPosition = getStacks().indexOf(stack);
4350 // The positions we calculated above (maxPosition, minPosition) do not take into
4351 // consideration the following edge cases.
4352 // 1) We need to adjust the position depending on the value "adding".
4353 // 2) When we are moving a stack to another position, we also need to adjust the
4354 // position depending on whether the stack is moving to a higher or lower position.
4355 if ((targetPosition != requestedPosition) &&
4356 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004357 targetPosition++;
4358 }
4359
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004360 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08004361 }
4362
4363 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004364 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
4365 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004366 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004367 if (super.forAllWindows(callback, traverseTopToBottom)) {
4368 return true;
4369 }
4370 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4371 return true;
4372 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004373 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004374 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4375 return true;
4376 }
4377 if (super.forAllWindows(callback, traverseTopToBottom)) {
4378 return true;
4379 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004380 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004381 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004382 }
4383
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004384 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004385 boolean traverseTopToBottom) {
4386 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
4387 // app tokens.
4388 // TODO: Investigate if we need to continue to do this or if we can just process them
4389 // in-order.
4390 if (traverseTopToBottom) {
4391 for (int i = mChildren.size() - 1; i >= 0; --i) {
4392 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4393 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004394 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4395 traverseTopToBottom)) {
4396 return true;
4397 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004398 }
4399 }
4400 } else {
4401 final int count = mChildren.size();
4402 for (int i = 0; i < count; ++i) {
4403 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4404 final int appTokensCount = appTokens.size();
4405 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004406 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4407 traverseTopToBottom)) {
4408 return true;
4409 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004410 }
4411 }
4412 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004413 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004414 }
4415
Wale Ogunwale1666e312016-12-16 11:27:18 -08004416 void setExitingTokensHasVisible(boolean hasVisible) {
4417 for (int i = mChildren.size() - 1; i >= 0; --i) {
4418 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4419 for (int j = appTokens.size() - 1; j >= 0; --j) {
4420 appTokens.get(j).hasVisible = hasVisible;
4421 }
4422 }
4423 }
4424
4425 void removeExistingAppTokensIfPossible() {
4426 for (int i = mChildren.size() - 1; i >= 0; --i) {
4427 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4428 for (int j = appTokens.size() - 1; j >= 0; --j) {
4429 final AppWindowToken token = appTokens.get(j);
lumark588a3e82018-07-20 18:53:54 +08004430 if (!token.hasVisible && !mClosingApps.contains(token)
Wale Ogunwale1666e312016-12-16 11:27:18 -08004431 && (!token.mIsExiting || token.isEmpty())) {
4432 // Make sure there is no animation running on this token, so any windows
4433 // associated with it will be removed as soon as their animations are
4434 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02004435 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004436 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
4437 "performLayout: App token exiting now removed" + token);
4438 token.removeIfPossible();
4439 }
4440 }
4441 }
4442 }
4443
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004444 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004445 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004446 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
4447 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004448 // Apps and their containers are not allowed to specify an orientation while the
Wale Ogunwaled0c225c2018-06-15 14:47:46 -07004449 // docked or freeform stack is visible...except for the home stack if the docked
4450 // stack is minimized and it actually set something and the bounds is different from
4451 // the display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004452 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004453 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004454 && !(mDividerControllerLocked.isHomeStackResizable()
4455 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004456 final int orientation = mHomeStack.getOrientation();
4457 if (orientation != SCREEN_ORIENTATION_UNSET) {
4458 return orientation;
4459 }
4460 }
4461 return SCREEN_ORIENTATION_UNSPECIFIED;
4462 }
4463
4464 final int orientation = super.getOrientation();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004465 boolean isCar = mWmService.mContext.getPackageManager().hasSystemFeature(
Dean Harding3e5a1522017-10-06 09:19:01 -07004466 PackageManager.FEATURE_AUTOMOTIVE);
4467 if (isCar) {
4468 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
4469 // allow anything but the default orientation.
4470 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004471 "Forcing UNSPECIFIED orientation in car for display id=" + mDisplayId
4472 + ". Ignoring " + orientation);
Dean Harding3e5a1522017-10-06 09:19:01 -07004473 return SCREEN_ORIENTATION_UNSPECIFIED;
4474 }
4475
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004476 if (orientation != SCREEN_ORIENTATION_UNSET
4477 && orientation != SCREEN_ORIENTATION_BEHIND) {
4478 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004479 "App is requesting an orientation, return " + orientation
4480 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004481 return orientation;
4482 }
4483
4484 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004485 "No app is requesting an orientation, return " + mLastOrientation
4486 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004487 // The next app has not been requested to be visible, so we keep the current orientation
4488 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08004489 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004490 }
Robert Carrb1579c82017-09-05 14:54:47 -07004491
4492 @Override
4493 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004494 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004495
Robert Carr2f8aa392018-01-31 14:46:51 -08004496 for (int i = 0; i < mChildren.size(); i++) {
4497 final TaskStack s = mChildren.get(i);
4498 s.assignChildLayers(t);
4499 }
4500 }
4501
4502 void assignStackOrdering(SurfaceControl.Transaction t) {
Jorim Jaggibe418292018-03-26 16:14:12 +02004503
Robert Carrf7a7ca82017-12-06 13:17:07 -08004504 final int HOME_STACK_STATE = 0;
4505 final int NORMAL_STACK_STATE = 1;
4506 final int ALWAYS_ON_TOP_STATE = 2;
4507
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004508 int layer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004509 int layerForAnimationLayer = 0;
Jorim Jaggibe418292018-03-26 16:14:12 +02004510 int layerForBoostedAnimationLayer = 0;
Jorim Jaggi391790622018-04-18 15:30:44 +02004511 int layerForHomeAnimationLayer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004512
Robert Carrf7a7ca82017-12-06 13:17:07 -08004513 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
4514 for (int i = 0; i < mChildren.size(); i++) {
4515 final TaskStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08004516 if (state == HOME_STACK_STATE && !s.isActivityTypeHome()) {
4517 continue;
4518 } else if (state == NORMAL_STACK_STATE && (s.isActivityTypeHome()
4519 || s.isAlwaysOnTop())) {
4520 continue;
4521 } else if (state == ALWAYS_ON_TOP_STATE && !s.isAlwaysOnTop()) {
4522 continue;
4523 }
4524 s.assignLayer(t, layer++);
4525 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
4526 t.setLayer(mSplitScreenDividerAnchor, layer++);
4527 }
Winson Chungd41f71d2018-03-16 15:26:07 -07004528 if ((s.isTaskAnimating() || s.isAppAnimating())
4529 && state != ALWAYS_ON_TOP_STATE) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004530 // Ensure the animation layer ends up above the
4531 // highest animating stack and no higher.
4532 layerForAnimationLayer = layer++;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004533 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004534 if (state != ALWAYS_ON_TOP_STATE) {
4535 layerForBoostedAnimationLayer = layer++;
4536 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08004537 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004538 if (state == HOME_STACK_STATE) {
4539 layerForHomeAnimationLayer = layer++;
4540 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004541 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004542 if (mAppAnimationLayer != null) {
4543 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004544 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004545 if (mBoostedAppAnimationLayer != null) {
4546 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
4547 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004548 if (mHomeAppAnimationLayer != null) {
4549 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
4550 }
Robert Carrb1579c82017-09-05 14:54:47 -07004551 }
4552
4553 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02004554 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
4555 switch (animationLayer) {
4556 case ANIMATION_LAYER_BOOSTED:
4557 return mBoostedAppAnimationLayer;
4558 case ANIMATION_LAYER_HOME:
4559 return mHomeAppAnimationLayer;
4560 case ANIMATION_LAYER_STANDARD:
4561 default:
4562 return mAppAnimationLayer;
4563 }
chaviw23ee71c2017-12-18 11:29:41 -08004564 }
4565
Robert Carrf7a7ca82017-12-06 13:17:07 -08004566 SurfaceControl getSplitScreenDividerAnchor() {
4567 return mSplitScreenDividerAnchor;
4568 }
4569
chaviw23ee71c2017-12-18 11:29:41 -08004570 @Override
Riddle Hsu3a4bb612019-01-31 00:02:22 +08004571 void onParentChanged() {
4572 super.onParentChanged();
Robert Carrb1579c82017-09-05 14:54:47 -07004573 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08004574 mAppAnimationLayer = makeChildSurface(null)
4575 .setName("animationLayer")
4576 .build();
Jorim Jaggibe418292018-03-26 16:14:12 +02004577 mBoostedAppAnimationLayer = makeChildSurface(null)
4578 .setName("boostedAnimationLayer")
4579 .build();
Jorim Jaggi391790622018-04-18 15:30:44 +02004580 mHomeAppAnimationLayer = makeChildSurface(null)
4581 .setName("homeAnimationLayer")
4582 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08004583 mSplitScreenDividerAnchor = makeChildSurface(null)
4584 .setName("splitScreenDividerAnchor")
4585 .build();
4586 getPendingTransaction()
4587 .show(mAppAnimationLayer)
Jorim Jaggibe418292018-03-26 16:14:12 +02004588 .show(mBoostedAppAnimationLayer)
Jorim Jaggi391790622018-04-18 15:30:44 +02004589 .show(mHomeAppAnimationLayer)
Robert Carrf7a7ca82017-12-06 13:17:07 -08004590 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08004591 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07004592 } else {
chaviwdd5bde02019-06-07 16:33:50 -07004593 mWmService.mTransactionFactory.make()
4594 .remove(mAppAnimationLayer)
4595 .remove(mBoostedAppAnimationLayer)
4596 .remove(mHomeAppAnimationLayer)
4597 .remove(mSplitScreenDividerAnchor)
4598 .apply();
chaviw23ee71c2017-12-18 11:29:41 -08004599 mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02004600 mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02004601 mHomeAppAnimationLayer = null;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004602 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07004603 }
4604 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004605 }
4606
Robert Carree4d4b92017-11-22 12:21:46 -08004607 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004608 AboveAppWindowContainers(String name, WindowManagerService service) {
4609 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08004610 }
4611
Robert Carrb9506032018-02-13 13:54:00 -08004612 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07004613 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
4614 final SurfaceControl.Builder builder = super.makeChildSurface(child);
4615 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
4616 // To draw above the ColorFade layer during the screen off transition, the
4617 // rounded corner overlays need to be at the root of the surface hierarchy.
4618 // TODO: move the ColorLayer into the display overlay layer such that this is not
4619 // necessary anymore.
4620 builder.setParent(null);
4621 }
4622 return builder;
4623 }
4624
4625 @Override
Robert Carrb9506032018-02-13 13:54:00 -08004626 void assignChildLayers(SurfaceControl.Transaction t) {
4627 assignChildLayers(t, null /* imeContainer */);
4628 }
4629
Robert Carree4d4b92017-11-22 12:21:46 -08004630 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
4631 boolean needAssignIme = imeContainer != null
4632 && imeContainer.getSurfaceControl() != null;
4633 for (int j = 0; j < mChildren.size(); ++j) {
4634 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004635
4636 // See {@link mSplitScreenDividerAnchor}
4637 if (wt.windowType == TYPE_DOCK_DIVIDER) {
4638 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
4639 continue;
4640 }
Vishnu Nair83537a72018-07-19 21:27:48 -07004641 if (wt.mRoundedCornerOverlay) {
4642 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
4643 continue;
4644 }
Robert Carree4d4b92017-11-22 12:21:46 -08004645 wt.assignLayer(t, j);
4646 wt.assignChildLayers(t);
4647
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004648 int layer = mWmService.mPolicy.getWindowLayerFromTypeLw(
Robert Carree4d4b92017-11-22 12:21:46 -08004649 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08004650
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004651 if (needAssignIme && layer >= mWmService.mPolicy.getWindowLayerFromTypeLw(
Robert Carrb8e78fa2017-11-28 13:07:58 -08004652 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004653 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08004654 needAssignIme = false;
4655 }
4656 }
4657 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004658 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08004659 }
4660 }
4661 }
4662
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004663 /**
4664 * Window container class that contains all containers on this display that are not related to
4665 * Apps. E.g. status bar.
4666 */
Robert Carree4d4b92017-11-22 12:21:46 -08004667 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07004668 /**
4669 * Compares two child window tokens returns -1 if the first is lesser than the second in
4670 * terms of z-order and 1 otherwise.
4671 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004672 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07004673 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004674 mWmService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004675 token1.mOwnerCanManageAppTokens)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004676 < mWmService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004677 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004678
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004679 private final Predicate<WindowState> mGetOrientingWindow = w -> {
chaviw15ad49f2019-04-24 15:05:39 -07004680 if (!w.isVisibleLw() || !w.mLegacyPolicyVisibilityAfterAnim) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004681 return false;
4682 }
4683 final int req = w.mAttrs.screenOrientation;
4684 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
4685 || req == SCREEN_ORIENTATION_UNSET) {
4686 return false;
4687 }
4688 return true;
4689 };
4690
Wale Ogunwale3a931692016-11-02 16:49:48 -07004691 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08004692 private final Dimmer mDimmer = new Dimmer(this);
4693 private final Rect mTmpDimBoundsRect = new Rect();
4694
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004695 NonAppWindowContainers(String name, WindowManagerService service) {
4696 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004697 mName = name;
4698 }
4699
4700 void addChild(WindowToken token) {
4701 addChild(token, mWindowComparator);
4702 }
4703
4704 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004705 int getOrientation() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004706 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004707 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004708 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004709
4710 if (win != null) {
4711 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004712 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004713 mLastKeyguardForcedOrientation = req;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004714 if (mWmService.mKeyguardGoingAway) {
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07004715 // Keyguard can't affect the orientation if it is going away...
4716 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
4717 return SCREEN_ORIENTATION_UNSET;
4718 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004719 }
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004720 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req
4721 + " for display id=" + mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08004722 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004723 }
4724
Andrii Kulian8ee72852017-03-10 10:36:45 -08004725 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004726
Jorim Jaggi1c530592018-04-06 15:11:47 +02004727 // Only allow force setting the orientation when all unknown visibilities have been
4728 // resolved, as otherwise we just may be starting another occluding activity.
4729 final boolean isUnoccluding =
lumark588a3e82018-07-20 18:53:54 +08004730 mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
4731 && mUnknownAppVisibilityController.allResolved();
Jorim Jaggi1c530592018-04-06 15:11:47 +02004732 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004733 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004734 }
4735
4736 return SCREEN_ORIENTATION_UNSET;
4737 }
4738
4739 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07004740 String getName() {
4741 return mName;
4742 }
chaviwf29223e2018-01-31 13:23:51 -08004743
4744 @Override
4745 Dimmer getDimmer() {
4746 return mDimmer;
4747 }
4748
4749 @Override
4750 void prepareSurfaces() {
4751 mDimmer.resetDimStates();
4752 super.prepareSurfaces();
4753 getBounds(mTmpDimBoundsRect);
4754
4755 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
4756 scheduleAnimation();
4757 }
4758 }
Robert Carr3b716242016-08-16 16:02:21 -07004759 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01004760
Robert Carrb1579c82017-09-05 14:54:47 -07004761 SurfaceControl.Builder makeSurface(SurfaceSession s) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004762 return mWmService.makeSurfaceBuilder(s)
Robert Carrb1579c82017-09-05 14:54:47 -07004763 .setParent(mWindowingLayer);
4764 }
4765
4766 @Override
4767 SurfaceSession getSession() {
4768 return mSession;
4769 }
4770
4771 @Override
4772 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07004773 SurfaceSession s = child != null ? child.getSession() : getSession();
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +00004774 final SurfaceControl.Builder b = mWmService.makeSurfaceBuilder(s).setContainerLayer();
Robert Carrf59b8dd2017-10-02 18:58:36 -07004775 if (child == null) {
4776 return b;
4777 }
4778
Robert Carree4d4b92017-11-22 12:21:46 -08004779 return b.setName(child.getName())
4780 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004781 }
4782
4783 /**
4784 * The makeSurface variants are for use by the window-container
4785 * hierarchy. makeOverlay here is a function for various non windowing
4786 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
4787 * and other potpourii.
4788 */
4789 SurfaceControl.Builder makeOverlay() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004790 return mWmService.makeSurfaceBuilder(mSession)
Robert Carrb1579c82017-09-05 14:54:47 -07004791 .setParent(mOverlayLayer);
4792 }
4793
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004794 /**
4795 * Reparents the given surface to mOverlayLayer.
4796 */
4797 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
Robert Carr10584fa2019-01-14 15:55:19 -08004798 transaction.reparent(surface, mOverlayLayer);
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004799 }
4800
Robert Carrb1579c82017-09-05 14:54:47 -07004801 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07004802 if (spec.scale != 1.0) {
4803 mMagnificationSpec = spec;
4804 } else {
4805 mMagnificationSpec = null;
4806 }
Jackal Guo818c3f12019-03-08 18:00:39 +08004807 // Re-parent IME's SurfaceControl when MagnificationSpec changed.
4808 updateImeParent();
Robert Carr24be9ab2018-04-30 17:54:53 -07004809
Robert Carrf59b8dd2017-10-02 18:58:36 -07004810 applyMagnificationSpec(getPendingTransaction(), spec);
4811 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07004812 }
4813
Robert Carr24be9ab2018-04-30 17:54:53 -07004814 void reapplyMagnificationSpec() {
4815 if (mMagnificationSpec != null) {
4816 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4817 }
4818 }
4819
Robert Carrb1579c82017-09-05 14:54:47 -07004820 @Override
Riddle Hsu3a4bb612019-01-31 00:02:22 +08004821 void onParentChanged() {
Robert Carrb1579c82017-09-05 14:54:47 -07004822 // Since we are the top of the SurfaceControl hierarchy here
4823 // we create the root surfaces explicitly rather than chaining
Riddle Hsu3a4bb612019-01-31 00:02:22 +08004824 // up as the default implementation in onParentChanged does. So we
Robert Carrb1579c82017-09-05 14:54:47 -07004825 // explicitly do NOT call super here.
4826 }
4827
4828 @Override
4829 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004830
4831 // These are layers as children of "mWindowingLayer"
4832 mBelowAppWindowsContainers.assignLayer(t, 0);
4833 mTaskStackContainers.assignLayer(t, 1);
4834 mAboveAppWindowsContainers.assignLayer(t, 2);
4835
lumarkff0ab692018-11-05 20:32:30 +08004836 final WindowState imeTarget = mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004837 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004838
Robert Carree4d4b92017-11-22 12:21:46 -08004839 // In the case where we have an IME target that is not in split-screen
4840 // mode IME assignment is easy. We just need the IME to go directly above
4841 // the target. This way children of the target will naturally go above the IME
4842 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004843 //
Robert Carree4d4b92017-11-22 12:21:46 -08004844 // In the case of split-screen windowing mode, we need to elevate the IME above the
4845 // docked divider while keeping the app itself below the docked divider, so instead
4846 // we use relative layering of the IME targets child windows, and place the
4847 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004848 //
Robert Carr234b6332018-03-20 13:38:16 -07004849 // In the case the IME target is animating, the animation Z order may be different
4850 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4851 // IME target. In this case we just layer the IME over all transitions by placing it in the
4852 // above applications layer.
4853 //
Robert Carree4d4b92017-11-22 12:21:46 -08004854 // In the case where we have no IME target we assign it where it's base layer would
4855 // place it in the AboveAppWindowContainers.
Tarandeep Singh69ab1042019-07-30 13:30:03 -07004856 //
4857 // Keep IME window in mAboveAppWindowsContainers as long as app's starting window exists
4858 // so it get's layered above the starting window.
4859 if (imeTarget != null
4860 && !(imeTarget.mAppToken != null && imeTarget.mAppToken.hasStartingWindow())
4861 && (!(imeTarget.inSplitScreenWindowingMode() || imeTarget.mToken.isAppAnimating())
4862 && (imeTarget.getSurfaceControl() != null))) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004863 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004864 // TODO: We need to use an extra level on the app surface to ensure
4865 // this is always above SurfaceView but always below attached window.
4866 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004867 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004868 }
4869
4870 // Above we have assigned layers to our children, now we ask them to assign
4871 // layers to their children.
4872 mBelowAppWindowsContainers.assignChildLayers(t);
4873 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004874 mAboveAppWindowsContainers.assignChildLayers(t,
4875 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004876 mImeWindowsContainers.assignChildLayers(t);
4877 }
4878
4879 /**
4880 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4881 * that the IME target is one of the docked applications. We'd like the docked divider to be
4882 * above both of the applications, and we'd like the IME to be above the docked divider.
4883 * However we need child windows of the applications to be above the IME (Text drag handles).
4884 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4885 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
Tiger Huang7c610aa2018-10-27 00:01:01 +08004886 * with {@link WindowState#assignLayer}
Robert Carrb1579c82017-09-05 14:54:47 -07004887 */
4888 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004889 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004890 }
4891
4892 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004893 void prepareSurfaces() {
Jorim Jaggi4981f152019-03-26 18:58:45 +01004894 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "prepareSurfaces");
4895 try {
4896 final ScreenRotationAnimation screenRotationAnimation =
4897 mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Tiger Huanged6794e2019-05-07 20:07:59 +08004898 final Transaction transaction = getPendingTransaction();
Jorim Jaggi4981f152019-03-26 18:58:45 +01004899 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
4900 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
Tiger Huanged6794e2019-05-07 20:07:59 +08004901 transaction.setMatrix(mWindowingLayer,
Jorim Jaggi4981f152019-03-26 18:58:45 +01004902 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
4903 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
Tiger Huanged6794e2019-05-07 20:07:59 +08004904 transaction.setPosition(mWindowingLayer,
Jorim Jaggi4981f152019-03-26 18:58:45 +01004905 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
Tiger Huanged6794e2019-05-07 20:07:59 +08004906 transaction.setAlpha(mWindowingLayer,
Jorim Jaggi4981f152019-03-26 18:58:45 +01004907 screenRotationAnimation.getEnterTransformation().getAlpha());
4908 }
Chavi Weingartenb736e322018-02-23 00:27:54 +00004909
Jorim Jaggi4981f152019-03-26 18:58:45 +01004910 super.prepareSurfaces();
Tiger Huanged6794e2019-05-07 20:07:59 +08004911
4912 // TODO: Once we totally eliminate global transaction we will pass transaction in here
4913 // rather than merging to global.
4914 SurfaceControl.mergeToGlobalTransaction(transaction);
Jorim Jaggi4981f152019-03-26 18:58:45 +01004915 } finally {
4916 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
4917 }
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004918 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004919
Jorim Jaggibe418292018-03-26 16:14:12 +02004920 void assignStackOrdering() {
4921 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004922 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004923
4924 /**
4925 * Increment the deferral count to determine whether to update the IME target.
4926 */
4927 void deferUpdateImeTarget() {
4928 mDeferUpdateImeTargetCount++;
4929 }
4930
4931 /**
4932 * Decrement the deferral count to determine whether to update the IME target. If the count
4933 * reaches 0, a new ime target will get computed.
4934 */
4935 void continueUpdateImeTarget() {
4936 if (mDeferUpdateImeTargetCount == 0) {
4937 return;
4938 }
4939
4940 mDeferUpdateImeTargetCount--;
4941 if (mDeferUpdateImeTargetCount == 0) {
4942 computeImeTarget(true /* updateImeTarget */);
4943 }
4944 }
4945
4946 /**
4947 * @return Whether a new IME target should be computed.
4948 */
4949 private boolean canUpdateImeTarget() {
4950 return mDeferUpdateImeTargetCount == 0;
4951 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004952
4953 InputMonitor getInputMonitor() {
4954 return mInputMonitor;
4955 }
Jorim Jaggif1292892018-09-10 11:58:13 +02004956
4957 /**
4958 * @return Cached value whether we told display manager that we have content.
4959 */
4960 boolean getLastHasContent() {
4961 return mLastHasContent;
4962 }
Arthur Hungbe5ce212018-09-13 18:41:56 +08004963
4964 void registerPointerEventListener(@NonNull PointerEventListener listener) {
Riddle Hsu2588ab02019-02-25 14:23:56 +08004965 mPointerEventDispatcher.registerInputEventListener(listener);
Arthur Hungbe5ce212018-09-13 18:41:56 +08004966 }
4967
4968 void unregisterPointerEventListener(@NonNull PointerEventListener listener) {
Riddle Hsu2588ab02019-02-25 14:23:56 +08004969 mPointerEventDispatcher.unregisterInputEventListener(listener);
Arthur Hungbe5ce212018-09-13 18:41:56 +08004970 }
lumark588a3e82018-07-20 18:53:54 +08004971
4972 void prepareAppTransition(@WindowManager.TransitionType int transit,
Wale Ogunwale3a256e62018-12-06 14:41:18 -08004973 boolean alwaysKeepCurrent) {
4974 prepareAppTransition(transit, alwaysKeepCurrent, 0 /* flags */, false /* forceOverride */);
4975 }
4976
4977 void prepareAppTransition(@WindowManager.TransitionType int transit,
lumark588a3e82018-07-20 18:53:54 +08004978 boolean alwaysKeepCurrent, @WindowManager.TransitionFlags int flags,
4979 boolean forceOverride) {
4980 final boolean prepared = mAppTransition.prepareAppTransitionLocked(
4981 transit, alwaysKeepCurrent, flags, forceOverride);
4982 if (prepared && okToAnimate()) {
4983 mSkipAppTransitionAnimation = false;
4984 }
4985 }
4986
4987 void executeAppTransition() {
4988 if (mAppTransition.isTransitionSet()) {
4989 if (DEBUG_APP_TRANSITIONS) {
4990 Slog.w(TAG_WM, "Execute app transition: " + mAppTransition + ", displayId: "
4991 + mDisplayId + " Callers=" + Debug.getCallers(5));
4992 }
4993 mAppTransition.setReady();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004994 mWmService.mWindowPlacerLocked.requestTraversal();
lumark588a3e82018-07-20 18:53:54 +08004995 }
4996 }
4997
4998 /**
4999 * Update pendingLayoutChanges after app transition has finished.
5000 */
5001 void handleAnimatingStoppedAndTransition() {
5002 int changes = 0;
5003
5004 mAppTransition.setIdle();
5005
5006 for (int i = mNoAnimationNotifyOnTransitionFinished.size() - 1; i >= 0; i--) {
5007 final IBinder token = mNoAnimationNotifyOnTransitionFinished.get(i);
5008 mAppTransition.notifyAppTransitionFinishedLocked(token);
5009 }
5010 mNoAnimationNotifyOnTransitionFinished.clear();
5011
5012 mWallpaperController.hideDeferredWallpapersIfNeeded();
5013
5014 onAppTransitionDone();
5015
5016 changes |= FINISH_LAYOUT_REDO_LAYOUT;
5017 if (DEBUG_WALLPAPER_LIGHT) {
5018 Slog.v(TAG_WM, "Wallpaper layer changed: assigning layers + relayout");
5019 }
5020 computeImeTarget(true /* updateImeTarget */);
wilsonshihc32538e2018-11-07 17:27:34 +08005021 mWallpaperMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08005022 // Since the window list has been rebuilt, focus might have to be recomputed since the
5023 // actual order of windows might have changed again.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08005024 mWmService.mFocusMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08005025
5026 pendingLayoutChanges |= changes;
5027 }
Andrii Kulian15cfb422018-11-07 13:38:49 -08005028
Wale Ogunwale3a256e62018-12-06 14:41:18 -08005029 /** Check if pending app transition is for activity / task launch. */
5030 boolean isNextTransitionForward() {
5031 final int transit = mAppTransition.getAppTransition();
5032 return transit == TRANSIT_ACTIVITY_OPEN
5033 || transit == TRANSIT_TASK_OPEN
5034 || transit == TRANSIT_TASK_TO_FRONT;
5035 }
5036
Andrii Kulian15cfb422018-11-07 13:38:49 -08005037 /**
5038 * @see Display#FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS
5039 */
5040 boolean supportsSystemDecorations() {
Arthur Hung879a8ab2019-03-13 19:51:10 +08005041 return (mWmService.mDisplayWindowSettings.shouldShowSystemDecorsLocked(this)
Andrii Kuliandd989612019-02-21 12:13:28 -08005042 || (mDisplay.getFlags() & FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS) != 0
Chilun891aed22019-05-02 18:08:47 +08005043 || (mWmService.mForceDesktopModeOnExternalDisplays && !isUntrustedVirtualDisplay()))
Arthur Hung879a8ab2019-03-13 19:51:10 +08005044 // VR virtual display will be used to run and render 2D app within a VR experience.
5045 && mDisplayId != mWmService.mVr2dDisplayId;
Andrii Kulian15cfb422018-11-07 13:38:49 -08005046 }
chaviwff2e7d82018-11-02 11:11:27 -07005047
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005048 /**
Chilun891aed22019-05-02 18:08:47 +08005049 * @return {@code true} if the display is non-system created virtual display.
5050 */
5051 boolean isUntrustedVirtualDisplay() {
5052 return mDisplay.getType() == Display.TYPE_VIRTUAL
5053 && mDisplay.getOwnerUid() != Process.SYSTEM_UID;
5054 }
5055
5056 /**
chaviwff2e7d82018-11-02 11:11:27 -07005057 * Re-parent the DisplayContent's top surfaces, {@link #mWindowingLayer} and
Tiger Huangd8ec9382019-04-18 14:35:09 -07005058 * {@link #mOverlayLayer} to the specified SurfaceControl.
chaviwff2e7d82018-11-02 11:11:27 -07005059 *
Tiger Huangd8ec9382019-04-18 14:35:09 -07005060 * @param win The window which owns the SurfaceControl. This indicates the z-order of the
5061 * windows of this display against the windows on the parent display.
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005062 * @param sc The new SurfaceControl, where the DisplayContent's surfaces will be re-parented to.
chaviwff2e7d82018-11-02 11:11:27 -07005063 */
Tiger Huangd8ec9382019-04-18 14:35:09 -07005064 void reparentDisplayContent(WindowState win, SurfaceControl sc) {
5065 mParentWindow = win;
Jackal Guoc43a0a62019-04-23 09:15:14 +08005066 mParentWindow.addEmbeddedDisplayContent(this);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005067 mParentSurfaceControl = sc;
5068 if (mPortalWindowHandle == null) {
5069 mPortalWindowHandle = createPortalWindowHandle(sc.toString());
5070 }
Tiger Huanged6794e2019-05-07 20:07:59 +08005071 getPendingTransaction().setInputWindowInfo(sc, mPortalWindowHandle)
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005072 .reparent(mWindowingLayer, sc).reparent(mOverlayLayer, sc);
5073 }
5074
Tiger Huangd8ec9382019-04-18 14:35:09 -07005075 /**
5076 * Get the window which owns the surface that this DisplayContent is re-parented to.
5077 *
5078 * @return the parent window.
5079 */
5080 WindowState getParentWindow() {
5081 return mParentWindow;
5082 }
5083
5084 /**
5085 * Update the location of this display in the parent window. This enables windows in this
5086 * display to compute the global transformation matrix.
5087 *
5088 * @param win The parent window of this display.
5089 * @param x The x coordinate in the parent window.
5090 * @param y The y coordinate in the parent window.
5091 */
5092 void updateLocation(WindowState win, int x, int y) {
5093 if (mParentWindow != win) {
5094 throw new IllegalArgumentException(
5095 "The given window is not the parent window of this display.");
5096 }
Jackal Guoc43a0a62019-04-23 09:15:14 +08005097 if (!mLocationInParentWindow.equals(x, y)) {
5098 mLocationInParentWindow.set(x, y);
Tiger Huangd8ec9382019-04-18 14:35:09 -07005099 if (mWmService.mAccessibilityController != null) {
5100 mWmService.mAccessibilityController.onSomeWindowResizedOrMovedLocked();
5101 }
Jackal Guoc43a0a62019-04-23 09:15:14 +08005102 notifyLocationInParentDisplayChanged();
Tiger Huangd8ec9382019-04-18 14:35:09 -07005103 }
5104 }
5105
5106 Point getLocationInParentWindow() {
5107 return mLocationInParentWindow;
5108 }
5109
Jackal Guoc43a0a62019-04-23 09:15:14 +08005110 Point getLocationInParentDisplay() {
5111 final Point location = new Point();
5112 if (mParentWindow != null) {
5113 // LocationInParentWindow indicates the offset to (0,0) of window, but what we need is
5114 // the offset to (0,0) of display.
5115 DisplayContent dc = this;
5116 do {
5117 final WindowState displayParent = dc.getParentWindow();
5118 location.x += displayParent.getFrameLw().left
5119 + (dc.getLocationInParentWindow().x * displayParent.mGlobalScale + 0.5f);
5120 location.y += displayParent.getFrameLw().top
5121 + (dc.getLocationInParentWindow().y * displayParent.mGlobalScale + 0.5f);
5122 dc = displayParent.getDisplayContent();
5123 } while (dc != null && dc.getParentWindow() != null);
5124 }
5125 return location;
5126 }
5127
5128 void notifyLocationInParentDisplayChanged() {
5129 forAllWindows(w -> {
5130 w.updateLocationInParentDisplayIfNeeded();
5131 }, false /* traverseTopToBottom */);
5132 }
5133
Tarandeep Singha6f35612019-01-11 19:50:46 -08005134 @VisibleForTesting
5135 SurfaceControl getWindowingLayer() {
5136 return mWindowingLayer;
5137 }
5138
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005139 /**
Adrian Roos4ffc8972019-02-07 20:45:11 +01005140 * Updates the display's system gesture exclusion.
5141 *
5142 * @return true, if the exclusion changed.
5143 */
5144 boolean updateSystemGestureExclusion() {
5145 if (mSystemGestureExclusionListeners.getRegisteredCallbackCount() == 0) {
5146 // No one's interested anyways.
5147 return false;
5148 }
5149
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005150 final Region systemGestureExclusion = Region.obtain();
5151 mSystemGestureExclusionWasRestricted = calculateSystemGestureExclusion(
5152 systemGestureExclusion, mSystemGestureExclusionUnrestricted);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005153 try {
5154 if (mSystemGestureExclusion.equals(systemGestureExclusion)) {
5155 return false;
5156 }
5157 mSystemGestureExclusion.set(systemGestureExclusion);
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005158 final Region unrestrictedOrNull = mSystemGestureExclusionWasRestricted
5159 ? mSystemGestureExclusionUnrestricted : null;
Adrian Roos4ffc8972019-02-07 20:45:11 +01005160 for (int i = mSystemGestureExclusionListeners.beginBroadcast() - 1; i >= 0; --i) {
5161 try {
5162 mSystemGestureExclusionListeners.getBroadcastItem(i)
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005163 .onSystemGestureExclusionChanged(mDisplayId, systemGestureExclusion,
5164 unrestrictedOrNull);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005165 } catch (RemoteException e) {
5166 Slog.e(TAG, "Failed to notify SystemGestureExclusionListener", e);
5167 }
5168 }
5169 mSystemGestureExclusionListeners.finishBroadcast();
5170 return true;
5171 } finally {
5172 systemGestureExclusion.recycle();
5173 }
5174 }
5175
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005176 /**
5177 * Calculates the system gesture exclusion.
5178 *
5179 * @param outExclusion will be set to the gesture exclusion region
5180 * @param outExclusionUnrestricted will be set to the gesture exclusion region without
5181 * any restrictions applied.
5182 * @return whether any restrictions were applied, i.e. outExclusion and outExclusionUnrestricted
5183 * differ.
5184 */
Adrian Roos4ffc8972019-02-07 20:45:11 +01005185 @VisibleForTesting
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005186 boolean calculateSystemGestureExclusion(Region outExclusion, @Nullable
5187 Region outExclusionUnrestricted) {
5188 outExclusion.setEmpty();
5189 if (outExclusionUnrestricted != null) {
5190 outExclusionUnrestricted.setEmpty();
5191 }
Adrian Roos506267d2019-06-18 16:13:53 +02005192 final Region unhandled = Region.obtain();
5193 unhandled.set(0, 0, mDisplayFrames.mDisplayWidth, mDisplayFrames.mDisplayHeight);
5194
5195 final Rect leftEdge = mInsetsStateController.getSourceProvider(TYPE_LEFT_GESTURES)
5196 .getSource().getFrame();
5197 final Rect rightEdge = mInsetsStateController.getSourceProvider(TYPE_RIGHT_GESTURES)
5198 .getSource().getFrame();
5199
Adrian Roos4ffc8972019-02-07 20:45:11 +01005200 final Region touchableRegion = Region.obtain();
5201 final Region local = Region.obtain();
Adrian Roos506267d2019-06-18 16:13:53 +02005202 final int[] remainingLeftRight =
5203 {mSystemGestureExclusionLimit, mSystemGestureExclusionLimit};
Adrian Roos4ffc8972019-02-07 20:45:11 +01005204
Adrian Roosb1063792019-06-28 12:10:51 +02005205 // Traverse all windows top down to assemble the gesture exclusion rects.
Adrian Roos4ffc8972019-02-07 20:45:11 +01005206 // For each window, we only take the rects that fall within its touchable region.
5207 forAllWindows(w -> {
5208 if (w.cantReceiveTouchInput() || !w.isVisible()
Adrian Roos506267d2019-06-18 16:13:53 +02005209 || (w.getAttrs().flags & FLAG_NOT_TOUCHABLE) != 0
5210 || unhandled.isEmpty()) {
Adrian Roos4ffc8972019-02-07 20:45:11 +01005211 return;
5212 }
Adrian Roos4ffc8972019-02-07 20:45:11 +01005213
Adrian Roos506267d2019-06-18 16:13:53 +02005214 // Get the touchable region of the window, and intersect with where the screen is still
5215 // touchable, i.e. touchable regions on top are not covering it yet.
Adrian Roosb1063792019-06-28 12:10:51 +02005216 w.getEffectiveTouchableRegion(touchableRegion);
Adrian Roos506267d2019-06-18 16:13:53 +02005217 touchableRegion.op(unhandled, Op.INTERSECT);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005218
Adrian Roos019a52b2019-07-02 16:47:44 +02005219 if (w.isImplicitlyExcludingAllSystemGestures()) {
5220 local.set(touchableRegion);
5221 } else {
5222 rectListToRegion(w.getSystemGestureExclusion(), local);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005223
Adrian Roos019a52b2019-07-02 16:47:44 +02005224 // Transform to display coordinates
5225 local.scale(w.mGlobalScale);
5226 final Rect frame = w.getWindowFrames().mFrame;
5227 local.translate(frame.left, frame.top);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005228
Adrian Roos019a52b2019-07-02 16:47:44 +02005229 // A window can only exclude system gestures where it is actually touchable
5230 local.op(touchableRegion, Op.INTERSECT);
5231 }
Adrian Roos4ffc8972019-02-07 20:45:11 +01005232
Adrian Roos506267d2019-06-18 16:13:53 +02005233 // Apply restriction if necessary.
5234 if (needsGestureExclusionRestrictions(w, mLastDispatchedSystemUiVisibility)) {
5235
5236 // Processes the region along the left edge.
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005237 remainingLeftRight[0] = addToGlobalAndConsumeLimit(local, outExclusion, leftEdge,
5238 remainingLeftRight[0], w, EXCLUSION_LEFT);
Adrian Roos506267d2019-06-18 16:13:53 +02005239
5240 // Processes the region along the right edge.
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005241 remainingLeftRight[1] = addToGlobalAndConsumeLimit(local, outExclusion, rightEdge,
5242 remainingLeftRight[1], w, EXCLUSION_RIGHT);
Adrian Roos506267d2019-06-18 16:13:53 +02005243
5244 // Adds the middle (unrestricted area)
5245 final Region middle = Region.obtain(local);
5246 middle.op(leftEdge, Op.DIFFERENCE);
5247 middle.op(rightEdge, Op.DIFFERENCE);
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005248 outExclusion.op(middle, Op.UNION);
Adrian Roos506267d2019-06-18 16:13:53 +02005249 middle.recycle();
5250 } else {
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005251 boolean loggable = needsGestureExclusionRestrictions(w, 0 /* lastSysUiVis */);
5252 if (loggable) {
5253 addToGlobalAndConsumeLimit(local, outExclusion, leftEdge,
5254 Integer.MAX_VALUE, w, EXCLUSION_LEFT);
5255 addToGlobalAndConsumeLimit(local, outExclusion, rightEdge,
5256 Integer.MAX_VALUE, w, EXCLUSION_RIGHT);
5257 }
5258 outExclusion.op(local, Op.UNION);
5259 }
5260 if (outExclusionUnrestricted != null) {
5261 outExclusionUnrestricted.op(local, Op.UNION);
Adrian Roos506267d2019-06-18 16:13:53 +02005262 }
5263 unhandled.op(touchableRegion, Op.DIFFERENCE);
5264 }, true /* topToBottom */);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005265 local.recycle();
5266 touchableRegion.recycle();
Adrian Roos506267d2019-06-18 16:13:53 +02005267 unhandled.recycle();
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005268 return remainingLeftRight[0] < mSystemGestureExclusionLimit
5269 || remainingLeftRight[1] < mSystemGestureExclusionLimit;
Adrian Roos4ffc8972019-02-07 20:45:11 +01005270 }
5271
Adrian Roos506267d2019-06-18 16:13:53 +02005272 /**
5273 * @return Whether gesture exclusion area should be restricted from the window depending on the
5274 * current SystemUI visibility flags.
5275 */
5276 private static boolean needsGestureExclusionRestrictions(WindowState win, int sysUiVisibility) {
5277 final int type = win.mAttrs.type;
5278 final int stickyHideNavFlags =
5279 SYSTEM_UI_FLAG_HIDE_NAVIGATION | SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
5280 final boolean stickyHideNav =
5281 (sysUiVisibility & stickyHideNavFlags) == stickyHideNavFlags;
5282 return !stickyHideNav && type != TYPE_INPUT_METHOD && type != TYPE_STATUS_BAR
5283 && win.getActivityType() != ACTIVITY_TYPE_HOME;
5284 }
5285
5286 /**
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005287 * @return Whether gesture exclusion area should be logged for the given window
5288 */
5289 static boolean logsGestureExclusionRestrictions(WindowState win) {
5290 if (win.mWmService.mSystemGestureExclusionLogDebounceTimeoutMillis <= 0) {
5291 return false;
5292 }
5293 final WindowManager.LayoutParams attrs = win.getAttrs();
5294 final int type = attrs.type;
5295 return type != TYPE_WALLPAPER
5296 && type != TYPE_APPLICATION_STARTING
5297 && type != TYPE_NAVIGATION_BAR
5298 && (attrs.flags & FLAG_NOT_TOUCHABLE) == 0
5299 && needsGestureExclusionRestrictions(win, 0 /* sysUiVisibility */)
5300 && win.getDisplayContent().mDisplayPolicy.hasSideGestures();
5301 }
5302
5303 /**
Adrian Roos506267d2019-06-18 16:13:53 +02005304 * Adds a local gesture exclusion area to the global area while applying a limit per edge.
5305 *
5306 * @param local The gesture exclusion area to add.
5307 * @param global The destination.
5308 * @param edge Only processes the part in that region.
5309 * @param limit How much limit in pixels we have.
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005310 * @param win The WindowState that is being processed
5311 * @param side The side that is being processed, either {@link WindowState#EXCLUSION_LEFT} or
5312 * {@link WindowState#EXCLUSION_RIGHT}
5313 * @return How much of the limit is remaining.
Adrian Roos506267d2019-06-18 16:13:53 +02005314 */
5315 private static int addToGlobalAndConsumeLimit(Region local, Region global, Rect edge,
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005316 int limit, WindowState win, int side) {
Adrian Roos506267d2019-06-18 16:13:53 +02005317 final Region r = Region.obtain(local);
5318 r.op(edge, Op.INTERSECT);
5319
5320 final int[] remaining = {limit};
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005321 final int[] requestedExclusion = {0};
Adrian Roos2351d5f2019-07-03 15:35:07 +02005322 forEachRectReverse(r, rect -> {
Adrian Roos506267d2019-06-18 16:13:53 +02005323 if (remaining[0] <= 0) {
5324 return;
5325 }
5326 final int height = rect.height();
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005327 requestedExclusion[0] += height;
Adrian Roos506267d2019-06-18 16:13:53 +02005328 if (height > remaining[0]) {
Adrian Roos2351d5f2019-07-03 15:35:07 +02005329 rect.top = rect.bottom - remaining[0];
Adrian Roos506267d2019-06-18 16:13:53 +02005330 }
5331 remaining[0] -= height;
5332 global.op(rect, Op.UNION);
5333 });
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005334
5335 final int grantedExclusion = limit - remaining[0];
5336 win.setLastExclusionHeights(side, requestedExclusion[0], grantedExclusion);
5337
Adrian Roos506267d2019-06-18 16:13:53 +02005338 r.recycle();
5339 return remaining[0];
5340 }
5341
Adrian Roos4ffc8972019-02-07 20:45:11 +01005342 void registerSystemGestureExclusionListener(ISystemGestureExclusionListener listener) {
5343 mSystemGestureExclusionListeners.register(listener);
5344 final boolean changed;
5345 if (mSystemGestureExclusionListeners.getRegisteredCallbackCount() == 1) {
5346 changed = updateSystemGestureExclusion();
5347 } else {
5348 changed = false;
5349 }
5350
5351 if (!changed) {
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005352 final Region unrestrictedOrNull = mSystemGestureExclusionWasRestricted
5353 ? mSystemGestureExclusionUnrestricted : null;
Adrian Roos4ffc8972019-02-07 20:45:11 +01005354 // If updateSystemGestureExclusion changed the exclusion, it will already have
5355 // notified the listener. Otherwise, we'll do it here.
5356 try {
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005357 listener.onSystemGestureExclusionChanged(mDisplayId, mSystemGestureExclusion,
5358 unrestrictedOrNull);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005359 } catch (RemoteException e) {
5360 Slog.e(TAG, "Failed to notify SystemGestureExclusionListener during register", e);
5361 }
5362 }
5363 }
5364
5365 void unregisterSystemGestureExclusionListener(ISystemGestureExclusionListener listener) {
5366 mSystemGestureExclusionListeners.unregister(listener);
5367 }
5368
5369 /**
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005370 * Create a portal window handle for input. This window transports any touch to the display
5371 * indicated by {@link InputWindowHandle#portalToDisplayId} if the touch hits this window.
5372 *
5373 * @param name The name of the portal window handle.
5374 * @return the new portal window handle.
5375 */
5376 private InputWindowHandle createPortalWindowHandle(String name) {
5377 // Let surface flinger to set the display ID of this input window handle because we don't
5378 // know which display the parent surface control is on.
5379 final InputWindowHandle portalWindowHandle = new InputWindowHandle(
5380 null /* inputApplicationHandle */, null /* clientWindow */, INVALID_DISPLAY);
5381 portalWindowHandle.name = name;
5382 portalWindowHandle.token = new Binder();
5383 portalWindowHandle.layoutParamsFlags =
5384 FLAG_SPLIT_TOUCH | FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL;
5385 getBounds(mTmpBounds);
5386 portalWindowHandle.touchableRegion.set(mTmpBounds);
5387 portalWindowHandle.scaleFactor = 1f;
5388 portalWindowHandle.ownerPid = Process.myPid();
5389 portalWindowHandle.ownerUid = Process.myUid();
5390 portalWindowHandle.portalToDisplayId = mDisplayId;
5391 return portalWindowHandle;
chaviwff2e7d82018-11-02 11:11:27 -07005392 }
Issei Suzukia5dbf522019-02-01 17:58:15 +01005393
5394 /**
5395 * @see IWindowManager#setForwardedInsets
5396 */
5397 public void setForwardedInsets(Insets insets) {
5398 if (insets == null) {
5399 insets = Insets.NONE;
5400 }
5401 if (mDisplayPolicy.getForwardedInsets().equals(insets)) {
5402 return;
5403 }
5404 mDisplayPolicy.setForwardedInsets(insets);
5405 setLayoutNeeded();
5406 mWmService.mWindowPlacerLocked.requestTraversal();
5407 }
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05005408
5409 protected MetricsLogger getMetricsLogger() {
5410 if (mMetricsLogger == null) {
5411 mMetricsLogger = new MetricsLogger();
5412 }
5413 return mMetricsLogger;
5414 }
Craig Mautner59c00972012-07-30 12:10:24 -07005415}