blob: f2b6447ce8e9e99d6a1197fc5131cc417793c452 [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) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800254 // Notify the activity manager about the timeout and let it decide whether
255 // to abort dispatching or keep waiting.
256 final AppWindowContainerController controller = appWindowToken.getController();
257 final boolean abort = controller != null && controller.keyDispatchingTimedOut(reason);
258 if (!abort) {
259 // The activity manager declined to abort dispatching.
260 // Wait a bit longer and timeout again later.
261 return appWindowToken.mInputDispatchingTimeoutNanos;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800262 }
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700263 } else if (windowState != null) {
264 try {
265 // Notify the activity manager about the timeout and let it decide whether
266 // to abort dispatching or keep waiting.
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800267 long timeout = ActivityManager.getService().inputDispatchingTimedOut(
Jeff Brownbd181bb2013-09-10 16:44:24 -0700268 windowState.mSession.mPid, aboveSystem, reason);
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700269 if (timeout >= 0) {
270 // The activity manager declined to abort dispatching.
271 // Wait a bit longer and timeout again later.
baik.handef340d2015-04-15 10:21:05 +0900272 return timeout * 1000000L; // nanoseconds
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700273 }
274 } catch (RemoteException ex) {
275 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800276 }
277 return 0; // abort dispatching
278 }
279
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700280 void addInputWindowHandle(final InputWindowHandle windowHandle) {
Jeff Brown9302c872011-07-13 22:51:29 -0700281 if (mInputWindowHandles == null) {
282 mInputWindowHandles = new InputWindowHandle[16];
283 }
284 if (mInputWindowHandleCount >= mInputWindowHandles.length) {
285 mInputWindowHandles = Arrays.copyOf(mInputWindowHandles,
286 mInputWindowHandleCount * 2);
287 }
288 mInputWindowHandles[mInputWindowHandleCount++] = windowHandle;
289 }
290
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700291 void addInputWindowHandle(final InputWindowHandle inputWindowHandle,
Craig Mautnerc08eab82014-11-11 09:03:59 -0800292 final WindowState child, int flags, final int type, final boolean isVisible,
Wale Ogunwale053c8e42015-11-16 14:27:21 -0800293 final boolean hasFocus, final boolean hasWallpaper) {
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700294 // Add a window to our list of input windows.
295 inputWindowHandle.name = child.toString();
Wale Ogunwale053c8e42015-11-16 14:27:21 -0800296 flags = child.getTouchableRegion(inputWindowHandle.touchableRegion, flags);
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700297 inputWindowHandle.layoutParamsFlags = flags;
298 inputWindowHandle.layoutParamsType = type;
299 inputWindowHandle.dispatchingTimeoutNanos = child.getInputDispatchingTimeoutNanos();
300 inputWindowHandle.visible = isVisible;
301 inputWindowHandle.canReceiveKeys = child.canReceiveKeys();
302 inputWindowHandle.hasFocus = hasFocus;
303 inputWindowHandle.hasWallpaper = hasWallpaper;
304 inputWindowHandle.paused = child.mAppToken != null ? child.mAppToken.paused : false;
305 inputWindowHandle.layer = child.mLayer;
306 inputWindowHandle.ownerPid = child.mSession.mPid;
307 inputWindowHandle.ownerUid = child.mSession.mUid;
308 inputWindowHandle.inputFeatures = child.mAttrs.inputFeatures;
309
310 final Rect frame = child.mFrame;
311 inputWindowHandle.frameLeft = frame.left;
312 inputWindowHandle.frameTop = frame.top;
313 inputWindowHandle.frameRight = frame.right;
314 inputWindowHandle.frameBottom = frame.bottom;
315
316 if (child.mGlobalScale != 1) {
317 // If we are scaling the window, input coordinates need
318 // to be inversely scaled to map from what is on screen
319 // to what is actually being touched in the UI.
320 inputWindowHandle.scaleFactor = 1.0f/child.mGlobalScale;
321 } else {
322 inputWindowHandle.scaleFactor = 1;
323 }
324
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700325 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800326 Slog.d(TAG_WM, "addInputWindowHandle: "
Chong Zhangb15758a2015-11-17 12:12:03 -0800327 + child + ", " + inputWindowHandle);
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700328 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700329 addInputWindowHandle(inputWindowHandle);
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700330 }
331
Jeff Brown9302c872011-07-13 22:51:29 -0700332 private void clearInputWindowHandlesLw() {
333 while (mInputWindowHandleCount != 0) {
334 mInputWindowHandles[--mInputWindowHandleCount] = null;
335 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800336 }
337
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000338 void setUpdateInputWindowsNeededLw() {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800339 mUpdateInputWindowsNeeded = true;
340 }
341
342 /* Updates the cached window information provided to the input dispatcher. */
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000343 void updateInputWindowsLw(boolean force) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800344 if (!force && !mUpdateInputWindowsNeeded) {
345 return;
346 }
347 mUpdateInputWindowsNeeded = false;
348
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800349 if (false) Slog.d(TAG_WM, ">>>>>> ENTERED updateInputWindowsLw");
Jeff Brown9302c872011-07-13 22:51:29 -0700350
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800351 // Populate the input window list with information about all of the windows that
352 // could potentially receive input.
353 // As an optimization, we could try to prune the list of windows but this turns
354 // out to be difficult because only the native code knows for sure which window
355 // currently has touch focus.
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800356
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700357 // If there's a drag in flight, provide a pseudo-window to catch drag input
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800358 final boolean inDrag = (mService.mDragState != null);
359 if (inDrag) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800360 if (DEBUG_DRAG) {
361 Log.d(TAG_WM, "Inserting drag window");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800362 }
Vladislav Kaznacheev64b103a2016-08-10 11:49:08 -0700363 final InputWindowHandle dragWindowHandle = mService.mDragState.getInputWindowHandle();
Jeff Browncc4f7db2011-08-30 20:34:48 -0700364 if (dragWindowHandle != null) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700365 addInputWindowHandle(dragWindowHandle);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700366 } else {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800367 Slog.w(TAG_WM, "Drag is in progress but there is no "
Jeff Browncc4f7db2011-08-30 20:34:48 -0700368 + "drag window handle.");
369 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800370 }
371
Chong Zhang8e89b312015-09-09 15:09:30 -0700372 final boolean inPositioning = (mService.mTaskPositioner != null);
373 if (inPositioning) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800374 if (DEBUG_TASK_POSITIONING) {
375 Log.d(TAG_WM, "Inserting window handle for repositioning");
Chong Zhang8e89b312015-09-09 15:09:30 -0700376 }
377 final InputWindowHandle dragWindowHandle = mService.mTaskPositioner.mDragWindowHandle;
378 if (dragWindowHandle != null) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700379 addInputWindowHandle(dragWindowHandle);
Chong Zhang8e89b312015-09-09 15:09:30 -0700380 } else {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800381 Slog.e(TAG_WM,
Chong Zhang8e89b312015-09-09 15:09:30 -0700382 "Repositioning is in progress but there is no drag window handle.");
383 }
384 }
385
Jeff Brown83d616a2012-09-09 20:33:43 -0700386 // Add all windows on the default display.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800387 mUpdateInputForAllWindowsConsumer.updateInputWindows(inDrag);
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000388
389 if (false) Slog.d(TAG_WM, "<<<<<<< EXITED updateInputWindowsLw");
390 }
391
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800392 /* Notifies that the input device configuration has changed. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700393 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800394 public void notifyConfigurationChanged() {
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700395 // TODO(multi-display): Notify proper displays that are associated with this input device.
396 mService.sendNewConfiguration(DEFAULT_DISPLAY);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800397
398 synchronized (mInputDevicesReadyMonitor) {
399 if (!mInputDevicesReady) {
400 mInputDevicesReady = true;
401 mInputDevicesReadyMonitor.notifyAll();
402 }
403 }
404 }
405
406 /* Waits until the built-in input devices have been configured. */
407 public boolean waitForInputDevicesReady(long timeoutMillis) {
408 synchronized (mInputDevicesReadyMonitor) {
409 if (!mInputDevicesReady) {
410 try {
411 mInputDevicesReadyMonitor.wait(timeoutMillis);
412 } catch (InterruptedException ex) {
413 }
414 }
415 return mInputDevicesReady;
416 }
417 }
418
419 /* Notifies that the lid switch changed state. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700420 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800421 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) {
422 mService.mPolicy.notifyLidSwitchChanged(whenNanos, lidOpen);
423 }
Craig Mautner58458122013-09-14 14:59:50 -0700424
Michael Wright3818c922014-09-02 13:59:07 -0700425 /* Notifies that the camera lens cover state has changed. */
426 @Override
427 public void notifyCameraLensCoverSwitchChanged(long whenNanos, boolean lensCovered) {
428 mService.mPolicy.notifyCameraLensCoverSwitchChanged(whenNanos, lensCovered);
429 }
430
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800431 /* Provides an opportunity for the window manager policy to intercept early key
432 * processing as soon as the key has been read from the device. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700433 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -0700434 public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
435 return mService.mPolicy.interceptKeyBeforeQueueing(event, policyFlags);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800436 }
Jeff Brown56194eb2011-03-02 19:23:13 -0800437
Michael Wright70af00a2014-09-03 19:30:20 -0700438 /* Provides an opportunity for the window manager policy to intercept early motion event
439 * processing when the device is in a non-interactive state since these events are normally
Jeff Brown56194eb2011-03-02 19:23:13 -0800440 * dropped. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700441 @Override
Michael Wright70af00a2014-09-03 19:30:20 -0700442 public int interceptMotionBeforeQueueingNonInteractive(long whenNanos, int policyFlags) {
443 return mService.mPolicy.interceptMotionBeforeQueueingNonInteractive(
444 whenNanos, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800445 }
446
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800447 /* Provides an opportunity for the window manager policy to process a key before
448 * ordinary dispatch. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700449 @Override
Jeff Brown905805a2011-10-12 13:57:59 -0700450 public long interceptKeyBeforeDispatching(
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800451 InputWindowHandle focus, KeyEvent event, int policyFlags) {
452 WindowState windowState = focus != null ? (WindowState) focus.windowState : null;
453 return mService.mPolicy.interceptKeyBeforeDispatching(windowState, event, policyFlags);
454 }
Craig Mautner58458122013-09-14 14:59:50 -0700455
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800456 /* Provides an opportunity for the window manager policy to process a key that
457 * the application did not handle. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700458 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800459 public KeyEvent dispatchUnhandledKey(
460 InputWindowHandle focus, KeyEvent event, int policyFlags) {
461 WindowState windowState = focus != null ? (WindowState) focus.windowState : null;
462 return mService.mPolicy.dispatchUnhandledKey(windowState, event, policyFlags);
463 }
Jeff Brown4532e612012-04-05 14:27:12 -0700464
465 /* Callback to get pointer layer. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700466 @Override
Jeff Brown4532e612012-04-05 14:27:12 -0700467 public int getPointerLayer() {
468 return mService.mPolicy.windowTypeToLayerLw(WindowManager.LayoutParams.TYPE_POINTER)
469 * WindowManagerService.TYPE_LAYER_MULTIPLIER
470 + WindowManagerService.TYPE_LAYER_OFFSET;
471 }
472
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800473 /* Called when the current input focus changes.
474 * Layer assignment is assumed to be complete by the time this is called.
475 */
476 public void setInputFocusLw(WindowState newWindow, boolean updateInputWindows) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700477 if (DEBUG_FOCUS_LIGHT || DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800478 Slog.d(TAG_WM, "Input focus has changed to " + newWindow);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800479 }
480
481 if (newWindow != mInputFocus) {
482 if (newWindow != null && newWindow.canReceiveKeys()) {
483 // Displaying a window implicitly causes dispatching to be unpaused.
484 // This is to protect against bugs if someone pauses dispatching but
485 // forgets to resume.
486 newWindow.mToken.paused = false;
487 }
488
489 mInputFocus = newWindow;
490 setUpdateInputWindowsNeededLw();
491
492 if (updateInputWindows) {
493 updateInputWindowsLw(false /*force*/);
494 }
495 }
496 }
Craig Mautner58458122013-09-14 14:59:50 -0700497
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800498 public void setFocusedAppLw(AppWindowToken newApp) {
499 // Focused app has changed.
500 if (newApp == null) {
501 mService.mInputManager.setFocusedApplication(null);
502 } else {
Jeff Brown9302c872011-07-13 22:51:29 -0700503 final InputApplicationHandle handle = newApp.mInputApplicationHandle;
504 handle.name = newApp.toString();
Wale Ogunwale72919d22016-12-08 18:58:50 -0800505 handle.dispatchingTimeoutNanos = newApp.mInputDispatchingTimeoutNanos;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800506
Jeff Brown9302c872011-07-13 22:51:29 -0700507 mService.mInputManager.setFocusedApplication(handle);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800508 }
509 }
Craig Mautner58458122013-09-14 14:59:50 -0700510
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800511 public void pauseDispatchingLw(WindowToken window) {
512 if (! window.paused) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700513 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800514 Slog.v(TAG_WM, "Pausing WindowToken " + window);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800515 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700516
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800517 window.paused = true;
518 updateInputWindowsLw(true /*force*/);
519 }
520 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700521
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800522 public void resumeDispatchingLw(WindowToken window) {
523 if (window.paused) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700524 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800525 Slog.v(TAG_WM, "Resuming WindowToken " + window);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800526 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700527
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800528 window.paused = false;
529 updateInputWindowsLw(true /*force*/);
530 }
531 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700532
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800533 public void freezeInputDispatchingLw() {
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800534 if (!mInputDispatchFrozen) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700535 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800536 Slog.v(TAG_WM, "Freezing input dispatching");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800537 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700538
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800539 mInputDispatchFrozen = true;
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800540
541 if (DEBUG_INPUT || true) {
542 mInputFreezeReason = Debug.getCallers(6);
543 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800544 updateInputDispatchModeLw();
545 }
546 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700547
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800548 public void thawInputDispatchingLw() {
549 if (mInputDispatchFrozen) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700550 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800551 Slog.v(TAG_WM, "Thawing input dispatching");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800552 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700553
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800554 mInputDispatchFrozen = false;
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800555 mInputFreezeReason = null;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800556 updateInputDispatchModeLw();
557 }
558 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700559
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800560 public void setEventDispatchingLw(boolean enabled) {
561 if (mInputDispatchEnabled != enabled) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700562 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800563 Slog.v(TAG_WM, "Setting event dispatching to " + enabled);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800564 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700565
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800566 mInputDispatchEnabled = enabled;
567 updateInputDispatchModeLw();
568 }
569 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700570
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800571 private void updateInputDispatchModeLw() {
572 mService.mInputManager.setInputDispatchMode(mInputDispatchEnabled, mInputDispatchFrozen);
573 }
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800574
575 void dump(PrintWriter pw, String prefix) {
576 if (mInputFreezeReason != null) {
577 pw.println(prefix + "mInputFreezeReason=" + mInputFreezeReason);
578 }
579 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800580
581 private final class UpdateInputForAllWindowsConsumer implements Consumer<WindowState> {
582
583 InputConsumerImpl navInputConsumer;
584 InputConsumerImpl pipInputConsumer;
585 InputConsumerImpl wallpaperInputConsumer;
586 Rect pipTouchableBounds;
587 boolean inDrag;
588 WallpaperController wallpaperController;
589
590 private void updateInputWindows(boolean inDrag) {
591
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800592 // TODO: multi-display
593 navInputConsumer = getInputConsumer(INPUT_CONSUMER_NAVIGATION, DEFAULT_DISPLAY);
594 pipInputConsumer = getInputConsumer(INPUT_CONSUMER_PIP, DEFAULT_DISPLAY);
595 wallpaperInputConsumer = getInputConsumer(INPUT_CONSUMER_WALLPAPER, DEFAULT_DISPLAY);
596 mAddInputConsumerHandle = navInputConsumer != null;
597 mAddPipInputConsumerHandle = pipInputConsumer != null;
598 mAddWallpaperInputConsumerHandle = wallpaperInputConsumer != null;
599 mTmpRect.setEmpty();
600 pipTouchableBounds = mAddPipInputConsumerHandle ? mTmpRect : null;
601 mDisableWallpaperTouchEvents = false;
602 this.inDrag = inDrag;
603 wallpaperController = mService.mRoot.mWallpaperController;
604
605 mService.mRoot.forAllWindows(this, true /* traverseTopToBottom */);
606 if (mAddWallpaperInputConsumerHandle) {
607 // No visible wallpaper found, add the wallpaper input consumer at the end.
608 addInputWindowHandle(wallpaperInputConsumer.mWindowHandle);
609 }
610
611 // Send windows to native code.
612 mService.mInputManager.setInputWindows(mInputWindowHandles);
Vladislav Kaznacheev2e96c632016-12-13 14:31:24 -0800613
614 clearInputWindowHandlesLw();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800615 }
616
617 @Override
618 public void accept(WindowState w) {
619 final InputChannel inputChannel = w.mInputChannel;
620 final InputWindowHandle inputWindowHandle = w.mInputWindowHandle;
621 if (inputChannel == null || inputWindowHandle == null || w.mRemoved
Matthew Nge15352e2016-12-20 15:36:29 -0800622 || w.canReceiveTouchInput()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800623 // Skip this window because it cannot possibly receive input.
624 return;
625 }
626
627 if (mAddPipInputConsumerHandle
628 && w.getStackId() == PINNED_STACK_ID
629 && inputWindowHandle.layer <= pipInputConsumer.mWindowHandle.layer) {
630 // Update the bounds of the Pip input consumer to match the Pinned stack
631 w.getStack().getBounds(pipTouchableBounds);
632 pipInputConsumer.mWindowHandle.touchableRegion.set(pipTouchableBounds);
633 addInputWindowHandle(pipInputConsumer.mWindowHandle);
634 mAddPipInputConsumerHandle = false;
635 }
636
637 if (mAddInputConsumerHandle
638 && inputWindowHandle.layer <= navInputConsumer.mWindowHandle.layer) {
639 addInputWindowHandle(navInputConsumer.mWindowHandle);
640 mAddInputConsumerHandle = false;
641 }
642
643 if (mAddWallpaperInputConsumerHandle) {
644 if (w.mAttrs.type == TYPE_WALLPAPER && w.isVisibleLw()) {
645 // Add the wallpaper input consumer above the first visible wallpaper.
646 addInputWindowHandle(wallpaperInputConsumer.mWindowHandle);
647 mAddWallpaperInputConsumerHandle = false;
648 }
649 }
650
651 final int flags = w.mAttrs.flags;
652 final int privateFlags = w.mAttrs.privateFlags;
653 final int type = w.mAttrs.type;
654
655 final boolean hasFocus = w == mInputFocus;
656 final boolean isVisible = w.isVisibleLw();
657 if ((privateFlags & PRIVATE_FLAG_DISABLE_WALLPAPER_TOUCH_EVENTS) != 0) {
658 mDisableWallpaperTouchEvents = true;
659 }
660 final boolean hasWallpaper = wallpaperController.isWallpaperTarget(w)
661 && (privateFlags & PRIVATE_FLAG_KEYGUARD) == 0
662 && !mDisableWallpaperTouchEvents;
663
664 // If there's a drag in progress and 'child' is a potential drop target,
665 // make sure it's been told about the drag
666 if (inDrag && isVisible && w.getDisplayContent().isDefaultDisplay) {
667 mService.mDragState.sendDragStartedIfNeededLw(w);
668 }
669
670 addInputWindowHandle(
671 inputWindowHandle, w, flags, type, isVisible, hasFocus, hasWallpaper);
672 }
673 }
Jeff Brownea426552011-07-18 16:53:48 -0700674}