blob: 3ff0e7a7d4c8cc6e50fbad05d45e396537681505 [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;
Andrii Kulian839def92016-11-02 10:58:58 -070079import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Jorim Jaggife762342016-10-13 14:33:27 +020080import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070081import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070082import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Wale Ogunwaleec731152016-09-08 20:18:57 -070083import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_MOVEMENT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070084import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070085import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070086import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080087import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070088import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
89import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070090import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070091import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
92import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER;
93import static com.android.server.wm.WindowManagerService.TYPE_LAYER_OFFSET;
94import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Wale Ogunwale494009b82016-10-21 09:01:38 -070095import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070096import static com.android.server.wm.WindowManagerService.dipToPixel;
97import static com.android.server.wm.WindowManagerService.localLOGV;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070098import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070099import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700100import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700101import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700102import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700103
Andrii Kulian3a507b52016-09-19 18:14:12 -0700104import android.annotation.NonNull;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700105import android.app.ActivityManager.StackId;
Andrii Kulian441e4492016-09-29 15:25:00 -0700106import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700107import android.graphics.Bitmap;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700108import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800109import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700110import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700111import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100112import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700113import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700114import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700115import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700116import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700117import android.os.RemoteException;
118import android.os.SystemClock;
Chong Zhang8e89b312015-09-09 15:09:30 -0700119import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700120import android.util.Slog;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700121import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700122import android.view.DisplayInfo;
Wale Ogunwaleec731152016-09-08 20:18:57 -0700123import android.view.IWindow;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700124import android.view.InputChannel;
125import android.view.Surface;
126import android.view.SurfaceControl;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700127import android.view.WindowManagerPolicy;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700128
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700129import com.android.internal.util.FastPrintWriter;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700130import com.android.internal.view.IInputMethodClient;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700131import com.android.server.input.InputWindowHandle;
Craig Mautner59c00972012-07-30 12:10:24 -0700132
Robert Carr3b716242016-08-16 16:02:21 -0700133import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700134import java.io.PrintWriter;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700135import java.io.StringWriter;
Craig Mautner59c00972012-07-30 12:10:24 -0700136import java.util.ArrayList;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700137import java.util.Arrays;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700138import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700139import java.util.HashMap;
140import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700141import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700142import java.util.List;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -0700143import java.util.function.Consumer;
Craig Mautner59c00972012-07-30 12:10:24 -0700144
Craig Mautner59c00972012-07-30 12:10:24 -0700145/**
146 * Utility class for keeping track of the WindowStates and other pertinent contents of a
147 * particular Display.
148 *
149 * IMPORTANT: No method from this class should ever be used without holding
150 * WindowManagerService.mWindowMap.
151 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700152class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700153 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700154
155 /** Unique identifier of this stack. */
156 private final int mDisplayId;
157
Wale Ogunwale3a931692016-11-02 16:49:48 -0700158 /** The containers below are the only child containers the display can have. */
159 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700160 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700161 // Contains all non-app window containers that should be displayed above the app containers
162 // (e.g. Status bar)
163 private final NonAppWindowContainers mAboveAppWindowsContainers =
164 new NonAppWindowContainers("mAboveAppWindowsContainers");
165 // Contains all non-app window containers that should be displayed below the app containers
166 // (e.g. Wallpaper).
167 private final NonAppWindowContainers mBelowAppWindowsContainers =
168 new NonAppWindowContainers("mBelowAppWindowsContainers");
169 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
170 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
171 // window containers together and move them in-sync if/when needed.
172 private final NonAppWindowContainers mImeWindowsContainers =
173 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700174
Wale Ogunwale3a931692016-11-02 16:49:48 -0700175 // Z-ordered (bottom-most first) list of all Window objects.
Craig Mautnerdc548482014-02-05 13:35:24 -0800176 private final WindowList mWindows = new WindowList();
Craig Mautner59c00972012-07-30 12:10:24 -0700177
Wale Ogunwale02319a62016-09-26 15:21:22 -0700178 // Mapping from a token IBinder to a WindowToken object on this display.
179 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
180
Craig Mautner59c00972012-07-30 12:10:24 -0700181 int mInitialDisplayWidth = 0;
182 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700183 int mInitialDisplayDensity = 0;
Craig Mautner59c00972012-07-30 12:10:24 -0700184 int mBaseDisplayWidth = 0;
185 int mBaseDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700186 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700187 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700188 private final DisplayInfo mDisplayInfo = new DisplayInfo();
189 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700190 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Craig Mautner59c00972012-07-30 12:10:24 -0700191
Craig Mautner6601b7b2013-04-29 10:29:11 -0700192 Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700193 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700194
Craig Mautner39834192012-09-02 07:47:24 -0700195 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700196 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700197 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700198 // TODO(multi-display): remove some of the usages.
Craig Mautner69b08182012-09-05 13:07:13 -0700199 final boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700200
Craig Mautnerdc548482014-02-05 13:35:24 -0800201 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700202 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800203
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800204 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
205 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700206 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700207
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700208 /** Detect user tapping outside of current focused task bounds .*/
209 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700210
Craig Mautner6601b7b2013-04-29 10:29:11 -0700211 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700212 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700213
Craig Mautner6601b7b2013-04-29 10:29:11 -0700214 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800215 private final Rect mTmpRect = new Rect();
216 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700217 private final RectF mTmpRectF = new RectF();
218 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800219 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700220
Craig Mautner9d808b12013-08-06 18:00:25 -0700221 final WindowManagerService mService;
222
Craig Mautner95da1082014-02-24 17:54:35 -0800223 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700224 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800225
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700226 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700227 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700228
Chong Zhang112eb8c2015-11-02 11:17:00 -0800229 final DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700230
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800231 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
232
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700233 /** Used when rebuilding window list to keep track of windows that have been removed. */
234 private WindowState[] mRebuildTmp = new WindowState[20];
235
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700236 /**
237 * Temporary list for comparison. Always clear this after use so we don't end up with
238 * orphaned windows references
239 */
240 private final ArrayList<WindowState> mTmpWindows = new ArrayList<>();
241
242 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
243
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700244 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
245 new TaskForResizePointSearchResult();
246 private final GetWindowOnDisplaySearchResult mTmpGetWindowOnDisplaySearchResult =
247 new GetWindowOnDisplaySearchResult();
248
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700249 // True if this display is in the process of being removed. Used to determine if the removal of
250 // the display's direct children should be allowed.
251 private boolean mRemovingDisplay = false;
252
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700253 private final WindowLayersController mLayersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700254 final WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700255 int mInputMethodAnimLayerAdjustment;
256
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800257 /**
Craig Mautner2d5618c2012-10-18 13:55:47 -0700258 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800259 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700260 * @param layersController window layer controller used to assign layer to the windows on this
261 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700262 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
263 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700264 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700265 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700266 WindowLayersController layersController, WallpaperController wallpaperController) {
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700267 mDisplay = display;
268 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700269 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700270 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700271 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700272 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700273 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700274 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700275 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800276 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700277 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800278 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700279
280 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700281 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700282 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700283 super.addChild(mAboveAppWindowsContainers, null);
284 super.addChild(mImeWindowsContainers, null);
Craig Mautner59c00972012-07-30 12:10:24 -0700285 }
286
287 int getDisplayId() {
288 return mDisplayId;
289 }
290
Wale Ogunwale02319a62016-09-26 15:21:22 -0700291 WindowToken getWindowToken(IBinder binder) {
292 return mTokenMap.get(binder);
293 }
294
295 AppWindowToken getAppWindowToken(IBinder binder) {
296 final WindowToken token = getWindowToken(binder);
297 if (token == null) {
298 return null;
299 }
300 return token.asAppWindowToken();
301 }
302
303 void setWindowToken(IBinder binder, WindowToken token) {
304 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
305 if (dc != null) {
306 // We currently don't support adding a window token to the display if the display
307 // already has the binder mapped to another token. If there is a use case for supporting
308 // this moving forward we will either need to merge the WindowTokens some how or have
309 // the binder map to a list of window tokens.
310 throw new IllegalArgumentException("Can't map token=" + token + " to display=" + this
311 + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
312 }
313 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700314
315 if (token.asAppWindowToken() == null) {
316 // Add non-app token to container hierarchy on the display. App tokens are added through
317 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700318 switch (token.windowType) {
319 case TYPE_WALLPAPER:
320 mBelowAppWindowsContainers.addChild(token);
321 break;
322 case TYPE_INPUT_METHOD:
323 case TYPE_INPUT_METHOD_DIALOG:
324 mImeWindowsContainers.addChild(token);
325 break;
326 default:
327 mAboveAppWindowsContainers.addChild(token);
328 break;
329 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700330 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700331 }
332
333 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700334 final WindowToken token = mTokenMap.remove(binder);
335 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3a931692016-11-02 16:49:48 -0700336 switch (token.windowType) {
337 case TYPE_WALLPAPER:
338 mBelowAppWindowsContainers.removeChild(token);
339 break;
340 case TYPE_INPUT_METHOD:
341 case TYPE_INPUT_METHOD_DIALOG:
342 mImeWindowsContainers.removeChild(token);
343 break;
344 default:
345 mAboveAppWindowsContainers.removeChild(token);
346 break;
347 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700348 }
349 return token;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700350 }
351
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700352 void removeAppToken(IBinder binder) {
353 final WindowToken token = removeWindowToken(binder);
354 if (token == null) {
355 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
356 return;
357 }
358
359 final AppWindowToken appToken = token.asAppWindowToken();
360
361 if (appToken == null) {
362 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
363 return;
364 }
365
366 appToken.onRemovedFromDisplay();
367 }
368
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700369 Display getDisplay() {
370 return mDisplay;
371 }
372
Craig Mautner59c00972012-07-30 12:10:24 -0700373 DisplayInfo getDisplayInfo() {
374 return mDisplayInfo;
375 }
376
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700377 DisplayMetrics getDisplayMetrics() {
378 return mDisplayMetrics;
379 }
380
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100381 DockedStackDividerController getDockedDividerController() {
382 return mDividerControllerLocked;
383 }
384
Winson Chung655332c2016-10-31 13:14:28 -0700385 PinnedStackController getPinnedStackController() {
386 return mPinnedStackControllerLocked;
387 }
388
Jeff Browna506a6e2013-06-04 00:02:38 -0700389 /**
390 * Returns true if the specified UID has access to this display.
391 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700392 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700393 return mDisplay.hasAccess(uid);
394 }
395
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700396 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700397 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -0700398 }
399
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700400 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700401 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800402 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -0800403 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700404 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700405 }
406
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700407 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700408 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
409 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700410 if (stack.mStackId == stackId) {
411 return stack;
412 }
413 }
414 return null;
415 }
416
Andrii Kulian441e4492016-09-29 15:25:00 -0700417 @Override
418 void onConfigurationChanged(Configuration newParentConfig) {
419 super.onConfigurationChanged(newParentConfig);
420
Andrii Kulian3a507b52016-09-19 18:14:12 -0700421 // The display size information is heavily dependent on the resources in the current
422 // configuration, so we need to reconfigure it every time the configuration changes.
423 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
424 mService.reconfigureDisplayLocked(this);
425
426 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -0700427 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -0700428 }
Andrii Kulian3a507b52016-09-19 18:14:12 -0700429
Andrii Kulian441e4492016-09-29 15:25:00 -0700430 /**
431 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
432 * bounds were updated.
433 */
434 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700435 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
436 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -0700437 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -0700438 changedStackList.add(stack.mStackId);
439 }
440 }
441 }
442
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700443 @Override
444 boolean fillsParent() {
445 return true;
446 }
447
448 @Override
449 boolean isVisible() {
450 return true;
451 }
452
453 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700454 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -0700455 super.onAppTransitionDone();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700456 rebuildAppWindowList();
457 }
458
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700459 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -0700460 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700461 final WindowManagerPolicy policy = mService.mPolicy;
462
463 // TODO: All the logic before the last return statement in this method should really go in
464 // #NonAppWindowContainer.getOrientation() since it is trying to decide orientation based
465 // on non-app windows. But, we can not do that until the window list is always correct in
466 // terms of z-ordering based on layers.
467 if (mService.mDisplayFrozen) {
468 if (mService.mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
469 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
470 "Display is frozen, return " + mService.mLastWindowForcedOrientation);
471 // If the display is frozen, some activities may be in the middle of restarting, and
472 // thus have removed their old window. If the window has the flag to hide the lock
473 // screen, then the lock screen can re-appear and inflict its own orientation on us.
474 // Keep the orientation stable until this all settles down.
475 return mService.mLastWindowForcedOrientation;
476 } else if (policy.isKeyguardLocked()) {
477 // Use the last orientation the while the display is frozen with the keyguard
478 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
479 // window. We don't want to check the show when locked window directly though as
480 // things aren't stable while the display is frozen, for example the window could be
481 // momentarily unavailable due to activity relaunch.
482 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
483 + "return " + mService.mLastOrientation);
484 return mService.mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -0700485 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700486 } else {
487 for (int pos = mWindows.size() - 1; pos >= 0; --pos) {
488 final WindowState win = mWindows.get(pos);
489 if (win.mAppToken != null) {
490 // We hit an application window. so the orientation will be determined by the
491 // app window. No point in continuing further.
492 break;
493 }
494 if (!win.isVisibleLw() || !win.mPolicyVisibilityAfterAnim) {
495 continue;
496 }
497 int req = win.mAttrs.screenOrientation;
498 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND) {
499 continue;
500 }
501
502 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
503 if (policy.isKeyguardHostWindow(win.mAttrs)) {
504 mService.mLastKeyguardForcedOrientation = req;
505 }
506 return (mService.mLastWindowForcedOrientation = req);
507 }
508 mService.mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
509
Jorim Jaggife762342016-10-13 14:33:27 +0200510 if (policy.isKeyguardShowingAndNotOccluded()) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700511 return mService.mLastKeyguardForcedOrientation;
512 }
Wale Ogunwale51362492016-09-08 17:49:17 -0700513 }
514
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700515 // Top system windows are not requesting an orientation. Start searching from apps.
516 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -0700517 }
518
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700519 void updateDisplayInfo() {
Craig Mautner722285e2012-09-07 13:55:58 -0700520 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700521 mDisplay.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700522 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
523 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800524 }
Craig Mautner722285e2012-09-07 13:55:58 -0700525 }
526
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700527 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700528 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
529 if (displayManagerInternal != null) {
530 // Bootstrap the default logical display from the display manager.
531 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
532 if (newDisplayInfo != null) {
533 mDisplayInfo.copyFrom(newDisplayInfo);
534 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700535 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700536
Filip Gruszczynski608797e2015-11-12 19:08:20 -0800537 mBaseDisplayWidth = mInitialDisplayWidth = mDisplayInfo.logicalWidth;
538 mBaseDisplayHeight = mInitialDisplayHeight = mDisplayInfo.logicalHeight;
539 mBaseDisplayDensity = mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
540 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700541 }
542
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700543 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700544 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800545 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700546 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800547 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
548 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700549 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800550 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700551 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800552 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700553 out.set(left, top, left + width, top + height);
554 }
555
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700556 private void getLogicalDisplayRect(Rect out, int orientation) {
557 getLogicalDisplayRect(out);
558
559 // Rotate the Rect if needed.
560 final int currentRotation = mDisplayInfo.rotation;
561 final int rotationDelta = deltaRotation(currentRotation, orientation);
562 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
563 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
564 mTmpRectF.set(out);
565 mTmpMatrix.mapRect(mTmpRectF);
566 mTmpRectF.round(out);
567 }
568 }
569
Chong Zhangf66db432016-01-13 10:39:51 -0800570 void getContentRect(Rect out) {
571 out.set(mContentRect);
572 }
573
Andrii Kulian839def92016-11-02 10:58:58 -0700574 /**
575 * Adds the stack to this display.
576 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
577 */
578 Rect addStackToDisplay(int stackId, boolean onTop) {
579 boolean attachedToDisplay = false;
580 TaskStack stack = mService.mStackIdToStack.get(stackId);
581 if (stack == null) {
582 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
583 + mDisplayId);
584
585 stack = getStackById(stackId);
586 if (stack != null) {
587 // It's already attached to the display...clear mDeferRemoval and move stack to
588 // appropriate z-order on display as needed.
589 stack.mDeferRemoval = false;
590 moveStack(stack, onTop);
591 attachedToDisplay = true;
592 } else {
593 stack = new TaskStack(mService, stackId);
594 }
595
596 mService.mStackIdToStack.put(stackId, stack);
597 if (stackId == DOCKED_STACK_ID) {
598 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
599 }
600 } else {
601 final DisplayContent currentDC = stack.getDisplayContent();
602 if (currentDC != null) {
603 throw new IllegalStateException("Trying to add stackId=" + stackId
604 + "to displayId=" + mDisplayId + ", but it's already attached to displayId="
605 + currentDC.getDisplayId());
606 }
607 }
608
609 if (!attachedToDisplay) {
610 mTaskStackContainers.addStackToDisplay(stack, onTop);
611 }
612
613 if (stack.getRawFullscreen()) {
614 return null;
615 }
616 final Rect bounds = new Rect();
617 stack.getRawBounds(bounds);
618 return bounds;
619 }
620
621 /** Removes the stack from the display and prepares for changing the parent. */
622 private void removeStackFromDisplay(TaskStack stack) {
623 mTaskStackContainers.removeStackFromDisplay(stack);
624 }
625
626 /** Moves the stack to this display and returns the updated bounds. */
627 Rect moveStackToDisplay(TaskStack stack) {
628 final DisplayContent currentDisplayContent = stack.getDisplayContent();
629 if (currentDisplayContent == null) {
630 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
631 + " which is not currently attached to any display");
632 }
633 if (stack.getDisplayContent().getDisplayId() == mDisplayId) {
634 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
635 + " to its current displayId=" + mDisplayId);
636 }
637
638 currentDisplayContent.removeStackFromDisplay(stack);
639 return addStackToDisplay(stack.mStackId, true /* onTop */);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800640 }
641
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800642 void moveStack(TaskStack stack, boolean toTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700643 mTaskStackContainers.moveStack(stack, toTop);
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700644 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700645
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700646 @Override
647 protected void addChild(DisplayChildWindowContainer child,
648 Comparator<DisplayChildWindowContainer> comparator) {
649 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
650 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700651
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700652 @Override
653 protected void addChild(DisplayChildWindowContainer child, int index) {
654 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
655 }
656
657 @Override
658 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700659 // Only allow removal of direct children from this display if the display is in the process
660 // of been removed.
661 if (mRemovingDisplay) {
662 super.removeChild(child);
663 return;
664 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700665 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800666 }
667
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700668 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700669 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
670 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700671 final int taskId = stack.taskIdFromPoint(x, y);
672 if (taskId != -1) {
673 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -0700674 }
675 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800676 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -0700677 }
678
Chong Zhang8e89b312015-09-09 15:09:30 -0700679 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -0800680 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -0700681 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -0700682 */
Wale Ogunwale15ead902016-09-02 14:30:11 -0700683 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700684 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700685 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700686 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
687 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -0700688 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700689 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -0700690 }
Chong Zhang9184ec62015-09-24 12:32:21 -0700691
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700692 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
693 if (mTmpTaskForResizePointSearchResult.searchDone) {
694 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -0700695 }
696 }
Chong Zhang9184ec62015-09-24 12:32:21 -0700697 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -0700698 }
699
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700700 void setTouchExcludeRegion(Task focusedTask) {
Craig Mautner6601b7b2013-04-29 10:29:11 -0700701 mTouchExcludeRegion.set(mBaseDisplayRect);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700702 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700703 mTmpRect2.setEmpty();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700704 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
705 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700706 stack.setTouchExcludeRegion(
707 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
Craig Mautner6601b7b2013-04-29 10:29:11 -0700708 }
Chong Zhangd8ceb852015-11-11 14:53:41 -0800709 // If we removed the focused task above, add it back and only leave its
710 // outside touch area in the exclusion. TapDectector is not interested in
711 // any touch inside the focused task itself.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700712 if (!mTmpRect2.isEmpty()) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800713 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
714 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800715 final WindowState inputMethod = mService.mInputMethodWindow;
716 if (inputMethod != null && inputMethod.isVisibleLw()) {
717 // If the input method is visible and the user is typing, we don't want these touch
718 // events to be intercepted and used to change focus. This would likely cause a
719 // disappearance of the input method.
720 inputMethod.getTouchableRegion(mTmpRegion);
721 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
722 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800723 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
724 WindowState win = mTapExcludedWindows.get(i);
725 win.getTouchableRegion(mTmpRegion);
726 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
727 }
Jorim Jaggife762342016-10-13 14:33:27 +0200728 if (getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100729 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -0700730 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100731 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
732 }
Craig Mautner1bef3892015-02-17 15:09:47 -0800733 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700734 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -0800735 }
Craig Mautner6601b7b2013-04-29 10:29:11 -0700736 }
737
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700738 void switchUser() {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700739 final int count = mWindows.size();
740 for (int i = 0; i < count; i++) {
741 final WindowState win = mWindows.get(i);
Craig Mautner858d8a62013-04-23 17:08:34 -0700742 if (win.isHiddenFromUserLocked()) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800743 if (DEBUG_VISIBILITY) Slog.w(TAG_WM, "user changing, hiding " + win
Wale Ogunwale498e8c92015-02-13 09:42:46 -0800744 + ", attrs=" + win.mAttrs.type + ", belonging to " + win.mOwnerUid);
Craig Mautner858d8a62013-04-23 17:08:34 -0700745 win.hideLw(false);
746 }
747 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700748
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700749 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
750 mTaskStackContainers.get(stackNdx).switchUser();
Craig Mautner858d8a62013-04-23 17:08:34 -0700751 }
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700752
753 rebuildAppWindowList();
Craig Mautner858d8a62013-04-23 17:08:34 -0700754 }
755
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700756 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700757 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
758 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -0700759 }
760 }
761
762 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800763 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -0700764 }
765
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700766 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800767 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -0700768 }
769
770 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800771 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -0700772 }
773
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700774 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800775 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -0700776 }
777
Wale Ogunwale10124582016-09-15 20:25:50 -0700778 @Override
779 void removeIfPossible() {
780 if (isAnimating()) {
781 mDeferredRemoval = true;
782 return;
Craig Mautner2eb15342013-08-07 13:13:35 -0700783 }
Wale Ogunwale10124582016-09-15 20:25:50 -0700784 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -0700785 }
786
Wale Ogunwale10124582016-09-15 20:25:50 -0700787 @Override
788 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700789 mRemovingDisplay = true;
790 try {
791 super.removeImmediately();
792 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
793 mDimLayerController.close();
794 if (mDisplayId == DEFAULT_DISPLAY) {
795 mService.unregisterPointerEventListener(mTapDetector);
796 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
797 }
798 } finally {
799 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -0800800 }
Craig Mautner95da1082014-02-24 17:54:35 -0800801 }
802
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700803 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -0700804 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700805 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -0700806 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
807
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700808 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -0700809 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -0800810 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700811 return false;
Craig Mautner95da1082014-02-24 17:54:35 -0800812 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700813 return true;
Craig Mautner95da1082014-02-24 17:54:35 -0800814 }
815
Wale Ogunwale10124582016-09-15 20:25:50 -0700816 boolean animateForIme(float interpolatedValue, float animationTarget,
817 float dividerAnimationTarget) {
818 boolean updated = false;
819
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700820 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
821 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700822 if (stack == null || !stack.isAdjustedForIme()) {
823 continue;
824 }
825
826 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
827 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
828 updated = true;
829 } else {
830 mDividerControllerLocked.mLastAnimationProgress =
831 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
832 mDividerControllerLocked.mLastDividerProgress =
833 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
834 updated |= stack.updateAdjustForIme(
835 mDividerControllerLocked.mLastAnimationProgress,
836 mDividerControllerLocked.mLastDividerProgress,
837 false /* force */);
838 }
839 if (interpolatedValue >= 1f) {
840 stack.endImeAdjustAnimation();
841 }
842 }
843
844 return updated;
845 }
846
847 boolean clearImeAdjustAnimation() {
848 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700849 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
850 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700851 if (stack != null && stack.isAdjustedForIme()) {
852 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
853 changed = true;
854 }
855 }
856 return changed;
857 }
858
859 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700860 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
861 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700862 if (stack.isVisible() && stack.isAdjustedForIme()) {
863 stack.beginImeAdjustAnimation();
864 }
865 }
866 }
867
868 void adjustForImeIfNeeded() {
869 final WindowState imeWin = mService.mInputMethodWindow;
870 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
871 && !mDividerControllerLocked.isImeHideRequested();
872 final boolean dockVisible = mService.isStackVisibleLocked(DOCKED_STACK_ID);
873 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
874 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
875 imeTargetStack.getDockSide() : DOCKED_INVALID;
876 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
877 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
878 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
879 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
880 final boolean imeHeightChanged = imeVisible &&
881 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
882
883 // The divider could be adjusted for IME position, or be thinner than usual,
884 // or both. There are three possible cases:
885 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
886 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
887 // - If IME is not visible, divider is not moved and is normal width.
888
889 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700890 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
891 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700892 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
893 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)) {
894 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
895 } else {
896 stack.resetAdjustedForIme(false);
897 }
898 }
899 mDividerControllerLocked.setAdjustedForIme(
900 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
901 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700902 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
903 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700904 stack.resetAdjustedForIme(!dockVisible);
905 }
906 mDividerControllerLocked.setAdjustedForIme(
907 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
908 }
Winson Chung655332c2016-10-31 13:14:28 -0700909 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -0700910 }
911
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700912 void setInputMethodAnimLayerAdjustment(int adj) {
913 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
914 mInputMethodAnimLayerAdjustment = adj;
915 final WindowState imw = mService.mInputMethodWindow;
916 if (imw != null) {
917 imw.adjustAnimLayer(adj);
918 }
919 for (int i = mService.mInputMethodDialogs.size() - 1; i >= 0; i--) {
920 final WindowState dialog = mService.mInputMethodDialogs.get(i);
921 // TODO: This and other places setting mAnimLayer can probably use WS.adjustAnimLayer,
922 // but need to make sure we are not setting things twice for child windows that are
923 // already in the list.
924 dialog.mWinAnimator.mAnimLayer = dialog.mLayer + adj;
925 if (DEBUG_LAYERS) Slog.v(TAG_WM, "IM win " + imw
926 + " anim layer: " + dialog.mWinAnimator.mAnimLayer);
927 }
928 }
929
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700930 /**
931 * If a window that has an animation specifying a colored background and the current wallpaper
932 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
933 * suddenly disappear.
934 */
935 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
936 for (int i = mWindows.size() - 1; i >= 0; --i) {
937 final WindowState win = mWindows.get(i);
938 if (win.mIsWallpaper && win.isVisibleNow()) {
939 return win.mWinAnimator.mAnimLayer;
940 }
941 }
942 return winAnimator.mAnimLayer;
943 }
944
Wale Ogunwale10124582016-09-15 20:25:50 -0700945 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700946 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
947 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -0700948 stack.prepareFreezingTaskBounds();
949 }
950 }
951
Wale Ogunwale94744212015-09-21 19:01:47 -0700952 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700953 getLogicalDisplayRect(mTmpRect, newRotation);
954
955 // Compute a transform matrix to undo the coordinate space transformation,
956 // and present the window at the same physical position it previously occupied.
957 final int deltaRotation = deltaRotation(newRotation, oldRotation);
958 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
959
960 mTmpRectF.set(bounds);
961 mTmpMatrix.mapRect(mTmpRectF);
962 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -0700963 }
964
Wale Ogunwale4a02d812015-02-12 23:01:38 -0800965 static int deltaRotation(int oldRotation, int newRotation) {
966 int delta = newRotation - oldRotation;
967 if (delta < 0) delta += 4;
968 return delta;
969 }
970
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700971 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700972 Matrix outMatrix) {
973 // For rotations without Z-ordering we don't need the target rectangle's position.
974 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
975 displayHeight, outMatrix);
976 }
977
978 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
979 float displayWidth, float displayHeight, Matrix outMatrix) {
980 switch (rotation) {
981 case ROTATION_0:
982 outMatrix.reset();
983 break;
984 case ROTATION_270:
985 outMatrix.setRotate(270, 0, 0);
986 outMatrix.postTranslate(0, displayHeight);
987 outMatrix.postTranslate(rectTop, 0);
988 break;
989 case ROTATION_180:
990 outMatrix.reset();
991 break;
992 case ROTATION_90:
993 outMatrix.setRotate(90, 0, 0);
994 outMatrix.postTranslate(displayWidth, 0);
995 outMatrix.postTranslate(-rectTop, rectLeft);
996 break;
997 }
998 }
999
Craig Mautnera91f9e22012-09-14 16:22:08 -07001000 public void dump(String prefix, PrintWriter pw) {
1001 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
1002 final String subPrefix = " " + prefix;
1003 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
1004 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
1005 pw.print("dpi");
1006 if (mInitialDisplayWidth != mBaseDisplayWidth
1007 || mInitialDisplayHeight != mBaseDisplayHeight
1008 || mInitialDisplayDensity != mBaseDisplayDensity) {
1009 pw.print(" base=");
1010 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
1011 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
1012 }
Jeff Brownd46747a2015-04-15 19:02:36 -07001013 if (mDisplayScalingDisabled) {
1014 pw.println(" noscale");
1015 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07001016 pw.print(" cur=");
1017 pw.print(mDisplayInfo.logicalWidth);
1018 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
1019 pw.print(" app=");
1020 pw.print(mDisplayInfo.appWidth);
1021 pw.print("x"); pw.print(mDisplayInfo.appHeight);
1022 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
1023 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
1024 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
1025 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001026 pw.println(subPrefix + "deferred=" + mDeferredRemoval
1027 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001028
Craig Mautnerdc548482014-02-05 13:35:24 -08001029 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001030 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001031 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1032 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001033 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001034 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001035
Craig Mautnerdc548482014-02-05 13:35:24 -08001036 pw.println();
1037 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001038 pw.println();
1039 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001040 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001041 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001042 pw.print(" Exiting #"); pw.print(i);
1043 pw.print(' '); pw.print(token);
1044 pw.println(':');
1045 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001046 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001047 }
Craig Mautner59c00972012-07-30 12:10:24 -07001048 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001049 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07001050 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001051 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07001052 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001053 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001054
1055 if (mInputMethodAnimLayerAdjustment != 0) {
1056 pw.println(subPrefix
1057 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
1058 }
Craig Mautner59c00972012-07-30 12:10:24 -07001059 }
Craig Mautnere0a38842013-12-16 16:14:02 -08001060
1061 @Override
1062 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001063 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001064 }
1065
1066 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001067 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08001068 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07001069
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001070 /**
1071 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
1072 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07001073 TaskStack getDockedStackLocked() {
Wale Ogunwalee45899a2015-10-01 11:30:34 -07001074 final TaskStack stack = mService.mStackIdToStack.get(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001075 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07001076 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001077
1078 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001079 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02001080 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001081 */
Jorim Jaggife762342016-10-13 14:33:27 +02001082 TaskStack getDockedStackIgnoringVisibility() {
1083 return mService.mStackIdToStack.get(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001084 }
1085
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001086 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001087 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
1088 WindowState touchedWin = null;
1089 final int x = (int) xf;
1090 final int y = (int) yf;
1091
1092 for (int i = mWindows.size() - 1; i >= 0; i--) {
1093 WindowState window = mWindows.get(i);
1094 final int flags = window.mAttrs.flags;
1095 if (!window.isVisibleLw()) {
1096 continue;
1097 }
1098 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
1099 continue;
1100 }
1101
1102 window.getVisibleBounds(mTmpRect);
1103 if (!mTmpRect.contains(x, y)) {
1104 continue;
1105 }
1106
1107 window.getTouchableRegion(mTmpRegion);
1108
1109 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
1110 if (mTmpRegion.contains(x, y) || touchFlags == 0) {
1111 touchedWin = window;
1112 break;
1113 }
1114 }
1115
1116 return touchedWin;
1117 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07001118
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001119 boolean canAddToastWindowForUid(int uid) {
1120 // We allow one toast window per UID being shown at a time.
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001121 final int windowCount = mWindows.size();
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001122 for (int i = 0; i < windowCount; i++) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001123 final WindowState window = mWindows.get(i);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001124 if (window.mAttrs.type == TYPE_TOAST && window.mOwnerUid == uid
Svet Ganov62a40f82016-09-29 00:43:51 -07001125 && !window.mPermanentlyHidden && !window.mAnimatingExit
1126 && !window.mRemoveOnExit) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001127 return false;
1128 }
1129 }
1130 return true;
1131 }
1132
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001133 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001134 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
1135 return;
1136 }
1137 final int lostFocusUid = oldFocus.mOwnerUid;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001138 final int windowCount = mWindows.size();
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001139 final Handler handler = mService.mH;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001140 for (int i = 0; i < windowCount; i++) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001141 final WindowState window = mWindows.get(i);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001142 if (window.mAttrs.type == TYPE_TOAST && window.mOwnerUid == lostFocusUid) {
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001143 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, window)) {
1144 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, window),
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001145 window.mAttrs.hideTimeoutMilliseconds);
1146 }
1147 }
1148 }
1149 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001150
1151 WindowState findFocusedWindow() {
1152 final AppWindowToken focusedApp = mService.mFocusedApp;
1153
1154 for (int i = mWindows.size() - 1; i >= 0; i--) {
1155 final WindowState win = mWindows.get(i);
1156
1157 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + i + " = " + win
1158 + ", flags=" + win.mAttrs.flags + ", canReceive=" + win.canReceiveKeys());
1159
1160 if (!win.canReceiveKeys()) {
1161 continue;
1162 }
1163
1164 final AppWindowToken wtoken = win.mAppToken;
1165
1166 // If this window's application has been removed, just skip it.
1167 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
1168 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
1169 + (wtoken.removed ? "removed" : "sendingToBottom"));
1170 continue;
1171 }
1172
1173 if (focusedApp == null) {
1174 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
1175 + " using new focus @ " + i + " = " + win);
1176 return win;
1177 }
1178
1179 if (!focusedApp.windowsAreFocusable()) {
1180 // Current focused app windows aren't focusable...
1181 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
1182 + " focusable using new focus @ " + i + " = " + win);
1183 return win;
1184 }
1185
1186 // Descend through all of the app tokens and find the first that either matches
1187 // win.mAppToken (return win) or mFocusedApp (return null).
1188 if (wtoken != null && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001189 if (focusedApp.compareTo(wtoken) > 0) {
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001190 // App stack below focused app stack. No focus for you!!!
1191 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
1192 "findFocusedWindow: Reached focused app=" + focusedApp);
1193 return null;
1194 }
1195 }
1196
1197 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ "
1198 + i + " = " + win);
1199 return win;
1200 }
1201
1202 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
1203 return null;
1204 }
Wale Ogunwaleec731152016-09-08 20:18:57 -07001205
Wale Ogunwale07bcab72016-10-14 15:30:09 -07001206 void addAppWindowToWindowList(final WindowState win) {
Wale Ogunwaleec731152016-09-08 20:18:57 -07001207 final IWindow client = win.mClient;
1208
1209 WindowList tokenWindowList = getTokenWindowsOnDisplay(win.mToken);
1210 if (!tokenWindowList.isEmpty()) {
Wale Ogunwale07bcab72016-10-14 15:30:09 -07001211 addAppWindowExisting(win, tokenWindowList);
1212 return;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001213 }
1214
1215 // No windows from this token on this display
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001216 if (localLOGV) Slog.v(TAG_WM, "Figuring out where to add app window "
Wale Ogunwaleec731152016-09-08 20:18:57 -07001217 + client.asBinder() + " (token=" + this + ")");
1218
1219 final WindowToken wToken = win.mToken;
1220
1221 // Figure out where the window should go, based on the order of applications.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001222 mTmpGetWindowOnDisplaySearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001223 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1224 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001225 stack.getWindowOnDisplayBeforeToken(this, wToken, mTmpGetWindowOnDisplaySearchResult);
1226 if (mTmpGetWindowOnDisplaySearchResult.reachedToken) {
Wale Ogunwaleec731152016-09-08 20:18:57 -07001227 // We have reach the token we are interested in. End search.
1228 break;
1229 }
1230 }
1231
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001232 WindowState pos = mTmpGetWindowOnDisplaySearchResult.foundWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001233
1234 // We now know the index into the apps. If we found an app window above, that gives us the
1235 // position; else we need to look some more.
1236 if (pos != null) {
1237 // Move behind any windows attached to this one.
Wale Ogunwale02319a62016-09-26 15:21:22 -07001238 final WindowToken atoken = getWindowToken(pos.mClient.asBinder());
Wale Ogunwaleec731152016-09-08 20:18:57 -07001239 if (atoken != null) {
1240 tokenWindowList = getTokenWindowsOnDisplay(atoken);
1241 final int NC = tokenWindowList.size();
1242 if (NC > 0) {
1243 WindowState bottom = tokenWindowList.get(0);
1244 if (bottom.mSubLayer < 0) {
1245 pos = bottom;
1246 }
1247 }
1248 }
1249 addWindowToListBefore(win, pos);
Wale Ogunwale07bcab72016-10-14 15:30:09 -07001250 return;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001251 }
1252
1253 // Continue looking down until we find the first token that has windows on this display.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001254 mTmpGetWindowOnDisplaySearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001255 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1256 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001257 stack.getWindowOnDisplayAfterToken(this, wToken, mTmpGetWindowOnDisplaySearchResult);
1258 if (mTmpGetWindowOnDisplaySearchResult.foundWindow != null) {
Wale Ogunwaleec731152016-09-08 20:18:57 -07001259 // We have found a window after the token. End search.
1260 break;
1261 }
1262 }
1263
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001264 pos = mTmpGetWindowOnDisplaySearchResult.foundWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001265
1266 if (pos != null) {
1267 // Move in front of any windows attached to this one.
Wale Ogunwale02319a62016-09-26 15:21:22 -07001268 final WindowToken atoken = getWindowToken(pos.mClient.asBinder());
Wale Ogunwaleec731152016-09-08 20:18:57 -07001269 if (atoken != null) {
1270 final WindowState top = atoken.getTopWindow();
1271 if (top != null && top.mSubLayer >= 0) {
1272 pos = top;
1273 }
1274 }
1275 addWindowToListAfter(win, pos);
Wale Ogunwale07bcab72016-10-14 15:30:09 -07001276 return;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001277 }
1278
1279 // Just search for the start of this layer.
1280 final int myLayer = win.mBaseLayer;
1281 int i;
1282 for (i = mWindows.size() - 1; i >= 0; --i) {
1283 final WindowState w = mWindows.get(i);
1284 // Dock divider shares the base layer with application windows, but we want to always
1285 // keep it above the application windows. The sharing of the base layer is intended
1286 // for window animations, which need to be above the dock divider for the duration
1287 // of the animation.
1288 if (w.mBaseLayer <= myLayer && w.mAttrs.type != TYPE_DOCK_DIVIDER) {
1289 break;
1290 }
1291 }
1292 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1293 "Based on layer: Adding window " + win + " at " + (i + 1) + " of "
1294 + mWindows.size());
1295 mWindows.add(i + 1, win);
1296 mService.mWindowsChanged = true;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001297 }
1298
1299 /** Adds this non-app window to the window list. */
1300 void addNonAppWindowToWindowList(WindowState win) {
1301 // Figure out where window should go, based on layer.
1302 int i;
1303 for (i = mWindows.size() - 1; i >= 0; i--) {
1304 final WindowState otherWin = mWindows.get(i);
1305 if (otherWin.getBaseType() != TYPE_WALLPAPER && otherWin.mBaseLayer <= win.mBaseLayer) {
1306 // Wallpaper wanders through the window list, for example to position itself
1307 // directly behind keyguard. Because of this it will break the ordering based on
1308 // WindowState.mBaseLayer. There might windows with higher mBaseLayer behind it and
1309 // we don't want the new window to appear above them. An example of this is adding
1310 // of the docked stack divider. Consider a scenario with the following ordering (top
1311 // to bottom): keyguard, wallpaper, assist preview, apps. We want the dock divider
1312 // to land below the assist preview, so the dock divider must ignore the wallpaper,
1313 // with which it shares the base layer.
1314 break;
1315 }
1316 }
1317
1318 i++;
1319 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1320 "Free window: Adding window " + this + " at " + i + " of " + mWindows.size());
1321 mWindows.add(i, win);
1322 mService.mWindowsChanged = true;
1323 }
1324
Wale Ogunwaleb9a07c32016-10-12 14:55:56 -07001325 void addToWindowList(WindowState win, int index) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001326 mService.mWindowsChanged = true;
Wale Ogunwaleb9a07c32016-10-12 14:55:56 -07001327 mWindows.add(index, win);
1328 }
1329
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001330 boolean removeFromWindowList(WindowState win) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001331 mService.mWindowsChanged = true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001332 return mWindows.remove(win);
1333 }
1334
1335 private int removeWindowAndChildrenFromWindowList(WindowState win, int interestingPos) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001336 int wpos = mWindows.indexOf(win);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001337 if (wpos < 0) {
1338 return interestingPos;
1339 }
1340
1341 if (wpos < interestingPos) interestingPos--;
1342 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG_WM, "Temp removing at " + wpos + ": " + this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001343 mWindows.remove(wpos);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001344 mService.mWindowsChanged = true;
1345 int childWinCount = win.mChildren.size();
1346 while (childWinCount > 0) {
1347 childWinCount--;
1348 final WindowState cw = win.mChildren.get(childWinCount);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001349 int cpos = mWindows.indexOf(cw);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001350 if (cpos >= 0) {
1351 if (cpos < interestingPos) interestingPos--;
1352 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG_WM,
1353 "Temp removing child at " + cpos + ": " + cw);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001354 mWindows.remove(cpos);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001355 }
1356 }
1357 return interestingPos;
1358 }
1359
Wale Ogunwaleec731152016-09-08 20:18:57 -07001360 void addChildWindowToWindowList(WindowState win) {
1361 final WindowState parentWindow = win.getParentWindow();
1362
1363 WindowList windowsOnSameDisplay = getTokenWindowsOnDisplay(win.mToken);
1364
1365 // Figure out this window's ordering relative to the parent window.
1366 final int wCount = windowsOnSameDisplay.size();
1367 final int sublayer = win.mSubLayer;
1368 int largestSublayer = Integer.MIN_VALUE;
1369 WindowState windowWithLargestSublayer = null;
1370 int i;
1371 for (i = 0; i < wCount; i++) {
1372 WindowState w = windowsOnSameDisplay.get(i);
1373 final int wSublayer = w.mSubLayer;
1374 if (wSublayer >= largestSublayer) {
1375 largestSublayer = wSublayer;
1376 windowWithLargestSublayer = w;
1377 }
1378 if (sublayer < 0) {
1379 // For negative sublayers, we go below all windows in the same sublayer.
1380 if (wSublayer >= sublayer) {
1381 addWindowToListBefore(win, wSublayer >= 0 ? parentWindow : w);
1382 break;
1383 }
1384 } else {
1385 // For positive sublayers, we go above all windows in the same sublayer.
1386 if (wSublayer > sublayer) {
1387 addWindowToListBefore(win, w);
1388 break;
1389 }
1390 }
1391 }
1392 if (i >= wCount) {
1393 if (sublayer < 0) {
1394 addWindowToListBefore(win, parentWindow);
1395 } else {
1396 addWindowToListAfter(win,
1397 largestSublayer >= 0 ? windowWithLargestSublayer : parentWindow);
1398 }
1399 }
1400 }
1401
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001402 /** Updates the layer assignment of windows on this display. */
1403 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001404 mLayersController.assignWindowLayers(mWindows.getReadOnly());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001405 if (setLayoutNeeded) {
1406 setLayoutNeeded();
1407 }
1408 }
1409
Wale Ogunwale0303c572016-10-20 10:16:29 -07001410 void adjustWallpaperWindows() {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001411 if (mWallpaperController.adjustWallpaperWindows(mWindows.getReadOnly())) {
Wale Ogunwale0303c572016-10-20 10:16:29 -07001412 assignWindowLayers(true /*setLayoutNeeded*/);
1413 }
1414 }
1415
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001416 /**
1417 * Z-orders the display window list so that:
1418 * <ul>
1419 * <li>Any windows that are currently below the wallpaper window stay below the wallpaper
1420 * window.
1421 * <li>Exiting application windows are at the bottom, but above the wallpaper window.
1422 * <li>All other application windows are above the exiting application windows and ordered based
1423 * on the ordering of their stacks and tasks on the display.
1424 * <li>Non-application windows are at the very top.
1425 * </ul>
1426 * <p>
1427 * NOTE: This isn't a complete picture of what the user see. Further manipulation of the window
1428 * surface layering is done in {@link WindowLayersController}.
1429 */
1430 void rebuildAppWindowList() {
1431 int count = mWindows.size();
1432 int i;
1433 int lastBelow = -1;
1434 int numRemoved = 0;
1435
1436 if (mRebuildTmp.length < count) {
1437 mRebuildTmp = new WindowState[count + 10];
1438 }
1439
1440 // First remove all existing app windows.
1441 i = 0;
1442 while (i < count) {
1443 final WindowState w = mWindows.get(i);
1444 if (w.mAppToken != null) {
1445 final WindowState win = mWindows.remove(i);
1446 win.mRebuilding = true;
1447 mRebuildTmp[numRemoved] = win;
1448 mService.mWindowsChanged = true;
1449 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG_WM, "Rebuild removing window: " + win);
1450 count--;
1451 numRemoved++;
1452 continue;
1453 } else if (lastBelow == i-1) {
1454 if (w.mAttrs.type == TYPE_WALLPAPER) {
1455 lastBelow = i;
1456 }
1457 }
1458 i++;
1459 }
1460
1461 // Keep whatever windows were below the app windows still below, by skipping them.
1462 lastBelow++;
1463 i = lastBelow;
1464
1465 // First add all of the exiting app tokens... these are no longer in the main app list,
1466 // but still have windows shown. We put them in the back because now that the animation is
1467 // over we no longer will care about them.
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001468 final int numStacks = mTaskStackContainers.size();
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001469 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001470 AppTokenList exitingAppTokens = mTaskStackContainers.get(stackNdx).mExitingAppTokens;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001471 int NT = exitingAppTokens.size();
1472 for (int j = 0; j < NT; j++) {
Wale Ogunwale360a8bc2016-10-10 13:25:26 -07001473 i = exitingAppTokens.get(j).rebuildWindowListUnchecked(i);
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001474 }
1475 }
1476
1477 // And add in the still active app tokens in Z order.
1478 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001479 i = mTaskStackContainers.get(stackNdx).rebuildWindowList(i);
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001480 }
1481
1482 i -= lastBelow;
1483 if (i != numRemoved) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001484 setLayoutNeeded();
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001485 Slog.w(TAG_WM, "On display=" + mDisplayId + " Rebuild removed " + numRemoved
1486 + " windows but added " + i + " rebuildAppWindowListLocked() "
1487 + " callers=" + Debug.getCallers(10));
1488 for (i = 0; i < numRemoved; i++) {
1489 WindowState ws = mRebuildTmp[i];
1490 if (ws.mRebuilding) {
1491 StringWriter sw = new StringWriter();
1492 PrintWriter pw = new FastPrintWriter(sw, false, 1024);
1493 ws.dump(pw, "", true);
1494 pw.flush();
1495 Slog.w(TAG_WM, "This window was lost: " + ws);
1496 Slog.w(TAG_WM, sw.toString());
1497 ws.mWinAnimator.destroySurfaceLocked();
1498 }
1499 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001500 Slog.w(TAG_WM, "Current window hierarchy:");
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001501 dumpChildrenNames();
1502 Slog.w(TAG_WM, "Final window list:");
1503 dumpWindows();
1504 }
1505 Arrays.fill(mRebuildTmp, null);
1506 }
1507
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001508 /** Rebuilds the display's window list and does a relayout if something changed. */
1509 void rebuildAppWindowsAndLayoutIfNeeded() {
1510 mTmpWindows.clear();
1511 mTmpWindows.addAll(mWindows);
1512
1513 rebuildAppWindowList();
1514
1515 // Set displayContent.mLayoutNeeded if window order changed.
1516 final int tmpSize = mTmpWindows.size();
1517 final int winSize = mWindows.size();
1518 int tmpNdx = 0, winNdx = 0;
1519 while (tmpNdx < tmpSize && winNdx < winSize) {
1520 // Skip over all exiting windows, they've been moved out of order.
1521 WindowState tmp;
1522 do {
1523 tmp = mTmpWindows.get(tmpNdx++);
1524 } while (tmpNdx < tmpSize && tmp.mAppToken != null && tmp.mAppToken.mIsExiting);
1525
1526 WindowState win;
1527 do {
1528 win = mWindows.get(winNdx++);
1529 } while (winNdx < winSize && win.mAppToken != null && win.mAppToken.mIsExiting);
1530
1531 if (tmp != win) {
1532 // Window order changed.
1533 setLayoutNeeded();
1534 break;
1535 }
1536 }
1537 if (tmpNdx != winNdx) {
1538 // One list was different from the other.
1539 setLayoutNeeded();
1540 }
1541 mTmpWindows.clear();
1542
1543 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
1544 false /*updateInputWindows*/)) {
1545 assignWindowLayers(false /* setLayoutNeeded */);
1546 }
1547
1548 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
1549 mService.mWindowPlacerLocked.performSurfacePlacement();
1550 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
1551 }
1552
1553 void updateInputWindows(InputMonitor inputMonitor, WindowState inputFocus, boolean inDrag) {
1554 final InputConsumerImpl navInputConsumer =
1555 mService.mInputMonitor.getInputConsumer(INPUT_CONSUMER_NAVIGATION, mDisplayId);
1556 final InputConsumerImpl pipInputConsumer =
1557 mService.mInputMonitor.getInputConsumer(INPUT_CONSUMER_PIP, mDisplayId);
1558 final InputConsumerImpl wallpaperInputConsumer =
1559 mService.mInputMonitor.getInputConsumer(INPUT_CONSUMER_WALLPAPER, mDisplayId);
1560 boolean addInputConsumerHandle = navInputConsumer != null;
1561 boolean addPipInputConsumerHandle = pipInputConsumer != null;
1562 boolean addWallpaperInputConsumerHandle = wallpaperInputConsumer != null;
1563 final Rect pipTouchableBounds = addPipInputConsumerHandle ? new Rect() : null;
1564 boolean disableWallpaperTouchEvents = false;
1565
1566 for (int winNdx = mWindows.size() - 1; winNdx >= 0; --winNdx) {
1567 final WindowState child = mWindows.get(winNdx);
1568 final InputChannel inputChannel = child.mInputChannel;
1569 final InputWindowHandle inputWindowHandle = child.mInputWindowHandle;
1570 if (inputChannel == null || inputWindowHandle == null || child.mRemoved
1571 || child.isAdjustedForMinimizedDock()) {
1572 // Skip this window because it cannot possibly receive input.
1573 continue;
1574 }
1575
1576 if (addPipInputConsumerHandle
1577 && child.getStackId() == PINNED_STACK_ID
1578 && inputWindowHandle.layer <= pipInputConsumer.mWindowHandle.layer) {
1579 // Update the bounds of the Pip input consumer to match the Pinned stack
1580 child.getStack().getBounds(pipTouchableBounds);
1581 pipInputConsumer.mWindowHandle.touchableRegion.set(pipTouchableBounds);
1582 inputMonitor.addInputWindowHandle(pipInputConsumer.mWindowHandle);
1583 addPipInputConsumerHandle = false;
1584 }
1585
1586 if (addInputConsumerHandle
1587 && inputWindowHandle.layer <= navInputConsumer.mWindowHandle.layer) {
1588 inputMonitor.addInputWindowHandle(navInputConsumer.mWindowHandle);
1589 addInputConsumerHandle = false;
1590 }
1591
1592 if (addWallpaperInputConsumerHandle) {
1593 if (child.mAttrs.type == TYPE_WALLPAPER && child.isVisibleLw()) {
1594 // Add the wallpaper input consumer above the first visible wallpaper.
1595 inputMonitor.addInputWindowHandle(wallpaperInputConsumer.mWindowHandle);
1596 addWallpaperInputConsumerHandle = false;
1597 }
1598 }
1599
1600 final int flags = child.mAttrs.flags;
1601 final int privateFlags = child.mAttrs.privateFlags;
1602 final int type = child.mAttrs.type;
1603
1604 final boolean hasFocus = child == inputFocus;
1605 final boolean isVisible = child.isVisibleLw();
1606 if ((privateFlags & PRIVATE_FLAG_DISABLE_WALLPAPER_TOUCH_EVENTS) != 0) {
1607 disableWallpaperTouchEvents = true;
1608 }
1609 final boolean hasWallpaper = mWallpaperController.isWallpaperTarget(child)
1610 && (privateFlags & PRIVATE_FLAG_KEYGUARD) == 0
1611 && !disableWallpaperTouchEvents;
1612
1613 // If there's a drag in progress and 'child' is a potential drop target,
1614 // make sure it's been told about the drag
1615 if (inDrag && isVisible && isDefaultDisplay) {
1616 mService.mDragState.sendDragStartedIfNeededLw(child);
1617 }
1618
1619 inputMonitor.addInputWindowHandle(
1620 inputWindowHandle, child, flags, type, isVisible, hasFocus, hasWallpaper);
1621 }
1622
1623 if (addWallpaperInputConsumerHandle) {
1624 // No visible wallpaper found, add the wallpaper input consumer at the end.
1625 inputMonitor.addInputWindowHandle(wallpaperInputConsumer.mWindowHandle);
1626 }
1627 }
1628
1629 /** Returns true if a leaked surface was destroyed */
1630 boolean destroyLeakedSurfaces() {
1631 boolean leakedSurface = false;
1632 final int numWindows = mWindows.size();
1633 for (int winNdx = 0; winNdx < numWindows; ++winNdx) {
1634 final WindowState ws = mWindows.get(winNdx);
1635 final WindowStateAnimator wsa = ws.mWinAnimator;
1636 if (wsa.mSurfaceController == null) {
1637 continue;
1638 }
1639 if (!mService.mSessions.contains(wsa.mSession)) {
1640 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
1641 + ws + " surface=" + wsa.mSurfaceController
1642 + " token=" + ws.mToken
1643 + " pid=" + ws.mSession.mPid
1644 + " uid=" + ws.mSession.mUid);
1645 wsa.destroySurface();
1646 mService.mForceRemoves.add(ws);
1647 leakedSurface = true;
1648 } else if (ws.mAppToken != null && ws.mAppToken.clientHidden) {
1649 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
1650 + ws + " surface=" + wsa.mSurfaceController
1651 + " token=" + ws.mAppToken
1652 + " saved=" + ws.hasSavedSurface());
1653 if (SHOW_TRANSACTIONS) logSurface(ws, "LEAK DESTROY", false);
1654 wsa.destroySurface();
1655 leakedSurface = true;
1656 }
1657 }
1658
1659 return leakedSurface;
1660 }
1661
Wale Ogunwaleec731152016-09-08 20:18:57 -07001662 /** Return the list of Windows on this display associated with the input token. */
1663 WindowList getTokenWindowsOnDisplay(WindowToken token) {
1664 final WindowList windowList = new WindowList();
1665 final int count = mWindows.size();
1666 for (int i = 0; i < count; i++) {
1667 final WindowState win = mWindows.get(i);
1668 if (win.mToken == token) {
1669 windowList.add(win);
1670 }
1671 }
1672 return windowList;
1673 }
1674
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001675 private void reAddToWindowList(WindowState win) {
1676 win.mToken.addWindow(win);
1677 // This is a hack to get all of the child windows added as well at the right position. Child
1678 // windows should be rare and this case should be rare, so it shouldn't be that big a deal.
1679 int wpos = mWindows.indexOf(win);
1680 if (wpos >= 0) {
1681 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG_WM, "ReAdd removing from " + wpos + ": " + win);
1682 mWindows.remove(wpos);
1683 mService.mWindowsChanged = true;
1684 win.reAddWindow(wpos);
1685 }
1686 }
1687
1688 void moveInputMethodDialogs(int pos) {
1689 ArrayList<WindowState> dialogs = mService.mInputMethodDialogs;
1690
1691 final int N = dialogs.size();
1692 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Removing " + N + " dialogs w/pos=" + pos);
1693 for (int i = 0; i < N; i++) {
1694 pos = removeWindowAndChildrenFromWindowList(dialogs.get(i), pos);
1695 }
1696 if (DEBUG_INPUT_METHOD) {
1697 Slog.v(TAG_WM, "Window list w/pos=" + pos);
1698 logWindowList(mWindows, " ");
1699 }
1700
1701 WindowState ime = mService.mInputMethodWindow;
1702 if (pos >= 0) {
1703 // Skip windows owned by the input method.
1704 if (ime != null) {
1705 while (pos < mWindows.size()) {
1706 WindowState wp = mWindows.get(pos);
1707 if (wp == ime || wp.getParentWindow() == ime) {
1708 pos++;
1709 continue;
1710 }
1711 break;
1712 }
1713 }
1714 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Adding " + N + " dialogs at pos=" + pos);
1715 for (int i=0; i<N; i++) {
1716 WindowState win = dialogs.get(i);
1717 pos = win.reAddWindow(pos);
1718 }
1719 if (DEBUG_INPUT_METHOD) {
1720 Slog.v(TAG_WM, "Final window list:");
1721 logWindowList(mWindows, " ");
1722 }
1723 return;
1724 }
1725 for (int i=0; i<N; i++) {
1726 WindowState win = dialogs.get(i);
1727 reAddToWindowList(win);
1728 if (DEBUG_INPUT_METHOD) {
1729 Slog.v(TAG_WM, "No IM target, final list:");
1730 logWindowList(mWindows, " ");
1731 }
1732 }
1733 }
1734
1735 boolean moveInputMethodWindowsIfNeeded(boolean needAssignLayers) {
1736 final WindowState imWin = mService.mInputMethodWindow;
1737 final int DN = mService.mInputMethodDialogs.size();
1738 if (imWin == null && DN == 0) {
1739 return false;
1740 }
1741
1742 // TODO(multidisplay): IMEs are only supported on the default display.
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001743 int imPos = findDesiredInputMethodWindowIndex(true);
1744 if (imPos >= 0) {
1745 // In this case, the input method windows are to be placed
1746 // immediately above the window they are targeting.
1747
1748 // First check to see if the input method windows are already
1749 // located here, and contiguous.
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001750 final int N = mWindows.size();
1751 final WindowState firstImWin = imPos < N ? mWindows.get(imPos) : null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001752
1753 // Figure out the actual input method window that should be
1754 // at the bottom of their stack.
1755 WindowState baseImWin = imWin != null ? imWin : mService.mInputMethodDialogs.get(0);
1756 final WindowState cw = baseImWin.getBottomChild();
1757 if (cw != null && cw.mSubLayer < 0) {
1758 baseImWin = cw;
1759 }
1760
1761 if (firstImWin == baseImWin) {
1762 // The windows haven't moved... but are they still contiguous?
1763 // First find the top IM window.
1764 int pos = imPos+1;
1765 while (pos < N) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001766 if (!(mWindows.get(pos)).mIsImWindow) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001767 break;
1768 }
1769 pos++;
1770 }
1771 pos++;
1772 // Now there should be no more input method windows above.
1773 while (pos < N) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001774 if ((mWindows.get(pos)).mIsImWindow) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001775 break;
1776 }
1777 pos++;
1778 }
1779 if (pos >= N) {
1780 return false;
1781 }
1782 }
1783
1784 if (imWin != null) {
1785 if (DEBUG_INPUT_METHOD) {
1786 Slog.v(TAG_WM, "Moving IM from " + imPos);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001787 logWindowList(mWindows, " ");
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001788 }
1789 imPos = removeWindowAndChildrenFromWindowList(imWin, imPos);
1790 if (DEBUG_INPUT_METHOD) {
1791 Slog.v(TAG_WM, "List after removing with new pos " + imPos + ":");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001792 logWindowList(mWindows, " ");
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001793 }
1794 imWin.reAddWindow(imPos);
1795 if (DEBUG_INPUT_METHOD) {
1796 Slog.v(TAG_WM, "List after moving IM to " + imPos + ":");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001797 logWindowList(mWindows, " ");
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001798 }
1799 if (DN > 0) moveInputMethodDialogs(imPos+1);
1800 } else {
1801 moveInputMethodDialogs(imPos);
1802 }
1803
1804 } else {
1805 // In this case, the input method windows go in a fixed layer,
1806 // because they aren't currently associated with a focus window.
1807
1808 if (imWin != null) {
1809 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Moving IM from " + imPos);
1810 removeWindowAndChildrenFromWindowList(imWin, 0);
1811 reAddToWindowList(imWin);
1812 if (DEBUG_INPUT_METHOD) {
1813 Slog.v(TAG_WM, "List with no IM target:");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001814 logWindowList(mWindows, " ");
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001815 }
1816 if (DN > 0) moveInputMethodDialogs(-1);
1817 } else {
1818 moveInputMethodDialogs(-1);
1819 }
1820
1821 }
1822
1823 if (needAssignLayers) {
1824 assignWindowLayers(false /* setLayoutNeeded */);
1825 }
1826
1827 return true;
1828 }
1829
1830 /**
1831 * Dig through the WindowStates and find the one that the Input Method will target.
1832 * @param willMove
1833 * @return The index+1 in mWindows of the discovered target.
1834 */
1835 int findDesiredInputMethodWindowIndex(boolean willMove) {
1836 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
1837 // same display. Or even when the current IME/target are not on the same screen as the next
1838 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001839 WindowState w = null;
1840 int i;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001841 for (i = mWindows.size() - 1; i >= 0; --i) {
1842 final WindowState win = mWindows.get(i);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001843
1844 if (DEBUG_INPUT_METHOD && willMove) Slog.i(TAG_WM, "Checking window @" + i
1845 + " " + win + " fl=0x" + Integer.toHexString(win.mAttrs.flags));
1846 if (canBeImeTarget(win)) {
1847 w = win;
1848 //Slog.i(TAG_WM, "Putting input method here!");
1849
1850 // Yet more tricksyness! If this window is a "starting" window, we do actually want
1851 // to be on top of it, but it is not -really- where input will go. So if the caller
1852 // is not actually looking to move the IME, look down below for a real window to
1853 // target...
1854 if (!willMove && w.mAttrs.type == TYPE_APPLICATION_STARTING && i > 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001855 final WindowState wb = mWindows.get(i-1);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001856 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
1857 i--;
1858 w = wb;
1859 }
1860 }
1861 break;
1862 }
1863 }
1864
1865 // Now w is either mWindows[0] or an IME (or null if mWindows is empty).
1866
1867 if (DEBUG_INPUT_METHOD && willMove) Slog.v(TAG_WM, "Proposed new IME target: " + w);
1868
1869 // Now, a special case -- if the last target's window is in the process of exiting, and is
1870 // above the new target, keep on the last target to avoid flicker. Consider for example a
1871 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
1872 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
1873 // scrim.
1874 final WindowState curTarget = mService.mInputMethodTarget;
1875 if (curTarget != null
1876 && curTarget.isDisplayedLw()
1877 && curTarget.isClosing()
1878 && (w == null || curTarget.mWinAnimator.mAnimLayer > w.mWinAnimator.mAnimLayer)) {
1879 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001880 return mWindows.indexOf(curTarget) + 1;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001881 }
1882
1883 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target="
1884 + w + " willMove=" + willMove);
1885
1886 if (willMove && w != null) {
1887 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
1888 if (token != null) {
1889
1890 // Now some fun for dealing with window animations that modify the Z order. We need
1891 // to look at all windows below the current target that are in this app, finding the
1892 // highest visible one in layering.
1893 WindowState highestTarget = null;
1894 int highestPos = 0;
1895 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001896 WindowList curWindows = token.getDisplayContent().mWindows;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001897 int pos = curWindows.indexOf(curTarget);
1898 while (pos >= 0) {
1899 WindowState win = curWindows.get(pos);
1900 if (win.mAppToken != token) {
1901 break;
1902 }
1903 if (!win.mRemoved) {
1904 if (highestTarget == null || win.mWinAnimator.mAnimLayer >
1905 highestTarget.mWinAnimator.mAnimLayer) {
1906 highestTarget = win;
1907 highestPos = pos;
1908 }
1909 }
1910 pos--;
1911 }
1912 }
1913
1914 if (highestTarget != null) {
1915 final AppTransition appTransition = mService.mAppTransition;
1916 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
1917 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
1918 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
1919 + " new layer=" + w.mWinAnimator.mAnimLayer);
1920
1921 if (appTransition.isTransitionSet()) {
1922 // If we are currently setting up for an animation, hold everything until we
1923 // can find out what will happen.
1924 mService.mInputMethodTargetWaitingAnim = true;
1925 mService.mInputMethodTarget = highestTarget;
1926 return highestPos + 1;
1927 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
1928 highestTarget.mWinAnimator.mAnimLayer > w.mWinAnimator.mAnimLayer) {
1929 // If the window we are currently targeting is involved with an animation,
1930 // and it is on top of the next target we will be over, then hold off on
1931 // moving until that is done.
1932 mService.mInputMethodTargetWaitingAnim = true;
1933 mService.mInputMethodTarget = highestTarget;
1934 return highestPos + 1;
1935 }
1936 }
1937 }
1938 }
1939
1940 //Slog.i(TAG_WM, "Placing input method @" + (i+1));
1941 if (w != null) {
1942 if (willMove) {
1943 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
1944 + w + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
1945 mService.mInputMethodTarget = w;
1946 mService.mInputMethodTargetWaitingAnim = false;
1947 if (w.mAppToken != null) {
1948 setInputMethodAnimLayerAdjustment(
1949 w.mAppToken.mAppAnimator.animLayerAdjustment);
1950 } else {
1951 setInputMethodAnimLayerAdjustment(0);
1952 }
1953 }
1954
1955 // If the docked divider is visible, we still need to go through this whole excercise to
1956 // find the appropriate input method target (used for animations and dialog
1957 // adjustments), but for purposes of Z ordering we simply wish to place it above the
1958 // docked divider. Unless it is already above the divider.
1959 final WindowState dockedDivider = mDividerControllerLocked.getWindow();
1960 if (dockedDivider != null && dockedDivider.isVisibleLw()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001961 int dividerIndex = mWindows.indexOf(dockedDivider);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001962 if (dividerIndex > 0 && dividerIndex > i) {
1963 return dividerIndex + 1;
1964 }
1965 }
1966 return i+1;
1967 }
1968 if (willMove) {
1969 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
1970 + " to null." + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
1971 mService.mInputMethodTarget = null;
1972 setInputMethodAnimLayerAdjustment(0);
1973 }
1974 return -1;
1975 }
1976
1977 private static boolean canBeImeTarget(WindowState w) {
1978 final int fl = w.mAttrs.flags & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
1979 final int type = w.mAttrs.type;
1980
1981 if (fl != 0 && fl != (FLAG_NOT_FOCUSABLE | FLAG_ALT_FOCUSABLE_IM)
1982 && type != TYPE_APPLICATION_STARTING) {
1983 return false;
1984 }
1985
1986 if (DEBUG_INPUT_METHOD) {
1987 Slog.i(TAG_WM, "isVisibleOrAdding " + w + ": " + w.isVisibleOrAdding());
1988 if (!w.isVisibleOrAdding()) {
1989 Slog.i(TAG_WM, " mSurfaceController=" + w.mWinAnimator.mSurfaceController
1990 + " relayoutCalled=" + w.mRelayoutCalled
1991 + " viewVis=" + w.mViewVisibility
1992 + " policyVis=" + w.mPolicyVisibility
1993 + " policyVisAfterAnim=" + w.mPolicyVisibilityAfterAnim
1994 + " parentHidden=" + w.isParentWindowHidden()
1995 + " exiting=" + w.mAnimatingExit + " destroying=" + w.mDestroying);
1996 if (w.mAppToken != null) {
1997 Slog.i(TAG_WM, " mAppToken.hiddenRequested=" + w.mAppToken.hiddenRequested);
1998 }
1999 }
2000 }
2001 return w.isVisibleOrAdding();
2002 }
2003
2004 private void logWindowList(final WindowList windows, String prefix) {
2005 int N = windows.size();
2006 while (N > 0) {
2007 N--;
2008 Slog.v(TAG_WM, prefix + "#" + N + ": " + windows.get(N));
2009 }
2010 }
2011
2012 boolean getNeedsMenu(WindowState win, WindowManagerPolicy.WindowState bottom) {
2013 int index = -1;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002014 while (true) {
2015 if (win.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2016 return win.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2017 }
2018 // If we reached the bottom of the range of windows we are considering,
2019 // assume no menu is needed.
2020 if (win == bottom) {
2021 return false;
2022 }
2023 // The current window hasn't specified whether menu key is needed; look behind it.
2024 // First, we may need to determine the starting position.
2025 if (index < 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002026 index = mWindows.indexOf(win);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002027 }
2028 index--;
2029 if (index < 0) {
2030 return false;
2031 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002032 win = mWindows.get(index);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002033 }
2034 }
2035
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002036 void setLayoutNeeded() {
2037 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2038 mLayoutNeeded = true;
2039 }
2040
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002041 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002042 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2043 mLayoutNeeded = false;
2044 }
2045
2046 boolean isLayoutNeeded() {
2047 return mLayoutNeeded;
2048 }
2049
Wale Ogunwale07bcab72016-10-14 15:30:09 -07002050 private void addAppWindowExisting(WindowState win, WindowList tokenWindowList) {
Wale Ogunwaleec731152016-09-08 20:18:57 -07002051
Wale Ogunwaleec731152016-09-08 20:18:57 -07002052 // If this application has existing windows, we simply place the new window on top of
2053 // them... but keep the starting window on top.
2054 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
2055 // Base windows go behind everything else.
2056 final WindowState lowestWindow = tokenWindowList.get(0);
2057 addWindowToListBefore(win, lowestWindow);
Wale Ogunwaleec731152016-09-08 20:18:57 -07002058 } else {
2059 final AppWindowToken atoken = win.mAppToken;
2060 final int windowListPos = tokenWindowList.size();
2061 final WindowState lastWindow = tokenWindowList.get(windowListPos - 1);
2062 if (atoken != null && lastWindow == atoken.startingWindow) {
2063 addWindowToListBefore(win, lastWindow);
Wale Ogunwaleec731152016-09-08 20:18:57 -07002064 } else {
2065 int newIdx = findIdxBasedOnAppTokens(win);
2066 // There is a window above this one associated with the same apptoken note that the
2067 // window could be a floating window that was created later or a window at the top
2068 // of the list of windows associated with this token.
2069 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
2070 "not Base app: Adding window " + win + " at " + (newIdx + 1) + " of "
2071 + mWindows.size());
2072 mWindows.add(newIdx + 1, win);
Wale Ogunwaleec731152016-09-08 20:18:57 -07002073 mService.mWindowsChanged = true;
2074 }
2075 }
Wale Ogunwaleec731152016-09-08 20:18:57 -07002076 }
2077
2078 /** Places the first input window after the second input window in the window list. */
2079 private void addWindowToListAfter(WindowState first, WindowState second) {
2080 final int i = mWindows.indexOf(second);
2081 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
2082 "Adding window " + this + " at " + (i + 1) + " of " + mWindows.size()
2083 + " (after " + second + ")");
2084 mWindows.add(i + 1, first);
2085 mService.mWindowsChanged = true;
2086 }
2087
2088 /** Places the first input window before the second input window in the window list. */
2089 private void addWindowToListBefore(WindowState first, WindowState second) {
2090 int i = mWindows.indexOf(second);
2091 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
2092 "Adding window " + this + " at " + i + " of " + mWindows.size()
2093 + " (before " + second + ")");
2094 if (i < 0) {
2095 Slog.w(TAG_WM, "addWindowToListBefore: Unable to find " + second + " in " + mWindows);
2096 i = 0;
2097 }
2098 mWindows.add(i, first);
2099 mService.mWindowsChanged = true;
2100 }
2101
2102 /**
2103 * This method finds out the index of a window that has the same app token as win. used for z
2104 * ordering the windows in mWindows
2105 */
2106 private int findIdxBasedOnAppTokens(WindowState win) {
2107 for(int j = mWindows.size() - 1; j >= 0; j--) {
2108 final WindowState wentry = mWindows.get(j);
2109 if(wentry.mAppToken == win.mAppToken) {
2110 return j;
2111 }
2112 }
2113 return -1;
2114 }
2115
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002116 private void dumpChildrenNames() {
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07002117 StringBuilder output = new StringBuilder();
2118 dumpChildrenNames(output, " ");
2119 Slog.v(TAG_WM, output.toString());
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002120 }
2121
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002122 private void dumpWindows() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002123 Slog.v(TAG_WM, " Display #" + mDisplayId);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002124 for (int winNdx = mWindows.size() - 1; winNdx >= 0; --winNdx) {
2125 Slog.v(TAG_WM, " #" + winNdx + ": " + mWindows.get(winNdx));
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002126 }
2127 }
2128
Wale Ogunwale02319a62016-09-26 15:21:22 -07002129 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2130 if (mTokenMap.isEmpty()) {
2131 return;
2132 }
2133 pw.println(" Display #" + mDisplayId);
2134 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2135 while (it.hasNext()) {
2136 final WindowToken token = it.next();
2137 pw.print(" ");
2138 pw.print(token);
2139 if (dumpAll) {
2140 pw.println(':');
2141 token.dump(pw, " ");
2142 } else {
2143 pw.println();
2144 }
2145 }
2146 }
2147
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002148 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
2149 final int count = mWindows.size();
2150 for (int j = 0; j < count; j++) {
2151 final WindowStateAnimator wAnim = mWindows.get(j).mWinAnimator;
2152 pw.println(subPrefix + "Window #" + j + ": " + wAnim);
2153 }
2154 }
2155
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002156 void enableSurfaceTrace(FileDescriptor fd) {
2157 for (int i = mWindows.size() - 1; i >= 0; i--) {
2158 final WindowState win = mWindows.get(i);
2159 win.mWinAnimator.enableSurfaceTrace(fd);
2160 }
2161 }
2162
2163 void disableSurfaceTrace() {
2164 for (int i = mWindows.size() - 1; i >= 0; i--) {
2165 final WindowState win = mWindows.get(i);
2166 win.mWinAnimator.disableSurfaceTrace();
2167 }
2168 }
2169
Jorim Jaggife762342016-10-13 14:33:27 +02002170 /**
2171 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2172 */
2173 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2174 final WindowManagerPolicy policy = mService.mPolicy;
2175 for (int i = mWindows.size() - 1; i >= 0; i--) {
2176 final WindowState window = mWindows.get(i);
2177 if (window.mAppToken == null && policy.canBeHiddenByKeyguardLw(window)) {
2178 window.mWinAnimator.setAnimation(
2179 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2180 }
2181 }
2182 }
2183
Wale Ogunwale494009b82016-10-21 09:01:38 -07002184 boolean checkWaitingForWindows() {
2185
2186 boolean haveBootMsg = false;
2187 boolean haveApp = false;
2188 // if the wallpaper service is disabled on the device, we're never going to have
2189 // wallpaper, don't bother waiting for it
2190 boolean haveWallpaper = false;
2191 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2192 com.android.internal.R.bool.config_enableWallpaperService)
2193 && !mService.mOnlyCore;
2194 boolean haveKeyguard = true;
2195 final int count = mWindows.size();
2196 for (int i = 0; i < count; i++) {
2197 final WindowState w = mWindows.get(i);
2198 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2199 return true;
2200 }
2201 if (w.isDrawnLw()) {
2202 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
2203 haveBootMsg = true;
2204 } else if (w.mAttrs.type == TYPE_APPLICATION
2205 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
2206 haveApp = true;
2207 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
2208 haveWallpaper = true;
2209 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
2210 haveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
2211 }
2212 }
2213 }
2214
2215 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2216 "******** booted=" + mService.mSystemBooted
2217 + " msg=" + mService.mShowingBootMessages
2218 + " haveBoot=" + haveBootMsg + " haveApp=" + haveApp
2219 + " haveWall=" + haveWallpaper + " wallEnabled=" + wallpaperEnabled
2220 + " haveKeyguard=" + haveKeyguard);
2221
2222 // If we are turning on the screen to show the boot message, don't do it until the boot
2223 // message is actually displayed.
2224 if (!mService.mSystemBooted && !haveBootMsg) {
2225 return true;
2226 }
2227
2228 // If we are turning on the screen after the boot is completed normally, don't do so until
2229 // we have the application and wallpaper.
2230 if (mService.mSystemBooted && ((!haveApp && !haveKeyguard) ||
2231 (wallpaperEnabled && !haveWallpaper))) {
2232 return true;
2233 }
2234
2235 return false;
2236 }
2237
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002238 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale0303c572016-10-20 10:16:29 -07002239 final WallpaperController wallpaperController = mWallpaperController;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002240 for (int i = mWindows.size() - 1; i >= 0; i--) {
2241 WindowState win = mWindows.get(i);
2242 WindowStateAnimator winAnimator = win.mWinAnimator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002243 if (winAnimator.hasSurface()) {
2244 final boolean wasAnimating = winAnimator.mWasAnimating;
2245 final boolean nowAnimating = winAnimator.stepAnimationLocked(animator.mCurrentTime);
2246 winAnimator.mWasAnimating = nowAnimating;
2247 animator.orAnimating(nowAnimating);
2248
2249 if (DEBUG_WALLPAPER) Slog.v(TAG,
2250 win + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
2251
2252 if (wasAnimating && !winAnimator.mAnimating
2253 && wallpaperController.isWallpaperTarget(win)) {
2254 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2255 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
2256 if (DEBUG_LAYOUT_REPEATS) {
2257 mService.mWindowPlacerLocked.debugLayoutRepeats(
2258 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
2259 }
2260 }
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002261 }
2262
2263 final AppWindowToken atoken = win.mAppToken;
2264 if (winAnimator.mDrawState == READY_TO_SHOW) {
2265 if (atoken == null || atoken.allDrawn) {
2266 if (win.performShowLocked()) {
2267 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
2268 if (DEBUG_LAYOUT_REPEATS) {
2269 mService.mWindowPlacerLocked.debugLayoutRepeats(
2270 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
2271 }
2272 }
2273 }
2274 }
2275 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
2276 if (appAnimator != null && appAnimator.thumbnail != null) {
2277 if (appAnimator.thumbnailTransactionSeq != animator.mAnimTransactionSequence) {
2278 appAnimator.thumbnailTransactionSeq = animator.mAnimTransactionSequence;
2279 appAnimator.thumbnailLayer = 0;
2280 }
2281 if (appAnimator.thumbnailLayer < winAnimator.mAnimLayer) {
2282 appAnimator.thumbnailLayer = winAnimator.mAnimLayer;
2283 }
2284 }
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002285 } // end forall windows
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002286 }
2287
2288 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002289 resetAnimationBackgroundAnimator();
2290
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002291 final WindowList windows = mWindows;
2292 WindowState detachedWallpaper = null;
2293
2294 for (int i = windows.size() - 1; i >= 0; i--) {
2295 final WindowState win = windows.get(i);
2296 final WindowStateAnimator winAnimator = win.mWinAnimator;
2297 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
2298 continue;
2299 }
2300
2301 final int flags = win.mAttrs.flags;
2302
2303 // If this window is animating, make a note that we have an animating window and take
2304 // care of a request to run a detached wallpaper animation.
2305 if (winAnimator.mAnimating) {
2306 if (winAnimator.mAnimation != null) {
2307 if ((flags & FLAG_SHOW_WALLPAPER) != 0
2308 && winAnimator.mAnimation.getDetachWallpaper()) {
2309 detachedWallpaper = win;
2310 }
2311 final int color = winAnimator.mAnimation.getBackgroundColor();
2312 if (color != 0) {
2313 final TaskStack stack = win.getStack();
2314 if (stack != null) {
2315 stack.setAnimationBackground(winAnimator, color);
2316 }
2317 }
2318 }
2319 animator.setAnimating(true);
2320 }
2321
2322 // If this window's app token is running a detached wallpaper animation, make a note so
2323 // we can ensure the wallpaper is displayed behind it.
2324 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
2325 if (appAnimator != null && appAnimator.animation != null
2326 && appAnimator.animating) {
2327 if ((flags & FLAG_SHOW_WALLPAPER) != 0
2328 && appAnimator.animation.getDetachWallpaper()) {
2329 detachedWallpaper = win;
2330 }
2331
2332 final int color = appAnimator.animation.getBackgroundColor();
2333 if (color != 0) {
2334 final TaskStack stack = win.getStack();
2335 if (stack != null) {
2336 stack.setAnimationBackground(winAnimator, color);
2337 }
2338 }
2339 }
2340 } // end forall windows
2341
2342 if (animator.mWindowDetachedWallpaper != detachedWallpaper) {
2343 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
2344 + animator.mWindowDetachedWallpaper + " to " + detachedWallpaper);
2345 animator.mWindowDetachedWallpaper = detachedWallpaper;
2346 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2347 }
2348 }
2349
2350 void prepareWindowSurfaces() {
2351 final int count = mWindows.size();
2352 for (int j = 0; j < count; j++) {
2353 mWindows.get(j).mWinAnimator.prepareSurfaceLocked(true);
2354 }
2355 }
2356
Wale Ogunwale494009b82016-10-21 09:01:38 -07002357 boolean inputMethodClientHasFocus(IInputMethodClient client) {
2358 // The focus for the client is the window immediately below where we would place the input
2359 // method window.
2360 int idx = findDesiredInputMethodWindowIndex(false);
2361 if (idx <= 0) {
2362 return false;
2363 }
2364
2365 WindowState imFocus = mWindows.get(idx - 1);
2366 if (DEBUG_INPUT_METHOD) {
2367 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2368 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2369 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
2370 }
2371
2372 if (imFocus == null) {
2373 return false;
2374 }
2375
2376 // This may be a starting window, in which case we still want to count it as okay.
2377 if (imFocus.mAttrs.type == TYPE_APPLICATION_STARTING && imFocus.mAppToken != null) {
2378 // The client has definitely started, so it really should have a window in this app
2379 // token. Let's look for it.
2380 final WindowState w = imFocus.mAppToken.getFirstNonStartingWindow();
2381 if (w != null) {
2382 if (DEBUG_INPUT_METHOD) Slog.i(TAG_WM, "Switching to real app window: " + w);
2383 imFocus = w;
2384 }
2385 }
2386
2387 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2388
2389 if (DEBUG_INPUT_METHOD) {
2390 Slog.i(TAG_WM, "IM target client: " + imeClient);
2391 if (imeClient != null) {
2392 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2393 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2394 }
2395 }
2396
2397 return imeClient != null && imeClient.asBinder() == client.asBinder();
2398 }
2399
2400 boolean hasSecureWindowOnScreen() {
2401 for (int i = mWindows.size() - 1; i >= 0; --i) {
2402 final WindowState ws = mWindows.get(i);
2403 if (ws.isOnScreen() && (ws.mAttrs.flags & FLAG_SECURE) != 0) {
2404 return true;
2405 }
2406 }
2407 return false;
2408 }
2409
2410 void updateSystemUiVisibility(int visibility, int globalDiff) {
2411 for (int i = mWindows.size() - 1; i >= 0; --i) {
2412 final WindowState ws = mWindows.get(i);
2413 try {
2414 int curValue = ws.mSystemUiVisibility;
2415 int diff = (curValue ^ visibility) & globalDiff;
2416 int newValue = (curValue & ~diff) | (visibility & diff);
2417 if (newValue != curValue) {
2418 ws.mSeq++;
2419 ws.mSystemUiVisibility = newValue;
2420 }
2421 if (newValue != curValue || ws.mAttrs.hasSystemUiListeners) {
2422 ws.mClient.dispatchSystemUiVisibilityChanged(ws.mSeq,
2423 visibility, newValue, diff);
2424 }
2425 } catch (RemoteException e) {
2426 // so sorry
2427 }
2428 }
2429 }
2430
2431 void onWindowFreezeTimeout() {
2432 Slog.w(TAG_WM, "Window freeze timeout expired.");
2433 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
2434 for (int i = mWindows.size() - 1; i >= 0; --i) {
2435 final WindowState w = mWindows.get(i);
2436 if (!w.mOrientationChanging) {
2437 continue;
2438 }
2439 w.mOrientationChanging = false;
2440 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2441 - mService.mDisplayFreezeTime);
2442 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
2443 }
2444 mService.mWindowPlacerLocked.performSurfacePlacement();
2445 }
2446
2447 void waitForAllWindowsDrawn() {
2448 final WindowManagerPolicy policy = mService.mPolicy;
2449 for (int winNdx = mWindows.size() - 1; winNdx >= 0; --winNdx) {
2450 final WindowState win = mWindows.get(winNdx);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002451 final boolean keyguard = policy.isKeyguardHostWindow(win.mAttrs);
Jorim Jaggife762342016-10-13 14:33:27 +02002452 if (win.isVisibleLw() && (win.mAppToken != null || keyguard)) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002453 win.mWinAnimator.mDrawState = DRAW_PENDING;
2454 // Force add to mResizingWindows.
2455 win.mLastContentInsets.set(-1, -1, -1, -1);
2456 mService.mWaitingForDrawn.add(win);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002457 }
2458 }
2459 }
2460
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002461 ReadOnlyWindowList getReadOnlyWindowList() {
2462 return mWindows.getReadOnly();
2463 }
2464
2465 void getWindows(WindowList output) {
2466 output.addAll(mWindows);
2467 }
2468
2469 // TODO: Super crazy long method that should be broken down...
2470 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2471
2472 boolean focusDisplayed = false;
2473 boolean displayHasContent = false;
2474 float preferredRefreshRate = 0;
2475 int preferredModeId = 0;
2476
2477
2478 final int dw = mDisplayInfo.logicalWidth;
2479 final int dh = mDisplayInfo.logicalHeight;
2480 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2481
2482 mTmpUpdateAllDrawn.clear();
2483
2484 int repeats = 0;
2485 do {
2486 repeats++;
2487 if (repeats > 6) {
2488 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2489 clearLayoutNeeded();
2490 break;
2491 }
2492
2493 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2494 pendingLayoutChanges);
2495
Andrii Kulian839def92016-11-02 10:58:58 -07002496 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2497 // the wallpaper window jumping across displays.
2498 // Remove check for default display when there will be support for multiple wallpaper
2499 // targets (on different displays).
2500 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002501 adjustWallpaperWindows();
2502 }
2503
2504 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2505 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2506 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2507 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002508 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002509 }
2510 }
2511
2512 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2513 setLayoutNeeded();
2514 }
2515
2516 // FIRST LOOP: Perform a layout, if needed.
2517 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2518 performLayout(repeats == 1, false /* updateInputWindows */);
2519 } else {
2520 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2521 }
2522
2523 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2524 pendingLayoutChanges = 0;
2525
2526 if (isDefaultDisplay) {
2527 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
2528 for (int i = mWindows.size() - 1; i >= 0; i--) {
2529 final WindowState w = mWindows.get(i);
Jorim Jaggife762342016-10-13 14:33:27 +02002530 mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
2531 mService.mInputMethodTarget);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002532 }
2533 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2534 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2535 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2536 }
2537 } while (pendingLayoutChanges != 0);
2538
2539 RootWindowContainer root = mService.mRoot;
2540 boolean obscured = false;
2541 boolean syswin = false;
2542 resetDimming();
2543
2544 // Only used if default window
2545 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
2546
2547 for (int i = mWindows.size() - 1; i >= 0; i--) {
2548 final WindowState w = mWindows.get(i);
2549 final Task task = w.getTask();
2550 final boolean obscuredChanged = w.mObscured != obscured;
2551
2552 // Update effect.
2553 w.mObscured = obscured;
2554 if (!obscured) {
2555 final boolean isDisplayed = w.isDisplayedLw();
2556
2557 if (isDisplayed && w.isObscuringFullscreen(mDisplayInfo)) {
2558 // This window completely covers everything behind it, so we want to leave all
2559 // of them as undimmed (for performance reasons).
2560 root.mObscuringWindow = w;
2561 obscured = true;
2562 }
2563
2564 displayHasContent |= root.handleNotObscuredLocked(w, obscured, syswin);
2565
2566 if (w.mHasSurface && isDisplayed) {
2567 final int type = w.mAttrs.type;
2568 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
2569 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
2570 syswin = true;
2571 }
2572 if (preferredRefreshRate == 0 && w.mAttrs.preferredRefreshRate != 0) {
2573 preferredRefreshRate = w.mAttrs.preferredRefreshRate;
2574 }
2575 if (preferredModeId == 0 && w.mAttrs.preferredDisplayModeId != 0) {
2576 preferredModeId = w.mAttrs.preferredDisplayModeId;
2577 }
2578 }
2579 }
2580
2581 w.applyDimLayerIfNeeded();
2582
2583 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
2584 && mWallpaperController.isWallpaperTarget(w)) {
2585 // This is the wallpaper target and its obscured state changed... make sure the
2586 // current wallpaper's visibility has been updated accordingly.
2587 mWallpaperController.updateWallpaperVisibility();
2588 }
2589
2590 w.handleWindowMovedIfNeeded();
2591
2592 final WindowStateAnimator winAnimator = w.mWinAnimator;
2593
2594 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
2595 w.mContentChanged = false;
2596
2597 // Moved from updateWindowsAndWallpaperLocked().
2598 if (w.mHasSurface) {
2599 // Take care of the window being ready to display.
2600 final boolean committed = winAnimator.commitFinishDrawingLocked();
2601 if (isDefaultDisplay && committed) {
2602 if (w.mAttrs.type == TYPE_DREAM) {
2603 // HACK: When a dream is shown, it may at that point hide the lock screen.
2604 // So we need to redo the layout to let the phone window manager make this
2605 // happen.
2606 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
2607 if (DEBUG_LAYOUT_REPEATS) {
2608 surfacePlacer.debugLayoutRepeats(
2609 "dream and commitFinishDrawingLocked true",
2610 pendingLayoutChanges);
2611 }
2612 }
2613 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
2614 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
2615 "First draw done in potential wallpaper target " + w);
2616 root.mWallpaperMayChange = true;
2617 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
2618 if (DEBUG_LAYOUT_REPEATS) {
2619 surfacePlacer.debugLayoutRepeats(
2620 "wallpaper and commitFinishDrawingLocked true",
2621 pendingLayoutChanges);
2622 }
2623 }
2624 }
2625 if (!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening()) {
2626 // Updates the shown frame before we set up the surface. This is needed
2627 // because the resizing could change the top-left position (in addition to
2628 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
2629 // position the surface.
2630 //
2631 // If an animation is being started, we can't call this method because the
2632 // animation hasn't processed its initial transformation yet, but in general
2633 // we do want to update the position if the window is animating.
2634 winAnimator.computeShownFrameLocked();
2635 }
2636 winAnimator.setSurfaceBoundariesLocked(recoveringMemory);
2637 }
2638
2639 final AppWindowToken atoken = w.mAppToken;
2640 if (atoken != null) {
2641 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
2642 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
2643 mTmpUpdateAllDrawn.add(atoken);
2644 }
2645 }
2646
2647 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
2648 && w.isDisplayedLw()) {
2649 focusDisplayed = true;
2650 }
2651
2652 w.updateResizingWindowIfNeeded();
2653 }
2654
2655 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
2656 displayHasContent,
2657 preferredRefreshRate,
2658 preferredModeId,
2659 true /* inTraversal, must call performTraversalInTrans... below */);
2660
2661 stopDimmingIfNeeded();
2662
2663 while (!mTmpUpdateAllDrawn.isEmpty()) {
2664 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2665 // See if any windows have been drawn, so they (and others associated with them)
2666 // can now be shown.
2667 atoken.updateAllDrawn(this);
2668 }
2669
2670 return focusDisplayed;
2671 }
2672
2673 void performLayout(boolean initial, boolean updateInputWindows) {
2674 if (!isLayoutNeeded()) {
2675 return;
2676 }
2677 clearLayoutNeeded();
2678
2679 final int dw = mDisplayInfo.logicalWidth;
2680 final int dh = mDisplayInfo.logicalHeight;
2681
2682 int i;
2683
2684 if (DEBUG_LAYOUT) {
2685 Slog.v(TAG, "-------------------------------------");
2686 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2687 }
2688
2689 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mService.mRotation,
2690 getConfiguration().uiMode);
2691 if (isDefaultDisplay) {
2692 // Not needed on non-default displays.
2693 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2694 mService.mScreenRect.set(0, 0, dw, dh);
2695 }
2696
2697 mService.mPolicy.getContentRectLw(mContentRect);
2698
2699 int seq = mService.mLayoutSeq + 1;
2700 if (seq < 0) seq = 0;
2701 mService.mLayoutSeq = seq;
2702
2703 boolean behindDream = false;
2704
2705 // First perform layout of any root windows (not attached to another window).
2706 int topAttached = -1;
2707 for (i = mWindows.size() - 1; i >= 0; i--) {
2708 final WindowState win = mWindows.get(i);
2709
2710 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
2711 // wasting time and funky changes while a window is animating away.
Jorim Jaggife762342016-10-13 14:33:27 +02002712 final boolean gone = (behindDream && mService.mPolicy.canBeHiddenByKeyguardLw(win))
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002713 || win.isGoneForLayoutLw();
2714
2715 if (DEBUG_LAYOUT && !win.mLayoutAttached) {
2716 Slog.v(TAG, "1ST PASS " + win + ": gone=" + gone + " mHaveFrame=" + win.mHaveFrame
2717 + " mLayoutAttached=" + win.mLayoutAttached
2718 + " screen changed=" + win.isConfigChanged());
2719 final AppWindowToken atoken = win.mAppToken;
2720 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + win.mViewVisibility
2721 + " mRelayoutCalled=" + win.mRelayoutCalled + " hidden=" + win.mToken.hidden
2722 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
2723 + " parentHidden=" + win.isParentWindowHidden());
2724 else Slog.v(TAG, " VIS: mViewVisibility=" + win.mViewVisibility
2725 + " mRelayoutCalled=" + win.mRelayoutCalled + " hidden=" + win.mToken.hidden
2726 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
2727 + " parentHidden=" + win.isParentWindowHidden());
2728 }
2729
2730 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
2731 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
2732 // since that means "perform layout as normal, just don't display").
2733 if (!gone || !win.mHaveFrame || win.mLayoutNeeded
2734 || ((win.isConfigChanged() || win.setReportResizeHints())
2735 && !win.isGoneForLayoutLw() &&
2736 ((win.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
2737 (win.mHasSurface && win.mAppToken != null &&
2738 win.mAppToken.layoutConfigChanges)))) {
2739 if (!win.mLayoutAttached) {
2740 if (initial) {
2741 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
2742 win.mContentChanged = false;
2743 }
2744 if (win.mAttrs.type == TYPE_DREAM) {
2745 // Don't layout windows behind a dream, so that if it does stuff like hide
2746 // the status bar we won't get a bad transition when it goes away.
2747 behindDream = true;
2748 }
2749 win.mLayoutNeeded = false;
2750 win.prelayout();
2751 mService.mPolicy.layoutWindowLw(win, null);
2752 win.mLayoutSeq = seq;
2753
2754 // Window frames may have changed. Update dim layer with the new bounds.
2755 final Task task = win.getTask();
2756 if (task != null) {
2757 mDimLayerController.updateDimLayer(task);
2758 }
2759
2760 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + win.mFrame
2761 + " mContainingFrame=" + win.mContainingFrame
2762 + " mDisplayFrame=" + win.mDisplayFrame);
2763 } else {
2764 if (topAttached < 0) topAttached = i;
2765 }
2766 }
2767 }
2768
2769 boolean attachedBehindDream = false;
2770
2771 // Now perform layout of attached windows, which usually depend on the position of the
2772 // window they are attached to. XXX does not deal with windows that are attached to windows
2773 // that are themselves attached.
2774 for (i = topAttached; i >= 0; i--) {
2775 final WindowState win = mWindows.get(i);
2776
2777 if (win.mLayoutAttached) {
2778 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + win + " mHaveFrame=" + win.mHaveFrame
2779 + " mViewVisibility=" + win.mViewVisibility
2780 + " mRelayoutCalled=" + win.mRelayoutCalled);
2781 // If this view is GONE, then skip it -- keep the current frame, and let the caller
2782 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
2783 // windows, since that means "perform layout as normal, just don't display").
Jorim Jaggife762342016-10-13 14:33:27 +02002784 if (attachedBehindDream && mService.mPolicy.canBeHiddenByKeyguardLw(win)) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002785 continue;
2786 }
2787 if ((win.mViewVisibility != GONE && win.mRelayoutCalled) || !win.mHaveFrame
2788 || win.mLayoutNeeded) {
2789 if (initial) {
2790 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
2791 win.mContentChanged = false;
2792 }
2793 win.mLayoutNeeded = false;
2794 win.prelayout();
2795 mService.mPolicy.layoutWindowLw(win, win.getParentWindow());
2796 win.mLayoutSeq = seq;
2797 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + win.mFrame
2798 + " mContainingFrame=" + win.mContainingFrame
2799 + " mDisplayFrame=" + win.mDisplayFrame);
2800 }
2801 } else if (win.mAttrs.type == TYPE_DREAM) {
2802 // Don't layout windows behind a dream, so that if it does stuff like hide the
2803 // status bar we won't get a bad transition when it goes away.
2804 attachedBehindDream = behindDream;
2805 }
2806 }
2807
2808 // Window frames may have changed. Tell the input dispatcher about it.
2809 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2810 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2811 if (updateInputWindows) {
2812 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2813 }
2814
2815 mService.mPolicy.finishLayoutLw();
2816 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2817 }
2818
2819 /**
2820 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2821 * In portrait mode, it grabs the full screenshot.
2822 *
2823 * @param width the width of the target bitmap
2824 * @param height the height of the target bitmap
2825 * @param includeFullDisplay true if the screen should not be cropped before capture
2826 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2827 * @param config of the output bitmap
2828 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
2829 */
2830 Bitmap screenshotApplications(IBinder appToken, int displayId, int width, int height,
2831 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
2832 boolean wallpaperOnly) {
2833 int dw = mDisplayInfo.logicalWidth;
2834 int dh = mDisplayInfo.logicalHeight;
2835 if (dw == 0 || dh == 0) {
2836 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2837 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2838 return null;
2839 }
2840
2841 Bitmap bm = null;
2842
2843 int maxLayer = 0;
2844 final Rect frame = new Rect();
2845 final Rect stackBounds = new Rect();
2846
2847 boolean screenshotReady;
2848 int minLayer;
2849 if (appToken == null && !wallpaperOnly) {
2850 screenshotReady = true;
2851 minLayer = 0;
2852 } else {
2853 screenshotReady = false;
2854 minLayer = Integer.MAX_VALUE;
2855 }
2856
2857 WindowState appWin = null;
2858
2859 boolean includeImeInScreenshot;
2860 synchronized(mService.mWindowMap) {
2861 final AppWindowToken imeTargetAppToken = mService.mInputMethodTarget != null
2862 ? mService.mInputMethodTarget.mAppToken : null;
2863 // We only include the Ime in the screenshot if the app we are screenshoting is the IME
2864 // target and isn't in multi-window mode. We don't screenshot the IME in multi-window
2865 // mode because the frame of the IME might not overlap with that of the app.
2866 // E.g. IME target app at the top in split-screen mode and the IME at the bottom
2867 // overlapping with the bottom app.
2868 includeImeInScreenshot = imeTargetAppToken != null
2869 && imeTargetAppToken.appToken != null
2870 && imeTargetAppToken.appToken.asBinder() == appToken
2871 && !mService.mInputMethodTarget.isInMultiWindowMode();
2872 }
2873
2874 final int aboveAppLayer = (mService.mPolicy.windowTypeToLayerLw(TYPE_APPLICATION) + 1)
2875 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
2876
2877 synchronized(mService.mWindowMap) {
2878 // Figure out the part of the screen that is actually the app.
2879 appWin = null;
2880 for (int i = mWindows.size() - 1; i >= 0; i--) {
2881 final WindowState ws = mWindows.get(i);
2882 if (!ws.mHasSurface) {
2883 continue;
2884 }
2885 if (ws.mLayer >= aboveAppLayer) {
2886 continue;
2887 }
2888 if (wallpaperOnly && !ws.mIsWallpaper) {
2889 continue;
2890 }
2891 if (ws.mIsImWindow) {
2892 if (!includeImeInScreenshot) {
2893 continue;
2894 }
2895 } else if (ws.mIsWallpaper) {
2896 // If this is the wallpaper layer and we're only looking for the wallpaper layer
2897 // then the target window state is this one.
2898 if (wallpaperOnly) {
2899 appWin = ws;
2900 }
2901
2902 if (appWin == null) {
2903 // We have not ran across the target window yet, so it is probably behind
2904 // the wallpaper. This can happen when the keyguard is up and all windows
2905 // are moved behind the wallpaper. We don't want to include the wallpaper
2906 // layer in the screenshot as it will cover-up the layer of the target
2907 // window.
2908 continue;
2909 }
2910 // Fall through. The target window is in front of the wallpaper. For this
2911 // case we want to include the wallpaper layer in the screenshot because
2912 // the target window might have some transparent areas.
2913 } else if (appToken != null) {
2914 if (ws.mAppToken == null || ws.mAppToken.token != appToken) {
2915 // This app window is of no interest if it is not associated with the
2916 // screenshot app.
2917 continue;
2918 }
2919 appWin = ws;
2920 }
2921
2922 // Include this window.
2923
2924 final WindowStateAnimator winAnim = ws.mWinAnimator;
2925 int layer = winAnim.mSurfaceController.getLayer();
2926 if (maxLayer < layer) {
2927 maxLayer = layer;
2928 }
2929 if (minLayer > layer) {
2930 minLayer = layer;
2931 }
2932
2933 // Don't include wallpaper in bounds calculation
2934 if (!includeFullDisplay && !ws.mIsWallpaper) {
2935 final Rect wf = ws.mFrame;
2936 final Rect cr = ws.mContentInsets;
2937 int left = wf.left + cr.left;
2938 int top = wf.top + cr.top;
2939 int right = wf.right - cr.right;
2940 int bottom = wf.bottom - cr.bottom;
2941 frame.union(left, top, right, bottom);
2942 ws.getVisibleBounds(stackBounds);
2943 if (!Rect.intersects(frame, stackBounds)) {
2944 // Set frame empty if there's no intersection.
2945 frame.setEmpty();
2946 }
2947 }
2948
2949 final boolean foundTargetWs =
2950 (ws.mAppToken != null && ws.mAppToken.token == appToken)
2951 || (appWin != null && wallpaperOnly);
2952 if (foundTargetWs && ws.isDisplayedLw() && winAnim.getShown()) {
2953 screenshotReady = true;
2954 }
2955
2956 if (ws.isObscuringFullscreen(mDisplayInfo)){
2957 break;
2958 }
2959 }
2960
2961 if (appToken != null && appWin == null) {
2962 // Can't find a window to snapshot.
2963 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
2964 "Screenshot: Couldn't find a surface matching " + appToken);
2965 return null;
2966 }
2967
2968 if (!screenshotReady) {
2969 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
2970 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
2971 appWin.mWinAnimator.mDrawState)));
2972 return null;
2973 }
2974
2975 // Screenshot is ready to be taken. Everything from here below will continue
2976 // through the bottom of the loop and return a value. We only stay in the loop
2977 // because we don't want to release the mWindowMap lock until the screenshot is
2978 // taken.
2979
2980 if (maxLayer == 0) {
2981 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2982 + ": returning null maxLayer=" + maxLayer);
2983 return null;
2984 }
2985
2986 if (!includeFullDisplay) {
2987 // Constrain frame to the screen size.
2988 if (!frame.intersect(0, 0, dw, dh)) {
2989 frame.setEmpty();
2990 }
2991 } else {
2992 // Caller just wants entire display.
2993 frame.set(0, 0, dw, dh);
2994 }
2995 if (frame.isEmpty()) {
2996 return null;
2997 }
2998
2999 if (width < 0) {
3000 width = (int) (frame.width() * frameScale);
3001 }
3002 if (height < 0) {
3003 height = (int) (frame.height() * frameScale);
3004 }
3005
3006 // Tell surface flinger what part of the image to crop. Take the top
3007 // right part of the application, and crop the larger dimension to fit.
3008 Rect crop = new Rect(frame);
3009 if (width / (float) frame.width() < height / (float) frame.height()) {
3010 int cropWidth = (int)((float)width / (float)height * frame.height());
3011 crop.right = crop.left + cropWidth;
3012 } else {
3013 int cropHeight = (int)((float)height / (float)width * frame.width());
3014 crop.bottom = crop.top + cropHeight;
3015 }
3016
3017 // The screenshot API does not apply the current screen rotation.
3018 int rot = mDisplay.getRotation();
3019
3020 if (rot == ROTATION_90 || rot == ROTATION_270) {
3021 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3022 }
3023
3024 // Surfaceflinger is not aware of orientation, so convert our logical
3025 // crop to surfaceflinger's portrait orientation.
3026 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3027
3028 if (DEBUG_SCREENSHOT) {
3029 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3030 + maxLayer + " appToken=" + appToken);
3031 for (int i = 0; i < mWindows.size(); i++) {
3032 final WindowState win = mWindows.get(i);
3033 final WindowSurfaceController controller = win.mWinAnimator.mSurfaceController;
3034 Slog.i(TAG_WM, win + ": " + win.mLayer
3035 + " animLayer=" + win.mWinAnimator.mAnimLayer
3036 + " surfaceLayer=" + ((controller == null)
3037 ? "null" : controller.getLayer()));
3038 }
3039 }
3040
3041 final ScreenRotationAnimation screenRotationAnimation =
3042 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3043 final boolean inRotation = screenRotationAnimation != null &&
3044 screenRotationAnimation.isAnimating();
3045 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3046 "Taking screenshot while rotating");
3047
3048 // We force pending transactions to flush before taking
3049 // the screenshot by pushing an empty synchronous transaction.
3050 SurfaceControl.openTransaction();
3051 SurfaceControl.closeTransactionSync();
3052
3053 bm = SurfaceControl.screenshot(crop, width, height, minLayer, maxLayer,
3054 inRotation, rot);
3055 if (bm == null) {
3056 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3057 + ") to layer " + maxLayer);
3058 return null;
3059 }
3060 }
3061
3062 if (DEBUG_SCREENSHOT) {
3063 // TEST IF IT's ALL BLACK
3064 int[] buffer = new int[bm.getWidth() * bm.getHeight()];
3065 bm.getPixels(buffer, 0, bm.getWidth(), 0, 0, bm.getWidth(), bm.getHeight());
3066 boolean allBlack = true;
3067 final int firstColor = buffer[0];
3068 for (int i = 0; i < buffer.length; i++) {
3069 if (buffer[i] != firstColor) {
3070 allBlack = false;
3071 break;
3072 }
3073 }
3074 if (allBlack) {
3075 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
3076 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
3077 (appWin != null ?
3078 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
3079 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
3080 }
3081 }
3082
3083 // Create a copy of the screenshot that is immutable and backed in ashmem.
3084 // This greatly reduces the overhead of passing the bitmap between processes.
3085 Bitmap ret = bm.createAshmemBitmap(config);
3086 bm.recycle();
3087 return ret;
3088 }
3089
3090 // TODO: Can this use createRotationMatrix()?
3091 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3092 if (rot == Surface.ROTATION_90) {
3093 final int tmp = crop.top;
3094 crop.top = dw - crop.right;
3095 crop.right = crop.bottom;
3096 crop.bottom = dw - crop.left;
3097 crop.left = tmp;
3098 } else if (rot == Surface.ROTATION_180) {
3099 int tmp = crop.top;
3100 crop.top = dh - crop.bottom;
3101 crop.bottom = dh - tmp;
3102 tmp = crop.right;
3103 crop.right = dw - crop.left;
3104 crop.left = dw - tmp;
3105 } else if (rot == Surface.ROTATION_270) {
3106 final int tmp = crop.top;
3107 crop.top = crop.left;
3108 crop.left = dh - crop.bottom;
3109 crop.bottom = crop.right;
3110 crop.right = dh - tmp;
3111 }
3112 }
3113
3114 void onSeamlessRotationTimeout() {
3115 boolean layoutNeeded = false;
3116 for (int i = mWindows.size() - 1; i >= 0; i--) {
3117 final WindowState w = mWindows.get(i);
3118 if (!w.mSeamlesslyRotated) {
3119 continue;
3120 }
3121 layoutNeeded = true;
3122 w.setDisplayLayoutNeeded();
3123 mService.markForSeamlessRotation(w, false);
3124 }
3125
3126 if (layoutNeeded) {
3127 mService.mWindowPlacerLocked.performSurfacePlacement();
3128 }
3129 }
3130
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003131 static final class GetWindowOnDisplaySearchResult {
Wale Ogunwaleec731152016-09-08 20:18:57 -07003132 boolean reachedToken;
3133 WindowState foundWindow;
3134
3135 void reset() {
3136 reachedToken = false;
3137 foundWindow = null;
3138 }
3139 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003140
3141 static final class TaskForResizePointSearchResult {
3142 boolean searchDone;
3143 Task taskForResize;
3144
3145 void reset() {
3146 searchDone = false;
3147 taskForResize = null;
3148 }
3149 }
Robert Carr3b716242016-08-16 16:02:21 -07003150
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003151 /**
3152 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3153 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3154 * homogeneous children type which is currently required by sub-classes of
3155 * {@link WindowContainer} class.
3156 */
3157 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3158
3159 int size() {
3160 return mChildren.size();
3161 }
3162
3163 E get(int index) {
3164 return mChildren.get(index);
3165 }
3166
3167 @Override
3168 boolean fillsParent() {
3169 return true;
3170 }
3171
3172 @Override
3173 boolean isVisible() {
3174 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003175 }
3176 }
3177
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003178 /**
3179 * Window container class that contains all containers on this display relating to Apps.
3180 * I.e Activities.
3181 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003182 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003183
Andrii Kulian839def92016-11-02 10:58:58 -07003184 /**
3185 * Adds the stack to this container.
3186 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3187 */
3188 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003189 if (stack.mStackId == HOME_STACK_ID) {
3190 if (mHomeStack != null) {
3191 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3192 }
3193 mHomeStack = stack;
3194 }
3195 addChild(stack, onTop);
3196 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003197 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003198
Andrii Kulian839def92016-11-02 10:58:58 -07003199 /** Removes the stack from its container and prepare for changing the parent. */
3200 void removeStackFromDisplay(TaskStack stack) {
3201 removeChild(stack);
3202 stack.onRemovedFromDisplay();
3203 // TODO: remove when window list will be gone.
3204 // Manually remove records from window list and tap excluded windows list.
3205 for (int i = mWindows.size() - 1; i >= 0; --i) {
3206 final WindowState windowState = mWindows.get(i);
3207 if (stack == windowState.getStack()) {
3208 mWindows.remove(i);
3209 mTapExcludedWindows.remove(windowState);
3210 }
3211 }
3212 }
3213
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003214 void moveStack(TaskStack stack, boolean toTop) {
3215 if (StackId.isAlwaysOnTop(stack.mStackId) && !toTop) {
3216 // This stack is always-on-top silly...
3217 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + stack + " to bottom");
3218 return;
3219 }
3220
3221 if (!mChildren.contains(stack)) {
3222 Slog.wtf(TAG_WM, "moving stack that was not added: " + stack, new Throwable());
3223 }
3224 removeChild(stack);
3225 addChild(stack, toTop);
3226 }
3227
3228 private void addChild(TaskStack stack, boolean toTop) {
3229 int addIndex = toTop ? mChildren.size() : 0;
3230
3231 if (toTop
3232 && mService.isStackVisibleLocked(PINNED_STACK_ID)
3233 && stack.mStackId != PINNED_STACK_ID) {
3234 // The pinned stack is always the top most stack (always-on-top) when it is visible.
3235 // So, stack is moved just below the pinned stack.
3236 addIndex--;
3237 TaskStack topStack = mChildren.get(addIndex);
3238 if (topStack.mStackId != PINNED_STACK_ID) {
3239 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3240 }
3241 }
3242 addChild(stack, addIndex);
3243 setLayoutNeeded();
3244 }
3245
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003246 @Override
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003247 void forAllWindows(Consumer<WindowState> callback, boolean traverseTopToBottom) {
3248 if (traverseTopToBottom) {
3249 super.forAllWindows(callback, traverseTopToBottom);
3250 forAllExitingAppTokenWindows(callback, traverseTopToBottom);
3251 } else {
3252 forAllExitingAppTokenWindows(callback, traverseTopToBottom);
3253 super.forAllWindows(callback, traverseTopToBottom);
3254 }
3255 }
3256
3257 private void forAllExitingAppTokenWindows(Consumer<WindowState> callback,
3258 boolean traverseTopToBottom) {
3259 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3260 // app tokens.
3261 // TODO: Investigate if we need to continue to do this or if we can just process them
3262 // in-order.
3263 if (traverseTopToBottom) {
3264 for (int i = mChildren.size() - 1; i >= 0; --i) {
3265 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3266 for (int j = appTokens.size() - 1; j >= 0; --j) {
3267 appTokens.get(j).forAllWindowsUnchecked(callback, traverseTopToBottom);
3268 }
3269 }
3270 } else {
3271 final int count = mChildren.size();
3272 for (int i = 0; i < count; ++i) {
3273 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3274 final int appTokensCount = appTokens.size();
3275 for (int j = 0; j < appTokensCount; j++) {
3276 appTokens.get(j).forAllWindowsUnchecked(callback, traverseTopToBottom);
3277 }
3278 }
3279 }
3280 }
3281
3282 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003283 int getOrientation() {
3284 if (mService.isStackVisibleLocked(DOCKED_STACK_ID)
3285 || mService.isStackVisibleLocked(FREEFORM_WORKSPACE_STACK_ID)) {
3286 // Apps and their containers are not allowed to specify an orientation while the
3287 // docked or freeform stack is visible...except for the home stack/task if the
3288 // docked stack is minimized and it actually set something.
3289 if (mHomeStack != null && mHomeStack.isVisible()
3290 && mDividerControllerLocked.isMinimizedDock()) {
3291 final int orientation = mHomeStack.getOrientation();
3292 if (orientation != SCREEN_ORIENTATION_UNSET) {
3293 return orientation;
3294 }
3295 }
3296 return SCREEN_ORIENTATION_UNSPECIFIED;
3297 }
3298
3299 final int orientation = super.getOrientation();
3300 if (orientation != SCREEN_ORIENTATION_UNSET
3301 && orientation != SCREEN_ORIENTATION_BEHIND) {
3302 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3303 "App is requesting an orientation, return " + orientation);
3304 return orientation;
3305 }
3306
3307 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3308 "No app is requesting an orientation, return " + mService.mLastOrientation);
3309 // The next app has not been requested to be visible, so we keep the current orientation
3310 // to prevent freezing/unfreezing the display too early.
3311 return mService.mLastOrientation;
3312 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003313 }
3314
3315 /**
3316 * Window container class that contains all containers on this display that are not related to
3317 * Apps. E.g. status bar.
3318 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003319 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3320 /**
3321 * Compares two child window tokens returns -1 if the first is lesser than the second in
3322 * terms of z-order and 1 otherwise.
3323 */
3324 final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
3325 // Tokens with higher base layer are z-ordered on-top.
3326 mService.mPolicy.windowTypeToLayerLw(token1.windowType)
3327 < mService.mPolicy.windowTypeToLayerLw(token2.windowType) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003328
Wale Ogunwale3a931692016-11-02 16:49:48 -07003329 private final String mName;
3330 NonAppWindowContainers(String name) {
3331 mName = name;
3332 }
3333
3334 void addChild(WindowToken token) {
3335 addChild(token, mWindowComparator);
3336 }
3337
3338 @Override
3339 String getName() {
3340 return mName;
3341 }
Robert Carr3b716242016-08-16 16:02:21 -07003342 }
Craig Mautner59c00972012-07-30 12:10:24 -07003343}