blob: ff841b147d9c4359a4047ce717b38b9f7d69e0e6 [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;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800127import java.util.function.Consumer;
128import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700129
Craig Mautner59c00972012-07-30 12:10:24 -0700130/**
131 * Utility class for keeping track of the WindowStates and other pertinent contents of a
132 * particular Display.
133 *
134 * IMPORTANT: No method from this class should ever be used without holding
135 * WindowManagerService.mWindowMap.
136 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700137class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700138 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700139
140 /** Unique identifier of this stack. */
141 private final int mDisplayId;
142
Wale Ogunwale3a931692016-11-02 16:49:48 -0700143 /** The containers below are the only child containers the display can have. */
144 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700145 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700146 // Contains all non-app window containers that should be displayed above the app containers
147 // (e.g. Status bar)
148 private final NonAppWindowContainers mAboveAppWindowsContainers =
149 new NonAppWindowContainers("mAboveAppWindowsContainers");
150 // Contains all non-app window containers that should be displayed below the app containers
151 // (e.g. Wallpaper).
152 private final NonAppWindowContainers mBelowAppWindowsContainers =
153 new NonAppWindowContainers("mBelowAppWindowsContainers");
154 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
155 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
156 // window containers together and move them in-sync if/when needed.
157 private final NonAppWindowContainers mImeWindowsContainers =
158 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700159
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000160 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800161 private WindowState mTmpWindow2;
162 private WindowAnimator mTmpWindowAnimator;
163 private boolean mTmpRecoveringMemory;
164 private boolean mUpdateImeTarget;
165 private boolean mTmpInitial;
Craig Mautner59c00972012-07-30 12:10:24 -0700166
Wale Ogunwale02319a62016-09-26 15:21:22 -0700167 // Mapping from a token IBinder to a WindowToken object on this display.
168 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
169
Craig Mautner59c00972012-07-30 12:10:24 -0700170 int mInitialDisplayWidth = 0;
171 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700172 int mInitialDisplayDensity = 0;
Craig Mautner59c00972012-07-30 12:10:24 -0700173 int mBaseDisplayWidth = 0;
174 int mBaseDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700175 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700176 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700177 private final DisplayInfo mDisplayInfo = new DisplayInfo();
178 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700179 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Craig Mautner59c00972012-07-30 12:10:24 -0700180
Craig Mautner6601b7b2013-04-29 10:29:11 -0700181 Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700182 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700183
Craig Mautner39834192012-09-02 07:47:24 -0700184 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700185 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700186 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700187 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800188 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700189
Craig Mautnerdc548482014-02-05 13:35:24 -0800190 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700191 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800192
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800193 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
194 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700195 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700196
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700197 /** Detect user tapping outside of current focused task bounds .*/
198 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700199
Craig Mautner6601b7b2013-04-29 10:29:11 -0700200 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700201 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700202
Craig Mautner6601b7b2013-04-29 10:29:11 -0700203 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800204 private final Rect mTmpRect = new Rect();
205 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700206 private final RectF mTmpRectF = new RectF();
207 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800208 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700209
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800210 WindowManagerService mService;
Craig Mautner9d808b12013-08-06 18:00:25 -0700211
Craig Mautner95da1082014-02-24 17:54:35 -0800212 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700213 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800214
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700215 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700216 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700217
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800218 DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700219
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800220 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
221
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000222 private boolean mHaveBootMsg = false;
223 private boolean mHaveApp = false;
224 private boolean mHaveWallpaper = false;
225 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700226
227 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
228
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700229 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
230 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000231 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
232 new ApplySurfaceChangesTransactionState();
233 private final ScreenshotApplicationState mScreenshotApplicationState =
234 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700235
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700236 // True if this display is in the process of being removed. Used to determine if the removal of
237 // the display's direct children should be allowed.
238 private boolean mRemovingDisplay = false;
239
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700240 private final WindowLayersController mLayersController;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800241 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700242 int mInputMethodAnimLayerAdjustment;
243
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800244 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
245 WindowStateAnimator winAnimator = w.mWinAnimator;
246 if (winAnimator.hasSurface()) {
247 final boolean wasAnimating = winAnimator.mWasAnimating;
248 final boolean nowAnimating = winAnimator.stepAnimationLocked(
249 mTmpWindowAnimator.mCurrentTime);
250 winAnimator.mWasAnimating = nowAnimating;
251 mTmpWindowAnimator.orAnimating(nowAnimating);
252
253 if (DEBUG_WALLPAPER) Slog.v(TAG,
254 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
255
256 if (wasAnimating && !winAnimator.mAnimating
257 && mWallpaperController.isWallpaperTarget(w)) {
258 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
259 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
260 if (DEBUG_LAYOUT_REPEATS) {
261 mService.mWindowPlacerLocked.debugLayoutRepeats(
262 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
263 }
264 }
265 }
266
267 final AppWindowToken atoken = w.mAppToken;
268 if (winAnimator.mDrawState == READY_TO_SHOW) {
269 if (atoken == null || atoken.allDrawn) {
270 if (w.performShowLocked()) {
271 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
272 if (DEBUG_LAYOUT_REPEATS) {
273 mService.mWindowPlacerLocked.debugLayoutRepeats(
274 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
275 }
276 }
277 }
278 }
279 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
280 if (appAnimator != null && appAnimator.thumbnail != null) {
281 if (appAnimator.thumbnailTransactionSeq
282 != mTmpWindowAnimator.mAnimTransactionSequence) {
283 appAnimator.thumbnailTransactionSeq =
284 mTmpWindowAnimator.mAnimTransactionSequence;
285 appAnimator.thumbnailLayer = 0;
286 }
287 if (appAnimator.thumbnailLayer < winAnimator.mAnimLayer) {
288 appAnimator.thumbnailLayer = winAnimator.mAnimLayer;
289 }
290 }
291 };
292
293 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
294 final WindowStateAnimator winAnimator = w.mWinAnimator;
295 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
296 return;
297 }
298
299 final int flags = w.mAttrs.flags;
300
301 // If this window is animating, make a note that we have an animating window and take
302 // care of a request to run a detached wallpaper animation.
303 if (winAnimator.mAnimating) {
304 if (winAnimator.mAnimation != null) {
305 if ((flags & FLAG_SHOW_WALLPAPER) != 0
306 && winAnimator.mAnimation.getDetachWallpaper()) {
307 mTmpWindow = w;
308 }
309 final int color = winAnimator.mAnimation.getBackgroundColor();
310 if (color != 0) {
311 final TaskStack stack = w.getStack();
312 if (stack != null) {
313 stack.setAnimationBackground(winAnimator, color);
314 }
315 }
316 }
317 mTmpWindowAnimator.setAnimating(true);
318 }
319
320 // If this window's app token is running a detached wallpaper animation, make a note so
321 // we can ensure the wallpaper is displayed behind it.
322 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
323 if (appAnimator != null && appAnimator.animation != null
324 && appAnimator.animating) {
325 if ((flags & FLAG_SHOW_WALLPAPER) != 0
326 && appAnimator.animation.getDetachWallpaper()) {
327 mTmpWindow = w;
328 }
329
330 final int color = appAnimator.animation.getBackgroundColor();
331 if (color != 0) {
332 final TaskStack stack = w.getStack();
333 if (stack != null) {
334 stack.setAnimationBackground(winAnimator, color);
335 }
336 }
337 }
338 };
339
340 private final Consumer<WindowState> mSetInputMethodAnimLayerAdjustment =
341 w -> w.adjustAnimLayer(mInputMethodAnimLayerAdjustment);
342
343 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
344 final int lostFocusUid = mTmpWindow.mOwnerUid;
345 final Handler handler = mService.mH;
346 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
347 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
348 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
349 w.mAttrs.hideTimeoutMilliseconds);
350 }
351 }
352 };
353
354 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
355 final AppWindowToken focusedApp = mService.mFocusedApp;
356 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
357 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
358
359 if (!w.canReceiveKeys()) {
360 return false;
361 }
362
363 final AppWindowToken wtoken = w.mAppToken;
364
365 // If this window's application has been removed, just skip it.
366 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
367 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
368 + (wtoken.removed ? "removed" : "sendingToBottom"));
369 return false;
370 }
371
372 if (focusedApp == null) {
373 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
374 + " using new focus @ " + w);
375 mTmpWindow = w;
376 return true;
377 }
378
379 if (!focusedApp.windowsAreFocusable()) {
380 // Current focused app windows aren't focusable...
381 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
382 + " focusable using new focus @ " + w);
383 mTmpWindow = w;
384 return true;
385 }
386
387 // Descend through all of the app tokens and find the first that either matches
388 // win.mAppToken (return win) or mFocusedApp (return null).
389 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
390 if (focusedApp.compareTo(wtoken) > 0) {
391 // App stack below focused app stack. No focus for you!!!
392 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
393 "findFocusedWindow: Reached focused app=" + focusedApp);
394 mTmpWindow = null;
395 return true;
396 }
397 }
398
399 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
400 mTmpWindow = w;
401 return true;
402 };
403
404 private final Consumer<WindowState> mPrepareWindowSurfaces =
405 w -> w.mWinAnimator.prepareSurfaceLocked(true);
406
407 private final Consumer<WindowState> mPerformLayout = w -> {
408 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
409 // wasting time and funky changes while a window is animating away.
410 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
411 || w.isGoneForLayoutLw();
412
413 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
414 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
415 + " mLayoutAttached=" + w.mLayoutAttached
416 + " screen changed=" + w.isConfigChanged());
417 final AppWindowToken atoken = w.mAppToken;
418 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
419 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
420 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
421 + " parentHidden=" + w.isParentWindowHidden());
422 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
423 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
424 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
425 + " parentHidden=" + w.isParentWindowHidden());
426 }
427
428 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
429 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
430 // since that means "perform layout as normal, just don't display").
431 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
432 || ((w.isConfigChanged() || w.setReportResizeHints())
433 && !w.isGoneForLayoutLw() &&
434 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
435 (w.mHasSurface && w.mAppToken != null &&
436 w.mAppToken.layoutConfigChanges)))) {
437 if (!w.mLayoutAttached) {
438 if (mTmpInitial) {
439 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
440 w.mContentChanged = false;
441 }
442 if (w.mAttrs.type == TYPE_DREAM) {
443 // Don't layout windows behind a dream, so that if it does stuff like hide
444 // the status bar we won't get a bad transition when it goes away.
445 mTmpWindow = w;
446 }
447 w.mLayoutNeeded = false;
448 w.prelayout();
449 mService.mPolicy.layoutWindowLw(w, null);
450 w.mLayoutSeq = mService.mLayoutSeq;
451
452 // Window frames may have changed. Update dim layer with the new bounds.
453 final Task task = w.getTask();
454 if (task != null) {
455 mDimLayerController.updateDimLayer(task);
456 }
457
458 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
459 + " mContainingFrame=" + w.mContainingFrame
460 + " mDisplayFrame=" + w.mDisplayFrame);
461 }
462 }
463 };
464
465 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
466 if (w.mLayoutAttached) {
467 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
468 + " mViewVisibility=" + w.mViewVisibility
469 + " mRelayoutCalled=" + w.mRelayoutCalled);
470 // If this view is GONE, then skip it -- keep the current frame, and let the caller
471 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
472 // windows, since that means "perform layout as normal, just don't display").
473 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
474 return;
475 }
476 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
477 || w.mLayoutNeeded) {
478 if (mTmpInitial) {
479 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
480 w.mContentChanged = false;
481 }
482 w.mLayoutNeeded = false;
483 w.prelayout();
484 mService.mPolicy.layoutWindowLw(w, w.getParentWindow());
485 w.mLayoutSeq = mService.mLayoutSeq;
486 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
487 + " mContainingFrame=" + w.mContainingFrame
488 + " mDisplayFrame=" + w.mDisplayFrame);
489 }
490 } else if (w.mAttrs.type == TYPE_DREAM) {
491 // Don't layout windows behind a dream, so that if it does stuff like hide the
492 // status bar we won't get a bad transition when it goes away.
493 mTmpWindow = mTmpWindow2;
494 }
495 };
496
497 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
498 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
499 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
500 return w.canBeImeTarget();
501 };
502
503 private final Consumer<WindowState> mApplyPostLayoutPolicy =
504 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
505 mService.mInputMethodTarget);
506
507 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
508 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
509 final boolean obscuredChanged = w.mObscured !=
510 mTmpApplySurfaceChangesTransactionState.obscured;
511 final RootWindowContainer root = mService.mRoot;
512 // Only used if default window
513 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
514
515 // Update effect.
516 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
517 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
518 final boolean isDisplayed = w.isDisplayedLw();
519
520 if (isDisplayed && w.isObscuringDisplay()) {
521 // This window completely covers everything behind it, so we want to leave all
522 // of them as undimmed (for performance reasons).
523 root.mObscuringWindow = w;
524 mTmpApplySurfaceChangesTransactionState.obscured = true;
525 }
526
527 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
528 root.handleNotObscuredLocked(w,
529 mTmpApplySurfaceChangesTransactionState.obscured,
530 mTmpApplySurfaceChangesTransactionState.syswin);
531
532 if (w.mHasSurface && isDisplayed) {
533 final int type = w.mAttrs.type;
534 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
535 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
536 mTmpApplySurfaceChangesTransactionState.syswin = true;
537 }
538 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
539 && w.mAttrs.preferredRefreshRate != 0) {
540 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
541 = w.mAttrs.preferredRefreshRate;
542 }
543 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
544 && w.mAttrs.preferredDisplayModeId != 0) {
545 mTmpApplySurfaceChangesTransactionState.preferredModeId
546 = w.mAttrs.preferredDisplayModeId;
547 }
548 }
549 }
550
551 w.applyDimLayerIfNeeded();
552
553 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
554 && mWallpaperController.isWallpaperTarget(w)) {
555 // This is the wallpaper target and its obscured state changed... make sure the
556 // current wallpaper's visibility has been updated accordingly.
557 mWallpaperController.updateWallpaperVisibility();
558 }
559
560 w.handleWindowMovedIfNeeded();
561
562 final WindowStateAnimator winAnimator = w.mWinAnimator;
563
564 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
565 w.mContentChanged = false;
566
567 // Moved from updateWindowsAndWallpaperLocked().
568 if (w.mHasSurface) {
569 // Take care of the window being ready to display.
570 final boolean committed = winAnimator.commitFinishDrawingLocked();
571 if (isDefaultDisplay && committed) {
572 if (w.mAttrs.type == TYPE_DREAM) {
573 // HACK: When a dream is shown, it may at that point hide the lock screen.
574 // So we need to redo the layout to let the phone window manager make this
575 // happen.
576 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
577 if (DEBUG_LAYOUT_REPEATS) {
578 surfacePlacer.debugLayoutRepeats(
579 "dream and commitFinishDrawingLocked true",
580 pendingLayoutChanges);
581 }
582 }
583 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
584 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
585 "First draw done in potential wallpaper target " + w);
586 root.mWallpaperMayChange = true;
587 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
588 if (DEBUG_LAYOUT_REPEATS) {
589 surfacePlacer.debugLayoutRepeats(
590 "wallpaper and commitFinishDrawingLocked true",
591 pendingLayoutChanges);
592 }
593 }
594 }
595 if (!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening()) {
596 // Updates the shown frame before we set up the surface. This is needed
597 // because the resizing could change the top-left position (in addition to
598 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
599 // position the surface.
600 //
601 // If an animation is being started, we can't call this method because the
602 // animation hasn't processed its initial transformation yet, but in general
603 // we do want to update the position if the window is animating.
604 winAnimator.computeShownFrameLocked();
605 }
606 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
607 }
608
609 final AppWindowToken atoken = w.mAppToken;
610 if (atoken != null) {
611 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
612 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
613 mTmpUpdateAllDrawn.add(atoken);
614 }
615 }
616
617 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
618 && w.isDisplayedLw()) {
619 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
620 }
621
622 w.updateResizingWindowIfNeeded();
623 };
624
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800625 /**
Craig Mautner2d5618c2012-10-18 13:55:47 -0700626 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800627 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700628 * @param layersController window layer controller used to assign layer to the windows on this
629 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700630 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
631 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700632 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700633 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700634 WindowLayersController layersController, WallpaperController wallpaperController) {
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700635 mDisplay = display;
636 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700637 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700638 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700639 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700640 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700641 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700642 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700643 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800644 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700645 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800646 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700647
648 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700649 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700650 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700651 super.addChild(mAboveAppWindowsContainers, null);
652 super.addChild(mImeWindowsContainers, null);
Craig Mautner59c00972012-07-30 12:10:24 -0700653 }
654
655 int getDisplayId() {
656 return mDisplayId;
657 }
658
Wale Ogunwale02319a62016-09-26 15:21:22 -0700659 WindowToken getWindowToken(IBinder binder) {
660 return mTokenMap.get(binder);
661 }
662
663 AppWindowToken getAppWindowToken(IBinder binder) {
664 final WindowToken token = getWindowToken(binder);
665 if (token == null) {
666 return null;
667 }
668 return token.asAppWindowToken();
669 }
670
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800671 void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700672 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
673 if (dc != null) {
674 // We currently don't support adding a window token to the display if the display
675 // already has the binder mapped to another token. If there is a use case for supporting
676 // this moving forward we will either need to merge the WindowTokens some how or have
677 // the binder map to a list of window tokens.
678 throw new IllegalArgumentException("Can't map token=" + token + " to display=" + this
679 + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
680 }
681 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700682
683 if (token.asAppWindowToken() == null) {
684 // Add non-app token to container hierarchy on the display. App tokens are added through
685 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700686 switch (token.windowType) {
687 case TYPE_WALLPAPER:
688 mBelowAppWindowsContainers.addChild(token);
689 break;
690 case TYPE_INPUT_METHOD:
691 case TYPE_INPUT_METHOD_DIALOG:
692 mImeWindowsContainers.addChild(token);
693 break;
694 default:
695 mAboveAppWindowsContainers.addChild(token);
696 break;
697 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700698 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700699 }
700
701 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700702 final WindowToken token = mTokenMap.remove(binder);
703 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800704 token.setExiting();
705 }
706 return token;
707 }
708
709 /** Changes the display the input window token is housed on to this one. */
710 void reParentWindowToken(WindowToken token) {
711 final DisplayContent prevDc = token.getDisplayContent();
712 if (prevDc == this) {
713 return;
714 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800715 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
716 && token.asAppWindowToken() == null) {
717 // Removed the token from the map, but made sure it's not an app token before removing
718 // from parent.
719 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700720 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800721
722 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700723 }
724
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700725 void removeAppToken(IBinder binder) {
726 final WindowToken token = removeWindowToken(binder);
727 if (token == null) {
728 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
729 return;
730 }
731
732 final AppWindowToken appToken = token.asAppWindowToken();
733
734 if (appToken == null) {
735 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
736 return;
737 }
738
739 appToken.onRemovedFromDisplay();
740 }
741
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700742 Display getDisplay() {
743 return mDisplay;
744 }
745
Craig Mautner59c00972012-07-30 12:10:24 -0700746 DisplayInfo getDisplayInfo() {
747 return mDisplayInfo;
748 }
749
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700750 DisplayMetrics getDisplayMetrics() {
751 return mDisplayMetrics;
752 }
753
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100754 DockedStackDividerController getDockedDividerController() {
755 return mDividerControllerLocked;
756 }
757
Winson Chung655332c2016-10-31 13:14:28 -0700758 PinnedStackController getPinnedStackController() {
759 return mPinnedStackControllerLocked;
760 }
761
Jeff Browna506a6e2013-06-04 00:02:38 -0700762 /**
763 * Returns true if the specified UID has access to this display.
764 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700765 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700766 return mDisplay.hasAccess(uid);
767 }
768
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700769 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700770 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -0700771 }
772
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700773 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700774 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800775 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -0800776 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700777 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700778 }
779
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700780 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700781 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
782 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700783 if (stack.mStackId == stackId) {
784 return stack;
785 }
786 }
787 return null;
788 }
789
Andrii Kulian441e4492016-09-29 15:25:00 -0700790 @Override
791 void onConfigurationChanged(Configuration newParentConfig) {
792 super.onConfigurationChanged(newParentConfig);
793
Andrii Kulian3a507b52016-09-19 18:14:12 -0700794 // The display size information is heavily dependent on the resources in the current
795 // configuration, so we need to reconfigure it every time the configuration changes.
796 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
797 mService.reconfigureDisplayLocked(this);
798
799 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -0700800 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -0700801 }
Andrii Kulian3a507b52016-09-19 18:14:12 -0700802
Andrii Kulian441e4492016-09-29 15:25:00 -0700803 /**
804 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
805 * bounds were updated.
806 */
807 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700808 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
809 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -0700810 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -0700811 changedStackList.add(stack.mStackId);
812 }
813 }
814 }
815
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700816 @Override
817 boolean fillsParent() {
818 return true;
819 }
820
821 @Override
822 boolean isVisible() {
823 return true;
824 }
825
826 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700827 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -0700828 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000829 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700830 }
831
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700832 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800833 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
834 // Special handling so we can process IME windows with #forAllImeWindows above their IME
835 // target, or here in order if there isn't an IME target.
836 if (traverseTopToBottom) {
837 for (int i = mChildren.size() - 1; i >= 0; --i) {
838 final DisplayChildWindowContainer child = mChildren.get(i);
839 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
840 // In this case the Ime windows will be processed above their target so we skip
841 // here.
842 continue;
843 }
844 if (child.forAllWindows(callback, traverseTopToBottom)) {
845 return true;
846 }
847 }
848 } else {
849 final int count = mChildren.size();
850 for (int i = 0; i < count; i++) {
851 final DisplayChildWindowContainer child = mChildren.get(i);
852 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
853 // In this case the Ime windows will be processed above their target so we skip
854 // here.
855 continue;
856 }
857 if (child.forAllWindows(callback, traverseTopToBottom)) {
858 return true;
859 }
860 }
861 }
862 return false;
863 }
864
865 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
866 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
867 }
868
869 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -0700870 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700871 final WindowManagerPolicy policy = mService.mPolicy;
872
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700873 if (mService.mDisplayFrozen) {
874 if (mService.mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
875 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
876 "Display is frozen, return " + mService.mLastWindowForcedOrientation);
877 // If the display is frozen, some activities may be in the middle of restarting, and
878 // thus have removed their old window. If the window has the flag to hide the lock
879 // screen, then the lock screen can re-appear and inflict its own orientation on us.
880 // Keep the orientation stable until this all settles down.
881 return mService.mLastWindowForcedOrientation;
882 } else if (policy.isKeyguardLocked()) {
883 // Use the last orientation the while the display is frozen with the keyguard
884 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
885 // window. We don't want to check the show when locked window directly though as
886 // things aren't stable while the display is frozen, for example the window could be
887 // momentarily unavailable due to activity relaunch.
888 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
889 + "return " + mService.mLastOrientation);
890 return mService.mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -0700891 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700892 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000893 final int orientation = mAboveAppWindowsContainers.getOrientation();
894 if (orientation != SCREEN_ORIENTATION_UNSET) {
895 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700896 }
Wale Ogunwale51362492016-09-08 17:49:17 -0700897 }
898
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700899 // Top system windows are not requesting an orientation. Start searching from apps.
900 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -0700901 }
902
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700903 void updateDisplayInfo() {
Craig Mautner722285e2012-09-07 13:55:58 -0700904 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700905 mDisplay.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700906 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
907 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800908 }
Craig Mautner722285e2012-09-07 13:55:58 -0700909 }
910
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700911 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700912 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
913 if (displayManagerInternal != null) {
914 // Bootstrap the default logical display from the display manager.
915 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
916 if (newDisplayInfo != null) {
917 mDisplayInfo.copyFrom(newDisplayInfo);
918 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700919 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700920
Filip Gruszczynski608797e2015-11-12 19:08:20 -0800921 mBaseDisplayWidth = mInitialDisplayWidth = mDisplayInfo.logicalWidth;
922 mBaseDisplayHeight = mInitialDisplayHeight = mDisplayInfo.logicalHeight;
923 mBaseDisplayDensity = mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
924 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700925 }
926
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700927 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700928 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800929 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700930 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800931 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
932 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700933 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800934 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700935 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800936 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700937 out.set(left, top, left + width, top + height);
938 }
939
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700940 private void getLogicalDisplayRect(Rect out, int orientation) {
941 getLogicalDisplayRect(out);
942
943 // Rotate the Rect if needed.
944 final int currentRotation = mDisplayInfo.rotation;
945 final int rotationDelta = deltaRotation(currentRotation, orientation);
946 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
947 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
948 mTmpRectF.set(out);
949 mTmpMatrix.mapRect(mTmpRectF);
950 mTmpRectF.round(out);
951 }
952 }
953
Chong Zhangf66db432016-01-13 10:39:51 -0800954 void getContentRect(Rect out) {
955 out.set(mContentRect);
956 }
957
Andrii Kulian839def92016-11-02 10:58:58 -0700958 /**
959 * Adds the stack to this display.
960 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
961 */
962 Rect addStackToDisplay(int stackId, boolean onTop) {
963 boolean attachedToDisplay = false;
964 TaskStack stack = mService.mStackIdToStack.get(stackId);
965 if (stack == null) {
966 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
967 + mDisplayId);
968
969 stack = getStackById(stackId);
970 if (stack != null) {
971 // It's already attached to the display...clear mDeferRemoval and move stack to
972 // appropriate z-order on display as needed.
973 stack.mDeferRemoval = false;
Andrii Kuliand2765632016-12-12 22:26:34 -0800974 // We're not moving the display to front when we're adding stacks, only when
975 // requested to change the position of stack explicitly.
976 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
977 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -0700978 attachedToDisplay = true;
979 } else {
980 stack = new TaskStack(mService, stackId);
981 }
982
983 mService.mStackIdToStack.put(stackId, stack);
984 if (stackId == DOCKED_STACK_ID) {
985 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
986 }
987 } else {
988 final DisplayContent currentDC = stack.getDisplayContent();
989 if (currentDC != null) {
990 throw new IllegalStateException("Trying to add stackId=" + stackId
991 + "to displayId=" + mDisplayId + ", but it's already attached to displayId="
992 + currentDC.getDisplayId());
993 }
994 }
995
996 if (!attachedToDisplay) {
997 mTaskStackContainers.addStackToDisplay(stack, onTop);
998 }
999
1000 if (stack.getRawFullscreen()) {
1001 return null;
1002 }
1003 final Rect bounds = new Rect();
1004 stack.getRawBounds(bounds);
1005 return bounds;
1006 }
1007
1008 /** Removes the stack from the display and prepares for changing the parent. */
1009 private void removeStackFromDisplay(TaskStack stack) {
1010 mTaskStackContainers.removeStackFromDisplay(stack);
1011 }
1012
1013 /** Moves the stack to this display and returns the updated bounds. */
1014 Rect moveStackToDisplay(TaskStack stack) {
1015 final DisplayContent currentDisplayContent = stack.getDisplayContent();
1016 if (currentDisplayContent == null) {
1017 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1018 + " which is not currently attached to any display");
1019 }
1020 if (stack.getDisplayContent().getDisplayId() == mDisplayId) {
1021 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1022 + " to its current displayId=" + mDisplayId);
1023 }
1024
1025 currentDisplayContent.removeStackFromDisplay(stack);
1026 return addStackToDisplay(stack.mStackId, true /* onTop */);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001027 }
1028
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001029 @Override
1030 protected void addChild(DisplayChildWindowContainer child,
1031 Comparator<DisplayChildWindowContainer> comparator) {
1032 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1033 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001034
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001035 @Override
1036 protected void addChild(DisplayChildWindowContainer child, int index) {
1037 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1038 }
1039
1040 @Override
1041 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001042 // Only allow removal of direct children from this display if the display is in the process
1043 // of been removed.
1044 if (mRemovingDisplay) {
1045 super.removeChild(child);
1046 return;
1047 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001048 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001049 }
1050
Andrii Kuliand2765632016-12-12 22:26:34 -08001051 @Override
1052 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1053 // Children of the display are statically ordered, so the real intention here is to perform
1054 // the operation on the display and not the static direct children.
1055 getParent().positionChildAt(position, this, includingParents);
1056 }
1057
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001058 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001059 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1060 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001061 final int taskId = stack.taskIdFromPoint(x, y);
1062 if (taskId != -1) {
1063 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001064 }
1065 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001066 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001067 }
1068
Chong Zhang8e89b312015-09-09 15:09:30 -07001069 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001070 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001071 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001072 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001073 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001074 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001075 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001076 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1077 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001078 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001079 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001080 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001081
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001082 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1083 if (mTmpTaskForResizePointSearchResult.searchDone) {
1084 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001085 }
1086 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001087 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001088 }
1089
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001090 void setTouchExcludeRegion(Task focusedTask) {
Craig Mautner6601b7b2013-04-29 10:29:11 -07001091 mTouchExcludeRegion.set(mBaseDisplayRect);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001092 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001093 mTmpRect2.setEmpty();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001094 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1095 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001096 stack.setTouchExcludeRegion(
1097 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
Craig Mautner6601b7b2013-04-29 10:29:11 -07001098 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001099 // If we removed the focused task above, add it back and only leave its
1100 // outside touch area in the exclusion. TapDectector is not interested in
1101 // any touch inside the focused task itself.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001102 if (!mTmpRect2.isEmpty()) {
Chong Zhangd8ceb852015-11-11 14:53:41 -08001103 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1104 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001105 final WindowState inputMethod = mService.mInputMethodWindow;
1106 if (inputMethod != null && inputMethod.isVisibleLw()) {
1107 // If the input method is visible and the user is typing, we don't want these touch
1108 // events to be intercepted and used to change focus. This would likely cause a
1109 // disappearance of the input method.
1110 inputMethod.getTouchableRegion(mTmpRegion);
1111 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1112 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001113 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1114 WindowState win = mTapExcludedWindows.get(i);
1115 win.getTouchableRegion(mTmpRegion);
1116 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1117 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001118 // TODO(multi-display): Support docked stacks on secondary displays.
1119 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001120 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001121 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001122 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1123 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001124 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001125 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001126 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001127 }
1128
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001129 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001130 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001131 super.switchUser();
1132 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001133 }
1134
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001135 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001136 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1137 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001138 }
1139 }
1140
1141 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001142 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001143 }
1144
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001145 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001146 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001147 }
1148
1149 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001150 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001151 }
1152
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001153 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001154 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001155 }
1156
Wale Ogunwale10124582016-09-15 20:25:50 -07001157 @Override
1158 void removeIfPossible() {
1159 if (isAnimating()) {
1160 mDeferredRemoval = true;
1161 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001162 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001163 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001164 }
1165
Wale Ogunwale10124582016-09-15 20:25:50 -07001166 @Override
1167 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001168 mRemovingDisplay = true;
1169 try {
1170 super.removeImmediately();
1171 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1172 mDimLayerController.close();
1173 if (mDisplayId == DEFAULT_DISPLAY) {
1174 mService.unregisterPointerEventListener(mTapDetector);
1175 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1176 }
1177 } finally {
1178 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001179 }
Craig Mautner95da1082014-02-24 17:54:35 -08001180 }
1181
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001182 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001183 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001184 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001185 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1186
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001187 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001188 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001189 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001190 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001191 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001192 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001193 }
1194
Wale Ogunwale10124582016-09-15 20:25:50 -07001195 boolean animateForIme(float interpolatedValue, float animationTarget,
1196 float dividerAnimationTarget) {
1197 boolean updated = false;
1198
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001199 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1200 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001201 if (stack == null || !stack.isAdjustedForIme()) {
1202 continue;
1203 }
1204
1205 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1206 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1207 updated = true;
1208 } else {
1209 mDividerControllerLocked.mLastAnimationProgress =
1210 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1211 mDividerControllerLocked.mLastDividerProgress =
1212 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1213 updated |= stack.updateAdjustForIme(
1214 mDividerControllerLocked.mLastAnimationProgress,
1215 mDividerControllerLocked.mLastDividerProgress,
1216 false /* force */);
1217 }
1218 if (interpolatedValue >= 1f) {
1219 stack.endImeAdjustAnimation();
1220 }
1221 }
1222
1223 return updated;
1224 }
1225
1226 boolean clearImeAdjustAnimation() {
1227 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001228 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1229 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001230 if (stack != null && stack.isAdjustedForIme()) {
1231 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1232 changed = true;
1233 }
1234 }
1235 return changed;
1236 }
1237
1238 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001239 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1240 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001241 if (stack.isVisible() && stack.isAdjustedForIme()) {
1242 stack.beginImeAdjustAnimation();
1243 }
1244 }
1245 }
1246
1247 void adjustForImeIfNeeded() {
1248 final WindowState imeWin = mService.mInputMethodWindow;
1249 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
1250 && !mDividerControllerLocked.isImeHideRequested();
1251 final boolean dockVisible = mService.isStackVisibleLocked(DOCKED_STACK_ID);
1252 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
1253 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
1254 imeTargetStack.getDockSide() : DOCKED_INVALID;
1255 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
1256 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
1257 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
1258 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
1259 final boolean imeHeightChanged = imeVisible &&
1260 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
1261
1262 // The divider could be adjusted for IME position, or be thinner than usual,
1263 // or both. There are three possible cases:
1264 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
1265 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
1266 // - If IME is not visible, divider is not moved and is normal width.
1267
1268 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001269 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1270 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001271 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
1272 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)) {
1273 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
1274 } else {
1275 stack.resetAdjustedForIme(false);
1276 }
1277 }
1278 mDividerControllerLocked.setAdjustedForIme(
1279 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
1280 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001281 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1282 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001283 stack.resetAdjustedForIme(!dockVisible);
1284 }
1285 mDividerControllerLocked.setAdjustedForIme(
1286 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
1287 }
Winson Chung655332c2016-10-31 13:14:28 -07001288 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07001289 }
1290
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001291 void setInputMethodAnimLayerAdjustment(int adj) {
1292 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
1293 mInputMethodAnimLayerAdjustment = adj;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08001294 mImeWindowsContainers.forAllWindows(mSetInputMethodAnimLayerAdjustment,
1295 true /* traverseTopToBottom */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001296 }
1297
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001298 /**
1299 * If a window that has an animation specifying a colored background and the current wallpaper
1300 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
1301 * suddenly disappear.
1302 */
1303 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001304 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
1305 w -> w.mIsWallpaper && w.isVisibleNow());
1306
1307 if (visibleWallpaper != null) {
1308 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001309 }
1310 return winAnimator.mAnimLayer;
1311 }
1312
Wale Ogunwale10124582016-09-15 20:25:50 -07001313 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001314 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1315 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07001316 stack.prepareFreezingTaskBounds();
1317 }
1318 }
1319
Wale Ogunwale94744212015-09-21 19:01:47 -07001320 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001321 getLogicalDisplayRect(mTmpRect, newRotation);
1322
1323 // Compute a transform matrix to undo the coordinate space transformation,
1324 // and present the window at the same physical position it previously occupied.
1325 final int deltaRotation = deltaRotation(newRotation, oldRotation);
1326 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
1327
1328 mTmpRectF.set(bounds);
1329 mTmpMatrix.mapRect(mTmpRectF);
1330 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07001331 }
1332
Wale Ogunwale4a02d812015-02-12 23:01:38 -08001333 static int deltaRotation(int oldRotation, int newRotation) {
1334 int delta = newRotation - oldRotation;
1335 if (delta < 0) delta += 4;
1336 return delta;
1337 }
1338
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001339 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001340 Matrix outMatrix) {
1341 // For rotations without Z-ordering we don't need the target rectangle's position.
1342 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
1343 displayHeight, outMatrix);
1344 }
1345
1346 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
1347 float displayWidth, float displayHeight, Matrix outMatrix) {
1348 switch (rotation) {
1349 case ROTATION_0:
1350 outMatrix.reset();
1351 break;
1352 case ROTATION_270:
1353 outMatrix.setRotate(270, 0, 0);
1354 outMatrix.postTranslate(0, displayHeight);
1355 outMatrix.postTranslate(rectTop, 0);
1356 break;
1357 case ROTATION_180:
1358 outMatrix.reset();
1359 break;
1360 case ROTATION_90:
1361 outMatrix.setRotate(90, 0, 0);
1362 outMatrix.postTranslate(displayWidth, 0);
1363 outMatrix.postTranslate(-rectTop, rectLeft);
1364 break;
1365 }
1366 }
1367
Craig Mautnera91f9e22012-09-14 16:22:08 -07001368 public void dump(String prefix, PrintWriter pw) {
1369 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
1370 final String subPrefix = " " + prefix;
1371 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
1372 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
1373 pw.print("dpi");
1374 if (mInitialDisplayWidth != mBaseDisplayWidth
1375 || mInitialDisplayHeight != mBaseDisplayHeight
1376 || mInitialDisplayDensity != mBaseDisplayDensity) {
1377 pw.print(" base=");
1378 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
1379 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
1380 }
Jeff Brownd46747a2015-04-15 19:02:36 -07001381 if (mDisplayScalingDisabled) {
1382 pw.println(" noscale");
1383 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07001384 pw.print(" cur=");
1385 pw.print(mDisplayInfo.logicalWidth);
1386 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
1387 pw.print(" app=");
1388 pw.print(mDisplayInfo.appWidth);
1389 pw.print("x"); pw.print(mDisplayInfo.appHeight);
1390 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
1391 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
1392 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
1393 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001394 pw.println(subPrefix + "deferred=" + mDeferredRemoval
1395 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001396
Craig Mautnerdc548482014-02-05 13:35:24 -08001397 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001398 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001399 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1400 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001401 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001402 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001403
Craig Mautnerdc548482014-02-05 13:35:24 -08001404 pw.println();
1405 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001406 pw.println();
1407 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001408 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001409 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001410 pw.print(" Exiting #"); pw.print(i);
1411 pw.print(' '); pw.print(token);
1412 pw.println(':');
1413 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001414 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001415 }
Craig Mautner59c00972012-07-30 12:10:24 -07001416 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001417 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07001418 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001419 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07001420 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001421 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001422
1423 if (mInputMethodAnimLayerAdjustment != 0) {
1424 pw.println(subPrefix
1425 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
1426 }
Craig Mautner59c00972012-07-30 12:10:24 -07001427 }
Craig Mautnere0a38842013-12-16 16:14:02 -08001428
1429 @Override
1430 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001431 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001432 }
1433
1434 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001435 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08001436 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07001437
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001438 /**
1439 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
1440 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07001441 TaskStack getDockedStackLocked() {
Wale Ogunwalee45899a2015-10-01 11:30:34 -07001442 final TaskStack stack = mService.mStackIdToStack.get(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001443 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07001444 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001445
1446 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001447 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02001448 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001449 */
Jorim Jaggife762342016-10-13 14:33:27 +02001450 TaskStack getDockedStackIgnoringVisibility() {
1451 return mService.mStackIdToStack.get(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001452 }
1453
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001454 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001455 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001456 final int x = (int) xf;
1457 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001458 final WindowState touchedWin = getWindow(w -> {
1459 final int flags = w.mAttrs.flags;
1460 if (!w.isVisibleLw()) {
1461 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001462 }
1463 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001464 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001465 }
1466
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001467 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001468 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001469 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001470 }
1471
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001472 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001473
1474 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001475 return mTmpRegion.contains(x, y) || touchFlags == 0;
1476 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001477
1478 return touchedWin;
1479 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07001480
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001481 boolean canAddToastWindowForUid(int uid) {
1482 // We allow one toast window per UID being shown at a time.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001483 final WindowState win = getWindow(w ->
1484 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
1485 && !w.mWindowRemovalAllowed);
1486 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001487 }
1488
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001489 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001490 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
1491 return;
1492 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001493
Wale Ogunwale1e129a42016-11-21 13:03:47 -08001494 // Used to communicate the old focus to the callback method.
1495 mTmpWindow = oldFocus;
1496
1497 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001498 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001499
1500 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001501 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001502
Wale Ogunwale1e129a42016-11-21 13:03:47 -08001503 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001504
1505 if (mTmpWindow == null) {
1506 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
1507 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001508 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001509 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001510 }
1511
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001512 /** Updates the layer assignment of windows on this display. */
1513 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08001514 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001515 if (setLayoutNeeded) {
1516 setLayoutNeeded();
1517 }
1518 }
1519
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001520 void layoutAndAssignWindowLayersIfNeeded() {
1521 mService.mWindowsChanged = true;
1522 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001523
1524 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
1525 false /*updateInputWindows*/)) {
1526 assignWindowLayers(false /* setLayoutNeeded */);
1527 }
1528
1529 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
1530 mService.mWindowPlacerLocked.performSurfacePlacement();
1531 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
1532 }
1533
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001534 /** Returns true if a leaked surface was destroyed */
1535 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001536 // Used to indicate that a surface was leaked.
1537 mTmpWindow = null;
1538 forAllWindows(w -> {
1539 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001540 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001541 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001542 }
1543 if (!mService.mSessions.contains(wsa.mSession)) {
1544 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001545 + w + " surface=" + wsa.mSurfaceController
1546 + " token=" + w.mToken
1547 + " pid=" + w.mSession.mPid
1548 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001549 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001550 mService.mForceRemoves.add(w);
1551 mTmpWindow = w;
1552 } else if (w.mAppToken != null && w.mAppToken.clientHidden) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001553 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001554 + w + " surface=" + wsa.mSurfaceController
1555 + " token=" + w.mAppToken
1556 + " saved=" + w.hasSavedSurface());
1557 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001558 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001559 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001560 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001561 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001562
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001563 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001564 }
1565
1566 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001567 * Determine and return the window that should be the IME target.
1568 * @param updateImeTarget If true the system IME target will be updated to match what we found.
1569 * @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 +00001570 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001571 WindowState computeImeTarget(boolean updateImeTarget) {
1572 if (mService.mInputMethodWindow == null) {
1573 // There isn't an IME so there shouldn't be a target...That was easy!
1574 if (updateImeTarget) {
1575 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
1576 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
1577 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
1578 }
1579 return null;
1580 }
1581
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001582 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
1583 // same display. Or even when the current IME/target are not on the same screen as the next
1584 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08001585 mUpdateImeTarget = updateImeTarget;
1586 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001587
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001588
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001589 // Yet more tricksyness! If this window is a "starting" window, we do actually want
1590 // to be on top of it, but it is not -really- where input will go. So look down below
1591 // for a real window to target...
1592 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
1593 final AppWindowToken token = target.mAppToken;
1594 if (token != null) {
1595 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
1596 if (betterTarget != null) {
1597 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001598 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001599 }
1600 }
1601
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001602 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
1603 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001604
1605 // Now, a special case -- if the last target's window is in the process of exiting, and is
1606 // above the new target, keep on the last target to avoid flicker. Consider for example a
1607 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
1608 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
1609 // scrim.
1610 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001611 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
1612 && (target == null
1613 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001614 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001615 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001616 }
1617
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001618 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
1619 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001620
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001621 if (target == null) {
1622 if (updateImeTarget) {
1623 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
1624 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
1625 + Debug.getCallers(4) : ""));
1626 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
1627 }
1628
1629 return null;
1630 }
1631
1632 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001633 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
1634 if (token != null) {
1635
1636 // Now some fun for dealing with window animations that modify the Z order. We need
1637 // to look at all windows below the current target that are in this app, finding the
1638 // highest visible one in layering.
1639 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001640 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001641 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001642 }
1643
1644 if (highestTarget != null) {
1645 final AppTransition appTransition = mService.mAppTransition;
1646 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
1647 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
1648 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001649 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001650
1651 if (appTransition.isTransitionSet()) {
1652 // If we are currently setting up for an animation, hold everything until we
1653 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001654 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
1655 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001656 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001657 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001658 // If the window we are currently targeting is involved with an animation,
1659 // and it is on top of the next target we will be over, then hold off on
1660 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001661 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
1662 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001663 }
1664 }
1665 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001666
1667 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
1668 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
1669 setInputMethodTarget(target, false, target.mAppToken != null
1670 ? target.mAppToken.mAppAnimator.animLayerAdjustment : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001671 }
1672
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001673 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001674 }
1675
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001676 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
1677 if (target == mService.mInputMethodTarget
1678 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
1679 && mInputMethodAnimLayerAdjustment == layerAdj) {
1680 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001681 }
1682
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001683 mService.mInputMethodTarget = target;
1684 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
1685 setInputMethodAnimLayerAdjustment(layerAdj);
1686 assignWindowLayers(false /* setLayoutNeeded */);
1687 }
1688
1689 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
1690 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
1691 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
1692 }
1693
1694 // Used to indicate we have reached the first window in the range we are interested in.
1695 mTmpWindow = null;
1696
1697 // TODO: Figure-out a more efficient way to do this.
1698 final WindowState candidate = getWindow(w -> {
1699 if (w == top) {
1700 // Reached the first window in the range we are interested in.
1701 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001702 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001703 if (mTmpWindow == null) {
1704 return false;
1705 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001706
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001707 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
1708 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001709 }
1710 // If we reached the bottom of the range of windows we are considering,
1711 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001712 if (w == bottom) {
1713 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001714 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001715 return false;
1716 });
1717
1718 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001719 }
1720
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001721 void setLayoutNeeded() {
1722 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
1723 mLayoutNeeded = true;
1724 }
1725
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001726 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001727 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
1728 mLayoutNeeded = false;
1729 }
1730
1731 boolean isLayoutNeeded() {
1732 return mLayoutNeeded;
1733 }
1734
Wale Ogunwale02319a62016-09-26 15:21:22 -07001735 void dumpTokens(PrintWriter pw, boolean dumpAll) {
1736 if (mTokenMap.isEmpty()) {
1737 return;
1738 }
1739 pw.println(" Display #" + mDisplayId);
1740 final Iterator<WindowToken> it = mTokenMap.values().iterator();
1741 while (it.hasNext()) {
1742 final WindowToken token = it.next();
1743 pw.print(" ");
1744 pw.print(token);
1745 if (dumpAll) {
1746 pw.println(':');
1747 token.dump(pw, " ");
1748 } else {
1749 pw.println();
1750 }
1751 }
1752 }
1753
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001754 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001755 final int[] index = new int[1];
1756 forAllWindows(w -> {
1757 final WindowStateAnimator wAnim = w.mWinAnimator;
1758 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
1759 index[0] = index[0] + 1;
1760 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001761 }
1762
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001763 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001764 forAllWindows(w -> {
1765 w.mWinAnimator.enableSurfaceTrace(fd);
1766 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001767 }
1768
1769 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001770 forAllWindows(w -> {
1771 w.mWinAnimator.disableSurfaceTrace();
1772 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001773 }
1774
Jorim Jaggife762342016-10-13 14:33:27 +02001775 /**
1776 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
1777 */
1778 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
1779 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001780 forAllWindows(w -> {
1781 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)) {
1782 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02001783 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
1784 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001785 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02001786 }
1787
Wale Ogunwale494009b82016-10-21 09:01:38 -07001788 boolean checkWaitingForWindows() {
1789
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001790 mHaveBootMsg = false;
1791 mHaveApp = false;
1792 mHaveWallpaper = false;
1793 mHaveKeyguard = true;
1794
1795 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07001796 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
1797 return true;
1798 }
1799 if (w.isDrawnLw()) {
1800 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001801 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001802 } else if (w.mAttrs.type == TYPE_APPLICATION
1803 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001804 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001805 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001806 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001807 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001808 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07001809 }
1810 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001811 return false;
1812 });
1813
1814 if (visibleWindow != null) {
1815 // We have a visible window.
1816 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001817 }
1818
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001819 // if the wallpaper service is disabled on the device, we're never going to have
1820 // wallpaper, don't bother waiting for it
1821 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
1822 com.android.internal.R.bool.config_enableWallpaperService)
1823 && !mService.mOnlyCore;
1824
Wale Ogunwale494009b82016-10-21 09:01:38 -07001825 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
1826 "******** booted=" + mService.mSystemBooted
1827 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001828 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
1829 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
1830 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07001831
1832 // If we are turning on the screen to show the boot message, don't do it until the boot
1833 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001834 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07001835 return true;
1836 }
1837
1838 // If we are turning on the screen after the boot is completed normally, don't do so until
1839 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001840 if (mService.mSystemBooted
1841 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07001842 return true;
1843 }
1844
1845 return false;
1846 }
1847
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001848 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08001849 mTmpWindowAnimator = animator;
1850 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001851 }
1852
1853 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07001854 resetAnimationBackgroundAnimator();
1855
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001856 // Used to indicate a detached wallpaper.
1857 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08001858 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001859
Wale Ogunwale1e129a42016-11-21 13:03:47 -08001860 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001861
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001862 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001863 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001864 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
1865 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001866 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
1867 }
1868 }
1869
1870 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08001871 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001872 }
1873
Wale Ogunwale494009b82016-10-21 09:01:38 -07001874 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001875 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001876 if (imFocus == null) {
1877 return false;
1878 }
1879
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001880 if (DEBUG_INPUT_METHOD) {
1881 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
1882 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
1883 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001884 }
1885
Wale Ogunwale494009b82016-10-21 09:01:38 -07001886 final IInputMethodClient imeClient = imFocus.mSession.mClient;
1887
1888 if (DEBUG_INPUT_METHOD) {
1889 Slog.i(TAG_WM, "IM target client: " + imeClient);
1890 if (imeClient != null) {
1891 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
1892 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
1893 }
1894 }
1895
1896 return imeClient != null && imeClient.asBinder() == client.asBinder();
1897 }
1898
1899 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001900 final WindowState win = getWindow(
1901 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
1902 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001903 }
1904
1905 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001906 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07001907 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001908 final int curValue = w.mSystemUiVisibility;
1909 final int diff = (curValue ^ visibility) & globalDiff;
1910 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07001911 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001912 w.mSeq++;
1913 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001914 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001915 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
1916 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07001917 visibility, newValue, diff);
1918 }
1919 } catch (RemoteException e) {
1920 // so sorry
1921 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001922 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07001923 }
1924
1925 void onWindowFreezeTimeout() {
1926 Slog.w(TAG_WM, "Window freeze timeout expired.");
1927 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001928
1929 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07001930 if (!w.mOrientationChanging) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001931 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001932 }
1933 w.mOrientationChanging = false;
1934 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
1935 - mService.mDisplayFreezeTime);
1936 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001937 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07001938 mService.mWindowPlacerLocked.performSurfacePlacement();
1939 }
1940
1941 void waitForAllWindowsDrawn() {
1942 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001943 forAllWindows(w -> {
1944 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
1945 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
1946 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001947 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001948 w.mLastContentInsets.set(-1, -1, -1, -1);
1949 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07001950 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001951 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07001952 }
1953
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001954 // TODO: Super crazy long method that should be broken down...
1955 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
1956
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001957 final int dw = mDisplayInfo.logicalWidth;
1958 final int dh = mDisplayInfo.logicalHeight;
1959 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
1960
1961 mTmpUpdateAllDrawn.clear();
1962
1963 int repeats = 0;
1964 do {
1965 repeats++;
1966 if (repeats > 6) {
1967 Slog.w(TAG, "Animation repeat aborted after too many iterations");
1968 clearLayoutNeeded();
1969 break;
1970 }
1971
1972 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
1973 pendingLayoutChanges);
1974
Andrii Kulian839def92016-11-02 10:58:58 -07001975 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
1976 // the wallpaper window jumping across displays.
1977 // Remove check for default display when there will be support for multiple wallpaper
1978 // targets (on different displays).
1979 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001980 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001981 }
1982
1983 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
1984 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
1985 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
1986 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07001987 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001988 }
1989 }
1990
1991 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
1992 setLayoutNeeded();
1993 }
1994
1995 // FIRST LOOP: Perform a layout, if needed.
1996 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
1997 performLayout(repeats == 1, false /* updateInputWindows */);
1998 } else {
1999 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2000 }
2001
2002 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2003 pendingLayoutChanges = 0;
2004
2005 if (isDefaultDisplay) {
2006 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002007 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002008 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2009 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2010 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2011 }
2012 } while (pendingLayoutChanges != 0);
2013
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002014 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002015 resetDimming();
2016
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002017 mTmpRecoveringMemory = recoveringMemory;
2018 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002019
2020 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002021 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2022 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2023 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002024 true /* inTraversal, must call performTraversalInTrans... below */);
2025
2026 stopDimmingIfNeeded();
2027
2028 while (!mTmpUpdateAllDrawn.isEmpty()) {
2029 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2030 // See if any windows have been drawn, so they (and others associated with them)
2031 // can now be shown.
2032 atoken.updateAllDrawn(this);
2033 }
2034
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002035 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002036 }
2037
2038 void performLayout(boolean initial, boolean updateInputWindows) {
2039 if (!isLayoutNeeded()) {
2040 return;
2041 }
2042 clearLayoutNeeded();
2043
2044 final int dw = mDisplayInfo.logicalWidth;
2045 final int dh = mDisplayInfo.logicalHeight;
2046
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002047 if (DEBUG_LAYOUT) {
2048 Slog.v(TAG, "-------------------------------------");
2049 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2050 }
2051
2052 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mService.mRotation,
2053 getConfiguration().uiMode);
2054 if (isDefaultDisplay) {
2055 // Not needed on non-default displays.
2056 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2057 mService.mScreenRect.set(0, 0, dw, dh);
2058 }
2059
2060 mService.mPolicy.getContentRectLw(mContentRect);
2061
2062 int seq = mService.mLayoutSeq + 1;
2063 if (seq < 0) seq = 0;
2064 mService.mLayoutSeq = seq;
2065
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002066 // Used to indicate that we have processed the dream window and all additional windows are
2067 // behind it.
2068 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002069 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002070
2071 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002072 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002073
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002074 // Used to indicate that we have processed the dream window and all additional attached
2075 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002076 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002077 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002078
2079 // Now perform layout of attached windows, which usually depend on the position of the
2080 // window they are attached to. XXX does not deal with windows that are attached to windows
2081 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002082 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002083
2084 // Window frames may have changed. Tell the input dispatcher about it.
2085 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2086 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2087 if (updateInputWindows) {
2088 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2089 }
2090
2091 mService.mPolicy.finishLayoutLw();
2092 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2093 }
2094
2095 /**
2096 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2097 * In portrait mode, it grabs the full screenshot.
2098 *
2099 * @param width the width of the target bitmap
2100 * @param height the height of the target bitmap
2101 * @param includeFullDisplay true if the screen should not be cropped before capture
2102 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2103 * @param config of the output bitmap
2104 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
2105 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002106 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002107 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
2108 boolean wallpaperOnly) {
2109 int dw = mDisplayInfo.logicalWidth;
2110 int dh = mDisplayInfo.logicalHeight;
2111 if (dw == 0 || dh == 0) {
2112 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2113 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2114 return null;
2115 }
2116
2117 Bitmap bm = null;
2118
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002119 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002120 final Rect frame = new Rect();
2121 final Rect stackBounds = new Rect();
2122
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002123 boolean includeImeInScreenshot;
2124 synchronized(mService.mWindowMap) {
2125 final AppWindowToken imeTargetAppToken = mService.mInputMethodTarget != null
2126 ? mService.mInputMethodTarget.mAppToken : null;
2127 // We only include the Ime in the screenshot if the app we are screenshoting is the IME
2128 // target and isn't in multi-window mode. We don't screenshot the IME in multi-window
2129 // mode because the frame of the IME might not overlap with that of the app.
2130 // E.g. IME target app at the top in split-screen mode and the IME at the bottom
2131 // overlapping with the bottom app.
2132 includeImeInScreenshot = imeTargetAppToken != null
2133 && imeTargetAppToken.appToken != null
2134 && imeTargetAppToken.appToken.asBinder() == appToken
2135 && !mService.mInputMethodTarget.isInMultiWindowMode();
2136 }
2137
2138 final int aboveAppLayer = (mService.mPolicy.windowTypeToLayerLw(TYPE_APPLICATION) + 1)
2139 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
2140
2141 synchronized(mService.mWindowMap) {
2142 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002143 mScreenshotApplicationState.appWin = null;
2144 forAllWindows(w -> {
2145 if (!w.mHasSurface) {
2146 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002147 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002148 if (w.mLayer >= aboveAppLayer) {
2149 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002150 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002151 if (wallpaperOnly && !w.mIsWallpaper) {
2152 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002153 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002154 if (w.mIsImWindow) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002155 if (!includeImeInScreenshot) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002156 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002157 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002158 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002159 // If this is the wallpaper layer and we're only looking for the wallpaper layer
2160 // then the target window state is this one.
2161 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002162 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002163 }
2164
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002165 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002166 // We have not ran across the target window yet, so it is probably behind
2167 // the wallpaper. This can happen when the keyguard is up and all windows
2168 // are moved behind the wallpaper. We don't want to include the wallpaper
2169 // layer in the screenshot as it will cover-up the layer of the target
2170 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002171 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002172 }
2173 // Fall through. The target window is in front of the wallpaper. For this
2174 // case we want to include the wallpaper layer in the screenshot because
2175 // the target window might have some transparent areas.
2176 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002177 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002178 // This app window is of no interest if it is not associated with the
2179 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002180 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002181 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002182 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002183 }
2184
2185 // Include this window.
2186
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002187 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002188 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002189 if (mScreenshotApplicationState.maxLayer < layer) {
2190 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002191 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002192 if (mScreenshotApplicationState.minLayer > layer) {
2193 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002194 }
2195
2196 // Don't include wallpaper in bounds calculation
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002197 if (!includeFullDisplay && !w.mIsWallpaper) {
2198 final Rect wf = w.mFrame;
2199 final Rect cr = w.mContentInsets;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002200 int left = wf.left + cr.left;
2201 int top = wf.top + cr.top;
2202 int right = wf.right - cr.right;
2203 int bottom = wf.bottom - cr.bottom;
2204 frame.union(left, top, right, bottom);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002205 w.getVisibleBounds(stackBounds);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002206 if (!Rect.intersects(frame, stackBounds)) {
2207 // Set frame empty if there's no intersection.
2208 frame.setEmpty();
2209 }
2210 }
2211
2212 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002213 (w.mAppToken != null && w.mAppToken.token == appToken)
2214 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
2215 if (foundTargetWs && w.isDisplayedLw() && winAnim.getShown()) {
2216 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002217 }
2218
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002219 if (w.isObscuringDisplay()){
2220 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002221 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002222 return false;
2223 }, true /* traverseTopToBottom */);
2224
2225 final WindowState appWin = mScreenshotApplicationState.appWin;
2226 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
2227 final int maxLayer = mScreenshotApplicationState.maxLayer;
2228 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002229
2230 if (appToken != null && appWin == null) {
2231 // Can't find a window to snapshot.
2232 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
2233 "Screenshot: Couldn't find a surface matching " + appToken);
2234 return null;
2235 }
2236
2237 if (!screenshotReady) {
2238 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
2239 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
2240 appWin.mWinAnimator.mDrawState)));
2241 return null;
2242 }
2243
2244 // Screenshot is ready to be taken. Everything from here below will continue
2245 // through the bottom of the loop and return a value. We only stay in the loop
2246 // because we don't want to release the mWindowMap lock until the screenshot is
2247 // taken.
2248
2249 if (maxLayer == 0) {
2250 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2251 + ": returning null maxLayer=" + maxLayer);
2252 return null;
2253 }
2254
2255 if (!includeFullDisplay) {
2256 // Constrain frame to the screen size.
2257 if (!frame.intersect(0, 0, dw, dh)) {
2258 frame.setEmpty();
2259 }
2260 } else {
2261 // Caller just wants entire display.
2262 frame.set(0, 0, dw, dh);
2263 }
2264 if (frame.isEmpty()) {
2265 return null;
2266 }
2267
2268 if (width < 0) {
2269 width = (int) (frame.width() * frameScale);
2270 }
2271 if (height < 0) {
2272 height = (int) (frame.height() * frameScale);
2273 }
2274
2275 // Tell surface flinger what part of the image to crop. Take the top
2276 // right part of the application, and crop the larger dimension to fit.
2277 Rect crop = new Rect(frame);
2278 if (width / (float) frame.width() < height / (float) frame.height()) {
2279 int cropWidth = (int)((float)width / (float)height * frame.height());
2280 crop.right = crop.left + cropWidth;
2281 } else {
2282 int cropHeight = (int)((float)height / (float)width * frame.width());
2283 crop.bottom = crop.top + cropHeight;
2284 }
2285
2286 // The screenshot API does not apply the current screen rotation.
2287 int rot = mDisplay.getRotation();
2288
2289 if (rot == ROTATION_90 || rot == ROTATION_270) {
2290 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
2291 }
2292
2293 // Surfaceflinger is not aware of orientation, so convert our logical
2294 // crop to surfaceflinger's portrait orientation.
2295 convertCropForSurfaceFlinger(crop, rot, dw, dh);
2296
2297 if (DEBUG_SCREENSHOT) {
2298 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
2299 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002300 forAllWindows(w -> {
2301 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
2302 Slog.i(TAG_WM, w + ": " + w.mLayer
2303 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002304 + " surfaceLayer=" + ((controller == null)
2305 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002306 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002307 }
2308
2309 final ScreenRotationAnimation screenRotationAnimation =
2310 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
2311 final boolean inRotation = screenRotationAnimation != null &&
2312 screenRotationAnimation.isAnimating();
2313 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
2314 "Taking screenshot while rotating");
2315
2316 // We force pending transactions to flush before taking
2317 // the screenshot by pushing an empty synchronous transaction.
2318 SurfaceControl.openTransaction();
2319 SurfaceControl.closeTransactionSync();
2320
2321 bm = SurfaceControl.screenshot(crop, width, height, minLayer, maxLayer,
2322 inRotation, rot);
2323 if (bm == null) {
2324 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
2325 + ") to layer " + maxLayer);
2326 return null;
2327 }
2328 }
2329
2330 if (DEBUG_SCREENSHOT) {
2331 // TEST IF IT's ALL BLACK
2332 int[] buffer = new int[bm.getWidth() * bm.getHeight()];
2333 bm.getPixels(buffer, 0, bm.getWidth(), 0, 0, bm.getWidth(), bm.getHeight());
2334 boolean allBlack = true;
2335 final int firstColor = buffer[0];
2336 for (int i = 0; i < buffer.length; i++) {
2337 if (buffer[i] != firstColor) {
2338 allBlack = false;
2339 break;
2340 }
2341 }
2342 if (allBlack) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002343 final WindowState appWin = mScreenshotApplicationState.appWin;
2344 final int maxLayer = mScreenshotApplicationState.maxLayer;
2345 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002346 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2347 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2348 (appWin != null ?
2349 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2350 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2351 }
2352 }
2353
2354 // Create a copy of the screenshot that is immutable and backed in ashmem.
2355 // This greatly reduces the overhead of passing the bitmap between processes.
2356 Bitmap ret = bm.createAshmemBitmap(config);
2357 bm.recycle();
2358 return ret;
2359 }
2360
2361 // TODO: Can this use createRotationMatrix()?
2362 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
2363 if (rot == Surface.ROTATION_90) {
2364 final int tmp = crop.top;
2365 crop.top = dw - crop.right;
2366 crop.right = crop.bottom;
2367 crop.bottom = dw - crop.left;
2368 crop.left = tmp;
2369 } else if (rot == Surface.ROTATION_180) {
2370 int tmp = crop.top;
2371 crop.top = dh - crop.bottom;
2372 crop.bottom = dh - tmp;
2373 tmp = crop.right;
2374 crop.right = dw - crop.left;
2375 crop.left = dw - tmp;
2376 } else if (rot == Surface.ROTATION_270) {
2377 final int tmp = crop.top;
2378 crop.top = crop.left;
2379 crop.left = dh - crop.bottom;
2380 crop.bottom = crop.right;
2381 crop.right = dh - tmp;
2382 }
2383 }
2384
2385 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002386 // Used to indicate the layout is needed.
2387 mTmpWindow = null;
2388
2389 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002390 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002391 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002392 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002393 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002394 w.setDisplayLayoutNeeded();
2395 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002396 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002397
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002398 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002399 mService.mWindowPlacerLocked.performSurfacePlacement();
2400 }
2401 }
2402
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002403 static final class TaskForResizePointSearchResult {
2404 boolean searchDone;
2405 Task taskForResize;
2406
2407 void reset() {
2408 searchDone = false;
2409 taskForResize = null;
2410 }
2411 }
Robert Carr3b716242016-08-16 16:02:21 -07002412
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002413 private static final class ApplySurfaceChangesTransactionState {
2414 boolean displayHasContent;
2415 boolean obscured;
2416 boolean syswin;
2417 boolean focusDisplayed;
2418 float preferredRefreshRate;
2419 int preferredModeId;
2420
2421 void reset() {
2422 displayHasContent = false;
2423 obscured = false;
2424 syswin = false;
2425 focusDisplayed = false;
2426 preferredRefreshRate = 0;
2427 preferredModeId = 0;
2428 }
2429 }
2430
2431 private static final class ScreenshotApplicationState {
2432 WindowState appWin;
2433 int maxLayer;
2434 int minLayer;
2435 boolean screenshotReady;
2436
2437 void reset(boolean screenshotReady) {
2438 appWin = null;
2439 maxLayer = 0;
2440 minLayer = 0;
2441 this.screenshotReady = screenshotReady;
2442 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
2443 }
2444 }
2445
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002446 /**
2447 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
2448 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
2449 * homogeneous children type which is currently required by sub-classes of
2450 * {@link WindowContainer} class.
2451 */
2452 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
2453
2454 int size() {
2455 return mChildren.size();
2456 }
2457
2458 E get(int index) {
2459 return mChildren.get(index);
2460 }
2461
2462 @Override
2463 boolean fillsParent() {
2464 return true;
2465 }
2466
2467 @Override
2468 boolean isVisible() {
2469 return true;
Robert Carr3b716242016-08-16 16:02:21 -07002470 }
2471 }
2472
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002473 /**
2474 * Window container class that contains all containers on this display relating to Apps.
2475 * I.e Activities.
2476 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07002477 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002478
Andrii Kulian839def92016-11-02 10:58:58 -07002479 /**
2480 * Adds the stack to this container.
2481 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
2482 */
2483 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002484 if (stack.mStackId == HOME_STACK_ID) {
2485 if (mHomeStack != null) {
2486 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
2487 }
2488 mHomeStack = stack;
2489 }
2490 addChild(stack, onTop);
2491 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07002492 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002493
Andrii Kulian839def92016-11-02 10:58:58 -07002494 /** Removes the stack from its container and prepare for changing the parent. */
2495 void removeStackFromDisplay(TaskStack stack) {
2496 removeChild(stack);
2497 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07002498 }
2499
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002500 private void addChild(TaskStack stack, boolean toTop) {
2501 int addIndex = toTop ? mChildren.size() : 0;
2502
2503 if (toTop
2504 && mService.isStackVisibleLocked(PINNED_STACK_ID)
2505 && stack.mStackId != PINNED_STACK_ID) {
2506 // The pinned stack is always the top most stack (always-on-top) when it is visible.
2507 // So, stack is moved just below the pinned stack.
2508 addIndex--;
2509 TaskStack topStack = mChildren.get(addIndex);
2510 if (topStack.mStackId != PINNED_STACK_ID) {
2511 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
2512 }
2513 }
2514 addChild(stack, addIndex);
2515 setLayoutNeeded();
2516 }
2517
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002518 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08002519 void positionChildAt(int position, TaskStack child, boolean includingParents) {
2520 if (StackId.isAlwaysOnTop(child.mStackId) && position != POSITION_TOP) {
2521 // This stack is always-on-top, override the default behavior.
2522 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
2523
2524 // Moving to its current position, as we must call super but we don't want to
2525 // perform any meaningful action.
2526 final int currentPosition = mChildren.indexOf(child);
2527 super.positionChildAt(currentPosition, child, false /* includingParents */);
2528 return;
2529 }
2530
2531 super.positionChildAt(position, child, includingParents);
2532 }
2533
2534 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002535 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
2536 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002537 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002538 if (super.forAllWindows(callback, traverseTopToBottom)) {
2539 return true;
2540 }
2541 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
2542 return true;
2543 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002544 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002545 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
2546 return true;
2547 }
2548 if (super.forAllWindows(callback, traverseTopToBottom)) {
2549 return true;
2550 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002551 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002552 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002553 }
2554
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002555 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002556 boolean traverseTopToBottom) {
2557 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
2558 // app tokens.
2559 // TODO: Investigate if we need to continue to do this or if we can just process them
2560 // in-order.
2561 if (traverseTopToBottom) {
2562 for (int i = mChildren.size() - 1; i >= 0; --i) {
2563 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
2564 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002565 if (appTokens.get(j).forAllWindowsUnchecked(callback,
2566 traverseTopToBottom)) {
2567 return true;
2568 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002569 }
2570 }
2571 } else {
2572 final int count = mChildren.size();
2573 for (int i = 0; i < count; ++i) {
2574 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
2575 final int appTokensCount = appTokens.size();
2576 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002577 if (appTokens.get(j).forAllWindowsUnchecked(callback,
2578 traverseTopToBottom)) {
2579 return true;
2580 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002581 }
2582 }
2583 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002584 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002585 }
2586
2587 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002588 int getOrientation() {
2589 if (mService.isStackVisibleLocked(DOCKED_STACK_ID)
2590 || mService.isStackVisibleLocked(FREEFORM_WORKSPACE_STACK_ID)) {
2591 // Apps and their containers are not allowed to specify an orientation while the
2592 // docked or freeform stack is visible...except for the home stack/task if the
2593 // docked stack is minimized and it actually set something.
2594 if (mHomeStack != null && mHomeStack.isVisible()
2595 && mDividerControllerLocked.isMinimizedDock()) {
2596 final int orientation = mHomeStack.getOrientation();
2597 if (orientation != SCREEN_ORIENTATION_UNSET) {
2598 return orientation;
2599 }
2600 }
2601 return SCREEN_ORIENTATION_UNSPECIFIED;
2602 }
2603
2604 final int orientation = super.getOrientation();
2605 if (orientation != SCREEN_ORIENTATION_UNSET
2606 && orientation != SCREEN_ORIENTATION_BEHIND) {
2607 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
2608 "App is requesting an orientation, return " + orientation);
2609 return orientation;
2610 }
2611
2612 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
2613 "No app is requesting an orientation, return " + mService.mLastOrientation);
2614 // The next app has not been requested to be visible, so we keep the current orientation
2615 // to prevent freezing/unfreezing the display too early.
2616 return mService.mLastOrientation;
2617 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002618 }
2619
2620 /**
2621 * Window container class that contains all containers on this display that are not related to
2622 * Apps. E.g. status bar.
2623 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07002624 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
2625 /**
2626 * Compares two child window tokens returns -1 if the first is lesser than the second in
2627 * terms of z-order and 1 otherwise.
2628 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002629 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07002630 // Tokens with higher base layer are z-ordered on-top.
2631 mService.mPolicy.windowTypeToLayerLw(token1.windowType)
2632 < mService.mPolicy.windowTypeToLayerLw(token2.windowType) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002633
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002634 private final Predicate<WindowState> mGetOrientingWindow = w -> {
2635 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
2636 return false;
2637 }
2638 final int req = w.mAttrs.screenOrientation;
2639 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
2640 || req == SCREEN_ORIENTATION_UNSET) {
2641 return false;
2642 }
2643 return true;
2644 };
2645
Wale Ogunwale3a931692016-11-02 16:49:48 -07002646 private final String mName;
2647 NonAppWindowContainers(String name) {
2648 mName = name;
2649 }
2650
2651 void addChild(WindowToken token) {
2652 addChild(token, mWindowComparator);
2653 }
2654
2655 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002656 int getOrientation() {
2657 final WindowManagerPolicy policy = mService.mPolicy;
2658 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002659 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002660
2661 if (win != null) {
2662 final int req = win.mAttrs.screenOrientation;
2663 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
2664 if (policy.isKeyguardHostWindow(win.mAttrs)) {
2665 mService.mLastKeyguardForcedOrientation = req;
2666 }
2667 return (mService.mLastWindowForcedOrientation = req);
2668 }
2669
2670 mService.mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
2671
2672 if (policy.isKeyguardShowingAndNotOccluded()) {
2673 return mService.mLastKeyguardForcedOrientation;
2674 }
2675
2676 return SCREEN_ORIENTATION_UNSET;
2677 }
2678
2679 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07002680 String getName() {
2681 return mName;
2682 }
Robert Carr3b716242016-08-16 16:02:21 -07002683 }
Craig Mautner59c00972012-07-30 12:10:24 -07002684}