blob: c28cfa248c54f5e78c3a83e71729c01a7271cf1a [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;
22
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080023import android.graphics.Rect;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080024import android.os.RemoteException;
25import android.util.Log;
26import android.util.Slog;
Jeff Browncc4f7db2011-08-30 20:34:48 -070027import android.view.InputChannel;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080028import android.view.KeyEvent;
29import android.view.WindowManager;
30
31import java.util.ArrayList;
Jeff Brown9302c872011-07-13 22:51:29 -070032import java.util.Arrays;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080033
Jeff Brown4532e612012-04-05 14:27:12 -070034final class InputMonitor implements InputManagerService.Callbacks {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080035 private final WindowManagerService mService;
36
37 // Current window with input focus for keys and other non-touch events. May be null.
38 private WindowState mInputFocus;
39
40 // When true, prevents input dispatch from proceeding until set to false again.
41 private boolean mInputDispatchFrozen;
42
43 // When true, input dispatch proceeds normally. Otherwise all events are dropped.
44 private boolean mInputDispatchEnabled = true;
45
46 // When true, need to call updateInputWindowsLw().
47 private boolean mUpdateInputWindowsNeeded = true;
48
Jeff Brown9302c872011-07-13 22:51:29 -070049 // Array of window handles to provide to the input dispatcher.
50 private InputWindowHandle[] mInputWindowHandles;
51 private int mInputWindowHandleCount;
52
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080053 // Set to true when the first input device configuration change notification
54 // is received to indicate that the input devices are ready.
55 private final Object mInputDevicesReadyMonitor = new Object();
56 private boolean mInputDevicesReady;
57
58 public InputMonitor(WindowManagerService service) {
59 mService = service;
60 }
61
62 /* Notifies the window manager about a broken input channel.
63 *
64 * Called by the InputManager.
65 */
66 public void notifyInputChannelBroken(InputWindowHandle inputWindowHandle) {
67 if (inputWindowHandle == null) {
68 return;
69 }
70
71 synchronized (mService.mWindowMap) {
72 WindowState windowState = (WindowState) inputWindowHandle.windowState;
Jeff Brown9302c872011-07-13 22:51:29 -070073 if (windowState != null) {
74 Slog.i(WindowManagerService.TAG, "WINDOW DIED " + windowState);
75 mService.removeWindowLocked(windowState.mSession, windowState);
76 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080077 }
78 }
79
80 /* Notifies the window manager about an application that is not responding.
81 * Returns a new timeout to continue waiting in nanoseconds, or 0 to abort dispatch.
82 *
83 * Called by the InputManager.
84 */
85 public long notifyANR(InputApplicationHandle inputApplicationHandle,
86 InputWindowHandle inputWindowHandle) {
87 AppWindowToken appWindowToken = null;
88 if (inputWindowHandle != null) {
89 synchronized (mService.mWindowMap) {
90 WindowState windowState = (WindowState) inputWindowHandle.windowState;
91 if (windowState != null) {
92 Slog.i(WindowManagerService.TAG, "Input event dispatching timed out sending to "
93 + windowState.mAttrs.getTitle());
94 appWindowToken = windowState.mAppToken;
95 }
96 }
97 }
98
99 if (appWindowToken == null && inputApplicationHandle != null) {
Jeff Brown4532e612012-04-05 14:27:12 -0700100 appWindowToken = (AppWindowToken)inputApplicationHandle.appWindowToken;
Jeff Brown9302c872011-07-13 22:51:29 -0700101 if (appWindowToken != null) {
102 Slog.i(WindowManagerService.TAG,
103 "Input event dispatching timed out sending to application "
104 + appWindowToken.stringName);
105 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800106 }
107
108 if (appWindowToken != null && appWindowToken.appToken != null) {
109 try {
110 // Notify the activity manager about the timeout and let it decide whether
111 // to abort dispatching or keep waiting.
112 boolean abort = appWindowToken.appToken.keyDispatchingTimedOut();
113 if (! abort) {
114 // The activity manager declined to abort dispatching.
115 // Wait a bit longer and timeout again later.
116 return appWindowToken.inputDispatchingTimeoutNanos;
117 }
118 } catch (RemoteException ex) {
119 }
120 }
121 return 0; // abort dispatching
122 }
123
Jeff Brown9302c872011-07-13 22:51:29 -0700124 private void addInputWindowHandleLw(InputWindowHandle windowHandle) {
125 if (mInputWindowHandles == null) {
126 mInputWindowHandles = new InputWindowHandle[16];
127 }
128 if (mInputWindowHandleCount >= mInputWindowHandles.length) {
129 mInputWindowHandles = Arrays.copyOf(mInputWindowHandles,
130 mInputWindowHandleCount * 2);
131 }
132 mInputWindowHandles[mInputWindowHandleCount++] = windowHandle;
133 }
134
135 private void clearInputWindowHandlesLw() {
136 while (mInputWindowHandleCount != 0) {
137 mInputWindowHandles[--mInputWindowHandleCount] = null;
138 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800139 }
140
141 public void setUpdateInputWindowsNeededLw() {
142 mUpdateInputWindowsNeeded = true;
143 }
144
145 /* Updates the cached window information provided to the input dispatcher. */
146 public void updateInputWindowsLw(boolean force) {
147 if (!force && !mUpdateInputWindowsNeeded) {
148 return;
149 }
150 mUpdateInputWindowsNeeded = false;
151
Dianne Hackbornb961cd22011-06-21 12:13:37 -0700152 if (false) Slog.d(WindowManagerService.TAG, ">>>>>> ENTERED updateInputWindowsLw");
Jeff Brown9302c872011-07-13 22:51:29 -0700153
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800154 // Populate the input window list with information about all of the windows that
155 // could potentially receive input.
156 // As an optimization, we could try to prune the list of windows but this turns
157 // out to be difficult because only the native code knows for sure which window
158 // currently has touch focus.
159 final ArrayList<WindowState> windows = mService.mWindows;
160
161 // If there's a drag in flight, provide a pseudowindow to catch drag input
162 final boolean inDrag = (mService.mDragState != null);
163 if (inDrag) {
164 if (WindowManagerService.DEBUG_DRAG) {
165 Log.d(WindowManagerService.TAG, "Inserting drag window");
166 }
Jeff Browncc4f7db2011-08-30 20:34:48 -0700167 final InputWindowHandle dragWindowHandle = mService.mDragState.mDragWindowHandle;
168 if (dragWindowHandle != null) {
169 addInputWindowHandleLw(dragWindowHandle);
170 } else {
171 Slog.w(WindowManagerService.TAG, "Drag is in progress but there is no "
172 + "drag window handle.");
173 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800174 }
175
Dianne Hackborndf89e652011-10-06 22:35:11 -0700176 final int NFW = mService.mFakeWindows.size();
177 for (int i = 0; i < NFW; i++) {
178 addInputWindowHandleLw(mService.mFakeWindows.get(i).mWindowHandle);
179 }
180
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800181 final int N = windows.size();
182 for (int i = N - 1; i >= 0; i--) {
183 final WindowState child = windows.get(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700184 final InputChannel inputChannel = child.mInputChannel;
185 final InputWindowHandle inputWindowHandle = child.mInputWindowHandle;
186 if (inputChannel == null || inputWindowHandle == null || child.mRemoved) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800187 // Skip this window because it cannot possibly receive input.
188 continue;
189 }
190
191 final int flags = child.mAttrs.flags;
192 final int type = child.mAttrs.type;
193
194 final boolean hasFocus = (child == mInputFocus);
195 final boolean isVisible = child.isVisibleLw();
196 final boolean hasWallpaper = (child == mService.mWallpaperTarget)
197 && (type != WindowManager.LayoutParams.TYPE_KEYGUARD);
198
199 // If there's a drag in progress and 'child' is a potential drop target,
200 // make sure it's been told about the drag
201 if (inDrag && isVisible) {
202 mService.mDragState.sendDragStartedIfNeededLw(child);
203 }
204
205 // Add a window to our list of input windows.
Jeff Brown9302c872011-07-13 22:51:29 -0700206 inputWindowHandle.name = child.toString();
207 inputWindowHandle.layoutParamsFlags = flags;
208 inputWindowHandle.layoutParamsType = type;
209 inputWindowHandle.dispatchingTimeoutNanos = child.getInputDispatchingTimeoutNanos();
210 inputWindowHandle.visible = isVisible;
211 inputWindowHandle.canReceiveKeys = child.canReceiveKeys();
212 inputWindowHandle.hasFocus = hasFocus;
213 inputWindowHandle.hasWallpaper = hasWallpaper;
214 inputWindowHandle.paused = child.mAppToken != null ? child.mAppToken.paused : false;
215 inputWindowHandle.layer = child.mLayer;
216 inputWindowHandle.ownerPid = child.mSession.mPid;
217 inputWindowHandle.ownerUid = child.mSession.mUid;
218 inputWindowHandle.inputFeatures = child.mAttrs.inputFeatures;
Jeff Brown474dcb52011-06-14 20:22:50 -0700219
Dianne Hackbornffb3d932011-05-17 17:44:51 -0700220 final Rect frame = child.mFrame;
Jeff Brown9302c872011-07-13 22:51:29 -0700221 inputWindowHandle.frameLeft = frame.left;
222 inputWindowHandle.frameTop = frame.top;
223 inputWindowHandle.frameRight = frame.right;
224 inputWindowHandle.frameBottom = frame.bottom;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800225
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400226 if (child.mGlobalScale != 1) {
227 // If we are scaling the window, input coordinates need
228 // to be inversely scaled to map from what is on screen
229 // to what is actually being touched in the UI.
Jeff Brown9302c872011-07-13 22:51:29 -0700230 inputWindowHandle.scaleFactor = 1.0f/child.mGlobalScale;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400231 } else {
Jeff Brown9302c872011-07-13 22:51:29 -0700232 inputWindowHandle.scaleFactor = 1;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400233 }
234
Jeff Brown9302c872011-07-13 22:51:29 -0700235 child.getTouchableRegion(inputWindowHandle.touchableRegion);
236
237 addInputWindowHandleLw(inputWindowHandle);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800238 }
239
240 // Send windows to native code.
Jeff Brown9302c872011-07-13 22:51:29 -0700241 mService.mInputManager.setInputWindows(mInputWindowHandles);
242
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800243 // Clear the list in preparation for the next round.
Jeff Brown9302c872011-07-13 22:51:29 -0700244 clearInputWindowHandlesLw();
245
Dianne Hackbornb961cd22011-06-21 12:13:37 -0700246 if (false) Slog.d(WindowManagerService.TAG, "<<<<<<< EXITED updateInputWindowsLw");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800247 }
248
249 /* Notifies that the input device configuration has changed. */
250 public void notifyConfigurationChanged() {
251 mService.sendNewConfiguration();
252
253 synchronized (mInputDevicesReadyMonitor) {
254 if (!mInputDevicesReady) {
255 mInputDevicesReady = true;
256 mInputDevicesReadyMonitor.notifyAll();
257 }
258 }
259 }
260
261 /* Waits until the built-in input devices have been configured. */
262 public boolean waitForInputDevicesReady(long timeoutMillis) {
263 synchronized (mInputDevicesReadyMonitor) {
264 if (!mInputDevicesReady) {
265 try {
266 mInputDevicesReadyMonitor.wait(timeoutMillis);
267 } catch (InterruptedException ex) {
268 }
269 }
270 return mInputDevicesReady;
271 }
272 }
273
274 /* Notifies that the lid switch changed state. */
275 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) {
276 mService.mPolicy.notifyLidSwitchChanged(whenNanos, lidOpen);
277 }
278
279 /* Provides an opportunity for the window manager policy to intercept early key
280 * processing as soon as the key has been read from the device. */
281 public int interceptKeyBeforeQueueing(
282 KeyEvent event, int policyFlags, boolean isScreenOn) {
283 return mService.mPolicy.interceptKeyBeforeQueueing(event, policyFlags, isScreenOn);
284 }
Jeff Brown56194eb2011-03-02 19:23:13 -0800285
286 /* Provides an opportunity for the window manager policy to intercept early
287 * motion event processing when the screen is off since these events are normally
288 * dropped. */
289 public int interceptMotionBeforeQueueingWhenScreenOff(int policyFlags) {
290 return mService.mPolicy.interceptMotionBeforeQueueingWhenScreenOff(policyFlags);
291 }
292
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800293 /* Provides an opportunity for the window manager policy to process a key before
294 * ordinary dispatch. */
Jeff Brown905805a2011-10-12 13:57:59 -0700295 public long interceptKeyBeforeDispatching(
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800296 InputWindowHandle focus, KeyEvent event, int policyFlags) {
297 WindowState windowState = focus != null ? (WindowState) focus.windowState : null;
298 return mService.mPolicy.interceptKeyBeforeDispatching(windowState, event, policyFlags);
299 }
300
301 /* Provides an opportunity for the window manager policy to process a key that
302 * the application did not handle. */
303 public KeyEvent dispatchUnhandledKey(
304 InputWindowHandle focus, KeyEvent event, int policyFlags) {
305 WindowState windowState = focus != null ? (WindowState) focus.windowState : null;
306 return mService.mPolicy.dispatchUnhandledKey(windowState, event, policyFlags);
307 }
Jeff Brown4532e612012-04-05 14:27:12 -0700308
309 /* Callback to get pointer layer. */
310 public int getPointerLayer() {
311 return mService.mPolicy.windowTypeToLayerLw(WindowManager.LayoutParams.TYPE_POINTER)
312 * WindowManagerService.TYPE_LAYER_MULTIPLIER
313 + WindowManagerService.TYPE_LAYER_OFFSET;
314 }
315
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800316 /* Called when the current input focus changes.
317 * Layer assignment is assumed to be complete by the time this is called.
318 */
319 public void setInputFocusLw(WindowState newWindow, boolean updateInputWindows) {
320 if (WindowManagerService.DEBUG_INPUT) {
321 Slog.d(WindowManagerService.TAG, "Input focus has changed to " + newWindow);
322 }
323
324 if (newWindow != mInputFocus) {
325 if (newWindow != null && newWindow.canReceiveKeys()) {
326 // Displaying a window implicitly causes dispatching to be unpaused.
327 // This is to protect against bugs if someone pauses dispatching but
328 // forgets to resume.
329 newWindow.mToken.paused = false;
330 }
331
332 mInputFocus = newWindow;
333 setUpdateInputWindowsNeededLw();
334
335 if (updateInputWindows) {
336 updateInputWindowsLw(false /*force*/);
337 }
338 }
339 }
340
341 public void setFocusedAppLw(AppWindowToken newApp) {
342 // Focused app has changed.
343 if (newApp == null) {
344 mService.mInputManager.setFocusedApplication(null);
345 } else {
Jeff Brown9302c872011-07-13 22:51:29 -0700346 final InputApplicationHandle handle = newApp.mInputApplicationHandle;
347 handle.name = newApp.toString();
348 handle.dispatchingTimeoutNanos = newApp.inputDispatchingTimeoutNanos;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800349
Jeff Brown9302c872011-07-13 22:51:29 -0700350 mService.mInputManager.setFocusedApplication(handle);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800351 }
352 }
353
354 public void pauseDispatchingLw(WindowToken window) {
355 if (! window.paused) {
356 if (WindowManagerService.DEBUG_INPUT) {
357 Slog.v(WindowManagerService.TAG, "Pausing WindowToken " + window);
358 }
359
360 window.paused = true;
361 updateInputWindowsLw(true /*force*/);
362 }
363 }
364
365 public void resumeDispatchingLw(WindowToken window) {
366 if (window.paused) {
367 if (WindowManagerService.DEBUG_INPUT) {
368 Slog.v(WindowManagerService.TAG, "Resuming WindowToken " + window);
369 }
370
371 window.paused = false;
372 updateInputWindowsLw(true /*force*/);
373 }
374 }
375
376 public void freezeInputDispatchingLw() {
377 if (! mInputDispatchFrozen) {
378 if (WindowManagerService.DEBUG_INPUT) {
379 Slog.v(WindowManagerService.TAG, "Freezing input dispatching");
380 }
381
382 mInputDispatchFrozen = true;
383 updateInputDispatchModeLw();
384 }
385 }
386
387 public void thawInputDispatchingLw() {
388 if (mInputDispatchFrozen) {
389 if (WindowManagerService.DEBUG_INPUT) {
390 Slog.v(WindowManagerService.TAG, "Thawing input dispatching");
391 }
392
393 mInputDispatchFrozen = false;
394 updateInputDispatchModeLw();
395 }
396 }
397
398 public void setEventDispatchingLw(boolean enabled) {
399 if (mInputDispatchEnabled != enabled) {
400 if (WindowManagerService.DEBUG_INPUT) {
401 Slog.v(WindowManagerService.TAG, "Setting event dispatching to " + enabled);
402 }
403
404 mInputDispatchEnabled = enabled;
405 updateInputDispatchModeLw();
406 }
407 }
408
409 private void updateInputDispatchModeLw() {
410 mService.mInputManager.setInputDispatchMode(mInputDispatchEnabled, mInputDispatchFrozen);
411 }
Jeff Brownea426552011-07-18 16:53:48 -0700412}