blob: 461c3faf1d9e1d77dc08209e3d0f97f9b7ae5b29 [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 Ogunwale1666e312016-12-16 11:27:18 -080060import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070061import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070062import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070063import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
64import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070065import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwalec69694a2016-10-18 13:51:15 -070066import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070067import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020069import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070070import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070076import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070077import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070078import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080079import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070080import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
81import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070082import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070083import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
84import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER;
85import static com.android.server.wm.WindowManagerService.TYPE_LAYER_OFFSET;
86import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Wale Ogunwale494009b82016-10-21 09:01:38 -070087import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070088import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070089import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070090import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -070091import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070092import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070093import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -070094
Andrii Kulian3a507b52016-09-19 18:14:12 -070095import android.annotation.NonNull;
Wale Ogunwale3797c222015-10-27 14:21:58 -070096import android.app.ActivityManager.StackId;
Andrii Kulian441e4492016-09-29 15:25:00 -070097import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070098import android.graphics.Bitmap;
Jorim Jaggi6a7a8592017-01-12 00:44:33 +010099import android.graphics.GraphicBuffer;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700100import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800101import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700102import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700103import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100104import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700105import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700106import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700107import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700108import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700109import android.os.RemoteException;
110import android.os.SystemClock;
Chong Zhang8e89b312015-09-09 15:09:30 -0700111import android.util.DisplayMetrics;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800112import android.util.MutableBoolean;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700113import android.util.Slog;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700114import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700115import android.view.DisplayInfo;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700116import android.view.Surface;
117import android.view.SurfaceControl;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700118import android.view.WindowManagerPolicy;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700119
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800120import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700121import com.android.internal.view.IInputMethodClient;
Craig Mautner59c00972012-07-30 12:10:24 -0700122
Robert Carr3b716242016-08-16 16:02:21 -0700123import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700124import java.io.PrintWriter;
125import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700126import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700127import java.util.HashMap;
128import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700129import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700130import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800131import java.util.function.Consumer;
132import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700133
Craig Mautner59c00972012-07-30 12:10:24 -0700134/**
135 * Utility class for keeping track of the WindowStates and other pertinent contents of a
136 * particular Display.
137 *
138 * IMPORTANT: No method from this class should ever be used without holding
139 * WindowManagerService.mWindowMap.
140 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700141class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700142 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700143
144 /** Unique identifier of this stack. */
145 private final int mDisplayId;
146
Wale Ogunwale3a931692016-11-02 16:49:48 -0700147 /** The containers below are the only child containers the display can have. */
148 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700149 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700150 // Contains all non-app window containers that should be displayed above the app containers
151 // (e.g. Status bar)
152 private final NonAppWindowContainers mAboveAppWindowsContainers =
153 new NonAppWindowContainers("mAboveAppWindowsContainers");
154 // Contains all non-app window containers that should be displayed below the app containers
155 // (e.g. Wallpaper).
156 private final NonAppWindowContainers mBelowAppWindowsContainers =
157 new NonAppWindowContainers("mBelowAppWindowsContainers");
158 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
159 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
160 // window containers together and move them in-sync if/when needed.
161 private final NonAppWindowContainers mImeWindowsContainers =
162 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700163
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000164 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800165 private WindowState mTmpWindow2;
166 private WindowAnimator mTmpWindowAnimator;
167 private boolean mTmpRecoveringMemory;
168 private boolean mUpdateImeTarget;
169 private boolean mTmpInitial;
Craig Mautner59c00972012-07-30 12:10:24 -0700170
Wale Ogunwale02319a62016-09-26 15:21:22 -0700171 // Mapping from a token IBinder to a WindowToken object on this display.
172 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
173
Craig Mautner59c00972012-07-30 12:10:24 -0700174 int mInitialDisplayWidth = 0;
175 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700176 int mInitialDisplayDensity = 0;
Craig Mautner59c00972012-07-30 12:10:24 -0700177 int mBaseDisplayWidth = 0;
178 int mBaseDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700179 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700180 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700181 private final DisplayInfo mDisplayInfo = new DisplayInfo();
182 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700183 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Craig Mautner59c00972012-07-30 12:10:24 -0700184
Craig Mautner6601b7b2013-04-29 10:29:11 -0700185 Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700186 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700187
Craig Mautner39834192012-09-02 07:47:24 -0700188 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700189 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700190 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700191 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800192 boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700193
Craig Mautnerdc548482014-02-05 13:35:24 -0800194 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700195 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800196
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800197 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
198 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700199 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700200
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700201 /** Detect user tapping outside of current focused task bounds .*/
202 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700203
Craig Mautner6601b7b2013-04-29 10:29:11 -0700204 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700205 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700206
Craig Mautner6601b7b2013-04-29 10:29:11 -0700207 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800208 private final Rect mTmpRect = new Rect();
209 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700210 private final RectF mTmpRectF = new RectF();
211 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800212 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700213
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800214 WindowManagerService mService;
Craig Mautner9d808b12013-08-06 18:00:25 -0700215
Craig Mautner95da1082014-02-24 17:54:35 -0800216 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700217 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800218
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700219 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700220 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700221
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800222 DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700223
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800224 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
225
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000226 private boolean mHaveBootMsg = false;
227 private boolean mHaveApp = false;
228 private boolean mHaveWallpaper = false;
229 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700230
231 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
232
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700233 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
234 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000235 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
236 new ApplySurfaceChangesTransactionState();
237 private final ScreenshotApplicationState mScreenshotApplicationState =
238 new ScreenshotApplicationState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700239
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700240 // True if this display is in the process of being removed. Used to determine if the removal of
241 // the display's direct children should be allowed.
242 private boolean mRemovingDisplay = false;
243
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700244 private final WindowLayersController mLayersController;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800245 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700246 int mInputMethodAnimLayerAdjustment;
247
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800248 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
249 WindowStateAnimator winAnimator = w.mWinAnimator;
250 if (winAnimator.hasSurface()) {
251 final boolean wasAnimating = winAnimator.mWasAnimating;
252 final boolean nowAnimating = winAnimator.stepAnimationLocked(
253 mTmpWindowAnimator.mCurrentTime);
254 winAnimator.mWasAnimating = nowAnimating;
255 mTmpWindowAnimator.orAnimating(nowAnimating);
256
257 if (DEBUG_WALLPAPER) Slog.v(TAG,
258 w + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
259
260 if (wasAnimating && !winAnimator.mAnimating
261 && mWallpaperController.isWallpaperTarget(w)) {
262 mTmpWindowAnimator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
263 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
264 if (DEBUG_LAYOUT_REPEATS) {
265 mService.mWindowPlacerLocked.debugLayoutRepeats(
266 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
267 }
268 }
269 }
270
271 final AppWindowToken atoken = w.mAppToken;
272 if (winAnimator.mDrawState == READY_TO_SHOW) {
273 if (atoken == null || atoken.allDrawn) {
274 if (w.performShowLocked()) {
275 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
276 if (DEBUG_LAYOUT_REPEATS) {
277 mService.mWindowPlacerLocked.debugLayoutRepeats(
278 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
279 }
280 }
281 }
282 }
283 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
284 if (appAnimator != null && appAnimator.thumbnail != null) {
285 if (appAnimator.thumbnailTransactionSeq
286 != mTmpWindowAnimator.mAnimTransactionSequence) {
287 appAnimator.thumbnailTransactionSeq =
288 mTmpWindowAnimator.mAnimTransactionSequence;
289 appAnimator.thumbnailLayer = 0;
290 }
291 if (appAnimator.thumbnailLayer < winAnimator.mAnimLayer) {
292 appAnimator.thumbnailLayer = winAnimator.mAnimLayer;
293 }
294 }
295 };
296
297 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
298 final WindowStateAnimator winAnimator = w.mWinAnimator;
299 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
300 return;
301 }
302
303 final int flags = w.mAttrs.flags;
304
305 // If this window is animating, make a note that we have an animating window and take
306 // care of a request to run a detached wallpaper animation.
307 if (winAnimator.mAnimating) {
308 if (winAnimator.mAnimation != null) {
309 if ((flags & FLAG_SHOW_WALLPAPER) != 0
310 && winAnimator.mAnimation.getDetachWallpaper()) {
311 mTmpWindow = w;
312 }
313 final int color = winAnimator.mAnimation.getBackgroundColor();
314 if (color != 0) {
315 final TaskStack stack = w.getStack();
316 if (stack != null) {
317 stack.setAnimationBackground(winAnimator, color);
318 }
319 }
320 }
321 mTmpWindowAnimator.setAnimating(true);
322 }
323
324 // If this window's app token is running a detached wallpaper animation, make a note so
325 // we can ensure the wallpaper is displayed behind it.
326 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
327 if (appAnimator != null && appAnimator.animation != null
328 && appAnimator.animating) {
329 if ((flags & FLAG_SHOW_WALLPAPER) != 0
330 && appAnimator.animation.getDetachWallpaper()) {
331 mTmpWindow = w;
332 }
333
334 final int color = appAnimator.animation.getBackgroundColor();
335 if (color != 0) {
336 final TaskStack stack = w.getStack();
337 if (stack != null) {
338 stack.setAnimationBackground(winAnimator, color);
339 }
340 }
341 }
342 };
343
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800344 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
345 final int lostFocusUid = mTmpWindow.mOwnerUid;
346 final Handler handler = mService.mH;
347 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
348 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
349 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
350 w.mAttrs.hideTimeoutMilliseconds);
351 }
352 }
353 };
354
355 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
356 final AppWindowToken focusedApp = mService.mFocusedApp;
357 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
358 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
359
360 if (!w.canReceiveKeys()) {
361 return false;
362 }
363
364 final AppWindowToken wtoken = w.mAppToken;
365
366 // If this window's application has been removed, just skip it.
367 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
368 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
369 + (wtoken.removed ? "removed" : "sendingToBottom"));
370 return false;
371 }
372
373 if (focusedApp == null) {
374 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
375 + " using new focus @ " + w);
376 mTmpWindow = w;
377 return true;
378 }
379
380 if (!focusedApp.windowsAreFocusable()) {
381 // Current focused app windows aren't focusable...
382 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
383 + " focusable using new focus @ " + w);
384 mTmpWindow = w;
385 return true;
386 }
387
388 // Descend through all of the app tokens and find the first that either matches
389 // win.mAppToken (return win) or mFocusedApp (return null).
390 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
391 if (focusedApp.compareTo(wtoken) > 0) {
392 // App stack below focused app stack. No focus for you!!!
393 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
394 "findFocusedWindow: Reached focused app=" + focusedApp);
395 mTmpWindow = null;
396 return true;
397 }
398 }
399
400 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
401 mTmpWindow = w;
402 return true;
403 };
404
405 private final Consumer<WindowState> mPrepareWindowSurfaces =
406 w -> w.mWinAnimator.prepareSurfaceLocked(true);
407
408 private final Consumer<WindowState> mPerformLayout = w -> {
409 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
410 // wasting time and funky changes while a window is animating away.
411 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
412 || w.isGoneForLayoutLw();
413
414 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
415 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
416 + " mLayoutAttached=" + w.mLayoutAttached
417 + " screen changed=" + w.isConfigChanged());
418 final AppWindowToken atoken = w.mAppToken;
419 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
420 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
421 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
422 + " parentHidden=" + w.isParentWindowHidden());
423 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
424 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
425 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
426 + " parentHidden=" + w.isParentWindowHidden());
427 }
428
429 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
430 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
431 // since that means "perform layout as normal, just don't display").
432 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
433 || ((w.isConfigChanged() || w.setReportResizeHints())
434 && !w.isGoneForLayoutLw() &&
435 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
436 (w.mHasSurface && w.mAppToken != null &&
437 w.mAppToken.layoutConfigChanges)))) {
438 if (!w.mLayoutAttached) {
439 if (mTmpInitial) {
440 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
441 w.mContentChanged = false;
442 }
443 if (w.mAttrs.type == TYPE_DREAM) {
444 // Don't layout windows behind a dream, so that if it does stuff like hide
445 // the status bar we won't get a bad transition when it goes away.
446 mTmpWindow = w;
447 }
448 w.mLayoutNeeded = false;
449 w.prelayout();
450 mService.mPolicy.layoutWindowLw(w, null);
451 w.mLayoutSeq = mService.mLayoutSeq;
452
453 // Window frames may have changed. Update dim layer with the new bounds.
454 final Task task = w.getTask();
455 if (task != null) {
456 mDimLayerController.updateDimLayer(task);
457 }
458
459 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
460 + " mContainingFrame=" + w.mContainingFrame
461 + " mDisplayFrame=" + w.mDisplayFrame);
462 }
463 }
464 };
465
466 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
467 if (w.mLayoutAttached) {
468 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
469 + " mViewVisibility=" + w.mViewVisibility
470 + " mRelayoutCalled=" + w.mRelayoutCalled);
471 // If this view is GONE, then skip it -- keep the current frame, and let the caller
472 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
473 // windows, since that means "perform layout as normal, just don't display").
474 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
475 return;
476 }
477 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
478 || w.mLayoutNeeded) {
479 if (mTmpInitial) {
480 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
481 w.mContentChanged = false;
482 }
483 w.mLayoutNeeded = false;
484 w.prelayout();
485 mService.mPolicy.layoutWindowLw(w, w.getParentWindow());
486 w.mLayoutSeq = mService.mLayoutSeq;
487 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
488 + " mContainingFrame=" + w.mContainingFrame
489 + " mDisplayFrame=" + w.mDisplayFrame);
490 }
491 } else if (w.mAttrs.type == TYPE_DREAM) {
492 // Don't layout windows behind a dream, so that if it does stuff like hide the
493 // status bar we won't get a bad transition when it goes away.
494 mTmpWindow = mTmpWindow2;
495 }
496 };
497
498 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
499 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
500 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
501 return w.canBeImeTarget();
502 };
503
504 private final Consumer<WindowState> mApplyPostLayoutPolicy =
505 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
506 mService.mInputMethodTarget);
507
508 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
509 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
510 final boolean obscuredChanged = w.mObscured !=
511 mTmpApplySurfaceChangesTransactionState.obscured;
512 final RootWindowContainer root = mService.mRoot;
513 // Only used if default window
514 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
515
516 // Update effect.
517 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
518 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
519 final boolean isDisplayed = w.isDisplayedLw();
520
521 if (isDisplayed && w.isObscuringDisplay()) {
522 // This window completely covers everything behind it, so we want to leave all
523 // of them as undimmed (for performance reasons).
524 root.mObscuringWindow = w;
525 mTmpApplySurfaceChangesTransactionState.obscured = true;
526 }
527
528 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
529 root.handleNotObscuredLocked(w,
530 mTmpApplySurfaceChangesTransactionState.obscured,
531 mTmpApplySurfaceChangesTransactionState.syswin);
532
533 if (w.mHasSurface && isDisplayed) {
534 final int type = w.mAttrs.type;
535 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
536 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
537 mTmpApplySurfaceChangesTransactionState.syswin = true;
538 }
539 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
540 && w.mAttrs.preferredRefreshRate != 0) {
541 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
542 = w.mAttrs.preferredRefreshRate;
543 }
544 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
545 && w.mAttrs.preferredDisplayModeId != 0) {
546 mTmpApplySurfaceChangesTransactionState.preferredModeId
547 = w.mAttrs.preferredDisplayModeId;
548 }
549 }
550 }
551
552 w.applyDimLayerIfNeeded();
553
554 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
555 && mWallpaperController.isWallpaperTarget(w)) {
556 // This is the wallpaper target and its obscured state changed... make sure the
557 // current wallpaper's visibility has been updated accordingly.
558 mWallpaperController.updateWallpaperVisibility();
559 }
560
561 w.handleWindowMovedIfNeeded();
562
563 final WindowStateAnimator winAnimator = w.mWinAnimator;
564
565 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
566 w.mContentChanged = false;
567
568 // Moved from updateWindowsAndWallpaperLocked().
569 if (w.mHasSurface) {
570 // Take care of the window being ready to display.
571 final boolean committed = winAnimator.commitFinishDrawingLocked();
572 if (isDefaultDisplay && committed) {
573 if (w.mAttrs.type == TYPE_DREAM) {
574 // HACK: When a dream is shown, it may at that point hide the lock screen.
575 // So we need to redo the layout to let the phone window manager make this
576 // happen.
577 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
578 if (DEBUG_LAYOUT_REPEATS) {
579 surfacePlacer.debugLayoutRepeats(
580 "dream and commitFinishDrawingLocked true",
581 pendingLayoutChanges);
582 }
583 }
584 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
585 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
586 "First draw done in potential wallpaper target " + w);
587 root.mWallpaperMayChange = true;
588 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
589 if (DEBUG_LAYOUT_REPEATS) {
590 surfacePlacer.debugLayoutRepeats(
591 "wallpaper and commitFinishDrawingLocked true",
592 pendingLayoutChanges);
593 }
594 }
595 }
596 if (!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening()) {
597 // Updates the shown frame before we set up the surface. This is needed
598 // because the resizing could change the top-left position (in addition to
599 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
600 // position the surface.
601 //
602 // If an animation is being started, we can't call this method because the
603 // animation hasn't processed its initial transformation yet, but in general
604 // we do want to update the position if the window is animating.
605 winAnimator.computeShownFrameLocked();
606 }
607 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
608 }
609
610 final AppWindowToken atoken = w.mAppToken;
611 if (atoken != null) {
612 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
613 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
614 mTmpUpdateAllDrawn.add(atoken);
615 }
616 }
617
618 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
619 && w.isDisplayedLw()) {
620 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
621 }
622
623 w.updateResizingWindowIfNeeded();
624 };
625
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800626 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800627 * Create new {@link DisplayContent} instance, add itself to the root window container and
628 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700629 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800630 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700631 * @param layersController window layer controller used to assign layer to the windows on this
632 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700633 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
634 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700635 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700636 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700637 WindowLayersController layersController, WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800638
639 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
640 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
641 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
642 + " new=" + display);
643 }
644
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700645 mDisplay = display;
646 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700647 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700648 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700649 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700650 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700651 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700652 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700653 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800654 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700655 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800656 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700657
658 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700659 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700660 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700661 super.addChild(mAboveAppWindowsContainers, null);
662 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800663
664 // Add itself as a child to the root container.
665 mService.mRoot.addChild(this, null);
Craig Mautner59c00972012-07-30 12:10:24 -0700666 }
667
668 int getDisplayId() {
669 return mDisplayId;
670 }
671
Wale Ogunwale02319a62016-09-26 15:21:22 -0700672 WindowToken getWindowToken(IBinder binder) {
673 return mTokenMap.get(binder);
674 }
675
676 AppWindowToken getAppWindowToken(IBinder binder) {
677 final WindowToken token = getWindowToken(binder);
678 if (token == null) {
679 return null;
680 }
681 return token.asAppWindowToken();
682 }
683
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800684 void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700685 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
686 if (dc != null) {
687 // We currently don't support adding a window token to the display if the display
688 // already has the binder mapped to another token. If there is a use case for supporting
689 // this moving forward we will either need to merge the WindowTokens some how or have
690 // the binder map to a list of window tokens.
691 throw new IllegalArgumentException("Can't map token=" + token + " to display=" + this
692 + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
693 }
694 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700695
696 if (token.asAppWindowToken() == null) {
697 // Add non-app token to container hierarchy on the display. App tokens are added through
698 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700699 switch (token.windowType) {
700 case TYPE_WALLPAPER:
701 mBelowAppWindowsContainers.addChild(token);
702 break;
703 case TYPE_INPUT_METHOD:
704 case TYPE_INPUT_METHOD_DIALOG:
705 mImeWindowsContainers.addChild(token);
706 break;
707 default:
708 mAboveAppWindowsContainers.addChild(token);
709 break;
710 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700711 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700712 }
713
714 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700715 final WindowToken token = mTokenMap.remove(binder);
716 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800717 token.setExiting();
718 }
719 return token;
720 }
721
722 /** Changes the display the input window token is housed on to this one. */
723 void reParentWindowToken(WindowToken token) {
724 final DisplayContent prevDc = token.getDisplayContent();
725 if (prevDc == this) {
726 return;
727 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800728 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
729 && token.asAppWindowToken() == null) {
730 // Removed the token from the map, but made sure it's not an app token before removing
731 // from parent.
732 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700733 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800734
735 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700736 }
737
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700738 void removeAppToken(IBinder binder) {
739 final WindowToken token = removeWindowToken(binder);
740 if (token == null) {
741 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
742 return;
743 }
744
745 final AppWindowToken appToken = token.asAppWindowToken();
746
747 if (appToken == null) {
748 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
749 return;
750 }
751
752 appToken.onRemovedFromDisplay();
753 }
754
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700755 Display getDisplay() {
756 return mDisplay;
757 }
758
Craig Mautner59c00972012-07-30 12:10:24 -0700759 DisplayInfo getDisplayInfo() {
760 return mDisplayInfo;
761 }
762
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700763 DisplayMetrics getDisplayMetrics() {
764 return mDisplayMetrics;
765 }
766
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100767 DockedStackDividerController getDockedDividerController() {
768 return mDividerControllerLocked;
769 }
770
Winson Chung655332c2016-10-31 13:14:28 -0700771 PinnedStackController getPinnedStackController() {
772 return mPinnedStackControllerLocked;
773 }
774
Jeff Browna506a6e2013-06-04 00:02:38 -0700775 /**
776 * Returns true if the specified UID has access to this display.
777 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700778 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700779 return mDisplay.hasAccess(uid);
780 }
781
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700782 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700783 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -0700784 }
785
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700786 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700787 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800788 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -0800789 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700790 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700791 }
792
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700793 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700794 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
795 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700796 if (stack.mStackId == stackId) {
797 return stack;
798 }
799 }
800 return null;
801 }
802
Andrii Kulian441e4492016-09-29 15:25:00 -0700803 @Override
804 void onConfigurationChanged(Configuration newParentConfig) {
805 super.onConfigurationChanged(newParentConfig);
806
Andrii Kulian3a507b52016-09-19 18:14:12 -0700807 // The display size information is heavily dependent on the resources in the current
808 // configuration, so we need to reconfigure it every time the configuration changes.
809 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
810 mService.reconfigureDisplayLocked(this);
811
812 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -0700813 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -0700814 }
Andrii Kulian3a507b52016-09-19 18:14:12 -0700815
Andrii Kulian441e4492016-09-29 15:25:00 -0700816 /**
817 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
818 * bounds were updated.
819 */
820 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700821 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
822 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -0700823 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -0700824 changedStackList.add(stack.mStackId);
825 }
826 }
827 }
828
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700829 @Override
830 boolean fillsParent() {
831 return true;
832 }
833
834 @Override
835 boolean isVisible() {
836 return true;
837 }
838
839 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700840 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -0700841 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000842 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700843 }
844
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700845 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800846 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
847 // Special handling so we can process IME windows with #forAllImeWindows above their IME
848 // target, or here in order if there isn't an IME target.
849 if (traverseTopToBottom) {
850 for (int i = mChildren.size() - 1; i >= 0; --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 } else {
862 final int count = mChildren.size();
863 for (int i = 0; i < count; i++) {
864 final DisplayChildWindowContainer child = mChildren.get(i);
865 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
866 // In this case the Ime windows will be processed above their target so we skip
867 // here.
868 continue;
869 }
870 if (child.forAllWindows(callback, traverseTopToBottom)) {
871 return true;
872 }
873 }
874 }
875 return false;
876 }
877
878 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
879 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
880 }
881
882 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -0700883 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700884 final WindowManagerPolicy policy = mService.mPolicy;
885
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700886 if (mService.mDisplayFrozen) {
887 if (mService.mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
888 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
889 "Display is frozen, return " + mService.mLastWindowForcedOrientation);
890 // If the display is frozen, some activities may be in the middle of restarting, and
891 // thus have removed their old window. If the window has the flag to hide the lock
892 // screen, then the lock screen can re-appear and inflict its own orientation on us.
893 // Keep the orientation stable until this all settles down.
894 return mService.mLastWindowForcedOrientation;
895 } else if (policy.isKeyguardLocked()) {
896 // Use the last orientation the while the display is frozen with the keyguard
897 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
898 // window. We don't want to check the show when locked window directly though as
899 // things aren't stable while the display is frozen, for example the window could be
900 // momentarily unavailable due to activity relaunch.
901 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
902 + "return " + mService.mLastOrientation);
903 return mService.mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -0700904 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700905 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000906 final int orientation = mAboveAppWindowsContainers.getOrientation();
907 if (orientation != SCREEN_ORIENTATION_UNSET) {
908 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700909 }
Wale Ogunwale51362492016-09-08 17:49:17 -0700910 }
911
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700912 // Top system windows are not requesting an orientation. Start searching from apps.
913 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -0700914 }
915
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700916 void updateDisplayInfo() {
Craig Mautner722285e2012-09-07 13:55:58 -0700917 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700918 mDisplay.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700919 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
920 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800921 }
Craig Mautner722285e2012-09-07 13:55:58 -0700922 }
923
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700924 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700925 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
926 if (displayManagerInternal != null) {
927 // Bootstrap the default logical display from the display manager.
928 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
929 if (newDisplayInfo != null) {
930 mDisplayInfo.copyFrom(newDisplayInfo);
931 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700932 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700933
Bryce Lee6addf5d2017-02-21 21:36:55 +0000934 mBaseDisplayWidth = mInitialDisplayWidth = mDisplayInfo.logicalWidth;
935 mBaseDisplayHeight = mInitialDisplayHeight = mDisplayInfo.logicalHeight;
936 mBaseDisplayDensity = mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
937 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700938 }
939
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700940 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700941 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800942 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700943 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800944 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
945 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700946 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800947 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700948 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800949 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700950 out.set(left, top, left + width, top + height);
951 }
952
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700953 private void getLogicalDisplayRect(Rect out, int orientation) {
954 getLogicalDisplayRect(out);
955
956 // Rotate the Rect if needed.
957 final int currentRotation = mDisplayInfo.rotation;
958 final int rotationDelta = deltaRotation(currentRotation, orientation);
959 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
960 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
961 mTmpRectF.set(out);
962 mTmpMatrix.mapRect(mTmpRectF);
963 mTmpRectF.round(out);
964 }
965 }
966
Chong Zhangf66db432016-01-13 10:39:51 -0800967 void getContentRect(Rect out) {
968 out.set(mContentRect);
969 }
970
Wale Ogunwale1666e312016-12-16 11:27:18 -0800971 TaskStack addStackToDisplay(int stackId, boolean onTop) {
972 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
973 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -0700974
Wale Ogunwale1666e312016-12-16 11:27:18 -0800975 TaskStack stack = getStackById(stackId);
976 if (stack != null) {
977 // It's already attached to the display...clear mDeferRemoval and move stack to
978 // appropriate z-order on display as needed.
979 stack.mDeferRemoval = false;
980 // We're not moving the display to front when we're adding stacks, only when
981 // requested to change the position of stack explicitly.
982 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
983 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -0700984 } else {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800985 stack = new TaskStack(mService, stackId);
Andrii Kulian839def92016-11-02 10:58:58 -0700986 mTaskStackContainers.addStackToDisplay(stack, onTop);
987 }
988
Wale Ogunwale1666e312016-12-16 11:27:18 -0800989 if (stackId == DOCKED_STACK_ID) {
990 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -0700991 }
Wale Ogunwale1666e312016-12-16 11:27:18 -0800992 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -0700993 }
994
Wale Ogunwale1666e312016-12-16 11:27:18 -0800995 void moveStackToDisplay(TaskStack stack) {
996 final DisplayContent prevDc = stack.getDisplayContent();
997 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -0700998 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
999 + " which is not currently attached to any display");
1000 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001001 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001002 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1003 + " to its current displayId=" + mDisplayId);
1004 }
1005
Wale Ogunwale1666e312016-12-16 11:27:18 -08001006 prevDc.mTaskStackContainers.removeStackFromDisplay(stack);
1007 mTaskStackContainers.addStackToDisplay(stack, true /* onTop */);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001008 }
1009
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001010 @Override
1011 protected void addChild(DisplayChildWindowContainer child,
1012 Comparator<DisplayChildWindowContainer> comparator) {
1013 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1014 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001015
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001016 @Override
1017 protected void addChild(DisplayChildWindowContainer child, int index) {
1018 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1019 }
1020
1021 @Override
1022 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001023 // Only allow removal of direct children from this display if the display is in the process
1024 // of been removed.
1025 if (mRemovingDisplay) {
1026 super.removeChild(child);
1027 return;
1028 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001029 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001030 }
1031
Andrii Kuliand2765632016-12-12 22:26:34 -08001032 @Override
1033 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1034 // Children of the display are statically ordered, so the real intention here is to perform
1035 // the operation on the display and not the static direct children.
1036 getParent().positionChildAt(position, this, includingParents);
1037 }
1038
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001039 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001040 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1041 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001042 final int taskId = stack.taskIdFromPoint(x, y);
1043 if (taskId != -1) {
1044 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001045 }
1046 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001047 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001048 }
1049
Chong Zhang8e89b312015-09-09 15:09:30 -07001050 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001051 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001052 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001053 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001054 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001055 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001056 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001057 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1058 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001059 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001060 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001061 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001062
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001063 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1064 if (mTmpTaskForResizePointSearchResult.searchDone) {
1065 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001066 }
1067 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001068 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001069 }
1070
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001071 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001072 // The provided task is the task on this display with focus, so if WindowManagerService's
1073 // focused app is not on this display, focusedTask will be null.
1074 if (focusedTask == null) {
1075 mTouchExcludeRegion.setEmpty();
1076 } else {
1077 mTouchExcludeRegion.set(mBaseDisplayRect);
1078 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1079 mTmpRect2.setEmpty();
1080 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1081 final TaskStack stack = mTaskStackContainers.get(stackNdx);
1082 stack.setTouchExcludeRegion(
1083 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
1084 }
1085 // If we removed the focused task above, add it back and only leave its
1086 // outside touch area in the exclusion. TapDectector is not interested in
1087 // any touch inside the focused task itself.
1088 if (!mTmpRect2.isEmpty()) {
1089 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1090 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001091 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001092 final WindowState inputMethod = mService.mInputMethodWindow;
1093 if (inputMethod != null && inputMethod.isVisibleLw()) {
1094 // If the input method is visible and the user is typing, we don't want these touch
1095 // events to be intercepted and used to change focus. This would likely cause a
1096 // disappearance of the input method.
1097 inputMethod.getTouchableRegion(mTmpRegion);
1098 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1099 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001100 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1101 WindowState win = mTapExcludedWindows.get(i);
1102 win.getTouchableRegion(mTmpRegion);
1103 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1104 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001105 // TODO(multi-display): Support docked stacks on secondary displays.
1106 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001107 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001108 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001109 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1110 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001111 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001112 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001113 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001114 }
1115
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001116 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001117 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001118 super.switchUser();
1119 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001120 }
1121
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001122 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001123 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1124 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001125 }
1126 }
1127
1128 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001129 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001130 }
1131
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001132 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001133 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001134 }
1135
1136 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001137 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001138 }
1139
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001140 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001141 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001142 }
1143
Wale Ogunwale10124582016-09-15 20:25:50 -07001144 @Override
1145 void removeIfPossible() {
1146 if (isAnimating()) {
1147 mDeferredRemoval = true;
1148 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001149 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001150 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001151 }
1152
Wale Ogunwale10124582016-09-15 20:25:50 -07001153 @Override
1154 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001155 mRemovingDisplay = true;
1156 try {
1157 super.removeImmediately();
1158 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1159 mDimLayerController.close();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001160 if (mDisplayId == DEFAULT_DISPLAY && mService.canDispatchPointerEvents()) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001161 mService.unregisterPointerEventListener(mTapDetector);
1162 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1163 }
1164 } finally {
1165 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001166 }
Craig Mautner95da1082014-02-24 17:54:35 -08001167 }
1168
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001169 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001170 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001171 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001172 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1173
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001174 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001175 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001176 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001177 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001178 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001179 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001180 }
1181
Wale Ogunwale10124582016-09-15 20:25:50 -07001182 boolean animateForIme(float interpolatedValue, float animationTarget,
1183 float dividerAnimationTarget) {
1184 boolean updated = false;
1185
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001186 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1187 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001188 if (stack == null || !stack.isAdjustedForIme()) {
1189 continue;
1190 }
1191
1192 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1193 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1194 updated = true;
1195 } else {
1196 mDividerControllerLocked.mLastAnimationProgress =
1197 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1198 mDividerControllerLocked.mLastDividerProgress =
1199 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1200 updated |= stack.updateAdjustForIme(
1201 mDividerControllerLocked.mLastAnimationProgress,
1202 mDividerControllerLocked.mLastDividerProgress,
1203 false /* force */);
1204 }
1205 if (interpolatedValue >= 1f) {
1206 stack.endImeAdjustAnimation();
1207 }
1208 }
1209
1210 return updated;
1211 }
1212
1213 boolean clearImeAdjustAnimation() {
1214 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001215 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1216 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001217 if (stack != null && stack.isAdjustedForIme()) {
1218 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1219 changed = true;
1220 }
1221 }
1222 return changed;
1223 }
1224
1225 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001226 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1227 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001228 if (stack.isVisible() && stack.isAdjustedForIme()) {
1229 stack.beginImeAdjustAnimation();
1230 }
1231 }
1232 }
1233
1234 void adjustForImeIfNeeded() {
1235 final WindowState imeWin = mService.mInputMethodWindow;
1236 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
1237 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001238 final boolean dockVisible = isStackVisible(DOCKED_STACK_ID);
Wale Ogunwale10124582016-09-15 20:25:50 -07001239 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
1240 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
1241 imeTargetStack.getDockSide() : DOCKED_INVALID;
1242 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
1243 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
1244 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
1245 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
1246 final boolean imeHeightChanged = imeVisible &&
1247 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
1248
1249 // The divider could be adjusted for IME position, or be thinner than usual,
1250 // or both. There are three possible cases:
1251 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
1252 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
1253 // - If IME is not visible, divider is not moved and is normal width.
1254
1255 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001256 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1257 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001258 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
1259 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)) {
1260 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
1261 } else {
1262 stack.resetAdjustedForIme(false);
1263 }
1264 }
1265 mDividerControllerLocked.setAdjustedForIme(
1266 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
1267 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001268 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1269 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001270 stack.resetAdjustedForIme(!dockVisible);
1271 }
1272 mDividerControllerLocked.setAdjustedForIme(
1273 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
1274 }
Winson Chung655332c2016-10-31 13:14:28 -07001275 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07001276 }
1277
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001278 void setInputMethodAnimLayerAdjustment(int adj) {
1279 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
1280 mInputMethodAnimLayerAdjustment = adj;
Robert Carrdee1b3f2017-02-27 11:33:33 -08001281 assignWindowLayers(false /* relayoutNeeded */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001282 }
1283
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001284 /**
1285 * If a window that has an animation specifying a colored background and the current wallpaper
1286 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
1287 * suddenly disappear.
1288 */
1289 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001290 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
1291 w -> w.mIsWallpaper && w.isVisibleNow());
1292
1293 if (visibleWallpaper != null) {
1294 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001295 }
1296 return winAnimator.mAnimLayer;
1297 }
1298
Wale Ogunwale10124582016-09-15 20:25:50 -07001299 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001300 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1301 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07001302 stack.prepareFreezingTaskBounds();
1303 }
1304 }
1305
Wale Ogunwale94744212015-09-21 19:01:47 -07001306 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001307 getLogicalDisplayRect(mTmpRect, newRotation);
1308
1309 // Compute a transform matrix to undo the coordinate space transformation,
1310 // and present the window at the same physical position it previously occupied.
1311 final int deltaRotation = deltaRotation(newRotation, oldRotation);
1312 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
1313
1314 mTmpRectF.set(bounds);
1315 mTmpMatrix.mapRect(mTmpRectF);
1316 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07001317 }
1318
Wale Ogunwale4a02d812015-02-12 23:01:38 -08001319 static int deltaRotation(int oldRotation, int newRotation) {
1320 int delta = newRotation - oldRotation;
1321 if (delta < 0) delta += 4;
1322 return delta;
1323 }
1324
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001325 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001326 Matrix outMatrix) {
1327 // For rotations without Z-ordering we don't need the target rectangle's position.
1328 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
1329 displayHeight, outMatrix);
1330 }
1331
1332 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
1333 float displayWidth, float displayHeight, Matrix outMatrix) {
1334 switch (rotation) {
1335 case ROTATION_0:
1336 outMatrix.reset();
1337 break;
1338 case ROTATION_270:
1339 outMatrix.setRotate(270, 0, 0);
1340 outMatrix.postTranslate(0, displayHeight);
1341 outMatrix.postTranslate(rectTop, 0);
1342 break;
1343 case ROTATION_180:
1344 outMatrix.reset();
1345 break;
1346 case ROTATION_90:
1347 outMatrix.setRotate(90, 0, 0);
1348 outMatrix.postTranslate(displayWidth, 0);
1349 outMatrix.postTranslate(-rectTop, rectLeft);
1350 break;
1351 }
1352 }
1353
Craig Mautnera91f9e22012-09-14 16:22:08 -07001354 public void dump(String prefix, PrintWriter pw) {
1355 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
1356 final String subPrefix = " " + prefix;
1357 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
1358 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
1359 pw.print("dpi");
1360 if (mInitialDisplayWidth != mBaseDisplayWidth
1361 || mInitialDisplayHeight != mBaseDisplayHeight
1362 || mInitialDisplayDensity != mBaseDisplayDensity) {
1363 pw.print(" base=");
1364 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
1365 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
1366 }
Jeff Brownd46747a2015-04-15 19:02:36 -07001367 if (mDisplayScalingDisabled) {
1368 pw.println(" noscale");
1369 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07001370 pw.print(" cur=");
1371 pw.print(mDisplayInfo.logicalWidth);
1372 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
1373 pw.print(" app=");
1374 pw.print(mDisplayInfo.appWidth);
1375 pw.print("x"); pw.print(mDisplayInfo.appHeight);
1376 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
1377 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
1378 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
1379 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001380 pw.println(subPrefix + "deferred=" + mDeferredRemoval
1381 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001382
Craig Mautnerdc548482014-02-05 13:35:24 -08001383 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001384 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001385 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1386 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001387 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001388 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001389
Craig Mautnerdc548482014-02-05 13:35:24 -08001390 pw.println();
1391 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001392 pw.println();
1393 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001394 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001395 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001396 pw.print(" Exiting #"); pw.print(i);
1397 pw.print(' '); pw.print(token);
1398 pw.println(':');
1399 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001400 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001401 }
Craig Mautner59c00972012-07-30 12:10:24 -07001402 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001403 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07001404 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001405 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07001406 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001407 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001408
1409 if (mInputMethodAnimLayerAdjustment != 0) {
1410 pw.println(subPrefix
1411 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
1412 }
Craig Mautner59c00972012-07-30 12:10:24 -07001413 }
Craig Mautnere0a38842013-12-16 16:14:02 -08001414
1415 @Override
1416 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001417 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001418 }
1419
1420 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001421 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08001422 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07001423
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08001424 /** Checks if stack with provided id is visible on this display. */
1425 boolean isStackVisible(int stackId) {
1426 final TaskStack stack = getStackById(stackId);
1427 return (stack != null && stack.isVisible());
1428 }
1429
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001430 /**
1431 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
1432 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07001433 TaskStack getDockedStackLocked() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001434 final TaskStack stack = getStackById(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001435 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07001436 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001437
1438 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001439 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02001440 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001441 */
Jorim Jaggife762342016-10-13 14:33:27 +02001442 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001443 return getStackById(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001444 }
1445
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001446 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001447 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001448 final int x = (int) xf;
1449 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001450 final WindowState touchedWin = getWindow(w -> {
1451 final int flags = w.mAttrs.flags;
1452 if (!w.isVisibleLw()) {
1453 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001454 }
1455 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001456 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001457 }
1458
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001459 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001460 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001461 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001462 }
1463
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001464 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001465
1466 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001467 return mTmpRegion.contains(x, y) || touchFlags == 0;
1468 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001469
1470 return touchedWin;
1471 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07001472
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001473 boolean canAddToastWindowForUid(int uid) {
1474 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08001475 // Also if the app is focused adding more than one toast at
1476 // a time for better backwards compatibility.
1477 final WindowState focusedWindowForUid = getWindow(w ->
1478 w.mOwnerUid == uid && w.isFocused());
1479 if (focusedWindowForUid != null) {
1480 return true;
1481 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001482 final WindowState win = getWindow(w ->
1483 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
1484 && !w.mWindowRemovalAllowed);
1485 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001486 }
1487
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001488 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001489 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
1490 return;
1491 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001492
Wale Ogunwale1e129a42016-11-21 13:03:47 -08001493 // Used to communicate the old focus to the callback method.
1494 mTmpWindow = oldFocus;
1495
1496 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001497 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001498
1499 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001500 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001501
Wale Ogunwale1e129a42016-11-21 13:03:47 -08001502 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001503
1504 if (mTmpWindow == null) {
1505 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
1506 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001507 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001508 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001509 }
1510
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001511 /** Updates the layer assignment of windows on this display. */
1512 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08001513 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001514 if (setLayoutNeeded) {
1515 setLayoutNeeded();
1516 }
1517 }
1518
Wale Ogunwale1666e312016-12-16 11:27:18 -08001519 // TODO: This should probably be called any time a visual change is made to the hierarchy like
1520 // moving containers or resizing them. Need to investigate the best way to have it automatically
1521 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001522 void layoutAndAssignWindowLayersIfNeeded() {
1523 mService.mWindowsChanged = true;
1524 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001525
1526 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
1527 false /*updateInputWindows*/)) {
1528 assignWindowLayers(false /* setLayoutNeeded */);
1529 }
1530
1531 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
1532 mService.mWindowPlacerLocked.performSurfacePlacement();
1533 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
1534 }
1535
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001536 /** Returns true if a leaked surface was destroyed */
1537 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001538 // Used to indicate that a surface was leaked.
1539 mTmpWindow = null;
1540 forAllWindows(w -> {
1541 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001542 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001543 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001544 }
1545 if (!mService.mSessions.contains(wsa.mSession)) {
1546 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001547 + w + " surface=" + wsa.mSurfaceController
1548 + " token=" + w.mToken
1549 + " pid=" + w.mSession.mPid
1550 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001551 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001552 mService.mForceRemoves.add(w);
1553 mTmpWindow = w;
1554 } else if (w.mAppToken != null && w.mAppToken.clientHidden) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001555 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001556 + w + " surface=" + wsa.mSurfaceController
1557 + " token=" + w.mAppToken
1558 + " saved=" + w.hasSavedSurface());
1559 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001560 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001561 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001562 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001563 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001564
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001565 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001566 }
1567
1568 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001569 * Determine and return the window that should be the IME target.
1570 * @param updateImeTarget If true the system IME target will be updated to match what we found.
1571 * @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 +00001572 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001573 WindowState computeImeTarget(boolean updateImeTarget) {
1574 if (mService.mInputMethodWindow == null) {
1575 // There isn't an IME so there shouldn't be a target...That was easy!
1576 if (updateImeTarget) {
1577 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
1578 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
1579 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
1580 }
1581 return null;
1582 }
1583
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001584 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
1585 // same display. Or even when the current IME/target are not on the same screen as the next
1586 // IME/target. For now only look for input windows on the main screen.
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001587
Wale Ogunwaledaab8652017-02-23 22:11:36 -08001588 // The target candidate provided by the IME tells us which window token, but not which
1589 // window within the token (e.g. child windows...). So, we use the token to look-up the
1590 // best target window.
1591 // TODO: Have the input method service report the right window with the token vs. just the
1592 // base window of the token.
1593 final WindowState baseWin = mService.getWindow(mService.mInputMethodTargetCandidate);
1594 final WindowToken targetToken = baseWin != null ? baseWin.mToken : null;
1595 WindowState target = targetToken != null ?
1596 targetToken.getWindow(mComputeImeTargetPredicate) : null;
1597 // If there isn't a better candidate in the token (maybe because they are not visible), then
1598 // fall back to targeting the base window of the token, so the IME can still maintain the
1599 // right z-order based on the last person that set it vs. changing its z-order to the very
1600 // up since there if target is null.
1601 // TODO: Consider z-ordering IME to bottom instead of top if there is no visible target.
1602 // Also, consider tying the visible the visibility of the IME to the current target. I.e if
1603 // target isn't visible, then IME shouldn't be visible.
1604 target = target == null ? baseWin : target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001605
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001606 // Yet more tricksyness! If this window is a "starting" window, we do actually want
1607 // to be on top of it, but it is not -really- where input will go. So look down below
1608 // for a real window to target...
1609 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
1610 final AppWindowToken token = target.mAppToken;
1611 if (token != null) {
1612 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
1613 if (betterTarget != null) {
1614 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001615 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001616 }
1617 }
1618
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001619 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
1620 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001621
1622 // Now, a special case -- if the last target's window is in the process of exiting, and is
1623 // above the new target, keep on the last target to avoid flicker. Consider for example a
1624 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
1625 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
1626 // scrim.
1627 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001628 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
1629 && (target == null
1630 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001631 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001632 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001633 }
1634
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001635 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
1636 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001637
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001638 if (target == null) {
1639 if (updateImeTarget) {
1640 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
1641 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
1642 + Debug.getCallers(4) : ""));
1643 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
1644 }
1645
1646 return null;
1647 }
1648
1649 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001650 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
1651 if (token != null) {
1652
1653 // Now some fun for dealing with window animations that modify the Z order. We need
1654 // to look at all windows below the current target that are in this app, finding the
1655 // highest visible one in layering.
1656 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001657 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001658 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001659 }
1660
1661 if (highestTarget != null) {
1662 final AppTransition appTransition = mService.mAppTransition;
1663 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
1664 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
1665 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001666 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001667
1668 if (appTransition.isTransitionSet()) {
1669 // If we are currently setting up for an animation, hold everything until we
1670 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001671 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
1672 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001673 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001674 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001675 // If the window we are currently targeting is involved with an animation,
1676 // and it is on top of the next target we will be over, then hold off on
1677 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001678 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
1679 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001680 }
1681 }
1682 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001683
1684 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
1685 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
1686 setInputMethodTarget(target, false, target.mAppToken != null
Robert Carrdee1b3f2017-02-27 11:33:33 -08001687 ? target.mAppToken.getAnimLayerAdjustment() : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001688 }
1689
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001690 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001691 }
1692
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001693 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
1694 if (target == mService.mInputMethodTarget
1695 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
1696 && mInputMethodAnimLayerAdjustment == layerAdj) {
1697 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001698 }
1699
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001700 mService.mInputMethodTarget = target;
1701 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
1702 setInputMethodAnimLayerAdjustment(layerAdj);
1703 assignWindowLayers(false /* setLayoutNeeded */);
1704 }
1705
1706 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
1707 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
1708 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
1709 }
1710
1711 // Used to indicate we have reached the first window in the range we are interested in.
1712 mTmpWindow = null;
1713
1714 // TODO: Figure-out a more efficient way to do this.
1715 final WindowState candidate = getWindow(w -> {
1716 if (w == top) {
1717 // Reached the first window in the range we are interested in.
1718 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001719 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001720 if (mTmpWindow == null) {
1721 return false;
1722 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001723
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001724 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
1725 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001726 }
1727 // If we reached the bottom of the range of windows we are considering,
1728 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001729 if (w == bottom) {
1730 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001731 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001732 return false;
1733 });
1734
1735 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001736 }
1737
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001738 void setLayoutNeeded() {
1739 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
1740 mLayoutNeeded = true;
1741 }
1742
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001743 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001744 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
1745 mLayoutNeeded = false;
1746 }
1747
1748 boolean isLayoutNeeded() {
1749 return mLayoutNeeded;
1750 }
1751
Wale Ogunwale02319a62016-09-26 15:21:22 -07001752 void dumpTokens(PrintWriter pw, boolean dumpAll) {
1753 if (mTokenMap.isEmpty()) {
1754 return;
1755 }
1756 pw.println(" Display #" + mDisplayId);
1757 final Iterator<WindowToken> it = mTokenMap.values().iterator();
1758 while (it.hasNext()) {
1759 final WindowToken token = it.next();
1760 pw.print(" ");
1761 pw.print(token);
1762 if (dumpAll) {
1763 pw.println(':');
1764 token.dump(pw, " ");
1765 } else {
1766 pw.println();
1767 }
1768 }
1769 }
1770
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001771 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001772 final int[] index = new int[1];
1773 forAllWindows(w -> {
1774 final WindowStateAnimator wAnim = w.mWinAnimator;
1775 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
1776 index[0] = index[0] + 1;
1777 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001778 }
1779
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001780 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001781 forAllWindows(w -> {
1782 w.mWinAnimator.enableSurfaceTrace(fd);
1783 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001784 }
1785
1786 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001787 forAllWindows(w -> {
1788 w.mWinAnimator.disableSurfaceTrace();
1789 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001790 }
1791
Jorim Jaggife762342016-10-13 14:33:27 +02001792 /**
1793 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
1794 */
1795 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
1796 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001797 forAllWindows(w -> {
1798 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)) {
1799 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02001800 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
1801 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001802 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02001803 }
1804
Wale Ogunwale494009b82016-10-21 09:01:38 -07001805 boolean checkWaitingForWindows() {
1806
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001807 mHaveBootMsg = false;
1808 mHaveApp = false;
1809 mHaveWallpaper = false;
1810 mHaveKeyguard = true;
1811
1812 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07001813 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
1814 return true;
1815 }
1816 if (w.isDrawnLw()) {
1817 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001818 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001819 } else if (w.mAttrs.type == TYPE_APPLICATION
1820 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001821 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001822 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001823 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001824 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001825 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07001826 }
1827 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001828 return false;
1829 });
1830
1831 if (visibleWindow != null) {
1832 // We have a visible window.
1833 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001834 }
1835
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001836 // if the wallpaper service is disabled on the device, we're never going to have
1837 // wallpaper, don't bother waiting for it
1838 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
1839 com.android.internal.R.bool.config_enableWallpaperService)
1840 && !mService.mOnlyCore;
1841
Wale Ogunwale494009b82016-10-21 09:01:38 -07001842 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
1843 "******** booted=" + mService.mSystemBooted
1844 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001845 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
1846 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
1847 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07001848
1849 // If we are turning on the screen to show the boot message, don't do it until the boot
1850 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001851 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07001852 return true;
1853 }
1854
1855 // If we are turning on the screen after the boot is completed normally, don't do so until
1856 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001857 if (mService.mSystemBooted
1858 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07001859 return true;
1860 }
1861
1862 return false;
1863 }
1864
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001865 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08001866 mTmpWindowAnimator = animator;
1867 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001868 }
1869
1870 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07001871 resetAnimationBackgroundAnimator();
1872
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001873 // Used to indicate a detached wallpaper.
1874 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08001875 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001876
Wale Ogunwale1e129a42016-11-21 13:03:47 -08001877 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001878
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001879 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001880 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001881 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
1882 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001883 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
1884 }
1885 }
1886
1887 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08001888 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001889 }
1890
Wale Ogunwale494009b82016-10-21 09:01:38 -07001891 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001892 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001893 if (imFocus == null) {
1894 return false;
1895 }
1896
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001897 if (DEBUG_INPUT_METHOD) {
1898 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
1899 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
1900 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001901 }
1902
Wale Ogunwale494009b82016-10-21 09:01:38 -07001903 final IInputMethodClient imeClient = imFocus.mSession.mClient;
1904
1905 if (DEBUG_INPUT_METHOD) {
1906 Slog.i(TAG_WM, "IM target client: " + imeClient);
1907 if (imeClient != null) {
1908 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
1909 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
1910 }
1911 }
1912
1913 return imeClient != null && imeClient.asBinder() == client.asBinder();
1914 }
1915
1916 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001917 final WindowState win = getWindow(
1918 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
1919 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001920 }
1921
1922 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001923 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07001924 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001925 final int curValue = w.mSystemUiVisibility;
1926 final int diff = (curValue ^ visibility) & globalDiff;
1927 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07001928 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001929 w.mSeq++;
1930 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001931 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001932 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
1933 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07001934 visibility, newValue, diff);
1935 }
1936 } catch (RemoteException e) {
1937 // so sorry
1938 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001939 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07001940 }
1941
1942 void onWindowFreezeTimeout() {
1943 Slog.w(TAG_WM, "Window freeze timeout expired.");
1944 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001945
1946 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07001947 if (!w.mOrientationChanging) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001948 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001949 }
1950 w.mOrientationChanging = false;
1951 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
1952 - mService.mDisplayFreezeTime);
1953 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001954 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07001955 mService.mWindowPlacerLocked.performSurfacePlacement();
1956 }
1957
1958 void waitForAllWindowsDrawn() {
1959 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001960 forAllWindows(w -> {
1961 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
1962 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
1963 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001964 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001965 w.mLastContentInsets.set(-1, -1, -1, -1);
1966 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07001967 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001968 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07001969 }
1970
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001971 // TODO: Super crazy long method that should be broken down...
1972 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
1973
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001974 final int dw = mDisplayInfo.logicalWidth;
1975 final int dh = mDisplayInfo.logicalHeight;
1976 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
1977
1978 mTmpUpdateAllDrawn.clear();
1979
1980 int repeats = 0;
1981 do {
1982 repeats++;
1983 if (repeats > 6) {
1984 Slog.w(TAG, "Animation repeat aborted after too many iterations");
1985 clearLayoutNeeded();
1986 break;
1987 }
1988
1989 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
1990 pendingLayoutChanges);
1991
Andrii Kulian839def92016-11-02 10:58:58 -07001992 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
1993 // the wallpaper window jumping across displays.
1994 // Remove check for default display when there will be support for multiple wallpaper
1995 // targets (on different displays).
1996 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001997 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001998 }
1999
2000 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2001 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2002 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2003 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002004 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002005 }
2006 }
2007
2008 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2009 setLayoutNeeded();
2010 }
2011
2012 // FIRST LOOP: Perform a layout, if needed.
2013 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2014 performLayout(repeats == 1, false /* updateInputWindows */);
2015 } else {
2016 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2017 }
2018
2019 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2020 pendingLayoutChanges = 0;
2021
2022 if (isDefaultDisplay) {
2023 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002024 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002025 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2026 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2027 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2028 }
2029 } while (pendingLayoutChanges != 0);
2030
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002031 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002032 resetDimming();
2033
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002034 mTmpRecoveringMemory = recoveringMemory;
2035 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002036
2037 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002038 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2039 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2040 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002041 true /* inTraversal, must call performTraversalInTrans... below */);
2042
2043 stopDimmingIfNeeded();
2044
2045 while (!mTmpUpdateAllDrawn.isEmpty()) {
2046 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2047 // See if any windows have been drawn, so they (and others associated with them)
2048 // can now be shown.
2049 atoken.updateAllDrawn(this);
2050 }
2051
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002052 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002053 }
2054
2055 void performLayout(boolean initial, boolean updateInputWindows) {
2056 if (!isLayoutNeeded()) {
2057 return;
2058 }
2059 clearLayoutNeeded();
2060
2061 final int dw = mDisplayInfo.logicalWidth;
2062 final int dh = mDisplayInfo.logicalHeight;
2063
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002064 if (DEBUG_LAYOUT) {
2065 Slog.v(TAG, "-------------------------------------");
2066 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2067 }
2068
2069 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mService.mRotation,
2070 getConfiguration().uiMode);
2071 if (isDefaultDisplay) {
2072 // Not needed on non-default displays.
2073 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2074 mService.mScreenRect.set(0, 0, dw, dh);
2075 }
2076
2077 mService.mPolicy.getContentRectLw(mContentRect);
2078
2079 int seq = mService.mLayoutSeq + 1;
2080 if (seq < 0) seq = 0;
2081 mService.mLayoutSeq = seq;
2082
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002083 // Used to indicate that we have processed the dream window and all additional windows are
2084 // behind it.
2085 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002086 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002087
2088 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002089 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002090
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002091 // Used to indicate that we have processed the dream window and all additional attached
2092 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002093 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002094 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002095
2096 // Now perform layout of attached windows, which usually depend on the position of the
2097 // window they are attached to. XXX does not deal with windows that are attached to windows
2098 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002099 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002100
2101 // Window frames may have changed. Tell the input dispatcher about it.
2102 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2103 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2104 if (updateInputWindows) {
2105 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2106 }
2107
2108 mService.mPolicy.finishLayoutLw();
2109 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2110 }
2111
2112 /**
2113 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2114 * In portrait mode, it grabs the full screenshot.
2115 *
2116 * @param width the width of the target bitmap
2117 * @param height the height of the target bitmap
2118 * @param includeFullDisplay true if the screen should not be cropped before capture
2119 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2120 * @param config of the output bitmap
2121 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002122 * @param includeDecor whether to include window decors, like the status or navigation bar
2123 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002124 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002125 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002126 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002127 boolean wallpaperOnly, boolean includeDecor) {
2128 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2129 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002130 if (bitmap == null) {
2131 return null;
2132 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002133
2134 if (DEBUG_SCREENSHOT) {
2135 // TEST IF IT's ALL BLACK
2136 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2137 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2138 bitmap.getHeight());
2139 boolean allBlack = true;
2140 final int firstColor = buffer[0];
2141 for (int i = 0; i < buffer.length; i++) {
2142 if (buffer[i] != firstColor) {
2143 allBlack = false;
2144 break;
2145 }
2146 }
2147 if (allBlack) {
2148 final WindowState appWin = mScreenshotApplicationState.appWin;
2149 final int maxLayer = mScreenshotApplicationState.maxLayer;
2150 final int minLayer = mScreenshotApplicationState.minLayer;
2151 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2152 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2153 (appWin != null ?
2154 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2155 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2156 }
2157 }
2158
2159 // Create a copy of the screenshot that is immutable and backed in ashmem.
2160 // This greatly reduces the overhead of passing the bitmap between processes.
2161 Bitmap ret = bitmap.createAshmemBitmap(config);
2162 bitmap.recycle();
2163 return ret;
2164 }
2165
2166 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2167 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2168 boolean includeDecor) {
2169 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2170 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2171 }
2172
2173 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2174 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2175 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002176 int dw = mDisplayInfo.logicalWidth;
2177 int dh = mDisplayInfo.logicalHeight;
2178 if (dw == 0 || dh == 0) {
2179 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2180 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2181 return null;
2182 }
2183
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002184 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002185
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002186 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002187 final Rect frame = new Rect();
2188 final Rect stackBounds = new Rect();
2189
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002190 boolean includeImeInScreenshot;
2191 synchronized(mService.mWindowMap) {
2192 final AppWindowToken imeTargetAppToken = mService.mInputMethodTarget != null
2193 ? mService.mInputMethodTarget.mAppToken : null;
2194 // We only include the Ime in the screenshot if the app we are screenshoting is the IME
2195 // target and isn't in multi-window mode. We don't screenshot the IME in multi-window
2196 // mode because the frame of the IME might not overlap with that of the app.
2197 // E.g. IME target app at the top in split-screen mode and the IME at the bottom
2198 // overlapping with the bottom app.
2199 includeImeInScreenshot = imeTargetAppToken != null
2200 && imeTargetAppToken.appToken != null
2201 && imeTargetAppToken.appToken.asBinder() == appToken
2202 && !mService.mInputMethodTarget.isInMultiWindowMode();
2203 }
2204
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08002205 final int aboveAppLayer = (mService.mPolicy.getWindowLayerFromTypeLw(TYPE_APPLICATION) + 1)
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002206 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08002207 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002208 synchronized(mService.mWindowMap) {
2209 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002210 mScreenshotApplicationState.appWin = null;
2211 forAllWindows(w -> {
2212 if (!w.mHasSurface) {
2213 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002214 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002215 if (w.mLayer >= aboveAppLayer) {
2216 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002217 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002218 if (wallpaperOnly && !w.mIsWallpaper) {
2219 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002220 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002221 if (w.mIsImWindow) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002222 if (!includeImeInScreenshot) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002223 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002224 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002225 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002226 // If this is the wallpaper layer and we're only looking for the wallpaper layer
2227 // then the target window state is this one.
2228 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002229 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002230 }
2231
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002232 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002233 // We have not ran across the target window yet, so it is probably behind
2234 // the wallpaper. This can happen when the keyguard is up and all windows
2235 // are moved behind the wallpaper. We don't want to include the wallpaper
2236 // layer in the screenshot as it will cover-up the layer of the target
2237 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002238 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002239 }
2240 // Fall through. The target window is in front of the wallpaper. For this
2241 // case we want to include the wallpaper layer in the screenshot because
2242 // the target window might have some transparent areas.
2243 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002244 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002245 // This app window is of no interest if it is not associated with the
2246 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002247 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002248 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002249 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002250 }
2251
2252 // Include this window.
2253
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002254 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002255 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002256 if (mScreenshotApplicationState.maxLayer < layer) {
2257 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002258 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002259 if (mScreenshotApplicationState.minLayer > layer) {
2260 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002261 }
2262
2263 // Don't include wallpaper in bounds calculation
Jorim Jaggibfbd9dc2017-01-20 18:13:01 +01002264 if (!mutableIncludeFullDisplay.value && includeDecor) {
2265 final TaskStack stack = w.getStack();
2266 if (stack != null) {
2267 stack.getBounds(frame);
2268 }
Jorim Jaggi02886a82016-12-06 09:10:06 -08002269 } else if (!mutableIncludeFullDisplay.value && !w.mIsWallpaper) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002270 final Rect wf = w.mFrame;
2271 final Rect cr = w.mContentInsets;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002272 int left = wf.left + cr.left;
2273 int top = wf.top + cr.top;
2274 int right = wf.right - cr.right;
2275 int bottom = wf.bottom - cr.bottom;
2276 frame.union(left, top, right, bottom);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002277 w.getVisibleBounds(stackBounds);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002278 if (!Rect.intersects(frame, stackBounds)) {
2279 // Set frame empty if there's no intersection.
2280 frame.setEmpty();
2281 }
2282 }
2283
2284 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002285 (w.mAppToken != null && w.mAppToken.token == appToken)
2286 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi52e85da2017-01-24 16:21:39 +01002287 if (foundTargetWs && winAnim.getShown()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002288 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002289 }
2290
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002291 if (w.isObscuringDisplay()){
2292 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002293 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002294 return false;
2295 }, true /* traverseTopToBottom */);
2296
2297 final WindowState appWin = mScreenshotApplicationState.appWin;
2298 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
2299 final int maxLayer = mScreenshotApplicationState.maxLayer;
2300 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002301
2302 if (appToken != null && appWin == null) {
2303 // Can't find a window to snapshot.
2304 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
2305 "Screenshot: Couldn't find a surface matching " + appToken);
2306 return null;
2307 }
2308
2309 if (!screenshotReady) {
2310 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
2311 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
2312 appWin.mWinAnimator.mDrawState)));
2313 return null;
2314 }
2315
2316 // Screenshot is ready to be taken. Everything from here below will continue
2317 // through the bottom of the loop and return a value. We only stay in the loop
2318 // because we don't want to release the mWindowMap lock until the screenshot is
2319 // taken.
2320
2321 if (maxLayer == 0) {
2322 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2323 + ": returning null maxLayer=" + maxLayer);
2324 return null;
2325 }
2326
Jorim Jaggi02886a82016-12-06 09:10:06 -08002327 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002328 // Constrain frame to the screen size.
2329 if (!frame.intersect(0, 0, dw, dh)) {
2330 frame.setEmpty();
2331 }
2332 } else {
2333 // Caller just wants entire display.
2334 frame.set(0, 0, dw, dh);
2335 }
2336 if (frame.isEmpty()) {
2337 return null;
2338 }
2339
2340 if (width < 0) {
2341 width = (int) (frame.width() * frameScale);
2342 }
2343 if (height < 0) {
2344 height = (int) (frame.height() * frameScale);
2345 }
2346
2347 // Tell surface flinger what part of the image to crop. Take the top
2348 // right part of the application, and crop the larger dimension to fit.
2349 Rect crop = new Rect(frame);
2350 if (width / (float) frame.width() < height / (float) frame.height()) {
2351 int cropWidth = (int)((float)width / (float)height * frame.height());
2352 crop.right = crop.left + cropWidth;
2353 } else {
2354 int cropHeight = (int)((float)height / (float)width * frame.width());
2355 crop.bottom = crop.top + cropHeight;
2356 }
2357
2358 // The screenshot API does not apply the current screen rotation.
2359 int rot = mDisplay.getRotation();
2360
2361 if (rot == ROTATION_90 || rot == ROTATION_270) {
2362 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
2363 }
2364
2365 // Surfaceflinger is not aware of orientation, so convert our logical
2366 // crop to surfaceflinger's portrait orientation.
2367 convertCropForSurfaceFlinger(crop, rot, dw, dh);
2368
2369 if (DEBUG_SCREENSHOT) {
2370 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
2371 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002372 forAllWindows(w -> {
2373 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
2374 Slog.i(TAG_WM, w + ": " + w.mLayer
2375 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002376 + " surfaceLayer=" + ((controller == null)
2377 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002378 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002379 }
2380
2381 final ScreenRotationAnimation screenRotationAnimation =
2382 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
2383 final boolean inRotation = screenRotationAnimation != null &&
2384 screenRotationAnimation.isAnimating();
2385 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
2386 "Taking screenshot while rotating");
2387
2388 // We force pending transactions to flush before taking
2389 // the screenshot by pushing an empty synchronous transaction.
2390 SurfaceControl.openTransaction();
2391 SurfaceControl.closeTransactionSync();
2392
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002393 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002394 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002395 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002396 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
2397 + ") to layer " + maxLayer);
2398 return null;
2399 }
2400 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002401 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002402 }
2403
2404 // TODO: Can this use createRotationMatrix()?
2405 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
2406 if (rot == Surface.ROTATION_90) {
2407 final int tmp = crop.top;
2408 crop.top = dw - crop.right;
2409 crop.right = crop.bottom;
2410 crop.bottom = dw - crop.left;
2411 crop.left = tmp;
2412 } else if (rot == Surface.ROTATION_180) {
2413 int tmp = crop.top;
2414 crop.top = dh - crop.bottom;
2415 crop.bottom = dh - tmp;
2416 tmp = crop.right;
2417 crop.right = dw - crop.left;
2418 crop.left = dw - tmp;
2419 } else if (rot == Surface.ROTATION_270) {
2420 final int tmp = crop.top;
2421 crop.top = crop.left;
2422 crop.left = dh - crop.bottom;
2423 crop.bottom = crop.right;
2424 crop.right = dh - tmp;
2425 }
2426 }
2427
2428 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002429 // Used to indicate the layout is needed.
2430 mTmpWindow = null;
2431
2432 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002433 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002434 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002435 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002436 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002437 w.setDisplayLayoutNeeded();
2438 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002439 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002440
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002441 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002442 mService.mWindowPlacerLocked.performSurfacePlacement();
2443 }
2444 }
2445
Wale Ogunwale1666e312016-12-16 11:27:18 -08002446 void setExitingTokensHasVisible(boolean hasVisible) {
2447 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
2448 mExitingTokens.get(i).hasVisible = hasVisible;
2449 }
2450
2451 // Initialize state of exiting applications.
2452 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
2453 }
2454
2455 void removeExistingTokensIfPossible() {
2456 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
2457 final WindowToken token = mExitingTokens.get(i);
2458 if (!token.hasVisible) {
2459 mExitingTokens.remove(i);
2460 }
2461 }
2462
2463 // Time to remove any exiting applications?
2464 mTaskStackContainers.removeExistingAppTokensIfPossible();
2465 }
2466
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002467 static final class TaskForResizePointSearchResult {
2468 boolean searchDone;
2469 Task taskForResize;
2470
2471 void reset() {
2472 searchDone = false;
2473 taskForResize = null;
2474 }
2475 }
Robert Carr3b716242016-08-16 16:02:21 -07002476
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002477 private static final class ApplySurfaceChangesTransactionState {
2478 boolean displayHasContent;
2479 boolean obscured;
2480 boolean syswin;
2481 boolean focusDisplayed;
2482 float preferredRefreshRate;
2483 int preferredModeId;
2484
2485 void reset() {
2486 displayHasContent = false;
2487 obscured = false;
2488 syswin = false;
2489 focusDisplayed = false;
2490 preferredRefreshRate = 0;
2491 preferredModeId = 0;
2492 }
2493 }
2494
2495 private static final class ScreenshotApplicationState {
2496 WindowState appWin;
2497 int maxLayer;
2498 int minLayer;
2499 boolean screenshotReady;
2500
2501 void reset(boolean screenshotReady) {
2502 appWin = null;
2503 maxLayer = 0;
2504 minLayer = 0;
2505 this.screenshotReady = screenshotReady;
2506 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
2507 }
2508 }
2509
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002510 /**
2511 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
2512 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
2513 * homogeneous children type which is currently required by sub-classes of
2514 * {@link WindowContainer} class.
2515 */
2516 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
2517
2518 int size() {
2519 return mChildren.size();
2520 }
2521
2522 E get(int index) {
2523 return mChildren.get(index);
2524 }
2525
2526 @Override
2527 boolean fillsParent() {
2528 return true;
2529 }
2530
2531 @Override
2532 boolean isVisible() {
2533 return true;
Robert Carr3b716242016-08-16 16:02:21 -07002534 }
2535 }
2536
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002537 /**
2538 * Window container class that contains all containers on this display relating to Apps.
2539 * I.e Activities.
2540 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07002541 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002542
Andrii Kulian839def92016-11-02 10:58:58 -07002543 /**
2544 * Adds the stack to this container.
2545 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
2546 */
2547 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002548 if (stack.mStackId == HOME_STACK_ID) {
2549 if (mHomeStack != null) {
2550 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
2551 }
2552 mHomeStack = stack;
2553 }
2554 addChild(stack, onTop);
2555 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07002556 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002557
Andrii Kulian839def92016-11-02 10:58:58 -07002558 /** Removes the stack from its container and prepare for changing the parent. */
2559 void removeStackFromDisplay(TaskStack stack) {
2560 removeChild(stack);
2561 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07002562 }
2563
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002564 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08002565 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
2566 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002567 addChild(stack, addIndex);
2568 setLayoutNeeded();
2569 }
2570
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002571 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08002572 void positionChildAt(int position, TaskStack child, boolean includingParents) {
2573 if (StackId.isAlwaysOnTop(child.mStackId) && position != POSITION_TOP) {
2574 // This stack is always-on-top, override the default behavior.
2575 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
2576
2577 // Moving to its current position, as we must call super but we don't want to
2578 // perform any meaningful action.
2579 final int currentPosition = mChildren.indexOf(child);
2580 super.positionChildAt(currentPosition, child, false /* includingParents */);
2581 return;
2582 }
2583
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08002584 final int targetPosition = findPositionForStack(position, child, false /* adding */);
2585 super.positionChildAt(targetPosition, child, includingParents);
2586
2587 setLayoutNeeded();
2588 }
2589
2590 /**
2591 * When stack is added or repositioned, find a proper position for it.
2592 * This will make sure that pinned stack always stays on top.
2593 * @param requestedPosition Position requested by caller.
2594 * @param stack Stack to be added or positioned.
2595 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
2596 * @return The proper position for the stack.
2597 */
2598 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
2599 final int topChildPosition = mChildren.size() - 1;
2600 boolean toTop = requestedPosition == POSITION_TOP;
2601 toTop |= adding ? requestedPosition >= topChildPosition + 1
2602 : requestedPosition >= topChildPosition;
2603 int targetPosition = requestedPosition;
2604
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002605 if (toTop && isStackVisible(PINNED_STACK_ID) && stack.mStackId != PINNED_STACK_ID) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08002606 // The pinned stack is always the top most stack (always-on-top) when it is visible.
2607 TaskStack topStack = mChildren.get(topChildPosition);
2608 if (topStack.mStackId != PINNED_STACK_ID) {
2609 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
2610 }
2611
2612 // So, stack is moved just below the pinned stack.
2613 // When we're adding a new stack the target is the current pinned stack position.
2614 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002615 // stack, because WindowContainer#positionAt() first removes element and then adds
2616 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08002617 targetPosition = adding ? topChildPosition : topChildPosition - 1;
2618 }
2619
2620 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08002621 }
2622
2623 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002624 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
2625 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002626 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002627 if (super.forAllWindows(callback, traverseTopToBottom)) {
2628 return true;
2629 }
2630 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
2631 return true;
2632 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002633 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002634 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
2635 return true;
2636 }
2637 if (super.forAllWindows(callback, traverseTopToBottom)) {
2638 return true;
2639 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002640 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002641 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002642 }
2643
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002644 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002645 boolean traverseTopToBottom) {
2646 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
2647 // app tokens.
2648 // TODO: Investigate if we need to continue to do this or if we can just process them
2649 // in-order.
2650 if (traverseTopToBottom) {
2651 for (int i = mChildren.size() - 1; i >= 0; --i) {
2652 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
2653 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002654 if (appTokens.get(j).forAllWindowsUnchecked(callback,
2655 traverseTopToBottom)) {
2656 return true;
2657 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002658 }
2659 }
2660 } else {
2661 final int count = mChildren.size();
2662 for (int i = 0; i < count; ++i) {
2663 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
2664 final int appTokensCount = appTokens.size();
2665 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002666 if (appTokens.get(j).forAllWindowsUnchecked(callback,
2667 traverseTopToBottom)) {
2668 return true;
2669 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002670 }
2671 }
2672 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002673 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002674 }
2675
Wale Ogunwale1666e312016-12-16 11:27:18 -08002676 void setExitingTokensHasVisible(boolean hasVisible) {
2677 for (int i = mChildren.size() - 1; i >= 0; --i) {
2678 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
2679 for (int j = appTokens.size() - 1; j >= 0; --j) {
2680 appTokens.get(j).hasVisible = hasVisible;
2681 }
2682 }
2683 }
2684
2685 void removeExistingAppTokensIfPossible() {
2686 for (int i = mChildren.size() - 1; i >= 0; --i) {
2687 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
2688 for (int j = appTokens.size() - 1; j >= 0; --j) {
2689 final AppWindowToken token = appTokens.get(j);
2690 if (!token.hasVisible && !mService.mClosingApps.contains(token)
2691 && (!token.mIsExiting || token.isEmpty())) {
2692 // Make sure there is no animation running on this token, so any windows
2693 // associated with it will be removed as soon as their animations are
2694 // complete.
2695 token.mAppAnimator.clearAnimation();
2696 token.mAppAnimator.animating = false;
2697 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
2698 "performLayout: App token exiting now removed" + token);
2699 token.removeIfPossible();
2700 }
2701 }
2702 }
2703 }
2704
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002705 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002706 int getOrientation() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002707 if (isStackVisible(DOCKED_STACK_ID) || isStackVisible(FREEFORM_WORKSPACE_STACK_ID)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002708 // Apps and their containers are not allowed to specify an orientation while the
2709 // docked or freeform stack is visible...except for the home stack/task if the
2710 // docked stack is minimized and it actually set something.
2711 if (mHomeStack != null && mHomeStack.isVisible()
2712 && mDividerControllerLocked.isMinimizedDock()) {
2713 final int orientation = mHomeStack.getOrientation();
2714 if (orientation != SCREEN_ORIENTATION_UNSET) {
2715 return orientation;
2716 }
2717 }
2718 return SCREEN_ORIENTATION_UNSPECIFIED;
2719 }
2720
2721 final int orientation = super.getOrientation();
2722 if (orientation != SCREEN_ORIENTATION_UNSET
2723 && orientation != SCREEN_ORIENTATION_BEHIND) {
2724 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
2725 "App is requesting an orientation, return " + orientation);
2726 return orientation;
2727 }
2728
2729 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
2730 "No app is requesting an orientation, return " + mService.mLastOrientation);
2731 // The next app has not been requested to be visible, so we keep the current orientation
2732 // to prevent freezing/unfreezing the display too early.
2733 return mService.mLastOrientation;
2734 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002735 }
2736
2737 /**
2738 * Window container class that contains all containers on this display that are not related to
2739 * Apps. E.g. status bar.
2740 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07002741 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
2742 /**
2743 * Compares two child window tokens returns -1 if the first is lesser than the second in
2744 * terms of z-order and 1 otherwise.
2745 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002746 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07002747 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08002748 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
2749 token1.mOwnerCanManageAppTokens)
2750 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
2751 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002752
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002753 private final Predicate<WindowState> mGetOrientingWindow = w -> {
2754 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
2755 return false;
2756 }
2757 final int req = w.mAttrs.screenOrientation;
2758 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
2759 || req == SCREEN_ORIENTATION_UNSET) {
2760 return false;
2761 }
2762 return true;
2763 };
2764
Wale Ogunwale3a931692016-11-02 16:49:48 -07002765 private final String mName;
2766 NonAppWindowContainers(String name) {
2767 mName = name;
2768 }
2769
2770 void addChild(WindowToken token) {
2771 addChild(token, mWindowComparator);
2772 }
2773
2774 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002775 int getOrientation() {
2776 final WindowManagerPolicy policy = mService.mPolicy;
2777 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002778 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002779
2780 if (win != null) {
2781 final int req = win.mAttrs.screenOrientation;
2782 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
2783 if (policy.isKeyguardHostWindow(win.mAttrs)) {
2784 mService.mLastKeyguardForcedOrientation = req;
2785 }
2786 return (mService.mLastWindowForcedOrientation = req);
2787 }
2788
2789 mService.mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
2790
2791 if (policy.isKeyguardShowingAndNotOccluded()) {
2792 return mService.mLastKeyguardForcedOrientation;
2793 }
2794
2795 return SCREEN_ORIENTATION_UNSET;
2796 }
2797
2798 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07002799 String getName() {
2800 return mName;
2801 }
Robert Carr3b716242016-08-16 16:02:21 -07002802 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002803
2804 /**
2805 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
2806 */
2807 @FunctionalInterface
2808 private interface Screenshoter<E> {
2809 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
2810 boolean useIdentityTransform, int rotation);
2811 }
Craig Mautner59c00972012-07-30 12:10:24 -07002812}