blob: 9dc92d3ed74b8f268d09db16d074476715fdd7dc [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;
28
29final class InputMonitor {
30 private final WindowManagerService mService;
31
32 // Current window with input focus for keys and other non-touch events. May be null.
33 private WindowState mInputFocus;
34
35 // When true, prevents input dispatch from proceeding until set to false again.
36 private boolean mInputDispatchFrozen;
37
38 // When true, input dispatch proceeds normally. Otherwise all events are dropped.
39 private boolean mInputDispatchEnabled = true;
40
41 // When true, need to call updateInputWindowsLw().
42 private boolean mUpdateInputWindowsNeeded = true;
43
44 // Temporary list of windows information to provide to the input dispatcher.
45 private InputWindowList mTempInputWindows = new InputWindowList();
46
47 // Temporary input application object to provide to the input dispatcher.
48 private InputApplication mTempInputApplication = new InputApplication();
49
50 // Set to true when the first input device configuration change notification
51 // is received to indicate that the input devices are ready.
52 private final Object mInputDevicesReadyMonitor = new Object();
53 private boolean mInputDevicesReady;
54
55 public InputMonitor(WindowManagerService service) {
56 mService = service;
57 }
58
59 /* Notifies the window manager about a broken input channel.
60 *
61 * Called by the InputManager.
62 */
63 public void notifyInputChannelBroken(InputWindowHandle inputWindowHandle) {
64 if (inputWindowHandle == null) {
65 return;
66 }
67
68 synchronized (mService.mWindowMap) {
69 WindowState windowState = (WindowState) inputWindowHandle.windowState;
70 Slog.i(WindowManagerService.TAG, "WINDOW DIED " + windowState);
71 mService.removeWindowLocked(windowState.mSession, windowState);
72 }
73 }
74
75 /* Notifies the window manager about an application that is not responding.
76 * Returns a new timeout to continue waiting in nanoseconds, or 0 to abort dispatch.
77 *
78 * Called by the InputManager.
79 */
80 public long notifyANR(InputApplicationHandle inputApplicationHandle,
81 InputWindowHandle inputWindowHandle) {
82 AppWindowToken appWindowToken = null;
83 if (inputWindowHandle != null) {
84 synchronized (mService.mWindowMap) {
85 WindowState windowState = (WindowState) inputWindowHandle.windowState;
86 if (windowState != null) {
87 Slog.i(WindowManagerService.TAG, "Input event dispatching timed out sending to "
88 + windowState.mAttrs.getTitle());
89 appWindowToken = windowState.mAppToken;
90 }
91 }
92 }
93
94 if (appWindowToken == null && inputApplicationHandle != null) {
95 appWindowToken = inputApplicationHandle.appWindowToken;
96 Slog.i(WindowManagerService.TAG, "Input event dispatching timed out sending to application "
97 + appWindowToken.stringName);
98 }
99
100 if (appWindowToken != null && appWindowToken.appToken != null) {
101 try {
102 // Notify the activity manager about the timeout and let it decide whether
103 // to abort dispatching or keep waiting.
104 boolean abort = appWindowToken.appToken.keyDispatchingTimedOut();
105 if (! abort) {
106 // The activity manager declined to abort dispatching.
107 // Wait a bit longer and timeout again later.
108 return appWindowToken.inputDispatchingTimeoutNanos;
109 }
110 } catch (RemoteException ex) {
111 }
112 }
113 return 0; // abort dispatching
114 }
115
116 private void addDragInputWindowLw(InputWindowList windowList) {
117 final InputWindow inputWindow = windowList.add();
118 inputWindow.inputChannel = mService.mDragState.mServerChannel;
119 inputWindow.name = "drag";
120 inputWindow.layoutParamsFlags = 0;
121 inputWindow.layoutParamsType = WindowManager.LayoutParams.TYPE_DRAG;
122 inputWindow.dispatchingTimeoutNanos = WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
123 inputWindow.visible = true;
124 inputWindow.canReceiveKeys = false;
125 inputWindow.hasFocus = true;
126 inputWindow.hasWallpaper = false;
127 inputWindow.paused = false;
128 inputWindow.layer = mService.mDragState.getDragLayerLw();
129 inputWindow.ownerPid = Process.myPid();
130 inputWindow.ownerUid = Process.myUid();
131
132 // The drag window covers the entire display
133 inputWindow.frameLeft = 0;
134 inputWindow.frameTop = 0;
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700135 inputWindow.frameRight = mService.mDisplay.getRealWidth();
136 inputWindow.frameBottom = mService.mDisplay.getRealHeight();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800137
138 // The drag window cannot receive new touches.
139 inputWindow.touchableRegion.setEmpty();
140 }
141
142 public void setUpdateInputWindowsNeededLw() {
143 mUpdateInputWindowsNeeded = true;
144 }
145
146 /* Updates the cached window information provided to the input dispatcher. */
147 public void updateInputWindowsLw(boolean force) {
148 if (!force && !mUpdateInputWindowsNeeded) {
149 return;
150 }
151 mUpdateInputWindowsNeeded = false;
152
153 // Populate the input window list with information about all of the windows that
154 // could potentially receive input.
155 // As an optimization, we could try to prune the list of windows but this turns
156 // out to be difficult because only the native code knows for sure which window
157 // currently has touch focus.
158 final ArrayList<WindowState> windows = mService.mWindows;
159
160 // If there's a drag in flight, provide a pseudowindow to catch drag input
161 final boolean inDrag = (mService.mDragState != null);
162 if (inDrag) {
163 if (WindowManagerService.DEBUG_DRAG) {
164 Log.d(WindowManagerService.TAG, "Inserting drag window");
165 }
166 addDragInputWindowLw(mTempInputWindows);
167 }
168
169 final int N = windows.size();
170 for (int i = N - 1; i >= 0; i--) {
171 final WindowState child = windows.get(i);
172 if (child.mInputChannel == null || child.mRemoved) {
173 // Skip this window because it cannot possibly receive input.
174 continue;
175 }
176
177 final int flags = child.mAttrs.flags;
178 final int type = child.mAttrs.type;
179
180 final boolean hasFocus = (child == mInputFocus);
181 final boolean isVisible = child.isVisibleLw();
182 final boolean hasWallpaper = (child == mService.mWallpaperTarget)
183 && (type != WindowManager.LayoutParams.TYPE_KEYGUARD);
184
185 // If there's a drag in progress and 'child' is a potential drop target,
186 // make sure it's been told about the drag
187 if (inDrag && isVisible) {
188 mService.mDragState.sendDragStartedIfNeededLw(child);
189 }
190
191 // Add a window to our list of input windows.
192 final InputWindow inputWindow = mTempInputWindows.add();
193 inputWindow.inputWindowHandle = child.mInputWindowHandle;
194 inputWindow.inputChannel = child.mInputChannel;
195 inputWindow.name = child.toString();
196 inputWindow.layoutParamsFlags = flags;
197 inputWindow.layoutParamsType = type;
198 inputWindow.dispatchingTimeoutNanos = child.getInputDispatchingTimeoutNanos();
199 inputWindow.visible = isVisible;
200 inputWindow.canReceiveKeys = child.canReceiveKeys();
201 inputWindow.hasFocus = hasFocus;
202 inputWindow.hasWallpaper = hasWallpaper;
203 inputWindow.paused = child.mAppToken != null ? child.mAppToken.paused : false;
204 inputWindow.layer = child.mLayer;
205 inputWindow.ownerPid = child.mSession.mPid;
206 inputWindow.ownerUid = child.mSession.mUid;
207
Dianne Hackbornffb3d932011-05-17 17:44:51 -0700208 final Rect frame = child.mFrame;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800209 inputWindow.frameLeft = frame.left;
210 inputWindow.frameTop = frame.top;
211 inputWindow.frameRight = frame.right;
212 inputWindow.frameBottom = frame.bottom;
213
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400214 if (child.mGlobalScale != 1) {
215 // If we are scaling the window, input coordinates need
216 // to be inversely scaled to map from what is on screen
217 // to what is actually being touched in the UI.
218 inputWindow.scaleFactor = 1.0f/child.mGlobalScale;
219 } else {
220 inputWindow.scaleFactor = 1;
221 }
222
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800223 child.getTouchableRegion(inputWindow.touchableRegion);
224 }
225
226 // Send windows to native code.
227 mService.mInputManager.setInputWindows(mTempInputWindows.toNullTerminatedArray());
228
229 // Clear the list in preparation for the next round.
230 // Also avoids keeping InputChannel objects referenced unnecessarily.
231 mTempInputWindows.clear();
232 }
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 {
324 mTempInputApplication.inputApplicationHandle = newApp.mInputApplicationHandle;
325 mTempInputApplication.name = newApp.toString();
326 mTempInputApplication.dispatchingTimeoutNanos =
327 newApp.inputDispatchingTimeoutNanos;
328
329 mService.mInputManager.setFocusedApplication(mTempInputApplication);
330
331 mTempInputApplication.recycle();
332 }
333 }
334
335 public void pauseDispatchingLw(WindowToken window) {
336 if (! window.paused) {
337 if (WindowManagerService.DEBUG_INPUT) {
338 Slog.v(WindowManagerService.TAG, "Pausing WindowToken " + window);
339 }
340
341 window.paused = true;
342 updateInputWindowsLw(true /*force*/);
343 }
344 }
345
346 public void resumeDispatchingLw(WindowToken window) {
347 if (window.paused) {
348 if (WindowManagerService.DEBUG_INPUT) {
349 Slog.v(WindowManagerService.TAG, "Resuming WindowToken " + window);
350 }
351
352 window.paused = false;
353 updateInputWindowsLw(true /*force*/);
354 }
355 }
356
357 public void freezeInputDispatchingLw() {
358 if (! mInputDispatchFrozen) {
359 if (WindowManagerService.DEBUG_INPUT) {
360 Slog.v(WindowManagerService.TAG, "Freezing input dispatching");
361 }
362
363 mInputDispatchFrozen = true;
364 updateInputDispatchModeLw();
365 }
366 }
367
368 public void thawInputDispatchingLw() {
369 if (mInputDispatchFrozen) {
370 if (WindowManagerService.DEBUG_INPUT) {
371 Slog.v(WindowManagerService.TAG, "Thawing input dispatching");
372 }
373
374 mInputDispatchFrozen = false;
375 updateInputDispatchModeLw();
376 }
377 }
378
379 public void setEventDispatchingLw(boolean enabled) {
380 if (mInputDispatchEnabled != enabled) {
381 if (WindowManagerService.DEBUG_INPUT) {
382 Slog.v(WindowManagerService.TAG, "Setting event dispatching to " + enabled);
383 }
384
385 mInputDispatchEnabled = enabled;
386 updateInputDispatchModeLw();
387 }
388 }
389
390 private void updateInputDispatchModeLw() {
391 mService.mInputManager.setInputDispatchMode(mInputDispatchEnabled, mInputDispatchFrozen);
392 }
393}