blob: 1df7c66c8cca1a26e25397a91a76565eb280fb3a [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 Ogunwale3797c222015-10-27 14:21:58 -070019import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
Wale Ogunwale51362492016-09-08 17:49:17 -070020import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
Wale Ogunwale3797c222015-10-27 14:21:58 -070021import static android.app.ActivityManager.StackId.HOME_STACK_ID;
22import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070023import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070024import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
25import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070026import static android.view.Display.DEFAULT_DISPLAY;
27import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070028import static android.view.Surface.ROTATION_0;
29import static android.view.Surface.ROTATION_180;
30import static android.view.Surface.ROTATION_270;
31import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070032import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070033import static android.view.WindowManager.DOCKED_BOTTOM;
34import static android.view.WindowManager.DOCKED_INVALID;
35import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080036import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
37import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
38import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070039import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070040import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070041import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
42import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070043import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070044import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070045import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070046import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Wale Ogunwale494009b82016-10-21 09:01:38 -070047import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070048import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070049import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
50import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070051import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070052import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
53import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070054import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070055import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070056import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070057import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
58import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070059import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwale494009b82016-10-21 09:01:38 -070060import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070061import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070062import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
63import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070064import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwalec69694a2016-10-18 13:51:15 -070065import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070066import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070067import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070069import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070070import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070074import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070075import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070076import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080077import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070078import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
79import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070080import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070081import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
82import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER;
83import static com.android.server.wm.WindowManagerService.TYPE_LAYER_OFFSET;
84import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Wale Ogunwale494009b82016-10-21 09:01:38 -070085import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070086import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070087import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070088import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -070089import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070090import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070091import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -070092
Andrii Kulian3a507b52016-09-19 18:14:12 -070093import android.annotation.NonNull;
Wale Ogunwale3797c222015-10-27 14:21:58 -070094import android.app.ActivityManager.StackId;
Andrii Kulian441e4492016-09-29 15:25:00 -070095import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070096import android.graphics.Bitmap;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070097import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -080098import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070099import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700100import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100101import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700102import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700103import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700104import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700105import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700106import android.os.RemoteException;
107import android.os.SystemClock;
Chong Zhang8e89b312015-09-09 15:09:30 -0700108import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700109import android.util.Slog;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700110import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700111import android.view.DisplayInfo;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700112import android.view.Surface;
113import android.view.SurfaceControl;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700114import android.view.WindowManagerPolicy;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700115
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800116import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700117import com.android.internal.view.IInputMethodClient;
Craig Mautner59c00972012-07-30 12:10:24 -0700118
Robert Carr3b716242016-08-16 16:02:21 -0700119import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700120import java.io.PrintWriter;
121import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700122import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700123import java.util.HashMap;
124import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700125import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700126import java.util.List;
Craig Mautner59c00972012-07-30 12:10:24 -0700127
Craig Mautner59c00972012-07-30 12:10:24 -0700128/**
129 * Utility class for keeping track of the WindowStates and other pertinent contents of a
130 * particular Display.
131 *
132 * IMPORTANT: No method from this class should ever be used without holding
133 * WindowManagerService.mWindowMap.
134 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700135class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700136 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700137
138 /** Unique identifier of this stack. */
139 private final int mDisplayId;
140
Wale Ogunwale3a931692016-11-02 16:49:48 -0700141 /** The containers below are the only child containers the display can have. */
142 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700143 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700144 // Contains all non-app window containers that should be displayed above the app containers
145 // (e.g. Status bar)
146 private final NonAppWindowContainers mAboveAppWindowsContainers =
147 new NonAppWindowContainers("mAboveAppWindowsContainers");
148 // Contains all non-app window containers that should be displayed below the app containers
149 // (e.g. Wallpaper).
150 private final NonAppWindowContainers mBelowAppWindowsContainers =
151 new NonAppWindowContainers("mBelowAppWindowsContainers");
152 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
153 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
154 // window containers together and move them in-sync if/when needed.
155 private final NonAppWindowContainers mImeWindowsContainers =
156 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700157
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000158 private WindowState mTmpWindow;
Craig Mautner59c00972012-07-30 12:10:24 -0700159
Wale Ogunwale02319a62016-09-26 15:21:22 -0700160 // Mapping from a token IBinder to a WindowToken object on this display.
161 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
162
Craig Mautner59c00972012-07-30 12:10:24 -0700163 int mInitialDisplayWidth = 0;
164 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700165 int mInitialDisplayDensity = 0;
Craig Mautner59c00972012-07-30 12:10:24 -0700166 int mBaseDisplayWidth = 0;
167 int mBaseDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700168 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700169 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700170 private final DisplayInfo mDisplayInfo = new DisplayInfo();
171 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700172 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Craig Mautner59c00972012-07-30 12:10:24 -0700173
Craig Mautner6601b7b2013-04-29 10:29:11 -0700174 Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700175 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700176
Craig Mautner39834192012-09-02 07:47:24 -0700177 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700178 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700179 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700180 // TODO(multi-display): remove some of the usages.
Craig Mautner69b08182012-09-05 13:07:13 -0700181 final boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700182
Craig Mautnerdc548482014-02-05 13:35:24 -0800183 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700184 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800185
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800186 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
187 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700188 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700189
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700190 /** Detect user tapping outside of current focused task bounds .*/
191 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700192
Craig Mautner6601b7b2013-04-29 10:29:11 -0700193 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700194 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700195
Craig Mautner6601b7b2013-04-29 10:29:11 -0700196 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800197 private final Rect mTmpRect = new Rect();
198 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700199 private final RectF mTmpRectF = new RectF();
200 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800201 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700202
Craig Mautner9d808b12013-08-06 18:00:25 -0700203 final WindowManagerService mService;
204
Craig Mautner95da1082014-02-24 17:54:35 -0800205 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700206 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800207
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700208 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700209 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700210
Chong Zhang112eb8c2015-11-02 11:17:00 -0800211 final DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700212
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800213 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
214
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000215 private boolean mHaveBootMsg = false;
216 private boolean mHaveApp = false;
217 private boolean mHaveWallpaper = false;
218 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700219
220 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
221
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700222 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
223 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000224 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
225 new ApplySurfaceChangesTransactionState();
226 private final ScreenshotApplicationState mScreenshotApplicationState =
227 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700228
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700229 // True if this display is in the process of being removed. Used to determine if the removal of
230 // the display's direct children should be allowed.
231 private boolean mRemovingDisplay = false;
232
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700233 private final WindowLayersController mLayersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700234 final WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700235 int mInputMethodAnimLayerAdjustment;
236
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800237 /**
Craig Mautner2d5618c2012-10-18 13:55:47 -0700238 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800239 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700240 * @param layersController window layer controller used to assign layer to the windows on this
241 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700242 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
243 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700244 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700245 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700246 WindowLayersController layersController, WallpaperController wallpaperController) {
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700247 mDisplay = display;
248 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700249 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700250 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700251 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700252 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700253 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700254 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700255 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800256 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700257 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800258 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700259
260 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700261 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700262 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700263 super.addChild(mAboveAppWindowsContainers, null);
264 super.addChild(mImeWindowsContainers, null);
Craig Mautner59c00972012-07-30 12:10:24 -0700265 }
266
267 int getDisplayId() {
268 return mDisplayId;
269 }
270
Wale Ogunwale02319a62016-09-26 15:21:22 -0700271 WindowToken getWindowToken(IBinder binder) {
272 return mTokenMap.get(binder);
273 }
274
275 AppWindowToken getAppWindowToken(IBinder binder) {
276 final WindowToken token = getWindowToken(binder);
277 if (token == null) {
278 return null;
279 }
280 return token.asAppWindowToken();
281 }
282
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800283 void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700284 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
285 if (dc != null) {
286 // We currently don't support adding a window token to the display if the display
287 // already has the binder mapped to another token. If there is a use case for supporting
288 // this moving forward we will either need to merge the WindowTokens some how or have
289 // the binder map to a list of window tokens.
290 throw new IllegalArgumentException("Can't map token=" + token + " to display=" + this
291 + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
292 }
293 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700294
295 if (token.asAppWindowToken() == null) {
296 // Add non-app token to container hierarchy on the display. App tokens are added through
297 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700298 switch (token.windowType) {
299 case TYPE_WALLPAPER:
300 mBelowAppWindowsContainers.addChild(token);
301 break;
302 case TYPE_INPUT_METHOD:
303 case TYPE_INPUT_METHOD_DIALOG:
304 mImeWindowsContainers.addChild(token);
305 break;
306 default:
307 mAboveAppWindowsContainers.addChild(token);
308 break;
309 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700310 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700311 }
312
313 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700314 final WindowToken token = mTokenMap.remove(binder);
315 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800316 token.setExiting();
317 }
318 return token;
319 }
320
321 /** Changes the display the input window token is housed on to this one. */
322 void reParentWindowToken(WindowToken token) {
323 final DisplayContent prevDc = token.getDisplayContent();
324 if (prevDc == this) {
325 return;
326 }
327 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null) {
Wale Ogunwale3a931692016-11-02 16:49:48 -0700328 switch (token.windowType) {
329 case TYPE_WALLPAPER:
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800330 prevDc.mBelowAppWindowsContainers.removeChild(token);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700331 break;
332 case TYPE_INPUT_METHOD:
333 case TYPE_INPUT_METHOD_DIALOG:
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800334 prevDc.mImeWindowsContainers.removeChild(token);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700335 break;
336 default:
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800337 prevDc.mAboveAppWindowsContainers.removeChild(token);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700338 break;
339 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700340 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800341
342 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700343 }
344
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700345 void removeAppToken(IBinder binder) {
346 final WindowToken token = removeWindowToken(binder);
347 if (token == null) {
348 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
349 return;
350 }
351
352 final AppWindowToken appToken = token.asAppWindowToken();
353
354 if (appToken == null) {
355 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
356 return;
357 }
358
359 appToken.onRemovedFromDisplay();
360 }
361
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700362 Display getDisplay() {
363 return mDisplay;
364 }
365
Craig Mautner59c00972012-07-30 12:10:24 -0700366 DisplayInfo getDisplayInfo() {
367 return mDisplayInfo;
368 }
369
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700370 DisplayMetrics getDisplayMetrics() {
371 return mDisplayMetrics;
372 }
373
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100374 DockedStackDividerController getDockedDividerController() {
375 return mDividerControllerLocked;
376 }
377
Winson Chung655332c2016-10-31 13:14:28 -0700378 PinnedStackController getPinnedStackController() {
379 return mPinnedStackControllerLocked;
380 }
381
Jeff Browna506a6e2013-06-04 00:02:38 -0700382 /**
383 * Returns true if the specified UID has access to this display.
384 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700385 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700386 return mDisplay.hasAccess(uid);
387 }
388
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700389 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700390 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -0700391 }
392
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700393 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700394 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800395 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -0800396 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700397 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700398 }
399
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700400 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700401 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
402 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700403 if (stack.mStackId == stackId) {
404 return stack;
405 }
406 }
407 return null;
408 }
409
Andrii Kulian441e4492016-09-29 15:25:00 -0700410 @Override
411 void onConfigurationChanged(Configuration newParentConfig) {
412 super.onConfigurationChanged(newParentConfig);
413
Andrii Kulian3a507b52016-09-19 18:14:12 -0700414 // The display size information is heavily dependent on the resources in the current
415 // configuration, so we need to reconfigure it every time the configuration changes.
416 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
417 mService.reconfigureDisplayLocked(this);
418
419 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -0700420 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -0700421 }
Andrii Kulian3a507b52016-09-19 18:14:12 -0700422
Andrii Kulian441e4492016-09-29 15:25:00 -0700423 /**
424 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
425 * bounds were updated.
426 */
427 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700428 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
429 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -0700430 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -0700431 changedStackList.add(stack.mStackId);
432 }
433 }
434 }
435
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700436 @Override
437 boolean fillsParent() {
438 return true;
439 }
440
441 @Override
442 boolean isVisible() {
443 return true;
444 }
445
446 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700447 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -0700448 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000449 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700450 }
451
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700452 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800453 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
454 // Special handling so we can process IME windows with #forAllImeWindows above their IME
455 // target, or here in order if there isn't an IME target.
456 if (traverseTopToBottom) {
457 for (int i = mChildren.size() - 1; i >= 0; --i) {
458 final DisplayChildWindowContainer child = mChildren.get(i);
459 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
460 // In this case the Ime windows will be processed above their target so we skip
461 // here.
462 continue;
463 }
464 if (child.forAllWindows(callback, traverseTopToBottom)) {
465 return true;
466 }
467 }
468 } else {
469 final int count = mChildren.size();
470 for (int i = 0; i < count; i++) {
471 final DisplayChildWindowContainer child = mChildren.get(i);
472 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
473 // In this case the Ime windows will be processed above their target so we skip
474 // here.
475 continue;
476 }
477 if (child.forAllWindows(callback, traverseTopToBottom)) {
478 return true;
479 }
480 }
481 }
482 return false;
483 }
484
485 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
486 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
487 }
488
489 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -0700490 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700491 final WindowManagerPolicy policy = mService.mPolicy;
492
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700493 if (mService.mDisplayFrozen) {
494 if (mService.mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
495 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
496 "Display is frozen, return " + mService.mLastWindowForcedOrientation);
497 // If the display is frozen, some activities may be in the middle of restarting, and
498 // thus have removed their old window. If the window has the flag to hide the lock
499 // screen, then the lock screen can re-appear and inflict its own orientation on us.
500 // Keep the orientation stable until this all settles down.
501 return mService.mLastWindowForcedOrientation;
502 } else if (policy.isKeyguardLocked()) {
503 // Use the last orientation the while the display is frozen with the keyguard
504 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
505 // window. We don't want to check the show when locked window directly though as
506 // things aren't stable while the display is frozen, for example the window could be
507 // momentarily unavailable due to activity relaunch.
508 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
509 + "return " + mService.mLastOrientation);
510 return mService.mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -0700511 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700512 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000513 final int orientation = mAboveAppWindowsContainers.getOrientation();
514 if (orientation != SCREEN_ORIENTATION_UNSET) {
515 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700516 }
Wale Ogunwale51362492016-09-08 17:49:17 -0700517 }
518
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700519 // Top system windows are not requesting an orientation. Start searching from apps.
520 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -0700521 }
522
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700523 void updateDisplayInfo() {
Craig Mautner722285e2012-09-07 13:55:58 -0700524 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700525 mDisplay.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700526 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
527 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800528 }
Craig Mautner722285e2012-09-07 13:55:58 -0700529 }
530
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700531 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700532 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
533 if (displayManagerInternal != null) {
534 // Bootstrap the default logical display from the display manager.
535 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
536 if (newDisplayInfo != null) {
537 mDisplayInfo.copyFrom(newDisplayInfo);
538 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700539 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700540
Filip Gruszczynski608797e2015-11-12 19:08:20 -0800541 mBaseDisplayWidth = mInitialDisplayWidth = mDisplayInfo.logicalWidth;
542 mBaseDisplayHeight = mInitialDisplayHeight = mDisplayInfo.logicalHeight;
543 mBaseDisplayDensity = mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
544 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700545 }
546
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700547 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700548 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800549 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700550 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800551 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
552 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700553 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800554 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700555 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800556 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700557 out.set(left, top, left + width, top + height);
558 }
559
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700560 private void getLogicalDisplayRect(Rect out, int orientation) {
561 getLogicalDisplayRect(out);
562
563 // Rotate the Rect if needed.
564 final int currentRotation = mDisplayInfo.rotation;
565 final int rotationDelta = deltaRotation(currentRotation, orientation);
566 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
567 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
568 mTmpRectF.set(out);
569 mTmpMatrix.mapRect(mTmpRectF);
570 mTmpRectF.round(out);
571 }
572 }
573
Chong Zhangf66db432016-01-13 10:39:51 -0800574 void getContentRect(Rect out) {
575 out.set(mContentRect);
576 }
577
Andrii Kulian839def92016-11-02 10:58:58 -0700578 /**
579 * Adds the stack to this display.
580 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
581 */
582 Rect addStackToDisplay(int stackId, boolean onTop) {
583 boolean attachedToDisplay = false;
584 TaskStack stack = mService.mStackIdToStack.get(stackId);
585 if (stack == null) {
586 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
587 + mDisplayId);
588
589 stack = getStackById(stackId);
590 if (stack != null) {
591 // It's already attached to the display...clear mDeferRemoval and move stack to
592 // appropriate z-order on display as needed.
593 stack.mDeferRemoval = false;
594 moveStack(stack, onTop);
595 attachedToDisplay = true;
596 } else {
597 stack = new TaskStack(mService, stackId);
598 }
599
600 mService.mStackIdToStack.put(stackId, stack);
601 if (stackId == DOCKED_STACK_ID) {
602 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
603 }
604 } else {
605 final DisplayContent currentDC = stack.getDisplayContent();
606 if (currentDC != null) {
607 throw new IllegalStateException("Trying to add stackId=" + stackId
608 + "to displayId=" + mDisplayId + ", but it's already attached to displayId="
609 + currentDC.getDisplayId());
610 }
611 }
612
613 if (!attachedToDisplay) {
614 mTaskStackContainers.addStackToDisplay(stack, onTop);
615 }
616
617 if (stack.getRawFullscreen()) {
618 return null;
619 }
620 final Rect bounds = new Rect();
621 stack.getRawBounds(bounds);
622 return bounds;
623 }
624
625 /** Removes the stack from the display and prepares for changing the parent. */
626 private void removeStackFromDisplay(TaskStack stack) {
627 mTaskStackContainers.removeStackFromDisplay(stack);
628 }
629
630 /** Moves the stack to this display and returns the updated bounds. */
631 Rect moveStackToDisplay(TaskStack stack) {
632 final DisplayContent currentDisplayContent = stack.getDisplayContent();
633 if (currentDisplayContent == null) {
634 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
635 + " which is not currently attached to any display");
636 }
637 if (stack.getDisplayContent().getDisplayId() == mDisplayId) {
638 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
639 + " to its current displayId=" + mDisplayId);
640 }
641
642 currentDisplayContent.removeStackFromDisplay(stack);
643 return addStackToDisplay(stack.mStackId, true /* onTop */);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800644 }
645
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800646 void moveStack(TaskStack stack, boolean toTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700647 mTaskStackContainers.moveStack(stack, toTop);
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700648 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700649
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700650 @Override
651 protected void addChild(DisplayChildWindowContainer child,
652 Comparator<DisplayChildWindowContainer> comparator) {
653 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
654 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700655
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700656 @Override
657 protected void addChild(DisplayChildWindowContainer child, int index) {
658 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
659 }
660
661 @Override
662 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700663 // Only allow removal of direct children from this display if the display is in the process
664 // of been removed.
665 if (mRemovingDisplay) {
666 super.removeChild(child);
667 return;
668 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700669 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800670 }
671
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700672 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700673 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
674 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700675 final int taskId = stack.taskIdFromPoint(x, y);
676 if (taskId != -1) {
677 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -0700678 }
679 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800680 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -0700681 }
682
Chong Zhang8e89b312015-09-09 15:09:30 -0700683 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -0800684 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -0700685 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -0700686 */
Wale Ogunwale15ead902016-09-02 14:30:11 -0700687 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700688 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700689 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700690 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
691 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -0700692 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700693 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -0700694 }
Chong Zhang9184ec62015-09-24 12:32:21 -0700695
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700696 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
697 if (mTmpTaskForResizePointSearchResult.searchDone) {
698 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -0700699 }
700 }
Chong Zhang9184ec62015-09-24 12:32:21 -0700701 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -0700702 }
703
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700704 void setTouchExcludeRegion(Task focusedTask) {
Craig Mautner6601b7b2013-04-29 10:29:11 -0700705 mTouchExcludeRegion.set(mBaseDisplayRect);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700706 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700707 mTmpRect2.setEmpty();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700708 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
709 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700710 stack.setTouchExcludeRegion(
711 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
Craig Mautner6601b7b2013-04-29 10:29:11 -0700712 }
Chong Zhangd8ceb852015-11-11 14:53:41 -0800713 // If we removed the focused task above, add it back and only leave its
714 // outside touch area in the exclusion. TapDectector is not interested in
715 // any touch inside the focused task itself.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700716 if (!mTmpRect2.isEmpty()) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800717 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
718 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800719 final WindowState inputMethod = mService.mInputMethodWindow;
720 if (inputMethod != null && inputMethod.isVisibleLw()) {
721 // If the input method is visible and the user is typing, we don't want these touch
722 // events to be intercepted and used to change focus. This would likely cause a
723 // disappearance of the input method.
724 inputMethod.getTouchableRegion(mTmpRegion);
725 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
726 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800727 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
728 WindowState win = mTapExcludedWindows.get(i);
729 win.getTouchableRegion(mTmpRegion);
730 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
731 }
Andrii Kulian03c403d2016-11-11 11:14:12 -0800732 // TODO(multi-display): Support docked stacks on secondary displays.
733 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100734 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -0700735 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100736 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
737 }
Craig Mautner1bef3892015-02-17 15:09:47 -0800738 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700739 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -0800740 }
Craig Mautner6601b7b2013-04-29 10:29:11 -0700741 }
742
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000743 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700744 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000745 super.switchUser();
746 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -0700747 }
748
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700749 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700750 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
751 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -0700752 }
753 }
754
755 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800756 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -0700757 }
758
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700759 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800760 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -0700761 }
762
763 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800764 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -0700765 }
766
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700767 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800768 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -0700769 }
770
Wale Ogunwale10124582016-09-15 20:25:50 -0700771 @Override
772 void removeIfPossible() {
773 if (isAnimating()) {
774 mDeferredRemoval = true;
775 return;
Craig Mautner2eb15342013-08-07 13:13:35 -0700776 }
Wale Ogunwale10124582016-09-15 20:25:50 -0700777 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -0700778 }
779
Wale Ogunwale10124582016-09-15 20:25:50 -0700780 @Override
781 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700782 mRemovingDisplay = true;
783 try {
784 super.removeImmediately();
785 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
786 mDimLayerController.close();
787 if (mDisplayId == DEFAULT_DISPLAY) {
788 mService.unregisterPointerEventListener(mTapDetector);
789 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
790 }
791 } finally {
792 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -0800793 }
Craig Mautner95da1082014-02-24 17:54:35 -0800794 }
795
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700796 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -0700797 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700798 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -0700799 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
800
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700801 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -0700802 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -0800803 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700804 return false;
Craig Mautner95da1082014-02-24 17:54:35 -0800805 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700806 return true;
Craig Mautner95da1082014-02-24 17:54:35 -0800807 }
808
Wale Ogunwale10124582016-09-15 20:25:50 -0700809 boolean animateForIme(float interpolatedValue, float animationTarget,
810 float dividerAnimationTarget) {
811 boolean updated = false;
812
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700813 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
814 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700815 if (stack == null || !stack.isAdjustedForIme()) {
816 continue;
817 }
818
819 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
820 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
821 updated = true;
822 } else {
823 mDividerControllerLocked.mLastAnimationProgress =
824 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
825 mDividerControllerLocked.mLastDividerProgress =
826 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
827 updated |= stack.updateAdjustForIme(
828 mDividerControllerLocked.mLastAnimationProgress,
829 mDividerControllerLocked.mLastDividerProgress,
830 false /* force */);
831 }
832 if (interpolatedValue >= 1f) {
833 stack.endImeAdjustAnimation();
834 }
835 }
836
837 return updated;
838 }
839
840 boolean clearImeAdjustAnimation() {
841 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700842 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
843 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700844 if (stack != null && stack.isAdjustedForIme()) {
845 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
846 changed = true;
847 }
848 }
849 return changed;
850 }
851
852 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700853 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
854 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700855 if (stack.isVisible() && stack.isAdjustedForIme()) {
856 stack.beginImeAdjustAnimation();
857 }
858 }
859 }
860
861 void adjustForImeIfNeeded() {
862 final WindowState imeWin = mService.mInputMethodWindow;
863 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
864 && !mDividerControllerLocked.isImeHideRequested();
865 final boolean dockVisible = mService.isStackVisibleLocked(DOCKED_STACK_ID);
866 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
867 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
868 imeTargetStack.getDockSide() : DOCKED_INVALID;
869 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
870 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
871 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
872 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
873 final boolean imeHeightChanged = imeVisible &&
874 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
875
876 // The divider could be adjusted for IME position, or be thinner than usual,
877 // or both. There are three possible cases:
878 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
879 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
880 // - If IME is not visible, divider is not moved and is normal width.
881
882 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700883 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
884 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700885 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
886 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)) {
887 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
888 } else {
889 stack.resetAdjustedForIme(false);
890 }
891 }
892 mDividerControllerLocked.setAdjustedForIme(
893 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
894 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700895 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
896 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700897 stack.resetAdjustedForIme(!dockVisible);
898 }
899 mDividerControllerLocked.setAdjustedForIme(
900 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
901 }
Winson Chung655332c2016-10-31 13:14:28 -0700902 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -0700903 }
904
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700905 void setInputMethodAnimLayerAdjustment(int adj) {
906 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
907 mInputMethodAnimLayerAdjustment = adj;
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000908 mImeWindowsContainers.forAllWindows(w -> {
909 w.adjustAnimLayer(adj);
910 }, true /* traverseTopToBottom */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700911 }
912
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700913 /**
914 * If a window that has an animation specifying a colored background and the current wallpaper
915 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
916 * suddenly disappear.
917 */
918 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000919 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
920 w -> w.mIsWallpaper && w.isVisibleNow());
921
922 if (visibleWallpaper != null) {
923 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700924 }
925 return winAnimator.mAnimLayer;
926 }
927
Wale Ogunwale10124582016-09-15 20:25:50 -0700928 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700929 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
930 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -0700931 stack.prepareFreezingTaskBounds();
932 }
933 }
934
Wale Ogunwale94744212015-09-21 19:01:47 -0700935 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700936 getLogicalDisplayRect(mTmpRect, newRotation);
937
938 // Compute a transform matrix to undo the coordinate space transformation,
939 // and present the window at the same physical position it previously occupied.
940 final int deltaRotation = deltaRotation(newRotation, oldRotation);
941 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
942
943 mTmpRectF.set(bounds);
944 mTmpMatrix.mapRect(mTmpRectF);
945 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -0700946 }
947
Wale Ogunwale4a02d812015-02-12 23:01:38 -0800948 static int deltaRotation(int oldRotation, int newRotation) {
949 int delta = newRotation - oldRotation;
950 if (delta < 0) delta += 4;
951 return delta;
952 }
953
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700954 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700955 Matrix outMatrix) {
956 // For rotations without Z-ordering we don't need the target rectangle's position.
957 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
958 displayHeight, outMatrix);
959 }
960
961 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
962 float displayWidth, float displayHeight, Matrix outMatrix) {
963 switch (rotation) {
964 case ROTATION_0:
965 outMatrix.reset();
966 break;
967 case ROTATION_270:
968 outMatrix.setRotate(270, 0, 0);
969 outMatrix.postTranslate(0, displayHeight);
970 outMatrix.postTranslate(rectTop, 0);
971 break;
972 case ROTATION_180:
973 outMatrix.reset();
974 break;
975 case ROTATION_90:
976 outMatrix.setRotate(90, 0, 0);
977 outMatrix.postTranslate(displayWidth, 0);
978 outMatrix.postTranslate(-rectTop, rectLeft);
979 break;
980 }
981 }
982
Craig Mautnera91f9e22012-09-14 16:22:08 -0700983 public void dump(String prefix, PrintWriter pw) {
984 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
985 final String subPrefix = " " + prefix;
986 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
987 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
988 pw.print("dpi");
989 if (mInitialDisplayWidth != mBaseDisplayWidth
990 || mInitialDisplayHeight != mBaseDisplayHeight
991 || mInitialDisplayDensity != mBaseDisplayDensity) {
992 pw.print(" base=");
993 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
994 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
995 }
Jeff Brownd46747a2015-04-15 19:02:36 -0700996 if (mDisplayScalingDisabled) {
997 pw.println(" noscale");
998 }
Craig Mautnera91f9e22012-09-14 16:22:08 -0700999 pw.print(" cur=");
1000 pw.print(mDisplayInfo.logicalWidth);
1001 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
1002 pw.print(" app=");
1003 pw.print(mDisplayInfo.appWidth);
1004 pw.print("x"); pw.print(mDisplayInfo.appHeight);
1005 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
1006 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
1007 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
1008 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001009 pw.println(subPrefix + "deferred=" + mDeferredRemoval
1010 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001011
Craig Mautnerdc548482014-02-05 13:35:24 -08001012 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001013 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001014 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1015 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001016 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001017 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001018
Craig Mautnerdc548482014-02-05 13:35:24 -08001019 pw.println();
1020 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001021 pw.println();
1022 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001023 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001024 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001025 pw.print(" Exiting #"); pw.print(i);
1026 pw.print(' '); pw.print(token);
1027 pw.println(':');
1028 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001029 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001030 }
Craig Mautner59c00972012-07-30 12:10:24 -07001031 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001032 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07001033 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001034 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07001035 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001036 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001037
1038 if (mInputMethodAnimLayerAdjustment != 0) {
1039 pw.println(subPrefix
1040 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
1041 }
Craig Mautner59c00972012-07-30 12:10:24 -07001042 }
Craig Mautnere0a38842013-12-16 16:14:02 -08001043
1044 @Override
1045 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001046 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001047 }
1048
1049 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001050 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08001051 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07001052
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001053 /**
1054 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
1055 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07001056 TaskStack getDockedStackLocked() {
Wale Ogunwalee45899a2015-10-01 11:30:34 -07001057 final TaskStack stack = mService.mStackIdToStack.get(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001058 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07001059 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001060
1061 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001062 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02001063 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001064 */
Jorim Jaggife762342016-10-13 14:33:27 +02001065 TaskStack getDockedStackIgnoringVisibility() {
1066 return mService.mStackIdToStack.get(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001067 }
1068
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001069 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001070 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001071 final int x = (int) xf;
1072 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001073 final WindowState touchedWin = getWindow(w -> {
1074 final int flags = w.mAttrs.flags;
1075 if (!w.isVisibleLw()) {
1076 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001077 }
1078 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001079 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001080 }
1081
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001082 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001083 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001084 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001085 }
1086
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001087 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001088
1089 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001090 return mTmpRegion.contains(x, y) || touchFlags == 0;
1091 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001092
1093 return touchedWin;
1094 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07001095
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001096 boolean canAddToastWindowForUid(int uid) {
1097 // We allow one toast window per UID being shown at a time.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001098 final WindowState win = getWindow(w ->
1099 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
1100 && !w.mWindowRemovalAllowed);
1101 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001102 }
1103
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001104 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001105 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
1106 return;
1107 }
1108 final int lostFocusUid = oldFocus.mOwnerUid;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001109 final Handler handler = mService.mH;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001110
1111 forAllWindows(w -> {
1112 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
1113 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
1114 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
1115 w.mAttrs.hideTimeoutMilliseconds);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001116 }
1117 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001118 }, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001119 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001120
1121 WindowState findFocusedWindow() {
1122 final AppWindowToken focusedApp = mService.mFocusedApp;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001123 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001124
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001125 forAllWindows(w -> {
1126 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
1127 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001128
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001129 if (!w.canReceiveKeys()) {
1130 return false;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001131 }
1132
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001133 final AppWindowToken wtoken = w.mAppToken;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001134
1135 // If this window's application has been removed, just skip it.
1136 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
1137 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
1138 + (wtoken.removed ? "removed" : "sendingToBottom"));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001139 return false;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001140 }
1141
1142 if (focusedApp == null) {
1143 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001144 + " using new focus @ " + w);
1145 mTmpWindow = w;
1146 return true;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001147 }
1148
1149 if (!focusedApp.windowsAreFocusable()) {
1150 // Current focused app windows aren't focusable...
1151 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001152 + " focusable using new focus @ " + w);
1153 mTmpWindow = w;
1154 return true;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001155 }
1156
1157 // Descend through all of the app tokens and find the first that either matches
1158 // win.mAppToken (return win) or mFocusedApp (return null).
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001159 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001160 if (focusedApp.compareTo(wtoken) > 0) {
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001161 // App stack below focused app stack. No focus for you!!!
1162 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
1163 "findFocusedWindow: Reached focused app=" + focusedApp);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001164 mTmpWindow = null;
1165 return true;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001166 }
1167 }
1168
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001169 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
1170 mTmpWindow = w;
1171 return true;
1172 }, true /* traverseTopToBottom */);
1173
1174 if (mTmpWindow == null) {
1175 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
1176 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001177 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001178 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001179 }
1180
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001181 /** Updates the layer assignment of windows on this display. */
1182 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08001183 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001184 if (setLayoutNeeded) {
1185 setLayoutNeeded();
1186 }
1187 }
1188
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001189 void layoutAndAssignWindowLayersIfNeeded() {
1190 mService.mWindowsChanged = true;
1191 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001192
1193 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
1194 false /*updateInputWindows*/)) {
1195 assignWindowLayers(false /* setLayoutNeeded */);
1196 }
1197
1198 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
1199 mService.mWindowPlacerLocked.performSurfacePlacement();
1200 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
1201 }
1202
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001203 /** Returns true if a leaked surface was destroyed */
1204 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001205 // Used to indicate that a surface was leaked.
1206 mTmpWindow = null;
1207 forAllWindows(w -> {
1208 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001209 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001210 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001211 }
1212 if (!mService.mSessions.contains(wsa.mSession)) {
1213 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001214 + w + " surface=" + wsa.mSurfaceController
1215 + " token=" + w.mToken
1216 + " pid=" + w.mSession.mPid
1217 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001218 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001219 mService.mForceRemoves.add(w);
1220 mTmpWindow = w;
1221 } else if (w.mAppToken != null && w.mAppToken.clientHidden) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001222 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001223 + w + " surface=" + wsa.mSurfaceController
1224 + " token=" + w.mAppToken
1225 + " saved=" + w.hasSavedSurface());
1226 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001227 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001228 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001229 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001230 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001231
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001232 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001233 }
1234
1235 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001236 * Determine and return the window that should be the IME target.
1237 * @param updateImeTarget If true the system IME target will be updated to match what we found.
1238 * @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 +00001239 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001240 WindowState computeImeTarget(boolean updateImeTarget) {
1241 if (mService.mInputMethodWindow == null) {
1242 // There isn't an IME so there shouldn't be a target...That was easy!
1243 if (updateImeTarget) {
1244 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
1245 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
1246 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
1247 }
1248 return null;
1249 }
1250
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001251 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
1252 // same display. Or even when the current IME/target are not on the same screen as the next
1253 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001254 WindowState target = getWindow(w -> {
1255 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.i(TAG_WM, "Checking window @"
1256 + w + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
1257 return w.canBeImeTarget();
1258 });
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001259
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001260
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001261 // Yet more tricksyness! If this window is a "starting" window, we do actually want
1262 // to be on top of it, but it is not -really- where input will go. So look down below
1263 // for a real window to target...
1264 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
1265 final AppWindowToken token = target.mAppToken;
1266 if (token != null) {
1267 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
1268 if (betterTarget != null) {
1269 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001270 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001271 }
1272 }
1273
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001274 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
1275 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001276
1277 // Now, a special case -- if the last target's window is in the process of exiting, and is
1278 // above the new target, keep on the last target to avoid flicker. Consider for example a
1279 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
1280 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
1281 // scrim.
1282 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001283 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
1284 && (target == null
1285 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001286 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001287 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001288 }
1289
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001290 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
1291 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001292
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001293 if (target == null) {
1294 if (updateImeTarget) {
1295 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
1296 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
1297 + Debug.getCallers(4) : ""));
1298 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
1299 }
1300
1301 return null;
1302 }
1303
1304 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001305 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
1306 if (token != null) {
1307
1308 // Now some fun for dealing with window animations that modify the Z order. We need
1309 // to look at all windows below the current target that are in this app, finding the
1310 // highest visible one in layering.
1311 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001312 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001313 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001314 }
1315
1316 if (highestTarget != null) {
1317 final AppTransition appTransition = mService.mAppTransition;
1318 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
1319 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
1320 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001321 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001322
1323 if (appTransition.isTransitionSet()) {
1324 // If we are currently setting up for an animation, hold everything until we
1325 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001326 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
1327 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001328 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001329 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001330 // If the window we are currently targeting is involved with an animation,
1331 // and it is on top of the next target we will be over, then hold off on
1332 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001333 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
1334 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001335 }
1336 }
1337 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001338
1339 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
1340 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
1341 setInputMethodTarget(target, false, target.mAppToken != null
1342 ? target.mAppToken.mAppAnimator.animLayerAdjustment : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001343 }
1344
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001345 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001346 }
1347
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001348 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
1349 if (target == mService.mInputMethodTarget
1350 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
1351 && mInputMethodAnimLayerAdjustment == layerAdj) {
1352 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001353 }
1354
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001355 mService.mInputMethodTarget = target;
1356 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
1357 setInputMethodAnimLayerAdjustment(layerAdj);
1358 assignWindowLayers(false /* setLayoutNeeded */);
1359 }
1360
1361 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
1362 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
1363 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
1364 }
1365
1366 // Used to indicate we have reached the first window in the range we are interested in.
1367 mTmpWindow = null;
1368
1369 // TODO: Figure-out a more efficient way to do this.
1370 final WindowState candidate = getWindow(w -> {
1371 if (w == top) {
1372 // Reached the first window in the range we are interested in.
1373 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001374 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001375 if (mTmpWindow == null) {
1376 return false;
1377 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001378
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001379 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
1380 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001381 }
1382 // If we reached the bottom of the range of windows we are considering,
1383 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001384 if (w == bottom) {
1385 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001386 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001387 return false;
1388 });
1389
1390 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001391 }
1392
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001393 void setLayoutNeeded() {
1394 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
1395 mLayoutNeeded = true;
1396 }
1397
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001398 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001399 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
1400 mLayoutNeeded = false;
1401 }
1402
1403 boolean isLayoutNeeded() {
1404 return mLayoutNeeded;
1405 }
1406
Wale Ogunwale02319a62016-09-26 15:21:22 -07001407 void dumpTokens(PrintWriter pw, boolean dumpAll) {
1408 if (mTokenMap.isEmpty()) {
1409 return;
1410 }
1411 pw.println(" Display #" + mDisplayId);
1412 final Iterator<WindowToken> it = mTokenMap.values().iterator();
1413 while (it.hasNext()) {
1414 final WindowToken token = it.next();
1415 pw.print(" ");
1416 pw.print(token);
1417 if (dumpAll) {
1418 pw.println(':');
1419 token.dump(pw, " ");
1420 } else {
1421 pw.println();
1422 }
1423 }
1424 }
1425
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001426 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001427 final int[] index = new int[1];
1428 forAllWindows(w -> {
1429 final WindowStateAnimator wAnim = w.mWinAnimator;
1430 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
1431 index[0] = index[0] + 1;
1432 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001433 }
1434
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001435 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001436 forAllWindows(w -> {
1437 w.mWinAnimator.enableSurfaceTrace(fd);
1438 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001439 }
1440
1441 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001442 forAllWindows(w -> {
1443 w.mWinAnimator.disableSurfaceTrace();
1444 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001445 }
1446
Jorim Jaggife762342016-10-13 14:33:27 +02001447 /**
1448 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
1449 */
1450 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
1451 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001452 forAllWindows(w -> {
1453 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)) {
1454 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02001455 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
1456 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001457 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02001458 }
1459
Wale Ogunwale494009b82016-10-21 09:01:38 -07001460 boolean checkWaitingForWindows() {
1461
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001462 mHaveBootMsg = false;
1463 mHaveApp = false;
1464 mHaveWallpaper = false;
1465 mHaveKeyguard = true;
1466
1467 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07001468 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
1469 return true;
1470 }
1471 if (w.isDrawnLw()) {
1472 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001473 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001474 } else if (w.mAttrs.type == TYPE_APPLICATION
1475 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001476 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001477 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001478 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001479 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001480 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07001481 }
1482 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001483 return false;
1484 });
1485
1486 if (visibleWindow != null) {
1487 // We have a visible window.
1488 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001489 }
1490
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001491 // if the wallpaper service is disabled on the device, we're never going to have
1492 // wallpaper, don't bother waiting for it
1493 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
1494 com.android.internal.R.bool.config_enableWallpaperService)
1495 && !mService.mOnlyCore;
1496
Wale Ogunwale494009b82016-10-21 09:01:38 -07001497 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
1498 "******** booted=" + mService.mSystemBooted
1499 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001500 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
1501 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
1502 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07001503
1504 // If we are turning on the screen to show the boot message, don't do it until the boot
1505 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001506 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07001507 return true;
1508 }
1509
1510 // If we are turning on the screen after the boot is completed normally, don't do so until
1511 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001512 if (mService.mSystemBooted
1513 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07001514 return true;
1515 }
1516
1517 return false;
1518 }
1519
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001520 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001521 forAllWindows(w -> {
1522 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001523 if (winAnimator.hasSurface()) {
1524 final boolean wasAnimating = winAnimator.mWasAnimating;
1525 final boolean nowAnimating = winAnimator.stepAnimationLocked(animator.mCurrentTime);
1526 winAnimator.mWasAnimating = nowAnimating;
1527 animator.orAnimating(nowAnimating);
1528
1529 if (DEBUG_WALLPAPER) Slog.v(TAG,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001530 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001531
1532 if (wasAnimating && !winAnimator.mAnimating
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001533 && mWallpaperController.isWallpaperTarget(w)) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001534 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
1535 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
1536 if (DEBUG_LAYOUT_REPEATS) {
1537 mService.mWindowPlacerLocked.debugLayoutRepeats(
1538 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
1539 }
1540 }
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001541 }
1542
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001543 final AppWindowToken atoken = w.mAppToken;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001544 if (winAnimator.mDrawState == READY_TO_SHOW) {
1545 if (atoken == null || atoken.allDrawn) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001546 if (w.performShowLocked()) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001547 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
1548 if (DEBUG_LAYOUT_REPEATS) {
1549 mService.mWindowPlacerLocked.debugLayoutRepeats(
1550 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
1551 }
1552 }
1553 }
1554 }
1555 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
1556 if (appAnimator != null && appAnimator.thumbnail != null) {
1557 if (appAnimator.thumbnailTransactionSeq != animator.mAnimTransactionSequence) {
1558 appAnimator.thumbnailTransactionSeq = animator.mAnimTransactionSequence;
1559 appAnimator.thumbnailLayer = 0;
1560 }
1561 if (appAnimator.thumbnailLayer < winAnimator.mAnimLayer) {
1562 appAnimator.thumbnailLayer = winAnimator.mAnimLayer;
1563 }
1564 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001565 }, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001566 }
1567
1568 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07001569 resetAnimationBackgroundAnimator();
1570
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001571 // Used to indicate a detached wallpaper.
1572 mTmpWindow = null;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001573
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001574 forAllWindows(w -> {
1575 final WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001576 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001577 return;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001578 }
1579
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001580 final int flags = w.mAttrs.flags;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001581
1582 // If this window is animating, make a note that we have an animating window and take
1583 // care of a request to run a detached wallpaper animation.
1584 if (winAnimator.mAnimating) {
1585 if (winAnimator.mAnimation != null) {
1586 if ((flags & FLAG_SHOW_WALLPAPER) != 0
1587 && winAnimator.mAnimation.getDetachWallpaper()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001588 mTmpWindow = w;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001589 }
1590 final int color = winAnimator.mAnimation.getBackgroundColor();
1591 if (color != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001592 final TaskStack stack = w.getStack();
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001593 if (stack != null) {
1594 stack.setAnimationBackground(winAnimator, color);
1595 }
1596 }
1597 }
1598 animator.setAnimating(true);
1599 }
1600
1601 // If this window's app token is running a detached wallpaper animation, make a note so
1602 // we can ensure the wallpaper is displayed behind it.
1603 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
1604 if (appAnimator != null && appAnimator.animation != null
1605 && appAnimator.animating) {
1606 if ((flags & FLAG_SHOW_WALLPAPER) != 0
1607 && appAnimator.animation.getDetachWallpaper()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001608 mTmpWindow = w;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001609 }
1610
1611 final int color = appAnimator.animation.getBackgroundColor();
1612 if (color != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001613 final TaskStack stack = w.getStack();
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001614 if (stack != null) {
1615 stack.setAnimationBackground(winAnimator, color);
1616 }
1617 }
1618 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001619 }, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001620
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001621 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001622 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001623 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
1624 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001625 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
1626 }
1627 }
1628
1629 void prepareWindowSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001630 forAllWindows(w -> {
1631 w.mWinAnimator.prepareSurfaceLocked(true);
1632 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001633 }
1634
Wale Ogunwale494009b82016-10-21 09:01:38 -07001635 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001636 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001637 if (imFocus == null) {
1638 return false;
1639 }
1640
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001641 if (DEBUG_INPUT_METHOD) {
1642 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
1643 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
1644 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001645 }
1646
Wale Ogunwale494009b82016-10-21 09:01:38 -07001647 final IInputMethodClient imeClient = imFocus.mSession.mClient;
1648
1649 if (DEBUG_INPUT_METHOD) {
1650 Slog.i(TAG_WM, "IM target client: " + imeClient);
1651 if (imeClient != null) {
1652 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
1653 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
1654 }
1655 }
1656
1657 return imeClient != null && imeClient.asBinder() == client.asBinder();
1658 }
1659
1660 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001661 final WindowState win = getWindow(
1662 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
1663 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001664 }
1665
1666 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001667 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07001668 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001669 final int curValue = w.mSystemUiVisibility;
1670 final int diff = (curValue ^ visibility) & globalDiff;
1671 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07001672 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001673 w.mSeq++;
1674 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001675 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001676 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
1677 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07001678 visibility, newValue, diff);
1679 }
1680 } catch (RemoteException e) {
1681 // so sorry
1682 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001683 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07001684 }
1685
1686 void onWindowFreezeTimeout() {
1687 Slog.w(TAG_WM, "Window freeze timeout expired.");
1688 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001689
1690 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07001691 if (!w.mOrientationChanging) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001692 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001693 }
1694 w.mOrientationChanging = false;
1695 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
1696 - mService.mDisplayFreezeTime);
1697 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001698 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07001699 mService.mWindowPlacerLocked.performSurfacePlacement();
1700 }
1701
1702 void waitForAllWindowsDrawn() {
1703 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001704 forAllWindows(w -> {
1705 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
1706 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
1707 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001708 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001709 w.mLastContentInsets.set(-1, -1, -1, -1);
1710 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07001711 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001712 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07001713 }
1714
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001715 // TODO: Super crazy long method that should be broken down...
1716 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
1717
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001718 final int dw = mDisplayInfo.logicalWidth;
1719 final int dh = mDisplayInfo.logicalHeight;
1720 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
1721
1722 mTmpUpdateAllDrawn.clear();
1723
1724 int repeats = 0;
1725 do {
1726 repeats++;
1727 if (repeats > 6) {
1728 Slog.w(TAG, "Animation repeat aborted after too many iterations");
1729 clearLayoutNeeded();
1730 break;
1731 }
1732
1733 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
1734 pendingLayoutChanges);
1735
Andrii Kulian839def92016-11-02 10:58:58 -07001736 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
1737 // the wallpaper window jumping across displays.
1738 // Remove check for default display when there will be support for multiple wallpaper
1739 // targets (on different displays).
1740 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001741 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001742 }
1743
1744 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
1745 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
1746 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
1747 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07001748 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001749 }
1750 }
1751
1752 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
1753 setLayoutNeeded();
1754 }
1755
1756 // FIRST LOOP: Perform a layout, if needed.
1757 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
1758 performLayout(repeats == 1, false /* updateInputWindows */);
1759 } else {
1760 Slog.w(TAG, "Layout repeat skipped after too many iterations");
1761 }
1762
1763 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
1764 pendingLayoutChanges = 0;
1765
1766 if (isDefaultDisplay) {
1767 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001768 forAllWindows(w -> {
Jorim Jaggife762342016-10-13 14:33:27 +02001769 mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
1770 mService.mInputMethodTarget);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001771 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001772 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
1773 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
1774 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
1775 }
1776 } while (pendingLayoutChanges != 0);
1777
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001778 final RootWindowContainer root = mService.mRoot;
1779 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001780 resetDimming();
1781
1782 // Only used if default window
1783 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
1784
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001785 forAllWindows(w -> {
1786 final boolean obscuredChanged = w.mObscured !=
1787 mTmpApplySurfaceChangesTransactionState.obscured;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001788
1789 // Update effect.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001790 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
1791 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001792 final boolean isDisplayed = w.isDisplayedLw();
1793
Wale Ogunwaleca9e0612016-12-02 07:45:59 -08001794 if (isDisplayed && w.isObscuringDisplay()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001795 // This window completely covers everything behind it, so we want to leave all
1796 // of them as undimmed (for performance reasons).
1797 root.mObscuringWindow = w;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001798 mTmpApplySurfaceChangesTransactionState.obscured = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001799 }
1800
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001801 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
1802 root.handleNotObscuredLocked(w,
1803 mTmpApplySurfaceChangesTransactionState.obscured,
1804 mTmpApplySurfaceChangesTransactionState.syswin);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001805
1806 if (w.mHasSurface && isDisplayed) {
1807 final int type = w.mAttrs.type;
1808 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
1809 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001810 mTmpApplySurfaceChangesTransactionState.syswin = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001811 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001812 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
1813 && w.mAttrs.preferredRefreshRate != 0) {
1814 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
1815 = w.mAttrs.preferredRefreshRate;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001816 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001817 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
1818 && w.mAttrs.preferredDisplayModeId != 0) {
1819 mTmpApplySurfaceChangesTransactionState.preferredModeId
1820 = w.mAttrs.preferredDisplayModeId;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001821 }
1822 }
1823 }
1824
1825 w.applyDimLayerIfNeeded();
1826
1827 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
1828 && mWallpaperController.isWallpaperTarget(w)) {
1829 // This is the wallpaper target and its obscured state changed... make sure the
1830 // current wallpaper's visibility has been updated accordingly.
1831 mWallpaperController.updateWallpaperVisibility();
1832 }
1833
1834 w.handleWindowMovedIfNeeded();
1835
1836 final WindowStateAnimator winAnimator = w.mWinAnimator;
1837
1838 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
1839 w.mContentChanged = false;
1840
1841 // Moved from updateWindowsAndWallpaperLocked().
1842 if (w.mHasSurface) {
1843 // Take care of the window being ready to display.
1844 final boolean committed = winAnimator.commitFinishDrawingLocked();
1845 if (isDefaultDisplay && committed) {
1846 if (w.mAttrs.type == TYPE_DREAM) {
1847 // HACK: When a dream is shown, it may at that point hide the lock screen.
1848 // So we need to redo the layout to let the phone window manager make this
1849 // happen.
1850 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
1851 if (DEBUG_LAYOUT_REPEATS) {
1852 surfacePlacer.debugLayoutRepeats(
1853 "dream and commitFinishDrawingLocked true",
1854 pendingLayoutChanges);
1855 }
1856 }
1857 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
1858 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
1859 "First draw done in potential wallpaper target " + w);
1860 root.mWallpaperMayChange = true;
1861 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
1862 if (DEBUG_LAYOUT_REPEATS) {
1863 surfacePlacer.debugLayoutRepeats(
1864 "wallpaper and commitFinishDrawingLocked true",
1865 pendingLayoutChanges);
1866 }
1867 }
1868 }
1869 if (!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening()) {
1870 // Updates the shown frame before we set up the surface. This is needed
1871 // because the resizing could change the top-left position (in addition to
1872 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
1873 // position the surface.
1874 //
1875 // If an animation is being started, we can't call this method because the
1876 // animation hasn't processed its initial transformation yet, but in general
1877 // we do want to update the position if the window is animating.
1878 winAnimator.computeShownFrameLocked();
1879 }
1880 winAnimator.setSurfaceBoundariesLocked(recoveringMemory);
1881 }
1882
1883 final AppWindowToken atoken = w.mAppToken;
1884 if (atoken != null) {
1885 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
1886 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
1887 mTmpUpdateAllDrawn.add(atoken);
1888 }
1889 }
1890
1891 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
1892 && w.isDisplayedLw()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001893 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001894 }
1895
1896 w.updateResizingWindowIfNeeded();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001897 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001898
1899 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001900 mTmpApplySurfaceChangesTransactionState.displayHasContent,
1901 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
1902 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001903 true /* inTraversal, must call performTraversalInTrans... below */);
1904
1905 stopDimmingIfNeeded();
1906
1907 while (!mTmpUpdateAllDrawn.isEmpty()) {
1908 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
1909 // See if any windows have been drawn, so they (and others associated with them)
1910 // can now be shown.
1911 atoken.updateAllDrawn(this);
1912 }
1913
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001914 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001915 }
1916
1917 void performLayout(boolean initial, boolean updateInputWindows) {
1918 if (!isLayoutNeeded()) {
1919 return;
1920 }
1921 clearLayoutNeeded();
1922
1923 final int dw = mDisplayInfo.logicalWidth;
1924 final int dh = mDisplayInfo.logicalHeight;
1925
1926 int i;
1927
1928 if (DEBUG_LAYOUT) {
1929 Slog.v(TAG, "-------------------------------------");
1930 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
1931 }
1932
1933 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mService.mRotation,
1934 getConfiguration().uiMode);
1935 if (isDefaultDisplay) {
1936 // Not needed on non-default displays.
1937 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
1938 mService.mScreenRect.set(0, 0, dw, dh);
1939 }
1940
1941 mService.mPolicy.getContentRectLw(mContentRect);
1942
1943 int seq = mService.mLayoutSeq + 1;
1944 if (seq < 0) seq = 0;
1945 mService.mLayoutSeq = seq;
1946
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001947 // Used to indicate that we have processed the dream window and all additional windows are
1948 // behind it.
1949 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001950
1951 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001952 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001953 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
1954 // wasting time and funky changes while a window is animating away.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001955 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
1956 || w.isGoneForLayoutLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001957
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001958 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
1959 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
1960 + " mLayoutAttached=" + w.mLayoutAttached
1961 + " screen changed=" + w.isConfigChanged());
1962 final AppWindowToken atoken = w.mAppToken;
1963 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
1964 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001965 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001966 + " parentHidden=" + w.isParentWindowHidden());
1967 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
1968 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001969 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001970 + " parentHidden=" + w.isParentWindowHidden());
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001971 }
1972
1973 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
1974 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
1975 // since that means "perform layout as normal, just don't display").
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001976 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
1977 || ((w.isConfigChanged() || w.setReportResizeHints())
1978 && !w.isGoneForLayoutLw() &&
1979 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
1980 (w.mHasSurface && w.mAppToken != null &&
1981 w.mAppToken.layoutConfigChanges)))) {
1982 if (!w.mLayoutAttached) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001983 if (initial) {
1984 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001985 w.mContentChanged = false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001986 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001987 if (w.mAttrs.type == TYPE_DREAM) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001988 // Don't layout windows behind a dream, so that if it does stuff like hide
1989 // the status bar we won't get a bad transition when it goes away.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001990 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001991 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001992 w.mLayoutNeeded = false;
1993 w.prelayout();
1994 mService.mPolicy.layoutWindowLw(w, null);
1995 w.mLayoutSeq = mService.mLayoutSeq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001996
1997 // Window frames may have changed. Update dim layer with the new bounds.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001998 final Task task = w.getTask();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001999 if (task != null) {
2000 mDimLayerController.updateDimLayer(task);
2001 }
2002
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002003 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
2004 + " mContainingFrame=" + w.mContainingFrame
2005 + " mDisplayFrame=" + w.mDisplayFrame);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002006 }
2007 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002008 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002009
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002010 // Used to indicate that we have processed the dream window and all additional attached
2011 // windows are behind it.
2012 final WindowState dreamWin = mTmpWindow;
2013 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002014
2015 // Now perform layout of attached windows, which usually depend on the position of the
2016 // window they are attached to. XXX does not deal with windows that are attached to windows
2017 // that are themselves attached.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002018 forAllWindows(w -> {
2019 if (w.mLayoutAttached) {
2020 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
2021 + " mViewVisibility=" + w.mViewVisibility
2022 + " mRelayoutCalled=" + w.mRelayoutCalled);
2023 // If this view is GONE, then skip it -- keep the current frame, and let the
2024 // caller know so they can ignore it if they want. (We do the normal layout for
2025 // INVISIBLE windows, since that means "perform layout as normal, just don't
2026 // display").
2027 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
2028 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002029 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002030 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
2031 || w.mLayoutNeeded) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002032 if (initial) {
2033 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002034 w.mContentChanged = false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002035 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002036 w.mLayoutNeeded = false;
2037 w.prelayout();
2038 mService.mPolicy.layoutWindowLw(w, w.getParentWindow());
2039 w.mLayoutSeq = mService.mLayoutSeq;
2040 if (DEBUG_LAYOUT)
2041 Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
2042 + " mContainingFrame=" + w.mContainingFrame
2043 + " mDisplayFrame=" + w.mDisplayFrame);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002044 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002045 } else if (w.mAttrs.type == TYPE_DREAM) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002046 // Don't layout windows behind a dream, so that if it does stuff like hide the
2047 // status bar we won't get a bad transition when it goes away.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002048 mTmpWindow = dreamWin;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002049 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002050 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002051
2052 // Window frames may have changed. Tell the input dispatcher about it.
2053 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2054 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2055 if (updateInputWindows) {
2056 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2057 }
2058
2059 mService.mPolicy.finishLayoutLw();
2060 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2061 }
2062
2063 /**
2064 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2065 * In portrait mode, it grabs the full screenshot.
2066 *
2067 * @param width the width of the target bitmap
2068 * @param height the height of the target bitmap
2069 * @param includeFullDisplay true if the screen should not be cropped before capture
2070 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2071 * @param config of the output bitmap
2072 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
2073 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002074 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002075 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
2076 boolean wallpaperOnly) {
2077 int dw = mDisplayInfo.logicalWidth;
2078 int dh = mDisplayInfo.logicalHeight;
2079 if (dw == 0 || dh == 0) {
2080 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2081 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2082 return null;
2083 }
2084
2085 Bitmap bm = null;
2086
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002087 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002088 final Rect frame = new Rect();
2089 final Rect stackBounds = new Rect();
2090
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002091 boolean includeImeInScreenshot;
2092 synchronized(mService.mWindowMap) {
2093 final AppWindowToken imeTargetAppToken = mService.mInputMethodTarget != null
2094 ? mService.mInputMethodTarget.mAppToken : null;
2095 // We only include the Ime in the screenshot if the app we are screenshoting is the IME
2096 // target and isn't in multi-window mode. We don't screenshot the IME in multi-window
2097 // mode because the frame of the IME might not overlap with that of the app.
2098 // E.g. IME target app at the top in split-screen mode and the IME at the bottom
2099 // overlapping with the bottom app.
2100 includeImeInScreenshot = imeTargetAppToken != null
2101 && imeTargetAppToken.appToken != null
2102 && imeTargetAppToken.appToken.asBinder() == appToken
2103 && !mService.mInputMethodTarget.isInMultiWindowMode();
2104 }
2105
2106 final int aboveAppLayer = (mService.mPolicy.windowTypeToLayerLw(TYPE_APPLICATION) + 1)
2107 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
2108
2109 synchronized(mService.mWindowMap) {
2110 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002111 mScreenshotApplicationState.appWin = null;
2112 forAllWindows(w -> {
2113 if (!w.mHasSurface) {
2114 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002115 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002116 if (w.mLayer >= aboveAppLayer) {
2117 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002118 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002119 if (wallpaperOnly && !w.mIsWallpaper) {
2120 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002121 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002122 if (w.mIsImWindow) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002123 if (!includeImeInScreenshot) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002124 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002125 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002126 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002127 // If this is the wallpaper layer and we're only looking for the wallpaper layer
2128 // then the target window state is this one.
2129 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002130 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002131 }
2132
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002133 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002134 // We have not ran across the target window yet, so it is probably behind
2135 // the wallpaper. This can happen when the keyguard is up and all windows
2136 // are moved behind the wallpaper. We don't want to include the wallpaper
2137 // layer in the screenshot as it will cover-up the layer of the target
2138 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002139 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002140 }
2141 // Fall through. The target window is in front of the wallpaper. For this
2142 // case we want to include the wallpaper layer in the screenshot because
2143 // the target window might have some transparent areas.
2144 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002145 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002146 // This app window is of no interest if it is not associated with the
2147 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002148 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002149 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002150 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002151 }
2152
2153 // Include this window.
2154
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002155 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002156 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002157 if (mScreenshotApplicationState.maxLayer < layer) {
2158 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002159 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002160 if (mScreenshotApplicationState.minLayer > layer) {
2161 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002162 }
2163
2164 // Don't include wallpaper in bounds calculation
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002165 if (!includeFullDisplay && !w.mIsWallpaper) {
2166 final Rect wf = w.mFrame;
2167 final Rect cr = w.mContentInsets;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002168 int left = wf.left + cr.left;
2169 int top = wf.top + cr.top;
2170 int right = wf.right - cr.right;
2171 int bottom = wf.bottom - cr.bottom;
2172 frame.union(left, top, right, bottom);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002173 w.getVisibleBounds(stackBounds);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002174 if (!Rect.intersects(frame, stackBounds)) {
2175 // Set frame empty if there's no intersection.
2176 frame.setEmpty();
2177 }
2178 }
2179
2180 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002181 (w.mAppToken != null && w.mAppToken.token == appToken)
2182 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
2183 if (foundTargetWs && w.isDisplayedLw() && winAnim.getShown()) {
2184 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002185 }
2186
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002187 if (w.isObscuringDisplay()){
2188 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002189 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002190 return false;
2191 }, true /* traverseTopToBottom */);
2192
2193 final WindowState appWin = mScreenshotApplicationState.appWin;
2194 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
2195 final int maxLayer = mScreenshotApplicationState.maxLayer;
2196 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002197
2198 if (appToken != null && appWin == null) {
2199 // Can't find a window to snapshot.
2200 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
2201 "Screenshot: Couldn't find a surface matching " + appToken);
2202 return null;
2203 }
2204
2205 if (!screenshotReady) {
2206 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
2207 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
2208 appWin.mWinAnimator.mDrawState)));
2209 return null;
2210 }
2211
2212 // Screenshot is ready to be taken. Everything from here below will continue
2213 // through the bottom of the loop and return a value. We only stay in the loop
2214 // because we don't want to release the mWindowMap lock until the screenshot is
2215 // taken.
2216
2217 if (maxLayer == 0) {
2218 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2219 + ": returning null maxLayer=" + maxLayer);
2220 return null;
2221 }
2222
2223 if (!includeFullDisplay) {
2224 // Constrain frame to the screen size.
2225 if (!frame.intersect(0, 0, dw, dh)) {
2226 frame.setEmpty();
2227 }
2228 } else {
2229 // Caller just wants entire display.
2230 frame.set(0, 0, dw, dh);
2231 }
2232 if (frame.isEmpty()) {
2233 return null;
2234 }
2235
2236 if (width < 0) {
2237 width = (int) (frame.width() * frameScale);
2238 }
2239 if (height < 0) {
2240 height = (int) (frame.height() * frameScale);
2241 }
2242
2243 // Tell surface flinger what part of the image to crop. Take the top
2244 // right part of the application, and crop the larger dimension to fit.
2245 Rect crop = new Rect(frame);
2246 if (width / (float) frame.width() < height / (float) frame.height()) {
2247 int cropWidth = (int)((float)width / (float)height * frame.height());
2248 crop.right = crop.left + cropWidth;
2249 } else {
2250 int cropHeight = (int)((float)height / (float)width * frame.width());
2251 crop.bottom = crop.top + cropHeight;
2252 }
2253
2254 // The screenshot API does not apply the current screen rotation.
2255 int rot = mDisplay.getRotation();
2256
2257 if (rot == ROTATION_90 || rot == ROTATION_270) {
2258 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
2259 }
2260
2261 // Surfaceflinger is not aware of orientation, so convert our logical
2262 // crop to surfaceflinger's portrait orientation.
2263 convertCropForSurfaceFlinger(crop, rot, dw, dh);
2264
2265 if (DEBUG_SCREENSHOT) {
2266 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
2267 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002268 forAllWindows(w -> {
2269 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
2270 Slog.i(TAG_WM, w + ": " + w.mLayer
2271 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002272 + " surfaceLayer=" + ((controller == null)
2273 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002274 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002275 }
2276
2277 final ScreenRotationAnimation screenRotationAnimation =
2278 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
2279 final boolean inRotation = screenRotationAnimation != null &&
2280 screenRotationAnimation.isAnimating();
2281 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
2282 "Taking screenshot while rotating");
2283
2284 // We force pending transactions to flush before taking
2285 // the screenshot by pushing an empty synchronous transaction.
2286 SurfaceControl.openTransaction();
2287 SurfaceControl.closeTransactionSync();
2288
2289 bm = SurfaceControl.screenshot(crop, width, height, minLayer, maxLayer,
2290 inRotation, rot);
2291 if (bm == null) {
2292 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
2293 + ") to layer " + maxLayer);
2294 return null;
2295 }
2296 }
2297
2298 if (DEBUG_SCREENSHOT) {
2299 // TEST IF IT's ALL BLACK
2300 int[] buffer = new int[bm.getWidth() * bm.getHeight()];
2301 bm.getPixels(buffer, 0, bm.getWidth(), 0, 0, bm.getWidth(), bm.getHeight());
2302 boolean allBlack = true;
2303 final int firstColor = buffer[0];
2304 for (int i = 0; i < buffer.length; i++) {
2305 if (buffer[i] != firstColor) {
2306 allBlack = false;
2307 break;
2308 }
2309 }
2310 if (allBlack) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002311 final WindowState appWin = mScreenshotApplicationState.appWin;
2312 final int maxLayer = mScreenshotApplicationState.maxLayer;
2313 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002314 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2315 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2316 (appWin != null ?
2317 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2318 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2319 }
2320 }
2321
2322 // Create a copy of the screenshot that is immutable and backed in ashmem.
2323 // This greatly reduces the overhead of passing the bitmap between processes.
2324 Bitmap ret = bm.createAshmemBitmap(config);
2325 bm.recycle();
2326 return ret;
2327 }
2328
2329 // TODO: Can this use createRotationMatrix()?
2330 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
2331 if (rot == Surface.ROTATION_90) {
2332 final int tmp = crop.top;
2333 crop.top = dw - crop.right;
2334 crop.right = crop.bottom;
2335 crop.bottom = dw - crop.left;
2336 crop.left = tmp;
2337 } else if (rot == Surface.ROTATION_180) {
2338 int tmp = crop.top;
2339 crop.top = dh - crop.bottom;
2340 crop.bottom = dh - tmp;
2341 tmp = crop.right;
2342 crop.right = dw - crop.left;
2343 crop.left = dw - tmp;
2344 } else if (rot == Surface.ROTATION_270) {
2345 final int tmp = crop.top;
2346 crop.top = crop.left;
2347 crop.left = dh - crop.bottom;
2348 crop.bottom = crop.right;
2349 crop.right = dh - tmp;
2350 }
2351 }
2352
2353 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002354 // Used to indicate the layout is needed.
2355 mTmpWindow = null;
2356
2357 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002358 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002359 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002360 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002361 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002362 w.setDisplayLayoutNeeded();
2363 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002364 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002365
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002366 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002367 mService.mWindowPlacerLocked.performSurfacePlacement();
2368 }
2369 }
2370
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002371 static final class TaskForResizePointSearchResult {
2372 boolean searchDone;
2373 Task taskForResize;
2374
2375 void reset() {
2376 searchDone = false;
2377 taskForResize = null;
2378 }
2379 }
Robert Carr3b716242016-08-16 16:02:21 -07002380
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002381 private static final class ApplySurfaceChangesTransactionState {
2382 boolean displayHasContent;
2383 boolean obscured;
2384 boolean syswin;
2385 boolean focusDisplayed;
2386 float preferredRefreshRate;
2387 int preferredModeId;
2388
2389 void reset() {
2390 displayHasContent = false;
2391 obscured = false;
2392 syswin = false;
2393 focusDisplayed = false;
2394 preferredRefreshRate = 0;
2395 preferredModeId = 0;
2396 }
2397 }
2398
2399 private static final class ScreenshotApplicationState {
2400 WindowState appWin;
2401 int maxLayer;
2402 int minLayer;
2403 boolean screenshotReady;
2404
2405 void reset(boolean screenshotReady) {
2406 appWin = null;
2407 maxLayer = 0;
2408 minLayer = 0;
2409 this.screenshotReady = screenshotReady;
2410 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
2411 }
2412 }
2413
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002414 /**
2415 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
2416 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
2417 * homogeneous children type which is currently required by sub-classes of
2418 * {@link WindowContainer} class.
2419 */
2420 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
2421
2422 int size() {
2423 return mChildren.size();
2424 }
2425
2426 E get(int index) {
2427 return mChildren.get(index);
2428 }
2429
2430 @Override
2431 boolean fillsParent() {
2432 return true;
2433 }
2434
2435 @Override
2436 boolean isVisible() {
2437 return true;
Robert Carr3b716242016-08-16 16:02:21 -07002438 }
2439 }
2440
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002441 /**
2442 * Window container class that contains all containers on this display relating to Apps.
2443 * I.e Activities.
2444 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07002445 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002446
Andrii Kulian839def92016-11-02 10:58:58 -07002447 /**
2448 * Adds the stack to this container.
2449 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
2450 */
2451 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002452 if (stack.mStackId == HOME_STACK_ID) {
2453 if (mHomeStack != null) {
2454 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
2455 }
2456 mHomeStack = stack;
2457 }
2458 addChild(stack, onTop);
2459 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07002460 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002461
Andrii Kulian839def92016-11-02 10:58:58 -07002462 /** Removes the stack from its container and prepare for changing the parent. */
2463 void removeStackFromDisplay(TaskStack stack) {
2464 removeChild(stack);
2465 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07002466 }
2467
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002468 void moveStack(TaskStack stack, boolean toTop) {
2469 if (StackId.isAlwaysOnTop(stack.mStackId) && !toTop) {
2470 // This stack is always-on-top silly...
2471 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + stack + " to bottom");
2472 return;
2473 }
2474
2475 if (!mChildren.contains(stack)) {
2476 Slog.wtf(TAG_WM, "moving stack that was not added: " + stack, new Throwable());
2477 }
2478 removeChild(stack);
2479 addChild(stack, toTop);
2480 }
2481
2482 private void addChild(TaskStack stack, boolean toTop) {
2483 int addIndex = toTop ? mChildren.size() : 0;
2484
2485 if (toTop
2486 && mService.isStackVisibleLocked(PINNED_STACK_ID)
2487 && stack.mStackId != PINNED_STACK_ID) {
2488 // The pinned stack is always the top most stack (always-on-top) when it is visible.
2489 // So, stack is moved just below the pinned stack.
2490 addIndex--;
2491 TaskStack topStack = mChildren.get(addIndex);
2492 if (topStack.mStackId != PINNED_STACK_ID) {
2493 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
2494 }
2495 }
2496 addChild(stack, addIndex);
2497 setLayoutNeeded();
2498 }
2499
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002500 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002501 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
2502 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002503 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002504 if (super.forAllWindows(callback, traverseTopToBottom)) {
2505 return true;
2506 }
2507 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
2508 return true;
2509 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002510 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002511 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
2512 return true;
2513 }
2514 if (super.forAllWindows(callback, traverseTopToBottom)) {
2515 return true;
2516 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002517 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002518 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002519 }
2520
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002521 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002522 boolean traverseTopToBottom) {
2523 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
2524 // app tokens.
2525 // TODO: Investigate if we need to continue to do this or if we can just process them
2526 // in-order.
2527 if (traverseTopToBottom) {
2528 for (int i = mChildren.size() - 1; i >= 0; --i) {
2529 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
2530 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002531 if (appTokens.get(j).forAllWindowsUnchecked(callback,
2532 traverseTopToBottom)) {
2533 return true;
2534 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002535 }
2536 }
2537 } else {
2538 final int count = mChildren.size();
2539 for (int i = 0; i < count; ++i) {
2540 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
2541 final int appTokensCount = appTokens.size();
2542 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002543 if (appTokens.get(j).forAllWindowsUnchecked(callback,
2544 traverseTopToBottom)) {
2545 return true;
2546 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002547 }
2548 }
2549 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002550 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002551 }
2552
2553 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002554 int getOrientation() {
2555 if (mService.isStackVisibleLocked(DOCKED_STACK_ID)
2556 || mService.isStackVisibleLocked(FREEFORM_WORKSPACE_STACK_ID)) {
2557 // Apps and their containers are not allowed to specify an orientation while the
2558 // docked or freeform stack is visible...except for the home stack/task if the
2559 // docked stack is minimized and it actually set something.
2560 if (mHomeStack != null && mHomeStack.isVisible()
2561 && mDividerControllerLocked.isMinimizedDock()) {
2562 final int orientation = mHomeStack.getOrientation();
2563 if (orientation != SCREEN_ORIENTATION_UNSET) {
2564 return orientation;
2565 }
2566 }
2567 return SCREEN_ORIENTATION_UNSPECIFIED;
2568 }
2569
2570 final int orientation = super.getOrientation();
2571 if (orientation != SCREEN_ORIENTATION_UNSET
2572 && orientation != SCREEN_ORIENTATION_BEHIND) {
2573 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
2574 "App is requesting an orientation, return " + orientation);
2575 return orientation;
2576 }
2577
2578 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
2579 "No app is requesting an orientation, return " + mService.mLastOrientation);
2580 // The next app has not been requested to be visible, so we keep the current orientation
2581 // to prevent freezing/unfreezing the display too early.
2582 return mService.mLastOrientation;
2583 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002584 }
2585
2586 /**
2587 * Window container class that contains all containers on this display that are not related to
2588 * Apps. E.g. status bar.
2589 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07002590 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
2591 /**
2592 * Compares two child window tokens returns -1 if the first is lesser than the second in
2593 * terms of z-order and 1 otherwise.
2594 */
2595 final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
2596 // Tokens with higher base layer are z-ordered on-top.
2597 mService.mPolicy.windowTypeToLayerLw(token1.windowType)
2598 < mService.mPolicy.windowTypeToLayerLw(token2.windowType) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002599
Wale Ogunwale3a931692016-11-02 16:49:48 -07002600 private final String mName;
2601 NonAppWindowContainers(String name) {
2602 mName = name;
2603 }
2604
2605 void addChild(WindowToken token) {
2606 addChild(token, mWindowComparator);
2607 }
2608
2609 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002610 int getOrientation() {
2611 final WindowManagerPolicy policy = mService.mPolicy;
2612 // Find a window requesting orientation.
2613 final WindowState win = getWindow((w) -> {
2614 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
2615 return false;
2616 }
2617 final int req = w.mAttrs.screenOrientation;
2618 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
2619 || req == SCREEN_ORIENTATION_UNSET) {
2620 return false;
2621 }
2622 return true;
2623 });
2624
2625 if (win != null) {
2626 final int req = win.mAttrs.screenOrientation;
2627 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
2628 if (policy.isKeyguardHostWindow(win.mAttrs)) {
2629 mService.mLastKeyguardForcedOrientation = req;
2630 }
2631 return (mService.mLastWindowForcedOrientation = req);
2632 }
2633
2634 mService.mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
2635
2636 if (policy.isKeyguardShowingAndNotOccluded()) {
2637 return mService.mLastKeyguardForcedOrientation;
2638 }
2639
2640 return SCREEN_ORIENTATION_UNSET;
2641 }
2642
2643 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07002644 String getName() {
2645 return mName;
2646 }
Robert Carr3b716242016-08-16 16:02:21 -07002647 }
Craig Mautner59c00972012-07-30 12:10:24 -07002648}