blob: 12ef2382734297edb96fd3d8964e48b4aad29a6b [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
19import android.graphics.Rect;
20import android.os.Process;
21import android.os.RemoteException;
22import android.util.Log;
23import android.util.Slog;
24import android.view.KeyEvent;
25import android.view.WindowManager;
26
27import java.util.ArrayList;
Jeff Brown9302c872011-07-13 22:51:29 -070028import java.util.Arrays;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080029
30final class InputMonitor {
31 private final WindowManagerService mService;
32
33 // Current window with input focus for keys and other non-touch events. May be null.
34 private WindowState mInputFocus;
35
36 // When true, prevents input dispatch from proceeding until set to false again.
37 private boolean mInputDispatchFrozen;
38
39 // When true, input dispatch proceeds normally. Otherwise all events are dropped.
40 private boolean mInputDispatchEnabled = true;
41
42 // When true, need to call updateInputWindowsLw().
43 private boolean mUpdateInputWindowsNeeded = true;
44
Jeff Brown9302c872011-07-13 22:51:29 -070045 // Array of window handles to provide to the input dispatcher.
46 private InputWindowHandle[] mInputWindowHandles;
47 private int mInputWindowHandleCount;
48
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080049 // Set to true when the first input device configuration change notification
50 // is received to indicate that the input devices are ready.
51 private final Object mInputDevicesReadyMonitor = new Object();
52 private boolean mInputDevicesReady;
53
54 public InputMonitor(WindowManagerService service) {
55 mService = service;
56 }
57
58 /* Notifies the window manager about a broken input channel.
59 *
60 * Called by the InputManager.
61 */
62 public void notifyInputChannelBroken(InputWindowHandle inputWindowHandle) {
63 if (inputWindowHandle == null) {
64 return;
65 }
66
67 synchronized (mService.mWindowMap) {
68 WindowState windowState = (WindowState) inputWindowHandle.windowState;
Jeff Brown9302c872011-07-13 22:51:29 -070069 if (windowState != null) {
70 Slog.i(WindowManagerService.TAG, "WINDOW DIED " + windowState);
71 mService.removeWindowLocked(windowState.mSession, windowState);
72 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080073 }
74 }
75
76 /* Notifies the window manager about an application that is not responding.
77 * Returns a new timeout to continue waiting in nanoseconds, or 0 to abort dispatch.
78 *
79 * Called by the InputManager.
80 */
81 public long notifyANR(InputApplicationHandle inputApplicationHandle,
82 InputWindowHandle inputWindowHandle) {
83 AppWindowToken appWindowToken = null;
84 if (inputWindowHandle != null) {
85 synchronized (mService.mWindowMap) {
86 WindowState windowState = (WindowState) inputWindowHandle.windowState;
87 if (windowState != null) {
88 Slog.i(WindowManagerService.TAG, "Input event dispatching timed out sending to "
89 + windowState.mAttrs.getTitle());
90 appWindowToken = windowState.mAppToken;
91 }
92 }
93 }
94
95 if (appWindowToken == null && inputApplicationHandle != null) {
96 appWindowToken = inputApplicationHandle.appWindowToken;
Jeff Brown9302c872011-07-13 22:51:29 -070097 if (appWindowToken != null) {
98 Slog.i(WindowManagerService.TAG,
99 "Input event dispatching timed out sending to application "
100 + appWindowToken.stringName);
101 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800102 }
103
104 if (appWindowToken != null && appWindowToken.appToken != null) {
105 try {
106 // Notify the activity manager about the timeout and let it decide whether
107 // to abort dispatching or keep waiting.
108 boolean abort = appWindowToken.appToken.keyDispatchingTimedOut();
109 if (! abort) {
110 // The activity manager declined to abort dispatching.
111 // Wait a bit longer and timeout again later.
112 return appWindowToken.inputDispatchingTimeoutNanos;
113 }
114 } catch (RemoteException ex) {
115 }
116 }
117 return 0; // abort dispatching
118 }
119
Jeff Brown9302c872011-07-13 22:51:29 -0700120 private void addInputWindowHandleLw(InputWindowHandle windowHandle) {
121 if (mInputWindowHandles == null) {
122 mInputWindowHandles = new InputWindowHandle[16];
123 }
124 if (mInputWindowHandleCount >= mInputWindowHandles.length) {
125 mInputWindowHandles = Arrays.copyOf(mInputWindowHandles,
126 mInputWindowHandleCount * 2);
127 }
128 mInputWindowHandles[mInputWindowHandleCount++] = windowHandle;
129 }
130
131 private void clearInputWindowHandlesLw() {
132 while (mInputWindowHandleCount != 0) {
133 mInputWindowHandles[--mInputWindowHandleCount] = null;
134 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800135 }
136
137 public void setUpdateInputWindowsNeededLw() {
138 mUpdateInputWindowsNeeded = true;
139 }
140
141 /* Updates the cached window information provided to the input dispatcher. */
142 public void updateInputWindowsLw(boolean force) {
143 if (!force && !mUpdateInputWindowsNeeded) {
144 return;
145 }
146 mUpdateInputWindowsNeeded = false;
147
Dianne Hackbornb961cd22011-06-21 12:13:37 -0700148 if (false) Slog.d(WindowManagerService.TAG, ">>>>>> ENTERED updateInputWindowsLw");
Jeff Brown9302c872011-07-13 22:51:29 -0700149
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800150 // Populate the input window list with information about all of the windows that
151 // could potentially receive input.
152 // As an optimization, we could try to prune the list of windows but this turns
153 // out to be difficult because only the native code knows for sure which window
154 // currently has touch focus.
155 final ArrayList<WindowState> windows = mService.mWindows;
156
157 // If there's a drag in flight, provide a pseudowindow to catch drag input
158 final boolean inDrag = (mService.mDragState != null);
159 if (inDrag) {
160 if (WindowManagerService.DEBUG_DRAG) {
161 Log.d(WindowManagerService.TAG, "Inserting drag window");
162 }
Jeff Brownea426552011-07-18 16:53:48 -0700163 addInputWindowHandleLw(mService.mDragState.mDragWindowHandle);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800164 }
165
166 final int N = windows.size();
167 for (int i = N - 1; i >= 0; i--) {
168 final WindowState child = windows.get(i);
169 if (child.mInputChannel == null || child.mRemoved) {
170 // Skip this window because it cannot possibly receive input.
171 continue;
172 }
173
174 final int flags = child.mAttrs.flags;
175 final int type = child.mAttrs.type;
176
177 final boolean hasFocus = (child == mInputFocus);
178 final boolean isVisible = child.isVisibleLw();
179 final boolean hasWallpaper = (child == mService.mWallpaperTarget)
180 && (type != WindowManager.LayoutParams.TYPE_KEYGUARD);
181
182 // If there's a drag in progress and 'child' is a potential drop target,
183 // make sure it's been told about the drag
184 if (inDrag && isVisible) {
185 mService.mDragState.sendDragStartedIfNeededLw(child);
186 }
187
188 // Add a window to our list of input windows.
Jeff Brown9302c872011-07-13 22:51:29 -0700189 final InputWindowHandle inputWindowHandle = child.mInputWindowHandle;
190 inputWindowHandle.inputChannel = child.mInputChannel;
191 inputWindowHandle.name = child.toString();
192 inputWindowHandle.layoutParamsFlags = flags;
193 inputWindowHandle.layoutParamsType = type;
194 inputWindowHandle.dispatchingTimeoutNanos = child.getInputDispatchingTimeoutNanos();
195 inputWindowHandle.visible = isVisible;
196 inputWindowHandle.canReceiveKeys = child.canReceiveKeys();
197 inputWindowHandle.hasFocus = hasFocus;
198 inputWindowHandle.hasWallpaper = hasWallpaper;
199 inputWindowHandle.paused = child.mAppToken != null ? child.mAppToken.paused : false;
200 inputWindowHandle.layer = child.mLayer;
201 inputWindowHandle.ownerPid = child.mSession.mPid;
202 inputWindowHandle.ownerUid = child.mSession.mUid;
203 inputWindowHandle.inputFeatures = child.mAttrs.inputFeatures;
Jeff Brown474dcb52011-06-14 20:22:50 -0700204
Dianne Hackbornffb3d932011-05-17 17:44:51 -0700205 final Rect frame = child.mFrame;
Jeff Brown9302c872011-07-13 22:51:29 -0700206 inputWindowHandle.frameLeft = frame.left;
207 inputWindowHandle.frameTop = frame.top;
208 inputWindowHandle.frameRight = frame.right;
209 inputWindowHandle.frameBottom = frame.bottom;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800210
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400211 if (child.mGlobalScale != 1) {
212 // If we are scaling the window, input coordinates need
213 // to be inversely scaled to map from what is on screen
214 // to what is actually being touched in the UI.
Jeff Brown9302c872011-07-13 22:51:29 -0700215 inputWindowHandle.scaleFactor = 1.0f/child.mGlobalScale;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400216 } else {
Jeff Brown9302c872011-07-13 22:51:29 -0700217 inputWindowHandle.scaleFactor = 1;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400218 }
219
Jeff Brown9302c872011-07-13 22:51:29 -0700220 child.getTouchableRegion(inputWindowHandle.touchableRegion);
221
222 addInputWindowHandleLw(inputWindowHandle);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800223 }
224
225 // Send windows to native code.
Jeff Brown9302c872011-07-13 22:51:29 -0700226 mService.mInputManager.setInputWindows(mInputWindowHandles);
227
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800228 // Clear the list in preparation for the next round.
Jeff Brown9302c872011-07-13 22:51:29 -0700229 clearInputWindowHandlesLw();
230
Dianne Hackbornb961cd22011-06-21 12:13:37 -0700231 if (false) Slog.d(WindowManagerService.TAG, "<<<<<<< EXITED updateInputWindowsLw");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800232 }
233
234 /* Notifies that the input device configuration has changed. */
235 public void notifyConfigurationChanged() {
236 mService.sendNewConfiguration();
237
238 synchronized (mInputDevicesReadyMonitor) {
239 if (!mInputDevicesReady) {
240 mInputDevicesReady = true;
241 mInputDevicesReadyMonitor.notifyAll();
242 }
243 }
244 }
245
246 /* Waits until the built-in input devices have been configured. */
247 public boolean waitForInputDevicesReady(long timeoutMillis) {
248 synchronized (mInputDevicesReadyMonitor) {
249 if (!mInputDevicesReady) {
250 try {
251 mInputDevicesReadyMonitor.wait(timeoutMillis);
252 } catch (InterruptedException ex) {
253 }
254 }
255 return mInputDevicesReady;
256 }
257 }
258
259 /* Notifies that the lid switch changed state. */
260 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) {
261 mService.mPolicy.notifyLidSwitchChanged(whenNanos, lidOpen);
262 }
263
264 /* Provides an opportunity for the window manager policy to intercept early key
265 * processing as soon as the key has been read from the device. */
266 public int interceptKeyBeforeQueueing(
267 KeyEvent event, int policyFlags, boolean isScreenOn) {
268 return mService.mPolicy.interceptKeyBeforeQueueing(event, policyFlags, isScreenOn);
269 }
Jeff Brown56194eb2011-03-02 19:23:13 -0800270
271 /* Provides an opportunity for the window manager policy to intercept early
272 * motion event processing when the screen is off since these events are normally
273 * dropped. */
274 public int interceptMotionBeforeQueueingWhenScreenOff(int policyFlags) {
275 return mService.mPolicy.interceptMotionBeforeQueueingWhenScreenOff(policyFlags);
276 }
277
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800278 /* Provides an opportunity for the window manager policy to process a key before
279 * ordinary dispatch. */
280 public boolean interceptKeyBeforeDispatching(
281 InputWindowHandle focus, KeyEvent event, int policyFlags) {
282 WindowState windowState = focus != null ? (WindowState) focus.windowState : null;
283 return mService.mPolicy.interceptKeyBeforeDispatching(windowState, event, policyFlags);
284 }
285
286 /* Provides an opportunity for the window manager policy to process a key that
287 * the application did not handle. */
288 public KeyEvent dispatchUnhandledKey(
289 InputWindowHandle focus, KeyEvent event, int policyFlags) {
290 WindowState windowState = focus != null ? (WindowState) focus.windowState : null;
291 return mService.mPolicy.dispatchUnhandledKey(windowState, event, policyFlags);
292 }
293
294 /* Called when the current input focus changes.
295 * Layer assignment is assumed to be complete by the time this is called.
296 */
297 public void setInputFocusLw(WindowState newWindow, boolean updateInputWindows) {
298 if (WindowManagerService.DEBUG_INPUT) {
299 Slog.d(WindowManagerService.TAG, "Input focus has changed to " + newWindow);
300 }
301
302 if (newWindow != mInputFocus) {
303 if (newWindow != null && newWindow.canReceiveKeys()) {
304 // Displaying a window implicitly causes dispatching to be unpaused.
305 // This is to protect against bugs if someone pauses dispatching but
306 // forgets to resume.
307 newWindow.mToken.paused = false;
308 }
309
310 mInputFocus = newWindow;
311 setUpdateInputWindowsNeededLw();
312
313 if (updateInputWindows) {
314 updateInputWindowsLw(false /*force*/);
315 }
316 }
317 }
318
319 public void setFocusedAppLw(AppWindowToken newApp) {
320 // Focused app has changed.
321 if (newApp == null) {
322 mService.mInputManager.setFocusedApplication(null);
323 } else {
Jeff Brown9302c872011-07-13 22:51:29 -0700324 final InputApplicationHandle handle = newApp.mInputApplicationHandle;
325 handle.name = newApp.toString();
326 handle.dispatchingTimeoutNanos = newApp.inputDispatchingTimeoutNanos;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800327
Jeff Brown9302c872011-07-13 22:51:29 -0700328 mService.mInputManager.setFocusedApplication(handle);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800329 }
330 }
331
332 public void pauseDispatchingLw(WindowToken window) {
333 if (! window.paused) {
334 if (WindowManagerService.DEBUG_INPUT) {
335 Slog.v(WindowManagerService.TAG, "Pausing WindowToken " + window);
336 }
337
338 window.paused = true;
339 updateInputWindowsLw(true /*force*/);
340 }
341 }
342
343 public void resumeDispatchingLw(WindowToken window) {
344 if (window.paused) {
345 if (WindowManagerService.DEBUG_INPUT) {
346 Slog.v(WindowManagerService.TAG, "Resuming WindowToken " + window);
347 }
348
349 window.paused = false;
350 updateInputWindowsLw(true /*force*/);
351 }
352 }
353
354 public void freezeInputDispatchingLw() {
355 if (! mInputDispatchFrozen) {
356 if (WindowManagerService.DEBUG_INPUT) {
357 Slog.v(WindowManagerService.TAG, "Freezing input dispatching");
358 }
359
360 mInputDispatchFrozen = true;
361 updateInputDispatchModeLw();
362 }
363 }
364
365 public void thawInputDispatchingLw() {
366 if (mInputDispatchFrozen) {
367 if (WindowManagerService.DEBUG_INPUT) {
368 Slog.v(WindowManagerService.TAG, "Thawing input dispatching");
369 }
370
371 mInputDispatchFrozen = false;
372 updateInputDispatchModeLw();
373 }
374 }
375
376 public void setEventDispatchingLw(boolean enabled) {
377 if (mInputDispatchEnabled != enabled) {
378 if (WindowManagerService.DEBUG_INPUT) {
379 Slog.v(WindowManagerService.TAG, "Setting event dispatching to " + enabled);
380 }
381
382 mInputDispatchEnabled = enabled;
383 updateInputDispatchModeLw();
384 }
385 }
386
387 private void updateInputDispatchModeLw() {
388 mService.mInputManager.setInputDispatchMode(mInputDispatchEnabled, mInputDispatchFrozen);
389 }
Jeff Brownea426552011-07-18 16:53:48 -0700390}