blob: 88b7a11f02fd52eddefa4e179d17bc1a9d280a6c [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;
Winson Chung6463c362017-09-25 16:23:26 -070028
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;
Winson Chung6463c362017-09-25 16:23:26 -070038import android.os.IBinder;
Winson41275482016-10-10 15:17:45 -070039import android.os.Looper;
Winson Chung6463c362017-09-25 16:23:26 -070040import android.os.Process;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080041import android.os.RemoteException;
Winson Chung6463c362017-09-25 16:23:26 -070042import android.os.UserHandle;
Winson41275482016-10-10 15:17:45 -070043import android.util.ArrayMap;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080044import android.util.Log;
45import android.util.Slog;
Jeff Browncc4f7db2011-08-30 20:34:48 -070046import android.view.InputChannel;
Winson41275482016-10-10 15:17:45 -070047import android.view.InputEventReceiver;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080048import android.view.KeyEvent;
49import android.view.WindowManager;
Wale Ogunwale1e129a42016-11-21 13:03:47 -080050
Selim Cinekf83e8242015-05-19 18:08:14 -070051import com.android.server.input.InputApplicationHandle;
52import com.android.server.input.InputManagerService;
53import com.android.server.input.InputWindowHandle;
Adrian Roose99bc052017-11-20 17:55:31 +010054import com.android.server.policy.WindowManagerPolicy;
Selim Cinekf83e8242015-05-19 18:08:14 -070055
Wale Ogunwalee89eeac2016-03-12 11:07:58 -080056import java.io.PrintWriter;
Jeff Brown9302c872011-07-13 22:51:29 -070057import java.util.Arrays;
Winson Chung853c99a2017-03-21 22:16:42 -070058import java.util.Set;
Wale Ogunwale1e129a42016-11-21 13:03:47 -080059import java.util.function.Consumer;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080060
Jeff Browna9d131c2012-09-20 16:48:17 -070061final class InputMonitor implements InputManagerService.WindowManagerCallbacks {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080062 private final WindowManagerService mService;
Selim Cinekf83e8242015-05-19 18:08:14 -070063
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080064 // Current window with input focus for keys and other non-touch events. May be null.
65 private WindowState mInputFocus;
Selim Cinekf83e8242015-05-19 18:08:14 -070066
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080067 // When true, prevents input dispatch from proceeding until set to false again.
68 private boolean mInputDispatchFrozen;
Selim Cinekf83e8242015-05-19 18:08:14 -070069
Wale Ogunwalee89eeac2016-03-12 11:07:58 -080070 // The reason the input is currently frozen or null if the input isn't frozen.
71 private String mInputFreezeReason = null;
72
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080073 // When true, input dispatch proceeds normally. Otherwise all events are dropped.
Jeff Brownc042ee22012-05-08 13:03:42 -070074 // Initially false, so that input does not get dispatched until boot is finished at
75 // which point the ActivityManager will enable dispatching.
76 private boolean mInputDispatchEnabled;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080077
78 // When true, need to call updateInputWindowsLw().
79 private boolean mUpdateInputWindowsNeeded = true;
80
Jeff Brown9302c872011-07-13 22:51:29 -070081 // Array of window handles to provide to the input dispatcher.
82 private InputWindowHandle[] mInputWindowHandles;
83 private int mInputWindowHandleCount;
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -080084 private InputWindowHandle mFocusedInputWindowHandle;
85
Wale Ogunwale6213caa2016-12-02 16:47:15 +000086 private boolean mAddInputConsumerHandle;
87 private boolean mAddPipInputConsumerHandle;
88 private boolean mAddWallpaperInputConsumerHandle;
89 private boolean mDisableWallpaperTouchEvents;
Wale Ogunwale1e129a42016-11-21 13:03:47 -080090 private final Rect mTmpRect = new Rect();
91 private final UpdateInputForAllWindowsConsumer mUpdateInputForAllWindowsConsumer =
92 new UpdateInputForAllWindowsConsumer();
Jeff Brown9302c872011-07-13 22:51:29 -070093
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080094 // Set to true when the first input device configuration change notification
95 // is received to indicate that the input devices are ready.
96 private final Object mInputDevicesReadyMonitor = new Object();
97 private boolean mInputDevicesReady;
98
Winson41275482016-10-10 15:17:45 -070099 /**
100 * The set of input consumer added to the window manager by name, which consumes input events
101 * for the windows below it.
102 */
103 private final ArrayMap<String, InputConsumerImpl> mInputConsumers = new ArrayMap();
104
105 private static final class EventReceiverInputConsumer extends InputConsumerImpl
106 implements WindowManagerPolicy.InputConsumer {
107 private InputMonitor mInputMonitor;
108 private final InputEventReceiver mInputEventReceiver;
109
110 EventReceiverInputConsumer(WindowManagerService service, InputMonitor monitor,
111 Looper looper, String name,
Winson Chung6463c362017-09-25 16:23:26 -0700112 InputEventReceiver.Factory inputEventReceiverFactory,
113 int clientPid, UserHandle clientUser) {
114 super(service, null /* token */, name, null /* inputChannel */, clientPid, clientUser);
Winson41275482016-10-10 15:17:45 -0700115 mInputMonitor = monitor;
116 mInputEventReceiver = inputEventReceiverFactory.createInputEventReceiver(
117 mClientChannel, looper);
118 }
119
120 @Override
121 public void dismiss() {
122 synchronized (mService.mWindowMap) {
123 if (mInputMonitor.destroyInputConsumer(mWindowHandle.name)) {
124 mInputEventReceiver.dispose();
125 }
126 }
127 }
128 }
129
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800130 public InputMonitor(WindowManagerService service) {
131 mService = service;
132 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700133
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700134 private void addInputConsumer(String name, InputConsumerImpl consumer) {
Winson41275482016-10-10 15:17:45 -0700135 mInputConsumers.put(name, consumer);
Winson Chung6463c362017-09-25 16:23:26 -0700136 consumer.linkToDeathRecipient();
Winson41275482016-10-10 15:17:45 -0700137 updateInputWindowsLw(true /* force */);
138 }
139
140 boolean destroyInputConsumer(String name) {
141 if (disposeInputConsumer(mInputConsumers.remove(name))) {
142 updateInputWindowsLw(true /* force */);
143 return true;
144 }
145 return false;
146 }
147
148 private boolean disposeInputConsumer(InputConsumerImpl consumer) {
149 if (consumer != null) {
150 consumer.disposeChannelsLw();
151 return true;
152 }
153 return false;
154 }
155
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700156 InputConsumerImpl getInputConsumer(String name, int displayId) {
157 // TODO(multi-display): Allow input consumers on non-default displays?
158 return (displayId == DEFAULT_DISPLAY) ? mInputConsumers.get(name) : null;
Winson41275482016-10-10 15:17:45 -0700159 }
160
161 void layoutInputConsumers(int dw, int dh) {
162 for (int i = mInputConsumers.size() - 1; i >= 0; i--) {
163 mInputConsumers.valueAt(i).layout(dw, dh);
164 }
165 }
166
167 WindowManagerPolicy.InputConsumer createInputConsumer(Looper looper, String name,
168 InputEventReceiver.Factory inputEventReceiverFactory) {
169 if (mInputConsumers.containsKey(name)) {
170 throw new IllegalStateException("Existing input consumer found with name: " + name);
171 }
172
173 final EventReceiverInputConsumer consumer = new EventReceiverInputConsumer(mService,
Winson Chung6463c362017-09-25 16:23:26 -0700174 this, looper, name, inputEventReceiverFactory, Process.myPid(),
175 UserHandle.SYSTEM);
Winson41275482016-10-10 15:17:45 -0700176 addInputConsumer(name, consumer);
177 return consumer;
178 }
179
Winson Chung6463c362017-09-25 16:23:26 -0700180 void createInputConsumer(IBinder token, String name, InputChannel inputChannel, int clientPid,
181 UserHandle clientUser) {
Winson41275482016-10-10 15:17:45 -0700182 if (mInputConsumers.containsKey(name)) {
183 throw new IllegalStateException("Existing input consumer found with name: " + name);
184 }
185
Winson Chung6463c362017-09-25 16:23:26 -0700186 final InputConsumerImpl consumer = new InputConsumerImpl(mService, token, name,
187 inputChannel, clientPid, clientUser);
Winson41275482016-10-10 15:17:45 -0700188 switch (name) {
189 case INPUT_CONSUMER_WALLPAPER:
190 consumer.mWindowHandle.hasWallpaper = true;
191 break;
192 case INPUT_CONSUMER_PIP:
193 // The touchable region of the Pip input window is cropped to the bounds of the
194 // stack, and we need FLAG_NOT_TOUCH_MODAL to ensure other events fall through
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700195 consumer.mWindowHandle.layoutParamsFlags |= FLAG_NOT_TOUCH_MODAL;
Winson41275482016-10-10 15:17:45 -0700196 break;
197 }
198 addInputConsumer(name, consumer);
199 }
200
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800201 /* Notifies the window manager about a broken input channel.
Chong Zhang8e89b312015-09-09 15:09:30 -0700202 *
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800203 * Called by the InputManager.
204 */
Craig Mautner4cd0c13f2013-04-16 15:55:52 -0700205 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800206 public void notifyInputChannelBroken(InputWindowHandle inputWindowHandle) {
207 if (inputWindowHandle == null) {
208 return;
209 }
210
211 synchronized (mService.mWindowMap) {
212 WindowState windowState = (WindowState) inputWindowHandle.windowState;
Jeff Brown9302c872011-07-13 22:51:29 -0700213 if (windowState != null) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800214 Slog.i(TAG_WM, "WINDOW DIED " + windowState);
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700215 windowState.removeIfPossible();
Jeff Brown9302c872011-07-13 22:51:29 -0700216 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800217 }
218 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700219
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800220 /* Notifies the window manager about an application that is not responding.
221 * Returns a new timeout to continue waiting in nanoseconds, or 0 to abort dispatch.
Chong Zhang8e89b312015-09-09 15:09:30 -0700222 *
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800223 * Called by the InputManager.
224 */
Craig Mautner4cd0c13f2013-04-16 15:55:52 -0700225 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800226 public long notifyANR(InputApplicationHandle inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700227 InputWindowHandle inputWindowHandle, String reason) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800228 AppWindowToken appWindowToken = null;
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700229 WindowState windowState = null;
230 boolean aboveSystem = false;
Jeff Brownee172412012-06-18 12:58:03 -0700231 synchronized (mService.mWindowMap) {
Jeff Brownee172412012-06-18 12:58:03 -0700232 if (inputWindowHandle != null) {
233 windowState = (WindowState) inputWindowHandle.windowState;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800234 if (windowState != null) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800235 appWindowToken = windowState.mAppToken;
236 }
237 }
Jeff Brownee172412012-06-18 12:58:03 -0700238 if (appWindowToken == null && inputApplicationHandle != null) {
239 appWindowToken = (AppWindowToken)inputApplicationHandle.appWindowToken;
Jeff Brown9302c872011-07-13 22:51:29 -0700240 }
Jeff Brownee172412012-06-18 12:58:03 -0700241
242 if (windowState != null) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800243 Slog.i(TAG_WM, "Input event dispatching timed out "
Jeff Brownbd181bb2013-09-10 16:44:24 -0700244 + "sending to " + windowState.mAttrs.getTitle()
245 + ". Reason: " + reason);
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700246 // Figure out whether this window is layered above system windows.
247 // We need to do this here to help the activity manager know how to
248 // layer its ANR dialog.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -0800249 int systemAlertLayer = mService.mPolicy.getWindowLayerFromTypeLw(
250 TYPE_APPLICATION_OVERLAY, windowState.mOwnerCanAddInternalSystemWindow);
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700251 aboveSystem = windowState.mBaseLayer > systemAlertLayer;
Jeff Brownee172412012-06-18 12:58:03 -0700252 } else if (appWindowToken != null) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800253 Slog.i(TAG_WM, "Input event dispatching timed out "
Jeff Brownbd181bb2013-09-10 16:44:24 -0700254 + "sending to application " + appWindowToken.stringName
255 + ". Reason: " + reason);
Jeff Brownee172412012-06-18 12:58:03 -0700256 } else {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800257 Slog.i(TAG_WM, "Input event dispatching timed out "
Jeff Brownbd181bb2013-09-10 16:44:24 -0700258 + ". Reason: " + reason);
Jeff Brownee172412012-06-18 12:58:03 -0700259 }
260
Jeff Brownbd181bb2013-09-10 16:44:24 -0700261 mService.saveANRStateLocked(appWindowToken, windowState, reason);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800262 }
263
Wale Ogunwalef1285912017-06-09 15:20:29 -0700264 // All the calls below need to happen without the WM lock held since they call into AM.
265 mService.mAmInternal.saveANRState(reason);
266
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800267 if (appWindowToken != null && appWindowToken.appToken != null) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800268 // Notify the activity manager about the timeout and let it decide whether
269 // to abort dispatching or keep waiting.
270 final AppWindowContainerController controller = appWindowToken.getController();
Wale Ogunwale7402ddf2017-03-29 12:58:24 -0700271 final boolean abort = controller != null
Brian Carlstrom7b0f2e82017-03-31 00:24:18 -0700272 && controller.keyDispatchingTimedOut(reason,
273 (windowState != null) ? windowState.mSession.mPid : -1);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800274 if (!abort) {
275 // The activity manager declined to abort dispatching.
276 // Wait a bit longer and timeout again later.
277 return appWindowToken.mInputDispatchingTimeoutNanos;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800278 }
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700279 } else if (windowState != null) {
280 try {
281 // Notify the activity manager about the timeout and let it decide whether
282 // to abort dispatching or keep waiting.
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800283 long timeout = ActivityManager.getService().inputDispatchingTimedOut(
Jeff Brownbd181bb2013-09-10 16:44:24 -0700284 windowState.mSession.mPid, aboveSystem, reason);
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700285 if (timeout >= 0) {
286 // The activity manager declined to abort dispatching.
287 // Wait a bit longer and timeout again later.
baik.handef340d2015-04-15 10:21:05 +0900288 return timeout * 1000000L; // nanoseconds
Dianne Hackborn5fe7e2a2012-10-04 11:58:16 -0700289 }
290 } catch (RemoteException ex) {
291 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800292 }
293 return 0; // abort dispatching
294 }
295
Wale Ogunwale7402ddf2017-03-29 12:58:24 -0700296 private void addInputWindowHandle(final InputWindowHandle windowHandle) {
Jeff Brown9302c872011-07-13 22:51:29 -0700297 if (mInputWindowHandles == null) {
298 mInputWindowHandles = new InputWindowHandle[16];
299 }
300 if (mInputWindowHandleCount >= mInputWindowHandles.length) {
301 mInputWindowHandles = Arrays.copyOf(mInputWindowHandles,
302 mInputWindowHandleCount * 2);
303 }
304 mInputWindowHandles[mInputWindowHandleCount++] = windowHandle;
305 }
306
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700307 void addInputWindowHandle(final InputWindowHandle inputWindowHandle,
Craig Mautnerc08eab82014-11-11 09:03:59 -0800308 final WindowState child, int flags, final int type, final boolean isVisible,
Wale Ogunwale053c8e42015-11-16 14:27:21 -0800309 final boolean hasFocus, final boolean hasWallpaper) {
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700310 // Add a window to our list of input windows.
311 inputWindowHandle.name = child.toString();
Wale Ogunwale053c8e42015-11-16 14:27:21 -0800312 flags = child.getTouchableRegion(inputWindowHandle.touchableRegion, flags);
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700313 inputWindowHandle.layoutParamsFlags = flags;
314 inputWindowHandle.layoutParamsType = type;
315 inputWindowHandle.dispatchingTimeoutNanos = child.getInputDispatchingTimeoutNanos();
316 inputWindowHandle.visible = isVisible;
317 inputWindowHandle.canReceiveKeys = child.canReceiveKeys();
318 inputWindowHandle.hasFocus = hasFocus;
319 inputWindowHandle.hasWallpaper = hasWallpaper;
320 inputWindowHandle.paused = child.mAppToken != null ? child.mAppToken.paused : false;
321 inputWindowHandle.layer = child.mLayer;
322 inputWindowHandle.ownerPid = child.mSession.mPid;
323 inputWindowHandle.ownerUid = child.mSession.mUid;
324 inputWindowHandle.inputFeatures = child.mAttrs.inputFeatures;
325
326 final Rect frame = child.mFrame;
327 inputWindowHandle.frameLeft = frame.left;
328 inputWindowHandle.frameTop = frame.top;
329 inputWindowHandle.frameRight = frame.right;
330 inputWindowHandle.frameBottom = frame.bottom;
331
332 if (child.mGlobalScale != 1) {
333 // If we are scaling the window, input coordinates need
334 // to be inversely scaled to map from what is on screen
335 // to what is actually being touched in the UI.
336 inputWindowHandle.scaleFactor = 1.0f/child.mGlobalScale;
337 } else {
338 inputWindowHandle.scaleFactor = 1;
339 }
340
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700341 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800342 Slog.d(TAG_WM, "addInputWindowHandle: "
Chong Zhangb15758a2015-11-17 12:12:03 -0800343 + child + ", " + inputWindowHandle);
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700344 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700345 addInputWindowHandle(inputWindowHandle);
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800346 if (hasFocus) {
347 mFocusedInputWindowHandle = inputWindowHandle;
348 }
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700349 }
350
Jeff Brown9302c872011-07-13 22:51:29 -0700351 private void clearInputWindowHandlesLw() {
352 while (mInputWindowHandleCount != 0) {
353 mInputWindowHandles[--mInputWindowHandleCount] = null;
354 }
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800355 mFocusedInputWindowHandle = null;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800356 }
357
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000358 void setUpdateInputWindowsNeededLw() {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800359 mUpdateInputWindowsNeeded = true;
360 }
361
362 /* Updates the cached window information provided to the input dispatcher. */
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000363 void updateInputWindowsLw(boolean force) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800364 if (!force && !mUpdateInputWindowsNeeded) {
365 return;
366 }
367 mUpdateInputWindowsNeeded = false;
368
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800369 if (false) Slog.d(TAG_WM, ">>>>>> ENTERED updateInputWindowsLw");
Jeff Brown9302c872011-07-13 22:51:29 -0700370
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800371 // Populate the input window list with information about all of the windows that
372 // could potentially receive input.
373 // As an optimization, we could try to prune the list of windows but this turns
374 // out to be difficult because only the native code knows for sure which window
375 // currently has touch focus.
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800376
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700377 // If there's a drag in flight, provide a pseudo-window to catch drag input
Daichi Hirono76a26aa2017-09-11 15:13:38 +0900378 final boolean inDrag = mService.mDragDropController.dragDropActiveLocked();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800379 if (inDrag) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800380 if (DEBUG_DRAG) {
381 Log.d(TAG_WM, "Inserting drag window");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800382 }
Daichi Hirono76a26aa2017-09-11 15:13:38 +0900383 final InputWindowHandle dragWindowHandle =
Daichi Hirono768012e2017-10-30 10:05:37 +0900384 mService.mDragDropController.getInputWindowHandleLocked();
Jeff Browncc4f7db2011-08-30 20:34:48 -0700385 if (dragWindowHandle != null) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700386 addInputWindowHandle(dragWindowHandle);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700387 } else {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800388 Slog.w(TAG_WM, "Drag is in progress but there is no "
Jeff Browncc4f7db2011-08-30 20:34:48 -0700389 + "drag window handle.");
390 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800391 }
392
Daichi Hirono34fb7312017-12-04 10:00:24 +0900393 final boolean inPositioning = mService.mTaskPositioningController.isPositioningLocked();
Chong Zhang8e89b312015-09-09 15:09:30 -0700394 if (inPositioning) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800395 if (DEBUG_TASK_POSITIONING) {
396 Log.d(TAG_WM, "Inserting window handle for repositioning");
Chong Zhang8e89b312015-09-09 15:09:30 -0700397 }
Daichi Hirono34fb7312017-12-04 10:00:24 +0900398 final InputWindowHandle dragWindowHandle =
399 mService.mTaskPositioningController.getDragWindowHandleLocked();
Chong Zhang8e89b312015-09-09 15:09:30 -0700400 if (dragWindowHandle != null) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700401 addInputWindowHandle(dragWindowHandle);
Chong Zhang8e89b312015-09-09 15:09:30 -0700402 } else {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800403 Slog.e(TAG_WM,
Chong Zhang8e89b312015-09-09 15:09:30 -0700404 "Repositioning is in progress but there is no drag window handle.");
405 }
406 }
407
Jeff Brown83d616a2012-09-09 20:33:43 -0700408 // Add all windows on the default display.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800409 mUpdateInputForAllWindowsConsumer.updateInputWindows(inDrag);
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000410
411 if (false) Slog.d(TAG_WM, "<<<<<<< EXITED updateInputWindowsLw");
412 }
413
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800414 /* Notifies that the input device configuration has changed. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700415 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800416 public void notifyConfigurationChanged() {
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700417 // TODO(multi-display): Notify proper displays that are associated with this input device.
418 mService.sendNewConfiguration(DEFAULT_DISPLAY);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800419
420 synchronized (mInputDevicesReadyMonitor) {
421 if (!mInputDevicesReady) {
422 mInputDevicesReady = true;
423 mInputDevicesReadyMonitor.notifyAll();
424 }
425 }
426 }
427
428 /* Waits until the built-in input devices have been configured. */
429 public boolean waitForInputDevicesReady(long timeoutMillis) {
430 synchronized (mInputDevicesReadyMonitor) {
431 if (!mInputDevicesReady) {
432 try {
433 mInputDevicesReadyMonitor.wait(timeoutMillis);
434 } catch (InterruptedException ex) {
435 }
436 }
437 return mInputDevicesReady;
438 }
439 }
440
441 /* Notifies that the lid switch changed state. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700442 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800443 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) {
444 mService.mPolicy.notifyLidSwitchChanged(whenNanos, lidOpen);
445 }
Craig Mautner58458122013-09-14 14:59:50 -0700446
Michael Wright3818c922014-09-02 13:59:07 -0700447 /* Notifies that the camera lens cover state has changed. */
448 @Override
449 public void notifyCameraLensCoverSwitchChanged(long whenNanos, boolean lensCovered) {
450 mService.mPolicy.notifyCameraLensCoverSwitchChanged(whenNanos, lensCovered);
451 }
452
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800453 /* Provides an opportunity for the window manager policy to intercept early key
454 * processing as soon as the key has been read from the device. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700455 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -0700456 public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
457 return mService.mPolicy.interceptKeyBeforeQueueing(event, policyFlags);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800458 }
Jeff Brown56194eb2011-03-02 19:23:13 -0800459
Michael Wright70af00a2014-09-03 19:30:20 -0700460 /* Provides an opportunity for the window manager policy to intercept early motion event
461 * processing when the device is in a non-interactive state since these events are normally
Jeff Brown56194eb2011-03-02 19:23:13 -0800462 * dropped. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700463 @Override
Michael Wright70af00a2014-09-03 19:30:20 -0700464 public int interceptMotionBeforeQueueingNonInteractive(long whenNanos, int policyFlags) {
465 return mService.mPolicy.interceptMotionBeforeQueueingNonInteractive(
466 whenNanos, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800467 }
468
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800469 /* Provides an opportunity for the window manager policy to process a key before
470 * ordinary dispatch. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700471 @Override
Jeff Brown905805a2011-10-12 13:57:59 -0700472 public long interceptKeyBeforeDispatching(
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800473 InputWindowHandle focus, KeyEvent event, int policyFlags) {
474 WindowState windowState = focus != null ? (WindowState) focus.windowState : null;
475 return mService.mPolicy.interceptKeyBeforeDispatching(windowState, event, policyFlags);
476 }
Craig Mautner58458122013-09-14 14:59:50 -0700477
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800478 /* Provides an opportunity for the window manager policy to process a key that
479 * the application did not handle. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700480 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800481 public KeyEvent dispatchUnhandledKey(
482 InputWindowHandle focus, KeyEvent event, int policyFlags) {
483 WindowState windowState = focus != null ? (WindowState) focus.windowState : null;
484 return mService.mPolicy.dispatchUnhandledKey(windowState, event, policyFlags);
485 }
Jeff Brown4532e612012-04-05 14:27:12 -0700486
487 /* Callback to get pointer layer. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700488 @Override
Jeff Brown4532e612012-04-05 14:27:12 -0700489 public int getPointerLayer() {
Wale Ogunwale5cd907d2017-01-26 14:14:08 -0800490 return mService.mPolicy.getWindowLayerFromTypeLw(WindowManager.LayoutParams.TYPE_POINTER)
Jeff Brown4532e612012-04-05 14:27:12 -0700491 * WindowManagerService.TYPE_LAYER_MULTIPLIER
492 + WindowManagerService.TYPE_LAYER_OFFSET;
493 }
494
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800495 /* Called when the current input focus changes.
496 * Layer assignment is assumed to be complete by the time this is called.
497 */
498 public void setInputFocusLw(WindowState newWindow, boolean updateInputWindows) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700499 if (DEBUG_FOCUS_LIGHT || DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800500 Slog.d(TAG_WM, "Input focus has changed to " + newWindow);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800501 }
502
503 if (newWindow != mInputFocus) {
504 if (newWindow != null && newWindow.canReceiveKeys()) {
505 // Displaying a window implicitly causes dispatching to be unpaused.
506 // This is to protect against bugs if someone pauses dispatching but
507 // forgets to resume.
508 newWindow.mToken.paused = false;
509 }
510
511 mInputFocus = newWindow;
512 setUpdateInputWindowsNeededLw();
513
514 if (updateInputWindows) {
515 updateInputWindowsLw(false /*force*/);
516 }
517 }
518 }
Craig Mautner58458122013-09-14 14:59:50 -0700519
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800520 public void setFocusedAppLw(AppWindowToken newApp) {
521 // Focused app has changed.
522 if (newApp == null) {
523 mService.mInputManager.setFocusedApplication(null);
524 } else {
Jeff Brown9302c872011-07-13 22:51:29 -0700525 final InputApplicationHandle handle = newApp.mInputApplicationHandle;
526 handle.name = newApp.toString();
Wale Ogunwale72919d22016-12-08 18:58:50 -0800527 handle.dispatchingTimeoutNanos = newApp.mInputDispatchingTimeoutNanos;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800528
Jeff Brown9302c872011-07-13 22:51:29 -0700529 mService.mInputManager.setFocusedApplication(handle);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800530 }
531 }
Craig Mautner58458122013-09-14 14:59:50 -0700532
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800533 public void pauseDispatchingLw(WindowToken window) {
534 if (! window.paused) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700535 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800536 Slog.v(TAG_WM, "Pausing WindowToken " + window);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800537 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700538
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800539 window.paused = true;
540 updateInputWindowsLw(true /*force*/);
541 }
542 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700543
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800544 public void resumeDispatchingLw(WindowToken window) {
545 if (window.paused) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700546 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800547 Slog.v(TAG_WM, "Resuming WindowToken " + window);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800548 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700549
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800550 window.paused = false;
551 updateInputWindowsLw(true /*force*/);
552 }
553 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700554
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800555 public void freezeInputDispatchingLw() {
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800556 if (!mInputDispatchFrozen) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700557 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800558 Slog.v(TAG_WM, "Freezing input dispatching");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800559 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700560
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800561 mInputDispatchFrozen = true;
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800562
563 if (DEBUG_INPUT || true) {
564 mInputFreezeReason = Debug.getCallers(6);
565 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800566 updateInputDispatchModeLw();
567 }
568 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700569
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800570 public void thawInputDispatchingLw() {
571 if (mInputDispatchFrozen) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700572 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800573 Slog.v(TAG_WM, "Thawing input dispatching");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800574 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700575
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800576 mInputDispatchFrozen = false;
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800577 mInputFreezeReason = null;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800578 updateInputDispatchModeLw();
579 }
580 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700581
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800582 public void setEventDispatchingLw(boolean enabled) {
583 if (mInputDispatchEnabled != enabled) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700584 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800585 Slog.v(TAG_WM, "Setting event dispatching to " + enabled);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800586 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700587
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800588 mInputDispatchEnabled = enabled;
589 updateInputDispatchModeLw();
590 }
591 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700592
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800593 private void updateInputDispatchModeLw() {
594 mService.mInputManager.setInputDispatchMode(mInputDispatchEnabled, mInputDispatchFrozen);
595 }
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800596
597 void dump(PrintWriter pw, String prefix) {
598 if (mInputFreezeReason != null) {
599 pw.println(prefix + "mInputFreezeReason=" + mInputFreezeReason);
600 }
Winson Chung853c99a2017-03-21 22:16:42 -0700601 final Set<String> inputConsumerKeys = mInputConsumers.keySet();
602 if (!inputConsumerKeys.isEmpty()) {
603 pw.println(prefix + "InputConsumers:");
604 for (String key : inputConsumerKeys) {
Winson Chung6463c362017-09-25 16:23:26 -0700605 mInputConsumers.get(key).dump(pw, key, prefix);
Winson Chung853c99a2017-03-21 22:16:42 -0700606 }
607 }
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800608 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800609
610 private final class UpdateInputForAllWindowsConsumer implements Consumer<WindowState> {
611
612 InputConsumerImpl navInputConsumer;
613 InputConsumerImpl pipInputConsumer;
614 InputConsumerImpl wallpaperInputConsumer;
615 Rect pipTouchableBounds;
616 boolean inDrag;
617 WallpaperController wallpaperController;
618
619 private void updateInputWindows(boolean inDrag) {
620
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800621 // TODO: multi-display
622 navInputConsumer = getInputConsumer(INPUT_CONSUMER_NAVIGATION, DEFAULT_DISPLAY);
623 pipInputConsumer = getInputConsumer(INPUT_CONSUMER_PIP, DEFAULT_DISPLAY);
624 wallpaperInputConsumer = getInputConsumer(INPUT_CONSUMER_WALLPAPER, DEFAULT_DISPLAY);
625 mAddInputConsumerHandle = navInputConsumer != null;
626 mAddPipInputConsumerHandle = pipInputConsumer != null;
627 mAddWallpaperInputConsumerHandle = wallpaperInputConsumer != null;
628 mTmpRect.setEmpty();
629 pipTouchableBounds = mAddPipInputConsumerHandle ? mTmpRect : null;
630 mDisableWallpaperTouchEvents = false;
631 this.inDrag = inDrag;
632 wallpaperController = mService.mRoot.mWallpaperController;
633
634 mService.mRoot.forAllWindows(this, true /* traverseTopToBottom */);
635 if (mAddWallpaperInputConsumerHandle) {
636 // No visible wallpaper found, add the wallpaper input consumer at the end.
637 addInputWindowHandle(wallpaperInputConsumer.mWindowHandle);
638 }
639
640 // Send windows to native code.
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800641 mService.mInputManager.setInputWindows(mInputWindowHandles, mFocusedInputWindowHandle);
Vladislav Kaznacheev2e96c632016-12-13 14:31:24 -0800642
643 clearInputWindowHandlesLw();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800644 }
645
646 @Override
647 public void accept(WindowState w) {
648 final InputChannel inputChannel = w.mInputChannel;
649 final InputWindowHandle inputWindowHandle = w.mInputWindowHandle;
650 if (inputChannel == null || inputWindowHandle == null || w.mRemoved
Matthew Nge15352e2016-12-20 15:36:29 -0800651 || w.canReceiveTouchInput()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800652 // Skip this window because it cannot possibly receive input.
653 return;
654 }
655
Winson Chung61ecc1b2017-02-17 10:46:17 -0800656 final int flags = w.mAttrs.flags;
657 final int privateFlags = w.mAttrs.privateFlags;
658 final int type = w.mAttrs.type;
659 final boolean hasFocus = w == mInputFocus;
660 final boolean isVisible = w.isVisibleLw();
661
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700662 if (w.inPinnedWindowingMode()) {
Winson Chung61ecc1b2017-02-17 10:46:17 -0800663 if (mAddPipInputConsumerHandle
664 && (inputWindowHandle.layer <= pipInputConsumer.mWindowHandle.layer)) {
Bryce Leef3c6a472017-11-14 14:53:06 -0800665 // Update the bounds of the Pip input consumer to match the window bounds.
666 w.getBounds(pipTouchableBounds);
Winson Chung61ecc1b2017-02-17 10:46:17 -0800667 pipInputConsumer.mWindowHandle.touchableRegion.set(pipTouchableBounds);
668 addInputWindowHandle(pipInputConsumer.mWindowHandle);
669 mAddPipInputConsumerHandle = false;
670 }
671 // TODO: Fix w.canReceiveTouchInput() to handle this case
672 if (!hasFocus) {
673 // Skip this pinned stack window if it does not have focus
674 return;
675 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800676 }
677
678 if (mAddInputConsumerHandle
679 && inputWindowHandle.layer <= navInputConsumer.mWindowHandle.layer) {
680 addInputWindowHandle(navInputConsumer.mWindowHandle);
681 mAddInputConsumerHandle = false;
682 }
683
684 if (mAddWallpaperInputConsumerHandle) {
685 if (w.mAttrs.type == TYPE_WALLPAPER && w.isVisibleLw()) {
686 // Add the wallpaper input consumer above the first visible wallpaper.
687 addInputWindowHandle(wallpaperInputConsumer.mWindowHandle);
688 mAddWallpaperInputConsumerHandle = false;
689 }
690 }
691
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800692 if ((privateFlags & PRIVATE_FLAG_DISABLE_WALLPAPER_TOUCH_EVENTS) != 0) {
693 mDisableWallpaperTouchEvents = true;
694 }
695 final boolean hasWallpaper = wallpaperController.isWallpaperTarget(w)
696 && (privateFlags & PRIVATE_FLAG_KEYGUARD) == 0
697 && !mDisableWallpaperTouchEvents;
698
699 // If there's a drag in progress and 'child' is a potential drop target,
700 // make sure it's been told about the drag
701 if (inDrag && isVisible && w.getDisplayContent().isDefaultDisplay) {
Daichi Hirono768012e2017-10-30 10:05:37 +0900702 mService.mDragDropController.sendDragStartedIfNeededLocked(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800703 }
704
705 addInputWindowHandle(
706 inputWindowHandle, w, flags, type, isVisible, hasFocus, hasWallpaper);
707 }
708 }
Jeff Brownea426552011-07-18 16:53:48 -0700709}