blob: c8e35eb0c0f8fdc6d8bd619d75789b66e3558ba7 [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 Ogunwalef4ebe2e2016-11-09 13:24:43 -0800130import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700131import com.android.internal.view.IInputMethodClient;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700132import com.android.server.input.InputWindowHandle;
Craig Mautner59c00972012-07-30 12:10:24 -0700133
Robert Carr3b716242016-08-16 16:02:21 -0700134import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700135import java.io.PrintWriter;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700136import java.io.StringWriter;
Craig Mautner59c00972012-07-30 12:10:24 -0700137import java.util.ArrayList;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700138import java.util.Arrays;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700139import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700140import java.util.HashMap;
141import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700142import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700143import java.util.List;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -0700144import java.util.function.Consumer;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800145import java.util.function.Function;
Craig Mautner59c00972012-07-30 12:10:24 -0700146
Craig Mautner59c00972012-07-30 12:10:24 -0700147/**
148 * Utility class for keeping track of the WindowStates and other pertinent contents of a
149 * particular Display.
150 *
151 * IMPORTANT: No method from this class should ever be used without holding
152 * WindowManagerService.mWindowMap.
153 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700154class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700155 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700156
157 /** Unique identifier of this stack. */
158 private final int mDisplayId;
159
Wale Ogunwale3a931692016-11-02 16:49:48 -0700160 /** The containers below are the only child containers the display can have. */
161 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700162 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
Wale Ogunwale3a931692016-11-02 16:49:48 -0700163 // Contains all non-app window containers that should be displayed above the app containers
164 // (e.g. Status bar)
165 private final NonAppWindowContainers mAboveAppWindowsContainers =
166 new NonAppWindowContainers("mAboveAppWindowsContainers");
167 // Contains all non-app window containers that should be displayed below the app containers
168 // (e.g. Wallpaper).
169 private final NonAppWindowContainers mBelowAppWindowsContainers =
170 new NonAppWindowContainers("mBelowAppWindowsContainers");
171 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
172 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
173 // window containers together and move them in-sync if/when needed.
174 private final NonAppWindowContainers mImeWindowsContainers =
175 new NonAppWindowContainers("mImeWindowsContainers");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700176
Wale Ogunwale3a931692016-11-02 16:49:48 -0700177 // Z-ordered (bottom-most first) list of all Window objects.
Craig Mautnerdc548482014-02-05 13:35:24 -0800178 private final WindowList mWindows = new WindowList();
Craig Mautner59c00972012-07-30 12:10:24 -0700179
Wale Ogunwale02319a62016-09-26 15:21:22 -0700180 // Mapping from a token IBinder to a WindowToken object on this display.
181 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
182
Craig Mautner59c00972012-07-30 12:10:24 -0700183 int mInitialDisplayWidth = 0;
184 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700185 int mInitialDisplayDensity = 0;
Craig Mautner59c00972012-07-30 12:10:24 -0700186 int mBaseDisplayWidth = 0;
187 int mBaseDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700188 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700189 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700190 private final DisplayInfo mDisplayInfo = new DisplayInfo();
191 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700192 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Craig Mautner59c00972012-07-30 12:10:24 -0700193
Craig Mautner6601b7b2013-04-29 10:29:11 -0700194 Rect mBaseDisplayRect = new Rect();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700195 private Rect mContentRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700196
Craig Mautner39834192012-09-02 07:47:24 -0700197 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700198 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700199 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700200 // TODO(multi-display): remove some of the usages.
Craig Mautner69b08182012-09-05 13:07:13 -0700201 final boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700202
Craig Mautnerdc548482014-02-05 13:35:24 -0800203 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700204 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800205
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800206 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
207 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700208 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700209
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700210 /** Detect user tapping outside of current focused task bounds .*/
211 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700212
Craig Mautner6601b7b2013-04-29 10:29:11 -0700213 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700214 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700215
Craig Mautner6601b7b2013-04-29 10:29:11 -0700216 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800217 private final Rect mTmpRect = new Rect();
218 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700219 private final RectF mTmpRectF = new RectF();
220 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800221 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700222
Craig Mautner9d808b12013-08-06 18:00:25 -0700223 final WindowManagerService mService;
224
Craig Mautner95da1082014-02-24 17:54:35 -0800225 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700226 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800227
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700228 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700229 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700230
Chong Zhang112eb8c2015-11-02 11:17:00 -0800231 final DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700232
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800233 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
234
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700235 /** Used when rebuilding window list to keep track of windows that have been removed. */
236 private WindowState[] mRebuildTmp = new WindowState[20];
237
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700238 /**
239 * Temporary list for comparison. Always clear this after use so we don't end up with
240 * orphaned windows references
241 */
242 private final ArrayList<WindowState> mTmpWindows = new ArrayList<>();
243
244 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
245
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700246 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
247 new TaskForResizePointSearchResult();
248 private final GetWindowOnDisplaySearchResult mTmpGetWindowOnDisplaySearchResult =
249 new GetWindowOnDisplaySearchResult();
250
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700251 // True if this display is in the process of being removed. Used to determine if the removal of
252 // the display's direct children should be allowed.
253 private boolean mRemovingDisplay = false;
254
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700255 private final WindowLayersController mLayersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700256 final WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700257 int mInputMethodAnimLayerAdjustment;
258
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800259 /**
Craig Mautner2d5618c2012-10-18 13:55:47 -0700260 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800261 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700262 * @param layersController window layer controller used to assign layer to the windows on this
263 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700264 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
265 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700266 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700267 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700268 WindowLayersController layersController, WallpaperController wallpaperController) {
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700269 mDisplay = display;
270 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700271 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700272 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700273 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700274 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700275 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700276 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700277 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800278 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700279 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800280 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700281
282 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700283 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700284 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700285 super.addChild(mAboveAppWindowsContainers, null);
286 super.addChild(mImeWindowsContainers, null);
Craig Mautner59c00972012-07-30 12:10:24 -0700287 }
288
289 int getDisplayId() {
290 return mDisplayId;
291 }
292
Wale Ogunwale02319a62016-09-26 15:21:22 -0700293 WindowToken getWindowToken(IBinder binder) {
294 return mTokenMap.get(binder);
295 }
296
297 AppWindowToken getAppWindowToken(IBinder binder) {
298 final WindowToken token = getWindowToken(binder);
299 if (token == null) {
300 return null;
301 }
302 return token.asAppWindowToken();
303 }
304
305 void setWindowToken(IBinder binder, WindowToken token) {
306 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
307 if (dc != null) {
308 // We currently don't support adding a window token to the display if the display
309 // already has the binder mapped to another token. If there is a use case for supporting
310 // this moving forward we will either need to merge the WindowTokens some how or have
311 // the binder map to a list of window tokens.
312 throw new IllegalArgumentException("Can't map token=" + token + " to display=" + this
313 + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
314 }
315 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700316
317 if (token.asAppWindowToken() == null) {
318 // Add non-app token to container hierarchy on the display. App tokens are added through
319 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700320 switch (token.windowType) {
321 case TYPE_WALLPAPER:
322 mBelowAppWindowsContainers.addChild(token);
323 break;
324 case TYPE_INPUT_METHOD:
325 case TYPE_INPUT_METHOD_DIALOG:
326 mImeWindowsContainers.addChild(token);
327 break;
328 default:
329 mAboveAppWindowsContainers.addChild(token);
330 break;
331 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700332 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700333 }
334
335 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700336 final WindowToken token = mTokenMap.remove(binder);
337 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3a931692016-11-02 16:49:48 -0700338 switch (token.windowType) {
339 case TYPE_WALLPAPER:
340 mBelowAppWindowsContainers.removeChild(token);
341 break;
342 case TYPE_INPUT_METHOD:
343 case TYPE_INPUT_METHOD_DIALOG:
344 mImeWindowsContainers.removeChild(token);
345 break;
346 default:
347 mAboveAppWindowsContainers.removeChild(token);
348 break;
349 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700350 }
351 return token;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700352 }
353
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700354 void removeAppToken(IBinder binder) {
355 final WindowToken token = removeWindowToken(binder);
356 if (token == null) {
357 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
358 return;
359 }
360
361 final AppWindowToken appToken = token.asAppWindowToken();
362
363 if (appToken == null) {
364 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
365 return;
366 }
367
368 appToken.onRemovedFromDisplay();
369 }
370
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700371 Display getDisplay() {
372 return mDisplay;
373 }
374
Craig Mautner59c00972012-07-30 12:10:24 -0700375 DisplayInfo getDisplayInfo() {
376 return mDisplayInfo;
377 }
378
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700379 DisplayMetrics getDisplayMetrics() {
380 return mDisplayMetrics;
381 }
382
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100383 DockedStackDividerController getDockedDividerController() {
384 return mDividerControllerLocked;
385 }
386
Winson Chung655332c2016-10-31 13:14:28 -0700387 PinnedStackController getPinnedStackController() {
388 return mPinnedStackControllerLocked;
389 }
390
Jeff Browna506a6e2013-06-04 00:02:38 -0700391 /**
392 * Returns true if the specified UID has access to this display.
393 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700394 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700395 return mDisplay.hasAccess(uid);
396 }
397
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700398 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700399 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -0700400 }
401
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700402 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700403 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800404 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -0800405 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700406 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700407 }
408
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700409 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700410 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
411 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700412 if (stack.mStackId == stackId) {
413 return stack;
414 }
415 }
416 return null;
417 }
418
Andrii Kulian441e4492016-09-29 15:25:00 -0700419 @Override
420 void onConfigurationChanged(Configuration newParentConfig) {
421 super.onConfigurationChanged(newParentConfig);
422
Andrii Kulian3a507b52016-09-19 18:14:12 -0700423 // The display size information is heavily dependent on the resources in the current
424 // configuration, so we need to reconfigure it every time the configuration changes.
425 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
426 mService.reconfigureDisplayLocked(this);
427
428 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -0700429 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -0700430 }
Andrii Kulian3a507b52016-09-19 18:14:12 -0700431
Andrii Kulian441e4492016-09-29 15:25:00 -0700432 /**
433 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
434 * bounds were updated.
435 */
436 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700437 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
438 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -0700439 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -0700440 changedStackList.add(stack.mStackId);
441 }
442 }
443 }
444
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700445 @Override
446 boolean fillsParent() {
447 return true;
448 }
449
450 @Override
451 boolean isVisible() {
452 return true;
453 }
454
455 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700456 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -0700457 super.onAppTransitionDone();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700458 rebuildAppWindowList();
459 }
460
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700461 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -0700462 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700463 final WindowManagerPolicy policy = mService.mPolicy;
464
465 // TODO: All the logic before the last return statement in this method should really go in
466 // #NonAppWindowContainer.getOrientation() since it is trying to decide orientation based
467 // on non-app windows. But, we can not do that until the window list is always correct in
468 // terms of z-ordering based on layers.
469 if (mService.mDisplayFrozen) {
470 if (mService.mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
471 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
472 "Display is frozen, return " + mService.mLastWindowForcedOrientation);
473 // If the display is frozen, some activities may be in the middle of restarting, and
474 // thus have removed their old window. If the window has the flag to hide the lock
475 // screen, then the lock screen can re-appear and inflict its own orientation on us.
476 // Keep the orientation stable until this all settles down.
477 return mService.mLastWindowForcedOrientation;
478 } else if (policy.isKeyguardLocked()) {
479 // Use the last orientation the while the display is frozen with the keyguard
480 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
481 // window. We don't want to check the show when locked window directly though as
482 // things aren't stable while the display is frozen, for example the window could be
483 // momentarily unavailable due to activity relaunch.
484 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
485 + "return " + mService.mLastOrientation);
486 return mService.mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -0700487 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700488 } else {
489 for (int pos = mWindows.size() - 1; pos >= 0; --pos) {
490 final WindowState win = mWindows.get(pos);
491 if (win.mAppToken != null) {
492 // We hit an application window. so the orientation will be determined by the
493 // app window. No point in continuing further.
494 break;
495 }
496 if (!win.isVisibleLw() || !win.mPolicyVisibilityAfterAnim) {
497 continue;
498 }
499 int req = win.mAttrs.screenOrientation;
500 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND) {
501 continue;
502 }
503
504 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
505 if (policy.isKeyguardHostWindow(win.mAttrs)) {
506 mService.mLastKeyguardForcedOrientation = req;
507 }
508 return (mService.mLastWindowForcedOrientation = req);
509 }
510 mService.mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
511
Jorim Jaggife762342016-10-13 14:33:27 +0200512 if (policy.isKeyguardShowingAndNotOccluded()) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700513 return mService.mLastKeyguardForcedOrientation;
514 }
Wale Ogunwale51362492016-09-08 17:49:17 -0700515 }
516
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700517 // Top system windows are not requesting an orientation. Start searching from apps.
518 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -0700519 }
520
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700521 void updateDisplayInfo() {
Craig Mautner722285e2012-09-07 13:55:58 -0700522 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700523 mDisplay.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700524 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
525 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800526 }
Craig Mautner722285e2012-09-07 13:55:58 -0700527 }
528
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700529 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700530 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
531 if (displayManagerInternal != null) {
532 // Bootstrap the default logical display from the display manager.
533 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
534 if (newDisplayInfo != null) {
535 mDisplayInfo.copyFrom(newDisplayInfo);
536 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700537 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700538
Filip Gruszczynski608797e2015-11-12 19:08:20 -0800539 mBaseDisplayWidth = mInitialDisplayWidth = mDisplayInfo.logicalWidth;
540 mBaseDisplayHeight = mInitialDisplayHeight = mDisplayInfo.logicalHeight;
541 mBaseDisplayDensity = mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
542 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700543 }
544
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700545 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700546 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800547 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700548 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800549 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
550 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700551 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800552 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700553 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800554 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700555 out.set(left, top, left + width, top + height);
556 }
557
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700558 private void getLogicalDisplayRect(Rect out, int orientation) {
559 getLogicalDisplayRect(out);
560
561 // Rotate the Rect if needed.
562 final int currentRotation = mDisplayInfo.rotation;
563 final int rotationDelta = deltaRotation(currentRotation, orientation);
564 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
565 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
566 mTmpRectF.set(out);
567 mTmpMatrix.mapRect(mTmpRectF);
568 mTmpRectF.round(out);
569 }
570 }
571
Chong Zhangf66db432016-01-13 10:39:51 -0800572 void getContentRect(Rect out) {
573 out.set(mContentRect);
574 }
575
Andrii Kulian839def92016-11-02 10:58:58 -0700576 /**
577 * Adds the stack to this display.
578 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
579 */
580 Rect addStackToDisplay(int stackId, boolean onTop) {
581 boolean attachedToDisplay = false;
582 TaskStack stack = mService.mStackIdToStack.get(stackId);
583 if (stack == null) {
584 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
585 + mDisplayId);
586
587 stack = getStackById(stackId);
588 if (stack != null) {
589 // It's already attached to the display...clear mDeferRemoval and move stack to
590 // appropriate z-order on display as needed.
591 stack.mDeferRemoval = false;
592 moveStack(stack, onTop);
593 attachedToDisplay = true;
594 } else {
595 stack = new TaskStack(mService, stackId);
596 }
597
598 mService.mStackIdToStack.put(stackId, stack);
599 if (stackId == DOCKED_STACK_ID) {
600 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
601 }
602 } else {
603 final DisplayContent currentDC = stack.getDisplayContent();
604 if (currentDC != null) {
605 throw new IllegalStateException("Trying to add stackId=" + stackId
606 + "to displayId=" + mDisplayId + ", but it's already attached to displayId="
607 + currentDC.getDisplayId());
608 }
609 }
610
611 if (!attachedToDisplay) {
612 mTaskStackContainers.addStackToDisplay(stack, onTop);
613 }
614
615 if (stack.getRawFullscreen()) {
616 return null;
617 }
618 final Rect bounds = new Rect();
619 stack.getRawBounds(bounds);
620 return bounds;
621 }
622
623 /** Removes the stack from the display and prepares for changing the parent. */
624 private void removeStackFromDisplay(TaskStack stack) {
625 mTaskStackContainers.removeStackFromDisplay(stack);
626 }
627
628 /** Moves the stack to this display and returns the updated bounds. */
629 Rect moveStackToDisplay(TaskStack stack) {
630 final DisplayContent currentDisplayContent = stack.getDisplayContent();
631 if (currentDisplayContent == null) {
632 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
633 + " which is not currently attached to any display");
634 }
635 if (stack.getDisplayContent().getDisplayId() == mDisplayId) {
636 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
637 + " to its current displayId=" + mDisplayId);
638 }
639
640 currentDisplayContent.removeStackFromDisplay(stack);
641 return addStackToDisplay(stack.mStackId, true /* onTop */);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800642 }
643
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800644 void moveStack(TaskStack stack, boolean toTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700645 mTaskStackContainers.moveStack(stack, toTop);
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700646 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700647
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700648 @Override
649 protected void addChild(DisplayChildWindowContainer child,
650 Comparator<DisplayChildWindowContainer> comparator) {
651 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
652 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700653
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700654 @Override
655 protected void addChild(DisplayChildWindowContainer child, int index) {
656 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
657 }
658
659 @Override
660 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700661 // Only allow removal of direct children from this display if the display is in the process
662 // of been removed.
663 if (mRemovingDisplay) {
664 super.removeChild(child);
665 return;
666 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700667 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800668 }
669
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700670 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700671 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
672 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700673 final int taskId = stack.taskIdFromPoint(x, y);
674 if (taskId != -1) {
675 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -0700676 }
677 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800678 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -0700679 }
680
Chong Zhang8e89b312015-09-09 15:09:30 -0700681 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -0800682 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -0700683 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -0700684 */
Wale Ogunwale15ead902016-09-02 14:30:11 -0700685 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700686 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700687 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700688 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
689 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -0700690 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700691 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -0700692 }
Chong Zhang9184ec62015-09-24 12:32:21 -0700693
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700694 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
695 if (mTmpTaskForResizePointSearchResult.searchDone) {
696 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -0700697 }
698 }
Chong Zhang9184ec62015-09-24 12:32:21 -0700699 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -0700700 }
701
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700702 void setTouchExcludeRegion(Task focusedTask) {
Craig Mautner6601b7b2013-04-29 10:29:11 -0700703 mTouchExcludeRegion.set(mBaseDisplayRect);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700704 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700705 mTmpRect2.setEmpty();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700706 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
707 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700708 stack.setTouchExcludeRegion(
709 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
Craig Mautner6601b7b2013-04-29 10:29:11 -0700710 }
Chong Zhangd8ceb852015-11-11 14:53:41 -0800711 // If we removed the focused task above, add it back and only leave its
712 // outside touch area in the exclusion. TapDectector is not interested in
713 // any touch inside the focused task itself.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700714 if (!mTmpRect2.isEmpty()) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800715 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
716 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800717 final WindowState inputMethod = mService.mInputMethodWindow;
718 if (inputMethod != null && inputMethod.isVisibleLw()) {
719 // If the input method is visible and the user is typing, we don't want these touch
720 // events to be intercepted and used to change focus. This would likely cause a
721 // disappearance of the input method.
722 inputMethod.getTouchableRegion(mTmpRegion);
723 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
724 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800725 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
726 WindowState win = mTapExcludedWindows.get(i);
727 win.getTouchableRegion(mTmpRegion);
728 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
729 }
Jorim Jaggife762342016-10-13 14:33:27 +0200730 if (getDockedStackLocked() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100731 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -0700732 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100733 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
734 }
Craig Mautner1bef3892015-02-17 15:09:47 -0800735 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700736 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -0800737 }
Craig Mautner6601b7b2013-04-29 10:29:11 -0700738 }
739
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700740 void switchUser() {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700741 final int count = mWindows.size();
742 for (int i = 0; i < count; i++) {
743 final WindowState win = mWindows.get(i);
Craig Mautner858d8a62013-04-23 17:08:34 -0700744 if (win.isHiddenFromUserLocked()) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800745 if (DEBUG_VISIBILITY) Slog.w(TAG_WM, "user changing, hiding " + win
Wale Ogunwale498e8c92015-02-13 09:42:46 -0800746 + ", attrs=" + win.mAttrs.type + ", belonging to " + win.mOwnerUid);
Craig Mautner858d8a62013-04-23 17:08:34 -0700747 win.hideLw(false);
748 }
749 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700750
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700751 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
752 mTaskStackContainers.get(stackNdx).switchUser();
Craig Mautner858d8a62013-04-23 17:08:34 -0700753 }
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700754
755 rebuildAppWindowList();
Craig Mautner858d8a62013-04-23 17:08:34 -0700756 }
757
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700758 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700759 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
760 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -0700761 }
762 }
763
764 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800765 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -0700766 }
767
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700768 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800769 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -0700770 }
771
772 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800773 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -0700774 }
775
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700776 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800777 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -0700778 }
779
Wale Ogunwale10124582016-09-15 20:25:50 -0700780 @Override
781 void removeIfPossible() {
782 if (isAnimating()) {
783 mDeferredRemoval = true;
784 return;
Craig Mautner2eb15342013-08-07 13:13:35 -0700785 }
Wale Ogunwale10124582016-09-15 20:25:50 -0700786 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -0700787 }
788
Wale Ogunwale10124582016-09-15 20:25:50 -0700789 @Override
790 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700791 mRemovingDisplay = true;
792 try {
793 super.removeImmediately();
794 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
795 mDimLayerController.close();
796 if (mDisplayId == DEFAULT_DISPLAY) {
797 mService.unregisterPointerEventListener(mTapDetector);
798 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
799 }
800 } finally {
801 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -0800802 }
Craig Mautner95da1082014-02-24 17:54:35 -0800803 }
804
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700805 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -0700806 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700807 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -0700808 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
809
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700810 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -0700811 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -0800812 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700813 return false;
Craig Mautner95da1082014-02-24 17:54:35 -0800814 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700815 return true;
Craig Mautner95da1082014-02-24 17:54:35 -0800816 }
817
Wale Ogunwale10124582016-09-15 20:25:50 -0700818 boolean animateForIme(float interpolatedValue, float animationTarget,
819 float dividerAnimationTarget) {
820 boolean updated = false;
821
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700822 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
823 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700824 if (stack == null || !stack.isAdjustedForIme()) {
825 continue;
826 }
827
828 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
829 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
830 updated = true;
831 } else {
832 mDividerControllerLocked.mLastAnimationProgress =
833 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
834 mDividerControllerLocked.mLastDividerProgress =
835 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
836 updated |= stack.updateAdjustForIme(
837 mDividerControllerLocked.mLastAnimationProgress,
838 mDividerControllerLocked.mLastDividerProgress,
839 false /* force */);
840 }
841 if (interpolatedValue >= 1f) {
842 stack.endImeAdjustAnimation();
843 }
844 }
845
846 return updated;
847 }
848
849 boolean clearImeAdjustAnimation() {
850 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700851 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
852 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700853 if (stack != null && stack.isAdjustedForIme()) {
854 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
855 changed = true;
856 }
857 }
858 return changed;
859 }
860
861 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700862 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
863 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700864 if (stack.isVisible() && stack.isAdjustedForIme()) {
865 stack.beginImeAdjustAnimation();
866 }
867 }
868 }
869
870 void adjustForImeIfNeeded() {
871 final WindowState imeWin = mService.mInputMethodWindow;
872 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
873 && !mDividerControllerLocked.isImeHideRequested();
874 final boolean dockVisible = mService.isStackVisibleLocked(DOCKED_STACK_ID);
875 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
876 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
877 imeTargetStack.getDockSide() : DOCKED_INVALID;
878 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
879 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
880 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
881 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
882 final boolean imeHeightChanged = imeVisible &&
883 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
884
885 // The divider could be adjusted for IME position, or be thinner than usual,
886 // or both. There are three possible cases:
887 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
888 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
889 // - If IME is not visible, divider is not moved and is normal width.
890
891 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700892 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
893 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700894 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
895 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)) {
896 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
897 } else {
898 stack.resetAdjustedForIme(false);
899 }
900 }
901 mDividerControllerLocked.setAdjustedForIme(
902 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
903 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700904 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
905 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700906 stack.resetAdjustedForIme(!dockVisible);
907 }
908 mDividerControllerLocked.setAdjustedForIme(
909 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
910 }
Winson Chung655332c2016-10-31 13:14:28 -0700911 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -0700912 }
913
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700914 void setInputMethodAnimLayerAdjustment(int adj) {
915 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
916 mInputMethodAnimLayerAdjustment = adj;
917 final WindowState imw = mService.mInputMethodWindow;
918 if (imw != null) {
919 imw.adjustAnimLayer(adj);
920 }
921 for (int i = mService.mInputMethodDialogs.size() - 1; i >= 0; i--) {
922 final WindowState dialog = mService.mInputMethodDialogs.get(i);
923 // TODO: This and other places setting mAnimLayer can probably use WS.adjustAnimLayer,
924 // but need to make sure we are not setting things twice for child windows that are
925 // already in the list.
926 dialog.mWinAnimator.mAnimLayer = dialog.mLayer + adj;
927 if (DEBUG_LAYERS) Slog.v(TAG_WM, "IM win " + imw
928 + " anim layer: " + dialog.mWinAnimator.mAnimLayer);
929 }
930 }
931
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700932 /**
933 * If a window that has an animation specifying a colored background and the current wallpaper
934 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
935 * suddenly disappear.
936 */
937 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
938 for (int i = mWindows.size() - 1; i >= 0; --i) {
939 final WindowState win = mWindows.get(i);
940 if (win.mIsWallpaper && win.isVisibleNow()) {
941 return win.mWinAnimator.mAnimLayer;
942 }
943 }
944 return winAnimator.mAnimLayer;
945 }
946
Wale Ogunwale10124582016-09-15 20:25:50 -0700947 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700948 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
949 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -0700950 stack.prepareFreezingTaskBounds();
951 }
952 }
953
Wale Ogunwale94744212015-09-21 19:01:47 -0700954 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700955 getLogicalDisplayRect(mTmpRect, newRotation);
956
957 // Compute a transform matrix to undo the coordinate space transformation,
958 // and present the window at the same physical position it previously occupied.
959 final int deltaRotation = deltaRotation(newRotation, oldRotation);
960 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
961
962 mTmpRectF.set(bounds);
963 mTmpMatrix.mapRect(mTmpRectF);
964 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -0700965 }
966
Wale Ogunwale4a02d812015-02-12 23:01:38 -0800967 static int deltaRotation(int oldRotation, int newRotation) {
968 int delta = newRotation - oldRotation;
969 if (delta < 0) delta += 4;
970 return delta;
971 }
972
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700973 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700974 Matrix outMatrix) {
975 // For rotations without Z-ordering we don't need the target rectangle's position.
976 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
977 displayHeight, outMatrix);
978 }
979
980 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
981 float displayWidth, float displayHeight, Matrix outMatrix) {
982 switch (rotation) {
983 case ROTATION_0:
984 outMatrix.reset();
985 break;
986 case ROTATION_270:
987 outMatrix.setRotate(270, 0, 0);
988 outMatrix.postTranslate(0, displayHeight);
989 outMatrix.postTranslate(rectTop, 0);
990 break;
991 case ROTATION_180:
992 outMatrix.reset();
993 break;
994 case ROTATION_90:
995 outMatrix.setRotate(90, 0, 0);
996 outMatrix.postTranslate(displayWidth, 0);
997 outMatrix.postTranslate(-rectTop, rectLeft);
998 break;
999 }
1000 }
1001
Craig Mautnera91f9e22012-09-14 16:22:08 -07001002 public void dump(String prefix, PrintWriter pw) {
1003 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
1004 final String subPrefix = " " + prefix;
1005 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
1006 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
1007 pw.print("dpi");
1008 if (mInitialDisplayWidth != mBaseDisplayWidth
1009 || mInitialDisplayHeight != mBaseDisplayHeight
1010 || mInitialDisplayDensity != mBaseDisplayDensity) {
1011 pw.print(" base=");
1012 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
1013 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
1014 }
Jeff Brownd46747a2015-04-15 19:02:36 -07001015 if (mDisplayScalingDisabled) {
1016 pw.println(" noscale");
1017 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07001018 pw.print(" cur=");
1019 pw.print(mDisplayInfo.logicalWidth);
1020 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
1021 pw.print(" app=");
1022 pw.print(mDisplayInfo.appWidth);
1023 pw.print("x"); pw.print(mDisplayInfo.appHeight);
1024 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
1025 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
1026 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
1027 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001028 pw.println(subPrefix + "deferred=" + mDeferredRemoval
1029 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001030
Craig Mautnerdc548482014-02-05 13:35:24 -08001031 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001032 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001033 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
1034 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001035 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001036 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001037
Craig Mautnerdc548482014-02-05 13:35:24 -08001038 pw.println();
1039 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07001040 pw.println();
1041 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08001042 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001043 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07001044 pw.print(" Exiting #"); pw.print(i);
1045 pw.print(' '); pw.print(token);
1046 pw.println(':');
1047 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08001048 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07001049 }
Craig Mautner59c00972012-07-30 12:10:24 -07001050 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001051 mDimLayerController.dump(prefix, pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -07001052 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001053 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07001054 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07001055 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001056
1057 if (mInputMethodAnimLayerAdjustment != 0) {
1058 pw.println(subPrefix
1059 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
1060 }
Craig Mautner59c00972012-07-30 12:10:24 -07001061 }
Craig Mautnere0a38842013-12-16 16:14:02 -08001062
1063 @Override
1064 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001065 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001066 }
1067
1068 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001069 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08001070 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07001071
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001072 /**
1073 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
1074 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -07001075 TaskStack getDockedStackLocked() {
Wale Ogunwalee45899a2015-10-01 11:30:34 -07001076 final TaskStack stack = mService.mStackIdToStack.get(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001077 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -07001078 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001079
1080 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001081 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
Jorim Jaggife762342016-10-13 14:33:27 +02001082 * visible.
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001083 */
Jorim Jaggife762342016-10-13 14:33:27 +02001084 TaskStack getDockedStackIgnoringVisibility() {
1085 return mService.mStackIdToStack.get(DOCKED_STACK_ID);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -08001086 }
1087
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001088 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001089 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
1090 WindowState touchedWin = null;
1091 final int x = (int) xf;
1092 final int y = (int) yf;
1093
1094 for (int i = mWindows.size() - 1; i >= 0; i--) {
1095 WindowState window = mWindows.get(i);
1096 final int flags = window.mAttrs.flags;
1097 if (!window.isVisibleLw()) {
1098 continue;
1099 }
1100 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
1101 continue;
1102 }
1103
1104 window.getVisibleBounds(mTmpRect);
1105 if (!mTmpRect.contains(x, y)) {
1106 continue;
1107 }
1108
1109 window.getTouchableRegion(mTmpRegion);
1110
1111 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
1112 if (mTmpRegion.contains(x, y) || touchFlags == 0) {
1113 touchedWin = window;
1114 break;
1115 }
1116 }
1117
1118 return touchedWin;
1119 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07001120
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001121 boolean canAddToastWindowForUid(int uid) {
1122 // We allow one toast window per UID being shown at a time.
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001123 final int windowCount = mWindows.size();
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001124 for (int i = 0; i < windowCount; i++) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001125 final WindowState window = mWindows.get(i);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001126 if (window.mAttrs.type == TYPE_TOAST && window.mOwnerUid == uid
Svetoslav Ganov200adfb2016-10-18 13:29:27 -07001127 && !window.mPermanentlyHidden && !window.mWindowRemovalAllowed) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001128 return false;
1129 }
1130 }
1131 return true;
1132 }
1133
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001134 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001135 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
1136 return;
1137 }
1138 final int lostFocusUid = oldFocus.mOwnerUid;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001139 final int windowCount = mWindows.size();
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001140 final Handler handler = mService.mH;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001141 for (int i = 0; i < windowCount; i++) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001142 final WindowState window = mWindows.get(i);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001143 if (window.mAttrs.type == TYPE_TOAST && window.mOwnerUid == lostFocusUid) {
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001144 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, window)) {
1145 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, window),
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001146 window.mAttrs.hideTimeoutMilliseconds);
1147 }
1148 }
1149 }
1150 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001151
1152 WindowState findFocusedWindow() {
1153 final AppWindowToken focusedApp = mService.mFocusedApp;
1154
1155 for (int i = mWindows.size() - 1; i >= 0; i--) {
1156 final WindowState win = mWindows.get(i);
1157
1158 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + i + " = " + win
1159 + ", flags=" + win.mAttrs.flags + ", canReceive=" + win.canReceiveKeys());
1160
1161 if (!win.canReceiveKeys()) {
1162 continue;
1163 }
1164
1165 final AppWindowToken wtoken = win.mAppToken;
1166
1167 // If this window's application has been removed, just skip it.
1168 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
1169 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
1170 + (wtoken.removed ? "removed" : "sendingToBottom"));
1171 continue;
1172 }
1173
1174 if (focusedApp == null) {
1175 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
1176 + " using new focus @ " + i + " = " + win);
1177 return win;
1178 }
1179
1180 if (!focusedApp.windowsAreFocusable()) {
1181 // Current focused app windows aren't focusable...
1182 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
1183 + " focusable using new focus @ " + i + " = " + win);
1184 return win;
1185 }
1186
1187 // Descend through all of the app tokens and find the first that either matches
1188 // win.mAppToken (return win) or mFocusedApp (return null).
1189 if (wtoken != null && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001190 if (focusedApp.compareTo(wtoken) > 0) {
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001191 // App stack below focused app stack. No focus for you!!!
1192 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
1193 "findFocusedWindow: Reached focused app=" + focusedApp);
1194 return null;
1195 }
1196 }
1197
1198 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ "
1199 + i + " = " + win);
1200 return win;
1201 }
1202
1203 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
1204 return null;
1205 }
Wale Ogunwaleec731152016-09-08 20:18:57 -07001206
Wale Ogunwale07bcab72016-10-14 15:30:09 -07001207 void addAppWindowToWindowList(final WindowState win) {
Wale Ogunwaleec731152016-09-08 20:18:57 -07001208 final IWindow client = win.mClient;
1209
1210 WindowList tokenWindowList = getTokenWindowsOnDisplay(win.mToken);
1211 if (!tokenWindowList.isEmpty()) {
Wale Ogunwale07bcab72016-10-14 15:30:09 -07001212 addAppWindowExisting(win, tokenWindowList);
1213 return;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001214 }
1215
1216 // No windows from this token on this display
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001217 if (localLOGV) Slog.v(TAG_WM, "Figuring out where to add app window "
Wale Ogunwaleec731152016-09-08 20:18:57 -07001218 + client.asBinder() + " (token=" + this + ")");
1219
1220 final WindowToken wToken = win.mToken;
1221
1222 // Figure out where the window should go, based on the order of applications.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001223 mTmpGetWindowOnDisplaySearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001224 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1225 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001226 stack.getWindowOnDisplayBeforeToken(this, wToken, mTmpGetWindowOnDisplaySearchResult);
1227 if (mTmpGetWindowOnDisplaySearchResult.reachedToken) {
Wale Ogunwaleec731152016-09-08 20:18:57 -07001228 // We have reach the token we are interested in. End search.
1229 break;
1230 }
1231 }
1232
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001233 WindowState pos = mTmpGetWindowOnDisplaySearchResult.foundWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001234
1235 // We now know the index into the apps. If we found an app window above, that gives us the
1236 // position; else we need to look some more.
1237 if (pos != null) {
1238 // Move behind any windows attached to this one.
Wale Ogunwale02319a62016-09-26 15:21:22 -07001239 final WindowToken atoken = getWindowToken(pos.mClient.asBinder());
Wale Ogunwaleec731152016-09-08 20:18:57 -07001240 if (atoken != null) {
1241 tokenWindowList = getTokenWindowsOnDisplay(atoken);
1242 final int NC = tokenWindowList.size();
1243 if (NC > 0) {
1244 WindowState bottom = tokenWindowList.get(0);
1245 if (bottom.mSubLayer < 0) {
1246 pos = bottom;
1247 }
1248 }
1249 }
1250 addWindowToListBefore(win, pos);
Wale Ogunwale07bcab72016-10-14 15:30:09 -07001251 return;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001252 }
1253
1254 // Continue looking down until we find the first token that has windows on this display.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001255 mTmpGetWindowOnDisplaySearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001256 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1257 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001258 stack.getWindowOnDisplayAfterToken(this, wToken, mTmpGetWindowOnDisplaySearchResult);
1259 if (mTmpGetWindowOnDisplaySearchResult.foundWindow != null) {
Wale Ogunwaleec731152016-09-08 20:18:57 -07001260 // We have found a window after the token. End search.
1261 break;
1262 }
1263 }
1264
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001265 pos = mTmpGetWindowOnDisplaySearchResult.foundWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001266
1267 if (pos != null) {
1268 // Move in front of any windows attached to this one.
Wale Ogunwale02319a62016-09-26 15:21:22 -07001269 final WindowToken atoken = getWindowToken(pos.mClient.asBinder());
Wale Ogunwaleec731152016-09-08 20:18:57 -07001270 if (atoken != null) {
1271 final WindowState top = atoken.getTopWindow();
1272 if (top != null && top.mSubLayer >= 0) {
1273 pos = top;
1274 }
1275 }
1276 addWindowToListAfter(win, pos);
Wale Ogunwale07bcab72016-10-14 15:30:09 -07001277 return;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001278 }
1279
1280 // Just search for the start of this layer.
1281 final int myLayer = win.mBaseLayer;
1282 int i;
1283 for (i = mWindows.size() - 1; i >= 0; --i) {
1284 final WindowState w = mWindows.get(i);
1285 // Dock divider shares the base layer with application windows, but we want to always
1286 // keep it above the application windows. The sharing of the base layer is intended
1287 // for window animations, which need to be above the dock divider for the duration
1288 // of the animation.
1289 if (w.mBaseLayer <= myLayer && w.mAttrs.type != TYPE_DOCK_DIVIDER) {
1290 break;
1291 }
1292 }
1293 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1294 "Based on layer: Adding window " + win + " at " + (i + 1) + " of "
1295 + mWindows.size());
1296 mWindows.add(i + 1, win);
1297 mService.mWindowsChanged = true;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001298 }
1299
1300 /** Adds this non-app window to the window list. */
1301 void addNonAppWindowToWindowList(WindowState win) {
1302 // Figure out where window should go, based on layer.
1303 int i;
1304 for (i = mWindows.size() - 1; i >= 0; i--) {
1305 final WindowState otherWin = mWindows.get(i);
1306 if (otherWin.getBaseType() != TYPE_WALLPAPER && otherWin.mBaseLayer <= win.mBaseLayer) {
1307 // Wallpaper wanders through the window list, for example to position itself
1308 // directly behind keyguard. Because of this it will break the ordering based on
1309 // WindowState.mBaseLayer. There might windows with higher mBaseLayer behind it and
1310 // we don't want the new window to appear above them. An example of this is adding
1311 // of the docked stack divider. Consider a scenario with the following ordering (top
1312 // to bottom): keyguard, wallpaper, assist preview, apps. We want the dock divider
1313 // to land below the assist preview, so the dock divider must ignore the wallpaper,
1314 // with which it shares the base layer.
1315 break;
1316 }
1317 }
1318
1319 i++;
1320 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1321 "Free window: Adding window " + this + " at " + i + " of " + mWindows.size());
1322 mWindows.add(i, win);
1323 mService.mWindowsChanged = true;
1324 }
1325
Wale Ogunwaleb9a07c32016-10-12 14:55:56 -07001326 void addToWindowList(WindowState win, int index) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001327 mService.mWindowsChanged = true;
Wale Ogunwaleb9a07c32016-10-12 14:55:56 -07001328 mWindows.add(index, win);
1329 }
1330
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001331 boolean removeFromWindowList(WindowState win) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001332 mService.mWindowsChanged = true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001333 return mWindows.remove(win);
1334 }
1335
1336 private int removeWindowAndChildrenFromWindowList(WindowState win, int interestingPos) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001337 int wpos = mWindows.indexOf(win);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001338 if (wpos < 0) {
1339 return interestingPos;
1340 }
1341
1342 if (wpos < interestingPos) interestingPos--;
1343 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG_WM, "Temp removing at " + wpos + ": " + this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001344 mWindows.remove(wpos);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001345 mService.mWindowsChanged = true;
1346 int childWinCount = win.mChildren.size();
1347 while (childWinCount > 0) {
1348 childWinCount--;
1349 final WindowState cw = win.mChildren.get(childWinCount);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001350 int cpos = mWindows.indexOf(cw);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001351 if (cpos >= 0) {
1352 if (cpos < interestingPos) interestingPos--;
1353 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG_WM,
1354 "Temp removing child at " + cpos + ": " + cw);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001355 mWindows.remove(cpos);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001356 }
1357 }
1358 return interestingPos;
1359 }
1360
Wale Ogunwaleec731152016-09-08 20:18:57 -07001361 void addChildWindowToWindowList(WindowState win) {
1362 final WindowState parentWindow = win.getParentWindow();
1363
1364 WindowList windowsOnSameDisplay = getTokenWindowsOnDisplay(win.mToken);
1365
1366 // Figure out this window's ordering relative to the parent window.
1367 final int wCount = windowsOnSameDisplay.size();
1368 final int sublayer = win.mSubLayer;
1369 int largestSublayer = Integer.MIN_VALUE;
1370 WindowState windowWithLargestSublayer = null;
1371 int i;
1372 for (i = 0; i < wCount; i++) {
1373 WindowState w = windowsOnSameDisplay.get(i);
1374 final int wSublayer = w.mSubLayer;
1375 if (wSublayer >= largestSublayer) {
1376 largestSublayer = wSublayer;
1377 windowWithLargestSublayer = w;
1378 }
1379 if (sublayer < 0) {
1380 // For negative sublayers, we go below all windows in the same sublayer.
1381 if (wSublayer >= sublayer) {
1382 addWindowToListBefore(win, wSublayer >= 0 ? parentWindow : w);
1383 break;
1384 }
1385 } else {
1386 // For positive sublayers, we go above all windows in the same sublayer.
1387 if (wSublayer > sublayer) {
1388 addWindowToListBefore(win, w);
1389 break;
1390 }
1391 }
1392 }
1393 if (i >= wCount) {
1394 if (sublayer < 0) {
1395 addWindowToListBefore(win, parentWindow);
1396 } else {
1397 addWindowToListAfter(win,
1398 largestSublayer >= 0 ? windowWithLargestSublayer : parentWindow);
1399 }
1400 }
1401 }
1402
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001403 /** Updates the layer assignment of windows on this display. */
1404 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08001405 mLayersController.assignWindowLayers(this);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001406 if (setLayoutNeeded) {
1407 setLayoutNeeded();
1408 }
1409 }
1410
Wale Ogunwale0303c572016-10-20 10:16:29 -07001411 void adjustWallpaperWindows() {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001412 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwale0303c572016-10-20 10:16:29 -07001413 }
1414
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001415 /**
1416 * Z-orders the display window list so that:
1417 * <ul>
1418 * <li>Any windows that are currently below the wallpaper window stay below the wallpaper
1419 * window.
1420 * <li>Exiting application windows are at the bottom, but above the wallpaper window.
1421 * <li>All other application windows are above the exiting application windows and ordered based
1422 * on the ordering of their stacks and tasks on the display.
1423 * <li>Non-application windows are at the very top.
1424 * </ul>
1425 * <p>
1426 * NOTE: This isn't a complete picture of what the user see. Further manipulation of the window
1427 * surface layering is done in {@link WindowLayersController}.
1428 */
1429 void rebuildAppWindowList() {
1430 int count = mWindows.size();
1431 int i;
1432 int lastBelow = -1;
1433 int numRemoved = 0;
1434
1435 if (mRebuildTmp.length < count) {
1436 mRebuildTmp = new WindowState[count + 10];
1437 }
1438
1439 // First remove all existing app windows.
1440 i = 0;
1441 while (i < count) {
1442 final WindowState w = mWindows.get(i);
1443 if (w.mAppToken != null) {
1444 final WindowState win = mWindows.remove(i);
1445 win.mRebuilding = true;
1446 mRebuildTmp[numRemoved] = win;
1447 mService.mWindowsChanged = true;
1448 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG_WM, "Rebuild removing window: " + win);
1449 count--;
1450 numRemoved++;
1451 continue;
1452 } else if (lastBelow == i-1) {
1453 if (w.mAttrs.type == TYPE_WALLPAPER) {
1454 lastBelow = i;
1455 }
1456 }
1457 i++;
1458 }
1459
1460 // Keep whatever windows were below the app windows still below, by skipping them.
1461 lastBelow++;
1462 i = lastBelow;
1463
1464 // First add all of the exiting app tokens... these are no longer in the main app list,
1465 // but still have windows shown. We put them in the back because now that the animation is
1466 // over we no longer will care about them.
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001467 final int numStacks = mTaskStackContainers.size();
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001468 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001469 AppTokenList exitingAppTokens = mTaskStackContainers.get(stackNdx).mExitingAppTokens;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001470 int NT = exitingAppTokens.size();
1471 for (int j = 0; j < NT; j++) {
Wale Ogunwale360a8bc2016-10-10 13:25:26 -07001472 i = exitingAppTokens.get(j).rebuildWindowListUnchecked(i);
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001473 }
1474 }
1475
1476 // And add in the still active app tokens in Z order.
1477 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001478 i = mTaskStackContainers.get(stackNdx).rebuildWindowList(i);
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001479 }
1480
1481 i -= lastBelow;
1482 if (i != numRemoved) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001483 setLayoutNeeded();
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001484 Slog.w(TAG_WM, "On display=" + mDisplayId + " Rebuild removed " + numRemoved
1485 + " windows but added " + i + " rebuildAppWindowListLocked() "
1486 + " callers=" + Debug.getCallers(10));
1487 for (i = 0; i < numRemoved; i++) {
1488 WindowState ws = mRebuildTmp[i];
1489 if (ws.mRebuilding) {
1490 StringWriter sw = new StringWriter();
1491 PrintWriter pw = new FastPrintWriter(sw, false, 1024);
1492 ws.dump(pw, "", true);
1493 pw.flush();
1494 Slog.w(TAG_WM, "This window was lost: " + ws);
1495 Slog.w(TAG_WM, sw.toString());
1496 ws.mWinAnimator.destroySurfaceLocked();
1497 }
1498 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001499 Slog.w(TAG_WM, "Current window hierarchy:");
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001500 dumpChildrenNames();
1501 Slog.w(TAG_WM, "Final window list:");
1502 dumpWindows();
1503 }
1504 Arrays.fill(mRebuildTmp, null);
1505 }
1506
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001507 /** Rebuilds the display's window list and does a relayout if something changed. */
1508 void rebuildAppWindowsAndLayoutIfNeeded() {
1509 mTmpWindows.clear();
1510 mTmpWindows.addAll(mWindows);
1511
1512 rebuildAppWindowList();
1513
1514 // Set displayContent.mLayoutNeeded if window order changed.
1515 final int tmpSize = mTmpWindows.size();
1516 final int winSize = mWindows.size();
1517 int tmpNdx = 0, winNdx = 0;
1518 while (tmpNdx < tmpSize && winNdx < winSize) {
1519 // Skip over all exiting windows, they've been moved out of order.
1520 WindowState tmp;
1521 do {
1522 tmp = mTmpWindows.get(tmpNdx++);
1523 } while (tmpNdx < tmpSize && tmp.mAppToken != null && tmp.mAppToken.mIsExiting);
1524
1525 WindowState win;
1526 do {
1527 win = mWindows.get(winNdx++);
1528 } while (winNdx < winSize && win.mAppToken != null && win.mAppToken.mIsExiting);
1529
1530 if (tmp != win) {
1531 // Window order changed.
1532 setLayoutNeeded();
1533 break;
1534 }
1535 }
1536 if (tmpNdx != winNdx) {
1537 // One list was different from the other.
1538 setLayoutNeeded();
1539 }
1540 mTmpWindows.clear();
1541
1542 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
1543 false /*updateInputWindows*/)) {
1544 assignWindowLayers(false /* setLayoutNeeded */);
1545 }
1546
1547 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
1548 mService.mWindowPlacerLocked.performSurfacePlacement();
1549 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
1550 }
1551
1552 void updateInputWindows(InputMonitor inputMonitor, WindowState inputFocus, boolean inDrag) {
1553 final InputConsumerImpl navInputConsumer =
1554 mService.mInputMonitor.getInputConsumer(INPUT_CONSUMER_NAVIGATION, mDisplayId);
1555 final InputConsumerImpl pipInputConsumer =
1556 mService.mInputMonitor.getInputConsumer(INPUT_CONSUMER_PIP, mDisplayId);
1557 final InputConsumerImpl wallpaperInputConsumer =
1558 mService.mInputMonitor.getInputConsumer(INPUT_CONSUMER_WALLPAPER, mDisplayId);
1559 boolean addInputConsumerHandle = navInputConsumer != null;
1560 boolean addPipInputConsumerHandle = pipInputConsumer != null;
1561 boolean addWallpaperInputConsumerHandle = wallpaperInputConsumer != null;
1562 final Rect pipTouchableBounds = addPipInputConsumerHandle ? new Rect() : null;
1563 boolean disableWallpaperTouchEvents = false;
1564
1565 for (int winNdx = mWindows.size() - 1; winNdx >= 0; --winNdx) {
1566 final WindowState child = mWindows.get(winNdx);
1567 final InputChannel inputChannel = child.mInputChannel;
1568 final InputWindowHandle inputWindowHandle = child.mInputWindowHandle;
1569 if (inputChannel == null || inputWindowHandle == null || child.mRemoved
1570 || child.isAdjustedForMinimizedDock()) {
1571 // Skip this window because it cannot possibly receive input.
1572 continue;
1573 }
1574
1575 if (addPipInputConsumerHandle
1576 && child.getStackId() == PINNED_STACK_ID
1577 && inputWindowHandle.layer <= pipInputConsumer.mWindowHandle.layer) {
1578 // Update the bounds of the Pip input consumer to match the Pinned stack
1579 child.getStack().getBounds(pipTouchableBounds);
1580 pipInputConsumer.mWindowHandle.touchableRegion.set(pipTouchableBounds);
1581 inputMonitor.addInputWindowHandle(pipInputConsumer.mWindowHandle);
1582 addPipInputConsumerHandle = false;
1583 }
1584
1585 if (addInputConsumerHandle
1586 && inputWindowHandle.layer <= navInputConsumer.mWindowHandle.layer) {
1587 inputMonitor.addInputWindowHandle(navInputConsumer.mWindowHandle);
1588 addInputConsumerHandle = false;
1589 }
1590
1591 if (addWallpaperInputConsumerHandle) {
1592 if (child.mAttrs.type == TYPE_WALLPAPER && child.isVisibleLw()) {
1593 // Add the wallpaper input consumer above the first visible wallpaper.
1594 inputMonitor.addInputWindowHandle(wallpaperInputConsumer.mWindowHandle);
1595 addWallpaperInputConsumerHandle = false;
1596 }
1597 }
1598
1599 final int flags = child.mAttrs.flags;
1600 final int privateFlags = child.mAttrs.privateFlags;
1601 final int type = child.mAttrs.type;
1602
1603 final boolean hasFocus = child == inputFocus;
1604 final boolean isVisible = child.isVisibleLw();
1605 if ((privateFlags & PRIVATE_FLAG_DISABLE_WALLPAPER_TOUCH_EVENTS) != 0) {
1606 disableWallpaperTouchEvents = true;
1607 }
1608 final boolean hasWallpaper = mWallpaperController.isWallpaperTarget(child)
1609 && (privateFlags & PRIVATE_FLAG_KEYGUARD) == 0
1610 && !disableWallpaperTouchEvents;
1611
1612 // If there's a drag in progress and 'child' is a potential drop target,
1613 // make sure it's been told about the drag
1614 if (inDrag && isVisible && isDefaultDisplay) {
1615 mService.mDragState.sendDragStartedIfNeededLw(child);
1616 }
1617
1618 inputMonitor.addInputWindowHandle(
1619 inputWindowHandle, child, flags, type, isVisible, hasFocus, hasWallpaper);
1620 }
1621
1622 if (addWallpaperInputConsumerHandle) {
1623 // No visible wallpaper found, add the wallpaper input consumer at the end.
1624 inputMonitor.addInputWindowHandle(wallpaperInputConsumer.mWindowHandle);
1625 }
1626 }
1627
1628 /** Returns true if a leaked surface was destroyed */
1629 boolean destroyLeakedSurfaces() {
1630 boolean leakedSurface = false;
1631 final int numWindows = mWindows.size();
1632 for (int winNdx = 0; winNdx < numWindows; ++winNdx) {
1633 final WindowState ws = mWindows.get(winNdx);
1634 final WindowStateAnimator wsa = ws.mWinAnimator;
1635 if (wsa.mSurfaceController == null) {
1636 continue;
1637 }
1638 if (!mService.mSessions.contains(wsa.mSession)) {
1639 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
1640 + ws + " surface=" + wsa.mSurfaceController
1641 + " token=" + ws.mToken
1642 + " pid=" + ws.mSession.mPid
1643 + " uid=" + ws.mSession.mUid);
1644 wsa.destroySurface();
1645 mService.mForceRemoves.add(ws);
1646 leakedSurface = true;
1647 } else if (ws.mAppToken != null && ws.mAppToken.clientHidden) {
1648 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
1649 + ws + " surface=" + wsa.mSurfaceController
1650 + " token=" + ws.mAppToken
1651 + " saved=" + ws.hasSavedSurface());
1652 if (SHOW_TRANSACTIONS) logSurface(ws, "LEAK DESTROY", false);
1653 wsa.destroySurface();
1654 leakedSurface = true;
1655 }
1656 }
1657
1658 return leakedSurface;
1659 }
1660
Wale Ogunwaleec731152016-09-08 20:18:57 -07001661 /** Return the list of Windows on this display associated with the input token. */
1662 WindowList getTokenWindowsOnDisplay(WindowToken token) {
1663 final WindowList windowList = new WindowList();
1664 final int count = mWindows.size();
1665 for (int i = 0; i < count; i++) {
1666 final WindowState win = mWindows.get(i);
1667 if (win.mToken == token) {
1668 windowList.add(win);
1669 }
1670 }
1671 return windowList;
1672 }
1673
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001674 private void reAddToWindowList(WindowState win) {
1675 win.mToken.addWindow(win);
1676 // This is a hack to get all of the child windows added as well at the right position. Child
1677 // windows should be rare and this case should be rare, so it shouldn't be that big a deal.
1678 int wpos = mWindows.indexOf(win);
1679 if (wpos >= 0) {
1680 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG_WM, "ReAdd removing from " + wpos + ": " + win);
1681 mWindows.remove(wpos);
1682 mService.mWindowsChanged = true;
1683 win.reAddWindow(wpos);
1684 }
1685 }
1686
1687 void moveInputMethodDialogs(int pos) {
1688 ArrayList<WindowState> dialogs = mService.mInputMethodDialogs;
1689
1690 final int N = dialogs.size();
1691 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Removing " + N + " dialogs w/pos=" + pos);
1692 for (int i = 0; i < N; i++) {
1693 pos = removeWindowAndChildrenFromWindowList(dialogs.get(i), pos);
1694 }
1695 if (DEBUG_INPUT_METHOD) {
1696 Slog.v(TAG_WM, "Window list w/pos=" + pos);
1697 logWindowList(mWindows, " ");
1698 }
1699
1700 WindowState ime = mService.mInputMethodWindow;
1701 if (pos >= 0) {
1702 // Skip windows owned by the input method.
1703 if (ime != null) {
1704 while (pos < mWindows.size()) {
1705 WindowState wp = mWindows.get(pos);
1706 if (wp == ime || wp.getParentWindow() == ime) {
1707 pos++;
1708 continue;
1709 }
1710 break;
1711 }
1712 }
1713 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Adding " + N + " dialogs at pos=" + pos);
1714 for (int i=0; i<N; i++) {
1715 WindowState win = dialogs.get(i);
1716 pos = win.reAddWindow(pos);
1717 }
1718 if (DEBUG_INPUT_METHOD) {
1719 Slog.v(TAG_WM, "Final window list:");
1720 logWindowList(mWindows, " ");
1721 }
1722 return;
1723 }
1724 for (int i=0; i<N; i++) {
1725 WindowState win = dialogs.get(i);
1726 reAddToWindowList(win);
1727 if (DEBUG_INPUT_METHOD) {
1728 Slog.v(TAG_WM, "No IM target, final list:");
1729 logWindowList(mWindows, " ");
1730 }
1731 }
1732 }
1733
1734 boolean moveInputMethodWindowsIfNeeded(boolean needAssignLayers) {
1735 final WindowState imWin = mService.mInputMethodWindow;
1736 final int DN = mService.mInputMethodDialogs.size();
1737 if (imWin == null && DN == 0) {
1738 return false;
1739 }
1740
1741 // TODO(multidisplay): IMEs are only supported on the default display.
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001742 int imPos = findDesiredInputMethodWindowIndex(true);
1743 if (imPos >= 0) {
1744 // In this case, the input method windows are to be placed
1745 // immediately above the window they are targeting.
1746
1747 // First check to see if the input method windows are already
1748 // located here, and contiguous.
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001749 final int N = mWindows.size();
1750 final WindowState firstImWin = imPos < N ? mWindows.get(imPos) : null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001751
1752 // Figure out the actual input method window that should be
1753 // at the bottom of their stack.
1754 WindowState baseImWin = imWin != null ? imWin : mService.mInputMethodDialogs.get(0);
1755 final WindowState cw = baseImWin.getBottomChild();
1756 if (cw != null && cw.mSubLayer < 0) {
1757 baseImWin = cw;
1758 }
1759
1760 if (firstImWin == baseImWin) {
1761 // The windows haven't moved... but are they still contiguous?
1762 // First find the top IM window.
1763 int pos = imPos+1;
1764 while (pos < N) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001765 if (!(mWindows.get(pos)).mIsImWindow) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001766 break;
1767 }
1768 pos++;
1769 }
1770 pos++;
1771 // Now there should be no more input method windows above.
1772 while (pos < N) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001773 if ((mWindows.get(pos)).mIsImWindow) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001774 break;
1775 }
1776 pos++;
1777 }
1778 if (pos >= N) {
1779 return false;
1780 }
1781 }
1782
1783 if (imWin != null) {
1784 if (DEBUG_INPUT_METHOD) {
1785 Slog.v(TAG_WM, "Moving IM from " + imPos);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001786 logWindowList(mWindows, " ");
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001787 }
1788 imPos = removeWindowAndChildrenFromWindowList(imWin, imPos);
1789 if (DEBUG_INPUT_METHOD) {
1790 Slog.v(TAG_WM, "List after removing with new pos " + imPos + ":");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001791 logWindowList(mWindows, " ");
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001792 }
1793 imWin.reAddWindow(imPos);
1794 if (DEBUG_INPUT_METHOD) {
1795 Slog.v(TAG_WM, "List after moving IM to " + imPos + ":");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001796 logWindowList(mWindows, " ");
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001797 }
1798 if (DN > 0) moveInputMethodDialogs(imPos+1);
1799 } else {
1800 moveInputMethodDialogs(imPos);
1801 }
1802
1803 } else {
1804 // In this case, the input method windows go in a fixed layer,
1805 // because they aren't currently associated with a focus window.
1806
1807 if (imWin != null) {
1808 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Moving IM from " + imPos);
1809 removeWindowAndChildrenFromWindowList(imWin, 0);
1810 reAddToWindowList(imWin);
1811 if (DEBUG_INPUT_METHOD) {
1812 Slog.v(TAG_WM, "List with no IM target:");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001813 logWindowList(mWindows, " ");
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001814 }
1815 if (DN > 0) moveInputMethodDialogs(-1);
1816 } else {
1817 moveInputMethodDialogs(-1);
1818 }
1819
1820 }
1821
1822 if (needAssignLayers) {
1823 assignWindowLayers(false /* setLayoutNeeded */);
1824 }
1825
1826 return true;
1827 }
1828
1829 /**
1830 * Dig through the WindowStates and find the one that the Input Method will target.
1831 * @param willMove
1832 * @return The index+1 in mWindows of the discovered target.
1833 */
1834 int findDesiredInputMethodWindowIndex(boolean willMove) {
1835 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
1836 // same display. Or even when the current IME/target are not on the same screen as the next
1837 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001838 WindowState w = null;
1839 int i;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001840 for (i = mWindows.size() - 1; i >= 0; --i) {
1841 final WindowState win = mWindows.get(i);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001842
1843 if (DEBUG_INPUT_METHOD && willMove) Slog.i(TAG_WM, "Checking window @" + i
1844 + " " + win + " fl=0x" + Integer.toHexString(win.mAttrs.flags));
1845 if (canBeImeTarget(win)) {
1846 w = win;
1847 //Slog.i(TAG_WM, "Putting input method here!");
1848
1849 // Yet more tricksyness! If this window is a "starting" window, we do actually want
1850 // to be on top of it, but it is not -really- where input will go. So if the caller
1851 // is not actually looking to move the IME, look down below for a real window to
1852 // target...
1853 if (!willMove && w.mAttrs.type == TYPE_APPLICATION_STARTING && i > 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001854 final WindowState wb = mWindows.get(i-1);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001855 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
1856 i--;
1857 w = wb;
1858 }
1859 }
1860 break;
1861 }
1862 }
1863
1864 // Now w is either mWindows[0] or an IME (or null if mWindows is empty).
1865
1866 if (DEBUG_INPUT_METHOD && willMove) Slog.v(TAG_WM, "Proposed new IME target: " + w);
1867
1868 // Now, a special case -- if the last target's window is in the process of exiting, and is
1869 // above the new target, keep on the last target to avoid flicker. Consider for example a
1870 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
1871 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
1872 // scrim.
1873 final WindowState curTarget = mService.mInputMethodTarget;
1874 if (curTarget != null
1875 && curTarget.isDisplayedLw()
1876 && curTarget.isClosing()
1877 && (w == null || curTarget.mWinAnimator.mAnimLayer > w.mWinAnimator.mAnimLayer)) {
1878 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001879 return mWindows.indexOf(curTarget) + 1;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001880 }
1881
1882 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target="
1883 + w + " willMove=" + willMove);
1884
1885 if (willMove && w != null) {
1886 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
1887 if (token != null) {
1888
1889 // Now some fun for dealing with window animations that modify the Z order. We need
1890 // to look at all windows below the current target that are in this app, finding the
1891 // highest visible one in layering.
1892 WindowState highestTarget = null;
1893 int highestPos = 0;
1894 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001895 WindowList curWindows = token.getDisplayContent().mWindows;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001896 int pos = curWindows.indexOf(curTarget);
1897 while (pos >= 0) {
1898 WindowState win = curWindows.get(pos);
1899 if (win.mAppToken != token) {
1900 break;
1901 }
1902 if (!win.mRemoved) {
1903 if (highestTarget == null || win.mWinAnimator.mAnimLayer >
1904 highestTarget.mWinAnimator.mAnimLayer) {
1905 highestTarget = win;
1906 highestPos = pos;
1907 }
1908 }
1909 pos--;
1910 }
1911 }
1912
1913 if (highestTarget != null) {
1914 final AppTransition appTransition = mService.mAppTransition;
1915 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
1916 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
1917 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
1918 + " new layer=" + w.mWinAnimator.mAnimLayer);
1919
1920 if (appTransition.isTransitionSet()) {
1921 // If we are currently setting up for an animation, hold everything until we
1922 // can find out what will happen.
1923 mService.mInputMethodTargetWaitingAnim = true;
1924 mService.mInputMethodTarget = highestTarget;
1925 return highestPos + 1;
1926 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
1927 highestTarget.mWinAnimator.mAnimLayer > w.mWinAnimator.mAnimLayer) {
1928 // If the window we are currently targeting is involved with an animation,
1929 // and it is on top of the next target we will be over, then hold off on
1930 // moving until that is done.
1931 mService.mInputMethodTargetWaitingAnim = true;
1932 mService.mInputMethodTarget = highestTarget;
1933 return highestPos + 1;
1934 }
1935 }
1936 }
1937 }
1938
1939 //Slog.i(TAG_WM, "Placing input method @" + (i+1));
1940 if (w != null) {
1941 if (willMove) {
1942 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
1943 + w + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
1944 mService.mInputMethodTarget = w;
1945 mService.mInputMethodTargetWaitingAnim = false;
1946 if (w.mAppToken != null) {
1947 setInputMethodAnimLayerAdjustment(
1948 w.mAppToken.mAppAnimator.animLayerAdjustment);
1949 } else {
1950 setInputMethodAnimLayerAdjustment(0);
1951 }
1952 }
1953
1954 // If the docked divider is visible, we still need to go through this whole excercise to
1955 // find the appropriate input method target (used for animations and dialog
1956 // adjustments), but for purposes of Z ordering we simply wish to place it above the
1957 // docked divider. Unless it is already above the divider.
1958 final WindowState dockedDivider = mDividerControllerLocked.getWindow();
1959 if (dockedDivider != null && dockedDivider.isVisibleLw()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001960 int dividerIndex = mWindows.indexOf(dockedDivider);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001961 if (dividerIndex > 0 && dividerIndex > i) {
1962 return dividerIndex + 1;
1963 }
1964 }
1965 return i+1;
1966 }
1967 if (willMove) {
1968 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
1969 + " to null." + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
1970 mService.mInputMethodTarget = null;
1971 setInputMethodAnimLayerAdjustment(0);
1972 }
1973 return -1;
1974 }
1975
1976 private static boolean canBeImeTarget(WindowState w) {
1977 final int fl = w.mAttrs.flags & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
1978 final int type = w.mAttrs.type;
1979
1980 if (fl != 0 && fl != (FLAG_NOT_FOCUSABLE | FLAG_ALT_FOCUSABLE_IM)
1981 && type != TYPE_APPLICATION_STARTING) {
1982 return false;
1983 }
1984
1985 if (DEBUG_INPUT_METHOD) {
1986 Slog.i(TAG_WM, "isVisibleOrAdding " + w + ": " + w.isVisibleOrAdding());
1987 if (!w.isVisibleOrAdding()) {
1988 Slog.i(TAG_WM, " mSurfaceController=" + w.mWinAnimator.mSurfaceController
1989 + " relayoutCalled=" + w.mRelayoutCalled
1990 + " viewVis=" + w.mViewVisibility
1991 + " policyVis=" + w.mPolicyVisibility
1992 + " policyVisAfterAnim=" + w.mPolicyVisibilityAfterAnim
1993 + " parentHidden=" + w.isParentWindowHidden()
1994 + " exiting=" + w.mAnimatingExit + " destroying=" + w.mDestroying);
1995 if (w.mAppToken != null) {
1996 Slog.i(TAG_WM, " mAppToken.hiddenRequested=" + w.mAppToken.hiddenRequested);
1997 }
1998 }
1999 }
2000 return w.isVisibleOrAdding();
2001 }
2002
2003 private void logWindowList(final WindowList windows, String prefix) {
2004 int N = windows.size();
2005 while (N > 0) {
2006 N--;
2007 Slog.v(TAG_WM, prefix + "#" + N + ": " + windows.get(N));
2008 }
2009 }
2010
2011 boolean getNeedsMenu(WindowState win, WindowManagerPolicy.WindowState bottom) {
2012 int index = -1;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002013 while (true) {
2014 if (win.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2015 return win.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2016 }
2017 // If we reached the bottom of the range of windows we are considering,
2018 // assume no menu is needed.
2019 if (win == bottom) {
2020 return false;
2021 }
2022 // The current window hasn't specified whether menu key is needed; look behind it.
2023 // First, we may need to determine the starting position.
2024 if (index < 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002025 index = mWindows.indexOf(win);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002026 }
2027 index--;
2028 if (index < 0) {
2029 return false;
2030 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002031 win = mWindows.get(index);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002032 }
2033 }
2034
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002035 void setLayoutNeeded() {
2036 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2037 mLayoutNeeded = true;
2038 }
2039
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002040 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002041 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2042 mLayoutNeeded = false;
2043 }
2044
2045 boolean isLayoutNeeded() {
2046 return mLayoutNeeded;
2047 }
2048
Wale Ogunwale07bcab72016-10-14 15:30:09 -07002049 private void addAppWindowExisting(WindowState win, WindowList tokenWindowList) {
Wale Ogunwaleec731152016-09-08 20:18:57 -07002050
Wale Ogunwaleec731152016-09-08 20:18:57 -07002051 // If this application has existing windows, we simply place the new window on top of
2052 // them... but keep the starting window on top.
2053 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
2054 // Base windows go behind everything else.
2055 final WindowState lowestWindow = tokenWindowList.get(0);
2056 addWindowToListBefore(win, lowestWindow);
Wale Ogunwaleec731152016-09-08 20:18:57 -07002057 } else {
2058 final AppWindowToken atoken = win.mAppToken;
2059 final int windowListPos = tokenWindowList.size();
2060 final WindowState lastWindow = tokenWindowList.get(windowListPos - 1);
2061 if (atoken != null && lastWindow == atoken.startingWindow) {
2062 addWindowToListBefore(win, lastWindow);
Wale Ogunwaleec731152016-09-08 20:18:57 -07002063 } else {
2064 int newIdx = findIdxBasedOnAppTokens(win);
2065 // There is a window above this one associated with the same apptoken note that the
2066 // window could be a floating window that was created later or a window at the top
2067 // of the list of windows associated with this token.
2068 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
2069 "not Base app: Adding window " + win + " at " + (newIdx + 1) + " of "
2070 + mWindows.size());
2071 mWindows.add(newIdx + 1, win);
Wale Ogunwaleec731152016-09-08 20:18:57 -07002072 mService.mWindowsChanged = true;
2073 }
2074 }
Wale Ogunwaleec731152016-09-08 20:18:57 -07002075 }
2076
2077 /** Places the first input window after the second input window in the window list. */
2078 private void addWindowToListAfter(WindowState first, WindowState second) {
2079 final int i = mWindows.indexOf(second);
2080 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
2081 "Adding window " + this + " at " + (i + 1) + " of " + mWindows.size()
2082 + " (after " + second + ")");
2083 mWindows.add(i + 1, first);
2084 mService.mWindowsChanged = true;
2085 }
2086
2087 /** Places the first input window before the second input window in the window list. */
2088 private void addWindowToListBefore(WindowState first, WindowState second) {
2089 int i = mWindows.indexOf(second);
2090 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
2091 "Adding window " + this + " at " + i + " of " + mWindows.size()
2092 + " (before " + second + ")");
2093 if (i < 0) {
2094 Slog.w(TAG_WM, "addWindowToListBefore: Unable to find " + second + " in " + mWindows);
2095 i = 0;
2096 }
2097 mWindows.add(i, first);
2098 mService.mWindowsChanged = true;
2099 }
2100
2101 /**
2102 * This method finds out the index of a window that has the same app token as win. used for z
2103 * ordering the windows in mWindows
2104 */
2105 private int findIdxBasedOnAppTokens(WindowState win) {
2106 for(int j = mWindows.size() - 1; j >= 0; j--) {
2107 final WindowState wentry = mWindows.get(j);
2108 if(wentry.mAppToken == win.mAppToken) {
2109 return j;
2110 }
2111 }
2112 return -1;
2113 }
2114
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002115 private void dumpChildrenNames() {
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07002116 StringBuilder output = new StringBuilder();
2117 dumpChildrenNames(output, " ");
2118 Slog.v(TAG_WM, output.toString());
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002119 }
2120
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002121 private void dumpWindows() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002122 Slog.v(TAG_WM, " Display #" + mDisplayId);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002123 for (int winNdx = mWindows.size() - 1; winNdx >= 0; --winNdx) {
2124 Slog.v(TAG_WM, " #" + winNdx + ": " + mWindows.get(winNdx));
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002125 }
2126 }
2127
Wale Ogunwale02319a62016-09-26 15:21:22 -07002128 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2129 if (mTokenMap.isEmpty()) {
2130 return;
2131 }
2132 pw.println(" Display #" + mDisplayId);
2133 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2134 while (it.hasNext()) {
2135 final WindowToken token = it.next();
2136 pw.print(" ");
2137 pw.print(token);
2138 if (dumpAll) {
2139 pw.println(':');
2140 token.dump(pw, " ");
2141 } else {
2142 pw.println();
2143 }
2144 }
2145 }
2146
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002147 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
2148 final int count = mWindows.size();
2149 for (int j = 0; j < count; j++) {
2150 final WindowStateAnimator wAnim = mWindows.get(j).mWinAnimator;
2151 pw.println(subPrefix + "Window #" + j + ": " + wAnim);
2152 }
2153 }
2154
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002155 void enableSurfaceTrace(FileDescriptor fd) {
2156 for (int i = mWindows.size() - 1; i >= 0; i--) {
2157 final WindowState win = mWindows.get(i);
2158 win.mWinAnimator.enableSurfaceTrace(fd);
2159 }
2160 }
2161
2162 void disableSurfaceTrace() {
2163 for (int i = mWindows.size() - 1; i >= 0; i--) {
2164 final WindowState win = mWindows.get(i);
2165 win.mWinAnimator.disableSurfaceTrace();
2166 }
2167 }
2168
Jorim Jaggife762342016-10-13 14:33:27 +02002169 /**
2170 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2171 */
2172 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2173 final WindowManagerPolicy policy = mService.mPolicy;
2174 for (int i = mWindows.size() - 1; i >= 0; i--) {
2175 final WindowState window = mWindows.get(i);
2176 if (window.mAppToken == null && policy.canBeHiddenByKeyguardLw(window)) {
2177 window.mWinAnimator.setAnimation(
2178 policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
2179 }
2180 }
2181 }
2182
Wale Ogunwale494009b82016-10-21 09:01:38 -07002183 boolean checkWaitingForWindows() {
2184
2185 boolean haveBootMsg = false;
2186 boolean haveApp = false;
2187 // if the wallpaper service is disabled on the device, we're never going to have
2188 // wallpaper, don't bother waiting for it
2189 boolean haveWallpaper = false;
2190 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2191 com.android.internal.R.bool.config_enableWallpaperService)
2192 && !mService.mOnlyCore;
2193 boolean haveKeyguard = true;
2194 final int count = mWindows.size();
2195 for (int i = 0; i < count; i++) {
2196 final WindowState w = mWindows.get(i);
2197 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2198 return true;
2199 }
2200 if (w.isDrawnLw()) {
2201 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
2202 haveBootMsg = true;
2203 } else if (w.mAttrs.type == TYPE_APPLICATION
2204 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
2205 haveApp = true;
2206 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
2207 haveWallpaper = true;
2208 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
2209 haveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
2210 }
2211 }
2212 }
2213
2214 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2215 "******** booted=" + mService.mSystemBooted
2216 + " msg=" + mService.mShowingBootMessages
2217 + " haveBoot=" + haveBootMsg + " haveApp=" + haveApp
2218 + " haveWall=" + haveWallpaper + " wallEnabled=" + wallpaperEnabled
2219 + " haveKeyguard=" + haveKeyguard);
2220
2221 // If we are turning on the screen to show the boot message, don't do it until the boot
2222 // message is actually displayed.
2223 if (!mService.mSystemBooted && !haveBootMsg) {
2224 return true;
2225 }
2226
2227 // If we are turning on the screen after the boot is completed normally, don't do so until
2228 // we have the application and wallpaper.
2229 if (mService.mSystemBooted && ((!haveApp && !haveKeyguard) ||
2230 (wallpaperEnabled && !haveWallpaper))) {
2231 return true;
2232 }
2233
2234 return false;
2235 }
2236
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002237 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale0303c572016-10-20 10:16:29 -07002238 final WallpaperController wallpaperController = mWallpaperController;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002239 for (int i = mWindows.size() - 1; i >= 0; i--) {
2240 WindowState win = mWindows.get(i);
2241 WindowStateAnimator winAnimator = win.mWinAnimator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002242 if (winAnimator.hasSurface()) {
2243 final boolean wasAnimating = winAnimator.mWasAnimating;
2244 final boolean nowAnimating = winAnimator.stepAnimationLocked(animator.mCurrentTime);
2245 winAnimator.mWasAnimating = nowAnimating;
2246 animator.orAnimating(nowAnimating);
2247
2248 if (DEBUG_WALLPAPER) Slog.v(TAG,
2249 win + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
2250
2251 if (wasAnimating && !winAnimator.mAnimating
2252 && wallpaperController.isWallpaperTarget(win)) {
2253 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2254 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
2255 if (DEBUG_LAYOUT_REPEATS) {
2256 mService.mWindowPlacerLocked.debugLayoutRepeats(
2257 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
2258 }
2259 }
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002260 }
2261
2262 final AppWindowToken atoken = win.mAppToken;
2263 if (winAnimator.mDrawState == READY_TO_SHOW) {
2264 if (atoken == null || atoken.allDrawn) {
2265 if (win.performShowLocked()) {
2266 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
2267 if (DEBUG_LAYOUT_REPEATS) {
2268 mService.mWindowPlacerLocked.debugLayoutRepeats(
2269 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
2270 }
2271 }
2272 }
2273 }
2274 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
2275 if (appAnimator != null && appAnimator.thumbnail != null) {
2276 if (appAnimator.thumbnailTransactionSeq != animator.mAnimTransactionSequence) {
2277 appAnimator.thumbnailTransactionSeq = animator.mAnimTransactionSequence;
2278 appAnimator.thumbnailLayer = 0;
2279 }
2280 if (appAnimator.thumbnailLayer < winAnimator.mAnimLayer) {
2281 appAnimator.thumbnailLayer = winAnimator.mAnimLayer;
2282 }
2283 }
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002284 } // end forall windows
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002285 }
2286
2287 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002288 resetAnimationBackgroundAnimator();
2289
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002290 final WindowList windows = mWindows;
2291 WindowState detachedWallpaper = null;
2292
2293 for (int i = windows.size() - 1; i >= 0; i--) {
2294 final WindowState win = windows.get(i);
2295 final WindowStateAnimator winAnimator = win.mWinAnimator;
2296 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
2297 continue;
2298 }
2299
2300 final int flags = win.mAttrs.flags;
2301
2302 // If this window is animating, make a note that we have an animating window and take
2303 // care of a request to run a detached wallpaper animation.
2304 if (winAnimator.mAnimating) {
2305 if (winAnimator.mAnimation != null) {
2306 if ((flags & FLAG_SHOW_WALLPAPER) != 0
2307 && winAnimator.mAnimation.getDetachWallpaper()) {
2308 detachedWallpaper = win;
2309 }
2310 final int color = winAnimator.mAnimation.getBackgroundColor();
2311 if (color != 0) {
2312 final TaskStack stack = win.getStack();
2313 if (stack != null) {
2314 stack.setAnimationBackground(winAnimator, color);
2315 }
2316 }
2317 }
2318 animator.setAnimating(true);
2319 }
2320
2321 // If this window's app token is running a detached wallpaper animation, make a note so
2322 // we can ensure the wallpaper is displayed behind it.
2323 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
2324 if (appAnimator != null && appAnimator.animation != null
2325 && appAnimator.animating) {
2326 if ((flags & FLAG_SHOW_WALLPAPER) != 0
2327 && appAnimator.animation.getDetachWallpaper()) {
2328 detachedWallpaper = win;
2329 }
2330
2331 final int color = appAnimator.animation.getBackgroundColor();
2332 if (color != 0) {
2333 final TaskStack stack = win.getStack();
2334 if (stack != null) {
2335 stack.setAnimationBackground(winAnimator, color);
2336 }
2337 }
2338 }
2339 } // end forall windows
2340
2341 if (animator.mWindowDetachedWallpaper != detachedWallpaper) {
2342 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
2343 + animator.mWindowDetachedWallpaper + " to " + detachedWallpaper);
2344 animator.mWindowDetachedWallpaper = detachedWallpaper;
2345 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2346 }
2347 }
2348
2349 void prepareWindowSurfaces() {
2350 final int count = mWindows.size();
2351 for (int j = 0; j < count; j++) {
2352 mWindows.get(j).mWinAnimator.prepareSurfaceLocked(true);
2353 }
2354 }
2355
Wale Ogunwale494009b82016-10-21 09:01:38 -07002356 boolean inputMethodClientHasFocus(IInputMethodClient client) {
2357 // The focus for the client is the window immediately below where we would place the input
2358 // method window.
2359 int idx = findDesiredInputMethodWindowIndex(false);
2360 if (idx <= 0) {
2361 return false;
2362 }
2363
2364 WindowState imFocus = mWindows.get(idx - 1);
2365 if (DEBUG_INPUT_METHOD) {
2366 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2367 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2368 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
2369 }
2370
2371 if (imFocus == null) {
2372 return false;
2373 }
2374
2375 // This may be a starting window, in which case we still want to count it as okay.
2376 if (imFocus.mAttrs.type == TYPE_APPLICATION_STARTING && imFocus.mAppToken != null) {
2377 // The client has definitely started, so it really should have a window in this app
2378 // token. Let's look for it.
2379 final WindowState w = imFocus.mAppToken.getFirstNonStartingWindow();
2380 if (w != null) {
2381 if (DEBUG_INPUT_METHOD) Slog.i(TAG_WM, "Switching to real app window: " + w);
2382 imFocus = w;
2383 }
2384 }
2385
2386 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2387
2388 if (DEBUG_INPUT_METHOD) {
2389 Slog.i(TAG_WM, "IM target client: " + imeClient);
2390 if (imeClient != null) {
2391 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2392 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2393 }
2394 }
2395
2396 return imeClient != null && imeClient.asBinder() == client.asBinder();
2397 }
2398
2399 boolean hasSecureWindowOnScreen() {
2400 for (int i = mWindows.size() - 1; i >= 0; --i) {
2401 final WindowState ws = mWindows.get(i);
2402 if (ws.isOnScreen() && (ws.mAttrs.flags & FLAG_SECURE) != 0) {
2403 return true;
2404 }
2405 }
2406 return false;
2407 }
2408
2409 void updateSystemUiVisibility(int visibility, int globalDiff) {
2410 for (int i = mWindows.size() - 1; i >= 0; --i) {
2411 final WindowState ws = mWindows.get(i);
2412 try {
2413 int curValue = ws.mSystemUiVisibility;
2414 int diff = (curValue ^ visibility) & globalDiff;
2415 int newValue = (curValue & ~diff) | (visibility & diff);
2416 if (newValue != curValue) {
2417 ws.mSeq++;
2418 ws.mSystemUiVisibility = newValue;
2419 }
2420 if (newValue != curValue || ws.mAttrs.hasSystemUiListeners) {
2421 ws.mClient.dispatchSystemUiVisibilityChanged(ws.mSeq,
2422 visibility, newValue, diff);
2423 }
2424 } catch (RemoteException e) {
2425 // so sorry
2426 }
2427 }
2428 }
2429
2430 void onWindowFreezeTimeout() {
2431 Slog.w(TAG_WM, "Window freeze timeout expired.");
2432 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
2433 for (int i = mWindows.size() - 1; i >= 0; --i) {
2434 final WindowState w = mWindows.get(i);
2435 if (!w.mOrientationChanging) {
2436 continue;
2437 }
2438 w.mOrientationChanging = false;
2439 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2440 - mService.mDisplayFreezeTime);
2441 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
2442 }
2443 mService.mWindowPlacerLocked.performSurfacePlacement();
2444 }
2445
2446 void waitForAllWindowsDrawn() {
2447 final WindowManagerPolicy policy = mService.mPolicy;
2448 for (int winNdx = mWindows.size() - 1; winNdx >= 0; --winNdx) {
2449 final WindowState win = mWindows.get(winNdx);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002450 final boolean keyguard = policy.isKeyguardHostWindow(win.mAttrs);
Jorim Jaggife762342016-10-13 14:33:27 +02002451 if (win.isVisibleLw() && (win.mAppToken != null || keyguard)) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002452 win.mWinAnimator.mDrawState = DRAW_PENDING;
2453 // Force add to mResizingWindows.
2454 win.mLastContentInsets.set(-1, -1, -1, -1);
2455 mService.mWaitingForDrawn.add(win);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002456 }
2457 }
2458 }
2459
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002460 // TODO: Super crazy long method that should be broken down...
2461 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2462
2463 boolean focusDisplayed = false;
2464 boolean displayHasContent = false;
2465 float preferredRefreshRate = 0;
2466 int preferredModeId = 0;
2467
2468
2469 final int dw = mDisplayInfo.logicalWidth;
2470 final int dh = mDisplayInfo.logicalHeight;
2471 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2472
2473 mTmpUpdateAllDrawn.clear();
2474
2475 int repeats = 0;
2476 do {
2477 repeats++;
2478 if (repeats > 6) {
2479 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2480 clearLayoutNeeded();
2481 break;
2482 }
2483
2484 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2485 pendingLayoutChanges);
2486
Andrii Kulian839def92016-11-02 10:58:58 -07002487 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2488 // the wallpaper window jumping across displays.
2489 // Remove check for default display when there will be support for multiple wallpaper
2490 // targets (on different displays).
2491 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002492 adjustWallpaperWindows();
2493 }
2494
2495 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2496 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2497 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2498 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002499 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002500 }
2501 }
2502
2503 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2504 setLayoutNeeded();
2505 }
2506
2507 // FIRST LOOP: Perform a layout, if needed.
2508 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2509 performLayout(repeats == 1, false /* updateInputWindows */);
2510 } else {
2511 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2512 }
2513
2514 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2515 pendingLayoutChanges = 0;
2516
2517 if (isDefaultDisplay) {
2518 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
2519 for (int i = mWindows.size() - 1; i >= 0; i--) {
2520 final WindowState w = mWindows.get(i);
Jorim Jaggife762342016-10-13 14:33:27 +02002521 mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
2522 mService.mInputMethodTarget);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002523 }
2524 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2525 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2526 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2527 }
2528 } while (pendingLayoutChanges != 0);
2529
2530 RootWindowContainer root = mService.mRoot;
2531 boolean obscured = false;
2532 boolean syswin = false;
2533 resetDimming();
2534
2535 // Only used if default window
2536 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
2537
2538 for (int i = mWindows.size() - 1; i >= 0; i--) {
2539 final WindowState w = mWindows.get(i);
2540 final Task task = w.getTask();
2541 final boolean obscuredChanged = w.mObscured != obscured;
2542
2543 // Update effect.
2544 w.mObscured = obscured;
2545 if (!obscured) {
2546 final boolean isDisplayed = w.isDisplayedLw();
2547
2548 if (isDisplayed && w.isObscuringFullscreen(mDisplayInfo)) {
2549 // This window completely covers everything behind it, so we want to leave all
2550 // of them as undimmed (for performance reasons).
2551 root.mObscuringWindow = w;
2552 obscured = true;
2553 }
2554
2555 displayHasContent |= root.handleNotObscuredLocked(w, obscured, syswin);
2556
2557 if (w.mHasSurface && isDisplayed) {
2558 final int type = w.mAttrs.type;
2559 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
2560 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
2561 syswin = true;
2562 }
2563 if (preferredRefreshRate == 0 && w.mAttrs.preferredRefreshRate != 0) {
2564 preferredRefreshRate = w.mAttrs.preferredRefreshRate;
2565 }
2566 if (preferredModeId == 0 && w.mAttrs.preferredDisplayModeId != 0) {
2567 preferredModeId = w.mAttrs.preferredDisplayModeId;
2568 }
2569 }
2570 }
2571
2572 w.applyDimLayerIfNeeded();
2573
2574 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
2575 && mWallpaperController.isWallpaperTarget(w)) {
2576 // This is the wallpaper target and its obscured state changed... make sure the
2577 // current wallpaper's visibility has been updated accordingly.
2578 mWallpaperController.updateWallpaperVisibility();
2579 }
2580
2581 w.handleWindowMovedIfNeeded();
2582
2583 final WindowStateAnimator winAnimator = w.mWinAnimator;
2584
2585 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
2586 w.mContentChanged = false;
2587
2588 // Moved from updateWindowsAndWallpaperLocked().
2589 if (w.mHasSurface) {
2590 // Take care of the window being ready to display.
2591 final boolean committed = winAnimator.commitFinishDrawingLocked();
2592 if (isDefaultDisplay && committed) {
2593 if (w.mAttrs.type == TYPE_DREAM) {
2594 // HACK: When a dream is shown, it may at that point hide the lock screen.
2595 // So we need to redo the layout to let the phone window manager make this
2596 // happen.
2597 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
2598 if (DEBUG_LAYOUT_REPEATS) {
2599 surfacePlacer.debugLayoutRepeats(
2600 "dream and commitFinishDrawingLocked true",
2601 pendingLayoutChanges);
2602 }
2603 }
2604 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
2605 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
2606 "First draw done in potential wallpaper target " + w);
2607 root.mWallpaperMayChange = true;
2608 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
2609 if (DEBUG_LAYOUT_REPEATS) {
2610 surfacePlacer.debugLayoutRepeats(
2611 "wallpaper and commitFinishDrawingLocked true",
2612 pendingLayoutChanges);
2613 }
2614 }
2615 }
2616 if (!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening()) {
2617 // Updates the shown frame before we set up the surface. This is needed
2618 // because the resizing could change the top-left position (in addition to
2619 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
2620 // position the surface.
2621 //
2622 // If an animation is being started, we can't call this method because the
2623 // animation hasn't processed its initial transformation yet, but in general
2624 // we do want to update the position if the window is animating.
2625 winAnimator.computeShownFrameLocked();
2626 }
2627 winAnimator.setSurfaceBoundariesLocked(recoveringMemory);
2628 }
2629
2630 final AppWindowToken atoken = w.mAppToken;
2631 if (atoken != null) {
2632 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
2633 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
2634 mTmpUpdateAllDrawn.add(atoken);
2635 }
2636 }
2637
2638 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
2639 && w.isDisplayedLw()) {
2640 focusDisplayed = true;
2641 }
2642
2643 w.updateResizingWindowIfNeeded();
2644 }
2645
2646 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
2647 displayHasContent,
2648 preferredRefreshRate,
2649 preferredModeId,
2650 true /* inTraversal, must call performTraversalInTrans... below */);
2651
2652 stopDimmingIfNeeded();
2653
2654 while (!mTmpUpdateAllDrawn.isEmpty()) {
2655 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2656 // See if any windows have been drawn, so they (and others associated with them)
2657 // can now be shown.
2658 atoken.updateAllDrawn(this);
2659 }
2660
2661 return focusDisplayed;
2662 }
2663
2664 void performLayout(boolean initial, boolean updateInputWindows) {
2665 if (!isLayoutNeeded()) {
2666 return;
2667 }
2668 clearLayoutNeeded();
2669
2670 final int dw = mDisplayInfo.logicalWidth;
2671 final int dh = mDisplayInfo.logicalHeight;
2672
2673 int i;
2674
2675 if (DEBUG_LAYOUT) {
2676 Slog.v(TAG, "-------------------------------------");
2677 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2678 }
2679
2680 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mService.mRotation,
2681 getConfiguration().uiMode);
2682 if (isDefaultDisplay) {
2683 // Not needed on non-default displays.
2684 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2685 mService.mScreenRect.set(0, 0, dw, dh);
2686 }
2687
2688 mService.mPolicy.getContentRectLw(mContentRect);
2689
2690 int seq = mService.mLayoutSeq + 1;
2691 if (seq < 0) seq = 0;
2692 mService.mLayoutSeq = seq;
2693
2694 boolean behindDream = false;
2695
2696 // First perform layout of any root windows (not attached to another window).
2697 int topAttached = -1;
2698 for (i = mWindows.size() - 1; i >= 0; i--) {
2699 final WindowState win = mWindows.get(i);
2700
2701 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
2702 // wasting time and funky changes while a window is animating away.
Jorim Jaggife762342016-10-13 14:33:27 +02002703 final boolean gone = (behindDream && mService.mPolicy.canBeHiddenByKeyguardLw(win))
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002704 || win.isGoneForLayoutLw();
2705
2706 if (DEBUG_LAYOUT && !win.mLayoutAttached) {
2707 Slog.v(TAG, "1ST PASS " + win + ": gone=" + gone + " mHaveFrame=" + win.mHaveFrame
2708 + " mLayoutAttached=" + win.mLayoutAttached
2709 + " screen changed=" + win.isConfigChanged());
2710 final AppWindowToken atoken = win.mAppToken;
2711 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + win.mViewVisibility
2712 + " mRelayoutCalled=" + win.mRelayoutCalled + " hidden=" + win.mToken.hidden
2713 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
2714 + " parentHidden=" + win.isParentWindowHidden());
2715 else Slog.v(TAG, " VIS: mViewVisibility=" + win.mViewVisibility
2716 + " mRelayoutCalled=" + win.mRelayoutCalled + " hidden=" + win.mToken.hidden
2717 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
2718 + " parentHidden=" + win.isParentWindowHidden());
2719 }
2720
2721 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
2722 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
2723 // since that means "perform layout as normal, just don't display").
2724 if (!gone || !win.mHaveFrame || win.mLayoutNeeded
2725 || ((win.isConfigChanged() || win.setReportResizeHints())
2726 && !win.isGoneForLayoutLw() &&
2727 ((win.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
2728 (win.mHasSurface && win.mAppToken != null &&
2729 win.mAppToken.layoutConfigChanges)))) {
2730 if (!win.mLayoutAttached) {
2731 if (initial) {
2732 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
2733 win.mContentChanged = false;
2734 }
2735 if (win.mAttrs.type == TYPE_DREAM) {
2736 // Don't layout windows behind a dream, so that if it does stuff like hide
2737 // the status bar we won't get a bad transition when it goes away.
2738 behindDream = true;
2739 }
2740 win.mLayoutNeeded = false;
2741 win.prelayout();
2742 mService.mPolicy.layoutWindowLw(win, null);
2743 win.mLayoutSeq = seq;
2744
2745 // Window frames may have changed. Update dim layer with the new bounds.
2746 final Task task = win.getTask();
2747 if (task != null) {
2748 mDimLayerController.updateDimLayer(task);
2749 }
2750
2751 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + win.mFrame
2752 + " mContainingFrame=" + win.mContainingFrame
2753 + " mDisplayFrame=" + win.mDisplayFrame);
2754 } else {
2755 if (topAttached < 0) topAttached = i;
2756 }
2757 }
2758 }
2759
2760 boolean attachedBehindDream = false;
2761
2762 // Now perform layout of attached windows, which usually depend on the position of the
2763 // window they are attached to. XXX does not deal with windows that are attached to windows
2764 // that are themselves attached.
2765 for (i = topAttached; i >= 0; i--) {
2766 final WindowState win = mWindows.get(i);
2767
2768 if (win.mLayoutAttached) {
2769 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + win + " mHaveFrame=" + win.mHaveFrame
2770 + " mViewVisibility=" + win.mViewVisibility
2771 + " mRelayoutCalled=" + win.mRelayoutCalled);
2772 // If this view is GONE, then skip it -- keep the current frame, and let the caller
2773 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
2774 // windows, since that means "perform layout as normal, just don't display").
Jorim Jaggife762342016-10-13 14:33:27 +02002775 if (attachedBehindDream && mService.mPolicy.canBeHiddenByKeyguardLw(win)) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002776 continue;
2777 }
2778 if ((win.mViewVisibility != GONE && win.mRelayoutCalled) || !win.mHaveFrame
2779 || win.mLayoutNeeded) {
2780 if (initial) {
2781 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
2782 win.mContentChanged = false;
2783 }
2784 win.mLayoutNeeded = false;
2785 win.prelayout();
2786 mService.mPolicy.layoutWindowLw(win, win.getParentWindow());
2787 win.mLayoutSeq = seq;
2788 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + win.mFrame
2789 + " mContainingFrame=" + win.mContainingFrame
2790 + " mDisplayFrame=" + win.mDisplayFrame);
2791 }
2792 } else if (win.mAttrs.type == TYPE_DREAM) {
2793 // Don't layout windows behind a dream, so that if it does stuff like hide the
2794 // status bar we won't get a bad transition when it goes away.
2795 attachedBehindDream = behindDream;
2796 }
2797 }
2798
2799 // Window frames may have changed. Tell the input dispatcher about it.
2800 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2801 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2802 if (updateInputWindows) {
2803 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2804 }
2805
2806 mService.mPolicy.finishLayoutLw();
2807 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2808 }
2809
2810 /**
2811 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2812 * In portrait mode, it grabs the full screenshot.
2813 *
2814 * @param width the width of the target bitmap
2815 * @param height the height of the target bitmap
2816 * @param includeFullDisplay true if the screen should not be cropped before capture
2817 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2818 * @param config of the output bitmap
2819 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
2820 */
2821 Bitmap screenshotApplications(IBinder appToken, int displayId, int width, int height,
2822 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
2823 boolean wallpaperOnly) {
2824 int dw = mDisplayInfo.logicalWidth;
2825 int dh = mDisplayInfo.logicalHeight;
2826 if (dw == 0 || dh == 0) {
2827 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2828 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2829 return null;
2830 }
2831
2832 Bitmap bm = null;
2833
2834 int maxLayer = 0;
2835 final Rect frame = new Rect();
2836 final Rect stackBounds = new Rect();
2837
2838 boolean screenshotReady;
2839 int minLayer;
2840 if (appToken == null && !wallpaperOnly) {
2841 screenshotReady = true;
2842 minLayer = 0;
2843 } else {
2844 screenshotReady = false;
2845 minLayer = Integer.MAX_VALUE;
2846 }
2847
2848 WindowState appWin = null;
2849
2850 boolean includeImeInScreenshot;
2851 synchronized(mService.mWindowMap) {
2852 final AppWindowToken imeTargetAppToken = mService.mInputMethodTarget != null
2853 ? mService.mInputMethodTarget.mAppToken : null;
2854 // We only include the Ime in the screenshot if the app we are screenshoting is the IME
2855 // target and isn't in multi-window mode. We don't screenshot the IME in multi-window
2856 // mode because the frame of the IME might not overlap with that of the app.
2857 // E.g. IME target app at the top in split-screen mode and the IME at the bottom
2858 // overlapping with the bottom app.
2859 includeImeInScreenshot = imeTargetAppToken != null
2860 && imeTargetAppToken.appToken != null
2861 && imeTargetAppToken.appToken.asBinder() == appToken
2862 && !mService.mInputMethodTarget.isInMultiWindowMode();
2863 }
2864
2865 final int aboveAppLayer = (mService.mPolicy.windowTypeToLayerLw(TYPE_APPLICATION) + 1)
2866 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
2867
2868 synchronized(mService.mWindowMap) {
2869 // Figure out the part of the screen that is actually the app.
2870 appWin = null;
2871 for (int i = mWindows.size() - 1; i >= 0; i--) {
2872 final WindowState ws = mWindows.get(i);
2873 if (!ws.mHasSurface) {
2874 continue;
2875 }
2876 if (ws.mLayer >= aboveAppLayer) {
2877 continue;
2878 }
2879 if (wallpaperOnly && !ws.mIsWallpaper) {
2880 continue;
2881 }
2882 if (ws.mIsImWindow) {
2883 if (!includeImeInScreenshot) {
2884 continue;
2885 }
2886 } else if (ws.mIsWallpaper) {
2887 // If this is the wallpaper layer and we're only looking for the wallpaper layer
2888 // then the target window state is this one.
2889 if (wallpaperOnly) {
2890 appWin = ws;
2891 }
2892
2893 if (appWin == null) {
2894 // We have not ran across the target window yet, so it is probably behind
2895 // the wallpaper. This can happen when the keyguard is up and all windows
2896 // are moved behind the wallpaper. We don't want to include the wallpaper
2897 // layer in the screenshot as it will cover-up the layer of the target
2898 // window.
2899 continue;
2900 }
2901 // Fall through. The target window is in front of the wallpaper. For this
2902 // case we want to include the wallpaper layer in the screenshot because
2903 // the target window might have some transparent areas.
2904 } else if (appToken != null) {
2905 if (ws.mAppToken == null || ws.mAppToken.token != appToken) {
2906 // This app window is of no interest if it is not associated with the
2907 // screenshot app.
2908 continue;
2909 }
2910 appWin = ws;
2911 }
2912
2913 // Include this window.
2914
2915 final WindowStateAnimator winAnim = ws.mWinAnimator;
2916 int layer = winAnim.mSurfaceController.getLayer();
2917 if (maxLayer < layer) {
2918 maxLayer = layer;
2919 }
2920 if (minLayer > layer) {
2921 minLayer = layer;
2922 }
2923
2924 // Don't include wallpaper in bounds calculation
2925 if (!includeFullDisplay && !ws.mIsWallpaper) {
2926 final Rect wf = ws.mFrame;
2927 final Rect cr = ws.mContentInsets;
2928 int left = wf.left + cr.left;
2929 int top = wf.top + cr.top;
2930 int right = wf.right - cr.right;
2931 int bottom = wf.bottom - cr.bottom;
2932 frame.union(left, top, right, bottom);
2933 ws.getVisibleBounds(stackBounds);
2934 if (!Rect.intersects(frame, stackBounds)) {
2935 // Set frame empty if there's no intersection.
2936 frame.setEmpty();
2937 }
2938 }
2939
2940 final boolean foundTargetWs =
2941 (ws.mAppToken != null && ws.mAppToken.token == appToken)
2942 || (appWin != null && wallpaperOnly);
2943 if (foundTargetWs && ws.isDisplayedLw() && winAnim.getShown()) {
2944 screenshotReady = true;
2945 }
2946
2947 if (ws.isObscuringFullscreen(mDisplayInfo)){
2948 break;
2949 }
2950 }
2951
2952 if (appToken != null && appWin == null) {
2953 // Can't find a window to snapshot.
2954 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
2955 "Screenshot: Couldn't find a surface matching " + appToken);
2956 return null;
2957 }
2958
2959 if (!screenshotReady) {
2960 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
2961 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
2962 appWin.mWinAnimator.mDrawState)));
2963 return null;
2964 }
2965
2966 // Screenshot is ready to be taken. Everything from here below will continue
2967 // through the bottom of the loop and return a value. We only stay in the loop
2968 // because we don't want to release the mWindowMap lock until the screenshot is
2969 // taken.
2970
2971 if (maxLayer == 0) {
2972 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2973 + ": returning null maxLayer=" + maxLayer);
2974 return null;
2975 }
2976
2977 if (!includeFullDisplay) {
2978 // Constrain frame to the screen size.
2979 if (!frame.intersect(0, 0, dw, dh)) {
2980 frame.setEmpty();
2981 }
2982 } else {
2983 // Caller just wants entire display.
2984 frame.set(0, 0, dw, dh);
2985 }
2986 if (frame.isEmpty()) {
2987 return null;
2988 }
2989
2990 if (width < 0) {
2991 width = (int) (frame.width() * frameScale);
2992 }
2993 if (height < 0) {
2994 height = (int) (frame.height() * frameScale);
2995 }
2996
2997 // Tell surface flinger what part of the image to crop. Take the top
2998 // right part of the application, and crop the larger dimension to fit.
2999 Rect crop = new Rect(frame);
3000 if (width / (float) frame.width() < height / (float) frame.height()) {
3001 int cropWidth = (int)((float)width / (float)height * frame.height());
3002 crop.right = crop.left + cropWidth;
3003 } else {
3004 int cropHeight = (int)((float)height / (float)width * frame.width());
3005 crop.bottom = crop.top + cropHeight;
3006 }
3007
3008 // The screenshot API does not apply the current screen rotation.
3009 int rot = mDisplay.getRotation();
3010
3011 if (rot == ROTATION_90 || rot == ROTATION_270) {
3012 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3013 }
3014
3015 // Surfaceflinger is not aware of orientation, so convert our logical
3016 // crop to surfaceflinger's portrait orientation.
3017 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3018
3019 if (DEBUG_SCREENSHOT) {
3020 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3021 + maxLayer + " appToken=" + appToken);
3022 for (int i = 0; i < mWindows.size(); i++) {
3023 final WindowState win = mWindows.get(i);
3024 final WindowSurfaceController controller = win.mWinAnimator.mSurfaceController;
3025 Slog.i(TAG_WM, win + ": " + win.mLayer
3026 + " animLayer=" + win.mWinAnimator.mAnimLayer
3027 + " surfaceLayer=" + ((controller == null)
3028 ? "null" : controller.getLayer()));
3029 }
3030 }
3031
3032 final ScreenRotationAnimation screenRotationAnimation =
3033 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3034 final boolean inRotation = screenRotationAnimation != null &&
3035 screenRotationAnimation.isAnimating();
3036 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3037 "Taking screenshot while rotating");
3038
3039 // We force pending transactions to flush before taking
3040 // the screenshot by pushing an empty synchronous transaction.
3041 SurfaceControl.openTransaction();
3042 SurfaceControl.closeTransactionSync();
3043
3044 bm = SurfaceControl.screenshot(crop, width, height, minLayer, maxLayer,
3045 inRotation, rot);
3046 if (bm == null) {
3047 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3048 + ") to layer " + maxLayer);
3049 return null;
3050 }
3051 }
3052
3053 if (DEBUG_SCREENSHOT) {
3054 // TEST IF IT's ALL BLACK
3055 int[] buffer = new int[bm.getWidth() * bm.getHeight()];
3056 bm.getPixels(buffer, 0, bm.getWidth(), 0, 0, bm.getWidth(), bm.getHeight());
3057 boolean allBlack = true;
3058 final int firstColor = buffer[0];
3059 for (int i = 0; i < buffer.length; i++) {
3060 if (buffer[i] != firstColor) {
3061 allBlack = false;
3062 break;
3063 }
3064 }
3065 if (allBlack) {
3066 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
3067 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
3068 (appWin != null ?
3069 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
3070 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
3071 }
3072 }
3073
3074 // Create a copy of the screenshot that is immutable and backed in ashmem.
3075 // This greatly reduces the overhead of passing the bitmap between processes.
3076 Bitmap ret = bm.createAshmemBitmap(config);
3077 bm.recycle();
3078 return ret;
3079 }
3080
3081 // TODO: Can this use createRotationMatrix()?
3082 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3083 if (rot == Surface.ROTATION_90) {
3084 final int tmp = crop.top;
3085 crop.top = dw - crop.right;
3086 crop.right = crop.bottom;
3087 crop.bottom = dw - crop.left;
3088 crop.left = tmp;
3089 } else if (rot == Surface.ROTATION_180) {
3090 int tmp = crop.top;
3091 crop.top = dh - crop.bottom;
3092 crop.bottom = dh - tmp;
3093 tmp = crop.right;
3094 crop.right = dw - crop.left;
3095 crop.left = dw - tmp;
3096 } else if (rot == Surface.ROTATION_270) {
3097 final int tmp = crop.top;
3098 crop.top = crop.left;
3099 crop.left = dh - crop.bottom;
3100 crop.bottom = crop.right;
3101 crop.right = dh - tmp;
3102 }
3103 }
3104
3105 void onSeamlessRotationTimeout() {
3106 boolean layoutNeeded = false;
3107 for (int i = mWindows.size() - 1; i >= 0; i--) {
3108 final WindowState w = mWindows.get(i);
3109 if (!w.mSeamlesslyRotated) {
3110 continue;
3111 }
3112 layoutNeeded = true;
3113 w.setDisplayLayoutNeeded();
3114 mService.markForSeamlessRotation(w, false);
3115 }
3116
3117 if (layoutNeeded) {
3118 mService.mWindowPlacerLocked.performSurfacePlacement();
3119 }
3120 }
3121
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003122 static final class GetWindowOnDisplaySearchResult {
Wale Ogunwaleec731152016-09-08 20:18:57 -07003123 boolean reachedToken;
3124 WindowState foundWindow;
3125
3126 void reset() {
3127 reachedToken = false;
3128 foundWindow = null;
3129 }
3130 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003131
3132 static final class TaskForResizePointSearchResult {
3133 boolean searchDone;
3134 Task taskForResize;
3135
3136 void reset() {
3137 searchDone = false;
3138 taskForResize = null;
3139 }
3140 }
Robert Carr3b716242016-08-16 16:02:21 -07003141
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003142 /**
3143 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3144 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3145 * homogeneous children type which is currently required by sub-classes of
3146 * {@link WindowContainer} class.
3147 */
3148 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3149
3150 int size() {
3151 return mChildren.size();
3152 }
3153
3154 E get(int index) {
3155 return mChildren.get(index);
3156 }
3157
3158 @Override
3159 boolean fillsParent() {
3160 return true;
3161 }
3162
3163 @Override
3164 boolean isVisible() {
3165 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003166 }
3167 }
3168
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003169 /**
3170 * Window container class that contains all containers on this display relating to Apps.
3171 * I.e Activities.
3172 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003173 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003174
Andrii Kulian839def92016-11-02 10:58:58 -07003175 /**
3176 * Adds the stack to this container.
3177 * @see WindowManagerService#addStackToDisplay(int, int, boolean)
3178 */
3179 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003180 if (stack.mStackId == HOME_STACK_ID) {
3181 if (mHomeStack != null) {
3182 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3183 }
3184 mHomeStack = stack;
3185 }
3186 addChild(stack, onTop);
3187 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003188 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003189
Andrii Kulian839def92016-11-02 10:58:58 -07003190 /** Removes the stack from its container and prepare for changing the parent. */
3191 void removeStackFromDisplay(TaskStack stack) {
3192 removeChild(stack);
3193 stack.onRemovedFromDisplay();
3194 // TODO: remove when window list will be gone.
3195 // Manually remove records from window list and tap excluded windows list.
3196 for (int i = mWindows.size() - 1; i >= 0; --i) {
3197 final WindowState windowState = mWindows.get(i);
3198 if (stack == windowState.getStack()) {
3199 mWindows.remove(i);
3200 mTapExcludedWindows.remove(windowState);
3201 }
3202 }
3203 }
3204
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003205 void moveStack(TaskStack stack, boolean toTop) {
3206 if (StackId.isAlwaysOnTop(stack.mStackId) && !toTop) {
3207 // This stack is always-on-top silly...
3208 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + stack + " to bottom");
3209 return;
3210 }
3211
3212 if (!mChildren.contains(stack)) {
3213 Slog.wtf(TAG_WM, "moving stack that was not added: " + stack, new Throwable());
3214 }
3215 removeChild(stack);
3216 addChild(stack, toTop);
3217 }
3218
3219 private void addChild(TaskStack stack, boolean toTop) {
3220 int addIndex = toTop ? mChildren.size() : 0;
3221
3222 if (toTop
3223 && mService.isStackVisibleLocked(PINNED_STACK_ID)
3224 && stack.mStackId != PINNED_STACK_ID) {
3225 // The pinned stack is always the top most stack (always-on-top) when it is visible.
3226 // So, stack is moved just below the pinned stack.
3227 addIndex--;
3228 TaskStack topStack = mChildren.get(addIndex);
3229 if (topStack.mStackId != PINNED_STACK_ID) {
3230 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3231 }
3232 }
3233 addChild(stack, addIndex);
3234 setLayoutNeeded();
3235 }
3236
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003237 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003238 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3239 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003240 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003241 if (super.forAllWindows(callback, traverseTopToBottom)) {
3242 return true;
3243 }
3244 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3245 return true;
3246 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003247 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003248 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3249 return true;
3250 }
3251 if (super.forAllWindows(callback, traverseTopToBottom)) {
3252 return true;
3253 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003254 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003255 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003256 }
3257
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003258 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003259 boolean traverseTopToBottom) {
3260 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3261 // app tokens.
3262 // TODO: Investigate if we need to continue to do this or if we can just process them
3263 // in-order.
3264 if (traverseTopToBottom) {
3265 for (int i = mChildren.size() - 1; i >= 0; --i) {
3266 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3267 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003268 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3269 traverseTopToBottom)) {
3270 return true;
3271 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003272 }
3273 }
3274 } else {
3275 final int count = mChildren.size();
3276 for (int i = 0; i < count; ++i) {
3277 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3278 final int appTokensCount = appTokens.size();
3279 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003280 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3281 traverseTopToBottom)) {
3282 return true;
3283 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003284 }
3285 }
3286 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003287 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003288 }
3289
3290 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003291 int getOrientation() {
3292 if (mService.isStackVisibleLocked(DOCKED_STACK_ID)
3293 || mService.isStackVisibleLocked(FREEFORM_WORKSPACE_STACK_ID)) {
3294 // Apps and their containers are not allowed to specify an orientation while the
3295 // docked or freeform stack is visible...except for the home stack/task if the
3296 // docked stack is minimized and it actually set something.
3297 if (mHomeStack != null && mHomeStack.isVisible()
3298 && mDividerControllerLocked.isMinimizedDock()) {
3299 final int orientation = mHomeStack.getOrientation();
3300 if (orientation != SCREEN_ORIENTATION_UNSET) {
3301 return orientation;
3302 }
3303 }
3304 return SCREEN_ORIENTATION_UNSPECIFIED;
3305 }
3306
3307 final int orientation = super.getOrientation();
3308 if (orientation != SCREEN_ORIENTATION_UNSET
3309 && orientation != SCREEN_ORIENTATION_BEHIND) {
3310 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3311 "App is requesting an orientation, return " + orientation);
3312 return orientation;
3313 }
3314
3315 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3316 "No app is requesting an orientation, return " + mService.mLastOrientation);
3317 // The next app has not been requested to be visible, so we keep the current orientation
3318 // to prevent freezing/unfreezing the display too early.
3319 return mService.mLastOrientation;
3320 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003321 }
3322
3323 /**
3324 * Window container class that contains all containers on this display that are not related to
3325 * Apps. E.g. status bar.
3326 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003327 private final class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3328 /**
3329 * Compares two child window tokens returns -1 if the first is lesser than the second in
3330 * terms of z-order and 1 otherwise.
3331 */
3332 final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
3333 // Tokens with higher base layer are z-ordered on-top.
3334 mService.mPolicy.windowTypeToLayerLw(token1.windowType)
3335 < mService.mPolicy.windowTypeToLayerLw(token2.windowType) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003336
Wale Ogunwale3a931692016-11-02 16:49:48 -07003337 private final String mName;
3338 NonAppWindowContainers(String name) {
3339 mName = name;
3340 }
3341
3342 void addChild(WindowToken token) {
3343 addChild(token, mWindowComparator);
3344 }
3345
3346 @Override
3347 String getName() {
3348 return mName;
3349 }
Robert Carr3b716242016-08-16 16:02:21 -07003350 }
Craig Mautner59c00972012-07-30 12:10:24 -07003351}