blob: 61310ca19a35efc14eee5208b32f05fffa371297 [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
Jeff Brown4532e612012-04-05 14:27:12 -070019import com.android.server.input.InputManagerService;
20import com.android.server.input.InputApplicationHandle;
21import com.android.server.input.InputWindowHandle;
Jeff Brown83d616a2012-09-09 20:33:43 -070022import com.android.server.wm.WindowManagerService.AllWindowsIterator;
Jeff Brown4532e612012-04-05 14:27:12 -070023
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080024import android.graphics.Rect;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080025import android.os.RemoteException;
26import android.util.Log;
27import android.util.Slog;
Jeff Brown83d616a2012-09-09 20:33:43 -070028import android.view.Display;
Jeff Browncc4f7db2011-08-30 20:34:48 -070029import android.view.InputChannel;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080030import android.view.KeyEvent;
31import android.view.WindowManager;
32
33import java.util.ArrayList;
Jeff Brown9302c872011-07-13 22:51:29 -070034import java.util.Arrays;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080035
Jeff Browna9d131c2012-09-20 16:48:17 -070036final class InputMonitor implements InputManagerService.WindowManagerCallbacks {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080037 private final WindowManagerService mService;
38
39 // Current window with input focus for keys and other non-touch events. May be null.
40 private WindowState mInputFocus;
41
42 // When true, prevents input dispatch from proceeding until set to false again.
43 private boolean mInputDispatchFrozen;
44
45 // When true, input dispatch proceeds normally. Otherwise all events are dropped.
Jeff Brownc042ee22012-05-08 13:03:42 -070046 // Initially false, so that input does not get dispatched until boot is finished at
47 // which point the ActivityManager will enable dispatching.
48 private boolean mInputDispatchEnabled;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080049
50 // When true, need to call updateInputWindowsLw().
51 private boolean mUpdateInputWindowsNeeded = true;
52
Jeff Brown9302c872011-07-13 22:51:29 -070053 // Array of window handles to provide to the input dispatcher.
54 private InputWindowHandle[] mInputWindowHandles;
55 private int mInputWindowHandleCount;
56
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080057 // Set to true when the first input device configuration change notification
58 // is received to indicate that the input devices are ready.
59 private final Object mInputDevicesReadyMonitor = new Object();
60 private boolean mInputDevicesReady;
61
62 public InputMonitor(WindowManagerService service) {
63 mService = service;
64 }
65
66 /* Notifies the window manager about a broken input channel.
67 *
68 * Called by the InputManager.
69 */
70 public void notifyInputChannelBroken(InputWindowHandle inputWindowHandle) {
71 if (inputWindowHandle == null) {
72 return;
73 }
74
75 synchronized (mService.mWindowMap) {
76 WindowState windowState = (WindowState) inputWindowHandle.windowState;
Jeff Brown9302c872011-07-13 22:51:29 -070077 if (windowState != null) {
78 Slog.i(WindowManagerService.TAG, "WINDOW DIED " + windowState);
79 mService.removeWindowLocked(windowState.mSession, windowState);
80 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080081 }
82 }
83
84 /* Notifies the window manager about an application that is not responding.
85 * Returns a new timeout to continue waiting in nanoseconds, or 0 to abort dispatch.
86 *
87 * Called by the InputManager.
88 */
89 public long notifyANR(InputApplicationHandle inputApplicationHandle,
90 InputWindowHandle inputWindowHandle) {
91 AppWindowToken appWindowToken = null;
Jeff Brownee172412012-06-18 12:58:03 -070092 synchronized (mService.mWindowMap) {
93 WindowState windowState = null;
94 if (inputWindowHandle != null) {
95 windowState = (WindowState) inputWindowHandle.windowState;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080096 if (windowState != null) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080097 appWindowToken = windowState.mAppToken;
98 }
99 }
Jeff Brownee172412012-06-18 12:58:03 -0700100 if (appWindowToken == null && inputApplicationHandle != null) {
101 appWindowToken = (AppWindowToken)inputApplicationHandle.appWindowToken;
Jeff Brown9302c872011-07-13 22:51:29 -0700102 }
Jeff Brownee172412012-06-18 12:58:03 -0700103
104 if (windowState != null) {
105 Slog.i(WindowManagerService.TAG, "Input event dispatching timed out "
106 + "sending to " + windowState.mAttrs.getTitle());
107 } else if (appWindowToken != null) {
108 Slog.i(WindowManagerService.TAG, "Input event dispatching timed out "
109 + "sending to application " + appWindowToken.stringName);
110 } else {
111 Slog.i(WindowManagerService.TAG, "Input event dispatching timed out.");
112 }
113
114 mService.saveANRStateLocked(appWindowToken, windowState);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800115 }
116
117 if (appWindowToken != null && appWindowToken.appToken != null) {
118 try {
119 // Notify the activity manager about the timeout and let it decide whether
120 // to abort dispatching or keep waiting.
121 boolean abort = appWindowToken.appToken.keyDispatchingTimedOut();
122 if (! abort) {
123 // The activity manager declined to abort dispatching.
124 // Wait a bit longer and timeout again later.
125 return appWindowToken.inputDispatchingTimeoutNanos;
126 }
127 } catch (RemoteException ex) {
128 }
129 }
130 return 0; // abort dispatching
131 }
132
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700133 private void addInputWindowHandleLw(final InputWindowHandle windowHandle) {
Jeff Brown9302c872011-07-13 22:51:29 -0700134 if (mInputWindowHandles == null) {
135 mInputWindowHandles = new InputWindowHandle[16];
136 }
137 if (mInputWindowHandleCount >= mInputWindowHandles.length) {
138 mInputWindowHandles = Arrays.copyOf(mInputWindowHandles,
139 mInputWindowHandleCount * 2);
140 }
141 mInputWindowHandles[mInputWindowHandleCount++] = windowHandle;
142 }
143
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700144 private void addInputWindowHandleLw(final InputWindowHandle inputWindowHandle,
145 final WindowState child, final int flags, final int type,
146 final boolean isVisible, final boolean hasFocus, final boolean hasWallpaper) {
147 // Add a window to our list of input windows.
148 inputWindowHandle.name = child.toString();
149 inputWindowHandle.layoutParamsFlags = flags;
150 inputWindowHandle.layoutParamsType = type;
151 inputWindowHandle.dispatchingTimeoutNanos = child.getInputDispatchingTimeoutNanos();
152 inputWindowHandle.visible = isVisible;
153 inputWindowHandle.canReceiveKeys = child.canReceiveKeys();
154 inputWindowHandle.hasFocus = hasFocus;
155 inputWindowHandle.hasWallpaper = hasWallpaper;
156 inputWindowHandle.paused = child.mAppToken != null ? child.mAppToken.paused : false;
157 inputWindowHandle.layer = child.mLayer;
158 inputWindowHandle.ownerPid = child.mSession.mPid;
159 inputWindowHandle.ownerUid = child.mSession.mUid;
160 inputWindowHandle.inputFeatures = child.mAttrs.inputFeatures;
161
162 final Rect frame = child.mFrame;
163 inputWindowHandle.frameLeft = frame.left;
164 inputWindowHandle.frameTop = frame.top;
165 inputWindowHandle.frameRight = frame.right;
166 inputWindowHandle.frameBottom = frame.bottom;
167
168 if (child.mGlobalScale != 1) {
169 // If we are scaling the window, input coordinates need
170 // to be inversely scaled to map from what is on screen
171 // to what is actually being touched in the UI.
172 inputWindowHandle.scaleFactor = 1.0f/child.mGlobalScale;
173 } else {
174 inputWindowHandle.scaleFactor = 1;
175 }
176
177 child.getTouchableRegion(inputWindowHandle.touchableRegion);
178
179 addInputWindowHandleLw(inputWindowHandle);
180 }
181
Jeff Brown9302c872011-07-13 22:51:29 -0700182 private void clearInputWindowHandlesLw() {
183 while (mInputWindowHandleCount != 0) {
184 mInputWindowHandles[--mInputWindowHandleCount] = null;
185 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800186 }
187
188 public void setUpdateInputWindowsNeededLw() {
189 mUpdateInputWindowsNeeded = true;
190 }
191
192 /* Updates the cached window information provided to the input dispatcher. */
193 public void updateInputWindowsLw(boolean force) {
194 if (!force && !mUpdateInputWindowsNeeded) {
195 return;
196 }
197 mUpdateInputWindowsNeeded = false;
198
Dianne Hackbornb961cd22011-06-21 12:13:37 -0700199 if (false) Slog.d(WindowManagerService.TAG, ">>>>>> ENTERED updateInputWindowsLw");
Jeff Brown9302c872011-07-13 22:51:29 -0700200
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800201 // Populate the input window list with information about all of the windows that
202 // could potentially receive input.
203 // As an optimization, we could try to prune the list of windows but this turns
204 // out to be difficult because only the native code knows for sure which window
205 // currently has touch focus.
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700206 final WindowStateAnimator universeBackground = mService.mAnimator.mUniverseBackground;
207 final int aboveUniverseLayer = mService.mAnimator.mAboveUniverseLayer;
208 boolean addedUniverse = false;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800209
210 // If there's a drag in flight, provide a pseudowindow to catch drag input
211 final boolean inDrag = (mService.mDragState != null);
212 if (inDrag) {
213 if (WindowManagerService.DEBUG_DRAG) {
214 Log.d(WindowManagerService.TAG, "Inserting drag window");
215 }
Jeff Browncc4f7db2011-08-30 20:34:48 -0700216 final InputWindowHandle dragWindowHandle = mService.mDragState.mDragWindowHandle;
217 if (dragWindowHandle != null) {
218 addInputWindowHandleLw(dragWindowHandle);
219 } else {
220 Slog.w(WindowManagerService.TAG, "Drag is in progress but there is no "
221 + "drag window handle.");
222 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800223 }
224
Dianne Hackborndf89e652011-10-06 22:35:11 -0700225 final int NFW = mService.mFakeWindows.size();
226 for (int i = 0; i < NFW; i++) {
227 addInputWindowHandleLw(mService.mFakeWindows.get(i).mWindowHandle);
228 }
229
Jeff Brown83d616a2012-09-09 20:33:43 -0700230 // Add all windows on the default display.
231 final AllWindowsIterator iterator = mService.new AllWindowsIterator(
232 WindowManagerService.REVERSE_ITERATOR);
233 while (iterator.hasNext()) {
234 final WindowState child = iterator.next();
Jeff Browncc4f7db2011-08-30 20:34:48 -0700235 final InputChannel inputChannel = child.mInputChannel;
236 final InputWindowHandle inputWindowHandle = child.mInputWindowHandle;
237 if (inputChannel == null || inputWindowHandle == null || child.mRemoved) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800238 // Skip this window because it cannot possibly receive input.
239 continue;
240 }
241
242 final int flags = child.mAttrs.flags;
243 final int type = child.mAttrs.type;
244
245 final boolean hasFocus = (child == mInputFocus);
246 final boolean isVisible = child.isVisibleLw();
247 final boolean hasWallpaper = (child == mService.mWallpaperTarget)
248 && (type != WindowManager.LayoutParams.TYPE_KEYGUARD);
Jeff Brown83d616a2012-09-09 20:33:43 -0700249 final boolean onDefaultDisplay = (child.getDisplayId() == Display.DEFAULT_DISPLAY);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800250
251 // If there's a drag in progress and 'child' is a potential drop target,
252 // make sure it's been told about the drag
Jeff Brown83d616a2012-09-09 20:33:43 -0700253 if (inDrag && isVisible && onDefaultDisplay) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800254 mService.mDragState.sendDragStartedIfNeededLw(child);
255 }
256
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700257 if (universeBackground != null && !addedUniverse
Jeff Brown83d616a2012-09-09 20:33:43 -0700258 && child.mBaseLayer < aboveUniverseLayer && onDefaultDisplay) {
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700259 final WindowState u = universeBackground.mWin;
260 if (u.mInputChannel != null && u.mInputWindowHandle != null) {
261 addInputWindowHandleLw(u.mInputWindowHandle, u, u.mAttrs.flags,
262 u.mAttrs.type, true, u == mInputFocus, false);
263 }
264 addedUniverse = true;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400265 }
266
Dianne Hackborna4b7f2f2012-05-21 11:28:41 -0700267 if (child.mWinAnimator != universeBackground) {
268 addInputWindowHandleLw(inputWindowHandle, child, flags, type,
269 isVisible, hasFocus, hasWallpaper);
270 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800271 }
272
273 // Send windows to native code.
Jeff Brown9302c872011-07-13 22:51:29 -0700274 mService.mInputManager.setInputWindows(mInputWindowHandles);
275
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800276 // Clear the list in preparation for the next round.
Jeff Brown9302c872011-07-13 22:51:29 -0700277 clearInputWindowHandlesLw();
278
Dianne Hackbornb961cd22011-06-21 12:13:37 -0700279 if (false) Slog.d(WindowManagerService.TAG, "<<<<<<< EXITED updateInputWindowsLw");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800280 }
281
282 /* Notifies that the input device configuration has changed. */
283 public void notifyConfigurationChanged() {
284 mService.sendNewConfiguration();
285
286 synchronized (mInputDevicesReadyMonitor) {
287 if (!mInputDevicesReady) {
288 mInputDevicesReady = true;
289 mInputDevicesReadyMonitor.notifyAll();
290 }
291 }
292 }
293
294 /* Waits until the built-in input devices have been configured. */
295 public boolean waitForInputDevicesReady(long timeoutMillis) {
296 synchronized (mInputDevicesReadyMonitor) {
297 if (!mInputDevicesReady) {
298 try {
299 mInputDevicesReadyMonitor.wait(timeoutMillis);
300 } catch (InterruptedException ex) {
301 }
302 }
303 return mInputDevicesReady;
304 }
305 }
306
307 /* Notifies that the lid switch changed state. */
308 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) {
309 mService.mPolicy.notifyLidSwitchChanged(whenNanos, lidOpen);
310 }
311
312 /* Provides an opportunity for the window manager policy to intercept early key
313 * processing as soon as the key has been read from the device. */
314 public int interceptKeyBeforeQueueing(
315 KeyEvent event, int policyFlags, boolean isScreenOn) {
316 return mService.mPolicy.interceptKeyBeforeQueueing(event, policyFlags, isScreenOn);
317 }
Jeff Brown56194eb2011-03-02 19:23:13 -0800318
319 /* Provides an opportunity for the window manager policy to intercept early
320 * motion event processing when the screen is off since these events are normally
321 * dropped. */
322 public int interceptMotionBeforeQueueingWhenScreenOff(int policyFlags) {
323 return mService.mPolicy.interceptMotionBeforeQueueingWhenScreenOff(policyFlags);
324 }
325
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800326 /* Provides an opportunity for the window manager policy to process a key before
327 * ordinary dispatch. */
Jeff Brown905805a2011-10-12 13:57:59 -0700328 public long interceptKeyBeforeDispatching(
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800329 InputWindowHandle focus, KeyEvent event, int policyFlags) {
330 WindowState windowState = focus != null ? (WindowState) focus.windowState : null;
331 return mService.mPolicy.interceptKeyBeforeDispatching(windowState, event, policyFlags);
332 }
333
334 /* Provides an opportunity for the window manager policy to process a key that
335 * the application did not handle. */
336 public KeyEvent dispatchUnhandledKey(
337 InputWindowHandle focus, KeyEvent event, int policyFlags) {
338 WindowState windowState = focus != null ? (WindowState) focus.windowState : null;
339 return mService.mPolicy.dispatchUnhandledKey(windowState, event, policyFlags);
340 }
Jeff Brown4532e612012-04-05 14:27:12 -0700341
342 /* Callback to get pointer layer. */
343 public int getPointerLayer() {
344 return mService.mPolicy.windowTypeToLayerLw(WindowManager.LayoutParams.TYPE_POINTER)
345 * WindowManagerService.TYPE_LAYER_MULTIPLIER
346 + WindowManagerService.TYPE_LAYER_OFFSET;
347 }
348
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800349 /* Called when the current input focus changes.
350 * Layer assignment is assumed to be complete by the time this is called.
351 */
352 public void setInputFocusLw(WindowState newWindow, boolean updateInputWindows) {
353 if (WindowManagerService.DEBUG_INPUT) {
354 Slog.d(WindowManagerService.TAG, "Input focus has changed to " + newWindow);
355 }
356
357 if (newWindow != mInputFocus) {
358 if (newWindow != null && newWindow.canReceiveKeys()) {
359 // Displaying a window implicitly causes dispatching to be unpaused.
360 // This is to protect against bugs if someone pauses dispatching but
361 // forgets to resume.
362 newWindow.mToken.paused = false;
363 }
364
365 mInputFocus = newWindow;
366 setUpdateInputWindowsNeededLw();
367
368 if (updateInputWindows) {
369 updateInputWindowsLw(false /*force*/);
370 }
371 }
372 }
373
374 public void setFocusedAppLw(AppWindowToken newApp) {
375 // Focused app has changed.
376 if (newApp == null) {
377 mService.mInputManager.setFocusedApplication(null);
378 } else {
Jeff Brown9302c872011-07-13 22:51:29 -0700379 final InputApplicationHandle handle = newApp.mInputApplicationHandle;
380 handle.name = newApp.toString();
381 handle.dispatchingTimeoutNanos = newApp.inputDispatchingTimeoutNanos;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800382
Jeff Brown9302c872011-07-13 22:51:29 -0700383 mService.mInputManager.setFocusedApplication(handle);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800384 }
385 }
386
387 public void pauseDispatchingLw(WindowToken window) {
388 if (! window.paused) {
389 if (WindowManagerService.DEBUG_INPUT) {
390 Slog.v(WindowManagerService.TAG, "Pausing WindowToken " + window);
391 }
392
393 window.paused = true;
394 updateInputWindowsLw(true /*force*/);
395 }
396 }
397
398 public void resumeDispatchingLw(WindowToken window) {
399 if (window.paused) {
400 if (WindowManagerService.DEBUG_INPUT) {
401 Slog.v(WindowManagerService.TAG, "Resuming WindowToken " + window);
402 }
403
404 window.paused = false;
405 updateInputWindowsLw(true /*force*/);
406 }
407 }
408
409 public void freezeInputDispatchingLw() {
410 if (! mInputDispatchFrozen) {
411 if (WindowManagerService.DEBUG_INPUT) {
412 Slog.v(WindowManagerService.TAG, "Freezing input dispatching");
413 }
414
415 mInputDispatchFrozen = true;
416 updateInputDispatchModeLw();
417 }
418 }
419
420 public void thawInputDispatchingLw() {
421 if (mInputDispatchFrozen) {
422 if (WindowManagerService.DEBUG_INPUT) {
423 Slog.v(WindowManagerService.TAG, "Thawing input dispatching");
424 }
425
426 mInputDispatchFrozen = false;
427 updateInputDispatchModeLw();
428 }
429 }
430
431 public void setEventDispatchingLw(boolean enabled) {
432 if (mInputDispatchEnabled != enabled) {
433 if (WindowManagerService.DEBUG_INPUT) {
434 Slog.v(WindowManagerService.TAG, "Setting event dispatching to " + enabled);
435 }
436
437 mInputDispatchEnabled = enabled;
438 updateInputDispatchModeLw();
439 }
440 }
441
442 private void updateInputDispatchModeLw() {
443 mService.mInputManager.setInputDispatchMode(mInputDispatchEnabled, mInputDispatchFrozen);
444 }
Jeff Brownea426552011-07-18 16:53:48 -0700445}