blob: 7e29a3aa811e326eae32b57e64e681b92627d361 [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
Chong Zhang8e89b312015-09-09 15:09:30 -0700393 final boolean inPositioning = (mService.mTaskPositioner != null);
394 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 }
398 final InputWindowHandle dragWindowHandle = mService.mTaskPositioner.mDragWindowHandle;
399 if (dragWindowHandle != null) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700400 addInputWindowHandle(dragWindowHandle);
Chong Zhang8e89b312015-09-09 15:09:30 -0700401 } else {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800402 Slog.e(TAG_WM,
Chong Zhang8e89b312015-09-09 15:09:30 -0700403 "Repositioning is in progress but there is no drag window handle.");
404 }
405 }
406
Jeff Brown83d616a2012-09-09 20:33:43 -0700407 // Add all windows on the default display.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800408 mUpdateInputForAllWindowsConsumer.updateInputWindows(inDrag);
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000409
410 if (false) Slog.d(TAG_WM, "<<<<<<< EXITED updateInputWindowsLw");
411 }
412
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800413 /* Notifies that the input device configuration has changed. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700414 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800415 public void notifyConfigurationChanged() {
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700416 // TODO(multi-display): Notify proper displays that are associated with this input device.
417 mService.sendNewConfiguration(DEFAULT_DISPLAY);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800418
419 synchronized (mInputDevicesReadyMonitor) {
420 if (!mInputDevicesReady) {
421 mInputDevicesReady = true;
422 mInputDevicesReadyMonitor.notifyAll();
423 }
424 }
425 }
426
427 /* Waits until the built-in input devices have been configured. */
428 public boolean waitForInputDevicesReady(long timeoutMillis) {
429 synchronized (mInputDevicesReadyMonitor) {
430 if (!mInputDevicesReady) {
431 try {
432 mInputDevicesReadyMonitor.wait(timeoutMillis);
433 } catch (InterruptedException ex) {
434 }
435 }
436 return mInputDevicesReady;
437 }
438 }
439
440 /* Notifies that the lid switch changed state. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700441 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800442 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) {
443 mService.mPolicy.notifyLidSwitchChanged(whenNanos, lidOpen);
444 }
Craig Mautner58458122013-09-14 14:59:50 -0700445
Michael Wright3818c922014-09-02 13:59:07 -0700446 /* Notifies that the camera lens cover state has changed. */
447 @Override
448 public void notifyCameraLensCoverSwitchChanged(long whenNanos, boolean lensCovered) {
449 mService.mPolicy.notifyCameraLensCoverSwitchChanged(whenNanos, lensCovered);
450 }
451
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800452 /* Provides an opportunity for the window manager policy to intercept early key
453 * processing as soon as the key has been read from the device. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700454 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -0700455 public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
456 return mService.mPolicy.interceptKeyBeforeQueueing(event, policyFlags);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800457 }
Jeff Brown56194eb2011-03-02 19:23:13 -0800458
Michael Wright70af00a2014-09-03 19:30:20 -0700459 /* Provides an opportunity for the window manager policy to intercept early motion event
460 * processing when the device is in a non-interactive state since these events are normally
Jeff Brown56194eb2011-03-02 19:23:13 -0800461 * dropped. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700462 @Override
Michael Wright70af00a2014-09-03 19:30:20 -0700463 public int interceptMotionBeforeQueueingNonInteractive(long whenNanos, int policyFlags) {
464 return mService.mPolicy.interceptMotionBeforeQueueingNonInteractive(
465 whenNanos, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800466 }
467
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800468 /* Provides an opportunity for the window manager policy to process a key before
469 * ordinary dispatch. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700470 @Override
Jeff Brown905805a2011-10-12 13:57:59 -0700471 public long interceptKeyBeforeDispatching(
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800472 InputWindowHandle focus, KeyEvent event, int policyFlags) {
473 WindowState windowState = focus != null ? (WindowState) focus.windowState : null;
474 return mService.mPolicy.interceptKeyBeforeDispatching(windowState, event, policyFlags);
475 }
Craig Mautner58458122013-09-14 14:59:50 -0700476
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800477 /* Provides an opportunity for the window manager policy to process a key that
478 * the application did not handle. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700479 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800480 public KeyEvent dispatchUnhandledKey(
481 InputWindowHandle focus, KeyEvent event, int policyFlags) {
482 WindowState windowState = focus != null ? (WindowState) focus.windowState : null;
483 return mService.mPolicy.dispatchUnhandledKey(windowState, event, policyFlags);
484 }
Jeff Brown4532e612012-04-05 14:27:12 -0700485
486 /* Callback to get pointer layer. */
Jeff Brown0b31d812013-08-22 19:41:29 -0700487 @Override
Jeff Brown4532e612012-04-05 14:27:12 -0700488 public int getPointerLayer() {
Wale Ogunwale5cd907d2017-01-26 14:14:08 -0800489 return mService.mPolicy.getWindowLayerFromTypeLw(WindowManager.LayoutParams.TYPE_POINTER)
Jeff Brown4532e612012-04-05 14:27:12 -0700490 * WindowManagerService.TYPE_LAYER_MULTIPLIER
491 + WindowManagerService.TYPE_LAYER_OFFSET;
492 }
493
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800494 /* Called when the current input focus changes.
495 * Layer assignment is assumed to be complete by the time this is called.
496 */
497 public void setInputFocusLw(WindowState newWindow, boolean updateInputWindows) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700498 if (DEBUG_FOCUS_LIGHT || DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800499 Slog.d(TAG_WM, "Input focus has changed to " + newWindow);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800500 }
501
502 if (newWindow != mInputFocus) {
503 if (newWindow != null && newWindow.canReceiveKeys()) {
504 // Displaying a window implicitly causes dispatching to be unpaused.
505 // This is to protect against bugs if someone pauses dispatching but
506 // forgets to resume.
507 newWindow.mToken.paused = false;
508 }
509
510 mInputFocus = newWindow;
511 setUpdateInputWindowsNeededLw();
512
513 if (updateInputWindows) {
514 updateInputWindowsLw(false /*force*/);
515 }
516 }
517 }
Craig Mautner58458122013-09-14 14:59:50 -0700518
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800519 public void setFocusedAppLw(AppWindowToken newApp) {
520 // Focused app has changed.
521 if (newApp == null) {
522 mService.mInputManager.setFocusedApplication(null);
523 } else {
Jeff Brown9302c872011-07-13 22:51:29 -0700524 final InputApplicationHandle handle = newApp.mInputApplicationHandle;
525 handle.name = newApp.toString();
Wale Ogunwale72919d22016-12-08 18:58:50 -0800526 handle.dispatchingTimeoutNanos = newApp.mInputDispatchingTimeoutNanos;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800527
Jeff Brown9302c872011-07-13 22:51:29 -0700528 mService.mInputManager.setFocusedApplication(handle);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800529 }
530 }
Craig Mautner58458122013-09-14 14:59:50 -0700531
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800532 public void pauseDispatchingLw(WindowToken window) {
533 if (! window.paused) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700534 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800535 Slog.v(TAG_WM, "Pausing WindowToken " + window);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800536 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700537
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800538 window.paused = true;
539 updateInputWindowsLw(true /*force*/);
540 }
541 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700542
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800543 public void resumeDispatchingLw(WindowToken window) {
544 if (window.paused) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700545 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800546 Slog.v(TAG_WM, "Resuming WindowToken " + window);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800547 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700548
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800549 window.paused = false;
550 updateInputWindowsLw(true /*force*/);
551 }
552 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700553
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800554 public void freezeInputDispatchingLw() {
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800555 if (!mInputDispatchFrozen) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700556 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800557 Slog.v(TAG_WM, "Freezing input dispatching");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800558 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700559
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800560 mInputDispatchFrozen = true;
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800561
562 if (DEBUG_INPUT || true) {
563 mInputFreezeReason = Debug.getCallers(6);
564 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800565 updateInputDispatchModeLw();
566 }
567 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700568
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800569 public void thawInputDispatchingLw() {
570 if (mInputDispatchFrozen) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700571 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800572 Slog.v(TAG_WM, "Thawing input dispatching");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800573 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700574
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800575 mInputDispatchFrozen = false;
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800576 mInputFreezeReason = null;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800577 updateInputDispatchModeLw();
578 }
579 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700580
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800581 public void setEventDispatchingLw(boolean enabled) {
582 if (mInputDispatchEnabled != enabled) {
Filip Gruszczynskif8a2a632015-10-28 11:18:02 -0700583 if (DEBUG_INPUT) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800584 Slog.v(TAG_WM, "Setting event dispatching to " + enabled);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800585 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700586
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800587 mInputDispatchEnabled = enabled;
588 updateInputDispatchModeLw();
589 }
590 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700591
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800592 private void updateInputDispatchModeLw() {
593 mService.mInputManager.setInputDispatchMode(mInputDispatchEnabled, mInputDispatchFrozen);
594 }
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800595
596 void dump(PrintWriter pw, String prefix) {
597 if (mInputFreezeReason != null) {
598 pw.println(prefix + "mInputFreezeReason=" + mInputFreezeReason);
599 }
Winson Chung853c99a2017-03-21 22:16:42 -0700600 final Set<String> inputConsumerKeys = mInputConsumers.keySet();
601 if (!inputConsumerKeys.isEmpty()) {
602 pw.println(prefix + "InputConsumers:");
603 for (String key : inputConsumerKeys) {
Winson Chung6463c362017-09-25 16:23:26 -0700604 mInputConsumers.get(key).dump(pw, key, prefix);
Winson Chung853c99a2017-03-21 22:16:42 -0700605 }
606 }
Wale Ogunwalee89eeac2016-03-12 11:07:58 -0800607 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800608
609 private final class UpdateInputForAllWindowsConsumer implements Consumer<WindowState> {
610
611 InputConsumerImpl navInputConsumer;
612 InputConsumerImpl pipInputConsumer;
613 InputConsumerImpl wallpaperInputConsumer;
614 Rect pipTouchableBounds;
615 boolean inDrag;
616 WallpaperController wallpaperController;
617
618 private void updateInputWindows(boolean inDrag) {
619
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800620 // TODO: multi-display
621 navInputConsumer = getInputConsumer(INPUT_CONSUMER_NAVIGATION, DEFAULT_DISPLAY);
622 pipInputConsumer = getInputConsumer(INPUT_CONSUMER_PIP, DEFAULT_DISPLAY);
623 wallpaperInputConsumer = getInputConsumer(INPUT_CONSUMER_WALLPAPER, DEFAULT_DISPLAY);
624 mAddInputConsumerHandle = navInputConsumer != null;
625 mAddPipInputConsumerHandle = pipInputConsumer != null;
626 mAddWallpaperInputConsumerHandle = wallpaperInputConsumer != null;
627 mTmpRect.setEmpty();
628 pipTouchableBounds = mAddPipInputConsumerHandle ? mTmpRect : null;
629 mDisableWallpaperTouchEvents = false;
630 this.inDrag = inDrag;
631 wallpaperController = mService.mRoot.mWallpaperController;
632
633 mService.mRoot.forAllWindows(this, true /* traverseTopToBottom */);
634 if (mAddWallpaperInputConsumerHandle) {
635 // No visible wallpaper found, add the wallpaper input consumer at the end.
636 addInputWindowHandle(wallpaperInputConsumer.mWindowHandle);
637 }
638
639 // Send windows to native code.
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800640 mService.mInputManager.setInputWindows(mInputWindowHandles, mFocusedInputWindowHandle);
Vladislav Kaznacheev2e96c632016-12-13 14:31:24 -0800641
642 clearInputWindowHandlesLw();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800643 }
644
645 @Override
646 public void accept(WindowState w) {
647 final InputChannel inputChannel = w.mInputChannel;
648 final InputWindowHandle inputWindowHandle = w.mInputWindowHandle;
649 if (inputChannel == null || inputWindowHandle == null || w.mRemoved
Matthew Nge15352e2016-12-20 15:36:29 -0800650 || w.canReceiveTouchInput()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800651 // Skip this window because it cannot possibly receive input.
652 return;
653 }
654
Winson Chung61ecc1b2017-02-17 10:46:17 -0800655 final int flags = w.mAttrs.flags;
656 final int privateFlags = w.mAttrs.privateFlags;
657 final int type = w.mAttrs.type;
658 final boolean hasFocus = w == mInputFocus;
659 final boolean isVisible = w.isVisibleLw();
660
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700661 if (w.inPinnedWindowingMode()) {
Winson Chung61ecc1b2017-02-17 10:46:17 -0800662 if (mAddPipInputConsumerHandle
663 && (inputWindowHandle.layer <= pipInputConsumer.mWindowHandle.layer)) {
Bryce Leef3c6a472017-11-14 14:53:06 -0800664 // Update the bounds of the Pip input consumer to match the window bounds.
665 w.getBounds(pipTouchableBounds);
Winson Chung61ecc1b2017-02-17 10:46:17 -0800666 pipInputConsumer.mWindowHandle.touchableRegion.set(pipTouchableBounds);
667 addInputWindowHandle(pipInputConsumer.mWindowHandle);
668 mAddPipInputConsumerHandle = false;
669 }
670 // TODO: Fix w.canReceiveTouchInput() to handle this case
671 if (!hasFocus) {
672 // Skip this pinned stack window if it does not have focus
673 return;
674 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800675 }
676
677 if (mAddInputConsumerHandle
678 && inputWindowHandle.layer <= navInputConsumer.mWindowHandle.layer) {
679 addInputWindowHandle(navInputConsumer.mWindowHandle);
680 mAddInputConsumerHandle = false;
681 }
682
683 if (mAddWallpaperInputConsumerHandle) {
684 if (w.mAttrs.type == TYPE_WALLPAPER && w.isVisibleLw()) {
685 // Add the wallpaper input consumer above the first visible wallpaper.
686 addInputWindowHandle(wallpaperInputConsumer.mWindowHandle);
687 mAddWallpaperInputConsumerHandle = false;
688 }
689 }
690
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800691 if ((privateFlags & PRIVATE_FLAG_DISABLE_WALLPAPER_TOUCH_EVENTS) != 0) {
692 mDisableWallpaperTouchEvents = true;
693 }
694 final boolean hasWallpaper = wallpaperController.isWallpaperTarget(w)
695 && (privateFlags & PRIVATE_FLAG_KEYGUARD) == 0
696 && !mDisableWallpaperTouchEvents;
697
698 // If there's a drag in progress and 'child' is a potential drop target,
699 // make sure it's been told about the drag
700 if (inDrag && isVisible && w.getDisplayContent().isDefaultDisplay) {
Daichi Hirono768012e2017-10-30 10:05:37 +0900701 mService.mDragDropController.sendDragStartedIfNeededLocked(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800702 }
703
704 addInputWindowHandle(
705 inputWindowHandle, w, flags, type, isVisible, hasFocus, hasWallpaper);
706 }
707 }
Jeff Brownea426552011-07-18 16:53:48 -0700708}