blob: 3c29e5d84becdc0bde18c2a89fb99698ea0cce35 [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;
Wale Ogunwale494009b82016-10-21 09:01:38 -070070import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070071import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
72import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070073import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070074import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale3a256e62018-12-06 14:41:18 -080075import static android.view.WindowManager.TRANSIT_ACTIVITY_OPEN;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080076import static android.view.WindowManager.TRANSIT_KEYGUARD_UNOCCLUDE;
Wale Ogunwale3a256e62018-12-06 14:41:18 -080077import static android.view.WindowManager.TRANSIT_TASK_OPEN;
78import static android.view.WindowManager.TRANSIT_TASK_TO_FRONT;
Tiger Huang51c5a1d2018-12-11 20:24:51 +080079
Adrian Roose99bc052017-11-20 17:55:31 +010080import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
81import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
82import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
83import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080084import static com.android.server.wm.DisplayContentProto.ABOVE_APP_WINDOWS;
lumark588a3e82018-07-20 18:53:54 +080085import static com.android.server.wm.DisplayContentProto.APP_TRANSITION;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080086import static com.android.server.wm.DisplayContentProto.BELOW_APP_WINDOWS;
Louis Changa9350fe2019-04-25 17:14:20 +080087import static com.android.server.wm.DisplayContentProto.CHANGING_APPS;
88import static com.android.server.wm.DisplayContentProto.CLOSING_APPS;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080089import static com.android.server.wm.DisplayContentProto.DISPLAY_FRAMES;
90import static com.android.server.wm.DisplayContentProto.DISPLAY_INFO;
91import static com.android.server.wm.DisplayContentProto.DOCKED_STACK_DIVIDER_CONTROLLER;
92import static com.android.server.wm.DisplayContentProto.DPI;
Tiger Huang1e5b10a2018-07-30 20:19:51 +080093import static com.android.server.wm.DisplayContentProto.FOCUSED_APP;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080094import static com.android.server.wm.DisplayContentProto.ID;
95import static com.android.server.wm.DisplayContentProto.IME_WINDOWS;
Louis Changa9350fe2019-04-25 17:14:20 +080096import static com.android.server.wm.DisplayContentProto.OPENING_APPS;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080097import static com.android.server.wm.DisplayContentProto.PINNED_STACK_CONTROLLER;
98import static com.android.server.wm.DisplayContentProto.ROTATION;
99import static com.android.server.wm.DisplayContentProto.SCREEN_ROTATION_ANIMATION;
100import static com.android.server.wm.DisplayContentProto.STACKS;
101import static com.android.server.wm.DisplayContentProto.WINDOW_CONTAINER;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800102import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
lumark588a3e82018-07-20 18:53:54 +0800103import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_APP_TRANSITIONS;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700104import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -0700105import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -0700106import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
107import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700108import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700109import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700110import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +0200111import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700112import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700113import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -0700114import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800115import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700116import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700117import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700118import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700119import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800120import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700121import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Tiger Huang51c5a1d2018-12-11 20:24:51 +0800122import static com.android.server.wm.WindowManagerService.H.REPORT_FOCUS_CHANGE;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800123import static com.android.server.wm.WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800124import static com.android.server.wm.WindowManagerService.H.REPORT_LOSING_FOCUS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700125import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
126import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700127import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700128import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700129import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
130import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800131import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_PLACING_SURFACES;
132import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_REMOVING_FOCUS;
133import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_ASSIGN_LAYERS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700134import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700135import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700136import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700137import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700138import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700139import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700140import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700141import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700142import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Adrian Roos506267d2019-06-18 16:13:53 +0200143import static com.android.server.wm.utils.RegionUtils.forEachRect;
Adrian Roos4ffc8972019-02-07 20:45:11 +0100144import static com.android.server.wm.utils.RegionUtils.rectListToRegion;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700145
lumark588a3e82018-07-20 18:53:54 +0800146import android.animation.AnimationHandler;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700147import android.annotation.CallSuper;
Riddle Hsuf53da812018-08-15 22:00:27 +0800148import android.annotation.IntDef;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700149import android.annotation.NonNull;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200150import android.annotation.Nullable;
Dean Harding3e5a1522017-10-06 09:19:01 -0700151import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700152import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700153import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700154import android.graphics.Bitmap;
Issei Suzukia5dbf522019-02-01 17:58:15 +0100155import android.graphics.Insets;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700156import android.graphics.Matrix;
Tiger Huangd8ec9382019-04-18 14:35:09 -0700157import android.graphics.Point;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800158import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700159import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700160import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100161import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700162import android.hardware.display.DisplayManagerInternal;
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500163import android.metrics.LogMaker;
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800164import android.os.Binder;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700165import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700166import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700167import android.os.IBinder;
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800168import android.os.Process;
Adrian Roos4ffc8972019-02-07 20:45:11 +0100169import android.os.RemoteCallbackList;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700170import android.os.RemoteException;
171import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100172import android.os.Trace;
Riddle Hsuf53da812018-08-15 22:00:27 +0800173import android.os.UserHandle;
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800174import android.util.ArraySet;
Chong Zhang8e89b312015-09-09 15:09:30 -0700175import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700176import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700177import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700178import android.view.Display;
Adrian Roos1cf585052018-01-03 18:43:27 +0100179import android.view.DisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700180import android.view.DisplayInfo;
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800181import android.view.Gravity;
Adrian Roos4ffc8972019-02-07 20:45:11 +0100182import android.view.ISystemGestureExclusionListener;
Arthur Hungbe5ce212018-09-13 18:41:56 +0800183import android.view.InputChannel;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700184import android.view.InputDevice;
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800185import android.view.InputWindowHandle;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200186import android.view.InsetsState.InternalInsetType;
Robert Carrb1579c82017-09-05 14:54:47 -0700187import android.view.MagnificationSpec;
Evan Rosky966759f2019-01-15 10:33:58 -0800188import android.view.RemoteAnimationDefinition;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700189import android.view.Surface;
190import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100191import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700192import android.view.SurfaceSession;
Tiger Huang7c610aa2018-10-27 00:01:01 +0800193import android.view.View;
lumark588a3e82018-07-20 18:53:54 +0800194import android.view.WindowManager;
Evan Rosky39b6f232018-10-30 18:35:41 -0700195import android.view.WindowManagerPolicyConstants.PointerEventListener;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700196
Bryce Lee48f4b572017-04-10 10:54:15 -0700197import com.android.internal.annotations.VisibleForTesting;
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500198import com.android.internal.logging.MetricsLogger;
199import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800200import com.android.internal.util.ToBooleanFunction;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200201import com.android.internal.util.function.TriConsumer;
Winson Chungc5fe7ff2019-02-19 14:49:25 -0800202import com.android.internal.util.function.pooled.PooledConsumer;
203import com.android.internal.util.function.pooled.PooledLambda;
Winson Chung6ba767a2018-12-13 18:20:11 -0800204import com.android.server.AnimationThread;
Adrian Roose99bc052017-11-20 17:55:31 +0100205import com.android.server.policy.WindowManagerPolicy;
Issei Suzuki43190bd2018-08-20 17:28:41 +0200206import com.android.server.wm.utils.DisplayRotationUtil;
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100207import com.android.server.wm.utils.RotationCache;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100208import com.android.server.wm.utils.WmDisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700209
210import java.io.PrintWriter;
Riddle Hsuf53da812018-08-15 22:00:27 +0800211import java.lang.annotation.Retention;
212import java.lang.annotation.RetentionPolicy;
Craig Mautner59c00972012-07-30 12:10:24 -0700213import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700214import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700215import java.util.HashMap;
216import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700217import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700218import java.util.List;
Adrian Roos1cf585052018-01-03 18:43:27 +0100219import java.util.Objects;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800220import java.util.function.Consumer;
221import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700222
Craig Mautner59c00972012-07-30 12:10:24 -0700223/**
224 * Utility class for keeping track of the WindowStates and other pertinent contents of a
225 * particular Display.
Craig Mautner59c00972012-07-30 12:10:24 -0700226 */
Riddle Hsuad256a12018-07-18 16:11:30 +0800227class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer>
228 implements WindowManagerPolicy.DisplayContentInfo {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700229 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700230
Riddle Hsuf53da812018-08-15 22:00:27 +0800231 /** The default scaling mode that scales content automatically. */
232 static final int FORCE_SCALING_MODE_AUTO = 0;
233 /** For {@link #setForcedScalingMode} to apply flag {@link Display#FLAG_SCALING_DISABLED}. */
234 static final int FORCE_SCALING_MODE_DISABLED = 1;
235
236 @IntDef(prefix = { "FORCE_SCALING_MODE_" }, value = {
237 FORCE_SCALING_MODE_AUTO,
238 FORCE_SCALING_MODE_DISABLED
239 })
240 @Retention(RetentionPolicy.SOURCE)
241 @interface ForceScalingMode {}
242
Craig Mautner59c00972012-07-30 12:10:24 -0700243 /** Unique identifier of this stack. */
244 private final int mDisplayId;
245
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800246 // TODO: Remove once unification is complete.
247 ActivityDisplay mAcitvityDisplay;
248
Wale Ogunwale3a931692016-11-02 16:49:48 -0700249 /** The containers below are the only child containers the display can have. */
250 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800251 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700252 // Contains all non-app window containers that should be displayed above the app containers
253 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800254 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800255 new AboveAppWindowContainers("mAboveAppWindowsContainers", mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700256 // Contains all non-app window containers that should be displayed below the app containers
257 // (e.g. Wallpaper).
258 private final NonAppWindowContainers mBelowAppWindowsContainers =
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800259 new NonAppWindowContainers("mBelowAppWindowsContainers", mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700260 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
261 // 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 -0800262 // window containers together and move them in-sync if/when needed. We use a subclass of
263 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
Eugene Susla25a24952019-04-08 13:57:44 -0700264 private final NonAppWindowContainers mImeWindowsContainers =
265 new NonAppWindowContainers("mImeWindowsContainers", mWmService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700266
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000267 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800268 private WindowState mTmpWindow2;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800269 private boolean mTmpRecoveringMemory;
270 private boolean mUpdateImeTarget;
271 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700272 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700273
lumark588a3e82018-07-20 18:53:54 +0800274 final AppTransition mAppTransition;
275 final AppTransitionController mAppTransitionController;
276 boolean mSkipAppTransitionAnimation = false;
277
278 final ArraySet<AppWindowToken> mOpeningApps = new ArraySet<>();
279 final ArraySet<AppWindowToken> mClosingApps = new ArraySet<>();
Evan Rosky2289ba12018-11-19 18:28:18 -0800280 final ArraySet<AppWindowToken> mChangingApps = new ArraySet<>();
lumark588a3e82018-07-20 18:53:54 +0800281 final UnknownAppVisibilityController mUnknownAppVisibilityController;
282 BoundsAnimationController mBoundsAnimationController;
283
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500284 private MetricsLogger mMetricsLogger;
285
lumark588a3e82018-07-20 18:53:54 +0800286 /**
287 * List of clients without a transtiton animation that we notify once we are done
288 * transitioning since they won't be notified through the app window animator.
289 */
290 final List<IBinder> mNoAnimationNotifyOnTransitionFinished = new ArrayList<>();
291
Wale Ogunwale02319a62016-09-26 15:21:22 -0700292 // Mapping from a token IBinder to a WindowToken object on this display.
293 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
294
Andrii Kuliancd097992017-03-23 18:31:59 -0700295 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700296 int mInitialDisplayWidth = 0;
297 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700298 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700299
Adrian Roos1cf585052018-01-03 18:43:27 +0100300 DisplayCutout mInitialDisplayCutout;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100301 private final RotationCache<DisplayCutout, WmDisplayCutout> mDisplayCutoutCache
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100302 = new RotationCache<>(this::calculateDisplayCutoutForRotationUncached);
Adrian Roos1cf585052018-01-03 18:43:27 +0100303
Andrii Kuliancd097992017-03-23 18:31:59 -0700304 /**
305 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
306 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
307 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
308 */
Craig Mautner59c00972012-07-30 12:10:24 -0700309 int mBaseDisplayWidth = 0;
310 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700311 /**
312 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
313 * but can be set from Settings or via shell command "adb shell wm density".
314 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
315 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700316 int mBaseDisplayDensity = 0;
Riddle Hsuf53da812018-08-15 22:00:27 +0800317
318 /**
319 * Whether to disable display scaling. This can be set via shell command "adb shell wm scaling".
320 * @see WindowManagerService#setForcedDisplayScalingMode(int, int)
321 */
Jeff Brownd46747a2015-04-15 19:02:36 -0700322 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700323 private final DisplayInfo mDisplayInfo = new DisplayInfo();
324 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700325 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800326 private final DisplayPolicy mDisplayPolicy;
327 private DisplayRotation mDisplayRotation;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000328 DisplayFrames mDisplayFrames;
329
Adrian Roos4ffc8972019-02-07 20:45:11 +0100330 private final RemoteCallbackList<ISystemGestureExclusionListener>
331 mSystemGestureExclusionListeners = new RemoteCallbackList<>();
332 private final Region mSystemGestureExclusion = new Region();
Adrian Roos506267d2019-06-18 16:13:53 +0200333 private int mSystemGestureExclusionLimit;
Adrian Roos4ffc8972019-02-07 20:45:11 +0100334
Andrii Kulian06d07d62017-03-14 11:11:47 -0700335 /**
336 * For default display it contains real metrics, empty for others.
337 * @see WindowManagerService#createWatermarkInTransaction()
338 */
339 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800340
341 /** @see #computeCompatSmallestWidth(boolean, int, int, int, DisplayCutout) */
Andrii Kulian06d07d62017-03-14 11:11:47 -0700342 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700343
Andrii Kulian06d07d62017-03-14 11:11:47 -0700344 /**
345 * Compat metrics computed based on {@link #mDisplayMetrics}.
346 * @see #updateDisplayAndOrientation(int)
347 */
348 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
349
350 /** The desired scaling factor for compatible apps. */
351 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700352
Andrii Kulian8ee72852017-03-10 10:36:45 -0800353 /**
354 * Current rotation of the display.
355 * Constants as per {@link android.view.Surface.Rotation}.
356 *
Robert Carrae606b42018-02-15 15:36:23 -0800357 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800358 */
359 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700360
Andrii Kulian8ee72852017-03-10 10:36:45 -0800361 /**
362 * Last applied orientation of the display.
363 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
364 *
Riddle Hsu4e611772018-10-31 18:58:28 +0800365 * @see #updateOrientationFromAppTokens()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800366 */
367 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700368
Andrii Kulian8ee72852017-03-10 10:36:45 -0800369 /**
Andrii Kulian8ee72852017-03-10 10:36:45 -0800370 * Orientation forced by some window. If there is no visible window that specifies orientation
371 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
372 *
373 * @see NonAppWindowContainers#getOrientation()
374 */
375 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700376
Andrii Kulian8ee72852017-03-10 10:36:45 -0800377 /**
378 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
379 * occluded.
380 *
381 * @see NonAppWindowContainers#getOrientation()
382 */
383 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
384
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700385 /**
Tiger Huang86e6d072019-05-02 20:23:47 +0800386 * The maximum aspect ratio (longerSide/shorterSide) that is treated as close-to-square. The
387 * orientation requests from apps would be ignored if the display is close-to-square.
388 */
389 @VisibleForTesting
390 final float mCloseToSquareMaxAspectRatio;
391
392 /**
393 * If this is true, we would not rotate the display for apps. The rotation would be either the
394 * sensor rotation or the user rotation, controlled by
395 * {@link WindowManagerPolicy.UserRotationMode}.
396 */
397 private boolean mIgnoreRotationForApps;
398
399 /**
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700400 * Keep track of wallpaper visibility to notify changes.
401 */
402 private boolean mLastWallpaperVisible = false;
403
Andrii Kulian06d07d62017-03-14 11:11:47 -0700404 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700405
Craig Mautner39834192012-09-02 07:47:24 -0700406 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700407 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700408 int pendingLayoutChanges;
Riddle Hsu654a6f92018-07-13 22:59:36 +0800409 int mDeferredRotationPauseCount;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800410
Vishnu Nairba183352018-11-21 11:16:49 -0800411 /**
412 * Used to gate application window layout until we have sent the complete configuration.
413 * TODO: There are still scenarios where we may be out of sync with the client. Ideally
414 * we want to replace this flag with a mechanism that will confirm the configuration
415 * applied by the client is the one expected by the system server.
416 */
417 boolean mWaitingForConfig;
418
Andrii Kulian839def92016-11-02 10:58:58 -0700419 // TODO(multi-display): remove some of the usages.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800420 @VisibleForTesting
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800421 boolean isDefaultDisplay;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800422
Andrii Kulianf0379de2018-03-14 16:24:07 -0700423 /**
424 * Flag indicating whether WindowManager should override info for this display in
425 * DisplayManager.
426 */
427 boolean mShouldOverrideDisplayConfiguration = true;
Craig Mautner39834192012-09-02 07:47:24 -0700428
Craig Mautnerdc548482014-02-05 13:35:24 -0800429 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700430 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800431
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700432 /** Detect user tapping outside of current focused task bounds .*/
Riddle Hsu2588ab02019-02-25 14:23:56 +0800433 @VisibleForTesting
434 final TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700435
Craig Mautner6601b7b2013-04-29 10:29:11 -0700436 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700437 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700438
Craig Mautner6601b7b2013-04-29 10:29:11 -0700439 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800440 private final Rect mTmpRect = new Rect();
441 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700442 private final RectF mTmpRectF = new RectF();
443 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800444 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700445
Bryce Leef3c6a472017-11-14 14:53:06 -0800446 /** Used for handing back size of display */
447 private final Rect mTmpBounds = new Rect();
448
Garfield Tan90b04282018-12-11 14:04:42 -0800449 private final Configuration mTmpConfiguration = new Configuration();
450
Craig Mautner95da1082014-02-24 17:54:35 -0800451 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700452 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800453
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700454 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700455 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700456
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800457 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800458 /** A collection of windows that provide tap exclude regions inside of them. */
459 final ArraySet<WindowState> mTapExcludeProvidingWindows = new ArraySet<>();
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800460
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000461 private boolean mHaveBootMsg = false;
462 private boolean mHaveApp = false;
463 private boolean mHaveWallpaper = false;
464 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700465
466 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
467
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700468 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
469 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000470 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
471 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700472
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700473 // True if this display is in the process of being removed. Used to determine if the removal of
474 // the display's direct children should be allowed.
475 private boolean mRemovingDisplay = false;
476
Bryce Leed1871262017-06-12 14:12:29 -0700477 // {@code false} if this display is in the processing of being created.
478 private boolean mDisplayReady = false;
479
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800480 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700481
wilsonshihc32538e2018-11-07 17:27:34 +0800482 boolean mWallpaperMayChange = false;
483
Robert Carrb1579c82017-09-05 14:54:47 -0700484 private final SurfaceSession mSession = new SurfaceSession();
485
486 /**
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800487 * Window that is currently interacting with the user. This window is responsible for receiving
488 * key events and pointer events from the user.
489 */
490 WindowState mCurrentFocus = null;
491
492 /**
493 * The last focused window that we've notified the client that the focus is changed.
494 */
495 WindowState mLastFocus = null;
496
497 /**
498 * Windows that have lost input focus and are waiting for the new focus window to be displayed
499 * before they are told about this.
500 */
501 ArrayList<WindowState> mLosingFocus = new ArrayList<>();
502
503 /**
504 * The foreground app of this display. Windows below this app cannot be the focused window. If
505 * the user taps on the area outside of the task of the focused app, we will notify AM about the
506 * new task the user wants to interact with.
507 */
508 AppWindowToken mFocusedApp = null;
509
510 /** Windows added since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
511 final ArrayList<WindowState> mWinAddedSinceNullFocus = new ArrayList<>();
512
513 /** Windows removed since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
514 final ArrayList<WindowState> mWinRemovedSinceNullFocus = new ArrayList<>();
515
516 /**
Robert Carrb1579c82017-09-05 14:54:47 -0700517 * We organize all top-level Surfaces in to the following layers.
518 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800519 * and omitted from Screen-Magnification, for example the strict mode flash or
520 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700521 * {@link #mWindowingLayer} contains everything else.
522 */
523 private SurfaceControl mOverlayLayer;
524
525 /**
526 * See {@link #mOverlayLayer}
527 */
528 private SurfaceControl mWindowingLayer;
529
530 /**
Adrian Roos5251b1d2018-03-23 18:57:43 +0100531 * Sequence number for the current layout pass.
532 */
533 int mLayoutSeq = 0;
534
Chavi Weingarten3a748552018-05-14 17:32:42 +0000535 /**
536 * Specifies the count to determine whether to defer updating the IME target until ready.
537 */
538 private int mDeferUpdateImeTargetCount;
539
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100540 /** Temporary float array to retrieve 3x3 matrix values. */
541 private final float[] mTmpFloats = new float[9];
542
Robert Carr24be9ab2018-04-30 17:54:53 -0700543 private MagnificationSpec mMagnificationSpec;
544
Arthur Hung95b38a92018-07-20 18:56:12 +0800545 private InputMonitor mInputMonitor;
546
Jorim Jaggif1292892018-09-10 11:58:13 +0200547 /** Caches the value whether told display manager that we have content. */
548 private boolean mLastHasContent;
549
Issei Suzuki43190bd2018-08-20 17:28:41 +0200550 private DisplayRotationUtil mRotationUtil = new DisplayRotationUtil();
551
lumark90120a82018-08-15 00:33:03 +0800552 /**
553 * The input method window for this display.
554 */
555 WindowState mInputMethodWindow;
556
lumarkff0ab692018-11-05 20:32:30 +0800557 /**
558 * This just indicates the window the input method is on top of, not
559 * necessarily the window its input is going to.
560 */
561 WindowState mInputMethodTarget;
562
563 /** If true hold off on modifying the animation layer of mInputMethodTarget */
564 boolean mInputMethodTargetWaitingAnim;
565
Arthur Hungbe5ce212018-09-13 18:41:56 +0800566 private final PointerEventDispatcher mPointerEventDispatcher;
567
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200568 private final InsetsStateController mInsetsStateController;
569
Tiger Huangd8ec9382019-04-18 14:35:09 -0700570 /** @see #getParentWindow() */
571 private WindowState mParentWindow;
572
573 private Point mLocationInParentWindow = new Point();
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800574 private SurfaceControl mParentSurfaceControl;
575 private InputWindowHandle mPortalWindowHandle;
576
Tiger Huang7c610aa2018-10-27 00:01:01 +0800577 // Last systemUiVisibility we received from status bar.
578 private int mLastStatusBarVisibility = 0;
579 // Last systemUiVisibility we dispatched to windows.
580 private int mLastDispatchedSystemUiVisibility = 0;
581
Tiger Huang43b8fc22019-04-26 11:49:29 +0800582 /** Corner radius that windows should have in order to match the display. */
583 private final float mWindowCornerRadius;
584
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800585 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
586 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800587 final AppWindowToken atoken = w.mAppToken;
588 if (winAnimator.mDrawState == READY_TO_SHOW) {
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +0200589 if (atoken == null || atoken.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800590 if (w.performShowLocked()) {
591 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
592 if (DEBUG_LAYOUT_REPEATS) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800593 mWmService.mWindowPlacerLocked.debugLayoutRepeats(
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800594 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
595 }
596 }
597 }
598 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800599 };
600
601 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
602 final WindowStateAnimator winAnimator = w.mWinAnimator;
603 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
604 return;
605 }
606
Jorim Jaggi8f520872018-08-14 17:00:20 +0200607 // If this window is animating, ensure the animation background is set.
608 final AnimationAdapter anim = w.mAppToken != null
609 ? w.mAppToken.getAnimation()
610 : w.getAnimation();
611 if (anim != null) {
612 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800613 if (color != 0) {
614 final TaskStack stack = w.getStack();
615 if (stack != null) {
616 stack.setAnimationBackground(winAnimator, color);
617 }
618 }
619 }
620 };
621
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800622 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
623 final int lostFocusUid = mTmpWindow.mOwnerUid;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800624 final Handler handler = mWmService.mH;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800625 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
626 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
627 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
628 w.mAttrs.hideTimeoutMilliseconds);
629 }
630 }
631 };
632
633 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800634 final AppWindowToken focusedApp = mFocusedApp;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800635 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
636 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
637
638 if (!w.canReceiveKeys()) {
639 return false;
640 }
641
642 final AppWindowToken wtoken = w.mAppToken;
643
644 // If this window's application has been removed, just skip it.
645 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
646 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
647 + (wtoken.removed ? "removed" : "sendingToBottom"));
648 return false;
649 }
650
651 if (focusedApp == null) {
652 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
653 + " using new focus @ " + w);
654 mTmpWindow = w;
655 return true;
656 }
657
658 if (!focusedApp.windowsAreFocusable()) {
659 // Current focused app windows aren't focusable...
660 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
661 + " focusable using new focus @ " + w);
662 mTmpWindow = w;
663 return true;
664 }
665
666 // Descend through all of the app tokens and find the first that either matches
667 // win.mAppToken (return win) or mFocusedApp (return null).
668 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
669 if (focusedApp.compareTo(wtoken) > 0) {
670 // App stack below focused app stack. No focus for you!!!
671 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
672 "findFocusedWindow: Reached focused app=" + focusedApp);
673 mTmpWindow = null;
674 return true;
675 }
676 }
677
678 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
679 mTmpWindow = w;
680 return true;
681 };
682
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800683 private final Consumer<WindowState> mPerformLayout = w -> {
684 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
685 // wasting time and funky changes while a window is animating away.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800686 final boolean gone = (mTmpWindow != null && mWmService.mPolicy.canBeHiddenByKeyguardLw(w))
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800687 || w.isGoneForLayoutLw();
688
689 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
690 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
691 + " mLayoutAttached=" + w.mLayoutAttached
Jorim Jaggi381cd722019-03-27 17:33:02 +0100692 + " config reported=" + w.isLastConfigReportedToClient());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800693 final AppWindowToken atoken = w.mAppToken;
694 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200695 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800696 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
697 + " parentHidden=" + w.isParentWindowHidden());
698 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200699 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800700 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
701 + " parentHidden=" + w.isParentWindowHidden());
702 }
703
704 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
705 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
706 // since that means "perform layout as normal, just don't display").
Jorim Jaggi91d382a2019-03-27 17:00:48 +0100707 if ((!gone || !w.mHaveFrame || w.mLayoutNeeded) && !w.mLayoutAttached) {
708 if (mTmpInitial) {
709 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800710 }
Jorim Jaggi91d382a2019-03-27 17:00:48 +0100711 if (w.mAttrs.type == TYPE_DREAM) {
712 // Don't layout windows behind a dream, so that if it does stuff like hide
713 // the status bar we won't get a bad transition when it goes away.
714 mTmpWindow = w;
715 }
716 w.mLayoutNeeded = false;
717 w.prelayout();
718 final boolean firstLayout = !w.isLaidOut();
719 getDisplayPolicy().layoutWindowLw(w, null, mDisplayFrames);
720 w.mLayoutSeq = mLayoutSeq;
721
722 // If this is the first layout, we need to initialize the last inset values as
723 // otherwise we'd immediately cause an unnecessary resize.
724 if (firstLayout) {
725 w.updateLastInsetValues();
726 }
727
728 if (w.mAppToken != null) {
729 w.mAppToken.layoutLetterbox(w);
730 }
731
732 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
733 + " mContainingFrame=" + w.getContainingFrame()
734 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800735 }
736 };
737
738 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
739 if (w.mLayoutAttached) {
740 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
741 + " mViewVisibility=" + w.mViewVisibility
742 + " mRelayoutCalled=" + w.mRelayoutCalled);
743 // If this view is GONE, then skip it -- keep the current frame, and let the caller
744 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
745 // windows, since that means "perform layout as normal, just don't display").
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800746 if (mTmpWindow != null && mWmService.mPolicy.canBeHiddenByKeyguardLw(w)) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800747 return;
748 }
749 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
750 || w.mLayoutNeeded) {
751 if (mTmpInitial) {
752 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700753 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800754 }
755 w.mLayoutNeeded = false;
756 w.prelayout();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800757 getDisplayPolicy().layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100758 w.mLayoutSeq = mLayoutSeq;
chaviw492139a2018-07-16 16:07:35 -0700759 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700760 + " mContainingFrame=" + w.getContainingFrame()
761 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800762 }
763 } else if (w.mAttrs.type == TYPE_DREAM) {
764 // Don't layout windows behind a dream, so that if it does stuff like hide the
765 // status bar we won't get a bad transition when it goes away.
766 mTmpWindow = mTmpWindow2;
767 }
768 };
769
770 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
771 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
772 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
773 return w.canBeImeTarget();
774 };
775
776 private final Consumer<WindowState> mApplyPostLayoutPolicy =
Tiger Huang7c610aa2018-10-27 00:01:01 +0800777 w -> getDisplayPolicy().applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
lumarkff0ab692018-11-05 20:32:30 +0800778 mInputMethodTarget);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800779
780 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800781 final WindowSurfacePlacer surfacePlacer = mWmService.mWindowPlacerLocked;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800782 final boolean obscuredChanged = w.mObscured !=
783 mTmpApplySurfaceChangesTransactionState.obscured;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800784 final RootWindowContainer root = mWmService.mRoot;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800785
786 // Update effect.
787 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
788 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
789 final boolean isDisplayed = w.isDisplayedLw();
790
791 if (isDisplayed && w.isObscuringDisplay()) {
792 // This window completely covers everything behind it, so we want to leave all
793 // of them as undimmed (for performance reasons).
794 root.mObscuringWindow = w;
795 mTmpApplySurfaceChangesTransactionState.obscured = true;
796 }
797
798 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
799 root.handleNotObscuredLocked(w,
800 mTmpApplySurfaceChangesTransactionState.obscured,
801 mTmpApplySurfaceChangesTransactionState.syswin);
802
803 if (w.mHasSurface && isDisplayed) {
804 final int type = w.mAttrs.type;
805 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
806 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
807 mTmpApplySurfaceChangesTransactionState.syswin = true;
808 }
809 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
810 && w.mAttrs.preferredRefreshRate != 0) {
811 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
812 = w.mAttrs.preferredRefreshRate;
813 }
Ady Abrahamf3e05312019-05-13 18:04:59 -0700814 final int preferredModeId = getDisplayPolicy().getRefreshRatePolicy()
815 .getPreferredModeId(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800816 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
Ady Abrahamf3e05312019-05-13 18:04:59 -0700817 && preferredModeId != 0) {
818 mTmpApplySurfaceChangesTransactionState.preferredModeId = preferredModeId;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800819 }
820 }
821 }
822
wilsonshihc32538e2018-11-07 17:27:34 +0800823 if (obscuredChanged && w.isVisibleLw() && mWallpaperController.isWallpaperTarget(w)) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800824 // This is the wallpaper target and its obscured state changed... make sure the
825 // current wallpaper's visibility has been updated accordingly.
826 mWallpaperController.updateWallpaperVisibility();
827 }
828
chaviw161ea3e2018-01-31 12:01:12 -0800829 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800830
831 final WindowStateAnimator winAnimator = w.mWinAnimator;
832
833 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
chaviw1454b392018-08-06 09:54:04 -0700834 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800835
836 // Moved from updateWindowsAndWallpaperLocked().
837 if (w.mHasSurface) {
838 // Take care of the window being ready to display.
839 final boolean committed = winAnimator.commitFinishDrawingLocked();
840 if (isDefaultDisplay && committed) {
841 if (w.mAttrs.type == TYPE_DREAM) {
842 // HACK: When a dream is shown, it may at that point hide the lock screen.
843 // So we need to redo the layout to let the phone window manager make this
844 // happen.
845 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
846 if (DEBUG_LAYOUT_REPEATS) {
847 surfacePlacer.debugLayoutRepeats(
848 "dream and commitFinishDrawingLocked true",
849 pendingLayoutChanges);
850 }
851 }
852 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
853 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
854 "First draw done in potential wallpaper target " + w);
wilsonshihc32538e2018-11-07 17:27:34 +0800855 mWallpaperMayChange = true;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800856 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
857 if (DEBUG_LAYOUT_REPEATS) {
858 surfacePlacer.debugLayoutRepeats(
859 "wallpaper and commitFinishDrawingLocked true",
860 pendingLayoutChanges);
861 }
862 }
863 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800864 }
865
866 final AppWindowToken atoken = w.mAppToken;
867 if (atoken != null) {
Adrian Roos23df3a32018-03-15 15:41:13 +0100868 atoken.updateLetterboxSurface(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800869 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
870 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
871 mTmpUpdateAllDrawn.add(atoken);
872 }
873 }
874
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800875 if (!mLosingFocus.isEmpty() && w.isFocused() && w.isDisplayedLw()) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800876 mWmService.mH.obtainMessage(REPORT_LOSING_FOCUS, this).sendToTarget();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800877 }
878
879 w.updateResizingWindowIfNeeded();
880 };
881
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800882 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800883 * Create new {@link DisplayContent} instance, add itself to the root window container and
884 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700885 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800886 * @param service You know.
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800887 * @param activityDisplay The ActivityDisplay for the display container.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700888 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700889 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800890 ActivityDisplay activityDisplay) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100891 super(service);
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800892 mAcitvityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800893 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
894 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
895 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
896 + " new=" + display);
897 }
898
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700899 mDisplay = display;
900 mDisplayId = display.getDisplayId();
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800901 mWallpaperController = new WallpaperController(mWmService, this);
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700902 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700903 display.getMetrics(mDisplayMetrics);
Adrian Roos506267d2019-06-18 16:13:53 +0200904 mSystemGestureExclusionLimit = mWmService.mSystemGestureExclusionLimitDp
905 * mDisplayMetrics.densityDpi / DENSITY_DEFAULT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700906 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100907 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo,
908 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700909 initializeDisplayBaseInfo();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700910
lumark588a3e82018-07-20 18:53:54 +0800911 mAppTransition = new AppTransition(service.mContext, service, this);
912 mAppTransition.registerListenerLocked(service.mActivityManagerAppTransitionNotifier);
913 mAppTransitionController = new AppTransitionController(service, this);
914 mUnknownAppVisibilityController = new UnknownAppVisibilityController(service, this);
915
916 AnimationHandler animationHandler = new AnimationHandler();
917 mBoundsAnimationController = new BoundsAnimationController(service.mContext,
Winson Chung6ba767a2018-12-13 18:20:11 -0800918 mAppTransition, AnimationThread.getHandler(), animationHandler);
lumark588a3e82018-07-20 18:53:54 +0800919
Riddle Hsu2588ab02019-02-25 14:23:56 +0800920 final InputChannel inputChannel = mWmService.mInputManager.monitorInput(
921 "PointerEventDispatcher" + mDisplayId, mDisplayId);
922 mPointerEventDispatcher = new PointerEventDispatcher(inputChannel);
923
924 // Tap Listeners are supported for:
925 // 1. All physical displays (multi-display).
926 // 2. VirtualDisplays on VR, AA (and everything else).
927 mTapDetector = new TaskTapPointerEventListener(mWmService, this);
928 registerPointerEventListener(mTapDetector);
929 registerPointerEventListener(mWmService.mMousePositionTracker);
Winson Chungc5fe7ff2019-02-19 14:49:25 -0800930 if (mWmService.mAtmService.getRecentTasks() != null) {
931 registerPointerEventListener(
932 mWmService.mAtmService.getRecentTasks().getInputListener());
933 }
Riddle Hsu2588ab02019-02-25 14:23:56 +0800934
Tiger Huang7c610aa2018-10-27 00:01:01 +0800935 mDisplayPolicy = new DisplayPolicy(service, this);
936 mDisplayRotation = new DisplayRotation(service, this);
Tiger Huang86e6d072019-05-02 20:23:47 +0800937 mCloseToSquareMaxAspectRatio = service.mContext.getResources().getFloat(
938 com.android.internal.R.dimen.config_closeToSquareDisplayMaxAspectRatio);
Tiger Huang7c610aa2018-10-27 00:01:01 +0800939 if (isDefaultDisplay) {
940 // The policy may be invoked right after here, so it requires the necessary default
941 // fields of this display content.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800942 mWmService.mPolicy.setDefaultDisplay(this);
Tiger Huang7c610aa2018-10-27 00:01:01 +0800943 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800944 if (mWmService.mDisplayReady) {
Tiger Huang7c610aa2018-10-27 00:01:01 +0800945 mDisplayPolicy.onConfigurationChanged();
946 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800947 if (mWmService.mSystemReady) {
Tiger Huang7c610aa2018-10-27 00:01:01 +0800948 mDisplayPolicy.systemReady();
949 }
Tiger Huang43b8fc22019-04-26 11:49:29 +0800950 mWindowCornerRadius = mDisplayPolicy.getWindowCornerRadius();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800951 mDividerControllerLocked = new DockedStackDividerController(service, this);
952 mPinnedStackControllerLocked = new PinnedStackController(service, this);
953
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000954 final SurfaceControl.Builder b = mWmService.makeSurfaceBuilder(mSession)
955 .setOpaque(true)
956 .setContainerLayer();
Robert Carrb1579c82017-09-05 14:54:47 -0700957 mWindowingLayer = b.setName("Display Root").build();
958 mOverlayLayer = b.setName("Display Overlays").build();
959
Robert Carrf59b8dd2017-10-02 18:58:36 -0700960 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700961 .setLayerStack(mWindowingLayer, mDisplayId)
962 .show(mWindowingLayer)
963 .setLayer(mOverlayLayer, 1)
964 .setLayerStack(mOverlayLayer, mDisplayId)
965 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700966 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700967
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700968 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700969 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700970 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700971 super.addChild(mAboveAppWindowsContainers, null);
972 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800973
974 // Add itself as a child to the root container.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800975 mWmService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700976
977 // TODO(b/62541591): evaluate whether this is the best spot to declare the
978 // {@link DisplayContent} ready for use.
979 mDisplayReady = true;
Arthur Hung95b38a92018-07-20 18:56:12 +0800980
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800981 mWmService.mAnimator.addDisplayLocked(mDisplayId);
Arthur Hung39134b22018-08-14 11:58:28 +0800982 mInputMonitor = new InputMonitor(service, mDisplayId);
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200983 mInsetsStateController = new InsetsStateController(this);
Bryce Leed1871262017-06-12 14:12:29 -0700984 }
985
986 boolean isReady() {
987 // The display is ready when the system and the individual display are both ready.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800988 return mWmService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700989 }
990
991 int getDisplayId() {
992 return mDisplayId;
993 }
994
Tiger Huang43b8fc22019-04-26 11:49:29 +0800995 float getWindowCornerRadius() {
996 return mWindowCornerRadius;
997 }
998
Wale Ogunwale02319a62016-09-26 15:21:22 -0700999 WindowToken getWindowToken(IBinder binder) {
1000 return mTokenMap.get(binder);
1001 }
1002
1003 AppWindowToken getAppWindowToken(IBinder binder) {
1004 final WindowToken token = getWindowToken(binder);
1005 if (token == null) {
1006 return null;
1007 }
1008 return token.asAppWindowToken();
1009 }
1010
Wale Ogunwale2f569ed2017-05-08 09:15:49 -07001011 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001012 final DisplayContent dc = mWmService.mRoot.getWindowTokenDisplay(token);
Wale Ogunwale02319a62016-09-26 15:21:22 -07001013 if (dc != null) {
1014 // We currently don't support adding a window token to the display if the display
1015 // already has the binder mapped to another token. If there is a use case for supporting
1016 // this moving forward we will either need to merge the WindowTokens some how or have
1017 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -07001018 throw new IllegalArgumentException("Can't map token=" + token + " to display="
1019 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -07001020 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -07001021 if (binder == null) {
1022 throw new IllegalArgumentException("Can't map token=" + token + " to display="
1023 + getName() + " binder is null");
1024 }
1025 if (token == null) {
1026 throw new IllegalArgumentException("Can't map null token to display="
1027 + getName() + " binder=" + binder);
1028 }
1029
Wale Ogunwale02319a62016-09-26 15:21:22 -07001030 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001031
1032 if (token.asAppWindowToken() == null) {
1033 // Add non-app token to container hierarchy on the display. App tokens are added through
1034 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -07001035 switch (token.windowType) {
1036 case TYPE_WALLPAPER:
1037 mBelowAppWindowsContainers.addChild(token);
1038 break;
1039 case TYPE_INPUT_METHOD:
1040 case TYPE_INPUT_METHOD_DIALOG:
1041 mImeWindowsContainers.addChild(token);
1042 break;
1043 default:
1044 mAboveAppWindowsContainers.addChild(token);
1045 break;
1046 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001047 }
Wale Ogunwale02319a62016-09-26 15:21:22 -07001048 }
1049
1050 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001051 final WindowToken token = mTokenMap.remove(binder);
1052 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -08001053 token.setExiting();
1054 }
1055 return token;
1056 }
1057
1058 /** Changes the display the input window token is housed on to this one. */
1059 void reParentWindowToken(WindowToken token) {
1060 final DisplayContent prevDc = token.getDisplayContent();
1061 if (prevDc == this) {
1062 return;
1063 }
Riddle Hsu85bd04b2018-11-17 00:34:36 +08001064 if (prevDc != null) {
1065 if (prevDc.mTokenMap.remove(token.token) != null && token.asAppWindowToken() == null) {
1066 // Removed the token from the map, but made sure it's not an app token before
1067 // removing from parent.
1068 token.getParent().removeChild(token);
1069 }
1070 if (prevDc.mLastFocus == mCurrentFocus) {
1071 // The window has become the focus of this display, so it should not be notified
1072 // that it lost focus from the previous display.
1073 prevDc.mLastFocus = null;
1074 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001075 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -08001076
1077 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -07001078 }
1079
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001080 void removeAppToken(IBinder binder) {
1081 final WindowToken token = removeWindowToken(binder);
1082 if (token == null) {
1083 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
1084 return;
1085 }
1086
1087 final AppWindowToken appToken = token.asAppWindowToken();
1088
1089 if (appToken == null) {
1090 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
1091 return;
1092 }
1093
1094 appToken.onRemovedFromDisplay();
1095 }
1096
Riddle Hsuad256a12018-07-18 16:11:30 +08001097 @Override
1098 public Display getDisplay() {
Craig Mautnerb47bbc32012-08-22 17:41:48 -07001099 return mDisplay;
1100 }
1101
Craig Mautner59c00972012-07-30 12:10:24 -07001102 DisplayInfo getDisplayInfo() {
1103 return mDisplayInfo;
1104 }
1105
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001106 DisplayMetrics getDisplayMetrics() {
1107 return mDisplayMetrics;
1108 }
1109
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001110 DisplayPolicy getDisplayPolicy() {
1111 return mDisplayPolicy;
1112 }
1113
Riddle Hsuad256a12018-07-18 16:11:30 +08001114 @Override
1115 public DisplayRotation getDisplayRotation() {
1116 return mDisplayRotation;
1117 }
1118
Jorim Jaggif96c90a2018-09-26 16:55:15 +02001119 /**
1120 * Marks a window as providing insets for the rest of the windows in the system.
1121 *
1122 * @param type The type of inset this window provides.
1123 * @param win The window.
1124 * @param frameProvider Function to compute the frame, or {@code null} if the just the frame of
1125 * the window should be taken.
1126 */
1127 void setInsetProvider(@InternalInsetType int type, WindowState win,
1128 @Nullable TriConsumer<DisplayFrames, WindowState, Rect> frameProvider) {
1129 mInsetsStateController.getSourceProvider(type).setWindow(win, frameProvider);
1130 }
1131
1132 InsetsStateController getInsetsStateController() {
1133 return mInsetsStateController;
1134 }
1135
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001136 @VisibleForTesting
1137 void setDisplayRotation(DisplayRotation displayRotation) {
1138 mDisplayRotation = displayRotation;
1139 }
1140
Andrii Kulian8ee72852017-03-10 10:36:45 -08001141 int getRotation() {
1142 return mRotation;
1143 }
1144
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001145 @VisibleForTesting
Andrii Kulian8ee72852017-03-10 10:36:45 -08001146 void setRotation(int newRotation) {
1147 mRotation = newRotation;
Riddle Hsuad256a12018-07-18 16:11:30 +08001148 mDisplayRotation.setRotation(newRotation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001149 }
1150
1151 int getLastOrientation() {
1152 return mLastOrientation;
1153 }
1154
Andrii Kulian8ee72852017-03-10 10:36:45 -08001155 int getLastWindowForcedOrientation() {
1156 return mLastWindowForcedOrientation;
1157 }
1158
Evan Rosky966759f2019-01-15 10:33:58 -08001159 void registerRemoteAnimations(RemoteAnimationDefinition definition) {
1160 mAppTransitionController.registerRemoteAnimations(definition);
1161 }
1162
Andrii Kulian06d07d62017-03-14 11:11:47 -07001163 /**
Riddle Hsu654a6f92018-07-13 22:59:36 +08001164 * Temporarily pauses rotation changes until resumed.
1165 *
1166 * This can be used to prevent rotation changes from occurring while the user is
1167 * performing certain operations, such as drag and drop.
1168 *
1169 * This call nests and must be matched by an equal number of calls to
1170 * {@link #resumeRotationLocked}.
1171 */
1172 void pauseRotationLocked() {
1173 mDeferredRotationPauseCount++;
1174 }
1175
1176 /**
1177 * Resumes normal rotation changes after being paused.
1178 */
1179 void resumeRotationLocked() {
1180 if (mDeferredRotationPauseCount <= 0) {
1181 return;
1182 }
1183
1184 mDeferredRotationPauseCount--;
1185 if (mDeferredRotationPauseCount == 0) {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001186 updateRotationAndSendNewConfigIfNeeded();
Riddle Hsu654a6f92018-07-13 22:59:36 +08001187 }
1188 }
1189
1190 /**
Riddle Hsuad256a12018-07-18 16:11:30 +08001191 * If this is true we have updated our desired orientation, but not yet changed the real
1192 * orientation our applied our screen rotation animation. For example, because a previous
1193 * screen rotation was in progress.
1194 *
1195 * @return {@code true} if the there is an ongoing rotation change.
1196 */
1197 boolean rotationNeedsUpdate() {
1198 final int lastOrientation = getLastOrientation();
1199 final int oldRotation = getRotation();
Riddle Hsuad256a12018-07-18 16:11:30 +08001200
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001201 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Garfield Tan4bb83472019-01-16 14:37:04 -08001202 return oldRotation != rotation;
Riddle Hsuad256a12018-07-18 16:11:30 +08001203 }
1204
Wale Ogunwale3a256e62018-12-06 14:41:18 -08001205 /**
1206 * The display content may have configuration set from {@link #DisplayWindowSettings}. This
1207 * callback let the owner of container know there is existing configuration to prevent the
1208 * values from being replaced by the initializing {@link #ActivityDisplay}.
1209 */
1210 void initializeDisplayOverrideConfiguration() {
1211 if (mAcitvityDisplay != null) {
1212 mAcitvityDisplay.onInitializeOverrideConfiguration(getRequestedOverrideConfiguration());
1213 }
1214 }
1215
Riddle Hsu4e611772018-10-31 18:58:28 +08001216 /** Notify the configuration change of this display. */
1217 void sendNewConfiguration() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001218 mWmService.mH.obtainMessage(SEND_NEW_CONFIGURATION, this).sendToTarget();
Riddle Hsu4e611772018-10-31 18:58:28 +08001219 }
1220
Garfield Tan90b04282018-12-11 14:04:42 -08001221 @Override
1222 boolean onDescendantOrientationChanged(IBinder freezeDisplayToken,
1223 ConfigurationContainer requestingContainer) {
1224 final Configuration config = updateOrientationFromAppTokens(
1225 getRequestedOverrideConfiguration(), freezeDisplayToken, false);
Garfield Tan49dae102019-02-04 09:51:59 -08001226 // If display rotation class tells us that it doesn't consider app requested orientation,
1227 // this display won't rotate just because of an app changes its requested orientation. Thus
1228 // it indicates that this display chooses not to handle this request.
1229 final boolean handled = getDisplayRotation().respectAppRequestedOrientation();
Garfield Tan90b04282018-12-11 14:04:42 -08001230 if (config == null) {
1231 return handled;
1232 }
1233
1234 if (handled && requestingContainer instanceof ActivityRecord) {
1235 final ActivityRecord activityRecord = (ActivityRecord) requestingContainer;
1236 final boolean kept = mWmService.mAtmService.updateDisplayOverrideConfigurationLocked(
1237 config, activityRecord, false /* deferResume */, getDisplayId());
1238 activityRecord.frozenBeforeDestroy = true;
1239 if (!kept) {
1240 mWmService.mAtmService.mRootActivityContainer.resumeFocusedStacksTopActivities();
1241 }
1242 } else {
1243 // We have a new configuration to push so we need to update ATMS for now.
1244 // TODO: Clean up display configuration push between ATMS and WMS after unification.
1245 mWmService.mAtmService.updateDisplayOverrideConfigurationLocked(
1246 config, null /* starting */, false /* deferResume */, getDisplayId());
1247 }
1248 return handled;
1249 }
1250
Garfield Tan49dae102019-02-04 09:51:59 -08001251 @Override
1252 boolean handlesOrientationChangeFromDescendant() {
1253 return getDisplayRotation().respectAppRequestedOrientation();
1254 }
1255
Riddle Hsu4e611772018-10-31 18:58:28 +08001256 /**
1257 * Determine the new desired orientation of this display.
1258 *
1259 * The orientation is computed from non-application windows first. If none of the
1260 * non-application windows specify orientation, the orientation is computed from application
1261 * tokens.
1262 *
1263 * @return {@code true} if the orientation is changed.
1264 */
1265 boolean updateOrientationFromAppTokens() {
1266 return updateOrientationFromAppTokens(false /* forceUpdate */);
1267 }
1268
Garfield Tan90b04282018-12-11 14:04:42 -08001269 /**
1270 * Update orientation of the target display, returning a non-null new Configuration if it has
1271 * changed from the current orientation. If a non-null configuration is returned, someone must
1272 * call {@link WindowManagerService#setNewDisplayOverrideConfiguration(Configuration,
1273 * DisplayContent)} to tell the window manager it can unfreeze the screen. This will typically
1274 * be done by calling {@link WindowManagerService#sendNewConfiguration(int)}.
1275 */
1276 Configuration updateOrientationFromAppTokens(Configuration currentConfig,
1277 IBinder freezeDisplayToken, boolean forceUpdate) {
1278 if (!mDisplayReady) {
1279 return null;
1280 }
1281
1282 Configuration config = null;
1283 if (updateOrientationFromAppTokens(forceUpdate)) {
1284 // If we changed the orientation but mOrientationChangeComplete is already true,
1285 // we used seamless rotation, and we don't need to freeze the screen.
1286 if (freezeDisplayToken != null && !mWmService.mRoot.mOrientationChangeComplete) {
1287 final AppWindowToken atoken = getAppWindowToken(freezeDisplayToken);
1288 if (atoken != null) {
1289 atoken.startFreezingScreen();
1290 }
1291 }
1292 config = new Configuration();
1293 computeScreenConfiguration(config);
1294 } else if (currentConfig != null) {
1295 // No obvious action we need to take, but if our current state mismatches the
1296 // activity manager's, update it, disregarding font scale, which should remain set
1297 // to the value of the previous configuration.
1298 // Here we're calling Configuration#unset() instead of setToDefaults() because we
1299 // need to keep override configs clear of non-empty values (e.g. fontSize).
1300 mTmpConfiguration.unset();
1301 mTmpConfiguration.updateFrom(currentConfig);
1302 computeScreenConfiguration(mTmpConfiguration);
1303 if (currentConfig.diff(mTmpConfiguration) != 0) {
1304 mWaitingForConfig = true;
1305 setLayoutNeeded();
1306 int[] anim = new int[2];
1307 getDisplayPolicy().selectRotationAnimationLw(anim);
1308
1309 mWmService.startFreezingDisplayLocked(anim[0], anim[1], this);
1310 config = new Configuration(mTmpConfiguration);
1311 }
1312 }
1313
1314 return config;
1315 }
1316
1317
1318 private boolean updateOrientationFromAppTokens(boolean forceUpdate) {
Riddle Hsu4e611772018-10-31 18:58:28 +08001319 final int req = getOrientation();
1320 if (req != mLastOrientation || forceUpdate) {
1321 mLastOrientation = req;
1322 mDisplayRotation.setCurrentOrientation(req);
1323 return updateRotationUnchecked(forceUpdate);
1324 }
1325 return false;
1326 }
1327
Riddle Hsuad256a12018-07-18 16:11:30 +08001328 /**
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001329 * Update rotation of the display and send configuration if the rotation is changed.
1330 *
1331 * @return {@code true} if the rotation has been changed and the new config is sent.
1332 */
1333 boolean updateRotationAndSendNewConfigIfNeeded() {
1334 final boolean changed = updateRotationUnchecked(false /* forceUpdate */);
1335 if (changed) {
Riddle Hsu4e611772018-10-31 18:58:28 +08001336 sendNewConfiguration();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001337 }
1338 return changed;
1339 }
1340
1341 /**
Andrii Kulian06d07d62017-03-14 11:11:47 -07001342 * Update rotation of the display.
1343 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001344 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1345 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001346 */
Robert Carrae606b42018-02-15 15:36:23 -08001347 boolean updateRotationUnchecked() {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001348 return updateRotationUnchecked(false /* forceUpdate */);
1349 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001350
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001351 /**
Evan Roskye747c3e2018-10-30 20:06:41 -07001352 * Update rotation of the DisplayContent with an option to force the update. This updates
1353 * the container's perception of rotation and, depending on the top activities, will freeze
1354 * the screen or start seamless rotation. The display itself gets rotated in
1355 * {@link #applyRotationLocked} during {@link WindowManagerService#sendNewConfiguration}.
1356 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001357 * @param forceUpdate Force the rotation update. Sometimes in WM we might skip updating
1358 * orientation because we're waiting for some rotation to finish or display
1359 * to unfreeze, which results in configuration of the previously visible
1360 * activity being applied to a newly visible one. Forcing the rotation
1361 * update allows to workaround this issue.
1362 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
Evan Roskye747c3e2018-10-30 20:06:41 -07001363 * {@link WindowManagerService#sendNewConfiguration(int)} TO COMPLETE THE ROTATION AND
1364 * UNFREEZE THE SCREEN.
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001365 */
1366 boolean updateRotationUnchecked(boolean forceUpdate) {
1367 ScreenRotationAnimation screenRotationAnimation;
1368 if (!forceUpdate) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001369 if (mDeferredRotationPauseCount > 0) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001370 // Rotation updates have been paused temporarily. Defer the update until
1371 // updates have been resumed.
1372 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
1373 return false;
1374 }
1375
1376 screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001377 mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001378 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
1379 // Rotation updates cannot be performed while the previous rotation change
1380 // animation is still in progress. Skip this update. We will try updating
1381 // again after the animation is finished and the display is unfrozen.
1382 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
1383 return false;
1384 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001385 if (mWmService.mDisplayFrozen) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001386 // Even if the screen rotation animation has finished (e.g. isAnimating
1387 // returns false), there is still some time where we haven't yet unfrozen
1388 // the display. We also need to abort rotation here.
1389 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
1390 "Deferring rotation, still finishing previous rotation");
1391 return false;
1392 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001393 }
1394
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001395 if (!mWmService.mDisplayEnabled) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001396 // No point choosing a rotation if the display is not enabled.
1397 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
1398 return false;
1399 }
1400
1401 final int oldRotation = mRotation;
1402 final int lastOrientation = mLastOrientation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001403 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001404 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Computed rotation=" + rotation + " for display id="
1405 + mDisplayId + " based on lastOrientation=" + lastOrientation
1406 + " and oldRotation=" + oldRotation);
Tiger Huang7c610aa2018-10-27 00:01:01 +08001407 boolean mayRotateSeamlessly = mDisplayPolicy.shouldRotateSeamlessly(mDisplayRotation,
Riddle Hsuad256a12018-07-18 16:11:30 +08001408 oldRotation, rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001409
Robert Carr0e007272017-10-02 13:00:55 -07001410 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001411 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001412 if (seamlessRotated != null && !forceUpdate) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001413 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1414 // to complete (that is, waiting for windows to redraw). It's tempting to check
1415 // w.mSeamlessRotationCount but that could be incorrect in the case of
1416 // window-removal.
1417 return false;
1418 }
Robert Carr0e007272017-10-02 13:00:55 -07001419
1420 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001421 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001422 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001423 mayRotateSeamlessly = false;
1424 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001425 for (int i = 0; i < mWmService.mSessions.size(); i++) {
1426 if (mWmService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
Robert Carr0e007272017-10-02 13:00:55 -07001427 mayRotateSeamlessly = false;
1428 break;
1429 }
1430 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001431 }
Robert Carr0e007272017-10-02 13:00:55 -07001432 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001433
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001434 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1435 + " selected orientation " + lastOrientation
Garfield Tan49dae102019-02-04 09:51:59 -08001436 + ", got rotation " + rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001437
Garfield Tan4bb83472019-01-16 14:37:04 -08001438 if (oldRotation == rotation) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001439 // No change.
1440 return false;
1441 }
1442
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001443 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1444 + " rotation changed to " + rotation
Garfield Tan4bb83472019-01-16 14:37:04 -08001445 + " from " + oldRotation
1446 + ", lastOrientation=" + lastOrientation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001447
1448 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
Vishnu Nairba183352018-11-21 11:16:49 -08001449 mWaitingForConfig = true;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001450 }
1451
Evan Roskye747c3e2018-10-30 20:06:41 -07001452 mRotation = rotation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001453
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001454 mWmService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1455 mWmService.mH.sendNewMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
Riddle Hsu654a6f92018-07-13 22:59:36 +08001456 this, WINDOW_FREEZE_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001457
1458 setLayoutNeeded();
1459 final int[] anim = new int[2];
Tiger Huang7c610aa2018-10-27 00:01:01 +08001460 mDisplayPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001461
1462 if (!rotateSeamlessly) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001463 mWmService.startFreezingDisplayLocked(anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001464 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001465 } else {
1466 // The screen rotation animation uses a screenshot to freeze the screen
1467 // while windows resize underneath.
1468 // When we are rotating seamlessly, we allow the elements to transition
1469 // to their rotated state independently and without a freeze required.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001470 mWmService.startSeamlessRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001471 }
1472
Evan Roskye747c3e2018-10-30 20:06:41 -07001473 return true;
1474 }
1475
1476 /**
1477 * Applies the rotation transaction. This must be called after {@link #updateRotationUnchecked}
1478 * (if it returned {@code true}) to actually finish the rotation.
1479 *
1480 * @param oldRotation the rotation we are coming from.
1481 * @param rotation the rotation to apply.
1482 */
1483 void applyRotationLocked(final int oldRotation, final int rotation) {
1484 mDisplayRotation.setRotation(rotation);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001485 final boolean rotateSeamlessly = mWmService.isRotatingSeamlessly();
Evan Roskye747c3e2018-10-30 20:06:41 -07001486 ScreenRotationAnimation screenRotationAnimation = rotateSeamlessly
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001487 ? null : mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001488 // We need to update our screen size information to match the new rotation. If the rotation
1489 // has actually changed then this method will return true and, according to the comment at
1490 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1491 // By updating the Display info here it will be available to
1492 // #computeScreenConfiguration() later.
Tiger Huanga817b1f2019-05-09 20:04:17 +08001493 updateDisplayAndOrientation(getConfiguration().uiMode, null /* outConfig */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001494
Robert Carrae606b42018-02-15 15:36:23 -08001495 // NOTE: We disable the rotation in the emulator because
1496 // it doesn't support hardware OpenGL emulation yet.
1497 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1498 && screenRotationAnimation.hasScreenshot()) {
1499 if (screenRotationAnimation.setRotation(getPendingTransaction(), rotation,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001500 MAX_ANIMATION_DURATION, mWmService.getTransitionAnimationScaleLocked(),
Robert Carrae606b42018-02-15 15:36:23 -08001501 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001502 mWmService.scheduleAnimationLocked();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001503 }
1504 }
1505
Vishnu Nair83537a72018-07-19 21:27:48 -07001506 forAllWindows(w -> {
Vishnu Nairddd80742018-08-21 14:12:46 -07001507 w.seamlesslyRotateIfAllowed(getPendingTransaction(), oldRotation, rotation,
1508 rotateSeamlessly);
Vishnu Nair83537a72018-07-19 21:27:48 -07001509 }, true /* traverseTopToBottom */);
1510
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001511 mWmService.mDisplayManagerInternal.performTraversal(getPendingTransaction());
Robert Carrae606b42018-02-15 15:36:23 -08001512 scheduleAnimation();
1513
Andrii Kulian06d07d62017-03-14 11:11:47 -07001514 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001515 if (w.mHasSurface && !rotateSeamlessly) {
1516 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001517 w.setOrientationChanging(true);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001518 mWmService.mRoot.mOrientationChangeComplete = false;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001519 w.mLastFreezeDuration = 0;
1520 }
1521 w.mReportOrientationChanged = true;
1522 }, true /* traverseTopToBottom */);
1523
1524 if (rotateSeamlessly) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001525 mWmService.mH.sendNewMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
Riddle Hsu654a6f92018-07-13 22:59:36 +08001526 this, SEAMLESS_ROTATION_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001527 }
1528
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001529 for (int i = mWmService.mRotationWatchers.size() - 1; i >= 0; i--) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001530 final WindowManagerService.RotationWatcher rotationWatcher
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001531 = mWmService.mRotationWatchers.get(i);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001532 if (rotationWatcher.mDisplayId == mDisplayId) {
1533 try {
1534 rotationWatcher.mWatcher.onRotationChanged(rotation);
1535 } catch (RemoteException e) {
1536 // Ignore
1537 }
1538 }
1539 }
1540
Andrii Kulian06d07d62017-03-14 11:11:47 -07001541 // Announce rotation only if we will not animate as we already have the
1542 // windows in final state. Otherwise, we make this call at the rotation end.
Rhed Jao02655dc2018-10-30 20:44:52 +08001543 if (screenRotationAnimation == null && mWmService.mAccessibilityController != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001544 mWmService.mAccessibilityController.onRotationChangedLocked(this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001545 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001546 }
1547
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001548 void configureDisplayPolicy() {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001549 final int width = mBaseDisplayWidth;
1550 final int height = mBaseDisplayHeight;
1551 final int shortSize;
1552 final int longSize;
1553 if (width > height) {
1554 shortSize = height;
1555 longSize = width;
1556 } else {
1557 shortSize = width;
1558 longSize = height;
1559 }
1560
Adrian Roos506267d2019-06-18 16:13:53 +02001561 final int shortSizeDp = shortSize * DENSITY_DEFAULT / mBaseDisplayDensity;
1562 final int longSizeDp = longSize * DENSITY_DEFAULT / mBaseDisplayDensity;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001563
Winson Chung4723b4e2019-03-25 16:49:36 -07001564 mDisplayPolicy.updateConfigurationAndScreenSizeDependentBehaviors();
Tiger Huang3d2b8982019-01-29 22:56:48 +08001565 mDisplayRotation.configure(width, height, shortSizeDp, longSizeDp);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001566
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001567 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1568 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Tiger Huang86e6d072019-05-02 20:23:47 +08001569
1570 // Not much of use to rotate the display for apps since it's close to square.
1571 mIgnoreRotationForApps = isNonDecorDisplayCloseToSquare(Surface.ROTATION_0, width, height);
1572 }
1573
1574 private boolean isNonDecorDisplayCloseToSquare(int rotation, int width, int height) {
1575 final DisplayCutout displayCutout =
1576 calculateDisplayCutoutForRotation(rotation).getDisplayCutout();
1577 final int uiMode = mWmService.mPolicy.getUiMode();
1578 final int w = mDisplayPolicy.getNonDecorDisplayWidth(
1579 width, height, rotation, uiMode, displayCutout);
1580 final int h = mDisplayPolicy.getNonDecorDisplayHeight(
1581 width, height, rotation, uiMode, displayCutout);
1582 final float aspectRatio = Math.max(w, h) / (float) Math.min(w, h);
1583 return aspectRatio <= mCloseToSquareMaxAspectRatio;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001584 }
1585
Andrii Kulian06d07d62017-03-14 11:11:47 -07001586 /**
1587 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1588 * changed.
1589 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1590 */
Tiger Huanga817b1f2019-05-09 20:04:17 +08001591 private DisplayInfo updateDisplayAndOrientation(int uiMode, Configuration outConfig) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001592 // Use the effective "visual" dimensions based on current rotation
1593 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
Garfield Tan4bb83472019-01-16 14:37:04 -08001594 final int dw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1595 final int dh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001596
1597 // Update application display metrics.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001598 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(mRotation);
1599 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1600
Tiger Huang7c610aa2018-10-27 00:01:01 +08001601 final int appWidth = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1602 displayCutout);
1603 final int appHeight = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1604 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001605 mDisplayInfo.rotation = mRotation;
1606 mDisplayInfo.logicalWidth = dw;
1607 mDisplayInfo.logicalHeight = dh;
1608 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1609 mDisplayInfo.appWidth = appWidth;
1610 mDisplayInfo.appHeight = appHeight;
1611 if (isDefaultDisplay) {
1612 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1613 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1614 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001615 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001616 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1617 if (mDisplayScalingDisabled) {
1618 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1619 } else {
1620 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1621 }
1622
Tiger Huanga817b1f2019-05-09 20:04:17 +08001623 computeSizeRangesAndScreenLayout(mDisplayInfo, rotated, uiMode, dw, dh,
1624 mDisplayMetrics.density, outConfig);
1625
Andrii Kulianf0379de2018-03-14 16:24:07 -07001626 // We usually set the override info in DisplayManager so that we get consistent display
1627 // metrics values when displays are changing and don't send out new values until WM is aware
1628 // of them. However, we don't do this for displays that serve as containers for ActivityView
1629 // because we don't want letter-/pillar-boxing during resize.
1630 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1631 ? mDisplayInfo : null;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001632 mWmService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001633 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001634
1635 mBaseDisplayRect.set(0, 0, dw, dh);
1636
1637 if (isDefaultDisplay) {
1638 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1639 mCompatDisplayMetrics);
1640 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001641
Andrii Kulian06d07d62017-03-14 11:11:47 -07001642 return mDisplayInfo;
1643 }
1644
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001645 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001646 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1647 }
1648
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001649 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001650 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001651 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001652 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001653 }
Adrian Roos11c25582018-02-19 18:06:36 +01001654 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001655 return WmDisplayCutout.computeSafeInsets(
1656 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001657 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001658 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Issei Suzuki43190bd2018-08-20 17:28:41 +02001659 final Rect[] newBounds = mRotationUtil.getRotatedBounds(
1660 WmDisplayCutout.computeSafeInsets(
Jorim Jaggi60640512018-06-29 01:14:31 +02001661 cutout, mInitialDisplayWidth, mInitialDisplayHeight)
Issei Suzuki43190bd2018-08-20 17:28:41 +02001662 .getDisplayCutout().getBoundingRectsAll(),
1663 rotation, mInitialDisplayWidth, mInitialDisplayHeight);
1664 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(newBounds),
Adrian Roos24264212018-02-19 16:26:15 +01001665 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1666 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001667 }
1668
Andrii Kulian06d07d62017-03-14 11:11:47 -07001669 /**
1670 * Compute display configuration based on display properties and policy settings.
1671 * Do not call if mDisplayReady == false.
1672 */
1673 void computeScreenConfiguration(Configuration config) {
Tiger Huanga817b1f2019-05-09 20:04:17 +08001674 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode, config);
Evan Roskye747c3e2018-10-30 20:06:41 -07001675 calculateBounds(displayInfo, mTmpBounds);
1676 config.windowConfiguration.setBounds(mTmpBounds);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001677
1678 final int dw = displayInfo.logicalWidth;
1679 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001680 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Garfield Tane0846042018-07-26 13:42:04 -07001681 config.windowConfiguration.setWindowingMode(getWindowingMode());
Yunfan Chen7daa6ac2018-11-29 18:16:44 -08001682 config.windowConfiguration.setDisplayWindowingMode(getWindowingMode());
Evan Roskye747c3e2018-10-30 20:06:41 -07001683 config.windowConfiguration.setRotation(displayInfo.rotation);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001684
Adrian Roos11c25582018-02-19 18:06:36 +01001685 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001686 config.screenWidthDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001687 (int)(mDisplayPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1688 config.uiMode, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001689 config.screenHeightDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001690 (int)(mDisplayPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1691 config.uiMode, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001692
Tiger Huang7c610aa2018-10-27 00:01:01 +08001693 mDisplayPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001694 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001695 final int leftInset = mTmpRect.left;
1696 final int topInset = mTmpRect.top;
1697 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001698 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1699 leftInset + displayInfo.appWidth /* right */,
1700 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001701 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1702 || displayInfo.rotation == Surface.ROTATION_270);
1703
Andrii Kulian06d07d62017-03-14 11:11:47 -07001704 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1705 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1706 ? Configuration.SCREENLAYOUT_ROUND_YES
1707 : Configuration.SCREENLAYOUT_ROUND_NO);
1708
1709 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1710 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1711 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001712 dh, displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001713 config.densityDpi = displayInfo.logicalDensityDpi;
1714
1715 config.colorMode =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001716 ((displayInfo.isHdr() && mWmService.hasHdrSupport())
Andrii Kulian06d07d62017-03-14 11:11:47 -07001717 ? Configuration.COLOR_MODE_HDR_YES
1718 : Configuration.COLOR_MODE_HDR_NO)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001719 | (displayInfo.isWideColorGamut() && mWmService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001720 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1721 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1722
1723 // Update the configuration based on available input devices, lid switch,
1724 // and platform configuration.
1725 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1726 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1727 config.navigation = Configuration.NAVIGATION_NONAV;
1728
1729 int keyboardPresence = 0;
1730 int navigationPresence = 0;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001731 final InputDevice[] devices = mWmService.mInputManager.getInputDevices();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001732 final int len = devices != null ? devices.length : 0;
1733 for (int i = 0; i < len; i++) {
1734 InputDevice device = devices[i];
Arthur Hung82bbfc32018-11-29 20:24:51 +08001735 // Ignore virtual input device.
1736 if (device.isVirtual()) {
1737 continue;
1738 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001739
Arthur Hung82bbfc32018-11-29 20:24:51 +08001740 // Check if input device can dispatch events to current display.
1741 // If display type is virtual, will follow the default display.
1742 if (!mWmService.mInputManager.canDispatchToDisplay(device.getId(),
1743 displayInfo.type == Display.TYPE_VIRTUAL ? DEFAULT_DISPLAY : mDisplayId)) {
1744 continue;
1745 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001746
Arthur Hung82bbfc32018-11-29 20:24:51 +08001747 final int sources = device.getSources();
1748 final int presenceFlag = device.isExternal()
1749 ? WindowManagerPolicy.PRESENCE_EXTERNAL : WindowManagerPolicy.PRESENCE_INTERNAL;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001750
Arthur Hung82bbfc32018-11-29 20:24:51 +08001751 if (mWmService.mIsTouchDevice) {
1752 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) == InputDevice.SOURCE_TOUCHSCREEN) {
1753 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001754 }
Arthur Hung82bbfc32018-11-29 20:24:51 +08001755 } else {
1756 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1757 }
1758
1759 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1760 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1761 navigationPresence |= presenceFlag;
1762 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1763 && config.navigation == Configuration.NAVIGATION_NONAV) {
1764 config.navigation = Configuration.NAVIGATION_DPAD;
1765 navigationPresence |= presenceFlag;
1766 }
1767
1768 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1769 config.keyboard = Configuration.KEYBOARD_QWERTY;
1770 keyboardPresence |= presenceFlag;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001771 }
1772 }
1773
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001774 if (config.navigation == Configuration.NAVIGATION_NONAV && mWmService.mHasPermanentDpad) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001775 config.navigation = Configuration.NAVIGATION_DPAD;
1776 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1777 }
1778
1779 // Determine whether a hard keyboard is available and enabled.
1780 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1781 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001782 if (hardKeyboardAvailable != mWmService.mHardKeyboardAvailable) {
1783 mWmService.mHardKeyboardAvailable = hardKeyboardAvailable;
1784 mWmService.mH.removeMessages(REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1785 mWmService.mH.sendEmptyMessage(REPORT_HARD_KEYBOARD_STATUS_CHANGE);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001786 }
1787
Winson Chung4723b4e2019-03-25 16:49:36 -07001788 mDisplayPolicy.updateConfigurationAndScreenSizeDependentBehaviors();
Tiger Huang7c610aa2018-10-27 00:01:01 +08001789
Andrii Kulian06d07d62017-03-14 11:11:47 -07001790 // Let the policy update hidden states.
1791 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1792 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1793 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001794 mWmService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001795 }
1796
1797 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001798 DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001799 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1800 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1801 final int unrotDw, unrotDh;
1802 if (rotated) {
1803 unrotDw = dh;
1804 unrotDh = dw;
1805 } else {
1806 unrotDw = dw;
1807 unrotDh = dh;
1808 }
1809 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001810 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001811 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001812 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001813 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001814 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001815 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001816 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001817 return sw;
1818 }
1819
1820 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001821 DisplayMetrics dm, int dw, int dh, DisplayCutout displayCutout) {
1822 dm.noncompatWidthPixels = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1823 displayCutout);
1824 dm.noncompatHeightPixels = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode,
1825 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001826 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1827 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1828 if (curSize == 0 || size < curSize) {
1829 curSize = size;
1830 }
1831 return curSize;
1832 }
1833
Tiger Huang7c610aa2018-10-27 00:01:01 +08001834 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, boolean rotated,
1835 int uiMode, int dw, int dh, float density, Configuration outConfig) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001836
1837 // We need to determine the smallest width that will occur under normal
1838 // operation. To this, start with the base screen size and compute the
1839 // width under the different possible rotations. We need to un-rotate
1840 // the current screen dimensions before doing this.
1841 int unrotDw, unrotDh;
1842 if (rotated) {
1843 unrotDw = dh;
1844 unrotDh = dw;
1845 } else {
1846 unrotDw = dw;
1847 unrotDh = dh;
1848 }
1849 displayInfo.smallestNominalAppWidth = 1<<30;
1850 displayInfo.smallestNominalAppHeight = 1<<30;
1851 displayInfo.largestNominalAppWidth = 0;
1852 displayInfo.largestNominalAppHeight = 0;
Tiger Huang7c610aa2018-10-27 00:01:01 +08001853 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_0, uiMode, unrotDw, unrotDh);
1854 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_90, uiMode, unrotDh, unrotDw);
1855 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_180, uiMode, unrotDw, unrotDh);
1856 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_270, uiMode, unrotDh, unrotDw);
Tiger Huanga817b1f2019-05-09 20:04:17 +08001857
1858 if (outConfig == null) {
1859 return;
1860 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001861 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1862 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001863 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001864 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001865 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001866 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001867 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001868 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001869 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001870 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1871 outConfig.screenLayout = sl;
1872 }
1873
1874 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001875 int uiMode, DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001876 // Get the app screen size at this rotation.
Tiger Huang7c610aa2018-10-27 00:01:01 +08001877 int w = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayCutout);
1878 int h = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001879
1880 // Compute the screen layout size class for this rotation.
1881 int longSize = w;
1882 int shortSize = h;
1883 if (longSize < shortSize) {
1884 int tmp = longSize;
1885 longSize = shortSize;
1886 shortSize = tmp;
1887 }
1888 longSize = (int)(longSize/density);
1889 shortSize = (int)(shortSize/density);
1890 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1891 }
1892
Tiger Huang7c610aa2018-10-27 00:01:01 +08001893 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001894 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001895 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1896 rotation).getDisplayCutout();
Tiger Huang7c610aa2018-10-27 00:01:01 +08001897 final int width = mDisplayPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1898 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001899 if (width < displayInfo.smallestNominalAppWidth) {
1900 displayInfo.smallestNominalAppWidth = width;
1901 }
1902 if (width > displayInfo.largestNominalAppWidth) {
1903 displayInfo.largestNominalAppWidth = width;
1904 }
Tiger Huang7c610aa2018-10-27 00:01:01 +08001905 final int height = mDisplayPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1906 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001907 if (height < displayInfo.smallestNominalAppHeight) {
1908 displayInfo.smallestNominalAppHeight = height;
1909 }
1910 if (height > displayInfo.largestNominalAppHeight) {
1911 displayInfo.largestNominalAppHeight = height;
1912 }
1913 }
1914
Riddle Hsua4d6fa22018-08-11 00:50:39 +08001915 /**
1916 * Apps that use the compact menu panel (as controlled by the panelMenuIsCompact
1917 * theme attribute) on devices that feature a physical options menu key attempt to position
1918 * their menu panel window along the edge of the screen nearest the physical menu key.
1919 * This lowers the travel distance between invoking the menu panel and selecting
1920 * a menu option.
1921 *
1922 * This method helps control where that menu is placed. Its current implementation makes
1923 * assumptions about the menu key and its relationship to the screen based on whether
1924 * the device's natural orientation is portrait (width < height) or landscape.
1925 *
1926 * The menu key is assumed to be located along the bottom edge of natural-portrait
1927 * devices and along the right edge of natural-landscape devices. If these assumptions
1928 * do not hold for the target device, this method should be changed to reflect that.
1929 *
1930 * @return A {@link Gravity} value for placing the options menu window.
1931 */
1932 int getPreferredOptionsPanelGravity() {
1933 final int rotation = getRotation();
1934 if (mInitialDisplayWidth < mInitialDisplayHeight) {
1935 // On devices with a natural orientation of portrait.
1936 switch (rotation) {
1937 default:
1938 case Surface.ROTATION_0:
1939 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1940 case Surface.ROTATION_90:
1941 return Gravity.RIGHT | Gravity.BOTTOM;
1942 case Surface.ROTATION_180:
1943 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1944 case Surface.ROTATION_270:
1945 return Gravity.START | Gravity.BOTTOM;
1946 }
1947 }
1948
1949 // On devices with a natural orientation of landscape.
1950 switch (rotation) {
1951 default:
1952 case Surface.ROTATION_0:
1953 return Gravity.RIGHT | Gravity.BOTTOM;
1954 case Surface.ROTATION_90:
1955 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1956 case Surface.ROTATION_180:
1957 return Gravity.START | Gravity.BOTTOM;
1958 case Surface.ROTATION_270:
1959 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1960 }
1961 }
1962
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001963 DockedStackDividerController getDockedDividerController() {
1964 return mDividerControllerLocked;
1965 }
1966
Winson Chung655332c2016-10-31 13:14:28 -07001967 PinnedStackController getPinnedStackController() {
1968 return mPinnedStackControllerLocked;
1969 }
1970
Jeff Browna506a6e2013-06-04 00:02:38 -07001971 /**
1972 * Returns true if the specified UID has access to this display.
1973 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001974 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001975 return mDisplay.hasAccess(uid);
1976 }
1977
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001978 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001979 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001980 }
1981
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001982 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001983 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001984 }
1985
Wale Ogunwale61911492017-10-11 08:50:50 -07001986 /**
1987 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1988 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001989 TaskStack getSplitScreenPrimaryStack() {
1990 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001991 return (stack != null && stack.isVisible()) ? stack : null;
1992 }
1993
Robert Carr9785cf32018-04-25 15:06:07 -07001994 boolean hasSplitScreenPrimaryStack() {
1995 return getSplitScreenPrimaryStack() != null;
1996 }
1997
Wale Ogunwale61911492017-10-11 08:50:50 -07001998 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001999 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07002000 * not visible.
2001 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07002002 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
2003 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002004 }
2005
2006 TaskStack getPinnedStack() {
2007 return mTaskStackContainers.getPinnedStack();
2008 }
2009
2010 private boolean hasPinnedStack() {
2011 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07002012 }
2013
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07002014 /**
2015 * Returns the topmost stack on the display that is compatible with the input windowing mode.
2016 * Null is no compatible stack on the display.
2017 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002018 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07002019 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
2020 }
2021
2022 /**
2023 * Returns the topmost stack on the display that is compatible with the input windowing mode and
2024 * activity type. Null is no compatible stack on the display.
2025 */
Wale Ogunwale68278562017-09-23 17:13:55 -07002026 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002027 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07002028 }
2029
Bryce Lee48f4b572017-04-10 10:54:15 -07002030 @VisibleForTesting
Kazuki Takise148d00a2018-05-31 15:32:19 +09002031 WindowList<TaskStack> getStacks() {
2032 return mTaskStackContainers.mChildren;
2033 }
2034
2035 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07002036 TaskStack getTopStack() {
2037 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07002038 }
2039
Winson Chunge2d72172018-01-25 17:46:20 +00002040 ArrayList<Task> getVisibleTasks() {
2041 return mTaskStackContainers.getVisibleTasks();
2042 }
2043
Wale Ogunwale61911492017-10-11 08:50:50 -07002044 void onStackWindowingModeChanged(TaskStack stack) {
2045 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07002046 }
2047
Andrii Kulian441e4492016-09-29 15:25:00 -07002048 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07002049 public void onConfigurationChanged(Configuration newParentConfig) {
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05002050 final int lastOrientation = getConfiguration().orientation;
Andrii Kulian441e4492016-09-29 15:25:00 -07002051 super.onConfigurationChanged(newParentConfig);
Tiger Huang7c610aa2018-10-27 00:01:01 +08002052 if (mDisplayPolicy != null) {
2053 mDisplayPolicy.onConfigurationChanged();
2054 }
Andrii Kulian441e4492016-09-29 15:25:00 -07002055
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05002056 if (lastOrientation != getConfiguration().orientation) {
2057 getMetricsLogger().write(
2058 new LogMaker(MetricsEvent.ACTION_PHONE_ORIENTATION_CHANGED)
2059 .setSubtype(getConfiguration().orientation)
2060 .addTaggedData(MetricsEvent.FIELD_DISPLAY_ID, getDisplayId()));
2061 }
2062
Evan Roskye747c3e2018-10-30 20:06:41 -07002063 // If there was no pinned stack, we still need to notify the controller of the display info
2064 // update as a result of the config change.
2065 if (mPinnedStackControllerLocked != null && !hasPinnedStack()) {
2066 mPinnedStackControllerLocked.onDisplayInfoChanged(getDisplayInfo());
2067 }
Evan Roskye747c3e2018-10-30 20:06:41 -07002068 }
2069
2070 /**
2071 * Updates the resources used by docked/pinned controllers. This needs to be called at the
2072 * beginning of a configuration update cascade since the metrics from these resources are used
2073 * for bounds calculations. Since ActivityDisplay initiates the configuration update, this
2074 * should be called from there instead of DisplayContent's onConfigurationChanged.
2075 */
2076 void preOnConfigurationChanged() {
Bryce Leef3c6a472017-11-14 14:53:06 -08002077 final DockedStackDividerController dividerController = getDockedDividerController();
2078
2079 if (dividerController != null) {
2080 getDockedDividerController().onConfigurationChanged();
2081 }
2082
2083 final PinnedStackController pinnedStackController = getPinnedStackController();
2084
2085 if (pinnedStackController != null) {
2086 getPinnedStackController().onConfigurationChanged();
2087 }
Andrii Kulian441e4492016-09-29 15:25:00 -07002088 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07002089
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07002090 @Override
2091 boolean fillsParent() {
2092 return true;
2093 }
2094
2095 @Override
2096 boolean isVisible() {
2097 return true;
2098 }
2099
2100 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002101 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002102 super.onAppTransitionDone();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002103 mWmService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002104 }
2105
Yunfan Chen7daa6ac2018-11-29 18:16:44 -08002106 @Override
2107 public void setWindowingMode(int windowingMode) {
2108 super.setWindowingMode(windowingMode);
2109 super.setDisplayWindowingMode(windowingMode);
2110 }
2111
2112 @Override
2113 void setDisplayWindowingMode(int windowingMode) {
2114 setWindowingMode(windowingMode);
2115 }
2116
Robert Carr9785cf32018-04-25 15:06:07 -07002117 /**
2118 * In split-screen mode we process the IME containers above the docked divider
2119 * rather than directly above their target.
2120 */
2121 private boolean skipTraverseChild(WindowContainer child) {
lumarkff0ab692018-11-05 20:32:30 +08002122 if (child == mImeWindowsContainers && mInputMethodTarget != null
Robert Carr9785cf32018-04-25 15:06:07 -07002123 && !hasSplitScreenPrimaryStack()) {
2124 return true;
2125 }
2126 return false;
2127 }
2128
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07002129 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002130 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
2131 // Special handling so we can process IME windows with #forAllImeWindows above their IME
2132 // target, or here in order if there isn't an IME target.
2133 if (traverseTopToBottom) {
2134 for (int i = mChildren.size() - 1; i >= 0; --i) {
2135 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07002136 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002137 continue;
2138 }
Robert Carr9785cf32018-04-25 15:06:07 -07002139
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002140 if (child.forAllWindows(callback, traverseTopToBottom)) {
2141 return true;
2142 }
2143 }
2144 } else {
2145 final int count = mChildren.size();
2146 for (int i = 0; i < count; i++) {
2147 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07002148 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002149 continue;
2150 }
Robert Carr9785cf32018-04-25 15:06:07 -07002151
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002152 if (child.forAllWindows(callback, traverseTopToBottom)) {
2153 return true;
2154 }
2155 }
2156 }
2157 return false;
2158 }
2159
2160 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
2161 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
2162 }
2163
Wale Ogunwale399c8692017-05-08 14:22:42 -07002164 @Override
2165 int getOrientation() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002166 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002167
Tiger Huang86e6d072019-05-02 20:23:47 +08002168 if (mIgnoreRotationForApps) {
2169 return SCREEN_ORIENTATION_USER;
2170 }
2171
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002172 if (mWmService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08002173 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07002174 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
2175 + " is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002176 // If the display is frozen, some activities may be in the middle of restarting, and
2177 // thus have removed their old window. If the window has the flag to hide the lock
2178 // screen, then the lock screen can re-appear and inflict its own orientation on us.
2179 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08002180 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002181 } else if (policy.isKeyguardLocked()) {
2182 // Use the last orientation the while the display is frozen with the keyguard
2183 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
2184 // window. We don't want to check the show when locked window directly though as
2185 // things aren't stable while the display is frozen, for example the window could be
2186 // momentarily unavailable due to activity relaunch.
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07002187 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
2188 + " is frozen while keyguard locked, return " + mLastOrientation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08002189 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07002190 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002191 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002192 final int orientation = mAboveAppWindowsContainers.getOrientation();
2193 if (orientation != SCREEN_ORIENTATION_UNSET) {
2194 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002195 }
Wale Ogunwale51362492016-09-08 17:49:17 -07002196 }
2197
Wale Ogunwale399c8692017-05-08 14:22:42 -07002198 // Top system windows are not requesting an orientation. Start searching from apps.
2199 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07002200 }
2201
Craig Mautner46ac6fa2013-08-01 10:06:34 -07002202 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07002203 // Check if display metrics changed and update base values if needed.
2204 updateBaseDisplayMetricsIfNeeded();
2205
Craig Mautner722285e2012-09-07 13:55:58 -07002206 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07002207 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07002208
Garfield Tan2f145f22018-11-01 15:27:03 -07002209 onDisplayChanged(this);
Craig Mautner722285e2012-09-07 13:55:58 -07002210 }
2211
Adrian Roos506267d2019-06-18 16:13:53 +02002212 @Override
2213 void onDisplayChanged(DisplayContent dc) {
2214 super.onDisplayChanged(dc);
2215 updateSystemGestureExclusionLimit();
2216 }
2217
2218 void updateSystemGestureExclusionLimit() {
2219 mSystemGestureExclusionLimit = mWmService.mSystemGestureExclusionLimitDp
2220 * mDisplayMetrics.densityDpi / DENSITY_DEFAULT;
2221 updateSystemGestureExclusion();
2222 }
2223
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002224 void initializeDisplayBaseInfo() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002225 final DisplayManagerInternal displayManagerInternal = mWmService.mDisplayManagerInternal;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002226 if (displayManagerInternal != null) {
2227 // Bootstrap the default logical display from the display manager.
2228 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
2229 if (newDisplayInfo != null) {
2230 mDisplayInfo.copyFrom(newDisplayInfo);
2231 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002232 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002233
Andrii Kuliancd097992017-03-23 18:31:59 -07002234 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
2235 mDisplayInfo.logicalDensityDpi);
2236 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
2237 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
2238 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002239 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002240 }
2241
Andrii Kuliancd097992017-03-23 18:31:59 -07002242 /**
2243 * If display metrics changed, overrides are not set and it's not just a rotation - update base
2244 * values.
2245 */
2246 private void updateBaseDisplayMetricsIfNeeded() {
2247 // Get real display metrics without overrides from WM.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002248 mWmService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
Andrii Kuliancd097992017-03-23 18:31:59 -07002249 final int orientation = mDisplayInfo.rotation;
2250 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2251 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
2252 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
2253 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002254 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07002255
2256 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
2257 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01002258 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
2259 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07002260
2261 if (displayMetricsChanged) {
2262 // Check if display size or density is forced.
2263 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
2264 || mBaseDisplayHeight != mInitialDisplayHeight;
2265 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
2266
2267 // If there is an override set for base values - use it, otherwise use new values.
2268 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
2269 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
2270 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
2271
2272 // Real display metrics changed, so we should also update initial values.
2273 mInitialDisplayWidth = newWidth;
2274 mInitialDisplayHeight = newHeight;
2275 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01002276 mInitialDisplayCutout = newCutout;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002277 mWmService.reconfigureDisplayLocked(this);
Andrii Kuliancd097992017-03-23 18:31:59 -07002278 }
2279 }
2280
Bryce Lee27cec322017-03-21 09:41:37 -07002281 /** Sets the maximum width the screen resolution can be */
2282 void setMaxUiWidth(int width) {
2283 if (DEBUG_DISPLAY) {
2284 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
2285 }
2286
2287 mMaxUiWidth = width;
2288
2289 // Update existing metrics.
2290 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
2291 }
2292
2293 /** Update base (override) display metrics. */
2294 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
2295 mBaseDisplayWidth = baseWidth;
2296 mBaseDisplayHeight = baseHeight;
2297 mBaseDisplayDensity = baseDensity;
2298
2299 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
2300 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
2301 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
2302 mBaseDisplayWidth = mMaxUiWidth;
2303
2304 if (DEBUG_DISPLAY) {
2305 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
2306 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
2307 + " on display:" + getDisplayId());
2308 }
2309 }
2310
2311 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08002312
2313 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07002314 }
2315
Riddle Hsuf53da812018-08-15 22:00:27 +08002316 /**
2317 * Forces this display to use the specified density.
2318 *
2319 * @param density The density in DPI to use. If the value equals to initial density, the setting
2320 * will be cleared.
2321 * @param userId The target user to apply. Only meaningful when this is default display. If the
2322 * user id is {@link UserHandle#USER_CURRENT}, it means to apply current settings
2323 * so only need to configure display.
2324 */
2325 void setForcedDensity(int density, int userId) {
2326 final boolean clear = density == mInitialDisplayDensity;
2327 final boolean updateCurrent = userId == UserHandle.USER_CURRENT;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002328 if (mWmService.mCurrentUserId == userId || updateCurrent) {
Riddle Hsuf53da812018-08-15 22:00:27 +08002329 mBaseDisplayDensity = density;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002330 mWmService.reconfigureDisplayLocked(this);
Riddle Hsuf53da812018-08-15 22:00:27 +08002331 }
2332 if (updateCurrent) {
2333 // We are applying existing settings so no need to save it again.
2334 return;
2335 }
2336
2337 if (density == mInitialDisplayDensity) {
2338 density = 0;
2339 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002340 mWmService.mDisplayWindowSettings.setForcedDensity(this, density, userId);
Riddle Hsuf53da812018-08-15 22:00:27 +08002341 }
2342
2343 /** @param mode {@link #FORCE_SCALING_MODE_AUTO} or {@link #FORCE_SCALING_MODE_DISABLED}. */
2344 void setForcedScalingMode(@ForceScalingMode int mode) {
2345 if (mode != FORCE_SCALING_MODE_DISABLED) {
2346 mode = FORCE_SCALING_MODE_AUTO;
2347 }
2348
2349 mDisplayScalingDisabled = (mode != FORCE_SCALING_MODE_AUTO);
2350 Slog.i(TAG_WM, "Using display scaling mode: " + (mDisplayScalingDisabled ? "off" : "auto"));
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002351 mWmService.reconfigureDisplayLocked(this);
Riddle Hsuf53da812018-08-15 22:00:27 +08002352
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002353 mWmService.mDisplayWindowSettings.setForcedScalingMode(this, mode);
Riddle Hsuf53da812018-08-15 22:00:27 +08002354 }
2355
2356 /** If the given width and height equal to initial size, the setting will be cleared. */
2357 void setForcedSize(int width, int height) {
2358 final boolean clear = mInitialDisplayWidth == width && mInitialDisplayHeight == height;
2359 if (!clear) {
2360 // Set some sort of reasonable bounds on the size of the display that we will try
2361 // to emulate.
2362 final int minSize = 200;
2363 final int maxScale = 2;
2364 width = Math.min(Math.max(width, minSize), mInitialDisplayWidth * maxScale);
2365 height = Math.min(Math.max(height, minSize), mInitialDisplayHeight * maxScale);
2366 }
2367
2368 Slog.i(TAG_WM, "Using new display size: " + width + "x" + height);
2369 updateBaseDisplayMetrics(width, height, mBaseDisplayDensity);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002370 mWmService.reconfigureDisplayLocked(this);
Riddle Hsuf53da812018-08-15 22:00:27 +08002371
2372 if (clear) {
2373 width = height = 0;
2374 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002375 mWmService.mDisplayWindowSettings.setForcedSize(this, width, height);
Riddle Hsuf53da812018-08-15 22:00:27 +08002376 }
2377
Wale Ogunwaledb506192017-12-08 10:57:32 -08002378 void getStableRect(Rect out) {
2379 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08002380 }
2381
Yunfan Chen279f5582018-12-12 15:24:50 -08002382 void setStackOnDisplay(int stackId, boolean onTop, TaskStack stack) {
2383 if (DEBUG_STACK) {
2384 Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId=" + mDisplayId);
2385 }
Andrii Kulian839def92016-11-02 10:58:58 -07002386
Wale Ogunwale61911492017-10-11 08:50:50 -07002387 mTaskStackContainers.addStackToDisplay(stack, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002388 }
2389
Andrii Kulian51c1b672017-04-07 18:39:32 -07002390 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002391 final DisplayContent prevDc = stack.getDisplayContent();
2392 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002393 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
2394 + " which is not currently attached to any display");
2395 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002396 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07002397 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
2398 + " to its current displayId=" + mDisplayId);
2399 }
2400
Wale Ogunwale61911492017-10-11 08:50:50 -07002401 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07002402 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08002403 }
2404
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002405 @Override
2406 protected void addChild(DisplayChildWindowContainer child,
2407 Comparator<DisplayChildWindowContainer> comparator) {
2408 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2409 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002410
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002411 @Override
2412 protected void addChild(DisplayChildWindowContainer child, int index) {
2413 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2414 }
2415
2416 @Override
2417 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002418 // Only allow removal of direct children from this display if the display is in the process
2419 // of been removed.
2420 if (mRemovingDisplay) {
2421 super.removeChild(child);
2422 return;
2423 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002424 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002425 }
2426
Andrii Kuliand2765632016-12-12 22:26:34 -08002427 @Override
2428 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
2429 // Children of the display are statically ordered, so the real intention here is to perform
2430 // the operation on the display and not the static direct children.
2431 getParent().positionChildAt(position, this, includingParents);
2432 }
2433
Riddle Hsu57831b52018-07-27 00:31:48 +08002434 void positionStackAt(int position, TaskStack child, boolean includingParents) {
2435 mTaskStackContainers.positionChildAt(position, child, includingParents);
Winson Chung59a47ded2018-01-25 17:46:06 +00002436 layoutAndAssignWindowLayersIfNeeded();
2437 }
2438
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002439 /**
Winson Chungc5fe7ff2019-02-19 14:49:25 -08002440 * Returns true if the input point is within an app window.
2441 */
2442 boolean pointWithinAppWindow(int x, int y) {
2443 final int[] targetWindowType = {-1};
2444 final Consumer fn = PooledLambda.obtainConsumer((w, nonArg) -> {
2445 if (targetWindowType[0] != -1) {
2446 return;
2447 }
2448
2449 if (w.isOnScreen() && w.isVisibleLw() && w.getFrameLw().contains(x, y)) {
2450 targetWindowType[0] = w.mAttrs.type;
2451 return;
2452 }
2453 }, PooledLambda.__(WindowState.class), mTmpRect);
2454 forAllWindows(fn, true /* traverseTopToBottom */);
2455 ((PooledConsumer) fn).recycle();
2456 return FIRST_APPLICATION_WINDOW <= targetWindowType[0]
2457 && targetWindowType[0] <= LAST_APPLICATION_WINDOW;
2458 }
2459
2460 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08002461 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07002462 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07002463 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07002464 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002465 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002466 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07002467 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2468 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002469 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002470 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002471 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002472
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002473 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
2474 if (mTmpTaskForResizePointSearchResult.searchDone) {
2475 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07002476 }
2477 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002478 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002479 }
2480
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002481 void updateTouchExcludeRegion() {
2482 final Task focusedTask = (mFocusedApp != null ? mFocusedApp.getTask() : null);
David Stevensee9e2772017-02-09 16:30:27 -08002483 if (focusedTask == null) {
2484 mTouchExcludeRegion.setEmpty();
2485 } else {
2486 mTouchExcludeRegion.set(mBaseDisplayRect);
2487 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
2488 mTmpRect2.setEmpty();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002489 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0;
2490 --stackNdx) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002491 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002492 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
2493 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08002494 }
2495 // If we removed the focused task above, add it back and only leave its
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002496 // outside touch area in the exclusion. TapDetector is not interested in
David Stevensee9e2772017-02-09 16:30:27 -08002497 // any touch inside the focused task itself.
2498 if (!mTmpRect2.isEmpty()) {
2499 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
2500 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08002501 }
lumark90120a82018-08-15 00:33:03 +08002502 if (mInputMethodWindow != null && mInputMethodWindow.isVisibleLw()) {
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002503 // If the input method is visible and the user is typing, we don't want these touch
2504 // events to be intercepted and used to change focus. This would likely cause a
2505 // disappearance of the input method.
lumark90120a82018-08-15 00:33:03 +08002506 mInputMethodWindow.getTouchableRegion(mTmpRegion);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002507 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002508 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002509 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002510 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002511 win.getTouchableRegion(mTmpRegion);
2512 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2513 }
Tiger Huang04dc4cc2019-01-17 18:41:41 +08002514 amendWindowTapExcludeRegion(mTouchExcludeRegion);
Andrii Kulian03c403d2016-11-11 11:14:12 -08002515 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002516 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002517 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002518 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002519 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2520 }
Riddle Hsu2588ab02019-02-25 14:23:56 +08002521 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner6601b7b2013-04-29 10:29:11 -07002522 }
2523
Tiger Huang04dc4cc2019-01-17 18:41:41 +08002524 /**
2525 * Union the region with all the tap exclude region provided by windows on this display.
2526 *
2527 * @param inOutRegion The region to be amended.
2528 */
2529 void amendWindowTapExcludeRegion(Region inOutRegion) {
2530 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2531 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2532 win.amendTapExcludeRegion(inOutRegion);
2533 }
2534 }
2535
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002536 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07002537 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002538 super.switchUser();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002539 mWmService.mWindowsChanged = true;
Winson Chungda20fec2019-04-10 12:19:59 -07002540 mDisplayPolicy.switchUser();
Craig Mautner858d8a62013-04-23 17:08:34 -07002541 }
2542
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07002543 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002544 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2545 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07002546 }
2547 }
2548
Wale Ogunwale10124582016-09-15 20:25:50 -07002549 @Override
2550 void removeIfPossible() {
2551 if (isAnimating()) {
2552 mDeferredRemoval = true;
2553 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002554 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002555 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002556 }
2557
Wale Ogunwale10124582016-09-15 20:25:50 -07002558 @Override
2559 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002560 mRemovingDisplay = true;
2561 try {
Jackal Guoc43a0a62019-04-23 09:15:14 +08002562 if (mParentWindow != null) {
2563 mParentWindow.removeEmbeddedDisplayContent(this);
2564 }
lumark588a3e82018-07-20 18:53:54 +08002565 // Clear all transitions & screen frozen states when removing display.
2566 mOpeningApps.clear();
2567 mClosingApps.clear();
Evan Rosky2289ba12018-11-19 18:28:18 -08002568 mChangingApps.clear();
lumark588a3e82018-07-20 18:53:54 +08002569 mUnknownAppVisibilityController.clear();
2570 mAppTransition.removeAppTransitionTimeoutCallbacks();
2571 handleAnimatingStoppedAndTransition();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002572 mWmService.stopFreezingDisplayLocked();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002573 super.removeImmediately();
2574 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Riddle Hsu2588ab02019-02-25 14:23:56 +08002575 mPointerEventDispatcher.dispose();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002576 mWmService.mAnimator.removeDisplayLocked(mDisplayId);
Louis Chang207f9112018-07-30 08:58:54 +08002577 mWindowingLayer.release();
2578 mOverlayLayer.release();
Jorim Jaggiae962e62018-12-27 17:23:48 +01002579 mInputMonitor.onDisplayRemoved();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002580 } finally {
Riddle Hsu4e611772018-10-31 18:58:28 +08002581 mDisplayReady = false;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002582 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002583 }
Robert Carr679ccb02018-08-08 15:32:35 -07002584
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002585 mWmService.mWindowPlacerLocked.requestTraversal();
Craig Mautner95da1082014-02-24 17:54:35 -08002586 }
2587
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002588 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002589 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002590 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002591 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2592
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002593 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002594 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002595 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002596 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002597 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002598 }
2599
Andrii Kulian0214ed92017-05-16 13:44:05 -07002600 /** @return 'true' if removal of this display content is deferred due to active animation. */
2601 boolean isRemovalDeferred() {
2602 return mDeferredRemoval;
2603 }
2604
Wale Ogunwale10124582016-09-15 20:25:50 -07002605 boolean animateForIme(float interpolatedValue, float animationTarget,
2606 float dividerAnimationTarget) {
2607 boolean updated = false;
2608
Wale Ogunwale61911492017-10-11 08:50:50 -07002609 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2610 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002611 if (stack == null || !stack.isAdjustedForIme()) {
2612 continue;
2613 }
2614
2615 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2616 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2617 updated = true;
2618 } else {
2619 mDividerControllerLocked.mLastAnimationProgress =
2620 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2621 mDividerControllerLocked.mLastDividerProgress =
2622 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2623 updated |= stack.updateAdjustForIme(
2624 mDividerControllerLocked.mLastAnimationProgress,
2625 mDividerControllerLocked.mLastDividerProgress,
2626 false /* force */);
2627 }
2628 if (interpolatedValue >= 1f) {
2629 stack.endImeAdjustAnimation();
2630 }
2631 }
2632
2633 return updated;
2634 }
2635
2636 boolean clearImeAdjustAnimation() {
2637 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002638 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2639 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002640 if (stack != null && stack.isAdjustedForIme()) {
2641 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2642 changed = true;
2643 }
2644 }
2645 return changed;
2646 }
2647
2648 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002649 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2650 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002651 if (stack.isVisible() && stack.isAdjustedForIme()) {
2652 stack.beginImeAdjustAnimation();
2653 }
2654 }
2655 }
2656
2657 void adjustForImeIfNeeded() {
lumark90120a82018-08-15 00:33:03 +08002658 final WindowState imeWin = mInputMethodWindow;
Wale Ogunwale10124582016-09-15 20:25:50 -07002659 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2660 && !mDividerControllerLocked.isImeHideRequested();
Riddle Hsuc6814252019-05-16 17:06:52 +08002661 final TaskStack dockedStack = getSplitScreenPrimaryStack();
2662 final boolean dockVisible = dockedStack != null;
2663 final Task topDockedTask = dockVisible ? dockedStack.getTopChild() : null;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002664 final TaskStack imeTargetStack = mWmService.getImeFocusStackLocked();
Wale Ogunwale10124582016-09-15 20:25:50 -07002665 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2666 imeTargetStack.getDockSide() : DOCKED_INVALID;
2667 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2668 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002669 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002670 final boolean imeHeightChanged = imeVisible &&
2671 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2672
Riddle Hsuc6814252019-05-16 17:06:52 +08002673 // This includes a case where the docked stack is unminimizing and IME is visible for the
2674 // bottom side stack. The condition prevents adjusting the override task bounds for IME to
2675 // the minimized docked stack bounds.
2676 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock()
2677 || (topDockedTask != null && imeOnBottom && !dockedStack.isAdjustedForIme()
2678 && dockedStack.getBounds().height() < topDockedTask.getBounds().height());
2679
Wale Ogunwale10124582016-09-15 20:25:50 -07002680 // The divider could be adjusted for IME position, or be thinner than usual,
2681 // or both. There are three possible cases:
2682 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2683 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2684 // - If IME is not visible, divider is not moved and is normal width.
2685
2686 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002687 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2688 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002689 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002690 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2691 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002692 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2693 } else {
2694 stack.resetAdjustedForIme(false);
2695 }
2696 }
2697 mDividerControllerLocked.setAdjustedForIme(
2698 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2699 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002700 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2701 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002702 stack.resetAdjustedForIme(!dockVisible);
2703 }
2704 mDividerControllerLocked.setAdjustedForIme(
2705 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2706 }
Winson Chung655332c2016-10-31 13:14:28 -07002707 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002708 }
2709
2710 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002711 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2712 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002713 stack.prepareFreezingTaskBounds();
2714 }
2715 }
2716
Wale Ogunwale94744212015-09-21 19:01:47 -07002717 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002718 getBounds(mTmpRect, newRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002719 rotateBounds(mTmpRect, oldRotation, newRotation, bounds);
2720 }
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002721
Evan Rosky39b6f232018-10-30 18:35:41 -07002722 void rotateBounds(Rect parentBounds, int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002723 // Compute a transform matrix to undo the coordinate space transformation,
2724 // and present the window at the same physical position it previously occupied.
2725 final int deltaRotation = deltaRotation(newRotation, oldRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002726 createRotationMatrix(
2727 deltaRotation, parentBounds.width(), parentBounds.height(), mTmpMatrix);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002728
2729 mTmpRectF.set(bounds);
2730 mTmpMatrix.mapRect(mTmpRectF);
2731 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002732 }
2733
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002734 static int deltaRotation(int oldRotation, int newRotation) {
2735 int delta = newRotation - oldRotation;
2736 if (delta < 0) delta += 4;
2737 return delta;
2738 }
2739
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002740 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002741 Matrix outMatrix) {
2742 // For rotations without Z-ordering we don't need the target rectangle's position.
2743 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2744 displayHeight, outMatrix);
2745 }
2746
2747 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2748 float displayWidth, float displayHeight, Matrix outMatrix) {
2749 switch (rotation) {
2750 case ROTATION_0:
2751 outMatrix.reset();
2752 break;
2753 case ROTATION_270:
2754 outMatrix.setRotate(270, 0, 0);
2755 outMatrix.postTranslate(0, displayHeight);
2756 outMatrix.postTranslate(rectTop, 0);
2757 break;
2758 case ROTATION_180:
2759 outMatrix.reset();
2760 break;
2761 case ROTATION_90:
2762 outMatrix.setRotate(90, 0, 0);
2763 outMatrix.postTranslate(displayWidth, 0);
2764 outMatrix.postTranslate(-rectTop, rectLeft);
2765 break;
2766 }
2767 }
2768
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002769 @CallSuper
2770 @Override
Nataniel Borges023ecb52019-01-16 14:15:43 -08002771 public void writeToProto(ProtoOutputStream proto, long fieldId,
2772 @WindowTraceLogLevel int logLevel) {
2773 // Critical log level logs only visible elements to mitigate performance overheard
2774 if (logLevel == WindowTraceLogLevel.CRITICAL && !isVisible()) {
2775 return;
2776 }
2777
Steven Timotiusaf03df62017-07-18 16:56:43 -07002778 final long token = proto.start(fieldId);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002779 super.writeToProto(proto, WINDOW_CONTAINER, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002780 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002781 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2782 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002783 stack.writeToProto(proto, STACKS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002784 }
2785 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2786 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002787 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2788 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002789 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002790 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002791 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2792 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002793 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002794 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002795 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2796 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002797 windowToken.writeToProto(proto, IME_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002798 }
2799 proto.write(DPI, mBaseDisplayDensity);
2800 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002801 proto.write(ROTATION, mRotation);
2802 final ScreenRotationAnimation screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002803 mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002804 if (screenRotationAnimation != null) {
2805 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2806 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002807 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
lumark588a3e82018-07-20 18:53:54 +08002808 mAppTransition.writeToProto(proto, APP_TRANSITION);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002809 if (mFocusedApp != null) {
2810 mFocusedApp.writeNameToProto(proto, FOCUSED_APP);
2811 }
Jorim Jaggi1112fed2019-04-15 13:32:14 +02002812 for (int i = mOpeningApps.size() - 1; i >= 0; i--) {
2813 mOpeningApps.valueAt(i).mActivityRecord.writeIdentifierToProto(proto, OPENING_APPS);
2814 }
2815 for (int i = mClosingApps.size() - 1; i >= 0; i--) {
2816 mClosingApps.valueAt(i).mActivityRecord.writeIdentifierToProto(proto, CLOSING_APPS);
2817 }
2818 for (int i = mChangingApps.size() - 1; i >= 0; i--) {
2819 mChangingApps.valueAt(i).mActivityRecord.writeIdentifierToProto(proto, CHANGING_APPS);
2820 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002821 proto.end(token);
2822 }
2823
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002824 @Override
2825 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2826 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002827 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2828 final String subPrefix = " " + prefix;
2829 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2830 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2831 pw.print("dpi");
2832 if (mInitialDisplayWidth != mBaseDisplayWidth
2833 || mInitialDisplayHeight != mBaseDisplayHeight
2834 || mInitialDisplayDensity != mBaseDisplayDensity) {
2835 pw.print(" base=");
2836 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2837 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2838 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002839 if (mDisplayScalingDisabled) {
2840 pw.println(" noscale");
2841 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002842 pw.print(" cur=");
2843 pw.print(mDisplayInfo.logicalWidth);
2844 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2845 pw.print(" app=");
2846 pw.print(mDisplayInfo.appWidth);
2847 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2848 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2849 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2850 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2851 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002852 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002853 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002854 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002855
Craig Mautnerdc548482014-02-05 13:35:24 -08002856 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002857 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002858 pw.print(prefix);
2859 pw.print("mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
Adrian Roos5251b1d2018-03-23 18:57:43 +01002860
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002861 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
2862 if (mLastFocus != mCurrentFocus) {
2863 pw.print(" mLastFocus="); pw.println(mLastFocus);
2864 }
2865 if (mLosingFocus.size() > 0) {
2866 pw.println();
2867 pw.println(" Windows losing focus:");
2868 for (int i = mLosingFocus.size() - 1; i >= 0; i--) {
2869 final WindowState w = mLosingFocus.get(i);
2870 pw.print(" Losing #"); pw.print(i); pw.print(' ');
2871 pw.print(w);
2872 if (dumpAll) {
2873 pw.println(":");
2874 w.dump(pw, " ", true);
2875 } else {
2876 pw.println();
2877 }
2878 }
2879 }
2880 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
Tiger Huang7c610aa2018-10-27 00:01:01 +08002881 if (mLastStatusBarVisibility != 0) {
2882 pw.print(" mLastStatusBarVisibility=0x");
2883 pw.println(Integer.toHexString(mLastStatusBarVisibility));
2884 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002885
Adrian Roos5251b1d2018-03-23 18:57:43 +01002886 pw.println();
wilsonshihc32538e2018-11-07 17:27:34 +08002887 mWallpaperController.dump(pw, " ");
2888
2889 pw.println();
Adrian Roos4ffc8972019-02-07 20:45:11 +01002890 pw.print("mSystemGestureExclusion=");
2891 if (mSystemGestureExclusionListeners.getRegisteredCallbackCount() > 0) {
2892 pw.println(mSystemGestureExclusion);
2893 } else {
2894 pw.println("<no lstnrs>");
2895 }
2896
2897 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002898 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002899 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2900 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002901 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002902 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002903
Craig Mautnerdc548482014-02-05 13:35:24 -08002904 pw.println();
2905 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002906 pw.println();
2907 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002908 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002909 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002910 pw.print(" Exiting #"); pw.print(i);
2911 pw.print(' '); pw.print(token);
2912 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002913 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002914 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002915 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002916
Jorim Jaggi31f71702016-05-04 16:43:04 -07002917 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002918
2919 // Dump stack references
2920 final TaskStack homeStack = getHomeStack();
2921 if (homeStack != null) {
2922 pw.println(prefix + "homeStack=" + homeStack.getName());
2923 }
2924 final TaskStack pinnedStack = getPinnedStack();
2925 if (pinnedStack != null) {
2926 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2927 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002928 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002929 if (splitScreenPrimaryStack != null) {
2930 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2931 }
2932
2933 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002934 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002935 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002936 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002937
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002938 pw.println();
2939 mDisplayFrames.dump(prefix, pw);
Arthur Hung95b38a92018-07-20 18:56:12 +08002940 pw.println();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08002941 mDisplayPolicy.dump(prefix, pw);
2942 pw.println();
Riddle Hsuad256a12018-07-18 16:11:30 +08002943 mDisplayRotation.dump(prefix, pw);
2944 pw.println();
Arthur Hung95b38a92018-07-20 18:56:12 +08002945 mInputMonitor.dump(pw, " ");
Jorim Jaggif96c90a2018-09-26 16:55:15 +02002946 pw.println();
2947 mInsetsStateController.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002948 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002949
2950 @Override
2951 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002952 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002953 }
2954
2955 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002956 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002957 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002958
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002959 /** Returns true if the stack in the windowing mode is visible. */
2960 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002961 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002962 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002963 }
2964
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002965 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002966 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002967 final int x = (int) xf;
2968 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002969 final WindowState touchedWin = getWindow(w -> {
2970 final int flags = w.mAttrs.flags;
2971 if (!w.isVisibleLw()) {
2972 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002973 }
2974 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002975 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002976 }
2977
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002978 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002979 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002980 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002981 }
2982
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002983 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002984
2985 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002986 return mTmpRegion.contains(x, y) || touchFlags == 0;
2987 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002988
2989 return touchedWin;
2990 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002991
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002992 boolean canAddToastWindowForUid(int uid) {
2993 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002994 // Also if the app is focused adding more than one toast at
2995 // a time for better backwards compatibility.
2996 final WindowState focusedWindowForUid = getWindow(w ->
2997 w.mOwnerUid == uid && w.isFocused());
2998 if (focusedWindowForUid != null) {
2999 return true;
3000 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003001 final WindowState win = getWindow(w ->
3002 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
3003 && !w.mWindowRemovalAllowed);
3004 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07003005 }
3006
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003007 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07003008 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
3009 return;
3010 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003011
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003012 // Used to communicate the old focus to the callback method.
3013 mTmpWindow = oldFocus;
3014
3015 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07003016 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07003017
Louis Changa9350fe2019-04-25 17:14:20 +08003018 /**
3019 * Looking for the focused window on this display if the top focused display hasn't been
3020 * found yet (topFocusedDisplayId is INVALID_DISPLAY) or per-display focused was allowed.
3021 *
3022 * @param topFocusedDisplayId Id of the top focused display.
3023 * @return The focused window or null if there isn't any or no need to seek.
3024 */
3025 WindowState findFocusedWindowIfNeeded(int topFocusedDisplayId) {
3026 return (mWmService.mPerDisplayFocusEnabled || topFocusedDisplayId == INVALID_DISPLAY)
3027 ? findFocusedWindow() : null;
Tiger Huang51c5a1d2018-12-11 20:24:51 +08003028 }
3029
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07003030 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003031 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07003032
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003033 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003034
3035 if (mTmpWindow == null) {
3036 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
3037 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07003038 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003039 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07003040 }
3041
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003042 /**
3043 * Update the focused window and make some adjustments if the focus has changed.
3044 *
3045 * @param mode Indicates the situation we are in. Possible modes are:
3046 * {@link WindowManagerService#UPDATE_FOCUS_NORMAL},
3047 * {@link WindowManagerService#UPDATE_FOCUS_PLACING_SURFACES},
3048 * {@link WindowManagerService#UPDATE_FOCUS_WILL_PLACE_SURFACES},
3049 * {@link WindowManagerService#UPDATE_FOCUS_REMOVING_FOCUS}
3050 * @param updateInputWindows Whether to sync the window information to the input module.
Louis Changa9350fe2019-04-25 17:14:20 +08003051 * @param topFocusedDisplayId Display id of current top focused display.
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003052 * @return {@code true} if the focused window has changed.
3053 */
Louis Changa9350fe2019-04-25 17:14:20 +08003054 boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows,
3055 int topFocusedDisplayId) {
3056 WindowState newFocus = findFocusedWindowIfNeeded(topFocusedDisplayId);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003057 if (mCurrentFocus == newFocus) {
3058 return false;
3059 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003060 boolean imWindowChanged = false;
Tiger Huang51c5a1d2018-12-11 20:24:51 +08003061 final WindowState imWindow = mInputMethodWindow;
3062 if (imWindow != null) {
3063 final WindowState prevTarget = mInputMethodTarget;
3064 final WindowState newTarget = computeImeTarget(true /* updateImeTarget*/);
3065 imWindowChanged = prevTarget != newTarget;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003066
Tiger Huang51c5a1d2018-12-11 20:24:51 +08003067 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS
3068 && mode != UPDATE_FOCUS_WILL_PLACE_SURFACES) {
3069 assignWindowLayers(false /* setLayoutNeeded */);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003070 }
3071 }
3072
3073 if (imWindowChanged) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003074 mWmService.mWindowsChanged = true;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003075 setLayoutNeeded();
Louis Changa9350fe2019-04-25 17:14:20 +08003076 newFocus = findFocusedWindowIfNeeded(topFocusedDisplayId);
Tiger Huang51c5a1d2018-12-11 20:24:51 +08003077 }
3078 if (mCurrentFocus != newFocus) {
3079 mWmService.mH.obtainMessage(REPORT_FOCUS_CHANGE, this).sendToTarget();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003080 }
3081
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003082 if (DEBUG_FOCUS_LIGHT || mWmService.localLOGV) Slog.v(TAG_WM, "Changing focus from "
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003083 + mCurrentFocus + " to " + newFocus + " displayId=" + getDisplayId()
3084 + " Callers=" + Debug.getCallers(4));
3085 final WindowState oldFocus = mCurrentFocus;
3086 mCurrentFocus = newFocus;
3087 mLosingFocus.remove(newFocus);
3088
3089 if (newFocus != null) {
3090 mWinAddedSinceNullFocus.clear();
3091 mWinRemovedSinceNullFocus.clear();
3092
3093 if (newFocus.canReceiveKeys()) {
3094 // Displaying a window implicitly causes dispatching to be unpaused.
3095 // This is to protect against bugs if someone pauses dispatching but
3096 // forgets to resume.
3097 newFocus.mToken.paused = false;
3098 }
3099 }
3100
Tiger Huang7c610aa2018-10-27 00:01:01 +08003101 int focusChanged = getDisplayPolicy().focusChangedLw(oldFocus, newFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003102
3103 if (imWindowChanged && oldFocus != mInputMethodWindow) {
3104 // Focus of the input method window changed. Perform layout if needed.
3105 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
3106 performLayout(true /*initial*/, updateInputWindows);
3107 focusChanged &= ~FINISH_LAYOUT_REDO_LAYOUT;
3108 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
3109 // Client will do the layout, but we need to assign layers
3110 // for handleNewWindowLocked() below.
3111 assignWindowLayers(false /* setLayoutNeeded */);
3112 }
3113 }
3114
3115 if ((focusChanged & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3116 // The change in focus caused us to need to do a layout. Okay.
3117 setLayoutNeeded();
3118 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
3119 performLayout(true /*initial*/, updateInputWindows);
3120 } else if (mode == UPDATE_FOCUS_REMOVING_FOCUS) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003121 mWmService.mRoot.performSurfacePlacement(false);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003122 }
3123 }
3124
3125 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
3126 // If we defer assigning layers, then the caller is responsible for doing this part.
3127 getInputMonitor().setInputFocusLw(newFocus, updateInputWindows);
3128 }
3129
3130 adjustForImeIfNeeded();
3131
3132 // We may need to schedule some toast windows to be removed. The toasts for an app that
3133 // does not have input focus are removed within a timeout to prevent apps to redress
3134 // other apps' UI.
3135 scheduleToastWindowsTimeoutIfNeededLocked(oldFocus, newFocus);
3136
3137 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
3138 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
3139 }
3140 return true;
3141 }
3142
3143 /**
3144 * Set the new focused app to this display.
3145 *
3146 * @param newFocus the new focused AppWindowToken.
3147 * @return true if the focused app is changed.
3148 */
3149 boolean setFocusedApp(AppWindowToken newFocus) {
3150 if (newFocus != null) {
3151 final DisplayContent appDisplay = newFocus.getDisplayContent();
3152 if (appDisplay != this) {
3153 throw new IllegalStateException(newFocus + " is not on " + getName()
3154 + " but " + ((appDisplay != null) ? appDisplay.getName() : "none"));
3155 }
3156 }
3157 if (mFocusedApp == newFocus) {
3158 return false;
3159 }
3160 mFocusedApp = newFocus;
3161 getInputMonitor().setFocusedAppLw(newFocus);
3162 updateTouchExcludeRegion();
3163 return true;
3164 }
3165
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003166 /** Updates the layer assignment of windows on this display. */
3167 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggi4981f152019-03-26 18:58:45 +01003168 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07003169 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003170 if (setLayoutNeeded) {
3171 setLayoutNeeded();
3172 }
Robert Carrb1579c82017-09-05 14:54:47 -07003173
Robert Carrf59b8dd2017-10-02 18:58:36 -07003174 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07003175 // the application of this transaction until the animation pass triggers
3176 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
3177 // the hiding and showing of surfaces.
3178 scheduleAnimation();
Jorim Jaggi4981f152019-03-26 18:58:45 +01003179 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003180 }
3181
Wale Ogunwale1666e312016-12-16 11:27:18 -08003182 // TODO: This should probably be called any time a visual change is made to the hierarchy like
3183 // moving containers or resizing them. Need to investigate the best way to have it automatically
3184 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003185 void layoutAndAssignWindowLayersIfNeeded() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003186 mWmService.mWindowsChanged = true;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003187 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003188
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003189 if (!mWmService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003190 false /*updateInputWindows*/)) {
3191 assignWindowLayers(false /* setLayoutNeeded */);
3192 }
3193
Arthur Hung95b38a92018-07-20 18:56:12 +08003194 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003195 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08003196 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003197 }
3198
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003199 /** Returns true if a leaked surface was destroyed */
3200 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003201 // Used to indicate that a surface was leaked.
3202 mTmpWindow = null;
3203 forAllWindows(w -> {
3204 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003205 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003206 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003207 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003208 if (!mWmService.mSessions.contains(wsa.mSession)) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003209 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003210 + w + " surface=" + wsa.mSurfaceController
3211 + " token=" + w.mToken
3212 + " pid=" + w.mSession.mPid
3213 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003214 wsa.destroySurface();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003215 mWmService.mForceRemoves.add(w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003216 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07003217 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003218 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003219 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02003220 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003221 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003222 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003223 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003224 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003225 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003226
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003227 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003228 }
3229
3230 /**
lumark90120a82018-08-15 00:33:03 +08003231 * Set input method window for the display.
3232 * @param win Set when window added or Null when destroyed.
3233 */
3234 void setInputMethodWindowLocked(WindowState win) {
3235 mInputMethodWindow = win;
lumark70865a82018-09-17 16:56:26 +08003236 // Update display configuration for IME process.
3237 if (mInputMethodWindow != null) {
3238 final int imePid = mInputMethodWindow.mSession.mPid;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003239 mWmService.mAtmInternal.onImeWindowSetOnDisplay(imePid,
lumark70865a82018-09-17 16:56:26 +08003240 mInputMethodWindow.getDisplayId());
3241 }
lumark90120a82018-08-15 00:33:03 +08003242 computeImeTarget(true /* updateImeTarget */);
Jorim Jaggif96c90a2018-09-26 16:55:15 +02003243 mInsetsStateController.getSourceProvider(TYPE_IME).setWindow(win,
3244 null /* frameProvider */);
lumark90120a82018-08-15 00:33:03 +08003245 }
3246
3247 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003248 * Determine and return the window that should be the IME target.
3249 * @param updateImeTarget If true the system IME target will be updated to match what we found.
3250 * @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 +00003251 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003252 WindowState computeImeTarget(boolean updateImeTarget) {
lumark90120a82018-08-15 00:33:03 +08003253 if (mInputMethodWindow == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003254 // There isn't an IME so there shouldn't be a target...That was easy!
3255 if (updateImeTarget) {
3256 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
lumarkff0ab692018-11-05 20:32:30 +08003257 + mInputMethodTarget + " to null since mInputMethodWindow is null");
3258 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003259 }
3260 return null;
3261 }
3262
lumarkff0ab692018-11-05 20:32:30 +08003263 final WindowState curTarget = mInputMethodTarget;
Chavi Weingarten3a748552018-05-14 17:32:42 +00003264 if (!canUpdateImeTarget()) {
3265 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
3266 return curTarget;
3267 }
3268
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003269 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
3270 // same display. Or even when the current IME/target are not on the same screen as the next
3271 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08003272 mUpdateImeTarget = updateImeTarget;
3273 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003274
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003275
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003276 // Yet more tricksyness! If this window is a "starting" window, we do actually want
3277 // to be on top of it, but it is not -really- where input will go. So look down below
3278 // for a real window to target...
3279 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
3280 final AppWindowToken token = target.mAppToken;
3281 if (token != null) {
3282 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
3283 if (betterTarget != null) {
3284 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003285 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003286 }
3287 }
3288
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003289 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
lumarkff0ab692018-11-05 20:32:30 +08003290 "Proposed new IME target: " + target + " for display: " + getDisplayId());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003291
chaviw1c13ad32018-06-12 16:44:23 -07003292 // Now, a special case -- if the last target's window is in the process of exiting, but
Tarandeep Singh2d0aca42019-03-28 14:08:58 -07003293 // not removed, keep on the last target to avoid IME flicker.
chaviw1c13ad32018-06-12 16:44:23 -07003294 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
Tarandeep Singh2d0aca42019-03-28 14:08:58 -07003295 && curTarget.isClosing()) {
3296 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Not changing target till current window is"
3297 + " closing and not removed");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003298 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003299 }
3300
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003301 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
3302 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003303
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003304 if (target == null) {
3305 if (updateImeTarget) {
3306 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
3307 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
3308 + Debug.getCallers(4) : ""));
lumarkff0ab692018-11-05 20:32:30 +08003309 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003310 }
3311
3312 return null;
3313 }
3314
3315 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003316 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
3317 if (token != null) {
3318
3319 // Now some fun for dealing with window animations that modify the Z order. We need
3320 // to look at all windows below the current target that are in this app, finding the
3321 // highest visible one in layering.
3322 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003323 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003324 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003325 }
3326
3327 if (highestTarget != null) {
lumark588a3e82018-07-20 18:53:54 +08003328 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, mAppTransition + " " + highestTarget
Jorim Jaggi35d328a2018-08-14 17:00:20 +02003329 + " animating=" + highestTarget.isAnimating());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003330
lumark588a3e82018-07-20 18:53:54 +08003331 if (mAppTransition.isTransitionSet()) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003332 // If we are currently setting up for an animation, hold everything until we
3333 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003334 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003335 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003336 }
3337 }
3338 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003339
3340 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
3341 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003342 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003343 }
3344
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003345 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003346 }
3347
lumarkff0ab692018-11-05 20:32:30 +08003348 /**
3349 * Calling {@link #computeImeTarget(boolean)} to update the input method target window in
3350 * the candidate app window token if needed.
3351 */
3352 void computeImeTargetIfNeeded(AppWindowToken candidate) {
3353 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == candidate) {
3354 computeImeTarget(true /* updateImeTarget */);
3355 }
3356 }
3357
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003358 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
lumarkff0ab692018-11-05 20:32:30 +08003359 if (target == mInputMethodTarget && mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003360 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003361 }
3362
lumarkff0ab692018-11-05 20:32:30 +08003363 mInputMethodTarget = target;
3364 mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003365 assignWindowLayers(false /* setLayoutNeeded */);
Tarandeep Singh215929b2019-01-11 18:24:37 -08003366 mInsetsStateController.onImeTargetChanged(target);
Tarandeep Singha6f35612019-01-11 19:50:46 -08003367 updateImeParent();
3368 }
3369
3370 private void updateImeParent() {
Jackal Guo818c3f12019-03-08 18:00:39 +08003371 // Force attaching IME to the display when magnifying, or it would be magnified with
3372 // target app together.
3373 final boolean shouldAttachToDisplay = (mMagnificationSpec != null);
3374 final SurfaceControl newParent =
3375 shouldAttachToDisplay ? mWindowingLayer : computeImeParent();
Tarandeep Singha6f35612019-01-11 19:50:46 -08003376 if (newParent != null) {
Tiger Huanged6794e2019-05-07 20:07:59 +08003377 getPendingTransaction().reparent(mImeWindowsContainers.mSurfaceControl, newParent);
Tarandeep Singha6f35612019-01-11 19:50:46 -08003378 scheduleAnimation();
3379 }
3380 }
3381
3382 /**
3383 * Computes the window the IME should be attached to.
3384 */
3385 @VisibleForTesting
3386 SurfaceControl computeImeParent() {
3387
3388 // Attach it to app if the target is part of an app and such app is covering the entire
3389 // screen. If it's not covering the entire screen the IME might extend beyond the apps
3390 // bounds.
Riddle Hsu6d6f67c2019-03-14 16:54:26 +08003391 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken != null
3392 && mInputMethodTarget.getWindowingMode() == WINDOWING_MODE_FULLSCREEN
3393 // An activity with override bounds should be letterboxed inside its parent bounds,
3394 // so it doesn't fill the screen.
3395 && mInputMethodTarget.mAppToken.matchParentBounds()) {
Tarandeep Singha6f35612019-01-11 19:50:46 -08003396 return mInputMethodTarget.mAppToken.getSurfaceControl();
3397 }
3398
3399 // Otherwise, we just attach it to the display.
3400 return mWindowingLayer;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003401 }
3402
3403 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
3404 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3405 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
3406 }
3407
3408 // Used to indicate we have reached the first window in the range we are interested in.
3409 mTmpWindow = null;
3410
3411 // TODO: Figure-out a more efficient way to do this.
3412 final WindowState candidate = getWindow(w -> {
3413 if (w == top) {
3414 // Reached the first window in the range we are interested in.
3415 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003416 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003417 if (mTmpWindow == null) {
3418 return false;
3419 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003420
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003421 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3422 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003423 }
3424 // If we reached the bottom of the range of windows we are considering,
3425 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003426 if (w == bottom) {
3427 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003428 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003429 return false;
3430 });
3431
3432 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003433 }
3434
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003435 void setLayoutNeeded() {
3436 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
3437 mLayoutNeeded = true;
3438 }
3439
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003440 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003441 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
3442 mLayoutNeeded = false;
3443 }
3444
3445 boolean isLayoutNeeded() {
3446 return mLayoutNeeded;
3447 }
3448
Wale Ogunwale02319a62016-09-26 15:21:22 -07003449 void dumpTokens(PrintWriter pw, boolean dumpAll) {
3450 if (mTokenMap.isEmpty()) {
3451 return;
3452 }
3453 pw.println(" Display #" + mDisplayId);
3454 final Iterator<WindowToken> it = mTokenMap.values().iterator();
3455 while (it.hasNext()) {
3456 final WindowToken token = it.next();
3457 pw.print(" ");
3458 pw.print(token);
3459 if (dumpAll) {
3460 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003461 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07003462 } else {
3463 pw.println();
3464 }
3465 }
lumark588a3e82018-07-20 18:53:54 +08003466
Evan Rosky2289ba12018-11-19 18:28:18 -08003467 if (!mOpeningApps.isEmpty() || !mClosingApps.isEmpty() || !mChangingApps.isEmpty()) {
lumark588a3e82018-07-20 18:53:54 +08003468 pw.println();
3469 if (mOpeningApps.size() > 0) {
3470 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
3471 }
3472 if (mClosingApps.size() > 0) {
3473 pw.print(" mClosingApps="); pw.println(mClosingApps);
3474 }
Evan Rosky2289ba12018-11-19 18:28:18 -08003475 if (mChangingApps.size() > 0) {
3476 pw.print(" mChangingApps="); pw.println(mChangingApps);
3477 }
lumark588a3e82018-07-20 18:53:54 +08003478 }
3479
3480 mUnknownAppVisibilityController.dump(pw, " ");
Wale Ogunwale02319a62016-09-26 15:21:22 -07003481 }
3482
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003483 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003484 final int[] index = new int[1];
3485 forAllWindows(w -> {
3486 final WindowStateAnimator wAnim = w.mWinAnimator;
3487 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
3488 index[0] = index[0] + 1;
3489 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003490 }
3491
Jorim Jaggife762342016-10-13 14:33:27 +02003492 /**
3493 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
3494 */
Issei Suzuki5609ccb2019-06-13 15:04:08 +02003495 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade,
3496 boolean subtle) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003497 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003498 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02003499 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
3500 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Issei Suzuki5609ccb2019-06-13 15:04:08 +02003501 w.startAnimation(policy.createHiddenByKeyguardExit(
3502 onWallpaper, goingToShade, subtle));
Jorim Jaggife762342016-10-13 14:33:27 +02003503 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003504 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02003505 }
3506
Wale Ogunwale494009b82016-10-21 09:01:38 -07003507 boolean checkWaitingForWindows() {
3508
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003509 mHaveBootMsg = false;
3510 mHaveApp = false;
3511 mHaveWallpaper = false;
3512 mHaveKeyguard = true;
3513
3514 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003515 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
3516 return true;
3517 }
3518 if (w.isDrawnLw()) {
3519 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003520 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003521 } else if (w.mAttrs.type == TYPE_APPLICATION
3522 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003523 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003524 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003525 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003526 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003527 mHaveKeyguard = mWmService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003528 }
3529 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003530 return false;
3531 });
3532
3533 if (visibleWindow != null) {
3534 // We have a visible window.
3535 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003536 }
3537
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003538 // if the wallpaper service is disabled on the device, we're never going to have
3539 // wallpaper, don't bother waiting for it
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003540 boolean wallpaperEnabled = mWmService.mContext.getResources().getBoolean(
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003541 com.android.internal.R.bool.config_enableWallpaperService)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003542 && mWmService.mContext.getResources().getBoolean(
Daichi Hironoc1432222018-03-29 13:06:07 +09003543 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003544 && !mWmService.mOnlyCore;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003545
Wale Ogunwale494009b82016-10-21 09:01:38 -07003546 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003547 "******** booted=" + mWmService.mSystemBooted
3548 + " msg=" + mWmService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003549 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
3550 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
3551 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003552
3553 // If we are turning on the screen to show the boot message, don't do it until the boot
3554 // message is actually displayed.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003555 if (!mWmService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003556 return true;
3557 }
3558
3559 // If we are turning on the screen after the boot is completed normally, don't do so until
3560 // we have the application and wallpaper.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003561 if (mWmService.mSystemBooted
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003562 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003563 return true;
3564 }
3565
3566 return false;
3567 }
3568
Jorim Jaggi8f520872018-08-14 17:00:20 +02003569 void updateWindowsForAnimator() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003570 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003571 }
3572
Jorim Jaggi8f520872018-08-14 17:00:20 +02003573 /**
3574 * Updates the {@link TaskStack#setAnimationBackground} for all windows.
3575 */
3576 void updateBackgroundForAnimator() {
Jorim Jaggi7156b652016-10-25 08:31:58 -07003577 resetAnimationBackgroundAnimator();
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003578 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003579 }
3580
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003581 boolean isInputMethodClientFocus(int uid, int pid) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003582 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003583 if (imFocus == null) {
3584 return false;
3585 }
3586
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003587 if (DEBUG_INPUT_METHOD) {
3588 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003589 Slog.i(TAG_WM, "Current focus: " + mCurrentFocus + " displayId=" + mDisplayId);
3590 Slog.i(TAG_WM, "Last focus: " + mLastFocus + " displayId=" + mDisplayId);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003591 }
3592
Wale Ogunwale494009b82016-10-21 09:01:38 -07003593 if (DEBUG_INPUT_METHOD) {
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003594 Slog.i(TAG_WM, "IM target uid/pid: " + imFocus.mSession.mUid
3595 + "/" + imFocus.mSession.mPid);
3596 Slog.i(TAG_WM, "Requesting client uid/pid: " + uid + "/" + pid);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003597 }
3598
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003599 return imFocus.mSession.mUid == uid && imFocus.mSession.mPid == pid;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003600 }
3601
3602 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003603 final WindowState win = getWindow(
3604 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
3605 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003606 }
3607
Tiger Huang7c610aa2018-10-27 00:01:01 +08003608 void statusBarVisibilityChanged(int visibility) {
3609 mLastStatusBarVisibility = visibility;
3610 visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(visibility);
3611 updateStatusBarVisibilityLocked(visibility);
3612 }
3613
3614 private boolean updateStatusBarVisibilityLocked(int visibility) {
3615 if (mLastDispatchedSystemUiVisibility == visibility) {
3616 return false;
3617 }
3618 final int globalDiff = (visibility ^ mLastDispatchedSystemUiVisibility)
3619 // We are only interested in differences of one of the
3620 // clearable flags...
3621 & View.SYSTEM_UI_CLEARABLE_FLAGS
3622 // ...if it has actually been cleared.
3623 & ~visibility;
3624
3625 mLastDispatchedSystemUiVisibility = visibility;
3626 if (isDefaultDisplay) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003627 mWmService.mInputManager.setSystemUiVisibility(visibility);
Tiger Huang7c610aa2018-10-27 00:01:01 +08003628 }
3629 updateSystemUiVisibility(visibility, globalDiff);
3630 return true;
3631 }
3632
Wale Ogunwale494009b82016-10-21 09:01:38 -07003633 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003634 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003635 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003636 final int curValue = w.mSystemUiVisibility;
3637 final int diff = (curValue ^ visibility) & globalDiff;
3638 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003639 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003640 w.mSeq++;
3641 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003642 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003643 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
3644 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07003645 visibility, newValue, diff);
3646 }
3647 } catch (RemoteException e) {
3648 // so sorry
3649 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003650 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003651 }
3652
Tiger Huang7c610aa2018-10-27 00:01:01 +08003653 void reevaluateStatusBarVisibility() {
3654 int visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(mLastStatusBarVisibility);
3655 if (updateStatusBarVisibilityLocked(visibility)) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003656 mWmService.mWindowPlacerLocked.requestTraversal();
Tiger Huang7c610aa2018-10-27 00:01:01 +08003657 }
3658 }
3659
Wale Ogunwale494009b82016-10-21 09:01:38 -07003660 void onWindowFreezeTimeout() {
3661 Slog.w(TAG_WM, "Window freeze timeout expired.");
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003662 mWmService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003663
3664 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07003665 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003666 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003667 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07003668 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003669 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003670 - mWmService.mDisplayFreezeTime);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003671 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003672 }, true /* traverseTopToBottom */);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003673 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003674 }
3675
3676 void waitForAllWindowsDrawn() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003677 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003678 forAllWindows(w -> {
3679 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
3680 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
3681 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003682 // Force add to mResizingWindows.
chaviw9c81e632018-07-31 11:17:52 -07003683 w.resetLastContentInsets();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003684 mWmService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003685 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003686 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003687 }
3688
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003689 // TODO: Super crazy long method that should be broken down...
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003690 void applySurfaceChangesTransaction(boolean recoveringMemory) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003691 final WindowSurfacePlacer surfacePlacer = mWmService.mWindowPlacerLocked;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003692
3693 mTmpUpdateAllDrawn.clear();
3694
3695 int repeats = 0;
3696 do {
3697 repeats++;
3698 if (repeats > 6) {
3699 Slog.w(TAG, "Animation repeat aborted after too many iterations");
3700 clearLayoutNeeded();
3701 break;
3702 }
3703
3704 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
3705 pendingLayoutChanges);
3706
wilsonshihc32538e2018-11-07 17:27:34 +08003707 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
3708 mWallpaperController.adjustWallpaperWindows();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003709 }
3710
Riddle Hsu654a6f92018-07-13 22:59:36 +08003711 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003712 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Riddle Hsu4e611772018-10-31 18:58:28 +08003713 if (updateOrientationFromAppTokens()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003714 setLayoutNeeded();
Riddle Hsu4e611772018-10-31 18:58:28 +08003715 sendNewConfiguration();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003716 }
3717 }
3718
3719 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3720 setLayoutNeeded();
3721 }
3722
3723 // FIRST LOOP: Perform a layout, if needed.
3724 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
3725 performLayout(repeats == 1, false /* updateInputWindows */);
3726 } else {
3727 Slog.w(TAG, "Layout repeat skipped after too many iterations");
3728 }
3729
3730 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
3731 pendingLayoutChanges = 0;
3732
Jorim Jaggi4981f152019-03-26 18:58:45 +01003733 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "applyPostLayoutPolicy");
3734 try {
3735 mDisplayPolicy.beginPostLayoutPolicyLw();
3736 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
3737 pendingLayoutChanges |= mDisplayPolicy.finishPostLayoutPolicyLw();
3738 } finally {
3739 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
3740 }
Tiger Huang7c610aa2018-10-27 00:01:01 +08003741 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
3742 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
Jorim Jaggif96c90a2018-09-26 16:55:15 +02003743 mInsetsStateController.onPostLayout();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003744 } while (pendingLayoutChanges != 0);
3745
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003746 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003747
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003748 mTmpRecoveringMemory = recoveringMemory;
Jorim Jaggi4981f152019-03-26 18:58:45 +01003749
3750 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "applyWindowSurfaceChanges");
3751 try {
3752 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
3753 } finally {
3754 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
3755 }
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01003756 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003757
Jorim Jaggif1292892018-09-10 11:58:13 +02003758 mLastHasContent = mTmpApplySurfaceChangesTransactionState.displayHasContent;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003759 mWmService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Jorim Jaggif1292892018-09-10 11:58:13 +02003760 mLastHasContent,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003761 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
3762 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003763 true /* inTraversal, must call performTraversalInTrans... below */);
3764
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003765 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
3766 if (wallpaperVisible != mLastWallpaperVisible) {
3767 mLastWallpaperVisible = wallpaperVisible;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003768 mWmService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003769 }
3770
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003771 while (!mTmpUpdateAllDrawn.isEmpty()) {
3772 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
3773 // See if any windows have been drawn, so they (and others associated with them)
3774 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07003775 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003776 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003777 }
3778
Bryce Leef3c6a472017-11-14 14:53:06 -08003779 private void updateBounds() {
Evan Rosky39b6f232018-10-30 18:35:41 -07003780 calculateBounds(mDisplayInfo, mTmpBounds);
Bryce Leef3c6a472017-11-14 14:53:06 -08003781 setBounds(mTmpBounds);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08003782 if (mPortalWindowHandle != null && mParentSurfaceControl != null) {
3783 mPortalWindowHandle.touchableRegion.getBounds(mTmpRect);
3784 if (!mTmpBounds.equals(mTmpRect)) {
3785 mPortalWindowHandle.touchableRegion.set(mTmpBounds);
Tiger Huanged6794e2019-05-07 20:07:59 +08003786 getPendingTransaction().setInputWindowInfo(
3787 mParentSurfaceControl, mPortalWindowHandle);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08003788 }
3789 }
Bryce Leef3c6a472017-11-14 14:53:06 -08003790 }
3791
3792 // Determines the current display bounds based on the current state
Evan Rosky39b6f232018-10-30 18:35:41 -07003793 private void calculateBounds(DisplayInfo displayInfo, Rect out) {
Bryce Leef3c6a472017-11-14 14:53:06 -08003794 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Evan Rosky39b6f232018-10-30 18:35:41 -07003795 final int rotation = displayInfo.rotation;
3796 boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Bryce Leef3c6a472017-11-14 14:53:06 -08003797 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
3798 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Evan Rosky39b6f232018-10-30 18:35:41 -07003799 int width = displayInfo.logicalWidth;
Bryce Leef3c6a472017-11-14 14:53:06 -08003800 int left = (physWidth - width) / 2;
Evan Rosky39b6f232018-10-30 18:35:41 -07003801 int height = displayInfo.logicalHeight;
Bryce Leef3c6a472017-11-14 14:53:06 -08003802 int top = (physHeight - height) / 2;
3803 out.set(left, top, left + width, top + height);
3804 }
3805
Bryce Leef3c6a472017-11-14 14:53:06 -08003806 private void getBounds(Rect out, int orientation) {
3807 getBounds(out);
3808
3809 // Rotate the Rect if needed.
3810 final int currentRotation = mDisplayInfo.rotation;
3811 final int rotationDelta = deltaRotation(currentRotation, orientation);
3812 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3813 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3814 mTmpRectF.set(out);
3815 mTmpMatrix.mapRect(mTmpRectF);
3816 mTmpRectF.round(out);
3817 }
3818 }
3819
Evan Rosky730f6e82018-12-03 17:40:11 -08003820 /** @returns the orientation of the display when it's rotation is ROTATION_0. */
3821 int getNaturalOrientation() {
3822 return mBaseDisplayWidth < mBaseDisplayHeight
3823 ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
3824 }
3825
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003826 void performLayout(boolean initial, boolean updateInputWindows) {
Jorim Jaggi4981f152019-03-26 18:58:45 +01003827 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "performLayout");
3828 try {
3829 performLayoutNoTrace(initial, updateInputWindows);
3830 } finally {
3831 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
3832 }
3833 }
3834
3835 private void performLayoutNoTrace(boolean initial, boolean updateInputWindows) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003836 if (!isLayoutNeeded()) {
3837 return;
3838 }
3839 clearLayoutNeeded();
3840
3841 final int dw = mDisplayInfo.logicalWidth;
3842 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003843 if (DEBUG_LAYOUT) {
3844 Slog.v(TAG, "-------------------------------------");
Jorim Jaggi4981f152019-03-26 18:58:45 +01003845 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw
3846 + " dh=" + dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003847 }
3848
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003849 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3850 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Jorim Jaggi4981f152019-03-26 18:58:45 +01003851 // TODO: Not sure if we really need to set the rotation here since we are updating from
3852 // the display info above...
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00003853 mDisplayFrames.mRotation = mRotation;
Tiger Huang7c610aa2018-10-27 00:01:01 +08003854 mDisplayPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003855
Adrian Roos5251b1d2018-03-23 18:57:43 +01003856 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003857 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003858 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003859
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003860 // Used to indicate that we have processed the dream window and all additional windows are
3861 // behind it.
3862 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003863 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003864
3865 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003866 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003867
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003868 // Used to indicate that we have processed the dream window and all additional attached
3869 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003870 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003871 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003872
3873 // Now perform layout of attached windows, which usually depend on the position of the
3874 // window they are attached to. XXX does not deal with windows that are attached to windows
3875 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003876 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003877
3878 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003879 mInputMonitor.layoutInputConsumers(dw, dh);
3880 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003881 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003882 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003883 }
3884
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003885 mWmService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003886 }
3887
3888 /**
3889 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3890 * In portrait mode, it grabs the full screenshot.
3891 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003892 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003893 */
chaviw0315a1a2018-03-05 15:28:35 -08003894 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003895 if (!mWmService.mPolicy.isScreenOn()) {
chaviw0315a1a2018-03-05 15:28:35 -08003896 if (DEBUG_SCREENSHOT) {
3897 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003898 }
chaviw0315a1a2018-03-05 15:28:35 -08003899 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003900 }
chaviwfbe47df2017-11-10 16:14:49 -08003901
chaviw0315a1a2018-03-05 15:28:35 -08003902 int dw = mDisplayInfo.logicalWidth;
3903 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003904
chaviw0315a1a2018-03-05 15:28:35 -08003905 if (dw <= 0 || dh <= 0) {
3906 return null;
3907 }
chaviwfbe47df2017-11-10 16:14:49 -08003908
chaviw0315a1a2018-03-05 15:28:35 -08003909 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003910
chaviw0315a1a2018-03-05 15:28:35 -08003911 // The screenshot API does not apply the current screen rotation.
3912 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003913
chaviw0315a1a2018-03-05 15:28:35 -08003914 if (rot == ROTATION_90 || rot == ROTATION_270) {
3915 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3916 }
chaviwfbe47df2017-11-10 16:14:49 -08003917
chaviw0315a1a2018-03-05 15:28:35 -08003918 // SurfaceFlinger is not aware of orientation, so convert our logical
3919 // crop to SurfaceFlinger's portrait orientation.
3920 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3921
3922 final ScreenRotationAnimation screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003923 mWmService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
chaviw0315a1a2018-03-05 15:28:35 -08003924 final boolean inRotation = screenRotationAnimation != null &&
3925 screenRotationAnimation.isAnimating();
3926 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3927
3928 // TODO(b/68392460): We should screenshot Task controls directly
3929 // but it's difficult at the moment as the Task doesn't have the
3930 // correct size set.
chaviw08520a02018-09-10 16:44:56 -07003931 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, inRotation, rot);
chaviw0315a1a2018-03-05 15:28:35 -08003932 if (bitmap == null) {
3933 Slog.w(TAG_WM, "Failed to take screenshot");
3934 return null;
3935 }
3936
3937 // Create a copy of the screenshot that is immutable and backed in ashmem.
3938 // This greatly reduces the overhead of passing the bitmap between processes.
3939 final Bitmap ret = bitmap.createAshmemBitmap(config);
3940 bitmap.recycle();
3941 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003942 }
3943
3944 // TODO: Can this use createRotationMatrix()?
3945 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3946 if (rot == Surface.ROTATION_90) {
3947 final int tmp = crop.top;
3948 crop.top = dw - crop.right;
3949 crop.right = crop.bottom;
3950 crop.bottom = dw - crop.left;
3951 crop.left = tmp;
3952 } else if (rot == Surface.ROTATION_180) {
3953 int tmp = crop.top;
3954 crop.top = dh - crop.bottom;
3955 crop.bottom = dh - tmp;
3956 tmp = crop.right;
3957 crop.right = dw - crop.left;
3958 crop.left = dw - tmp;
3959 } else if (rot == Surface.ROTATION_270) {
3960 final int tmp = crop.top;
3961 crop.top = crop.left;
3962 crop.left = dh - crop.bottom;
3963 crop.bottom = crop.right;
3964 crop.right = dh - tmp;
3965 }
3966 }
3967
3968 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003969 // Used to indicate the layout is needed.
3970 mTmpWindow = null;
3971
3972 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003973 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003974 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003975 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003976 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003977 w.setDisplayLayoutNeeded();
Vishnu Nairba183352018-11-21 11:16:49 -08003978 w.finishSeamlessRotation(true /* timeout */);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003979 mWmService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003980 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003981
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003982 if (mTmpWindow != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003983 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003984 }
3985 }
3986
Wale Ogunwale1666e312016-12-16 11:27:18 -08003987 void setExitingTokensHasVisible(boolean hasVisible) {
3988 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3989 mExitingTokens.get(i).hasVisible = hasVisible;
3990 }
3991
3992 // Initialize state of exiting applications.
3993 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3994 }
3995
3996 void removeExistingTokensIfPossible() {
3997 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3998 final WindowToken token = mExitingTokens.get(i);
3999 if (!token.hasVisible) {
4000 mExitingTokens.remove(i);
4001 }
4002 }
4003
4004 // Time to remove any exiting applications?
4005 mTaskStackContainers.removeExistingAppTokensIfPossible();
4006 }
4007
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07004008 @Override
4009 void onDescendantOverrideConfigurationChanged() {
4010 setLayoutNeeded();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004011 mWmService.requestTraversal();
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07004012 }
4013
David Stevens9440dc82017-03-16 19:00:20 -07004014 boolean okToDisplay() {
4015 if (mDisplayId == DEFAULT_DISPLAY) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004016 return !mWmService.mDisplayFrozen
4017 && mWmService.mDisplayEnabled && mWmService.mPolicy.isScreenOn();
David Stevens9440dc82017-03-16 19:00:20 -07004018 }
4019 return mDisplayInfo.state == Display.STATE_ON;
4020 }
4021
4022 boolean okToAnimate() {
4023 return okToDisplay() &&
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004024 (mDisplayId != DEFAULT_DISPLAY || mWmService.mPolicy.okToAnimate());
David Stevens9440dc82017-03-16 19:00:20 -07004025 }
4026
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07004027 static final class TaskForResizePointSearchResult {
4028 boolean searchDone;
4029 Task taskForResize;
4030
4031 void reset() {
4032 searchDone = false;
4033 taskForResize = null;
4034 }
4035 }
Robert Carr3b716242016-08-16 16:02:21 -07004036
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004037 private static final class ApplySurfaceChangesTransactionState {
4038 boolean displayHasContent;
4039 boolean obscured;
4040 boolean syswin;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004041 float preferredRefreshRate;
4042 int preferredModeId;
4043
4044 void reset() {
4045 displayHasContent = false;
4046 obscured = false;
4047 syswin = false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004048 preferredRefreshRate = 0;
4049 preferredModeId = 0;
4050 }
4051 }
4052
4053 private static final class ScreenshotApplicationState {
4054 WindowState appWin;
4055 int maxLayer;
4056 int minLayer;
4057 boolean screenshotReady;
4058
4059 void reset(boolean screenshotReady) {
4060 appWin = null;
4061 maxLayer = 0;
4062 minLayer = 0;
4063 this.screenshotReady = screenshotReady;
4064 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
4065 }
4066 }
4067
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004068 /**
4069 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
4070 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
4071 * homogeneous children type which is currently required by sub-classes of
4072 * {@link WindowContainer} class.
4073 */
4074 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
4075
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004076 DisplayChildWindowContainer(WindowManagerService service) {
4077 super(service);
4078 }
4079
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004080 @Override
4081 boolean fillsParent() {
4082 return true;
4083 }
4084
4085 @Override
4086 boolean isVisible() {
4087 return true;
Robert Carr3b716242016-08-16 16:02:21 -07004088 }
4089 }
4090
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004091 /**
4092 * Window container class that contains all containers on this display relating to Apps.
4093 * I.e Activities.
4094 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07004095 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07004096 /**
4097 * A control placed at the appropriate level for transitions to occur.
4098 */
chaviw23ee71c2017-12-18 11:29:41 -08004099 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02004100 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02004101 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004102
Robert Carrf7a7ca82017-12-06 13:17:07 -08004103 /**
4104 * Given that the split-screen divider does not have an AppWindowToken, it
4105 * will have to live inside of a "NonAppWindowContainer", in particular
4106 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
4107 * it will need to be interleaved with some of our children, appearing on top of
4108 * both docked stacks but underneath any assistant stacks.
4109 *
4110 * To solve this problem we have this anchor control, which will always exist so
4111 * we can always assign it the correct value in our {@link #assignChildLayers}.
4112 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
4113 * assign the divider a layer relative to it. This way we prevent linking lifecycle
4114 * events between the two containers.
4115 */
4116 SurfaceControl mSplitScreenDividerAnchor = null;
4117
Wale Ogunwale61911492017-10-11 08:50:50 -07004118 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
4119 // through the list to find them.
4120 private TaskStack mHomeStack = null;
4121 private TaskStack mPinnedStack = null;
4122 private TaskStack mSplitScreenPrimaryStack = null;
4123
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004124 TaskStackContainers(WindowManagerService service) {
4125 super(service);
4126 }
4127
Wale Ogunwale61911492017-10-11 08:50:50 -07004128 /**
4129 * Returns the topmost stack on the display that is compatible with the input windowing mode
4130 * and activity type. Null is no compatible stack on the display.
4131 */
4132 TaskStack getStack(int windowingMode, int activityType) {
4133 if (activityType == ACTIVITY_TYPE_HOME) {
4134 return mHomeStack;
4135 }
4136 if (windowingMode == WINDOWING_MODE_PINNED) {
4137 return mPinnedStack;
4138 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4139 return mSplitScreenPrimaryStack;
4140 }
4141 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
4142 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004143 if (activityType == ACTIVITY_TYPE_UNDEFINED
4144 && windowingMode == stack.getWindowingMode()) {
4145 // Passing in undefined type means we want to match the topmost stack with the
4146 // windowing mode.
4147 return stack;
4148 }
Wale Ogunwale61911492017-10-11 08:50:50 -07004149 if (stack.isCompatible(windowingMode, activityType)) {
4150 return stack;
4151 }
4152 }
4153 return null;
4154 }
4155
4156 @VisibleForTesting
4157 TaskStack getTopStack() {
4158 return mTaskStackContainers.getChildCount() > 0
4159 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
4160 }
4161
4162 TaskStack getHomeStack() {
4163 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
4164 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
4165 }
4166 return mHomeStack;
4167 }
4168
4169 TaskStack getPinnedStack() {
4170 return mPinnedStack;
4171 }
4172
Matthew Ng64e77cf2017-10-31 14:01:31 -07004173 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07004174 return mSplitScreenPrimaryStack;
4175 }
4176
Winson Chunge2d72172018-01-25 17:46:20 +00004177 ArrayList<Task> getVisibleTasks() {
4178 final ArrayList<Task> visibleTasks = new ArrayList<>();
4179 forAllTasks(task -> {
4180 if (task.isVisible()) {
4181 visibleTasks.add(task);
4182 }
4183 });
4184 return visibleTasks;
4185 }
4186
Andrii Kulian839def92016-11-02 10:58:58 -07004187 /**
4188 * Adds the stack to this container.
Andrii Kulian839def92016-11-02 10:58:58 -07004189 */
4190 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07004191 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004192 addChild(stack, onTop);
4193 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07004194 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004195
Wale Ogunwale61911492017-10-11 08:50:50 -07004196 void onStackWindowingModeChanged(TaskStack stack) {
4197 removeStackReferenceIfNeeded(stack);
4198 addStackReferenceIfNeeded(stack);
4199 if (stack == mPinnedStack && getTopStack() != stack) {
4200 // Looks like this stack changed windowing mode to pinned. Move it to the top.
4201 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
4202 }
4203 }
4204
4205 private void addStackReferenceIfNeeded(TaskStack stack) {
4206 if (stack.isActivityTypeHome()) {
4207 if (mHomeStack != null) {
Louis Chang7d0037c2018-08-13 12:42:06 +08004208 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07004209 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang7d0037c2018-08-13 12:42:06 +08004210
Wale Ogunwale61911492017-10-11 08:50:50 -07004211 }
Louis Chang7d0037c2018-08-13 12:42:06 +08004212 mHomeStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07004213 }
4214 final int windowingMode = stack.getWindowingMode();
4215 if (windowingMode == WINDOWING_MODE_PINNED) {
4216 if (mPinnedStack != null) {
4217 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
4218 + mPinnedStack + " already exist on display=" + this
4219 + " stack=" + stack);
4220 }
4221 mPinnedStack = stack;
4222 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4223 if (mSplitScreenPrimaryStack != null) {
4224 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
4225 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
4226 + " already exist on display=" + this + " stack=" + stack);
4227 }
4228 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004229 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07004230 }
4231 }
4232
4233 private void removeStackReferenceIfNeeded(TaskStack stack) {
4234 if (stack == mHomeStack) {
4235 mHomeStack = null;
4236 } else if (stack == mPinnedStack) {
4237 mPinnedStack = null;
4238 } else if (stack == mSplitScreenPrimaryStack) {
4239 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004240 // Re-set the split-screen create mode whenever the split-screen stack is removed.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004241 mWmService.setDockedStackCreateStateLocked(
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004242 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
4243 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07004244 }
Andrii Kulian839def92016-11-02 10:58:58 -07004245 }
4246
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004247 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004248 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
4249 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004250 addChild(stack, addIndex);
4251 setLayoutNeeded();
4252 }
4253
Wale Ogunwale61911492017-10-11 08:50:50 -07004254 @Override
4255 protected void removeChild(TaskStack stack) {
4256 super.removeChild(stack);
4257 removeStackReferenceIfNeeded(stack);
4258 }
Bryce Lee00d586d2017-07-28 20:48:43 -07004259
4260 @Override
4261 boolean isOnTop() {
4262 // Considered always on top
4263 return true;
4264 }
4265
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004266 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08004267 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07004268 if (child.getWindowConfiguration().isAlwaysOnTop()
4269 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08004270 // This stack is always-on-top, override the default behavior.
4271 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
4272
4273 // Moving to its current position, as we must call super but we don't want to
4274 // perform any meaningful action.
4275 final int currentPosition = mChildren.indexOf(child);
4276 super.positionChildAt(currentPosition, child, false /* includingParents */);
4277 return;
4278 }
4279
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004280 final int targetPosition = findPositionForStack(position, child, false /* adding */);
4281 super.positionChildAt(targetPosition, child, includingParents);
4282
Riddle Hsu2f6e1742018-08-23 22:44:36 +08004283 if (includingParents) {
4284 // We still want to move the display of this stack container to top because even the
4285 // target position is adjusted to non-top, the intention of the condition is to have
4286 // higher z-order to gain focus (e.g. moving a task of a fullscreen stack to front
4287 // in a non-top display which is using picture-in-picture mode).
4288 final int topChildPosition = getChildCount() - 1;
4289 if (targetPosition < topChildPosition && position >= topChildPosition) {
4290 getParent().positionChildAt(POSITION_TOP, this /* child */,
4291 true /* includingParents */);
4292 }
4293 }
4294
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004295 setLayoutNeeded();
4296 }
4297
4298 /**
4299 * When stack is added or repositioned, find a proper position for it.
4300 * This will make sure that pinned stack always stays on top.
4301 * @param requestedPosition Position requested by caller.
4302 * @param stack Stack to be added or positioned.
4303 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
4304 * @return The proper position for the stack.
4305 */
4306 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004307 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09004308 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09004309 }
4310
Kazuki Takisef85197b2018-06-18 18:18:36 +09004311 final int topChildPosition = mChildren.size() - 1;
4312 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
4313 for (int i = topChildPosition; i >= 0; --i) {
4314 if (getStacks().get(i) != stack && !getStacks().get(i).isAlwaysOnTop()) {
4315 belowAlwaysOnTopPosition = i;
4316 break;
4317 }
4318 }
4319
4320 // The max possible position we can insert the stack at.
4321 int maxPosition = POSITION_TOP;
4322 // The min possible position we can insert the stack at.
4323 int minPosition = POSITION_BOTTOM;
4324
4325 if (stack.isAlwaysOnTop()) {
4326 if (hasPinnedStack()) {
4327 // Always-on-top stacks go below the pinned stack.
4328 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
4329 }
4330 // Always-on-top stacks need to be above all other stacks.
4331 minPosition = belowAlwaysOnTopPosition !=
4332 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
4333 } else {
4334 // Other stacks need to be below the always-on-top stacks.
4335 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08004336 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09004337 }
4338
4339 int targetPosition = requestedPosition;
4340 targetPosition = Math.min(targetPosition, maxPosition);
4341 targetPosition = Math.max(targetPosition, minPosition);
4342
4343 int prevPosition = getStacks().indexOf(stack);
4344 // The positions we calculated above (maxPosition, minPosition) do not take into
4345 // consideration the following edge cases.
4346 // 1) We need to adjust the position depending on the value "adding".
4347 // 2) When we are moving a stack to another position, we also need to adjust the
4348 // position depending on whether the stack is moving to a higher or lower position.
4349 if ((targetPosition != requestedPosition) &&
4350 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004351 targetPosition++;
4352 }
4353
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004354 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08004355 }
4356
4357 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004358 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
4359 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004360 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004361 if (super.forAllWindows(callback, traverseTopToBottom)) {
4362 return true;
4363 }
4364 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4365 return true;
4366 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004367 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004368 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4369 return true;
4370 }
4371 if (super.forAllWindows(callback, traverseTopToBottom)) {
4372 return true;
4373 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004374 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004375 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004376 }
4377
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004378 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004379 boolean traverseTopToBottom) {
4380 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
4381 // app tokens.
4382 // TODO: Investigate if we need to continue to do this or if we can just process them
4383 // in-order.
4384 if (traverseTopToBottom) {
4385 for (int i = mChildren.size() - 1; i >= 0; --i) {
4386 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4387 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004388 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4389 traverseTopToBottom)) {
4390 return true;
4391 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004392 }
4393 }
4394 } else {
4395 final int count = mChildren.size();
4396 for (int i = 0; i < count; ++i) {
4397 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4398 final int appTokensCount = appTokens.size();
4399 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004400 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4401 traverseTopToBottom)) {
4402 return true;
4403 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004404 }
4405 }
4406 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004407 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004408 }
4409
Wale Ogunwale1666e312016-12-16 11:27:18 -08004410 void setExitingTokensHasVisible(boolean hasVisible) {
4411 for (int i = mChildren.size() - 1; i >= 0; --i) {
4412 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4413 for (int j = appTokens.size() - 1; j >= 0; --j) {
4414 appTokens.get(j).hasVisible = hasVisible;
4415 }
4416 }
4417 }
4418
4419 void removeExistingAppTokensIfPossible() {
4420 for (int i = mChildren.size() - 1; i >= 0; --i) {
4421 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4422 for (int j = appTokens.size() - 1; j >= 0; --j) {
4423 final AppWindowToken token = appTokens.get(j);
lumark588a3e82018-07-20 18:53:54 +08004424 if (!token.hasVisible && !mClosingApps.contains(token)
Wale Ogunwale1666e312016-12-16 11:27:18 -08004425 && (!token.mIsExiting || token.isEmpty())) {
4426 // Make sure there is no animation running on this token, so any windows
4427 // associated with it will be removed as soon as their animations are
4428 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02004429 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004430 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
4431 "performLayout: App token exiting now removed" + token);
4432 token.removeIfPossible();
4433 }
4434 }
4435 }
4436 }
4437
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004438 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004439 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004440 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
4441 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004442 // Apps and their containers are not allowed to specify an orientation while the
Wale Ogunwaled0c225c2018-06-15 14:47:46 -07004443 // docked or freeform stack is visible...except for the home stack if the docked
4444 // stack is minimized and it actually set something and the bounds is different from
4445 // the display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004446 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004447 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004448 && !(mDividerControllerLocked.isHomeStackResizable()
4449 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004450 final int orientation = mHomeStack.getOrientation();
4451 if (orientation != SCREEN_ORIENTATION_UNSET) {
4452 return orientation;
4453 }
4454 }
4455 return SCREEN_ORIENTATION_UNSPECIFIED;
4456 }
4457
4458 final int orientation = super.getOrientation();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004459 boolean isCar = mWmService.mContext.getPackageManager().hasSystemFeature(
Dean Harding3e5a1522017-10-06 09:19:01 -07004460 PackageManager.FEATURE_AUTOMOTIVE);
4461 if (isCar) {
4462 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
4463 // allow anything but the default orientation.
4464 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004465 "Forcing UNSPECIFIED orientation in car for display id=" + mDisplayId
4466 + ". Ignoring " + orientation);
Dean Harding3e5a1522017-10-06 09:19:01 -07004467 return SCREEN_ORIENTATION_UNSPECIFIED;
4468 }
4469
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004470 if (orientation != SCREEN_ORIENTATION_UNSET
4471 && orientation != SCREEN_ORIENTATION_BEHIND) {
4472 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004473 "App is requesting an orientation, return " + orientation
4474 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004475 return orientation;
4476 }
4477
4478 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004479 "No app is requesting an orientation, return " + mLastOrientation
4480 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004481 // The next app has not been requested to be visible, so we keep the current orientation
4482 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08004483 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004484 }
Robert Carrb1579c82017-09-05 14:54:47 -07004485
4486 @Override
4487 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004488 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004489
Robert Carr2f8aa392018-01-31 14:46:51 -08004490 for (int i = 0; i < mChildren.size(); i++) {
4491 final TaskStack s = mChildren.get(i);
4492 s.assignChildLayers(t);
4493 }
4494 }
4495
4496 void assignStackOrdering(SurfaceControl.Transaction t) {
Jorim Jaggibe418292018-03-26 16:14:12 +02004497
Robert Carrf7a7ca82017-12-06 13:17:07 -08004498 final int HOME_STACK_STATE = 0;
4499 final int NORMAL_STACK_STATE = 1;
4500 final int ALWAYS_ON_TOP_STATE = 2;
4501
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004502 int layer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004503 int layerForAnimationLayer = 0;
Jorim Jaggibe418292018-03-26 16:14:12 +02004504 int layerForBoostedAnimationLayer = 0;
Jorim Jaggi391790622018-04-18 15:30:44 +02004505 int layerForHomeAnimationLayer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004506
Robert Carrf7a7ca82017-12-06 13:17:07 -08004507 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
4508 for (int i = 0; i < mChildren.size(); i++) {
4509 final TaskStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08004510 if (state == HOME_STACK_STATE && !s.isActivityTypeHome()) {
4511 continue;
4512 } else if (state == NORMAL_STACK_STATE && (s.isActivityTypeHome()
4513 || s.isAlwaysOnTop())) {
4514 continue;
4515 } else if (state == ALWAYS_ON_TOP_STATE && !s.isAlwaysOnTop()) {
4516 continue;
4517 }
4518 s.assignLayer(t, layer++);
4519 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
4520 t.setLayer(mSplitScreenDividerAnchor, layer++);
4521 }
Winson Chungd41f71d2018-03-16 15:26:07 -07004522 if ((s.isTaskAnimating() || s.isAppAnimating())
4523 && state != ALWAYS_ON_TOP_STATE) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004524 // Ensure the animation layer ends up above the
4525 // highest animating stack and no higher.
4526 layerForAnimationLayer = layer++;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004527 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004528 if (state != ALWAYS_ON_TOP_STATE) {
4529 layerForBoostedAnimationLayer = layer++;
4530 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08004531 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004532 if (state == HOME_STACK_STATE) {
4533 layerForHomeAnimationLayer = layer++;
4534 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004535 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004536 if (mAppAnimationLayer != null) {
4537 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004538 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004539 if (mBoostedAppAnimationLayer != null) {
4540 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
4541 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004542 if (mHomeAppAnimationLayer != null) {
4543 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
4544 }
Robert Carrb1579c82017-09-05 14:54:47 -07004545 }
4546
4547 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02004548 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
4549 switch (animationLayer) {
4550 case ANIMATION_LAYER_BOOSTED:
4551 return mBoostedAppAnimationLayer;
4552 case ANIMATION_LAYER_HOME:
4553 return mHomeAppAnimationLayer;
4554 case ANIMATION_LAYER_STANDARD:
4555 default:
4556 return mAppAnimationLayer;
4557 }
chaviw23ee71c2017-12-18 11:29:41 -08004558 }
4559
Robert Carrf7a7ca82017-12-06 13:17:07 -08004560 SurfaceControl getSplitScreenDividerAnchor() {
4561 return mSplitScreenDividerAnchor;
4562 }
4563
chaviw23ee71c2017-12-18 11:29:41 -08004564 @Override
Riddle Hsu3a4bb612019-01-31 00:02:22 +08004565 void onParentChanged() {
4566 super.onParentChanged();
Robert Carrb1579c82017-09-05 14:54:47 -07004567 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08004568 mAppAnimationLayer = makeChildSurface(null)
4569 .setName("animationLayer")
4570 .build();
Jorim Jaggibe418292018-03-26 16:14:12 +02004571 mBoostedAppAnimationLayer = makeChildSurface(null)
4572 .setName("boostedAnimationLayer")
4573 .build();
Jorim Jaggi391790622018-04-18 15:30:44 +02004574 mHomeAppAnimationLayer = makeChildSurface(null)
4575 .setName("homeAnimationLayer")
4576 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08004577 mSplitScreenDividerAnchor = makeChildSurface(null)
4578 .setName("splitScreenDividerAnchor")
4579 .build();
4580 getPendingTransaction()
4581 .show(mAppAnimationLayer)
Jorim Jaggibe418292018-03-26 16:14:12 +02004582 .show(mBoostedAppAnimationLayer)
Jorim Jaggi391790622018-04-18 15:30:44 +02004583 .show(mHomeAppAnimationLayer)
Robert Carrf7a7ca82017-12-06 13:17:07 -08004584 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08004585 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07004586 } else {
Robert Carr5ea304d2019-02-04 16:04:55 -08004587 mAppAnimationLayer.remove();
chaviw23ee71c2017-12-18 11:29:41 -08004588 mAppAnimationLayer = null;
Robert Carr5ea304d2019-02-04 16:04:55 -08004589 mBoostedAppAnimationLayer.remove();
Jorim Jaggibe418292018-03-26 16:14:12 +02004590 mBoostedAppAnimationLayer = null;
Robert Carr5ea304d2019-02-04 16:04:55 -08004591 mHomeAppAnimationLayer.remove();
Jorim Jaggi391790622018-04-18 15:30:44 +02004592 mHomeAppAnimationLayer = null;
Robert Carr5ea304d2019-02-04 16:04:55 -08004593 mSplitScreenDividerAnchor.remove();
Robert Carrf7a7ca82017-12-06 13:17:07 -08004594 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07004595 }
4596 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004597 }
4598
Robert Carree4d4b92017-11-22 12:21:46 -08004599 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004600 AboveAppWindowContainers(String name, WindowManagerService service) {
4601 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08004602 }
4603
Robert Carrb9506032018-02-13 13:54:00 -08004604 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07004605 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
4606 final SurfaceControl.Builder builder = super.makeChildSurface(child);
4607 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
4608 // To draw above the ColorFade layer during the screen off transition, the
4609 // rounded corner overlays need to be at the root of the surface hierarchy.
4610 // TODO: move the ColorLayer into the display overlay layer such that this is not
4611 // necessary anymore.
4612 builder.setParent(null);
4613 }
4614 return builder;
4615 }
4616
4617 @Override
Robert Carrb9506032018-02-13 13:54:00 -08004618 void assignChildLayers(SurfaceControl.Transaction t) {
4619 assignChildLayers(t, null /* imeContainer */);
4620 }
4621
Robert Carree4d4b92017-11-22 12:21:46 -08004622 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
4623 boolean needAssignIme = imeContainer != null
4624 && imeContainer.getSurfaceControl() != null;
4625 for (int j = 0; j < mChildren.size(); ++j) {
4626 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004627
4628 // See {@link mSplitScreenDividerAnchor}
4629 if (wt.windowType == TYPE_DOCK_DIVIDER) {
4630 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
4631 continue;
4632 }
Vishnu Nair83537a72018-07-19 21:27:48 -07004633 if (wt.mRoundedCornerOverlay) {
4634 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
4635 continue;
4636 }
Robert Carree4d4b92017-11-22 12:21:46 -08004637 wt.assignLayer(t, j);
4638 wt.assignChildLayers(t);
4639
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004640 int layer = mWmService.mPolicy.getWindowLayerFromTypeLw(
Robert Carree4d4b92017-11-22 12:21:46 -08004641 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08004642
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004643 if (needAssignIme && layer >= mWmService.mPolicy.getWindowLayerFromTypeLw(
Robert Carrb8e78fa2017-11-28 13:07:58 -08004644 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004645 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08004646 needAssignIme = false;
4647 }
4648 }
4649 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004650 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08004651 }
4652 }
4653 }
4654
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004655 /**
4656 * Window container class that contains all containers on this display that are not related to
4657 * Apps. E.g. status bar.
4658 */
Robert Carree4d4b92017-11-22 12:21:46 -08004659 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07004660 /**
4661 * Compares two child window tokens returns -1 if the first is lesser than the second in
4662 * terms of z-order and 1 otherwise.
4663 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004664 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07004665 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004666 mWmService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004667 token1.mOwnerCanManageAppTokens)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004668 < mWmService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004669 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004670
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004671 private final Predicate<WindowState> mGetOrientingWindow = w -> {
chaviw15ad49f2019-04-24 15:05:39 -07004672 if (!w.isVisibleLw() || !w.mLegacyPolicyVisibilityAfterAnim) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004673 return false;
4674 }
4675 final int req = w.mAttrs.screenOrientation;
4676 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
4677 || req == SCREEN_ORIENTATION_UNSET) {
4678 return false;
4679 }
4680 return true;
4681 };
4682
Wale Ogunwale3a931692016-11-02 16:49:48 -07004683 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08004684 private final Dimmer mDimmer = new Dimmer(this);
4685 private final Rect mTmpDimBoundsRect = new Rect();
4686
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004687 NonAppWindowContainers(String name, WindowManagerService service) {
4688 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004689 mName = name;
4690 }
4691
4692 void addChild(WindowToken token) {
4693 addChild(token, mWindowComparator);
4694 }
4695
4696 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004697 int getOrientation() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004698 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004699 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004700 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004701
4702 if (win != null) {
4703 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004704 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004705 mLastKeyguardForcedOrientation = req;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004706 if (mWmService.mKeyguardGoingAway) {
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07004707 // Keyguard can't affect the orientation if it is going away...
4708 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
4709 return SCREEN_ORIENTATION_UNSET;
4710 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004711 }
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004712 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req
4713 + " for display id=" + mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08004714 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004715 }
4716
Andrii Kulian8ee72852017-03-10 10:36:45 -08004717 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004718
Jorim Jaggi1c530592018-04-06 15:11:47 +02004719 // Only allow force setting the orientation when all unknown visibilities have been
4720 // resolved, as otherwise we just may be starting another occluding activity.
4721 final boolean isUnoccluding =
lumark588a3e82018-07-20 18:53:54 +08004722 mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
4723 && mUnknownAppVisibilityController.allResolved();
Jorim Jaggi1c530592018-04-06 15:11:47 +02004724 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004725 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004726 }
4727
4728 return SCREEN_ORIENTATION_UNSET;
4729 }
4730
4731 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07004732 String getName() {
4733 return mName;
4734 }
chaviwf29223e2018-01-31 13:23:51 -08004735
4736 @Override
4737 Dimmer getDimmer() {
4738 return mDimmer;
4739 }
4740
4741 @Override
4742 void prepareSurfaces() {
4743 mDimmer.resetDimStates();
4744 super.prepareSurfaces();
4745 getBounds(mTmpDimBoundsRect);
4746
4747 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
4748 scheduleAnimation();
4749 }
4750 }
Robert Carr3b716242016-08-16 16:02:21 -07004751 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01004752
Robert Carrb1579c82017-09-05 14:54:47 -07004753 SurfaceControl.Builder makeSurface(SurfaceSession s) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004754 return mWmService.makeSurfaceBuilder(s)
Robert Carrb1579c82017-09-05 14:54:47 -07004755 .setParent(mWindowingLayer);
4756 }
4757
4758 @Override
4759 SurfaceSession getSession() {
4760 return mSession;
4761 }
4762
4763 @Override
4764 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07004765 SurfaceSession s = child != null ? child.getSession() : getSession();
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +00004766 final SurfaceControl.Builder b = mWmService.makeSurfaceBuilder(s).setContainerLayer();
Robert Carrf59b8dd2017-10-02 18:58:36 -07004767 if (child == null) {
4768 return b;
4769 }
4770
Robert Carree4d4b92017-11-22 12:21:46 -08004771 return b.setName(child.getName())
4772 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004773 }
4774
4775 /**
4776 * The makeSurface variants are for use by the window-container
4777 * hierarchy. makeOverlay here is a function for various non windowing
4778 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
4779 * and other potpourii.
4780 */
4781 SurfaceControl.Builder makeOverlay() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004782 return mWmService.makeSurfaceBuilder(mSession)
Robert Carrb1579c82017-09-05 14:54:47 -07004783 .setParent(mOverlayLayer);
4784 }
4785
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004786 /**
4787 * Reparents the given surface to mOverlayLayer.
4788 */
4789 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
Robert Carr10584fa2019-01-14 15:55:19 -08004790 transaction.reparent(surface, mOverlayLayer);
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004791 }
4792
Robert Carrb1579c82017-09-05 14:54:47 -07004793 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07004794 if (spec.scale != 1.0) {
4795 mMagnificationSpec = spec;
4796 } else {
4797 mMagnificationSpec = null;
4798 }
Jackal Guo818c3f12019-03-08 18:00:39 +08004799 // Re-parent IME's SurfaceControl when MagnificationSpec changed.
4800 updateImeParent();
Robert Carr24be9ab2018-04-30 17:54:53 -07004801
Robert Carrf59b8dd2017-10-02 18:58:36 -07004802 applyMagnificationSpec(getPendingTransaction(), spec);
4803 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07004804 }
4805
Robert Carr24be9ab2018-04-30 17:54:53 -07004806 void reapplyMagnificationSpec() {
4807 if (mMagnificationSpec != null) {
4808 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4809 }
4810 }
4811
Robert Carrb1579c82017-09-05 14:54:47 -07004812 @Override
Riddle Hsu3a4bb612019-01-31 00:02:22 +08004813 void onParentChanged() {
Robert Carrb1579c82017-09-05 14:54:47 -07004814 // Since we are the top of the SurfaceControl hierarchy here
4815 // we create the root surfaces explicitly rather than chaining
Riddle Hsu3a4bb612019-01-31 00:02:22 +08004816 // up as the default implementation in onParentChanged does. So we
Robert Carrb1579c82017-09-05 14:54:47 -07004817 // explicitly do NOT call super here.
4818 }
4819
4820 @Override
4821 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004822
4823 // These are layers as children of "mWindowingLayer"
4824 mBelowAppWindowsContainers.assignLayer(t, 0);
4825 mTaskStackContainers.assignLayer(t, 1);
4826 mAboveAppWindowsContainers.assignLayer(t, 2);
4827
lumarkff0ab692018-11-05 20:32:30 +08004828 final WindowState imeTarget = mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004829 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004830
Robert Carree4d4b92017-11-22 12:21:46 -08004831 // In the case where we have an IME target that is not in split-screen
4832 // mode IME assignment is easy. We just need the IME to go directly above
4833 // the target. This way children of the target will naturally go above the IME
4834 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004835 //
Robert Carree4d4b92017-11-22 12:21:46 -08004836 // In the case of split-screen windowing mode, we need to elevate the IME above the
4837 // docked divider while keeping the app itself below the docked divider, so instead
4838 // we use relative layering of the IME targets child windows, and place the
4839 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004840 //
Robert Carr234b6332018-03-20 13:38:16 -07004841 // In the case the IME target is animating, the animation Z order may be different
4842 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4843 // IME target. In this case we just layer the IME over all transitions by placing it in the
4844 // above applications layer.
4845 //
Robert Carree4d4b92017-11-22 12:21:46 -08004846 // In the case where we have no IME target we assign it where it's base layer would
4847 // place it in the AboveAppWindowContainers.
Robert Carr234b6332018-03-20 13:38:16 -07004848 if (imeTarget != null && !(imeTarget.inSplitScreenWindowingMode()
4849 || imeTarget.mToken.isAppAnimating())
Robert Carree4d4b92017-11-22 12:21:46 -08004850 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004851 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004852 // TODO: We need to use an extra level on the app surface to ensure
4853 // this is always above SurfaceView but always below attached window.
4854 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004855 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004856 }
4857
4858 // Above we have assigned layers to our children, now we ask them to assign
4859 // layers to their children.
4860 mBelowAppWindowsContainers.assignChildLayers(t);
4861 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004862 mAboveAppWindowsContainers.assignChildLayers(t,
4863 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004864 mImeWindowsContainers.assignChildLayers(t);
4865 }
4866
4867 /**
4868 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4869 * that the IME target is one of the docked applications. We'd like the docked divider to be
4870 * above both of the applications, and we'd like the IME to be above the docked divider.
4871 * However we need child windows of the applications to be above the IME (Text drag handles).
4872 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4873 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
Tiger Huang7c610aa2018-10-27 00:01:01 +08004874 * with {@link WindowState#assignLayer}
Robert Carrb1579c82017-09-05 14:54:47 -07004875 */
4876 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004877 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004878 }
4879
4880 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004881 void prepareSurfaces() {
Jorim Jaggi4981f152019-03-26 18:58:45 +01004882 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "prepareSurfaces");
4883 try {
4884 final ScreenRotationAnimation screenRotationAnimation =
4885 mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Tiger Huanged6794e2019-05-07 20:07:59 +08004886 final Transaction transaction = getPendingTransaction();
Jorim Jaggi4981f152019-03-26 18:58:45 +01004887 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
4888 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
Tiger Huanged6794e2019-05-07 20:07:59 +08004889 transaction.setMatrix(mWindowingLayer,
Jorim Jaggi4981f152019-03-26 18:58:45 +01004890 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
4891 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
Tiger Huanged6794e2019-05-07 20:07:59 +08004892 transaction.setPosition(mWindowingLayer,
Jorim Jaggi4981f152019-03-26 18:58:45 +01004893 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
Tiger Huanged6794e2019-05-07 20:07:59 +08004894 transaction.setAlpha(mWindowingLayer,
Jorim Jaggi4981f152019-03-26 18:58:45 +01004895 screenRotationAnimation.getEnterTransformation().getAlpha());
4896 }
Chavi Weingartenb736e322018-02-23 00:27:54 +00004897
Jorim Jaggi4981f152019-03-26 18:58:45 +01004898 super.prepareSurfaces();
Tiger Huanged6794e2019-05-07 20:07:59 +08004899
4900 // TODO: Once we totally eliminate global transaction we will pass transaction in here
4901 // rather than merging to global.
4902 SurfaceControl.mergeToGlobalTransaction(transaction);
Jorim Jaggi4981f152019-03-26 18:58:45 +01004903 } finally {
4904 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
4905 }
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004906 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004907
Jorim Jaggibe418292018-03-26 16:14:12 +02004908 void assignStackOrdering() {
4909 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004910 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004911
4912 /**
4913 * Increment the deferral count to determine whether to update the IME target.
4914 */
4915 void deferUpdateImeTarget() {
4916 mDeferUpdateImeTargetCount++;
4917 }
4918
4919 /**
4920 * Decrement the deferral count to determine whether to update the IME target. If the count
4921 * reaches 0, a new ime target will get computed.
4922 */
4923 void continueUpdateImeTarget() {
4924 if (mDeferUpdateImeTargetCount == 0) {
4925 return;
4926 }
4927
4928 mDeferUpdateImeTargetCount--;
4929 if (mDeferUpdateImeTargetCount == 0) {
4930 computeImeTarget(true /* updateImeTarget */);
4931 }
4932 }
4933
4934 /**
4935 * @return Whether a new IME target should be computed.
4936 */
4937 private boolean canUpdateImeTarget() {
4938 return mDeferUpdateImeTargetCount == 0;
4939 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004940
4941 InputMonitor getInputMonitor() {
4942 return mInputMonitor;
4943 }
Jorim Jaggif1292892018-09-10 11:58:13 +02004944
4945 /**
4946 * @return Cached value whether we told display manager that we have content.
4947 */
4948 boolean getLastHasContent() {
4949 return mLastHasContent;
4950 }
Arthur Hungbe5ce212018-09-13 18:41:56 +08004951
4952 void registerPointerEventListener(@NonNull PointerEventListener listener) {
Riddle Hsu2588ab02019-02-25 14:23:56 +08004953 mPointerEventDispatcher.registerInputEventListener(listener);
Arthur Hungbe5ce212018-09-13 18:41:56 +08004954 }
4955
4956 void unregisterPointerEventListener(@NonNull PointerEventListener listener) {
Riddle Hsu2588ab02019-02-25 14:23:56 +08004957 mPointerEventDispatcher.unregisterInputEventListener(listener);
Arthur Hungbe5ce212018-09-13 18:41:56 +08004958 }
lumark588a3e82018-07-20 18:53:54 +08004959
4960 void prepareAppTransition(@WindowManager.TransitionType int transit,
Wale Ogunwale3a256e62018-12-06 14:41:18 -08004961 boolean alwaysKeepCurrent) {
4962 prepareAppTransition(transit, alwaysKeepCurrent, 0 /* flags */, false /* forceOverride */);
4963 }
4964
4965 void prepareAppTransition(@WindowManager.TransitionType int transit,
lumark588a3e82018-07-20 18:53:54 +08004966 boolean alwaysKeepCurrent, @WindowManager.TransitionFlags int flags,
4967 boolean forceOverride) {
4968 final boolean prepared = mAppTransition.prepareAppTransitionLocked(
4969 transit, alwaysKeepCurrent, flags, forceOverride);
4970 if (prepared && okToAnimate()) {
4971 mSkipAppTransitionAnimation = false;
4972 }
4973 }
4974
4975 void executeAppTransition() {
4976 if (mAppTransition.isTransitionSet()) {
4977 if (DEBUG_APP_TRANSITIONS) {
4978 Slog.w(TAG_WM, "Execute app transition: " + mAppTransition + ", displayId: "
4979 + mDisplayId + " Callers=" + Debug.getCallers(5));
4980 }
4981 mAppTransition.setReady();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004982 mWmService.mWindowPlacerLocked.requestTraversal();
lumark588a3e82018-07-20 18:53:54 +08004983 }
4984 }
4985
4986 /**
4987 * Update pendingLayoutChanges after app transition has finished.
4988 */
4989 void handleAnimatingStoppedAndTransition() {
4990 int changes = 0;
4991
4992 mAppTransition.setIdle();
4993
4994 for (int i = mNoAnimationNotifyOnTransitionFinished.size() - 1; i >= 0; i--) {
4995 final IBinder token = mNoAnimationNotifyOnTransitionFinished.get(i);
4996 mAppTransition.notifyAppTransitionFinishedLocked(token);
4997 }
4998 mNoAnimationNotifyOnTransitionFinished.clear();
4999
5000 mWallpaperController.hideDeferredWallpapersIfNeeded();
5001
5002 onAppTransitionDone();
5003
5004 changes |= FINISH_LAYOUT_REDO_LAYOUT;
5005 if (DEBUG_WALLPAPER_LIGHT) {
5006 Slog.v(TAG_WM, "Wallpaper layer changed: assigning layers + relayout");
5007 }
5008 computeImeTarget(true /* updateImeTarget */);
wilsonshihc32538e2018-11-07 17:27:34 +08005009 mWallpaperMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08005010 // Since the window list has been rebuilt, focus might have to be recomputed since the
5011 // actual order of windows might have changed again.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08005012 mWmService.mFocusMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08005013
5014 pendingLayoutChanges |= changes;
5015 }
Andrii Kulian15cfb422018-11-07 13:38:49 -08005016
Wale Ogunwale3a256e62018-12-06 14:41:18 -08005017 /** Check if pending app transition is for activity / task launch. */
5018 boolean isNextTransitionForward() {
5019 final int transit = mAppTransition.getAppTransition();
5020 return transit == TRANSIT_ACTIVITY_OPEN
5021 || transit == TRANSIT_TASK_OPEN
5022 || transit == TRANSIT_TASK_TO_FRONT;
5023 }
5024
Andrii Kulian15cfb422018-11-07 13:38:49 -08005025 /**
5026 * @see Display#FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS
5027 */
5028 boolean supportsSystemDecorations() {
Arthur Hung879a8ab2019-03-13 19:51:10 +08005029 return (mWmService.mDisplayWindowSettings.shouldShowSystemDecorsLocked(this)
Andrii Kuliandd989612019-02-21 12:13:28 -08005030 || (mDisplay.getFlags() & FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS) != 0
Chilun891aed22019-05-02 18:08:47 +08005031 || (mWmService.mForceDesktopModeOnExternalDisplays && !isUntrustedVirtualDisplay()))
Arthur Hung879a8ab2019-03-13 19:51:10 +08005032 // VR virtual display will be used to run and render 2D app within a VR experience.
5033 && mDisplayId != mWmService.mVr2dDisplayId;
Andrii Kulian15cfb422018-11-07 13:38:49 -08005034 }
chaviwff2e7d82018-11-02 11:11:27 -07005035
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005036 /**
Chilun891aed22019-05-02 18:08:47 +08005037 * @return {@code true} if the display is non-system created virtual display.
5038 */
5039 boolean isUntrustedVirtualDisplay() {
5040 return mDisplay.getType() == Display.TYPE_VIRTUAL
5041 && mDisplay.getOwnerUid() != Process.SYSTEM_UID;
5042 }
5043
5044 /**
chaviwff2e7d82018-11-02 11:11:27 -07005045 * Re-parent the DisplayContent's top surfaces, {@link #mWindowingLayer} and
Tiger Huangd8ec9382019-04-18 14:35:09 -07005046 * {@link #mOverlayLayer} to the specified SurfaceControl.
chaviwff2e7d82018-11-02 11:11:27 -07005047 *
Tiger Huangd8ec9382019-04-18 14:35:09 -07005048 * @param win The window which owns the SurfaceControl. This indicates the z-order of the
5049 * windows of this display against the windows on the parent display.
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005050 * @param sc The new SurfaceControl, where the DisplayContent's surfaces will be re-parented to.
chaviwff2e7d82018-11-02 11:11:27 -07005051 */
Tiger Huangd8ec9382019-04-18 14:35:09 -07005052 void reparentDisplayContent(WindowState win, SurfaceControl sc) {
5053 mParentWindow = win;
Jackal Guoc43a0a62019-04-23 09:15:14 +08005054 mParentWindow.addEmbeddedDisplayContent(this);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005055 mParentSurfaceControl = sc;
5056 if (mPortalWindowHandle == null) {
5057 mPortalWindowHandle = createPortalWindowHandle(sc.toString());
5058 }
Tiger Huanged6794e2019-05-07 20:07:59 +08005059 getPendingTransaction().setInputWindowInfo(sc, mPortalWindowHandle)
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005060 .reparent(mWindowingLayer, sc).reparent(mOverlayLayer, sc);
5061 }
5062
Tiger Huangd8ec9382019-04-18 14:35:09 -07005063 /**
5064 * Get the window which owns the surface that this DisplayContent is re-parented to.
5065 *
5066 * @return the parent window.
5067 */
5068 WindowState getParentWindow() {
5069 return mParentWindow;
5070 }
5071
5072 /**
5073 * Update the location of this display in the parent window. This enables windows in this
5074 * display to compute the global transformation matrix.
5075 *
5076 * @param win The parent window of this display.
5077 * @param x The x coordinate in the parent window.
5078 * @param y The y coordinate in the parent window.
5079 */
5080 void updateLocation(WindowState win, int x, int y) {
5081 if (mParentWindow != win) {
5082 throw new IllegalArgumentException(
5083 "The given window is not the parent window of this display.");
5084 }
Jackal Guoc43a0a62019-04-23 09:15:14 +08005085 if (!mLocationInParentWindow.equals(x, y)) {
5086 mLocationInParentWindow.set(x, y);
Tiger Huangd8ec9382019-04-18 14:35:09 -07005087 if (mWmService.mAccessibilityController != null) {
5088 mWmService.mAccessibilityController.onSomeWindowResizedOrMovedLocked();
5089 }
Jackal Guoc43a0a62019-04-23 09:15:14 +08005090 notifyLocationInParentDisplayChanged();
Tiger Huangd8ec9382019-04-18 14:35:09 -07005091 }
5092 }
5093
5094 Point getLocationInParentWindow() {
5095 return mLocationInParentWindow;
5096 }
5097
Jackal Guoc43a0a62019-04-23 09:15:14 +08005098 Point getLocationInParentDisplay() {
5099 final Point location = new Point();
5100 if (mParentWindow != null) {
5101 // LocationInParentWindow indicates the offset to (0,0) of window, but what we need is
5102 // the offset to (0,0) of display.
5103 DisplayContent dc = this;
5104 do {
5105 final WindowState displayParent = dc.getParentWindow();
5106 location.x += displayParent.getFrameLw().left
5107 + (dc.getLocationInParentWindow().x * displayParent.mGlobalScale + 0.5f);
5108 location.y += displayParent.getFrameLw().top
5109 + (dc.getLocationInParentWindow().y * displayParent.mGlobalScale + 0.5f);
5110 dc = displayParent.getDisplayContent();
5111 } while (dc != null && dc.getParentWindow() != null);
5112 }
5113 return location;
5114 }
5115
5116 void notifyLocationInParentDisplayChanged() {
5117 forAllWindows(w -> {
5118 w.updateLocationInParentDisplayIfNeeded();
5119 }, false /* traverseTopToBottom */);
5120 }
5121
Tarandeep Singha6f35612019-01-11 19:50:46 -08005122 @VisibleForTesting
5123 SurfaceControl getWindowingLayer() {
5124 return mWindowingLayer;
5125 }
5126
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005127 /**
Adrian Roos4ffc8972019-02-07 20:45:11 +01005128 * Updates the display's system gesture exclusion.
5129 *
5130 * @return true, if the exclusion changed.
5131 */
5132 boolean updateSystemGestureExclusion() {
5133 if (mSystemGestureExclusionListeners.getRegisteredCallbackCount() == 0) {
5134 // No one's interested anyways.
5135 return false;
5136 }
5137
5138 final Region systemGestureExclusion = calculateSystemGestureExclusion();
5139 try {
5140 if (mSystemGestureExclusion.equals(systemGestureExclusion)) {
5141 return false;
5142 }
5143 mSystemGestureExclusion.set(systemGestureExclusion);
5144 for (int i = mSystemGestureExclusionListeners.beginBroadcast() - 1; i >= 0; --i) {
5145 try {
5146 mSystemGestureExclusionListeners.getBroadcastItem(i)
5147 .onSystemGestureExclusionChanged(mDisplayId, systemGestureExclusion);
5148 } catch (RemoteException e) {
5149 Slog.e(TAG, "Failed to notify SystemGestureExclusionListener", e);
5150 }
5151 }
5152 mSystemGestureExclusionListeners.finishBroadcast();
5153 return true;
5154 } finally {
5155 systemGestureExclusion.recycle();
5156 }
5157 }
5158
5159 @VisibleForTesting
5160 Region calculateSystemGestureExclusion() {
Adrian Roos506267d2019-06-18 16:13:53 +02005161 final Region unhandled = Region.obtain();
5162 unhandled.set(0, 0, mDisplayFrames.mDisplayWidth, mDisplayFrames.mDisplayHeight);
5163
5164 final Rect leftEdge = mInsetsStateController.getSourceProvider(TYPE_LEFT_GESTURES)
5165 .getSource().getFrame();
5166 final Rect rightEdge = mInsetsStateController.getSourceProvider(TYPE_RIGHT_GESTURES)
5167 .getSource().getFrame();
5168
Adrian Roos4ffc8972019-02-07 20:45:11 +01005169 final Region global = Region.obtain();
5170 final Region touchableRegion = Region.obtain();
5171 final Region local = Region.obtain();
Adrian Roos506267d2019-06-18 16:13:53 +02005172 final int[] remainingLeftRight =
5173 {mSystemGestureExclusionLimit, mSystemGestureExclusionLimit};
Adrian Roos4ffc8972019-02-07 20:45:11 +01005174
5175 // Traverse all windows bottom up to assemble the gesture exclusion rects.
5176 // For each window, we only take the rects that fall within its touchable region.
5177 forAllWindows(w -> {
5178 if (w.cantReceiveTouchInput() || !w.isVisible()
Adrian Roos506267d2019-06-18 16:13:53 +02005179 || (w.getAttrs().flags & FLAG_NOT_TOUCHABLE) != 0
5180 || unhandled.isEmpty()) {
Adrian Roos4ffc8972019-02-07 20:45:11 +01005181 return;
5182 }
5183 final boolean modal =
5184 (w.mAttrs.flags & (FLAG_NOT_TOUCH_MODAL | FLAG_NOT_FOCUSABLE)) == 0;
5185
Adrian Roos506267d2019-06-18 16:13:53 +02005186 // Get the touchable region of the window, and intersect with where the screen is still
5187 // touchable, i.e. touchable regions on top are not covering it yet.
Adrian Roos4ffc8972019-02-07 20:45:11 +01005188 w.getTouchableRegion(touchableRegion);
Adrian Roos506267d2019-06-18 16:13:53 +02005189 touchableRegion.op(unhandled, Op.INTERSECT);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005190
5191 rectListToRegion(w.getSystemGestureExclusion(), local);
5192
5193 // Transform to display coordinates
5194 local.scale(w.mGlobalScale);
5195 final Rect frame = w.getWindowFrames().mFrame;
5196 local.translate(frame.left, frame.top);
5197
5198 // A window can only exclude system gestures where it is actually touchable
5199 local.op(touchableRegion, Op.INTERSECT);
5200
Adrian Roos506267d2019-06-18 16:13:53 +02005201 // Apply restriction if necessary.
5202 if (needsGestureExclusionRestrictions(w, mLastDispatchedSystemUiVisibility)) {
5203
5204 // Processes the region along the left edge.
5205 remainingLeftRight[0] = addToGlobalAndConsumeLimit(local, global, leftEdge,
5206 remainingLeftRight[0]);
5207
5208 // Processes the region along the right edge.
5209 remainingLeftRight[1] = addToGlobalAndConsumeLimit(local, global, rightEdge,
5210 remainingLeftRight[1]);
5211
5212 // Adds the middle (unrestricted area)
5213 final Region middle = Region.obtain(local);
5214 middle.op(leftEdge, Op.DIFFERENCE);
5215 middle.op(rightEdge, Op.DIFFERENCE);
5216 global.op(middle, Op.UNION);
5217 middle.recycle();
5218 } else {
5219 global.op(local, Op.UNION);
5220 }
5221 unhandled.op(touchableRegion, Op.DIFFERENCE);
5222 }, true /* topToBottom */);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005223 local.recycle();
5224 touchableRegion.recycle();
Adrian Roos506267d2019-06-18 16:13:53 +02005225 unhandled.recycle();
Adrian Roos4ffc8972019-02-07 20:45:11 +01005226 return global;
5227 }
5228
Adrian Roos506267d2019-06-18 16:13:53 +02005229 /**
5230 * @return Whether gesture exclusion area should be restricted from the window depending on the
5231 * current SystemUI visibility flags.
5232 */
5233 private static boolean needsGestureExclusionRestrictions(WindowState win, int sysUiVisibility) {
5234 final int type = win.mAttrs.type;
5235 final int stickyHideNavFlags =
5236 SYSTEM_UI_FLAG_HIDE_NAVIGATION | SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
5237 final boolean stickyHideNav =
5238 (sysUiVisibility & stickyHideNavFlags) == stickyHideNavFlags;
5239 return !stickyHideNav && type != TYPE_INPUT_METHOD && type != TYPE_STATUS_BAR
5240 && win.getActivityType() != ACTIVITY_TYPE_HOME;
5241 }
5242
5243 /**
5244 * Adds a local gesture exclusion area to the global area while applying a limit per edge.
5245 *
5246 * @param local The gesture exclusion area to add.
5247 * @param global The destination.
5248 * @param edge Only processes the part in that region.
5249 * @param limit How much limit in pixels we have.
5250 * @return How much of the limit are remaining.
5251 */
5252 private static int addToGlobalAndConsumeLimit(Region local, Region global, Rect edge,
5253 int limit) {
5254 final Region r = Region.obtain(local);
5255 r.op(edge, Op.INTERSECT);
5256
5257 final int[] remaining = {limit};
5258 forEachRect(r, rect -> {
5259 if (remaining[0] <= 0) {
5260 return;
5261 }
5262 final int height = rect.height();
5263 if (height > remaining[0]) {
5264 rect.bottom = rect.top + remaining[0];
5265 }
5266 remaining[0] -= height;
5267 global.op(rect, Op.UNION);
5268 });
5269 r.recycle();
5270 return remaining[0];
5271 }
5272
Adrian Roos4ffc8972019-02-07 20:45:11 +01005273 void registerSystemGestureExclusionListener(ISystemGestureExclusionListener listener) {
5274 mSystemGestureExclusionListeners.register(listener);
5275 final boolean changed;
5276 if (mSystemGestureExclusionListeners.getRegisteredCallbackCount() == 1) {
5277 changed = updateSystemGestureExclusion();
5278 } else {
5279 changed = false;
5280 }
5281
5282 if (!changed) {
5283 // If updateSystemGestureExclusion changed the exclusion, it will already have
5284 // notified the listener. Otherwise, we'll do it here.
5285 try {
5286 listener.onSystemGestureExclusionChanged(mDisplayId, mSystemGestureExclusion);
5287 } catch (RemoteException e) {
5288 Slog.e(TAG, "Failed to notify SystemGestureExclusionListener during register", e);
5289 }
5290 }
5291 }
5292
5293 void unregisterSystemGestureExclusionListener(ISystemGestureExclusionListener listener) {
5294 mSystemGestureExclusionListeners.unregister(listener);
5295 }
5296
5297 /**
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005298 * Create a portal window handle for input. This window transports any touch to the display
5299 * indicated by {@link InputWindowHandle#portalToDisplayId} if the touch hits this window.
5300 *
5301 * @param name The name of the portal window handle.
5302 * @return the new portal window handle.
5303 */
5304 private InputWindowHandle createPortalWindowHandle(String name) {
5305 // Let surface flinger to set the display ID of this input window handle because we don't
5306 // know which display the parent surface control is on.
5307 final InputWindowHandle portalWindowHandle = new InputWindowHandle(
5308 null /* inputApplicationHandle */, null /* clientWindow */, INVALID_DISPLAY);
5309 portalWindowHandle.name = name;
5310 portalWindowHandle.token = new Binder();
5311 portalWindowHandle.layoutParamsFlags =
5312 FLAG_SPLIT_TOUCH | FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL;
5313 getBounds(mTmpBounds);
5314 portalWindowHandle.touchableRegion.set(mTmpBounds);
5315 portalWindowHandle.scaleFactor = 1f;
5316 portalWindowHandle.ownerPid = Process.myPid();
5317 portalWindowHandle.ownerUid = Process.myUid();
5318 portalWindowHandle.portalToDisplayId = mDisplayId;
5319 return portalWindowHandle;
chaviwff2e7d82018-11-02 11:11:27 -07005320 }
Issei Suzukia5dbf522019-02-01 17:58:15 +01005321
5322 /**
5323 * @see IWindowManager#setForwardedInsets
5324 */
5325 public void setForwardedInsets(Insets insets) {
5326 if (insets == null) {
5327 insets = Insets.NONE;
5328 }
5329 if (mDisplayPolicy.getForwardedInsets().equals(insets)) {
5330 return;
5331 }
5332 mDisplayPolicy.setForwardedInsets(insets);
5333 setLayoutNeeded();
5334 mWmService.mWindowPlacerLocked.requestTraversal();
5335 }
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05005336
5337 protected MetricsLogger getMetricsLogger() {
5338 if (mMetricsLogger == null) {
5339 mMetricsLogger = new MetricsLogger();
5340 }
5341 return mMetricsLogger;
5342 }
Craig Mautner59c00972012-07-30 12:10:24 -07005343}