blob: d053015cea8210f08e71524a8f7548ce2ddb2b33 [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 Ogunwale30e441d2017-11-09 08:28:45 -080019import static android.app.ActivityManager.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;
Wale Ogunwale687b4272017-07-27 02:56:23 -070023import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070024import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
25import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070026import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070027import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
28import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale687b4272017-07-27 02:56:23 -070029import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
30import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070031import static android.view.Display.DEFAULT_DISPLAY;
32import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070033import static android.view.Surface.ROTATION_0;
34import static android.view.Surface.ROTATION_180;
35import static android.view.Surface.ROTATION_270;
36import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070037import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070038import static android.view.WindowManager.DOCKED_BOTTOM;
39import static android.view.WindowManager.DOCKED_INVALID;
40import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080041import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
42import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
43import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070044import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070045import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070046import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
47import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070048import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070049import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070050import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070051import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Wale Ogunwale494009b82016-10-21 09:01:38 -070052import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070053import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070054import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
55import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070056import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070057import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
58import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070059import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070060import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Adrian Roose99bc052017-11-20 17:55:31 +010061import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
62import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
63import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
64import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Jorim Jaggi75520d52017-08-24 17:23:19 +020065import static com.android.server.wm.AppTransition.TRANSIT_KEYGUARD_UNOCCLUDE;
Wale Ogunwale1666e312016-12-16 11:27:18 -080066import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070067import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070069import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
70import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070076import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070077import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080078import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070079import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070080import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070081import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070082import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070083import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070084import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080085import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -070086import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070087import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
88import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070089import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070090import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -070091import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
92import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070093import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -070094import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070095import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070096import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070097import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070098import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070099import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700100import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700101import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700102import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700103import static com.android.server.wm.proto.DisplayProto.ABOVE_APP_WINDOWS;
104import static com.android.server.wm.proto.DisplayProto.BELOW_APP_WINDOWS;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000105import static com.android.server.wm.proto.DisplayProto.DISPLAY_FRAMES;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700106import static com.android.server.wm.proto.DisplayProto.DISPLAY_INFO;
107import static com.android.server.wm.proto.DisplayProto.DOCKED_STACK_DIVIDER_CONTROLLER;
108import static com.android.server.wm.proto.DisplayProto.DPI;
109import static com.android.server.wm.proto.DisplayProto.ID;
110import static com.android.server.wm.proto.DisplayProto.IME_WINDOWS;
111import static com.android.server.wm.proto.DisplayProto.PINNED_STACK_CONTROLLER;
Steven Timotiusf2d68892017-08-28 17:00:01 -0700112import static com.android.server.wm.proto.DisplayProto.ROTATION;
113import static com.android.server.wm.proto.DisplayProto.SCREEN_ROTATION_ANIMATION;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700114import static com.android.server.wm.proto.DisplayProto.STACKS;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700115import static com.android.server.wm.proto.DisplayProto.WINDOW_CONTAINER;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700116
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700117import android.annotation.CallSuper;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700118import android.annotation.NonNull;
Dean Harding3e5a1522017-10-06 09:19:01 -0700119import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700120import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700121import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700122import android.graphics.Bitmap;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700123import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800124import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700125import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700126import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100127import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700128import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700129import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700130import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700131import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700132import android.os.RemoteException;
133import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100134import android.os.Trace;
Chong Zhang8e89b312015-09-09 15:09:30 -0700135import android.util.DisplayMetrics;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800136import android.util.MutableBoolean;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700137import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700138import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700139import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700140import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700141import android.view.InputDevice;
Robert Carrb1579c82017-09-05 14:54:47 -0700142import android.view.MagnificationSpec;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700143import android.view.Surface;
144import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100145import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700146import android.view.SurfaceSession;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700147
Bryce Lee48f4b572017-04-10 10:54:15 -0700148import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800149import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700150import com.android.internal.view.IInputMethodClient;
Adrian Roose99bc052017-11-20 17:55:31 +0100151import com.android.server.policy.WindowManagerPolicy;
Craig Mautner59c00972012-07-30 12:10:24 -0700152
Robert Carr3b716242016-08-16 16:02:21 -0700153import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700154import java.io.PrintWriter;
155import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700156import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700157import java.util.HashMap;
158import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700159import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700160import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800161import java.util.function.Consumer;
162import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700163
Craig Mautner59c00972012-07-30 12:10:24 -0700164/**
165 * Utility class for keeping track of the WindowStates and other pertinent contents of a
166 * particular Display.
167 *
168 * IMPORTANT: No method from this class should ever be used without holding
169 * WindowManagerService.mWindowMap.
170 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700171class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700172 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700173
174 /** Unique identifier of this stack. */
175 private final int mDisplayId;
176
Wale Ogunwale3a931692016-11-02 16:49:48 -0700177 /** The containers below are the only child containers the display can have. */
178 // Contains all window containers that are related to apps (Activities)
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100179 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700180 // Contains all non-app window containers that should be displayed above the app containers
181 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800182 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100183 new AboveAppWindowContainers("mAboveAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700184 // Contains all non-app window containers that should be displayed below the app containers
185 // (e.g. Wallpaper).
186 private final NonAppWindowContainers mBelowAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100187 new NonAppWindowContainers("mBelowAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700188 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
189 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
190 // window containers together and move them in-sync if/when needed.
191 private final NonAppWindowContainers mImeWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100192 new NonAppWindowContainers("mImeWindowsContainers", mService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700193
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000194 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800195 private WindowState mTmpWindow2;
196 private WindowAnimator mTmpWindowAnimator;
197 private boolean mTmpRecoveringMemory;
198 private boolean mUpdateImeTarget;
199 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700200 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700201
Wale Ogunwale02319a62016-09-26 15:21:22 -0700202 // Mapping from a token IBinder to a WindowToken object on this display.
203 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
204
Andrii Kuliancd097992017-03-23 18:31:59 -0700205 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700206 int mInitialDisplayWidth = 0;
207 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700208 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700209
210 /**
211 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
212 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
213 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
214 */
Craig Mautner59c00972012-07-30 12:10:24 -0700215 int mBaseDisplayWidth = 0;
216 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700217 /**
218 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
219 * but can be set from Settings or via shell command "adb shell wm density".
220 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
221 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700222 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700223 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700224 private final DisplayInfo mDisplayInfo = new DisplayInfo();
225 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700226 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000227 DisplayFrames mDisplayFrames;
228
Andrii Kulian06d07d62017-03-14 11:11:47 -0700229 /**
230 * For default display it contains real metrics, empty for others.
231 * @see WindowManagerService#createWatermarkInTransaction()
232 */
233 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
234 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
235 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700236
Andrii Kulian06d07d62017-03-14 11:11:47 -0700237 /**
238 * Compat metrics computed based on {@link #mDisplayMetrics}.
239 * @see #updateDisplayAndOrientation(int)
240 */
241 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
242
243 /** The desired scaling factor for compatible apps. */
244 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700245
Andrii Kulian8ee72852017-03-10 10:36:45 -0800246 /**
247 * Current rotation of the display.
248 * Constants as per {@link android.view.Surface.Rotation}.
249 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700250 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800251 */
252 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700253
Andrii Kulian8ee72852017-03-10 10:36:45 -0800254 /**
255 * Last applied orientation of the display.
256 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
257 *
258 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
259 */
260 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700261
Andrii Kulian8ee72852017-03-10 10:36:45 -0800262 /**
263 * Flag indicating that the application is receiving an orientation that has different metrics
264 * than it expected. E.g. Portrait instead of Landscape.
265 *
Andrii Kulian06d07d62017-03-14 11:11:47 -0700266 * @see #updateRotationUnchecked(boolean)
Andrii Kulian8ee72852017-03-10 10:36:45 -0800267 */
268 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700269
Andrii Kulian8ee72852017-03-10 10:36:45 -0800270 /**
271 * Orientation forced by some window. If there is no visible window that specifies orientation
272 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
273 *
274 * @see NonAppWindowContainers#getOrientation()
275 */
276 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700277
Andrii Kulian8ee72852017-03-10 10:36:45 -0800278 /**
279 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
280 * occluded.
281 *
282 * @see NonAppWindowContainers#getOrientation()
283 */
284 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
285
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700286 /**
287 * Keep track of wallpaper visibility to notify changes.
288 */
289 private boolean mLastWallpaperVisible = false;
290
Andrii Kulian06d07d62017-03-14 11:11:47 -0700291 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700292
Craig Mautner39834192012-09-02 07:47:24 -0700293 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700294 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700295 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700296 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800297 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700298
Craig Mautnerdc548482014-02-05 13:35:24 -0800299 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700300 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800301
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700302 /** Detect user tapping outside of current focused task bounds .*/
303 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700304
Craig Mautner6601b7b2013-04-29 10:29:11 -0700305 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700306 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700307
Craig Mautner6601b7b2013-04-29 10:29:11 -0700308 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800309 private final Rect mTmpRect = new Rect();
310 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700311 private final RectF mTmpRectF = new RectF();
312 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800313 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700314
Bryce Leef3c6a472017-11-14 14:53:06 -0800315 /** Used for handing back size of display */
316 private final Rect mTmpBounds = new Rect();
317
Craig Mautner95da1082014-02-24 17:54:35 -0800318 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700319 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800320
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700321 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700322 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700323
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800324 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
325
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000326 private boolean mHaveBootMsg = false;
327 private boolean mHaveApp = false;
328 private boolean mHaveWallpaper = false;
329 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700330
331 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
332
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700333 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
334 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000335 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
336 new ApplySurfaceChangesTransactionState();
337 private final ScreenshotApplicationState mScreenshotApplicationState =
338 new ScreenshotApplicationState();
Jorim Jaggia5e10572017-11-15 14:36:26 +0100339 private final Transaction mTmpTransaction = new Transaction();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700340
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700341 // True if this display is in the process of being removed. Used to determine if the removal of
342 // the display's direct children should be allowed.
343 private boolean mRemovingDisplay = false;
344
Bryce Leed1871262017-06-12 14:12:29 -0700345 // {@code false} if this display is in the processing of being created.
346 private boolean mDisplayReady = false;
347
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800348 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700349
Robert Carrb1579c82017-09-05 14:54:47 -0700350 private final SurfaceSession mSession = new SurfaceSession();
351
352 /**
353 * We organize all top-level Surfaces in to the following layers.
354 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800355 * and omitted from Screen-Magnification, for example the strict mode flash or
356 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700357 * {@link #mWindowingLayer} contains everything else.
358 */
359 private SurfaceControl mOverlayLayer;
360
361 /**
362 * See {@link #mOverlayLayer}
363 */
364 private SurfaceControl mWindowingLayer;
365
366 /**
367 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
368 * <p>
369 * For these surfaces currently we use a surface based on the larger of width or height so we
370 * don't have to resize when rotating the display.
371 */
372 private int mSurfaceSize;
373
374 /**
375 * A list of surfaces to be destroyed after {@link #mPendingTransaction} is applied.
376 */
377 private final ArrayList<SurfaceControl> mPendingDestroyingSurfaces = new ArrayList<>();
378
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100379 /** Temporary float array to retrieve 3x3 matrix values. */
380 private final float[] mTmpFloats = new float[9];
381
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800382 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
383 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800384 final AppWindowToken atoken = w.mAppToken;
385 if (winAnimator.mDrawState == READY_TO_SHOW) {
386 if (atoken == null || atoken.allDrawn) {
387 if (w.performShowLocked()) {
388 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
389 if (DEBUG_LAYOUT_REPEATS) {
390 mService.mWindowPlacerLocked.debugLayoutRepeats(
391 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
392 }
393 }
394 }
395 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800396 };
397
398 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
399 final WindowStateAnimator winAnimator = w.mWinAnimator;
400 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
401 return;
402 }
403
404 final int flags = w.mAttrs.flags;
405
406 // If this window is animating, make a note that we have an animating window and take
407 // care of a request to run a detached wallpaper animation.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100408 if (winAnimator.isAnimationSet()) {
409 final AnimationAdapter anim = w.getAnimation();
410 if (anim != null) {
411 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && anim.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800412 mTmpWindow = w;
413 }
Jorim Jaggia5e10572017-11-15 14:36:26 +0100414 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800415 if (color != 0) {
416 final TaskStack stack = w.getStack();
417 if (stack != null) {
418 stack.setAnimationBackground(winAnimator, color);
419 }
420 }
421 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800422 }
423
424 // If this window's app token is running a detached wallpaper animation, make a note so
425 // we can ensure the wallpaper is displayed behind it.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200426 final AppWindowToken atoken = winAnimator.mWin.mAppToken;
427 final AnimationAdapter animation = atoken != null ? atoken.getAnimation() : null;
428 if (animation != null) {
429 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && animation.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800430 mTmpWindow = w;
431 }
432
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200433 final int color = animation.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800434 if (color != 0) {
435 final TaskStack stack = w.getStack();
436 if (stack != null) {
437 stack.setAnimationBackground(winAnimator, color);
438 }
439 }
440 }
441 };
442
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800443 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
444 final int lostFocusUid = mTmpWindow.mOwnerUid;
445 final Handler handler = mService.mH;
446 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
447 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
448 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
449 w.mAttrs.hideTimeoutMilliseconds);
450 }
451 }
452 };
453
454 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
455 final AppWindowToken focusedApp = mService.mFocusedApp;
456 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
457 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
458
459 if (!w.canReceiveKeys()) {
460 return false;
461 }
462
463 final AppWindowToken wtoken = w.mAppToken;
464
465 // If this window's application has been removed, just skip it.
466 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
467 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
468 + (wtoken.removed ? "removed" : "sendingToBottom"));
469 return false;
470 }
471
472 if (focusedApp == null) {
473 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
474 + " using new focus @ " + w);
475 mTmpWindow = w;
476 return true;
477 }
478
479 if (!focusedApp.windowsAreFocusable()) {
480 // Current focused app windows aren't focusable...
481 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
482 + " focusable using new focus @ " + w);
483 mTmpWindow = w;
484 return true;
485 }
486
487 // Descend through all of the app tokens and find the first that either matches
488 // win.mAppToken (return win) or mFocusedApp (return null).
489 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
490 if (focusedApp.compareTo(wtoken) > 0) {
491 // App stack below focused app stack. No focus for you!!!
492 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
493 "findFocusedWindow: Reached focused app=" + focusedApp);
494 mTmpWindow = null;
495 return true;
496 }
497 }
498
499 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
500 mTmpWindow = w;
501 return true;
502 };
503
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800504 private final Consumer<WindowState> mPerformLayout = w -> {
505 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
506 // wasting time and funky changes while a window is animating away.
507 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
508 || w.isGoneForLayoutLw();
509
510 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
511 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
512 + " mLayoutAttached=" + w.mLayoutAttached
513 + " screen changed=" + w.isConfigChanged());
514 final AppWindowToken atoken = w.mAppToken;
515 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200516 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800517 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
518 + " parentHidden=" + w.isParentWindowHidden());
519 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200520 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800521 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
522 + " parentHidden=" + w.isParentWindowHidden());
523 }
524
525 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
526 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
527 // since that means "perform layout as normal, just don't display").
528 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
529 || ((w.isConfigChanged() || w.setReportResizeHints())
530 && !w.isGoneForLayoutLw() &&
531 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
532 (w.mHasSurface && w.mAppToken != null &&
533 w.mAppToken.layoutConfigChanges)))) {
534 if (!w.mLayoutAttached) {
535 if (mTmpInitial) {
536 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
537 w.mContentChanged = false;
538 }
539 if (w.mAttrs.type == TYPE_DREAM) {
540 // Don't layout windows behind a dream, so that if it does stuff like hide
541 // the status bar we won't get a bad transition when it goes away.
542 mTmpWindow = w;
543 }
544 w.mLayoutNeeded = false;
545 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200546 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000547 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800548 w.mLayoutSeq = mService.mLayoutSeq;
549
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200550 // If this is the first layout, we need to initialize the last inset values as
551 // otherwise we'd immediately cause an unnecessary resize.
552 if (firstLayout) {
553 w.updateLastInsetValues();
554 }
555
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800556 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
557 + " mContainingFrame=" + w.mContainingFrame
558 + " mDisplayFrame=" + w.mDisplayFrame);
559 }
560 }
561 };
562
563 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
564 if (w.mLayoutAttached) {
565 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
566 + " mViewVisibility=" + w.mViewVisibility
567 + " mRelayoutCalled=" + w.mRelayoutCalled);
568 // If this view is GONE, then skip it -- keep the current frame, and let the caller
569 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
570 // windows, since that means "perform layout as normal, just don't display").
571 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
572 return;
573 }
574 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
575 || w.mLayoutNeeded) {
576 if (mTmpInitial) {
577 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
578 w.mContentChanged = false;
579 }
580 w.mLayoutNeeded = false;
581 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000582 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800583 w.mLayoutSeq = mService.mLayoutSeq;
584 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
585 + " mContainingFrame=" + w.mContainingFrame
586 + " mDisplayFrame=" + w.mDisplayFrame);
587 }
588 } else if (w.mAttrs.type == TYPE_DREAM) {
589 // Don't layout windows behind a dream, so that if it does stuff like hide the
590 // status bar we won't get a bad transition when it goes away.
591 mTmpWindow = mTmpWindow2;
592 }
593 };
594
595 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
596 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
597 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
598 return w.canBeImeTarget();
599 };
600
601 private final Consumer<WindowState> mApplyPostLayoutPolicy =
602 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
603 mService.mInputMethodTarget);
604
605 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
606 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
607 final boolean obscuredChanged = w.mObscured !=
608 mTmpApplySurfaceChangesTransactionState.obscured;
609 final RootWindowContainer root = mService.mRoot;
610 // Only used if default window
611 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
612
613 // Update effect.
614 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
615 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
616 final boolean isDisplayed = w.isDisplayedLw();
617
618 if (isDisplayed && w.isObscuringDisplay()) {
619 // This window completely covers everything behind it, so we want to leave all
620 // of them as undimmed (for performance reasons).
621 root.mObscuringWindow = w;
622 mTmpApplySurfaceChangesTransactionState.obscured = true;
623 }
624
625 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
626 root.handleNotObscuredLocked(w,
627 mTmpApplySurfaceChangesTransactionState.obscured,
628 mTmpApplySurfaceChangesTransactionState.syswin);
629
630 if (w.mHasSurface && isDisplayed) {
631 final int type = w.mAttrs.type;
632 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
633 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
634 mTmpApplySurfaceChangesTransactionState.syswin = true;
635 }
636 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
637 && w.mAttrs.preferredRefreshRate != 0) {
638 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
639 = w.mAttrs.preferredRefreshRate;
640 }
641 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
642 && w.mAttrs.preferredDisplayModeId != 0) {
643 mTmpApplySurfaceChangesTransactionState.preferredModeId
644 = w.mAttrs.preferredDisplayModeId;
645 }
646 }
647 }
648
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800649 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
650 && mWallpaperController.isWallpaperTarget(w)) {
651 // This is the wallpaper target and its obscured state changed... make sure the
652 // current wallpaper's visibility has been updated accordingly.
653 mWallpaperController.updateWallpaperVisibility();
654 }
655
Jorim Jaggia5e10572017-11-15 14:36:26 +0100656 // Use mTmpTransaction instead of mPendingTransaction because we don't want to commit
657 // other changes in mPendingTransaction at this point.
658 w.handleWindowMovedIfNeeded(mTmpTransaction);
659 SurfaceControl.mergeToGlobalTransaction(mTmpTransaction);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800660
661 final WindowStateAnimator winAnimator = w.mWinAnimator;
662
663 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
664 w.mContentChanged = false;
665
666 // Moved from updateWindowsAndWallpaperLocked().
667 if (w.mHasSurface) {
668 // Take care of the window being ready to display.
669 final boolean committed = winAnimator.commitFinishDrawingLocked();
670 if (isDefaultDisplay && committed) {
671 if (w.mAttrs.type == TYPE_DREAM) {
672 // HACK: When a dream is shown, it may at that point hide the lock screen.
673 // So we need to redo the layout to let the phone window manager make this
674 // happen.
675 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
676 if (DEBUG_LAYOUT_REPEATS) {
677 surfacePlacer.debugLayoutRepeats(
678 "dream and commitFinishDrawingLocked true",
679 pendingLayoutChanges);
680 }
681 }
682 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
683 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
684 "First draw done in potential wallpaper target " + w);
685 root.mWallpaperMayChange = true;
686 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
687 if (DEBUG_LAYOUT_REPEATS) {
688 surfacePlacer.debugLayoutRepeats(
689 "wallpaper and commitFinishDrawingLocked true",
690 pendingLayoutChanges);
691 }
692 }
693 }
Robert Carr2117aaf2017-04-25 14:46:50 -0700694 final TaskStack stack = w.getStack();
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200695 if (!winAnimator.isWaitingForOpening()
Robert Carr2117aaf2017-04-25 14:46:50 -0700696 || (stack != null && stack.isAnimatingBounds())) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800697 // Updates the shown frame before we set up the surface. This is needed
698 // because the resizing could change the top-left position (in addition to
699 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
700 // position the surface.
701 //
702 // If an animation is being started, we can't call this method because the
703 // animation hasn't processed its initial transformation yet, but in general
Robert Carr2117aaf2017-04-25 14:46:50 -0700704 // we do want to update the position if the window is animating. We make an exception
705 // for the bounds animating state, where an application may have been waiting
706 // for an exit animation to start, but instead enters PiP. We need to ensure
707 // we always recompute the top-left in this case.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800708 winAnimator.computeShownFrameLocked();
709 }
710 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
Jorim Jaggia5e10572017-11-15 14:36:26 +0100711
712 // Since setSurfaceBoundariesLocked applies the clipping, we need to apply the position
chaviwe07246a2017-12-12 16:18:29 -0800713 // to the surface of the window container and also the position of the stack window
714 // container as well. Use mTmpTransaction instead of mPendingTransaction to avoid
715 // committing any existing changes in there.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100716 w.updateSurfacePosition(mTmpTransaction);
chaviwe07246a2017-12-12 16:18:29 -0800717 if (stack != null) {
718 stack.updateSurfaceBounds(mTmpTransaction);
719 }
Jorim Jaggia5e10572017-11-15 14:36:26 +0100720 SurfaceControl.mergeToGlobalTransaction(mTmpTransaction);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800721 }
722
723 final AppWindowToken atoken = w.mAppToken;
724 if (atoken != null) {
725 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
726 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
727 mTmpUpdateAllDrawn.add(atoken);
728 }
729 }
730
731 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
732 && w.isDisplayedLw()) {
733 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
734 }
735
736 w.updateResizingWindowIfNeeded();
737 };
738
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800739 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800740 * Create new {@link DisplayContent} instance, add itself to the root window container and
741 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700742 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800743 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700744 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
745 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700746 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700747 DisplayContent(Display display, WindowManagerService service,
Robert Carrb1579c82017-09-05 14:54:47 -0700748 WallpaperController wallpaperController) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100749 super(service);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800750 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
751 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
752 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
753 + " new=" + display);
754 }
755
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700756 mDisplay = display;
757 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700758 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700759 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700760 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700761 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000762 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700763 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800764 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700765 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700766
Robert Carrb1579c82017-09-05 14:54:47 -0700767 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth);
768
769 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
770 .setSize(mSurfaceSize, mSurfaceSize)
771 .setOpaque(true);
772 mWindowingLayer = b.setName("Display Root").build();
773 mOverlayLayer = b.setName("Display Overlays").build();
774
Robert Carrf59b8dd2017-10-02 18:58:36 -0700775 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700776 .setLayerStack(mWindowingLayer, mDisplayId)
777 .show(mWindowingLayer)
778 .setLayer(mOverlayLayer, 1)
779 .setLayerStack(mOverlayLayer, mDisplayId)
780 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700781 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700782
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700783 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700784 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700785 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700786 super.addChild(mAboveAppWindowsContainers, null);
787 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800788
789 // Add itself as a child to the root container.
790 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700791
792 // TODO(b/62541591): evaluate whether this is the best spot to declare the
793 // {@link DisplayContent} ready for use.
794 mDisplayReady = true;
795 }
796
797 boolean isReady() {
798 // The display is ready when the system and the individual display are both ready.
799 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700800 }
801
802 int getDisplayId() {
803 return mDisplayId;
804 }
805
Wale Ogunwale02319a62016-09-26 15:21:22 -0700806 WindowToken getWindowToken(IBinder binder) {
807 return mTokenMap.get(binder);
808 }
809
810 AppWindowToken getAppWindowToken(IBinder binder) {
811 final WindowToken token = getWindowToken(binder);
812 if (token == null) {
813 return null;
814 }
815 return token.asAppWindowToken();
816 }
817
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700818 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700819 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
820 if (dc != null) {
821 // We currently don't support adding a window token to the display if the display
822 // already has the binder mapped to another token. If there is a use case for supporting
823 // this moving forward we will either need to merge the WindowTokens some how or have
824 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700825 throw new IllegalArgumentException("Can't map token=" + token + " to display="
826 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700827 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700828 if (binder == null) {
829 throw new IllegalArgumentException("Can't map token=" + token + " to display="
830 + getName() + " binder is null");
831 }
832 if (token == null) {
833 throw new IllegalArgumentException("Can't map null token to display="
834 + getName() + " binder=" + binder);
835 }
836
Wale Ogunwale02319a62016-09-26 15:21:22 -0700837 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700838
839 if (token.asAppWindowToken() == null) {
840 // Add non-app token to container hierarchy on the display. App tokens are added through
841 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700842 switch (token.windowType) {
843 case TYPE_WALLPAPER:
844 mBelowAppWindowsContainers.addChild(token);
845 break;
846 case TYPE_INPUT_METHOD:
847 case TYPE_INPUT_METHOD_DIALOG:
848 mImeWindowsContainers.addChild(token);
849 break;
850 default:
851 mAboveAppWindowsContainers.addChild(token);
852 break;
853 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700854 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700855 }
856
857 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700858 final WindowToken token = mTokenMap.remove(binder);
859 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800860 token.setExiting();
861 }
862 return token;
863 }
864
865 /** Changes the display the input window token is housed on to this one. */
866 void reParentWindowToken(WindowToken token) {
867 final DisplayContent prevDc = token.getDisplayContent();
868 if (prevDc == this) {
869 return;
870 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800871 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
872 && token.asAppWindowToken() == null) {
873 // Removed the token from the map, but made sure it's not an app token before removing
874 // from parent.
875 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700876 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800877
878 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700879 }
880
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700881 void removeAppToken(IBinder binder) {
882 final WindowToken token = removeWindowToken(binder);
883 if (token == null) {
884 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
885 return;
886 }
887
888 final AppWindowToken appToken = token.asAppWindowToken();
889
890 if (appToken == null) {
891 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
892 return;
893 }
894
895 appToken.onRemovedFromDisplay();
896 }
897
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700898 Display getDisplay() {
899 return mDisplay;
900 }
901
Craig Mautner59c00972012-07-30 12:10:24 -0700902 DisplayInfo getDisplayInfo() {
903 return mDisplayInfo;
904 }
905
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700906 DisplayMetrics getDisplayMetrics() {
907 return mDisplayMetrics;
908 }
909
Andrii Kulian8ee72852017-03-10 10:36:45 -0800910 int getRotation() {
911 return mRotation;
912 }
913
914 void setRotation(int newRotation) {
915 mRotation = newRotation;
916 }
917
918 int getLastOrientation() {
919 return mLastOrientation;
920 }
921
922 void setLastOrientation(int orientation) {
923 mLastOrientation = orientation;
924 }
925
926 boolean getAltOrientation() {
927 return mAltOrientation;
928 }
929
930 void setAltOrientation(boolean altOrientation) {
931 mAltOrientation = altOrientation;
932 }
933
934 int getLastWindowForcedOrientation() {
935 return mLastWindowForcedOrientation;
936 }
937
Andrii Kulian06d07d62017-03-14 11:11:47 -0700938 /**
939 * Update rotation of the display.
940 *
941 * Returns true if the rotation has been changed. In this case YOU MUST CALL
942 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
943 */
944 boolean updateRotationUnchecked(boolean inTransaction) {
945 if (mService.mDeferredRotationPauseCount > 0) {
946 // Rotation updates have been paused temporarily. Defer the update until
947 // updates have been resumed.
948 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
949 return false;
950 }
951
952 ScreenRotationAnimation screenRotationAnimation =
953 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
954 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
955 // Rotation updates cannot be performed while the previous rotation change
956 // animation is still in progress. Skip this update. We will try updating
957 // again after the animation is finished and the display is unfrozen.
958 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
959 return false;
960 }
961 if (mService.mDisplayFrozen) {
962 // Even if the screen rotation animation has finished (e.g. isAnimating
963 // returns false), there is still some time where we haven't yet unfrozen
964 // the display. We also need to abort rotation here.
965 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
966 "Deferring rotation, still finishing previous rotation");
967 return false;
968 }
969
970 if (!mService.mDisplayEnabled) {
971 // No point choosing a rotation if the display is not enabled.
972 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
973 return false;
974 }
975
976 final int oldRotation = mRotation;
977 final int lastOrientation = mLastOrientation;
978 final boolean oldAltOrientation = mAltOrientation;
979 int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation);
Robert Carr0e007272017-10-02 13:00:55 -0700980 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
Robert Carr897215d2017-03-29 12:25:50 -0700981 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -0700982
Robert Carr0e007272017-10-02 13:00:55 -0700983 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -0700984 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
985 if (seamlessRotated != null) {
986 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
987 // to complete (that is, waiting for windows to redraw). It's tempting to check
988 // w.mSeamlessRotationCount but that could be incorrect in the case of
989 // window-removal.
990 return false;
991 }
Robert Carr0e007272017-10-02 13:00:55 -0700992
993 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700994 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -0700995 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -0700996 mayRotateSeamlessly = false;
997 }
998 for (int i = 0; i < mService.mSessions.size(); i++) {
999 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1000 mayRotateSeamlessly = false;
1001 break;
1002 }
1003 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001004 }
Robert Carr0e007272017-10-02 13:00:55 -07001005 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001006
1007 // TODO: Implement forced rotation changes.
1008 // Set mAltOrientation to indicate that the application is receiving
1009 // an orientation that has different metrics than it expected.
1010 // eg. Portrait instead of Landscape.
1011
1012 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
1013 lastOrientation, rotation);
1014
1015 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Selected orientation " + lastOrientation
1016 + ", got rotation " + rotation + " which has "
1017 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1018
1019 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1020 // No change.
1021 return false;
1022 }
1023
1024 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Rotation changed to " + rotation
1025 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1026 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1027
1028 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1029 mService.mWaitingForConfig = true;
1030 }
1031
1032 mRotation = rotation;
1033 mAltOrientation = altOrientation;
1034 if (isDefaultDisplay) {
1035 mService.mPolicy.setRotationLw(rotation);
1036 }
1037
1038 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1039 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1040 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1041 WINDOW_FREEZE_TIMEOUT_DURATION);
1042
1043 setLayoutNeeded();
1044 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001045 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001046
1047 if (!rotateSeamlessly) {
Bryce Lee8f853582017-06-05 17:25:59 -07001048 mService.startFreezingDisplayLocked(inTransaction, anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001049 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1050 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1051 mDisplayId);
1052 } else {
1053 // The screen rotation animation uses a screenshot to freeze the screen
1054 // while windows resize underneath.
1055 // When we are rotating seamlessly, we allow the elements to transition
1056 // to their rotated state independently and without a freeze required.
1057 screenRotationAnimation = null;
1058
1059 // We have to reset this in case a window was removed before it
1060 // finished seamless rotation.
1061 mService.mSeamlessRotationCount = 0;
1062 }
1063
1064 // We need to update our screen size information to match the new rotation. If the rotation
1065 // has actually changed then this method will return true and, according to the comment at
1066 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1067 // By updating the Display info here it will be available to
1068 // #computeScreenConfiguration() later.
1069 updateDisplayAndOrientation(getConfiguration().uiMode);
1070
1071 if (!inTransaction) {
1072 if (SHOW_TRANSACTIONS) {
1073 Slog.i(TAG_WM, ">>> OPEN TRANSACTION setRotationUnchecked");
1074 }
1075 mService.openSurfaceTransaction();
1076 }
1077 try {
1078 // NOTE: We disable the rotation in the emulator because
1079 // it doesn't support hardware OpenGL emulation yet.
1080 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1081 && screenRotationAnimation.hasScreenshot()) {
Robert Carrb1579c82017-09-05 14:54:47 -07001082 if (screenRotationAnimation.setRotationInTransaction(rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001083 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1084 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1085 mService.scheduleAnimationLocked();
1086 }
1087 }
1088
1089 if (rotateSeamlessly) {
1090 forAllWindows(w -> {
1091 w.mWinAnimator.seamlesslyRotateWindow(oldRotation, rotation);
1092 }, true /* traverseTopToBottom */);
1093 }
1094
1095 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1096 } finally {
1097 if (!inTransaction) {
Adrian Roos111aff92017-09-27 18:11:46 +02001098 mService.closeSurfaceTransaction("setRotationUnchecked");
Andrii Kulian06d07d62017-03-14 11:11:47 -07001099 if (SHOW_LIGHT_TRANSACTIONS) {
1100 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION setRotationUnchecked");
1101 }
1102 }
1103 }
1104
1105 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001106 if (w.mHasSurface && !rotateSeamlessly) {
1107 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001108 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001109 mService.mRoot.mOrientationChangeComplete = false;
1110 w.mLastFreezeDuration = 0;
1111 }
1112 w.mReportOrientationChanged = true;
1113 }, true /* traverseTopToBottom */);
1114
1115 if (rotateSeamlessly) {
1116 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1117 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1118 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1119 }
1120
1121 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1122 final WindowManagerService.RotationWatcher rotationWatcher
1123 = mService.mRotationWatchers.get(i);
1124 if (rotationWatcher.mDisplayId == mDisplayId) {
1125 try {
1126 rotationWatcher.mWatcher.onRotationChanged(rotation);
1127 } catch (RemoteException e) {
1128 // Ignore
1129 }
1130 }
1131 }
1132
1133 // TODO (multi-display): Magnification is supported only for the default display.
1134 // Announce rotation only if we will not animate as we already have the
1135 // windows in final state. Otherwise, we make this call at the rotation end.
1136 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1137 && isDefaultDisplay) {
1138 mService.mAccessibilityController.onRotationChangedLocked(this);
1139 }
1140
1141 return true;
1142 }
1143
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001144 void configureDisplayPolicy() {
1145 mService.mPolicy.setInitialDisplaySize(getDisplay(),
1146 mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1147
1148 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
1149 }
1150
Andrii Kulian06d07d62017-03-14 11:11:47 -07001151 /**
1152 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1153 * changed.
1154 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1155 */
1156 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1157 // Use the effective "visual" dimensions based on current rotation
1158 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1159 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1160 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1161 int dw = realdw;
1162 int dh = realdh;
1163
1164 if (mAltOrientation) {
1165 if (realdw > realdh) {
1166 // Turn landscape into portrait.
1167 int maxw = (int)(realdh/1.3f);
1168 if (maxw < realdw) {
1169 dw = maxw;
1170 }
1171 } else {
1172 // Turn portrait into landscape.
1173 int maxh = (int)(realdw/1.3f);
1174 if (maxh < realdh) {
1175 dh = maxh;
1176 }
1177 }
1178 }
1179
1180 // Update application display metrics.
1181 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1182 mDisplayId);
1183 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1184 mDisplayId);
1185 mDisplayInfo.rotation = mRotation;
1186 mDisplayInfo.logicalWidth = dw;
1187 mDisplayInfo.logicalHeight = dh;
1188 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1189 mDisplayInfo.appWidth = appWidth;
1190 mDisplayInfo.appHeight = appHeight;
1191 if (isDefaultDisplay) {
1192 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1193 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1194 }
1195 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1196 if (mDisplayScalingDisabled) {
1197 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1198 } else {
1199 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1200 }
1201
1202 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
1203 mDisplayInfo);
1204
1205 mBaseDisplayRect.set(0, 0, dw, dh);
1206
1207 if (isDefaultDisplay) {
1208 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1209 mCompatDisplayMetrics);
1210 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001211
1212 updateBounds();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001213 return mDisplayInfo;
1214 }
1215
1216 /**
1217 * Compute display configuration based on display properties and policy settings.
1218 * Do not call if mDisplayReady == false.
1219 */
1220 void computeScreenConfiguration(Configuration config) {
1221 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1222
1223 final int dw = displayInfo.logicalWidth;
1224 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001225 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1226 // TODO: Probably best to set this based on some setting in the display content object,
1227 // so the display can be configured for things like fullscreen.
1228 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001229
Andrii Kulian06d07d62017-03-14 11:11:47 -07001230 config.screenWidthDp =
1231 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1232 config.uiMode, mDisplayId) / mDisplayMetrics.density);
1233 config.screenHeightDp =
1234 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1235 config.uiMode, mDisplayId) / mDisplayMetrics.density);
Bryce Lee7566d762017-03-30 09:34:15 -07001236
1237 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh, mTmpRect);
1238 final int leftInset = mTmpRect.left;
1239 final int topInset = mTmpRect.top;
1240 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001241 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1242 leftInset + displayInfo.appWidth /* right */,
1243 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001244 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1245 || displayInfo.rotation == Surface.ROTATION_270);
1246
1247 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
1248 mDisplayMetrics.density, config);
1249
1250 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1251 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1252 ? Configuration.SCREENLAYOUT_ROUND_YES
1253 : Configuration.SCREENLAYOUT_ROUND_NO);
1254
1255 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1256 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1257 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1258 dh, mDisplayId);
1259 config.densityDpi = displayInfo.logicalDensityDpi;
1260
1261 config.colorMode =
1262 (displayInfo.isHdr()
1263 ? Configuration.COLOR_MODE_HDR_YES
1264 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001265 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001266 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1267 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1268
1269 // Update the configuration based on available input devices, lid switch,
1270 // and platform configuration.
1271 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1272 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1273 config.navigation = Configuration.NAVIGATION_NONAV;
1274
1275 int keyboardPresence = 0;
1276 int navigationPresence = 0;
1277 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1278 final int len = devices != null ? devices.length : 0;
1279 for (int i = 0; i < len; i++) {
1280 InputDevice device = devices[i];
1281 if (!device.isVirtual()) {
1282 final int sources = device.getSources();
1283 final int presenceFlag = device.isExternal() ?
1284 WindowManagerPolicy.PRESENCE_EXTERNAL :
1285 WindowManagerPolicy.PRESENCE_INTERNAL;
1286
1287 // TODO(multi-display): Configure on per-display basis.
1288 if (mService.mIsTouchDevice) {
1289 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1290 InputDevice.SOURCE_TOUCHSCREEN) {
1291 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1292 }
1293 } else {
1294 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1295 }
1296
1297 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1298 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1299 navigationPresence |= presenceFlag;
1300 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1301 && config.navigation == Configuration.NAVIGATION_NONAV) {
1302 config.navigation = Configuration.NAVIGATION_DPAD;
1303 navigationPresence |= presenceFlag;
1304 }
1305
1306 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1307 config.keyboard = Configuration.KEYBOARD_QWERTY;
1308 keyboardPresence |= presenceFlag;
1309 }
1310 }
1311 }
1312
1313 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1314 config.navigation = Configuration.NAVIGATION_DPAD;
1315 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1316 }
1317
1318 // Determine whether a hard keyboard is available and enabled.
1319 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1320 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1321 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1322 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1323 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1324 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1325 }
1326
1327 // Let the policy update hidden states.
1328 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1329 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1330 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1331 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1332 }
1333
1334 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1335 int displayId) {
1336 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1337 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1338 final int unrotDw, unrotDh;
1339 if (rotated) {
1340 unrotDw = dh;
1341 unrotDh = dw;
1342 } else {
1343 unrotDw = dw;
1344 unrotDh = dh;
1345 }
1346 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1347 displayId);
1348 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1349 displayId);
1350 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1351 displayId);
1352 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1353 displayId);
1354 return sw;
1355 }
1356
1357 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1358 DisplayMetrics dm, int dw, int dh, int displayId) {
1359 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1360 displayId);
1361 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
1362 uiMode, displayId);
1363 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1364 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1365 if (curSize == 0 || size < curSize) {
1366 curSize = size;
1367 }
1368 return curSize;
1369 }
1370
1371 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1372 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1373
1374 // We need to determine the smallest width that will occur under normal
1375 // operation. To this, start with the base screen size and compute the
1376 // width under the different possible rotations. We need to un-rotate
1377 // the current screen dimensions before doing this.
1378 int unrotDw, unrotDh;
1379 if (rotated) {
1380 unrotDw = dh;
1381 unrotDh = dw;
1382 } else {
1383 unrotDw = dw;
1384 unrotDh = dh;
1385 }
1386 displayInfo.smallestNominalAppWidth = 1<<30;
1387 displayInfo.smallestNominalAppHeight = 1<<30;
1388 displayInfo.largestNominalAppWidth = 0;
1389 displayInfo.largestNominalAppHeight = 0;
1390 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1391 unrotDh);
1392 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1393 unrotDw);
1394 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1395 unrotDh);
1396 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1397 unrotDw);
1398 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1399 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1400 displayId);
1401 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1402 displayId);
1403 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1404 displayId);
1405 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1406 displayId);
1407 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1408 outConfig.screenLayout = sl;
1409 }
1410
1411 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1412 int uiMode, int displayId) {
1413 // Get the app screen size at this rotation.
1414 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId);
1415 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId);
1416
1417 // Compute the screen layout size class for this rotation.
1418 int longSize = w;
1419 int shortSize = h;
1420 if (longSize < shortSize) {
1421 int tmp = longSize;
1422 longSize = shortSize;
1423 shortSize = tmp;
1424 }
1425 longSize = (int)(longSize/density);
1426 shortSize = (int)(shortSize/density);
1427 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1428 }
1429
1430 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1431 int uiMode, int dw, int dh) {
1432 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1433 displayId);
1434 if (width < displayInfo.smallestNominalAppWidth) {
1435 displayInfo.smallestNominalAppWidth = width;
1436 }
1437 if (width > displayInfo.largestNominalAppWidth) {
1438 displayInfo.largestNominalAppWidth = width;
1439 }
1440 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1441 displayId);
1442 if (height < displayInfo.smallestNominalAppHeight) {
1443 displayInfo.smallestNominalAppHeight = height;
1444 }
1445 if (height > displayInfo.largestNominalAppHeight) {
1446 displayInfo.largestNominalAppHeight = height;
1447 }
1448 }
1449
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001450 DockedStackDividerController getDockedDividerController() {
1451 return mDividerControllerLocked;
1452 }
1453
Winson Chung655332c2016-10-31 13:14:28 -07001454 PinnedStackController getPinnedStackController() {
1455 return mPinnedStackControllerLocked;
1456 }
1457
Jeff Browna506a6e2013-06-04 00:02:38 -07001458 /**
1459 * Returns true if the specified UID has access to this display.
1460 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001461 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001462 return mDisplay.hasAccess(uid);
1463 }
1464
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001465 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001466 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001467 }
1468
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001469 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001470 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001471 }
1472
Wale Ogunwale61911492017-10-11 08:50:50 -07001473 /**
1474 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1475 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001476 TaskStack getSplitScreenPrimaryStack() {
1477 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001478 return (stack != null && stack.isVisible()) ? stack : null;
1479 }
1480
1481 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001482 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001483 * not visible.
1484 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001485 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1486 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001487 }
1488
1489 TaskStack getPinnedStack() {
1490 return mTaskStackContainers.getPinnedStack();
1491 }
1492
1493 private boolean hasPinnedStack() {
1494 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001495 }
1496
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001497 /**
1498 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1499 * Null is no compatible stack on the display.
1500 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001501 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001502 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1503 }
1504
1505 /**
1506 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1507 * activity type. Null is no compatible stack on the display.
1508 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001509 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001510 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001511 }
1512
Bryce Lee48f4b572017-04-10 10:54:15 -07001513 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001514 TaskStack getTopStack() {
1515 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001516 }
1517
Wale Ogunwale61911492017-10-11 08:50:50 -07001518 void onStackWindowingModeChanged(TaskStack stack) {
1519 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001520 }
1521
Andrii Kulian441e4492016-09-29 15:25:00 -07001522 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001523 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001524 super.onConfigurationChanged(newParentConfig);
1525
Andrii Kulian3a507b52016-09-19 18:14:12 -07001526 // The display size information is heavily dependent on the resources in the current
1527 // configuration, so we need to reconfigure it every time the configuration changes.
1528 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1529 mService.reconfigureDisplayLocked(this);
1530
Bryce Leef3c6a472017-11-14 14:53:06 -08001531 final DockedStackDividerController dividerController = getDockedDividerController();
1532
1533 if (dividerController != null) {
1534 getDockedDividerController().onConfigurationChanged();
1535 }
1536
1537 final PinnedStackController pinnedStackController = getPinnedStackController();
1538
1539 if (pinnedStackController != null) {
1540 getPinnedStackController().onConfigurationChanged();
1541 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001542 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001543
Andrii Kulian441e4492016-09-29 15:25:00 -07001544 /**
1545 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1546 * bounds were updated.
1547 */
1548 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001549 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1550 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001551 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -07001552 changedStackList.add(stack.mStackId);
1553 }
1554 }
Winson Chung32c566f2017-04-11 18:31:21 -07001555
1556 // If there was no pinned stack, we still need to notify the controller of the display info
1557 // update as a result of the config change. We do this here to consolidate the flow between
1558 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001559 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001560 mPinnedStackControllerLocked.onDisplayInfoChanged();
1561 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001562 }
1563
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001564 @Override
1565 boolean fillsParent() {
1566 return true;
1567 }
1568
1569 @Override
1570 boolean isVisible() {
1571 return true;
1572 }
1573
1574 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001575 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001576 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001577 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001578 }
1579
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001580 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001581 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1582 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1583 // target, or here in order if there isn't an IME target.
1584 if (traverseTopToBottom) {
1585 for (int i = mChildren.size() - 1; i >= 0; --i) {
1586 final DisplayChildWindowContainer child = mChildren.get(i);
1587 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1588 // In this case the Ime windows will be processed above their target so we skip
1589 // here.
1590 continue;
1591 }
1592 if (child.forAllWindows(callback, traverseTopToBottom)) {
1593 return true;
1594 }
1595 }
1596 } else {
1597 final int count = mChildren.size();
1598 for (int i = 0; i < count; i++) {
1599 final DisplayChildWindowContainer child = mChildren.get(i);
1600 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
1601 // In this case the Ime windows will be processed above their target so we skip
1602 // here.
1603 continue;
1604 }
1605 if (child.forAllWindows(callback, traverseTopToBottom)) {
1606 return true;
1607 }
1608 }
1609 }
1610 return false;
1611 }
1612
1613 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1614 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1615 }
1616
Wale Ogunwale399c8692017-05-08 14:22:42 -07001617 @Override
1618 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001619 final WindowManagerPolicy policy = mService.mPolicy;
1620
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001621 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001622 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001623 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08001624 "Display is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001625 // If the display is frozen, some activities may be in the middle of restarting, and
1626 // thus have removed their old window. If the window has the flag to hide the lock
1627 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1628 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001629 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001630 } else if (policy.isKeyguardLocked()) {
1631 // Use the last orientation the while the display is frozen with the keyguard
1632 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1633 // window. We don't want to check the show when locked window directly though as
1634 // things aren't stable while the display is frozen, for example the window could be
1635 // momentarily unavailable due to activity relaunch.
1636 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
Andrii Kulian8ee72852017-03-10 10:36:45 -08001637 + "return " + mLastOrientation);
1638 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001639 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001640 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001641 final int orientation = mAboveAppWindowsContainers.getOrientation();
1642 if (orientation != SCREEN_ORIENTATION_UNSET) {
1643 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001644 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001645 }
1646
Wale Ogunwale399c8692017-05-08 14:22:42 -07001647 // Top system windows are not requesting an orientation. Start searching from apps.
1648 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001649 }
1650
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001651 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001652 // Check if display metrics changed and update base values if needed.
1653 updateBaseDisplayMetricsIfNeeded();
1654
Craig Mautner722285e2012-09-07 13:55:58 -07001655 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001656 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001657
Wale Ogunwale61911492017-10-11 08:50:50 -07001658 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1659 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001660 }
Craig Mautner722285e2012-09-07 13:55:58 -07001661 }
1662
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001663 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001664 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1665 if (displayManagerInternal != null) {
1666 // Bootstrap the default logical display from the display manager.
1667 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1668 if (newDisplayInfo != null) {
1669 mDisplayInfo.copyFrom(newDisplayInfo);
1670 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001671 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001672
Andrii Kuliancd097992017-03-23 18:31:59 -07001673 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1674 mDisplayInfo.logicalDensityDpi);
1675 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1676 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1677 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001678 }
1679
Andrii Kuliancd097992017-03-23 18:31:59 -07001680 /**
1681 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1682 * values.
1683 */
1684 private void updateBaseDisplayMetricsIfNeeded() {
1685 // Get real display metrics without overrides from WM.
1686 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1687 final int orientation = mDisplayInfo.rotation;
1688 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1689 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1690 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1691 final int newDensity = mDisplayInfo.logicalDensityDpi;
1692
1693 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1694 || mInitialDisplayHeight != newHeight
1695 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi;
1696
1697 if (displayMetricsChanged) {
1698 // Check if display size or density is forced.
1699 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1700 || mBaseDisplayHeight != mInitialDisplayHeight;
1701 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1702
1703 // If there is an override set for base values - use it, otherwise use new values.
1704 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1705 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1706 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1707
1708 // Real display metrics changed, so we should also update initial values.
1709 mInitialDisplayWidth = newWidth;
1710 mInitialDisplayHeight = newHeight;
1711 mInitialDisplayDensity = newDensity;
1712 mService.reconfigureDisplayLocked(this);
1713 }
1714 }
1715
Bryce Lee27cec322017-03-21 09:41:37 -07001716 /** Sets the maximum width the screen resolution can be */
1717 void setMaxUiWidth(int width) {
1718 if (DEBUG_DISPLAY) {
1719 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1720 }
1721
1722 mMaxUiWidth = width;
1723
1724 // Update existing metrics.
1725 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1726 }
1727
1728 /** Update base (override) display metrics. */
1729 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1730 mBaseDisplayWidth = baseWidth;
1731 mBaseDisplayHeight = baseHeight;
1732 mBaseDisplayDensity = baseDensity;
1733
1734 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1735 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1736 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1737 mBaseDisplayWidth = mMaxUiWidth;
1738
1739 if (DEBUG_DISPLAY) {
1740 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1741 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1742 + " on display:" + getDisplayId());
1743 }
1744 }
1745
1746 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08001747
1748 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07001749 }
1750
Wale Ogunwaledb506192017-12-08 10:57:32 -08001751 void getStableRect(Rect out) {
1752 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08001753 }
1754
Wale Ogunwale61911492017-10-11 08:50:50 -07001755 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001756 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1757 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001758
Wale Ogunwale61911492017-10-11 08:50:50 -07001759 final TaskStack stack = new TaskStack(mService, stackId, controller);
1760 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001761 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001762 }
1763
Andrii Kulian51c1b672017-04-07 18:39:32 -07001764 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001765 final DisplayContent prevDc = stack.getDisplayContent();
1766 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001767 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1768 + " which is not currently attached to any display");
1769 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001770 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001771 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1772 + " to its current displayId=" + mDisplayId);
1773 }
1774
Wale Ogunwale61911492017-10-11 08:50:50 -07001775 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001776 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001777 }
1778
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001779 @Override
1780 protected void addChild(DisplayChildWindowContainer child,
1781 Comparator<DisplayChildWindowContainer> comparator) {
1782 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1783 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001784
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001785 @Override
1786 protected void addChild(DisplayChildWindowContainer child, int index) {
1787 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1788 }
1789
1790 @Override
1791 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001792 // Only allow removal of direct children from this display if the display is in the process
1793 // of been removed.
1794 if (mRemovingDisplay) {
1795 super.removeChild(child);
1796 return;
1797 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001798 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001799 }
1800
Andrii Kuliand2765632016-12-12 22:26:34 -08001801 @Override
1802 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1803 // Children of the display are statically ordered, so the real intention here is to perform
1804 // the operation on the display and not the static direct children.
1805 getParent().positionChildAt(position, this, includingParents);
1806 }
1807
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001808 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001809 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1810 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001811 final int taskId = stack.taskIdFromPoint(x, y);
1812 if (taskId != -1) {
1813 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001814 }
1815 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001816 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001817 }
1818
Chong Zhang8e89b312015-09-09 15:09:30 -07001819 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001820 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001821 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001822 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001823 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001824 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001825 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07001826 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1827 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001828 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001829 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001830 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001831
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001832 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1833 if (mTmpTaskForResizePointSearchResult.searchDone) {
1834 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001835 }
1836 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001837 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001838 }
1839
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001840 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001841 // The provided task is the task on this display with focus, so if WindowManagerService's
1842 // focused app is not on this display, focusedTask will be null.
1843 if (focusedTask == null) {
1844 mTouchExcludeRegion.setEmpty();
1845 } else {
1846 mTouchExcludeRegion.set(mBaseDisplayRect);
1847 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1848 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07001849 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1850 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001851 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
1852 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08001853 }
1854 // If we removed the focused task above, add it back and only leave its
1855 // outside touch area in the exclusion. TapDectector is not interested in
1856 // any touch inside the focused task itself.
1857 if (!mTmpRect2.isEmpty()) {
1858 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1859 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001860 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001861 final WindowState inputMethod = mService.mInputMethodWindow;
1862 if (inputMethod != null && inputMethod.isVisibleLw()) {
1863 // If the input method is visible and the user is typing, we don't want these touch
1864 // events to be intercepted and used to change focus. This would likely cause a
1865 // disappearance of the input method.
1866 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001867 if (inputMethod.getDisplayId() == mDisplayId) {
1868 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1869 } else {
1870 // IME is on a different display, so we need to update its tap detector.
1871 // TODO(multidisplay): Remove when IME will always appear on same display.
1872 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1873 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001874 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001875 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1876 WindowState win = mTapExcludedWindows.get(i);
1877 win.getTouchableRegion(mTmpRegion);
1878 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1879 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001880 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07001881 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001882 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001883 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001884 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1885 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001886 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001887 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001888 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001889 }
1890
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001891 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001892 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001893 super.switchUser();
1894 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001895 }
1896
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001897 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001898 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1899 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001900 }
1901 }
1902
Wale Ogunwale10124582016-09-15 20:25:50 -07001903 @Override
1904 void removeIfPossible() {
1905 if (isAnimating()) {
1906 mDeferredRemoval = true;
1907 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001908 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001909 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001910 }
1911
Wale Ogunwale10124582016-09-15 20:25:50 -07001912 @Override
1913 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001914 mRemovingDisplay = true;
1915 try {
1916 super.removeImmediately();
1917 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07001918 if (mService.canDispatchPointerEvents()) {
1919 if (mTapDetector != null) {
1920 mService.unregisterPointerEventListener(mTapDetector);
1921 }
1922 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
1923 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1924 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001925 }
Robert Carrb1579c82017-09-05 14:54:47 -07001926 // The pending transaction won't be applied so we should
1927 // just clean up any surfaces pending destruction.
1928 onPendingTransactionApplied();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001929 } finally {
1930 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001931 }
Craig Mautner95da1082014-02-24 17:54:35 -08001932 }
1933
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001934 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001935 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001936 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001937 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1938
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001939 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001940 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001941 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001942 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001943 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001944 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001945 }
1946
Andrii Kulian0214ed92017-05-16 13:44:05 -07001947 /** @return 'true' if removal of this display content is deferred due to active animation. */
1948 boolean isRemovalDeferred() {
1949 return mDeferredRemoval;
1950 }
1951
Wale Ogunwale10124582016-09-15 20:25:50 -07001952 boolean animateForIme(float interpolatedValue, float animationTarget,
1953 float dividerAnimationTarget) {
1954 boolean updated = false;
1955
Wale Ogunwale61911492017-10-11 08:50:50 -07001956 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1957 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001958 if (stack == null || !stack.isAdjustedForIme()) {
1959 continue;
1960 }
1961
1962 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1963 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1964 updated = true;
1965 } else {
1966 mDividerControllerLocked.mLastAnimationProgress =
1967 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1968 mDividerControllerLocked.mLastDividerProgress =
1969 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1970 updated |= stack.updateAdjustForIme(
1971 mDividerControllerLocked.mLastAnimationProgress,
1972 mDividerControllerLocked.mLastDividerProgress,
1973 false /* force */);
1974 }
1975 if (interpolatedValue >= 1f) {
1976 stack.endImeAdjustAnimation();
1977 }
1978 }
1979
1980 return updated;
1981 }
1982
1983 boolean clearImeAdjustAnimation() {
1984 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07001985 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1986 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001987 if (stack != null && stack.isAdjustedForIme()) {
1988 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1989 changed = true;
1990 }
1991 }
1992 return changed;
1993 }
1994
1995 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001996 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1997 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001998 if (stack.isVisible() && stack.isAdjustedForIme()) {
1999 stack.beginImeAdjustAnimation();
2000 }
2001 }
2002 }
2003
2004 void adjustForImeIfNeeded() {
2005 final WindowState imeWin = mService.mInputMethodWindow;
2006 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2007 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002008 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002009 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2010 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2011 imeTargetStack.getDockSide() : DOCKED_INVALID;
2012 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2013 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2014 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002015 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002016 final boolean imeHeightChanged = imeVisible &&
2017 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2018
2019 // The divider could be adjusted for IME position, or be thinner than usual,
2020 // or both. There are three possible cases:
2021 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2022 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2023 // - If IME is not visible, divider is not moved and is normal width.
2024
2025 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002026 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2027 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002028 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002029 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2030 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002031 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2032 } else {
2033 stack.resetAdjustedForIme(false);
2034 }
2035 }
2036 mDividerControllerLocked.setAdjustedForIme(
2037 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2038 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002039 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2040 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002041 stack.resetAdjustedForIme(!dockVisible);
2042 }
2043 mDividerControllerLocked.setAdjustedForIme(
2044 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2045 }
Winson Chung655332c2016-10-31 13:14:28 -07002046 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002047 }
2048
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002049 /**
2050 * If a window that has an animation specifying a colored background and the current wallpaper
2051 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2052 * suddenly disappear.
2053 */
2054 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002055 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2056 w -> w.mIsWallpaper && w.isVisibleNow());
2057
2058 if (visibleWallpaper != null) {
2059 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002060 }
2061 return winAnimator.mAnimLayer;
2062 }
2063
Wale Ogunwale10124582016-09-15 20:25:50 -07002064 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002065 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2066 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002067 stack.prepareFreezingTaskBounds();
2068 }
2069 }
2070
Wale Ogunwale94744212015-09-21 19:01:47 -07002071 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002072 getBounds(mTmpRect, newRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002073
2074 // Compute a transform matrix to undo the coordinate space transformation,
2075 // and present the window at the same physical position it previously occupied.
2076 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2077 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2078
2079 mTmpRectF.set(bounds);
2080 mTmpMatrix.mapRect(mTmpRectF);
2081 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002082 }
2083
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002084 static int deltaRotation(int oldRotation, int newRotation) {
2085 int delta = newRotation - oldRotation;
2086 if (delta < 0) delta += 4;
2087 return delta;
2088 }
2089
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002090 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002091 Matrix outMatrix) {
2092 // For rotations without Z-ordering we don't need the target rectangle's position.
2093 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2094 displayHeight, outMatrix);
2095 }
2096
2097 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2098 float displayWidth, float displayHeight, Matrix outMatrix) {
2099 switch (rotation) {
2100 case ROTATION_0:
2101 outMatrix.reset();
2102 break;
2103 case ROTATION_270:
2104 outMatrix.setRotate(270, 0, 0);
2105 outMatrix.postTranslate(0, displayHeight);
2106 outMatrix.postTranslate(rectTop, 0);
2107 break;
2108 case ROTATION_180:
2109 outMatrix.reset();
2110 break;
2111 case ROTATION_90:
2112 outMatrix.setRotate(90, 0, 0);
2113 outMatrix.postTranslate(displayWidth, 0);
2114 outMatrix.postTranslate(-rectTop, rectLeft);
2115 break;
2116 }
2117 }
2118
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002119 @CallSuper
2120 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002121 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002122 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002123 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002124 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002125 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2126 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002127 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002128 }
2129 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2130 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002131 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2132 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002133 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002134 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002135 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2136 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002137 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002138 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002139 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2140 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002141 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002142 }
2143 proto.write(DPI, mBaseDisplayDensity);
2144 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002145 proto.write(ROTATION, mRotation);
2146 final ScreenRotationAnimation screenRotationAnimation =
2147 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2148 if (screenRotationAnimation != null) {
2149 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2150 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002151 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002152 proto.end(token);
2153 }
2154
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002155 @Override
2156 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2157 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002158 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2159 final String subPrefix = " " + prefix;
2160 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2161 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2162 pw.print("dpi");
2163 if (mInitialDisplayWidth != mBaseDisplayWidth
2164 || mInitialDisplayHeight != mBaseDisplayHeight
2165 || mInitialDisplayDensity != mBaseDisplayDensity) {
2166 pw.print(" base=");
2167 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2168 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2169 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002170 if (mDisplayScalingDisabled) {
2171 pw.println(" noscale");
2172 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002173 pw.print(" cur=");
2174 pw.print(mDisplayInfo.logicalWidth);
2175 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2176 pw.print(" app=");
2177 pw.print(mDisplayInfo.appWidth);
2178 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2179 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2180 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2181 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2182 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002183 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002184 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002185 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002186
Craig Mautnerdc548482014-02-05 13:35:24 -08002187 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002188 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002189 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2190 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002191 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002192 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002193
Craig Mautnerdc548482014-02-05 13:35:24 -08002194 pw.println();
2195 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002196 pw.println();
2197 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002198 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002199 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002200 pw.print(" Exiting #"); pw.print(i);
2201 pw.print(' '); pw.print(token);
2202 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002203 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002204 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002205 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002206
Jorim Jaggi31f71702016-05-04 16:43:04 -07002207 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002208
2209 // Dump stack references
2210 final TaskStack homeStack = getHomeStack();
2211 if (homeStack != null) {
2212 pw.println(prefix + "homeStack=" + homeStack.getName());
2213 }
2214 final TaskStack pinnedStack = getPinnedStack();
2215 if (pinnedStack != null) {
2216 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2217 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002218 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002219 if (splitScreenPrimaryStack != null) {
2220 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2221 }
2222
2223 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002224 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002225 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002226 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002227
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002228 pw.println();
2229 mDisplayFrames.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002230 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002231
2232 @Override
2233 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002234 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002235 }
2236
2237 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002238 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002239 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002240
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002241 /** Returns true if the stack in the windowing mode is visible. */
2242 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002243 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002244 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002245 }
2246
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002247 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002248 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002249 final int x = (int) xf;
2250 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002251 final WindowState touchedWin = getWindow(w -> {
2252 final int flags = w.mAttrs.flags;
2253 if (!w.isVisibleLw()) {
2254 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002255 }
2256 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002257 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002258 }
2259
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002260 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002261 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002262 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002263 }
2264
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002265 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002266
2267 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002268 return mTmpRegion.contains(x, y) || touchFlags == 0;
2269 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002270
2271 return touchedWin;
2272 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002273
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002274 boolean canAddToastWindowForUid(int uid) {
2275 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002276 // Also if the app is focused adding more than one toast at
2277 // a time for better backwards compatibility.
2278 final WindowState focusedWindowForUid = getWindow(w ->
2279 w.mOwnerUid == uid && w.isFocused());
2280 if (focusedWindowForUid != null) {
2281 return true;
2282 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002283 final WindowState win = getWindow(w ->
2284 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2285 && !w.mWindowRemovalAllowed);
2286 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002287 }
2288
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002289 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002290 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2291 return;
2292 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002293
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002294 // Used to communicate the old focus to the callback method.
2295 mTmpWindow = oldFocus;
2296
2297 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002298 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002299
2300 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002301 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002302
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002303 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002304
2305 if (mTmpWindow == null) {
2306 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2307 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002308 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002309 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002310 }
2311
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002312 /** Updates the layer assignment of windows on this display. */
2313 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002314 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002315 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002316 if (setLayoutNeeded) {
2317 setLayoutNeeded();
2318 }
Robert Carrb1579c82017-09-05 14:54:47 -07002319
Robert Carrf59b8dd2017-10-02 18:58:36 -07002320 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002321 // the application of this transaction until the animation pass triggers
2322 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2323 // the hiding and showing of surfaces.
2324 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002325 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002326 }
2327
Wale Ogunwale1666e312016-12-16 11:27:18 -08002328 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2329 // moving containers or resizing them. Need to investigate the best way to have it automatically
2330 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002331 void layoutAndAssignWindowLayersIfNeeded() {
2332 mService.mWindowsChanged = true;
2333 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002334
2335 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2336 false /*updateInputWindows*/)) {
2337 assignWindowLayers(false /* setLayoutNeeded */);
2338 }
2339
2340 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2341 mService.mWindowPlacerLocked.performSurfacePlacement();
2342 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2343 }
2344
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002345 /** Returns true if a leaked surface was destroyed */
2346 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002347 // Used to indicate that a surface was leaked.
2348 mTmpWindow = null;
2349 forAllWindows(w -> {
2350 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002351 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002352 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002353 }
2354 if (!mService.mSessions.contains(wsa.mSession)) {
2355 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002356 + w + " surface=" + wsa.mSurfaceController
2357 + " token=" + w.mToken
2358 + " pid=" + w.mSession.mPid
2359 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002360 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002361 mService.mForceRemoves.add(w);
2362 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002363 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002364 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002365 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002366 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002367 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002368 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002369 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002370 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002371 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002372
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002373 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002374 }
2375
2376 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002377 * Determine and return the window that should be the IME target.
2378 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2379 * @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 +00002380 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002381 WindowState computeImeTarget(boolean updateImeTarget) {
2382 if (mService.mInputMethodWindow == null) {
2383 // There isn't an IME so there shouldn't be a target...That was easy!
2384 if (updateImeTarget) {
2385 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2386 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002387 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002388 }
2389 return null;
2390 }
2391
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002392 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2393 // same display. Or even when the current IME/target are not on the same screen as the next
2394 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002395 mUpdateImeTarget = updateImeTarget;
2396 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002397
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002398
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002399 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2400 // to be on top of it, but it is not -really- where input will go. So look down below
2401 // for a real window to target...
2402 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2403 final AppWindowToken token = target.mAppToken;
2404 if (token != null) {
2405 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2406 if (betterTarget != null) {
2407 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002408 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002409 }
2410 }
2411
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002412 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2413 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002414
2415 // Now, a special case -- if the last target's window is in the process of exiting, and is
2416 // above the new target, keep on the last target to avoid flicker. Consider for example a
2417 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
2418 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
2419 // scrim.
2420 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002421 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
2422 && (target == null
2423 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002424 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002425 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002426 }
2427
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002428 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2429 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002430
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002431 if (target == null) {
2432 if (updateImeTarget) {
2433 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2434 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2435 + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002436 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002437 }
2438
2439 return null;
2440 }
2441
2442 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002443 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2444 if (token != null) {
2445
2446 // Now some fun for dealing with window animations that modify the Z order. We need
2447 // to look at all windows below the current target that are in this app, finding the
2448 // highest visible one in layering.
2449 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002450 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002451 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002452 }
2453
2454 if (highestTarget != null) {
2455 final AppTransition appTransition = mService.mAppTransition;
2456 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2457 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2458 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002459 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002460
2461 if (appTransition.isTransitionSet()) {
2462 // If we are currently setting up for an animation, hold everything until we
2463 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002464 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002465 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002466 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002467 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002468 // If the window we are currently targeting is involved with an animation,
2469 // and it is on top of the next target we will be over, then hold off on
2470 // moving until that is done.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002471 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002472 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002473 }
2474 }
2475 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002476
2477 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2478 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002479 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002480 }
2481
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002482 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002483 }
2484
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002485 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002486 if (target == mService.mInputMethodTarget
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002487 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002488 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002489 }
2490
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002491 mService.mInputMethodTarget = target;
2492 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002493 assignWindowLayers(false /* setLayoutNeeded */);
2494 }
2495
2496 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2497 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2498 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2499 }
2500
2501 // Used to indicate we have reached the first window in the range we are interested in.
2502 mTmpWindow = null;
2503
2504 // TODO: Figure-out a more efficient way to do this.
2505 final WindowState candidate = getWindow(w -> {
2506 if (w == top) {
2507 // Reached the first window in the range we are interested in.
2508 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002509 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002510 if (mTmpWindow == null) {
2511 return false;
2512 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002513
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002514 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2515 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002516 }
2517 // If we reached the bottom of the range of windows we are considering,
2518 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002519 if (w == bottom) {
2520 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002521 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002522 return false;
2523 });
2524
2525 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002526 }
2527
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002528 void setLayoutNeeded() {
2529 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2530 mLayoutNeeded = true;
2531 }
2532
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002533 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002534 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2535 mLayoutNeeded = false;
2536 }
2537
2538 boolean isLayoutNeeded() {
2539 return mLayoutNeeded;
2540 }
2541
Wale Ogunwale02319a62016-09-26 15:21:22 -07002542 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2543 if (mTokenMap.isEmpty()) {
2544 return;
2545 }
2546 pw.println(" Display #" + mDisplayId);
2547 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2548 while (it.hasNext()) {
2549 final WindowToken token = it.next();
2550 pw.print(" ");
2551 pw.print(token);
2552 if (dumpAll) {
2553 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002554 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07002555 } else {
2556 pw.println();
2557 }
2558 }
2559 }
2560
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002561 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002562 final int[] index = new int[1];
2563 forAllWindows(w -> {
2564 final WindowStateAnimator wAnim = w.mWinAnimator;
2565 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2566 index[0] = index[0] + 1;
2567 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002568 }
2569
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002570 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002571 forAllWindows(w -> {
2572 w.mWinAnimator.enableSurfaceTrace(fd);
2573 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002574 }
2575
2576 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002577 forAllWindows(w -> {
2578 w.mWinAnimator.disableSurfaceTrace();
2579 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002580 }
2581
Jorim Jaggife762342016-10-13 14:33:27 +02002582 /**
2583 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2584 */
2585 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2586 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002587 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002588 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2589 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002590 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02002591 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002592 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002593 }
2594
Wale Ogunwale494009b82016-10-21 09:01:38 -07002595 boolean checkWaitingForWindows() {
2596
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002597 mHaveBootMsg = false;
2598 mHaveApp = false;
2599 mHaveWallpaper = false;
2600 mHaveKeyguard = true;
2601
2602 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002603 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2604 return true;
2605 }
2606 if (w.isDrawnLw()) {
2607 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002608 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002609 } else if (w.mAttrs.type == TYPE_APPLICATION
2610 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002611 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002612 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002613 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002614 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002615 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002616 }
2617 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002618 return false;
2619 });
2620
2621 if (visibleWindow != null) {
2622 // We have a visible window.
2623 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002624 }
2625
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002626 // if the wallpaper service is disabled on the device, we're never going to have
2627 // wallpaper, don't bother waiting for it
2628 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2629 com.android.internal.R.bool.config_enableWallpaperService)
2630 && !mService.mOnlyCore;
2631
Wale Ogunwale494009b82016-10-21 09:01:38 -07002632 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2633 "******** booted=" + mService.mSystemBooted
2634 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002635 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2636 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2637 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002638
2639 // If we are turning on the screen to show the boot message, don't do it until the boot
2640 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002641 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002642 return true;
2643 }
2644
2645 // If we are turning on the screen after the boot is completed normally, don't do so until
2646 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002647 if (mService.mSystemBooted
2648 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002649 return true;
2650 }
2651
2652 return false;
2653 }
2654
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002655 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002656 mTmpWindowAnimator = animator;
2657 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002658 }
2659
2660 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002661 resetAnimationBackgroundAnimator();
2662
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002663 // Used to indicate a detached wallpaper.
2664 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002665 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002666
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002667 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002668
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002669 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002670 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002671 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2672 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002673 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2674 }
2675 }
2676
Wale Ogunwale494009b82016-10-21 09:01:38 -07002677 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002678 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002679 if (imFocus == null) {
2680 return false;
2681 }
2682
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002683 if (DEBUG_INPUT_METHOD) {
2684 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2685 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2686 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002687 }
2688
Wale Ogunwale494009b82016-10-21 09:01:38 -07002689 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2690
2691 if (DEBUG_INPUT_METHOD) {
2692 Slog.i(TAG_WM, "IM target client: " + imeClient);
2693 if (imeClient != null) {
2694 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2695 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2696 }
2697 }
2698
2699 return imeClient != null && imeClient.asBinder() == client.asBinder();
2700 }
2701
2702 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002703 final WindowState win = getWindow(
2704 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2705 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002706 }
2707
2708 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002709 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002710 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002711 final int curValue = w.mSystemUiVisibility;
2712 final int diff = (curValue ^ visibility) & globalDiff;
2713 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002714 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002715 w.mSeq++;
2716 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002717 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002718 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2719 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002720 visibility, newValue, diff);
2721 }
2722 } catch (RemoteException e) {
2723 // so sorry
2724 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002725 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002726 }
2727
2728 void onWindowFreezeTimeout() {
2729 Slog.w(TAG_WM, "Window freeze timeout expired.");
2730 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002731
2732 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002733 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002734 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002735 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002736 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002737 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2738 - mService.mDisplayFreezeTime);
2739 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002740 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002741 mService.mWindowPlacerLocked.performSurfacePlacement();
2742 }
2743
2744 void waitForAllWindowsDrawn() {
2745 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002746 forAllWindows(w -> {
2747 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2748 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2749 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002750 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002751 w.mLastContentInsets.set(-1, -1, -1, -1);
2752 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002753 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002754 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002755 }
2756
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002757 // TODO: Super crazy long method that should be broken down...
2758 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2759
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002760 final int dw = mDisplayInfo.logicalWidth;
2761 final int dh = mDisplayInfo.logicalHeight;
2762 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2763
2764 mTmpUpdateAllDrawn.clear();
2765
2766 int repeats = 0;
2767 do {
2768 repeats++;
2769 if (repeats > 6) {
2770 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2771 clearLayoutNeeded();
2772 break;
2773 }
2774
2775 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2776 pendingLayoutChanges);
2777
Andrii Kulian839def92016-11-02 10:58:58 -07002778 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2779 // the wallpaper window jumping across displays.
2780 // Remove check for default display when there will be support for multiple wallpaper
2781 // targets (on different displays).
2782 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002783 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002784 }
2785
2786 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2787 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2788 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2789 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002790 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002791 }
2792 }
2793
2794 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2795 setLayoutNeeded();
2796 }
2797
2798 // FIRST LOOP: Perform a layout, if needed.
2799 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2800 performLayout(repeats == 1, false /* updateInputWindows */);
2801 } else {
2802 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2803 }
2804
2805 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2806 pendingLayoutChanges = 0;
2807
2808 if (isDefaultDisplay) {
2809 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002810 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002811 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2812 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2813 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2814 }
2815 } while (pendingLayoutChanges != 0);
2816
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002817 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002818
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002819 mTmpRecoveringMemory = recoveringMemory;
2820 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002821
2822 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002823 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2824 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2825 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002826 true /* inTraversal, must call performTraversalInTrans... below */);
2827
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002828 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2829 if (wallpaperVisible != mLastWallpaperVisible) {
2830 mLastWallpaperVisible = wallpaperVisible;
2831 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2832 }
2833
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002834 while (!mTmpUpdateAllDrawn.isEmpty()) {
2835 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2836 // See if any windows have been drawn, so they (and others associated with them)
2837 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002838 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002839 }
2840
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002841 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002842 }
2843
Bryce Leef3c6a472017-11-14 14:53:06 -08002844 private void updateBounds() {
2845 calculateBounds(mTmpBounds);
2846 setBounds(mTmpBounds);
2847 }
2848
2849 // Determines the current display bounds based on the current state
2850 private void calculateBounds(Rect out) {
2851 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
2852 final int orientation = mDisplayInfo.rotation;
2853 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2854 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
2855 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
2856 int width = mDisplayInfo.logicalWidth;
2857 int left = (physWidth - width) / 2;
2858 int height = mDisplayInfo.logicalHeight;
2859 int top = (physHeight - height) / 2;
2860 out.set(left, top, left + width, top + height);
2861 }
2862
2863 @Override
2864 public void getBounds(Rect out) {
2865 calculateBounds(out);
2866 }
2867
2868 private void getBounds(Rect out, int orientation) {
2869 getBounds(out);
2870
2871 // Rotate the Rect if needed.
2872 final int currentRotation = mDisplayInfo.rotation;
2873 final int rotationDelta = deltaRotation(currentRotation, orientation);
2874 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
2875 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
2876 mTmpRectF.set(out);
2877 mTmpMatrix.mapRect(mTmpRectF);
2878 mTmpRectF.round(out);
2879 }
2880 }
2881
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002882 void performLayout(boolean initial, boolean updateInputWindows) {
2883 if (!isLayoutNeeded()) {
2884 return;
2885 }
2886 clearLayoutNeeded();
2887
2888 final int dw = mDisplayInfo.logicalWidth;
2889 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002890 if (DEBUG_LAYOUT) {
2891 Slog.v(TAG, "-------------------------------------");
2892 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2893 }
2894
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002895 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo);
2896 // TODO: Not sure if we really need to set the rotation here since we are updating from the
2897 // display info above...
2898 mDisplayFrames.mRotation = mRotation;
2899 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002900 if (isDefaultDisplay) {
2901 // Not needed on non-default displays.
2902 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2903 mService.mScreenRect.set(0, 0, dw, dh);
2904 }
2905
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002906 int seq = mService.mLayoutSeq + 1;
2907 if (seq < 0) seq = 0;
2908 mService.mLayoutSeq = seq;
2909
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002910 // Used to indicate that we have processed the dream window and all additional windows are
2911 // behind it.
2912 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002913 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002914
2915 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002916 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002917
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002918 // Used to indicate that we have processed the dream window and all additional attached
2919 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002920 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002921 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002922
2923 // Now perform layout of attached windows, which usually depend on the position of the
2924 // window they are attached to. XXX does not deal with windows that are attached to windows
2925 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002926 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002927
2928 // Window frames may have changed. Tell the input dispatcher about it.
2929 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2930 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2931 if (updateInputWindows) {
2932 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2933 }
2934
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002935 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2936 }
2937
2938 /**
2939 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2940 * In portrait mode, it grabs the full screenshot.
2941 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002942 * @param config of the output bitmap
2943 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
2944 */
chaviwfbe47df2017-11-10 16:14:49 -08002945 Bitmap screenshotDisplay(Bitmap.Config config, boolean wallpaperOnly) {
2946 synchronized (mService.mWindowMap) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02002947 if (!mService.mPolicy.isScreenOn()) {
chaviwfbe47df2017-11-10 16:14:49 -08002948 if (DEBUG_SCREENSHOT) {
2949 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002950 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002951 return null;
2952 }
2953
chaviwfbe47df2017-11-10 16:14:49 -08002954 if (wallpaperOnly && !shouldScreenshotWallpaper()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002955 return null;
2956 }
2957
chaviwfbe47df2017-11-10 16:14:49 -08002958 int dw = mDisplayInfo.logicalWidth;
2959 int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002960
chaviwfbe47df2017-11-10 16:14:49 -08002961 if (dw <= 0 || dh <= 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002962 return null;
2963 }
2964
chaviwfbe47df2017-11-10 16:14:49 -08002965 final Rect frame = new Rect(0, 0, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002966
2967 // The screenshot API does not apply the current screen rotation.
2968 int rot = mDisplay.getRotation();
2969
2970 if (rot == ROTATION_90 || rot == ROTATION_270) {
2971 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
2972 }
2973
chaviwfbe47df2017-11-10 16:14:49 -08002974 // SurfaceFlinger is not aware of orientation, so convert our logical
2975 // crop to SurfaceFlinger's portrait orientation.
2976 convertCropForSurfaceFlinger(frame, rot, dw, dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002977
2978 final ScreenRotationAnimation screenRotationAnimation =
2979 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
2980 final boolean inRotation = screenRotationAnimation != null &&
2981 screenRotationAnimation.isAnimating();
chaviwfbe47df2017-11-10 16:14:49 -08002982 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002983
Robert Carrb1579c82017-09-05 14:54:47 -07002984 // TODO(b/68392460): We should screenshot Task controls directly
2985 // but it's difficult at the moment as the Task doesn't have the
2986 // correct size set.
chaviwfbe47df2017-11-10 16:14:49 -08002987 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, 0, 1, inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002988 if (bitmap == null) {
Robert Carrb1579c82017-09-05 14:54:47 -07002989 Slog.w(TAG_WM, "Failed to take screenshot");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002990 return null;
2991 }
chaviwfbe47df2017-11-10 16:14:49 -08002992
2993 // Create a copy of the screenshot that is immutable and backed in ashmem.
2994 // This greatly reduces the overhead of passing the bitmap between processes.
2995 final Bitmap ret = bitmap.createAshmemBitmap(config);
2996 bitmap.recycle();
2997 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002998 }
chaviwfbe47df2017-11-10 16:14:49 -08002999 }
3000
3001 private boolean shouldScreenshotWallpaper() {
3002 MutableBoolean screenshotReady = new MutableBoolean(false);
3003
3004 forAllWindows(w -> {
3005 if (!w.mIsWallpaper) {
3006 return false;
3007 }
3008
3009 // Found the wallpaper window
3010 final WindowStateAnimator winAnim = w.mWinAnimator;
3011
3012 if (winAnim.getShown() && winAnim.mLastAlpha > 0f) {
3013 screenshotReady.value = true;
3014 }
3015
3016 return true;
3017 }, true /* traverseTopToBottom */);
3018
3019 return screenshotReady.value;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003020 }
3021
3022 // TODO: Can this use createRotationMatrix()?
3023 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3024 if (rot == Surface.ROTATION_90) {
3025 final int tmp = crop.top;
3026 crop.top = dw - crop.right;
3027 crop.right = crop.bottom;
3028 crop.bottom = dw - crop.left;
3029 crop.left = tmp;
3030 } else if (rot == Surface.ROTATION_180) {
3031 int tmp = crop.top;
3032 crop.top = dh - crop.bottom;
3033 crop.bottom = dh - tmp;
3034 tmp = crop.right;
3035 crop.right = dw - crop.left;
3036 crop.left = dw - tmp;
3037 } else if (rot == Surface.ROTATION_270) {
3038 final int tmp = crop.top;
3039 crop.top = crop.left;
3040 crop.left = dh - crop.bottom;
3041 crop.bottom = crop.right;
3042 crop.right = dh - tmp;
3043 }
3044 }
3045
3046 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003047 // Used to indicate the layout is needed.
3048 mTmpWindow = null;
3049
3050 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003051 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003052 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003053 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003054 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003055 w.setDisplayLayoutNeeded();
3056 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003057 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003058
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003059 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003060 mService.mWindowPlacerLocked.performSurfacePlacement();
3061 }
3062 }
3063
Wale Ogunwale1666e312016-12-16 11:27:18 -08003064 void setExitingTokensHasVisible(boolean hasVisible) {
3065 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3066 mExitingTokens.get(i).hasVisible = hasVisible;
3067 }
3068
3069 // Initialize state of exiting applications.
3070 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3071 }
3072
3073 void removeExistingTokensIfPossible() {
3074 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3075 final WindowToken token = mExitingTokens.get(i);
3076 if (!token.hasVisible) {
3077 mExitingTokens.remove(i);
3078 }
3079 }
3080
3081 // Time to remove any exiting applications?
3082 mTaskStackContainers.removeExistingAppTokensIfPossible();
3083 }
3084
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003085 @Override
3086 void onDescendantOverrideConfigurationChanged() {
3087 setLayoutNeeded();
3088 mService.requestTraversal();
3089 }
3090
David Stevens9440dc82017-03-16 19:00:20 -07003091 boolean okToDisplay() {
3092 if (mDisplayId == DEFAULT_DISPLAY) {
3093 return !mService.mDisplayFrozen
3094 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3095 }
3096 return mDisplayInfo.state == Display.STATE_ON;
3097 }
3098
3099 boolean okToAnimate() {
3100 return okToDisplay() &&
3101 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3102 }
3103
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003104 static final class TaskForResizePointSearchResult {
3105 boolean searchDone;
3106 Task taskForResize;
3107
3108 void reset() {
3109 searchDone = false;
3110 taskForResize = null;
3111 }
3112 }
Robert Carr3b716242016-08-16 16:02:21 -07003113
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003114 private static final class ApplySurfaceChangesTransactionState {
3115 boolean displayHasContent;
3116 boolean obscured;
3117 boolean syswin;
3118 boolean focusDisplayed;
3119 float preferredRefreshRate;
3120 int preferredModeId;
3121
3122 void reset() {
3123 displayHasContent = false;
3124 obscured = false;
3125 syswin = false;
3126 focusDisplayed = false;
3127 preferredRefreshRate = 0;
3128 preferredModeId = 0;
3129 }
3130 }
3131
3132 private static final class ScreenshotApplicationState {
3133 WindowState appWin;
3134 int maxLayer;
3135 int minLayer;
3136 boolean screenshotReady;
3137
3138 void reset(boolean screenshotReady) {
3139 appWin = null;
3140 maxLayer = 0;
3141 minLayer = 0;
3142 this.screenshotReady = screenshotReady;
3143 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3144 }
3145 }
3146
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003147 /**
3148 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3149 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3150 * homogeneous children type which is currently required by sub-classes of
3151 * {@link WindowContainer} class.
3152 */
3153 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3154
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003155 DisplayChildWindowContainer(WindowManagerService service) {
3156 super(service);
3157 }
3158
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003159 @Override
3160 boolean fillsParent() {
3161 return true;
3162 }
3163
3164 @Override
3165 boolean isVisible() {
3166 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003167 }
3168 }
3169
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003170 /**
3171 * Window container class that contains all containers on this display relating to Apps.
3172 * I.e Activities.
3173 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003174 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003175 /**
3176 * A control placed at the appropriate level for transitions to occur.
3177 */
chaviw23ee71c2017-12-18 11:29:41 -08003178 SurfaceControl mAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003179
Wale Ogunwale61911492017-10-11 08:50:50 -07003180 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3181 // through the list to find them.
3182 private TaskStack mHomeStack = null;
3183 private TaskStack mPinnedStack = null;
3184 private TaskStack mSplitScreenPrimaryStack = null;
3185
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003186 TaskStackContainers(WindowManagerService service) {
3187 super(service);
3188 }
3189
Wale Ogunwale61911492017-10-11 08:50:50 -07003190 /**
3191 * Returns the topmost stack on the display that is compatible with the input windowing mode
3192 * and activity type. Null is no compatible stack on the display.
3193 */
3194 TaskStack getStack(int windowingMode, int activityType) {
3195 if (activityType == ACTIVITY_TYPE_HOME) {
3196 return mHomeStack;
3197 }
3198 if (windowingMode == WINDOWING_MODE_PINNED) {
3199 return mPinnedStack;
3200 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3201 return mSplitScreenPrimaryStack;
3202 }
3203 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3204 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003205 if (activityType == ACTIVITY_TYPE_UNDEFINED
3206 && windowingMode == stack.getWindowingMode()) {
3207 // Passing in undefined type means we want to match the topmost stack with the
3208 // windowing mode.
3209 return stack;
3210 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003211 if (stack.isCompatible(windowingMode, activityType)) {
3212 return stack;
3213 }
3214 }
3215 return null;
3216 }
3217
3218 @VisibleForTesting
3219 TaskStack getTopStack() {
3220 return mTaskStackContainers.getChildCount() > 0
3221 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3222 }
3223
3224 TaskStack getHomeStack() {
3225 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3226 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3227 }
3228 return mHomeStack;
3229 }
3230
3231 TaskStack getPinnedStack() {
3232 return mPinnedStack;
3233 }
3234
Matthew Ng64e77cf2017-10-31 14:01:31 -07003235 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003236 return mSplitScreenPrimaryStack;
3237 }
3238
Andrii Kulian839def92016-11-02 10:58:58 -07003239 /**
3240 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003241 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003242 */
3243 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003244 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003245 addChild(stack, onTop);
3246 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003247 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003248
Wale Ogunwale61911492017-10-11 08:50:50 -07003249 void onStackWindowingModeChanged(TaskStack stack) {
3250 removeStackReferenceIfNeeded(stack);
3251 addStackReferenceIfNeeded(stack);
3252 if (stack == mPinnedStack && getTopStack() != stack) {
3253 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3254 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3255 }
3256 }
3257
3258 private void addStackReferenceIfNeeded(TaskStack stack) {
3259 if (stack.isActivityTypeHome()) {
3260 if (mHomeStack != null) {
3261 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
3262 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
3263 }
3264 mHomeStack = stack;
3265 }
3266 final int windowingMode = stack.getWindowingMode();
3267 if (windowingMode == WINDOWING_MODE_PINNED) {
3268 if (mPinnedStack != null) {
3269 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3270 + mPinnedStack + " already exist on display=" + this
3271 + " stack=" + stack);
3272 }
3273 mPinnedStack = stack;
3274 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3275 if (mSplitScreenPrimaryStack != null) {
3276 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3277 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3278 + " already exist on display=" + this + " stack=" + stack);
3279 }
3280 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003281 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003282 }
3283 }
3284
3285 private void removeStackReferenceIfNeeded(TaskStack stack) {
3286 if (stack == mHomeStack) {
3287 mHomeStack = null;
3288 } else if (stack == mPinnedStack) {
3289 mPinnedStack = null;
3290 } else if (stack == mSplitScreenPrimaryStack) {
3291 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003292 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3293 mService.setDockedStackCreateStateLocked(
3294 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3295 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003296 }
Andrii Kulian839def92016-11-02 10:58:58 -07003297 }
3298
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003299 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003300 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3301 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003302 addChild(stack, addIndex);
3303 setLayoutNeeded();
3304 }
3305
Wale Ogunwale61911492017-10-11 08:50:50 -07003306 @Override
3307 protected void removeChild(TaskStack stack) {
3308 super.removeChild(stack);
3309 removeStackReferenceIfNeeded(stack);
3310 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003311
3312 @Override
3313 boolean isOnTop() {
3314 // Considered always on top
3315 return true;
3316 }
3317
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003318 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003319 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003320 if (child.getWindowConfiguration().isAlwaysOnTop()
3321 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003322 // This stack is always-on-top, override the default behavior.
3323 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3324
3325 // Moving to its current position, as we must call super but we don't want to
3326 // perform any meaningful action.
3327 final int currentPosition = mChildren.indexOf(child);
3328 super.positionChildAt(currentPosition, child, false /* includingParents */);
3329 return;
3330 }
3331
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003332 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3333 super.positionChildAt(targetPosition, child, includingParents);
3334
3335 setLayoutNeeded();
3336 }
3337
3338 /**
3339 * When stack is added or repositioned, find a proper position for it.
3340 * This will make sure that pinned stack always stays on top.
3341 * @param requestedPosition Position requested by caller.
3342 * @param stack Stack to be added or positioned.
3343 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3344 * @return The proper position for the stack.
3345 */
3346 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3347 final int topChildPosition = mChildren.size() - 1;
3348 boolean toTop = requestedPosition == POSITION_TOP;
3349 toTop |= adding ? requestedPosition >= topChildPosition + 1
3350 : requestedPosition >= topChildPosition;
3351 int targetPosition = requestedPosition;
3352
Wale Ogunwale61911492017-10-11 08:50:50 -07003353 if (toTop && stack.getWindowingMode() != WINDOWING_MODE_PINNED && hasPinnedStack()) {
Bryce Lee48f4b572017-04-10 10:54:15 -07003354 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003355 TaskStack topStack = mChildren.get(topChildPosition);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003356 if (topStack.getWindowingMode() != WINDOWING_MODE_PINNED) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003357 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3358 }
3359
3360 // So, stack is moved just below the pinned stack.
3361 // When we're adding a new stack the target is the current pinned stack position.
3362 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003363 // stack, because WindowContainer#positionAt() first removes element and then adds
3364 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003365 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3366 }
3367
3368 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003369 }
3370
3371 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003372 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3373 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003374 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003375 if (super.forAllWindows(callback, traverseTopToBottom)) {
3376 return true;
3377 }
3378 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3379 return true;
3380 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003381 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003382 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3383 return true;
3384 }
3385 if (super.forAllWindows(callback, traverseTopToBottom)) {
3386 return true;
3387 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003388 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003389 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003390 }
3391
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003392 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003393 boolean traverseTopToBottom) {
3394 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3395 // app tokens.
3396 // TODO: Investigate if we need to continue to do this or if we can just process them
3397 // in-order.
3398 if (traverseTopToBottom) {
3399 for (int i = mChildren.size() - 1; i >= 0; --i) {
3400 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3401 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003402 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3403 traverseTopToBottom)) {
3404 return true;
3405 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003406 }
3407 }
3408 } else {
3409 final int count = mChildren.size();
3410 for (int i = 0; i < count; ++i) {
3411 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3412 final int appTokensCount = appTokens.size();
3413 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003414 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3415 traverseTopToBottom)) {
3416 return true;
3417 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003418 }
3419 }
3420 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003421 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003422 }
3423
Wale Ogunwale1666e312016-12-16 11:27:18 -08003424 void setExitingTokensHasVisible(boolean hasVisible) {
3425 for (int i = mChildren.size() - 1; i >= 0; --i) {
3426 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3427 for (int j = appTokens.size() - 1; j >= 0; --j) {
3428 appTokens.get(j).hasVisible = hasVisible;
3429 }
3430 }
3431 }
3432
3433 void removeExistingAppTokensIfPossible() {
3434 for (int i = mChildren.size() - 1; i >= 0; --i) {
3435 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3436 for (int j = appTokens.size() - 1; j >= 0; --j) {
3437 final AppWindowToken token = appTokens.get(j);
3438 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3439 && (!token.mIsExiting || token.isEmpty())) {
3440 // Make sure there is no animation running on this token, so any windows
3441 // associated with it will be removed as soon as their animations are
3442 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003443 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003444 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3445 "performLayout: App token exiting now removed" + token);
3446 token.removeIfPossible();
3447 }
3448 }
3449 }
3450 }
3451
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003452 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003453 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003454 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3455 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003456 // Apps and their containers are not allowed to specify an orientation while the
3457 // docked or freeform stack is visible...except for the home stack/task if the
3458 // docked stack is minimized and it actually set something.
3459 if (mHomeStack != null && mHomeStack.isVisible()
3460 && mDividerControllerLocked.isMinimizedDock()) {
3461 final int orientation = mHomeStack.getOrientation();
3462 if (orientation != SCREEN_ORIENTATION_UNSET) {
3463 return orientation;
3464 }
3465 }
3466 return SCREEN_ORIENTATION_UNSPECIFIED;
3467 }
3468
3469 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003470 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3471 PackageManager.FEATURE_AUTOMOTIVE);
3472 if (isCar) {
3473 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3474 // allow anything but the default orientation.
3475 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3476 "Forcing UNSPECIFIED orientation in car. Ignoring " + orientation);
3477 return SCREEN_ORIENTATION_UNSPECIFIED;
3478 }
3479
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003480 if (orientation != SCREEN_ORIENTATION_UNSET
3481 && orientation != SCREEN_ORIENTATION_BEHIND) {
3482 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3483 "App is requesting an orientation, return " + orientation);
3484 return orientation;
3485 }
3486
3487 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulian8ee72852017-03-10 10:36:45 -08003488 "No app is requesting an orientation, return " + mLastOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003489 // The next app has not been requested to be visible, so we keep the current orientation
3490 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003491 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003492 }
Robert Carrb1579c82017-09-05 14:54:47 -07003493
3494 @Override
3495 void assignChildLayers(SurfaceControl.Transaction t) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003496 int layer = 0;
Robert Carrb1579c82017-09-05 14:54:47 -07003497
3498 // We allow stacks to change visual order from the AM specified order due to
3499 // Z-boosting during animations. However we must take care to ensure TaskStacks
3500 // which are marked as alwaysOnTop remain that way.
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003501 for (int i = 0; i < mChildren.size(); i++) {
3502 final TaskStack s = mChildren.get(i);
3503 s.assignChildLayers();
3504 if (!s.needsZBoost() && !s.isAlwaysOnTop()) {
3505 s.assignLayer(t, layer++);
Robert Carrb1579c82017-09-05 14:54:47 -07003506 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003507 }
3508 for (int i = 0; i < mChildren.size(); i++) {
3509 final TaskStack s = mChildren.get(i);
3510 if (s.needsZBoost() && !s.isAlwaysOnTop()) {
3511 s.assignLayer(t, layer++);
Robert Carrb1579c82017-09-05 14:54:47 -07003512 }
3513 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003514 for (int i = 0; i < mChildren.size(); i++) {
3515 final TaskStack s = mChildren.get(i);
3516 if (s.isAlwaysOnTop()) {
3517 s.assignLayer(t, layer++);
3518 }
3519 }
3520
3521 // The appropriate place for App-Transitions to occur is right
3522 // above all other animations but still below things in the Picture-and-Picture
3523 // windowing mode.
chaviw23ee71c2017-12-18 11:29:41 -08003524 if (mAppAnimationLayer != null) {
3525 t.setLayer(mAppAnimationLayer, layer++);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003526 }
Robert Carrb1579c82017-09-05 14:54:47 -07003527 }
3528
3529 @Override
chaviw23ee71c2017-12-18 11:29:41 -08003530 SurfaceControl getAppAnimationLayer() {
3531 return mAppAnimationLayer;
3532 }
3533
3534 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07003535 void onParentSet() {
3536 super.onParentSet();
3537 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08003538 mAppAnimationLayer = makeChildSurface(null)
3539 .setName("animationLayer")
3540 .build();
3541 getPendingTransaction().show(mAppAnimationLayer);
3542 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07003543 } else {
chaviw23ee71c2017-12-18 11:29:41 -08003544 mAppAnimationLayer.destroy();
3545 mAppAnimationLayer = null;
Robert Carrb1579c82017-09-05 14:54:47 -07003546 }
3547 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003548 }
3549
Robert Carree4d4b92017-11-22 12:21:46 -08003550 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003551 AboveAppWindowContainers(String name, WindowManagerService service) {
3552 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08003553 }
3554
3555 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
3556 boolean needAssignIme = imeContainer != null
3557 && imeContainer.getSurfaceControl() != null;
3558 for (int j = 0; j < mChildren.size(); ++j) {
3559 final WindowToken wt = mChildren.get(j);
3560 wt.assignLayer(t, j);
3561 wt.assignChildLayers(t);
3562
3563 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
3564 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08003565
3566 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
3567 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003568 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08003569 needAssignIme = false;
3570 }
3571 }
3572 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003573 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08003574 }
3575 }
3576 }
3577
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003578 /**
3579 * Window container class that contains all containers on this display that are not related to
3580 * Apps. E.g. status bar.
3581 */
Robert Carree4d4b92017-11-22 12:21:46 -08003582 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07003583 /**
3584 * Compares two child window tokens returns -1 if the first is lesser than the second in
3585 * terms of z-order and 1 otherwise.
3586 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003587 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003588 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003589 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3590 token1.mOwnerCanManageAppTokens)
3591 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3592 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003593
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003594 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3595 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3596 return false;
3597 }
3598 final int req = w.mAttrs.screenOrientation;
3599 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3600 || req == SCREEN_ORIENTATION_UNSET) {
3601 return false;
3602 }
3603 return true;
3604 };
3605
Wale Ogunwale3a931692016-11-02 16:49:48 -07003606 private final String mName;
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003607 NonAppWindowContainers(String name, WindowManagerService service) {
3608 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003609 mName = name;
3610 }
3611
3612 void addChild(WindowToken token) {
3613 addChild(token, mWindowComparator);
3614 }
3615
3616 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003617 int getOrientation() {
3618 final WindowManagerPolicy policy = mService.mPolicy;
3619 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003620 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003621
3622 if (win != null) {
3623 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003624 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003625 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003626 if (mService.mKeyguardGoingAway) {
3627 // Keyguard can't affect the orientation if it is going away...
3628 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3629 return SCREEN_ORIENTATION_UNSET;
3630 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003631 }
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003632 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003633 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003634 }
3635
Andrii Kulian8ee72852017-03-10 10:36:45 -08003636 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003637
Jorim Jaggi75520d52017-08-24 17:23:19 +02003638 if (policy.isKeyguardShowingAndNotOccluded()
3639 || mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003640 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003641 }
3642
3643 return SCREEN_ORIENTATION_UNSET;
3644 }
3645
3646 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003647 String getName() {
3648 return mName;
3649 }
Robert Carr3b716242016-08-16 16:02:21 -07003650 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003651
Robert Carrb1579c82017-09-05 14:54:47 -07003652 SurfaceControl.Builder makeSurface(SurfaceSession s) {
3653 return mService.makeSurfaceBuilder(s)
3654 .setParent(mWindowingLayer);
3655 }
3656
3657 @Override
3658 SurfaceSession getSession() {
3659 return mSession;
3660 }
3661
3662 @Override
3663 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003664 SurfaceSession s = child != null ? child.getSession() : getSession();
3665 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07003666 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07003667
3668 if (child == null) {
3669 return b;
3670 }
3671
Robert Carree4d4b92017-11-22 12:21:46 -08003672 return b.setName(child.getName())
3673 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07003674 }
3675
3676 /**
3677 * The makeSurface variants are for use by the window-container
3678 * hierarchy. makeOverlay here is a function for various non windowing
3679 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
3680 * and other potpourii.
3681 */
3682 SurfaceControl.Builder makeOverlay() {
3683 return mService.makeSurfaceBuilder(mSession)
3684 .setParent(mOverlayLayer);
3685 }
3686
3687 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003688 applyMagnificationSpec(getPendingTransaction(), spec);
3689 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07003690 }
3691
3692 @Override
3693 void onParentSet() {
3694 // Since we are the top of the SurfaceControl hierarchy here
3695 // we create the root surfaces explicitly rather than chaining
3696 // up as the default implementation in onParentSet does. So we
3697 // explicitly do NOT call super here.
3698 }
3699
3700 @Override
3701 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07003702
3703 // These are layers as children of "mWindowingLayer"
3704 mBelowAppWindowsContainers.assignLayer(t, 0);
3705 mTaskStackContainers.assignLayer(t, 1);
3706 mAboveAppWindowsContainers.assignLayer(t, 2);
3707
3708 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08003709 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08003710
Robert Carree4d4b92017-11-22 12:21:46 -08003711 // In the case where we have an IME target that is not in split-screen
3712 // mode IME assignment is easy. We just need the IME to go directly above
3713 // the target. This way children of the target will naturally go above the IME
3714 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08003715 //
Robert Carree4d4b92017-11-22 12:21:46 -08003716 // In the case of split-screen windowing mode, we need to elevate the IME above the
3717 // docked divider while keeping the app itself below the docked divider, so instead
3718 // we use relative layering of the IME targets child windows, and place the
3719 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08003720 //
Robert Carree4d4b92017-11-22 12:21:46 -08003721 // In the case where we have no IME target we assign it where it's base layer would
3722 // place it in the AboveAppWindowContainers.
3723 if (imeTarget != null && !imeTarget.inSplitScreenWindowingMode()
3724 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003725 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07003726 // TODO: We need to use an extra level on the app surface to ensure
3727 // this is always above SurfaceView but always below attached window.
3728 1);
Robert Carree4d4b92017-11-22 12:21:46 -08003729 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07003730 }
3731
3732 // Above we have assigned layers to our children, now we ask them to assign
3733 // layers to their children.
3734 mBelowAppWindowsContainers.assignChildLayers(t);
3735 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08003736 mAboveAppWindowsContainers.assignChildLayers(t,
3737 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07003738 mImeWindowsContainers.assignChildLayers(t);
3739 }
3740
3741 /**
3742 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
3743 * that the IME target is one of the docked applications. We'd like the docked divider to be
3744 * above both of the applications, and we'd like the IME to be above the docked divider.
3745 * However we need child windows of the applications to be above the IME (Text drag handles).
3746 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
3747 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
3748 * with {@link #WindowState#assignLayer}
3749 */
3750 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
3751 t.setRelativeLayer(child.getSurfaceControl(), mImeWindowsContainers.getSurfaceControl(), 1);
3752 }
3753
3754 @Override
Jorim Jaggia5e10572017-11-15 14:36:26 +01003755 public void destroyAfterPendingTransaction(SurfaceControl surface) {
Robert Carrb1579c82017-09-05 14:54:47 -07003756 mPendingDestroyingSurfaces.add(surface);
3757 }
3758
3759 /**
3760 * Destroys any surfaces that have been put into the pending list with
Jorim Jaggia5e10572017-11-15 14:36:26 +01003761 * {@link #destroyAfterPendingTransaction}.
Robert Carrb1579c82017-09-05 14:54:47 -07003762 */
3763 void onPendingTransactionApplied() {
3764 for (int i = mPendingDestroyingSurfaces.size() - 1; i >= 0; i--) {
3765 mPendingDestroyingSurfaces.get(i).destroy();
3766 }
3767 mPendingDestroyingSurfaces.clear();
3768 }
Jorim Jaggi9af095b2017-12-12 17:18:57 +01003769
3770 @Override
3771 void prepareSurfaces() {
3772 final ScreenRotationAnimation screenRotationAnimation =
3773 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
3774 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
3775 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
3776 mPendingTransaction.setMatrix(mWindowingLayer,
3777 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
3778 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
3779 mPendingTransaction.setPosition(mWindowingLayer,
3780 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
3781 mPendingTransaction.setAlpha(mWindowingLayer,
3782 screenRotationAnimation.getEnterTransformation().getAlpha());
3783 }
3784 super.prepareSurfaces();
3785 }
Craig Mautner59c00972012-07-30 12:10:24 -07003786}