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