blob: 0c85b986c631453270f6c8f2703b05a80fa00d8c [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;
27import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080028import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DRAG;
29import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
30import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT;
31import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TASK_POSITIONING;
32import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Filip Gruszczynski9cac3b42015-10-30 14:20:37 -070033
Sudheer Shankadc589ac2016-11-10 15:30:17 -080034import android.app.ActivityManager;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080035import android.graphics.Rect;
Wale Ogunwalee89eeac2016-03-12 11:07:58 -080036import android.os.Debug;
Winson41275482016-10-10 15:17:45 -070037import android.os.Looper;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080038import android.os.RemoteException;
Winson41275482016-10-10 15:17:45 -070039import android.util.ArrayMap;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080040import android.util.Log;
41import android.util.Slog;
Jeff Browncc4f7db2011-08-30 20:34:48 -070042import android.view.InputChannel;
Winson41275482016-10-10 15:17:45 -070043import android.view.InputEventReceiver;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080044import android.view.KeyEvent;
45import android.view.WindowManager;
46
Winson41275482016-10-10 15:17:45 -070047import android.view.WindowManagerPolicy;
Wale Ogunwale1e129a42016-11-21 13:03:47 -080048
Selim Cinekf83e8242015-05-19 18:08:14 -070049import com.android.server.input.InputApplicationHandle;
50import com.android.server.input.InputManagerService;
51import com.android.server.input.InputWindowHandle;
52
Wale Ogunwalee89eeac2016-03-12 11:07:58 -080053import java.io.PrintWriter;
Jeff Brown9302c872011-07-13 22:51:29 -070054import java.util.Arrays;
Wale Ogunwale1e129a42016-11-21 13:03:47 -080055import java.util.function.Consumer;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080056
Jeff Browna9d131c2012-09-20 16:48:17 -070057final class InputMonitor implements InputManagerService.WindowManagerCallbacks {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080058 private final WindowManagerService mService;
Selim Cinekf83e8242015-05-19 18:08:14 -070059
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080060 // Current window with input focus for keys and other non-touch events. May be null.
61 private WindowState mInputFocus;
Selim Cinekf83e8242015-05-19 18:08:14 -070062
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080063 // When true, prevents input dispatch from proceeding until set to false again.
64 private boolean mInputDispatchFrozen;
Selim Cinekf83e8242015-05-19 18:08:14 -070065
Wale Ogunwalee89eeac2016-03-12 11:07:58 -080066 // The reason the input is currently frozen or null if the input isn't frozen.
67 private String mInputFreezeReason = null;
68
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080069 // When true, input dispatch proceeds normally. Otherwise all events are dropped.
Jeff Brownc042ee22012-05-08 13:03:42 -070070 // Initially false, so that input does not get dispatched until boot is finished at
71 // which point the ActivityManager will enable dispatching.
72 private boolean mInputDispatchEnabled;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080073
74 // When true, need to call updateInputWindowsLw().
75 private boolean mUpdateInputWindowsNeeded = true;
76
Jeff Brown9302c872011-07-13 22:51:29 -070077 // Array of window handles to provide to the input dispatcher.
78 private InputWindowHandle[] mInputWindowHandles;
79 private int mInputWindowHandleCount;
Wale Ogunwale6213caa2016-12-02 16:47:15 +000080 private boolean mAddInputConsumerHandle;
81 private boolean mAddPipInputConsumerHandle;
82 private boolean mAddWallpaperInputConsumerHandle;
83 private boolean mDisableWallpaperTouchEvents;
Wale Ogunwale1e129a42016-11-21 13:03:47 -080084 private final Rect mTmpRect = new Rect();
85 private final UpdateInputForAllWindowsConsumer mUpdateInputForAllWindowsConsumer =
86 new UpdateInputForAllWindowsConsumer();
Jeff Brown9302c872011-07-13 22:51:29 -070087
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080088 // Set to true when the first input device configuration change notification
89 // is received to indicate that the input devices are ready.
90 private final Object mInputDevicesReadyMonitor = new Object();
91 private boolean mInputDevicesReady;
92
Winson41275482016-10-10 15:17:45 -070093 /**
94 * The set of input consumer added to the window manager by name, which consumes input events
95 * for the windows below it.
96 */
97 private final ArrayMap<String, InputConsumerImpl> mInputConsumers = new ArrayMap();
98
99 private static final class EventReceiverInputConsumer extends InputConsumerImpl
100 implements WindowManagerPolicy.InputConsumer {
101 private InputMonitor mInputMonitor;
102 private final InputEventReceiver mInputEventReceiver;
103
104 EventReceiverInputConsumer(WindowManagerService service, InputMonitor monitor,
105 Looper looper, String name,
106 InputEventReceiver.Factory inputEventReceiverFactory) {
107 super(service, name, null);
108 mInputMonitor = monitor;
109 mInputEventReceiver = inputEventReceiverFactory.createInputEventReceiver(
110 mClientChannel, looper);
111 }
112
113 @Override
114 public void dismiss() {
115 synchronized (mService.mWindowMap) {
116 if (mInputMonitor.destroyInputConsumer(mWindowHandle.name)) {
117 mInputEventReceiver.dispose();
118 }
119 }
120 }
121 }
122
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800123 public InputMonitor(WindowManagerService service) {
124 mService = service;
125 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700126
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700127 private void addInputConsumer(String name, InputConsumerImpl consumer) {
Winson41275482016-10-10 15:17:45 -0700128 mInputConsumers.put(name, consumer);
129 updateInputWindowsLw(true /* force */);
130 }
131
132 boolean destroyInputConsumer(String name) {
133 if (disposeInputConsumer(mInputConsumers.remove(name))) {
134 updateInputWindowsLw(true /* force */);
135 return true;
136 }
137 return false;
138 }
139
140 private boolean disposeInputConsumer(InputConsumerImpl consumer) {
141 if (consumer != null) {
142 consumer.disposeChannelsLw();
143 return true;
144 }
145 return false;
146 }
147
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700148 InputConsumerImpl getInputConsumer(String name, int displayId) {
149 // TODO(multi-display): Allow input consumers on non-default displays?
150 return (displayId == DEFAULT_DISPLAY) ? mInputConsumers.get(name) : null;
Winson41275482016-10-10 15:17:45 -0700151 }
152
153 void layoutInputConsumers(int dw, int dh) {
154 for (int i = mInputConsumers.size() - 1; i >= 0; i--) {
155 mInputConsumers.valueAt(i).layout(dw, dh);
156 }
157 }
158
159 WindowManagerPolicy.InputConsumer createInputConsumer(Looper looper, String name,
160 InputEventReceiver.Factory inputEventReceiverFactory) {
161 if (mInputConsumers.containsKey(name)) {
162 throw new IllegalStateException("Existing input consumer found with name: " + name);
163 }
164
165 final EventReceiverInputConsumer consumer = new EventReceiverInputConsumer(mService,
166 this, looper, name, inputEventReceiverFactory);
167 addInputConsumer(name, consumer);
168 return consumer;
169 }
170
171 void createInputConsumer(String name, InputChannel inputChannel) {
172 if (mInputConsumers.containsKey(name)) {
173 throw new IllegalStateException("Existing input consumer found with name: " + name);
174 }
175
176 final InputConsumerImpl consumer = new InputConsumerImpl(mService, name, inputChannel);
177 switch (name) {
178 case INPUT_CONSUMER_WALLPAPER:
179 consumer.mWindowHandle.hasWallpaper = true;
180 break;
181 case INPUT_CONSUMER_PIP:
182 // The touchable region of the Pip input window is cropped to the bounds of the
183 // stack, and we need FLAG_NOT_TOUCH_MODAL to ensure other events fall through
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700184 consumer.mWindowHandle.layoutParamsFlags |= FLAG_NOT_TOUCH_MODAL;
Winson41275482016-10-10 15:17:45 -0700185 break;
186 }
187 addInputConsumer(name, consumer);
188 }
189
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800190 /* Notifies the window manager about a broken input channel.
Chong Zhang8e89b312015-09-09 15:09:30 -0700191 *
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800192 * Called by the InputManager.
193 */
Craig Mautner4cd0c13f2013-04-16 15:55:52 -0700194 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800195 public void notifyInputChannelBroken(InputWindowHandle inputWindowHandle) {
196 if (inputWindowHandle == null) {
197 return;
198 }
199
200 synchronized (mService.mWindowMap) {
201 WindowState windowState = (WindowState) inputWindowHandle.windowState;
Jeff Brown9302c872011-07-13 22:51:29 -0700202 if (windowState != null) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800203 Slog.i(TAG_WM, "WINDOW DIED " + windowState);
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700204 windowState.removeIfPossible();
Jeff Brown9302c872011-07-13 22:51:29 -0700205 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800206 }
207 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700208
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800209 /* Notifies the window manager about an application that is not responding.
210 * Returns a new timeout to continue waiting in nanoseconds, or 0 to abort dispatch.
Chong Zhang8e89b312015-09-09 15:09:30 -0700211 *
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800212 * Called by the InputManager.
213 */
Craig Mautner4cd0c13f2013-04-16 15:55:52 -0700214 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800215 public long notifyANR(InputApplicationHandle inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700216 InputWindowHandle inputWindowHandle, String reason) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800217 AppWindowToken appWindowToken = null;
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700218 WindowState windowState = null;
219 boolean aboveSystem = false;
Jeff Brownee172412012-06-18 12:58:03 -0700220 synchronized (mService.mWindowMap) {
Jeff Brownee172412012-06-18 12:58:03 -0700221 if (inputWindowHandle != null) {
222 windowState = (WindowState) inputWindowHandle.windowState;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800223 if (windowState != null) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800224 appWindowToken = windowState.mAppToken;
225 }
226 }
Jeff Brownee172412012-06-18 12:58:03 -0700227 if (appWindowToken == null && inputApplicationHandle != null) {
228 appWindowToken = (AppWindowToken)inputApplicationHandle.appWindowToken;
Jeff Brown9302c872011-07-13 22:51:29 -0700229 }
Jeff Brownee172412012-06-18 12:58:03 -0700230
231 if (windowState != null) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800232 Slog.i(TAG_WM, "Input event dispatching timed out "
Jeff Brownbd181bb2013-09-10 16:44:24 -0700233 + "sending to " + windowState.mAttrs.getTitle()
234 + ". Reason: " + reason);
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700235 // Figure out whether this window is layered above system windows.
236 // We need to do this here to help the activity manager know how to
237 // layer its ANR dialog.
238 int systemAlertLayer = mService.mPolicy.windowTypeToLayerLw(
239 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
240 aboveSystem = windowState.mBaseLayer > systemAlertLayer;
Jeff Brownee172412012-06-18 12:58:03 -0700241 } else if (appWindowToken != null) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800242 Slog.i(TAG_WM, "Input event dispatching timed out "
Jeff Brownbd181bb2013-09-10 16:44:24 -0700243 + "sending to application " + appWindowToken.stringName
244 + ". Reason: " + reason);
Jeff Brownee172412012-06-18 12:58:03 -0700245 } else {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800246 Slog.i(TAG_WM, "Input event dispatching timed out "
Jeff Brownbd181bb2013-09-10 16:44:24 -0700247 + ". Reason: " + reason);
Jeff Brownee172412012-06-18 12:58:03 -0700248 }
249
Jeff Brownbd181bb2013-09-10 16:44:24 -0700250 mService.saveANRStateLocked(appWindowToken, windowState, reason);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800251 }
252
253 if (appWindowToken != null && appWindowToken.appToken != null) {
254 try {
255 // Notify the activity manager about the timeout and let it decide whether
256 // to abort dispatching or keep waiting.
Jeff Brownbd181bb2013-09-10 16:44:24 -0700257 boolean abort = appWindowToken.appToken.keyDispatchingTimedOut(reason);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800258 if (! abort) {
259 // The activity manager declined to abort dispatching.
260 // Wait a bit longer and timeout again later.
261 return appWindowToken.inputDispatchingTimeoutNanos;
262 }
263 } catch (RemoteException ex) {
264 }
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700265 } else if (windowState != null) {
266 try {
267 // Notify the activity manager about the timeout and let it decide whether
268 // to abort dispatching or keep waiting.
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800269 long timeout = ActivityManager.getService().inputDispatchingTimedOut(
Jeff Brownbd181bb2013-09-10 16:44:24 -0700270 windowState.mSession.mPid, aboveSystem, reason);
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700271 if (timeout >= 0) {
272 // The activity manager declined to abort dispatching.
273 // Wait a bit longer and timeout again later.
baik.handef340d2015-04-15 10:21:05 +0900274 return timeout * 1000000L; // nanoseconds
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700275 }
276 } catch (RemoteException ex) {
277 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800278 }
279 return 0; // abort dispatching
280 }
281
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700282 void addInputWindowHandle(final InputWindowHandle windowHandle) {
Jeff Brown9302c872011-07-13 22:51:29 -0700283 if (mInputWindowHandles == null) {
284 mInputWindowHandles = new InputWindowHandle[16];
285 }
286 if (mInputWindowHandleCount >= mInputWindowHandles.length) {
287 mInputWindowHandles = Arrays.copyOf(mInputWindowHandles,
288 mInputWindowHandleCount * 2);
289 }
290 mInputWindowHandles[mInputWindowHandleCount++] = windowHandle;
291 }
292
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700293 void addInputWindowHandle(final InputWindowHandle inputWindowHandle,
Craig Mautnerc08eab82014-11-11 09:03:59 -0800294 final WindowState child, int flags, final int type, final boolean isVisible,
Wale Ogunwale053c8e42015-11-16 14:27:21 -0800295 final boolean hasFocus, final boolean hasWallpaper) {
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700296 // Add a window to our list of input windows.
297 inputWindowHandle.name = child.toString();
Wale Ogunwale053c8e42015-11-16 14:27:21 -0800298 flags = child.getTouchableRegion(inputWindowHandle.touchableRegion, flags);
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700299 inputWindowHandle.layoutParamsFlags = flags;
300 inputWindowHandle.layoutParamsType = type;
301 inputWindowHandle.dispatchingTimeoutNanos = child.getInputDispatchingTimeoutNanos();
302 inputWindowHandle.visible = isVisible;
303 inputWindowHandle.canReceiveKeys = child.canReceiveKeys();
304 inputWindowHandle.hasFocus = hasFocus;
305 inputWindowHandle.hasWallpaper = hasWallpaper;
306 inputWindowHandle.paused = child.mAppToken != null ? child.mAppToken.paused : false;
307 inputWindowHandle.layer = child.mLayer;
308 inputWindowHandle.ownerPid = child.mSession.mPid;
309 inputWindowHandle.ownerUid = child.mSession.mUid;
310 inputWindowHandle.inputFeatures = child.mAttrs.inputFeatures;
311
312 final Rect frame = child.mFrame;
313 inputWindowHandle.frameLeft = frame.left;
314 inputWindowHandle.frameTop = frame.top;
315 inputWindowHandle.frameRight = frame.right;
316 inputWindowHandle.frameBottom = frame.bottom;
317
318 if (child.mGlobalScale != 1) {
319 // If we are scaling the window, input coordinates need
320 // to be inversely scaled to map from what is on screen
321 // to what is actually being touched in the UI.
322 inputWindowHandle.scaleFactor = 1.0f/child.mGlobalScale;
323 } else {
324 inputWindowHandle.scaleFactor = 1;
325 }
326
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700327 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800328 Slog.d(TAG_WM, "addInputWindowHandle: "
Chong Zhangb15758a2015-11-17 12:12:03 -0800329 + child + ", " + inputWindowHandle);
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700330 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700331 addInputWindowHandle(inputWindowHandle);
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700332 }
333
Jeff Brown9302c872011-07-13 22:51:29 -0700334 private void clearInputWindowHandlesLw() {
335 while (mInputWindowHandleCount != 0) {
336 mInputWindowHandles[--mInputWindowHandleCount] = null;
337 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800338 }
339
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000340 void setUpdateInputWindowsNeededLw() {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800341 mUpdateInputWindowsNeeded = true;
342 }
343
344 /* Updates the cached window information provided to the input dispatcher. */
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000345 void updateInputWindowsLw(boolean force) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800346 if (!force && !mUpdateInputWindowsNeeded) {
347 return;
348 }
349 mUpdateInputWindowsNeeded = false;
350
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800351 if (false) Slog.d(TAG_WM, ">>>>>> ENTERED updateInputWindowsLw");
Jeff Brown9302c872011-07-13 22:51:29 -0700352
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800353 // Populate the input window list with information about all of the windows that
354 // could potentially receive input.
355 // As an optimization, we could try to prune the list of windows but this turns
356 // out to be difficult because only the native code knows for sure which window
357 // currently has touch focus.
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800358
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700359 // If there's a drag in flight, provide a pseudo-window to catch drag input
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800360 final boolean inDrag = (mService.mDragState != null);
361 if (inDrag) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800362 if (DEBUG_DRAG) {
363 Log.d(TAG_WM, "Inserting drag window");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800364 }
Vladislav Kaznacheev64b103a2016-08-10 11:49:08 -0700365 final InputWindowHandle dragWindowHandle = mService.mDragState.getInputWindowHandle();
Jeff Browncc4f7db2011-08-30 20:34:48 -0700366 if (dragWindowHandle != null) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700367 addInputWindowHandle(dragWindowHandle);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700368 } else {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800369 Slog.w(TAG_WM, "Drag is in progress but there is no "
Jeff Browncc4f7db2011-08-30 20:34:48 -0700370 + "drag window handle.");
371 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800372 }
373
Chong Zhang8e89b312015-09-09 15:09:30 -0700374 final boolean inPositioning = (mService.mTaskPositioner != null);
375 if (inPositioning) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800376 if (DEBUG_TASK_POSITIONING) {
377 Log.d(TAG_WM, "Inserting window handle for repositioning");
Chong Zhang8e89b312015-09-09 15:09:30 -0700378 }
379 final InputWindowHandle dragWindowHandle = mService.mTaskPositioner.mDragWindowHandle;
380 if (dragWindowHandle != null) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700381 addInputWindowHandle(dragWindowHandle);
Chong Zhang8e89b312015-09-09 15:09:30 -0700382 } else {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800383 Slog.e(TAG_WM,
Chong Zhang8e89b312015-09-09 15:09:30 -0700384 "Repositioning is in progress but there is no drag window handle.");
385 }
386 }
387
Jeff Brown83d616a2012-09-09 20:33:43 -0700388 // Add all windows on the default display.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800389 mUpdateInputForAllWindowsConsumer.updateInputWindows(inDrag);
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000390
391 if (false) Slog.d(TAG_WM, "<<<<<<< EXITED updateInputWindowsLw");
392 }
393
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800394 /* Notifies that the input device configuration has changed. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700395 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800396 public void notifyConfigurationChanged() {
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700397 // TODO(multi-display): Notify proper displays that are associated with this input device.
398 mService.sendNewConfiguration(DEFAULT_DISPLAY);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800399
400 synchronized (mInputDevicesReadyMonitor) {
401 if (!mInputDevicesReady) {
402 mInputDevicesReady = true;
403 mInputDevicesReadyMonitor.notifyAll();
404 }
405 }
406 }
407
408 /* Waits until the built-in input devices have been configured. */
409 public boolean waitForInputDevicesReady(long timeoutMillis) {
410 synchronized (mInputDevicesReadyMonitor) {
411 if (!mInputDevicesReady) {
412 try {
413 mInputDevicesReadyMonitor.wait(timeoutMillis);
414 } catch (InterruptedException ex) {
415 }
416 }
417 return mInputDevicesReady;
418 }
419 }
420
421 /* Notifies that the lid switch changed state. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700422 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800423 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) {
424 mService.mPolicy.notifyLidSwitchChanged(whenNanos, lidOpen);
425 }
Craig Mautner58458122013-09-14 14:59:50 -0700426
Michael Wright3818c922014-09-02 13:59:07 -0700427 /* Notifies that the camera lens cover state has changed. */
428 @Override
429 public void notifyCameraLensCoverSwitchChanged(long whenNanos, boolean lensCovered) {
430 mService.mPolicy.notifyCameraLensCoverSwitchChanged(whenNanos, lensCovered);
431 }
432
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800433 /* Provides an opportunity for the window manager policy to intercept early key
434 * processing as soon as the key has been read from the device. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700435 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -0700436 public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
437 return mService.mPolicy.interceptKeyBeforeQueueing(event, policyFlags);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800438 }
Jeff Brown56194eb2011-03-02 19:23:13 -0800439
Michael Wright70af00a2014-09-03 19:30:20 -0700440 /* Provides an opportunity for the window manager policy to intercept early motion event
441 * processing when the device is in a non-interactive state since these events are normally
Jeff Brown56194eb2011-03-02 19:23:13 -0800442 * dropped. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700443 @Override
Michael Wright70af00a2014-09-03 19:30:20 -0700444 public int interceptMotionBeforeQueueingNonInteractive(long whenNanos, int policyFlags) {
445 return mService.mPolicy.interceptMotionBeforeQueueingNonInteractive(
446 whenNanos, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800447 }
448
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800449 /* Provides an opportunity for the window manager policy to process a key before
450 * ordinary dispatch. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700451 @Override
Jeff Brown905805a2011-10-12 13:57:59 -0700452 public long interceptKeyBeforeDispatching(
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800453 InputWindowHandle focus, KeyEvent event, int policyFlags) {
454 WindowState windowState = focus != null ? (WindowState) focus.windowState : null;
455 return mService.mPolicy.interceptKeyBeforeDispatching(windowState, event, policyFlags);
456 }
Craig Mautner58458122013-09-14 14:59:50 -0700457
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800458 /* Provides an opportunity for the window manager policy to process a key that
459 * the application did not handle. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700460 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800461 public KeyEvent dispatchUnhandledKey(
462 InputWindowHandle focus, KeyEvent event, int policyFlags) {
463 WindowState windowState = focus != null ? (WindowState) focus.windowState : null;
464 return mService.mPolicy.dispatchUnhandledKey(windowState, event, policyFlags);
465 }
Jeff Brown4532e612012-04-05 14:27:12 -0700466
467 /* Callback to get pointer layer. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700468 @Override
Jeff Brown4532e612012-04-05 14:27:12 -0700469 public int getPointerLayer() {
470 return mService.mPolicy.windowTypeToLayerLw(WindowManager.LayoutParams.TYPE_POINTER)
471 * WindowManagerService.TYPE_LAYER_MULTIPLIER
472 + WindowManagerService.TYPE_LAYER_OFFSET;
473 }
474
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800475 /* Called when the current input focus changes.
476 * Layer assignment is assumed to be complete by the time this is called.
477 */
478 public void setInputFocusLw(WindowState newWindow, boolean updateInputWindows) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700479 if (DEBUG_FOCUS_LIGHT || DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800480 Slog.d(TAG_WM, "Input focus has changed to " + newWindow);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800481 }
482
483 if (newWindow != mInputFocus) {
484 if (newWindow != null && newWindow.canReceiveKeys()) {
485 // Displaying a window implicitly causes dispatching to be unpaused.
486 // This is to protect against bugs if someone pauses dispatching but
487 // forgets to resume.
488 newWindow.mToken.paused = false;
489 }
490
491 mInputFocus = newWindow;
492 setUpdateInputWindowsNeededLw();
493
494 if (updateInputWindows) {
495 updateInputWindowsLw(false /*force*/);
496 }
497 }
498 }
Craig Mautner58458122013-09-14 14:59:50 -0700499
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800500 public void setFocusedAppLw(AppWindowToken newApp) {
501 // Focused app has changed.
502 if (newApp == null) {
503 mService.mInputManager.setFocusedApplication(null);
504 } else {
Jeff Brown9302c872011-07-13 22:51:29 -0700505 final InputApplicationHandle handle = newApp.mInputApplicationHandle;
506 handle.name = newApp.toString();
507 handle.dispatchingTimeoutNanos = newApp.inputDispatchingTimeoutNanos;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800508
Jeff Brown9302c872011-07-13 22:51:29 -0700509 mService.mInputManager.setFocusedApplication(handle);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800510 }
511 }
Craig Mautner58458122013-09-14 14:59:50 -0700512
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800513 public void pauseDispatchingLw(WindowToken window) {
514 if (! window.paused) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700515 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800516 Slog.v(TAG_WM, "Pausing WindowToken " + window);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800517 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700518
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800519 window.paused = true;
520 updateInputWindowsLw(true /*force*/);
521 }
522 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700523
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800524 public void resumeDispatchingLw(WindowToken window) {
525 if (window.paused) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700526 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800527 Slog.v(TAG_WM, "Resuming WindowToken " + window);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800528 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700529
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800530 window.paused = false;
531 updateInputWindowsLw(true /*force*/);
532 }
533 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700534
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800535 public void freezeInputDispatchingLw() {
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800536 if (!mInputDispatchFrozen) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700537 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800538 Slog.v(TAG_WM, "Freezing input dispatching");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800539 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700540
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800541 mInputDispatchFrozen = true;
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800542
543 if (DEBUG_INPUT || true) {
544 mInputFreezeReason = Debug.getCallers(6);
545 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800546 updateInputDispatchModeLw();
547 }
548 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700549
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800550 public void thawInputDispatchingLw() {
551 if (mInputDispatchFrozen) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700552 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800553 Slog.v(TAG_WM, "Thawing input dispatching");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800554 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700555
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800556 mInputDispatchFrozen = false;
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800557 mInputFreezeReason = null;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800558 updateInputDispatchModeLw();
559 }
560 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700561
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800562 public void setEventDispatchingLw(boolean enabled) {
563 if (mInputDispatchEnabled != enabled) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700564 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800565 Slog.v(TAG_WM, "Setting event dispatching to " + enabled);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800566 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700567
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800568 mInputDispatchEnabled = enabled;
569 updateInputDispatchModeLw();
570 }
571 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700572
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800573 private void updateInputDispatchModeLw() {
574 mService.mInputManager.setInputDispatchMode(mInputDispatchEnabled, mInputDispatchFrozen);
575 }
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800576
577 void dump(PrintWriter pw, String prefix) {
578 if (mInputFreezeReason != null) {
579 pw.println(prefix + "mInputFreezeReason=" + mInputFreezeReason);
580 }
581 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800582
583 private final class UpdateInputForAllWindowsConsumer implements Consumer<WindowState> {
584
585 InputConsumerImpl navInputConsumer;
586 InputConsumerImpl pipInputConsumer;
587 InputConsumerImpl wallpaperInputConsumer;
588 Rect pipTouchableBounds;
589 boolean inDrag;
590 WallpaperController wallpaperController;
591
592 private void updateInputWindows(boolean inDrag) {
593
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800594 // TODO: multi-display
595 navInputConsumer = getInputConsumer(INPUT_CONSUMER_NAVIGATION, DEFAULT_DISPLAY);
596 pipInputConsumer = getInputConsumer(INPUT_CONSUMER_PIP, DEFAULT_DISPLAY);
597 wallpaperInputConsumer = getInputConsumer(INPUT_CONSUMER_WALLPAPER, DEFAULT_DISPLAY);
598 mAddInputConsumerHandle = navInputConsumer != null;
599 mAddPipInputConsumerHandle = pipInputConsumer != null;
600 mAddWallpaperInputConsumerHandle = wallpaperInputConsumer != null;
601 mTmpRect.setEmpty();
602 pipTouchableBounds = mAddPipInputConsumerHandle ? mTmpRect : null;
603 mDisableWallpaperTouchEvents = false;
604 this.inDrag = inDrag;
605 wallpaperController = mService.mRoot.mWallpaperController;
606
607 mService.mRoot.forAllWindows(this, true /* traverseTopToBottom */);
608 if (mAddWallpaperInputConsumerHandle) {
609 // No visible wallpaper found, add the wallpaper input consumer at the end.
610 addInputWindowHandle(wallpaperInputConsumer.mWindowHandle);
611 }
612
613 // Send windows to native code.
614 mService.mInputManager.setInputWindows(mInputWindowHandles);
Vladislav Kaznacheev2e96c632016-12-13 14:31:24 -0800615
616 clearInputWindowHandlesLw();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800617 }
618
619 @Override
620 public void accept(WindowState w) {
621 final InputChannel inputChannel = w.mInputChannel;
622 final InputWindowHandle inputWindowHandle = w.mInputWindowHandle;
623 if (inputChannel == null || inputWindowHandle == null || w.mRemoved
624 || w.isAdjustedForMinimizedDock()) {
625 // Skip this window because it cannot possibly receive input.
626 return;
627 }
628
629 if (mAddPipInputConsumerHandle
630 && w.getStackId() == PINNED_STACK_ID
631 && inputWindowHandle.layer <= pipInputConsumer.mWindowHandle.layer) {
632 // Update the bounds of the Pip input consumer to match the Pinned stack
633 w.getStack().getBounds(pipTouchableBounds);
634 pipInputConsumer.mWindowHandle.touchableRegion.set(pipTouchableBounds);
635 addInputWindowHandle(pipInputConsumer.mWindowHandle);
636 mAddPipInputConsumerHandle = false;
637 }
638
639 if (mAddInputConsumerHandle
640 && inputWindowHandle.layer <= navInputConsumer.mWindowHandle.layer) {
641 addInputWindowHandle(navInputConsumer.mWindowHandle);
642 mAddInputConsumerHandle = false;
643 }
644
645 if (mAddWallpaperInputConsumerHandle) {
646 if (w.mAttrs.type == TYPE_WALLPAPER && w.isVisibleLw()) {
647 // Add the wallpaper input consumer above the first visible wallpaper.
648 addInputWindowHandle(wallpaperInputConsumer.mWindowHandle);
649 mAddWallpaperInputConsumerHandle = false;
650 }
651 }
652
653 final int flags = w.mAttrs.flags;
654 final int privateFlags = w.mAttrs.privateFlags;
655 final int type = w.mAttrs.type;
656
657 final boolean hasFocus = w == mInputFocus;
658 final boolean isVisible = w.isVisibleLw();
659 if ((privateFlags & PRIVATE_FLAG_DISABLE_WALLPAPER_TOUCH_EVENTS) != 0) {
660 mDisableWallpaperTouchEvents = true;
661 }
662 final boolean hasWallpaper = wallpaperController.isWallpaperTarget(w)
663 && (privateFlags & PRIVATE_FLAG_KEYGUARD) == 0
664 && !mDisableWallpaperTouchEvents;
665
666 // If there's a drag in progress and 'child' is a potential drop target,
667 // make sure it's been told about the drag
668 if (inDrag && isVisible && w.getDisplayContent().isDefaultDisplay) {
669 mService.mDragState.sendDragStartedIfNeededLw(w);
670 }
671
672 addInputWindowHandle(
673 inputWindowHandle, w, flags, type, isVisible, hasFocus, hasWallpaper);
674 }
675 }
Jeff Brownea426552011-07-18 16:53:48 -0700676}