blob: c4bb5194cfd195e622d960bd7a091a7b6ae7502a [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.
Jeff Brownc042ee22012-05-08 13:03:42 -070044 // Initially false, so that input does not get dispatched until boot is finished at
45 // which point the ActivityManager will enable dispatching.
46 private boolean mInputDispatchEnabled;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080047
48 // When true, need to call updateInputWindowsLw().
49 private boolean mUpdateInputWindowsNeeded = true;
50
Jeff Brown9302c872011-07-13 22:51:29 -070051 // Array of window handles to provide to the input dispatcher.
52 private InputWindowHandle[] mInputWindowHandles;
53 private int mInputWindowHandleCount;
54
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080055 // Set to true when the first input device configuration change notification
56 // is received to indicate that the input devices are ready.
57 private final Object mInputDevicesReadyMonitor = new Object();
58 private boolean mInputDevicesReady;
59
60 public InputMonitor(WindowManagerService service) {
61 mService = service;
62 }
63
64 /* Notifies the window manager about a broken input channel.
65 *
66 * Called by the InputManager.
67 */
68 public void notifyInputChannelBroken(InputWindowHandle inputWindowHandle) {
69 if (inputWindowHandle == null) {
70 return;
71 }
72
73 synchronized (mService.mWindowMap) {
74 WindowState windowState = (WindowState) inputWindowHandle.windowState;
Jeff Brown9302c872011-07-13 22:51:29 -070075 if (windowState != null) {
76 Slog.i(WindowManagerService.TAG, "WINDOW DIED " + windowState);
77 mService.removeWindowLocked(windowState.mSession, windowState);
78 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080079 }
80 }
81
82 /* Notifies the window manager about an application that is not responding.
83 * Returns a new timeout to continue waiting in nanoseconds, or 0 to abort dispatch.
84 *
85 * Called by the InputManager.
86 */
87 public long notifyANR(InputApplicationHandle inputApplicationHandle,
88 InputWindowHandle inputWindowHandle) {
89 AppWindowToken appWindowToken = null;
90 if (inputWindowHandle != null) {
91 synchronized (mService.mWindowMap) {
92 WindowState windowState = (WindowState) inputWindowHandle.windowState;
93 if (windowState != null) {
94 Slog.i(WindowManagerService.TAG, "Input event dispatching timed out sending to "
95 + windowState.mAttrs.getTitle());
96 appWindowToken = windowState.mAppToken;
97 }
98 }
99 }
100
101 if (appWindowToken == null && inputApplicationHandle != null) {
Jeff Brown4532e612012-04-05 14:27:12 -0700102 appWindowToken = (AppWindowToken)inputApplicationHandle.appWindowToken;
Jeff Brown9302c872011-07-13 22:51:29 -0700103 if (appWindowToken != null) {
104 Slog.i(WindowManagerService.TAG,
105 "Input event dispatching timed out sending to application "
106 + appWindowToken.stringName);
107 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800108 }
109
110 if (appWindowToken != null && appWindowToken.appToken != null) {
111 try {
112 // Notify the activity manager about the timeout and let it decide whether
113 // to abort dispatching or keep waiting.
114 boolean abort = appWindowToken.appToken.keyDispatchingTimedOut();
115 if (! abort) {
116 // The activity manager declined to abort dispatching.
117 // Wait a bit longer and timeout again later.
118 return appWindowToken.inputDispatchingTimeoutNanos;
119 }
120 } catch (RemoteException ex) {
121 }
122 }
123 return 0; // abort dispatching
124 }
125
Jeff Brown9302c872011-07-13 22:51:29 -0700126 private void addInputWindowHandleLw(InputWindowHandle windowHandle) {
127 if (mInputWindowHandles == null) {
128 mInputWindowHandles = new InputWindowHandle[16];
129 }
130 if (mInputWindowHandleCount >= mInputWindowHandles.length) {
131 mInputWindowHandles = Arrays.copyOf(mInputWindowHandles,
132 mInputWindowHandleCount * 2);
133 }
134 mInputWindowHandles[mInputWindowHandleCount++] = windowHandle;
135 }
136
137 private void clearInputWindowHandlesLw() {
138 while (mInputWindowHandleCount != 0) {
139 mInputWindowHandles[--mInputWindowHandleCount] = null;
140 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800141 }
142
143 public void setUpdateInputWindowsNeededLw() {
144 mUpdateInputWindowsNeeded = true;
145 }
146
147 /* Updates the cached window information provided to the input dispatcher. */
148 public void updateInputWindowsLw(boolean force) {
149 if (!force && !mUpdateInputWindowsNeeded) {
150 return;
151 }
152 mUpdateInputWindowsNeeded = false;
153
Dianne Hackbornb961cd22011-06-21 12:13:37 -0700154 if (false) Slog.d(WindowManagerService.TAG, ">>>>>> ENTERED updateInputWindowsLw");
Jeff Brown9302c872011-07-13 22:51:29 -0700155
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800156 // Populate the input window list with information about all of the windows that
157 // could potentially receive input.
158 // As an optimization, we could try to prune the list of windows but this turns
159 // out to be difficult because only the native code knows for sure which window
160 // currently has touch focus.
161 final ArrayList<WindowState> windows = mService.mWindows;
162
163 // If there's a drag in flight, provide a pseudowindow to catch drag input
164 final boolean inDrag = (mService.mDragState != null);
165 if (inDrag) {
166 if (WindowManagerService.DEBUG_DRAG) {
167 Log.d(WindowManagerService.TAG, "Inserting drag window");
168 }
Jeff Browncc4f7db2011-08-30 20:34:48 -0700169 final InputWindowHandle dragWindowHandle = mService.mDragState.mDragWindowHandle;
170 if (dragWindowHandle != null) {
171 addInputWindowHandleLw(dragWindowHandle);
172 } else {
173 Slog.w(WindowManagerService.TAG, "Drag is in progress but there is no "
174 + "drag window handle.");
175 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800176 }
177
Dianne Hackborndf89e652011-10-06 22:35:11 -0700178 final int NFW = mService.mFakeWindows.size();
179 for (int i = 0; i < NFW; i++) {
180 addInputWindowHandleLw(mService.mFakeWindows.get(i).mWindowHandle);
181 }
182
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800183 final int N = windows.size();
184 for (int i = N - 1; i >= 0; i--) {
185 final WindowState child = windows.get(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700186 final InputChannel inputChannel = child.mInputChannel;
187 final InputWindowHandle inputWindowHandle = child.mInputWindowHandle;
188 if (inputChannel == null || inputWindowHandle == null || child.mRemoved) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800189 // Skip this window because it cannot possibly receive input.
190 continue;
191 }
192
193 final int flags = child.mAttrs.flags;
194 final int type = child.mAttrs.type;
195
196 final boolean hasFocus = (child == mInputFocus);
197 final boolean isVisible = child.isVisibleLw();
198 final boolean hasWallpaper = (child == mService.mWallpaperTarget)
199 && (type != WindowManager.LayoutParams.TYPE_KEYGUARD);
200
201 // If there's a drag in progress and 'child' is a potential drop target,
202 // make sure it's been told about the drag
203 if (inDrag && isVisible) {
204 mService.mDragState.sendDragStartedIfNeededLw(child);
205 }
206
207 // Add a window to our list of input windows.
Jeff Brown9302c872011-07-13 22:51:29 -0700208 inputWindowHandle.name = child.toString();
209 inputWindowHandle.layoutParamsFlags = flags;
210 inputWindowHandle.layoutParamsType = type;
211 inputWindowHandle.dispatchingTimeoutNanos = child.getInputDispatchingTimeoutNanos();
212 inputWindowHandle.visible = isVisible;
213 inputWindowHandle.canReceiveKeys = child.canReceiveKeys();
214 inputWindowHandle.hasFocus = hasFocus;
215 inputWindowHandle.hasWallpaper = hasWallpaper;
216 inputWindowHandle.paused = child.mAppToken != null ? child.mAppToken.paused : false;
217 inputWindowHandle.layer = child.mLayer;
218 inputWindowHandle.ownerPid = child.mSession.mPid;
219 inputWindowHandle.ownerUid = child.mSession.mUid;
220 inputWindowHandle.inputFeatures = child.mAttrs.inputFeatures;
Jeff Brown474dcb52011-06-14 20:22:50 -0700221
Dianne Hackbornffb3d932011-05-17 17:44:51 -0700222 final Rect frame = child.mFrame;
Jeff Brown9302c872011-07-13 22:51:29 -0700223 inputWindowHandle.frameLeft = frame.left;
224 inputWindowHandle.frameTop = frame.top;
225 inputWindowHandle.frameRight = frame.right;
226 inputWindowHandle.frameBottom = frame.bottom;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800227
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400228 if (child.mGlobalScale != 1) {
229 // If we are scaling the window, input coordinates need
230 // to be inversely scaled to map from what is on screen
231 // to what is actually being touched in the UI.
Jeff Brown9302c872011-07-13 22:51:29 -0700232 inputWindowHandle.scaleFactor = 1.0f/child.mGlobalScale;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400233 } else {
Jeff Brown9302c872011-07-13 22:51:29 -0700234 inputWindowHandle.scaleFactor = 1;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400235 }
236
Jeff Brown9302c872011-07-13 22:51:29 -0700237 child.getTouchableRegion(inputWindowHandle.touchableRegion);
238
239 addInputWindowHandleLw(inputWindowHandle);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800240 }
241
242 // Send windows to native code.
Jeff Brown9302c872011-07-13 22:51:29 -0700243 mService.mInputManager.setInputWindows(mInputWindowHandles);
244
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800245 // Clear the list in preparation for the next round.
Jeff Brown9302c872011-07-13 22:51:29 -0700246 clearInputWindowHandlesLw();
247
Dianne Hackbornb961cd22011-06-21 12:13:37 -0700248 if (false) Slog.d(WindowManagerService.TAG, "<<<<<<< EXITED updateInputWindowsLw");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800249 }
250
251 /* Notifies that the input device configuration has changed. */
252 public void notifyConfigurationChanged() {
253 mService.sendNewConfiguration();
254
255 synchronized (mInputDevicesReadyMonitor) {
256 if (!mInputDevicesReady) {
257 mInputDevicesReady = true;
258 mInputDevicesReadyMonitor.notifyAll();
259 }
260 }
261 }
262
263 /* Waits until the built-in input devices have been configured. */
264 public boolean waitForInputDevicesReady(long timeoutMillis) {
265 synchronized (mInputDevicesReadyMonitor) {
266 if (!mInputDevicesReady) {
267 try {
268 mInputDevicesReadyMonitor.wait(timeoutMillis);
269 } catch (InterruptedException ex) {
270 }
271 }
272 return mInputDevicesReady;
273 }
274 }
275
276 /* Notifies that the lid switch changed state. */
277 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) {
278 mService.mPolicy.notifyLidSwitchChanged(whenNanos, lidOpen);
279 }
280
281 /* Provides an opportunity for the window manager policy to intercept early key
282 * processing as soon as the key has been read from the device. */
283 public int interceptKeyBeforeQueueing(
284 KeyEvent event, int policyFlags, boolean isScreenOn) {
285 return mService.mPolicy.interceptKeyBeforeQueueing(event, policyFlags, isScreenOn);
286 }
Jeff Brown56194eb2011-03-02 19:23:13 -0800287
288 /* Provides an opportunity for the window manager policy to intercept early
289 * motion event processing when the screen is off since these events are normally
290 * dropped. */
291 public int interceptMotionBeforeQueueingWhenScreenOff(int policyFlags) {
292 return mService.mPolicy.interceptMotionBeforeQueueingWhenScreenOff(policyFlags);
293 }
294
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800295 /* Provides an opportunity for the window manager policy to process a key before
296 * ordinary dispatch. */
Jeff Brown905805a2011-10-12 13:57:59 -0700297 public long interceptKeyBeforeDispatching(
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800298 InputWindowHandle focus, KeyEvent event, int policyFlags) {
299 WindowState windowState = focus != null ? (WindowState) focus.windowState : null;
300 return mService.mPolicy.interceptKeyBeforeDispatching(windowState, event, policyFlags);
301 }
302
303 /* Provides an opportunity for the window manager policy to process a key that
304 * the application did not handle. */
305 public KeyEvent dispatchUnhandledKey(
306 InputWindowHandle focus, KeyEvent event, int policyFlags) {
307 WindowState windowState = focus != null ? (WindowState) focus.windowState : null;
308 return mService.mPolicy.dispatchUnhandledKey(windowState, event, policyFlags);
309 }
Jeff Brown4532e612012-04-05 14:27:12 -0700310
311 /* Callback to get pointer layer. */
312 public int getPointerLayer() {
313 return mService.mPolicy.windowTypeToLayerLw(WindowManager.LayoutParams.TYPE_POINTER)
314 * WindowManagerService.TYPE_LAYER_MULTIPLIER
315 + WindowManagerService.TYPE_LAYER_OFFSET;
316 }
317
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800318 /* Called when the current input focus changes.
319 * Layer assignment is assumed to be complete by the time this is called.
320 */
321 public void setInputFocusLw(WindowState newWindow, boolean updateInputWindows) {
322 if (WindowManagerService.DEBUG_INPUT) {
323 Slog.d(WindowManagerService.TAG, "Input focus has changed to " + newWindow);
324 }
325
326 if (newWindow != mInputFocus) {
327 if (newWindow != null && newWindow.canReceiveKeys()) {
328 // Displaying a window implicitly causes dispatching to be unpaused.
329 // This is to protect against bugs if someone pauses dispatching but
330 // forgets to resume.
331 newWindow.mToken.paused = false;
332 }
333
334 mInputFocus = newWindow;
335 setUpdateInputWindowsNeededLw();
336
337 if (updateInputWindows) {
338 updateInputWindowsLw(false /*force*/);
339 }
340 }
341 }
342
343 public void setFocusedAppLw(AppWindowToken newApp) {
344 // Focused app has changed.
345 if (newApp == null) {
346 mService.mInputManager.setFocusedApplication(null);
347 } else {
Jeff Brown9302c872011-07-13 22:51:29 -0700348 final InputApplicationHandle handle = newApp.mInputApplicationHandle;
349 handle.name = newApp.toString();
350 handle.dispatchingTimeoutNanos = newApp.inputDispatchingTimeoutNanos;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800351
Jeff Brown9302c872011-07-13 22:51:29 -0700352 mService.mInputManager.setFocusedApplication(handle);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800353 }
354 }
355
356 public void pauseDispatchingLw(WindowToken window) {
357 if (! window.paused) {
358 if (WindowManagerService.DEBUG_INPUT) {
359 Slog.v(WindowManagerService.TAG, "Pausing WindowToken " + window);
360 }
361
362 window.paused = true;
363 updateInputWindowsLw(true /*force*/);
364 }
365 }
366
367 public void resumeDispatchingLw(WindowToken window) {
368 if (window.paused) {
369 if (WindowManagerService.DEBUG_INPUT) {
370 Slog.v(WindowManagerService.TAG, "Resuming WindowToken " + window);
371 }
372
373 window.paused = false;
374 updateInputWindowsLw(true /*force*/);
375 }
376 }
377
378 public void freezeInputDispatchingLw() {
379 if (! mInputDispatchFrozen) {
380 if (WindowManagerService.DEBUG_INPUT) {
381 Slog.v(WindowManagerService.TAG, "Freezing input dispatching");
382 }
383
384 mInputDispatchFrozen = true;
385 updateInputDispatchModeLw();
386 }
387 }
388
389 public void thawInputDispatchingLw() {
390 if (mInputDispatchFrozen) {
391 if (WindowManagerService.DEBUG_INPUT) {
392 Slog.v(WindowManagerService.TAG, "Thawing input dispatching");
393 }
394
395 mInputDispatchFrozen = false;
396 updateInputDispatchModeLw();
397 }
398 }
399
400 public void setEventDispatchingLw(boolean enabled) {
401 if (mInputDispatchEnabled != enabled) {
402 if (WindowManagerService.DEBUG_INPUT) {
403 Slog.v(WindowManagerService.TAG, "Setting event dispatching to " + enabled);
404 }
405
406 mInputDispatchEnabled = enabled;
407 updateInputDispatchModeLw();
408 }
409 }
410
411 private void updateInputDispatchModeLw() {
412 mService.mInputManager.setInputDispatchMode(mInputDispatchEnabled, mInputDispatchFrozen);
413 }
Jeff Brownea426552011-07-18 16:53:48 -0700414}