InputMonitor.java revision e05f5014905569d69d33ff323a3c62c046552789
1/* 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. 15 */ 16 17package com.android.server.wm; 18 19import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DRAG; 20import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT; 21import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT; 22import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TASK_POSITIONING; 23import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; 24 25import android.app.ActivityManagerNative; 26import android.graphics.Rect; 27import android.os.Debug; 28import android.os.RemoteException; 29import android.util.Log; 30import android.util.Slog; 31import android.view.Display; 32import android.view.InputChannel; 33import android.view.KeyEvent; 34import android.view.WindowManager; 35 36import com.android.server.input.InputApplicationHandle; 37import com.android.server.input.InputManagerService; 38import com.android.server.input.InputWindowHandle; 39 40import java.io.PrintWriter; 41import java.util.Arrays; 42 43final class InputMonitor implements InputManagerService.WindowManagerCallbacks { 44 private final WindowManagerService mService; 45 46 // Current window with input focus for keys and other non-touch events. May be null. 47 private WindowState mInputFocus; 48 49 // When true, prevents input dispatch from proceeding until set to false again. 50 private boolean mInputDispatchFrozen; 51 52 // The reason the input is currently frozen or null if the input isn't frozen. 53 private String mInputFreezeReason = null; 54 55 // When true, input dispatch proceeds normally. Otherwise all events are dropped. 56 // Initially false, so that input does not get dispatched until boot is finished at 57 // which point the ActivityManager will enable dispatching. 58 private boolean mInputDispatchEnabled; 59 60 // When true, need to call updateInputWindowsLw(). 61 private boolean mUpdateInputWindowsNeeded = true; 62 63 // Array of window handles to provide to the input dispatcher. 64 private InputWindowHandle[] mInputWindowHandles; 65 private int mInputWindowHandleCount; 66 67 // Set to true when the first input device configuration change notification 68 // is received to indicate that the input devices are ready. 69 private final Object mInputDevicesReadyMonitor = new Object(); 70 private boolean mInputDevicesReady; 71 72 public InputMonitor(WindowManagerService service) { 73 mService = service; 74 } 75 76 /* Notifies the window manager about a broken input channel. 77 * 78 * Called by the InputManager. 79 */ 80 @Override 81 public void notifyInputChannelBroken(InputWindowHandle inputWindowHandle) { 82 if (inputWindowHandle == null) { 83 return; 84 } 85 86 synchronized (mService.mWindowMap) { 87 WindowState windowState = (WindowState) inputWindowHandle.windowState; 88 if (windowState != null) { 89 Slog.i(TAG_WM, "WINDOW DIED " + windowState); 90 windowState.removeIfPossible(); 91 } 92 } 93 } 94 95 /* Notifies the window manager about an application that is not responding. 96 * Returns a new timeout to continue waiting in nanoseconds, or 0 to abort dispatch. 97 * 98 * Called by the InputManager. 99 */ 100 @Override 101 public long notifyANR(InputApplicationHandle inputApplicationHandle, 102 InputWindowHandle inputWindowHandle, String reason) { 103 AppWindowToken appWindowToken = null; 104 WindowState windowState = null; 105 boolean aboveSystem = false; 106 synchronized (mService.mWindowMap) { 107 if (inputWindowHandle != null) { 108 windowState = (WindowState) inputWindowHandle.windowState; 109 if (windowState != null) { 110 appWindowToken = windowState.mAppToken; 111 } 112 } 113 if (appWindowToken == null && inputApplicationHandle != null) { 114 appWindowToken = (AppWindowToken)inputApplicationHandle.appWindowToken; 115 } 116 117 if (windowState != null) { 118 Slog.i(TAG_WM, "Input event dispatching timed out " 119 + "sending to " + windowState.mAttrs.getTitle() 120 + ". Reason: " + reason); 121 // Figure out whether this window is layered above system windows. 122 // We need to do this here to help the activity manager know how to 123 // layer its ANR dialog. 124 int systemAlertLayer = mService.mPolicy.windowTypeToLayerLw( 125 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 126 aboveSystem = windowState.mBaseLayer > systemAlertLayer; 127 } else if (appWindowToken != null) { 128 Slog.i(TAG_WM, "Input event dispatching timed out " 129 + "sending to application " + appWindowToken.stringName 130 + ". Reason: " + reason); 131 } else { 132 Slog.i(TAG_WM, "Input event dispatching timed out " 133 + ". Reason: " + reason); 134 } 135 136 mService.saveANRStateLocked(appWindowToken, windowState, reason); 137 } 138 139 if (appWindowToken != null && appWindowToken.appToken != null) { 140 try { 141 // Notify the activity manager about the timeout and let it decide whether 142 // to abort dispatching or keep waiting. 143 boolean abort = appWindowToken.appToken.keyDispatchingTimedOut(reason); 144 if (! abort) { 145 // The activity manager declined to abort dispatching. 146 // Wait a bit longer and timeout again later. 147 return appWindowToken.inputDispatchingTimeoutNanos; 148 } 149 } catch (RemoteException ex) { 150 } 151 } else if (windowState != null) { 152 try { 153 // Notify the activity manager about the timeout and let it decide whether 154 // to abort dispatching or keep waiting. 155 long timeout = ActivityManagerNative.getDefault().inputDispatchingTimedOut( 156 windowState.mSession.mPid, aboveSystem, reason); 157 if (timeout >= 0) { 158 // The activity manager declined to abort dispatching. 159 // Wait a bit longer and timeout again later. 160 return timeout * 1000000L; // nanoseconds 161 } 162 } catch (RemoteException ex) { 163 } 164 } 165 return 0; // abort dispatching 166 } 167 168 void addInputWindowHandle(final InputWindowHandle windowHandle) { 169 if (mInputWindowHandles == null) { 170 mInputWindowHandles = new InputWindowHandle[16]; 171 } 172 if (mInputWindowHandleCount >= mInputWindowHandles.length) { 173 mInputWindowHandles = Arrays.copyOf(mInputWindowHandles, 174 mInputWindowHandleCount * 2); 175 } 176 mInputWindowHandles[mInputWindowHandleCount++] = windowHandle; 177 } 178 179 void addInputWindowHandle(final InputWindowHandle inputWindowHandle, 180 final WindowState child, int flags, final int type, final boolean isVisible, 181 final boolean hasFocus, final boolean hasWallpaper) { 182 // Add a window to our list of input windows. 183 inputWindowHandle.name = child.toString(); 184 flags = child.getTouchableRegion(inputWindowHandle.touchableRegion, flags); 185 inputWindowHandle.layoutParamsFlags = flags; 186 inputWindowHandle.layoutParamsType = type; 187 inputWindowHandle.dispatchingTimeoutNanos = child.getInputDispatchingTimeoutNanos(); 188 inputWindowHandle.visible = isVisible; 189 inputWindowHandle.canReceiveKeys = child.canReceiveKeys(); 190 inputWindowHandle.hasFocus = hasFocus; 191 inputWindowHandle.hasWallpaper = hasWallpaper; 192 inputWindowHandle.paused = child.mAppToken != null ? child.mAppToken.paused : false; 193 inputWindowHandle.layer = child.mLayer; 194 inputWindowHandle.ownerPid = child.mSession.mPid; 195 inputWindowHandle.ownerUid = child.mSession.mUid; 196 inputWindowHandle.inputFeatures = child.mAttrs.inputFeatures; 197 198 final Rect frame = child.mFrame; 199 inputWindowHandle.frameLeft = frame.left; 200 inputWindowHandle.frameTop = frame.top; 201 inputWindowHandle.frameRight = frame.right; 202 inputWindowHandle.frameBottom = frame.bottom; 203 204 if (child.mGlobalScale != 1) { 205 // If we are scaling the window, input coordinates need 206 // to be inversely scaled to map from what is on screen 207 // to what is actually being touched in the UI. 208 inputWindowHandle.scaleFactor = 1.0f/child.mGlobalScale; 209 } else { 210 inputWindowHandle.scaleFactor = 1; 211 } 212 213 if (DEBUG_INPUT) { 214 Slog.d(TAG_WM, "addInputWindowHandle: " 215 + child + ", " + inputWindowHandle); 216 } 217 addInputWindowHandle(inputWindowHandle); 218 } 219 220 private void clearInputWindowHandlesLw() { 221 while (mInputWindowHandleCount != 0) { 222 mInputWindowHandles[--mInputWindowHandleCount] = null; 223 } 224 } 225 226 public void setUpdateInputWindowsNeededLw() { 227 mUpdateInputWindowsNeeded = true; 228 } 229 230 /* Updates the cached window information provided to the input dispatcher. */ 231 public void updateInputWindowsLw(boolean force) { 232 if (!force && !mUpdateInputWindowsNeeded) { 233 return; 234 } 235 mUpdateInputWindowsNeeded = false; 236 237 if (false) Slog.d(TAG_WM, ">>>>>> ENTERED updateInputWindowsLw"); 238 239 // Populate the input window list with information about all of the windows that 240 // could potentially receive input. 241 // As an optimization, we could try to prune the list of windows but this turns 242 // out to be difficult because only the native code knows for sure which window 243 // currently has touch focus. 244 245 // If there's a drag in flight, provide a pseudo-window to catch drag input 246 final boolean inDrag = (mService.mDragState != null); 247 if (inDrag) { 248 if (DEBUG_DRAG) { 249 Log.d(TAG_WM, "Inserting drag window"); 250 } 251 final InputWindowHandle dragWindowHandle = mService.mDragState.getInputWindowHandle(); 252 if (dragWindowHandle != null) { 253 addInputWindowHandle(dragWindowHandle); 254 } else { 255 Slog.w(TAG_WM, "Drag is in progress but there is no " 256 + "drag window handle."); 257 } 258 } 259 260 final boolean inPositioning = (mService.mTaskPositioner != null); 261 if (inPositioning) { 262 if (DEBUG_TASK_POSITIONING) { 263 Log.d(TAG_WM, "Inserting window handle for repositioning"); 264 } 265 final InputWindowHandle dragWindowHandle = mService.mTaskPositioner.mDragWindowHandle; 266 if (dragWindowHandle != null) { 267 addInputWindowHandle(dragWindowHandle); 268 } else { 269 Slog.e(TAG_WM, 270 "Repositioning is in progress but there is no drag window handle."); 271 } 272 } 273 274 // Add all windows on the default display. 275 mService.mRoot.updateInputWindows(this, mInputFocus, inDrag); 276 277 // Send windows to native code. 278 mService.mInputManager.setInputWindows(mInputWindowHandles); 279 280 // Clear the list in preparation for the next round. 281 clearInputWindowHandlesLw(); 282 283 if (false) Slog.d(TAG_WM, "<<<<<<< EXITED updateInputWindowsLw"); 284 } 285 286 /* Notifies that the input device configuration has changed. */ 287 @Override 288 public void notifyConfigurationChanged() { 289 mService.sendNewConfiguration(); 290 291 synchronized (mInputDevicesReadyMonitor) { 292 if (!mInputDevicesReady) { 293 mInputDevicesReady = true; 294 mInputDevicesReadyMonitor.notifyAll(); 295 } 296 } 297 } 298 299 /* Waits until the built-in input devices have been configured. */ 300 public boolean waitForInputDevicesReady(long timeoutMillis) { 301 synchronized (mInputDevicesReadyMonitor) { 302 if (!mInputDevicesReady) { 303 try { 304 mInputDevicesReadyMonitor.wait(timeoutMillis); 305 } catch (InterruptedException ex) { 306 } 307 } 308 return mInputDevicesReady; 309 } 310 } 311 312 /* Notifies that the lid switch changed state. */ 313 @Override 314 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) { 315 mService.mPolicy.notifyLidSwitchChanged(whenNanos, lidOpen); 316 } 317 318 /* Notifies that the camera lens cover state has changed. */ 319 @Override 320 public void notifyCameraLensCoverSwitchChanged(long whenNanos, boolean lensCovered) { 321 mService.mPolicy.notifyCameraLensCoverSwitchChanged(whenNanos, lensCovered); 322 } 323 324 /* Provides an opportunity for the window manager policy to intercept early key 325 * processing as soon as the key has been read from the device. */ 326 @Override 327 public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) { 328 return mService.mPolicy.interceptKeyBeforeQueueing(event, policyFlags); 329 } 330 331 /* Provides an opportunity for the window manager policy to intercept early motion event 332 * processing when the device is in a non-interactive state since these events are normally 333 * dropped. */ 334 @Override 335 public int interceptMotionBeforeQueueingNonInteractive(long whenNanos, int policyFlags) { 336 return mService.mPolicy.interceptMotionBeforeQueueingNonInteractive( 337 whenNanos, policyFlags); 338 } 339 340 /* Provides an opportunity for the window manager policy to process a key before 341 * ordinary dispatch. */ 342 @Override 343 public long interceptKeyBeforeDispatching( 344 InputWindowHandle focus, KeyEvent event, int policyFlags) { 345 WindowState windowState = focus != null ? (WindowState) focus.windowState : null; 346 return mService.mPolicy.interceptKeyBeforeDispatching(windowState, event, policyFlags); 347 } 348 349 /* Provides an opportunity for the window manager policy to process a key that 350 * the application did not handle. */ 351 @Override 352 public KeyEvent dispatchUnhandledKey( 353 InputWindowHandle focus, KeyEvent event, int policyFlags) { 354 WindowState windowState = focus != null ? (WindowState) focus.windowState : null; 355 return mService.mPolicy.dispatchUnhandledKey(windowState, event, policyFlags); 356 } 357 358 /* Callback to get pointer layer. */ 359 @Override 360 public int getPointerLayer() { 361 return mService.mPolicy.windowTypeToLayerLw(WindowManager.LayoutParams.TYPE_POINTER) 362 * WindowManagerService.TYPE_LAYER_MULTIPLIER 363 + WindowManagerService.TYPE_LAYER_OFFSET; 364 } 365 366 /* Called when the current input focus changes. 367 * Layer assignment is assumed to be complete by the time this is called. 368 */ 369 public void setInputFocusLw(WindowState newWindow, boolean updateInputWindows) { 370 if (DEBUG_FOCUS_LIGHT || DEBUG_INPUT) { 371 Slog.d(TAG_WM, "Input focus has changed to " + newWindow); 372 } 373 374 if (newWindow != mInputFocus) { 375 if (newWindow != null && newWindow.canReceiveKeys()) { 376 // Displaying a window implicitly causes dispatching to be unpaused. 377 // This is to protect against bugs if someone pauses dispatching but 378 // forgets to resume. 379 newWindow.mToken.paused = false; 380 } 381 382 mInputFocus = newWindow; 383 setUpdateInputWindowsNeededLw(); 384 385 if (updateInputWindows) { 386 updateInputWindowsLw(false /*force*/); 387 } 388 } 389 } 390 391 public void setFocusedAppLw(AppWindowToken newApp) { 392 // Focused app has changed. 393 if (newApp == null) { 394 mService.mInputManager.setFocusedApplication(null); 395 } else { 396 final InputApplicationHandle handle = newApp.mInputApplicationHandle; 397 handle.name = newApp.toString(); 398 handle.dispatchingTimeoutNanos = newApp.inputDispatchingTimeoutNanos; 399 400 mService.mInputManager.setFocusedApplication(handle); 401 } 402 } 403 404 public void pauseDispatchingLw(WindowToken window) { 405 if (! window.paused) { 406 if (DEBUG_INPUT) { 407 Slog.v(TAG_WM, "Pausing WindowToken " + window); 408 } 409 410 window.paused = true; 411 updateInputWindowsLw(true /*force*/); 412 } 413 } 414 415 public void resumeDispatchingLw(WindowToken window) { 416 if (window.paused) { 417 if (DEBUG_INPUT) { 418 Slog.v(TAG_WM, "Resuming WindowToken " + window); 419 } 420 421 window.paused = false; 422 updateInputWindowsLw(true /*force*/); 423 } 424 } 425 426 public void freezeInputDispatchingLw() { 427 if (!mInputDispatchFrozen) { 428 if (DEBUG_INPUT) { 429 Slog.v(TAG_WM, "Freezing input dispatching"); 430 } 431 432 mInputDispatchFrozen = true; 433 434 if (DEBUG_INPUT || true) { 435 mInputFreezeReason = Debug.getCallers(6); 436 } 437 updateInputDispatchModeLw(); 438 } 439 } 440 441 public void thawInputDispatchingLw() { 442 if (mInputDispatchFrozen) { 443 if (DEBUG_INPUT) { 444 Slog.v(TAG_WM, "Thawing input dispatching"); 445 } 446 447 mInputDispatchFrozen = false; 448 mInputFreezeReason = null; 449 updateInputDispatchModeLw(); 450 } 451 } 452 453 public void setEventDispatchingLw(boolean enabled) { 454 if (mInputDispatchEnabled != enabled) { 455 if (DEBUG_INPUT) { 456 Slog.v(TAG_WM, "Setting event dispatching to " + enabled); 457 } 458 459 mInputDispatchEnabled = enabled; 460 updateInputDispatchModeLw(); 461 } 462 } 463 464 private void updateInputDispatchModeLw() { 465 mService.mInputManager.setInputDispatchMode(mInputDispatchEnabled, mInputDispatchFrozen); 466 } 467 468 void dump(PrintWriter pw, String prefix) { 469 if (mInputFreezeReason != null) { 470 pw.println(prefix + "mInputFreezeReason=" + mInputFreezeReason); 471 } 472 } 473} 474