blob: 1659131d880dbe9a3b098758753749b495a2cbd3 [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 {
lumark588a3e82018-07-20 18:53:54 +08002562 // Clear all transitions & screen frozen states when removing display.
2563 mOpeningApps.clear();
2564 mClosingApps.clear();
Evan Rosky2289ba12018-11-19 18:28:18 -08002565 mChangingApps.clear();
lumark588a3e82018-07-20 18:53:54 +08002566 mUnknownAppVisibilityController.clear();
2567 mAppTransition.removeAppTransitionTimeoutCallbacks();
2568 handleAnimatingStoppedAndTransition();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002569 mWmService.stopFreezingDisplayLocked();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002570 super.removeImmediately();
2571 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Riddle Hsu2588ab02019-02-25 14:23:56 +08002572 mPointerEventDispatcher.dispose();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002573 mWmService.mAnimator.removeDisplayLocked(mDisplayId);
Louis Chang207f9112018-07-30 08:58:54 +08002574 mWindowingLayer.release();
2575 mOverlayLayer.release();
Jorim Jaggiae962e62018-12-27 17:23:48 +01002576 mInputMonitor.onDisplayRemoved();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002577 } finally {
Riddle Hsu4e611772018-10-31 18:58:28 +08002578 mDisplayReady = false;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002579 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002580 }
Robert Carr679ccb02018-08-08 15:32:35 -07002581
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002582 mWmService.mWindowPlacerLocked.requestTraversal();
Craig Mautner95da1082014-02-24 17:54:35 -08002583 }
2584
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002585 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002586 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002587 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002588 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2589
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002590 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002591 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002592 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002593 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002594 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002595 }
2596
Andrii Kulian0214ed92017-05-16 13:44:05 -07002597 /** @return 'true' if removal of this display content is deferred due to active animation. */
2598 boolean isRemovalDeferred() {
2599 return mDeferredRemoval;
2600 }
2601
Wale Ogunwale10124582016-09-15 20:25:50 -07002602 boolean animateForIme(float interpolatedValue, float animationTarget,
2603 float dividerAnimationTarget) {
2604 boolean updated = false;
2605
Wale Ogunwale61911492017-10-11 08:50:50 -07002606 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2607 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002608 if (stack == null || !stack.isAdjustedForIme()) {
2609 continue;
2610 }
2611
2612 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2613 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2614 updated = true;
2615 } else {
2616 mDividerControllerLocked.mLastAnimationProgress =
2617 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2618 mDividerControllerLocked.mLastDividerProgress =
2619 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2620 updated |= stack.updateAdjustForIme(
2621 mDividerControllerLocked.mLastAnimationProgress,
2622 mDividerControllerLocked.mLastDividerProgress,
2623 false /* force */);
2624 }
2625 if (interpolatedValue >= 1f) {
2626 stack.endImeAdjustAnimation();
2627 }
2628 }
2629
2630 return updated;
2631 }
2632
2633 boolean clearImeAdjustAnimation() {
2634 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002635 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2636 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002637 if (stack != null && stack.isAdjustedForIme()) {
2638 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2639 changed = true;
2640 }
2641 }
2642 return changed;
2643 }
2644
2645 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002646 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2647 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002648 if (stack.isVisible() && stack.isAdjustedForIme()) {
2649 stack.beginImeAdjustAnimation();
2650 }
2651 }
2652 }
2653
2654 void adjustForImeIfNeeded() {
lumark90120a82018-08-15 00:33:03 +08002655 final WindowState imeWin = mInputMethodWindow;
Wale Ogunwale10124582016-09-15 20:25:50 -07002656 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2657 && !mDividerControllerLocked.isImeHideRequested();
Riddle Hsuc6814252019-05-16 17:06:52 +08002658 final TaskStack dockedStack = getSplitScreenPrimaryStack();
2659 final boolean dockVisible = dockedStack != null;
2660 final Task topDockedTask = dockVisible ? dockedStack.getTopChild() : null;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002661 final TaskStack imeTargetStack = mWmService.getImeFocusStackLocked();
Wale Ogunwale10124582016-09-15 20:25:50 -07002662 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2663 imeTargetStack.getDockSide() : DOCKED_INVALID;
2664 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2665 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002666 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002667 final boolean imeHeightChanged = imeVisible &&
2668 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2669
Riddle Hsuc6814252019-05-16 17:06:52 +08002670 // This includes a case where the docked stack is unminimizing and IME is visible for the
2671 // bottom side stack. The condition prevents adjusting the override task bounds for IME to
2672 // the minimized docked stack bounds.
2673 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock()
2674 || (topDockedTask != null && imeOnBottom && !dockedStack.isAdjustedForIme()
2675 && dockedStack.getBounds().height() < topDockedTask.getBounds().height());
2676
Wale Ogunwale10124582016-09-15 20:25:50 -07002677 // The divider could be adjusted for IME position, or be thinner than usual,
2678 // or both. There are three possible cases:
2679 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2680 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2681 // - If IME is not visible, divider is not moved and is normal width.
2682
2683 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002684 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2685 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002686 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002687 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2688 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002689 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2690 } else {
2691 stack.resetAdjustedForIme(false);
2692 }
2693 }
2694 mDividerControllerLocked.setAdjustedForIme(
2695 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2696 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002697 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2698 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002699 stack.resetAdjustedForIme(!dockVisible);
2700 }
2701 mDividerControllerLocked.setAdjustedForIme(
2702 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2703 }
Winson Chung655332c2016-10-31 13:14:28 -07002704 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002705 }
2706
2707 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002708 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2709 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002710 stack.prepareFreezingTaskBounds();
2711 }
2712 }
2713
Wale Ogunwale94744212015-09-21 19:01:47 -07002714 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002715 getBounds(mTmpRect, newRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002716 rotateBounds(mTmpRect, oldRotation, newRotation, bounds);
2717 }
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002718
Evan Rosky39b6f232018-10-30 18:35:41 -07002719 void rotateBounds(Rect parentBounds, int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002720 // Compute a transform matrix to undo the coordinate space transformation,
2721 // and present the window at the same physical position it previously occupied.
2722 final int deltaRotation = deltaRotation(newRotation, oldRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002723 createRotationMatrix(
2724 deltaRotation, parentBounds.width(), parentBounds.height(), mTmpMatrix);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002725
2726 mTmpRectF.set(bounds);
2727 mTmpMatrix.mapRect(mTmpRectF);
2728 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002729 }
2730
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002731 static int deltaRotation(int oldRotation, int newRotation) {
2732 int delta = newRotation - oldRotation;
2733 if (delta < 0) delta += 4;
2734 return delta;
2735 }
2736
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002737 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002738 Matrix outMatrix) {
2739 // For rotations without Z-ordering we don't need the target rectangle's position.
2740 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2741 displayHeight, outMatrix);
2742 }
2743
2744 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2745 float displayWidth, float displayHeight, Matrix outMatrix) {
2746 switch (rotation) {
2747 case ROTATION_0:
2748 outMatrix.reset();
2749 break;
2750 case ROTATION_270:
2751 outMatrix.setRotate(270, 0, 0);
2752 outMatrix.postTranslate(0, displayHeight);
2753 outMatrix.postTranslate(rectTop, 0);
2754 break;
2755 case ROTATION_180:
2756 outMatrix.reset();
2757 break;
2758 case ROTATION_90:
2759 outMatrix.setRotate(90, 0, 0);
2760 outMatrix.postTranslate(displayWidth, 0);
2761 outMatrix.postTranslate(-rectTop, rectLeft);
2762 break;
2763 }
2764 }
2765
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002766 @CallSuper
2767 @Override
Nataniel Borges023ecb52019-01-16 14:15:43 -08002768 public void writeToProto(ProtoOutputStream proto, long fieldId,
2769 @WindowTraceLogLevel int logLevel) {
2770 // Critical log level logs only visible elements to mitigate performance overheard
2771 if (logLevel == WindowTraceLogLevel.CRITICAL && !isVisible()) {
2772 return;
2773 }
2774
Steven Timotiusaf03df62017-07-18 16:56:43 -07002775 final long token = proto.start(fieldId);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002776 super.writeToProto(proto, WINDOW_CONTAINER, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002777 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002778 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2779 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002780 stack.writeToProto(proto, STACKS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002781 }
2782 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2783 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002784 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2785 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002786 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002787 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002788 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2789 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002790 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002791 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002792 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2793 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002794 windowToken.writeToProto(proto, IME_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002795 }
2796 proto.write(DPI, mBaseDisplayDensity);
2797 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002798 proto.write(ROTATION, mRotation);
2799 final ScreenRotationAnimation screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002800 mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002801 if (screenRotationAnimation != null) {
2802 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2803 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002804 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
lumark588a3e82018-07-20 18:53:54 +08002805 mAppTransition.writeToProto(proto, APP_TRANSITION);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002806 if (mFocusedApp != null) {
2807 mFocusedApp.writeNameToProto(proto, FOCUSED_APP);
2808 }
Jorim Jaggi1112fed2019-04-15 13:32:14 +02002809 for (int i = mOpeningApps.size() - 1; i >= 0; i--) {
2810 mOpeningApps.valueAt(i).mActivityRecord.writeIdentifierToProto(proto, OPENING_APPS);
2811 }
2812 for (int i = mClosingApps.size() - 1; i >= 0; i--) {
2813 mClosingApps.valueAt(i).mActivityRecord.writeIdentifierToProto(proto, CLOSING_APPS);
2814 }
2815 for (int i = mChangingApps.size() - 1; i >= 0; i--) {
2816 mChangingApps.valueAt(i).mActivityRecord.writeIdentifierToProto(proto, CHANGING_APPS);
2817 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002818 proto.end(token);
2819 }
2820
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002821 @Override
2822 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2823 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002824 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2825 final String subPrefix = " " + prefix;
2826 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2827 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2828 pw.print("dpi");
2829 if (mInitialDisplayWidth != mBaseDisplayWidth
2830 || mInitialDisplayHeight != mBaseDisplayHeight
2831 || mInitialDisplayDensity != mBaseDisplayDensity) {
2832 pw.print(" base=");
2833 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2834 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2835 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002836 if (mDisplayScalingDisabled) {
2837 pw.println(" noscale");
2838 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002839 pw.print(" cur=");
2840 pw.print(mDisplayInfo.logicalWidth);
2841 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2842 pw.print(" app=");
2843 pw.print(mDisplayInfo.appWidth);
2844 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2845 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2846 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2847 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2848 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002849 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002850 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002851 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002852
Craig Mautnerdc548482014-02-05 13:35:24 -08002853 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002854 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002855 pw.print(prefix);
2856 pw.print("mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
Adrian Roos5251b1d2018-03-23 18:57:43 +01002857
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002858 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
2859 if (mLastFocus != mCurrentFocus) {
2860 pw.print(" mLastFocus="); pw.println(mLastFocus);
2861 }
2862 if (mLosingFocus.size() > 0) {
2863 pw.println();
2864 pw.println(" Windows losing focus:");
2865 for (int i = mLosingFocus.size() - 1; i >= 0; i--) {
2866 final WindowState w = mLosingFocus.get(i);
2867 pw.print(" Losing #"); pw.print(i); pw.print(' ');
2868 pw.print(w);
2869 if (dumpAll) {
2870 pw.println(":");
2871 w.dump(pw, " ", true);
2872 } else {
2873 pw.println();
2874 }
2875 }
2876 }
2877 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
Tiger Huang7c610aa2018-10-27 00:01:01 +08002878 if (mLastStatusBarVisibility != 0) {
2879 pw.print(" mLastStatusBarVisibility=0x");
2880 pw.println(Integer.toHexString(mLastStatusBarVisibility));
2881 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002882
Adrian Roos5251b1d2018-03-23 18:57:43 +01002883 pw.println();
wilsonshihc32538e2018-11-07 17:27:34 +08002884 mWallpaperController.dump(pw, " ");
2885
2886 pw.println();
Adrian Roos4ffc8972019-02-07 20:45:11 +01002887 pw.print("mSystemGestureExclusion=");
2888 if (mSystemGestureExclusionListeners.getRegisteredCallbackCount() > 0) {
2889 pw.println(mSystemGestureExclusion);
2890 } else {
2891 pw.println("<no lstnrs>");
2892 }
2893
2894 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002895 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002896 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2897 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002898 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002899 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002900
Craig Mautnerdc548482014-02-05 13:35:24 -08002901 pw.println();
2902 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002903 pw.println();
2904 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002905 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002906 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002907 pw.print(" Exiting #"); pw.print(i);
2908 pw.print(' '); pw.print(token);
2909 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002910 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002911 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002912 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002913
Jorim Jaggi31f71702016-05-04 16:43:04 -07002914 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002915
2916 // Dump stack references
2917 final TaskStack homeStack = getHomeStack();
2918 if (homeStack != null) {
2919 pw.println(prefix + "homeStack=" + homeStack.getName());
2920 }
2921 final TaskStack pinnedStack = getPinnedStack();
2922 if (pinnedStack != null) {
2923 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2924 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002925 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002926 if (splitScreenPrimaryStack != null) {
2927 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2928 }
2929
2930 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002931 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002932 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002933 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002934
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002935 pw.println();
2936 mDisplayFrames.dump(prefix, pw);
Arthur Hung95b38a92018-07-20 18:56:12 +08002937 pw.println();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08002938 mDisplayPolicy.dump(prefix, pw);
2939 pw.println();
Riddle Hsuad256a12018-07-18 16:11:30 +08002940 mDisplayRotation.dump(prefix, pw);
2941 pw.println();
Arthur Hung95b38a92018-07-20 18:56:12 +08002942 mInputMonitor.dump(pw, " ");
Jorim Jaggif96c90a2018-09-26 16:55:15 +02002943 pw.println();
2944 mInsetsStateController.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002945 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002946
2947 @Override
2948 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002949 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002950 }
2951
2952 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002953 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002954 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002955
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002956 /** Returns true if the stack in the windowing mode is visible. */
2957 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002958 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002959 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002960 }
2961
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002962 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002963 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002964 final int x = (int) xf;
2965 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002966 final WindowState touchedWin = getWindow(w -> {
2967 final int flags = w.mAttrs.flags;
2968 if (!w.isVisibleLw()) {
2969 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002970 }
2971 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002972 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002973 }
2974
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002975 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002976 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002977 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002978 }
2979
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002980 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002981
2982 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002983 return mTmpRegion.contains(x, y) || touchFlags == 0;
2984 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002985
2986 return touchedWin;
2987 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002988
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002989 boolean canAddToastWindowForUid(int uid) {
2990 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002991 // Also if the app is focused adding more than one toast at
2992 // a time for better backwards compatibility.
2993 final WindowState focusedWindowForUid = getWindow(w ->
2994 w.mOwnerUid == uid && w.isFocused());
2995 if (focusedWindowForUid != null) {
2996 return true;
2997 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002998 final WindowState win = getWindow(w ->
2999 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
3000 && !w.mWindowRemovalAllowed);
3001 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07003002 }
3003
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003004 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07003005 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
3006 return;
3007 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003008
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003009 // Used to communicate the old focus to the callback method.
3010 mTmpWindow = oldFocus;
3011
3012 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07003013 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07003014
Louis Changa9350fe2019-04-25 17:14:20 +08003015 /**
3016 * Looking for the focused window on this display if the top focused display hasn't been
3017 * found yet (topFocusedDisplayId is INVALID_DISPLAY) or per-display focused was allowed.
3018 *
3019 * @param topFocusedDisplayId Id of the top focused display.
3020 * @return The focused window or null if there isn't any or no need to seek.
3021 */
3022 WindowState findFocusedWindowIfNeeded(int topFocusedDisplayId) {
3023 return (mWmService.mPerDisplayFocusEnabled || topFocusedDisplayId == INVALID_DISPLAY)
3024 ? findFocusedWindow() : null;
Tiger Huang51c5a1d2018-12-11 20:24:51 +08003025 }
3026
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07003027 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003028 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07003029
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003030 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003031
3032 if (mTmpWindow == null) {
3033 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
3034 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07003035 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003036 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07003037 }
3038
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003039 /**
3040 * Update the focused window and make some adjustments if the focus has changed.
3041 *
3042 * @param mode Indicates the situation we are in. Possible modes are:
3043 * {@link WindowManagerService#UPDATE_FOCUS_NORMAL},
3044 * {@link WindowManagerService#UPDATE_FOCUS_PLACING_SURFACES},
3045 * {@link WindowManagerService#UPDATE_FOCUS_WILL_PLACE_SURFACES},
3046 * {@link WindowManagerService#UPDATE_FOCUS_REMOVING_FOCUS}
3047 * @param updateInputWindows Whether to sync the window information to the input module.
Louis Changa9350fe2019-04-25 17:14:20 +08003048 * @param topFocusedDisplayId Display id of current top focused display.
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003049 * @return {@code true} if the focused window has changed.
3050 */
Louis Changa9350fe2019-04-25 17:14:20 +08003051 boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows,
3052 int topFocusedDisplayId) {
3053 WindowState newFocus = findFocusedWindowIfNeeded(topFocusedDisplayId);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003054 if (mCurrentFocus == newFocus) {
3055 return false;
3056 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003057 boolean imWindowChanged = false;
Tiger Huang51c5a1d2018-12-11 20:24:51 +08003058 final WindowState imWindow = mInputMethodWindow;
3059 if (imWindow != null) {
3060 final WindowState prevTarget = mInputMethodTarget;
3061 final WindowState newTarget = computeImeTarget(true /* updateImeTarget*/);
3062 imWindowChanged = prevTarget != newTarget;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003063
Tiger Huang51c5a1d2018-12-11 20:24:51 +08003064 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS
3065 && mode != UPDATE_FOCUS_WILL_PLACE_SURFACES) {
3066 assignWindowLayers(false /* setLayoutNeeded */);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003067 }
3068 }
3069
3070 if (imWindowChanged) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003071 mWmService.mWindowsChanged = true;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003072 setLayoutNeeded();
Louis Changa9350fe2019-04-25 17:14:20 +08003073 newFocus = findFocusedWindowIfNeeded(topFocusedDisplayId);
Tiger Huang51c5a1d2018-12-11 20:24:51 +08003074 }
3075 if (mCurrentFocus != newFocus) {
3076 mWmService.mH.obtainMessage(REPORT_FOCUS_CHANGE, this).sendToTarget();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003077 }
3078
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003079 if (DEBUG_FOCUS_LIGHT || mWmService.localLOGV) Slog.v(TAG_WM, "Changing focus from "
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003080 + mCurrentFocus + " to " + newFocus + " displayId=" + getDisplayId()
3081 + " Callers=" + Debug.getCallers(4));
3082 final WindowState oldFocus = mCurrentFocus;
3083 mCurrentFocus = newFocus;
3084 mLosingFocus.remove(newFocus);
3085
3086 if (newFocus != null) {
3087 mWinAddedSinceNullFocus.clear();
3088 mWinRemovedSinceNullFocus.clear();
3089
3090 if (newFocus.canReceiveKeys()) {
3091 // Displaying a window implicitly causes dispatching to be unpaused.
3092 // This is to protect against bugs if someone pauses dispatching but
3093 // forgets to resume.
3094 newFocus.mToken.paused = false;
3095 }
3096 }
3097
Tiger Huang7c610aa2018-10-27 00:01:01 +08003098 int focusChanged = getDisplayPolicy().focusChangedLw(oldFocus, newFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003099
3100 if (imWindowChanged && oldFocus != mInputMethodWindow) {
3101 // Focus of the input method window changed. Perform layout if needed.
3102 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
3103 performLayout(true /*initial*/, updateInputWindows);
3104 focusChanged &= ~FINISH_LAYOUT_REDO_LAYOUT;
3105 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
3106 // Client will do the layout, but we need to assign layers
3107 // for handleNewWindowLocked() below.
3108 assignWindowLayers(false /* setLayoutNeeded */);
3109 }
3110 }
3111
3112 if ((focusChanged & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3113 // The change in focus caused us to need to do a layout. Okay.
3114 setLayoutNeeded();
3115 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
3116 performLayout(true /*initial*/, updateInputWindows);
3117 } else if (mode == UPDATE_FOCUS_REMOVING_FOCUS) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003118 mWmService.mRoot.performSurfacePlacement(false);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003119 }
3120 }
3121
3122 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
3123 // If we defer assigning layers, then the caller is responsible for doing this part.
3124 getInputMonitor().setInputFocusLw(newFocus, updateInputWindows);
3125 }
3126
3127 adjustForImeIfNeeded();
3128
3129 // We may need to schedule some toast windows to be removed. The toasts for an app that
3130 // does not have input focus are removed within a timeout to prevent apps to redress
3131 // other apps' UI.
3132 scheduleToastWindowsTimeoutIfNeededLocked(oldFocus, newFocus);
3133
3134 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
3135 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
3136 }
3137 return true;
3138 }
3139
3140 /**
3141 * Set the new focused app to this display.
3142 *
3143 * @param newFocus the new focused AppWindowToken.
3144 * @return true if the focused app is changed.
3145 */
3146 boolean setFocusedApp(AppWindowToken newFocus) {
3147 if (newFocus != null) {
3148 final DisplayContent appDisplay = newFocus.getDisplayContent();
3149 if (appDisplay != this) {
3150 throw new IllegalStateException(newFocus + " is not on " + getName()
3151 + " but " + ((appDisplay != null) ? appDisplay.getName() : "none"));
3152 }
3153 }
3154 if (mFocusedApp == newFocus) {
3155 return false;
3156 }
3157 mFocusedApp = newFocus;
3158 getInputMonitor().setFocusedAppLw(newFocus);
3159 updateTouchExcludeRegion();
3160 return true;
3161 }
3162
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003163 /** Updates the layer assignment of windows on this display. */
3164 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggi4981f152019-03-26 18:58:45 +01003165 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07003166 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003167 if (setLayoutNeeded) {
3168 setLayoutNeeded();
3169 }
Robert Carrb1579c82017-09-05 14:54:47 -07003170
Robert Carrf59b8dd2017-10-02 18:58:36 -07003171 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07003172 // the application of this transaction until the animation pass triggers
3173 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
3174 // the hiding and showing of surfaces.
3175 scheduleAnimation();
Jorim Jaggi4981f152019-03-26 18:58:45 +01003176 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003177 }
3178
Wale Ogunwale1666e312016-12-16 11:27:18 -08003179 // TODO: This should probably be called any time a visual change is made to the hierarchy like
3180 // moving containers or resizing them. Need to investigate the best way to have it automatically
3181 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003182 void layoutAndAssignWindowLayersIfNeeded() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003183 mWmService.mWindowsChanged = true;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003184 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003185
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003186 if (!mWmService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003187 false /*updateInputWindows*/)) {
3188 assignWindowLayers(false /* setLayoutNeeded */);
3189 }
3190
Arthur Hung95b38a92018-07-20 18:56:12 +08003191 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003192 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08003193 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003194 }
3195
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003196 /** Returns true if a leaked surface was destroyed */
3197 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003198 // Used to indicate that a surface was leaked.
3199 mTmpWindow = null;
3200 forAllWindows(w -> {
3201 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003202 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003203 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003204 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003205 if (!mWmService.mSessions.contains(wsa.mSession)) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003206 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003207 + w + " surface=" + wsa.mSurfaceController
3208 + " token=" + w.mToken
3209 + " pid=" + w.mSession.mPid
3210 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003211 wsa.destroySurface();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003212 mWmService.mForceRemoves.add(w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003213 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07003214 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003215 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003216 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02003217 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003218 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003219 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003220 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003221 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003222 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003223
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003224 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003225 }
3226
3227 /**
lumark90120a82018-08-15 00:33:03 +08003228 * Set input method window for the display.
3229 * @param win Set when window added or Null when destroyed.
3230 */
3231 void setInputMethodWindowLocked(WindowState win) {
3232 mInputMethodWindow = win;
lumark70865a82018-09-17 16:56:26 +08003233 // Update display configuration for IME process.
3234 if (mInputMethodWindow != null) {
3235 final int imePid = mInputMethodWindow.mSession.mPid;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003236 mWmService.mAtmInternal.onImeWindowSetOnDisplay(imePid,
lumark70865a82018-09-17 16:56:26 +08003237 mInputMethodWindow.getDisplayId());
3238 }
lumark90120a82018-08-15 00:33:03 +08003239 computeImeTarget(true /* updateImeTarget */);
Jorim Jaggif96c90a2018-09-26 16:55:15 +02003240 mInsetsStateController.getSourceProvider(TYPE_IME).setWindow(win,
3241 null /* frameProvider */);
lumark90120a82018-08-15 00:33:03 +08003242 }
3243
3244 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003245 * Determine and return the window that should be the IME target.
3246 * @param updateImeTarget If true the system IME target will be updated to match what we found.
3247 * @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 +00003248 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003249 WindowState computeImeTarget(boolean updateImeTarget) {
lumark90120a82018-08-15 00:33:03 +08003250 if (mInputMethodWindow == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003251 // There isn't an IME so there shouldn't be a target...That was easy!
3252 if (updateImeTarget) {
3253 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
lumarkff0ab692018-11-05 20:32:30 +08003254 + mInputMethodTarget + " to null since mInputMethodWindow is null");
3255 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003256 }
3257 return null;
3258 }
3259
lumarkff0ab692018-11-05 20:32:30 +08003260 final WindowState curTarget = mInputMethodTarget;
Chavi Weingarten3a748552018-05-14 17:32:42 +00003261 if (!canUpdateImeTarget()) {
3262 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
3263 return curTarget;
3264 }
3265
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003266 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
3267 // same display. Or even when the current IME/target are not on the same screen as the next
3268 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08003269 mUpdateImeTarget = updateImeTarget;
3270 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003271
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003272
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003273 // Yet more tricksyness! If this window is a "starting" window, we do actually want
3274 // to be on top of it, but it is not -really- where input will go. So look down below
3275 // for a real window to target...
3276 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
3277 final AppWindowToken token = target.mAppToken;
3278 if (token != null) {
3279 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
3280 if (betterTarget != null) {
3281 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003282 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003283 }
3284 }
3285
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003286 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
lumarkff0ab692018-11-05 20:32:30 +08003287 "Proposed new IME target: " + target + " for display: " + getDisplayId());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003288
chaviw1c13ad32018-06-12 16:44:23 -07003289 // Now, a special case -- if the last target's window is in the process of exiting, but
Tarandeep Singh2d0aca42019-03-28 14:08:58 -07003290 // not removed, keep on the last target to avoid IME flicker.
chaviw1c13ad32018-06-12 16:44:23 -07003291 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
Tarandeep Singh2d0aca42019-03-28 14:08:58 -07003292 && curTarget.isClosing()) {
3293 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Not changing target till current window is"
3294 + " closing and not removed");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003295 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003296 }
3297
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003298 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
3299 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003300
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003301 if (target == null) {
3302 if (updateImeTarget) {
3303 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
3304 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
3305 + Debug.getCallers(4) : ""));
lumarkff0ab692018-11-05 20:32:30 +08003306 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003307 }
3308
3309 return null;
3310 }
3311
3312 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003313 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
3314 if (token != null) {
3315
3316 // Now some fun for dealing with window animations that modify the Z order. We need
3317 // to look at all windows below the current target that are in this app, finding the
3318 // highest visible one in layering.
3319 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003320 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003321 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003322 }
3323
3324 if (highestTarget != null) {
lumark588a3e82018-07-20 18:53:54 +08003325 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, mAppTransition + " " + highestTarget
Jorim Jaggi35d328a2018-08-14 17:00:20 +02003326 + " animating=" + highestTarget.isAnimating());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003327
lumark588a3e82018-07-20 18:53:54 +08003328 if (mAppTransition.isTransitionSet()) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003329 // If we are currently setting up for an animation, hold everything until we
3330 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003331 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003332 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003333 }
3334 }
3335 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003336
3337 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
3338 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003339 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003340 }
3341
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003342 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003343 }
3344
lumarkff0ab692018-11-05 20:32:30 +08003345 /**
3346 * Calling {@link #computeImeTarget(boolean)} to update the input method target window in
3347 * the candidate app window token if needed.
3348 */
3349 void computeImeTargetIfNeeded(AppWindowToken candidate) {
3350 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == candidate) {
3351 computeImeTarget(true /* updateImeTarget */);
3352 }
3353 }
3354
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003355 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
lumarkff0ab692018-11-05 20:32:30 +08003356 if (target == mInputMethodTarget && mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003357 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003358 }
3359
lumarkff0ab692018-11-05 20:32:30 +08003360 mInputMethodTarget = target;
3361 mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003362 assignWindowLayers(false /* setLayoutNeeded */);
Tarandeep Singh215929b2019-01-11 18:24:37 -08003363 mInsetsStateController.onImeTargetChanged(target);
Tarandeep Singha6f35612019-01-11 19:50:46 -08003364 updateImeParent();
3365 }
3366
3367 private void updateImeParent() {
Jackal Guo818c3f12019-03-08 18:00:39 +08003368 // Force attaching IME to the display when magnifying, or it would be magnified with
3369 // target app together.
3370 final boolean shouldAttachToDisplay = (mMagnificationSpec != null);
3371 final SurfaceControl newParent =
3372 shouldAttachToDisplay ? mWindowingLayer : computeImeParent();
Tarandeep Singha6f35612019-01-11 19:50:46 -08003373 if (newParent != null) {
Tiger Huanged6794e2019-05-07 20:07:59 +08003374 getPendingTransaction().reparent(mImeWindowsContainers.mSurfaceControl, newParent);
Tarandeep Singha6f35612019-01-11 19:50:46 -08003375 scheduleAnimation();
3376 }
3377 }
3378
3379 /**
3380 * Computes the window the IME should be attached to.
3381 */
3382 @VisibleForTesting
3383 SurfaceControl computeImeParent() {
3384
3385 // Attach it to app if the target is part of an app and such app is covering the entire
3386 // screen. If it's not covering the entire screen the IME might extend beyond the apps
3387 // bounds.
Riddle Hsu6d6f67c2019-03-14 16:54:26 +08003388 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken != null
3389 && mInputMethodTarget.getWindowingMode() == WINDOWING_MODE_FULLSCREEN
3390 // An activity with override bounds should be letterboxed inside its parent bounds,
3391 // so it doesn't fill the screen.
3392 && mInputMethodTarget.mAppToken.matchParentBounds()) {
Tarandeep Singha6f35612019-01-11 19:50:46 -08003393 return mInputMethodTarget.mAppToken.getSurfaceControl();
3394 }
3395
3396 // Otherwise, we just attach it to the display.
3397 return mWindowingLayer;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003398 }
3399
3400 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
3401 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3402 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
3403 }
3404
3405 // Used to indicate we have reached the first window in the range we are interested in.
3406 mTmpWindow = null;
3407
3408 // TODO: Figure-out a more efficient way to do this.
3409 final WindowState candidate = getWindow(w -> {
3410 if (w == top) {
3411 // Reached the first window in the range we are interested in.
3412 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003413 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003414 if (mTmpWindow == null) {
3415 return false;
3416 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003417
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003418 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3419 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003420 }
3421 // If we reached the bottom of the range of windows we are considering,
3422 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003423 if (w == bottom) {
3424 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003425 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003426 return false;
3427 });
3428
3429 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003430 }
3431
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003432 void setLayoutNeeded() {
3433 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
3434 mLayoutNeeded = true;
3435 }
3436
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003437 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003438 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
3439 mLayoutNeeded = false;
3440 }
3441
3442 boolean isLayoutNeeded() {
3443 return mLayoutNeeded;
3444 }
3445
Wale Ogunwale02319a62016-09-26 15:21:22 -07003446 void dumpTokens(PrintWriter pw, boolean dumpAll) {
3447 if (mTokenMap.isEmpty()) {
3448 return;
3449 }
3450 pw.println(" Display #" + mDisplayId);
3451 final Iterator<WindowToken> it = mTokenMap.values().iterator();
3452 while (it.hasNext()) {
3453 final WindowToken token = it.next();
3454 pw.print(" ");
3455 pw.print(token);
3456 if (dumpAll) {
3457 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003458 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07003459 } else {
3460 pw.println();
3461 }
3462 }
lumark588a3e82018-07-20 18:53:54 +08003463
Evan Rosky2289ba12018-11-19 18:28:18 -08003464 if (!mOpeningApps.isEmpty() || !mClosingApps.isEmpty() || !mChangingApps.isEmpty()) {
lumark588a3e82018-07-20 18:53:54 +08003465 pw.println();
3466 if (mOpeningApps.size() > 0) {
3467 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
3468 }
3469 if (mClosingApps.size() > 0) {
3470 pw.print(" mClosingApps="); pw.println(mClosingApps);
3471 }
Evan Rosky2289ba12018-11-19 18:28:18 -08003472 if (mChangingApps.size() > 0) {
3473 pw.print(" mChangingApps="); pw.println(mChangingApps);
3474 }
lumark588a3e82018-07-20 18:53:54 +08003475 }
3476
3477 mUnknownAppVisibilityController.dump(pw, " ");
Wale Ogunwale02319a62016-09-26 15:21:22 -07003478 }
3479
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003480 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003481 final int[] index = new int[1];
3482 forAllWindows(w -> {
3483 final WindowStateAnimator wAnim = w.mWinAnimator;
3484 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
3485 index[0] = index[0] + 1;
3486 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003487 }
3488
Jorim Jaggife762342016-10-13 14:33:27 +02003489 /**
3490 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
3491 */
3492 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003493 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003494 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02003495 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
3496 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01003497 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02003498 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003499 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02003500 }
3501
Wale Ogunwale494009b82016-10-21 09:01:38 -07003502 boolean checkWaitingForWindows() {
3503
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003504 mHaveBootMsg = false;
3505 mHaveApp = false;
3506 mHaveWallpaper = false;
3507 mHaveKeyguard = true;
3508
3509 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003510 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
3511 return true;
3512 }
3513 if (w.isDrawnLw()) {
3514 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003515 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003516 } else if (w.mAttrs.type == TYPE_APPLICATION
3517 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003518 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003519 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003520 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003521 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003522 mHaveKeyguard = mWmService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003523 }
3524 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003525 return false;
3526 });
3527
3528 if (visibleWindow != null) {
3529 // We have a visible window.
3530 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003531 }
3532
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003533 // if the wallpaper service is disabled on the device, we're never going to have
3534 // wallpaper, don't bother waiting for it
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003535 boolean wallpaperEnabled = mWmService.mContext.getResources().getBoolean(
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003536 com.android.internal.R.bool.config_enableWallpaperService)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003537 && mWmService.mContext.getResources().getBoolean(
Daichi Hironoc1432222018-03-29 13:06:07 +09003538 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003539 && !mWmService.mOnlyCore;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003540
Wale Ogunwale494009b82016-10-21 09:01:38 -07003541 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003542 "******** booted=" + mWmService.mSystemBooted
3543 + " msg=" + mWmService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003544 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
3545 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
3546 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003547
3548 // If we are turning on the screen to show the boot message, don't do it until the boot
3549 // message is actually displayed.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003550 if (!mWmService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003551 return true;
3552 }
3553
3554 // If we are turning on the screen after the boot is completed normally, don't do so until
3555 // we have the application and wallpaper.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003556 if (mWmService.mSystemBooted
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003557 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003558 return true;
3559 }
3560
3561 return false;
3562 }
3563
Jorim Jaggi8f520872018-08-14 17:00:20 +02003564 void updateWindowsForAnimator() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003565 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003566 }
3567
Jorim Jaggi8f520872018-08-14 17:00:20 +02003568 /**
3569 * Updates the {@link TaskStack#setAnimationBackground} for all windows.
3570 */
3571 void updateBackgroundForAnimator() {
Jorim Jaggi7156b652016-10-25 08:31:58 -07003572 resetAnimationBackgroundAnimator();
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003573 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003574 }
3575
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003576 boolean isInputMethodClientFocus(int uid, int pid) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003577 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003578 if (imFocus == null) {
3579 return false;
3580 }
3581
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003582 if (DEBUG_INPUT_METHOD) {
3583 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003584 Slog.i(TAG_WM, "Current focus: " + mCurrentFocus + " displayId=" + mDisplayId);
3585 Slog.i(TAG_WM, "Last focus: " + mLastFocus + " displayId=" + mDisplayId);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003586 }
3587
Wale Ogunwale494009b82016-10-21 09:01:38 -07003588 if (DEBUG_INPUT_METHOD) {
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003589 Slog.i(TAG_WM, "IM target uid/pid: " + imFocus.mSession.mUid
3590 + "/" + imFocus.mSession.mPid);
3591 Slog.i(TAG_WM, "Requesting client uid/pid: " + uid + "/" + pid);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003592 }
3593
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003594 return imFocus.mSession.mUid == uid && imFocus.mSession.mPid == pid;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003595 }
3596
3597 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003598 final WindowState win = getWindow(
3599 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
3600 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003601 }
3602
Tiger Huang7c610aa2018-10-27 00:01:01 +08003603 void statusBarVisibilityChanged(int visibility) {
3604 mLastStatusBarVisibility = visibility;
3605 visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(visibility);
3606 updateStatusBarVisibilityLocked(visibility);
3607 }
3608
3609 private boolean updateStatusBarVisibilityLocked(int visibility) {
3610 if (mLastDispatchedSystemUiVisibility == visibility) {
3611 return false;
3612 }
3613 final int globalDiff = (visibility ^ mLastDispatchedSystemUiVisibility)
3614 // We are only interested in differences of one of the
3615 // clearable flags...
3616 & View.SYSTEM_UI_CLEARABLE_FLAGS
3617 // ...if it has actually been cleared.
3618 & ~visibility;
3619
3620 mLastDispatchedSystemUiVisibility = visibility;
3621 if (isDefaultDisplay) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003622 mWmService.mInputManager.setSystemUiVisibility(visibility);
Tiger Huang7c610aa2018-10-27 00:01:01 +08003623 }
3624 updateSystemUiVisibility(visibility, globalDiff);
3625 return true;
3626 }
3627
Wale Ogunwale494009b82016-10-21 09:01:38 -07003628 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003629 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003630 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003631 final int curValue = w.mSystemUiVisibility;
3632 final int diff = (curValue ^ visibility) & globalDiff;
3633 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003634 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003635 w.mSeq++;
3636 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003637 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003638 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
3639 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07003640 visibility, newValue, diff);
3641 }
3642 } catch (RemoteException e) {
3643 // so sorry
3644 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003645 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003646 }
3647
Tiger Huang7c610aa2018-10-27 00:01:01 +08003648 void reevaluateStatusBarVisibility() {
3649 int visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(mLastStatusBarVisibility);
3650 if (updateStatusBarVisibilityLocked(visibility)) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003651 mWmService.mWindowPlacerLocked.requestTraversal();
Tiger Huang7c610aa2018-10-27 00:01:01 +08003652 }
3653 }
3654
Wale Ogunwale494009b82016-10-21 09:01:38 -07003655 void onWindowFreezeTimeout() {
3656 Slog.w(TAG_WM, "Window freeze timeout expired.");
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003657 mWmService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003658
3659 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07003660 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003661 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003662 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07003663 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003664 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003665 - mWmService.mDisplayFreezeTime);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003666 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003667 }, true /* traverseTopToBottom */);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003668 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003669 }
3670
3671 void waitForAllWindowsDrawn() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003672 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003673 forAllWindows(w -> {
3674 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
3675 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
3676 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003677 // Force add to mResizingWindows.
chaviw9c81e632018-07-31 11:17:52 -07003678 w.resetLastContentInsets();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003679 mWmService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003680 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003681 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003682 }
3683
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003684 // TODO: Super crazy long method that should be broken down...
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003685 void applySurfaceChangesTransaction(boolean recoveringMemory) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003686 final WindowSurfacePlacer surfacePlacer = mWmService.mWindowPlacerLocked;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003687
3688 mTmpUpdateAllDrawn.clear();
3689
3690 int repeats = 0;
3691 do {
3692 repeats++;
3693 if (repeats > 6) {
3694 Slog.w(TAG, "Animation repeat aborted after too many iterations");
3695 clearLayoutNeeded();
3696 break;
3697 }
3698
3699 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
3700 pendingLayoutChanges);
3701
wilsonshihc32538e2018-11-07 17:27:34 +08003702 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
3703 mWallpaperController.adjustWallpaperWindows();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003704 }
3705
Riddle Hsu654a6f92018-07-13 22:59:36 +08003706 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003707 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Riddle Hsu4e611772018-10-31 18:58:28 +08003708 if (updateOrientationFromAppTokens()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003709 setLayoutNeeded();
Riddle Hsu4e611772018-10-31 18:58:28 +08003710 sendNewConfiguration();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003711 }
3712 }
3713
3714 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3715 setLayoutNeeded();
3716 }
3717
3718 // FIRST LOOP: Perform a layout, if needed.
3719 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
3720 performLayout(repeats == 1, false /* updateInputWindows */);
3721 } else {
3722 Slog.w(TAG, "Layout repeat skipped after too many iterations");
3723 }
3724
3725 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
3726 pendingLayoutChanges = 0;
3727
Jorim Jaggi4981f152019-03-26 18:58:45 +01003728 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "applyPostLayoutPolicy");
3729 try {
3730 mDisplayPolicy.beginPostLayoutPolicyLw();
3731 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
3732 pendingLayoutChanges |= mDisplayPolicy.finishPostLayoutPolicyLw();
3733 } finally {
3734 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
3735 }
Tiger Huang7c610aa2018-10-27 00:01:01 +08003736 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
3737 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
Jorim Jaggif96c90a2018-09-26 16:55:15 +02003738 mInsetsStateController.onPostLayout();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003739 } while (pendingLayoutChanges != 0);
3740
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003741 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003742
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003743 mTmpRecoveringMemory = recoveringMemory;
Jorim Jaggi4981f152019-03-26 18:58:45 +01003744
3745 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "applyWindowSurfaceChanges");
3746 try {
3747 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
3748 } finally {
3749 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
3750 }
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01003751 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003752
Jorim Jaggif1292892018-09-10 11:58:13 +02003753 mLastHasContent = mTmpApplySurfaceChangesTransactionState.displayHasContent;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003754 mWmService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Jorim Jaggif1292892018-09-10 11:58:13 +02003755 mLastHasContent,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003756 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
3757 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003758 true /* inTraversal, must call performTraversalInTrans... below */);
3759
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003760 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
3761 if (wallpaperVisible != mLastWallpaperVisible) {
3762 mLastWallpaperVisible = wallpaperVisible;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003763 mWmService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003764 }
3765
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003766 while (!mTmpUpdateAllDrawn.isEmpty()) {
3767 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
3768 // See if any windows have been drawn, so they (and others associated with them)
3769 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07003770 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003771 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003772 }
3773
Bryce Leef3c6a472017-11-14 14:53:06 -08003774 private void updateBounds() {
Evan Rosky39b6f232018-10-30 18:35:41 -07003775 calculateBounds(mDisplayInfo, mTmpBounds);
Bryce Leef3c6a472017-11-14 14:53:06 -08003776 setBounds(mTmpBounds);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08003777 if (mPortalWindowHandle != null && mParentSurfaceControl != null) {
3778 mPortalWindowHandle.touchableRegion.getBounds(mTmpRect);
3779 if (!mTmpBounds.equals(mTmpRect)) {
3780 mPortalWindowHandle.touchableRegion.set(mTmpBounds);
Tiger Huanged6794e2019-05-07 20:07:59 +08003781 getPendingTransaction().setInputWindowInfo(
3782 mParentSurfaceControl, mPortalWindowHandle);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08003783 }
3784 }
Bryce Leef3c6a472017-11-14 14:53:06 -08003785 }
3786
3787 // Determines the current display bounds based on the current state
Evan Rosky39b6f232018-10-30 18:35:41 -07003788 private void calculateBounds(DisplayInfo displayInfo, Rect out) {
Bryce Leef3c6a472017-11-14 14:53:06 -08003789 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Evan Rosky39b6f232018-10-30 18:35:41 -07003790 final int rotation = displayInfo.rotation;
3791 boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Bryce Leef3c6a472017-11-14 14:53:06 -08003792 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
3793 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Evan Rosky39b6f232018-10-30 18:35:41 -07003794 int width = displayInfo.logicalWidth;
Bryce Leef3c6a472017-11-14 14:53:06 -08003795 int left = (physWidth - width) / 2;
Evan Rosky39b6f232018-10-30 18:35:41 -07003796 int height = displayInfo.logicalHeight;
Bryce Leef3c6a472017-11-14 14:53:06 -08003797 int top = (physHeight - height) / 2;
3798 out.set(left, top, left + width, top + height);
3799 }
3800
Bryce Leef3c6a472017-11-14 14:53:06 -08003801 private void getBounds(Rect out, int orientation) {
3802 getBounds(out);
3803
3804 // Rotate the Rect if needed.
3805 final int currentRotation = mDisplayInfo.rotation;
3806 final int rotationDelta = deltaRotation(currentRotation, orientation);
3807 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3808 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3809 mTmpRectF.set(out);
3810 mTmpMatrix.mapRect(mTmpRectF);
3811 mTmpRectF.round(out);
3812 }
3813 }
3814
Evan Rosky730f6e82018-12-03 17:40:11 -08003815 /** @returns the orientation of the display when it's rotation is ROTATION_0. */
3816 int getNaturalOrientation() {
3817 return mBaseDisplayWidth < mBaseDisplayHeight
3818 ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
3819 }
3820
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003821 void performLayout(boolean initial, boolean updateInputWindows) {
Jorim Jaggi4981f152019-03-26 18:58:45 +01003822 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "performLayout");
3823 try {
3824 performLayoutNoTrace(initial, updateInputWindows);
3825 } finally {
3826 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
3827 }
3828 }
3829
3830 private void performLayoutNoTrace(boolean initial, boolean updateInputWindows) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003831 if (!isLayoutNeeded()) {
3832 return;
3833 }
3834 clearLayoutNeeded();
3835
3836 final int dw = mDisplayInfo.logicalWidth;
3837 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003838 if (DEBUG_LAYOUT) {
3839 Slog.v(TAG, "-------------------------------------");
Jorim Jaggi4981f152019-03-26 18:58:45 +01003840 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw
3841 + " dh=" + dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003842 }
3843
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003844 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3845 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Jorim Jaggi4981f152019-03-26 18:58:45 +01003846 // TODO: Not sure if we really need to set the rotation here since we are updating from
3847 // the display info above...
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00003848 mDisplayFrames.mRotation = mRotation;
Tiger Huang7c610aa2018-10-27 00:01:01 +08003849 mDisplayPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003850
Adrian Roos5251b1d2018-03-23 18:57:43 +01003851 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003852 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003853 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003854
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003855 // Used to indicate that we have processed the dream window and all additional windows are
3856 // behind it.
3857 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003858 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003859
3860 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003861 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003862
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003863 // Used to indicate that we have processed the dream window and all additional attached
3864 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003865 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003866 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003867
3868 // Now perform layout of attached windows, which usually depend on the position of the
3869 // window they are attached to. XXX does not deal with windows that are attached to windows
3870 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003871 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003872
3873 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003874 mInputMonitor.layoutInputConsumers(dw, dh);
3875 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003876 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003877 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003878 }
3879
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003880 mWmService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003881 }
3882
3883 /**
3884 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3885 * In portrait mode, it grabs the full screenshot.
3886 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003887 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003888 */
chaviw0315a1a2018-03-05 15:28:35 -08003889 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003890 if (!mWmService.mPolicy.isScreenOn()) {
chaviw0315a1a2018-03-05 15:28:35 -08003891 if (DEBUG_SCREENSHOT) {
3892 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003893 }
chaviw0315a1a2018-03-05 15:28:35 -08003894 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003895 }
chaviwfbe47df2017-11-10 16:14:49 -08003896
chaviw0315a1a2018-03-05 15:28:35 -08003897 int dw = mDisplayInfo.logicalWidth;
3898 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003899
chaviw0315a1a2018-03-05 15:28:35 -08003900 if (dw <= 0 || dh <= 0) {
3901 return null;
3902 }
chaviwfbe47df2017-11-10 16:14:49 -08003903
chaviw0315a1a2018-03-05 15:28:35 -08003904 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003905
chaviw0315a1a2018-03-05 15:28:35 -08003906 // The screenshot API does not apply the current screen rotation.
3907 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003908
chaviw0315a1a2018-03-05 15:28:35 -08003909 if (rot == ROTATION_90 || rot == ROTATION_270) {
3910 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3911 }
chaviwfbe47df2017-11-10 16:14:49 -08003912
chaviw0315a1a2018-03-05 15:28:35 -08003913 // SurfaceFlinger is not aware of orientation, so convert our logical
3914 // crop to SurfaceFlinger's portrait orientation.
3915 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3916
3917 final ScreenRotationAnimation screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003918 mWmService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
chaviw0315a1a2018-03-05 15:28:35 -08003919 final boolean inRotation = screenRotationAnimation != null &&
3920 screenRotationAnimation.isAnimating();
3921 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3922
3923 // TODO(b/68392460): We should screenshot Task controls directly
3924 // but it's difficult at the moment as the Task doesn't have the
3925 // correct size set.
chaviw08520a02018-09-10 16:44:56 -07003926 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, inRotation, rot);
chaviw0315a1a2018-03-05 15:28:35 -08003927 if (bitmap == null) {
3928 Slog.w(TAG_WM, "Failed to take screenshot");
3929 return null;
3930 }
3931
3932 // Create a copy of the screenshot that is immutable and backed in ashmem.
3933 // This greatly reduces the overhead of passing the bitmap between processes.
3934 final Bitmap ret = bitmap.createAshmemBitmap(config);
3935 bitmap.recycle();
3936 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003937 }
3938
3939 // TODO: Can this use createRotationMatrix()?
3940 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3941 if (rot == Surface.ROTATION_90) {
3942 final int tmp = crop.top;
3943 crop.top = dw - crop.right;
3944 crop.right = crop.bottom;
3945 crop.bottom = dw - crop.left;
3946 crop.left = tmp;
3947 } else if (rot == Surface.ROTATION_180) {
3948 int tmp = crop.top;
3949 crop.top = dh - crop.bottom;
3950 crop.bottom = dh - tmp;
3951 tmp = crop.right;
3952 crop.right = dw - crop.left;
3953 crop.left = dw - tmp;
3954 } else if (rot == Surface.ROTATION_270) {
3955 final int tmp = crop.top;
3956 crop.top = crop.left;
3957 crop.left = dh - crop.bottom;
3958 crop.bottom = crop.right;
3959 crop.right = dh - tmp;
3960 }
3961 }
3962
3963 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003964 // Used to indicate the layout is needed.
3965 mTmpWindow = null;
3966
3967 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003968 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003969 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003970 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003971 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003972 w.setDisplayLayoutNeeded();
Vishnu Nairba183352018-11-21 11:16:49 -08003973 w.finishSeamlessRotation(true /* timeout */);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003974 mWmService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003975 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003976
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003977 if (mTmpWindow != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003978 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003979 }
3980 }
3981
Wale Ogunwale1666e312016-12-16 11:27:18 -08003982 void setExitingTokensHasVisible(boolean hasVisible) {
3983 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3984 mExitingTokens.get(i).hasVisible = hasVisible;
3985 }
3986
3987 // Initialize state of exiting applications.
3988 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3989 }
3990
3991 void removeExistingTokensIfPossible() {
3992 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3993 final WindowToken token = mExitingTokens.get(i);
3994 if (!token.hasVisible) {
3995 mExitingTokens.remove(i);
3996 }
3997 }
3998
3999 // Time to remove any exiting applications?
4000 mTaskStackContainers.removeExistingAppTokensIfPossible();
4001 }
4002
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07004003 @Override
4004 void onDescendantOverrideConfigurationChanged() {
4005 setLayoutNeeded();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004006 mWmService.requestTraversal();
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07004007 }
4008
David Stevens9440dc82017-03-16 19:00:20 -07004009 boolean okToDisplay() {
4010 if (mDisplayId == DEFAULT_DISPLAY) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004011 return !mWmService.mDisplayFrozen
4012 && mWmService.mDisplayEnabled && mWmService.mPolicy.isScreenOn();
David Stevens9440dc82017-03-16 19:00:20 -07004013 }
4014 return mDisplayInfo.state == Display.STATE_ON;
4015 }
4016
4017 boolean okToAnimate() {
4018 return okToDisplay() &&
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004019 (mDisplayId != DEFAULT_DISPLAY || mWmService.mPolicy.okToAnimate());
David Stevens9440dc82017-03-16 19:00:20 -07004020 }
4021
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07004022 static final class TaskForResizePointSearchResult {
4023 boolean searchDone;
4024 Task taskForResize;
4025
4026 void reset() {
4027 searchDone = false;
4028 taskForResize = null;
4029 }
4030 }
Robert Carr3b716242016-08-16 16:02:21 -07004031
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004032 private static final class ApplySurfaceChangesTransactionState {
4033 boolean displayHasContent;
4034 boolean obscured;
4035 boolean syswin;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004036 float preferredRefreshRate;
4037 int preferredModeId;
4038
4039 void reset() {
4040 displayHasContent = false;
4041 obscured = false;
4042 syswin = false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004043 preferredRefreshRate = 0;
4044 preferredModeId = 0;
4045 }
4046 }
4047
4048 private static final class ScreenshotApplicationState {
4049 WindowState appWin;
4050 int maxLayer;
4051 int minLayer;
4052 boolean screenshotReady;
4053
4054 void reset(boolean screenshotReady) {
4055 appWin = null;
4056 maxLayer = 0;
4057 minLayer = 0;
4058 this.screenshotReady = screenshotReady;
4059 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
4060 }
4061 }
4062
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004063 /**
4064 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
4065 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
4066 * homogeneous children type which is currently required by sub-classes of
4067 * {@link WindowContainer} class.
4068 */
4069 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
4070
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004071 DisplayChildWindowContainer(WindowManagerService service) {
4072 super(service);
4073 }
4074
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004075 @Override
4076 boolean fillsParent() {
4077 return true;
4078 }
4079
4080 @Override
4081 boolean isVisible() {
4082 return true;
Robert Carr3b716242016-08-16 16:02:21 -07004083 }
4084 }
4085
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004086 /**
4087 * Window container class that contains all containers on this display relating to Apps.
4088 * I.e Activities.
4089 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07004090 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07004091 /**
4092 * A control placed at the appropriate level for transitions to occur.
4093 */
chaviw23ee71c2017-12-18 11:29:41 -08004094 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02004095 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02004096 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004097
Robert Carrf7a7ca82017-12-06 13:17:07 -08004098 /**
4099 * Given that the split-screen divider does not have an AppWindowToken, it
4100 * will have to live inside of a "NonAppWindowContainer", in particular
4101 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
4102 * it will need to be interleaved with some of our children, appearing on top of
4103 * both docked stacks but underneath any assistant stacks.
4104 *
4105 * To solve this problem we have this anchor control, which will always exist so
4106 * we can always assign it the correct value in our {@link #assignChildLayers}.
4107 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
4108 * assign the divider a layer relative to it. This way we prevent linking lifecycle
4109 * events between the two containers.
4110 */
4111 SurfaceControl mSplitScreenDividerAnchor = null;
4112
Wale Ogunwale61911492017-10-11 08:50:50 -07004113 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
4114 // through the list to find them.
4115 private TaskStack mHomeStack = null;
4116 private TaskStack mPinnedStack = null;
4117 private TaskStack mSplitScreenPrimaryStack = null;
4118
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004119 TaskStackContainers(WindowManagerService service) {
4120 super(service);
4121 }
4122
Wale Ogunwale61911492017-10-11 08:50:50 -07004123 /**
4124 * Returns the topmost stack on the display that is compatible with the input windowing mode
4125 * and activity type. Null is no compatible stack on the display.
4126 */
4127 TaskStack getStack(int windowingMode, int activityType) {
4128 if (activityType == ACTIVITY_TYPE_HOME) {
4129 return mHomeStack;
4130 }
4131 if (windowingMode == WINDOWING_MODE_PINNED) {
4132 return mPinnedStack;
4133 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4134 return mSplitScreenPrimaryStack;
4135 }
4136 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
4137 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004138 if (activityType == ACTIVITY_TYPE_UNDEFINED
4139 && windowingMode == stack.getWindowingMode()) {
4140 // Passing in undefined type means we want to match the topmost stack with the
4141 // windowing mode.
4142 return stack;
4143 }
Wale Ogunwale61911492017-10-11 08:50:50 -07004144 if (stack.isCompatible(windowingMode, activityType)) {
4145 return stack;
4146 }
4147 }
4148 return null;
4149 }
4150
4151 @VisibleForTesting
4152 TaskStack getTopStack() {
4153 return mTaskStackContainers.getChildCount() > 0
4154 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
4155 }
4156
4157 TaskStack getHomeStack() {
4158 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
4159 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
4160 }
4161 return mHomeStack;
4162 }
4163
4164 TaskStack getPinnedStack() {
4165 return mPinnedStack;
4166 }
4167
Matthew Ng64e77cf2017-10-31 14:01:31 -07004168 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07004169 return mSplitScreenPrimaryStack;
4170 }
4171
Winson Chunge2d72172018-01-25 17:46:20 +00004172 ArrayList<Task> getVisibleTasks() {
4173 final ArrayList<Task> visibleTasks = new ArrayList<>();
4174 forAllTasks(task -> {
4175 if (task.isVisible()) {
4176 visibleTasks.add(task);
4177 }
4178 });
4179 return visibleTasks;
4180 }
4181
Andrii Kulian839def92016-11-02 10:58:58 -07004182 /**
4183 * Adds the stack to this container.
Andrii Kulian839def92016-11-02 10:58:58 -07004184 */
4185 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07004186 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004187 addChild(stack, onTop);
4188 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07004189 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004190
Wale Ogunwale61911492017-10-11 08:50:50 -07004191 void onStackWindowingModeChanged(TaskStack stack) {
4192 removeStackReferenceIfNeeded(stack);
4193 addStackReferenceIfNeeded(stack);
4194 if (stack == mPinnedStack && getTopStack() != stack) {
4195 // Looks like this stack changed windowing mode to pinned. Move it to the top.
4196 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
4197 }
4198 }
4199
4200 private void addStackReferenceIfNeeded(TaskStack stack) {
4201 if (stack.isActivityTypeHome()) {
4202 if (mHomeStack != null) {
Louis Chang7d0037c2018-08-13 12:42:06 +08004203 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07004204 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang7d0037c2018-08-13 12:42:06 +08004205
Wale Ogunwale61911492017-10-11 08:50:50 -07004206 }
Louis Chang7d0037c2018-08-13 12:42:06 +08004207 mHomeStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07004208 }
4209 final int windowingMode = stack.getWindowingMode();
4210 if (windowingMode == WINDOWING_MODE_PINNED) {
4211 if (mPinnedStack != null) {
4212 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
4213 + mPinnedStack + " already exist on display=" + this
4214 + " stack=" + stack);
4215 }
4216 mPinnedStack = stack;
4217 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4218 if (mSplitScreenPrimaryStack != null) {
4219 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
4220 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
4221 + " already exist on display=" + this + " stack=" + stack);
4222 }
4223 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004224 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07004225 }
4226 }
4227
4228 private void removeStackReferenceIfNeeded(TaskStack stack) {
4229 if (stack == mHomeStack) {
4230 mHomeStack = null;
4231 } else if (stack == mPinnedStack) {
4232 mPinnedStack = null;
4233 } else if (stack == mSplitScreenPrimaryStack) {
4234 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004235 // Re-set the split-screen create mode whenever the split-screen stack is removed.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004236 mWmService.setDockedStackCreateStateLocked(
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004237 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
4238 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07004239 }
Andrii Kulian839def92016-11-02 10:58:58 -07004240 }
4241
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004242 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004243 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
4244 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004245 addChild(stack, addIndex);
4246 setLayoutNeeded();
4247 }
4248
Wale Ogunwale61911492017-10-11 08:50:50 -07004249 @Override
4250 protected void removeChild(TaskStack stack) {
4251 super.removeChild(stack);
4252 removeStackReferenceIfNeeded(stack);
4253 }
Bryce Lee00d586d2017-07-28 20:48:43 -07004254
4255 @Override
4256 boolean isOnTop() {
4257 // Considered always on top
4258 return true;
4259 }
4260
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004261 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08004262 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07004263 if (child.getWindowConfiguration().isAlwaysOnTop()
4264 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08004265 // This stack is always-on-top, override the default behavior.
4266 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
4267
4268 // Moving to its current position, as we must call super but we don't want to
4269 // perform any meaningful action.
4270 final int currentPosition = mChildren.indexOf(child);
4271 super.positionChildAt(currentPosition, child, false /* includingParents */);
4272 return;
4273 }
4274
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004275 final int targetPosition = findPositionForStack(position, child, false /* adding */);
4276 super.positionChildAt(targetPosition, child, includingParents);
4277
Riddle Hsu2f6e1742018-08-23 22:44:36 +08004278 if (includingParents) {
4279 // We still want to move the display of this stack container to top because even the
4280 // target position is adjusted to non-top, the intention of the condition is to have
4281 // higher z-order to gain focus (e.g. moving a task of a fullscreen stack to front
4282 // in a non-top display which is using picture-in-picture mode).
4283 final int topChildPosition = getChildCount() - 1;
4284 if (targetPosition < topChildPosition && position >= topChildPosition) {
4285 getParent().positionChildAt(POSITION_TOP, this /* child */,
4286 true /* includingParents */);
4287 }
4288 }
4289
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004290 setLayoutNeeded();
4291 }
4292
4293 /**
4294 * When stack is added or repositioned, find a proper position for it.
4295 * This will make sure that pinned stack always stays on top.
4296 * @param requestedPosition Position requested by caller.
4297 * @param stack Stack to be added or positioned.
4298 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
4299 * @return The proper position for the stack.
4300 */
4301 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004302 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09004303 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09004304 }
4305
Kazuki Takisef85197b2018-06-18 18:18:36 +09004306 final int topChildPosition = mChildren.size() - 1;
4307 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
4308 for (int i = topChildPosition; i >= 0; --i) {
4309 if (getStacks().get(i) != stack && !getStacks().get(i).isAlwaysOnTop()) {
4310 belowAlwaysOnTopPosition = i;
4311 break;
4312 }
4313 }
4314
4315 // The max possible position we can insert the stack at.
4316 int maxPosition = POSITION_TOP;
4317 // The min possible position we can insert the stack at.
4318 int minPosition = POSITION_BOTTOM;
4319
4320 if (stack.isAlwaysOnTop()) {
4321 if (hasPinnedStack()) {
4322 // Always-on-top stacks go below the pinned stack.
4323 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
4324 }
4325 // Always-on-top stacks need to be above all other stacks.
4326 minPosition = belowAlwaysOnTopPosition !=
4327 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
4328 } else {
4329 // Other stacks need to be below the always-on-top stacks.
4330 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08004331 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09004332 }
4333
4334 int targetPosition = requestedPosition;
4335 targetPosition = Math.min(targetPosition, maxPosition);
4336 targetPosition = Math.max(targetPosition, minPosition);
4337
4338 int prevPosition = getStacks().indexOf(stack);
4339 // The positions we calculated above (maxPosition, minPosition) do not take into
4340 // consideration the following edge cases.
4341 // 1) We need to adjust the position depending on the value "adding".
4342 // 2) When we are moving a stack to another position, we also need to adjust the
4343 // position depending on whether the stack is moving to a higher or lower position.
4344 if ((targetPosition != requestedPosition) &&
4345 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004346 targetPosition++;
4347 }
4348
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004349 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08004350 }
4351
4352 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004353 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
4354 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004355 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004356 if (super.forAllWindows(callback, traverseTopToBottom)) {
4357 return true;
4358 }
4359 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4360 return true;
4361 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004362 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004363 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4364 return true;
4365 }
4366 if (super.forAllWindows(callback, traverseTopToBottom)) {
4367 return true;
4368 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004369 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004370 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004371 }
4372
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004373 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004374 boolean traverseTopToBottom) {
4375 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
4376 // app tokens.
4377 // TODO: Investigate if we need to continue to do this or if we can just process them
4378 // in-order.
4379 if (traverseTopToBottom) {
4380 for (int i = mChildren.size() - 1; i >= 0; --i) {
4381 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4382 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004383 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4384 traverseTopToBottom)) {
4385 return true;
4386 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004387 }
4388 }
4389 } else {
4390 final int count = mChildren.size();
4391 for (int i = 0; i < count; ++i) {
4392 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4393 final int appTokensCount = appTokens.size();
4394 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004395 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4396 traverseTopToBottom)) {
4397 return true;
4398 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004399 }
4400 }
4401 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004402 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004403 }
4404
Wale Ogunwale1666e312016-12-16 11:27:18 -08004405 void setExitingTokensHasVisible(boolean hasVisible) {
4406 for (int i = mChildren.size() - 1; i >= 0; --i) {
4407 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4408 for (int j = appTokens.size() - 1; j >= 0; --j) {
4409 appTokens.get(j).hasVisible = hasVisible;
4410 }
4411 }
4412 }
4413
4414 void removeExistingAppTokensIfPossible() {
4415 for (int i = mChildren.size() - 1; i >= 0; --i) {
4416 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4417 for (int j = appTokens.size() - 1; j >= 0; --j) {
4418 final AppWindowToken token = appTokens.get(j);
lumark588a3e82018-07-20 18:53:54 +08004419 if (!token.hasVisible && !mClosingApps.contains(token)
Wale Ogunwale1666e312016-12-16 11:27:18 -08004420 && (!token.mIsExiting || token.isEmpty())) {
4421 // Make sure there is no animation running on this token, so any windows
4422 // associated with it will be removed as soon as their animations are
4423 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02004424 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004425 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
4426 "performLayout: App token exiting now removed" + token);
4427 token.removeIfPossible();
4428 }
4429 }
4430 }
4431 }
4432
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004433 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004434 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004435 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
4436 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004437 // Apps and their containers are not allowed to specify an orientation while the
Wale Ogunwaled0c225c2018-06-15 14:47:46 -07004438 // docked or freeform stack is visible...except for the home stack if the docked
4439 // stack is minimized and it actually set something and the bounds is different from
4440 // the display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004441 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004442 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004443 && !(mDividerControllerLocked.isHomeStackResizable()
4444 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004445 final int orientation = mHomeStack.getOrientation();
4446 if (orientation != SCREEN_ORIENTATION_UNSET) {
4447 return orientation;
4448 }
4449 }
4450 return SCREEN_ORIENTATION_UNSPECIFIED;
4451 }
4452
4453 final int orientation = super.getOrientation();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004454 boolean isCar = mWmService.mContext.getPackageManager().hasSystemFeature(
Dean Harding3e5a1522017-10-06 09:19:01 -07004455 PackageManager.FEATURE_AUTOMOTIVE);
4456 if (isCar) {
4457 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
4458 // allow anything but the default orientation.
4459 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004460 "Forcing UNSPECIFIED orientation in car for display id=" + mDisplayId
4461 + ". Ignoring " + orientation);
Dean Harding3e5a1522017-10-06 09:19:01 -07004462 return SCREEN_ORIENTATION_UNSPECIFIED;
4463 }
4464
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004465 if (orientation != SCREEN_ORIENTATION_UNSET
4466 && orientation != SCREEN_ORIENTATION_BEHIND) {
4467 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004468 "App is requesting an orientation, return " + orientation
4469 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004470 return orientation;
4471 }
4472
4473 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004474 "No app is requesting an orientation, return " + mLastOrientation
4475 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004476 // The next app has not been requested to be visible, so we keep the current orientation
4477 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08004478 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004479 }
Robert Carrb1579c82017-09-05 14:54:47 -07004480
4481 @Override
4482 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004483 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004484
Robert Carr2f8aa392018-01-31 14:46:51 -08004485 for (int i = 0; i < mChildren.size(); i++) {
4486 final TaskStack s = mChildren.get(i);
4487 s.assignChildLayers(t);
4488 }
4489 }
4490
4491 void assignStackOrdering(SurfaceControl.Transaction t) {
Jorim Jaggibe418292018-03-26 16:14:12 +02004492
Robert Carrf7a7ca82017-12-06 13:17:07 -08004493 final int HOME_STACK_STATE = 0;
4494 final int NORMAL_STACK_STATE = 1;
4495 final int ALWAYS_ON_TOP_STATE = 2;
4496
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004497 int layer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004498 int layerForAnimationLayer = 0;
Jorim Jaggibe418292018-03-26 16:14:12 +02004499 int layerForBoostedAnimationLayer = 0;
Jorim Jaggi391790622018-04-18 15:30:44 +02004500 int layerForHomeAnimationLayer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004501
Robert Carrf7a7ca82017-12-06 13:17:07 -08004502 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
4503 for (int i = 0; i < mChildren.size(); i++) {
4504 final TaskStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08004505 if (state == HOME_STACK_STATE && !s.isActivityTypeHome()) {
4506 continue;
4507 } else if (state == NORMAL_STACK_STATE && (s.isActivityTypeHome()
4508 || s.isAlwaysOnTop())) {
4509 continue;
4510 } else if (state == ALWAYS_ON_TOP_STATE && !s.isAlwaysOnTop()) {
4511 continue;
4512 }
4513 s.assignLayer(t, layer++);
4514 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
4515 t.setLayer(mSplitScreenDividerAnchor, layer++);
4516 }
Winson Chungd41f71d2018-03-16 15:26:07 -07004517 if ((s.isTaskAnimating() || s.isAppAnimating())
4518 && state != ALWAYS_ON_TOP_STATE) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004519 // Ensure the animation layer ends up above the
4520 // highest animating stack and no higher.
4521 layerForAnimationLayer = layer++;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004522 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004523 if (state != ALWAYS_ON_TOP_STATE) {
4524 layerForBoostedAnimationLayer = layer++;
4525 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08004526 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004527 if (state == HOME_STACK_STATE) {
4528 layerForHomeAnimationLayer = layer++;
4529 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004530 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004531 if (mAppAnimationLayer != null) {
4532 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004533 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004534 if (mBoostedAppAnimationLayer != null) {
4535 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
4536 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004537 if (mHomeAppAnimationLayer != null) {
4538 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
4539 }
Robert Carrb1579c82017-09-05 14:54:47 -07004540 }
4541
4542 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02004543 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
4544 switch (animationLayer) {
4545 case ANIMATION_LAYER_BOOSTED:
4546 return mBoostedAppAnimationLayer;
4547 case ANIMATION_LAYER_HOME:
4548 return mHomeAppAnimationLayer;
4549 case ANIMATION_LAYER_STANDARD:
4550 default:
4551 return mAppAnimationLayer;
4552 }
chaviw23ee71c2017-12-18 11:29:41 -08004553 }
4554
Robert Carrf7a7ca82017-12-06 13:17:07 -08004555 SurfaceControl getSplitScreenDividerAnchor() {
4556 return mSplitScreenDividerAnchor;
4557 }
4558
chaviw23ee71c2017-12-18 11:29:41 -08004559 @Override
Riddle Hsu3a4bb612019-01-31 00:02:22 +08004560 void onParentChanged() {
4561 super.onParentChanged();
Robert Carrb1579c82017-09-05 14:54:47 -07004562 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08004563 mAppAnimationLayer = makeChildSurface(null)
4564 .setName("animationLayer")
4565 .build();
Jorim Jaggibe418292018-03-26 16:14:12 +02004566 mBoostedAppAnimationLayer = makeChildSurface(null)
4567 .setName("boostedAnimationLayer")
4568 .build();
Jorim Jaggi391790622018-04-18 15:30:44 +02004569 mHomeAppAnimationLayer = makeChildSurface(null)
4570 .setName("homeAnimationLayer")
4571 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08004572 mSplitScreenDividerAnchor = makeChildSurface(null)
4573 .setName("splitScreenDividerAnchor")
4574 .build();
4575 getPendingTransaction()
4576 .show(mAppAnimationLayer)
Jorim Jaggibe418292018-03-26 16:14:12 +02004577 .show(mBoostedAppAnimationLayer)
Jorim Jaggi391790622018-04-18 15:30:44 +02004578 .show(mHomeAppAnimationLayer)
Robert Carrf7a7ca82017-12-06 13:17:07 -08004579 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08004580 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07004581 } else {
Robert Carr5ea304d2019-02-04 16:04:55 -08004582 mAppAnimationLayer.remove();
chaviw23ee71c2017-12-18 11:29:41 -08004583 mAppAnimationLayer = null;
Robert Carr5ea304d2019-02-04 16:04:55 -08004584 mBoostedAppAnimationLayer.remove();
Jorim Jaggibe418292018-03-26 16:14:12 +02004585 mBoostedAppAnimationLayer = null;
Robert Carr5ea304d2019-02-04 16:04:55 -08004586 mHomeAppAnimationLayer.remove();
Jorim Jaggi391790622018-04-18 15:30:44 +02004587 mHomeAppAnimationLayer = null;
Robert Carr5ea304d2019-02-04 16:04:55 -08004588 mSplitScreenDividerAnchor.remove();
Robert Carrf7a7ca82017-12-06 13:17:07 -08004589 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07004590 }
4591 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004592 }
4593
Robert Carree4d4b92017-11-22 12:21:46 -08004594 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004595 AboveAppWindowContainers(String name, WindowManagerService service) {
4596 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08004597 }
4598
Robert Carrb9506032018-02-13 13:54:00 -08004599 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07004600 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
4601 final SurfaceControl.Builder builder = super.makeChildSurface(child);
4602 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
4603 // To draw above the ColorFade layer during the screen off transition, the
4604 // rounded corner overlays need to be at the root of the surface hierarchy.
4605 // TODO: move the ColorLayer into the display overlay layer such that this is not
4606 // necessary anymore.
4607 builder.setParent(null);
4608 }
4609 return builder;
4610 }
4611
4612 @Override
Robert Carrb9506032018-02-13 13:54:00 -08004613 void assignChildLayers(SurfaceControl.Transaction t) {
4614 assignChildLayers(t, null /* imeContainer */);
4615 }
4616
Robert Carree4d4b92017-11-22 12:21:46 -08004617 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
4618 boolean needAssignIme = imeContainer != null
4619 && imeContainer.getSurfaceControl() != null;
4620 for (int j = 0; j < mChildren.size(); ++j) {
4621 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004622
4623 // See {@link mSplitScreenDividerAnchor}
4624 if (wt.windowType == TYPE_DOCK_DIVIDER) {
4625 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
4626 continue;
4627 }
Vishnu Nair83537a72018-07-19 21:27:48 -07004628 if (wt.mRoundedCornerOverlay) {
4629 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
4630 continue;
4631 }
Robert Carree4d4b92017-11-22 12:21:46 -08004632 wt.assignLayer(t, j);
4633 wt.assignChildLayers(t);
4634
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004635 int layer = mWmService.mPolicy.getWindowLayerFromTypeLw(
Robert Carree4d4b92017-11-22 12:21:46 -08004636 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08004637
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004638 if (needAssignIme && layer >= mWmService.mPolicy.getWindowLayerFromTypeLw(
Robert Carrb8e78fa2017-11-28 13:07:58 -08004639 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004640 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08004641 needAssignIme = false;
4642 }
4643 }
4644 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004645 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08004646 }
4647 }
4648 }
4649
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004650 /**
4651 * Window container class that contains all containers on this display that are not related to
4652 * Apps. E.g. status bar.
4653 */
Robert Carree4d4b92017-11-22 12:21:46 -08004654 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07004655 /**
4656 * Compares two child window tokens returns -1 if the first is lesser than the second in
4657 * terms of z-order and 1 otherwise.
4658 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004659 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07004660 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004661 mWmService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004662 token1.mOwnerCanManageAppTokens)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004663 < mWmService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004664 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004665
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004666 private final Predicate<WindowState> mGetOrientingWindow = w -> {
chaviw15ad49f2019-04-24 15:05:39 -07004667 if (!w.isVisibleLw() || !w.mLegacyPolicyVisibilityAfterAnim) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004668 return false;
4669 }
4670 final int req = w.mAttrs.screenOrientation;
4671 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
4672 || req == SCREEN_ORIENTATION_UNSET) {
4673 return false;
4674 }
4675 return true;
4676 };
4677
Wale Ogunwale3a931692016-11-02 16:49:48 -07004678 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08004679 private final Dimmer mDimmer = new Dimmer(this);
4680 private final Rect mTmpDimBoundsRect = new Rect();
4681
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004682 NonAppWindowContainers(String name, WindowManagerService service) {
4683 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004684 mName = name;
4685 }
4686
4687 void addChild(WindowToken token) {
4688 addChild(token, mWindowComparator);
4689 }
4690
4691 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004692 int getOrientation() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004693 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004694 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004695 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004696
4697 if (win != null) {
4698 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004699 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004700 mLastKeyguardForcedOrientation = req;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004701 if (mWmService.mKeyguardGoingAway) {
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07004702 // Keyguard can't affect the orientation if it is going away...
4703 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
4704 return SCREEN_ORIENTATION_UNSET;
4705 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004706 }
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004707 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req
4708 + " for display id=" + mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08004709 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004710 }
4711
Andrii Kulian8ee72852017-03-10 10:36:45 -08004712 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004713
Jorim Jaggi1c530592018-04-06 15:11:47 +02004714 // Only allow force setting the orientation when all unknown visibilities have been
4715 // resolved, as otherwise we just may be starting another occluding activity.
4716 final boolean isUnoccluding =
lumark588a3e82018-07-20 18:53:54 +08004717 mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
4718 && mUnknownAppVisibilityController.allResolved();
Jorim Jaggi1c530592018-04-06 15:11:47 +02004719 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004720 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004721 }
4722
4723 return SCREEN_ORIENTATION_UNSET;
4724 }
4725
4726 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07004727 String getName() {
4728 return mName;
4729 }
chaviwf29223e2018-01-31 13:23:51 -08004730
4731 @Override
4732 Dimmer getDimmer() {
4733 return mDimmer;
4734 }
4735
4736 @Override
4737 void prepareSurfaces() {
4738 mDimmer.resetDimStates();
4739 super.prepareSurfaces();
4740 getBounds(mTmpDimBoundsRect);
4741
4742 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
4743 scheduleAnimation();
4744 }
4745 }
Robert Carr3b716242016-08-16 16:02:21 -07004746 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01004747
Robert Carrb1579c82017-09-05 14:54:47 -07004748 SurfaceControl.Builder makeSurface(SurfaceSession s) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004749 return mWmService.makeSurfaceBuilder(s)
Robert Carrb1579c82017-09-05 14:54:47 -07004750 .setParent(mWindowingLayer);
4751 }
4752
4753 @Override
4754 SurfaceSession getSession() {
4755 return mSession;
4756 }
4757
4758 @Override
4759 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07004760 SurfaceSession s = child != null ? child.getSession() : getSession();
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +00004761 final SurfaceControl.Builder b = mWmService.makeSurfaceBuilder(s).setContainerLayer();
Robert Carrf59b8dd2017-10-02 18:58:36 -07004762 if (child == null) {
4763 return b;
4764 }
4765
Robert Carree4d4b92017-11-22 12:21:46 -08004766 return b.setName(child.getName())
4767 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004768 }
4769
4770 /**
4771 * The makeSurface variants are for use by the window-container
4772 * hierarchy. makeOverlay here is a function for various non windowing
4773 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
4774 * and other potpourii.
4775 */
4776 SurfaceControl.Builder makeOverlay() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004777 return mWmService.makeSurfaceBuilder(mSession)
Robert Carrb1579c82017-09-05 14:54:47 -07004778 .setParent(mOverlayLayer);
4779 }
4780
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004781 /**
4782 * Reparents the given surface to mOverlayLayer.
4783 */
4784 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
Robert Carr10584fa2019-01-14 15:55:19 -08004785 transaction.reparent(surface, mOverlayLayer);
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004786 }
4787
Robert Carrb1579c82017-09-05 14:54:47 -07004788 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07004789 if (spec.scale != 1.0) {
4790 mMagnificationSpec = spec;
4791 } else {
4792 mMagnificationSpec = null;
4793 }
Jackal Guo818c3f12019-03-08 18:00:39 +08004794 // Re-parent IME's SurfaceControl when MagnificationSpec changed.
4795 updateImeParent();
Robert Carr24be9ab2018-04-30 17:54:53 -07004796
Robert Carrf59b8dd2017-10-02 18:58:36 -07004797 applyMagnificationSpec(getPendingTransaction(), spec);
4798 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07004799 }
4800
Robert Carr24be9ab2018-04-30 17:54:53 -07004801 void reapplyMagnificationSpec() {
4802 if (mMagnificationSpec != null) {
4803 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4804 }
4805 }
4806
Robert Carrb1579c82017-09-05 14:54:47 -07004807 @Override
Riddle Hsu3a4bb612019-01-31 00:02:22 +08004808 void onParentChanged() {
Robert Carrb1579c82017-09-05 14:54:47 -07004809 // Since we are the top of the SurfaceControl hierarchy here
4810 // we create the root surfaces explicitly rather than chaining
Riddle Hsu3a4bb612019-01-31 00:02:22 +08004811 // up as the default implementation in onParentChanged does. So we
Robert Carrb1579c82017-09-05 14:54:47 -07004812 // explicitly do NOT call super here.
4813 }
4814
4815 @Override
4816 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004817
4818 // These are layers as children of "mWindowingLayer"
4819 mBelowAppWindowsContainers.assignLayer(t, 0);
4820 mTaskStackContainers.assignLayer(t, 1);
4821 mAboveAppWindowsContainers.assignLayer(t, 2);
4822
lumarkff0ab692018-11-05 20:32:30 +08004823 final WindowState imeTarget = mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004824 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004825
Robert Carree4d4b92017-11-22 12:21:46 -08004826 // In the case where we have an IME target that is not in split-screen
4827 // mode IME assignment is easy. We just need the IME to go directly above
4828 // the target. This way children of the target will naturally go above the IME
4829 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004830 //
Robert Carree4d4b92017-11-22 12:21:46 -08004831 // In the case of split-screen windowing mode, we need to elevate the IME above the
4832 // docked divider while keeping the app itself below the docked divider, so instead
4833 // we use relative layering of the IME targets child windows, and place the
4834 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004835 //
Robert Carr234b6332018-03-20 13:38:16 -07004836 // In the case the IME target is animating, the animation Z order may be different
4837 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4838 // IME target. In this case we just layer the IME over all transitions by placing it in the
4839 // above applications layer.
4840 //
Robert Carree4d4b92017-11-22 12:21:46 -08004841 // In the case where we have no IME target we assign it where it's base layer would
4842 // place it in the AboveAppWindowContainers.
Robert Carr234b6332018-03-20 13:38:16 -07004843 if (imeTarget != null && !(imeTarget.inSplitScreenWindowingMode()
4844 || imeTarget.mToken.isAppAnimating())
Robert Carree4d4b92017-11-22 12:21:46 -08004845 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004846 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004847 // TODO: We need to use an extra level on the app surface to ensure
4848 // this is always above SurfaceView but always below attached window.
4849 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004850 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004851 }
4852
4853 // Above we have assigned layers to our children, now we ask them to assign
4854 // layers to their children.
4855 mBelowAppWindowsContainers.assignChildLayers(t);
4856 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004857 mAboveAppWindowsContainers.assignChildLayers(t,
4858 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004859 mImeWindowsContainers.assignChildLayers(t);
4860 }
4861
4862 /**
4863 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4864 * that the IME target is one of the docked applications. We'd like the docked divider to be
4865 * above both of the applications, and we'd like the IME to be above the docked divider.
4866 * However we need child windows of the applications to be above the IME (Text drag handles).
4867 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4868 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
Tiger Huang7c610aa2018-10-27 00:01:01 +08004869 * with {@link WindowState#assignLayer}
Robert Carrb1579c82017-09-05 14:54:47 -07004870 */
4871 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004872 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004873 }
4874
4875 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004876 void prepareSurfaces() {
Jorim Jaggi4981f152019-03-26 18:58:45 +01004877 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "prepareSurfaces");
4878 try {
4879 final ScreenRotationAnimation screenRotationAnimation =
4880 mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Tiger Huanged6794e2019-05-07 20:07:59 +08004881 final Transaction transaction = getPendingTransaction();
Jorim Jaggi4981f152019-03-26 18:58:45 +01004882 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
4883 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
Tiger Huanged6794e2019-05-07 20:07:59 +08004884 transaction.setMatrix(mWindowingLayer,
Jorim Jaggi4981f152019-03-26 18:58:45 +01004885 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
4886 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
Tiger Huanged6794e2019-05-07 20:07:59 +08004887 transaction.setPosition(mWindowingLayer,
Jorim Jaggi4981f152019-03-26 18:58:45 +01004888 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
Tiger Huanged6794e2019-05-07 20:07:59 +08004889 transaction.setAlpha(mWindowingLayer,
Jorim Jaggi4981f152019-03-26 18:58:45 +01004890 screenRotationAnimation.getEnterTransformation().getAlpha());
4891 }
Chavi Weingartenb736e322018-02-23 00:27:54 +00004892
Jorim Jaggi4981f152019-03-26 18:58:45 +01004893 super.prepareSurfaces();
Tiger Huanged6794e2019-05-07 20:07:59 +08004894
4895 // TODO: Once we totally eliminate global transaction we will pass transaction in here
4896 // rather than merging to global.
4897 SurfaceControl.mergeToGlobalTransaction(transaction);
Jorim Jaggi4981f152019-03-26 18:58:45 +01004898 } finally {
4899 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
4900 }
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004901 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004902
Jorim Jaggibe418292018-03-26 16:14:12 +02004903 void assignStackOrdering() {
4904 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004905 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004906
4907 /**
4908 * Increment the deferral count to determine whether to update the IME target.
4909 */
4910 void deferUpdateImeTarget() {
4911 mDeferUpdateImeTargetCount++;
4912 }
4913
4914 /**
4915 * Decrement the deferral count to determine whether to update the IME target. If the count
4916 * reaches 0, a new ime target will get computed.
4917 */
4918 void continueUpdateImeTarget() {
4919 if (mDeferUpdateImeTargetCount == 0) {
4920 return;
4921 }
4922
4923 mDeferUpdateImeTargetCount--;
4924 if (mDeferUpdateImeTargetCount == 0) {
4925 computeImeTarget(true /* updateImeTarget */);
4926 }
4927 }
4928
4929 /**
4930 * @return Whether a new IME target should be computed.
4931 */
4932 private boolean canUpdateImeTarget() {
4933 return mDeferUpdateImeTargetCount == 0;
4934 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004935
4936 InputMonitor getInputMonitor() {
4937 return mInputMonitor;
4938 }
Jorim Jaggif1292892018-09-10 11:58:13 +02004939
4940 /**
4941 * @return Cached value whether we told display manager that we have content.
4942 */
4943 boolean getLastHasContent() {
4944 return mLastHasContent;
4945 }
Arthur Hungbe5ce212018-09-13 18:41:56 +08004946
4947 void registerPointerEventListener(@NonNull PointerEventListener listener) {
Riddle Hsu2588ab02019-02-25 14:23:56 +08004948 mPointerEventDispatcher.registerInputEventListener(listener);
Arthur Hungbe5ce212018-09-13 18:41:56 +08004949 }
4950
4951 void unregisterPointerEventListener(@NonNull PointerEventListener listener) {
Riddle Hsu2588ab02019-02-25 14:23:56 +08004952 mPointerEventDispatcher.unregisterInputEventListener(listener);
Arthur Hungbe5ce212018-09-13 18:41:56 +08004953 }
lumark588a3e82018-07-20 18:53:54 +08004954
4955 void prepareAppTransition(@WindowManager.TransitionType int transit,
Wale Ogunwale3a256e62018-12-06 14:41:18 -08004956 boolean alwaysKeepCurrent) {
4957 prepareAppTransition(transit, alwaysKeepCurrent, 0 /* flags */, false /* forceOverride */);
4958 }
4959
4960 void prepareAppTransition(@WindowManager.TransitionType int transit,
lumark588a3e82018-07-20 18:53:54 +08004961 boolean alwaysKeepCurrent, @WindowManager.TransitionFlags int flags,
4962 boolean forceOverride) {
4963 final boolean prepared = mAppTransition.prepareAppTransitionLocked(
4964 transit, alwaysKeepCurrent, flags, forceOverride);
4965 if (prepared && okToAnimate()) {
4966 mSkipAppTransitionAnimation = false;
4967 }
4968 }
4969
4970 void executeAppTransition() {
4971 if (mAppTransition.isTransitionSet()) {
4972 if (DEBUG_APP_TRANSITIONS) {
4973 Slog.w(TAG_WM, "Execute app transition: " + mAppTransition + ", displayId: "
4974 + mDisplayId + " Callers=" + Debug.getCallers(5));
4975 }
4976 mAppTransition.setReady();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004977 mWmService.mWindowPlacerLocked.requestTraversal();
lumark588a3e82018-07-20 18:53:54 +08004978 }
4979 }
4980
4981 /**
4982 * Update pendingLayoutChanges after app transition has finished.
4983 */
4984 void handleAnimatingStoppedAndTransition() {
4985 int changes = 0;
4986
4987 mAppTransition.setIdle();
4988
4989 for (int i = mNoAnimationNotifyOnTransitionFinished.size() - 1; i >= 0; i--) {
4990 final IBinder token = mNoAnimationNotifyOnTransitionFinished.get(i);
4991 mAppTransition.notifyAppTransitionFinishedLocked(token);
4992 }
4993 mNoAnimationNotifyOnTransitionFinished.clear();
4994
4995 mWallpaperController.hideDeferredWallpapersIfNeeded();
4996
4997 onAppTransitionDone();
4998
4999 changes |= FINISH_LAYOUT_REDO_LAYOUT;
5000 if (DEBUG_WALLPAPER_LIGHT) {
5001 Slog.v(TAG_WM, "Wallpaper layer changed: assigning layers + relayout");
5002 }
5003 computeImeTarget(true /* updateImeTarget */);
wilsonshihc32538e2018-11-07 17:27:34 +08005004 mWallpaperMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08005005 // Since the window list has been rebuilt, focus might have to be recomputed since the
5006 // actual order of windows might have changed again.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08005007 mWmService.mFocusMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08005008
5009 pendingLayoutChanges |= changes;
5010 }
Andrii Kulian15cfb422018-11-07 13:38:49 -08005011
Wale Ogunwale3a256e62018-12-06 14:41:18 -08005012 /** Check if pending app transition is for activity / task launch. */
5013 boolean isNextTransitionForward() {
5014 final int transit = mAppTransition.getAppTransition();
5015 return transit == TRANSIT_ACTIVITY_OPEN
5016 || transit == TRANSIT_TASK_OPEN
5017 || transit == TRANSIT_TASK_TO_FRONT;
5018 }
5019
Andrii Kulian15cfb422018-11-07 13:38:49 -08005020 /**
5021 * @see Display#FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS
5022 */
5023 boolean supportsSystemDecorations() {
Arthur Hung879a8ab2019-03-13 19:51:10 +08005024 return (mWmService.mDisplayWindowSettings.shouldShowSystemDecorsLocked(this)
Andrii Kuliandd989612019-02-21 12:13:28 -08005025 || (mDisplay.getFlags() & FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS) != 0
Chilun891aed22019-05-02 18:08:47 +08005026 || (mWmService.mForceDesktopModeOnExternalDisplays && !isUntrustedVirtualDisplay()))
Arthur Hung879a8ab2019-03-13 19:51:10 +08005027 // VR virtual display will be used to run and render 2D app within a VR experience.
5028 && mDisplayId != mWmService.mVr2dDisplayId;
Andrii Kulian15cfb422018-11-07 13:38:49 -08005029 }
chaviwff2e7d82018-11-02 11:11:27 -07005030
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005031 /**
Chilun891aed22019-05-02 18:08:47 +08005032 * @return {@code true} if the display is non-system created virtual display.
5033 */
5034 boolean isUntrustedVirtualDisplay() {
5035 return mDisplay.getType() == Display.TYPE_VIRTUAL
5036 && mDisplay.getOwnerUid() != Process.SYSTEM_UID;
5037 }
5038
5039 /**
chaviwff2e7d82018-11-02 11:11:27 -07005040 * Re-parent the DisplayContent's top surfaces, {@link #mWindowingLayer} and
Tiger Huangd8ec9382019-04-18 14:35:09 -07005041 * {@link #mOverlayLayer} to the specified SurfaceControl.
chaviwff2e7d82018-11-02 11:11:27 -07005042 *
Tiger Huangd8ec9382019-04-18 14:35:09 -07005043 * @param win The window which owns the SurfaceControl. This indicates the z-order of the
5044 * windows of this display against the windows on the parent display.
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005045 * @param sc The new SurfaceControl, where the DisplayContent's surfaces will be re-parented to.
chaviwff2e7d82018-11-02 11:11:27 -07005046 */
Tiger Huangd8ec9382019-04-18 14:35:09 -07005047 void reparentDisplayContent(WindowState win, SurfaceControl sc) {
5048 mParentWindow = win;
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005049 mParentSurfaceControl = sc;
5050 if (mPortalWindowHandle == null) {
5051 mPortalWindowHandle = createPortalWindowHandle(sc.toString());
5052 }
Tiger Huanged6794e2019-05-07 20:07:59 +08005053 getPendingTransaction().setInputWindowInfo(sc, mPortalWindowHandle)
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005054 .reparent(mWindowingLayer, sc).reparent(mOverlayLayer, sc);
5055 }
5056
Tiger Huangd8ec9382019-04-18 14:35:09 -07005057 /**
5058 * Get the window which owns the surface that this DisplayContent is re-parented to.
5059 *
5060 * @return the parent window.
5061 */
5062 WindowState getParentWindow() {
5063 return mParentWindow;
5064 }
5065
5066 /**
5067 * Update the location of this display in the parent window. This enables windows in this
5068 * display to compute the global transformation matrix.
5069 *
5070 * @param win The parent window of this display.
5071 * @param x The x coordinate in the parent window.
5072 * @param y The y coordinate in the parent window.
5073 */
5074 void updateLocation(WindowState win, int x, int y) {
5075 if (mParentWindow != win) {
5076 throw new IllegalArgumentException(
5077 "The given window is not the parent window of this display.");
5078 }
5079 if (mLocationInParentWindow.x != x || mLocationInParentWindow.y != y) {
5080 mLocationInParentWindow.x = x;
5081 mLocationInParentWindow.y = y;
5082 if (mWmService.mAccessibilityController != null) {
5083 mWmService.mAccessibilityController.onSomeWindowResizedOrMovedLocked();
5084 }
5085 }
5086 }
5087
5088 Point getLocationInParentWindow() {
5089 return mLocationInParentWindow;
5090 }
5091
Tarandeep Singha6f35612019-01-11 19:50:46 -08005092 @VisibleForTesting
5093 SurfaceControl getWindowingLayer() {
5094 return mWindowingLayer;
5095 }
5096
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005097 /**
Adrian Roos4ffc8972019-02-07 20:45:11 +01005098 * Updates the display's system gesture exclusion.
5099 *
5100 * @return true, if the exclusion changed.
5101 */
5102 boolean updateSystemGestureExclusion() {
5103 if (mSystemGestureExclusionListeners.getRegisteredCallbackCount() == 0) {
5104 // No one's interested anyways.
5105 return false;
5106 }
5107
5108 final Region systemGestureExclusion = calculateSystemGestureExclusion();
5109 try {
5110 if (mSystemGestureExclusion.equals(systemGestureExclusion)) {
5111 return false;
5112 }
5113 mSystemGestureExclusion.set(systemGestureExclusion);
5114 for (int i = mSystemGestureExclusionListeners.beginBroadcast() - 1; i >= 0; --i) {
5115 try {
5116 mSystemGestureExclusionListeners.getBroadcastItem(i)
5117 .onSystemGestureExclusionChanged(mDisplayId, systemGestureExclusion);
5118 } catch (RemoteException e) {
5119 Slog.e(TAG, "Failed to notify SystemGestureExclusionListener", e);
5120 }
5121 }
5122 mSystemGestureExclusionListeners.finishBroadcast();
5123 return true;
5124 } finally {
5125 systemGestureExclusion.recycle();
5126 }
5127 }
5128
5129 @VisibleForTesting
5130 Region calculateSystemGestureExclusion() {
Adrian Roos506267d2019-06-18 16:13:53 +02005131 final Region unhandled = Region.obtain();
5132 unhandled.set(0, 0, mDisplayFrames.mDisplayWidth, mDisplayFrames.mDisplayHeight);
5133
5134 final Rect leftEdge = mInsetsStateController.getSourceProvider(TYPE_LEFT_GESTURES)
5135 .getSource().getFrame();
5136 final Rect rightEdge = mInsetsStateController.getSourceProvider(TYPE_RIGHT_GESTURES)
5137 .getSource().getFrame();
5138
Adrian Roos4ffc8972019-02-07 20:45:11 +01005139 final Region global = Region.obtain();
5140 final Region touchableRegion = Region.obtain();
5141 final Region local = Region.obtain();
Adrian Roos506267d2019-06-18 16:13:53 +02005142 final int[] remainingLeftRight =
5143 {mSystemGestureExclusionLimit, mSystemGestureExclusionLimit};
Adrian Roos4ffc8972019-02-07 20:45:11 +01005144
5145 // Traverse all windows bottom up to assemble the gesture exclusion rects.
5146 // For each window, we only take the rects that fall within its touchable region.
5147 forAllWindows(w -> {
5148 if (w.cantReceiveTouchInput() || !w.isVisible()
Adrian Roos506267d2019-06-18 16:13:53 +02005149 || (w.getAttrs().flags & FLAG_NOT_TOUCHABLE) != 0
5150 || unhandled.isEmpty()) {
Adrian Roos4ffc8972019-02-07 20:45:11 +01005151 return;
5152 }
5153 final boolean modal =
5154 (w.mAttrs.flags & (FLAG_NOT_TOUCH_MODAL | FLAG_NOT_FOCUSABLE)) == 0;
5155
Adrian Roos506267d2019-06-18 16:13:53 +02005156 // Get the touchable region of the window, and intersect with where the screen is still
5157 // touchable, i.e. touchable regions on top are not covering it yet.
Adrian Roos4ffc8972019-02-07 20:45:11 +01005158 w.getTouchableRegion(touchableRegion);
Adrian Roos506267d2019-06-18 16:13:53 +02005159 touchableRegion.op(unhandled, Op.INTERSECT);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005160
5161 rectListToRegion(w.getSystemGestureExclusion(), local);
5162
5163 // Transform to display coordinates
5164 local.scale(w.mGlobalScale);
5165 final Rect frame = w.getWindowFrames().mFrame;
5166 local.translate(frame.left, frame.top);
5167
5168 // A window can only exclude system gestures where it is actually touchable
5169 local.op(touchableRegion, Op.INTERSECT);
5170
Adrian Roos506267d2019-06-18 16:13:53 +02005171 // Apply restriction if necessary.
5172 if (needsGestureExclusionRestrictions(w, mLastDispatchedSystemUiVisibility)) {
5173
5174 // Processes the region along the left edge.
5175 remainingLeftRight[0] = addToGlobalAndConsumeLimit(local, global, leftEdge,
5176 remainingLeftRight[0]);
5177
5178 // Processes the region along the right edge.
5179 remainingLeftRight[1] = addToGlobalAndConsumeLimit(local, global, rightEdge,
5180 remainingLeftRight[1]);
5181
5182 // Adds the middle (unrestricted area)
5183 final Region middle = Region.obtain(local);
5184 middle.op(leftEdge, Op.DIFFERENCE);
5185 middle.op(rightEdge, Op.DIFFERENCE);
5186 global.op(middle, Op.UNION);
5187 middle.recycle();
5188 } else {
5189 global.op(local, Op.UNION);
5190 }
5191 unhandled.op(touchableRegion, Op.DIFFERENCE);
5192 }, true /* topToBottom */);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005193 local.recycle();
5194 touchableRegion.recycle();
Adrian Roos506267d2019-06-18 16:13:53 +02005195 unhandled.recycle();
Adrian Roos4ffc8972019-02-07 20:45:11 +01005196 return global;
5197 }
5198
Adrian Roos506267d2019-06-18 16:13:53 +02005199 /**
5200 * @return Whether gesture exclusion area should be restricted from the window depending on the
5201 * current SystemUI visibility flags.
5202 */
5203 private static boolean needsGestureExclusionRestrictions(WindowState win, int sysUiVisibility) {
5204 final int type = win.mAttrs.type;
5205 final int stickyHideNavFlags =
5206 SYSTEM_UI_FLAG_HIDE_NAVIGATION | SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
5207 final boolean stickyHideNav =
5208 (sysUiVisibility & stickyHideNavFlags) == stickyHideNavFlags;
5209 return !stickyHideNav && type != TYPE_INPUT_METHOD && type != TYPE_STATUS_BAR
5210 && win.getActivityType() != ACTIVITY_TYPE_HOME;
5211 }
5212
5213 /**
5214 * Adds a local gesture exclusion area to the global area while applying a limit per edge.
5215 *
5216 * @param local The gesture exclusion area to add.
5217 * @param global The destination.
5218 * @param edge Only processes the part in that region.
5219 * @param limit How much limit in pixels we have.
5220 * @return How much of the limit are remaining.
5221 */
5222 private static int addToGlobalAndConsumeLimit(Region local, Region global, Rect edge,
5223 int limit) {
5224 final Region r = Region.obtain(local);
5225 r.op(edge, Op.INTERSECT);
5226
5227 final int[] remaining = {limit};
5228 forEachRect(r, rect -> {
5229 if (remaining[0] <= 0) {
5230 return;
5231 }
5232 final int height = rect.height();
5233 if (height > remaining[0]) {
5234 rect.bottom = rect.top + remaining[0];
5235 }
5236 remaining[0] -= height;
5237 global.op(rect, Op.UNION);
5238 });
5239 r.recycle();
5240 return remaining[0];
5241 }
5242
Adrian Roos4ffc8972019-02-07 20:45:11 +01005243 void registerSystemGestureExclusionListener(ISystemGestureExclusionListener listener) {
5244 mSystemGestureExclusionListeners.register(listener);
5245 final boolean changed;
5246 if (mSystemGestureExclusionListeners.getRegisteredCallbackCount() == 1) {
5247 changed = updateSystemGestureExclusion();
5248 } else {
5249 changed = false;
5250 }
5251
5252 if (!changed) {
5253 // If updateSystemGestureExclusion changed the exclusion, it will already have
5254 // notified the listener. Otherwise, we'll do it here.
5255 try {
5256 listener.onSystemGestureExclusionChanged(mDisplayId, mSystemGestureExclusion);
5257 } catch (RemoteException e) {
5258 Slog.e(TAG, "Failed to notify SystemGestureExclusionListener during register", e);
5259 }
5260 }
5261 }
5262
5263 void unregisterSystemGestureExclusionListener(ISystemGestureExclusionListener listener) {
5264 mSystemGestureExclusionListeners.unregister(listener);
5265 }
5266
5267 /**
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005268 * Create a portal window handle for input. This window transports any touch to the display
5269 * indicated by {@link InputWindowHandle#portalToDisplayId} if the touch hits this window.
5270 *
5271 * @param name The name of the portal window handle.
5272 * @return the new portal window handle.
5273 */
5274 private InputWindowHandle createPortalWindowHandle(String name) {
5275 // Let surface flinger to set the display ID of this input window handle because we don't
5276 // know which display the parent surface control is on.
5277 final InputWindowHandle portalWindowHandle = new InputWindowHandle(
5278 null /* inputApplicationHandle */, null /* clientWindow */, INVALID_DISPLAY);
5279 portalWindowHandle.name = name;
5280 portalWindowHandle.token = new Binder();
5281 portalWindowHandle.layoutParamsFlags =
5282 FLAG_SPLIT_TOUCH | FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL;
5283 getBounds(mTmpBounds);
5284 portalWindowHandle.touchableRegion.set(mTmpBounds);
5285 portalWindowHandle.scaleFactor = 1f;
5286 portalWindowHandle.ownerPid = Process.myPid();
5287 portalWindowHandle.ownerUid = Process.myUid();
5288 portalWindowHandle.portalToDisplayId = mDisplayId;
5289 return portalWindowHandle;
chaviwff2e7d82018-11-02 11:11:27 -07005290 }
Issei Suzukia5dbf522019-02-01 17:58:15 +01005291
5292 /**
5293 * @see IWindowManager#setForwardedInsets
5294 */
5295 public void setForwardedInsets(Insets insets) {
5296 if (insets == null) {
5297 insets = Insets.NONE;
5298 }
5299 if (mDisplayPolicy.getForwardedInsets().equals(insets)) {
5300 return;
5301 }
5302 mDisplayPolicy.setForwardedInsets(insets);
5303 setLayoutNeeded();
5304 mWmService.mWindowPlacerLocked.requestTraversal();
5305 }
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05005306
5307 protected MetricsLogger getMetricsLogger() {
5308 if (mMetricsLogger == null) {
5309 mMetricsLogger = new MetricsLogger();
5310 }
5311 return mMetricsLogger;
5312 }
Craig Mautner59c00972012-07-30 12:10:24 -07005313}