blob: 8db811805bd3efe25ecb3173536f7c07e501de24 [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 Ogunwale494009b82016-10-21 09:01:38 -070056import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070057import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
58import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070059import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070060import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070061import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070062import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
63import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070064import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
65import static android.view.WindowManagerPolicy.KEYGUARD_GOING_AWAY_FLAG_NO_WINDOW_ANIMATIONS;
66import static android.view.WindowManagerPolicy.KEYGUARD_GOING_AWAY_FLAG_TO_SHADE;
67import static android.view.WindowManagerPolicy.KEYGUARD_GOING_AWAY_FLAG_WITH_WALLPAPER;
68import static com.android.server.wm.WindowAnimator.KEYGUARD_ANIMATING_OUT;
69import static com.android.server.wm.WindowAnimator.KEYGUARD_ANIM_TIMEOUT_MS;
70import static com.android.server.wm.WindowAnimator.KEYGUARD_NOT_SHOWN;
71import static com.android.server.wm.WindowAnimator.KEYGUARD_SHOWN;
Wale Ogunwaleec731152016-09-08 20:18:57 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM;
Wale Ogunwale494009b82016-10-21 09:01:38 -070074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070076import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
77import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070078import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070079import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_KEYGUARD;
Wale Ogunwalec69694a2016-10-18 13:51:15 -070080import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYERS;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070081import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070082import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070083import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070084import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070085import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070086import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Wale Ogunwaleec731152016-09-08 20:18:57 -070087import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_MOVEMENT;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080088import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
Wale Ogunwale51362492016-09-08 17:49:17 -070089import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070090import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070091import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070092import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080093import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070094import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
95import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070096import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070097import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
98import static com.android.server.wm.WindowManagerService.TYPE_LAYER_MULTIPLIER;
99import static com.android.server.wm.WindowManagerService.TYPE_LAYER_OFFSET;
100import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700101import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700102import static com.android.server.wm.WindowManagerService.dipToPixel;
103import static com.android.server.wm.WindowManagerService.localLOGV;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700104import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700105import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700106import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700107import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
108import static com.android.server.wm.WindowStateAnimator.STACK_CLIP_BEFORE_ANIM;
109import static com.android.server.wm.WindowSurfacePlacer.SET_FORCE_HIDING_CHANGED;
110import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700111
Andrii Kulian3a507b52016-09-19 18:14:12 -0700112import android.annotation.NonNull;
Wale Ogunwale3797c222015-10-27 14:21:58 -0700113import android.app.ActivityManager.StackId;
Andrii Kulian441e4492016-09-29 15:25:00 -0700114import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700115import android.graphics.Bitmap;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700116import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800117import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700118import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700119import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100120import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700121import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700122import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700123import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700124import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700125import android.os.RemoteException;
126import android.os.SystemClock;
Chong Zhang8e89b312015-09-09 15:09:30 -0700127import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700128import android.util.Slog;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700129import android.view.Display;
Craig Mautner59c00972012-07-30 12:10:24 -0700130import android.view.DisplayInfo;
Wale Ogunwaleec731152016-09-08 20:18:57 -0700131import android.view.IWindow;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700132import android.view.InputChannel;
133import android.view.Surface;
134import android.view.SurfaceControl;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700135import android.view.WindowManagerPolicy;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700136import android.view.animation.AlphaAnimation;
137import android.view.animation.Animation;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700138
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700139import com.android.internal.util.FastPrintWriter;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700140import com.android.internal.view.IInputMethodClient;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700141import com.android.server.input.InputWindowHandle;
Craig Mautner59c00972012-07-30 12:10:24 -0700142
Robert Carr3b716242016-08-16 16:02:21 -0700143import java.io.FileDescriptor;
Craig Mautner59c00972012-07-30 12:10:24 -0700144import java.io.PrintWriter;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700145import java.io.StringWriter;
Craig Mautner59c00972012-07-30 12:10:24 -0700146import java.util.ArrayList;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700147import java.util.Arrays;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700148import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700149import java.util.HashMap;
150import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700151import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700152import java.util.List;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700153import java.util.function.BiConsumer;
154import java.util.function.Consumer;
155import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700156
Craig Mautner59c00972012-07-30 12:10:24 -0700157/**
158 * Utility class for keeping track of the WindowStates and other pertinent contents of a
159 * particular Display.
160 *
161 * IMPORTANT: No method from this class should ever be used without holding
162 * WindowManagerService.mWindowMap.
163 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700164class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700165 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700166
167 /** Unique identifier of this stack. */
168 private final int mDisplayId;
169
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700170 // The display only has 2 child window containers. mTaskStackContainers which contains all
171 // window containers that are related to apps (Activities) and mNonAppWindowContainers which
172 // contains all window containers not related to apps (e.g. Status bar).
173 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers();
174 private final NonAppWindowContainers mNonAppWindowContainers = new NonAppWindowContainers();
175
Craig Mautner59c00972012-07-30 12:10:24 -0700176 /** Z-ordered (bottom-most first) list of all Window objects. Assigned to an element
177 * from mDisplayWindows; */
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;
Craig Mautner69b08182012-09-05 13:07:13 -0700200 final boolean isDefaultDisplay;
Craig Mautner39834192012-09-02 07:47:24 -0700201
Craig Mautnerdc548482014-02-05 13:35:24 -0800202 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700203 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800204
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800205 /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
206 * (except a future lockscreen TaskStack) moves to the top. */
Craig Mautnerde4ef022013-04-07 19:01:33 -0700207 private TaskStack mHomeStack = null;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700208
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700209 /** Detect user tapping outside of current focused task bounds .*/
210 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700211
Craig Mautner6601b7b2013-04-29 10:29:11 -0700212 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700213 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700214
Craig Mautner6601b7b2013-04-29 10:29:11 -0700215 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800216 private final Rect mTmpRect = new Rect();
217 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700218 private final RectF mTmpRectF = new RectF();
219 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800220 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700221
Craig Mautner9d808b12013-08-06 18:00:25 -0700222 final WindowManagerService mService;
223
Craig Mautner95da1082014-02-24 17:54:35 -0800224 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700225 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800226
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700227 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700228 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700229
Chong Zhang112eb8c2015-11-02 11:17:00 -0800230 final DimLayerController mDimLayerController;
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700231
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800232 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
233
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700234 /** Used when rebuilding window list to keep track of windows that have been removed. */
235 private WindowState[] mRebuildTmp = new WindowState[20];
236
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700237 /**
238 * Temporary list for comparison. Always clear this after use so we don't end up with
239 * orphaned windows references
240 */
241 private final ArrayList<WindowState> mTmpWindows = new ArrayList<>();
242
243 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
244
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700245 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
246 new TaskForResizePointSearchResult();
247 private final GetWindowOnDisplaySearchResult mTmpGetWindowOnDisplaySearchResult =
248 new GetWindowOnDisplaySearchResult();
249
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700250 // True if this display is in the process of being removed. Used to determine if the removal of
251 // the display's direct children should be allowed.
252 private boolean mRemovingDisplay = false;
253
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700254 private final WindowLayersController mLayersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700255 final WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700256 int mInputMethodAnimLayerAdjustment;
257
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800258 /**
Craig Mautner2d5618c2012-10-18 13:55:47 -0700259 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800260 * @param service You know.
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700261 * @param layersController window layer controller used to assign layer to the windows on this
262 * display.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700263 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
264 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700265 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700266 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700267 WindowLayersController layersController, WallpaperController wallpaperController) {
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700268 mDisplay = display;
269 mDisplayId = display.getDisplayId();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700270 mLayersController = layersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700271 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700272 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700273 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700274 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Craig Mautner9d808b12013-08-06 18:00:25 -0700275 mService = service;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700276 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800277 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700278 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800279 mDimLayerController = new DimLayerController(this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700280
281 // These are the only direct children we should ever have and they are permanent.
282 super.addChild(mTaskStackContainers, null);
283 super.addChild(mNonAppWindowContainers, null);
Craig Mautner59c00972012-07-30 12:10:24 -0700284 }
285
286 int getDisplayId() {
287 return mDisplayId;
288 }
289
Wale Ogunwale02319a62016-09-26 15:21:22 -0700290 WindowToken getWindowToken(IBinder binder) {
291 return mTokenMap.get(binder);
292 }
293
294 AppWindowToken getAppWindowToken(IBinder binder) {
295 final WindowToken token = getWindowToken(binder);
296 if (token == null) {
297 return null;
298 }
299 return token.asAppWindowToken();
300 }
301
302 void setWindowToken(IBinder binder, WindowToken token) {
303 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
304 if (dc != null) {
305 // We currently don't support adding a window token to the display if the display
306 // already has the binder mapped to another token. If there is a use case for supporting
307 // this moving forward we will either need to merge the WindowTokens some how or have
308 // the binder map to a list of window tokens.
309 throw new IllegalArgumentException("Can't map token=" + token + " to display=" + this
310 + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
311 }
312 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700313
314 if (token.asAppWindowToken() == null) {
315 // Add non-app token to container hierarchy on the display. App tokens are added through
316 // the parent container managing them (e.g. Tasks).
317 mNonAppWindowContainers.addChild(token, null);
318 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700319 }
320
321 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700322 final WindowToken token = mTokenMap.remove(binder);
323 if (token != null && token.asAppWindowToken() == null) {
324 mNonAppWindowContainers.removeChild(token);
325 }
326 return token;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700327 }
328
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700329 Display getDisplay() {
330 return mDisplay;
331 }
332
Craig Mautner59c00972012-07-30 12:10:24 -0700333 DisplayInfo getDisplayInfo() {
334 return mDisplayInfo;
335 }
336
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700337 DisplayMetrics getDisplayMetrics() {
338 return mDisplayMetrics;
339 }
340
Jorim Jaggi61f39a72015-10-29 16:54:18 +0100341 DockedStackDividerController getDockedDividerController() {
342 return mDividerControllerLocked;
343 }
344
Winson Chung655332c2016-10-31 13:14:28 -0700345 PinnedStackController getPinnedStackController() {
346 return mPinnedStackControllerLocked;
347 }
348
Jeff Browna506a6e2013-06-04 00:02:38 -0700349 /**
350 * Returns true if the specified UID has access to this display.
351 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700352 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700353 return mDisplay.hasAccess(uid);
354 }
355
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700356 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700357 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -0700358 }
359
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700360 TaskStack getHomeStack() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700361 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800362 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
Craig Mautnere0a38842013-12-16 16:14:02 -0800363 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700364 return mHomeStack;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700365 }
366
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700367 TaskStack getStackById(int stackId) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700368 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
369 final TaskStack stack = mTaskStackContainers.get(i);
Chong Zhangd9d35bd2016-08-04 17:55:21 -0700370 if (stack.mStackId == stackId) {
371 return stack;
372 }
373 }
374 return null;
375 }
376
Andrii Kulian441e4492016-09-29 15:25:00 -0700377 @Override
378 void onConfigurationChanged(Configuration newParentConfig) {
379 super.onConfigurationChanged(newParentConfig);
380
Andrii Kulian3a507b52016-09-19 18:14:12 -0700381 // The display size information is heavily dependent on the resources in the current
382 // configuration, so we need to reconfigure it every time the configuration changes.
383 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
384 mService.reconfigureDisplayLocked(this);
385
386 getDockedDividerController().onConfigurationChanged();
Winson Chung655332c2016-10-31 13:14:28 -0700387 getPinnedStackController().onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -0700388 }
Andrii Kulian3a507b52016-09-19 18:14:12 -0700389
Andrii Kulian441e4492016-09-29 15:25:00 -0700390 /**
391 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
392 * bounds were updated.
393 */
394 void updateStackBoundsAfterConfigChange(@NonNull List<Integer> changedStackList) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700395 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
396 final TaskStack stack = mTaskStackContainers.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -0700397 if (stack.updateBoundsAfterConfigChange()) {
Andrii Kulian3a507b52016-09-19 18:14:12 -0700398 changedStackList.add(stack.mStackId);
399 }
400 }
401 }
402
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700403 @Override
404 boolean fillsParent() {
405 return true;
406 }
407
408 @Override
409 boolean isVisible() {
410 return true;
411 }
412
413 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700414 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -0700415 super.onAppTransitionDone();
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700416 rebuildAppWindowList();
417 }
418
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700419 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -0700420 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700421 final WindowManagerPolicy policy = mService.mPolicy;
422
423 // TODO: All the logic before the last return statement in this method should really go in
424 // #NonAppWindowContainer.getOrientation() since it is trying to decide orientation based
425 // on non-app windows. But, we can not do that until the window list is always correct in
426 // terms of z-ordering based on layers.
427 if (mService.mDisplayFrozen) {
428 if (mService.mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
429 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
430 "Display is frozen, return " + mService.mLastWindowForcedOrientation);
431 // If the display is frozen, some activities may be in the middle of restarting, and
432 // thus have removed their old window. If the window has the flag to hide the lock
433 // screen, then the lock screen can re-appear and inflict its own orientation on us.
434 // Keep the orientation stable until this all settles down.
435 return mService.mLastWindowForcedOrientation;
436 } else if (policy.isKeyguardLocked()) {
437 // Use the last orientation the while the display is frozen with the keyguard
438 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
439 // window. We don't want to check the show when locked window directly though as
440 // things aren't stable while the display is frozen, for example the window could be
441 // momentarily unavailable due to activity relaunch.
442 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
443 + "return " + mService.mLastOrientation);
444 return mService.mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -0700445 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700446 } else {
447 for (int pos = mWindows.size() - 1; pos >= 0; --pos) {
448 final WindowState win = mWindows.get(pos);
449 if (win.mAppToken != null) {
450 // We hit an application window. so the orientation will be determined by the
451 // app window. No point in continuing further.
452 break;
453 }
454 if (!win.isVisibleLw() || !win.mPolicyVisibilityAfterAnim) {
455 continue;
456 }
457 int req = win.mAttrs.screenOrientation;
458 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND) {
459 continue;
460 }
461
462 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req);
463 if (policy.isKeyguardHostWindow(win.mAttrs)) {
464 mService.mLastKeyguardForcedOrientation = req;
465 }
466 return (mService.mLastWindowForcedOrientation = req);
467 }
468 mService.mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
469
470 if (policy.isKeyguardLocked()) {
471 // The screen is locked and no top system window is requesting an orientation.
472 // Return either the orientation of the show-when-locked app (if there is any) or
473 // the orientation of the keyguard. No point in searching from the rest of apps.
474 WindowState winShowWhenLocked = (WindowState) policy.getWinShowWhenLockedLw();
475 AppWindowToken appShowWhenLocked = winShowWhenLocked == null
476 ? null : winShowWhenLocked.mAppToken;
477 if (appShowWhenLocked != null) {
478 int req = appShowWhenLocked.getOrientation();
479 if (req == SCREEN_ORIENTATION_BEHIND) {
480 req = mService.mLastKeyguardForcedOrientation;
481 }
482 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Done at " + appShowWhenLocked
483 + " -- show when locked, return " + req);
484 return req;
485 }
486 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
487 "No one is requesting an orientation when the screen is locked");
488 return mService.mLastKeyguardForcedOrientation;
489 }
Wale Ogunwale51362492016-09-08 17:49:17 -0700490 }
491
Wale Ogunwalee6f806e2016-10-20 15:29:42 -0700492 // Top system windows are not requesting an orientation. Start searching from apps.
493 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -0700494 }
495
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700496 void updateDisplayInfo() {
Craig Mautner722285e2012-09-07 13:55:58 -0700497 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700498 mDisplay.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700499 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
500 mTaskStackContainers.get(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800501 }
Craig Mautner722285e2012-09-07 13:55:58 -0700502 }
503
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700504 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700505 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
506 if (displayManagerInternal != null) {
507 // Bootstrap the default logical display from the display manager.
508 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
509 if (newDisplayInfo != null) {
510 mDisplayInfo.copyFrom(newDisplayInfo);
511 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700512 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700513
Filip Gruszczynski608797e2015-11-12 19:08:20 -0800514 mBaseDisplayWidth = mInitialDisplayWidth = mDisplayInfo.logicalWidth;
515 mBaseDisplayHeight = mInitialDisplayHeight = mDisplayInfo.logicalHeight;
516 mBaseDisplayDensity = mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
517 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700518 }
519
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700520 void getLogicalDisplayRect(Rect out) {
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700521 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Craig Mautner4a1cb222013-12-04 16:14:06 -0800522 final int orientation = mDisplayInfo.rotation;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700523 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
Craig Mautner4a1cb222013-12-04 16:14:06 -0800524 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
525 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700526 int width = mDisplayInfo.logicalWidth;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800527 int left = (physWidth - width) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700528 int height = mDisplayInfo.logicalHeight;
Craig Mautner4a1cb222013-12-04 16:14:06 -0800529 int top = (physHeight - height) / 2;
Craig Mautner46ac6fa2013-08-01 10:06:34 -0700530 out.set(left, top, left + width, top + height);
531 }
532
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700533 private void getLogicalDisplayRect(Rect out, int orientation) {
534 getLogicalDisplayRect(out);
535
536 // Rotate the Rect if needed.
537 final int currentRotation = mDisplayInfo.rotation;
538 final int rotationDelta = deltaRotation(currentRotation, orientation);
539 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
540 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
541 mTmpRectF.set(out);
542 mTmpMatrix.mapRect(mTmpRectF);
543 mTmpRectF.round(out);
544 }
545 }
546
Chong Zhangf66db432016-01-13 10:39:51 -0800547 void getContentRect(Rect out) {
548 out.set(mContentRect);
549 }
550
Wale Ogunwaleddc1cb22015-07-25 19:23:04 -0700551 /** Refer to {@link WindowManagerService#attachStack(int, int, boolean)} */
552 void attachStack(TaskStack stack, boolean onTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700553 mTaskStackContainers.attachStack(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -0800554 }
555
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800556 void moveStack(TaskStack stack, boolean toTop) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700557 mTaskStackContainers.moveStack(stack, toTop);
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700558 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700559
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700560 @Override
561 protected void addChild(DisplayChildWindowContainer child,
562 Comparator<DisplayChildWindowContainer> comparator) {
563 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
564 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -0700565
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700566 @Override
567 protected void addChild(DisplayChildWindowContainer child, int index) {
568 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
569 }
570
571 @Override
572 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700573 // Only allow removal of direct children from this display if the display is in the process
574 // of been removed.
575 if (mRemovingDisplay) {
576 super.removeChild(child);
577 return;
578 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700579 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800580 }
581
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700582 int taskIdFromPoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700583 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
584 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700585 final int taskId = stack.taskIdFromPoint(x, y);
586 if (taskId != -1) {
587 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -0700588 }
589 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -0800590 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -0700591 }
592
Chong Zhang8e89b312015-09-09 15:09:30 -0700593 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -0800594 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -0700595 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -0700596 */
Wale Ogunwale15ead902016-09-02 14:30:11 -0700597 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700598 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700599 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700600 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
601 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3797c222015-10-27 14:21:58 -0700602 if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700603 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -0700604 }
Chong Zhang9184ec62015-09-24 12:32:21 -0700605
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700606 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
607 if (mTmpTaskForResizePointSearchResult.searchDone) {
608 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -0700609 }
610 }
Chong Zhang9184ec62015-09-24 12:32:21 -0700611 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -0700612 }
613
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700614 void setTouchExcludeRegion(Task focusedTask) {
Craig Mautner6601b7b2013-04-29 10:29:11 -0700615 mTouchExcludeRegion.set(mBaseDisplayRect);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700616 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700617 mTmpRect2.setEmpty();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700618 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
619 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700620 stack.setTouchExcludeRegion(
621 focusedTask, delta, mTouchExcludeRegion, mContentRect, mTmpRect2);
Craig Mautner6601b7b2013-04-29 10:29:11 -0700622 }
Chong Zhangd8ceb852015-11-11 14:53:41 -0800623 // If we removed the focused task above, add it back and only leave its
624 // outside touch area in the exclusion. TapDectector is not interested in
625 // any touch inside the focused task itself.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700626 if (!mTmpRect2.isEmpty()) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800627 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
628 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800629 final WindowState inputMethod = mService.mInputMethodWindow;
630 if (inputMethod != null && inputMethod.isVisibleLw()) {
631 // If the input method is visible and the user is typing, we don't want these touch
632 // events to be intercepted and used to change focus. This would likely cause a
633 // disappearance of the input method.
634 inputMethod.getTouchableRegion(mTmpRegion);
635 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
636 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800637 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
638 WindowState win = mTapExcludedWindows.get(i);
639 win.getTouchableRegion(mTmpRegion);
640 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
641 }
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100642 if (getDockedStackVisibleForUserLocked() != null) {
643 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -0700644 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100645 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
646 }
Craig Mautner1bef3892015-02-17 15:09:47 -0800647 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700648 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -0800649 }
Craig Mautner6601b7b2013-04-29 10:29:11 -0700650 }
651
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700652 void switchUser() {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700653 final int count = mWindows.size();
654 for (int i = 0; i < count; i++) {
655 final WindowState win = mWindows.get(i);
Craig Mautner858d8a62013-04-23 17:08:34 -0700656 if (win.isHiddenFromUserLocked()) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800657 if (DEBUG_VISIBILITY) Slog.w(TAG_WM, "user changing, hiding " + win
Wale Ogunwale498e8c92015-02-13 09:42:46 -0800658 + ", attrs=" + win.mAttrs.type + ", belonging to " + win.mOwnerUid);
Craig Mautner858d8a62013-04-23 17:08:34 -0700659 win.hideLw(false);
660 }
661 }
Craig Mautnerac6f8432013-07-17 13:24:59 -0700662
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700663 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
664 mTaskStackContainers.get(stackNdx).switchUser();
Craig Mautner858d8a62013-04-23 17:08:34 -0700665 }
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700666
667 rebuildAppWindowList();
Craig Mautner858d8a62013-04-23 17:08:34 -0700668 }
669
Craig Mautner05d29032013-05-03 13:40:13 -0700670 void resetAnimationBackgroundAnimator() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700671 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
672 mTaskStackContainers.get(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -0700673 }
674 }
675
676 boolean animateDimLayers() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800677 return mDimLayerController.animateDimLayers();
Craig Mautner05d29032013-05-03 13:40:13 -0700678 }
679
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700680 private void resetDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800681 mDimLayerController.resetDimming();
Craig Mautner05d29032013-05-03 13:40:13 -0700682 }
683
684 boolean isDimming() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800685 return mDimLayerController.isDimming();
Craig Mautner05d29032013-05-03 13:40:13 -0700686 }
687
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700688 private void stopDimmingIfNeeded() {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800689 mDimLayerController.stopDimmingIfNeeded();
Craig Mautner05d29032013-05-03 13:40:13 -0700690 }
691
Wale Ogunwale10124582016-09-15 20:25:50 -0700692 @Override
693 void removeIfPossible() {
694 if (isAnimating()) {
695 mDeferredRemoval = true;
696 return;
Craig Mautner2eb15342013-08-07 13:13:35 -0700697 }
Wale Ogunwale10124582016-09-15 20:25:50 -0700698 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -0700699 }
700
Wale Ogunwale10124582016-09-15 20:25:50 -0700701 @Override
702 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700703 mRemovingDisplay = true;
704 try {
705 super.removeImmediately();
706 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
707 mDimLayerController.close();
708 if (mDisplayId == DEFAULT_DISPLAY) {
709 mService.unregisterPointerEventListener(mTapDetector);
710 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
711 }
712 } finally {
713 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -0800714 }
Craig Mautner95da1082014-02-24 17:54:35 -0800715 }
716
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700717 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -0700718 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700719 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -0700720 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
721
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700722 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -0700723 removeImmediately();
Craig Mautner95da1082014-02-24 17:54:35 -0800724 mService.onDisplayRemoved(mDisplayId);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700725 return false;
Craig Mautner95da1082014-02-24 17:54:35 -0800726 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700727 return true;
Craig Mautner95da1082014-02-24 17:54:35 -0800728 }
729
Wale Ogunwale10124582016-09-15 20:25:50 -0700730 boolean animateForIme(float interpolatedValue, float animationTarget,
731 float dividerAnimationTarget) {
732 boolean updated = false;
733
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700734 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
735 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700736 if (stack == null || !stack.isAdjustedForIme()) {
737 continue;
738 }
739
740 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
741 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
742 updated = true;
743 } else {
744 mDividerControllerLocked.mLastAnimationProgress =
745 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
746 mDividerControllerLocked.mLastDividerProgress =
747 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
748 updated |= stack.updateAdjustForIme(
749 mDividerControllerLocked.mLastAnimationProgress,
750 mDividerControllerLocked.mLastDividerProgress,
751 false /* force */);
752 }
753 if (interpolatedValue >= 1f) {
754 stack.endImeAdjustAnimation();
755 }
756 }
757
758 return updated;
759 }
760
761 boolean clearImeAdjustAnimation() {
762 boolean changed = false;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700763 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
764 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700765 if (stack != null && stack.isAdjustedForIme()) {
766 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
767 changed = true;
768 }
769 }
770 return changed;
771 }
772
773 void beginImeAdjustAnimation() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700774 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
775 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700776 if (stack.isVisible() && stack.isAdjustedForIme()) {
777 stack.beginImeAdjustAnimation();
778 }
779 }
780 }
781
782 void adjustForImeIfNeeded() {
783 final WindowState imeWin = mService.mInputMethodWindow;
784 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
785 && !mDividerControllerLocked.isImeHideRequested();
786 final boolean dockVisible = mService.isStackVisibleLocked(DOCKED_STACK_ID);
787 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
788 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
789 imeTargetStack.getDockSide() : DOCKED_INVALID;
790 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
791 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
792 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
793 final int imeHeight = mService.mPolicy.getInputMethodWindowVisibleHeightLw();
794 final boolean imeHeightChanged = imeVisible &&
795 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
796
797 // The divider could be adjusted for IME position, or be thinner than usual,
798 // or both. There are three possible cases:
799 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
800 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
801 // - If IME is not visible, divider is not moved and is normal width.
802
803 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700804 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
805 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700806 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
807 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)) {
808 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
809 } else {
810 stack.resetAdjustedForIme(false);
811 }
812 }
813 mDividerControllerLocked.setAdjustedForIme(
814 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
815 } else {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700816 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
817 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale10124582016-09-15 20:25:50 -0700818 stack.resetAdjustedForIme(!dockVisible);
819 }
820 mDividerControllerLocked.setAdjustedForIme(
821 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
822 }
Winson Chung655332c2016-10-31 13:14:28 -0700823 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -0700824 }
825
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700826 void setInputMethodAnimLayerAdjustment(int adj) {
827 if (DEBUG_LAYERS) Slog.v(TAG_WM, "Setting im layer adj to " + adj);
828 mInputMethodAnimLayerAdjustment = adj;
829 final WindowState imw = mService.mInputMethodWindow;
830 if (imw != null) {
831 imw.adjustAnimLayer(adj);
832 }
833 for (int i = mService.mInputMethodDialogs.size() - 1; i >= 0; i--) {
834 final WindowState dialog = mService.mInputMethodDialogs.get(i);
835 // TODO: This and other places setting mAnimLayer can probably use WS.adjustAnimLayer,
836 // but need to make sure we are not setting things twice for child windows that are
837 // already in the list.
838 dialog.mWinAnimator.mAnimLayer = dialog.mLayer + adj;
839 if (DEBUG_LAYERS) Slog.v(TAG_WM, "IM win " + imw
840 + " anim layer: " + dialog.mWinAnimator.mAnimLayer);
841 }
842 }
843
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700844 /**
845 * If a window that has an animation specifying a colored background and the current wallpaper
846 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
847 * suddenly disappear.
848 */
849 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
850 for (int i = mWindows.size() - 1; i >= 0; --i) {
851 final WindowState win = mWindows.get(i);
852 if (win.mIsWallpaper && win.isVisibleNow()) {
853 return win.mWinAnimator.mAnimLayer;
854 }
855 }
856 return winAnimator.mAnimLayer;
857 }
858
Wale Ogunwale10124582016-09-15 20:25:50 -0700859 void prepareFreezingTaskBounds() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700860 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
861 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -0700862 stack.prepareFreezingTaskBounds();
863 }
864 }
865
Wale Ogunwale94744212015-09-21 19:01:47 -0700866 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700867 getLogicalDisplayRect(mTmpRect, newRotation);
868
869 // Compute a transform matrix to undo the coordinate space transformation,
870 // and present the window at the same physical position it previously occupied.
871 final int deltaRotation = deltaRotation(newRotation, oldRotation);
872 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
873
874 mTmpRectF.set(bounds);
875 mTmpMatrix.mapRect(mTmpRectF);
876 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -0700877 }
878
Wale Ogunwale4a02d812015-02-12 23:01:38 -0800879 static int deltaRotation(int oldRotation, int newRotation) {
880 int delta = newRotation - oldRotation;
881 if (delta < 0) delta += 4;
882 return delta;
883 }
884
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700885 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700886 Matrix outMatrix) {
887 // For rotations without Z-ordering we don't need the target rectangle's position.
888 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
889 displayHeight, outMatrix);
890 }
891
892 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
893 float displayWidth, float displayHeight, Matrix outMatrix) {
894 switch (rotation) {
895 case ROTATION_0:
896 outMatrix.reset();
897 break;
898 case ROTATION_270:
899 outMatrix.setRotate(270, 0, 0);
900 outMatrix.postTranslate(0, displayHeight);
901 outMatrix.postTranslate(rectTop, 0);
902 break;
903 case ROTATION_180:
904 outMatrix.reset();
905 break;
906 case ROTATION_90:
907 outMatrix.setRotate(90, 0, 0);
908 outMatrix.postTranslate(displayWidth, 0);
909 outMatrix.postTranslate(-rectTop, rectLeft);
910 break;
911 }
912 }
913
Craig Mautnera91f9e22012-09-14 16:22:08 -0700914 public void dump(String prefix, PrintWriter pw) {
915 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
916 final String subPrefix = " " + prefix;
917 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
918 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
919 pw.print("dpi");
920 if (mInitialDisplayWidth != mBaseDisplayWidth
921 || mInitialDisplayHeight != mBaseDisplayHeight
922 || mInitialDisplayDensity != mBaseDisplayDensity) {
923 pw.print(" base=");
924 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
925 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
926 }
Jeff Brownd46747a2015-04-15 19:02:36 -0700927 if (mDisplayScalingDisabled) {
928 pw.println(" noscale");
929 }
Craig Mautnera91f9e22012-09-14 16:22:08 -0700930 pw.print(" cur=");
931 pw.print(mDisplayInfo.logicalWidth);
932 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
933 pw.print(" app=");
934 pw.print(mDisplayInfo.appWidth);
935 pw.print("x"); pw.print(mDisplayInfo.appHeight);
936 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
937 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
938 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
939 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700940 pw.println(subPrefix + "deferred=" + mDeferredRemoval
941 + " mLayoutNeeded=" + mLayoutNeeded);
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800942
Craig Mautnerdc548482014-02-05 13:35:24 -0800943 pw.println();
Craig Mautnere8b85fd2014-10-22 09:23:25 -0700944 pw.println(" Application tokens in top down Z order:");
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700945 for (int stackNdx = mTaskStackContainers.size() - 1; stackNdx >= 0; --stackNdx) {
946 final TaskStack stack = mTaskStackContainers.get(stackNdx);
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800947 stack.dump(prefix + " ", pw);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700948 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800949
Craig Mautnerdc548482014-02-05 13:35:24 -0800950 pw.println();
951 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -0700952 pw.println();
953 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -0800954 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700955 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -0700956 pw.print(" Exiting #"); pw.print(i);
957 pw.print(' '); pw.print(token);
958 pw.println(':');
959 token.dump(pw, " ");
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800960 }
Craig Mautnerde4ef022013-04-07 19:01:33 -0700961 }
Craig Mautner59c00972012-07-30 12:10:24 -0700962 pw.println();
Chong Zhang112eb8c2015-11-02 11:17:00 -0800963 mDimLayerController.dump(prefix + " ", pw);
Jorim Jaggi31f71702016-05-04 16:43:04 -0700964 pw.println();
965 mDividerControllerLocked.dump(prefix + " ", pw);
Winson Chung655332c2016-10-31 13:14:28 -0700966 pw.println();
967 mPinnedStackControllerLocked.dump(prefix + " ", pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700968
969 if (mInputMethodAnimLayerAdjustment != 0) {
970 pw.println(subPrefix
971 + "mInputMethodAnimLayerAdjustment=" + mInputMethodAnimLayerAdjustment);
972 }
Craig Mautner59c00972012-07-30 12:10:24 -0700973 }
Craig Mautnere0a38842013-12-16 16:14:02 -0800974
975 @Override
976 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700977 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700978 }
979
980 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700981 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -0800982 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700983
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800984 /**
985 * @return The docked stack, but only if it is visible, and {@code null} otherwise.
986 */
Filip Gruszczynski3ddc5d62015-09-23 15:01:30 -0700987 TaskStack getDockedStackLocked() {
Wale Ogunwalee45899a2015-10-01 11:30:34 -0700988 final TaskStack stack = mService.mStackIdToStack.get(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700989 return (stack != null && stack.isVisible()) ? stack : null;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700990 }
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -0800991
992 /**
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800993 * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
994 * visible, as long as it's not hidden because the current user doesn't have any tasks there.
995 */
996 TaskStack getDockedStackVisibleForUserLocked() {
997 final TaskStack stack = mService.mStackIdToStack.get(DOCKED_STACK_ID);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700998 return (stack != null && stack.isVisible(true /* ignoreKeyguard */)) ? stack : null;
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800999 }
1000
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001001 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08001002 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
1003 WindowState touchedWin = null;
1004 final int x = (int) xf;
1005 final int y = (int) yf;
1006
1007 for (int i = mWindows.size() - 1; i >= 0; i--) {
1008 WindowState window = mWindows.get(i);
1009 final int flags = window.mAttrs.flags;
1010 if (!window.isVisibleLw()) {
1011 continue;
1012 }
1013 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
1014 continue;
1015 }
1016
1017 window.getVisibleBounds(mTmpRect);
1018 if (!mTmpRect.contains(x, y)) {
1019 continue;
1020 }
1021
1022 window.getTouchableRegion(mTmpRegion);
1023
1024 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
1025 if (mTmpRegion.contains(x, y) || touchFlags == 0) {
1026 touchedWin = window;
1027 break;
1028 }
1029 }
1030
1031 return touchedWin;
1032 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07001033
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001034 boolean canAddToastWindowForUid(int uid) {
1035 // We allow one toast window per UID being shown at a time.
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001036 final int windowCount = mWindows.size();
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001037 for (int i = 0; i < windowCount; i++) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001038 final WindowState window = mWindows.get(i);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001039 if (window.mAttrs.type == TYPE_TOAST && window.mOwnerUid == uid
Svet Ganov62a40f82016-09-29 00:43:51 -07001040 && !window.mPermanentlyHidden && !window.mAnimatingExit
1041 && !window.mRemoveOnExit) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001042 return false;
1043 }
1044 }
1045 return true;
1046 }
1047
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001048 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001049 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
1050 return;
1051 }
1052 final int lostFocusUid = oldFocus.mOwnerUid;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001053 final int windowCount = mWindows.size();
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001054 final Handler handler = mService.mH;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001055 for (int i = 0; i < windowCount; i++) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001056 final WindowState window = mWindows.get(i);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001057 if (window.mAttrs.type == TYPE_TOAST && window.mOwnerUid == lostFocusUid) {
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001058 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, window)) {
1059 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, window),
Svetoslav Ganovaa076532016-08-01 19:16:43 -07001060 window.mAttrs.hideTimeoutMilliseconds);
1061 }
1062 }
1063 }
1064 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001065
1066 WindowState findFocusedWindow() {
1067 final AppWindowToken focusedApp = mService.mFocusedApp;
1068
1069 for (int i = mWindows.size() - 1; i >= 0; i--) {
1070 final WindowState win = mWindows.get(i);
1071
1072 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + i + " = " + win
1073 + ", flags=" + win.mAttrs.flags + ", canReceive=" + win.canReceiveKeys());
1074
1075 if (!win.canReceiveKeys()) {
1076 continue;
1077 }
1078
1079 final AppWindowToken wtoken = win.mAppToken;
1080
1081 // If this window's application has been removed, just skip it.
1082 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
1083 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
1084 + (wtoken.removed ? "removed" : "sendingToBottom"));
1085 continue;
1086 }
1087
1088 if (focusedApp == null) {
1089 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
1090 + " using new focus @ " + i + " = " + win);
1091 return win;
1092 }
1093
1094 if (!focusedApp.windowsAreFocusable()) {
1095 // Current focused app windows aren't focusable...
1096 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
1097 + " focusable using new focus @ " + i + " = " + win);
1098 return win;
1099 }
1100
1101 // Descend through all of the app tokens and find the first that either matches
1102 // win.mAppToken (return win) or mFocusedApp (return null).
1103 if (wtoken != null && win.mAttrs.type != TYPE_APPLICATION_STARTING) {
Wale Ogunwale10124582016-09-15 20:25:50 -07001104 if (focusedApp.compareTo(wtoken) > 0) {
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07001105 // App stack below focused app stack. No focus for you!!!
1106 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
1107 "findFocusedWindow: Reached focused app=" + focusedApp);
1108 return null;
1109 }
1110 }
1111
1112 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ "
1113 + i + " = " + win);
1114 return win;
1115 }
1116
1117 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
1118 return null;
1119 }
Wale Ogunwaleec731152016-09-08 20:18:57 -07001120
Wale Ogunwale07bcab72016-10-14 15:30:09 -07001121 void addAppWindowToWindowList(final WindowState win) {
Wale Ogunwaleec731152016-09-08 20:18:57 -07001122 final IWindow client = win.mClient;
1123
1124 WindowList tokenWindowList = getTokenWindowsOnDisplay(win.mToken);
1125 if (!tokenWindowList.isEmpty()) {
Wale Ogunwale07bcab72016-10-14 15:30:09 -07001126 addAppWindowExisting(win, tokenWindowList);
1127 return;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001128 }
1129
1130 // No windows from this token on this display
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001131 if (localLOGV) Slog.v(TAG_WM, "Figuring out where to add app window "
Wale Ogunwaleec731152016-09-08 20:18:57 -07001132 + client.asBinder() + " (token=" + this + ")");
1133
1134 final WindowToken wToken = win.mToken;
1135
1136 // Figure out where the window should go, based on the order of applications.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001137 mTmpGetWindowOnDisplaySearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001138 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1139 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001140 stack.getWindowOnDisplayBeforeToken(this, wToken, mTmpGetWindowOnDisplaySearchResult);
1141 if (mTmpGetWindowOnDisplaySearchResult.reachedToken) {
Wale Ogunwaleec731152016-09-08 20:18:57 -07001142 // We have reach the token we are interested in. End search.
1143 break;
1144 }
1145 }
1146
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001147 WindowState pos = mTmpGetWindowOnDisplaySearchResult.foundWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001148
1149 // We now know the index into the apps. If we found an app window above, that gives us the
1150 // position; else we need to look some more.
1151 if (pos != null) {
1152 // Move behind any windows attached to this one.
Wale Ogunwale02319a62016-09-26 15:21:22 -07001153 final WindowToken atoken = getWindowToken(pos.mClient.asBinder());
Wale Ogunwaleec731152016-09-08 20:18:57 -07001154 if (atoken != null) {
1155 tokenWindowList = getTokenWindowsOnDisplay(atoken);
1156 final int NC = tokenWindowList.size();
1157 if (NC > 0) {
1158 WindowState bottom = tokenWindowList.get(0);
1159 if (bottom.mSubLayer < 0) {
1160 pos = bottom;
1161 }
1162 }
1163 }
1164 addWindowToListBefore(win, pos);
Wale Ogunwale07bcab72016-10-14 15:30:09 -07001165 return;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001166 }
1167
1168 // Continue looking down until we find the first token that has windows on this display.
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001169 mTmpGetWindowOnDisplaySearchResult.reset();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001170 for (int i = mTaskStackContainers.size() - 1; i >= 0; --i) {
1171 final TaskStack stack = mTaskStackContainers.get(i);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001172 stack.getWindowOnDisplayAfterToken(this, wToken, mTmpGetWindowOnDisplaySearchResult);
1173 if (mTmpGetWindowOnDisplaySearchResult.foundWindow != null) {
Wale Ogunwaleec731152016-09-08 20:18:57 -07001174 // We have found a window after the token. End search.
1175 break;
1176 }
1177 }
1178
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001179 pos = mTmpGetWindowOnDisplaySearchResult.foundWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001180
1181 if (pos != null) {
1182 // Move in front of any windows attached to this one.
Wale Ogunwale02319a62016-09-26 15:21:22 -07001183 final WindowToken atoken = getWindowToken(pos.mClient.asBinder());
Wale Ogunwaleec731152016-09-08 20:18:57 -07001184 if (atoken != null) {
1185 final WindowState top = atoken.getTopWindow();
1186 if (top != null && top.mSubLayer >= 0) {
1187 pos = top;
1188 }
1189 }
1190 addWindowToListAfter(win, pos);
Wale Ogunwale07bcab72016-10-14 15:30:09 -07001191 return;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001192 }
1193
1194 // Just search for the start of this layer.
1195 final int myLayer = win.mBaseLayer;
1196 int i;
1197 for (i = mWindows.size() - 1; i >= 0; --i) {
1198 final WindowState w = mWindows.get(i);
1199 // Dock divider shares the base layer with application windows, but we want to always
1200 // keep it above the application windows. The sharing of the base layer is intended
1201 // for window animations, which need to be above the dock divider for the duration
1202 // of the animation.
1203 if (w.mBaseLayer <= myLayer && w.mAttrs.type != TYPE_DOCK_DIVIDER) {
1204 break;
1205 }
1206 }
1207 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1208 "Based on layer: Adding window " + win + " at " + (i + 1) + " of "
1209 + mWindows.size());
1210 mWindows.add(i + 1, win);
1211 mService.mWindowsChanged = true;
Wale Ogunwaleec731152016-09-08 20:18:57 -07001212 }
1213
1214 /** Adds this non-app window to the window list. */
1215 void addNonAppWindowToWindowList(WindowState win) {
1216 // Figure out where window should go, based on layer.
1217 int i;
1218 for (i = mWindows.size() - 1; i >= 0; i--) {
1219 final WindowState otherWin = mWindows.get(i);
1220 if (otherWin.getBaseType() != TYPE_WALLPAPER && otherWin.mBaseLayer <= win.mBaseLayer) {
1221 // Wallpaper wanders through the window list, for example to position itself
1222 // directly behind keyguard. Because of this it will break the ordering based on
1223 // WindowState.mBaseLayer. There might windows with higher mBaseLayer behind it and
1224 // we don't want the new window to appear above them. An example of this is adding
1225 // of the docked stack divider. Consider a scenario with the following ordering (top
1226 // to bottom): keyguard, wallpaper, assist preview, apps. We want the dock divider
1227 // to land below the assist preview, so the dock divider must ignore the wallpaper,
1228 // with which it shares the base layer.
1229 break;
1230 }
1231 }
1232
1233 i++;
1234 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1235 "Free window: Adding window " + this + " at " + i + " of " + mWindows.size());
1236 mWindows.add(i, win);
1237 mService.mWindowsChanged = true;
1238 }
1239
Wale Ogunwaleb9a07c32016-10-12 14:55:56 -07001240 void addToWindowList(WindowState win, int index) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001241 mService.mWindowsChanged = true;
Wale Ogunwaleb9a07c32016-10-12 14:55:56 -07001242 mWindows.add(index, win);
1243 }
1244
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001245 boolean removeFromWindowList(WindowState win) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001246 mService.mWindowsChanged = true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001247 return mWindows.remove(win);
1248 }
1249
1250 private int removeWindowAndChildrenFromWindowList(WindowState win, int interestingPos) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001251 int wpos = mWindows.indexOf(win);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001252 if (wpos < 0) {
1253 return interestingPos;
1254 }
1255
1256 if (wpos < interestingPos) interestingPos--;
1257 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG_WM, "Temp removing at " + wpos + ": " + this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001258 mWindows.remove(wpos);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001259 mService.mWindowsChanged = true;
1260 int childWinCount = win.mChildren.size();
1261 while (childWinCount > 0) {
1262 childWinCount--;
1263 final WindowState cw = win.mChildren.get(childWinCount);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001264 int cpos = mWindows.indexOf(cw);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001265 if (cpos >= 0) {
1266 if (cpos < interestingPos) interestingPos--;
1267 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG_WM,
1268 "Temp removing child at " + cpos + ": " + cw);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001269 mWindows.remove(cpos);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001270 }
1271 }
1272 return interestingPos;
1273 }
1274
Wale Ogunwaleec731152016-09-08 20:18:57 -07001275 void addChildWindowToWindowList(WindowState win) {
1276 final WindowState parentWindow = win.getParentWindow();
1277
1278 WindowList windowsOnSameDisplay = getTokenWindowsOnDisplay(win.mToken);
1279
1280 // Figure out this window's ordering relative to the parent window.
1281 final int wCount = windowsOnSameDisplay.size();
1282 final int sublayer = win.mSubLayer;
1283 int largestSublayer = Integer.MIN_VALUE;
1284 WindowState windowWithLargestSublayer = null;
1285 int i;
1286 for (i = 0; i < wCount; i++) {
1287 WindowState w = windowsOnSameDisplay.get(i);
1288 final int wSublayer = w.mSubLayer;
1289 if (wSublayer >= largestSublayer) {
1290 largestSublayer = wSublayer;
1291 windowWithLargestSublayer = w;
1292 }
1293 if (sublayer < 0) {
1294 // For negative sublayers, we go below all windows in the same sublayer.
1295 if (wSublayer >= sublayer) {
1296 addWindowToListBefore(win, wSublayer >= 0 ? parentWindow : w);
1297 break;
1298 }
1299 } else {
1300 // For positive sublayers, we go above all windows in the same sublayer.
1301 if (wSublayer > sublayer) {
1302 addWindowToListBefore(win, w);
1303 break;
1304 }
1305 }
1306 }
1307 if (i >= wCount) {
1308 if (sublayer < 0) {
1309 addWindowToListBefore(win, parentWindow);
1310 } else {
1311 addWindowToListAfter(win,
1312 largestSublayer >= 0 ? windowWithLargestSublayer : parentWindow);
1313 }
1314 }
1315 }
1316
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001317 /** Updates the layer assignment of windows on this display. */
1318 void assignWindowLayers(boolean setLayoutNeeded) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001319 mLayersController.assignWindowLayers(mWindows.getReadOnly());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001320 if (setLayoutNeeded) {
1321 setLayoutNeeded();
1322 }
1323 }
1324
Wale Ogunwale0303c572016-10-20 10:16:29 -07001325 void adjustWallpaperWindows() {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001326 if (mWallpaperController.adjustWallpaperWindows(mWindows.getReadOnly())) {
Wale Ogunwale0303c572016-10-20 10:16:29 -07001327 assignWindowLayers(true /*setLayoutNeeded*/);
1328 }
1329 }
1330
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001331 /**
1332 * Z-orders the display window list so that:
1333 * <ul>
1334 * <li>Any windows that are currently below the wallpaper window stay below the wallpaper
1335 * window.
1336 * <li>Exiting application windows are at the bottom, but above the wallpaper window.
1337 * <li>All other application windows are above the exiting application windows and ordered based
1338 * on the ordering of their stacks and tasks on the display.
1339 * <li>Non-application windows are at the very top.
1340 * </ul>
1341 * <p>
1342 * NOTE: This isn't a complete picture of what the user see. Further manipulation of the window
1343 * surface layering is done in {@link WindowLayersController}.
1344 */
1345 void rebuildAppWindowList() {
1346 int count = mWindows.size();
1347 int i;
1348 int lastBelow = -1;
1349 int numRemoved = 0;
1350
1351 if (mRebuildTmp.length < count) {
1352 mRebuildTmp = new WindowState[count + 10];
1353 }
1354
1355 // First remove all existing app windows.
1356 i = 0;
1357 while (i < count) {
1358 final WindowState w = mWindows.get(i);
1359 if (w.mAppToken != null) {
1360 final WindowState win = mWindows.remove(i);
1361 win.mRebuilding = true;
1362 mRebuildTmp[numRemoved] = win;
1363 mService.mWindowsChanged = true;
1364 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG_WM, "Rebuild removing window: " + win);
1365 count--;
1366 numRemoved++;
1367 continue;
1368 } else if (lastBelow == i-1) {
1369 if (w.mAttrs.type == TYPE_WALLPAPER) {
1370 lastBelow = i;
1371 }
1372 }
1373 i++;
1374 }
1375
1376 // Keep whatever windows were below the app windows still below, by skipping them.
1377 lastBelow++;
1378 i = lastBelow;
1379
1380 // First add all of the exiting app tokens... these are no longer in the main app list,
1381 // but still have windows shown. We put them in the back because now that the animation is
1382 // over we no longer will care about them.
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001383 final int numStacks = mTaskStackContainers.size();
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001384 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001385 AppTokenList exitingAppTokens = mTaskStackContainers.get(stackNdx).mExitingAppTokens;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001386 int NT = exitingAppTokens.size();
1387 for (int j = 0; j < NT; j++) {
Wale Ogunwale360a8bc2016-10-10 13:25:26 -07001388 i = exitingAppTokens.get(j).rebuildWindowListUnchecked(i);
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001389 }
1390 }
1391
1392 // And add in the still active app tokens in Z order.
1393 for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001394 i = mTaskStackContainers.get(stackNdx).rebuildWindowList(i);
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001395 }
1396
1397 i -= lastBelow;
1398 if (i != numRemoved) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001399 setLayoutNeeded();
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001400 Slog.w(TAG_WM, "On display=" + mDisplayId + " Rebuild removed " + numRemoved
1401 + " windows but added " + i + " rebuildAppWindowListLocked() "
1402 + " callers=" + Debug.getCallers(10));
1403 for (i = 0; i < numRemoved; i++) {
1404 WindowState ws = mRebuildTmp[i];
1405 if (ws.mRebuilding) {
1406 StringWriter sw = new StringWriter();
1407 PrintWriter pw = new FastPrintWriter(sw, false, 1024);
1408 ws.dump(pw, "", true);
1409 pw.flush();
1410 Slog.w(TAG_WM, "This window was lost: " + ws);
1411 Slog.w(TAG_WM, sw.toString());
1412 ws.mWinAnimator.destroySurfaceLocked();
1413 }
1414 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001415 Slog.w(TAG_WM, "Current window hierarchy:");
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001416 dumpChildrenNames();
1417 Slog.w(TAG_WM, "Final window list:");
1418 dumpWindows();
1419 }
1420 Arrays.fill(mRebuildTmp, null);
1421 }
1422
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001423 /** Rebuilds the display's window list and does a relayout if something changed. */
1424 void rebuildAppWindowsAndLayoutIfNeeded() {
1425 mTmpWindows.clear();
1426 mTmpWindows.addAll(mWindows);
1427
1428 rebuildAppWindowList();
1429
1430 // Set displayContent.mLayoutNeeded if window order changed.
1431 final int tmpSize = mTmpWindows.size();
1432 final int winSize = mWindows.size();
1433 int tmpNdx = 0, winNdx = 0;
1434 while (tmpNdx < tmpSize && winNdx < winSize) {
1435 // Skip over all exiting windows, they've been moved out of order.
1436 WindowState tmp;
1437 do {
1438 tmp = mTmpWindows.get(tmpNdx++);
1439 } while (tmpNdx < tmpSize && tmp.mAppToken != null && tmp.mAppToken.mIsExiting);
1440
1441 WindowState win;
1442 do {
1443 win = mWindows.get(winNdx++);
1444 } while (winNdx < winSize && win.mAppToken != null && win.mAppToken.mIsExiting);
1445
1446 if (tmp != win) {
1447 // Window order changed.
1448 setLayoutNeeded();
1449 break;
1450 }
1451 }
1452 if (tmpNdx != winNdx) {
1453 // One list was different from the other.
1454 setLayoutNeeded();
1455 }
1456 mTmpWindows.clear();
1457
1458 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
1459 false /*updateInputWindows*/)) {
1460 assignWindowLayers(false /* setLayoutNeeded */);
1461 }
1462
1463 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
1464 mService.mWindowPlacerLocked.performSurfacePlacement();
1465 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
1466 }
1467
1468 void updateInputWindows(InputMonitor inputMonitor, WindowState inputFocus, boolean inDrag) {
1469 final InputConsumerImpl navInputConsumer =
1470 mService.mInputMonitor.getInputConsumer(INPUT_CONSUMER_NAVIGATION, mDisplayId);
1471 final InputConsumerImpl pipInputConsumer =
1472 mService.mInputMonitor.getInputConsumer(INPUT_CONSUMER_PIP, mDisplayId);
1473 final InputConsumerImpl wallpaperInputConsumer =
1474 mService.mInputMonitor.getInputConsumer(INPUT_CONSUMER_WALLPAPER, mDisplayId);
1475 boolean addInputConsumerHandle = navInputConsumer != null;
1476 boolean addPipInputConsumerHandle = pipInputConsumer != null;
1477 boolean addWallpaperInputConsumerHandle = wallpaperInputConsumer != null;
1478 final Rect pipTouchableBounds = addPipInputConsumerHandle ? new Rect() : null;
1479 boolean disableWallpaperTouchEvents = false;
1480
1481 for (int winNdx = mWindows.size() - 1; winNdx >= 0; --winNdx) {
1482 final WindowState child = mWindows.get(winNdx);
1483 final InputChannel inputChannel = child.mInputChannel;
1484 final InputWindowHandle inputWindowHandle = child.mInputWindowHandle;
1485 if (inputChannel == null || inputWindowHandle == null || child.mRemoved
1486 || child.isAdjustedForMinimizedDock()) {
1487 // Skip this window because it cannot possibly receive input.
1488 continue;
1489 }
1490
1491 if (addPipInputConsumerHandle
1492 && child.getStackId() == PINNED_STACK_ID
1493 && inputWindowHandle.layer <= pipInputConsumer.mWindowHandle.layer) {
1494 // Update the bounds of the Pip input consumer to match the Pinned stack
1495 child.getStack().getBounds(pipTouchableBounds);
1496 pipInputConsumer.mWindowHandle.touchableRegion.set(pipTouchableBounds);
1497 inputMonitor.addInputWindowHandle(pipInputConsumer.mWindowHandle);
1498 addPipInputConsumerHandle = false;
1499 }
1500
1501 if (addInputConsumerHandle
1502 && inputWindowHandle.layer <= navInputConsumer.mWindowHandle.layer) {
1503 inputMonitor.addInputWindowHandle(navInputConsumer.mWindowHandle);
1504 addInputConsumerHandle = false;
1505 }
1506
1507 if (addWallpaperInputConsumerHandle) {
1508 if (child.mAttrs.type == TYPE_WALLPAPER && child.isVisibleLw()) {
1509 // Add the wallpaper input consumer above the first visible wallpaper.
1510 inputMonitor.addInputWindowHandle(wallpaperInputConsumer.mWindowHandle);
1511 addWallpaperInputConsumerHandle = false;
1512 }
1513 }
1514
1515 final int flags = child.mAttrs.flags;
1516 final int privateFlags = child.mAttrs.privateFlags;
1517 final int type = child.mAttrs.type;
1518
1519 final boolean hasFocus = child == inputFocus;
1520 final boolean isVisible = child.isVisibleLw();
1521 if ((privateFlags & PRIVATE_FLAG_DISABLE_WALLPAPER_TOUCH_EVENTS) != 0) {
1522 disableWallpaperTouchEvents = true;
1523 }
1524 final boolean hasWallpaper = mWallpaperController.isWallpaperTarget(child)
1525 && (privateFlags & PRIVATE_FLAG_KEYGUARD) == 0
1526 && !disableWallpaperTouchEvents;
1527
1528 // If there's a drag in progress and 'child' is a potential drop target,
1529 // make sure it's been told about the drag
1530 if (inDrag && isVisible && isDefaultDisplay) {
1531 mService.mDragState.sendDragStartedIfNeededLw(child);
1532 }
1533
1534 inputMonitor.addInputWindowHandle(
1535 inputWindowHandle, child, flags, type, isVisible, hasFocus, hasWallpaper);
1536 }
1537
1538 if (addWallpaperInputConsumerHandle) {
1539 // No visible wallpaper found, add the wallpaper input consumer at the end.
1540 inputMonitor.addInputWindowHandle(wallpaperInputConsumer.mWindowHandle);
1541 }
1542 }
1543
1544 /** Returns true if a leaked surface was destroyed */
1545 boolean destroyLeakedSurfaces() {
1546 boolean leakedSurface = false;
1547 final int numWindows = mWindows.size();
1548 for (int winNdx = 0; winNdx < numWindows; ++winNdx) {
1549 final WindowState ws = mWindows.get(winNdx);
1550 final WindowStateAnimator wsa = ws.mWinAnimator;
1551 if (wsa.mSurfaceController == null) {
1552 continue;
1553 }
1554 if (!mService.mSessions.contains(wsa.mSession)) {
1555 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
1556 + ws + " surface=" + wsa.mSurfaceController
1557 + " token=" + ws.mToken
1558 + " pid=" + ws.mSession.mPid
1559 + " uid=" + ws.mSession.mUid);
1560 wsa.destroySurface();
1561 mService.mForceRemoves.add(ws);
1562 leakedSurface = true;
1563 } else if (ws.mAppToken != null && ws.mAppToken.clientHidden) {
1564 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
1565 + ws + " surface=" + wsa.mSurfaceController
1566 + " token=" + ws.mAppToken
1567 + " saved=" + ws.hasSavedSurface());
1568 if (SHOW_TRANSACTIONS) logSurface(ws, "LEAK DESTROY", false);
1569 wsa.destroySurface();
1570 leakedSurface = true;
1571 }
1572 }
1573
1574 return leakedSurface;
1575 }
1576
Wale Ogunwaleec731152016-09-08 20:18:57 -07001577 /** Return the list of Windows on this display associated with the input token. */
1578 WindowList getTokenWindowsOnDisplay(WindowToken token) {
1579 final WindowList windowList = new WindowList();
1580 final int count = mWindows.size();
1581 for (int i = 0; i < count; i++) {
1582 final WindowState win = mWindows.get(i);
1583 if (win.mToken == token) {
1584 windowList.add(win);
1585 }
1586 }
1587 return windowList;
1588 }
1589
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001590 private void reAddToWindowList(WindowState win) {
1591 win.mToken.addWindow(win);
1592 // This is a hack to get all of the child windows added as well at the right position. Child
1593 // windows should be rare and this case should be rare, so it shouldn't be that big a deal.
1594 int wpos = mWindows.indexOf(win);
1595 if (wpos >= 0) {
1596 if (DEBUG_WINDOW_MOVEMENT) Slog.v(TAG_WM, "ReAdd removing from " + wpos + ": " + win);
1597 mWindows.remove(wpos);
1598 mService.mWindowsChanged = true;
1599 win.reAddWindow(wpos);
1600 }
1601 }
1602
1603 void moveInputMethodDialogs(int pos) {
1604 ArrayList<WindowState> dialogs = mService.mInputMethodDialogs;
1605
1606 final int N = dialogs.size();
1607 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Removing " + N + " dialogs w/pos=" + pos);
1608 for (int i = 0; i < N; i++) {
1609 pos = removeWindowAndChildrenFromWindowList(dialogs.get(i), pos);
1610 }
1611 if (DEBUG_INPUT_METHOD) {
1612 Slog.v(TAG_WM, "Window list w/pos=" + pos);
1613 logWindowList(mWindows, " ");
1614 }
1615
1616 WindowState ime = mService.mInputMethodWindow;
1617 if (pos >= 0) {
1618 // Skip windows owned by the input method.
1619 if (ime != null) {
1620 while (pos < mWindows.size()) {
1621 WindowState wp = mWindows.get(pos);
1622 if (wp == ime || wp.getParentWindow() == ime) {
1623 pos++;
1624 continue;
1625 }
1626 break;
1627 }
1628 }
1629 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Adding " + N + " dialogs at pos=" + pos);
1630 for (int i=0; i<N; i++) {
1631 WindowState win = dialogs.get(i);
1632 pos = win.reAddWindow(pos);
1633 }
1634 if (DEBUG_INPUT_METHOD) {
1635 Slog.v(TAG_WM, "Final window list:");
1636 logWindowList(mWindows, " ");
1637 }
1638 return;
1639 }
1640 for (int i=0; i<N; i++) {
1641 WindowState win = dialogs.get(i);
1642 reAddToWindowList(win);
1643 if (DEBUG_INPUT_METHOD) {
1644 Slog.v(TAG_WM, "No IM target, final list:");
1645 logWindowList(mWindows, " ");
1646 }
1647 }
1648 }
1649
1650 boolean moveInputMethodWindowsIfNeeded(boolean needAssignLayers) {
1651 final WindowState imWin = mService.mInputMethodWindow;
1652 final int DN = mService.mInputMethodDialogs.size();
1653 if (imWin == null && DN == 0) {
1654 return false;
1655 }
1656
1657 // TODO(multidisplay): IMEs are only supported on the default display.
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001658 int imPos = findDesiredInputMethodWindowIndex(true);
1659 if (imPos >= 0) {
1660 // In this case, the input method windows are to be placed
1661 // immediately above the window they are targeting.
1662
1663 // First check to see if the input method windows are already
1664 // located here, and contiguous.
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001665 final int N = mWindows.size();
1666 final WindowState firstImWin = imPos < N ? mWindows.get(imPos) : null;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001667
1668 // Figure out the actual input method window that should be
1669 // at the bottom of their stack.
1670 WindowState baseImWin = imWin != null ? imWin : mService.mInputMethodDialogs.get(0);
1671 final WindowState cw = baseImWin.getBottomChild();
1672 if (cw != null && cw.mSubLayer < 0) {
1673 baseImWin = cw;
1674 }
1675
1676 if (firstImWin == baseImWin) {
1677 // The windows haven't moved... but are they still contiguous?
1678 // First find the top IM window.
1679 int pos = imPos+1;
1680 while (pos < N) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001681 if (!(mWindows.get(pos)).mIsImWindow) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001682 break;
1683 }
1684 pos++;
1685 }
1686 pos++;
1687 // Now there should be no more input method windows above.
1688 while (pos < N) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001689 if ((mWindows.get(pos)).mIsImWindow) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001690 break;
1691 }
1692 pos++;
1693 }
1694 if (pos >= N) {
1695 return false;
1696 }
1697 }
1698
1699 if (imWin != null) {
1700 if (DEBUG_INPUT_METHOD) {
1701 Slog.v(TAG_WM, "Moving IM from " + imPos);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001702 logWindowList(mWindows, " ");
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001703 }
1704 imPos = removeWindowAndChildrenFromWindowList(imWin, imPos);
1705 if (DEBUG_INPUT_METHOD) {
1706 Slog.v(TAG_WM, "List after removing with new pos " + imPos + ":");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001707 logWindowList(mWindows, " ");
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001708 }
1709 imWin.reAddWindow(imPos);
1710 if (DEBUG_INPUT_METHOD) {
1711 Slog.v(TAG_WM, "List after moving IM to " + imPos + ":");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001712 logWindowList(mWindows, " ");
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001713 }
1714 if (DN > 0) moveInputMethodDialogs(imPos+1);
1715 } else {
1716 moveInputMethodDialogs(imPos);
1717 }
1718
1719 } else {
1720 // In this case, the input method windows go in a fixed layer,
1721 // because they aren't currently associated with a focus window.
1722
1723 if (imWin != null) {
1724 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Moving IM from " + imPos);
1725 removeWindowAndChildrenFromWindowList(imWin, 0);
1726 reAddToWindowList(imWin);
1727 if (DEBUG_INPUT_METHOD) {
1728 Slog.v(TAG_WM, "List with no IM target:");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001729 logWindowList(mWindows, " ");
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001730 }
1731 if (DN > 0) moveInputMethodDialogs(-1);
1732 } else {
1733 moveInputMethodDialogs(-1);
1734 }
1735
1736 }
1737
1738 if (needAssignLayers) {
1739 assignWindowLayers(false /* setLayoutNeeded */);
1740 }
1741
1742 return true;
1743 }
1744
1745 /**
1746 * Dig through the WindowStates and find the one that the Input Method will target.
1747 * @param willMove
1748 * @return The index+1 in mWindows of the discovered target.
1749 */
1750 int findDesiredInputMethodWindowIndex(boolean willMove) {
1751 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
1752 // same display. Or even when the current IME/target are not on the same screen as the next
1753 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001754 WindowState w = null;
1755 int i;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001756 for (i = mWindows.size() - 1; i >= 0; --i) {
1757 final WindowState win = mWindows.get(i);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001758
1759 if (DEBUG_INPUT_METHOD && willMove) Slog.i(TAG_WM, "Checking window @" + i
1760 + " " + win + " fl=0x" + Integer.toHexString(win.mAttrs.flags));
1761 if (canBeImeTarget(win)) {
1762 w = win;
1763 //Slog.i(TAG_WM, "Putting input method here!");
1764
1765 // Yet more tricksyness! If this window is a "starting" window, we do actually want
1766 // to be on top of it, but it is not -really- where input will go. So if the caller
1767 // is not actually looking to move the IME, look down below for a real window to
1768 // target...
1769 if (!willMove && w.mAttrs.type == TYPE_APPLICATION_STARTING && i > 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001770 final WindowState wb = mWindows.get(i-1);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001771 if (wb.mAppToken == w.mAppToken && canBeImeTarget(wb)) {
1772 i--;
1773 w = wb;
1774 }
1775 }
1776 break;
1777 }
1778 }
1779
1780 // Now w is either mWindows[0] or an IME (or null if mWindows is empty).
1781
1782 if (DEBUG_INPUT_METHOD && willMove) Slog.v(TAG_WM, "Proposed new IME target: " + w);
1783
1784 // Now, a special case -- if the last target's window is in the process of exiting, and is
1785 // above the new target, keep on the last target to avoid flicker. Consider for example a
1786 // Dialog with the IME shown: when the Dialog is dismissed, we want to keep the IME above it
1787 // until it is completely gone so it doesn't drop behind the dialog or its full-screen
1788 // scrim.
1789 final WindowState curTarget = mService.mInputMethodTarget;
1790 if (curTarget != null
1791 && curTarget.isDisplayedLw()
1792 && curTarget.isClosing()
1793 && (w == null || curTarget.mWinAnimator.mAnimLayer > w.mWinAnimator.mAnimLayer)) {
1794 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Current target higher, not changing");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001795 return mWindows.indexOf(curTarget) + 1;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001796 }
1797
1798 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target="
1799 + w + " willMove=" + willMove);
1800
1801 if (willMove && w != null) {
1802 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
1803 if (token != null) {
1804
1805 // Now some fun for dealing with window animations that modify the Z order. We need
1806 // to look at all windows below the current target that are in this app, finding the
1807 // highest visible one in layering.
1808 WindowState highestTarget = null;
1809 int highestPos = 0;
1810 if (token.mAppAnimator.animating || token.mAppAnimator.animation != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001811 WindowList curWindows = token.getDisplayContent().mWindows;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001812 int pos = curWindows.indexOf(curTarget);
1813 while (pos >= 0) {
1814 WindowState win = curWindows.get(pos);
1815 if (win.mAppToken != token) {
1816 break;
1817 }
1818 if (!win.mRemoved) {
1819 if (highestTarget == null || win.mWinAnimator.mAnimLayer >
1820 highestTarget.mWinAnimator.mAnimLayer) {
1821 highestTarget = win;
1822 highestPos = pos;
1823 }
1824 }
1825 pos--;
1826 }
1827 }
1828
1829 if (highestTarget != null) {
1830 final AppTransition appTransition = mService.mAppTransition;
1831 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
1832 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
1833 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
1834 + " new layer=" + w.mWinAnimator.mAnimLayer);
1835
1836 if (appTransition.isTransitionSet()) {
1837 // If we are currently setting up for an animation, hold everything until we
1838 // can find out what will happen.
1839 mService.mInputMethodTargetWaitingAnim = true;
1840 mService.mInputMethodTarget = highestTarget;
1841 return highestPos + 1;
1842 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
1843 highestTarget.mWinAnimator.mAnimLayer > w.mWinAnimator.mAnimLayer) {
1844 // If the window we are currently targeting is involved with an animation,
1845 // and it is on top of the next target we will be over, then hold off on
1846 // moving until that is done.
1847 mService.mInputMethodTargetWaitingAnim = true;
1848 mService.mInputMethodTarget = highestTarget;
1849 return highestPos + 1;
1850 }
1851 }
1852 }
1853 }
1854
1855 //Slog.i(TAG_WM, "Placing input method @" + (i+1));
1856 if (w != null) {
1857 if (willMove) {
1858 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
1859 + w + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
1860 mService.mInputMethodTarget = w;
1861 mService.mInputMethodTargetWaitingAnim = false;
1862 if (w.mAppToken != null) {
1863 setInputMethodAnimLayerAdjustment(
1864 w.mAppToken.mAppAnimator.animLayerAdjustment);
1865 } else {
1866 setInputMethodAnimLayerAdjustment(0);
1867 }
1868 }
1869
1870 // If the docked divider is visible, we still need to go through this whole excercise to
1871 // find the appropriate input method target (used for animations and dialog
1872 // adjustments), but for purposes of Z ordering we simply wish to place it above the
1873 // docked divider. Unless it is already above the divider.
1874 final WindowState dockedDivider = mDividerControllerLocked.getWindow();
1875 if (dockedDivider != null && dockedDivider.isVisibleLw()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001876 int dividerIndex = mWindows.indexOf(dockedDivider);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001877 if (dividerIndex > 0 && dividerIndex > i) {
1878 return dividerIndex + 1;
1879 }
1880 }
1881 return i+1;
1882 }
1883 if (willMove) {
1884 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
1885 + " to null." + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
1886 mService.mInputMethodTarget = null;
1887 setInputMethodAnimLayerAdjustment(0);
1888 }
1889 return -1;
1890 }
1891
1892 private static boolean canBeImeTarget(WindowState w) {
1893 final int fl = w.mAttrs.flags & (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM);
1894 final int type = w.mAttrs.type;
1895
1896 if (fl != 0 && fl != (FLAG_NOT_FOCUSABLE | FLAG_ALT_FOCUSABLE_IM)
1897 && type != TYPE_APPLICATION_STARTING) {
1898 return false;
1899 }
1900
1901 if (DEBUG_INPUT_METHOD) {
1902 Slog.i(TAG_WM, "isVisibleOrAdding " + w + ": " + w.isVisibleOrAdding());
1903 if (!w.isVisibleOrAdding()) {
1904 Slog.i(TAG_WM, " mSurfaceController=" + w.mWinAnimator.mSurfaceController
1905 + " relayoutCalled=" + w.mRelayoutCalled
1906 + " viewVis=" + w.mViewVisibility
1907 + " policyVis=" + w.mPolicyVisibility
1908 + " policyVisAfterAnim=" + w.mPolicyVisibilityAfterAnim
1909 + " parentHidden=" + w.isParentWindowHidden()
1910 + " exiting=" + w.mAnimatingExit + " destroying=" + w.mDestroying);
1911 if (w.mAppToken != null) {
1912 Slog.i(TAG_WM, " mAppToken.hiddenRequested=" + w.mAppToken.hiddenRequested);
1913 }
1914 }
1915 }
1916 return w.isVisibleOrAdding();
1917 }
1918
1919 private void logWindowList(final WindowList windows, String prefix) {
1920 int N = windows.size();
1921 while (N > 0) {
1922 N--;
1923 Slog.v(TAG_WM, prefix + "#" + N + ": " + windows.get(N));
1924 }
1925 }
1926
1927 boolean getNeedsMenu(WindowState win, WindowManagerPolicy.WindowState bottom) {
1928 int index = -1;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001929 while (true) {
1930 if (win.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
1931 return win.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
1932 }
1933 // If we reached the bottom of the range of windows we are considering,
1934 // assume no menu is needed.
1935 if (win == bottom) {
1936 return false;
1937 }
1938 // The current window hasn't specified whether menu key is needed; look behind it.
1939 // First, we may need to determine the starting position.
1940 if (index < 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001941 index = mWindows.indexOf(win);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001942 }
1943 index--;
1944 if (index < 0) {
1945 return false;
1946 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001947 win = mWindows.get(index);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07001948 }
1949 }
1950
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001951 void setLayoutNeeded() {
1952 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
1953 mLayoutNeeded = true;
1954 }
1955
Wale Ogunwalef7cab102016-10-25 15:25:14 -07001956 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001957 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
1958 mLayoutNeeded = false;
1959 }
1960
1961 boolean isLayoutNeeded() {
1962 return mLayoutNeeded;
1963 }
1964
Wale Ogunwale07bcab72016-10-14 15:30:09 -07001965 private void addAppWindowExisting(WindowState win, WindowList tokenWindowList) {
Wale Ogunwaleec731152016-09-08 20:18:57 -07001966
Wale Ogunwaleec731152016-09-08 20:18:57 -07001967 // If this application has existing windows, we simply place the new window on top of
1968 // them... but keep the starting window on top.
1969 if (win.mAttrs.type == TYPE_BASE_APPLICATION) {
1970 // Base windows go behind everything else.
1971 final WindowState lowestWindow = tokenWindowList.get(0);
1972 addWindowToListBefore(win, lowestWindow);
Wale Ogunwaleec731152016-09-08 20:18:57 -07001973 } else {
1974 final AppWindowToken atoken = win.mAppToken;
1975 final int windowListPos = tokenWindowList.size();
1976 final WindowState lastWindow = tokenWindowList.get(windowListPos - 1);
1977 if (atoken != null && lastWindow == atoken.startingWindow) {
1978 addWindowToListBefore(win, lastWindow);
Wale Ogunwaleec731152016-09-08 20:18:57 -07001979 } else {
1980 int newIdx = findIdxBasedOnAppTokens(win);
1981 // There is a window above this one associated with the same apptoken note that the
1982 // window could be a floating window that was created later or a window at the top
1983 // of the list of windows associated with this token.
1984 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1985 "not Base app: Adding window " + win + " at " + (newIdx + 1) + " of "
1986 + mWindows.size());
1987 mWindows.add(newIdx + 1, win);
Wale Ogunwaleec731152016-09-08 20:18:57 -07001988 mService.mWindowsChanged = true;
1989 }
1990 }
Wale Ogunwaleec731152016-09-08 20:18:57 -07001991 }
1992
1993 /** Places the first input window after the second input window in the window list. */
1994 private void addWindowToListAfter(WindowState first, WindowState second) {
1995 final int i = mWindows.indexOf(second);
1996 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
1997 "Adding window " + this + " at " + (i + 1) + " of " + mWindows.size()
1998 + " (after " + second + ")");
1999 mWindows.add(i + 1, first);
2000 mService.mWindowsChanged = true;
2001 }
2002
2003 /** Places the first input window before the second input window in the window list. */
2004 private void addWindowToListBefore(WindowState first, WindowState second) {
2005 int i = mWindows.indexOf(second);
2006 if (DEBUG_FOCUS || DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
2007 "Adding window " + this + " at " + i + " of " + mWindows.size()
2008 + " (before " + second + ")");
2009 if (i < 0) {
2010 Slog.w(TAG_WM, "addWindowToListBefore: Unable to find " + second + " in " + mWindows);
2011 i = 0;
2012 }
2013 mWindows.add(i, first);
2014 mService.mWindowsChanged = true;
2015 }
2016
2017 /**
2018 * This method finds out the index of a window that has the same app token as win. used for z
2019 * ordering the windows in mWindows
2020 */
2021 private int findIdxBasedOnAppTokens(WindowState win) {
2022 for(int j = mWindows.size() - 1; j >= 0; j--) {
2023 final WindowState wentry = mWindows.get(j);
2024 if(wentry.mAppToken == win.mAppToken) {
2025 return j;
2026 }
2027 }
2028 return -1;
2029 }
2030
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002031 private void dumpChildrenNames() {
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07002032 StringBuilder output = new StringBuilder();
2033 dumpChildrenNames(output, " ");
2034 Slog.v(TAG_WM, output.toString());
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002035 }
2036
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002037 private void dumpWindows() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002038 Slog.v(TAG_WM, " Display #" + mDisplayId);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002039 for (int winNdx = mWindows.size() - 1; winNdx >= 0; --winNdx) {
2040 Slog.v(TAG_WM, " #" + winNdx + ": " + mWindows.get(winNdx));
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002041 }
2042 }
2043
Wale Ogunwale02319a62016-09-26 15:21:22 -07002044 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2045 if (mTokenMap.isEmpty()) {
2046 return;
2047 }
2048 pw.println(" Display #" + mDisplayId);
2049 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2050 while (it.hasNext()) {
2051 final WindowToken token = it.next();
2052 pw.print(" ");
2053 pw.print(token);
2054 if (dumpAll) {
2055 pw.println(':');
2056 token.dump(pw, " ");
2057 } else {
2058 pw.println();
2059 }
2060 }
2061 }
2062
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002063 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
2064 final int count = mWindows.size();
2065 for (int j = 0; j < count; j++) {
2066 final WindowStateAnimator wAnim = mWindows.get(j).mWinAnimator;
2067 pw.println(subPrefix + "Window #" + j + ": " + wAnim);
2068 }
2069 }
2070
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002071 void enableSurfaceTrace(FileDescriptor fd) {
2072 for (int i = mWindows.size() - 1; i >= 0; i--) {
2073 final WindowState win = mWindows.get(i);
2074 win.mWinAnimator.enableSurfaceTrace(fd);
2075 }
2076 }
2077
2078 void disableSurfaceTrace() {
2079 for (int i = mWindows.size() - 1; i >= 0; i--) {
2080 final WindowState win = mWindows.get(i);
2081 win.mWinAnimator.disableSurfaceTrace();
2082 }
2083 }
2084
Wale Ogunwale494009b82016-10-21 09:01:38 -07002085 boolean checkWaitingForWindows() {
2086
2087 boolean haveBootMsg = false;
2088 boolean haveApp = false;
2089 // if the wallpaper service is disabled on the device, we're never going to have
2090 // wallpaper, don't bother waiting for it
2091 boolean haveWallpaper = false;
2092 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2093 com.android.internal.R.bool.config_enableWallpaperService)
2094 && !mService.mOnlyCore;
2095 boolean haveKeyguard = true;
2096 final int count = mWindows.size();
2097 for (int i = 0; i < count; i++) {
2098 final WindowState w = mWindows.get(i);
2099 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2100 return true;
2101 }
2102 if (w.isDrawnLw()) {
2103 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
2104 haveBootMsg = true;
2105 } else if (w.mAttrs.type == TYPE_APPLICATION
2106 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
2107 haveApp = true;
2108 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
2109 haveWallpaper = true;
2110 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
2111 haveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
2112 }
2113 }
2114 }
2115
2116 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2117 "******** booted=" + mService.mSystemBooted
2118 + " msg=" + mService.mShowingBootMessages
2119 + " haveBoot=" + haveBootMsg + " haveApp=" + haveApp
2120 + " haveWall=" + haveWallpaper + " wallEnabled=" + wallpaperEnabled
2121 + " haveKeyguard=" + haveKeyguard);
2122
2123 // If we are turning on the screen to show the boot message, don't do it until the boot
2124 // message is actually displayed.
2125 if (!mService.mSystemBooted && !haveBootMsg) {
2126 return true;
2127 }
2128
2129 // If we are turning on the screen after the boot is completed normally, don't do so until
2130 // we have the application and wallpaper.
2131 if (mService.mSystemBooted && ((!haveApp && !haveKeyguard) ||
2132 (wallpaperEnabled && !haveWallpaper))) {
2133 return true;
2134 }
2135
2136 return false;
2137 }
2138
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002139 void updateWindowsForAnimator(WindowAnimator animator) {
2140 final WindowManagerPolicy policy = animator.mPolicy;
2141 final int keyguardGoingAwayFlags = animator.mKeyguardGoingAwayFlags;
2142 final boolean keyguardGoingAwayToShade =
2143 (keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_TO_SHADE) != 0;
2144 final boolean keyguardGoingAwayNoAnimation =
2145 (keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_NO_WINDOW_ANIMATIONS) != 0;
2146 final boolean keyguardGoingAwayWithWallpaper =
2147 (keyguardGoingAwayFlags & KEYGUARD_GOING_AWAY_FLAG_WITH_WALLPAPER) != 0;
2148
2149 if (animator.mKeyguardGoingAway) {
2150 for (int i = mWindows.size() - 1; i >= 0; i--) {
2151 WindowState win = mWindows.get(i);
2152 if (!policy.isKeyguardHostWindow(win.mAttrs)) {
2153 continue;
2154 }
2155 final WindowStateAnimator winAnimator = win.mWinAnimator;
2156 if (policy.isKeyguardShowingAndNotOccluded()) {
2157 if (!winAnimator.mAnimating) {
2158 if (DEBUG_KEYGUARD) Slog.d(TAG,
2159 "updateWindowsForAnimator: creating delay animation");
2160
2161 // Create a new animation to delay until keyguard is gone on its own.
2162 winAnimator.mAnimation = new AlphaAnimation(1.0f, 1.0f);
2163 winAnimator.mAnimation.setDuration(KEYGUARD_ANIM_TIMEOUT_MS);
2164 winAnimator.mAnimationIsEntrance = false;
2165 winAnimator.mAnimationStartTime = -1;
2166 winAnimator.mKeyguardGoingAwayAnimation = true;
2167 winAnimator.mKeyguardGoingAwayWithWallpaper
2168 = keyguardGoingAwayWithWallpaper;
2169 }
2170 } else {
2171 if (DEBUG_KEYGUARD) Slog.d(TAG,
2172 "updateWindowsForAnimator: StatusBar is no longer keyguard");
2173 animator.mKeyguardGoingAway = false;
2174 winAnimator.clearAnimation();
2175 }
2176 break;
2177 }
2178 }
2179
2180 animator.mForceHiding = KEYGUARD_NOT_SHOWN;
2181
2182 boolean wallpaperInUnForceHiding = false;
2183 boolean startingInUnForceHiding = false;
2184 ArrayList<WindowStateAnimator> unForceHiding = null;
2185 WindowState wallpaper = null;
Wale Ogunwale0303c572016-10-20 10:16:29 -07002186 final WallpaperController wallpaperController = mWallpaperController;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002187 for (int i = mWindows.size() - 1; i >= 0; i--) {
2188 WindowState win = mWindows.get(i);
2189 WindowStateAnimator winAnimator = win.mWinAnimator;
2190 final int flags = win.mAttrs.flags;
2191 boolean canBeForceHidden = policy.canBeForceHidden(win, win.mAttrs);
2192 boolean shouldBeForceHidden = animator.shouldForceHide(win);
2193 if (winAnimator.hasSurface()) {
2194 final boolean wasAnimating = winAnimator.mWasAnimating;
2195 final boolean nowAnimating = winAnimator.stepAnimationLocked(animator.mCurrentTime);
2196 winAnimator.mWasAnimating = nowAnimating;
2197 animator.orAnimating(nowAnimating);
2198
2199 if (DEBUG_WALLPAPER) Slog.v(TAG,
2200 win + ": wasAnimating=" + wasAnimating + ", nowAnimating=" + nowAnimating);
2201
2202 if (wasAnimating && !winAnimator.mAnimating
2203 && wallpaperController.isWallpaperTarget(win)) {
2204 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2205 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
2206 if (DEBUG_LAYOUT_REPEATS) {
2207 mService.mWindowPlacerLocked.debugLayoutRepeats(
2208 "updateWindowsAndWallpaperLocked 2", pendingLayoutChanges);
2209 }
2210 }
2211
2212 if (policy.isForceHiding(win.mAttrs)) {
2213 if (!wasAnimating && nowAnimating) {
2214 if (DEBUG_KEYGUARD || DEBUG_ANIM || DEBUG_VISIBILITY) Slog.v(TAG,
2215 "Animation started that could impact force hide: " + win);
2216 animator.mBulkUpdateParams |= SET_FORCE_HIDING_CHANGED;
2217 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
2218 if (DEBUG_LAYOUT_REPEATS) {
2219 mService.mWindowPlacerLocked.debugLayoutRepeats(
2220 "updateWindowsAndWallpaperLocked 3", pendingLayoutChanges);
2221 }
2222 mService.mFocusMayChange = true;
2223 } else if (animator.mKeyguardGoingAway && !nowAnimating) {
2224 // Timeout!!
2225 Slog.e(TAG, "Timeout waiting for animation to startup");
2226 policy.startKeyguardExitAnimation(0, 0);
2227 animator.mKeyguardGoingAway = false;
2228 }
2229 if (win.isReadyForDisplay()) {
2230 if (nowAnimating && win.mWinAnimator.mKeyguardGoingAwayAnimation) {
2231 animator.mForceHiding = KEYGUARD_ANIMATING_OUT;
2232 } else {
2233 animator.mForceHiding = win.isDrawnLw()
2234 ? KEYGUARD_SHOWN : KEYGUARD_NOT_SHOWN;
2235 }
2236 }
2237 if (DEBUG_KEYGUARD || DEBUG_VISIBILITY) Slog.v(TAG,
2238 "Force hide " + animator.forceHidingToString()
2239 + " hasSurface=" + win.mHasSurface
2240 + " policyVis=" + win.mPolicyVisibility
2241 + " destroying=" + win.mDestroying
2242 + " parentHidden=" + win.isParentWindowHidden()
2243 + " vis=" + win.mViewVisibility
2244 + " hidden=" + win.mToken.hidden
2245 + " anim=" + win.mWinAnimator.mAnimation);
2246 } else if (canBeForceHidden) {
2247 if (shouldBeForceHidden) {
2248 if (!win.hideLw(false, false)) {
2249 // Was already hidden
2250 continue;
2251 }
2252 if (DEBUG_KEYGUARD || DEBUG_VISIBILITY) Slog.v(TAG,
2253 "Now policy hidden: " + win);
2254 } else {
2255 final Animation postKeyguardExitAnimation =
2256 animator.mPostKeyguardExitAnimation;
2257 boolean applyExistingExitAnimation = postKeyguardExitAnimation != null
2258 && !postKeyguardExitAnimation.hasEnded()
2259 && !winAnimator.mKeyguardGoingAwayAnimation
2260 && win.hasDrawnLw()
2261 && !win.isChildWindow()
2262 && !win.mIsImWindow
2263 && isDefaultDisplay;
2264
2265 // If the window is already showing and we don't need to apply an existing
2266 // Keyguard exit animation, skip.
2267 if (!win.showLw(false, false) && !applyExistingExitAnimation) {
2268 continue;
2269 }
2270 final boolean visibleNow = win.isVisibleNow();
2271 if (!visibleNow) {
2272 // Couldn't really show, must showLw() again when win becomes visible.
2273 win.hideLw(false, false);
2274 continue;
2275 }
2276 if (DEBUG_KEYGUARD || DEBUG_VISIBILITY) Slog.v(TAG,
2277 "Now policy shown: " + win);
2278 if ((animator.mBulkUpdateParams & SET_FORCE_HIDING_CHANGED) != 0
2279 && !win.isChildWindow()) {
2280 if (unForceHiding == null) {
2281 unForceHiding = new ArrayList<>();
2282 }
2283 unForceHiding.add(winAnimator);
2284 if ((flags & FLAG_SHOW_WALLPAPER) != 0) {
2285 wallpaperInUnForceHiding = true;
2286 }
2287 if (win.mAttrs.type == TYPE_APPLICATION_STARTING) {
2288 startingInUnForceHiding = true;
2289 }
2290 } else if (applyExistingExitAnimation) {
2291 // We're already in the middle of an animation. Use the existing
2292 // animation to bring in this window.
2293 if (DEBUG_KEYGUARD) Slog.v(TAG,
2294 "Applying existing Keyguard exit animation to new window: win="
2295 + win);
2296
2297 final Animation a = policy.createForceHideEnterAnimation(false,
2298 keyguardGoingAwayToShade);
2299 winAnimator.setAnimation(a, postKeyguardExitAnimation.getStartTime(),
2300 STACK_CLIP_BEFORE_ANIM);
2301 winAnimator.mKeyguardGoingAwayAnimation = true;
2302 winAnimator.mKeyguardGoingAwayWithWallpaper
2303 = keyguardGoingAwayWithWallpaper;
2304 }
2305 final WindowState currentFocus = mService.mCurrentFocus;
2306 if (currentFocus == null || currentFocus.mLayer < win.mLayer) {
2307 // We are showing on top of the current
2308 // focus, so re-evaluate focus to make
2309 // sure it is correct.
2310 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG,
2311 "updateWindowsForAnimator: setting mFocusMayChange true");
2312 mService.mFocusMayChange = true;
2313 }
2314 }
2315 if ((flags & FLAG_SHOW_WALLPAPER) != 0) {
2316 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2317 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
2318 if (DEBUG_LAYOUT_REPEATS) {
2319 mService.mWindowPlacerLocked.debugLayoutRepeats(
2320 "updateWindowsAndWallpaperLocked 4", pendingLayoutChanges);
2321 }
2322 }
2323 }
2324 }
2325
2326 // If the window doesn't have a surface, the only thing we care about is the correct
2327 // policy visibility.
2328 else if (canBeForceHidden) {
2329 if (shouldBeForceHidden) {
2330 win.hideLw(false, false);
2331 } else {
2332 win.showLw(false, false);
2333 }
2334 }
2335
2336 final AppWindowToken atoken = win.mAppToken;
2337 if (winAnimator.mDrawState == READY_TO_SHOW) {
2338 if (atoken == null || atoken.allDrawn) {
2339 if (win.performShowLocked()) {
2340 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
2341 if (DEBUG_LAYOUT_REPEATS) {
2342 mService.mWindowPlacerLocked.debugLayoutRepeats(
2343 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
2344 }
2345 }
2346 }
2347 }
2348 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
2349 if (appAnimator != null && appAnimator.thumbnail != null) {
2350 if (appAnimator.thumbnailTransactionSeq != animator.mAnimTransactionSequence) {
2351 appAnimator.thumbnailTransactionSeq = animator.mAnimTransactionSequence;
2352 appAnimator.thumbnailLayer = 0;
2353 }
2354 if (appAnimator.thumbnailLayer < winAnimator.mAnimLayer) {
2355 appAnimator.thumbnailLayer = winAnimator.mAnimLayer;
2356 }
2357 }
2358 if (win.mIsWallpaper) {
2359 wallpaper = win;
2360 }
2361 } // end forall windows
2362
2363 // If we have windows that are being shown due to them no longer being force-hidden, apply
2364 // the appropriate animation to them if animations are not disabled.
2365 if (unForceHiding != null) {
2366 if (!keyguardGoingAwayNoAnimation) {
2367 boolean first = true;
2368 for (int i=unForceHiding.size()-1; i>=0; i--) {
2369 final WindowStateAnimator winAnimator = unForceHiding.get(i);
2370 final Animation a = policy.createForceHideEnterAnimation(
2371 wallpaperInUnForceHiding && !startingInUnForceHiding,
2372 keyguardGoingAwayToShade);
2373 if (a != null) {
2374 if (DEBUG_KEYGUARD) Slog.v(TAG,
2375 "Starting keyguard exit animation on window " + winAnimator.mWin);
2376 winAnimator.setAnimation(a, STACK_CLIP_BEFORE_ANIM);
2377 winAnimator.mKeyguardGoingAwayAnimation = true;
2378 winAnimator.mKeyguardGoingAwayWithWallpaper
2379 = keyguardGoingAwayWithWallpaper;
2380 if (first) {
2381 animator.mPostKeyguardExitAnimation = a;
2382 animator.mPostKeyguardExitAnimation.setStartTime(animator.mCurrentTime);
2383 first = false;
2384 }
2385 }
2386 }
2387 } else if (animator.mKeyguardGoingAway) {
2388 policy.startKeyguardExitAnimation(animator.mCurrentTime, 0 /* duration */);
2389 animator.mKeyguardGoingAway = false;
2390 }
2391
2392
2393 // Wallpaper is going away in un-force-hide motion, animate it as well.
2394 if (!wallpaperInUnForceHiding && wallpaper != null && !keyguardGoingAwayNoAnimation) {
2395 if (DEBUG_KEYGUARD) Slog.d(TAG,
2396 "updateWindowsForAnimator: wallpaper animating away");
2397 final Animation a = policy.createForceHideWallpaperExitAnimation(
2398 keyguardGoingAwayToShade);
2399 if (a != null) {
2400 wallpaper.mWinAnimator.setAnimation(a);
2401 }
2402 }
2403 }
2404
2405 if (animator.mPostKeyguardExitAnimation != null) {
2406 // We're in the midst of a keyguard exit animation.
2407 if (animator.mKeyguardGoingAway) {
2408 policy.startKeyguardExitAnimation(animator.mCurrentTime +
2409 animator.mPostKeyguardExitAnimation.getStartOffset(),
2410 animator.mPostKeyguardExitAnimation.getDuration());
2411 animator.mKeyguardGoingAway = false;
2412 }
2413 // mPostKeyguardExitAnimation might either be ended normally, cancelled, or "orphaned",
2414 // meaning that the window it was running on was removed. We check for hasEnded() for
2415 // ended normally and cancelled case, and check the time for the "orphaned" case.
2416 else if (animator.mPostKeyguardExitAnimation.hasEnded()
2417 || animator.mCurrentTime - animator.mPostKeyguardExitAnimation.getStartTime()
2418 > animator.mPostKeyguardExitAnimation.getDuration()) {
2419 // Done with the animation, reset.
2420 if (DEBUG_KEYGUARD) Slog.v(TAG, "Done with Keyguard exit animations.");
2421 animator.mPostKeyguardExitAnimation = null;
2422 }
2423 }
2424
2425 final WindowState winShowWhenLocked = (WindowState) policy.getWinShowWhenLockedLw();
2426 if (winShowWhenLocked != null) {
2427 animator.mLastShowWinWhenLocked = winShowWhenLocked;
2428 }
2429 }
2430
2431 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002432 resetAnimationBackgroundAnimator();
2433
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002434 final WindowList windows = mWindows;
2435 WindowState detachedWallpaper = null;
2436
2437 for (int i = windows.size() - 1; i >= 0; i--) {
2438 final WindowState win = windows.get(i);
2439 final WindowStateAnimator winAnimator = win.mWinAnimator;
2440 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
2441 continue;
2442 }
2443
2444 final int flags = win.mAttrs.flags;
2445
2446 // If this window is animating, make a note that we have an animating window and take
2447 // care of a request to run a detached wallpaper animation.
2448 if (winAnimator.mAnimating) {
2449 if (winAnimator.mAnimation != null) {
2450 if ((flags & FLAG_SHOW_WALLPAPER) != 0
2451 && winAnimator.mAnimation.getDetachWallpaper()) {
2452 detachedWallpaper = win;
2453 }
2454 final int color = winAnimator.mAnimation.getBackgroundColor();
2455 if (color != 0) {
2456 final TaskStack stack = win.getStack();
2457 if (stack != null) {
2458 stack.setAnimationBackground(winAnimator, color);
2459 }
2460 }
2461 }
2462 animator.setAnimating(true);
2463 }
2464
2465 // If this window's app token is running a detached wallpaper animation, make a note so
2466 // we can ensure the wallpaper is displayed behind it.
2467 final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
2468 if (appAnimator != null && appAnimator.animation != null
2469 && appAnimator.animating) {
2470 if ((flags & FLAG_SHOW_WALLPAPER) != 0
2471 && appAnimator.animation.getDetachWallpaper()) {
2472 detachedWallpaper = win;
2473 }
2474
2475 final int color = appAnimator.animation.getBackgroundColor();
2476 if (color != 0) {
2477 final TaskStack stack = win.getStack();
2478 if (stack != null) {
2479 stack.setAnimationBackground(winAnimator, color);
2480 }
2481 }
2482 }
2483 } // end forall windows
2484
2485 if (animator.mWindowDetachedWallpaper != detachedWallpaper) {
2486 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
2487 + animator.mWindowDetachedWallpaper + " to " + detachedWallpaper);
2488 animator.mWindowDetachedWallpaper = detachedWallpaper;
2489 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2490 }
2491 }
2492
2493 void prepareWindowSurfaces() {
2494 final int count = mWindows.size();
2495 for (int j = 0; j < count; j++) {
2496 mWindows.get(j).mWinAnimator.prepareSurfaceLocked(true);
2497 }
2498 }
2499
Wale Ogunwale494009b82016-10-21 09:01:38 -07002500 boolean inputMethodClientHasFocus(IInputMethodClient client) {
2501 // The focus for the client is the window immediately below where we would place the input
2502 // method window.
2503 int idx = findDesiredInputMethodWindowIndex(false);
2504 if (idx <= 0) {
2505 return false;
2506 }
2507
2508 WindowState imFocus = mWindows.get(idx - 1);
2509 if (DEBUG_INPUT_METHOD) {
2510 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2511 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2512 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
2513 }
2514
2515 if (imFocus == null) {
2516 return false;
2517 }
2518
2519 // This may be a starting window, in which case we still want to count it as okay.
2520 if (imFocus.mAttrs.type == TYPE_APPLICATION_STARTING && imFocus.mAppToken != null) {
2521 // The client has definitely started, so it really should have a window in this app
2522 // token. Let's look for it.
2523 final WindowState w = imFocus.mAppToken.getFirstNonStartingWindow();
2524 if (w != null) {
2525 if (DEBUG_INPUT_METHOD) Slog.i(TAG_WM, "Switching to real app window: " + w);
2526 imFocus = w;
2527 }
2528 }
2529
2530 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2531
2532 if (DEBUG_INPUT_METHOD) {
2533 Slog.i(TAG_WM, "IM target client: " + imeClient);
2534 if (imeClient != null) {
2535 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2536 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2537 }
2538 }
2539
2540 return imeClient != null && imeClient.asBinder() == client.asBinder();
2541 }
2542
2543 boolean hasSecureWindowOnScreen() {
2544 for (int i = mWindows.size() - 1; i >= 0; --i) {
2545 final WindowState ws = mWindows.get(i);
2546 if (ws.isOnScreen() && (ws.mAttrs.flags & FLAG_SECURE) != 0) {
2547 return true;
2548 }
2549 }
2550 return false;
2551 }
2552
2553 void updateSystemUiVisibility(int visibility, int globalDiff) {
2554 for (int i = mWindows.size() - 1; i >= 0; --i) {
2555 final WindowState ws = mWindows.get(i);
2556 try {
2557 int curValue = ws.mSystemUiVisibility;
2558 int diff = (curValue ^ visibility) & globalDiff;
2559 int newValue = (curValue & ~diff) | (visibility & diff);
2560 if (newValue != curValue) {
2561 ws.mSeq++;
2562 ws.mSystemUiVisibility = newValue;
2563 }
2564 if (newValue != curValue || ws.mAttrs.hasSystemUiListeners) {
2565 ws.mClient.dispatchSystemUiVisibilityChanged(ws.mSeq,
2566 visibility, newValue, diff);
2567 }
2568 } catch (RemoteException e) {
2569 // so sorry
2570 }
2571 }
2572 }
2573
2574 void onWindowFreezeTimeout() {
2575 Slog.w(TAG_WM, "Window freeze timeout expired.");
2576 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
2577 for (int i = mWindows.size() - 1; i >= 0; --i) {
2578 final WindowState w = mWindows.get(i);
2579 if (!w.mOrientationChanging) {
2580 continue;
2581 }
2582 w.mOrientationChanging = false;
2583 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2584 - mService.mDisplayFreezeTime);
2585 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
2586 }
2587 mService.mWindowPlacerLocked.performSurfacePlacement();
2588 }
2589
2590 void waitForAllWindowsDrawn() {
2591 final WindowManagerPolicy policy = mService.mPolicy;
2592 for (int winNdx = mWindows.size() - 1; winNdx >= 0; --winNdx) {
2593 final WindowState win = mWindows.get(winNdx);
2594 final boolean isForceHiding = policy.isForceHiding(win.mAttrs);
2595 final boolean keyguard = policy.isKeyguardHostWindow(win.mAttrs);
2596 if (win.isVisibleLw() && (win.mAppToken != null || isForceHiding || keyguard)) {
2597 win.mWinAnimator.mDrawState = DRAW_PENDING;
2598 // Force add to mResizingWindows.
2599 win.mLastContentInsets.set(-1, -1, -1, -1);
2600 mService.mWaitingForDrawn.add(win);
2601
2602 // No need to wait for the windows below Keyguard.
2603 if (isForceHiding) {
2604 return;
2605 }
2606 }
2607 }
2608 }
2609
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002610 ReadOnlyWindowList getReadOnlyWindowList() {
2611 return mWindows.getReadOnly();
2612 }
2613
2614 void getWindows(WindowList output) {
2615 output.addAll(mWindows);
2616 }
2617
2618 // TODO: Super crazy long method that should be broken down...
2619 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2620
2621 boolean focusDisplayed = false;
2622 boolean displayHasContent = false;
2623 float preferredRefreshRate = 0;
2624 int preferredModeId = 0;
2625
2626
2627 final int dw = mDisplayInfo.logicalWidth;
2628 final int dh = mDisplayInfo.logicalHeight;
2629 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2630
2631 mTmpUpdateAllDrawn.clear();
2632
2633 int repeats = 0;
2634 do {
2635 repeats++;
2636 if (repeats > 6) {
2637 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2638 clearLayoutNeeded();
2639 break;
2640 }
2641
2642 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2643 pendingLayoutChanges);
2644
2645 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
2646 adjustWallpaperWindows();
2647 }
2648
2649 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2650 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
2651 if (mService.updateOrientationFromAppTokensLocked(true, mDisplayId)) {
2652 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002653 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002654 }
2655 }
2656
2657 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2658 setLayoutNeeded();
2659 }
2660
2661 // FIRST LOOP: Perform a layout, if needed.
2662 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2663 performLayout(repeats == 1, false /* updateInputWindows */);
2664 } else {
2665 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2666 }
2667
2668 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2669 pendingLayoutChanges = 0;
2670
2671 if (isDefaultDisplay) {
2672 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
2673 for (int i = mWindows.size() - 1; i >= 0; i--) {
2674 final WindowState w = mWindows.get(i);
2675 if (w.mHasSurface) {
2676 mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow());
2677 }
2678 }
2679 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2680 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2681 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2682 }
2683 } while (pendingLayoutChanges != 0);
2684
2685 RootWindowContainer root = mService.mRoot;
2686 boolean obscured = false;
2687 boolean syswin = false;
2688 resetDimming();
2689
2690 // Only used if default window
2691 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
2692
2693 for (int i = mWindows.size() - 1; i >= 0; i--) {
2694 final WindowState w = mWindows.get(i);
2695 final Task task = w.getTask();
2696 final boolean obscuredChanged = w.mObscured != obscured;
2697
2698 // Update effect.
2699 w.mObscured = obscured;
2700 if (!obscured) {
2701 final boolean isDisplayed = w.isDisplayedLw();
2702
2703 if (isDisplayed && w.isObscuringFullscreen(mDisplayInfo)) {
2704 // This window completely covers everything behind it, so we want to leave all
2705 // of them as undimmed (for performance reasons).
2706 root.mObscuringWindow = w;
2707 obscured = true;
2708 }
2709
2710 displayHasContent |= root.handleNotObscuredLocked(w, obscured, syswin);
2711
2712 if (w.mHasSurface && isDisplayed) {
2713 final int type = w.mAttrs.type;
2714 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
2715 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
2716 syswin = true;
2717 }
2718 if (preferredRefreshRate == 0 && w.mAttrs.preferredRefreshRate != 0) {
2719 preferredRefreshRate = w.mAttrs.preferredRefreshRate;
2720 }
2721 if (preferredModeId == 0 && w.mAttrs.preferredDisplayModeId != 0) {
2722 preferredModeId = w.mAttrs.preferredDisplayModeId;
2723 }
2724 }
2725 }
2726
2727 w.applyDimLayerIfNeeded();
2728
2729 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
2730 && mWallpaperController.isWallpaperTarget(w)) {
2731 // This is the wallpaper target and its obscured state changed... make sure the
2732 // current wallpaper's visibility has been updated accordingly.
2733 mWallpaperController.updateWallpaperVisibility();
2734 }
2735
2736 w.handleWindowMovedIfNeeded();
2737
2738 final WindowStateAnimator winAnimator = w.mWinAnimator;
2739
2740 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
2741 w.mContentChanged = false;
2742
2743 // Moved from updateWindowsAndWallpaperLocked().
2744 if (w.mHasSurface) {
2745 // Take care of the window being ready to display.
2746 final boolean committed = winAnimator.commitFinishDrawingLocked();
2747 if (isDefaultDisplay && committed) {
2748 if (w.mAttrs.type == TYPE_DREAM) {
2749 // HACK: When a dream is shown, it may at that point hide the lock screen.
2750 // So we need to redo the layout to let the phone window manager make this
2751 // happen.
2752 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
2753 if (DEBUG_LAYOUT_REPEATS) {
2754 surfacePlacer.debugLayoutRepeats(
2755 "dream and commitFinishDrawingLocked true",
2756 pendingLayoutChanges);
2757 }
2758 }
2759 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
2760 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
2761 "First draw done in potential wallpaper target " + w);
2762 root.mWallpaperMayChange = true;
2763 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
2764 if (DEBUG_LAYOUT_REPEATS) {
2765 surfacePlacer.debugLayoutRepeats(
2766 "wallpaper and commitFinishDrawingLocked true",
2767 pendingLayoutChanges);
2768 }
2769 }
2770 }
2771 if (!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening()) {
2772 // Updates the shown frame before we set up the surface. This is needed
2773 // because the resizing could change the top-left position (in addition to
2774 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
2775 // position the surface.
2776 //
2777 // If an animation is being started, we can't call this method because the
2778 // animation hasn't processed its initial transformation yet, but in general
2779 // we do want to update the position if the window is animating.
2780 winAnimator.computeShownFrameLocked();
2781 }
2782 winAnimator.setSurfaceBoundariesLocked(recoveringMemory);
2783 }
2784
2785 final AppWindowToken atoken = w.mAppToken;
2786 if (atoken != null) {
2787 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
2788 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
2789 mTmpUpdateAllDrawn.add(atoken);
2790 }
2791 }
2792
2793 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
2794 && w.isDisplayedLw()) {
2795 focusDisplayed = true;
2796 }
2797
2798 w.updateResizingWindowIfNeeded();
2799 }
2800
2801 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
2802 displayHasContent,
2803 preferredRefreshRate,
2804 preferredModeId,
2805 true /* inTraversal, must call performTraversalInTrans... below */);
2806
2807 stopDimmingIfNeeded();
2808
2809 while (!mTmpUpdateAllDrawn.isEmpty()) {
2810 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2811 // See if any windows have been drawn, so they (and others associated with them)
2812 // can now be shown.
2813 atoken.updateAllDrawn(this);
2814 }
2815
2816 return focusDisplayed;
2817 }
2818
2819 void performLayout(boolean initial, boolean updateInputWindows) {
2820 if (!isLayoutNeeded()) {
2821 return;
2822 }
2823 clearLayoutNeeded();
2824
2825 final int dw = mDisplayInfo.logicalWidth;
2826 final int dh = mDisplayInfo.logicalHeight;
2827
2828 int i;
2829
2830 if (DEBUG_LAYOUT) {
2831 Slog.v(TAG, "-------------------------------------");
2832 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2833 }
2834
2835 mService.mPolicy.beginLayoutLw(isDefaultDisplay, dw, dh, mService.mRotation,
2836 getConfiguration().uiMode);
2837 if (isDefaultDisplay) {
2838 // Not needed on non-default displays.
2839 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2840 mService.mScreenRect.set(0, 0, dw, dh);
2841 }
2842
2843 mService.mPolicy.getContentRectLw(mContentRect);
2844
2845 int seq = mService.mLayoutSeq + 1;
2846 if (seq < 0) seq = 0;
2847 mService.mLayoutSeq = seq;
2848
2849 boolean behindDream = false;
2850
2851 // First perform layout of any root windows (not attached to another window).
2852 int topAttached = -1;
2853 for (i = mWindows.size() - 1; i >= 0; i--) {
2854 final WindowState win = mWindows.get(i);
2855
2856 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
2857 // wasting time and funky changes while a window is animating away.
2858 final boolean gone = (behindDream && mService.mPolicy.canBeForceHidden(win, win.mAttrs))
2859 || win.isGoneForLayoutLw();
2860
2861 if (DEBUG_LAYOUT && !win.mLayoutAttached) {
2862 Slog.v(TAG, "1ST PASS " + win + ": gone=" + gone + " mHaveFrame=" + win.mHaveFrame
2863 + " mLayoutAttached=" + win.mLayoutAttached
2864 + " screen changed=" + win.isConfigChanged());
2865 final AppWindowToken atoken = win.mAppToken;
2866 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + win.mViewVisibility
2867 + " mRelayoutCalled=" + win.mRelayoutCalled + " hidden=" + win.mToken.hidden
2868 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
2869 + " parentHidden=" + win.isParentWindowHidden());
2870 else Slog.v(TAG, " VIS: mViewVisibility=" + win.mViewVisibility
2871 + " mRelayoutCalled=" + win.mRelayoutCalled + " hidden=" + win.mToken.hidden
2872 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
2873 + " parentHidden=" + win.isParentWindowHidden());
2874 }
2875
2876 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
2877 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
2878 // since that means "perform layout as normal, just don't display").
2879 if (!gone || !win.mHaveFrame || win.mLayoutNeeded
2880 || ((win.isConfigChanged() || win.setReportResizeHints())
2881 && !win.isGoneForLayoutLw() &&
2882 ((win.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
2883 (win.mHasSurface && win.mAppToken != null &&
2884 win.mAppToken.layoutConfigChanges)))) {
2885 if (!win.mLayoutAttached) {
2886 if (initial) {
2887 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
2888 win.mContentChanged = false;
2889 }
2890 if (win.mAttrs.type == TYPE_DREAM) {
2891 // Don't layout windows behind a dream, so that if it does stuff like hide
2892 // the status bar we won't get a bad transition when it goes away.
2893 behindDream = true;
2894 }
2895 win.mLayoutNeeded = false;
2896 win.prelayout();
2897 mService.mPolicy.layoutWindowLw(win, null);
2898 win.mLayoutSeq = seq;
2899
2900 // Window frames may have changed. Update dim layer with the new bounds.
2901 final Task task = win.getTask();
2902 if (task != null) {
2903 mDimLayerController.updateDimLayer(task);
2904 }
2905
2906 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + win.mFrame
2907 + " mContainingFrame=" + win.mContainingFrame
2908 + " mDisplayFrame=" + win.mDisplayFrame);
2909 } else {
2910 if (topAttached < 0) topAttached = i;
2911 }
2912 }
2913 }
2914
2915 boolean attachedBehindDream = false;
2916
2917 // Now perform layout of attached windows, which usually depend on the position of the
2918 // window they are attached to. XXX does not deal with windows that are attached to windows
2919 // that are themselves attached.
2920 for (i = topAttached; i >= 0; i--) {
2921 final WindowState win = mWindows.get(i);
2922
2923 if (win.mLayoutAttached) {
2924 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + win + " mHaveFrame=" + win.mHaveFrame
2925 + " mViewVisibility=" + win.mViewVisibility
2926 + " mRelayoutCalled=" + win.mRelayoutCalled);
2927 // If this view is GONE, then skip it -- keep the current frame, and let the caller
2928 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
2929 // windows, since that means "perform layout as normal, just don't display").
2930 if (attachedBehindDream && mService.mPolicy.canBeForceHidden(win, win.mAttrs)) {
2931 continue;
2932 }
2933 if ((win.mViewVisibility != GONE && win.mRelayoutCalled) || !win.mHaveFrame
2934 || win.mLayoutNeeded) {
2935 if (initial) {
2936 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
2937 win.mContentChanged = false;
2938 }
2939 win.mLayoutNeeded = false;
2940 win.prelayout();
2941 mService.mPolicy.layoutWindowLw(win, win.getParentWindow());
2942 win.mLayoutSeq = seq;
2943 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + win.mFrame
2944 + " mContainingFrame=" + win.mContainingFrame
2945 + " mDisplayFrame=" + win.mDisplayFrame);
2946 }
2947 } else if (win.mAttrs.type == TYPE_DREAM) {
2948 // Don't layout windows behind a dream, so that if it does stuff like hide the
2949 // status bar we won't get a bad transition when it goes away.
2950 attachedBehindDream = behindDream;
2951 }
2952 }
2953
2954 // Window frames may have changed. Tell the input dispatcher about it.
2955 mService.mInputMonitor.layoutInputConsumers(dw, dh);
2956 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2957 if (updateInputWindows) {
2958 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2959 }
2960
2961 mService.mPolicy.finishLayoutLw();
2962 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
2963 }
2964
2965 /**
2966 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
2967 * In portrait mode, it grabs the full screenshot.
2968 *
2969 * @param width the width of the target bitmap
2970 * @param height the height of the target bitmap
2971 * @param includeFullDisplay true if the screen should not be cropped before capture
2972 * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
2973 * @param config of the output bitmap
2974 * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
2975 */
2976 Bitmap screenshotApplications(IBinder appToken, int displayId, int width, int height,
2977 boolean includeFullDisplay, float frameScale, Bitmap.Config config,
2978 boolean wallpaperOnly) {
2979 int dw = mDisplayInfo.logicalWidth;
2980 int dh = mDisplayInfo.logicalHeight;
2981 if (dw == 0 || dh == 0) {
2982 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
2983 + ": returning null. logical widthxheight=" + dw + "x" + dh);
2984 return null;
2985 }
2986
2987 Bitmap bm = null;
2988
2989 int maxLayer = 0;
2990 final Rect frame = new Rect();
2991 final Rect stackBounds = new Rect();
2992
2993 boolean screenshotReady;
2994 int minLayer;
2995 if (appToken == null && !wallpaperOnly) {
2996 screenshotReady = true;
2997 minLayer = 0;
2998 } else {
2999 screenshotReady = false;
3000 minLayer = Integer.MAX_VALUE;
3001 }
3002
3003 WindowState appWin = null;
3004
3005 boolean includeImeInScreenshot;
3006 synchronized(mService.mWindowMap) {
3007 final AppWindowToken imeTargetAppToken = mService.mInputMethodTarget != null
3008 ? mService.mInputMethodTarget.mAppToken : null;
3009 // We only include the Ime in the screenshot if the app we are screenshoting is the IME
3010 // target and isn't in multi-window mode. We don't screenshot the IME in multi-window
3011 // mode because the frame of the IME might not overlap with that of the app.
3012 // E.g. IME target app at the top in split-screen mode and the IME at the bottom
3013 // overlapping with the bottom app.
3014 includeImeInScreenshot = imeTargetAppToken != null
3015 && imeTargetAppToken.appToken != null
3016 && imeTargetAppToken.appToken.asBinder() == appToken
3017 && !mService.mInputMethodTarget.isInMultiWindowMode();
3018 }
3019
3020 final int aboveAppLayer = (mService.mPolicy.windowTypeToLayerLw(TYPE_APPLICATION) + 1)
3021 * TYPE_LAYER_MULTIPLIER + TYPE_LAYER_OFFSET;
3022
3023 synchronized(mService.mWindowMap) {
3024 // Figure out the part of the screen that is actually the app.
3025 appWin = null;
3026 for (int i = mWindows.size() - 1; i >= 0; i--) {
3027 final WindowState ws = mWindows.get(i);
3028 if (!ws.mHasSurface) {
3029 continue;
3030 }
3031 if (ws.mLayer >= aboveAppLayer) {
3032 continue;
3033 }
3034 if (wallpaperOnly && !ws.mIsWallpaper) {
3035 continue;
3036 }
3037 if (ws.mIsImWindow) {
3038 if (!includeImeInScreenshot) {
3039 continue;
3040 }
3041 } else if (ws.mIsWallpaper) {
3042 // If this is the wallpaper layer and we're only looking for the wallpaper layer
3043 // then the target window state is this one.
3044 if (wallpaperOnly) {
3045 appWin = ws;
3046 }
3047
3048 if (appWin == null) {
3049 // We have not ran across the target window yet, so it is probably behind
3050 // the wallpaper. This can happen when the keyguard is up and all windows
3051 // are moved behind the wallpaper. We don't want to include the wallpaper
3052 // layer in the screenshot as it will cover-up the layer of the target
3053 // window.
3054 continue;
3055 }
3056 // Fall through. The target window is in front of the wallpaper. For this
3057 // case we want to include the wallpaper layer in the screenshot because
3058 // the target window might have some transparent areas.
3059 } else if (appToken != null) {
3060 if (ws.mAppToken == null || ws.mAppToken.token != appToken) {
3061 // This app window is of no interest if it is not associated with the
3062 // screenshot app.
3063 continue;
3064 }
3065 appWin = ws;
3066 }
3067
3068 // Include this window.
3069
3070 final WindowStateAnimator winAnim = ws.mWinAnimator;
3071 int layer = winAnim.mSurfaceController.getLayer();
3072 if (maxLayer < layer) {
3073 maxLayer = layer;
3074 }
3075 if (minLayer > layer) {
3076 minLayer = layer;
3077 }
3078
3079 // Don't include wallpaper in bounds calculation
3080 if (!includeFullDisplay && !ws.mIsWallpaper) {
3081 final Rect wf = ws.mFrame;
3082 final Rect cr = ws.mContentInsets;
3083 int left = wf.left + cr.left;
3084 int top = wf.top + cr.top;
3085 int right = wf.right - cr.right;
3086 int bottom = wf.bottom - cr.bottom;
3087 frame.union(left, top, right, bottom);
3088 ws.getVisibleBounds(stackBounds);
3089 if (!Rect.intersects(frame, stackBounds)) {
3090 // Set frame empty if there's no intersection.
3091 frame.setEmpty();
3092 }
3093 }
3094
3095 final boolean foundTargetWs =
3096 (ws.mAppToken != null && ws.mAppToken.token == appToken)
3097 || (appWin != null && wallpaperOnly);
3098 if (foundTargetWs && ws.isDisplayedLw() && winAnim.getShown()) {
3099 screenshotReady = true;
3100 }
3101
3102 if (ws.isObscuringFullscreen(mDisplayInfo)){
3103 break;
3104 }
3105 }
3106
3107 if (appToken != null && appWin == null) {
3108 // Can't find a window to snapshot.
3109 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM,
3110 "Screenshot: Couldn't find a surface matching " + appToken);
3111 return null;
3112 }
3113
3114 if (!screenshotReady) {
3115 Slog.i(TAG_WM, "Failed to capture screenshot of " + appToken +
3116 " appWin=" + (appWin == null ? "null" : (appWin + " drawState=" +
3117 appWin.mWinAnimator.mDrawState)));
3118 return null;
3119 }
3120
3121 // Screenshot is ready to be taken. Everything from here below will continue
3122 // through the bottom of the loop and return a value. We only stay in the loop
3123 // because we don't want to release the mWindowMap lock until the screenshot is
3124 // taken.
3125
3126 if (maxLayer == 0) {
3127 if (DEBUG_SCREENSHOT) Slog.i(TAG_WM, "Screenshot of " + appToken
3128 + ": returning null maxLayer=" + maxLayer);
3129 return null;
3130 }
3131
3132 if (!includeFullDisplay) {
3133 // Constrain frame to the screen size.
3134 if (!frame.intersect(0, 0, dw, dh)) {
3135 frame.setEmpty();
3136 }
3137 } else {
3138 // Caller just wants entire display.
3139 frame.set(0, 0, dw, dh);
3140 }
3141 if (frame.isEmpty()) {
3142 return null;
3143 }
3144
3145 if (width < 0) {
3146 width = (int) (frame.width() * frameScale);
3147 }
3148 if (height < 0) {
3149 height = (int) (frame.height() * frameScale);
3150 }
3151
3152 // Tell surface flinger what part of the image to crop. Take the top
3153 // right part of the application, and crop the larger dimension to fit.
3154 Rect crop = new Rect(frame);
3155 if (width / (float) frame.width() < height / (float) frame.height()) {
3156 int cropWidth = (int)((float)width / (float)height * frame.height());
3157 crop.right = crop.left + cropWidth;
3158 } else {
3159 int cropHeight = (int)((float)height / (float)width * frame.width());
3160 crop.bottom = crop.top + cropHeight;
3161 }
3162
3163 // The screenshot API does not apply the current screen rotation.
3164 int rot = mDisplay.getRotation();
3165
3166 if (rot == ROTATION_90 || rot == ROTATION_270) {
3167 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3168 }
3169
3170 // Surfaceflinger is not aware of orientation, so convert our logical
3171 // crop to surfaceflinger's portrait orientation.
3172 convertCropForSurfaceFlinger(crop, rot, dw, dh);
3173
3174 if (DEBUG_SCREENSHOT) {
3175 Slog.i(TAG_WM, "Screenshot: " + dw + "x" + dh + " from " + minLayer + " to "
3176 + maxLayer + " appToken=" + appToken);
3177 for (int i = 0; i < mWindows.size(); i++) {
3178 final WindowState win = mWindows.get(i);
3179 final WindowSurfaceController controller = win.mWinAnimator.mSurfaceController;
3180 Slog.i(TAG_WM, win + ": " + win.mLayer
3181 + " animLayer=" + win.mWinAnimator.mAnimLayer
3182 + " surfaceLayer=" + ((controller == null)
3183 ? "null" : controller.getLayer()));
3184 }
3185 }
3186
3187 final ScreenRotationAnimation screenRotationAnimation =
3188 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3189 final boolean inRotation = screenRotationAnimation != null &&
3190 screenRotationAnimation.isAnimating();
3191 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM,
3192 "Taking screenshot while rotating");
3193
3194 // We force pending transactions to flush before taking
3195 // the screenshot by pushing an empty synchronous transaction.
3196 SurfaceControl.openTransaction();
3197 SurfaceControl.closeTransactionSync();
3198
3199 bm = SurfaceControl.screenshot(crop, width, height, minLayer, maxLayer,
3200 inRotation, rot);
3201 if (bm == null) {
3202 Slog.w(TAG_WM, "Screenshot failure taking screenshot for (" + dw + "x" + dh
3203 + ") to layer " + maxLayer);
3204 return null;
3205 }
3206 }
3207
3208 if (DEBUG_SCREENSHOT) {
3209 // TEST IF IT's ALL BLACK
3210 int[] buffer = new int[bm.getWidth() * bm.getHeight()];
3211 bm.getPixels(buffer, 0, bm.getWidth(), 0, 0, bm.getWidth(), bm.getHeight());
3212 boolean allBlack = true;
3213 final int firstColor = buffer[0];
3214 for (int i = 0; i < buffer.length; i++) {
3215 if (buffer[i] != firstColor) {
3216 allBlack = false;
3217 break;
3218 }
3219 }
3220 if (allBlack) {
3221 Slog.i(TAG_WM, "Screenshot " + appWin + " was monochrome(" +
3222 Integer.toHexString(firstColor) + ")! mSurfaceLayer=" +
3223 (appWin != null ?
3224 appWin.mWinAnimator.mSurfaceController.getLayer() : "null") +
3225 " minLayer=" + minLayer + " maxLayer=" + maxLayer);
3226 }
3227 }
3228
3229 // Create a copy of the screenshot that is immutable and backed in ashmem.
3230 // This greatly reduces the overhead of passing the bitmap between processes.
3231 Bitmap ret = bm.createAshmemBitmap(config);
3232 bm.recycle();
3233 return ret;
3234 }
3235
3236 // TODO: Can this use createRotationMatrix()?
3237 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3238 if (rot == Surface.ROTATION_90) {
3239 final int tmp = crop.top;
3240 crop.top = dw - crop.right;
3241 crop.right = crop.bottom;
3242 crop.bottom = dw - crop.left;
3243 crop.left = tmp;
3244 } else if (rot == Surface.ROTATION_180) {
3245 int tmp = crop.top;
3246 crop.top = dh - crop.bottom;
3247 crop.bottom = dh - tmp;
3248 tmp = crop.right;
3249 crop.right = dw - crop.left;
3250 crop.left = dw - tmp;
3251 } else if (rot == Surface.ROTATION_270) {
3252 final int tmp = crop.top;
3253 crop.top = crop.left;
3254 crop.left = dh - crop.bottom;
3255 crop.bottom = crop.right;
3256 crop.right = dh - tmp;
3257 }
3258 }
3259
3260 void onSeamlessRotationTimeout() {
3261 boolean layoutNeeded = false;
3262 for (int i = mWindows.size() - 1; i >= 0; i--) {
3263 final WindowState w = mWindows.get(i);
3264 if (!w.mSeamlesslyRotated) {
3265 continue;
3266 }
3267 layoutNeeded = true;
3268 w.setDisplayLayoutNeeded();
3269 mService.markForSeamlessRotation(w, false);
3270 }
3271
3272 if (layoutNeeded) {
3273 mService.mWindowPlacerLocked.performSurfacePlacement();
3274 }
3275 }
3276
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003277 static final class GetWindowOnDisplaySearchResult {
Wale Ogunwaleec731152016-09-08 20:18:57 -07003278 boolean reachedToken;
3279 WindowState foundWindow;
3280
3281 void reset() {
3282 reachedToken = false;
3283 foundWindow = null;
3284 }
3285 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003286
3287 static final class TaskForResizePointSearchResult {
3288 boolean searchDone;
3289 Task taskForResize;
3290
3291 void reset() {
3292 searchDone = false;
3293 taskForResize = null;
3294 }
3295 }
Robert Carr3b716242016-08-16 16:02:21 -07003296
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003297 /**
3298 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3299 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3300 * homogeneous children type which is currently required by sub-classes of
3301 * {@link WindowContainer} class.
3302 */
3303 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3304
3305 int size() {
3306 return mChildren.size();
3307 }
3308
3309 E get(int index) {
3310 return mChildren.get(index);
3311 }
3312
3313 @Override
3314 boolean fillsParent() {
3315 return true;
3316 }
3317
3318 @Override
3319 boolean isVisible() {
3320 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003321 }
3322 }
3323
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003324 /**
3325 * Window container class that contains all containers on this display relating to Apps.
3326 * I.e Activities.
3327 */
3328 private class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
3329
3330 void attachStack(TaskStack stack, boolean onTop) {
3331 if (stack.mStackId == HOME_STACK_ID) {
3332 if (mHomeStack != null) {
3333 throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
3334 }
3335 mHomeStack = stack;
3336 }
3337 addChild(stack, onTop);
3338 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003339 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003340
3341 void moveStack(TaskStack stack, boolean toTop) {
3342 if (StackId.isAlwaysOnTop(stack.mStackId) && !toTop) {
3343 // This stack is always-on-top silly...
3344 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + stack + " to bottom");
3345 return;
3346 }
3347
3348 if (!mChildren.contains(stack)) {
3349 Slog.wtf(TAG_WM, "moving stack that was not added: " + stack, new Throwable());
3350 }
3351 removeChild(stack);
3352 addChild(stack, toTop);
3353 }
3354
3355 private void addChild(TaskStack stack, boolean toTop) {
3356 int addIndex = toTop ? mChildren.size() : 0;
3357
3358 if (toTop
3359 && mService.isStackVisibleLocked(PINNED_STACK_ID)
3360 && stack.mStackId != PINNED_STACK_ID) {
3361 // The pinned stack is always the top most stack (always-on-top) when it is visible.
3362 // So, stack is moved just below the pinned stack.
3363 addIndex--;
3364 TaskStack topStack = mChildren.get(addIndex);
3365 if (topStack.mStackId != PINNED_STACK_ID) {
3366 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3367 }
3368 }
3369 addChild(stack, addIndex);
3370 setLayoutNeeded();
3371 }
3372
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003373 @Override
3374 int getOrientation() {
3375 if (mService.isStackVisibleLocked(DOCKED_STACK_ID)
3376 || mService.isStackVisibleLocked(FREEFORM_WORKSPACE_STACK_ID)) {
3377 // Apps and their containers are not allowed to specify an orientation while the
3378 // docked or freeform stack is visible...except for the home stack/task if the
3379 // docked stack is minimized and it actually set something.
3380 if (mHomeStack != null && mHomeStack.isVisible()
3381 && mDividerControllerLocked.isMinimizedDock()) {
3382 final int orientation = mHomeStack.getOrientation();
3383 if (orientation != SCREEN_ORIENTATION_UNSET) {
3384 return orientation;
3385 }
3386 }
3387 return SCREEN_ORIENTATION_UNSPECIFIED;
3388 }
3389
3390 final int orientation = super.getOrientation();
3391 if (orientation != SCREEN_ORIENTATION_UNSET
3392 && orientation != SCREEN_ORIENTATION_BEHIND) {
3393 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3394 "App is requesting an orientation, return " + orientation);
3395 return orientation;
3396 }
3397
3398 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
3399 "No app is requesting an orientation, return " + mService.mLastOrientation);
3400 // The next app has not been requested to be visible, so we keep the current orientation
3401 // to prevent freezing/unfreezing the display too early.
3402 return mService.mLastOrientation;
3403 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003404 }
3405
3406 /**
3407 * Window container class that contains all containers on this display that are not related to
3408 * Apps. E.g. status bar.
3409 */
3410 private static class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
3411
Robert Carr3b716242016-08-16 16:02:21 -07003412 }
Craig Mautner59c00972012-07-30 12:10:24 -07003413}