blob: 3a74ded144b875f5eaf363b1263b251b364cdc0f [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
344 private final Consumer<WindowState> mSetInputMethodAnimLayerAdjustment =
345 w -> w.adjustAnimLayer(mInputMethodAnimLayerAdjustment);
346
347 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
348 final int lostFocusUid = mTmpWindow.mOwnerUid;
349 final Handler handler = mService.mH;
350 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
351 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
352 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
353 w.mAttrs.hideTimeoutMilliseconds);
354 }
355 }
356 };
357
358 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
359 final AppWindowToken focusedApp = mService.mFocusedApp;
360 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
361 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
362
363 if (!w.canReceiveKeys()) {
364 return false;
365 }
366
367 final AppWindowToken wtoken = w.mAppToken;
368
369 // If this window's application has been removed, just skip it.
370 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
371 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
372 + (wtoken.removed ? "removed" : "sendingToBottom"));
373 return false;
374 }
375
376 if (focusedApp == null) {
377 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
378 + " using new focus @ " + w);
379 mTmpWindow = w;
380 return true;
381 }
382
383 if (!focusedApp.windowsAreFocusable()) {
384 // Current focused app windows aren't focusable...
385 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
386 + " focusable using new focus @ " + w);
387 mTmpWindow = w;
388 return true;
389 }
390
391 // Descend through all of the app tokens and find the first that either matches
392 // win.mAppToken (return win) or mFocusedApp (return null).
393 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
394 if (focusedApp.compareTo(wtoken) > 0) {
395 // App stack below focused app stack. No focus for you!!!
396 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
397 "findFocusedWindow: Reached focused app=" + focusedApp);
398 mTmpWindow = null;
399 return true;
400 }
401 }
402
403 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
404 mTmpWindow = w;
405 return true;
406 };
407
408 private final Consumer<WindowState> mPrepareWindowSurfaces =
409 w -> w.mWinAnimator.prepareSurfaceLocked(true);
410
411 private final Consumer<WindowState> mPerformLayout = w -> {
412 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
413 // wasting time and funky changes while a window is animating away.
414 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
415 || w.isGoneForLayoutLw();
416
417 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
418 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
419 + " mLayoutAttached=" + w.mLayoutAttached
420 + " screen changed=" + w.isConfigChanged());
421 final AppWindowToken atoken = w.mAppToken;
422 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
423 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
424 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
425 + " parentHidden=" + w.isParentWindowHidden());
426 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
427 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.hidden
428 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
429 + " parentHidden=" + w.isParentWindowHidden());
430 }
431
432 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
433 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
434 // since that means "perform layout as normal, just don't display").
435 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
436 || ((w.isConfigChanged() || w.setReportResizeHints())
437 && !w.isGoneForLayoutLw() &&
438 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
439 (w.mHasSurface && w.mAppToken != null &&
440 w.mAppToken.layoutConfigChanges)))) {
441 if (!w.mLayoutAttached) {
442 if (mTmpInitial) {
443 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
444 w.mContentChanged = false;
445 }
446 if (w.mAttrs.type == TYPE_DREAM) {
447 // Don't layout windows behind a dream, so that if it does stuff like hide
448 // the status bar we won't get a bad transition when it goes away.
449 mTmpWindow = w;
450 }
451 w.mLayoutNeeded = false;
452 w.prelayout();
453 mService.mPolicy.layoutWindowLw(w, null);
454 w.mLayoutSeq = mService.mLayoutSeq;
455
456 // Window frames may have changed. Update dim layer with the new bounds.
457 final Task task = w.getTask();
458 if (task != null) {
459 mDimLayerController.updateDimLayer(task);
460 }
461
462 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
463 + " mContainingFrame=" + w.mContainingFrame
464 + " mDisplayFrame=" + w.mDisplayFrame);
465 }
466 }
467 };
468
469 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
470 if (w.mLayoutAttached) {
471 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
472 + " mViewVisibility=" + w.mViewVisibility
473 + " mRelayoutCalled=" + w.mRelayoutCalled);
474 // If this view is GONE, then skip it -- keep the current frame, and let the caller
475 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
476 // windows, since that means "perform layout as normal, just don't display").
477 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
478 return;
479 }
480 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
481 || w.mLayoutNeeded) {
482 if (mTmpInitial) {
483 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
484 w.mContentChanged = false;
485 }
486 w.mLayoutNeeded = false;
487 w.prelayout();
488 mService.mPolicy.layoutWindowLw(w, w.getParentWindow());
489 w.mLayoutSeq = mService.mLayoutSeq;
490 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
491 + " mContainingFrame=" + w.mContainingFrame
492 + " mDisplayFrame=" + w.mDisplayFrame);
493 }
494 } else if (w.mAttrs.type == TYPE_DREAM) {
495 // Don't layout windows behind a dream, so that if it does stuff like hide the
496 // status bar we won't get a bad transition when it goes away.
497 mTmpWindow = mTmpWindow2;
498 }
499 };
500
501 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
502 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
503 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
504 return w.canBeImeTarget();
505 };
506
507 private final Consumer<WindowState> mApplyPostLayoutPolicy =
508 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
509 mService.mInputMethodTarget);
510
511 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
512 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
513 final boolean obscuredChanged = w.mObscured !=
514 mTmpApplySurfaceChangesTransactionState.obscured;
515 final RootWindowContainer root = mService.mRoot;
516 // Only used if default window
517 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
518
519 // Update effect.
520 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
521 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
522 final boolean isDisplayed = w.isDisplayedLw();
523
524 if (isDisplayed && w.isObscuringDisplay()) {
525 // This window completely covers everything behind it, so we want to leave all
526 // of them as undimmed (for performance reasons).
527 root.mObscuringWindow = w;
528 mTmpApplySurfaceChangesTransactionState.obscured = true;
529 }
530
531 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
532 root.handleNotObscuredLocked(w,
533 mTmpApplySurfaceChangesTransactionState.obscured,
534 mTmpApplySurfaceChangesTransactionState.syswin);
535
536 if (w.mHasSurface && isDisplayed) {
537 final int type = w.mAttrs.type;
538 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
539 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
540 mTmpApplySurfaceChangesTransactionState.syswin = true;
541 }
542 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
543 && w.mAttrs.preferredRefreshRate != 0) {
544 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
545 = w.mAttrs.preferredRefreshRate;
546 }
547 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
548 && w.mAttrs.preferredDisplayModeId != 0) {
549 mTmpApplySurfaceChangesTransactionState.preferredModeId
550 = w.mAttrs.preferredDisplayModeId;
551 }
552 }
553 }
554
555 w.applyDimLayerIfNeeded();
556
557 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
558 && mWallpaperController.isWallpaperTarget(w)) {
559 // This is the wallpaper target and its obscured state changed... make sure the
560 // current wallpaper's visibility has been updated accordingly.
561 mWallpaperController.updateWallpaperVisibility();
562 }
563
564 w.handleWindowMovedIfNeeded();
565
566 final WindowStateAnimator winAnimator = w.mWinAnimator;
567
568 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
569 w.mContentChanged = false;
570
571 // Moved from updateWindowsAndWallpaperLocked().
572 if (w.mHasSurface) {
573 // Take care of the window being ready to display.
574 final boolean committed = winAnimator.commitFinishDrawingLocked();
575 if (isDefaultDisplay && committed) {
576 if (w.mAttrs.type == TYPE_DREAM) {
577 // HACK: When a dream is shown, it may at that point hide the lock screen.
578 // So we need to redo the layout to let the phone window manager make this
579 // happen.
580 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
581 if (DEBUG_LAYOUT_REPEATS) {
582 surfacePlacer.debugLayoutRepeats(
583 "dream and commitFinishDrawingLocked true",
584 pendingLayoutChanges);
585 }
586 }
587 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
588 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
589 "First draw done in potential wallpaper target " + w);
590 root.mWallpaperMayChange = true;
591 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
592 if (DEBUG_LAYOUT_REPEATS) {
593 surfacePlacer.debugLayoutRepeats(
594 "wallpaper and commitFinishDrawingLocked true",
595 pendingLayoutChanges);
596 }
597 }
598 }
599 if (!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening()) {
600 // Updates the shown frame before we set up the surface. This is needed
601 // because the resizing could change the top-left position (in addition to
602 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
603 // position the surface.
604 //
605 // If an animation is being started, we can't call this method because the
606 // animation hasn't processed its initial transformation yet, but in general
607 // we do want to update the position if the window is animating.
608 winAnimator.computeShownFrameLocked();
609 }
610 winAnimator.setSurfaceBoundariesLocked(mTmpRecoveringMemory /* recoveringMemory */);
611 }
612
613 final AppWindowToken atoken = w.mAppToken;
614 if (atoken != null) {
615 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
616 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
617 mTmpUpdateAllDrawn.add(atoken);
618 }
619 }
620
621 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
622 && w.isDisplayedLw()) {
623 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
624 }
625
626 w.updateResizingWindowIfNeeded();
627 };
628
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800629 /**
Craig Mautner2d5618c2012-10-18 13:55:47 -0700630 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800631 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700632 * @param layersController window layer controller used to assign layer to the windows on this
633 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700634 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
635 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700636 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700637 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700638 WindowLayersController layersController, WallpaperController wallpaperController) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800639
640 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
641 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
642 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
643 + " new=" + display);
644 }
645
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700646 mDisplay = display;
647 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700648 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700649 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700650 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700651 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700652 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700653 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700654 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800655 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700656 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800657 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700658
659 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700660 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700661 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700662 super.addChild(mAboveAppWindowsContainers, null);
663 super.addChild(mImeWindowsContainers, null);
Craig Mautner59c00972012-07-30 12:10:24 -0700664 }
665
666 int getDisplayId() {
667 return mDisplayId;
668 }
669
Wale Ogunwale02319a62016-09-26 15:21:22 -0700670 WindowToken getWindowToken(IBinder binder) {
671 return mTokenMap.get(binder);
672 }
673
674 AppWindowToken getAppWindowToken(IBinder binder) {
675 final WindowToken token = getWindowToken(binder);
676 if (token == null) {
677 return null;
678 }
679 return token.asAppWindowToken();
680 }
681
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800682 void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700683 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
684 if (dc != null) {
685 // We currently don't support adding a window token to the display if the display
686 // already has the binder mapped to another token. If there is a use case for supporting
687 // this moving forward we will either need to merge the WindowTokens some how or have
688 // the binder map to a list of window tokens.
689 throw new IllegalArgumentException("Can't map token=" + token + " to display=" + this
690 + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
691 }
692 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700693
694 if (token.asAppWindowToken() == null) {
695 // Add non-app token to container hierarchy on the display. App tokens are added through
696 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700697 switch (token.windowType) {
698 case TYPE_WALLPAPER:
699 mBelowAppWindowsContainers.addChild(token);
700 break;
701 case TYPE_INPUT_METHOD:
702 case TYPE_INPUT_METHOD_DIALOG:
703 mImeWindowsContainers.addChild(token);
704 break;
705 default:
706 mAboveAppWindowsContainers.addChild(token);
707 break;
708 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700709 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700710 }
711
712 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700713 final WindowToken token = mTokenMap.remove(binder);
714 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800715 token.setExiting();
716 }
717 return token;
718 }
719
720 /** Changes the display the input window token is housed on to this one. */
721 void reParentWindowToken(WindowToken token) {
722 final DisplayContent prevDc = token.getDisplayContent();
723 if (prevDc == this) {
724 return;
725 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800726 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
727 && token.asAppWindowToken() == null) {
728 // Removed the token from the map, but made sure it's not an app token before removing
729 // from parent.
730 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700731 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800732
733 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700734 }
735
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700736 void removeAppToken(IBinder binder) {
737 final WindowToken token = removeWindowToken(binder);
738 if (token == null) {
739 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
740 return;
741 }
742
743 final AppWindowToken appToken = token.asAppWindowToken();
744
745 if (appToken == null) {
746 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
747 return;
748 }
749
750 appToken.onRemovedFromDisplay();
751 }
752
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700753 Display getDisplay() {
754 return mDisplay;
755 }
756
Craig Mautner59c00972012-07-30 12:10:24 -0700757 DisplayInfo getDisplayInfo() {
758 return mDisplayInfo;
759 }
760
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700761 DisplayMetrics getDisplayMetrics() {
762 return mDisplayMetrics;
763 }
764
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100765 DockedStackDividerController getDockedDividerController() {
766 return mDividerControllerLocked;
767 }
768
Winson Chung655332c2016-10-31 13:14:28 -0700769 PinnedStackController getPinnedStackController() {
770 return mPinnedStackControllerLocked;
771 }
772
Jeff Browna506a6e2013-06-04 00:02:38 -0700773 /**
774 * Returns true if the specified UID has access to this display.
775 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700776 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700777 return mDisplay.hasAccess(uid);
778 }
779
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700780 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700781 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -0700782 }
783
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700784 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700785 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800786 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -0800787 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700788 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700789 }
790
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700791 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700792 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
793 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700794 if (stack.mStackId == stackId) {
795 return stack;
796 }
797 }
798 return null;
799 }
800
Andrii Kulian441e4492016-09-29 15:25:00 -0700801 @Override
802 void onConfigurationChanged(Configuration newParentConfig) {
803 super.onConfigurationChanged(newParentConfig);
804
Andrii Kulian3a507b52016-09-19 18:14:12 -0700805 // The display size information is heavily dependent on the resources in the current
806 // configuration, so we need to reconfigure it every time the configuration changes.
807 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
808 mService.reconfigureDisplayLocked(this);
809
810 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -0700811 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -0700812 }
Andrii Kulian3a507b52016-09-19 18:14:12 -0700813
Andrii Kulian441e4492016-09-29 15:25:00 -0700814 /**
815 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
816 * bounds were updated.
817 */
818 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700819 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
820 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -0700821 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -0700822 changedStackList.add(stack.mStackId);
823 }
824 }
825 }
826
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700827 @Override
828 boolean fillsParent() {
829 return true;
830 }
831
832 @Override
833 boolean isVisible() {
834 return true;
835 }
836
837 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700838 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -0700839 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000840 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700841 }
842
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700843 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800844 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
845 // Special handling so we can process IME windows with #forAllImeWindows above their IME
846 // target, or here in order if there isn't an IME target.
847 if (traverseTopToBottom) {
848 for (int i = mChildren.size() - 1; i >= 0; --i) {
849 final DisplayChildWindowContainer child = mChildren.get(i);
850 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
851 // In this case the Ime windows will be processed above their target so we skip
852 // here.
853 continue;
854 }
855 if (child.forAllWindows(callback, traverseTopToBottom)) {
856 return true;
857 }
858 }
859 } else {
860 final int count = mChildren.size();
861 for (int i = 0; i < count; i++) {
862 final DisplayChildWindowContainer child = mChildren.get(i);
863 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null) {
864 // In this case the Ime windows will be processed above their target so we skip
865 // here.
866 continue;
867 }
868 if (child.forAllWindows(callback, traverseTopToBottom)) {
869 return true;
870 }
871 }
872 }
873 return false;
874 }
875
876 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
877 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
878 }
879
880 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -0700881 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700882 final WindowManagerPolicy policy = mService.mPolicy;
883
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700884 if (mService.mDisplayFrozen) {
885 if (mService.mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
886 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
887 "Display is frozen, return " + mService.mLastWindowForcedOrientation);
888 // If the display is frozen, some activities may be in the middle of restarting, and
889 // thus have removed their old window. If the window has the flag to hide the lock
890 // screen, then the lock screen can re-appear and inflict its own orientation on us.
891 // Keep the orientation stable until this all settles down.
892 return mService.mLastWindowForcedOrientation;
893 } else if (policy.isKeyguardLocked()) {
894 // Use the last orientation the while the display is frozen with the keyguard
895 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
896 // window. We don't want to check the show when locked window directly though as
897 // things aren't stable while the display is frozen, for example the window could be
898 // momentarily unavailable due to activity relaunch.
899 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
900 + "return " + mService.mLastOrientation);
901 return mService.mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -0700902 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700903 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000904 final int orientation = mAboveAppWindowsContainers.getOrientation();
905 if (orientation != SCREEN_ORIENTATION_UNSET) {
906 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700907 }
Wale Ogunwale51362492016-09-08 17:49:17 -0700908 }
909
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700910 // Top system windows are not requesting an orientation. Start searching from apps.
911 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -0700912 }
913
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700914 void updateDisplayInfo() {
Craig Mautner722285e2012-09-07 13:55:58 -0700915 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700916 mDisplay.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700917 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
918 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800919 }
Craig Mautner722285e2012-09-07 13:55:58 -0700920 }
921
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700922 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700923 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
924 if (displayManagerInternal != null) {
925 // Bootstrap the default logical display from the display manager.
926 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
927 if (newDisplayInfo != null) {
928 mDisplayInfo.copyFrom(newDisplayInfo);
929 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700930 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700931
Filip Gruszczynski608797e2015-11-12 19:08:20 -0800932 mBaseDisplayWidth = mInitialDisplayWidth = mDisplayInfo.logicalWidth;
933 mBaseDisplayHeight = mInitialDisplayHeight = mDisplayInfo.logicalHeight;
934 mBaseDisplayDensity = mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
935 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700936 }
937
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700938 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700939 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800940 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700941 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800942 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
943 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700944 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800945 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700946 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800947 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700948 out.set(left, top, left + width, top + height);
949 }
950
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700951 private void getLogicalDisplayRect(Rect out, int orientation) {
952 getLogicalDisplayRect(out);
953
954 // Rotate the Rect if needed.
955 final int currentRotation = mDisplayInfo.rotation;
956 final int rotationDelta = deltaRotation(currentRotation, orientation);
957 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
958 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
959 mTmpRectF.set(out);
960 mTmpMatrix.mapRect(mTmpRectF);
961 mTmpRectF.round(out);
962 }
963 }
964
Chong Zhangf66db432016-01-13 10:39:51 -0800965 void getContentRect(Rect out) {
966 out.set(mContentRect);
967 }
968
Wale Ogunwale1666e312016-12-16 11:27:18 -0800969 TaskStack addStackToDisplay(int stackId, boolean onTop) {
970 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
971 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -0700972
Wale Ogunwale1666e312016-12-16 11:27:18 -0800973 TaskStack stack = getStackById(stackId);
974 if (stack != null) {
975 // It's already attached to the display...clear mDeferRemoval and move stack to
976 // appropriate z-order on display as needed.
977 stack.mDeferRemoval = false;
978 // We're not moving the display to front when we're adding stacks, only when
979 // requested to change the position of stack explicitly.
980 mTaskStackContainers.positionChildAt(onTop ? POSITION_TOP : POSITION_BOTTOM, stack,
981 false /* includingParents */);
Andrii Kulian839def92016-11-02 10:58:58 -0700982 } else {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800983 stack = new TaskStack(mService, stackId);
Andrii Kulian839def92016-11-02 10:58:58 -0700984 mTaskStackContainers.addStackToDisplay(stack, onTop);
985 }
986
Wale Ogunwale1666e312016-12-16 11:27:18 -0800987 if (stackId == DOCKED_STACK_ID) {
988 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Andrii Kulian839def92016-11-02 10:58:58 -0700989 }
Wale Ogunwale1666e312016-12-16 11:27:18 -0800990 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -0700991 }
992
Wale Ogunwale1666e312016-12-16 11:27:18 -0800993 void moveStackToDisplay(TaskStack stack) {
994 final DisplayContent prevDc = stack.getDisplayContent();
995 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -0700996 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
997 + " which is not currently attached to any display");
998 }
Wale Ogunwale1666e312016-12-16 11:27:18 -0800999 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001000 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1001 + " to its current displayId=" + mDisplayId);
1002 }
1003
Wale Ogunwale1666e312016-12-16 11:27:18 -08001004 prevDc.mTaskStackContainers.removeStackFromDisplay(stack);
1005 mTaskStackContainers.addStackToDisplay(stack, true /* onTop */);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001006 }
1007
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001008 @Override
1009 protected void addChild(DisplayChildWindowContainer child,
1010 Comparator<DisplayChildWindowContainer> comparator) {
1011 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1012 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001013
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001014 @Override
1015 protected void addChild(DisplayChildWindowContainer child, int index) {
1016 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1017 }
1018
1019 @Override
1020 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001021 // Only allow removal of direct children from this display if the display is in the process
1022 // of been removed.
1023 if (mRemovingDisplay) {
1024 super.removeChild(child);
1025 return;
1026 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001027 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001028 }
1029
Andrii Kuliand2765632016-12-12 22:26:34 -08001030 @Override
1031 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1032 // Children of the display are statically ordered, so the real intention here is to perform
1033 // the operation on the display and not the static direct children.
1034 getParent().positionChildAt(position, this, includingParents);
1035 }
1036
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001037 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001038 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1039 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001040 final int taskId = stack.taskIdFromPoint(x, y);
1041 if (taskId != -1) {
1042 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001043 }
1044 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001045 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001046 }
1047
Chong Zhang8e89b312015-09-09 15:09:30 -07001048 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001049 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001050 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001051 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001052 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001053 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001054 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001055 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1056 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -07001057 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001058 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001059 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001060
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001061 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1062 if (mTmpTaskForResizePointSearchResult.searchDone) {
1063 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001064 }
1065 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001066 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001067 }
1068
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001069 void setTouchExcludeRegion(Task focusedTask) {
Craig Mautner6601b7b2013-04-29 10:29:11 -07001070 mTouchExcludeRegion.set(mBaseDisplayRect);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001071 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001072 mTmpRect2.setEmpty();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001073 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1074 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001075 stack.setTouchExcludeRegion(
1076 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
Craig Mautner6601b7b2013-04-29 10:29:11 -07001077 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001078 // If we removed the focused task above, add it back and only leave its
1079 // outside touch area in the exclusion. TapDectector is not interested in
1080 // any touch inside the focused task itself.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001081 if (!mTmpRect2.isEmpty()) {
Chong Zhangd8ceb852015-11-11 14:53:41 -08001082 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1083 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001084 final WindowState inputMethod = mService.mInputMethodWindow;
1085 if (inputMethod != null && inputMethod.isVisibleLw()) {
1086 // If the input method is visible and the user is typing, we don't want these touch
1087 // events to be intercepted and used to change focus. This would likely cause a
1088 // disappearance of the input method.
1089 inputMethod.getTouchableRegion(mTmpRegion);
1090 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1091 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001092 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
1093 WindowState win = mTapExcludedWindows.get(i);
1094 win.getTouchableRegion(mTmpRegion);
1095 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1096 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001097 // TODO(multi-display): Support docked stacks on secondary displays.
1098 if (mDisplayId == DEFAULT_DISPLAY && getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001099 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001100 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001101 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1102 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001103 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001104 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001105 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001106 }
1107
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001108 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001109 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001110 super.switchUser();
1111 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001112 }
1113
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001114 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001115 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1116 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001117 }
1118 }
1119
1120 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001121 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -07001122 }
1123
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001124 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001125 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001126 }
1127
1128 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001129 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -07001130 }
1131
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001132 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -08001133 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -07001134 }
1135
Wale Ogunwale10124582016-09-15 20:25:50 -07001136 @Override
1137 void removeIfPossible() {
1138 if (isAnimating()) {
1139 mDeferredRemoval = true;
1140 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001141 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001142 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001143 }
1144
Wale Ogunwale10124582016-09-15 20:25:50 -07001145 @Override
1146 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001147 mRemovingDisplay = true;
1148 try {
1149 super.removeImmediately();
1150 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
1151 mDimLayerController.close();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001152 if (mDisplayId == DEFAULT_DISPLAY && mService.canDispatchPointerEvents()) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001153 mService.unregisterPointerEventListener(mTapDetector);
1154 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
1155 }
1156 } finally {
1157 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08001158 }
Craig Mautner95da1082014-02-24 17:54:35 -08001159 }
1160
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001161 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07001162 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001163 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001164 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
1165
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001166 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001167 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -08001168 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001169 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08001170 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001171 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08001172 }
1173
Wale Ogunwale10124582016-09-15 20:25:50 -07001174 boolean animateForIme(float interpolatedValue, float animationTarget,
1175 float dividerAnimationTarget) {
1176 boolean updated = false;
1177
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001178 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1179 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001180 if (stack == null || !stack.isAdjustedForIme()) {
1181 continue;
1182 }
1183
1184 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
1185 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1186 updated = true;
1187 } else {
1188 mDividerControllerLocked.mLastAnimationProgress =
1189 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
1190 mDividerControllerLocked.mLastDividerProgress =
1191 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
1192 updated |= stack.updateAdjustForIme(
1193 mDividerControllerLocked.mLastAnimationProgress,
1194 mDividerControllerLocked.mLastDividerProgress,
1195 false /* force */);
1196 }
1197 if (interpolatedValue >= 1f) {
1198 stack.endImeAdjustAnimation();
1199 }
1200 }
1201
1202 return updated;
1203 }
1204
1205 boolean clearImeAdjustAnimation() {
1206 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001207 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1208 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001209 if (stack != null && stack.isAdjustedForIme()) {
1210 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
1211 changed = true;
1212 }
1213 }
1214 return changed;
1215 }
1216
1217 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001218 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1219 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001220 if (stack.isVisible() && stack.isAdjustedForIme()) {
1221 stack.beginImeAdjustAnimation();
1222 }
1223 }
1224 }
1225
1226 void adjustForImeIfNeeded() {
1227 final WindowState imeWin = mService.mInputMethodWindow;
1228 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
1229 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale1666e312016-12-16 11:27:18 -08001230 final boolean dockVisible = isStackVisible(DOCKED_STACK_ID);
Wale Ogunwale10124582016-09-15 20:25:50 -07001231 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
1232 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
1233 imeTargetStack.getDockSide() : DOCKED_INVALID;
1234 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
1235 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
1236 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
1237 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
1238 final boolean imeHeightChanged = imeVisible &&
1239 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
1240
1241 // The divider could be adjusted for IME position, or be thinner than usual,
1242 // or both. There are three possible cases:
1243 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
1244 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
1245 // - If IME is not visible, divider is not moved and is normal width.
1246
1247 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001248 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1249 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001250 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
1251 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)) {
1252 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
1253 } else {
1254 stack.resetAdjustedForIme(false);
1255 }
1256 }
1257 mDividerControllerLocked.setAdjustedForIme(
1258 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
1259 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001260 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1261 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07001262 stack.resetAdjustedForIme(!dockVisible);
1263 }
1264 mDividerControllerLocked.setAdjustedForIme(
1265 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
1266 }
Winson Chung655332c2016-10-31 13:14:28 -07001267 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07001268 }
1269
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001270 void setInputMethodAnimLayerAdjustment(int adj) {
1271 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
1272 mInputMethodAnimLayerAdjustment = adj;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08001273 mImeWindowsContainers.forAllWindows(mSetInputMethodAnimLayerAdjustment,
1274 true /* traverseTopToBottom */);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001275 }
1276
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001277 /**
1278 * If a window that has an animation specifying a colored background and the current wallpaper
1279 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
1280 * suddenly disappear.
1281 */
1282 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001283 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
1284 w -> w.mIsWallpaper && w.isVisibleNow());
1285
1286 if (visibleWallpaper != null) {
1287 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001288 }
1289 return winAnimator.mAnimLayer;
1290 }
1291
Wale Ogunwale10124582016-09-15 20:25:50 -07001292 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001293 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1294 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07001295 stack.prepareFreezingTaskBounds();
1296 }
1297 }
1298
Wale Ogunwale94744212015-09-21 19:01:47 -07001299 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001300 getLogicalDisplayRect(mTmpRect, newRotation);
1301
1302 // Compute a transform matrix to undo the coordinate space transformation,
1303 // and present the window at the same physical position it previously occupied.
1304 final int deltaRotation = deltaRotation(newRotation, oldRotation);
1305 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
1306
1307 mTmpRectF.set(bounds);
1308 mTmpMatrix.mapRect(mTmpRectF);
1309 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07001310 }
1311
Wale Ogunwale4a02d812015-02-12 23:01:38 -08001312 static int deltaRotation(int oldRotation, int newRotation) {
1313 int delta = newRotation - oldRotation;
1314 if (delta < 0) delta += 4;
1315 return delta;
1316 }
1317
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001318 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07001319 Matrix outMatrix) {
1320 // For rotations without Z-ordering we don't need the target rectangle's position.
1321 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
1322 displayHeight, outMatrix);
1323 }
1324
1325 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
1326 float displayWidth, float displayHeight, Matrix outMatrix) {
1327 switch (rotation) {
1328 case ROTATION_0:
1329 outMatrix.reset();
1330 break;
1331 case ROTATION_270:
1332 outMatrix.setRotate(270, 0, 0);
1333 outMatrix.postTranslate(0, displayHeight);
1334 outMatrix.postTranslate(rectTop, 0);
1335 break;
1336 case ROTATION_180:
1337 outMatrix.reset();
1338 break;
1339 case ROTATION_90:
1340 outMatrix.setRotate(90, 0, 0);
1341 outMatrix.postTranslate(displayWidth, 0);
1342 outMatrix.postTranslate(-rectTop, rectLeft);
1343 break;
1344 }
1345 }
1346
Craig Mautnera91f9e22012-09-14 16:22:08 -07001347 public void dump(String prefix, PrintWriter pw) {
1348 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
1349 final String subPrefix = " " + prefix;
1350 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
1351 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
1352 pw.print("dpi");
1353 if (mInitialDisplayWidth != mBaseDisplayWidth
1354 || mInitialDisplayHeight != mBaseDisplayHeight
1355 || mInitialDisplayDensity != mBaseDisplayDensity) {
1356 pw.print(" base=");
1357 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
1358 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
1359 }
Jeff Brownd46747a2015-04-15 19:02:36 -07001360 if (mDisplayScalingDisabled) {
1361 pw.println(" noscale");
1362 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07001363 pw.print(" cur=");
1364 pw.print(mDisplayInfo.logicalWidth);
1365 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
1366 pw.print(" app=");
1367 pw.print(mDisplayInfo.appWidth);
1368 pw.print("x"); pw.print(mDisplayInfo.appHeight);
1369 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
1370 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
1371 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
1372 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001373 pw.println(subPrefix + "deferred=" + mDeferredRemoval
1374 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001375
Craig Mautnerdc548482014-02-05 13:35:24 -08001376 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001377 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001378 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1379 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001380 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001381 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001382
Craig Mautnerdc548482014-02-05 13:35:24 -08001383 pw.println();
1384 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001385 pw.println();
1386 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001387 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001388 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001389 pw.print(" Exiting #"); pw.print(i);
1390 pw.print(' '); pw.print(token);
1391 pw.println(':');
1392 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001393 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001394 }
Craig Mautner59c00972012-07-30 12:10:24 -07001395 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001396 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07001397 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001398 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07001399 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001400 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001401
1402 if (mInputMethodAnimLayerAdjustment != 0) {
1403 pw.println(subPrefix
1404 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
1405 }
Craig Mautner59c00972012-07-30 12:10:24 -07001406 }
Craig Mautnere0a38842013-12-16 16:14:02 -08001407
1408 @Override
1409 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001410 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001411 }
1412
1413 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001414 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08001415 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07001416
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08001417 /** Checks if stack with provided id is visible on this display. */
1418 boolean isStackVisible(int stackId) {
1419 final TaskStack stack = getStackById(stackId);
1420 return (stack != null && stack.isVisible());
1421 }
1422
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001423 /**
1424 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
1425 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07001426 TaskStack getDockedStackLocked() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001427 final TaskStack stack = getStackById(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001428 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07001429 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001430
1431 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001432 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02001433 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001434 */
Jorim Jaggife762342016-10-13 14:33:27 +02001435 TaskStack getDockedStackIgnoringVisibility() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001436 return getStackById(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001437 }
1438
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001439 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001440 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001441 final int x = (int) xf;
1442 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001443 final WindowState touchedWin = getWindow(w -> {
1444 final int flags = w.mAttrs.flags;
1445 if (!w.isVisibleLw()) {
1446 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001447 }
1448 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001449 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001450 }
1451
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001452 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001453 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001454 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001455 }
1456
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001457 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001458
1459 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001460 return mTmpRegion.contains(x, y) || touchFlags == 0;
1461 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001462
1463 return touchedWin;
1464 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07001465
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001466 boolean canAddToastWindowForUid(int uid) {
1467 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08001468 // Also if the app is focused adding more than one toast at
1469 // a time for better backwards compatibility.
1470 final WindowState focusedWindowForUid = getWindow(w ->
1471 w.mOwnerUid == uid && w.isFocused());
1472 if (focusedWindowForUid != null) {
1473 return true;
1474 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001475 final WindowState win = getWindow(w ->
1476 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
1477 && !w.mWindowRemovalAllowed);
1478 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001479 }
1480
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001481 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001482 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
1483 return;
1484 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001485
Wale Ogunwale1e129a42016-11-21 13:03:47 -08001486 // Used to communicate the old focus to the callback method.
1487 mTmpWindow = oldFocus;
1488
1489 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001490 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001491
1492 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001493 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001494
Wale Ogunwale1e129a42016-11-21 13:03:47 -08001495 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001496
1497 if (mTmpWindow == null) {
1498 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
1499 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001500 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001501 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001502 }
1503
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001504 /** Updates the layer assignment of windows on this display. */
1505 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08001506 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001507 if (setLayoutNeeded) {
1508 setLayoutNeeded();
1509 }
1510 }
1511
Wale Ogunwale1666e312016-12-16 11:27:18 -08001512 // TODO: This should probably be called any time a visual change is made to the hierarchy like
1513 // moving containers or resizing them. Need to investigate the best way to have it automatically
1514 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001515 void layoutAndAssignWindowLayersIfNeeded() {
1516 mService.mWindowsChanged = true;
1517 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001518
1519 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
1520 false /*updateInputWindows*/)) {
1521 assignWindowLayers(false /* setLayoutNeeded */);
1522 }
1523
1524 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
1525 mService.mWindowPlacerLocked.performSurfacePlacement();
1526 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
1527 }
1528
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001529 /** Returns true if a leaked surface was destroyed */
1530 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001531 // Used to indicate that a surface was leaked.
1532 mTmpWindow = null;
1533 forAllWindows(w -> {
1534 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001535 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001536 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001537 }
1538 if (!mService.mSessions.contains(wsa.mSession)) {
1539 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001540 + w + " surface=" + wsa.mSurfaceController
1541 + " token=" + w.mToken
1542 + " pid=" + w.mSession.mPid
1543 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001544 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001545 mService.mForceRemoves.add(w);
1546 mTmpWindow = w;
1547 } else if (w.mAppToken != null && w.mAppToken.clientHidden) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001548 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001549 + w + " surface=" + wsa.mSurfaceController
1550 + " token=" + w.mAppToken
1551 + " saved=" + w.hasSavedSurface());
1552 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001553 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001554 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001555 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001556 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001557
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001558 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001559 }
1560
1561 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001562 * Determine and return the window that should be the IME target.
1563 * @param updateImeTarget If true the system IME target will be updated to match what we found.
1564 * @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 +00001565 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001566 WindowState computeImeTarget(boolean updateImeTarget) {
1567 if (mService.mInputMethodWindow == null) {
1568 // There isn't an IME so there shouldn't be a target...That was easy!
1569 if (updateImeTarget) {
1570 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
1571 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
1572 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
1573 }
1574 return null;
1575 }
1576
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001577 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
1578 // same display. Or even when the current IME/target are not on the same screen as the next
1579 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08001580 mUpdateImeTarget = updateImeTarget;
1581 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001582
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001583
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001584 // Yet more tricksyness! If this window is a "starting" window, we do actually want
1585 // to be on top of it, but it is not -really- where input will go. So look down below
1586 // for a real window to target...
1587 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
1588 final AppWindowToken token = target.mAppToken;
1589 if (token != null) {
1590 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
1591 if (betterTarget != null) {
1592 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001593 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001594 }
1595 }
1596
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001597 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
1598 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001599
1600 // Now, a special case -- if the last target's window is in the process of exiting, and is
1601 // above the new target, keep on the last target to avoid flicker. Consider for example a
1602 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
1603 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
1604 // scrim.
1605 final WindowState curTarget = mService.mInputMethodTarget;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001606 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
1607 && (target == null
1608 || curTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer)) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001609 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001610 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001611 }
1612
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001613 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
1614 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001615
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001616 if (target == null) {
1617 if (updateImeTarget) {
1618 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
1619 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
1620 + Debug.getCallers(4) : ""));
1621 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim, 0);
1622 }
1623
1624 return null;
1625 }
1626
1627 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001628 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
1629 if (token != null) {
1630
1631 // Now some fun for dealing with window animations that modify the Z order. We need
1632 // to look at all windows below the current target that are in this app, finding the
1633 // highest visible one in layering.
1634 WindowState highestTarget = null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001635 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001636 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001637 }
1638
1639 if (highestTarget != null) {
1640 final AppTransition appTransition = mService.mAppTransition;
1641 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
1642 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
1643 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001644 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001645
1646 if (appTransition.isTransitionSet()) {
1647 // If we are currently setting up for an animation, hold everything until we
1648 // can find out what will happen.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001649 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
1650 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001651 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001652 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001653 // If the window we are currently targeting is involved with an animation,
1654 // and it is on top of the next target we will be over, then hold off on
1655 // moving until that is done.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001656 setInputMethodTarget(highestTarget, true, mInputMethodAnimLayerAdjustment);
1657 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001658 }
1659 }
1660 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001661
1662 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
1663 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
1664 setInputMethodTarget(target, false, target.mAppToken != null
1665 ? target.mAppToken.mAppAnimator.animLayerAdjustment : 0);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001666 }
1667
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001668 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001669 }
1670
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001671 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim, int layerAdj) {
1672 if (target == mService.mInputMethodTarget
1673 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim
1674 && mInputMethodAnimLayerAdjustment == layerAdj) {
1675 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001676 }
1677
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001678 mService.mInputMethodTarget = target;
1679 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
1680 setInputMethodAnimLayerAdjustment(layerAdj);
1681 assignWindowLayers(false /* setLayoutNeeded */);
1682 }
1683
1684 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
1685 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
1686 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
1687 }
1688
1689 // Used to indicate we have reached the first window in the range we are interested in.
1690 mTmpWindow = null;
1691
1692 // TODO: Figure-out a more efficient way to do this.
1693 final WindowState candidate = getWindow(w -> {
1694 if (w == top) {
1695 // Reached the first window in the range we are interested in.
1696 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001697 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001698 if (mTmpWindow == null) {
1699 return false;
1700 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001701
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001702 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
1703 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001704 }
1705 // If we reached the bottom of the range of windows we are considering,
1706 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001707 if (w == bottom) {
1708 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001709 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001710 return false;
1711 });
1712
1713 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001714 }
1715
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001716 void setLayoutNeeded() {
1717 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
1718 mLayoutNeeded = true;
1719 }
1720
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001721 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001722 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
1723 mLayoutNeeded = false;
1724 }
1725
1726 boolean isLayoutNeeded() {
1727 return mLayoutNeeded;
1728 }
1729
Wale Ogunwale02319a62016-09-26 15:21:22 -07001730 void dumpTokens(PrintWriter pw, boolean dumpAll) {
1731 if (mTokenMap.isEmpty()) {
1732 return;
1733 }
1734 pw.println(" Display #" + mDisplayId);
1735 final Iterator<WindowToken> it = mTokenMap.values().iterator();
1736 while (it.hasNext()) {
1737 final WindowToken token = it.next();
1738 pw.print(" ");
1739 pw.print(token);
1740 if (dumpAll) {
1741 pw.println(':');
1742 token.dump(pw, " ");
1743 } else {
1744 pw.println();
1745 }
1746 }
1747 }
1748
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001749 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001750 final int[] index = new int[1];
1751 forAllWindows(w -> {
1752 final WindowStateAnimator wAnim = w.mWinAnimator;
1753 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
1754 index[0] = index[0] + 1;
1755 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001756 }
1757
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001758 void enableSurfaceTrace(FileDescriptor fd) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001759 forAllWindows(w -> {
1760 w.mWinAnimator.enableSurfaceTrace(fd);
1761 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001762 }
1763
1764 void disableSurfaceTrace() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001765 forAllWindows(w -> {
1766 w.mWinAnimator.disableSurfaceTrace();
1767 }, true /* traverseTopToBottom */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001768 }
1769
Jorim Jaggife762342016-10-13 14:33:27 +02001770 /**
1771 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
1772 */
1773 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
1774 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001775 forAllWindows(w -> {
1776 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)) {
1777 w.mWinAnimator.setAnimation(
Jorim Jaggife762342016-10-13 14:33:27 +02001778 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
1779 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001780 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02001781 }
1782
Wale Ogunwale494009b82016-10-21 09:01:38 -07001783 boolean checkWaitingForWindows() {
1784
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001785 mHaveBootMsg = false;
1786 mHaveApp = false;
1787 mHaveWallpaper = false;
1788 mHaveKeyguard = true;
1789
1790 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07001791 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
1792 return true;
1793 }
1794 if (w.isDrawnLw()) {
1795 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001796 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001797 } else if (w.mAttrs.type == TYPE_APPLICATION
1798 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001799 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001800 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001801 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001802 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001803 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07001804 }
1805 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001806 return false;
1807 });
1808
1809 if (visibleWindow != null) {
1810 // We have a visible window.
1811 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001812 }
1813
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001814 // if the wallpaper service is disabled on the device, we're never going to have
1815 // wallpaper, don't bother waiting for it
1816 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
1817 com.android.internal.R.bool.config_enableWallpaperService)
1818 && !mService.mOnlyCore;
1819
Wale Ogunwale494009b82016-10-21 09:01:38 -07001820 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
1821 "******** booted=" + mService.mSystemBooted
1822 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001823 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
1824 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
1825 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07001826
1827 // If we are turning on the screen to show the boot message, don't do it until the boot
1828 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001829 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07001830 return true;
1831 }
1832
1833 // If we are turning on the screen after the boot is completed normally, don't do so until
1834 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001835 if (mService.mSystemBooted
1836 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07001837 return true;
1838 }
1839
1840 return false;
1841 }
1842
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001843 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08001844 mTmpWindowAnimator = animator;
1845 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001846 }
1847
1848 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07001849 resetAnimationBackgroundAnimator();
1850
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001851 // Used to indicate a detached wallpaper.
1852 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08001853 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001854
Wale Ogunwale1e129a42016-11-21 13:03:47 -08001855 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001856
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001857 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001858 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001859 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
1860 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001861 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
1862 }
1863 }
1864
1865 void prepareWindowSurfaces() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08001866 forAllWindows(mPrepareWindowSurfaces, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07001867 }
1868
Wale Ogunwale494009b82016-10-21 09:01:38 -07001869 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001870 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001871 if (imFocus == null) {
1872 return false;
1873 }
1874
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001875 if (DEBUG_INPUT_METHOD) {
1876 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
1877 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
1878 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00001879 }
1880
Wale Ogunwale494009b82016-10-21 09:01:38 -07001881 final IInputMethodClient imeClient = imFocus.mSession.mClient;
1882
1883 if (DEBUG_INPUT_METHOD) {
1884 Slog.i(TAG_WM, "IM target client: " + imeClient);
1885 if (imeClient != null) {
1886 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
1887 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
1888 }
1889 }
1890
1891 return imeClient != null && imeClient.asBinder() == client.asBinder();
1892 }
1893
1894 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001895 final WindowState win = getWindow(
1896 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
1897 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001898 }
1899
1900 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001901 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07001902 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001903 final int curValue = w.mSystemUiVisibility;
1904 final int diff = (curValue ^ visibility) & globalDiff;
1905 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07001906 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001907 w.mSeq++;
1908 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001909 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001910 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
1911 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07001912 visibility, newValue, diff);
1913 }
1914 } catch (RemoteException e) {
1915 // so sorry
1916 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001917 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07001918 }
1919
1920 void onWindowFreezeTimeout() {
1921 Slog.w(TAG_WM, "Window freeze timeout expired.");
1922 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001923
1924 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07001925 if (!w.mOrientationChanging) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001926 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001927 }
1928 w.mOrientationChanging = false;
1929 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
1930 - mService.mDisplayFreezeTime);
1931 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001932 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07001933 mService.mWindowPlacerLocked.performSurfacePlacement();
1934 }
1935
1936 void waitForAllWindowsDrawn() {
1937 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001938 forAllWindows(w -> {
1939 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
1940 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
1941 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07001942 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001943 w.mLastContentInsets.set(-1, -1, -1, -1);
1944 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07001945 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001946 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07001947 }
1948
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001949 // TODO: Super crazy long method that should be broken down...
1950 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
1951
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001952 final int dw = mDisplayInfo.logicalWidth;
1953 final int dh = mDisplayInfo.logicalHeight;
1954 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
1955
1956 mTmpUpdateAllDrawn.clear();
1957
1958 int repeats = 0;
1959 do {
1960 repeats++;
1961 if (repeats > 6) {
1962 Slog.w(TAG, "Animation repeat aborted after too many iterations");
1963 clearLayoutNeeded();
1964 break;
1965 }
1966
1967 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
1968 pendingLayoutChanges);
1969
Andrii Kulian839def92016-11-02 10:58:58 -07001970 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
1971 // the wallpaper window jumping across displays.
1972 // Remove check for default display when there will be support for multiple wallpaper
1973 // targets (on different displays).
1974 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001975 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001976 }
1977
1978 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
1979 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
1980 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
1981 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07001982 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001983 }
1984 }
1985
1986 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
1987 setLayoutNeeded();
1988 }
1989
1990 // FIRST LOOP: Perform a layout, if needed.
1991 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
1992 performLayout(repeats == 1, false /* updateInputWindows */);
1993 } else {
1994 Slog.w(TAG, "Layout repeat skipped after too many iterations");
1995 }
1996
1997 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
1998 pendingLayoutChanges = 0;
1999
2000 if (isDefaultDisplay) {
2001 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002002 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002003 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2004 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2005 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2006 }
2007 } while (pendingLayoutChanges != 0);
2008
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002009 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002010 resetDimming();
2011
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002012 mTmpRecoveringMemory = recoveringMemory;
2013 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002014
2015 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002016 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2017 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2018 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002019 true /* inTraversal, must call performTraversalInTrans... below */);
2020
2021 stopDimmingIfNeeded();
2022
2023 while (!mTmpUpdateAllDrawn.isEmpty()) {
2024 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2025 // See if any windows have been drawn, so they (and others associated with them)
2026 // can now be shown.
2027 atoken.updateAllDrawn(this);
2028 }
2029
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002030 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002031 }
2032
2033 void performLayout(boolean initial, boolean updateInputWindows) {
2034 if (!isLayoutNeeded()) {
2035 return;
2036 }
2037 clearLayoutNeeded();
2038
2039 final int dw = mDisplayInfo.logicalWidth;
2040 final int dh = mDisplayInfo.logicalHeight;
2041
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002042 if (DEBUG_LAYOUT) {
2043 Slog.v(TAG, "-------------------------------------");
2044 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2045 }
2046
2047 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mService.mRotation,
2048 getConfiguration().uiMode);
2049 if (isDefaultDisplay) {
2050 // Not needed on non-default displays.
2051 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2052 mService.mScreenRect.set(0, 0, dw, dh);
2053 }
2054
2055 mService.mPolicy.getContentRectLw(mContentRect);
2056
2057 int seq = mService.mLayoutSeq + 1;
2058 if (seq < 0) seq = 0;
2059 mService.mLayoutSeq = seq;
2060
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002061 // Used to indicate that we have processed the dream window and all additional windows are
2062 // behind it.
2063 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002064 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002065
2066 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002067 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002068
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002069 // Used to indicate that we have processed the dream window and all additional attached
2070 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002071 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002072 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002073
2074 // Now perform layout of attached windows, which usually depend on the position of the
2075 // window they are attached to. XXX does not deal with windows that are attached to windows
2076 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002077 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002078
2079 // Window frames may have changed. Tell the input dispatcher about it.
2080 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2081 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2082 if (updateInputWindows) {
2083 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2084 }
2085
2086 mService.mPolicy.finishLayoutLw();
2087 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2088 }
2089
2090 /**
2091 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2092 * In portrait mode, it grabs the full screenshot.
2093 *
2094 * @param width the width of the target bitmap
2095 * @param height the height of the target bitmap
2096 * @param includeFullDisplay true if the screen should not be cropped before capture
2097 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2098 * @param config of the output bitmap
2099 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
Jorim Jaggi02886a82016-12-06 09:10:06 -08002100 * @param includeDecor whether to include window decors, like the status or navigation bar
2101 * background of the window
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002102 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002103 Bitmap screenshotApplications(IBinder appToken, int width, int height,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002104 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002105 boolean wallpaperOnly, boolean includeDecor) {
2106 Bitmap bitmap = screenshotApplications(appToken, width, height, includeFullDisplay,
2107 frameScale, wallpaperOnly, includeDecor, SurfaceControl::screenshot);
Jorim Jaggi9f9d61f2017-01-17 11:15:37 +01002108 if (bitmap == null) {
2109 return null;
2110 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002111
2112 if (DEBUG_SCREENSHOT) {
2113 // TEST IF IT's ALL BLACK
2114 int[] buffer = new int[bitmap.getWidth() * bitmap.getHeight()];
2115 bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),
2116 bitmap.getHeight());
2117 boolean allBlack = true;
2118 final int firstColor = buffer[0];
2119 for (int i = 0; i < buffer.length; i++) {
2120 if (buffer[i] != firstColor) {
2121 allBlack = false;
2122 break;
2123 }
2124 }
2125 if (allBlack) {
2126 final WindowState appWin = mScreenshotApplicationState.appWin;
2127 final int maxLayer = mScreenshotApplicationState.maxLayer;
2128 final int minLayer = mScreenshotApplicationState.minLayer;
2129 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
2130 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
2131 (appWin != null ?
2132 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
2133 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
2134 }
2135 }
2136
2137 // Create a copy of the screenshot that is immutable and backed in ashmem.
2138 // This greatly reduces the overhead of passing the bitmap between processes.
2139 Bitmap ret = bitmap.createAshmemBitmap(config);
2140 bitmap.recycle();
2141 return ret;
2142 }
2143
2144 GraphicBuffer screenshotApplicationsToBuffer(IBinder appToken, int width, int height,
2145 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2146 boolean includeDecor) {
2147 return screenshotApplications(appToken, width, height, includeFullDisplay, frameScale,
2148 wallpaperOnly, includeDecor, SurfaceControl::screenshotToBuffer);
2149 }
2150
2151 private <E> E screenshotApplications(IBinder appToken, int width, int height,
2152 boolean includeFullDisplay, float frameScale, boolean wallpaperOnly,
2153 boolean includeDecor, Screenshoter<E> screenshoter) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002154 int dw = mDisplayInfo.logicalWidth;
2155 int dh = mDisplayInfo.logicalHeight;
2156 if (dw == 0 || dh == 0) {
2157 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2158 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2159 return null;
2160 }
2161
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002162 E bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002163
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002164 mScreenshotApplicationState.reset(appToken == null && !wallpaperOnly);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002165 final Rect frame = new Rect();
2166 final Rect stackBounds = new Rect();
2167
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002168 boolean includeImeInScreenshot;
2169 synchronized(mService.mWindowMap) {
2170 final AppWindowToken imeTargetAppToken = mService.mInputMethodTarget != null
2171 ? mService.mInputMethodTarget.mAppToken : null;
2172 // We only include the Ime in the screenshot if the app we are screenshoting is the IME
2173 // target and isn't in multi-window mode. We don't screenshot the IME in multi-window
2174 // mode because the frame of the IME might not overlap with that of the app.
2175 // E.g. IME target app at the top in split-screen mode and the IME at the bottom
2176 // overlapping with the bottom app.
2177 includeImeInScreenshot = imeTargetAppToken != null
2178 && imeTargetAppToken.appToken != null
2179 && imeTargetAppToken.appToken.asBinder() == appToken
2180 && !mService.mInputMethodTarget.isInMultiWindowMode();
2181 }
2182
2183 final int aboveAppLayer = (mService.mPolicy.windowTypeToLayerLw(TYPE_APPLICATION) + 1)
2184 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
Jorim Jaggi02886a82016-12-06 09:10:06 -08002185 final MutableBoolean mutableIncludeFullDisplay = new MutableBoolean(includeFullDisplay);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002186 synchronized(mService.mWindowMap) {
2187 // Figure out the part of the screen that is actually the app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002188 mScreenshotApplicationState.appWin = null;
2189 forAllWindows(w -> {
2190 if (!w.mHasSurface) {
2191 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002192 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002193 if (w.mLayer >= aboveAppLayer) {
2194 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002195 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002196 if (wallpaperOnly && !w.mIsWallpaper) {
2197 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002198 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002199 if (w.mIsImWindow) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002200 if (!includeImeInScreenshot) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002201 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002202 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002203 } else if (w.mIsWallpaper) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002204 // If this is the wallpaper layer and we're only looking for the wallpaper layer
2205 // then the target window state is this one.
2206 if (wallpaperOnly) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002207 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002208 }
2209
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002210 if (mScreenshotApplicationState.appWin == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002211 // We have not ran across the target window yet, so it is probably behind
2212 // the wallpaper. This can happen when the keyguard is up and all windows
2213 // are moved behind the wallpaper. We don't want to include the wallpaper
2214 // layer in the screenshot as it will cover-up the layer of the target
2215 // window.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002216 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002217 }
2218 // Fall through. The target window is in front of the wallpaper. For this
2219 // case we want to include the wallpaper layer in the screenshot because
2220 // the target window might have some transparent areas.
2221 } else if (appToken != null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002222 if (w.mAppToken == null || w.mAppToken.token != appToken) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002223 // This app window is of no interest if it is not associated with the
2224 // screenshot app.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002225 return false;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002226 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002227 mScreenshotApplicationState.appWin = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002228 }
2229
2230 // Include this window.
2231
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002232 final WindowStateAnimator winAnim = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002233 int layer = winAnim.mSurfaceController.getLayer();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002234 if (mScreenshotApplicationState.maxLayer < layer) {
2235 mScreenshotApplicationState.maxLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002236 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002237 if (mScreenshotApplicationState.minLayer > layer) {
2238 mScreenshotApplicationState.minLayer = layer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002239 }
2240
2241 // Don't include wallpaper in bounds calculation
Jorim Jaggibfbd9dc2017-01-20 18:13:01 +01002242 if (!mutableIncludeFullDisplay.value && includeDecor) {
2243 final TaskStack stack = w.getStack();
2244 if (stack != null) {
2245 stack.getBounds(frame);
2246 }
Jorim Jaggi02886a82016-12-06 09:10:06 -08002247 } else if (!mutableIncludeFullDisplay.value && !w.mIsWallpaper) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002248 final Rect wf = w.mFrame;
2249 final Rect cr = w.mContentInsets;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002250 int left = wf.left + cr.left;
2251 int top = wf.top + cr.top;
2252 int right = wf.right - cr.right;
2253 int bottom = wf.bottom - cr.bottom;
2254 frame.union(left, top, right, bottom);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002255 w.getVisibleBounds(stackBounds);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002256 if (!Rect.intersects(frame, stackBounds)) {
2257 // Set frame empty if there's no intersection.
2258 frame.setEmpty();
2259 }
2260 }
2261
2262 final boolean foundTargetWs =
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002263 (w.mAppToken != null && w.mAppToken.token == appToken)
2264 || (mScreenshotApplicationState.appWin != null && wallpaperOnly);
Jorim Jaggi52e85da2017-01-24 16:21:39 +01002265 if (foundTargetWs && winAnim.getShown()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002266 mScreenshotApplicationState.screenshotReady = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002267 }
2268
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002269 if (w.isObscuringDisplay()){
2270 return true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002271 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002272 return false;
2273 }, true /* traverseTopToBottom */);
2274
2275 final WindowState appWin = mScreenshotApplicationState.appWin;
2276 final boolean screenshotReady = mScreenshotApplicationState.screenshotReady;
2277 final int maxLayer = mScreenshotApplicationState.maxLayer;
2278 final int minLayer = mScreenshotApplicationState.minLayer;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002279
2280 if (appToken != null && appWin == null) {
2281 // Can't find a window to snapshot.
2282 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
2283 "Screenshot: Couldn't find a surface matching " + appToken);
2284 return null;
2285 }
2286
2287 if (!screenshotReady) {
2288 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
2289 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
2290 appWin.mWinAnimator.mDrawState)));
2291 return null;
2292 }
2293
2294 // Screenshot is ready to be taken. Everything from here below will continue
2295 // through the bottom of the loop and return a value. We only stay in the loop
2296 // because we don't want to release the mWindowMap lock until the screenshot is
2297 // taken.
2298
2299 if (maxLayer == 0) {
2300 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2301 + ": returning null maxLayer=" + maxLayer);
2302 return null;
2303 }
2304
Jorim Jaggi02886a82016-12-06 09:10:06 -08002305 if (!mutableIncludeFullDisplay.value) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002306 // Constrain frame to the screen size.
2307 if (!frame.intersect(0, 0, dw, dh)) {
2308 frame.setEmpty();
2309 }
2310 } else {
2311 // Caller just wants entire display.
2312 frame.set(0, 0, dw, dh);
2313 }
2314 if (frame.isEmpty()) {
2315 return null;
2316 }
2317
2318 if (width < 0) {
2319 width = (int) (frame.width() * frameScale);
2320 }
2321 if (height < 0) {
2322 height = (int) (frame.height() * frameScale);
2323 }
2324
2325 // Tell surface flinger what part of the image to crop. Take the top
2326 // right part of the application, and crop the larger dimension to fit.
2327 Rect crop = new Rect(frame);
2328 if (width / (float) frame.width() < height / (float) frame.height()) {
2329 int cropWidth = (int)((float)width / (float)height * frame.height());
2330 crop.right = crop.left + cropWidth;
2331 } else {
2332 int cropHeight = (int)((float)height / (float)width * frame.width());
2333 crop.bottom = crop.top + cropHeight;
2334 }
2335
2336 // The screenshot API does not apply the current screen rotation.
2337 int rot = mDisplay.getRotation();
2338
2339 if (rot == ROTATION_90 || rot == ROTATION_270) {
2340 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
2341 }
2342
2343 // Surfaceflinger is not aware of orientation, so convert our logical
2344 // crop to surfaceflinger's portrait orientation.
2345 convertCropForSurfaceFlinger(crop, rot, dw, dh);
2346
2347 if (DEBUG_SCREENSHOT) {
2348 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
2349 + maxLayer + " appToken=" + appToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002350 forAllWindows(w -> {
2351 final WindowSurfaceController controller = w.mWinAnimator.mSurfaceController;
2352 Slog.i(TAG_WM, w + ": " + w.mLayer
2353 + " animLayer=" + w.mWinAnimator.mAnimLayer
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002354 + " surfaceLayer=" + ((controller == null)
2355 ? "null" : controller.getLayer()));
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002356 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002357 }
2358
2359 final ScreenRotationAnimation screenRotationAnimation =
2360 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
2361 final boolean inRotation = screenRotationAnimation != null &&
2362 screenRotationAnimation.isAnimating();
2363 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
2364 "Taking screenshot while rotating");
2365
2366 // We force pending transactions to flush before taking
2367 // the screenshot by pushing an empty synchronous transaction.
2368 SurfaceControl.openTransaction();
2369 SurfaceControl.closeTransactionSync();
2370
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002371 bitmap = screenshoter.screenshot(crop, width, height, minLayer, maxLayer,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002372 inRotation, rot);
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002373 if (bitmap == null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002374 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
2375 + ") to layer " + maxLayer);
2376 return null;
2377 }
2378 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002379 return bitmap;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002380 }
2381
2382 // TODO: Can this use createRotationMatrix()?
2383 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
2384 if (rot == Surface.ROTATION_90) {
2385 final int tmp = crop.top;
2386 crop.top = dw - crop.right;
2387 crop.right = crop.bottom;
2388 crop.bottom = dw - crop.left;
2389 crop.left = tmp;
2390 } else if (rot == Surface.ROTATION_180) {
2391 int tmp = crop.top;
2392 crop.top = dh - crop.bottom;
2393 crop.bottom = dh - tmp;
2394 tmp = crop.right;
2395 crop.right = dw - crop.left;
2396 crop.left = dw - tmp;
2397 } else if (rot == Surface.ROTATION_270) {
2398 final int tmp = crop.top;
2399 crop.top = crop.left;
2400 crop.left = dh - crop.bottom;
2401 crop.bottom = crop.right;
2402 crop.right = dh - tmp;
2403 }
2404 }
2405
2406 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002407 // Used to indicate the layout is needed.
2408 mTmpWindow = null;
2409
2410 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002411 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002412 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002413 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002414 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002415 w.setDisplayLayoutNeeded();
2416 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002417 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002418
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002419 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002420 mService.mWindowPlacerLocked.performSurfacePlacement();
2421 }
2422 }
2423
Wale Ogunwale1666e312016-12-16 11:27:18 -08002424 void setExitingTokensHasVisible(boolean hasVisible) {
2425 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
2426 mExitingTokens.get(i).hasVisible = hasVisible;
2427 }
2428
2429 // Initialize state of exiting applications.
2430 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
2431 }
2432
2433 void removeExistingTokensIfPossible() {
2434 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
2435 final WindowToken token = mExitingTokens.get(i);
2436 if (!token.hasVisible) {
2437 mExitingTokens.remove(i);
2438 }
2439 }
2440
2441 // Time to remove any exiting applications?
2442 mTaskStackContainers.removeExistingAppTokensIfPossible();
2443 }
2444
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002445 static final class TaskForResizePointSearchResult {
2446 boolean searchDone;
2447 Task taskForResize;
2448
2449 void reset() {
2450 searchDone = false;
2451 taskForResize = null;
2452 }
2453 }
Robert Carr3b716242016-08-16 16:02:21 -07002454
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002455 private static final class ApplySurfaceChangesTransactionState {
2456 boolean displayHasContent;
2457 boolean obscured;
2458 boolean syswin;
2459 boolean focusDisplayed;
2460 float preferredRefreshRate;
2461 int preferredModeId;
2462
2463 void reset() {
2464 displayHasContent = false;
2465 obscured = false;
2466 syswin = false;
2467 focusDisplayed = false;
2468 preferredRefreshRate = 0;
2469 preferredModeId = 0;
2470 }
2471 }
2472
2473 private static final class ScreenshotApplicationState {
2474 WindowState appWin;
2475 int maxLayer;
2476 int minLayer;
2477 boolean screenshotReady;
2478
2479 void reset(boolean screenshotReady) {
2480 appWin = null;
2481 maxLayer = 0;
2482 minLayer = 0;
2483 this.screenshotReady = screenshotReady;
2484 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
2485 }
2486 }
2487
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002488 /**
2489 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
2490 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
2491 * homogeneous children type which is currently required by sub-classes of
2492 * {@link WindowContainer} class.
2493 */
2494 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
2495
2496 int size() {
2497 return mChildren.size();
2498 }
2499
2500 E get(int index) {
2501 return mChildren.get(index);
2502 }
2503
2504 @Override
2505 boolean fillsParent() {
2506 return true;
2507 }
2508
2509 @Override
2510 boolean isVisible() {
2511 return true;
Robert Carr3b716242016-08-16 16:02:21 -07002512 }
2513 }
2514
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002515 /**
2516 * Window container class that contains all containers on this display relating to Apps.
2517 * I.e Activities.
2518 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07002519 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002520
Andrii Kulian839def92016-11-02 10:58:58 -07002521 /**
2522 * Adds the stack to this container.
2523 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
2524 */
2525 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002526 if (stack.mStackId == HOME_STACK_ID) {
2527 if (mHomeStack != null) {
2528 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
2529 }
2530 mHomeStack = stack;
2531 }
2532 addChild(stack, onTop);
2533 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07002534 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002535
Andrii Kulian839def92016-11-02 10:58:58 -07002536 /** Removes the stack from its container and prepare for changing the parent. */
2537 void removeStackFromDisplay(TaskStack stack) {
2538 removeChild(stack);
2539 stack.onRemovedFromDisplay();
Andrii Kulian839def92016-11-02 10:58:58 -07002540 }
2541
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002542 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08002543 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
2544 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002545 addChild(stack, addIndex);
2546 setLayoutNeeded();
2547 }
2548
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002549 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08002550 void positionChildAt(int position, TaskStack child, boolean includingParents) {
2551 if (StackId.isAlwaysOnTop(child.mStackId) && position != POSITION_TOP) {
2552 // This stack is always-on-top, override the default behavior.
2553 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
2554
2555 // Moving to its current position, as we must call super but we don't want to
2556 // perform any meaningful action.
2557 final int currentPosition = mChildren.indexOf(child);
2558 super.positionChildAt(currentPosition, child, false /* includingParents */);
2559 return;
2560 }
2561
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08002562 final int targetPosition = findPositionForStack(position, child, false /* adding */);
2563 super.positionChildAt(targetPosition, child, includingParents);
2564
2565 setLayoutNeeded();
2566 }
2567
2568 /**
2569 * When stack is added or repositioned, find a proper position for it.
2570 * This will make sure that pinned stack always stays on top.
2571 * @param requestedPosition Position requested by caller.
2572 * @param stack Stack to be added or positioned.
2573 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
2574 * @return The proper position for the stack.
2575 */
2576 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
2577 final int topChildPosition = mChildren.size() - 1;
2578 boolean toTop = requestedPosition == POSITION_TOP;
2579 toTop |= adding ? requestedPosition >= topChildPosition + 1
2580 : requestedPosition >= topChildPosition;
2581 int targetPosition = requestedPosition;
2582
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002583 if (toTop && isStackVisible(PINNED_STACK_ID) && stack.mStackId != PINNED_STACK_ID) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08002584 // The pinned stack is always the top most stack (always-on-top) when it is visible.
2585 TaskStack topStack = mChildren.get(topChildPosition);
2586 if (topStack.mStackId != PINNED_STACK_ID) {
2587 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
2588 }
2589
2590 // So, stack is moved just below the pinned stack.
2591 // When we're adding a new stack the target is the current pinned stack position.
2592 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002593 // stack, because WindowContainer#positionAt() first removes element and then adds
2594 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08002595 targetPosition = adding ? topChildPosition : topChildPosition - 1;
2596 }
2597
2598 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08002599 }
2600
2601 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002602 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
2603 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002604 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002605 if (super.forAllWindows(callback, traverseTopToBottom)) {
2606 return true;
2607 }
2608 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
2609 return true;
2610 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002611 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002612 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
2613 return true;
2614 }
2615 if (super.forAllWindows(callback, traverseTopToBottom)) {
2616 return true;
2617 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002618 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002619 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002620 }
2621
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002622 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002623 boolean traverseTopToBottom) {
2624 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
2625 // app tokens.
2626 // TODO: Investigate if we need to continue to do this or if we can just process them
2627 // in-order.
2628 if (traverseTopToBottom) {
2629 for (int i = mChildren.size() - 1; i >= 0; --i) {
2630 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
2631 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002632 if (appTokens.get(j).forAllWindowsUnchecked(callback,
2633 traverseTopToBottom)) {
2634 return true;
2635 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002636 }
2637 }
2638 } else {
2639 final int count = mChildren.size();
2640 for (int i = 0; i < count; ++i) {
2641 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
2642 final int appTokensCount = appTokens.size();
2643 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002644 if (appTokens.get(j).forAllWindowsUnchecked(callback,
2645 traverseTopToBottom)) {
2646 return true;
2647 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002648 }
2649 }
2650 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002651 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002652 }
2653
Wale Ogunwale1666e312016-12-16 11:27:18 -08002654 void setExitingTokensHasVisible(boolean hasVisible) {
2655 for (int i = mChildren.size() - 1; i >= 0; --i) {
2656 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
2657 for (int j = appTokens.size() - 1; j >= 0; --j) {
2658 appTokens.get(j).hasVisible = hasVisible;
2659 }
2660 }
2661 }
2662
2663 void removeExistingAppTokensIfPossible() {
2664 for (int i = mChildren.size() - 1; i >= 0; --i) {
2665 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
2666 for (int j = appTokens.size() - 1; j >= 0; --j) {
2667 final AppWindowToken token = appTokens.get(j);
2668 if (!token.hasVisible && !mService.mClosingApps.contains(token)
2669 && (!token.mIsExiting || token.isEmpty())) {
2670 // Make sure there is no animation running on this token, so any windows
2671 // associated with it will be removed as soon as their animations are
2672 // complete.
2673 token.mAppAnimator.clearAnimation();
2674 token.mAppAnimator.animating = false;
2675 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
2676 "performLayout: App token exiting now removed" + token);
2677 token.removeIfPossible();
2678 }
2679 }
2680 }
2681 }
2682
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002683 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002684 int getOrientation() {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002685 if (isStackVisible(DOCKED_STACK_ID) || isStackVisible(FREEFORM_WORKSPACE_STACK_ID)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002686 // Apps and their containers are not allowed to specify an orientation while the
2687 // docked or freeform stack is visible...except for the home stack/task if the
2688 // docked stack is minimized and it actually set something.
2689 if (mHomeStack != null && mHomeStack.isVisible()
2690 && mDividerControllerLocked.isMinimizedDock()) {
2691 final int orientation = mHomeStack.getOrientation();
2692 if (orientation != SCREEN_ORIENTATION_UNSET) {
2693 return orientation;
2694 }
2695 }
2696 return SCREEN_ORIENTATION_UNSPECIFIED;
2697 }
2698
2699 final int orientation = super.getOrientation();
2700 if (orientation != SCREEN_ORIENTATION_UNSET
2701 && orientation != SCREEN_ORIENTATION_BEHIND) {
2702 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
2703 "App is requesting an orientation, return " + orientation);
2704 return orientation;
2705 }
2706
2707 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
2708 "No app is requesting an orientation, return " + mService.mLastOrientation);
2709 // The next app has not been requested to be visible, so we keep the current orientation
2710 // to prevent freezing/unfreezing the display too early.
2711 return mService.mLastOrientation;
2712 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002713 }
2714
2715 /**
2716 * Window container class that contains all containers on this display that are not related to
2717 * Apps. E.g. status bar.
2718 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07002719 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
2720 /**
2721 * Compares two child window tokens returns -1 if the first is lesser than the second in
2722 * terms of z-order and 1 otherwise.
2723 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002724 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07002725 // Tokens with higher base layer are z-ordered on-top.
2726 mService.mPolicy.windowTypeToLayerLw(token1.windowType)
2727 < mService.mPolicy.windowTypeToLayerLw(token2.windowType) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002728
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002729 private final Predicate<WindowState> mGetOrientingWindow = w -> {
2730 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
2731 return false;
2732 }
2733 final int req = w.mAttrs.screenOrientation;
2734 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
2735 || req == SCREEN_ORIENTATION_UNSET) {
2736 return false;
2737 }
2738 return true;
2739 };
2740
Wale Ogunwale3a931692016-11-02 16:49:48 -07002741 private final String mName;
2742 NonAppWindowContainers(String name) {
2743 mName = name;
2744 }
2745
2746 void addChild(WindowToken token) {
2747 addChild(token, mWindowComparator);
2748 }
2749
2750 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002751 int getOrientation() {
2752 final WindowManagerPolicy policy = mService.mPolicy;
2753 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002754 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002755
2756 if (win != null) {
2757 final int req = win.mAttrs.screenOrientation;
2758 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
2759 if (policy.isKeyguardHostWindow(win.mAttrs)) {
2760 mService.mLastKeyguardForcedOrientation = req;
2761 }
2762 return (mService.mLastWindowForcedOrientation = req);
2763 }
2764
2765 mService.mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
2766
2767 if (policy.isKeyguardShowingAndNotOccluded()) {
2768 return mService.mLastKeyguardForcedOrientation;
2769 }
2770
2771 return SCREEN_ORIENTATION_UNSET;
2772 }
2773
2774 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07002775 String getName() {
2776 return mName;
2777 }
Robert Carr3b716242016-08-16 16:02:21 -07002778 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01002779
2780 /**
2781 * Interface to screenshot into various types, i.e. {@link Bitmap} and {@link GraphicBuffer}.
2782 */
2783 @FunctionalInterface
2784 private interface Screenshoter<E> {
2785 E screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
2786 boolean useIdentityTransform, int rotation);
2787 }
Craig Mautner59c00972012-07-30 12:10:24 -07002788}