blob: 5d55ac7cf41c71a4bd17cedcdc4c83de22ae7e81 [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;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070036import static android.view.WindowManager.INPUT_CONSUMER_NAVIGATION;
37import static android.view.WindowManager.INPUT_CONSUMER_PIP;
38import static android.view.WindowManager.INPUT_CONSUMER_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070039import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080040import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
41import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
42import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070043import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070044import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070045import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
46import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070047import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_DISABLE_WALLPAPER_TOUCH_EVENTS;
48import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070049import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070050import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwaleec731152016-09-08 20:18:57 -070051import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
Wale Ogunwale494009b82016-10-21 09:01:38 -070052import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Wale Ogunwaleec731152016-09-08 20:18:57 -070053import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
Wale Ogunwale494009b82016-10-21 09:01:38 -070054import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070055import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070056import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
57import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070058import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070059import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
60import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070061import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070062import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070063import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070064import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
65import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070066import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwaleec731152016-09-08 20:18:57 -070067import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070069import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070070import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
71import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwalec69694a2016-10-18 13:51:15 -070073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020076import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070077import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070078import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Jorim Jaggife762342016-10-13 14:33:27 +020079import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070080import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070081import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Wale Ogunwaleec731152016-09-08 20:18:57 -070082import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_MOVEMENT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070083import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070084import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070085import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080086import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070087import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
88import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070089import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070090import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
91import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER;
92import static com.android.server.wm.WindowManagerService.TYPE_LAYER_OFFSET;
93import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Wale Ogunwale494009b82016-10-21 09:01:38 -070094import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070095import static com.android.server.wm.WindowManagerService.dipToPixel;
96import static com.android.server.wm.WindowManagerService.localLOGV;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070097import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070098import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -070099import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700100import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700101import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700102
Andrii Kulian3a507b52016-09-19 18:14:12 -0700103import android.annotation.NonNull;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700104import android.app.ActivityManager.StackId;
Andrii Kulian441e4492016-09-29 15:25:00 -0700105import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700106import android.graphics.Bitmap;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700107import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800108import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700109import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700110import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100111import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700112import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700113import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700114import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700115import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700116import android.os.RemoteException;
117import android.os.SystemClock;
Chong Zhang8e89b312015-09-09 15:09:30 -0700118import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700119import android.util.Slog;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700120import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700121import android.view.DisplayInfo;
Wale Ogunwaleec731152016-09-08 20:18:57 -0700122import android.view.IWindow;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700123import android.view.InputChannel;
124import android.view.Surface;
125import android.view.SurfaceControl;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700126import android.view.WindowManagerPolicy;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700127
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700128import com.android.internal.util.FastPrintWriter;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700129import com.android.internal.view.IInputMethodClient;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700130import com.android.server.input.InputWindowHandle;
Craig Mautner59c00972012-07-30 12:10:24 -0700131
Robert Carr3b716242016-08-16 16:02:21 -0700132import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700133import java.io.PrintWriter;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700134import java.io.StringWriter;
Craig Mautner59c00972012-07-30 12:10:24 -0700135import java.util.ArrayList;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700136import java.util.Arrays;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700137import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700138import java.util.HashMap;
139import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700140import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700141import java.util.List;
Craig Mautner59c00972012-07-30 12:10:24 -0700142
Craig Mautner59c00972012-07-30 12:10:24 -0700143/**
144 * Utility class for keeping track of the WindowStates and other pertinent contents of a
145 * particular Display.
146 *
147 * IMPORTANT: No method from this class should ever be used without holding
148 * WindowManagerService.mWindowMap.
149 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700150class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700151 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700152
153 /** Unique identifier of this stack. */
154 private final int mDisplayId;
155
Wale Ogunwale3a931692016-11-02 16:49:48 -0700156 /** The containers below are the only child containers the display can have. */
157 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700158 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700159 // Contains all non-app window containers that should be displayed above the app containers
160 // (e.g. Status bar)
161 private final NonAppWindowContainers mAboveAppWindowsContainers =
162 new NonAppWindowContainers("mAboveAppWindowsContainers");
163 // Contains all non-app window containers that should be displayed below the app containers
164 // (e.g. Wallpaper).
165 private final NonAppWindowContainers mBelowAppWindowsContainers =
166 new NonAppWindowContainers("mBelowAppWindowsContainers");
167 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
168 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
169 // window containers together and move them in-sync if/when needed.
170 private final NonAppWindowContainers mImeWindowsContainers =
171 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700172
Wale Ogunwale3a931692016-11-02 16:49:48 -0700173 // Z-ordered (bottom-most first) list of all Window objects.
Craig Mautnerdc548482014-02-05 13:35:24 -0800174 private final WindowList mWindows = new WindowList();
Craig Mautner59c00972012-07-30 12:10:24 -0700175
Wale Ogunwale02319a62016-09-26 15:21:22 -0700176 // Mapping from a token IBinder to a WindowToken object on this display.
177 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
178
Craig Mautner59c00972012-07-30 12:10:24 -0700179 int mInitialDisplayWidth = 0;
180 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700181 int mInitialDisplayDensity = 0;
Craig Mautner59c00972012-07-30 12:10:24 -0700182 int mBaseDisplayWidth = 0;
183 int mBaseDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700184 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700185 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700186 private final DisplayInfo mDisplayInfo = new DisplayInfo();
187 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700188 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Craig Mautner59c00972012-07-30 12:10:24 -0700189
Craig Mautner6601b7b2013-04-29 10:29:11 -0700190 Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700191 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700192
Craig Mautner39834192012-09-02 07:47:24 -0700193 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700194 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700195 int pendingLayoutChanges;
Craig Mautner69b08182012-09-05 13:07:13 -0700196 final boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700197
Craig Mautnerdc548482014-02-05 13:35:24 -0800198 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700199 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800200
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800201 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
202 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700203 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700204
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700205 /** Detect user tapping outside of current focused task bounds .*/
206 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700207
Craig Mautner6601b7b2013-04-29 10:29:11 -0700208 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700209 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700210
Craig Mautner6601b7b2013-04-29 10:29:11 -0700211 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800212 private final Rect mTmpRect = new Rect();
213 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700214 private final RectF mTmpRectF = new RectF();
215 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800216 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700217
Craig Mautner9d808b12013-08-06 18:00:25 -0700218 final WindowManagerService mService;
219
Craig Mautner95da1082014-02-24 17:54:35 -0800220 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700221 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800222
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700223 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700224 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700225
Chong Zhang112eb8c2015-11-02 11:17:00 -0800226 final DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700227
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800228 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
229
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700230 /** Used when rebuilding window list to keep track of windows that have been removed. */
231 private WindowState[] mRebuildTmp = new WindowState[20];
232
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700233 /**
234 * Temporary list for comparison. Always clear this after use so we don't end up with
235 * orphaned windows references
236 */
237 private final ArrayList<WindowState> mTmpWindows = new ArrayList<>();
238
239 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
240
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700241 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
242 new TaskForResizePointSearchResult();
243 private final GetWindowOnDisplaySearchResult mTmpGetWindowOnDisplaySearchResult =
244 new GetWindowOnDisplaySearchResult();
245
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700246 // True if this display is in the process of being removed. Used to determine if the removal of
247 // the display's direct children should be allowed.
248 private boolean mRemovingDisplay = false;
249
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700250 private final WindowLayersController mLayersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700251 final WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700252 int mInputMethodAnimLayerAdjustment;
253
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800254 /**
Craig Mautner2d5618c2012-10-18 13:55:47 -0700255 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800256 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700257 * @param layersController window layer controller used to assign layer to the windows on this
258 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700259 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
260 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700261 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700262 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700263 WindowLayersController layersController, WallpaperController wallpaperController) {
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700264 mDisplay = display;
265 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700266 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700267 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700268 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700269 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700270 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700271 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700272 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800273 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700274 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800275 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700276
277 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700278 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700279 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700280 super.addChild(mAboveAppWindowsContainers, null);
281 super.addChild(mImeWindowsContainers, null);
Craig Mautner59c00972012-07-30 12:10:24 -0700282 }
283
284 int getDisplayId() {
285 return mDisplayId;
286 }
287
Wale Ogunwale02319a62016-09-26 15:21:22 -0700288 WindowToken getWindowToken(IBinder binder) {
289 return mTokenMap.get(binder);
290 }
291
292 AppWindowToken getAppWindowToken(IBinder binder) {
293 final WindowToken token = getWindowToken(binder);
294 if (token == null) {
295 return null;
296 }
297 return token.asAppWindowToken();
298 }
299
300 void setWindowToken(IBinder binder, WindowToken token) {
301 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
302 if (dc != null) {
303 // We currently don't support adding a window token to the display if the display
304 // already has the binder mapped to another token. If there is a use case for supporting
305 // this moving forward we will either need to merge the WindowTokens some how or have
306 // the binder map to a list of window tokens.
307 throw new IllegalArgumentException("Can't map token=" + token + " to display=" + this
308 + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
309 }
310 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700311
312 if (token.asAppWindowToken() == null) {
313 // Add non-app token to container hierarchy on the display. App tokens are added through
314 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700315 switch (token.windowType) {
316 case TYPE_WALLPAPER:
317 mBelowAppWindowsContainers.addChild(token);
318 break;
319 case TYPE_INPUT_METHOD:
320 case TYPE_INPUT_METHOD_DIALOG:
321 mImeWindowsContainers.addChild(token);
322 break;
323 default:
324 mAboveAppWindowsContainers.addChild(token);
325 break;
326 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700327 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700328 }
329
330 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700331 final WindowToken token = mTokenMap.remove(binder);
332 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3a931692016-11-02 16:49:48 -0700333 switch (token.windowType) {
334 case TYPE_WALLPAPER:
335 mBelowAppWindowsContainers.removeChild(token);
336 break;
337 case TYPE_INPUT_METHOD:
338 case TYPE_INPUT_METHOD_DIALOG:
339 mImeWindowsContainers.removeChild(token);
340 break;
341 default:
342 mAboveAppWindowsContainers.removeChild(token);
343 break;
344 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700345 }
346 return token;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700347 }
348
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700349 void removeAppToken(IBinder binder) {
350 final WindowToken token = removeWindowToken(binder);
351 if (token == null) {
352 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
353 return;
354 }
355
356 final AppWindowToken appToken = token.asAppWindowToken();
357
358 if (appToken == null) {
359 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
360 return;
361 }
362
363 appToken.onRemovedFromDisplay();
364 }
365
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700366 Display getDisplay() {
367 return mDisplay;
368 }
369
Craig Mautner59c00972012-07-30 12:10:24 -0700370 DisplayInfo getDisplayInfo() {
371 return mDisplayInfo;
372 }
373
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700374 DisplayMetrics getDisplayMetrics() {
375 return mDisplayMetrics;
376 }
377
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100378 DockedStackDividerController getDockedDividerController() {
379 return mDividerControllerLocked;
380 }
381
Winson Chung655332c2016-10-31 13:14:28 -0700382 PinnedStackController getPinnedStackController() {
383 return mPinnedStackControllerLocked;
384 }
385
Jeff Browna506a6e2013-06-04 00:02:38 -0700386 /**
387 * Returns true if the specified UID has access to this display.
388 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700389 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700390 return mDisplay.hasAccess(uid);
391 }
392
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700393 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700394 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -0700395 }
396
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700397 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700398 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800399 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -0800400 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700401 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700402 }
403
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700404 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700405 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
406 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700407 if (stack.mStackId == stackId) {
408 return stack;
409 }
410 }
411 return null;
412 }
413
Andrii Kulian441e4492016-09-29 15:25:00 -0700414 @Override
415 void onConfigurationChanged(Configuration newParentConfig) {
416 super.onConfigurationChanged(newParentConfig);
417
Andrii Kulian3a507b52016-09-19 18:14:12 -0700418 // The display size information is heavily dependent on the resources in the current
419 // configuration, so we need to reconfigure it every time the configuration changes.
420 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
421 mService.reconfigureDisplayLocked(this);
422
423 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -0700424 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -0700425 }
Andrii Kulian3a507b52016-09-19 18:14:12 -0700426
Andrii Kulian441e4492016-09-29 15:25:00 -0700427 /**
428 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
429 * bounds were updated.
430 */
431 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700432 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
433 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -0700434 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -0700435 changedStackList.add(stack.mStackId);
436 }
437 }
438 }
439
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700440 @Override
441 boolean fillsParent() {
442 return true;
443 }
444
445 @Override
446 boolean isVisible() {
447 return true;
448 }
449
450 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700451 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -0700452 super.onAppTransitionDone();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700453 rebuildAppWindowList();
454 }
455
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700456 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -0700457 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700458 final WindowManagerPolicy policy = mService.mPolicy;
459
460 // TODO: All the logic before the last return statement in this method should really go in
461 // #NonAppWindowContainer.getOrientation() since it is trying to decide orientation based
462 // on non-app windows. But, we can not do that until the window list is always correct in
463 // terms of z-ordering based on layers.
464 if (mService.mDisplayFrozen) {
465 if (mService.mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
466 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
467 "Display is frozen, return " + mService.mLastWindowForcedOrientation);
468 // If the display is frozen, some activities may be in the middle of restarting, and
469 // thus have removed their old window. If the window has the flag to hide the lock
470 // screen, then the lock screen can re-appear and inflict its own orientation on us.
471 // Keep the orientation stable until this all settles down.
472 return mService.mLastWindowForcedOrientation;
473 } else if (policy.isKeyguardLocked()) {
474 // Use the last orientation the while the display is frozen with the keyguard
475 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
476 // window. We don't want to check the show when locked window directly though as
477 // things aren't stable while the display is frozen, for example the window could be
478 // momentarily unavailable due to activity relaunch.
479 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
480 + "return " + mService.mLastOrientation);
481 return mService.mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -0700482 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700483 } else {
484 for (int pos = mWindows.size() - 1; pos >= 0; --pos) {
485 final WindowState win = mWindows.get(pos);
486 if (win.mAppToken != null) {
487 // We hit an application window. so the orientation will be determined by the
488 // app window. No point in continuing further.
489 break;
490 }
491 if (!win.isVisibleLw() || !win.mPolicyVisibilityAfterAnim) {
492 continue;
493 }
494 int req = win.mAttrs.screenOrientation;
495 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND) {
496 continue;
497 }
498
499 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
500 if (policy.isKeyguardHostWindow(win.mAttrs)) {
501 mService.mLastKeyguardForcedOrientation = req;
502 }
503 return (mService.mLastWindowForcedOrientation = req);
504 }
505 mService.mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
506
Jorim Jaggife762342016-10-13 14:33:27 +0200507 if (policy.isKeyguardShowingAndNotOccluded()) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700508 return mService.mLastKeyguardForcedOrientation;
509 }
Wale Ogunwale51362492016-09-08 17:49:17 -0700510 }
511
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700512 // Top system windows are not requesting an orientation. Start searching from apps.
513 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -0700514 }
515
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700516 void updateDisplayInfo() {
Craig Mautner722285e2012-09-07 13:55:58 -0700517 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700518 mDisplay.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700519 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
520 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800521 }
Craig Mautner722285e2012-09-07 13:55:58 -0700522 }
523
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700524 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700525 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
526 if (displayManagerInternal != null) {
527 // Bootstrap the default logical display from the display manager.
528 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
529 if (newDisplayInfo != null) {
530 mDisplayInfo.copyFrom(newDisplayInfo);
531 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700532 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700533
Filip Gruszczynski608797e2015-11-12 19:08:20 -0800534 mBaseDisplayWidth = mInitialDisplayWidth = mDisplayInfo.logicalWidth;
535 mBaseDisplayHeight = mInitialDisplayHeight = mDisplayInfo.logicalHeight;
536 mBaseDisplayDensity = mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
537 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700538 }
539
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700540 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700541 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800542 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700543 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800544 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
545 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700546 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800547 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700548 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800549 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700550 out.set(left, top, left + width, top + height);
551 }
552
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700553 private void getLogicalDisplayRect(Rect out, int orientation) {
554 getLogicalDisplayRect(out);
555
556 // Rotate the Rect if needed.
557 final int currentRotation = mDisplayInfo.rotation;
558 final int rotationDelta = deltaRotation(currentRotation, orientation);
559 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
560 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
561 mTmpRectF.set(out);
562 mTmpMatrix.mapRect(mTmpRectF);
563 mTmpRectF.round(out);
564 }
565 }
566
Chong Zhangf66db432016-01-13 10:39:51 -0800567 void getContentRect(Rect out) {
568 out.set(mContentRect);
569 }
570
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700571 /** Refer to {@link WindowManagerService#attachStack(int, int, boolean)} */
572 void attachStack(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700573 mTaskStackContainers.attachStack(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800574 }
575
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800576 void moveStack(TaskStack stack, boolean toTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700577 mTaskStackContainers.moveStack(stack, toTop);
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700578 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700579
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700580 @Override
581 protected void addChild(DisplayChildWindowContainer child,
582 Comparator<DisplayChildWindowContainer> comparator) {
583 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
584 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700585
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700586 @Override
587 protected void addChild(DisplayChildWindowContainer child, int index) {
588 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
589 }
590
591 @Override
592 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700593 // Only allow removal of direct children from this display if the display is in the process
594 // of been removed.
595 if (mRemovingDisplay) {
596 super.removeChild(child);
597 return;
598 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700599 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800600 }
601
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700602 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700603 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
604 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700605 final int taskId = stack.taskIdFromPoint(x, y);
606 if (taskId != -1) {
607 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -0700608 }
609 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800610 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -0700611 }
612
Chong Zhang8e89b312015-09-09 15:09:30 -0700613 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -0800614 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -0700615 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -0700616 */
Wale Ogunwale15ead902016-09-02 14:30:11 -0700617 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700618 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700619 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700620 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
621 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -0700622 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700623 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -0700624 }
Chong Zhang9184ec62015-09-24 12:32:21 -0700625
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700626 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
627 if (mTmpTaskForResizePointSearchResult.searchDone) {
628 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -0700629 }
630 }
Chong Zhang9184ec62015-09-24 12:32:21 -0700631 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -0700632 }
633
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700634 void setTouchExcludeRegion(Task focusedTask) {
Craig Mautner6601b7b2013-04-29 10:29:11 -0700635 mTouchExcludeRegion.set(mBaseDisplayRect);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700636 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700637 mTmpRect2.setEmpty();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700638 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
639 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700640 stack.setTouchExcludeRegion(
641 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
Craig Mautner6601b7b2013-04-29 10:29:11 -0700642 }
Chong Zhangd8ceb852015-11-11 14:53:41 -0800643 // If we removed the focused task above, add it back and only leave its
644 // outside touch area in the exclusion. TapDectector is not interested in
645 // any touch inside the focused task itself.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700646 if (!mTmpRect2.isEmpty()) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800647 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
648 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800649 final WindowState inputMethod = mService.mInputMethodWindow;
650 if (inputMethod != null && inputMethod.isVisibleLw()) {
651 // If the input method is visible and the user is typing, we don't want these touch
652 // events to be intercepted and used to change focus. This would likely cause a
653 // disappearance of the input method.
654 inputMethod.getTouchableRegion(mTmpRegion);
655 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
656 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800657 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
658 WindowState win = mTapExcludedWindows.get(i);
659 win.getTouchableRegion(mTmpRegion);
660 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
661 }
Jorim Jaggife762342016-10-13 14:33:27 +0200662 if (getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100663 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -0700664 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100665 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
666 }
Craig Mautner1bef3892015-02-17 15:09:47 -0800667 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700668 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -0800669 }
Craig Mautner6601b7b2013-04-29 10:29:11 -0700670 }
671
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700672 void switchUser() {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700673 final int count = mWindows.size();
674 for (int i = 0; i < count; i++) {
675 final WindowState win = mWindows.get(i);
Craig Mautner858d8a62013-04-23 17:08:34 -0700676 if (win.isHiddenFromUserLocked()) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800677 if (DEBUG_VISIBILITY) Slog.w(TAG_WM, "user changing, hiding " + win
Wale Ogunwale498e8c92015-02-13 09:42:46 -0800678 + ", attrs=" + win.mAttrs.type + ", belonging to " + win.mOwnerUid);
Craig Mautner858d8a62013-04-23 17:08:34 -0700679 win.hideLw(false);
680 }
681 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700682
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700683 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
684 mTaskStackContainers.get(stackNdx).switchUser();
Craig Mautner858d8a62013-04-23 17:08:34 -0700685 }
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700686
687 rebuildAppWindowList();
Craig Mautner858d8a62013-04-23 17:08:34 -0700688 }
689
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700690 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700691 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
692 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -0700693 }
694 }
695
696 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800697 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -0700698 }
699
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700700 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800701 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -0700702 }
703
704 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800705 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -0700706 }
707
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700708 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800709 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -0700710 }
711
Wale Ogunwale10124582016-09-15 20:25:50 -0700712 @Override
713 void removeIfPossible() {
714 if (isAnimating()) {
715 mDeferredRemoval = true;
716 return;
Craig Mautner2eb15342013-08-07 13:13:35 -0700717 }
Wale Ogunwale10124582016-09-15 20:25:50 -0700718 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -0700719 }
720
Wale Ogunwale10124582016-09-15 20:25:50 -0700721 @Override
722 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700723 mRemovingDisplay = true;
724 try {
725 super.removeImmediately();
726 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
727 mDimLayerController.close();
728 if (mDisplayId == DEFAULT_DISPLAY) {
729 mService.unregisterPointerEventListener(mTapDetector);
730 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
731 }
732 } finally {
733 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -0800734 }
Craig Mautner95da1082014-02-24 17:54:35 -0800735 }
736
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700737 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -0700738 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700739 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -0700740 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
741
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700742 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -0700743 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -0800744 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700745 return false;
Craig Mautner95da1082014-02-24 17:54:35 -0800746 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700747 return true;
Craig Mautner95da1082014-02-24 17:54:35 -0800748 }
749
Wale Ogunwale10124582016-09-15 20:25:50 -0700750 boolean animateForIme(float interpolatedValue, float animationTarget,
751 float dividerAnimationTarget) {
752 boolean updated = false;
753
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700754 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
755 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700756 if (stack == null || !stack.isAdjustedForIme()) {
757 continue;
758 }
759
760 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
761 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
762 updated = true;
763 } else {
764 mDividerControllerLocked.mLastAnimationProgress =
765 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
766 mDividerControllerLocked.mLastDividerProgress =
767 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
768 updated |= stack.updateAdjustForIme(
769 mDividerControllerLocked.mLastAnimationProgress,
770 mDividerControllerLocked.mLastDividerProgress,
771 false /* force */);
772 }
773 if (interpolatedValue >= 1f) {
774 stack.endImeAdjustAnimation();
775 }
776 }
777
778 return updated;
779 }
780
781 boolean clearImeAdjustAnimation() {
782 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700783 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
784 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700785 if (stack != null && stack.isAdjustedForIme()) {
786 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
787 changed = true;
788 }
789 }
790 return changed;
791 }
792
793 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700794 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
795 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700796 if (stack.isVisible() && stack.isAdjustedForIme()) {
797 stack.beginImeAdjustAnimation();
798 }
799 }
800 }
801
802 void adjustForImeIfNeeded() {
803 final WindowState imeWin = mService.mInputMethodWindow;
804 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
805 && !mDividerControllerLocked.isImeHideRequested();
806 final boolean dockVisible = mService.isStackVisibleLocked(DOCKED_STACK_ID);
807 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
808 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
809 imeTargetStack.getDockSide() : DOCKED_INVALID;
810 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
811 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
812 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
813 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
814 final boolean imeHeightChanged = imeVisible &&
815 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
816
817 // The divider could be adjusted for IME position, or be thinner than usual,
818 // or both. There are three possible cases:
819 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
820 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
821 // - If IME is not visible, divider is not moved and is normal width.
822
823 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700824 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
825 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700826 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
827 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)) {
828 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
829 } else {
830 stack.resetAdjustedForIme(false);
831 }
832 }
833 mDividerControllerLocked.setAdjustedForIme(
834 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
835 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700836 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
837 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700838 stack.resetAdjustedForIme(!dockVisible);
839 }
840 mDividerControllerLocked.setAdjustedForIme(
841 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
842 }
Winson Chung655332c2016-10-31 13:14:28 -0700843 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -0700844 }
845
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700846 void setInputMethodAnimLayerAdjustment(int adj) {
847 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
848 mInputMethodAnimLayerAdjustment = adj;
849 final WindowState imw = mService.mInputMethodWindow;
850 if (imw != null) {
851 imw.adjustAnimLayer(adj);
852 }
853 for (int i = mService.mInputMethodDialogs.size() - 1; i >= 0; i--) {
854 final WindowState dialog = mService.mInputMethodDialogs.get(i);
855 // TODO: This and other places setting mAnimLayer can probably use WS.adjustAnimLayer,
856 // but need to make sure we are not setting things twice for child windows that are
857 // already in the list.
858 dialog.mWinAnimator.mAnimLayer = dialog.mLayer + adj;
859 if (DEBUG_LAYERS) Slog.v(TAG_WM, "IM win " + imw
860 + " anim layer: " + dialog.mWinAnimator.mAnimLayer);
861 }
862 }
863
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700864 /**
865 * If a window that has an animation specifying a colored background and the current wallpaper
866 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
867 * suddenly disappear.
868 */
869 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
870 for (int i = mWindows.size() - 1; i >= 0; --i) {
871 final WindowState win = mWindows.get(i);
872 if (win.mIsWallpaper && win.isVisibleNow()) {
873 return win.mWinAnimator.mAnimLayer;
874 }
875 }
876 return winAnimator.mAnimLayer;
877 }
878
Wale Ogunwale10124582016-09-15 20:25:50 -0700879 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700880 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
881 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -0700882 stack.prepareFreezingTaskBounds();
883 }
884 }
885
Wale Ogunwale94744212015-09-21 19:01:47 -0700886 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700887 getLogicalDisplayRect(mTmpRect, newRotation);
888
889 // Compute a transform matrix to undo the coordinate space transformation,
890 // and present the window at the same physical position it previously occupied.
891 final int deltaRotation = deltaRotation(newRotation, oldRotation);
892 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
893
894 mTmpRectF.set(bounds);
895 mTmpMatrix.mapRect(mTmpRectF);
896 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -0700897 }
898
Wale Ogunwale4a02d812015-02-12 23:01:38 -0800899 static int deltaRotation(int oldRotation, int newRotation) {
900 int delta = newRotation - oldRotation;
901 if (delta < 0) delta += 4;
902 return delta;
903 }
904
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700905 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700906 Matrix outMatrix) {
907 // For rotations without Z-ordering we don't need the target rectangle's position.
908 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
909 displayHeight, outMatrix);
910 }
911
912 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
913 float displayWidth, float displayHeight, Matrix outMatrix) {
914 switch (rotation) {
915 case ROTATION_0:
916 outMatrix.reset();
917 break;
918 case ROTATION_270:
919 outMatrix.setRotate(270, 0, 0);
920 outMatrix.postTranslate(0, displayHeight);
921 outMatrix.postTranslate(rectTop, 0);
922 break;
923 case ROTATION_180:
924 outMatrix.reset();
925 break;
926 case ROTATION_90:
927 outMatrix.setRotate(90, 0, 0);
928 outMatrix.postTranslate(displayWidth, 0);
929 outMatrix.postTranslate(-rectTop, rectLeft);
930 break;
931 }
932 }
933
Craig Mautnera91f9e22012-09-14 16:22:08 -0700934 public void dump(String prefix, PrintWriter pw) {
935 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
936 final String subPrefix = " " + prefix;
937 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
938 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
939 pw.print("dpi");
940 if (mInitialDisplayWidth != mBaseDisplayWidth
941 || mInitialDisplayHeight != mBaseDisplayHeight
942 || mInitialDisplayDensity != mBaseDisplayDensity) {
943 pw.print(" base=");
944 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
945 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
946 }
Jeff Brownd46747a2015-04-15 19:02:36 -0700947 if (mDisplayScalingDisabled) {
948 pw.println(" noscale");
949 }
Craig Mautnera91f9e22012-09-14 16:22:08 -0700950 pw.print(" cur=");
951 pw.print(mDisplayInfo.logicalWidth);
952 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
953 pw.print(" app=");
954 pw.print(mDisplayInfo.appWidth);
955 pw.print("x"); pw.print(mDisplayInfo.appHeight);
956 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
957 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
958 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
959 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700960 pw.println(subPrefix + "deferred=" + mDeferredRemoval
961 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800962
Craig Mautnerdc548482014-02-05 13:35:24 -0800963 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -0700964 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700965 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
966 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800967 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700968 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800969
Craig Mautnerdc548482014-02-05 13:35:24 -0800970 pw.println();
971 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700972 pw.println();
973 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800974 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700975 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700976 pw.print(" Exiting #"); pw.print(i);
977 pw.print(' '); pw.print(token);
978 pw.println(':');
979 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800980 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700981 }
Craig Mautner59c00972012-07-30 12:10:24 -0700982 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -0700983 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -0700984 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -0700985 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -0700986 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -0700987 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700988
989 if (mInputMethodAnimLayerAdjustment != 0) {
990 pw.println(subPrefix
991 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
992 }
Craig Mautner59c00972012-07-30 12:10:24 -0700993 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800994
995 @Override
996 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700997 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700998 }
999
1000 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001001 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08001002 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07001003
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001004 /**
1005 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
1006 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07001007 TaskStack getDockedStackLocked() {
Wale Ogunwalee45899a2015-10-01 11:30:34 -07001008 final TaskStack stack = mService.mStackIdToStack.get(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001009 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07001010 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001011
1012 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001013 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02001014 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001015 */
Jorim Jaggife762342016-10-13 14:33:27 +02001016 TaskStack getDockedStackIgnoringVisibility() {
1017 return mService.mStackIdToStack.get(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001018 }
1019
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001020 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001021 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
1022 WindowState touchedWin = null;
1023 final int x = (int) xf;
1024 final int y = (int) yf;
1025
1026 for (int i = mWindows.size() - 1; i >= 0; i--) {
1027 WindowState window = mWindows.get(i);
1028 final int flags = window.mAttrs.flags;
1029 if (!window.isVisibleLw()) {
1030 continue;
1031 }
1032 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
1033 continue;
1034 }
1035
1036 window.getVisibleBounds(mTmpRect);
1037 if (!mTmpRect.contains(x, y)) {
1038 continue;
1039 }
1040
1041 window.getTouchableRegion(mTmpRegion);
1042
1043 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
1044 if (mTmpRegion.contains(x, y) || touchFlags == 0) {
1045 touchedWin = window;
1046 break;
1047 }
1048 }
1049
1050 return touchedWin;
1051 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07001052
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001053 boolean canAddToastWindowForUid(int uid) {
1054 // We allow one toast window per UID being shown at a time.
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001055 final int windowCount = mWindows.size();
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001056 for (int i = 0; i < windowCount; i++) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001057 final WindowState window = mWindows.get(i);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001058 if (window.mAttrs.type == TYPE_TOAST && window.mOwnerUid == uid
Svet Ganov62a40f82016-09-29 00:43:51 -07001059 && !window.mPermanentlyHidden && !window.mAnimatingExit
1060 && !window.mRemoveOnExit) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001061 return false;
1062 }
1063 }
1064 return true;
1065 }
1066
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001067 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001068 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
1069 return;
1070 }
1071 final int lostFocusUid = oldFocus.mOwnerUid;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001072 final int windowCount = mWindows.size();
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001073 final Handler handler = mService.mH;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001074 for (int i = 0; i < windowCount; i++) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001075 final WindowState window = mWindows.get(i);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001076 if (window.mAttrs.type == TYPE_TOAST && window.mOwnerUid == lostFocusUid) {
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001077 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, window)) {
1078 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, window),
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001079 window.mAttrs.hideTimeoutMilliseconds);
1080 }
1081 }
1082 }
1083 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001084
1085 WindowState findFocusedWindow() {
1086 final AppWindowToken focusedApp = mService.mFocusedApp;
1087
1088 for (int i = mWindows.size() - 1; i >= 0; i--) {
1089 final WindowState win = mWindows.get(i);
1090
1091 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + i + " = " + win
1092 + ", flags=" + win.mAttrs.flags + ", canReceive=" + win.canReceiveKeys());
1093
1094 if (!win.canReceiveKeys()) {
1095 continue;
1096 }
1097
1098 final AppWindowToken wtoken = win.mAppToken;
1099
1100 // If this window's application has been removed, just skip it.
1101 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
1102 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
1103 + (wtoken.removed ? "removed" : "sendingToBottom"));
1104 continue;
1105 }
1106
1107 if (focusedApp == null) {
1108 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
1109 + " using new focus @ " + i + " = " + win);
1110 return win;
1111 }
1112
1113 if (!focusedApp.windowsAreFocusable()) {
1114 // Current focused app windows aren't focusable...
1115 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
1116 + " focusable using new focus @ " + i + " = " + win);
1117 return win;
1118 }
1119
1120 // Descend through all of the app tokens and find the first that either matches
1121 // win.mAppToken (return win) or mFocusedApp (return null).
1122 if (wtoken != null && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001123 if (focusedApp.compareTo(wtoken) > 0) {
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001124 // App stack below focused app stack. No focus for you!!!
1125 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
1126 "findFocusedWindow: Reached focused app=" + focusedApp);
1127 return null;
1128 }
1129 }
1130
1131 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ "
1132 + i + " = " + win);
1133 return win;
1134 }
1135
1136 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
1137 return null;
1138 }
Wale Ogunwaleec731152016-09-08 20:18:57 -07001139
Wale Ogunwale07bcab72016-10-14 15:30:09 -07001140 void addAppWindowToWindowList(final WindowState win) {
Wale Ogunwaleec731152016-09-08 20:18:57 -07001141 final IWindow client = win.mClient;
1142
1143 WindowList tokenWindowList = getTokenWindowsOnDisplay(win.mToken);
1144 if (!tokenWindowList.isEmpty()) {
Wale Ogunwale07bcab72016-10-14 15:30:09 -07001145 addAppWindowExisting(win, tokenWindowList);
1146 return;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001147 }
1148
1149 // No windows from this token on this display
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001150 if (localLOGV) Slog.v(TAG_WM, "Figuring out where to add app window "
Wale Ogunwaleec731152016-09-08 20:18:57 -07001151 + client.asBinder() + " (token=" + this + ")");
1152
1153 final WindowToken wToken = win.mToken;
1154
1155 // Figure out where the window should go, based on the order of applications.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001156 mTmpGetWindowOnDisplaySearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001157 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1158 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001159 stack.getWindowOnDisplayBeforeToken(this, wToken, mTmpGetWindowOnDisplaySearchResult);
1160 if (mTmpGetWindowOnDisplaySearchResult.reachedToken) {
Wale Ogunwaleec731152016-09-08 20:18:57 -07001161 // We have reach the token we are interested in. End search.
1162 break;
1163 }
1164 }
1165
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001166 WindowState pos = mTmpGetWindowOnDisplaySearchResult.foundWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001167
1168 // We now know the index into the apps. If we found an app window above, that gives us the
1169 // position; else we need to look some more.
1170 if (pos != null) {
1171 // Move behind any windows attached to this one.
Wale Ogunwale02319a62016-09-26 15:21:22 -07001172 final WindowToken atoken = getWindowToken(pos.mClient.asBinder());
Wale Ogunwaleec731152016-09-08 20:18:57 -07001173 if (atoken != null) {
1174 tokenWindowList = getTokenWindowsOnDisplay(atoken);
1175 final int NC = tokenWindowList.size();
1176 if (NC > 0) {
1177 WindowState bottom = tokenWindowList.get(0);
1178 if (bottom.mSubLayer < 0) {
1179 pos = bottom;
1180 }
1181 }
1182 }
1183 addWindowToListBefore(win, pos);
Wale Ogunwale07bcab72016-10-14 15:30:09 -07001184 return;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001185 }
1186
1187 // Continue looking down until we find the first token that has windows on this display.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001188 mTmpGetWindowOnDisplaySearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001189 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1190 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001191 stack.getWindowOnDisplayAfterToken(this, wToken, mTmpGetWindowOnDisplaySearchResult);
1192 if (mTmpGetWindowOnDisplaySearchResult.foundWindow != null) {
Wale Ogunwaleec731152016-09-08 20:18:57 -07001193 // We have found a window after the token. End search.
1194 break;
1195 }
1196 }
1197
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001198 pos = mTmpGetWindowOnDisplaySearchResult.foundWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001199
1200 if (pos != null) {
1201 // Move in front of any windows attached to this one.
Wale Ogunwale02319a62016-09-26 15:21:22 -07001202 final WindowToken atoken = getWindowToken(pos.mClient.asBinder());
Wale Ogunwaleec731152016-09-08 20:18:57 -07001203 if (atoken != null) {
1204 final WindowState top = atoken.getTopWindow();
1205 if (top != null && top.mSubLayer >= 0) {
1206 pos = top;
1207 }
1208 }
1209 addWindowToListAfter(win, pos);
Wale Ogunwale07bcab72016-10-14 15:30:09 -07001210 return;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001211 }
1212
1213 // Just search for the start of this layer.
1214 final int myLayer = win.mBaseLayer;
1215 int i;
1216 for (i = mWindows.size() - 1; i >= 0; --i) {
1217 final WindowState w = mWindows.get(i);
1218 // Dock divider shares the base layer with application windows, but we want to always
1219 // keep it above the application windows. The sharing of the base layer is intended
1220 // for window animations, which need to be above the dock divider for the duration
1221 // of the animation.
1222 if (w.mBaseLayer <= myLayer && w.mAttrs.type != TYPE_DOCK_DIVIDER) {
1223 break;
1224 }
1225 }
1226 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1227 "Based on layer: Adding window " + win + " at " + (i + 1) + " of "
1228 + mWindows.size());
1229 mWindows.add(i + 1, win);
1230 mService.mWindowsChanged = true;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001231 }
1232
1233 /** Adds this non-app window to the window list. */
1234 void addNonAppWindowToWindowList(WindowState win) {
1235 // Figure out where window should go, based on layer.
1236 int i;
1237 for (i = mWindows.size() - 1; i >= 0; i--) {
1238 final WindowState otherWin = mWindows.get(i);
1239 if (otherWin.getBaseType() != TYPE_WALLPAPER && otherWin.mBaseLayer <= win.mBaseLayer) {
1240 // Wallpaper wanders through the window list, for example to position itself
1241 // directly behind keyguard. Because of this it will break the ordering based on
1242 // WindowState.mBaseLayer. There might windows with higher mBaseLayer behind it and
1243 // we don't want the new window to appear above them. An example of this is adding
1244 // of the docked stack divider. Consider a scenario with the following ordering (top
1245 // to bottom): keyguard, wallpaper, assist preview, apps. We want the dock divider
1246 // to land below the assist preview, so the dock divider must ignore the wallpaper,
1247 // with which it shares the base layer.
1248 break;
1249 }
1250 }
1251
1252 i++;
1253 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1254 "Free window: Adding window " + this + " at " + i + " of " + mWindows.size());
1255 mWindows.add(i, win);
1256 mService.mWindowsChanged = true;
1257 }
1258
Wale Ogunwaleb9a07c32016-10-12 14:55:56 -07001259 void addToWindowList(WindowState win, int index) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001260 mService.mWindowsChanged = true;
Wale Ogunwaleb9a07c32016-10-12 14:55:56 -07001261 mWindows.add(index, win);
1262 }
1263
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001264 boolean removeFromWindowList(WindowState win) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001265 mService.mWindowsChanged = true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001266 return mWindows.remove(win);
1267 }
1268
1269 private int removeWindowAndChildrenFromWindowList(WindowState win, int interestingPos) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001270 int wpos = mWindows.indexOf(win);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001271 if (wpos < 0) {
1272 return interestingPos;
1273 }
1274
1275 if (wpos < interestingPos) interestingPos--;
1276 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG_WM, "Temp removing at " + wpos + ": " + this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001277 mWindows.remove(wpos);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001278 mService.mWindowsChanged = true;
1279 int childWinCount = win.mChildren.size();
1280 while (childWinCount > 0) {
1281 childWinCount--;
1282 final WindowState cw = win.mChildren.get(childWinCount);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001283 int cpos = mWindows.indexOf(cw);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001284 if (cpos >= 0) {
1285 if (cpos < interestingPos) interestingPos--;
1286 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG_WM,
1287 "Temp removing child at " + cpos + ": " + cw);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001288 mWindows.remove(cpos);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001289 }
1290 }
1291 return interestingPos;
1292 }
1293
Wale Ogunwaleec731152016-09-08 20:18:57 -07001294 void addChildWindowToWindowList(WindowState win) {
1295 final WindowState parentWindow = win.getParentWindow();
1296
1297 WindowList windowsOnSameDisplay = getTokenWindowsOnDisplay(win.mToken);
1298
1299 // Figure out this window's ordering relative to the parent window.
1300 final int wCount = windowsOnSameDisplay.size();
1301 final int sublayer = win.mSubLayer;
1302 int largestSublayer = Integer.MIN_VALUE;
1303 WindowState windowWithLargestSublayer = null;
1304 int i;
1305 for (i = 0; i < wCount; i++) {
1306 WindowState w = windowsOnSameDisplay.get(i);
1307 final int wSublayer = w.mSubLayer;
1308 if (wSublayer >= largestSublayer) {
1309 largestSublayer = wSublayer;
1310 windowWithLargestSublayer = w;
1311 }
1312 if (sublayer < 0) {
1313 // For negative sublayers, we go below all windows in the same sublayer.
1314 if (wSublayer >= sublayer) {
1315 addWindowToListBefore(win, wSublayer >= 0 ? parentWindow : w);
1316 break;
1317 }
1318 } else {
1319 // For positive sublayers, we go above all windows in the same sublayer.
1320 if (wSublayer > sublayer) {
1321 addWindowToListBefore(win, w);
1322 break;
1323 }
1324 }
1325 }
1326 if (i >= wCount) {
1327 if (sublayer < 0) {
1328 addWindowToListBefore(win, parentWindow);
1329 } else {
1330 addWindowToListAfter(win,
1331 largestSublayer >= 0 ? windowWithLargestSublayer : parentWindow);
1332 }
1333 }
1334 }
1335
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001336 /** Updates the layer assignment of windows on this display. */
1337 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001338 mLayersController.assignWindowLayers(mWindows.getReadOnly());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001339 if (setLayoutNeeded) {
1340 setLayoutNeeded();
1341 }
1342 }
1343
Wale Ogunwale0303c572016-10-20 10:16:29 -07001344 void adjustWallpaperWindows() {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001345 if (mWallpaperController.adjustWallpaperWindows(mWindows.getReadOnly())) {
Wale Ogunwale0303c572016-10-20 10:16:29 -07001346 assignWindowLayers(true /*setLayoutNeeded*/);
1347 }
1348 }
1349
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001350 /**
1351 * Z-orders the display window list so that:
1352 * <ul>
1353 * <li>Any windows that are currently below the wallpaper window stay below the wallpaper
1354 * window.
1355 * <li>Exiting application windows are at the bottom, but above the wallpaper window.
1356 * <li>All other application windows are above the exiting application windows and ordered based
1357 * on the ordering of their stacks and tasks on the display.
1358 * <li>Non-application windows are at the very top.
1359 * </ul>
1360 * <p>
1361 * NOTE: This isn't a complete picture of what the user see. Further manipulation of the window
1362 * surface layering is done in {@link WindowLayersController}.
1363 */
1364 void rebuildAppWindowList() {
1365 int count = mWindows.size();
1366 int i;
1367 int lastBelow = -1;
1368 int numRemoved = 0;
1369
1370 if (mRebuildTmp.length < count) {
1371 mRebuildTmp = new WindowState[count + 10];
1372 }
1373
1374 // First remove all existing app windows.
1375 i = 0;
1376 while (i < count) {
1377 final WindowState w = mWindows.get(i);
1378 if (w.mAppToken != null) {
1379 final WindowState win = mWindows.remove(i);
1380 win.mRebuilding = true;
1381 mRebuildTmp[numRemoved] = win;
1382 mService.mWindowsChanged = true;
1383 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG_WM, "Rebuild removing window: " + win);
1384 count--;
1385 numRemoved++;
1386 continue;
1387 } else if (lastBelow == i-1) {
1388 if (w.mAttrs.type == TYPE_WALLPAPER) {
1389 lastBelow = i;
1390 }
1391 }
1392 i++;
1393 }
1394
1395 // Keep whatever windows were below the app windows still below, by skipping them.
1396 lastBelow++;
1397 i = lastBelow;
1398
1399 // First add all of the exiting app tokens... these are no longer in the main app list,
1400 // but still have windows shown. We put them in the back because now that the animation is
1401 // over we no longer will care about them.
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001402 final int numStacks = mTaskStackContainers.size();
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001403 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001404 AppTokenList exitingAppTokens = mTaskStackContainers.get(stackNdx).mExitingAppTokens;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001405 int NT = exitingAppTokens.size();
1406 for (int j = 0; j < NT; j++) {
Wale Ogunwale360a8bc2016-10-10 13:25:26 -07001407 i = exitingAppTokens.get(j).rebuildWindowListUnchecked(i);
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001408 }
1409 }
1410
1411 // And add in the still active app tokens in Z order.
1412 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001413 i = mTaskStackContainers.get(stackNdx).rebuildWindowList(i);
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001414 }
1415
1416 i -= lastBelow;
1417 if (i != numRemoved) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001418 setLayoutNeeded();
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001419 Slog.w(TAG_WM, "On display=" + mDisplayId + " Rebuild removed " + numRemoved
1420 + " windows but added " + i + " rebuildAppWindowListLocked() "
1421 + " callers=" + Debug.getCallers(10));
1422 for (i = 0; i < numRemoved; i++) {
1423 WindowState ws = mRebuildTmp[i];
1424 if (ws.mRebuilding) {
1425 StringWriter sw = new StringWriter();
1426 PrintWriter pw = new FastPrintWriter(sw, false, 1024);
1427 ws.dump(pw, "", true);
1428 pw.flush();
1429 Slog.w(TAG_WM, "This window was lost: " + ws);
1430 Slog.w(TAG_WM, sw.toString());
1431 ws.mWinAnimator.destroySurfaceLocked();
1432 }
1433 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001434 Slog.w(TAG_WM, "Current window hierarchy:");
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001435 dumpChildrenNames();
1436 Slog.w(TAG_WM, "Final window list:");
1437 dumpWindows();
1438 }
1439 Arrays.fill(mRebuildTmp, null);
1440 }
1441
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001442 /** Rebuilds the display's window list and does a relayout if something changed. */
1443 void rebuildAppWindowsAndLayoutIfNeeded() {
1444 mTmpWindows.clear();
1445 mTmpWindows.addAll(mWindows);
1446
1447 rebuildAppWindowList();
1448
1449 // Set displayContent.mLayoutNeeded if window order changed.
1450 final int tmpSize = mTmpWindows.size();
1451 final int winSize = mWindows.size();
1452 int tmpNdx = 0, winNdx = 0;
1453 while (tmpNdx < tmpSize && winNdx < winSize) {
1454 // Skip over all exiting windows, they've been moved out of order.
1455 WindowState tmp;
1456 do {
1457 tmp = mTmpWindows.get(tmpNdx++);
1458 } while (tmpNdx < tmpSize && tmp.mAppToken != null && tmp.mAppToken.mIsExiting);
1459
1460 WindowState win;
1461 do {
1462 win = mWindows.get(winNdx++);
1463 } while (winNdx < winSize && win.mAppToken != null && win.mAppToken.mIsExiting);
1464
1465 if (tmp != win) {
1466 // Window order changed.
1467 setLayoutNeeded();
1468 break;
1469 }
1470 }
1471 if (tmpNdx != winNdx) {
1472 // One list was different from the other.
1473 setLayoutNeeded();
1474 }
1475 mTmpWindows.clear();
1476
1477 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
1478 false /*updateInputWindows*/)) {
1479 assignWindowLayers(false /* setLayoutNeeded */);
1480 }
1481
1482 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
1483 mService.mWindowPlacerLocked.performSurfacePlacement();
1484 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
1485 }
1486
1487 void updateInputWindows(InputMonitor inputMonitor, WindowState inputFocus, boolean inDrag) {
1488 final InputConsumerImpl navInputConsumer =
1489 mService.mInputMonitor.getInputConsumer(INPUT_CONSUMER_NAVIGATION, mDisplayId);
1490 final InputConsumerImpl pipInputConsumer =
1491 mService.mInputMonitor.getInputConsumer(INPUT_CONSUMER_PIP, mDisplayId);
1492 final InputConsumerImpl wallpaperInputConsumer =
1493 mService.mInputMonitor.getInputConsumer(INPUT_CONSUMER_WALLPAPER, mDisplayId);
1494 boolean addInputConsumerHandle = navInputConsumer != null;
1495 boolean addPipInputConsumerHandle = pipInputConsumer != null;
1496 boolean addWallpaperInputConsumerHandle = wallpaperInputConsumer != null;
1497 final Rect pipTouchableBounds = addPipInputConsumerHandle ? new Rect() : null;
1498 boolean disableWallpaperTouchEvents = false;
1499
1500 for (int winNdx = mWindows.size() - 1; winNdx >= 0; --winNdx) {
1501 final WindowState child = mWindows.get(winNdx);
1502 final InputChannel inputChannel = child.mInputChannel;
1503 final InputWindowHandle inputWindowHandle = child.mInputWindowHandle;
1504 if (inputChannel == null || inputWindowHandle == null || child.mRemoved
1505 || child.isAdjustedForMinimizedDock()) {
1506 // Skip this window because it cannot possibly receive input.
1507 continue;
1508 }
1509
1510 if (addPipInputConsumerHandle
1511 && child.getStackId() == PINNED_STACK_ID
1512 && inputWindowHandle.layer <= pipInputConsumer.mWindowHandle.layer) {
1513 // Update the bounds of the Pip input consumer to match the Pinned stack
1514 child.getStack().getBounds(pipTouchableBounds);
1515 pipInputConsumer.mWindowHandle.touchableRegion.set(pipTouchableBounds);
1516 inputMonitor.addInputWindowHandle(pipInputConsumer.mWindowHandle);
1517 addPipInputConsumerHandle = false;
1518 }
1519
1520 if (addInputConsumerHandle
1521 && inputWindowHandle.layer <= navInputConsumer.mWindowHandle.layer) {
1522 inputMonitor.addInputWindowHandle(navInputConsumer.mWindowHandle);
1523 addInputConsumerHandle = false;
1524 }
1525
1526 if (addWallpaperInputConsumerHandle) {
1527 if (child.mAttrs.type == TYPE_WALLPAPER && child.isVisibleLw()) {
1528 // Add the wallpaper input consumer above the first visible wallpaper.
1529 inputMonitor.addInputWindowHandle(wallpaperInputConsumer.mWindowHandle);
1530 addWallpaperInputConsumerHandle = false;
1531 }
1532 }
1533
1534 final int flags = child.mAttrs.flags;
1535 final int privateFlags = child.mAttrs.privateFlags;
1536 final int type = child.mAttrs.type;
1537
1538 final boolean hasFocus = child == inputFocus;
1539 final boolean isVisible = child.isVisibleLw();
1540 if ((privateFlags & PRIVATE_FLAG_DISABLE_WALLPAPER_TOUCH_EVENTS) != 0) {
1541 disableWallpaperTouchEvents = true;
1542 }
1543 final boolean hasWallpaper = mWallpaperController.isWallpaperTarget(child)
1544 && (privateFlags & PRIVATE_FLAG_KEYGUARD) == 0
1545 && !disableWallpaperTouchEvents;
1546
1547 // If there's a drag in progress and 'child' is a potential drop target,
1548 // make sure it's been told about the drag
1549 if (inDrag && isVisible && isDefaultDisplay) {
1550 mService.mDragState.sendDragStartedIfNeededLw(child);
1551 }
1552
1553 inputMonitor.addInputWindowHandle(
1554 inputWindowHandle, child, flags, type, isVisible, hasFocus, hasWallpaper);
1555 }
1556
1557 if (addWallpaperInputConsumerHandle) {
1558 // No visible wallpaper found, add the wallpaper input consumer at the end.
1559 inputMonitor.addInputWindowHandle(wallpaperInputConsumer.mWindowHandle);
1560 }
1561 }
1562
1563 /** Returns true if a leaked surface was destroyed */
1564 boolean destroyLeakedSurfaces() {
1565 boolean leakedSurface = false;
1566 final int numWindows = mWindows.size();
1567 for (int winNdx = 0; winNdx < numWindows; ++winNdx) {
1568 final WindowState ws = mWindows.get(winNdx);
1569 final WindowStateAnimator wsa = ws.mWinAnimator;
1570 if (wsa.mSurfaceController == null) {
1571 continue;
1572 }
1573 if (!mService.mSessions.contains(wsa.mSession)) {
1574 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
1575 + ws + " surface=" + wsa.mSurfaceController
1576 + " token=" + ws.mToken
1577 + " pid=" + ws.mSession.mPid
1578 + " uid=" + ws.mSession.mUid);
1579 wsa.destroySurface();
1580 mService.mForceRemoves.add(ws);
1581 leakedSurface = true;
1582 } else if (ws.mAppToken != null && ws.mAppToken.clientHidden) {
1583 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
1584 + ws + " surface=" + wsa.mSurfaceController
1585 + " token=" + ws.mAppToken
1586 + " saved=" + ws.hasSavedSurface());
1587 if (SHOW_TRANSACTIONS) logSurface(ws, "LEAK DESTROY", false);
1588 wsa.destroySurface();
1589 leakedSurface = true;
1590 }
1591 }
1592
1593 return leakedSurface;
1594 }
1595
Wale Ogunwaleec731152016-09-08 20:18:57 -07001596 /** Return the list of Windows on this display associated with the input token. */
1597 WindowList getTokenWindowsOnDisplay(WindowToken token) {
1598 final WindowList windowList = new WindowList();
1599 final int count = mWindows.size();
1600 for (int i = 0; i < count; i++) {
1601 final WindowState win = mWindows.get(i);
1602 if (win.mToken == token) {
1603 windowList.add(win);
1604 }
1605 }
1606 return windowList;
1607 }
1608
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001609 private void reAddToWindowList(WindowState win) {
1610 win.mToken.addWindow(win);
1611 // This is a hack to get all of the child windows added as well at the right position. Child
1612 // windows should be rare and this case should be rare, so it shouldn't be that big a deal.
1613 int wpos = mWindows.indexOf(win);
1614 if (wpos >= 0) {
1615 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG_WM, "ReAdd removing from " + wpos + ": " + win);
1616 mWindows.remove(wpos);
1617 mService.mWindowsChanged = true;
1618 win.reAddWindow(wpos);
1619 }
1620 }
1621
1622 void moveInputMethodDialogs(int pos) {
1623 ArrayList<WindowState> dialogs = mService.mInputMethodDialogs;
1624
1625 final int N = dialogs.size();
1626 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Removing " + N + " dialogs w/pos=" + pos);
1627 for (int i = 0; i < N; i++) {
1628 pos = removeWindowAndChildrenFromWindowList(dialogs.get(i), pos);
1629 }
1630 if (DEBUG_INPUT_METHOD) {
1631 Slog.v(TAG_WM, "Window list w/pos=" + pos);
1632 logWindowList(mWindows, " ");
1633 }
1634
1635 WindowState ime = mService.mInputMethodWindow;
1636 if (pos >= 0) {
1637 // Skip windows owned by the input method.
1638 if (ime != null) {
1639 while (pos < mWindows.size()) {
1640 WindowState wp = mWindows.get(pos);
1641 if (wp == ime || wp.getParentWindow() == ime) {
1642 pos++;
1643 continue;
1644 }
1645 break;
1646 }
1647 }
1648 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Adding " + N + " dialogs at pos=" + pos);
1649 for (int i=0; i<N; i++) {
1650 WindowState win = dialogs.get(i);
1651 pos = win.reAddWindow(pos);
1652 }
1653 if (DEBUG_INPUT_METHOD) {
1654 Slog.v(TAG_WM, "Final window list:");
1655 logWindowList(mWindows, " ");
1656 }
1657 return;
1658 }
1659 for (int i=0; i<N; i++) {
1660 WindowState win = dialogs.get(i);
1661 reAddToWindowList(win);
1662 if (DEBUG_INPUT_METHOD) {
1663 Slog.v(TAG_WM, "No IM target, final list:");
1664 logWindowList(mWindows, " ");
1665 }
1666 }
1667 }
1668
1669 boolean moveInputMethodWindowsIfNeeded(boolean needAssignLayers) {
1670 final WindowState imWin = mService.mInputMethodWindow;
1671 final int DN = mService.mInputMethodDialogs.size();
1672 if (imWin == null && DN == 0) {
1673 return false;
1674 }
1675
1676 // TODO(multidisplay): IMEs are only supported on the default display.
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001677 int imPos = findDesiredInputMethodWindowIndex(true);
1678 if (imPos >= 0) {
1679 // In this case, the input method windows are to be placed
1680 // immediately above the window they are targeting.
1681
1682 // First check to see if the input method windows are already
1683 // located here, and contiguous.
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001684 final int N = mWindows.size();
1685 final WindowState firstImWin = imPos < N ? mWindows.get(imPos) : null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001686
1687 // Figure out the actual input method window that should be
1688 // at the bottom of their stack.
1689 WindowState baseImWin = imWin != null ? imWin : mService.mInputMethodDialogs.get(0);
1690 final WindowState cw = baseImWin.getBottomChild();
1691 if (cw != null && cw.mSubLayer < 0) {
1692 baseImWin = cw;
1693 }
1694
1695 if (firstImWin == baseImWin) {
1696 // The windows haven't moved... but are they still contiguous?
1697 // First find the top IM window.
1698 int pos = imPos+1;
1699 while (pos < N) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001700 if (!(mWindows.get(pos)).mIsImWindow) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001701 break;
1702 }
1703 pos++;
1704 }
1705 pos++;
1706 // Now there should be no more input method windows above.
1707 while (pos < N) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001708 if ((mWindows.get(pos)).mIsImWindow) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001709 break;
1710 }
1711 pos++;
1712 }
1713 if (pos >= N) {
1714 return false;
1715 }
1716 }
1717
1718 if (imWin != null) {
1719 if (DEBUG_INPUT_METHOD) {
1720 Slog.v(TAG_WM, "Moving IM from " + imPos);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001721 logWindowList(mWindows, " ");
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001722 }
1723 imPos = removeWindowAndChildrenFromWindowList(imWin, imPos);
1724 if (DEBUG_INPUT_METHOD) {
1725 Slog.v(TAG_WM, "List after removing with new pos " + imPos + ":");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001726 logWindowList(mWindows, " ");
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001727 }
1728 imWin.reAddWindow(imPos);
1729 if (DEBUG_INPUT_METHOD) {
1730 Slog.v(TAG_WM, "List after moving IM to " + imPos + ":");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001731 logWindowList(mWindows, " ");
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001732 }
1733 if (DN > 0) moveInputMethodDialogs(imPos+1);
1734 } else {
1735 moveInputMethodDialogs(imPos);
1736 }
1737
1738 } else {
1739 // In this case, the input method windows go in a fixed layer,
1740 // because they aren't currently associated with a focus window.
1741
1742 if (imWin != null) {
1743 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Moving IM from " + imPos);
1744 removeWindowAndChildrenFromWindowList(imWin, 0);
1745 reAddToWindowList(imWin);
1746 if (DEBUG_INPUT_METHOD) {
1747 Slog.v(TAG_WM, "List with no IM target:");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001748 logWindowList(mWindows, " ");
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001749 }
1750 if (DN > 0) moveInputMethodDialogs(-1);
1751 } else {
1752 moveInputMethodDialogs(-1);
1753 }
1754
1755 }
1756
1757 if (needAssignLayers) {
1758 assignWindowLayers(false /* setLayoutNeeded */);
1759 }
1760
1761 return true;
1762 }
1763
1764 /**
1765 * Dig through the WindowStates and find the one that the Input Method will target.
1766 * @param willMove
1767 * @return The index+1 in mWindows of the discovered target.
1768 */
1769 int findDesiredInputMethodWindowIndex(boolean willMove) {
1770 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
1771 // same display. Or even when the current IME/target are not on the same screen as the next
1772 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001773 WindowState w = null;
1774 int i;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001775 for (i = mWindows.size() - 1; i >= 0; --i) {
1776 final WindowState win = mWindows.get(i);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001777
1778 if (DEBUG_INPUT_METHOD && willMove) Slog.i(TAG_WM, "Checking window @" + i
1779 + " " + win + " fl=0x" + Integer.toHexString(win.mAttrs.flags));
1780 if (canBeImeTarget(win)) {
1781 w = win;
1782 //Slog.i(TAG_WM, "Putting input method here!");
1783
1784 // Yet more tricksyness! If this window is a "starting" window, we do actually want
1785 // to be on top of it, but it is not -really- where input will go. So if the caller
1786 // is not actually looking to move the IME, look down below for a real window to
1787 // target...
1788 if (!willMove && w.mAttrs.type == TYPE_APPLICATION_STARTING && i > 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001789 final WindowState wb = mWindows.get(i-1);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001790 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
1791 i--;
1792 w = wb;
1793 }
1794 }
1795 break;
1796 }
1797 }
1798
1799 // Now w is either mWindows[0] or an IME (or null if mWindows is empty).
1800
1801 if (DEBUG_INPUT_METHOD && willMove) Slog.v(TAG_WM, "Proposed new IME target: " + w);
1802
1803 // Now, a special case -- if the last target's window is in the process of exiting, and is
1804 // above the new target, keep on the last target to avoid flicker. Consider for example a
1805 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
1806 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
1807 // scrim.
1808 final WindowState curTarget = mService.mInputMethodTarget;
1809 if (curTarget != null
1810 && curTarget.isDisplayedLw()
1811 && curTarget.isClosing()
1812 && (w == null || curTarget.mWinAnimator.mAnimLayer > w.mWinAnimator.mAnimLayer)) {
1813 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001814 return mWindows.indexOf(curTarget) + 1;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001815 }
1816
1817 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target="
1818 + w + " willMove=" + willMove);
1819
1820 if (willMove && w != null) {
1821 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
1822 if (token != null) {
1823
1824 // Now some fun for dealing with window animations that modify the Z order. We need
1825 // to look at all windows below the current target that are in this app, finding the
1826 // highest visible one in layering.
1827 WindowState highestTarget = null;
1828 int highestPos = 0;
1829 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001830 WindowList curWindows = token.getDisplayContent().mWindows;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001831 int pos = curWindows.indexOf(curTarget);
1832 while (pos >= 0) {
1833 WindowState win = curWindows.get(pos);
1834 if (win.mAppToken != token) {
1835 break;
1836 }
1837 if (!win.mRemoved) {
1838 if (highestTarget == null || win.mWinAnimator.mAnimLayer >
1839 highestTarget.mWinAnimator.mAnimLayer) {
1840 highestTarget = win;
1841 highestPos = pos;
1842 }
1843 }
1844 pos--;
1845 }
1846 }
1847
1848 if (highestTarget != null) {
1849 final AppTransition appTransition = mService.mAppTransition;
1850 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
1851 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
1852 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
1853 + " new layer=" + w.mWinAnimator.mAnimLayer);
1854
1855 if (appTransition.isTransitionSet()) {
1856 // If we are currently setting up for an animation, hold everything until we
1857 // can find out what will happen.
1858 mService.mInputMethodTargetWaitingAnim = true;
1859 mService.mInputMethodTarget = highestTarget;
1860 return highestPos + 1;
1861 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
1862 highestTarget.mWinAnimator.mAnimLayer > w.mWinAnimator.mAnimLayer) {
1863 // If the window we are currently targeting is involved with an animation,
1864 // and it is on top of the next target we will be over, then hold off on
1865 // moving until that is done.
1866 mService.mInputMethodTargetWaitingAnim = true;
1867 mService.mInputMethodTarget = highestTarget;
1868 return highestPos + 1;
1869 }
1870 }
1871 }
1872 }
1873
1874 //Slog.i(TAG_WM, "Placing input method @" + (i+1));
1875 if (w != null) {
1876 if (willMove) {
1877 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
1878 + w + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
1879 mService.mInputMethodTarget = w;
1880 mService.mInputMethodTargetWaitingAnim = false;
1881 if (w.mAppToken != null) {
1882 setInputMethodAnimLayerAdjustment(
1883 w.mAppToken.mAppAnimator.animLayerAdjustment);
1884 } else {
1885 setInputMethodAnimLayerAdjustment(0);
1886 }
1887 }
1888
1889 // If the docked divider is visible, we still need to go through this whole excercise to
1890 // find the appropriate input method target (used for animations and dialog
1891 // adjustments), but for purposes of Z ordering we simply wish to place it above the
1892 // docked divider. Unless it is already above the divider.
1893 final WindowState dockedDivider = mDividerControllerLocked.getWindow();
1894 if (dockedDivider != null && dockedDivider.isVisibleLw()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001895 int dividerIndex = mWindows.indexOf(dockedDivider);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001896 if (dividerIndex > 0 && dividerIndex > i) {
1897 return dividerIndex + 1;
1898 }
1899 }
1900 return i+1;
1901 }
1902 if (willMove) {
1903 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
1904 + " to null." + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
1905 mService.mInputMethodTarget = null;
1906 setInputMethodAnimLayerAdjustment(0);
1907 }
1908 return -1;
1909 }
1910
1911 private static boolean canBeImeTarget(WindowState w) {
1912 final int fl = w.mAttrs.flags & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
1913 final int type = w.mAttrs.type;
1914
1915 if (fl != 0 && fl != (FLAG_NOT_FOCUSABLE | FLAG_ALT_FOCUSABLE_IM)
1916 && type != TYPE_APPLICATION_STARTING) {
1917 return false;
1918 }
1919
1920 if (DEBUG_INPUT_METHOD) {
1921 Slog.i(TAG_WM, "isVisibleOrAdding " + w + ": " + w.isVisibleOrAdding());
1922 if (!w.isVisibleOrAdding()) {
1923 Slog.i(TAG_WM, " mSurfaceController=" + w.mWinAnimator.mSurfaceController
1924 + " relayoutCalled=" + w.mRelayoutCalled
1925 + " viewVis=" + w.mViewVisibility
1926 + " policyVis=" + w.mPolicyVisibility
1927 + " policyVisAfterAnim=" + w.mPolicyVisibilityAfterAnim
1928 + " parentHidden=" + w.isParentWindowHidden()
1929 + " exiting=" + w.mAnimatingExit + " destroying=" + w.mDestroying);
1930 if (w.mAppToken != null) {
1931 Slog.i(TAG_WM, " mAppToken.hiddenRequested=" + w.mAppToken.hiddenRequested);
1932 }
1933 }
1934 }
1935 return w.isVisibleOrAdding();
1936 }
1937
1938 private void logWindowList(final WindowList windows, String prefix) {
1939 int N = windows.size();
1940 while (N > 0) {
1941 N--;
1942 Slog.v(TAG_WM, prefix + "#" + N + ": " + windows.get(N));
1943 }
1944 }
1945
1946 boolean getNeedsMenu(WindowState win, WindowManagerPolicy.WindowState bottom) {
1947 int index = -1;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001948 while (true) {
1949 if (win.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
1950 return win.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
1951 }
1952 // If we reached the bottom of the range of windows we are considering,
1953 // assume no menu is needed.
1954 if (win == bottom) {
1955 return false;
1956 }
1957 // The current window hasn't specified whether menu key is needed; look behind it.
1958 // First, we may need to determine the starting position.
1959 if (index < 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001960 index = mWindows.indexOf(win);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001961 }
1962 index--;
1963 if (index < 0) {
1964 return false;
1965 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001966 win = mWindows.get(index);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001967 }
1968 }
1969
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001970 void setLayoutNeeded() {
1971 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
1972 mLayoutNeeded = true;
1973 }
1974
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001975 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001976 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
1977 mLayoutNeeded = false;
1978 }
1979
1980 boolean isLayoutNeeded() {
1981 return mLayoutNeeded;
1982 }
1983
Wale Ogunwale07bcab72016-10-14 15:30:09 -07001984 private void addAppWindowExisting(WindowState win, WindowList tokenWindowList) {
Wale Ogunwaleec731152016-09-08 20:18:57 -07001985
Wale Ogunwaleec731152016-09-08 20:18:57 -07001986 // If this application has existing windows, we simply place the new window on top of
1987 // them... but keep the starting window on top.
1988 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
1989 // Base windows go behind everything else.
1990 final WindowState lowestWindow = tokenWindowList.get(0);
1991 addWindowToListBefore(win, lowestWindow);
Wale Ogunwaleec731152016-09-08 20:18:57 -07001992 } else {
1993 final AppWindowToken atoken = win.mAppToken;
1994 final int windowListPos = tokenWindowList.size();
1995 final WindowState lastWindow = tokenWindowList.get(windowListPos - 1);
1996 if (atoken != null && lastWindow == atoken.startingWindow) {
1997 addWindowToListBefore(win, lastWindow);
Wale Ogunwaleec731152016-09-08 20:18:57 -07001998 } else {
1999 int newIdx = findIdxBasedOnAppTokens(win);
2000 // There is a window above this one associated with the same apptoken note that the
2001 // window could be a floating window that was created later or a window at the top
2002 // of the list of windows associated with this token.
2003 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
2004 "not Base app: Adding window " + win + " at " + (newIdx + 1) + " of "
2005 + mWindows.size());
2006 mWindows.add(newIdx + 1, win);
Wale Ogunwaleec731152016-09-08 20:18:57 -07002007 mService.mWindowsChanged = true;
2008 }
2009 }
Wale Ogunwaleec731152016-09-08 20:18:57 -07002010 }
2011
2012 /** Places the first input window after the second input window in the window list. */
2013 private void addWindowToListAfter(WindowState first, WindowState second) {
2014 final int i = mWindows.indexOf(second);
2015 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
2016 "Adding window " + this + " at " + (i + 1) + " of " + mWindows.size()
2017 + " (after " + second + ")");
2018 mWindows.add(i + 1, first);
2019 mService.mWindowsChanged = true;
2020 }
2021
2022 /** Places the first input window before the second input window in the window list. */
2023 private void addWindowToListBefore(WindowState first, WindowState second) {
2024 int i = mWindows.indexOf(second);
2025 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
2026 "Adding window " + this + " at " + i + " of " + mWindows.size()
2027 + " (before " + second + ")");
2028 if (i < 0) {
2029 Slog.w(TAG_WM, "addWindowToListBefore: Unable to find " + second + " in " + mWindows);
2030 i = 0;
2031 }
2032 mWindows.add(i, first);
2033 mService.mWindowsChanged = true;
2034 }
2035
2036 /**
2037 * This method finds out the index of a window that has the same app token as win. used for z
2038 * ordering the windows in mWindows
2039 */
2040 private int findIdxBasedOnAppTokens(WindowState win) {
2041 for(int j = mWindows.size() - 1; j >= 0; j--) {
2042 final WindowState wentry = mWindows.get(j);
2043 if(wentry.mAppToken == win.mAppToken) {
2044 return j;
2045 }
2046 }
2047 return -1;
2048 }
2049
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002050 private void dumpChildrenNames() {
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07002051 StringBuilder output = new StringBuilder();
2052 dumpChildrenNames(output, " ");
2053 Slog.v(TAG_WM, output.toString());
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002054 }
2055
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002056 private void dumpWindows() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002057 Slog.v(TAG_WM, " Display #" + mDisplayId);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002058 for (int winNdx = mWindows.size() - 1; winNdx >= 0; --winNdx) {
2059 Slog.v(TAG_WM, " #" + winNdx + ": " + mWindows.get(winNdx));
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002060 }
2061 }
2062
Wale Ogunwale02319a62016-09-26 15:21:22 -07002063 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2064 if (mTokenMap.isEmpty()) {
2065 return;
2066 }
2067 pw.println(" Display #" + mDisplayId);
2068 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2069 while (it.hasNext()) {
2070 final WindowToken token = it.next();
2071 pw.print(" ");
2072 pw.print(token);
2073 if (dumpAll) {
2074 pw.println(':');
2075 token.dump(pw, " ");
2076 } else {
2077 pw.println();
2078 }
2079 }
2080 }
2081
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002082 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
2083 final int count = mWindows.size();
2084 for (int j = 0; j < count; j++) {
2085 final WindowStateAnimator wAnim = mWindows.get(j).mWinAnimator;
2086 pw.println(subPrefix + "Window #" + j + ": " + wAnim);
2087 }
2088 }
2089
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002090 void enableSurfaceTrace(FileDescriptor fd) {
2091 for (int i = mWindows.size() - 1; i >= 0; i--) {
2092 final WindowState win = mWindows.get(i);
2093 win.mWinAnimator.enableSurfaceTrace(fd);
2094 }
2095 }
2096
2097 void disableSurfaceTrace() {
2098 for (int i = mWindows.size() - 1; i >= 0; i--) {
2099 final WindowState win = mWindows.get(i);
2100 win.mWinAnimator.disableSurfaceTrace();
2101 }
2102 }
2103
Jorim Jaggife762342016-10-13 14:33:27 +02002104 /**
2105 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2106 */
2107 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2108 final WindowManagerPolicy policy = mService.mPolicy;
2109 for (int i = mWindows.size() - 1; i >= 0; i--) {
2110 final WindowState window = mWindows.get(i);
2111 if (window.mAppToken == null && policy.canBeHiddenByKeyguardLw(window)) {
2112 window.mWinAnimator.setAnimation(
2113 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2114 }
2115 }
2116 }
2117
Wale Ogunwale494009b82016-10-21 09:01:38 -07002118 boolean checkWaitingForWindows() {
2119
2120 boolean haveBootMsg = false;
2121 boolean haveApp = false;
2122 // if the wallpaper service is disabled on the device, we're never going to have
2123 // wallpaper, don't bother waiting for it
2124 boolean haveWallpaper = false;
2125 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2126 com.android.internal.R.bool.config_enableWallpaperService)
2127 && !mService.mOnlyCore;
2128 boolean haveKeyguard = true;
2129 final int count = mWindows.size();
2130 for (int i = 0; i < count; i++) {
2131 final WindowState w = mWindows.get(i);
2132 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2133 return true;
2134 }
2135 if (w.isDrawnLw()) {
2136 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
2137 haveBootMsg = true;
2138 } else if (w.mAttrs.type == TYPE_APPLICATION
2139 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
2140 haveApp = true;
2141 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
2142 haveWallpaper = true;
2143 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
2144 haveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
2145 }
2146 }
2147 }
2148
2149 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2150 "******** booted=" + mService.mSystemBooted
2151 + " msg=" + mService.mShowingBootMessages
2152 + " haveBoot=" + haveBootMsg + " haveApp=" + haveApp
2153 + " haveWall=" + haveWallpaper + " wallEnabled=" + wallpaperEnabled
2154 + " haveKeyguard=" + haveKeyguard);
2155
2156 // If we are turning on the screen to show the boot message, don't do it until the boot
2157 // message is actually displayed.
2158 if (!mService.mSystemBooted && !haveBootMsg) {
2159 return true;
2160 }
2161
2162 // If we are turning on the screen after the boot is completed normally, don't do so until
2163 // we have the application and wallpaper.
2164 if (mService.mSystemBooted && ((!haveApp && !haveKeyguard) ||
2165 (wallpaperEnabled && !haveWallpaper))) {
2166 return true;
2167 }
2168
2169 return false;
2170 }
2171
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002172 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale0303c572016-10-20 10:16:29 -07002173 final WallpaperController wallpaperController = mWallpaperController;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002174 for (int i = mWindows.size() - 1; i >= 0; i--) {
2175 WindowState win = mWindows.get(i);
2176 WindowStateAnimator winAnimator = win.mWinAnimator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002177 if (winAnimator.hasSurface()) {
2178 final boolean wasAnimating = winAnimator.mWasAnimating;
2179 final boolean nowAnimating = winAnimator.stepAnimationLocked(animator.mCurrentTime);
2180 winAnimator.mWasAnimating = nowAnimating;
2181 animator.orAnimating(nowAnimating);
2182
2183 if (DEBUG_WALLPAPER) Slog.v(TAG,
2184 win + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
2185
2186 if (wasAnimating && !winAnimator.mAnimating
2187 && wallpaperController.isWallpaperTarget(win)) {
2188 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2189 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
2190 if (DEBUG_LAYOUT_REPEATS) {
2191 mService.mWindowPlacerLocked.debugLayoutRepeats(
2192 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
2193 }
2194 }
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002195 }
2196
2197 final AppWindowToken atoken = win.mAppToken;
2198 if (winAnimator.mDrawState == READY_TO_SHOW) {
2199 if (atoken == null || atoken.allDrawn) {
2200 if (win.performShowLocked()) {
2201 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
2202 if (DEBUG_LAYOUT_REPEATS) {
2203 mService.mWindowPlacerLocked.debugLayoutRepeats(
2204 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
2205 }
2206 }
2207 }
2208 }
2209 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
2210 if (appAnimator != null && appAnimator.thumbnail != null) {
2211 if (appAnimator.thumbnailTransactionSeq != animator.mAnimTransactionSequence) {
2212 appAnimator.thumbnailTransactionSeq = animator.mAnimTransactionSequence;
2213 appAnimator.thumbnailLayer = 0;
2214 }
2215 if (appAnimator.thumbnailLayer < winAnimator.mAnimLayer) {
2216 appAnimator.thumbnailLayer = winAnimator.mAnimLayer;
2217 }
2218 }
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002219 } // end forall windows
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002220 }
2221
2222 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002223 resetAnimationBackgroundAnimator();
2224
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002225 final WindowList windows = mWindows;
2226 WindowState detachedWallpaper = null;
2227
2228 for (int i = windows.size() - 1; i >= 0; i--) {
2229 final WindowState win = windows.get(i);
2230 final WindowStateAnimator winAnimator = win.mWinAnimator;
2231 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
2232 continue;
2233 }
2234
2235 final int flags = win.mAttrs.flags;
2236
2237 // If this window is animating, make a note that we have an animating window and take
2238 // care of a request to run a detached wallpaper animation.
2239 if (winAnimator.mAnimating) {
2240 if (winAnimator.mAnimation != null) {
2241 if ((flags & FLAG_SHOW_WALLPAPER) != 0
2242 && winAnimator.mAnimation.getDetachWallpaper()) {
2243 detachedWallpaper = win;
2244 }
2245 final int color = winAnimator.mAnimation.getBackgroundColor();
2246 if (color != 0) {
2247 final TaskStack stack = win.getStack();
2248 if (stack != null) {
2249 stack.setAnimationBackground(winAnimator, color);
2250 }
2251 }
2252 }
2253 animator.setAnimating(true);
2254 }
2255
2256 // If this window's app token is running a detached wallpaper animation, make a note so
2257 // we can ensure the wallpaper is displayed behind it.
2258 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
2259 if (appAnimator != null && appAnimator.animation != null
2260 && appAnimator.animating) {
2261 if ((flags & FLAG_SHOW_WALLPAPER) != 0
2262 && appAnimator.animation.getDetachWallpaper()) {
2263 detachedWallpaper = win;
2264 }
2265
2266 final int color = appAnimator.animation.getBackgroundColor();
2267 if (color != 0) {
2268 final TaskStack stack = win.getStack();
2269 if (stack != null) {
2270 stack.setAnimationBackground(winAnimator, color);
2271 }
2272 }
2273 }
2274 } // end forall windows
2275
2276 if (animator.mWindowDetachedWallpaper != detachedWallpaper) {
2277 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
2278 + animator.mWindowDetachedWallpaper + " to " + detachedWallpaper);
2279 animator.mWindowDetachedWallpaper = detachedWallpaper;
2280 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2281 }
2282 }
2283
2284 void prepareWindowSurfaces() {
2285 final int count = mWindows.size();
2286 for (int j = 0; j < count; j++) {
2287 mWindows.get(j).mWinAnimator.prepareSurfaceLocked(true);
2288 }
2289 }
2290
Wale Ogunwale494009b82016-10-21 09:01:38 -07002291 boolean inputMethodClientHasFocus(IInputMethodClient client) {
2292 // The focus for the client is the window immediately below where we would place the input
2293 // method window.
2294 int idx = findDesiredInputMethodWindowIndex(false);
2295 if (idx <= 0) {
2296 return false;
2297 }
2298
2299 WindowState imFocus = mWindows.get(idx - 1);
2300 if (DEBUG_INPUT_METHOD) {
2301 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2302 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2303 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
2304 }
2305
2306 if (imFocus == null) {
2307 return false;
2308 }
2309
2310 // This may be a starting window, in which case we still want to count it as okay.
2311 if (imFocus.mAttrs.type == TYPE_APPLICATION_STARTING && imFocus.mAppToken != null) {
2312 // The client has definitely started, so it really should have a window in this app
2313 // token. Let's look for it.
2314 final WindowState w = imFocus.mAppToken.getFirstNonStartingWindow();
2315 if (w != null) {
2316 if (DEBUG_INPUT_METHOD) Slog.i(TAG_WM, "Switching to real app window: " + w);
2317 imFocus = w;
2318 }
2319 }
2320
2321 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2322
2323 if (DEBUG_INPUT_METHOD) {
2324 Slog.i(TAG_WM, "IM target client: " + imeClient);
2325 if (imeClient != null) {
2326 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2327 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2328 }
2329 }
2330
2331 return imeClient != null && imeClient.asBinder() == client.asBinder();
2332 }
2333
2334 boolean hasSecureWindowOnScreen() {
2335 for (int i = mWindows.size() - 1; i >= 0; --i) {
2336 final WindowState ws = mWindows.get(i);
2337 if (ws.isOnScreen() && (ws.mAttrs.flags & FLAG_SECURE) != 0) {
2338 return true;
2339 }
2340 }
2341 return false;
2342 }
2343
2344 void updateSystemUiVisibility(int visibility, int globalDiff) {
2345 for (int i = mWindows.size() - 1; i >= 0; --i) {
2346 final WindowState ws = mWindows.get(i);
2347 try {
2348 int curValue = ws.mSystemUiVisibility;
2349 int diff = (curValue ^ visibility) & globalDiff;
2350 int newValue = (curValue & ~diff) | (visibility & diff);
2351 if (newValue != curValue) {
2352 ws.mSeq++;
2353 ws.mSystemUiVisibility = newValue;
2354 }
2355 if (newValue != curValue || ws.mAttrs.hasSystemUiListeners) {
2356 ws.mClient.dispatchSystemUiVisibilityChanged(ws.mSeq,
2357 visibility, newValue, diff);
2358 }
2359 } catch (RemoteException e) {
2360 // so sorry
2361 }
2362 }
2363 }
2364
2365 void onWindowFreezeTimeout() {
2366 Slog.w(TAG_WM, "Window freeze timeout expired.");
2367 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
2368 for (int i = mWindows.size() - 1; i >= 0; --i) {
2369 final WindowState w = mWindows.get(i);
2370 if (!w.mOrientationChanging) {
2371 continue;
2372 }
2373 w.mOrientationChanging = false;
2374 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2375 - mService.mDisplayFreezeTime);
2376 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
2377 }
2378 mService.mWindowPlacerLocked.performSurfacePlacement();
2379 }
2380
2381 void waitForAllWindowsDrawn() {
2382 final WindowManagerPolicy policy = mService.mPolicy;
2383 for (int winNdx = mWindows.size() - 1; winNdx >= 0; --winNdx) {
2384 final WindowState win = mWindows.get(winNdx);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002385 final boolean keyguard = policy.isKeyguardHostWindow(win.mAttrs);
Jorim Jaggife762342016-10-13 14:33:27 +02002386 if (win.isVisibleLw() && (win.mAppToken != null || keyguard)) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002387 win.mWinAnimator.mDrawState = DRAW_PENDING;
2388 // Force add to mResizingWindows.
2389 win.mLastContentInsets.set(-1, -1, -1, -1);
2390 mService.mWaitingForDrawn.add(win);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002391 }
2392 }
2393 }
2394
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002395 ReadOnlyWindowList getReadOnlyWindowList() {
2396 return mWindows.getReadOnly();
2397 }
2398
2399 void getWindows(WindowList output) {
2400 output.addAll(mWindows);
2401 }
2402
2403 // TODO: Super crazy long method that should be broken down...
2404 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2405
2406 boolean focusDisplayed = false;
2407 boolean displayHasContent = false;
2408 float preferredRefreshRate = 0;
2409 int preferredModeId = 0;
2410
2411
2412 final int dw = mDisplayInfo.logicalWidth;
2413 final int dh = mDisplayInfo.logicalHeight;
2414 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2415
2416 mTmpUpdateAllDrawn.clear();
2417
2418 int repeats = 0;
2419 do {
2420 repeats++;
2421 if (repeats > 6) {
2422 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2423 clearLayoutNeeded();
2424 break;
2425 }
2426
2427 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2428 pendingLayoutChanges);
2429
2430 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
2431 adjustWallpaperWindows();
2432 }
2433
2434 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2435 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2436 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2437 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002438 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002439 }
2440 }
2441
2442 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2443 setLayoutNeeded();
2444 }
2445
2446 // FIRST LOOP: Perform a layout, if needed.
2447 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2448 performLayout(repeats == 1, false /* updateInputWindows */);
2449 } else {
2450 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2451 }
2452
2453 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2454 pendingLayoutChanges = 0;
2455
2456 if (isDefaultDisplay) {
2457 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
2458 for (int i = mWindows.size() - 1; i >= 0; i--) {
2459 final WindowState w = mWindows.get(i);
Jorim Jaggife762342016-10-13 14:33:27 +02002460 mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
2461 mService.mInputMethodTarget);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002462 }
2463 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2464 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2465 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2466 }
2467 } while (pendingLayoutChanges != 0);
2468
2469 RootWindowContainer root = mService.mRoot;
2470 boolean obscured = false;
2471 boolean syswin = false;
2472 resetDimming();
2473
2474 // Only used if default window
2475 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
2476
2477 for (int i = mWindows.size() - 1; i >= 0; i--) {
2478 final WindowState w = mWindows.get(i);
2479 final Task task = w.getTask();
2480 final boolean obscuredChanged = w.mObscured != obscured;
2481
2482 // Update effect.
2483 w.mObscured = obscured;
2484 if (!obscured) {
2485 final boolean isDisplayed = w.isDisplayedLw();
2486
2487 if (isDisplayed && w.isObscuringFullscreen(mDisplayInfo)) {
2488 // This window completely covers everything behind it, so we want to leave all
2489 // of them as undimmed (for performance reasons).
2490 root.mObscuringWindow = w;
2491 obscured = true;
2492 }
2493
2494 displayHasContent |= root.handleNotObscuredLocked(w, obscured, syswin);
2495
2496 if (w.mHasSurface && isDisplayed) {
2497 final int type = w.mAttrs.type;
2498 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
2499 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
2500 syswin = true;
2501 }
2502 if (preferredRefreshRate == 0 && w.mAttrs.preferredRefreshRate != 0) {
2503 preferredRefreshRate = w.mAttrs.preferredRefreshRate;
2504 }
2505 if (preferredModeId == 0 && w.mAttrs.preferredDisplayModeId != 0) {
2506 preferredModeId = w.mAttrs.preferredDisplayModeId;
2507 }
2508 }
2509 }
2510
2511 w.applyDimLayerIfNeeded();
2512
2513 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
2514 && mWallpaperController.isWallpaperTarget(w)) {
2515 // This is the wallpaper target and its obscured state changed... make sure the
2516 // current wallpaper's visibility has been updated accordingly.
2517 mWallpaperController.updateWallpaperVisibility();
2518 }
2519
2520 w.handleWindowMovedIfNeeded();
2521
2522 final WindowStateAnimator winAnimator = w.mWinAnimator;
2523
2524 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
2525 w.mContentChanged = false;
2526
2527 // Moved from updateWindowsAndWallpaperLocked().
2528 if (w.mHasSurface) {
2529 // Take care of the window being ready to display.
2530 final boolean committed = winAnimator.commitFinishDrawingLocked();
2531 if (isDefaultDisplay && committed) {
2532 if (w.mAttrs.type == TYPE_DREAM) {
2533 // HACK: When a dream is shown, it may at that point hide the lock screen.
2534 // So we need to redo the layout to let the phone window manager make this
2535 // happen.
2536 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
2537 if (DEBUG_LAYOUT_REPEATS) {
2538 surfacePlacer.debugLayoutRepeats(
2539 "dream and commitFinishDrawingLocked true",
2540 pendingLayoutChanges);
2541 }
2542 }
2543 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
2544 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
2545 "First draw done in potential wallpaper target " + w);
2546 root.mWallpaperMayChange = true;
2547 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
2548 if (DEBUG_LAYOUT_REPEATS) {
2549 surfacePlacer.debugLayoutRepeats(
2550 "wallpaper and commitFinishDrawingLocked true",
2551 pendingLayoutChanges);
2552 }
2553 }
2554 }
2555 if (!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening()) {
2556 // Updates the shown frame before we set up the surface. This is needed
2557 // because the resizing could change the top-left position (in addition to
2558 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
2559 // position the surface.
2560 //
2561 // If an animation is being started, we can't call this method because the
2562 // animation hasn't processed its initial transformation yet, but in general
2563 // we do want to update the position if the window is animating.
2564 winAnimator.computeShownFrameLocked();
2565 }
2566 winAnimator.setSurfaceBoundariesLocked(recoveringMemory);
2567 }
2568
2569 final AppWindowToken atoken = w.mAppToken;
2570 if (atoken != null) {
2571 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
2572 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
2573 mTmpUpdateAllDrawn.add(atoken);
2574 }
2575 }
2576
2577 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
2578 && w.isDisplayedLw()) {
2579 focusDisplayed = true;
2580 }
2581
2582 w.updateResizingWindowIfNeeded();
2583 }
2584
2585 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
2586 displayHasContent,
2587 preferredRefreshRate,
2588 preferredModeId,
2589 true /* inTraversal, must call performTraversalInTrans... below */);
2590
2591 stopDimmingIfNeeded();
2592
2593 while (!mTmpUpdateAllDrawn.isEmpty()) {
2594 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2595 // See if any windows have been drawn, so they (and others associated with them)
2596 // can now be shown.
2597 atoken.updateAllDrawn(this);
2598 }
2599
2600 return focusDisplayed;
2601 }
2602
2603 void performLayout(boolean initial, boolean updateInputWindows) {
2604 if (!isLayoutNeeded()) {
2605 return;
2606 }
2607 clearLayoutNeeded();
2608
2609 final int dw = mDisplayInfo.logicalWidth;
2610 final int dh = mDisplayInfo.logicalHeight;
2611
2612 int i;
2613
2614 if (DEBUG_LAYOUT) {
2615 Slog.v(TAG, "-------------------------------------");
2616 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2617 }
2618
2619 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mService.mRotation,
2620 getConfiguration().uiMode);
2621 if (isDefaultDisplay) {
2622 // Not needed on non-default displays.
2623 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2624 mService.mScreenRect.set(0, 0, dw, dh);
2625 }
2626
2627 mService.mPolicy.getContentRectLw(mContentRect);
2628
2629 int seq = mService.mLayoutSeq + 1;
2630 if (seq < 0) seq = 0;
2631 mService.mLayoutSeq = seq;
2632
2633 boolean behindDream = false;
2634
2635 // First perform layout of any root windows (not attached to another window).
2636 int topAttached = -1;
2637 for (i = mWindows.size() - 1; i >= 0; i--) {
2638 final WindowState win = mWindows.get(i);
2639
2640 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
2641 // wasting time and funky changes while a window is animating away.
Jorim Jaggife762342016-10-13 14:33:27 +02002642 final boolean gone = (behindDream && mService.mPolicy.canBeHiddenByKeyguardLw(win))
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002643 || win.isGoneForLayoutLw();
2644
2645 if (DEBUG_LAYOUT && !win.mLayoutAttached) {
2646 Slog.v(TAG, "1ST PASS " + win + ": gone=" + gone + " mHaveFrame=" + win.mHaveFrame
2647 + " mLayoutAttached=" + win.mLayoutAttached
2648 + " screen changed=" + win.isConfigChanged());
2649 final AppWindowToken atoken = win.mAppToken;
2650 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + win.mViewVisibility
2651 + " mRelayoutCalled=" + win.mRelayoutCalled + " hidden=" + win.mToken.hidden
2652 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
2653 + " parentHidden=" + win.isParentWindowHidden());
2654 else Slog.v(TAG, " VIS: mViewVisibility=" + win.mViewVisibility
2655 + " mRelayoutCalled=" + win.mRelayoutCalled + " hidden=" + win.mToken.hidden
2656 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
2657 + " parentHidden=" + win.isParentWindowHidden());
2658 }
2659
2660 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
2661 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
2662 // since that means "perform layout as normal, just don't display").
2663 if (!gone || !win.mHaveFrame || win.mLayoutNeeded
2664 || ((win.isConfigChanged() || win.setReportResizeHints())
2665 && !win.isGoneForLayoutLw() &&
2666 ((win.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
2667 (win.mHasSurface && win.mAppToken != null &&
2668 win.mAppToken.layoutConfigChanges)))) {
2669 if (!win.mLayoutAttached) {
2670 if (initial) {
2671 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
2672 win.mContentChanged = false;
2673 }
2674 if (win.mAttrs.type == TYPE_DREAM) {
2675 // Don't layout windows behind a dream, so that if it does stuff like hide
2676 // the status bar we won't get a bad transition when it goes away.
2677 behindDream = true;
2678 }
2679 win.mLayoutNeeded = false;
2680 win.prelayout();
2681 mService.mPolicy.layoutWindowLw(win, null);
2682 win.mLayoutSeq = seq;
2683
2684 // Window frames may have changed. Update dim layer with the new bounds.
2685 final Task task = win.getTask();
2686 if (task != null) {
2687 mDimLayerController.updateDimLayer(task);
2688 }
2689
2690 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + win.mFrame
2691 + " mContainingFrame=" + win.mContainingFrame
2692 + " mDisplayFrame=" + win.mDisplayFrame);
2693 } else {
2694 if (topAttached < 0) topAttached = i;
2695 }
2696 }
2697 }
2698
2699 boolean attachedBehindDream = false;
2700
2701 // Now perform layout of attached windows, which usually depend on the position of the
2702 // window they are attached to. XXX does not deal with windows that are attached to windows
2703 // that are themselves attached.
2704 for (i = topAttached; i >= 0; i--) {
2705 final WindowState win = mWindows.get(i);
2706
2707 if (win.mLayoutAttached) {
2708 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + win + " mHaveFrame=" + win.mHaveFrame
2709 + " mViewVisibility=" + win.mViewVisibility
2710 + " mRelayoutCalled=" + win.mRelayoutCalled);
2711 // If this view is GONE, then skip it -- keep the current frame, and let the caller
2712 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
2713 // windows, since that means "perform layout as normal, just don't display").
Jorim Jaggife762342016-10-13 14:33:27 +02002714 if (attachedBehindDream && mService.mPolicy.canBeHiddenByKeyguardLw(win)) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002715 continue;
2716 }
2717 if ((win.mViewVisibility != GONE && win.mRelayoutCalled) || !win.mHaveFrame
2718 || win.mLayoutNeeded) {
2719 if (initial) {
2720 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
2721 win.mContentChanged = false;
2722 }
2723 win.mLayoutNeeded = false;
2724 win.prelayout();
2725 mService.mPolicy.layoutWindowLw(win, win.getParentWindow());
2726 win.mLayoutSeq = seq;
2727 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + win.mFrame
2728 + " mContainingFrame=" + win.mContainingFrame
2729 + " mDisplayFrame=" + win.mDisplayFrame);
2730 }
2731 } else if (win.mAttrs.type == TYPE_DREAM) {
2732 // Don't layout windows behind a dream, so that if it does stuff like hide the
2733 // status bar we won't get a bad transition when it goes away.
2734 attachedBehindDream = behindDream;
2735 }
2736 }
2737
2738 // Window frames may have changed. Tell the input dispatcher about it.
2739 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2740 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2741 if (updateInputWindows) {
2742 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2743 }
2744
2745 mService.mPolicy.finishLayoutLw();
2746 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2747 }
2748
2749 /**
2750 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2751 * In portrait mode, it grabs the full screenshot.
2752 *
2753 * @param width the width of the target bitmap
2754 * @param height the height of the target bitmap
2755 * @param includeFullDisplay true if the screen should not be cropped before capture
2756 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2757 * @param config of the output bitmap
2758 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
2759 */
2760 Bitmap screenshotApplications(IBinder appToken, int displayId, int width, int height,
2761 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
2762 boolean wallpaperOnly) {
2763 int dw = mDisplayInfo.logicalWidth;
2764 int dh = mDisplayInfo.logicalHeight;
2765 if (dw == 0 || dh == 0) {
2766 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2767 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2768 return null;
2769 }
2770
2771 Bitmap bm = null;
2772
2773 int maxLayer = 0;
2774 final Rect frame = new Rect();
2775 final Rect stackBounds = new Rect();
2776
2777 boolean screenshotReady;
2778 int minLayer;
2779 if (appToken == null && !wallpaperOnly) {
2780 screenshotReady = true;
2781 minLayer = 0;
2782 } else {
2783 screenshotReady = false;
2784 minLayer = Integer.MAX_VALUE;
2785 }
2786
2787 WindowState appWin = null;
2788
2789 boolean includeImeInScreenshot;
2790 synchronized(mService.mWindowMap) {
2791 final AppWindowToken imeTargetAppToken = mService.mInputMethodTarget != null
2792 ? mService.mInputMethodTarget.mAppToken : null;
2793 // We only include the Ime in the screenshot if the app we are screenshoting is the IME
2794 // target and isn't in multi-window mode. We don't screenshot the IME in multi-window
2795 // mode because the frame of the IME might not overlap with that of the app.
2796 // E.g. IME target app at the top in split-screen mode and the IME at the bottom
2797 // overlapping with the bottom app.
2798 includeImeInScreenshot = imeTargetAppToken != null
2799 && imeTargetAppToken.appToken != null
2800 && imeTargetAppToken.appToken.asBinder() == appToken
2801 && !mService.mInputMethodTarget.isInMultiWindowMode();
2802 }
2803
2804 final int aboveAppLayer = (mService.mPolicy.windowTypeToLayerLw(TYPE_APPLICATION) + 1)
2805 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
2806
2807 synchronized(mService.mWindowMap) {
2808 // Figure out the part of the screen that is actually the app.
2809 appWin = null;
2810 for (int i = mWindows.size() - 1; i >= 0; i--) {
2811 final WindowState ws = mWindows.get(i);
2812 if (!ws.mHasSurface) {
2813 continue;
2814 }
2815 if (ws.mLayer >= aboveAppLayer) {
2816 continue;
2817 }
2818 if (wallpaperOnly && !ws.mIsWallpaper) {
2819 continue;
2820 }
2821 if (ws.mIsImWindow) {
2822 if (!includeImeInScreenshot) {
2823 continue;
2824 }
2825 } else if (ws.mIsWallpaper) {
2826 // If this is the wallpaper layer and we're only looking for the wallpaper layer
2827 // then the target window state is this one.
2828 if (wallpaperOnly) {
2829 appWin = ws;
2830 }
2831
2832 if (appWin == null) {
2833 // We have not ran across the target window yet, so it is probably behind
2834 // the wallpaper. This can happen when the keyguard is up and all windows
2835 // are moved behind the wallpaper. We don't want to include the wallpaper
2836 // layer in the screenshot as it will cover-up the layer of the target
2837 // window.
2838 continue;
2839 }
2840 // Fall through. The target window is in front of the wallpaper. For this
2841 // case we want to include the wallpaper layer in the screenshot because
2842 // the target window might have some transparent areas.
2843 } else if (appToken != null) {
2844 if (ws.mAppToken == null || ws.mAppToken.token != appToken) {
2845 // This app window is of no interest if it is not associated with the
2846 // screenshot app.
2847 continue;
2848 }
2849 appWin = ws;
2850 }
2851
2852 // Include this window.
2853
2854 final WindowStateAnimator winAnim = ws.mWinAnimator;
2855 int layer = winAnim.mSurfaceController.getLayer();
2856 if (maxLayer < layer) {
2857 maxLayer = layer;
2858 }
2859 if (minLayer > layer) {
2860 minLayer = layer;
2861 }
2862
2863 // Don't include wallpaper in bounds calculation
2864 if (!includeFullDisplay && !ws.mIsWallpaper) {
2865 final Rect wf = ws.mFrame;
2866 final Rect cr = ws.mContentInsets;
2867 int left = wf.left + cr.left;
2868 int top = wf.top + cr.top;
2869 int right = wf.right - cr.right;
2870 int bottom = wf.bottom - cr.bottom;
2871 frame.union(left, top, right, bottom);
2872 ws.getVisibleBounds(stackBounds);
2873 if (!Rect.intersects(frame, stackBounds)) {
2874 // Set frame empty if there's no intersection.
2875 frame.setEmpty();
2876 }
2877 }
2878
2879 final boolean foundTargetWs =
2880 (ws.mAppToken != null && ws.mAppToken.token == appToken)
2881 || (appWin != null && wallpaperOnly);
2882 if (foundTargetWs && ws.isDisplayedLw() && winAnim.getShown()) {
2883 screenshotReady = true;
2884 }
2885
2886 if (ws.isObscuringFullscreen(mDisplayInfo)){
2887 break;
2888 }
2889 }
2890
2891 if (appToken != null && appWin == null) {
2892 // Can't find a window to snapshot.
2893 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
2894 "Screenshot: Couldn't find a surface matching " + appToken);
2895 return null;
2896 }
2897
2898 if (!screenshotReady) {
2899 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
2900 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
2901 appWin.mWinAnimator.mDrawState)));
2902 return null;
2903 }
2904
2905 // Screenshot is ready to be taken. Everything from here below will continue
2906 // through the bottom of the loop and return a value. We only stay in the loop
2907 // because we don't want to release the mWindowMap lock until the screenshot is
2908 // taken.
2909
2910 if (maxLayer == 0) {
2911 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2912 + ": returning null maxLayer=" + maxLayer);
2913 return null;
2914 }
2915
2916 if (!includeFullDisplay) {
2917 // Constrain frame to the screen size.
2918 if (!frame.intersect(0, 0, dw, dh)) {
2919 frame.setEmpty();
2920 }
2921 } else {
2922 // Caller just wants entire display.
2923 frame.set(0, 0, dw, dh);
2924 }
2925 if (frame.isEmpty()) {
2926 return null;
2927 }
2928
2929 if (width < 0) {
2930 width = (int) (frame.width() * frameScale);
2931 }
2932 if (height < 0) {
2933 height = (int) (frame.height() * frameScale);
2934 }
2935
2936 // Tell surface flinger what part of the image to crop. Take the top
2937 // right part of the application, and crop the larger dimension to fit.
2938 Rect crop = new Rect(frame);
2939 if (width / (float) frame.width() < height / (float) frame.height()) {
2940 int cropWidth = (int)((float)width / (float)height * frame.height());
2941 crop.right = crop.left + cropWidth;
2942 } else {
2943 int cropHeight = (int)((float)height / (float)width * frame.width());
2944 crop.bottom = crop.top + cropHeight;
2945 }
2946
2947 // The screenshot API does not apply the current screen rotation.
2948 int rot = mDisplay.getRotation();
2949
2950 if (rot == ROTATION_90 || rot == ROTATION_270) {
2951 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
2952 }
2953
2954 // Surfaceflinger is not aware of orientation, so convert our logical
2955 // crop to surfaceflinger's portrait orientation.
2956 convertCropForSurfaceFlinger(crop, rot, dw, dh);
2957
2958 if (DEBUG_SCREENSHOT) {
2959 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
2960 + maxLayer + " appToken=" + appToken);
2961 for (int i = 0; i < mWindows.size(); i++) {
2962 final WindowState win = mWindows.get(i);
2963 final WindowSurfaceController controller = win.mWinAnimator.mSurfaceController;
2964 Slog.i(TAG_WM, win + ": " + win.mLayer
2965 + " animLayer=" + win.mWinAnimator.mAnimLayer
2966 + " surfaceLayer=" + ((controller == null)
2967 ? "null" : controller.getLayer()));
2968 }
2969 }
2970
2971 final ScreenRotationAnimation screenRotationAnimation =
2972 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
2973 final boolean inRotation = screenRotationAnimation != null &&
2974 screenRotationAnimation.isAnimating();
2975 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
2976 "Taking screenshot while rotating");
2977
2978 // We force pending transactions to flush before taking
2979 // the screenshot by pushing an empty synchronous transaction.
2980 SurfaceControl.openTransaction();
2981 SurfaceControl.closeTransactionSync();
2982
2983 bm = SurfaceControl.screenshot(crop, width, height, minLayer, maxLayer,
2984 inRotation, rot);
2985 if (bm == null) {
2986 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
2987 + ") to layer " + maxLayer);
2988 return null;
2989 }
2990 }
2991
2992 if (DEBUG_SCREENSHOT) {
2993 // TEST IF IT's ALL BLACK
2994 int[] buffer = new int[bm.getWidth() * bm.getHeight()];
2995 bm.getPixels(buffer, 0, bm.getWidth(), 0, 0, bm.getWidth(), bm.getHeight());
2996 boolean allBlack = true;
2997 final int firstColor = buffer[0];
2998 for (int i = 0; i < buffer.length; i++) {
2999 if (buffer[i] != firstColor) {
3000 allBlack = false;
3001 break;
3002 }
3003 }
3004 if (allBlack) {
3005 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
3006 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
3007 (appWin != null ?
3008 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
3009 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
3010 }
3011 }
3012
3013 // Create a copy of the screenshot that is immutable and backed in ashmem.
3014 // This greatly reduces the overhead of passing the bitmap between processes.
3015 Bitmap ret = bm.createAshmemBitmap(config);
3016 bm.recycle();
3017 return ret;
3018 }
3019
3020 // TODO: Can this use createRotationMatrix()?
3021 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3022 if (rot == Surface.ROTATION_90) {
3023 final int tmp = crop.top;
3024 crop.top = dw - crop.right;
3025 crop.right = crop.bottom;
3026 crop.bottom = dw - crop.left;
3027 crop.left = tmp;
3028 } else if (rot == Surface.ROTATION_180) {
3029 int tmp = crop.top;
3030 crop.top = dh - crop.bottom;
3031 crop.bottom = dh - tmp;
3032 tmp = crop.right;
3033 crop.right = dw - crop.left;
3034 crop.left = dw - tmp;
3035 } else if (rot == Surface.ROTATION_270) {
3036 final int tmp = crop.top;
3037 crop.top = crop.left;
3038 crop.left = dh - crop.bottom;
3039 crop.bottom = crop.right;
3040 crop.right = dh - tmp;
3041 }
3042 }
3043
3044 void onSeamlessRotationTimeout() {
3045 boolean layoutNeeded = false;
3046 for (int i = mWindows.size() - 1; i >= 0; i--) {
3047 final WindowState w = mWindows.get(i);
3048 if (!w.mSeamlesslyRotated) {
3049 continue;
3050 }
3051 layoutNeeded = true;
3052 w.setDisplayLayoutNeeded();
3053 mService.markForSeamlessRotation(w, false);
3054 }
3055
3056 if (layoutNeeded) {
3057 mService.mWindowPlacerLocked.performSurfacePlacement();
3058 }
3059 }
3060
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003061 static final class GetWindowOnDisplaySearchResult {
Wale Ogunwaleec731152016-09-08 20:18:57 -07003062 boolean reachedToken;
3063 WindowState foundWindow;
3064
3065 void reset() {
3066 reachedToken = false;
3067 foundWindow = null;
3068 }
3069 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003070
3071 static final class TaskForResizePointSearchResult {
3072 boolean searchDone;
3073 Task taskForResize;
3074
3075 void reset() {
3076 searchDone = false;
3077 taskForResize = null;
3078 }
3079 }
Robert Carr3b716242016-08-16 16:02:21 -07003080
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003081 /**
3082 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3083 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3084 * homogeneous children type which is currently required by sub-classes of
3085 * {@link WindowContainer} class.
3086 */
3087 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3088
3089 int size() {
3090 return mChildren.size();
3091 }
3092
3093 E get(int index) {
3094 return mChildren.get(index);
3095 }
3096
3097 @Override
3098 boolean fillsParent() {
3099 return true;
3100 }
3101
3102 @Override
3103 boolean isVisible() {
3104 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003105 }
3106 }
3107
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003108 /**
3109 * Window container class that contains all containers on this display relating to Apps.
3110 * I.e Activities.
3111 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003112 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003113
3114 void attachStack(TaskStack stack, boolean onTop) {
3115 if (stack.mStackId == HOME_STACK_ID) {
3116 if (mHomeStack != null) {
3117 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3118 }
3119 mHomeStack = stack;
3120 }
3121 addChild(stack, onTop);
3122 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003123 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003124
3125 void moveStack(TaskStack stack, boolean toTop) {
3126 if (StackId.isAlwaysOnTop(stack.mStackId) && !toTop) {
3127 // This stack is always-on-top silly...
3128 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + stack + " to bottom");
3129 return;
3130 }
3131
3132 if (!mChildren.contains(stack)) {
3133 Slog.wtf(TAG_WM, "moving stack that was not added: " + stack, new Throwable());
3134 }
3135 removeChild(stack);
3136 addChild(stack, toTop);
3137 }
3138
3139 private void addChild(TaskStack stack, boolean toTop) {
3140 int addIndex = toTop ? mChildren.size() : 0;
3141
3142 if (toTop
3143 && mService.isStackVisibleLocked(PINNED_STACK_ID)
3144 && stack.mStackId != PINNED_STACK_ID) {
3145 // The pinned stack is always the top most stack (always-on-top) when it is visible.
3146 // So, stack is moved just below the pinned stack.
3147 addIndex--;
3148 TaskStack topStack = mChildren.get(addIndex);
3149 if (topStack.mStackId != PINNED_STACK_ID) {
3150 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3151 }
3152 }
3153 addChild(stack, addIndex);
3154 setLayoutNeeded();
3155 }
3156
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003157 @Override
3158 int getOrientation() {
3159 if (mService.isStackVisibleLocked(DOCKED_STACK_ID)
3160 || mService.isStackVisibleLocked(FREEFORM_WORKSPACE_STACK_ID)) {
3161 // Apps and their containers are not allowed to specify an orientation while the
3162 // docked or freeform stack is visible...except for the home stack/task if the
3163 // docked stack is minimized and it actually set something.
3164 if (mHomeStack != null && mHomeStack.isVisible()
3165 && mDividerControllerLocked.isMinimizedDock()) {
3166 final int orientation = mHomeStack.getOrientation();
3167 if (orientation != SCREEN_ORIENTATION_UNSET) {
3168 return orientation;
3169 }
3170 }
3171 return SCREEN_ORIENTATION_UNSPECIFIED;
3172 }
3173
3174 final int orientation = super.getOrientation();
3175 if (orientation != SCREEN_ORIENTATION_UNSET
3176 && orientation != SCREEN_ORIENTATION_BEHIND) {
3177 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3178 "App is requesting an orientation, return " + orientation);
3179 return orientation;
3180 }
3181
3182 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3183 "No app is requesting an orientation, return " + mService.mLastOrientation);
3184 // The next app has not been requested to be visible, so we keep the current orientation
3185 // to prevent freezing/unfreezing the display too early.
3186 return mService.mLastOrientation;
3187 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003188 }
3189
3190 /**
3191 * Window container class that contains all containers on this display that are not related to
3192 * Apps. E.g. status bar.
3193 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003194 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3195 /**
3196 * Compares two child window tokens returns -1 if the first is lesser than the second in
3197 * terms of z-order and 1 otherwise.
3198 */
3199 final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
3200 // Tokens with higher base layer are z-ordered on-top.
3201 mService.mPolicy.windowTypeToLayerLw(token1.windowType)
3202 < mService.mPolicy.windowTypeToLayerLw(token2.windowType) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003203
Wale Ogunwale3a931692016-11-02 16:49:48 -07003204 private final String mName;
3205 NonAppWindowContainers(String name) {
3206 mName = name;
3207 }
3208
3209 void addChild(WindowToken token) {
3210 addChild(token, mWindowComparator);
3211 }
3212
3213 @Override
3214 String getName() {
3215 return mName;
3216 }
Robert Carr3b716242016-08-16 16:02:21 -07003217 }
Craig Mautner59c00972012-07-30 12:10:24 -07003218}