blob: d23369efd2971d5d5da7ddc29ee7ab9625df121a [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
Andrii Kulian5406e7a2016-10-21 11:55:23 -070019import static android.view.Display.DEFAULT_DISPLAY;
Wale Ogunwale6213caa2016-12-02 16:47:15 +000020import static android.view.WindowManager.INPUT_CONSUMER_NAVIGATION;
Winson41275482016-10-10 15:17:45 -070021import static android.view.WindowManager.INPUT_CONSUMER_PIP;
22import static android.view.WindowManager.INPUT_CONSUMER_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070023import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale6213caa2016-12-02 16:47:15 +000024import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_DISABLE_WALLPAPER_TOUCH_EVENTS;
25import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale5cd907d2017-01-26 14:14:08 -080026import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
Wale Ogunwale6213caa2016-12-02 16:47:15 +000027import 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;
Winson Chung853c99a2017-03-21 22:16:42 -070055import java.util.Set;
Wale Ogunwale1e129a42016-11-21 13:03:47 -080056import java.util.function.Consumer;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080057
Jeff Browna9d131c2012-09-20 16:48:17 -070058final class InputMonitor implements InputManagerService.WindowManagerCallbacks {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080059 private final WindowManagerService mService;
Selim Cinekf83e8242015-05-19 18:08:14 -070060
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080061 // Current window with input focus for keys and other non-touch events. May be null.
62 private WindowState mInputFocus;
Selim Cinekf83e8242015-05-19 18:08:14 -070063
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080064 // When true, prevents input dispatch from proceeding until set to false again.
65 private boolean mInputDispatchFrozen;
Selim Cinekf83e8242015-05-19 18:08:14 -070066
Wale Ogunwalee89eeac2016-03-12 11:07:58 -080067 // The reason the input is currently frozen or null if the input isn't frozen.
68 private String mInputFreezeReason = null;
69
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080070 // When true, input dispatch proceeds normally. Otherwise all events are dropped.
Jeff Brownc042ee22012-05-08 13:03:42 -070071 // Initially false, so that input does not get dispatched until boot is finished at
72 // which point the ActivityManager will enable dispatching.
73 private boolean mInputDispatchEnabled;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080074
75 // When true, need to call updateInputWindowsLw().
76 private boolean mUpdateInputWindowsNeeded = true;
77
Jeff Brown9302c872011-07-13 22:51:29 -070078 // Array of window handles to provide to the input dispatcher.
79 private InputWindowHandle[] mInputWindowHandles;
80 private int mInputWindowHandleCount;
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -080081 private InputWindowHandle mFocusedInputWindowHandle;
82
Wale Ogunwale6213caa2016-12-02 16:47:15 +000083 private boolean mAddInputConsumerHandle;
84 private boolean mAddPipInputConsumerHandle;
85 private boolean mAddWallpaperInputConsumerHandle;
86 private boolean mDisableWallpaperTouchEvents;
Wale Ogunwale1e129a42016-11-21 13:03:47 -080087 private final Rect mTmpRect = new Rect();
88 private final UpdateInputForAllWindowsConsumer mUpdateInputForAllWindowsConsumer =
89 new UpdateInputForAllWindowsConsumer();
Jeff Brown9302c872011-07-13 22:51:29 -070090
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080091 // Set to true when the first input device configuration change notification
92 // is received to indicate that the input devices are ready.
93 private final Object mInputDevicesReadyMonitor = new Object();
94 private boolean mInputDevicesReady;
95
Winson41275482016-10-10 15:17:45 -070096 /**
97 * The set of input consumer added to the window manager by name, which consumes input events
98 * for the windows below it.
99 */
100 private final ArrayMap<String, InputConsumerImpl> mInputConsumers = new ArrayMap();
101
102 private static final class EventReceiverInputConsumer extends InputConsumerImpl
103 implements WindowManagerPolicy.InputConsumer {
104 private InputMonitor mInputMonitor;
105 private final InputEventReceiver mInputEventReceiver;
106
107 EventReceiverInputConsumer(WindowManagerService service, InputMonitor monitor,
108 Looper looper, String name,
109 InputEventReceiver.Factory inputEventReceiverFactory) {
110 super(service, name, null);
111 mInputMonitor = monitor;
112 mInputEventReceiver = inputEventReceiverFactory.createInputEventReceiver(
113 mClientChannel, looper);
114 }
115
116 @Override
117 public void dismiss() {
118 synchronized (mService.mWindowMap) {
119 if (mInputMonitor.destroyInputConsumer(mWindowHandle.name)) {
120 mInputEventReceiver.dispose();
121 }
122 }
123 }
124 }
125
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800126 public InputMonitor(WindowManagerService service) {
127 mService = service;
128 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700129
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700130 private void addInputConsumer(String name, InputConsumerImpl consumer) {
Winson41275482016-10-10 15:17:45 -0700131 mInputConsumers.put(name, consumer);
132 updateInputWindowsLw(true /* force */);
133 }
134
135 boolean destroyInputConsumer(String name) {
136 if (disposeInputConsumer(mInputConsumers.remove(name))) {
137 updateInputWindowsLw(true /* force */);
138 return true;
139 }
140 return false;
141 }
142
143 private boolean disposeInputConsumer(InputConsumerImpl consumer) {
144 if (consumer != null) {
145 consumer.disposeChannelsLw();
146 return true;
147 }
148 return false;
149 }
150
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700151 InputConsumerImpl getInputConsumer(String name, int displayId) {
152 // TODO(multi-display): Allow input consumers on non-default displays?
153 return (displayId == DEFAULT_DISPLAY) ? mInputConsumers.get(name) : null;
Winson41275482016-10-10 15:17:45 -0700154 }
155
156 void layoutInputConsumers(int dw, int dh) {
157 for (int i = mInputConsumers.size() - 1; i >= 0; i--) {
158 mInputConsumers.valueAt(i).layout(dw, dh);
159 }
160 }
161
162 WindowManagerPolicy.InputConsumer createInputConsumer(Looper looper, String name,
163 InputEventReceiver.Factory inputEventReceiverFactory) {
164 if (mInputConsumers.containsKey(name)) {
165 throw new IllegalStateException("Existing input consumer found with name: " + name);
166 }
167
168 final EventReceiverInputConsumer consumer = new EventReceiverInputConsumer(mService,
169 this, looper, name, inputEventReceiverFactory);
170 addInputConsumer(name, consumer);
171 return consumer;
172 }
173
174 void createInputConsumer(String name, InputChannel inputChannel) {
175 if (mInputConsumers.containsKey(name)) {
176 throw new IllegalStateException("Existing input consumer found with name: " + name);
177 }
178
179 final InputConsumerImpl consumer = new InputConsumerImpl(mService, name, inputChannel);
180 switch (name) {
181 case INPUT_CONSUMER_WALLPAPER:
182 consumer.mWindowHandle.hasWallpaper = true;
183 break;
184 case INPUT_CONSUMER_PIP:
185 // The touchable region of the Pip input window is cropped to the bounds of the
186 // stack, and we need FLAG_NOT_TOUCH_MODAL to ensure other events fall through
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700187 consumer.mWindowHandle.layoutParamsFlags |= FLAG_NOT_TOUCH_MODAL;
Winson41275482016-10-10 15:17:45 -0700188 break;
189 }
190 addInputConsumer(name, consumer);
191 }
192
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800193 /* Notifies the window manager about a broken input channel.
Chong Zhang8e89b312015-09-09 15:09:30 -0700194 *
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800195 * Called by the InputManager.
196 */
Craig Mautner4cd0c13f2013-04-16 15:55:52 -0700197 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800198 public void notifyInputChannelBroken(InputWindowHandle inputWindowHandle) {
199 if (inputWindowHandle == null) {
200 return;
201 }
202
203 synchronized (mService.mWindowMap) {
204 WindowState windowState = (WindowState) inputWindowHandle.windowState;
Jeff Brown9302c872011-07-13 22:51:29 -0700205 if (windowState != null) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800206 Slog.i(TAG_WM, "WINDOW DIED " + windowState);
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700207 windowState.removeIfPossible();
Jeff Brown9302c872011-07-13 22:51:29 -0700208 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800209 }
210 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700211
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800212 /* Notifies the window manager about an application that is not responding.
213 * Returns a new timeout to continue waiting in nanoseconds, or 0 to abort dispatch.
Chong Zhang8e89b312015-09-09 15:09:30 -0700214 *
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800215 * Called by the InputManager.
216 */
Craig Mautner4cd0c13f2013-04-16 15:55:52 -0700217 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800218 public long notifyANR(InputApplicationHandle inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700219 InputWindowHandle inputWindowHandle, String reason) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800220 AppWindowToken appWindowToken = null;
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700221 WindowState windowState = null;
222 boolean aboveSystem = false;
Jeff Brownee172412012-06-18 12:58:03 -0700223 synchronized (mService.mWindowMap) {
Jeff Brownee172412012-06-18 12:58:03 -0700224 if (inputWindowHandle != null) {
225 windowState = (WindowState) inputWindowHandle.windowState;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800226 if (windowState != null) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800227 appWindowToken = windowState.mAppToken;
228 }
229 }
Jeff Brownee172412012-06-18 12:58:03 -0700230 if (appWindowToken == null && inputApplicationHandle != null) {
231 appWindowToken = (AppWindowToken)inputApplicationHandle.appWindowToken;
Jeff Brown9302c872011-07-13 22:51:29 -0700232 }
Jeff Brownee172412012-06-18 12:58:03 -0700233
234 if (windowState != null) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800235 Slog.i(TAG_WM, "Input event dispatching timed out "
Jeff Brownbd181bb2013-09-10 16:44:24 -0700236 + "sending to " + windowState.mAttrs.getTitle()
237 + ". Reason: " + reason);
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700238 // Figure out whether this window is layered above system windows.
239 // We need to do this here to help the activity manager know how to
240 // layer its ANR dialog.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -0800241 int systemAlertLayer = mService.mPolicy.getWindowLayerFromTypeLw(
242 TYPE_APPLICATION_OVERLAY, windowState.mOwnerCanAddInternalSystemWindow);
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700243 aboveSystem = windowState.mBaseLayer > systemAlertLayer;
Jeff Brownee172412012-06-18 12:58:03 -0700244 } else if (appWindowToken != null) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800245 Slog.i(TAG_WM, "Input event dispatching timed out "
Jeff Brownbd181bb2013-09-10 16:44:24 -0700246 + "sending to application " + appWindowToken.stringName
247 + ". Reason: " + reason);
Jeff Brownee172412012-06-18 12:58:03 -0700248 } else {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800249 Slog.i(TAG_WM, "Input event dispatching timed out "
Jeff Brownbd181bb2013-09-10 16:44:24 -0700250 + ". Reason: " + reason);
Jeff Brownee172412012-06-18 12:58:03 -0700251 }
252
Jeff Brownbd181bb2013-09-10 16:44:24 -0700253 mService.saveANRStateLocked(appWindowToken, windowState, reason);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800254 }
255
Wale Ogunwalef1285912017-06-09 15:20:29 -0700256 // All the calls below need to happen without the WM lock held since they call into AM.
257 mService.mAmInternal.saveANRState(reason);
258
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800259 if (appWindowToken != null && appWindowToken.appToken != null) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800260 // Notify the activity manager about the timeout and let it decide whether
261 // to abort dispatching or keep waiting.
262 final AppWindowContainerController controller = appWindowToken.getController();
Wale Ogunwale7402ddf2017-03-29 12:58:24 -0700263 final boolean abort = controller != null
Brian Carlstrom7b0f2e82017-03-31 00:24:18 -0700264 && controller.keyDispatchingTimedOut(reason,
265 (windowState != null) ? windowState.mSession.mPid : -1);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800266 if (!abort) {
267 // The activity manager declined to abort dispatching.
268 // Wait a bit longer and timeout again later.
269 return appWindowToken.mInputDispatchingTimeoutNanos;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800270 }
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700271 } else if (windowState != null) {
272 try {
273 // Notify the activity manager about the timeout and let it decide whether
274 // to abort dispatching or keep waiting.
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800275 long timeout = ActivityManager.getService().inputDispatchingTimedOut(
Jeff Brownbd181bb2013-09-10 16:44:24 -0700276 windowState.mSession.mPid, aboveSystem, reason);
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700277 if (timeout >= 0) {
278 // The activity manager declined to abort dispatching.
279 // Wait a bit longer and timeout again later.
baik.handef340d2015-04-15 10:21:05 +0900280 return timeout * 1000000L; // nanoseconds
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700281 }
282 } catch (RemoteException ex) {
283 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800284 }
285 return 0; // abort dispatching
286 }
287
Wale Ogunwale7402ddf2017-03-29 12:58:24 -0700288 private void addInputWindowHandle(final InputWindowHandle windowHandle) {
Jeff Brown9302c872011-07-13 22:51:29 -0700289 if (mInputWindowHandles == null) {
290 mInputWindowHandles = new InputWindowHandle[16];
291 }
292 if (mInputWindowHandleCount >= mInputWindowHandles.length) {
293 mInputWindowHandles = Arrays.copyOf(mInputWindowHandles,
294 mInputWindowHandleCount * 2);
295 }
296 mInputWindowHandles[mInputWindowHandleCount++] = windowHandle;
297 }
298
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700299 void addInputWindowHandle(final InputWindowHandle inputWindowHandle,
Craig Mautnerc08eab82014-11-11 09:03:59 -0800300 final WindowState child, int flags, final int type, final boolean isVisible,
Wale Ogunwale053c8e42015-11-16 14:27:21 -0800301 final boolean hasFocus, final boolean hasWallpaper) {
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700302 // Add a window to our list of input windows.
303 inputWindowHandle.name = child.toString();
Wale Ogunwale053c8e42015-11-16 14:27:21 -0800304 flags = child.getTouchableRegion(inputWindowHandle.touchableRegion, flags);
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700305 inputWindowHandle.layoutParamsFlags = flags;
306 inputWindowHandle.layoutParamsType = type;
307 inputWindowHandle.dispatchingTimeoutNanos = child.getInputDispatchingTimeoutNanos();
308 inputWindowHandle.visible = isVisible;
309 inputWindowHandle.canReceiveKeys = child.canReceiveKeys();
310 inputWindowHandle.hasFocus = hasFocus;
311 inputWindowHandle.hasWallpaper = hasWallpaper;
312 inputWindowHandle.paused = child.mAppToken != null ? child.mAppToken.paused : false;
313 inputWindowHandle.layer = child.mLayer;
314 inputWindowHandle.ownerPid = child.mSession.mPid;
315 inputWindowHandle.ownerUid = child.mSession.mUid;
316 inputWindowHandle.inputFeatures = child.mAttrs.inputFeatures;
317
318 final Rect frame = child.mFrame;
319 inputWindowHandle.frameLeft = frame.left;
320 inputWindowHandle.frameTop = frame.top;
321 inputWindowHandle.frameRight = frame.right;
322 inputWindowHandle.frameBottom = frame.bottom;
323
324 if (child.mGlobalScale != 1) {
325 // If we are scaling the window, input coordinates need
326 // to be inversely scaled to map from what is on screen
327 // to what is actually being touched in the UI.
328 inputWindowHandle.scaleFactor = 1.0f/child.mGlobalScale;
329 } else {
330 inputWindowHandle.scaleFactor = 1;
331 }
332
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700333 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800334 Slog.d(TAG_WM, "addInputWindowHandle: "
Chong Zhangb15758a2015-11-17 12:12:03 -0800335 + child + ", " + inputWindowHandle);
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700336 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700337 addInputWindowHandle(inputWindowHandle);
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800338 if (hasFocus) {
339 mFocusedInputWindowHandle = inputWindowHandle;
340 }
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700341 }
342
Jeff Brown9302c872011-07-13 22:51:29 -0700343 private void clearInputWindowHandlesLw() {
344 while (mInputWindowHandleCount != 0) {
345 mInputWindowHandles[--mInputWindowHandleCount] = null;
346 }
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800347 mFocusedInputWindowHandle = null;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800348 }
349
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000350 void setUpdateInputWindowsNeededLw() {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800351 mUpdateInputWindowsNeeded = true;
352 }
353
354 /* Updates the cached window information provided to the input dispatcher. */
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000355 void updateInputWindowsLw(boolean force) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800356 if (!force && !mUpdateInputWindowsNeeded) {
357 return;
358 }
359 mUpdateInputWindowsNeeded = false;
360
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800361 if (false) Slog.d(TAG_WM, ">>>>>> ENTERED updateInputWindowsLw");
Jeff Brown9302c872011-07-13 22:51:29 -0700362
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800363 // Populate the input window list with information about all of the windows that
364 // could potentially receive input.
365 // As an optimization, we could try to prune the list of windows but this turns
366 // out to be difficult because only the native code knows for sure which window
367 // currently has touch focus.
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800368
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700369 // If there's a drag in flight, provide a pseudo-window to catch drag input
Daichi Hirono76a26aa2017-09-11 15:13:38 +0900370 final boolean inDrag = mService.mDragDropController.dragDropActiveLocked();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800371 if (inDrag) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800372 if (DEBUG_DRAG) {
373 Log.d(TAG_WM, "Inserting drag window");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800374 }
Daichi Hirono76a26aa2017-09-11 15:13:38 +0900375 final InputWindowHandle dragWindowHandle =
376 mService.mDragDropController.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
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700653 if (w.inPinnedWindowingMode()) {
Winson Chung61ecc1b2017-02-17 10:46:17 -0800654 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) {
Daichi Hirono58e25e12017-10-25 15:48:08 +0900693 mService.mDragDropController.mDragState.sendDragStartedIfNeededLocked(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800694 }
695
696 addInputWindowHandle(
697 inputWindowHandle, w, flags, type, isVisible, hasFocus, hasWallpaper);
698 }
699 }
Jeff Brownea426552011-07-18 16:53:48 -0700700}