blob: 5057f632461cb7eac952a4a044dec6ba5473e24c [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;
Winson Chung853c99a2017-03-21 22:16:42 -070056import java.util.Set;
Wale Ogunwale1e129a42016-11-21 13:03:47 -080057import java.util.function.Consumer;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080058
Jeff Browna9d131c2012-09-20 16:48:17 -070059final class InputMonitor implements InputManagerService.WindowManagerCallbacks {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080060 private final WindowManagerService mService;
Selim Cinekf83e8242015-05-19 18:08:14 -070061
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080062 // Current window with input focus for keys and other non-touch events. May be null.
63 private WindowState mInputFocus;
Selim Cinekf83e8242015-05-19 18:08:14 -070064
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080065 // When true, prevents input dispatch from proceeding until set to false again.
66 private boolean mInputDispatchFrozen;
Selim Cinekf83e8242015-05-19 18:08:14 -070067
Wale Ogunwalee89eeac2016-03-12 11:07:58 -080068 // The reason the input is currently frozen or null if the input isn't frozen.
69 private String mInputFreezeReason = null;
70
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080071 // When true, input dispatch proceeds normally. Otherwise all events are dropped.
Jeff Brownc042ee22012-05-08 13:03:42 -070072 // Initially false, so that input does not get dispatched until boot is finished at
73 // which point the ActivityManager will enable dispatching.
74 private boolean mInputDispatchEnabled;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080075
76 // When true, need to call updateInputWindowsLw().
77 private boolean mUpdateInputWindowsNeeded = true;
78
Jeff Brown9302c872011-07-13 22:51:29 -070079 // Array of window handles to provide to the input dispatcher.
80 private InputWindowHandle[] mInputWindowHandles;
81 private int mInputWindowHandleCount;
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -080082 private InputWindowHandle mFocusedInputWindowHandle;
83
Wale Ogunwale6213caa2016-12-02 16:47:15 +000084 private boolean mAddInputConsumerHandle;
85 private boolean mAddPipInputConsumerHandle;
86 private boolean mAddWallpaperInputConsumerHandle;
87 private boolean mDisableWallpaperTouchEvents;
Wale Ogunwale1e129a42016-11-21 13:03:47 -080088 private final Rect mTmpRect = new Rect();
89 private final UpdateInputForAllWindowsConsumer mUpdateInputForAllWindowsConsumer =
90 new UpdateInputForAllWindowsConsumer();
Jeff Brown9302c872011-07-13 22:51:29 -070091
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080092 // Set to true when the first input device configuration change notification
93 // is received to indicate that the input devices are ready.
94 private final Object mInputDevicesReadyMonitor = new Object();
95 private boolean mInputDevicesReady;
96
Winson41275482016-10-10 15:17:45 -070097 /**
98 * The set of input consumer added to the window manager by name, which consumes input events
99 * for the windows below it.
100 */
101 private final ArrayMap<String, InputConsumerImpl> mInputConsumers = new ArrayMap();
102
103 private static final class EventReceiverInputConsumer extends InputConsumerImpl
104 implements WindowManagerPolicy.InputConsumer {
105 private InputMonitor mInputMonitor;
106 private final InputEventReceiver mInputEventReceiver;
107
108 EventReceiverInputConsumer(WindowManagerService service, InputMonitor monitor,
109 Looper looper, String name,
110 InputEventReceiver.Factory inputEventReceiverFactory) {
111 super(service, name, null);
112 mInputMonitor = monitor;
113 mInputEventReceiver = inputEventReceiverFactory.createInputEventReceiver(
114 mClientChannel, looper);
115 }
116
117 @Override
118 public void dismiss() {
119 synchronized (mService.mWindowMap) {
120 if (mInputMonitor.destroyInputConsumer(mWindowHandle.name)) {
121 mInputEventReceiver.dispose();
122 }
123 }
124 }
125 }
126
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800127 public InputMonitor(WindowManagerService service) {
128 mService = service;
129 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700130
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700131 private void addInputConsumer(String name, InputConsumerImpl consumer) {
Winson41275482016-10-10 15:17:45 -0700132 mInputConsumers.put(name, consumer);
133 updateInputWindowsLw(true /* force */);
134 }
135
136 boolean destroyInputConsumer(String name) {
137 if (disposeInputConsumer(mInputConsumers.remove(name))) {
138 updateInputWindowsLw(true /* force */);
139 return true;
140 }
141 return false;
142 }
143
144 private boolean disposeInputConsumer(InputConsumerImpl consumer) {
145 if (consumer != null) {
146 consumer.disposeChannelsLw();
147 return true;
148 }
149 return false;
150 }
151
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700152 InputConsumerImpl getInputConsumer(String name, int displayId) {
153 // TODO(multi-display): Allow input consumers on non-default displays?
154 return (displayId == DEFAULT_DISPLAY) ? mInputConsumers.get(name) : null;
Winson41275482016-10-10 15:17:45 -0700155 }
156
157 void layoutInputConsumers(int dw, int dh) {
158 for (int i = mInputConsumers.size() - 1; i >= 0; i--) {
159 mInputConsumers.valueAt(i).layout(dw, dh);
160 }
161 }
162
163 WindowManagerPolicy.InputConsumer createInputConsumer(Looper looper, String name,
164 InputEventReceiver.Factory inputEventReceiverFactory) {
165 if (mInputConsumers.containsKey(name)) {
166 throw new IllegalStateException("Existing input consumer found with name: " + name);
167 }
168
169 final EventReceiverInputConsumer consumer = new EventReceiverInputConsumer(mService,
170 this, looper, name, inputEventReceiverFactory);
171 addInputConsumer(name, consumer);
172 return consumer;
173 }
174
175 void createInputConsumer(String name, InputChannel inputChannel) {
176 if (mInputConsumers.containsKey(name)) {
177 throw new IllegalStateException("Existing input consumer found with name: " + name);
178 }
179
180 final InputConsumerImpl consumer = new InputConsumerImpl(mService, name, inputChannel);
181 switch (name) {
182 case INPUT_CONSUMER_WALLPAPER:
183 consumer.mWindowHandle.hasWallpaper = true;
184 break;
185 case INPUT_CONSUMER_PIP:
186 // The touchable region of the Pip input window is cropped to the bounds of the
187 // stack, and we need FLAG_NOT_TOUCH_MODAL to ensure other events fall through
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700188 consumer.mWindowHandle.layoutParamsFlags |= FLAG_NOT_TOUCH_MODAL;
Winson41275482016-10-10 15:17:45 -0700189 break;
190 }
191 addInputConsumer(name, consumer);
192 }
193
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800194 /* Notifies the window manager about a broken input channel.
Chong Zhang8e89b312015-09-09 15:09:30 -0700195 *
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800196 * Called by the InputManager.
197 */
Craig Mautner4cd0c13f2013-04-16 15:55:52 -0700198 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800199 public void notifyInputChannelBroken(InputWindowHandle inputWindowHandle) {
200 if (inputWindowHandle == null) {
201 return;
202 }
203
204 synchronized (mService.mWindowMap) {
205 WindowState windowState = (WindowState) inputWindowHandle.windowState;
Jeff Brown9302c872011-07-13 22:51:29 -0700206 if (windowState != null) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800207 Slog.i(TAG_WM, "WINDOW DIED " + windowState);
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700208 windowState.removeIfPossible();
Jeff Brown9302c872011-07-13 22:51:29 -0700209 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800210 }
211 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700212
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800213 /* Notifies the window manager about an application that is not responding.
214 * Returns a new timeout to continue waiting in nanoseconds, or 0 to abort dispatch.
Chong Zhang8e89b312015-09-09 15:09:30 -0700215 *
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800216 * Called by the InputManager.
217 */
Craig Mautner4cd0c13f2013-04-16 15:55:52 -0700218 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800219 public long notifyANR(InputApplicationHandle inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700220 InputWindowHandle inputWindowHandle, String reason) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800221 AppWindowToken appWindowToken = null;
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700222 WindowState windowState = null;
223 boolean aboveSystem = false;
Jeff Brownee172412012-06-18 12:58:03 -0700224 synchronized (mService.mWindowMap) {
Jeff Brownee172412012-06-18 12:58:03 -0700225 if (inputWindowHandle != null) {
226 windowState = (WindowState) inputWindowHandle.windowState;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800227 if (windowState != null) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800228 appWindowToken = windowState.mAppToken;
229 }
230 }
Jeff Brownee172412012-06-18 12:58:03 -0700231 if (appWindowToken == null && inputApplicationHandle != null) {
232 appWindowToken = (AppWindowToken)inputApplicationHandle.appWindowToken;
Jeff Brown9302c872011-07-13 22:51:29 -0700233 }
Jeff Brownee172412012-06-18 12:58:03 -0700234
235 if (windowState != null) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800236 Slog.i(TAG_WM, "Input event dispatching timed out "
Jeff Brownbd181bb2013-09-10 16:44:24 -0700237 + "sending to " + windowState.mAttrs.getTitle()
238 + ". Reason: " + reason);
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700239 // Figure out whether this window is layered above system windows.
240 // We need to do this here to help the activity manager know how to
241 // layer its ANR dialog.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -0800242 int systemAlertLayer = mService.mPolicy.getWindowLayerFromTypeLw(
243 TYPE_APPLICATION_OVERLAY, windowState.mOwnerCanAddInternalSystemWindow);
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700244 aboveSystem = windowState.mBaseLayer > systemAlertLayer;
Jeff Brownee172412012-06-18 12:58:03 -0700245 } else if (appWindowToken != null) {
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 + "sending to application " + appWindowToken.stringName
248 + ". Reason: " + reason);
Jeff Brownee172412012-06-18 12:58:03 -0700249 } else {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800250 Slog.i(TAG_WM, "Input event dispatching timed out "
Jeff Brownbd181bb2013-09-10 16:44:24 -0700251 + ". Reason: " + reason);
Jeff Brownee172412012-06-18 12:58:03 -0700252 }
253
Jeff Brownbd181bb2013-09-10 16:44:24 -0700254 mService.saveANRStateLocked(appWindowToken, windowState, reason);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800255 }
256
Wale Ogunwalef1285912017-06-09 15:20:29 -0700257 // All the calls below need to happen without the WM lock held since they call into AM.
258 mService.mAmInternal.saveANRState(reason);
259
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800260 if (appWindowToken != null && appWindowToken.appToken != null) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800261 // Notify the activity manager about the timeout and let it decide whether
262 // to abort dispatching or keep waiting.
263 final AppWindowContainerController controller = appWindowToken.getController();
Wale Ogunwale7402ddf2017-03-29 12:58:24 -0700264 final boolean abort = controller != null
Brian Carlstrom7b0f2e82017-03-31 00:24:18 -0700265 && controller.keyDispatchingTimedOut(reason,
266 (windowState != null) ? windowState.mSession.mPid : -1);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800267 if (!abort) {
268 // The activity manager declined to abort dispatching.
269 // Wait a bit longer and timeout again later.
270 return appWindowToken.mInputDispatchingTimeoutNanos;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800271 }
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700272 } else if (windowState != null) {
273 try {
274 // Notify the activity manager about the timeout and let it decide whether
275 // to abort dispatching or keep waiting.
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800276 long timeout = ActivityManager.getService().inputDispatchingTimedOut(
Jeff Brownbd181bb2013-09-10 16:44:24 -0700277 windowState.mSession.mPid, aboveSystem, reason);
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700278 if (timeout >= 0) {
279 // The activity manager declined to abort dispatching.
280 // Wait a bit longer and timeout again later.
baik.handef340d2015-04-15 10:21:05 +0900281 return timeout * 1000000L; // nanoseconds
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700282 }
283 } catch (RemoteException ex) {
284 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800285 }
286 return 0; // abort dispatching
287 }
288
Wale Ogunwale7402ddf2017-03-29 12:58:24 -0700289 private void addInputWindowHandle(final InputWindowHandle windowHandle) {
Jeff Brown9302c872011-07-13 22:51:29 -0700290 if (mInputWindowHandles == null) {
291 mInputWindowHandles = new InputWindowHandle[16];
292 }
293 if (mInputWindowHandleCount >= mInputWindowHandles.length) {
294 mInputWindowHandles = Arrays.copyOf(mInputWindowHandles,
295 mInputWindowHandleCount * 2);
296 }
297 mInputWindowHandles[mInputWindowHandleCount++] = windowHandle;
298 }
299
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700300 void addInputWindowHandle(final InputWindowHandle inputWindowHandle,
Craig Mautnerc08eab82014-11-11 09:03:59 -0800301 final WindowState child, int flags, final int type, final boolean isVisible,
Wale Ogunwale053c8e42015-11-16 14:27:21 -0800302 final boolean hasFocus, final boolean hasWallpaper) {
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700303 // Add a window to our list of input windows.
304 inputWindowHandle.name = child.toString();
Wale Ogunwale053c8e42015-11-16 14:27:21 -0800305 flags = child.getTouchableRegion(inputWindowHandle.touchableRegion, flags);
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700306 inputWindowHandle.layoutParamsFlags = flags;
307 inputWindowHandle.layoutParamsType = type;
308 inputWindowHandle.dispatchingTimeoutNanos = child.getInputDispatchingTimeoutNanos();
309 inputWindowHandle.visible = isVisible;
310 inputWindowHandle.canReceiveKeys = child.canReceiveKeys();
311 inputWindowHandle.hasFocus = hasFocus;
312 inputWindowHandle.hasWallpaper = hasWallpaper;
313 inputWindowHandle.paused = child.mAppToken != null ? child.mAppToken.paused : false;
314 inputWindowHandle.layer = child.mLayer;
315 inputWindowHandle.ownerPid = child.mSession.mPid;
316 inputWindowHandle.ownerUid = child.mSession.mUid;
317 inputWindowHandle.inputFeatures = child.mAttrs.inputFeatures;
318
319 final Rect frame = child.mFrame;
320 inputWindowHandle.frameLeft = frame.left;
321 inputWindowHandle.frameTop = frame.top;
322 inputWindowHandle.frameRight = frame.right;
323 inputWindowHandle.frameBottom = frame.bottom;
324
325 if (child.mGlobalScale != 1) {
326 // If we are scaling the window, input coordinates need
327 // to be inversely scaled to map from what is on screen
328 // to what is actually being touched in the UI.
329 inputWindowHandle.scaleFactor = 1.0f/child.mGlobalScale;
330 } else {
331 inputWindowHandle.scaleFactor = 1;
332 }
333
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700334 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800335 Slog.d(TAG_WM, "addInputWindowHandle: "
Chong Zhangb15758a2015-11-17 12:12:03 -0800336 + child + ", " + inputWindowHandle);
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700337 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700338 addInputWindowHandle(inputWindowHandle);
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800339 if (hasFocus) {
340 mFocusedInputWindowHandle = inputWindowHandle;
341 }
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700342 }
343
Jeff Brown9302c872011-07-13 22:51:29 -0700344 private void clearInputWindowHandlesLw() {
345 while (mInputWindowHandleCount != 0) {
346 mInputWindowHandles[--mInputWindowHandleCount] = null;
347 }
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800348 mFocusedInputWindowHandle = null;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800349 }
350
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000351 void setUpdateInputWindowsNeededLw() {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800352 mUpdateInputWindowsNeeded = true;
353 }
354
355 /* Updates the cached window information provided to the input dispatcher. */
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000356 void updateInputWindowsLw(boolean force) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800357 if (!force && !mUpdateInputWindowsNeeded) {
358 return;
359 }
360 mUpdateInputWindowsNeeded = false;
361
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800362 if (false) Slog.d(TAG_WM, ">>>>>> ENTERED updateInputWindowsLw");
Jeff Brown9302c872011-07-13 22:51:29 -0700363
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800364 // Populate the input window list with information about all of the windows that
365 // could potentially receive input.
366 // As an optimization, we could try to prune the list of windows but this turns
367 // out to be difficult because only the native code knows for sure which window
368 // currently has touch focus.
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800369
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700370 // If there's a drag in flight, provide a pseudo-window to catch drag input
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800371 final boolean inDrag = (mService.mDragState != null);
372 if (inDrag) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800373 if (DEBUG_DRAG) {
374 Log.d(TAG_WM, "Inserting drag window");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800375 }
Vladislav Kaznacheev64b103a2016-08-10 11:49:08 -0700376 final InputWindowHandle dragWindowHandle = mService.mDragState.getInputWindowHandle();
Jeff Browncc4f7db2011-08-30 20:34:48 -0700377 if (dragWindowHandle != null) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700378 addInputWindowHandle(dragWindowHandle);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700379 } else {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800380 Slog.w(TAG_WM, "Drag is in progress but there is no "
Jeff Browncc4f7db2011-08-30 20:34:48 -0700381 + "drag window handle.");
382 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800383 }
384
Chong Zhang8e89b312015-09-09 15:09:30 -0700385 final boolean inPositioning = (mService.mTaskPositioner != null);
386 if (inPositioning) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800387 if (DEBUG_TASK_POSITIONING) {
388 Log.d(TAG_WM, "Inserting window handle for repositioning");
Chong Zhang8e89b312015-09-09 15:09:30 -0700389 }
390 final InputWindowHandle dragWindowHandle = mService.mTaskPositioner.mDragWindowHandle;
391 if (dragWindowHandle != null) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700392 addInputWindowHandle(dragWindowHandle);
Chong Zhang8e89b312015-09-09 15:09:30 -0700393 } else {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800394 Slog.e(TAG_WM,
Chong Zhang8e89b312015-09-09 15:09:30 -0700395 "Repositioning is in progress but there is no drag window handle.");
396 }
397 }
398
Jeff Brown83d616a2012-09-09 20:33:43 -0700399 // Add all windows on the default display.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800400 mUpdateInputForAllWindowsConsumer.updateInputWindows(inDrag);
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000401
402 if (false) Slog.d(TAG_WM, "<<<<<<< EXITED updateInputWindowsLw");
403 }
404
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800405 /* Notifies that the input device configuration has changed. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700406 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800407 public void notifyConfigurationChanged() {
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700408 // TODO(multi-display): Notify proper displays that are associated with this input device.
409 mService.sendNewConfiguration(DEFAULT_DISPLAY);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800410
411 synchronized (mInputDevicesReadyMonitor) {
412 if (!mInputDevicesReady) {
413 mInputDevicesReady = true;
414 mInputDevicesReadyMonitor.notifyAll();
415 }
416 }
417 }
418
419 /* Waits until the built-in input devices have been configured. */
420 public boolean waitForInputDevicesReady(long timeoutMillis) {
421 synchronized (mInputDevicesReadyMonitor) {
422 if (!mInputDevicesReady) {
423 try {
424 mInputDevicesReadyMonitor.wait(timeoutMillis);
425 } catch (InterruptedException ex) {
426 }
427 }
428 return mInputDevicesReady;
429 }
430 }
431
432 /* Notifies that the lid switch changed state. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700433 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800434 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) {
435 mService.mPolicy.notifyLidSwitchChanged(whenNanos, lidOpen);
436 }
Craig Mautner58458122013-09-14 14:59:50 -0700437
Michael Wright3818c922014-09-02 13:59:07 -0700438 /* Notifies that the camera lens cover state has changed. */
439 @Override
440 public void notifyCameraLensCoverSwitchChanged(long whenNanos, boolean lensCovered) {
441 mService.mPolicy.notifyCameraLensCoverSwitchChanged(whenNanos, lensCovered);
442 }
443
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800444 /* Provides an opportunity for the window manager policy to intercept early key
445 * processing as soon as the key has been read from the device. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700446 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -0700447 public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
448 return mService.mPolicy.interceptKeyBeforeQueueing(event, policyFlags);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800449 }
Jeff Brown56194eb2011-03-02 19:23:13 -0800450
Michael Wright70af00a2014-09-03 19:30:20 -0700451 /* Provides an opportunity for the window manager policy to intercept early motion event
452 * processing when the device is in a non-interactive state since these events are normally
Jeff Brown56194eb2011-03-02 19:23:13 -0800453 * dropped. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700454 @Override
Michael Wright70af00a2014-09-03 19:30:20 -0700455 public int interceptMotionBeforeQueueingNonInteractive(long whenNanos, int policyFlags) {
456 return mService.mPolicy.interceptMotionBeforeQueueingNonInteractive(
457 whenNanos, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800458 }
459
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800460 /* Provides an opportunity for the window manager policy to process a key before
461 * ordinary dispatch. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700462 @Override
Jeff Brown905805a2011-10-12 13:57:59 -0700463 public long interceptKeyBeforeDispatching(
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800464 InputWindowHandle focus, KeyEvent event, int policyFlags) {
465 WindowState windowState = focus != null ? (WindowState) focus.windowState : null;
466 return mService.mPolicy.interceptKeyBeforeDispatching(windowState, event, policyFlags);
467 }
Craig Mautner58458122013-09-14 14:59:50 -0700468
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800469 /* Provides an opportunity for the window manager policy to process a key that
470 * the application did not handle. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700471 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800472 public KeyEvent dispatchUnhandledKey(
473 InputWindowHandle focus, KeyEvent event, int policyFlags) {
474 WindowState windowState = focus != null ? (WindowState) focus.windowState : null;
475 return mService.mPolicy.dispatchUnhandledKey(windowState, event, policyFlags);
476 }
Jeff Brown4532e612012-04-05 14:27:12 -0700477
478 /* Callback to get pointer layer. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700479 @Override
Jeff Brown4532e612012-04-05 14:27:12 -0700480 public int getPointerLayer() {
Wale Ogunwale5cd907d2017-01-26 14:14:08 -0800481 return mService.mPolicy.getWindowLayerFromTypeLw(WindowManager.LayoutParams.TYPE_POINTER)
Jeff Brown4532e612012-04-05 14:27:12 -0700482 * WindowManagerService.TYPE_LAYER_MULTIPLIER
483 + WindowManagerService.TYPE_LAYER_OFFSET;
484 }
485
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800486 /* Called when the current input focus changes.
487 * Layer assignment is assumed to be complete by the time this is called.
488 */
489 public void setInputFocusLw(WindowState newWindow, boolean updateInputWindows) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700490 if (DEBUG_FOCUS_LIGHT || DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800491 Slog.d(TAG_WM, "Input focus has changed to " + newWindow);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800492 }
493
494 if (newWindow != mInputFocus) {
495 if (newWindow != null && newWindow.canReceiveKeys()) {
496 // Displaying a window implicitly causes dispatching to be unpaused.
497 // This is to protect against bugs if someone pauses dispatching but
498 // forgets to resume.
499 newWindow.mToken.paused = false;
500 }
501
502 mInputFocus = newWindow;
503 setUpdateInputWindowsNeededLw();
504
505 if (updateInputWindows) {
506 updateInputWindowsLw(false /*force*/);
507 }
508 }
509 }
Craig Mautner58458122013-09-14 14:59:50 -0700510
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800511 public void setFocusedAppLw(AppWindowToken newApp) {
512 // Focused app has changed.
513 if (newApp == null) {
514 mService.mInputManager.setFocusedApplication(null);
515 } else {
Jeff Brown9302c872011-07-13 22:51:29 -0700516 final InputApplicationHandle handle = newApp.mInputApplicationHandle;
517 handle.name = newApp.toString();
Wale Ogunwale72919d22016-12-08 18:58:50 -0800518 handle.dispatchingTimeoutNanos = newApp.mInputDispatchingTimeoutNanos;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800519
Jeff Brown9302c872011-07-13 22:51:29 -0700520 mService.mInputManager.setFocusedApplication(handle);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800521 }
522 }
Craig Mautner58458122013-09-14 14:59:50 -0700523
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800524 public void pauseDispatchingLw(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, "Pausing 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 = true;
531 updateInputWindowsLw(true /*force*/);
532 }
533 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700534
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800535 public void resumeDispatchingLw(WindowToken window) {
536 if (window.paused) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700537 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800538 Slog.v(TAG_WM, "Resuming WindowToken " + window);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800539 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700540
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800541 window.paused = false;
542 updateInputWindowsLw(true /*force*/);
543 }
544 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700545
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800546 public void freezeInputDispatchingLw() {
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800547 if (!mInputDispatchFrozen) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700548 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800549 Slog.v(TAG_WM, "Freezing input dispatching");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800550 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700551
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800552 mInputDispatchFrozen = true;
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800553
554 if (DEBUG_INPUT || true) {
555 mInputFreezeReason = Debug.getCallers(6);
556 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800557 updateInputDispatchModeLw();
558 }
559 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700560
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800561 public void thawInputDispatchingLw() {
562 if (mInputDispatchFrozen) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700563 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800564 Slog.v(TAG_WM, "Thawing input dispatching");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800565 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700566
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800567 mInputDispatchFrozen = false;
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800568 mInputFreezeReason = null;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800569 updateInputDispatchModeLw();
570 }
571 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700572
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800573 public void setEventDispatchingLw(boolean enabled) {
574 if (mInputDispatchEnabled != enabled) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700575 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800576 Slog.v(TAG_WM, "Setting event dispatching to " + enabled);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800577 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700578
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800579 mInputDispatchEnabled = enabled;
580 updateInputDispatchModeLw();
581 }
582 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700583
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800584 private void updateInputDispatchModeLw() {
585 mService.mInputManager.setInputDispatchMode(mInputDispatchEnabled, mInputDispatchFrozen);
586 }
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800587
588 void dump(PrintWriter pw, String prefix) {
589 if (mInputFreezeReason != null) {
590 pw.println(prefix + "mInputFreezeReason=" + mInputFreezeReason);
591 }
Winson Chung853c99a2017-03-21 22:16:42 -0700592 final Set<String> inputConsumerKeys = mInputConsumers.keySet();
593 if (!inputConsumerKeys.isEmpty()) {
594 pw.println(prefix + "InputConsumers:");
595 for (String key : inputConsumerKeys) {
596 pw.println(prefix + " name=" + key);
597 }
598 }
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800599 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800600
601 private final class UpdateInputForAllWindowsConsumer implements Consumer<WindowState> {
602
603 InputConsumerImpl navInputConsumer;
604 InputConsumerImpl pipInputConsumer;
605 InputConsumerImpl wallpaperInputConsumer;
606 Rect pipTouchableBounds;
607 boolean inDrag;
608 WallpaperController wallpaperController;
609
610 private void updateInputWindows(boolean inDrag) {
611
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800612 // TODO: multi-display
613 navInputConsumer = getInputConsumer(INPUT_CONSUMER_NAVIGATION, DEFAULT_DISPLAY);
614 pipInputConsumer = getInputConsumer(INPUT_CONSUMER_PIP, DEFAULT_DISPLAY);
615 wallpaperInputConsumer = getInputConsumer(INPUT_CONSUMER_WALLPAPER, DEFAULT_DISPLAY);
616 mAddInputConsumerHandle = navInputConsumer != null;
617 mAddPipInputConsumerHandle = pipInputConsumer != null;
618 mAddWallpaperInputConsumerHandle = wallpaperInputConsumer != null;
619 mTmpRect.setEmpty();
620 pipTouchableBounds = mAddPipInputConsumerHandle ? mTmpRect : null;
621 mDisableWallpaperTouchEvents = false;
622 this.inDrag = inDrag;
623 wallpaperController = mService.mRoot.mWallpaperController;
624
625 mService.mRoot.forAllWindows(this, true /* traverseTopToBottom */);
626 if (mAddWallpaperInputConsumerHandle) {
627 // No visible wallpaper found, add the wallpaper input consumer at the end.
628 addInputWindowHandle(wallpaperInputConsumer.mWindowHandle);
629 }
630
631 // Send windows to native code.
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800632 mService.mInputManager.setInputWindows(mInputWindowHandles, mFocusedInputWindowHandle);
Vladislav Kaznacheev2e96c632016-12-13 14:31:24 -0800633
634 clearInputWindowHandlesLw();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800635 }
636
637 @Override
638 public void accept(WindowState w) {
639 final InputChannel inputChannel = w.mInputChannel;
640 final InputWindowHandle inputWindowHandle = w.mInputWindowHandle;
641 if (inputChannel == null || inputWindowHandle == null || w.mRemoved
Matthew Nge15352e2016-12-20 15:36:29 -0800642 || w.canReceiveTouchInput()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800643 // Skip this window because it cannot possibly receive input.
644 return;
645 }
646
Winson Chung61ecc1b2017-02-17 10:46:17 -0800647 final int flags = w.mAttrs.flags;
648 final int privateFlags = w.mAttrs.privateFlags;
649 final int type = w.mAttrs.type;
650 final boolean hasFocus = w == mInputFocus;
651 final boolean isVisible = w.isVisibleLw();
652
653 if (w.getStackId() == PINNED_STACK_ID) {
654 if (mAddPipInputConsumerHandle
655 && (inputWindowHandle.layer <= pipInputConsumer.mWindowHandle.layer)) {
656 // Update the bounds of the Pip input consumer to match the Pinned stack
657 w.getStack().getBounds(pipTouchableBounds);
658 pipInputConsumer.mWindowHandle.touchableRegion.set(pipTouchableBounds);
659 addInputWindowHandle(pipInputConsumer.mWindowHandle);
660 mAddPipInputConsumerHandle = false;
661 }
662 // TODO: Fix w.canReceiveTouchInput() to handle this case
663 if (!hasFocus) {
664 // Skip this pinned stack window if it does not have focus
665 return;
666 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800667 }
668
669 if (mAddInputConsumerHandle
670 && inputWindowHandle.layer <= navInputConsumer.mWindowHandle.layer) {
671 addInputWindowHandle(navInputConsumer.mWindowHandle);
672 mAddInputConsumerHandle = false;
673 }
674
675 if (mAddWallpaperInputConsumerHandle) {
676 if (w.mAttrs.type == TYPE_WALLPAPER && w.isVisibleLw()) {
677 // Add the wallpaper input consumer above the first visible wallpaper.
678 addInputWindowHandle(wallpaperInputConsumer.mWindowHandle);
679 mAddWallpaperInputConsumerHandle = false;
680 }
681 }
682
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800683 if ((privateFlags & PRIVATE_FLAG_DISABLE_WALLPAPER_TOUCH_EVENTS) != 0) {
684 mDisableWallpaperTouchEvents = true;
685 }
686 final boolean hasWallpaper = wallpaperController.isWallpaperTarget(w)
687 && (privateFlags & PRIVATE_FLAG_KEYGUARD) == 0
688 && !mDisableWallpaperTouchEvents;
689
690 // If there's a drag in progress and 'child' is a potential drop target,
691 // make sure it's been told about the drag
692 if (inDrag && isVisible && w.getDisplayContent().isDefaultDisplay) {
693 mService.mDragState.sendDragStartedIfNeededLw(w);
694 }
695
696 addInputWindowHandle(
697 inputWindowHandle, w, flags, type, isVisible, hasFocus, hasWallpaper);
698 }
699 }
Jeff Brownea426552011-07-18 16:53:48 -0700700}