blob: 37b8deb0a24fe00e36eacd6420b20201e7a99480 [file] [log] [blame]
Dianne Hackbornf56e1022011-02-22 10:47:13 -08001/*
2 * Copyright (C) 2010 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.
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080015 */
Dianne Hackbornf56e1022011-02-22 10:47:13 -080016
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080017package com.android.server.wm;
18
Wale Ogunwale6213caa2016-12-02 16:47:15 +000019import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Andrii Kulian5406e7a2016-10-21 11:55:23 -070020import static android.view.Display.DEFAULT_DISPLAY;
Wale Ogunwale6213caa2016-12-02 16:47:15 +000021import static android.view.WindowManager.INPUT_CONSUMER_NAVIGATION;
Winson41275482016-10-10 15:17:45 -070022import static android.view.WindowManager.INPUT_CONSUMER_PIP;
23import static android.view.WindowManager.INPUT_CONSUMER_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070024import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale6213caa2016-12-02 16:47:15 +000025import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_DISABLE_WALLPAPER_TOUCH_EVENTS;
26import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale5cd907d2017-01-26 14:14:08 -080027import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
Wale Ogunwale6213caa2016-12-02 16:47:15 +000028import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080029import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DRAG;
30import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
31import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT;
32import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TASK_POSITIONING;
33import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Filip Gruszczynski9cac3b42015-10-30 14:20:37 -070034
Sudheer Shankadc589ac2016-11-10 15:30:17 -080035import android.app.ActivityManager;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080036import android.graphics.Rect;
Wale Ogunwalee89eeac2016-03-12 11:07:58 -080037import android.os.Debug;
Winson41275482016-10-10 15:17:45 -070038import android.os.Looper;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080039import android.os.RemoteException;
Winson41275482016-10-10 15:17:45 -070040import android.util.ArrayMap;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080041import android.util.Log;
42import android.util.Slog;
Jeff Browncc4f7db2011-08-30 20:34:48 -070043import android.view.InputChannel;
Winson41275482016-10-10 15:17:45 -070044import android.view.InputEventReceiver;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080045import android.view.KeyEvent;
46import android.view.WindowManager;
47
Winson41275482016-10-10 15:17:45 -070048import android.view.WindowManagerPolicy;
Wale Ogunwale1e129a42016-11-21 13:03:47 -080049
Selim Cinekf83e8242015-05-19 18:08:14 -070050import com.android.server.input.InputApplicationHandle;
51import com.android.server.input.InputManagerService;
52import com.android.server.input.InputWindowHandle;
53
Wale Ogunwalee89eeac2016-03-12 11:07:58 -080054import java.io.PrintWriter;
Jeff Brown9302c872011-07-13 22:51:29 -070055import java.util.Arrays;
Wale Ogunwale1e129a42016-11-21 13:03:47 -080056import java.util.function.Consumer;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080057
Jeff Browna9d131c2012-09-20 16:48:17 -070058final class InputMonitor implements InputManagerService.WindowManagerCallbacks {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080059 private final WindowManagerService mService;
Selim Cinekf83e8242015-05-19 18:08:14 -070060
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080061 // Current window with input focus for keys and other non-touch events. May be null.
62 private WindowState mInputFocus;
Selim Cinekf83e8242015-05-19 18:08:14 -070063
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080064 // When true, prevents input dispatch from proceeding until set to false again.
65 private boolean mInputDispatchFrozen;
Selim Cinekf83e8242015-05-19 18:08:14 -070066
Wale Ogunwalee89eeac2016-03-12 11:07:58 -080067 // The reason the input is currently frozen or null if the input isn't frozen.
68 private String mInputFreezeReason = null;
69
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080070 // When true, input dispatch proceeds normally. Otherwise all events are dropped.
Jeff Brownc042ee22012-05-08 13:03:42 -070071 // Initially false, so that input does not get dispatched until boot is finished at
72 // which point the ActivityManager will enable dispatching.
73 private boolean mInputDispatchEnabled;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080074
75 // When true, need to call updateInputWindowsLw().
76 private boolean mUpdateInputWindowsNeeded = true;
77
Jeff Brown9302c872011-07-13 22:51:29 -070078 // Array of window handles to provide to the input dispatcher.
79 private InputWindowHandle[] mInputWindowHandles;
80 private int mInputWindowHandleCount;
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -080081 private InputWindowHandle mFocusedInputWindowHandle;
82
Wale Ogunwale6213caa2016-12-02 16:47:15 +000083 private boolean mAddInputConsumerHandle;
84 private boolean mAddPipInputConsumerHandle;
85 private boolean mAddWallpaperInputConsumerHandle;
86 private boolean mDisableWallpaperTouchEvents;
Wale Ogunwale1e129a42016-11-21 13:03:47 -080087 private final Rect mTmpRect = new Rect();
88 private final UpdateInputForAllWindowsConsumer mUpdateInputForAllWindowsConsumer =
89 new UpdateInputForAllWindowsConsumer();
Jeff Brown9302c872011-07-13 22:51:29 -070090
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080091 // Set to true when the first input device configuration change notification
92 // is received to indicate that the input devices are ready.
93 private final Object mInputDevicesReadyMonitor = new Object();
94 private boolean mInputDevicesReady;
95
Winson41275482016-10-10 15:17:45 -070096 /**
97 * The set of input consumer added to the window manager by name, which consumes input events
98 * for the windows below it.
99 */
100 private final ArrayMap<String, InputConsumerImpl> mInputConsumers = new ArrayMap();
101
102 private static final class EventReceiverInputConsumer extends InputConsumerImpl
103 implements WindowManagerPolicy.InputConsumer {
104 private InputMonitor mInputMonitor;
105 private final InputEventReceiver mInputEventReceiver;
106
107 EventReceiverInputConsumer(WindowManagerService service, InputMonitor monitor,
108 Looper looper, String name,
109 InputEventReceiver.Factory inputEventReceiverFactory) {
110 super(service, name, null);
111 mInputMonitor = monitor;
112 mInputEventReceiver = inputEventReceiverFactory.createInputEventReceiver(
113 mClientChannel, looper);
114 }
115
116 @Override
117 public void dismiss() {
118 synchronized (mService.mWindowMap) {
119 if (mInputMonitor.destroyInputConsumer(mWindowHandle.name)) {
120 mInputEventReceiver.dispose();
121 }
122 }
123 }
124 }
125
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800126 public InputMonitor(WindowManagerService service) {
127 mService = service;
128 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700129
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700130 private void addInputConsumer(String name, InputConsumerImpl consumer) {
Winson41275482016-10-10 15:17:45 -0700131 mInputConsumers.put(name, consumer);
132 updateInputWindowsLw(true /* force */);
133 }
134
135 boolean destroyInputConsumer(String name) {
136 if (disposeInputConsumer(mInputConsumers.remove(name))) {
137 updateInputWindowsLw(true /* force */);
138 return true;
139 }
140 return false;
141 }
142
143 private boolean disposeInputConsumer(InputConsumerImpl consumer) {
144 if (consumer != null) {
145 consumer.disposeChannelsLw();
146 return true;
147 }
148 return false;
149 }
150
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700151 InputConsumerImpl getInputConsumer(String name, int displayId) {
152 // TODO(multi-display): Allow input consumers on non-default displays?
153 return (displayId == DEFAULT_DISPLAY) ? mInputConsumers.get(name) : null;
Winson41275482016-10-10 15:17:45 -0700154 }
155
156 void layoutInputConsumers(int dw, int dh) {
157 for (int i = mInputConsumers.size() - 1; i >= 0; i--) {
158 mInputConsumers.valueAt(i).layout(dw, dh);
159 }
160 }
161
162 WindowManagerPolicy.InputConsumer createInputConsumer(Looper looper, String name,
163 InputEventReceiver.Factory inputEventReceiverFactory) {
164 if (mInputConsumers.containsKey(name)) {
165 throw new IllegalStateException("Existing input consumer found with name: " + name);
166 }
167
168 final EventReceiverInputConsumer consumer = new EventReceiverInputConsumer(mService,
169 this, looper, name, inputEventReceiverFactory);
170 addInputConsumer(name, consumer);
171 return consumer;
172 }
173
174 void createInputConsumer(String name, InputChannel inputChannel) {
175 if (mInputConsumers.containsKey(name)) {
176 throw new IllegalStateException("Existing input consumer found with name: " + name);
177 }
178
179 final InputConsumerImpl consumer = new InputConsumerImpl(mService, name, inputChannel);
180 switch (name) {
181 case INPUT_CONSUMER_WALLPAPER:
182 consumer.mWindowHandle.hasWallpaper = true;
183 break;
184 case INPUT_CONSUMER_PIP:
185 // The touchable region of the Pip input window is cropped to the bounds of the
186 // stack, and we need FLAG_NOT_TOUCH_MODAL to ensure other events fall through
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700187 consumer.mWindowHandle.layoutParamsFlags |= FLAG_NOT_TOUCH_MODAL;
Winson41275482016-10-10 15:17:45 -0700188 break;
189 }
190 addInputConsumer(name, consumer);
191 }
192
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800193 /* Notifies the window manager about a broken input channel.
Chong Zhang8e89b312015-09-09 15:09:30 -0700194 *
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800195 * Called by the InputManager.
196 */
Craig Mautner4cd0c13f2013-04-16 15:55:52 -0700197 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800198 public void notifyInputChannelBroken(InputWindowHandle inputWindowHandle) {
199 if (inputWindowHandle == null) {
200 return;
201 }
202
203 synchronized (mService.mWindowMap) {
204 WindowState windowState = (WindowState) inputWindowHandle.windowState;
Jeff Brown9302c872011-07-13 22:51:29 -0700205 if (windowState != null) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800206 Slog.i(TAG_WM, "WINDOW DIED " + windowState);
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700207 windowState.removeIfPossible();
Jeff Brown9302c872011-07-13 22:51:29 -0700208 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800209 }
210 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700211
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800212 /* Notifies the window manager about an application that is not responding.
213 * Returns a new timeout to continue waiting in nanoseconds, or 0 to abort dispatch.
Chong Zhang8e89b312015-09-09 15:09:30 -0700214 *
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800215 * Called by the InputManager.
216 */
Craig Mautner4cd0c13f2013-04-16 15:55:52 -0700217 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800218 public long notifyANR(InputApplicationHandle inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700219 InputWindowHandle inputWindowHandle, String reason) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800220 AppWindowToken appWindowToken = null;
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700221 WindowState windowState = null;
222 boolean aboveSystem = false;
Jeff Brownee172412012-06-18 12:58:03 -0700223 synchronized (mService.mWindowMap) {
Jeff Brownee172412012-06-18 12:58:03 -0700224 if (inputWindowHandle != null) {
225 windowState = (WindowState) inputWindowHandle.windowState;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800226 if (windowState != null) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800227 appWindowToken = windowState.mAppToken;
228 }
229 }
Jeff Brownee172412012-06-18 12:58:03 -0700230 if (appWindowToken == null && inputApplicationHandle != null) {
231 appWindowToken = (AppWindowToken)inputApplicationHandle.appWindowToken;
Jeff Brown9302c872011-07-13 22:51:29 -0700232 }
Jeff Brownee172412012-06-18 12:58:03 -0700233
234 if (windowState != null) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800235 Slog.i(TAG_WM, "Input event dispatching timed out "
Jeff Brownbd181bb2013-09-10 16:44:24 -0700236 + "sending to " + windowState.mAttrs.getTitle()
237 + ". Reason: " + reason);
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700238 // Figure out whether this window is layered above system windows.
239 // We need to do this here to help the activity manager know how to
240 // layer its ANR dialog.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -0800241 int systemAlertLayer = mService.mPolicy.getWindowLayerFromTypeLw(
242 TYPE_APPLICATION_OVERLAY, windowState.mOwnerCanAddInternalSystemWindow);
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700243 aboveSystem = windowState.mBaseLayer > systemAlertLayer;
Jeff Brownee172412012-06-18 12:58:03 -0700244 } else if (appWindowToken != null) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800245 Slog.i(TAG_WM, "Input event dispatching timed out "
Jeff Brownbd181bb2013-09-10 16:44:24 -0700246 + "sending to application " + appWindowToken.stringName
247 + ". Reason: " + reason);
Jeff Brownee172412012-06-18 12:58:03 -0700248 } else {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800249 Slog.i(TAG_WM, "Input event dispatching timed out "
Jeff Brownbd181bb2013-09-10 16:44:24 -0700250 + ". Reason: " + reason);
Jeff Brownee172412012-06-18 12:58:03 -0700251 }
252
Jeff Brownbd181bb2013-09-10 16:44:24 -0700253 mService.saveANRStateLocked(appWindowToken, windowState, reason);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800254 }
255
256 if (appWindowToken != null && appWindowToken.appToken != null) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800257 // Notify the activity manager about the timeout and let it decide whether
258 // to abort dispatching or keep waiting.
259 final AppWindowContainerController controller = appWindowToken.getController();
260 final boolean abort = controller != null && controller.keyDispatchingTimedOut(reason);
261 if (!abort) {
262 // The activity manager declined to abort dispatching.
263 // Wait a bit longer and timeout again later.
264 return appWindowToken.mInputDispatchingTimeoutNanos;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800265 }
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700266 } else if (windowState != null) {
267 try {
268 // Notify the activity manager about the timeout and let it decide whether
269 // to abort dispatching or keep waiting.
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800270 long timeout = ActivityManager.getService().inputDispatchingTimedOut(
Jeff Brownbd181bb2013-09-10 16:44:24 -0700271 windowState.mSession.mPid, aboveSystem, reason);
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700272 if (timeout >= 0) {
273 // The activity manager declined to abort dispatching.
274 // Wait a bit longer and timeout again later.
baik.handef340d2015-04-15 10:21:05 +0900275 return timeout * 1000000L; // nanoseconds
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700276 }
277 } catch (RemoteException ex) {
278 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800279 }
280 return 0; // abort dispatching
281 }
282
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700283 void addInputWindowHandle(final InputWindowHandle windowHandle) {
Jeff Brown9302c872011-07-13 22:51:29 -0700284 if (mInputWindowHandles == null) {
285 mInputWindowHandles = new InputWindowHandle[16];
286 }
287 if (mInputWindowHandleCount >= mInputWindowHandles.length) {
288 mInputWindowHandles = Arrays.copyOf(mInputWindowHandles,
289 mInputWindowHandleCount * 2);
290 }
291 mInputWindowHandles[mInputWindowHandleCount++] = windowHandle;
292 }
293
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700294 void addInputWindowHandle(final InputWindowHandle inputWindowHandle,
Craig Mautnerc08eab82014-11-11 09:03:59 -0800295 final WindowState child, int flags, final int type, final boolean isVisible,
Wale Ogunwale053c8e42015-11-16 14:27:21 -0800296 final boolean hasFocus, final boolean hasWallpaper) {
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700297 // Add a window to our list of input windows.
298 inputWindowHandle.name = child.toString();
Wale Ogunwale053c8e42015-11-16 14:27:21 -0800299 flags = child.getTouchableRegion(inputWindowHandle.touchableRegion, flags);
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700300 inputWindowHandle.layoutParamsFlags = flags;
301 inputWindowHandle.layoutParamsType = type;
302 inputWindowHandle.dispatchingTimeoutNanos = child.getInputDispatchingTimeoutNanos();
303 inputWindowHandle.visible = isVisible;
304 inputWindowHandle.canReceiveKeys = child.canReceiveKeys();
305 inputWindowHandle.hasFocus = hasFocus;
306 inputWindowHandle.hasWallpaper = hasWallpaper;
307 inputWindowHandle.paused = child.mAppToken != null ? child.mAppToken.paused : false;
308 inputWindowHandle.layer = child.mLayer;
309 inputWindowHandle.ownerPid = child.mSession.mPid;
310 inputWindowHandle.ownerUid = child.mSession.mUid;
311 inputWindowHandle.inputFeatures = child.mAttrs.inputFeatures;
312
313 final Rect frame = child.mFrame;
314 inputWindowHandle.frameLeft = frame.left;
315 inputWindowHandle.frameTop = frame.top;
316 inputWindowHandle.frameRight = frame.right;
317 inputWindowHandle.frameBottom = frame.bottom;
318
319 if (child.mGlobalScale != 1) {
320 // If we are scaling the window, input coordinates need
321 // to be inversely scaled to map from what is on screen
322 // to what is actually being touched in the UI.
323 inputWindowHandle.scaleFactor = 1.0f/child.mGlobalScale;
324 } else {
325 inputWindowHandle.scaleFactor = 1;
326 }
327
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700328 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800329 Slog.d(TAG_WM, "addInputWindowHandle: "
Chong Zhangb15758a2015-11-17 12:12:03 -0800330 + child + ", " + inputWindowHandle);
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700331 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700332 addInputWindowHandle(inputWindowHandle);
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800333 if (hasFocus) {
334 mFocusedInputWindowHandle = inputWindowHandle;
335 }
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700336 }
337
Jeff Brown9302c872011-07-13 22:51:29 -0700338 private void clearInputWindowHandlesLw() {
339 while (mInputWindowHandleCount != 0) {
340 mInputWindowHandles[--mInputWindowHandleCount] = null;
341 }
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800342 mFocusedInputWindowHandle = null;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800343 }
344
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000345 void setUpdateInputWindowsNeededLw() {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800346 mUpdateInputWindowsNeeded = true;
347 }
348
349 /* Updates the cached window information provided to the input dispatcher. */
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000350 void updateInputWindowsLw(boolean force) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800351 if (!force && !mUpdateInputWindowsNeeded) {
352 return;
353 }
354 mUpdateInputWindowsNeeded = false;
355
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800356 if (false) Slog.d(TAG_WM, ">>>>>> ENTERED updateInputWindowsLw");
Jeff Brown9302c872011-07-13 22:51:29 -0700357
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800358 // Populate the input window list with information about all of the windows that
359 // could potentially receive input.
360 // As an optimization, we could try to prune the list of windows but this turns
361 // out to be difficult because only the native code knows for sure which window
362 // currently has touch focus.
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800363
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700364 // If there's a drag in flight, provide a pseudo-window to catch drag input
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800365 final boolean inDrag = (mService.mDragState != null);
366 if (inDrag) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800367 if (DEBUG_DRAG) {
368 Log.d(TAG_WM, "Inserting drag window");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800369 }
Vladislav Kaznacheev64b103a2016-08-10 11:49:08 -0700370 final InputWindowHandle dragWindowHandle = mService.mDragState.getInputWindowHandle();
Jeff Browncc4f7db2011-08-30 20:34:48 -0700371 if (dragWindowHandle != null) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700372 addInputWindowHandle(dragWindowHandle);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700373 } else {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800374 Slog.w(TAG_WM, "Drag is in progress but there is no "
Jeff Browncc4f7db2011-08-30 20:34:48 -0700375 + "drag window handle.");
376 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800377 }
378
Chong Zhang8e89b312015-09-09 15:09:30 -0700379 final boolean inPositioning = (mService.mTaskPositioner != null);
380 if (inPositioning) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800381 if (DEBUG_TASK_POSITIONING) {
382 Log.d(TAG_WM, "Inserting window handle for repositioning");
Chong Zhang8e89b312015-09-09 15:09:30 -0700383 }
384 final InputWindowHandle dragWindowHandle = mService.mTaskPositioner.mDragWindowHandle;
385 if (dragWindowHandle != null) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700386 addInputWindowHandle(dragWindowHandle);
Chong Zhang8e89b312015-09-09 15:09:30 -0700387 } else {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800388 Slog.e(TAG_WM,
Chong Zhang8e89b312015-09-09 15:09:30 -0700389 "Repositioning is in progress but there is no drag window handle.");
390 }
391 }
392
Jeff Brown83d616a2012-09-09 20:33:43 -0700393 // Add all windows on the default display.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800394 mUpdateInputForAllWindowsConsumer.updateInputWindows(inDrag);
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000395
396 if (false) Slog.d(TAG_WM, "<<<<<<< EXITED updateInputWindowsLw");
397 }
398
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800399 /* Notifies that the input device configuration has changed. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700400 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800401 public void notifyConfigurationChanged() {
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700402 // TODO(multi-display): Notify proper displays that are associated with this input device.
403 mService.sendNewConfiguration(DEFAULT_DISPLAY);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800404
405 synchronized (mInputDevicesReadyMonitor) {
406 if (!mInputDevicesReady) {
407 mInputDevicesReady = true;
408 mInputDevicesReadyMonitor.notifyAll();
409 }
410 }
411 }
412
413 /* Waits until the built-in input devices have been configured. */
414 public boolean waitForInputDevicesReady(long timeoutMillis) {
415 synchronized (mInputDevicesReadyMonitor) {
416 if (!mInputDevicesReady) {
417 try {
418 mInputDevicesReadyMonitor.wait(timeoutMillis);
419 } catch (InterruptedException ex) {
420 }
421 }
422 return mInputDevicesReady;
423 }
424 }
425
426 /* Notifies that the lid switch changed state. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700427 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800428 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) {
429 mService.mPolicy.notifyLidSwitchChanged(whenNanos, lidOpen);
430 }
Craig Mautner58458122013-09-14 14:59:50 -0700431
Michael Wright3818c922014-09-02 13:59:07 -0700432 /* Notifies that the camera lens cover state has changed. */
433 @Override
434 public void notifyCameraLensCoverSwitchChanged(long whenNanos, boolean lensCovered) {
435 mService.mPolicy.notifyCameraLensCoverSwitchChanged(whenNanos, lensCovered);
436 }
437
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800438 /* Provides an opportunity for the window manager policy to intercept early key
439 * processing as soon as the key has been read from the device. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700440 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -0700441 public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
442 return mService.mPolicy.interceptKeyBeforeQueueing(event, policyFlags);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800443 }
Jeff Brown56194eb2011-03-02 19:23:13 -0800444
Michael Wright70af00a2014-09-03 19:30:20 -0700445 /* Provides an opportunity for the window manager policy to intercept early motion event
446 * processing when the device is in a non-interactive state since these events are normally
Jeff Brown56194eb2011-03-02 19:23:13 -0800447 * dropped. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700448 @Override
Michael Wright70af00a2014-09-03 19:30:20 -0700449 public int interceptMotionBeforeQueueingNonInteractive(long whenNanos, int policyFlags) {
450 return mService.mPolicy.interceptMotionBeforeQueueingNonInteractive(
451 whenNanos, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800452 }
453
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800454 /* Provides an opportunity for the window manager policy to process a key before
455 * ordinary dispatch. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700456 @Override
Jeff Brown905805a2011-10-12 13:57:59 -0700457 public long interceptKeyBeforeDispatching(
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800458 InputWindowHandle focus, KeyEvent event, int policyFlags) {
459 WindowState windowState = focus != null ? (WindowState) focus.windowState : null;
460 return mService.mPolicy.interceptKeyBeforeDispatching(windowState, event, policyFlags);
461 }
Craig Mautner58458122013-09-14 14:59:50 -0700462
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800463 /* Provides an opportunity for the window manager policy to process a key that
464 * the application did not handle. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700465 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800466 public KeyEvent dispatchUnhandledKey(
467 InputWindowHandle focus, KeyEvent event, int policyFlags) {
468 WindowState windowState = focus != null ? (WindowState) focus.windowState : null;
469 return mService.mPolicy.dispatchUnhandledKey(windowState, event, policyFlags);
470 }
Jeff Brown4532e612012-04-05 14:27:12 -0700471
472 /* Callback to get pointer layer. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700473 @Override
Jeff Brown4532e612012-04-05 14:27:12 -0700474 public int getPointerLayer() {
Wale Ogunwale5cd907d2017-01-26 14:14:08 -0800475 return mService.mPolicy.getWindowLayerFromTypeLw(WindowManager.LayoutParams.TYPE_POINTER)
Jeff Brown4532e612012-04-05 14:27:12 -0700476 * WindowManagerService.TYPE_LAYER_MULTIPLIER
477 + WindowManagerService.TYPE_LAYER_OFFSET;
478 }
479
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800480 /* Called when the current input focus changes.
481 * Layer assignment is assumed to be complete by the time this is called.
482 */
483 public void setInputFocusLw(WindowState newWindow, boolean updateInputWindows) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700484 if (DEBUG_FOCUS_LIGHT || DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800485 Slog.d(TAG_WM, "Input focus has changed to " + newWindow);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800486 }
487
488 if (newWindow != mInputFocus) {
489 if (newWindow != null && newWindow.canReceiveKeys()) {
490 // Displaying a window implicitly causes dispatching to be unpaused.
491 // This is to protect against bugs if someone pauses dispatching but
492 // forgets to resume.
493 newWindow.mToken.paused = false;
494 }
495
496 mInputFocus = newWindow;
497 setUpdateInputWindowsNeededLw();
498
499 if (updateInputWindows) {
500 updateInputWindowsLw(false /*force*/);
501 }
502 }
503 }
Craig Mautner58458122013-09-14 14:59:50 -0700504
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800505 public void setFocusedAppLw(AppWindowToken newApp) {
506 // Focused app has changed.
507 if (newApp == null) {
508 mService.mInputManager.setFocusedApplication(null);
509 } else {
Jeff Brown9302c872011-07-13 22:51:29 -0700510 final InputApplicationHandle handle = newApp.mInputApplicationHandle;
511 handle.name = newApp.toString();
Wale Ogunwale72919d22016-12-08 18:58:50 -0800512 handle.dispatchingTimeoutNanos = newApp.mInputDispatchingTimeoutNanos;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800513
Jeff Brown9302c872011-07-13 22:51:29 -0700514 mService.mInputManager.setFocusedApplication(handle);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800515 }
516 }
Craig Mautner58458122013-09-14 14:59:50 -0700517
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800518 public void pauseDispatchingLw(WindowToken window) {
519 if (! window.paused) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700520 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800521 Slog.v(TAG_WM, "Pausing WindowToken " + window);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800522 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700523
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800524 window.paused = true;
525 updateInputWindowsLw(true /*force*/);
526 }
527 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700528
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800529 public void resumeDispatchingLw(WindowToken window) {
530 if (window.paused) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700531 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800532 Slog.v(TAG_WM, "Resuming WindowToken " + window);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800533 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700534
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800535 window.paused = false;
536 updateInputWindowsLw(true /*force*/);
537 }
538 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700539
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800540 public void freezeInputDispatchingLw() {
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800541 if (!mInputDispatchFrozen) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700542 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800543 Slog.v(TAG_WM, "Freezing input dispatching");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800544 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700545
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800546 mInputDispatchFrozen = true;
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800547
548 if (DEBUG_INPUT || true) {
549 mInputFreezeReason = Debug.getCallers(6);
550 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800551 updateInputDispatchModeLw();
552 }
553 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700554
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800555 public void thawInputDispatchingLw() {
556 if (mInputDispatchFrozen) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700557 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800558 Slog.v(TAG_WM, "Thawing input dispatching");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800559 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700560
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800561 mInputDispatchFrozen = false;
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800562 mInputFreezeReason = null;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800563 updateInputDispatchModeLw();
564 }
565 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700566
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800567 public void setEventDispatchingLw(boolean enabled) {
568 if (mInputDispatchEnabled != enabled) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700569 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800570 Slog.v(TAG_WM, "Setting event dispatching to " + enabled);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800571 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700572
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800573 mInputDispatchEnabled = enabled;
574 updateInputDispatchModeLw();
575 }
576 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700577
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800578 private void updateInputDispatchModeLw() {
579 mService.mInputManager.setInputDispatchMode(mInputDispatchEnabled, mInputDispatchFrozen);
580 }
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800581
582 void dump(PrintWriter pw, String prefix) {
583 if (mInputFreezeReason != null) {
584 pw.println(prefix + "mInputFreezeReason=" + mInputFreezeReason);
585 }
586 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800587
588 private final class UpdateInputForAllWindowsConsumer implements Consumer<WindowState> {
589
590 InputConsumerImpl navInputConsumer;
591 InputConsumerImpl pipInputConsumer;
592 InputConsumerImpl wallpaperInputConsumer;
593 Rect pipTouchableBounds;
594 boolean inDrag;
595 WallpaperController wallpaperController;
596
597 private void updateInputWindows(boolean inDrag) {
598
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800599 // TODO: multi-display
600 navInputConsumer = getInputConsumer(INPUT_CONSUMER_NAVIGATION, DEFAULT_DISPLAY);
601 pipInputConsumer = getInputConsumer(INPUT_CONSUMER_PIP, DEFAULT_DISPLAY);
602 wallpaperInputConsumer = getInputConsumer(INPUT_CONSUMER_WALLPAPER, DEFAULT_DISPLAY);
603 mAddInputConsumerHandle = navInputConsumer != null;
604 mAddPipInputConsumerHandle = pipInputConsumer != null;
605 mAddWallpaperInputConsumerHandle = wallpaperInputConsumer != null;
606 mTmpRect.setEmpty();
607 pipTouchableBounds = mAddPipInputConsumerHandle ? mTmpRect : null;
608 mDisableWallpaperTouchEvents = false;
609 this.inDrag = inDrag;
610 wallpaperController = mService.mRoot.mWallpaperController;
611
612 mService.mRoot.forAllWindows(this, true /* traverseTopToBottom */);
613 if (mAddWallpaperInputConsumerHandle) {
614 // No visible wallpaper found, add the wallpaper input consumer at the end.
615 addInputWindowHandle(wallpaperInputConsumer.mWindowHandle);
616 }
617
618 // Send windows to native code.
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800619 mService.mInputManager.setInputWindows(mInputWindowHandles, mFocusedInputWindowHandle);
Vladislav Kaznacheev2e96c632016-12-13 14:31:24 -0800620
621 clearInputWindowHandlesLw();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800622 }
623
624 @Override
625 public void accept(WindowState w) {
626 final InputChannel inputChannel = w.mInputChannel;
627 final InputWindowHandle inputWindowHandle = w.mInputWindowHandle;
628 if (inputChannel == null || inputWindowHandle == null || w.mRemoved
Matthew Nge15352e2016-12-20 15:36:29 -0800629 || w.canReceiveTouchInput()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800630 // Skip this window because it cannot possibly receive input.
631 return;
632 }
633
634 if (mAddPipInputConsumerHandle
635 && w.getStackId() == PINNED_STACK_ID
636 && inputWindowHandle.layer <= pipInputConsumer.mWindowHandle.layer) {
637 // Update the bounds of the Pip input consumer to match the Pinned stack
638 w.getStack().getBounds(pipTouchableBounds);
639 pipInputConsumer.mWindowHandle.touchableRegion.set(pipTouchableBounds);
640 addInputWindowHandle(pipInputConsumer.mWindowHandle);
641 mAddPipInputConsumerHandle = false;
642 }
643
644 if (mAddInputConsumerHandle
645 && inputWindowHandle.layer <= navInputConsumer.mWindowHandle.layer) {
646 addInputWindowHandle(navInputConsumer.mWindowHandle);
647 mAddInputConsumerHandle = false;
648 }
649
650 if (mAddWallpaperInputConsumerHandle) {
651 if (w.mAttrs.type == TYPE_WALLPAPER && w.isVisibleLw()) {
652 // Add the wallpaper input consumer above the first visible wallpaper.
653 addInputWindowHandle(wallpaperInputConsumer.mWindowHandle);
654 mAddWallpaperInputConsumerHandle = false;
655 }
656 }
657
658 final int flags = w.mAttrs.flags;
659 final int privateFlags = w.mAttrs.privateFlags;
660 final int type = w.mAttrs.type;
661
662 final boolean hasFocus = w == mInputFocus;
663 final boolean isVisible = w.isVisibleLw();
664 if ((privateFlags & PRIVATE_FLAG_DISABLE_WALLPAPER_TOUCH_EVENTS) != 0) {
665 mDisableWallpaperTouchEvents = true;
666 }
667 final boolean hasWallpaper = wallpaperController.isWallpaperTarget(w)
668 && (privateFlags & PRIVATE_FLAG_KEYGUARD) == 0
669 && !mDisableWallpaperTouchEvents;
670
671 // If there's a drag in progress and 'child' is a potential drop target,
672 // make sure it's been told about the drag
673 if (inDrag && isVisible && w.getDisplayContent().isDefaultDisplay) {
674 mService.mDragState.sendDragStartedIfNeededLw(w);
675 }
676
677 addInputWindowHandle(
678 inputWindowHandle, w, flags, type, isVisible, hasFocus, hasWallpaper);
679 }
680 }
Jeff Brownea426552011-07-18 16:53:48 -0700681}