blob: 45a78af37af360c99d2a3ac1bddd72014284209d [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;
135 inputWindow.frameRight = mService.mDisplay.getWidth();
136 inputWindow.frameBottom = mService.mDisplay.getHeight();
137
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
208 final Rect frame = child.mFrame;
209 inputWindow.frameLeft = frame.left;
210 inputWindow.frameTop = frame.top;
211 inputWindow.frameRight = frame.right;
212 inputWindow.frameBottom = frame.bottom;
213
214 child.getTouchableRegion(inputWindow.touchableRegion);
215 }
216
217 // Send windows to native code.
218 mService.mInputManager.setInputWindows(mTempInputWindows.toNullTerminatedArray());
219
220 // Clear the list in preparation for the next round.
221 // Also avoids keeping InputChannel objects referenced unnecessarily.
222 mTempInputWindows.clear();
223 }
224
225 /* Notifies that the input device configuration has changed. */
226 public void notifyConfigurationChanged() {
227 mService.sendNewConfiguration();
228
229 synchronized (mInputDevicesReadyMonitor) {
230 if (!mInputDevicesReady) {
231 mInputDevicesReady = true;
232 mInputDevicesReadyMonitor.notifyAll();
233 }
234 }
235 }
236
237 /* Waits until the built-in input devices have been configured. */
238 public boolean waitForInputDevicesReady(long timeoutMillis) {
239 synchronized (mInputDevicesReadyMonitor) {
240 if (!mInputDevicesReady) {
241 try {
242 mInputDevicesReadyMonitor.wait(timeoutMillis);
243 } catch (InterruptedException ex) {
244 }
245 }
246 return mInputDevicesReady;
247 }
248 }
249
250 /* Notifies that the lid switch changed state. */
251 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) {
252 mService.mPolicy.notifyLidSwitchChanged(whenNanos, lidOpen);
253 }
254
255 /* Provides an opportunity for the window manager policy to intercept early key
256 * processing as soon as the key has been read from the device. */
257 public int interceptKeyBeforeQueueing(
258 KeyEvent event, int policyFlags, boolean isScreenOn) {
259 return mService.mPolicy.interceptKeyBeforeQueueing(event, policyFlags, isScreenOn);
260 }
Jeff Brown56194eb2011-03-02 19:23:13 -0800261
262 /* Provides an opportunity for the window manager policy to intercept early
263 * motion event processing when the screen is off since these events are normally
264 * dropped. */
265 public int interceptMotionBeforeQueueingWhenScreenOff(int policyFlags) {
266 return mService.mPolicy.interceptMotionBeforeQueueingWhenScreenOff(policyFlags);
267 }
268
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800269 /* Provides an opportunity for the window manager policy to process a key before
270 * ordinary dispatch. */
271 public boolean interceptKeyBeforeDispatching(
272 InputWindowHandle focus, KeyEvent event, int policyFlags) {
273 WindowState windowState = focus != null ? (WindowState) focus.windowState : null;
274 return mService.mPolicy.interceptKeyBeforeDispatching(windowState, event, policyFlags);
275 }
276
277 /* Provides an opportunity for the window manager policy to process a key that
278 * the application did not handle. */
279 public KeyEvent dispatchUnhandledKey(
280 InputWindowHandle focus, KeyEvent event, int policyFlags) {
281 WindowState windowState = focus != null ? (WindowState) focus.windowState : null;
282 return mService.mPolicy.dispatchUnhandledKey(windowState, event, policyFlags);
283 }
284
285 /* Called when the current input focus changes.
286 * Layer assignment is assumed to be complete by the time this is called.
287 */
288 public void setInputFocusLw(WindowState newWindow, boolean updateInputWindows) {
289 if (WindowManagerService.DEBUG_INPUT) {
290 Slog.d(WindowManagerService.TAG, "Input focus has changed to " + newWindow);
291 }
292
293 if (newWindow != mInputFocus) {
294 if (newWindow != null && newWindow.canReceiveKeys()) {
295 // Displaying a window implicitly causes dispatching to be unpaused.
296 // This is to protect against bugs if someone pauses dispatching but
297 // forgets to resume.
298 newWindow.mToken.paused = false;
299 }
300
301 mInputFocus = newWindow;
302 setUpdateInputWindowsNeededLw();
303
304 if (updateInputWindows) {
305 updateInputWindowsLw(false /*force*/);
306 }
307 }
308 }
309
310 public void setFocusedAppLw(AppWindowToken newApp) {
311 // Focused app has changed.
312 if (newApp == null) {
313 mService.mInputManager.setFocusedApplication(null);
314 } else {
315 mTempInputApplication.inputApplicationHandle = newApp.mInputApplicationHandle;
316 mTempInputApplication.name = newApp.toString();
317 mTempInputApplication.dispatchingTimeoutNanos =
318 newApp.inputDispatchingTimeoutNanos;
319
320 mService.mInputManager.setFocusedApplication(mTempInputApplication);
321
322 mTempInputApplication.recycle();
323 }
324 }
325
326 public void pauseDispatchingLw(WindowToken window) {
327 if (! window.paused) {
328 if (WindowManagerService.DEBUG_INPUT) {
329 Slog.v(WindowManagerService.TAG, "Pausing WindowToken " + window);
330 }
331
332 window.paused = true;
333 updateInputWindowsLw(true /*force*/);
334 }
335 }
336
337 public void resumeDispatchingLw(WindowToken window) {
338 if (window.paused) {
339 if (WindowManagerService.DEBUG_INPUT) {
340 Slog.v(WindowManagerService.TAG, "Resuming WindowToken " + window);
341 }
342
343 window.paused = false;
344 updateInputWindowsLw(true /*force*/);
345 }
346 }
347
348 public void freezeInputDispatchingLw() {
349 if (! mInputDispatchFrozen) {
350 if (WindowManagerService.DEBUG_INPUT) {
351 Slog.v(WindowManagerService.TAG, "Freezing input dispatching");
352 }
353
354 mInputDispatchFrozen = true;
355 updateInputDispatchModeLw();
356 }
357 }
358
359 public void thawInputDispatchingLw() {
360 if (mInputDispatchFrozen) {
361 if (WindowManagerService.DEBUG_INPUT) {
362 Slog.v(WindowManagerService.TAG, "Thawing input dispatching");
363 }
364
365 mInputDispatchFrozen = false;
366 updateInputDispatchModeLw();
367 }
368 }
369
370 public void setEventDispatchingLw(boolean enabled) {
371 if (mInputDispatchEnabled != enabled) {
372 if (WindowManagerService.DEBUG_INPUT) {
373 Slog.v(WindowManagerService.TAG, "Setting event dispatching to " + enabled);
374 }
375
376 mInputDispatchEnabled = enabled;
377 updateInputDispatchModeLw();
378 }
379 }
380
381 private void updateInputDispatchModeLw() {
382 mService.mInputManager.setInputDispatchMode(mInputDispatchEnabled, mInputDispatchFrozen);
383 }
384}