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