ViewRootImpl.java revision bea0c7daa6611d8b96e1271f8854f500a87342fc
1/* 2 * Copyright (C) 2006 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 android.view; 18 19import android.Manifest; 20import android.animation.LayoutTransition; 21import android.app.ActivityManagerNative; 22import android.content.ClipDescription; 23import android.content.ComponentCallbacks; 24import android.content.Context; 25import android.content.pm.PackageManager; 26import android.content.res.CompatibilityInfo; 27import android.content.res.Configuration; 28import android.content.res.Resources; 29import android.graphics.Canvas; 30import android.graphics.Matrix; 31import android.graphics.PixelFormat; 32import android.graphics.Point; 33import android.graphics.PointF; 34import android.graphics.PorterDuff; 35import android.graphics.Rect; 36import android.graphics.Region; 37import android.graphics.drawable.Drawable; 38import android.hardware.display.DisplayManager; 39import android.hardware.display.DisplayManager.DisplayListener; 40import android.media.AudioManager; 41import android.os.Binder; 42import android.os.Build; 43import android.os.Bundle; 44import android.os.Debug; 45import android.os.Handler; 46import android.os.Looper; 47import android.os.Message; 48import android.os.ParcelFileDescriptor; 49import android.os.Process; 50import android.os.RemoteException; 51import android.os.SystemClock; 52import android.os.SystemProperties; 53import android.os.Trace; 54import android.util.AndroidRuntimeException; 55import android.util.DisplayMetrics; 56import android.util.Log; 57import android.util.Slog; 58import android.util.TimeUtils; 59import android.util.TypedValue; 60import android.view.Surface.OutOfResourcesException; 61import android.view.View.AttachInfo; 62import android.view.View.MeasureSpec; 63import android.view.accessibility.AccessibilityEvent; 64import android.view.accessibility.AccessibilityManager; 65import android.view.accessibility.AccessibilityManager.AccessibilityStateChangeListener; 66import android.view.accessibility.AccessibilityManager.HighTextContrastChangeListener; 67import android.view.accessibility.AccessibilityNodeInfo; 68import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction; 69import android.view.accessibility.AccessibilityNodeProvider; 70import android.view.accessibility.IAccessibilityInteractionConnection; 71import android.view.accessibility.IAccessibilityInteractionConnectionCallback; 72import android.view.animation.AccelerateDecelerateInterpolator; 73import android.view.animation.Interpolator; 74import android.view.inputmethod.InputConnection; 75import android.view.inputmethod.InputMethodManager; 76import android.widget.Scroller; 77 78import com.android.internal.R; 79import com.android.internal.os.SomeArgs; 80import com.android.internal.policy.PhoneFallbackEventHandler; 81import com.android.internal.view.BaseSurfaceHolder; 82import com.android.internal.view.RootViewSurfaceTaker; 83 84import java.io.FileDescriptor; 85import java.io.IOException; 86import java.io.OutputStream; 87import java.io.PrintWriter; 88import java.lang.ref.WeakReference; 89import java.util.ArrayList; 90import java.util.HashSet; 91 92/** 93 * The top of a view hierarchy, implementing the needed protocol between View 94 * and the WindowManager. This is for the most part an internal implementation 95 * detail of {@link WindowManagerGlobal}. 96 * 97 * {@hide} 98 */ 99@SuppressWarnings({"EmptyCatchBlock", "PointlessBooleanExpression"}) 100public final class ViewRootImpl implements ViewParent, 101 View.AttachInfo.Callbacks, HardwareRenderer.HardwareDrawCallbacks { 102 private static final String TAG = "ViewRootImpl"; 103 private static final boolean DBG = false; 104 private static final boolean LOCAL_LOGV = false; 105 /** @noinspection PointlessBooleanExpression*/ 106 private static final boolean DEBUG_DRAW = false || LOCAL_LOGV; 107 private static final boolean DEBUG_LAYOUT = false || LOCAL_LOGV; 108 private static final boolean DEBUG_DIALOG = false || LOCAL_LOGV; 109 private static final boolean DEBUG_INPUT_RESIZE = false || LOCAL_LOGV; 110 private static final boolean DEBUG_ORIENTATION = false || LOCAL_LOGV; 111 private static final boolean DEBUG_TRACKBALL = false || LOCAL_LOGV; 112 private static final boolean DEBUG_IMF = false || LOCAL_LOGV; 113 private static final boolean DEBUG_CONFIGURATION = false || LOCAL_LOGV; 114 private static final boolean DEBUG_FPS = false; 115 private static final boolean DEBUG_INPUT_STAGES = false || LOCAL_LOGV; 116 117 /** 118 * Set this system property to true to force the view hierarchy to render 119 * at 60 Hz. This can be used to measure the potential framerate. 120 */ 121 private static final String PROPERTY_PROFILE_RENDERING = "viewroot.profile_rendering"; 122 123 // properties used by emulator to determine display shape 124 public static final String PROPERTY_EMULATOR_WIN_OUTSET_BOTTOM_PX = 125 "ro.emu.win_outset_bottom_px"; 126 127 /** 128 * Maximum time we allow the user to roll the trackball enough to generate 129 * a key event, before resetting the counters. 130 */ 131 static final int MAX_TRACKBALL_DELAY = 250; 132 133 static final ThreadLocal<HandlerActionQueue> sRunQueues = new ThreadLocal<HandlerActionQueue>(); 134 135 static final ArrayList<Runnable> sFirstDrawHandlers = new ArrayList<Runnable>(); 136 static boolean sFirstDrawComplete = false; 137 138 static final ArrayList<ComponentCallbacks> sConfigCallbacks 139 = new ArrayList<ComponentCallbacks>(); 140 141 final Context mContext; 142 final IWindowSession mWindowSession; 143 final Display mDisplay; 144 final DisplayManager mDisplayManager; 145 final String mBasePackageName; 146 147 final int[] mTmpLocation = new int[2]; 148 149 final TypedValue mTmpValue = new TypedValue(); 150 151 final Thread mThread; 152 153 final WindowLeaked mLocation; 154 155 final WindowManager.LayoutParams mWindowAttributes = new WindowManager.LayoutParams(); 156 157 final W mWindow; 158 159 final int mTargetSdkVersion; 160 161 int mSeq; 162 163 View mView; 164 165 View mAccessibilityFocusedHost; 166 AccessibilityNodeInfo mAccessibilityFocusedVirtualView; 167 168 int mViewVisibility; 169 boolean mAppVisible = true; 170 int mOrigWindowType = -1; 171 172 // Set to true if the owner of this window is in the stopped state, 173 // so the window should no longer be active. 174 boolean mStopped = false; 175 176 // Set to true to stop input during an Activity Transition. 177 boolean mPausedForTransition = false; 178 179 boolean mLastInCompatMode = false; 180 181 SurfaceHolder.Callback2 mSurfaceHolderCallback; 182 BaseSurfaceHolder mSurfaceHolder; 183 boolean mIsCreating; 184 boolean mDrawingAllowed; 185 186 final Region mTransparentRegion; 187 final Region mPreviousTransparentRegion; 188 189 int mWidth; 190 int mHeight; 191 Rect mDirty; 192 boolean mIsAnimating; 193 194 CompatibilityInfo.Translator mTranslator; 195 196 final View.AttachInfo mAttachInfo; 197 InputChannel mInputChannel; 198 InputQueue.Callback mInputQueueCallback; 199 InputQueue mInputQueue; 200 FallbackEventHandler mFallbackEventHandler; 201 Choreographer mChoreographer; 202 203 final Rect mTempRect; // used in the transaction to not thrash the heap. 204 final Rect mVisRect; // used to retrieve visible rect of focused view. 205 206 boolean mTraversalScheduled; 207 int mTraversalBarrier; 208 boolean mWillDrawSoon; 209 /** Set to true while in performTraversals for detecting when die(true) is called from internal 210 * callbacks such as onMeasure, onPreDraw, onDraw and deferring doDie() until later. */ 211 boolean mIsInTraversal; 212 boolean mApplyInsetsRequested; 213 boolean mLayoutRequested; 214 boolean mFirst; 215 boolean mReportNextDraw; 216 boolean mFullRedrawNeeded; 217 boolean mNewSurfaceNeeded; 218 boolean mHasHadWindowFocus; 219 boolean mLastWasImTarget; 220 boolean mWindowsAnimating; 221 boolean mDrawDuringWindowsAnimating; 222 223 /** How many frames the app is still allowed to draw when a window animation is happening. */ 224 private int mRemainingFrameCount; 225 boolean mIsDrawing; 226 int mLastSystemUiVisibility; 227 int mClientWindowLayoutFlags; 228 boolean mLastOverscanRequested; 229 230 // Pool of queued input events. 231 private static final int MAX_QUEUED_INPUT_EVENT_POOL_SIZE = 10; 232 private QueuedInputEvent mQueuedInputEventPool; 233 private int mQueuedInputEventPoolSize; 234 235 /* Input event queue. 236 * Pending input events are input events waiting to be delivered to the input stages 237 * and handled by the application. 238 */ 239 QueuedInputEvent mPendingInputEventHead; 240 QueuedInputEvent mPendingInputEventTail; 241 int mPendingInputEventCount; 242 boolean mProcessInputEventsScheduled; 243 boolean mUnbufferedInputDispatch; 244 String mPendingInputEventQueueLengthCounterName = "pq"; 245 246 InputStage mFirstInputStage; 247 InputStage mFirstPostImeInputStage; 248 InputStage mSyntheticInputStage; 249 250 boolean mWindowAttributesChanged = false; 251 int mWindowAttributesChangesFlag = 0; 252 253 // These can be accessed by any thread, must be protected with a lock. 254 // Surface can never be reassigned or cleared (use Surface.clear()). 255 final Surface mSurface = new Surface(); 256 257 boolean mAdded; 258 boolean mAddedTouchMode; 259 260 final DisplayAdjustments mDisplayAdjustments; 261 262 // These are accessed by multiple threads. 263 final Rect mWinFrame; // frame given by window manager. 264 265 final Rect mPendingOverscanInsets = new Rect(); 266 final Rect mPendingVisibleInsets = new Rect(); 267 final Rect mPendingStableInsets = new Rect(); 268 final Rect mPendingContentInsets = new Rect(); 269 final Rect mPendingOutsets = new Rect(); 270 final ViewTreeObserver.InternalInsetsInfo mLastGivenInsets 271 = new ViewTreeObserver.InternalInsetsInfo(); 272 273 final Rect mDispatchContentInsets = new Rect(); 274 final Rect mDispatchStableInsets = new Rect(); 275 276 private WindowInsets mLastWindowInsets; 277 278 final Configuration mLastConfiguration = new Configuration(); 279 final Configuration mPendingConfiguration = new Configuration(); 280 281 boolean mScrollMayChange; 282 int mSoftInputMode; 283 WeakReference<View> mLastScrolledFocus; 284 int mScrollY; 285 int mCurScrollY; 286 Scroller mScroller; 287 static final Interpolator mResizeInterpolator = new AccelerateDecelerateInterpolator(); 288 private ArrayList<LayoutTransition> mPendingTransitions; 289 290 final ViewConfiguration mViewConfiguration; 291 292 /* Drag/drop */ 293 ClipDescription mDragDescription; 294 View mCurrentDragView; 295 volatile Object mLocalDragState; 296 final PointF mDragPoint = new PointF(); 297 final PointF mLastTouchPoint = new PointF(); 298 299 private boolean mProfileRendering; 300 private Choreographer.FrameCallback mRenderProfiler; 301 private boolean mRenderProfilingEnabled; 302 303 // Variables to track frames per second, enabled via DEBUG_FPS flag 304 private long mFpsStartTime = -1; 305 private long mFpsPrevTime = -1; 306 private int mFpsNumFrames; 307 308 /** 309 * see {@link #playSoundEffect(int)} 310 */ 311 AudioManager mAudioManager; 312 313 final AccessibilityManager mAccessibilityManager; 314 315 AccessibilityInteractionController mAccessibilityInteractionController; 316 317 AccessibilityInteractionConnectionManager mAccessibilityInteractionConnectionManager; 318 HighContrastTextManager mHighContrastTextManager; 319 320 SendWindowContentChangedAccessibilityEvent mSendWindowContentChangedAccessibilityEvent; 321 322 HashSet<View> mTempHashSet; 323 324 private final int mDensity; 325 private final int mNoncompatDensity; 326 327 private boolean mInLayout = false; 328 ArrayList<View> mLayoutRequesters = new ArrayList<View>(); 329 boolean mHandlingLayoutInLayoutRequest = false; 330 331 private int mViewLayoutDirectionInitial; 332 333 /** Set to true once doDie() has been called. */ 334 private boolean mRemoved; 335 336 /** 337 * Consistency verifier for debugging purposes. 338 */ 339 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier = 340 InputEventConsistencyVerifier.isInstrumentationEnabled() ? 341 new InputEventConsistencyVerifier(this, 0) : null; 342 343 static final class SystemUiVisibilityInfo { 344 int seq; 345 int globalVisibility; 346 int localValue; 347 int localChanges; 348 } 349 350 public ViewRootImpl(Context context, Display display) { 351 mContext = context; 352 mWindowSession = WindowManagerGlobal.getWindowSession(); 353 mDisplay = display; 354 mBasePackageName = context.getBasePackageName(); 355 356 mDisplayAdjustments = display.getDisplayAdjustments(); 357 358 mThread = Thread.currentThread(); 359 mLocation = new WindowLeaked(null); 360 mLocation.fillInStackTrace(); 361 mWidth = -1; 362 mHeight = -1; 363 mDirty = new Rect(); 364 mTempRect = new Rect(); 365 mVisRect = new Rect(); 366 mWinFrame = new Rect(); 367 mWindow = new W(this); 368 mTargetSdkVersion = context.getApplicationInfo().targetSdkVersion; 369 mViewVisibility = View.GONE; 370 mTransparentRegion = new Region(); 371 mPreviousTransparentRegion = new Region(); 372 mFirst = true; // true for the first time the view is added 373 mAdded = false; 374 mAttachInfo = new View.AttachInfo(mWindowSession, mWindow, display, this, mHandler, this); 375 mAccessibilityManager = AccessibilityManager.getInstance(context); 376 mAccessibilityInteractionConnectionManager = 377 new AccessibilityInteractionConnectionManager(); 378 mAccessibilityManager.addAccessibilityStateChangeListener( 379 mAccessibilityInteractionConnectionManager); 380 mHighContrastTextManager = new HighContrastTextManager(); 381 mAccessibilityManager.addHighTextContrastStateChangeListener( 382 mHighContrastTextManager); 383 mViewConfiguration = ViewConfiguration.get(context); 384 mDensity = context.getResources().getDisplayMetrics().densityDpi; 385 mNoncompatDensity = context.getResources().getDisplayMetrics().noncompatDensityDpi; 386 mFallbackEventHandler = new PhoneFallbackEventHandler(context); 387 mChoreographer = Choreographer.getInstance(); 388 mDisplayManager = (DisplayManager)context.getSystemService(Context.DISPLAY_SERVICE); 389 loadSystemProperties(); 390 } 391 392 public static void addFirstDrawHandler(Runnable callback) { 393 synchronized (sFirstDrawHandlers) { 394 if (!sFirstDrawComplete) { 395 sFirstDrawHandlers.add(callback); 396 } 397 } 398 } 399 400 public static void addConfigCallback(ComponentCallbacks callback) { 401 synchronized (sConfigCallbacks) { 402 sConfigCallbacks.add(callback); 403 } 404 } 405 406 // FIXME for perf testing only 407 private boolean mProfile = false; 408 409 /** 410 * Call this to profile the next traversal call. 411 * FIXME for perf testing only. Remove eventually 412 */ 413 public void profile() { 414 mProfile = true; 415 } 416 417 /** 418 * Indicates whether we are in touch mode. Calling this method triggers an IPC 419 * call and should be avoided whenever possible. 420 * 421 * @return True, if the device is in touch mode, false otherwise. 422 * 423 * @hide 424 */ 425 static boolean isInTouchMode() { 426 IWindowSession windowSession = WindowManagerGlobal.peekWindowSession(); 427 if (windowSession != null) { 428 try { 429 return windowSession.getInTouchMode(); 430 } catch (RemoteException e) { 431 } 432 } 433 return false; 434 } 435 436 /** 437 * We have one child 438 */ 439 public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) { 440 synchronized (this) { 441 if (mView == null) { 442 mView = view; 443 444 mAttachInfo.mDisplayState = mDisplay.getState(); 445 mDisplayManager.registerDisplayListener(mDisplayListener, mHandler); 446 447 mViewLayoutDirectionInitial = mView.getRawLayoutDirection(); 448 mFallbackEventHandler.setView(view); 449 mWindowAttributes.copyFrom(attrs); 450 if (mWindowAttributes.packageName == null) { 451 mWindowAttributes.packageName = mBasePackageName; 452 } 453 attrs = mWindowAttributes; 454 // Keep track of the actual window flags supplied by the client. 455 mClientWindowLayoutFlags = attrs.flags; 456 457 setAccessibilityFocus(null, null); 458 459 if (view instanceof RootViewSurfaceTaker) { 460 mSurfaceHolderCallback = 461 ((RootViewSurfaceTaker)view).willYouTakeTheSurface(); 462 if (mSurfaceHolderCallback != null) { 463 mSurfaceHolder = new TakenSurfaceHolder(); 464 mSurfaceHolder.setFormat(PixelFormat.UNKNOWN); 465 } 466 } 467 468 // Compute surface insets required to draw at specified Z value. 469 // TODO: Use real shadow insets for a constant max Z. 470 if (!attrs.hasManualSurfaceInsets) { 471 final int surfaceInset = (int) Math.ceil(view.getZ() * 2); 472 attrs.surfaceInsets.set(surfaceInset, surfaceInset, surfaceInset, surfaceInset); 473 } 474 475 CompatibilityInfo compatibilityInfo = mDisplayAdjustments.getCompatibilityInfo(); 476 mTranslator = compatibilityInfo.getTranslator(); 477 478 // If the application owns the surface, don't enable hardware acceleration 479 if (mSurfaceHolder == null) { 480 enableHardwareAcceleration(attrs); 481 } 482 483 boolean restore = false; 484 if (mTranslator != null) { 485 mSurface.setCompatibilityTranslator(mTranslator); 486 restore = true; 487 attrs.backup(); 488 mTranslator.translateWindowLayout(attrs); 489 } 490 if (DEBUG_LAYOUT) Log.d(TAG, "WindowLayout in setView:" + attrs); 491 492 if (!compatibilityInfo.supportsScreen()) { 493 attrs.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW; 494 mLastInCompatMode = true; 495 } 496 497 mSoftInputMode = attrs.softInputMode; 498 mWindowAttributesChanged = true; 499 mWindowAttributesChangesFlag = WindowManager.LayoutParams.EVERYTHING_CHANGED; 500 mAttachInfo.mRootView = view; 501 mAttachInfo.mScalingRequired = mTranslator != null; 502 mAttachInfo.mApplicationScale = 503 mTranslator == null ? 1.0f : mTranslator.applicationScale; 504 if (panelParentView != null) { 505 mAttachInfo.mPanelParentWindowToken 506 = panelParentView.getApplicationWindowToken(); 507 } 508 mAdded = true; 509 int res; /* = WindowManagerImpl.ADD_OKAY; */ 510 511 // Schedule the first layout -before- adding to the window 512 // manager, to make sure we do the relayout before receiving 513 // any other events from the system. 514 requestLayout(); 515 if ((mWindowAttributes.inputFeatures 516 & WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0) { 517 mInputChannel = new InputChannel(); 518 } 519 try { 520 mOrigWindowType = mWindowAttributes.type; 521 mAttachInfo.mRecomputeGlobalAttributes = true; 522 collectViewAttributes(); 523 res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes, 524 getHostVisibility(), mDisplay.getDisplayId(), 525 mAttachInfo.mContentInsets, mAttachInfo.mStableInsets, 526 mAttachInfo.mOutsets, mInputChannel); 527 } catch (RemoteException e) { 528 mAdded = false; 529 mView = null; 530 mAttachInfo.mRootView = null; 531 mInputChannel = null; 532 mFallbackEventHandler.setView(null); 533 unscheduleTraversals(); 534 setAccessibilityFocus(null, null); 535 throw new RuntimeException("Adding window failed", e); 536 } finally { 537 if (restore) { 538 attrs.restore(); 539 } 540 } 541 542 if (mTranslator != null) { 543 mTranslator.translateRectInScreenToAppWindow(mAttachInfo.mContentInsets); 544 } 545 mPendingOverscanInsets.set(0, 0, 0, 0); 546 mPendingContentInsets.set(mAttachInfo.mContentInsets); 547 mPendingStableInsets.set(mAttachInfo.mStableInsets); 548 mPendingVisibleInsets.set(0, 0, 0, 0); 549 if (DEBUG_LAYOUT) Log.v(TAG, "Added window " + mWindow); 550 if (res < WindowManagerGlobal.ADD_OKAY) { 551 mAttachInfo.mRootView = null; 552 mAdded = false; 553 mFallbackEventHandler.setView(null); 554 unscheduleTraversals(); 555 setAccessibilityFocus(null, null); 556 switch (res) { 557 case WindowManagerGlobal.ADD_BAD_APP_TOKEN: 558 case WindowManagerGlobal.ADD_BAD_SUBWINDOW_TOKEN: 559 throw new WindowManager.BadTokenException( 560 "Unable to add window -- token " + attrs.token 561 + " is not valid; is your activity running?"); 562 case WindowManagerGlobal.ADD_NOT_APP_TOKEN: 563 throw new WindowManager.BadTokenException( 564 "Unable to add window -- token " + attrs.token 565 + " is not for an application"); 566 case WindowManagerGlobal.ADD_APP_EXITING: 567 throw new WindowManager.BadTokenException( 568 "Unable to add window -- app for token " + attrs.token 569 + " is exiting"); 570 case WindowManagerGlobal.ADD_DUPLICATE_ADD: 571 throw new WindowManager.BadTokenException( 572 "Unable to add window -- window " + mWindow 573 + " has already been added"); 574 case WindowManagerGlobal.ADD_STARTING_NOT_NEEDED: 575 // Silently ignore -- we would have just removed it 576 // right away, anyway. 577 return; 578 case WindowManagerGlobal.ADD_MULTIPLE_SINGLETON: 579 throw new WindowManager.BadTokenException( 580 "Unable to add window " + mWindow + 581 " -- another window of this type already exists"); 582 case WindowManagerGlobal.ADD_PERMISSION_DENIED: 583 throw new WindowManager.BadTokenException( 584 "Unable to add window " + mWindow + 585 " -- permission denied for this window type"); 586 case WindowManagerGlobal.ADD_INVALID_DISPLAY: 587 throw new WindowManager.InvalidDisplayException( 588 "Unable to add window " + mWindow + 589 " -- the specified display can not be found"); 590 case WindowManagerGlobal.ADD_INVALID_TYPE: 591 throw new WindowManager.InvalidDisplayException( 592 "Unable to add window " + mWindow 593 + " -- the specified window type is not valid"); 594 } 595 throw new RuntimeException( 596 "Unable to add window -- unknown error code " + res); 597 } 598 599 if (view instanceof RootViewSurfaceTaker) { 600 mInputQueueCallback = 601 ((RootViewSurfaceTaker)view).willYouTakeTheInputQueue(); 602 } 603 if (mInputChannel != null) { 604 if (mInputQueueCallback != null) { 605 mInputQueue = new InputQueue(); 606 mInputQueueCallback.onInputQueueCreated(mInputQueue); 607 } 608 mInputEventReceiver = new WindowInputEventReceiver(mInputChannel, 609 Looper.myLooper()); 610 } 611 612 view.assignParent(this); 613 mAddedTouchMode = (res & WindowManagerGlobal.ADD_FLAG_IN_TOUCH_MODE) != 0; 614 mAppVisible = (res & WindowManagerGlobal.ADD_FLAG_APP_VISIBLE) != 0; 615 616 if (mAccessibilityManager.isEnabled()) { 617 mAccessibilityInteractionConnectionManager.ensureConnection(); 618 } 619 620 if (view.getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) { 621 view.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES); 622 } 623 624 // Set up the input pipeline. 625 CharSequence counterSuffix = attrs.getTitle(); 626 mSyntheticInputStage = new SyntheticInputStage(); 627 InputStage viewPostImeStage = new ViewPostImeInputStage(mSyntheticInputStage); 628 InputStage nativePostImeStage = new NativePostImeInputStage(viewPostImeStage, 629 "aq:native-post-ime:" + counterSuffix); 630 InputStage earlyPostImeStage = new EarlyPostImeInputStage(nativePostImeStage); 631 InputStage imeStage = new ImeInputStage(earlyPostImeStage, 632 "aq:ime:" + counterSuffix); 633 InputStage viewPreImeStage = new ViewPreImeInputStage(imeStage); 634 InputStage nativePreImeStage = new NativePreImeInputStage(viewPreImeStage, 635 "aq:native-pre-ime:" + counterSuffix); 636 637 mFirstInputStage = nativePreImeStage; 638 mFirstPostImeInputStage = earlyPostImeStage; 639 mPendingInputEventQueueLengthCounterName = "aq:pending:" + counterSuffix; 640 } 641 } 642 } 643 644 /** Whether the window is in local focus mode or not */ 645 private boolean isInLocalFocusMode() { 646 return (mWindowAttributes.flags & WindowManager.LayoutParams.FLAG_LOCAL_FOCUS_MODE) != 0; 647 } 648 649 public int getWindowFlags() { 650 return mWindowAttributes.flags; 651 } 652 653 public int getDisplayId() { 654 return mDisplay.getDisplayId(); 655 } 656 657 public CharSequence getTitle() { 658 return mWindowAttributes.getTitle(); 659 } 660 661 void destroyHardwareResources() { 662 if (mAttachInfo.mHardwareRenderer != null) { 663 mAttachInfo.mHardwareRenderer.destroyHardwareResources(mView); 664 mAttachInfo.mHardwareRenderer.destroy(); 665 } 666 } 667 668 public void detachFunctor(long functor) { 669 if (mAttachInfo.mHardwareRenderer != null) { 670 // Fence so that any pending invokeFunctor() messages will be processed 671 // before we return from detachFunctor. 672 mAttachInfo.mHardwareRenderer.stopDrawing(); 673 } 674 } 675 676 /** 677 * Schedules the functor for execution in either kModeProcess or 678 * kModeProcessNoContext, depending on whether or not there is an EGLContext. 679 * 680 * @param functor The native functor to invoke 681 * @param waitForCompletion If true, this will not return until the functor 682 * has invoked. If false, the functor may be invoked 683 * asynchronously. 684 */ 685 public void invokeFunctor(long functor, boolean waitForCompletion) { 686 ThreadedRenderer.invokeFunctor(functor, waitForCompletion); 687 } 688 689 public void registerAnimatingRenderNode(RenderNode animator) { 690 if (mAttachInfo.mHardwareRenderer != null) { 691 mAttachInfo.mHardwareRenderer.registerAnimatingRenderNode(animator); 692 } else { 693 if (mAttachInfo.mPendingAnimatingRenderNodes == null) { 694 mAttachInfo.mPendingAnimatingRenderNodes = new ArrayList<RenderNode>(); 695 } 696 mAttachInfo.mPendingAnimatingRenderNodes.add(animator); 697 } 698 } 699 700 private void enableHardwareAcceleration(WindowManager.LayoutParams attrs) { 701 mAttachInfo.mHardwareAccelerated = false; 702 mAttachInfo.mHardwareAccelerationRequested = false; 703 704 // Don't enable hardware acceleration when the application is in compatibility mode 705 if (mTranslator != null) return; 706 707 // Try to enable hardware acceleration if requested 708 final boolean hardwareAccelerated = 709 (attrs.flags & WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0; 710 711 if (hardwareAccelerated) { 712 if (!HardwareRenderer.isAvailable()) { 713 return; 714 } 715 716 // Persistent processes (including the system) should not do 717 // accelerated rendering on low-end devices. In that case, 718 // sRendererDisabled will be set. In addition, the system process 719 // itself should never do accelerated rendering. In that case, both 720 // sRendererDisabled and sSystemRendererDisabled are set. When 721 // sSystemRendererDisabled is set, PRIVATE_FLAG_FORCE_HARDWARE_ACCELERATED 722 // can be used by code on the system process to escape that and enable 723 // HW accelerated drawing. (This is basically for the lock screen.) 724 725 final boolean fakeHwAccelerated = (attrs.privateFlags & 726 WindowManager.LayoutParams.PRIVATE_FLAG_FAKE_HARDWARE_ACCELERATED) != 0; 727 final boolean forceHwAccelerated = (attrs.privateFlags & 728 WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_HARDWARE_ACCELERATED) != 0; 729 730 if (fakeHwAccelerated) { 731 // This is exclusively for the preview windows the window manager 732 // shows for launching applications, so they will look more like 733 // the app being launched. 734 mAttachInfo.mHardwareAccelerationRequested = true; 735 } else if (!HardwareRenderer.sRendererDisabled 736 || (HardwareRenderer.sSystemRendererDisabled && forceHwAccelerated)) { 737 if (mAttachInfo.mHardwareRenderer != null) { 738 mAttachInfo.mHardwareRenderer.destroy(); 739 } 740 741 final Rect insets = attrs.surfaceInsets; 742 final boolean hasSurfaceInsets = insets.left != 0 || insets.right != 0 743 || insets.top != 0 || insets.bottom != 0; 744 final boolean translucent = attrs.format != PixelFormat.OPAQUE || hasSurfaceInsets; 745 mAttachInfo.mHardwareRenderer = HardwareRenderer.create(mContext, translucent); 746 if (mAttachInfo.mHardwareRenderer != null) { 747 mAttachInfo.mHardwareRenderer.setName(attrs.getTitle().toString()); 748 mAttachInfo.mHardwareAccelerated = 749 mAttachInfo.mHardwareAccelerationRequested = true; 750 } 751 } 752 } 753 } 754 755 public View getView() { 756 return mView; 757 } 758 759 final WindowLeaked getLocation() { 760 return mLocation; 761 } 762 763 void setLayoutParams(WindowManager.LayoutParams attrs, boolean newView) { 764 synchronized (this) { 765 final int oldInsetLeft = mWindowAttributes.surfaceInsets.left; 766 final int oldInsetTop = mWindowAttributes.surfaceInsets.top; 767 final int oldInsetRight = mWindowAttributes.surfaceInsets.right; 768 final int oldInsetBottom = mWindowAttributes.surfaceInsets.bottom; 769 final int oldSoftInputMode = mWindowAttributes.softInputMode; 770 final boolean oldHasManualSurfaceInsets = mWindowAttributes.hasManualSurfaceInsets; 771 772 // Keep track of the actual window flags supplied by the client. 773 mClientWindowLayoutFlags = attrs.flags; 774 775 // Preserve compatible window flag if exists. 776 final int compatibleWindowFlag = mWindowAttributes.privateFlags 777 & WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW; 778 779 // Transfer over system UI visibility values as they carry current state. 780 attrs.systemUiVisibility = mWindowAttributes.systemUiVisibility; 781 attrs.subtreeSystemUiVisibility = mWindowAttributes.subtreeSystemUiVisibility; 782 783 mWindowAttributesChangesFlag = mWindowAttributes.copyFrom(attrs); 784 if ((mWindowAttributesChangesFlag 785 & WindowManager.LayoutParams.TRANSLUCENT_FLAGS_CHANGED) != 0) { 786 // Recompute system ui visibility. 787 mAttachInfo.mRecomputeGlobalAttributes = true; 788 } 789 if (mWindowAttributes.packageName == null) { 790 mWindowAttributes.packageName = mBasePackageName; 791 } 792 mWindowAttributes.privateFlags |= compatibleWindowFlag; 793 794 // Restore old surface insets. 795 mWindowAttributes.surfaceInsets.set( 796 oldInsetLeft, oldInsetTop, oldInsetRight, oldInsetBottom); 797 mWindowAttributes.hasManualSurfaceInsets = oldHasManualSurfaceInsets; 798 799 applyKeepScreenOnFlag(mWindowAttributes); 800 801 if (newView) { 802 mSoftInputMode = attrs.softInputMode; 803 requestLayout(); 804 } 805 806 // Don't lose the mode we last auto-computed. 807 if ((attrs.softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) 808 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED) { 809 mWindowAttributes.softInputMode = (mWindowAttributes.softInputMode 810 & ~WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) 811 | (oldSoftInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST); 812 } 813 814 mWindowAttributesChanged = true; 815 scheduleTraversals(); 816 } 817 } 818 819 void handleAppVisibility(boolean visible) { 820 if (mAppVisible != visible) { 821 mAppVisible = visible; 822 scheduleTraversals(); 823 if (!mAppVisible) { 824 WindowManagerGlobal.trimForeground(); 825 } 826 } 827 } 828 829 void handleGetNewSurface() { 830 mNewSurfaceNeeded = true; 831 mFullRedrawNeeded = true; 832 scheduleTraversals(); 833 } 834 835 private final DisplayListener mDisplayListener = new DisplayListener() { 836 @Override 837 public void onDisplayChanged(int displayId) { 838 if (mView != null && mDisplay.getDisplayId() == displayId) { 839 final int oldDisplayState = mAttachInfo.mDisplayState; 840 final int newDisplayState = mDisplay.getState(); 841 if (oldDisplayState != newDisplayState) { 842 mAttachInfo.mDisplayState = newDisplayState; 843 pokeDrawLockIfNeeded(); 844 if (oldDisplayState != Display.STATE_UNKNOWN) { 845 final int oldScreenState = toViewScreenState(oldDisplayState); 846 final int newScreenState = toViewScreenState(newDisplayState); 847 if (oldScreenState != newScreenState) { 848 mView.dispatchScreenStateChanged(newScreenState); 849 } 850 if (oldDisplayState == Display.STATE_OFF) { 851 // Draw was suppressed so we need to for it to happen here. 852 mFullRedrawNeeded = true; 853 scheduleTraversals(); 854 } 855 } 856 } 857 } 858 } 859 860 @Override 861 public void onDisplayRemoved(int displayId) { 862 } 863 864 @Override 865 public void onDisplayAdded(int displayId) { 866 } 867 868 private int toViewScreenState(int displayState) { 869 return displayState == Display.STATE_OFF ? 870 View.SCREEN_STATE_OFF : View.SCREEN_STATE_ON; 871 } 872 }; 873 874 void pokeDrawLockIfNeeded() { 875 final int displayState = mAttachInfo.mDisplayState; 876 if (mView != null && mAdded && mTraversalScheduled 877 && (displayState == Display.STATE_DOZE 878 || displayState == Display.STATE_DOZE_SUSPEND)) { 879 try { 880 mWindowSession.pokeDrawLock(mWindow); 881 } catch (RemoteException ex) { 882 // System server died, oh well. 883 } 884 } 885 } 886 887 @Override 888 public void requestFitSystemWindows() { 889 checkThread(); 890 mApplyInsetsRequested = true; 891 scheduleTraversals(); 892 } 893 894 @Override 895 public void requestLayout() { 896 if (!mHandlingLayoutInLayoutRequest) { 897 checkThread(); 898 mLayoutRequested = true; 899 scheduleTraversals(); 900 } 901 } 902 903 @Override 904 public boolean isLayoutRequested() { 905 return mLayoutRequested; 906 } 907 908 void invalidate() { 909 mDirty.set(0, 0, mWidth, mHeight); 910 if (!mWillDrawSoon) { 911 scheduleTraversals(); 912 } 913 } 914 915 void invalidateWorld(View view) { 916 view.invalidate(); 917 if (view instanceof ViewGroup) { 918 ViewGroup parent = (ViewGroup) view; 919 for (int i = 0; i < parent.getChildCount(); i++) { 920 invalidateWorld(parent.getChildAt(i)); 921 } 922 } 923 } 924 925 @Override 926 public void invalidateChild(View child, Rect dirty) { 927 invalidateChildInParent(null, dirty); 928 } 929 930 @Override 931 public ViewParent invalidateChildInParent(int[] location, Rect dirty) { 932 checkThread(); 933 if (DEBUG_DRAW) Log.v(TAG, "Invalidate child: " + dirty); 934 935 if (dirty == null) { 936 invalidate(); 937 return null; 938 } else if (dirty.isEmpty() && !mIsAnimating) { 939 return null; 940 } 941 942 if (mCurScrollY != 0 || mTranslator != null) { 943 mTempRect.set(dirty); 944 dirty = mTempRect; 945 if (mCurScrollY != 0) { 946 dirty.offset(0, -mCurScrollY); 947 } 948 if (mTranslator != null) { 949 mTranslator.translateRectInAppWindowToScreen(dirty); 950 } 951 if (mAttachInfo.mScalingRequired) { 952 dirty.inset(-1, -1); 953 } 954 } 955 956 invalidateRectOnScreen(dirty); 957 958 return null; 959 } 960 961 private void invalidateRectOnScreen(Rect dirty) { 962 final Rect localDirty = mDirty; 963 if (!localDirty.isEmpty() && !localDirty.contains(dirty)) { 964 mAttachInfo.mSetIgnoreDirtyState = true; 965 mAttachInfo.mIgnoreDirtyState = true; 966 } 967 968 // Add the new dirty rect to the current one 969 localDirty.union(dirty.left, dirty.top, dirty.right, dirty.bottom); 970 // Intersect with the bounds of the window to skip 971 // updates that lie outside of the visible region 972 final float appScale = mAttachInfo.mApplicationScale; 973 final boolean intersected = localDirty.intersect(0, 0, 974 (int) (mWidth * appScale + 0.5f), (int) (mHeight * appScale + 0.5f)); 975 if (!intersected) { 976 localDirty.setEmpty(); 977 } 978 if (!mWillDrawSoon && (intersected || mIsAnimating)) { 979 scheduleTraversals(); 980 } 981 } 982 983 void setWindowStopped(boolean stopped) { 984 if (mStopped != stopped) { 985 mStopped = stopped; 986 if (!mStopped) { 987 scheduleTraversals(); 988 } 989 } 990 } 991 992 /** 993 * Block the input events during an Activity Transition. The KEYCODE_BACK event is allowed 994 * through to allow quick reversal of the Activity Transition. 995 * 996 * @param paused true to pause, false to resume. 997 */ 998 public void setPausedForTransition(boolean paused) { 999 mPausedForTransition = paused; 1000 } 1001 1002 @Override 1003 public ViewParent getParent() { 1004 return null; 1005 } 1006 1007 @Override 1008 public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) { 1009 if (child != mView) { 1010 throw new RuntimeException("child is not mine, honest!"); 1011 } 1012 // Note: don't apply scroll offset, because we want to know its 1013 // visibility in the virtual canvas being given to the view hierarchy. 1014 return r.intersect(0, 0, mWidth, mHeight); 1015 } 1016 1017 @Override 1018 public void bringChildToFront(View child) { 1019 } 1020 1021 int getHostVisibility() { 1022 return mAppVisible ? mView.getVisibility() : View.GONE; 1023 } 1024 1025 /** 1026 * Add LayoutTransition to the list of transitions to be started in the next traversal. 1027 * This list will be cleared after the transitions on the list are start()'ed. These 1028 * transitionsa re added by LayoutTransition itself when it sets up animations. The setup 1029 * happens during the layout phase of traversal, which we want to complete before any of the 1030 * animations are started (because those animations may side-effect properties that layout 1031 * depends upon, like the bounding rectangles of the affected views). So we add the transition 1032 * to the list and it is started just prior to starting the drawing phase of traversal. 1033 * 1034 * @param transition The LayoutTransition to be started on the next traversal. 1035 * 1036 * @hide 1037 */ 1038 public void requestTransitionStart(LayoutTransition transition) { 1039 if (mPendingTransitions == null || !mPendingTransitions.contains(transition)) { 1040 if (mPendingTransitions == null) { 1041 mPendingTransitions = new ArrayList<LayoutTransition>(); 1042 } 1043 mPendingTransitions.add(transition); 1044 } 1045 } 1046 1047 /** 1048 * Notifies the HardwareRenderer that a new frame will be coming soon. 1049 * Currently only {@link ThreadedRenderer} cares about this, and uses 1050 * this knowledge to adjust the scheduling of off-thread animations 1051 */ 1052 void notifyRendererOfFramePending() { 1053 if (mAttachInfo.mHardwareRenderer != null) { 1054 mAttachInfo.mHardwareRenderer.notifyFramePending(); 1055 } 1056 } 1057 1058 void scheduleTraversals() { 1059 if (!mTraversalScheduled) { 1060 mTraversalScheduled = true; 1061 mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier(); 1062 mChoreographer.postCallback( 1063 Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null); 1064 if (!mUnbufferedInputDispatch) { 1065 scheduleConsumeBatchedInput(); 1066 } 1067 notifyRendererOfFramePending(); 1068 pokeDrawLockIfNeeded(); 1069 } 1070 } 1071 1072 void unscheduleTraversals() { 1073 if (mTraversalScheduled) { 1074 mTraversalScheduled = false; 1075 mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier); 1076 mChoreographer.removeCallbacks( 1077 Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null); 1078 } 1079 } 1080 1081 void doTraversal() { 1082 if (mTraversalScheduled) { 1083 mTraversalScheduled = false; 1084 mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier); 1085 1086 if (mProfile) { 1087 Debug.startMethodTracing("ViewAncestor"); 1088 } 1089 1090 performTraversals(); 1091 1092 if (mProfile) { 1093 Debug.stopMethodTracing(); 1094 mProfile = false; 1095 } 1096 } 1097 } 1098 1099 private void applyKeepScreenOnFlag(WindowManager.LayoutParams params) { 1100 // Update window's global keep screen on flag: if a view has requested 1101 // that the screen be kept on, then it is always set; otherwise, it is 1102 // set to whatever the client last requested for the global state. 1103 if (mAttachInfo.mKeepScreenOn) { 1104 params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; 1105 } else { 1106 params.flags = (params.flags&~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) 1107 | (mClientWindowLayoutFlags&WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 1108 } 1109 } 1110 1111 private boolean collectViewAttributes() { 1112 if (mAttachInfo.mRecomputeGlobalAttributes) { 1113 //Log.i(TAG, "Computing view hierarchy attributes!"); 1114 mAttachInfo.mRecomputeGlobalAttributes = false; 1115 boolean oldScreenOn = mAttachInfo.mKeepScreenOn; 1116 mAttachInfo.mKeepScreenOn = false; 1117 mAttachInfo.mSystemUiVisibility = 0; 1118 mAttachInfo.mHasSystemUiListeners = false; 1119 mView.dispatchCollectViewAttributes(mAttachInfo, 0); 1120 mAttachInfo.mSystemUiVisibility &= ~mAttachInfo.mDisabledSystemUiVisibility; 1121 WindowManager.LayoutParams params = mWindowAttributes; 1122 mAttachInfo.mSystemUiVisibility |= getImpliedSystemUiVisibility(params); 1123 if (mAttachInfo.mKeepScreenOn != oldScreenOn 1124 || mAttachInfo.mSystemUiVisibility != params.subtreeSystemUiVisibility 1125 || mAttachInfo.mHasSystemUiListeners != params.hasSystemUiListeners) { 1126 applyKeepScreenOnFlag(params); 1127 params.subtreeSystemUiVisibility = mAttachInfo.mSystemUiVisibility; 1128 params.hasSystemUiListeners = mAttachInfo.mHasSystemUiListeners; 1129 mView.dispatchWindowSystemUiVisiblityChanged(mAttachInfo.mSystemUiVisibility); 1130 return true; 1131 } 1132 } 1133 return false; 1134 } 1135 1136 private int getImpliedSystemUiVisibility(WindowManager.LayoutParams params) { 1137 int vis = 0; 1138 // Translucent decor window flags imply stable system ui visibility. 1139 if ((params.flags & WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) != 0) { 1140 vis |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN; 1141 } 1142 if ((params.flags & WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION) != 0) { 1143 vis |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION; 1144 } 1145 return vis; 1146 } 1147 1148 private boolean measureHierarchy(final View host, final WindowManager.LayoutParams lp, 1149 final Resources res, final int desiredWindowWidth, final int desiredWindowHeight) { 1150 int childWidthMeasureSpec; 1151 int childHeightMeasureSpec; 1152 boolean windowSizeMayChange = false; 1153 1154 if (DEBUG_ORIENTATION || DEBUG_LAYOUT) Log.v(TAG, 1155 "Measuring " + host + " in display " + desiredWindowWidth 1156 + "x" + desiredWindowHeight + "..."); 1157 1158 boolean goodMeasure = false; 1159 if (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT) { 1160 // On large screens, we don't want to allow dialogs to just 1161 // stretch to fill the entire width of the screen to display 1162 // one line of text. First try doing the layout at a smaller 1163 // size to see if it will fit. 1164 final DisplayMetrics packageMetrics = res.getDisplayMetrics(); 1165 res.getValue(com.android.internal.R.dimen.config_prefDialogWidth, mTmpValue, true); 1166 int baseSize = 0; 1167 if (mTmpValue.type == TypedValue.TYPE_DIMENSION) { 1168 baseSize = (int)mTmpValue.getDimension(packageMetrics); 1169 } 1170 if (DEBUG_DIALOG) Log.v(TAG, "Window " + mView + ": baseSize=" + baseSize); 1171 if (baseSize != 0 && desiredWindowWidth > baseSize) { 1172 childWidthMeasureSpec = getRootMeasureSpec(baseSize, lp.width); 1173 childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height); 1174 performMeasure(childWidthMeasureSpec, childHeightMeasureSpec); 1175 if (DEBUG_DIALOG) Log.v(TAG, "Window " + mView + ": measured (" 1176 + host.getMeasuredWidth() + "," + host.getMeasuredHeight() + ")"); 1177 if ((host.getMeasuredWidthAndState()&View.MEASURED_STATE_TOO_SMALL) == 0) { 1178 goodMeasure = true; 1179 } else { 1180 // Didn't fit in that size... try expanding a bit. 1181 baseSize = (baseSize+desiredWindowWidth)/2; 1182 if (DEBUG_DIALOG) Log.v(TAG, "Window " + mView + ": next baseSize=" 1183 + baseSize); 1184 childWidthMeasureSpec = getRootMeasureSpec(baseSize, lp.width); 1185 performMeasure(childWidthMeasureSpec, childHeightMeasureSpec); 1186 if (DEBUG_DIALOG) Log.v(TAG, "Window " + mView + ": measured (" 1187 + host.getMeasuredWidth() + "," + host.getMeasuredHeight() + ")"); 1188 if ((host.getMeasuredWidthAndState()&View.MEASURED_STATE_TOO_SMALL) == 0) { 1189 if (DEBUG_DIALOG) Log.v(TAG, "Good!"); 1190 goodMeasure = true; 1191 } 1192 } 1193 } 1194 } 1195 1196 if (!goodMeasure) { 1197 childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width); 1198 childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height); 1199 performMeasure(childWidthMeasureSpec, childHeightMeasureSpec); 1200 if (mWidth != host.getMeasuredWidth() || mHeight != host.getMeasuredHeight()) { 1201 windowSizeMayChange = true; 1202 } 1203 } 1204 1205 if (DBG) { 1206 System.out.println("======================================"); 1207 System.out.println("performTraversals -- after measure"); 1208 host.debug(); 1209 } 1210 1211 return windowSizeMayChange; 1212 } 1213 1214 /** 1215 * Modifies the input matrix such that it maps view-local coordinates to 1216 * on-screen coordinates. 1217 * 1218 * @param m input matrix to modify 1219 */ 1220 void transformMatrixToGlobal(Matrix m) { 1221 m.preTranslate(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop); 1222 } 1223 1224 /** 1225 * Modifies the input matrix such that it maps on-screen coordinates to 1226 * view-local coordinates. 1227 * 1228 * @param m input matrix to modify 1229 */ 1230 void transformMatrixToLocal(Matrix m) { 1231 m.postTranslate(-mAttachInfo.mWindowLeft, -mAttachInfo.mWindowTop); 1232 } 1233 1234 /* package */ WindowInsets getWindowInsets(boolean forceConstruct) { 1235 if (mLastWindowInsets == null || forceConstruct) { 1236 mDispatchContentInsets.set(mAttachInfo.mContentInsets); 1237 mDispatchStableInsets.set(mAttachInfo.mStableInsets); 1238 Rect contentInsets = mDispatchContentInsets; 1239 Rect stableInsets = mDispatchStableInsets; 1240 // For dispatch we preserve old logic, but for direct requests from Views we allow to 1241 // immediately use pending insets. 1242 if (!forceConstruct 1243 && (!mPendingContentInsets.equals(contentInsets) || 1244 !mPendingStableInsets.equals(stableInsets))) { 1245 contentInsets = mPendingContentInsets; 1246 stableInsets = mPendingStableInsets; 1247 } 1248 Rect outsets = mAttachInfo.mOutsets; 1249 if (outsets.left > 0 || outsets.top > 0 || outsets.right > 0 || outsets.bottom > 0) { 1250 contentInsets = new Rect(contentInsets.left + outsets.left, 1251 contentInsets.top + outsets.top, contentInsets.right + outsets.right, 1252 contentInsets.bottom + outsets.bottom); 1253 } 1254 mLastWindowInsets = new WindowInsets(contentInsets, 1255 null /* windowDecorInsets */, stableInsets, 1256 mContext.getResources().getConfiguration().isScreenRound()); 1257 } 1258 return mLastWindowInsets; 1259 } 1260 1261 void dispatchApplyInsets(View host) { 1262 host.dispatchApplyWindowInsets(getWindowInsets(true /* forceConstruct */)); 1263 } 1264 1265 private void performTraversals() { 1266 // cache mView since it is used so much below... 1267 final View host = mView; 1268 1269 if (DBG) { 1270 System.out.println("======================================"); 1271 System.out.println("performTraversals"); 1272 host.debug(); 1273 } 1274 1275 if (host == null || !mAdded) 1276 return; 1277 1278 mIsInTraversal = true; 1279 mWillDrawSoon = true; 1280 boolean windowSizeMayChange = false; 1281 boolean newSurface = false; 1282 boolean surfaceChanged = false; 1283 WindowManager.LayoutParams lp = mWindowAttributes; 1284 1285 int desiredWindowWidth; 1286 int desiredWindowHeight; 1287 1288 final int viewVisibility = getHostVisibility(); 1289 boolean viewVisibilityChanged = mViewVisibility != viewVisibility 1290 || mNewSurfaceNeeded; 1291 1292 WindowManager.LayoutParams params = null; 1293 if (mWindowAttributesChanged) { 1294 mWindowAttributesChanged = false; 1295 surfaceChanged = true; 1296 params = lp; 1297 } 1298 CompatibilityInfo compatibilityInfo = mDisplayAdjustments.getCompatibilityInfo(); 1299 if (compatibilityInfo.supportsScreen() == mLastInCompatMode) { 1300 params = lp; 1301 mFullRedrawNeeded = true; 1302 mLayoutRequested = true; 1303 if (mLastInCompatMode) { 1304 params.privateFlags &= ~WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW; 1305 mLastInCompatMode = false; 1306 } else { 1307 params.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW; 1308 mLastInCompatMode = true; 1309 } 1310 } 1311 1312 mWindowAttributesChangesFlag = 0; 1313 1314 Rect frame = mWinFrame; 1315 if (mFirst) { 1316 mFullRedrawNeeded = true; 1317 mLayoutRequested = true; 1318 1319 if (lp.type == WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL 1320 || lp.type == WindowManager.LayoutParams.TYPE_INPUT_METHOD) { 1321 // NOTE -- system code, won't try to do compat mode. 1322 Point size = new Point(); 1323 mDisplay.getRealSize(size); 1324 desiredWindowWidth = size.x; 1325 desiredWindowHeight = size.y; 1326 } else { 1327 DisplayMetrics packageMetrics = 1328 mView.getContext().getResources().getDisplayMetrics(); 1329 desiredWindowWidth = packageMetrics.widthPixels; 1330 desiredWindowHeight = packageMetrics.heightPixels; 1331 } 1332 1333 // We used to use the following condition to choose 32 bits drawing caches: 1334 // PixelFormat.hasAlpha(lp.format) || lp.format == PixelFormat.RGBX_8888 1335 // However, windows are now always 32 bits by default, so choose 32 bits 1336 mAttachInfo.mUse32BitDrawingCache = true; 1337 mAttachInfo.mHasWindowFocus = false; 1338 mAttachInfo.mWindowVisibility = viewVisibility; 1339 mAttachInfo.mRecomputeGlobalAttributes = false; 1340 viewVisibilityChanged = false; 1341 mLastConfiguration.setTo(host.getResources().getConfiguration()); 1342 mLastSystemUiVisibility = mAttachInfo.mSystemUiVisibility; 1343 // Set the layout direction if it has not been set before (inherit is the default) 1344 if (mViewLayoutDirectionInitial == View.LAYOUT_DIRECTION_INHERIT) { 1345 host.setLayoutDirection(mLastConfiguration.getLayoutDirection()); 1346 } 1347 host.dispatchAttachedToWindow(mAttachInfo, 0); 1348 mAttachInfo.mTreeObserver.dispatchOnWindowAttachedChange(true); 1349 dispatchApplyInsets(host); 1350 //Log.i(TAG, "Screen on initialized: " + attachInfo.mKeepScreenOn); 1351 1352 } else { 1353 desiredWindowWidth = frame.width(); 1354 desiredWindowHeight = frame.height(); 1355 if (desiredWindowWidth != mWidth || desiredWindowHeight != mHeight) { 1356 if (DEBUG_ORIENTATION) Log.v(TAG, 1357 "View " + host + " resized to: " + frame); 1358 mFullRedrawNeeded = true; 1359 mLayoutRequested = true; 1360 windowSizeMayChange = true; 1361 } 1362 } 1363 1364 if (viewVisibilityChanged) { 1365 mAttachInfo.mWindowVisibility = viewVisibility; 1366 host.dispatchWindowVisibilityChanged(viewVisibility); 1367 if (viewVisibility != View.VISIBLE || mNewSurfaceNeeded) { 1368 destroyHardwareResources(); 1369 } 1370 if (viewVisibility == View.GONE) { 1371 // After making a window gone, we will count it as being 1372 // shown for the first time the next time it gets focus. 1373 mHasHadWindowFocus = false; 1374 } 1375 } 1376 1377 // Non-visible windows can't hold accessibility focus. 1378 if (mAttachInfo.mWindowVisibility != View.VISIBLE) { 1379 host.clearAccessibilityFocus(); 1380 } 1381 1382 // Execute enqueued actions on every traversal in case a detached view enqueued an action 1383 getRunQueue().executeActions(mAttachInfo.mHandler); 1384 1385 boolean insetsChanged = false; 1386 1387 boolean layoutRequested = mLayoutRequested && (!mStopped || mReportNextDraw); 1388 if (layoutRequested) { 1389 1390 final Resources res = mView.getContext().getResources(); 1391 1392 if (mFirst) { 1393 // make sure touch mode code executes by setting cached value 1394 // to opposite of the added touch mode. 1395 mAttachInfo.mInTouchMode = !mAddedTouchMode; 1396 ensureTouchModeLocally(mAddedTouchMode); 1397 } else { 1398 if (!mPendingOverscanInsets.equals(mAttachInfo.mOverscanInsets)) { 1399 insetsChanged = true; 1400 } 1401 if (!mPendingContentInsets.equals(mAttachInfo.mContentInsets)) { 1402 insetsChanged = true; 1403 } 1404 if (!mPendingStableInsets.equals(mAttachInfo.mStableInsets)) { 1405 insetsChanged = true; 1406 } 1407 if (!mPendingVisibleInsets.equals(mAttachInfo.mVisibleInsets)) { 1408 mAttachInfo.mVisibleInsets.set(mPendingVisibleInsets); 1409 if (DEBUG_LAYOUT) Log.v(TAG, "Visible insets changing to: " 1410 + mAttachInfo.mVisibleInsets); 1411 } 1412 if (!mPendingOutsets.equals(mAttachInfo.mOutsets)) { 1413 insetsChanged = true; 1414 } 1415 if (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT 1416 || lp.height == ViewGroup.LayoutParams.WRAP_CONTENT) { 1417 windowSizeMayChange = true; 1418 1419 if (lp.type == WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL 1420 || lp.type == WindowManager.LayoutParams.TYPE_INPUT_METHOD) { 1421 // NOTE -- system code, won't try to do compat mode. 1422 Point size = new Point(); 1423 mDisplay.getRealSize(size); 1424 desiredWindowWidth = size.x; 1425 desiredWindowHeight = size.y; 1426 } else { 1427 DisplayMetrics packageMetrics = res.getDisplayMetrics(); 1428 desiredWindowWidth = packageMetrics.widthPixels; 1429 desiredWindowHeight = packageMetrics.heightPixels; 1430 } 1431 } 1432 } 1433 1434 // Ask host how big it wants to be 1435 windowSizeMayChange |= measureHierarchy(host, lp, res, 1436 desiredWindowWidth, desiredWindowHeight); 1437 } 1438 1439 if (collectViewAttributes()) { 1440 params = lp; 1441 } 1442 if (mAttachInfo.mForceReportNewAttributes) { 1443 mAttachInfo.mForceReportNewAttributes = false; 1444 params = lp; 1445 } 1446 1447 if (mFirst || mAttachInfo.mViewVisibilityChanged) { 1448 mAttachInfo.mViewVisibilityChanged = false; 1449 int resizeMode = mSoftInputMode & 1450 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST; 1451 // If we are in auto resize mode, then we need to determine 1452 // what mode to use now. 1453 if (resizeMode == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED) { 1454 final int N = mAttachInfo.mScrollContainers.size(); 1455 for (int i=0; i<N; i++) { 1456 if (mAttachInfo.mScrollContainers.get(i).isShown()) { 1457 resizeMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE; 1458 } 1459 } 1460 if (resizeMode == 0) { 1461 resizeMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN; 1462 } 1463 if ((lp.softInputMode & 1464 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) != resizeMode) { 1465 lp.softInputMode = (lp.softInputMode & 1466 ~WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) | 1467 resizeMode; 1468 params = lp; 1469 } 1470 } 1471 } 1472 1473 if (params != null) { 1474 if ((host.mPrivateFlags & View.PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) { 1475 if (!PixelFormat.formatHasAlpha(params.format)) { 1476 params.format = PixelFormat.TRANSLUCENT; 1477 } 1478 } 1479 mAttachInfo.mOverscanRequested = (params.flags 1480 & WindowManager.LayoutParams.FLAG_LAYOUT_IN_OVERSCAN) != 0; 1481 } 1482 1483 if (mApplyInsetsRequested) { 1484 mApplyInsetsRequested = false; 1485 mLastOverscanRequested = mAttachInfo.mOverscanRequested; 1486 dispatchApplyInsets(host); 1487 if (mLayoutRequested) { 1488 // Short-circuit catching a new layout request here, so 1489 // we don't need to go through two layout passes when things 1490 // change due to fitting system windows, which can happen a lot. 1491 windowSizeMayChange |= measureHierarchy(host, lp, 1492 mView.getContext().getResources(), 1493 desiredWindowWidth, desiredWindowHeight); 1494 } 1495 } 1496 1497 if (layoutRequested) { 1498 // Clear this now, so that if anything requests a layout in the 1499 // rest of this function we will catch it and re-run a full 1500 // layout pass. 1501 mLayoutRequested = false; 1502 } 1503 1504 boolean windowShouldResize = layoutRequested && windowSizeMayChange 1505 && ((mWidth != host.getMeasuredWidth() || mHeight != host.getMeasuredHeight()) 1506 || (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT && 1507 frame.width() < desiredWindowWidth && frame.width() != mWidth) 1508 || (lp.height == ViewGroup.LayoutParams.WRAP_CONTENT && 1509 frame.height() < desiredWindowHeight && frame.height() != mHeight)); 1510 1511 // Determine whether to compute insets. 1512 // If there are no inset listeners remaining then we may still need to compute 1513 // insets in case the old insets were non-empty and must be reset. 1514 final boolean computesInternalInsets = 1515 mAttachInfo.mTreeObserver.hasComputeInternalInsetsListeners() 1516 || mAttachInfo.mHasNonEmptyGivenInternalInsets; 1517 1518 boolean insetsPending = false; 1519 int relayoutResult = 0; 1520 1521 if (mFirst || windowShouldResize || insetsChanged || 1522 viewVisibilityChanged || params != null) { 1523 1524 if (viewVisibility == View.VISIBLE) { 1525 // If this window is giving internal insets to the window 1526 // manager, and it is being added or changing its visibility, 1527 // then we want to first give the window manager "fake" 1528 // insets to cause it to effectively ignore the content of 1529 // the window during layout. This avoids it briefly causing 1530 // other windows to resize/move based on the raw frame of the 1531 // window, waiting until we can finish laying out this window 1532 // and get back to the window manager with the ultimately 1533 // computed insets. 1534 insetsPending = computesInternalInsets && (mFirst || viewVisibilityChanged); 1535 } 1536 1537 if (mSurfaceHolder != null) { 1538 mSurfaceHolder.mSurfaceLock.lock(); 1539 mDrawingAllowed = true; 1540 } 1541 1542 boolean hwInitialized = false; 1543 boolean contentInsetsChanged = false; 1544 boolean hadSurface = mSurface.isValid(); 1545 1546 try { 1547 if (DEBUG_LAYOUT) { 1548 Log.i(TAG, "host=w:" + host.getMeasuredWidth() + ", h:" + 1549 host.getMeasuredHeight() + ", params=" + params); 1550 } 1551 1552 if (mAttachInfo.mHardwareRenderer != null) { 1553 // relayoutWindow may decide to destroy mSurface. As that decision 1554 // happens in WindowManager service, we need to be defensive here 1555 // and stop using the surface in case it gets destroyed. 1556 if (mAttachInfo.mHardwareRenderer.pauseSurface(mSurface)) { 1557 // Animations were running so we need to push a frame 1558 // to resume them 1559 mDirty.set(0, 0, mWidth, mHeight); 1560 } 1561 mChoreographer.mFrameInfo.addFlags(FrameInfo.FLAG_WINDOW_LAYOUT_CHANGED); 1562 } 1563 final int surfaceGenerationId = mSurface.getGenerationId(); 1564 relayoutResult = relayoutWindow(params, viewVisibility, insetsPending); 1565 1566 if (DEBUG_LAYOUT) Log.v(TAG, "relayout: frame=" + frame.toShortString() 1567 + " overscan=" + mPendingOverscanInsets.toShortString() 1568 + " content=" + mPendingContentInsets.toShortString() 1569 + " visible=" + mPendingVisibleInsets.toShortString() 1570 + " visible=" + mPendingStableInsets.toShortString() 1571 + " outsets=" + mPendingOutsets.toShortString() 1572 + " surface=" + mSurface); 1573 1574 if (mPendingConfiguration.seq != 0) { 1575 if (DEBUG_CONFIGURATION) Log.v(TAG, "Visible with new config: " 1576 + mPendingConfiguration); 1577 updateConfiguration(mPendingConfiguration, !mFirst); 1578 mPendingConfiguration.seq = 0; 1579 } 1580 1581 final boolean overscanInsetsChanged = !mPendingOverscanInsets.equals( 1582 mAttachInfo.mOverscanInsets); 1583 contentInsetsChanged = !mPendingContentInsets.equals( 1584 mAttachInfo.mContentInsets); 1585 final boolean visibleInsetsChanged = !mPendingVisibleInsets.equals( 1586 mAttachInfo.mVisibleInsets); 1587 final boolean stableInsetsChanged = !mPendingStableInsets.equals( 1588 mAttachInfo.mStableInsets); 1589 final boolean outsetsChanged = !mPendingOutsets.equals(mAttachInfo.mOutsets); 1590 if (contentInsetsChanged) { 1591 mAttachInfo.mContentInsets.set(mPendingContentInsets); 1592 if (DEBUG_LAYOUT) Log.v(TAG, "Content insets changing to: " 1593 + mAttachInfo.mContentInsets); 1594 } 1595 if (overscanInsetsChanged) { 1596 mAttachInfo.mOverscanInsets.set(mPendingOverscanInsets); 1597 if (DEBUG_LAYOUT) Log.v(TAG, "Overscan insets changing to: " 1598 + mAttachInfo.mOverscanInsets); 1599 // Need to relayout with content insets. 1600 contentInsetsChanged = true; 1601 } 1602 if (stableInsetsChanged) { 1603 mAttachInfo.mStableInsets.set(mPendingStableInsets); 1604 if (DEBUG_LAYOUT) Log.v(TAG, "Decor insets changing to: " 1605 + mAttachInfo.mStableInsets); 1606 // Need to relayout with content insets. 1607 contentInsetsChanged = true; 1608 } 1609 if (contentInsetsChanged || mLastSystemUiVisibility != 1610 mAttachInfo.mSystemUiVisibility || mApplyInsetsRequested 1611 || mLastOverscanRequested != mAttachInfo.mOverscanRequested 1612 || outsetsChanged) { 1613 mLastSystemUiVisibility = mAttachInfo.mSystemUiVisibility; 1614 mLastOverscanRequested = mAttachInfo.mOverscanRequested; 1615 mAttachInfo.mOutsets.set(mPendingOutsets); 1616 mApplyInsetsRequested = false; 1617 dispatchApplyInsets(host); 1618 } 1619 if (visibleInsetsChanged) { 1620 mAttachInfo.mVisibleInsets.set(mPendingVisibleInsets); 1621 if (DEBUG_LAYOUT) Log.v(TAG, "Visible insets changing to: " 1622 + mAttachInfo.mVisibleInsets); 1623 } 1624 1625 if (!hadSurface) { 1626 if (mSurface.isValid()) { 1627 // If we are creating a new surface, then we need to 1628 // completely redraw it. Also, when we get to the 1629 // point of drawing it we will hold off and schedule 1630 // a new traversal instead. This is so we can tell the 1631 // window manager about all of the windows being displayed 1632 // before actually drawing them, so it can display then 1633 // all at once. 1634 newSurface = true; 1635 mFullRedrawNeeded = true; 1636 mPreviousTransparentRegion.setEmpty(); 1637 1638 // Only initialize up-front if transparent regions are not 1639 // requested, otherwise defer to see if the entire window 1640 // will be transparent 1641 if (mAttachInfo.mHardwareRenderer != null) { 1642 try { 1643 hwInitialized = mAttachInfo.mHardwareRenderer.initialize( 1644 mSurface); 1645 if (hwInitialized && (host.mPrivateFlags 1646 & View.PFLAG_REQUEST_TRANSPARENT_REGIONS) == 0) { 1647 // Don't pre-allocate if transparent regions 1648 // are requested as they may not be needed 1649 mSurface.allocateBuffers(); 1650 } 1651 } catch (OutOfResourcesException e) { 1652 handleOutOfResourcesException(e); 1653 return; 1654 } 1655 } 1656 } 1657 } else if (!mSurface.isValid()) { 1658 // If the surface has been removed, then reset the scroll 1659 // positions. 1660 if (mLastScrolledFocus != null) { 1661 mLastScrolledFocus.clear(); 1662 } 1663 mScrollY = mCurScrollY = 0; 1664 if (mView instanceof RootViewSurfaceTaker) { 1665 ((RootViewSurfaceTaker) mView).onRootViewScrollYChanged(mCurScrollY); 1666 } 1667 if (mScroller != null) { 1668 mScroller.abortAnimation(); 1669 } 1670 // Our surface is gone 1671 if (mAttachInfo.mHardwareRenderer != null && 1672 mAttachInfo.mHardwareRenderer.isEnabled()) { 1673 mAttachInfo.mHardwareRenderer.destroy(); 1674 } 1675 } else if (surfaceGenerationId != mSurface.getGenerationId() && 1676 mSurfaceHolder == null && mAttachInfo.mHardwareRenderer != null) { 1677 mFullRedrawNeeded = true; 1678 try { 1679 mAttachInfo.mHardwareRenderer.updateSurface(mSurface); 1680 } catch (OutOfResourcesException e) { 1681 handleOutOfResourcesException(e); 1682 return; 1683 } 1684 } 1685 } catch (RemoteException e) { 1686 } 1687 1688 if (DEBUG_ORIENTATION) Log.v( 1689 TAG, "Relayout returned: frame=" + frame + ", surface=" + mSurface); 1690 1691 mAttachInfo.mWindowLeft = frame.left; 1692 mAttachInfo.mWindowTop = frame.top; 1693 1694 // !!FIXME!! This next section handles the case where we did not get the 1695 // window size we asked for. We should avoid this by getting a maximum size from 1696 // the window session beforehand. 1697 if (mWidth != frame.width() || mHeight != frame.height()) { 1698 mWidth = frame.width(); 1699 mHeight = frame.height(); 1700 } 1701 1702 if (mSurfaceHolder != null) { 1703 // The app owns the surface; tell it about what is going on. 1704 if (mSurface.isValid()) { 1705 // XXX .copyFrom() doesn't work! 1706 //mSurfaceHolder.mSurface.copyFrom(mSurface); 1707 mSurfaceHolder.mSurface = mSurface; 1708 } 1709 mSurfaceHolder.setSurfaceFrameSize(mWidth, mHeight); 1710 mSurfaceHolder.mSurfaceLock.unlock(); 1711 if (mSurface.isValid()) { 1712 if (!hadSurface) { 1713 mSurfaceHolder.ungetCallbacks(); 1714 1715 mIsCreating = true; 1716 mSurfaceHolderCallback.surfaceCreated(mSurfaceHolder); 1717 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks(); 1718 if (callbacks != null) { 1719 for (SurfaceHolder.Callback c : callbacks) { 1720 c.surfaceCreated(mSurfaceHolder); 1721 } 1722 } 1723 surfaceChanged = true; 1724 } 1725 if (surfaceChanged) { 1726 mSurfaceHolderCallback.surfaceChanged(mSurfaceHolder, 1727 lp.format, mWidth, mHeight); 1728 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks(); 1729 if (callbacks != null) { 1730 for (SurfaceHolder.Callback c : callbacks) { 1731 c.surfaceChanged(mSurfaceHolder, lp.format, 1732 mWidth, mHeight); 1733 } 1734 } 1735 } 1736 mIsCreating = false; 1737 } else if (hadSurface) { 1738 mSurfaceHolder.ungetCallbacks(); 1739 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks(); 1740 mSurfaceHolderCallback.surfaceDestroyed(mSurfaceHolder); 1741 if (callbacks != null) { 1742 for (SurfaceHolder.Callback c : callbacks) { 1743 c.surfaceDestroyed(mSurfaceHolder); 1744 } 1745 } 1746 mSurfaceHolder.mSurfaceLock.lock(); 1747 try { 1748 mSurfaceHolder.mSurface = new Surface(); 1749 } finally { 1750 mSurfaceHolder.mSurfaceLock.unlock(); 1751 } 1752 } 1753 } 1754 1755 final HardwareRenderer hardwareRenderer = mAttachInfo.mHardwareRenderer; 1756 if (hardwareRenderer != null && hardwareRenderer.isEnabled()) { 1757 if (hwInitialized 1758 || mWidth != hardwareRenderer.getWidth() 1759 || mHeight != hardwareRenderer.getHeight()) { 1760 hardwareRenderer.setup(mWidth, mHeight, mAttachInfo, 1761 mWindowAttributes.surfaceInsets); 1762 if (!hwInitialized) { 1763 hardwareRenderer.invalidate(mSurface); 1764 mFullRedrawNeeded = true; 1765 } 1766 } 1767 } 1768 1769 if (!mStopped || mReportNextDraw) { 1770 boolean focusChangedDueToTouchMode = ensureTouchModeLocally( 1771 (relayoutResult&WindowManagerGlobal.RELAYOUT_RES_IN_TOUCH_MODE) != 0); 1772 if (focusChangedDueToTouchMode || mWidth != host.getMeasuredWidth() 1773 || mHeight != host.getMeasuredHeight() || contentInsetsChanged) { 1774 int childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width); 1775 int childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height); 1776 1777 if (DEBUG_LAYOUT) Log.v(TAG, "Ooops, something changed! mWidth=" 1778 + mWidth + " measuredWidth=" + host.getMeasuredWidth() 1779 + " mHeight=" + mHeight 1780 + " measuredHeight=" + host.getMeasuredHeight() 1781 + " coveredInsetsChanged=" + contentInsetsChanged); 1782 1783 // Ask host how big it wants to be 1784 performMeasure(childWidthMeasureSpec, childHeightMeasureSpec); 1785 1786 // Implementation of weights from WindowManager.LayoutParams 1787 // We just grow the dimensions as needed and re-measure if 1788 // needs be 1789 int width = host.getMeasuredWidth(); 1790 int height = host.getMeasuredHeight(); 1791 boolean measureAgain = false; 1792 1793 if (lp.horizontalWeight > 0.0f) { 1794 width += (int) ((mWidth - width) * lp.horizontalWeight); 1795 childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width, 1796 MeasureSpec.EXACTLY); 1797 measureAgain = true; 1798 } 1799 if (lp.verticalWeight > 0.0f) { 1800 height += (int) ((mHeight - height) * lp.verticalWeight); 1801 childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, 1802 MeasureSpec.EXACTLY); 1803 measureAgain = true; 1804 } 1805 1806 if (measureAgain) { 1807 if (DEBUG_LAYOUT) Log.v(TAG, 1808 "And hey let's measure once more: width=" + width 1809 + " height=" + height); 1810 performMeasure(childWidthMeasureSpec, childHeightMeasureSpec); 1811 } 1812 1813 layoutRequested = true; 1814 } 1815 } 1816 } else { 1817 // Not the first pass and no window/insets/visibility change but the window 1818 // may have moved and we need check that and if so to update the left and right 1819 // in the attach info. We translate only the window frame since on window move 1820 // the window manager tells us only for the new frame but the insets are the 1821 // same and we do not want to translate them more than once. 1822 1823 // TODO: Well, we are checking whether the frame has changed similarly 1824 // to how this is done for the insets. This is however incorrect since 1825 // the insets and the frame are translated. For example, the old frame 1826 // was (1, 1 - 1, 1) and was translated to say (2, 2 - 2, 2), now the new 1827 // reported frame is (2, 2 - 2, 2) which implies no change but this is not 1828 // true since we are comparing a not translated value to a translated one. 1829 // This scenario is rare but we may want to fix that. 1830 1831 final boolean windowMoved = (mAttachInfo.mWindowLeft != frame.left 1832 || mAttachInfo.mWindowTop != frame.top); 1833 if (windowMoved) { 1834 if (mTranslator != null) { 1835 mTranslator.translateRectInScreenToAppWinFrame(frame); 1836 } 1837 mAttachInfo.mWindowLeft = frame.left; 1838 mAttachInfo.mWindowTop = frame.top; 1839 1840 // Update the light position for the new window offsets. 1841 if (mAttachInfo.mHardwareRenderer != null) { 1842 mAttachInfo.mHardwareRenderer.setLightCenter(mAttachInfo); 1843 } 1844 } 1845 } 1846 1847 final boolean didLayout = layoutRequested && (!mStopped || mReportNextDraw); 1848 boolean triggerGlobalLayoutListener = didLayout 1849 || mAttachInfo.mRecomputeGlobalAttributes; 1850 if (didLayout) { 1851 performLayout(lp, desiredWindowWidth, desiredWindowHeight); 1852 1853 // By this point all views have been sized and positioned 1854 // We can compute the transparent area 1855 1856 if ((host.mPrivateFlags & View.PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) { 1857 // start out transparent 1858 // TODO: AVOID THAT CALL BY CACHING THE RESULT? 1859 host.getLocationInWindow(mTmpLocation); 1860 mTransparentRegion.set(mTmpLocation[0], mTmpLocation[1], 1861 mTmpLocation[0] + host.mRight - host.mLeft, 1862 mTmpLocation[1] + host.mBottom - host.mTop); 1863 1864 host.gatherTransparentRegion(mTransparentRegion); 1865 if (mTranslator != null) { 1866 mTranslator.translateRegionInWindowToScreen(mTransparentRegion); 1867 } 1868 1869 if (!mTransparentRegion.equals(mPreviousTransparentRegion)) { 1870 mPreviousTransparentRegion.set(mTransparentRegion); 1871 mFullRedrawNeeded = true; 1872 // reconfigure window manager 1873 try { 1874 mWindowSession.setTransparentRegion(mWindow, mTransparentRegion); 1875 } catch (RemoteException e) { 1876 } 1877 } 1878 } 1879 1880 if (DBG) { 1881 System.out.println("======================================"); 1882 System.out.println("performTraversals -- after setFrame"); 1883 host.debug(); 1884 } 1885 } 1886 1887 if (triggerGlobalLayoutListener) { 1888 mAttachInfo.mRecomputeGlobalAttributes = false; 1889 mAttachInfo.mTreeObserver.dispatchOnGlobalLayout(); 1890 } 1891 1892 if (computesInternalInsets) { 1893 // Clear the original insets. 1894 final ViewTreeObserver.InternalInsetsInfo insets = mAttachInfo.mGivenInternalInsets; 1895 insets.reset(); 1896 1897 // Compute new insets in place. 1898 mAttachInfo.mTreeObserver.dispatchOnComputeInternalInsets(insets); 1899 mAttachInfo.mHasNonEmptyGivenInternalInsets = !insets.isEmpty(); 1900 1901 // Tell the window manager. 1902 if (insetsPending || !mLastGivenInsets.equals(insets)) { 1903 mLastGivenInsets.set(insets); 1904 1905 // Translate insets to screen coordinates if needed. 1906 final Rect contentInsets; 1907 final Rect visibleInsets; 1908 final Region touchableRegion; 1909 if (mTranslator != null) { 1910 contentInsets = mTranslator.getTranslatedContentInsets(insets.contentInsets); 1911 visibleInsets = mTranslator.getTranslatedVisibleInsets(insets.visibleInsets); 1912 touchableRegion = mTranslator.getTranslatedTouchableArea(insets.touchableRegion); 1913 } else { 1914 contentInsets = insets.contentInsets; 1915 visibleInsets = insets.visibleInsets; 1916 touchableRegion = insets.touchableRegion; 1917 } 1918 1919 try { 1920 mWindowSession.setInsets(mWindow, insets.mTouchableInsets, 1921 contentInsets, visibleInsets, touchableRegion); 1922 } catch (RemoteException e) { 1923 } 1924 } 1925 } 1926 1927 boolean skipDraw = false; 1928 1929 if (mFirst) { 1930 // handle first focus request 1931 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: mView.hasFocus()=" 1932 + mView.hasFocus()); 1933 if (mView != null) { 1934 if (!mView.hasFocus()) { 1935 mView.requestFocus(View.FOCUS_FORWARD); 1936 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: requested focused view=" 1937 + mView.findFocus()); 1938 } else { 1939 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: existing focused view=" 1940 + mView.findFocus()); 1941 } 1942 } 1943 } else if (mWindowsAnimating) { 1944 if (mRemainingFrameCount <= 0) { 1945 skipDraw = true; 1946 } 1947 mRemainingFrameCount--; 1948 } 1949 1950 mFirst = false; 1951 mWillDrawSoon = false; 1952 mNewSurfaceNeeded = false; 1953 mViewVisibility = viewVisibility; 1954 1955 if (mAttachInfo.mHasWindowFocus && !isInLocalFocusMode()) { 1956 final boolean imTarget = WindowManager.LayoutParams 1957 .mayUseInputMethod(mWindowAttributes.flags); 1958 if (imTarget != mLastWasImTarget) { 1959 mLastWasImTarget = imTarget; 1960 InputMethodManager imm = InputMethodManager.peekInstance(); 1961 if (imm != null && imTarget) { 1962 imm.onPreWindowFocus(mView, true /* hasWindowFocus */); 1963 imm.onPostWindowFocus(mView, mView.findFocus(), 1964 mWindowAttributes.softInputMode, 1965 !mHasHadWindowFocus, mWindowAttributes.flags); 1966 } 1967 } 1968 } 1969 1970 // Remember if we must report the next draw. 1971 if ((relayoutResult & WindowManagerGlobal.RELAYOUT_RES_FIRST_TIME) != 0) { 1972 mReportNextDraw = true; 1973 } 1974 1975 boolean cancelDraw = mAttachInfo.mTreeObserver.dispatchOnPreDraw() || 1976 viewVisibility != View.VISIBLE; 1977 1978 if (!cancelDraw && !newSurface) { 1979 if (!skipDraw || mReportNextDraw) { 1980 if (mPendingTransitions != null && mPendingTransitions.size() > 0) { 1981 for (int i = 0; i < mPendingTransitions.size(); ++i) { 1982 mPendingTransitions.get(i).startChangingAnimations(); 1983 } 1984 mPendingTransitions.clear(); 1985 } 1986 1987 performDraw(); 1988 } 1989 } else { 1990 if (viewVisibility == View.VISIBLE) { 1991 // Try again 1992 scheduleTraversals(); 1993 } else if (mPendingTransitions != null && mPendingTransitions.size() > 0) { 1994 for (int i = 0; i < mPendingTransitions.size(); ++i) { 1995 mPendingTransitions.get(i).endChangingAnimations(); 1996 } 1997 mPendingTransitions.clear(); 1998 } 1999 } 2000 2001 mIsInTraversal = false; 2002 } 2003 2004 private void handleOutOfResourcesException(Surface.OutOfResourcesException e) { 2005 Log.e(TAG, "OutOfResourcesException initializing HW surface", e); 2006 try { 2007 if (!mWindowSession.outOfMemory(mWindow) && 2008 Process.myUid() != Process.SYSTEM_UID) { 2009 Slog.w(TAG, "No processes killed for memory; killing self"); 2010 Process.killProcess(Process.myPid()); 2011 } 2012 } catch (RemoteException ex) { 2013 } 2014 mLayoutRequested = true; // ask wm for a new surface next time. 2015 } 2016 2017 private void performMeasure(int childWidthMeasureSpec, int childHeightMeasureSpec) { 2018 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "measure"); 2019 try { 2020 mView.measure(childWidthMeasureSpec, childHeightMeasureSpec); 2021 } finally { 2022 Trace.traceEnd(Trace.TRACE_TAG_VIEW); 2023 } 2024 } 2025 2026 /** 2027 * Called by {@link android.view.View#isInLayout()} to determine whether the view hierarchy 2028 * is currently undergoing a layout pass. 2029 * 2030 * @return whether the view hierarchy is currently undergoing a layout pass 2031 */ 2032 boolean isInLayout() { 2033 return mInLayout; 2034 } 2035 2036 /** 2037 * Called by {@link android.view.View#requestLayout()} if the view hierarchy is currently 2038 * undergoing a layout pass. requestLayout() should not generally be called during layout, 2039 * unless the container hierarchy knows what it is doing (i.e., it is fine as long as 2040 * all children in that container hierarchy are measured and laid out at the end of the layout 2041 * pass for that container). If requestLayout() is called anyway, we handle it correctly 2042 * by registering all requesters during a frame as it proceeds. At the end of the frame, 2043 * we check all of those views to see if any still have pending layout requests, which 2044 * indicates that they were not correctly handled by their container hierarchy. If that is 2045 * the case, we clear all such flags in the tree, to remove the buggy flag state that leads 2046 * to blank containers, and force a second request/measure/layout pass in this frame. If 2047 * more requestLayout() calls are received during that second layout pass, we post those 2048 * requests to the next frame to avoid possible infinite loops. 2049 * 2050 * <p>The return value from this method indicates whether the request should proceed 2051 * (if it is a request during the first layout pass) or should be skipped and posted to the 2052 * next frame (if it is a request during the second layout pass).</p> 2053 * 2054 * @param view the view that requested the layout. 2055 * 2056 * @return true if request should proceed, false otherwise. 2057 */ 2058 boolean requestLayoutDuringLayout(final View view) { 2059 if (view.mParent == null || view.mAttachInfo == null) { 2060 // Would not normally trigger another layout, so just let it pass through as usual 2061 return true; 2062 } 2063 if (!mLayoutRequesters.contains(view)) { 2064 mLayoutRequesters.add(view); 2065 } 2066 if (!mHandlingLayoutInLayoutRequest) { 2067 // Let the request proceed normally; it will be processed in a second layout pass 2068 // if necessary 2069 return true; 2070 } else { 2071 // Don't let the request proceed during the second layout pass. 2072 // It will post to the next frame instead. 2073 return false; 2074 } 2075 } 2076 2077 private void performLayout(WindowManager.LayoutParams lp, int desiredWindowWidth, 2078 int desiredWindowHeight) { 2079 mLayoutRequested = false; 2080 mScrollMayChange = true; 2081 mInLayout = true; 2082 2083 final View host = mView; 2084 if (DEBUG_ORIENTATION || DEBUG_LAYOUT) { 2085 Log.v(TAG, "Laying out " + host + " to (" + 2086 host.getMeasuredWidth() + ", " + host.getMeasuredHeight() + ")"); 2087 } 2088 2089 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "layout"); 2090 try { 2091 host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight()); 2092 2093 mInLayout = false; 2094 int numViewsRequestingLayout = mLayoutRequesters.size(); 2095 if (numViewsRequestingLayout > 0) { 2096 // requestLayout() was called during layout. 2097 // If no layout-request flags are set on the requesting views, there is no problem. 2098 // If some requests are still pending, then we need to clear those flags and do 2099 // a full request/measure/layout pass to handle this situation. 2100 ArrayList<View> validLayoutRequesters = getValidLayoutRequesters(mLayoutRequesters, 2101 false); 2102 if (validLayoutRequesters != null) { 2103 // Set this flag to indicate that any further requests are happening during 2104 // the second pass, which may result in posting those requests to the next 2105 // frame instead 2106 mHandlingLayoutInLayoutRequest = true; 2107 2108 // Process fresh layout requests, then measure and layout 2109 int numValidRequests = validLayoutRequesters.size(); 2110 for (int i = 0; i < numValidRequests; ++i) { 2111 final View view = validLayoutRequesters.get(i); 2112 Log.w("View", "requestLayout() improperly called by " + view + 2113 " during layout: running second layout pass"); 2114 view.requestLayout(); 2115 } 2116 measureHierarchy(host, lp, mView.getContext().getResources(), 2117 desiredWindowWidth, desiredWindowHeight); 2118 mInLayout = true; 2119 host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight()); 2120 2121 mHandlingLayoutInLayoutRequest = false; 2122 2123 // Check the valid requests again, this time without checking/clearing the 2124 // layout flags, since requests happening during the second pass get noop'd 2125 validLayoutRequesters = getValidLayoutRequesters(mLayoutRequesters, true); 2126 if (validLayoutRequesters != null) { 2127 final ArrayList<View> finalRequesters = validLayoutRequesters; 2128 // Post second-pass requests to the next frame 2129 getRunQueue().post(new Runnable() { 2130 @Override 2131 public void run() { 2132 int numValidRequests = finalRequesters.size(); 2133 for (int i = 0; i < numValidRequests; ++i) { 2134 final View view = finalRequesters.get(i); 2135 Log.w("View", "requestLayout() improperly called by " + view + 2136 " during second layout pass: posting in next frame"); 2137 view.requestLayout(); 2138 } 2139 } 2140 }); 2141 } 2142 } 2143 2144 } 2145 } finally { 2146 Trace.traceEnd(Trace.TRACE_TAG_VIEW); 2147 } 2148 mInLayout = false; 2149 } 2150 2151 /** 2152 * This method is called during layout when there have been calls to requestLayout() during 2153 * layout. It walks through the list of views that requested layout to determine which ones 2154 * still need it, based on visibility in the hierarchy and whether they have already been 2155 * handled (as is usually the case with ListView children). 2156 * 2157 * @param layoutRequesters The list of views that requested layout during layout 2158 * @param secondLayoutRequests Whether the requests were issued during the second layout pass. 2159 * If so, the FORCE_LAYOUT flag was not set on requesters. 2160 * @return A list of the actual views that still need to be laid out. 2161 */ 2162 private ArrayList<View> getValidLayoutRequesters(ArrayList<View> layoutRequesters, 2163 boolean secondLayoutRequests) { 2164 2165 int numViewsRequestingLayout = layoutRequesters.size(); 2166 ArrayList<View> validLayoutRequesters = null; 2167 for (int i = 0; i < numViewsRequestingLayout; ++i) { 2168 View view = layoutRequesters.get(i); 2169 if (view != null && view.mAttachInfo != null && view.mParent != null && 2170 (secondLayoutRequests || (view.mPrivateFlags & View.PFLAG_FORCE_LAYOUT) == 2171 View.PFLAG_FORCE_LAYOUT)) { 2172 boolean gone = false; 2173 View parent = view; 2174 // Only trigger new requests for views in a non-GONE hierarchy 2175 while (parent != null) { 2176 if ((parent.mViewFlags & View.VISIBILITY_MASK) == View.GONE) { 2177 gone = true; 2178 break; 2179 } 2180 if (parent.mParent instanceof View) { 2181 parent = (View) parent.mParent; 2182 } else { 2183 parent = null; 2184 } 2185 } 2186 if (!gone) { 2187 if (validLayoutRequesters == null) { 2188 validLayoutRequesters = new ArrayList<View>(); 2189 } 2190 validLayoutRequesters.add(view); 2191 } 2192 } 2193 } 2194 if (!secondLayoutRequests) { 2195 // If we're checking the layout flags, then we need to clean them up also 2196 for (int i = 0; i < numViewsRequestingLayout; ++i) { 2197 View view = layoutRequesters.get(i); 2198 while (view != null && 2199 (view.mPrivateFlags & View.PFLAG_FORCE_LAYOUT) != 0) { 2200 view.mPrivateFlags &= ~View.PFLAG_FORCE_LAYOUT; 2201 if (view.mParent instanceof View) { 2202 view = (View) view.mParent; 2203 } else { 2204 view = null; 2205 } 2206 } 2207 } 2208 } 2209 layoutRequesters.clear(); 2210 return validLayoutRequesters; 2211 } 2212 2213 @Override 2214 public void requestTransparentRegion(View child) { 2215 // the test below should not fail unless someone is messing with us 2216 checkThread(); 2217 if (mView == child) { 2218 mView.mPrivateFlags |= View.PFLAG_REQUEST_TRANSPARENT_REGIONS; 2219 // Need to make sure we re-evaluate the window attributes next 2220 // time around, to ensure the window has the correct format. 2221 mWindowAttributesChanged = true; 2222 mWindowAttributesChangesFlag = 0; 2223 requestLayout(); 2224 } 2225 } 2226 2227 /** 2228 * Figures out the measure spec for the root view in a window based on it's 2229 * layout params. 2230 * 2231 * @param windowSize 2232 * The available width or height of the window 2233 * 2234 * @param rootDimension 2235 * The layout params for one dimension (width or height) of the 2236 * window. 2237 * 2238 * @return The measure spec to use to measure the root view. 2239 */ 2240 private static int getRootMeasureSpec(int windowSize, int rootDimension) { 2241 int measureSpec; 2242 switch (rootDimension) { 2243 2244 case ViewGroup.LayoutParams.MATCH_PARENT: 2245 // Window can't resize. Force root view to be windowSize. 2246 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY); 2247 break; 2248 case ViewGroup.LayoutParams.WRAP_CONTENT: 2249 // Window can resize. Set max size for root view. 2250 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST); 2251 break; 2252 default: 2253 // Window wants to be an exact size. Force root view to be that size. 2254 measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY); 2255 break; 2256 } 2257 return measureSpec; 2258 } 2259 2260 int mHardwareXOffset; 2261 int mHardwareYOffset; 2262 2263 @Override 2264 public void onHardwarePreDraw(DisplayListCanvas canvas) { 2265 canvas.translate(-mHardwareXOffset, -mHardwareYOffset); 2266 } 2267 2268 @Override 2269 public void onHardwarePostDraw(DisplayListCanvas canvas) { 2270 drawAccessibilityFocusedDrawableIfNeeded(canvas); 2271 } 2272 2273 /** 2274 * @hide 2275 */ 2276 void outputDisplayList(View view) { 2277 view.mRenderNode.output(); 2278 if (mAttachInfo.mHardwareRenderer != null) { 2279 ((ThreadedRenderer)mAttachInfo.mHardwareRenderer).serializeDisplayListTree(); 2280 } 2281 } 2282 2283 /** 2284 * @see #PROPERTY_PROFILE_RENDERING 2285 */ 2286 private void profileRendering(boolean enabled) { 2287 if (mProfileRendering) { 2288 mRenderProfilingEnabled = enabled; 2289 2290 if (mRenderProfiler != null) { 2291 mChoreographer.removeFrameCallback(mRenderProfiler); 2292 } 2293 if (mRenderProfilingEnabled) { 2294 if (mRenderProfiler == null) { 2295 mRenderProfiler = new Choreographer.FrameCallback() { 2296 @Override 2297 public void doFrame(long frameTimeNanos) { 2298 mDirty.set(0, 0, mWidth, mHeight); 2299 scheduleTraversals(); 2300 if (mRenderProfilingEnabled) { 2301 mChoreographer.postFrameCallback(mRenderProfiler); 2302 } 2303 } 2304 }; 2305 } 2306 mChoreographer.postFrameCallback(mRenderProfiler); 2307 } else { 2308 mRenderProfiler = null; 2309 } 2310 } 2311 } 2312 2313 /** 2314 * Called from draw() when DEBUG_FPS is enabled 2315 */ 2316 private void trackFPS() { 2317 // Tracks frames per second drawn. First value in a series of draws may be bogus 2318 // because it down not account for the intervening idle time 2319 long nowTime = System.currentTimeMillis(); 2320 if (mFpsStartTime < 0) { 2321 mFpsStartTime = mFpsPrevTime = nowTime; 2322 mFpsNumFrames = 0; 2323 } else { 2324 ++mFpsNumFrames; 2325 String thisHash = Integer.toHexString(System.identityHashCode(this)); 2326 long frameTime = nowTime - mFpsPrevTime; 2327 long totalTime = nowTime - mFpsStartTime; 2328 Log.v(TAG, "0x" + thisHash + "\tFrame time:\t" + frameTime); 2329 mFpsPrevTime = nowTime; 2330 if (totalTime > 1000) { 2331 float fps = (float) mFpsNumFrames * 1000 / totalTime; 2332 Log.v(TAG, "0x" + thisHash + "\tFPS:\t" + fps); 2333 mFpsStartTime = nowTime; 2334 mFpsNumFrames = 0; 2335 } 2336 } 2337 } 2338 2339 private void performDraw() { 2340 if (mAttachInfo.mDisplayState == Display.STATE_OFF && !mReportNextDraw) { 2341 return; 2342 } 2343 2344 final boolean fullRedrawNeeded = mFullRedrawNeeded; 2345 mFullRedrawNeeded = false; 2346 2347 mIsDrawing = true; 2348 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "draw"); 2349 try { 2350 draw(fullRedrawNeeded); 2351 } finally { 2352 mIsDrawing = false; 2353 Trace.traceEnd(Trace.TRACE_TAG_VIEW); 2354 } 2355 2356 // For whatever reason we didn't create a HardwareRenderer, end any 2357 // hardware animations that are now dangling 2358 if (mAttachInfo.mPendingAnimatingRenderNodes != null) { 2359 final int count = mAttachInfo.mPendingAnimatingRenderNodes.size(); 2360 for (int i = 0; i < count; i++) { 2361 mAttachInfo.mPendingAnimatingRenderNodes.get(i).endAllAnimators(); 2362 } 2363 mAttachInfo.mPendingAnimatingRenderNodes.clear(); 2364 } 2365 2366 if (mReportNextDraw) { 2367 mReportNextDraw = false; 2368 if (mAttachInfo.mHardwareRenderer != null) { 2369 mAttachInfo.mHardwareRenderer.fence(); 2370 } 2371 2372 if (LOCAL_LOGV) { 2373 Log.v(TAG, "FINISHED DRAWING: " + mWindowAttributes.getTitle()); 2374 } 2375 if (mSurfaceHolder != null && mSurface.isValid()) { 2376 mSurfaceHolderCallback.surfaceRedrawNeeded(mSurfaceHolder); 2377 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks(); 2378 if (callbacks != null) { 2379 for (SurfaceHolder.Callback c : callbacks) { 2380 if (c instanceof SurfaceHolder.Callback2) { 2381 ((SurfaceHolder.Callback2)c).surfaceRedrawNeeded( 2382 mSurfaceHolder); 2383 } 2384 } 2385 } 2386 } 2387 try { 2388 mWindowSession.finishDrawing(mWindow); 2389 } catch (RemoteException e) { 2390 } 2391 } 2392 } 2393 2394 private void draw(boolean fullRedrawNeeded) { 2395 Surface surface = mSurface; 2396 if (!surface.isValid()) { 2397 return; 2398 } 2399 2400 if (DEBUG_FPS) { 2401 trackFPS(); 2402 } 2403 2404 if (!sFirstDrawComplete) { 2405 synchronized (sFirstDrawHandlers) { 2406 sFirstDrawComplete = true; 2407 final int count = sFirstDrawHandlers.size(); 2408 for (int i = 0; i< count; i++) { 2409 mHandler.post(sFirstDrawHandlers.get(i)); 2410 } 2411 } 2412 } 2413 2414 scrollToRectOrFocus(null, false); 2415 2416 if (mAttachInfo.mViewScrollChanged) { 2417 mAttachInfo.mViewScrollChanged = false; 2418 mAttachInfo.mTreeObserver.dispatchOnScrollChanged(); 2419 } 2420 2421 boolean animating = mScroller != null && mScroller.computeScrollOffset(); 2422 final int curScrollY; 2423 if (animating) { 2424 curScrollY = mScroller.getCurrY(); 2425 } else { 2426 curScrollY = mScrollY; 2427 } 2428 if (mCurScrollY != curScrollY) { 2429 mCurScrollY = curScrollY; 2430 fullRedrawNeeded = true; 2431 if (mView instanceof RootViewSurfaceTaker) { 2432 ((RootViewSurfaceTaker) mView).onRootViewScrollYChanged(mCurScrollY); 2433 } 2434 } 2435 2436 final float appScale = mAttachInfo.mApplicationScale; 2437 final boolean scalingRequired = mAttachInfo.mScalingRequired; 2438 2439 int resizeAlpha = 0; 2440 2441 final Rect dirty = mDirty; 2442 if (mSurfaceHolder != null) { 2443 // The app owns the surface, we won't draw. 2444 dirty.setEmpty(); 2445 if (animating && mScroller != null) { 2446 mScroller.abortAnimation(); 2447 } 2448 return; 2449 } 2450 2451 if (fullRedrawNeeded) { 2452 mAttachInfo.mIgnoreDirtyState = true; 2453 dirty.set(0, 0, (int) (mWidth * appScale + 0.5f), (int) (mHeight * appScale + 0.5f)); 2454 } 2455 2456 if (DEBUG_ORIENTATION || DEBUG_DRAW) { 2457 Log.v(TAG, "Draw " + mView + "/" 2458 + mWindowAttributes.getTitle() 2459 + ": dirty={" + dirty.left + "," + dirty.top 2460 + "," + dirty.right + "," + dirty.bottom + "} surface=" 2461 + surface + " surface.isValid()=" + surface.isValid() + ", appScale:" + 2462 appScale + ", width=" + mWidth + ", height=" + mHeight); 2463 } 2464 2465 mAttachInfo.mTreeObserver.dispatchOnDraw(); 2466 2467 int xOffset = 0; 2468 int yOffset = curScrollY; 2469 final WindowManager.LayoutParams params = mWindowAttributes; 2470 final Rect surfaceInsets = params != null ? params.surfaceInsets : null; 2471 if (surfaceInsets != null) { 2472 xOffset -= surfaceInsets.left; 2473 yOffset -= surfaceInsets.top; 2474 2475 // Offset dirty rect for surface insets. 2476 dirty.offset(surfaceInsets.left, surfaceInsets.right); 2477 } 2478 2479 boolean accessibilityFocusDirty = false; 2480 final Drawable drawable = mAttachInfo.mAccessibilityFocusDrawable; 2481 if (drawable != null) { 2482 final Rect bounds = mAttachInfo.mTmpInvalRect; 2483 final boolean hasFocus = getAccessibilityFocusedRect(bounds); 2484 if (!hasFocus) { 2485 bounds.setEmpty(); 2486 } 2487 if (!bounds.equals(drawable.getBounds())) { 2488 accessibilityFocusDirty = true; 2489 } 2490 } 2491 2492 mAttachInfo.mDrawingTime = 2493 mChoreographer.getFrameTimeNanos() / TimeUtils.NANOS_PER_MS; 2494 2495 if (!dirty.isEmpty() || mIsAnimating || accessibilityFocusDirty) { 2496 if (mAttachInfo.mHardwareRenderer != null && mAttachInfo.mHardwareRenderer.isEnabled()) { 2497 // If accessibility focus moved, always invalidate the root. 2498 boolean invalidateRoot = accessibilityFocusDirty; 2499 2500 // Draw with hardware renderer. 2501 mIsAnimating = false; 2502 2503 if (mHardwareYOffset != yOffset || mHardwareXOffset != xOffset) { 2504 mHardwareYOffset = yOffset; 2505 mHardwareXOffset = xOffset; 2506 invalidateRoot = true; 2507 } 2508 2509 if (invalidateRoot) { 2510 mAttachInfo.mHardwareRenderer.invalidateRoot(); 2511 } 2512 2513 dirty.setEmpty(); 2514 2515 mAttachInfo.mHardwareRenderer.draw(mView, mAttachInfo, this); 2516 } else { 2517 // If we get here with a disabled & requested hardware renderer, something went 2518 // wrong (an invalidate posted right before we destroyed the hardware surface 2519 // for instance) so we should just bail out. Locking the surface with software 2520 // rendering at this point would lock it forever and prevent hardware renderer 2521 // from doing its job when it comes back. 2522 // Before we request a new frame we must however attempt to reinitiliaze the 2523 // hardware renderer if it's in requested state. This would happen after an 2524 // eglTerminate() for instance. 2525 if (mAttachInfo.mHardwareRenderer != null && 2526 !mAttachInfo.mHardwareRenderer.isEnabled() && 2527 mAttachInfo.mHardwareRenderer.isRequested()) { 2528 2529 try { 2530 mAttachInfo.mHardwareRenderer.initializeIfNeeded( 2531 mWidth, mHeight, mAttachInfo, mSurface, surfaceInsets); 2532 } catch (OutOfResourcesException e) { 2533 handleOutOfResourcesException(e); 2534 return; 2535 } 2536 2537 mFullRedrawNeeded = true; 2538 scheduleTraversals(); 2539 return; 2540 } 2541 2542 if (!drawSoftware(surface, mAttachInfo, xOffset, yOffset, scalingRequired, dirty)) { 2543 return; 2544 } 2545 } 2546 } 2547 2548 if (animating) { 2549 mFullRedrawNeeded = true; 2550 scheduleTraversals(); 2551 } 2552 } 2553 2554 /** 2555 * @return true if drawing was successful, false if an error occurred 2556 */ 2557 private boolean drawSoftware(Surface surface, AttachInfo attachInfo, int xoff, int yoff, 2558 boolean scalingRequired, Rect dirty) { 2559 2560 // Draw with software renderer. 2561 final Canvas canvas; 2562 try { 2563 final int left = dirty.left; 2564 final int top = dirty.top; 2565 final int right = dirty.right; 2566 final int bottom = dirty.bottom; 2567 2568 canvas = mSurface.lockCanvas(dirty); 2569 2570 // The dirty rectangle can be modified by Surface.lockCanvas() 2571 //noinspection ConstantConditions 2572 if (left != dirty.left || top != dirty.top || right != dirty.right 2573 || bottom != dirty.bottom) { 2574 attachInfo.mIgnoreDirtyState = true; 2575 } 2576 2577 // TODO: Do this in native 2578 canvas.setDensity(mDensity); 2579 } catch (Surface.OutOfResourcesException e) { 2580 handleOutOfResourcesException(e); 2581 return false; 2582 } catch (IllegalArgumentException e) { 2583 Log.e(TAG, "Could not lock surface", e); 2584 // Don't assume this is due to out of memory, it could be 2585 // something else, and if it is something else then we could 2586 // kill stuff (or ourself) for no reason. 2587 mLayoutRequested = true; // ask wm for a new surface next time. 2588 return false; 2589 } 2590 2591 try { 2592 if (DEBUG_ORIENTATION || DEBUG_DRAW) { 2593 Log.v(TAG, "Surface " + surface + " drawing to bitmap w=" 2594 + canvas.getWidth() + ", h=" + canvas.getHeight()); 2595 //canvas.drawARGB(255, 255, 0, 0); 2596 } 2597 2598 // If this bitmap's format includes an alpha channel, we 2599 // need to clear it before drawing so that the child will 2600 // properly re-composite its drawing on a transparent 2601 // background. This automatically respects the clip/dirty region 2602 // or 2603 // If we are applying an offset, we need to clear the area 2604 // where the offset doesn't appear to avoid having garbage 2605 // left in the blank areas. 2606 if (!canvas.isOpaque() || yoff != 0 || xoff != 0) { 2607 canvas.drawColor(0, PorterDuff.Mode.CLEAR); 2608 } 2609 2610 dirty.setEmpty(); 2611 mIsAnimating = false; 2612 mView.mPrivateFlags |= View.PFLAG_DRAWN; 2613 2614 if (DEBUG_DRAW) { 2615 Context cxt = mView.getContext(); 2616 Log.i(TAG, "Drawing: package:" + cxt.getPackageName() + 2617 ", metrics=" + cxt.getResources().getDisplayMetrics() + 2618 ", compatibilityInfo=" + cxt.getResources().getCompatibilityInfo()); 2619 } 2620 try { 2621 canvas.translate(-xoff, -yoff); 2622 if (mTranslator != null) { 2623 mTranslator.translateCanvas(canvas); 2624 } 2625 canvas.setScreenDensity(scalingRequired ? mNoncompatDensity : 0); 2626 attachInfo.mSetIgnoreDirtyState = false; 2627 2628 mView.draw(canvas); 2629 2630 drawAccessibilityFocusedDrawableIfNeeded(canvas); 2631 } finally { 2632 if (!attachInfo.mSetIgnoreDirtyState) { 2633 // Only clear the flag if it was not set during the mView.draw() call 2634 attachInfo.mIgnoreDirtyState = false; 2635 } 2636 } 2637 } finally { 2638 try { 2639 surface.unlockCanvasAndPost(canvas); 2640 } catch (IllegalArgumentException e) { 2641 Log.e(TAG, "Could not unlock surface", e); 2642 mLayoutRequested = true; // ask wm for a new surface next time. 2643 //noinspection ReturnInsideFinallyBlock 2644 return false; 2645 } 2646 2647 if (LOCAL_LOGV) { 2648 Log.v(TAG, "Surface " + surface + " unlockCanvasAndPost"); 2649 } 2650 } 2651 return true; 2652 } 2653 2654 /** 2655 * We want to draw a highlight around the current accessibility focused. 2656 * Since adding a style for all possible view is not a viable option we 2657 * have this specialized drawing method. 2658 * 2659 * Note: We are doing this here to be able to draw the highlight for 2660 * virtual views in addition to real ones. 2661 * 2662 * @param canvas The canvas on which to draw. 2663 */ 2664 private void drawAccessibilityFocusedDrawableIfNeeded(Canvas canvas) { 2665 final Rect bounds = mAttachInfo.mTmpInvalRect; 2666 if (getAccessibilityFocusedRect(bounds)) { 2667 final Drawable drawable = getAccessibilityFocusedDrawable(); 2668 if (drawable != null) { 2669 drawable.setBounds(bounds); 2670 drawable.draw(canvas); 2671 } 2672 } else if (mAttachInfo.mAccessibilityFocusDrawable != null) { 2673 mAttachInfo.mAccessibilityFocusDrawable.setBounds(0, 0, 0, 0); 2674 } 2675 } 2676 2677 private boolean getAccessibilityFocusedRect(Rect bounds) { 2678 final AccessibilityManager manager = AccessibilityManager.getInstance(mView.mContext); 2679 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) { 2680 return false; 2681 } 2682 2683 final View host = mAccessibilityFocusedHost; 2684 if (host == null || host.mAttachInfo == null) { 2685 return false; 2686 } 2687 2688 final AccessibilityNodeProvider provider = host.getAccessibilityNodeProvider(); 2689 if (provider == null) { 2690 host.getBoundsOnScreen(bounds, true); 2691 } else if (mAccessibilityFocusedVirtualView != null) { 2692 mAccessibilityFocusedVirtualView.getBoundsInScreen(bounds); 2693 } else { 2694 return false; 2695 } 2696 2697 // Transform the rect into window-relative coordinates. 2698 final AttachInfo attachInfo = mAttachInfo; 2699 bounds.offset(0, attachInfo.mViewRootImpl.mScrollY); 2700 bounds.offset(-attachInfo.mWindowLeft, -attachInfo.mWindowTop); 2701 if (!bounds.intersect(0, 0, attachInfo.mViewRootImpl.mWidth, 2702 attachInfo.mViewRootImpl.mHeight)) { 2703 // If no intersection, set bounds to empty. 2704 bounds.setEmpty(); 2705 } 2706 return !bounds.isEmpty(); 2707 } 2708 2709 private Drawable getAccessibilityFocusedDrawable() { 2710 // Lazily load the accessibility focus drawable. 2711 if (mAttachInfo.mAccessibilityFocusDrawable == null) { 2712 final TypedValue value = new TypedValue(); 2713 final boolean resolved = mView.mContext.getTheme().resolveAttribute( 2714 R.attr.accessibilityFocusedDrawable, value, true); 2715 if (resolved) { 2716 mAttachInfo.mAccessibilityFocusDrawable = 2717 mView.mContext.getDrawable(value.resourceId); 2718 } 2719 } 2720 return mAttachInfo.mAccessibilityFocusDrawable; 2721 } 2722 2723 /** 2724 * @hide 2725 */ 2726 public void setDrawDuringWindowsAnimating(boolean value) { 2727 mDrawDuringWindowsAnimating = value; 2728 if (value) { 2729 handleDispatchWindowAnimationStopped(); 2730 } 2731 } 2732 2733 boolean scrollToRectOrFocus(Rect rectangle, boolean immediate) { 2734 final Rect ci = mAttachInfo.mContentInsets; 2735 final Rect vi = mAttachInfo.mVisibleInsets; 2736 int scrollY = 0; 2737 boolean handled = false; 2738 2739 if (vi.left > ci.left || vi.top > ci.top 2740 || vi.right > ci.right || vi.bottom > ci.bottom) { 2741 // We'll assume that we aren't going to change the scroll 2742 // offset, since we want to avoid that unless it is actually 2743 // going to make the focus visible... otherwise we scroll 2744 // all over the place. 2745 scrollY = mScrollY; 2746 // We can be called for two different situations: during a draw, 2747 // to update the scroll position if the focus has changed (in which 2748 // case 'rectangle' is null), or in response to a 2749 // requestChildRectangleOnScreen() call (in which case 'rectangle' 2750 // is non-null and we just want to scroll to whatever that 2751 // rectangle is). 2752 final View focus = mView.findFocus(); 2753 if (focus == null) { 2754 return false; 2755 } 2756 View lastScrolledFocus = (mLastScrolledFocus != null) ? mLastScrolledFocus.get() : null; 2757 if (focus != lastScrolledFocus) { 2758 // If the focus has changed, then ignore any requests to scroll 2759 // to a rectangle; first we want to make sure the entire focus 2760 // view is visible. 2761 rectangle = null; 2762 } 2763 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Eval scroll: focus=" + focus 2764 + " rectangle=" + rectangle + " ci=" + ci 2765 + " vi=" + vi); 2766 if (focus == lastScrolledFocus && !mScrollMayChange && rectangle == null) { 2767 // Optimization: if the focus hasn't changed since last 2768 // time, and no layout has happened, then just leave things 2769 // as they are. 2770 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Keeping scroll y=" 2771 + mScrollY + " vi=" + vi.toShortString()); 2772 } else { 2773 // We need to determine if the currently focused view is 2774 // within the visible part of the window and, if not, apply 2775 // a pan so it can be seen. 2776 mLastScrolledFocus = new WeakReference<View>(focus); 2777 mScrollMayChange = false; 2778 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Need to scroll?"); 2779 // Try to find the rectangle from the focus view. 2780 if (focus.getGlobalVisibleRect(mVisRect, null)) { 2781 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Root w=" 2782 + mView.getWidth() + " h=" + mView.getHeight() 2783 + " ci=" + ci.toShortString() 2784 + " vi=" + vi.toShortString()); 2785 if (rectangle == null) { 2786 focus.getFocusedRect(mTempRect); 2787 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Focus " + focus 2788 + ": focusRect=" + mTempRect.toShortString()); 2789 if (mView instanceof ViewGroup) { 2790 ((ViewGroup) mView).offsetDescendantRectToMyCoords( 2791 focus, mTempRect); 2792 } 2793 if (DEBUG_INPUT_RESIZE) Log.v(TAG, 2794 "Focus in window: focusRect=" 2795 + mTempRect.toShortString() 2796 + " visRect=" + mVisRect.toShortString()); 2797 } else { 2798 mTempRect.set(rectangle); 2799 if (DEBUG_INPUT_RESIZE) Log.v(TAG, 2800 "Request scroll to rect: " 2801 + mTempRect.toShortString() 2802 + " visRect=" + mVisRect.toShortString()); 2803 } 2804 if (mTempRect.intersect(mVisRect)) { 2805 if (DEBUG_INPUT_RESIZE) Log.v(TAG, 2806 "Focus window visible rect: " 2807 + mTempRect.toShortString()); 2808 if (mTempRect.height() > 2809 (mView.getHeight()-vi.top-vi.bottom)) { 2810 // If the focus simply is not going to fit, then 2811 // best is probably just to leave things as-is. 2812 if (DEBUG_INPUT_RESIZE) Log.v(TAG, 2813 "Too tall; leaving scrollY=" + scrollY); 2814 } else if ((mTempRect.top-scrollY) < vi.top) { 2815 scrollY -= vi.top - (mTempRect.top-scrollY); 2816 if (DEBUG_INPUT_RESIZE) Log.v(TAG, 2817 "Top covered; scrollY=" + scrollY); 2818 } else if ((mTempRect.bottom-scrollY) 2819 > (mView.getHeight()-vi.bottom)) { 2820 scrollY += (mTempRect.bottom-scrollY) 2821 - (mView.getHeight()-vi.bottom); 2822 if (DEBUG_INPUT_RESIZE) Log.v(TAG, 2823 "Bottom covered; scrollY=" + scrollY); 2824 } 2825 handled = true; 2826 } 2827 } 2828 } 2829 } 2830 2831 if (scrollY != mScrollY) { 2832 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Pan scroll changed: old=" 2833 + mScrollY + " , new=" + scrollY); 2834 if (!immediate) { 2835 if (mScroller == null) { 2836 mScroller = new Scroller(mView.getContext()); 2837 } 2838 mScroller.startScroll(0, mScrollY, 0, scrollY-mScrollY); 2839 } else if (mScroller != null) { 2840 mScroller.abortAnimation(); 2841 } 2842 mScrollY = scrollY; 2843 } 2844 2845 return handled; 2846 } 2847 2848 /** 2849 * @hide 2850 */ 2851 public View getAccessibilityFocusedHost() { 2852 return mAccessibilityFocusedHost; 2853 } 2854 2855 /** 2856 * @hide 2857 */ 2858 public AccessibilityNodeInfo getAccessibilityFocusedVirtualView() { 2859 return mAccessibilityFocusedVirtualView; 2860 } 2861 2862 void setAccessibilityFocus(View view, AccessibilityNodeInfo node) { 2863 // If we have a virtual view with accessibility focus we need 2864 // to clear the focus and invalidate the virtual view bounds. 2865 if (mAccessibilityFocusedVirtualView != null) { 2866 2867 AccessibilityNodeInfo focusNode = mAccessibilityFocusedVirtualView; 2868 View focusHost = mAccessibilityFocusedHost; 2869 2870 // Wipe the state of the current accessibility focus since 2871 // the call into the provider to clear accessibility focus 2872 // will fire an accessibility event which will end up calling 2873 // this method and we want to have clean state when this 2874 // invocation happens. 2875 mAccessibilityFocusedHost = null; 2876 mAccessibilityFocusedVirtualView = null; 2877 2878 // Clear accessibility focus on the host after clearing state since 2879 // this method may be reentrant. 2880 focusHost.clearAccessibilityFocusNoCallbacks(); 2881 2882 AccessibilityNodeProvider provider = focusHost.getAccessibilityNodeProvider(); 2883 if (provider != null) { 2884 // Invalidate the area of the cleared accessibility focus. 2885 focusNode.getBoundsInParent(mTempRect); 2886 focusHost.invalidate(mTempRect); 2887 // Clear accessibility focus in the virtual node. 2888 final int virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId( 2889 focusNode.getSourceNodeId()); 2890 provider.performAction(virtualNodeId, 2891 AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS, null); 2892 } 2893 focusNode.recycle(); 2894 } 2895 if (mAccessibilityFocusedHost != null) { 2896 // Clear accessibility focus in the view. 2897 mAccessibilityFocusedHost.clearAccessibilityFocusNoCallbacks(); 2898 } 2899 2900 // Set the new focus host and node. 2901 mAccessibilityFocusedHost = view; 2902 mAccessibilityFocusedVirtualView = node; 2903 2904 if (mAttachInfo.mHardwareRenderer != null) { 2905 mAttachInfo.mHardwareRenderer.invalidateRoot(); 2906 } 2907 } 2908 2909 @Override 2910 public void requestChildFocus(View child, View focused) { 2911 if (DEBUG_INPUT_RESIZE) { 2912 Log.v(TAG, "Request child focus: focus now " + focused); 2913 } 2914 checkThread(); 2915 scheduleTraversals(); 2916 } 2917 2918 @Override 2919 public void clearChildFocus(View child) { 2920 if (DEBUG_INPUT_RESIZE) { 2921 Log.v(TAG, "Clearing child focus"); 2922 } 2923 checkThread(); 2924 scheduleTraversals(); 2925 } 2926 2927 @Override 2928 public ViewParent getParentForAccessibility() { 2929 return null; 2930 } 2931 2932 @Override 2933 public void focusableViewAvailable(View v) { 2934 checkThread(); 2935 if (mView != null) { 2936 if (!mView.hasFocus()) { 2937 v.requestFocus(); 2938 } else { 2939 // the one case where will transfer focus away from the current one 2940 // is if the current view is a view group that prefers to give focus 2941 // to its children first AND the view is a descendant of it. 2942 View focused = mView.findFocus(); 2943 if (focused instanceof ViewGroup) { 2944 ViewGroup group = (ViewGroup) focused; 2945 if (group.getDescendantFocusability() == ViewGroup.FOCUS_AFTER_DESCENDANTS 2946 && isViewDescendantOf(v, focused)) { 2947 v.requestFocus(); 2948 } 2949 } 2950 } 2951 } 2952 } 2953 2954 @Override 2955 public void recomputeViewAttributes(View child) { 2956 checkThread(); 2957 if (mView == child) { 2958 mAttachInfo.mRecomputeGlobalAttributes = true; 2959 if (!mWillDrawSoon) { 2960 scheduleTraversals(); 2961 } 2962 } 2963 } 2964 2965 void dispatchDetachedFromWindow() { 2966 if (mView != null && mView.mAttachInfo != null) { 2967 mAttachInfo.mTreeObserver.dispatchOnWindowAttachedChange(false); 2968 mView.dispatchDetachedFromWindow(); 2969 } 2970 2971 mAccessibilityInteractionConnectionManager.ensureNoConnection(); 2972 mAccessibilityManager.removeAccessibilityStateChangeListener( 2973 mAccessibilityInteractionConnectionManager); 2974 mAccessibilityManager.removeHighTextContrastStateChangeListener( 2975 mHighContrastTextManager); 2976 removeSendWindowContentChangedCallback(); 2977 2978 destroyHardwareRenderer(); 2979 2980 setAccessibilityFocus(null, null); 2981 2982 mView.assignParent(null); 2983 mView = null; 2984 mAttachInfo.mRootView = null; 2985 2986 mSurface.release(); 2987 2988 if (mInputQueueCallback != null && mInputQueue != null) { 2989 mInputQueueCallback.onInputQueueDestroyed(mInputQueue); 2990 mInputQueue.dispose(); 2991 mInputQueueCallback = null; 2992 mInputQueue = null; 2993 } 2994 if (mInputEventReceiver != null) { 2995 mInputEventReceiver.dispose(); 2996 mInputEventReceiver = null; 2997 } 2998 try { 2999 mWindowSession.remove(mWindow); 3000 } catch (RemoteException e) { 3001 } 3002 3003 // Dispose the input channel after removing the window so the Window Manager 3004 // doesn't interpret the input channel being closed as an abnormal termination. 3005 if (mInputChannel != null) { 3006 mInputChannel.dispose(); 3007 mInputChannel = null; 3008 } 3009 3010 mDisplayManager.unregisterDisplayListener(mDisplayListener); 3011 3012 unscheduleTraversals(); 3013 } 3014 3015 void updateConfiguration(Configuration config, boolean force) { 3016 if (DEBUG_CONFIGURATION) Log.v(TAG, 3017 "Applying new config to window " 3018 + mWindowAttributes.getTitle() 3019 + ": " + config); 3020 3021 CompatibilityInfo ci = mDisplayAdjustments.getCompatibilityInfo(); 3022 if (!ci.equals(CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO)) { 3023 config = new Configuration(config); 3024 ci.applyToConfiguration(mNoncompatDensity, config); 3025 } 3026 3027 synchronized (sConfigCallbacks) { 3028 for (int i=sConfigCallbacks.size()-1; i>=0; i--) { 3029 sConfigCallbacks.get(i).onConfigurationChanged(config); 3030 } 3031 } 3032 if (mView != null) { 3033 // At this point the resources have been updated to 3034 // have the most recent config, whatever that is. Use 3035 // the one in them which may be newer. 3036 config = mView.getResources().getConfiguration(); 3037 if (force || mLastConfiguration.diff(config) != 0) { 3038 final int lastLayoutDirection = mLastConfiguration.getLayoutDirection(); 3039 final int currentLayoutDirection = config.getLayoutDirection(); 3040 mLastConfiguration.setTo(config); 3041 if (lastLayoutDirection != currentLayoutDirection && 3042 mViewLayoutDirectionInitial == View.LAYOUT_DIRECTION_INHERIT) { 3043 mView.setLayoutDirection(currentLayoutDirection); 3044 } 3045 mView.dispatchConfigurationChanged(config); 3046 } 3047 } 3048 } 3049 3050 /** 3051 * Return true if child is an ancestor of parent, (or equal to the parent). 3052 */ 3053 public static boolean isViewDescendantOf(View child, View parent) { 3054 if (child == parent) { 3055 return true; 3056 } 3057 3058 final ViewParent theParent = child.getParent(); 3059 return (theParent instanceof ViewGroup) && isViewDescendantOf((View) theParent, parent); 3060 } 3061 3062 private static void forceLayout(View view) { 3063 view.forceLayout(); 3064 if (view instanceof ViewGroup) { 3065 ViewGroup group = (ViewGroup) view; 3066 final int count = group.getChildCount(); 3067 for (int i = 0; i < count; i++) { 3068 forceLayout(group.getChildAt(i)); 3069 } 3070 } 3071 } 3072 3073 private final static int MSG_INVALIDATE = 1; 3074 private final static int MSG_INVALIDATE_RECT = 2; 3075 private final static int MSG_DIE = 3; 3076 private final static int MSG_RESIZED = 4; 3077 private final static int MSG_RESIZED_REPORT = 5; 3078 private final static int MSG_WINDOW_FOCUS_CHANGED = 6; 3079 private final static int MSG_DISPATCH_INPUT_EVENT = 7; 3080 private final static int MSG_DISPATCH_APP_VISIBILITY = 8; 3081 private final static int MSG_DISPATCH_GET_NEW_SURFACE = 9; 3082 private final static int MSG_DISPATCH_KEY_FROM_IME = 11; 3083 private final static int MSG_FINISH_INPUT_CONNECTION = 12; 3084 private final static int MSG_CHECK_FOCUS = 13; 3085 private final static int MSG_CLOSE_SYSTEM_DIALOGS = 14; 3086 private final static int MSG_DISPATCH_DRAG_EVENT = 15; 3087 private final static int MSG_DISPATCH_DRAG_LOCATION_EVENT = 16; 3088 private final static int MSG_DISPATCH_SYSTEM_UI_VISIBILITY = 17; 3089 private final static int MSG_UPDATE_CONFIGURATION = 18; 3090 private final static int MSG_PROCESS_INPUT_EVENTS = 19; 3091 private final static int MSG_CLEAR_ACCESSIBILITY_FOCUS_HOST = 21; 3092 private final static int MSG_INVALIDATE_WORLD = 22; 3093 private final static int MSG_WINDOW_MOVED = 23; 3094 private final static int MSG_SYNTHESIZE_INPUT_EVENT = 24; 3095 private final static int MSG_DISPATCH_WINDOW_SHOWN = 25; 3096 private final static int MSG_DISPATCH_WINDOW_ANIMATION_STOPPED = 26; 3097 private final static int MSG_DISPATCH_WINDOW_ANIMATION_STARTED = 27; 3098 3099 final class ViewRootHandler extends Handler { 3100 @Override 3101 public String getMessageName(Message message) { 3102 switch (message.what) { 3103 case MSG_INVALIDATE: 3104 return "MSG_INVALIDATE"; 3105 case MSG_INVALIDATE_RECT: 3106 return "MSG_INVALIDATE_RECT"; 3107 case MSG_DIE: 3108 return "MSG_DIE"; 3109 case MSG_RESIZED: 3110 return "MSG_RESIZED"; 3111 case MSG_RESIZED_REPORT: 3112 return "MSG_RESIZED_REPORT"; 3113 case MSG_WINDOW_FOCUS_CHANGED: 3114 return "MSG_WINDOW_FOCUS_CHANGED"; 3115 case MSG_DISPATCH_INPUT_EVENT: 3116 return "MSG_DISPATCH_INPUT_EVENT"; 3117 case MSG_DISPATCH_APP_VISIBILITY: 3118 return "MSG_DISPATCH_APP_VISIBILITY"; 3119 case MSG_DISPATCH_GET_NEW_SURFACE: 3120 return "MSG_DISPATCH_GET_NEW_SURFACE"; 3121 case MSG_DISPATCH_KEY_FROM_IME: 3122 return "MSG_DISPATCH_KEY_FROM_IME"; 3123 case MSG_FINISH_INPUT_CONNECTION: 3124 return "MSG_FINISH_INPUT_CONNECTION"; 3125 case MSG_CHECK_FOCUS: 3126 return "MSG_CHECK_FOCUS"; 3127 case MSG_CLOSE_SYSTEM_DIALOGS: 3128 return "MSG_CLOSE_SYSTEM_DIALOGS"; 3129 case MSG_DISPATCH_DRAG_EVENT: 3130 return "MSG_DISPATCH_DRAG_EVENT"; 3131 case MSG_DISPATCH_DRAG_LOCATION_EVENT: 3132 return "MSG_DISPATCH_DRAG_LOCATION_EVENT"; 3133 case MSG_DISPATCH_SYSTEM_UI_VISIBILITY: 3134 return "MSG_DISPATCH_SYSTEM_UI_VISIBILITY"; 3135 case MSG_UPDATE_CONFIGURATION: 3136 return "MSG_UPDATE_CONFIGURATION"; 3137 case MSG_PROCESS_INPUT_EVENTS: 3138 return "MSG_PROCESS_INPUT_EVENTS"; 3139 case MSG_CLEAR_ACCESSIBILITY_FOCUS_HOST: 3140 return "MSG_CLEAR_ACCESSIBILITY_FOCUS_HOST"; 3141 case MSG_DISPATCH_WINDOW_ANIMATION_STARTED: 3142 return "MSG_DISPATCH_WINDOW_ANIMATION_STARTED"; 3143 case MSG_DISPATCH_WINDOW_ANIMATION_STOPPED: 3144 return "MSG_DISPATCH_WINDOW_ANIMATION_STOPPED"; 3145 case MSG_WINDOW_MOVED: 3146 return "MSG_WINDOW_MOVED"; 3147 case MSG_SYNTHESIZE_INPUT_EVENT: 3148 return "MSG_SYNTHESIZE_INPUT_EVENT"; 3149 case MSG_DISPATCH_WINDOW_SHOWN: 3150 return "MSG_DISPATCH_WINDOW_SHOWN"; 3151 } 3152 return super.getMessageName(message); 3153 } 3154 3155 @Override 3156 public void handleMessage(Message msg) { 3157 switch (msg.what) { 3158 case MSG_INVALIDATE: 3159 ((View) msg.obj).invalidate(); 3160 break; 3161 case MSG_INVALIDATE_RECT: 3162 final View.AttachInfo.InvalidateInfo info = (View.AttachInfo.InvalidateInfo) msg.obj; 3163 info.target.invalidate(info.left, info.top, info.right, info.bottom); 3164 info.recycle(); 3165 break; 3166 case MSG_PROCESS_INPUT_EVENTS: 3167 mProcessInputEventsScheduled = false; 3168 doProcessInputEvents(); 3169 break; 3170 case MSG_DISPATCH_APP_VISIBILITY: 3171 handleAppVisibility(msg.arg1 != 0); 3172 break; 3173 case MSG_DISPATCH_GET_NEW_SURFACE: 3174 handleGetNewSurface(); 3175 break; 3176 case MSG_RESIZED: { 3177 // Recycled in the fall through... 3178 SomeArgs args = (SomeArgs) msg.obj; 3179 if (mWinFrame.equals(args.arg1) 3180 && mPendingOverscanInsets.equals(args.arg5) 3181 && mPendingContentInsets.equals(args.arg2) 3182 && mPendingStableInsets.equals(args.arg6) 3183 && mPendingVisibleInsets.equals(args.arg3) 3184 && mPendingOutsets.equals(args.arg7) 3185 && args.arg4 == null) { 3186 break; 3187 } 3188 } // fall through... 3189 case MSG_RESIZED_REPORT: 3190 if (mAdded) { 3191 SomeArgs args = (SomeArgs) msg.obj; 3192 3193 Configuration config = (Configuration) args.arg4; 3194 if (config != null) { 3195 updateConfiguration(config, false); 3196 } 3197 3198 mWinFrame.set((Rect) args.arg1); 3199 mPendingOverscanInsets.set((Rect) args.arg5); 3200 mPendingContentInsets.set((Rect) args.arg2); 3201 mPendingStableInsets.set((Rect) args.arg6); 3202 mPendingVisibleInsets.set((Rect) args.arg3); 3203 mPendingOutsets.set((Rect) args.arg7); 3204 3205 args.recycle(); 3206 3207 if (msg.what == MSG_RESIZED_REPORT) { 3208 mReportNextDraw = true; 3209 } 3210 3211 if (mView != null) { 3212 forceLayout(mView); 3213 } 3214 3215 requestLayout(); 3216 } 3217 break; 3218 case MSG_WINDOW_MOVED: 3219 if (mAdded) { 3220 final int w = mWinFrame.width(); 3221 final int h = mWinFrame.height(); 3222 final int l = msg.arg1; 3223 final int t = msg.arg2; 3224 mWinFrame.left = l; 3225 mWinFrame.right = l + w; 3226 mWinFrame.top = t; 3227 mWinFrame.bottom = t + h; 3228 3229 if (mView != null) { 3230 forceLayout(mView); 3231 } 3232 requestLayout(); 3233 } 3234 break; 3235 case MSG_WINDOW_FOCUS_CHANGED: { 3236 if (mAdded) { 3237 boolean hasWindowFocus = msg.arg1 != 0; 3238 mAttachInfo.mHasWindowFocus = hasWindowFocus; 3239 3240 profileRendering(hasWindowFocus); 3241 3242 if (hasWindowFocus) { 3243 boolean inTouchMode = msg.arg2 != 0; 3244 ensureTouchModeLocally(inTouchMode); 3245 3246 if (mAttachInfo.mHardwareRenderer != null && mSurface.isValid()){ 3247 mFullRedrawNeeded = true; 3248 try { 3249 final WindowManager.LayoutParams lp = mWindowAttributes; 3250 final Rect surfaceInsets = lp != null ? lp.surfaceInsets : null; 3251 mAttachInfo.mHardwareRenderer.initializeIfNeeded( 3252 mWidth, mHeight, mAttachInfo, mSurface, surfaceInsets); 3253 } catch (OutOfResourcesException e) { 3254 Log.e(TAG, "OutOfResourcesException locking surface", e); 3255 try { 3256 if (!mWindowSession.outOfMemory(mWindow)) { 3257 Slog.w(TAG, "No processes killed for memory; killing self"); 3258 Process.killProcess(Process.myPid()); 3259 } 3260 } catch (RemoteException ex) { 3261 } 3262 // Retry in a bit. 3263 sendMessageDelayed(obtainMessage(msg.what, msg.arg1, msg.arg2), 500); 3264 return; 3265 } 3266 } 3267 } 3268 3269 mLastWasImTarget = WindowManager.LayoutParams 3270 .mayUseInputMethod(mWindowAttributes.flags); 3271 3272 InputMethodManager imm = InputMethodManager.peekInstance(); 3273 if (imm != null && mLastWasImTarget && !isInLocalFocusMode()) { 3274 imm.onPreWindowFocus(mView, hasWindowFocus); 3275 } 3276 if (mView != null) { 3277 mAttachInfo.mKeyDispatchState.reset(); 3278 mView.dispatchWindowFocusChanged(hasWindowFocus); 3279 mAttachInfo.mTreeObserver.dispatchOnWindowFocusChange(hasWindowFocus); 3280 } 3281 3282 // Note: must be done after the focus change callbacks, 3283 // so all of the view state is set up correctly. 3284 if (hasWindowFocus) { 3285 if (imm != null && mLastWasImTarget && !isInLocalFocusMode()) { 3286 imm.onPostWindowFocus(mView, mView.findFocus(), 3287 mWindowAttributes.softInputMode, 3288 !mHasHadWindowFocus, mWindowAttributes.flags); 3289 } 3290 // Clear the forward bit. We can just do this directly, since 3291 // the window manager doesn't care about it. 3292 mWindowAttributes.softInputMode &= 3293 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION; 3294 ((WindowManager.LayoutParams)mView.getLayoutParams()) 3295 .softInputMode &= 3296 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION; 3297 mHasHadWindowFocus = true; 3298 } 3299 3300 if (mView != null && mAccessibilityManager.isEnabled()) { 3301 if (hasWindowFocus) { 3302 mView.sendAccessibilityEvent( 3303 AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED); 3304 } 3305 } 3306 } 3307 } break; 3308 case MSG_DIE: 3309 doDie(); 3310 break; 3311 case MSG_DISPATCH_INPUT_EVENT: { 3312 SomeArgs args = (SomeArgs)msg.obj; 3313 InputEvent event = (InputEvent)args.arg1; 3314 InputEventReceiver receiver = (InputEventReceiver)args.arg2; 3315 enqueueInputEvent(event, receiver, 0, true); 3316 args.recycle(); 3317 } break; 3318 case MSG_SYNTHESIZE_INPUT_EVENT: { 3319 InputEvent event = (InputEvent)msg.obj; 3320 enqueueInputEvent(event, null, QueuedInputEvent.FLAG_UNHANDLED, true); 3321 } break; 3322 case MSG_DISPATCH_KEY_FROM_IME: { 3323 if (LOCAL_LOGV) Log.v( 3324 TAG, "Dispatching key " 3325 + msg.obj + " from IME to " + mView); 3326 KeyEvent event = (KeyEvent)msg.obj; 3327 if ((event.getFlags()&KeyEvent.FLAG_FROM_SYSTEM) != 0) { 3328 // The IME is trying to say this event is from the 3329 // system! Bad bad bad! 3330 //noinspection UnusedAssignment 3331 event = KeyEvent.changeFlags(event, event.getFlags() & 3332 ~KeyEvent.FLAG_FROM_SYSTEM); 3333 } 3334 enqueueInputEvent(event, null, QueuedInputEvent.FLAG_DELIVER_POST_IME, true); 3335 } break; 3336 case MSG_FINISH_INPUT_CONNECTION: { 3337 InputMethodManager imm = InputMethodManager.peekInstance(); 3338 if (imm != null) { 3339 imm.reportFinishInputConnection((InputConnection)msg.obj); 3340 } 3341 } break; 3342 case MSG_CHECK_FOCUS: { 3343 InputMethodManager imm = InputMethodManager.peekInstance(); 3344 if (imm != null) { 3345 imm.checkFocus(); 3346 } 3347 } break; 3348 case MSG_CLOSE_SYSTEM_DIALOGS: { 3349 if (mView != null) { 3350 mView.onCloseSystemDialogs((String)msg.obj); 3351 } 3352 } break; 3353 case MSG_DISPATCH_DRAG_EVENT: 3354 case MSG_DISPATCH_DRAG_LOCATION_EVENT: { 3355 DragEvent event = (DragEvent)msg.obj; 3356 event.mLocalState = mLocalDragState; // only present when this app called startDrag() 3357 handleDragEvent(event); 3358 } break; 3359 case MSG_DISPATCH_SYSTEM_UI_VISIBILITY: { 3360 handleDispatchSystemUiVisibilityChanged((SystemUiVisibilityInfo) msg.obj); 3361 } break; 3362 case MSG_UPDATE_CONFIGURATION: { 3363 Configuration config = (Configuration)msg.obj; 3364 if (config.isOtherSeqNewer(mLastConfiguration)) { 3365 config = mLastConfiguration; 3366 } 3367 updateConfiguration(config, false); 3368 } break; 3369 case MSG_CLEAR_ACCESSIBILITY_FOCUS_HOST: { 3370 setAccessibilityFocus(null, null); 3371 } break; 3372 case MSG_DISPATCH_WINDOW_ANIMATION_STARTED: { 3373 int remainingFrameCount = msg.arg1; 3374 handleDispatchWindowAnimationStarted(remainingFrameCount); 3375 } break; 3376 case MSG_DISPATCH_WINDOW_ANIMATION_STOPPED: { 3377 handleDispatchWindowAnimationStopped(); 3378 } break; 3379 case MSG_INVALIDATE_WORLD: { 3380 if (mView != null) { 3381 invalidateWorld(mView); 3382 } 3383 } break; 3384 case MSG_DISPATCH_WINDOW_SHOWN: { 3385 handleDispatchWindowShown(); 3386 } 3387 } 3388 } 3389 } 3390 3391 final ViewRootHandler mHandler = new ViewRootHandler(); 3392 3393 /** 3394 * Something in the current window tells us we need to change the touch mode. For 3395 * example, we are not in touch mode, and the user touches the screen. 3396 * 3397 * If the touch mode has changed, tell the window manager, and handle it locally. 3398 * 3399 * @param inTouchMode Whether we want to be in touch mode. 3400 * @return True if the touch mode changed and focus changed was changed as a result 3401 */ 3402 boolean ensureTouchMode(boolean inTouchMode) { 3403 if (DBG) Log.d("touchmode", "ensureTouchMode(" + inTouchMode + "), current " 3404 + "touch mode is " + mAttachInfo.mInTouchMode); 3405 if (mAttachInfo.mInTouchMode == inTouchMode) return false; 3406 3407 // tell the window manager 3408 try { 3409 if (!isInLocalFocusMode()) { 3410 mWindowSession.setInTouchMode(inTouchMode); 3411 } 3412 } catch (RemoteException e) { 3413 throw new RuntimeException(e); 3414 } 3415 3416 // handle the change 3417 return ensureTouchModeLocally(inTouchMode); 3418 } 3419 3420 /** 3421 * Ensure that the touch mode for this window is set, and if it is changing, 3422 * take the appropriate action. 3423 * @param inTouchMode Whether we want to be in touch mode. 3424 * @return True if the touch mode changed and focus changed was changed as a result 3425 */ 3426 private boolean ensureTouchModeLocally(boolean inTouchMode) { 3427 if (DBG) Log.d("touchmode", "ensureTouchModeLocally(" + inTouchMode + "), current " 3428 + "touch mode is " + mAttachInfo.mInTouchMode); 3429 3430 if (mAttachInfo.mInTouchMode == inTouchMode) return false; 3431 3432 mAttachInfo.mInTouchMode = inTouchMode; 3433 mAttachInfo.mTreeObserver.dispatchOnTouchModeChanged(inTouchMode); 3434 3435 return (inTouchMode) ? enterTouchMode() : leaveTouchMode(); 3436 } 3437 3438 private boolean enterTouchMode() { 3439 if (mView != null && mView.hasFocus()) { 3440 // note: not relying on mFocusedView here because this could 3441 // be when the window is first being added, and mFocused isn't 3442 // set yet. 3443 final View focused = mView.findFocus(); 3444 if (focused != null && !focused.isFocusableInTouchMode()) { 3445 final ViewGroup ancestorToTakeFocus = findAncestorToTakeFocusInTouchMode(focused); 3446 if (ancestorToTakeFocus != null) { 3447 // there is an ancestor that wants focus after its 3448 // descendants that is focusable in touch mode.. give it 3449 // focus 3450 return ancestorToTakeFocus.requestFocus(); 3451 } else { 3452 // There's nothing to focus. Clear and propagate through the 3453 // hierarchy, but don't attempt to place new focus. 3454 focused.clearFocusInternal(null, true, false); 3455 return true; 3456 } 3457 } 3458 } 3459 return false; 3460 } 3461 3462 /** 3463 * Find an ancestor of focused that wants focus after its descendants and is 3464 * focusable in touch mode. 3465 * @param focused The currently focused view. 3466 * @return An appropriate view, or null if no such view exists. 3467 */ 3468 private static ViewGroup findAncestorToTakeFocusInTouchMode(View focused) { 3469 ViewParent parent = focused.getParent(); 3470 while (parent instanceof ViewGroup) { 3471 final ViewGroup vgParent = (ViewGroup) parent; 3472 if (vgParent.getDescendantFocusability() == ViewGroup.FOCUS_AFTER_DESCENDANTS 3473 && vgParent.isFocusableInTouchMode()) { 3474 return vgParent; 3475 } 3476 if (vgParent.isRootNamespace()) { 3477 return null; 3478 } else { 3479 parent = vgParent.getParent(); 3480 } 3481 } 3482 return null; 3483 } 3484 3485 private boolean leaveTouchMode() { 3486 if (mView != null) { 3487 if (mView.hasFocus()) { 3488 View focusedView = mView.findFocus(); 3489 if (!(focusedView instanceof ViewGroup)) { 3490 // some view has focus, let it keep it 3491 return false; 3492 } else if (((ViewGroup) focusedView).getDescendantFocusability() != 3493 ViewGroup.FOCUS_AFTER_DESCENDANTS) { 3494 // some view group has focus, and doesn't prefer its children 3495 // over itself for focus, so let them keep it. 3496 return false; 3497 } 3498 } 3499 3500 // find the best view to give focus to in this brave new non-touch-mode 3501 // world 3502 final View focused = focusSearch(null, View.FOCUS_DOWN); 3503 if (focused != null) { 3504 return focused.requestFocus(View.FOCUS_DOWN); 3505 } 3506 } 3507 return false; 3508 } 3509 3510 /** 3511 * Base class for implementing a stage in the chain of responsibility 3512 * for processing input events. 3513 * <p> 3514 * Events are delivered to the stage by the {@link #deliver} method. The stage 3515 * then has the choice of finishing the event or forwarding it to the next stage. 3516 * </p> 3517 */ 3518 abstract class InputStage { 3519 private final InputStage mNext; 3520 3521 protected static final int FORWARD = 0; 3522 protected static final int FINISH_HANDLED = 1; 3523 protected static final int FINISH_NOT_HANDLED = 2; 3524 3525 /** 3526 * Creates an input stage. 3527 * @param next The next stage to which events should be forwarded. 3528 */ 3529 public InputStage(InputStage next) { 3530 mNext = next; 3531 } 3532 3533 /** 3534 * Delivers an event to be processed. 3535 */ 3536 public final void deliver(QueuedInputEvent q) { 3537 if ((q.mFlags & QueuedInputEvent.FLAG_FINISHED) != 0) { 3538 forward(q); 3539 } else if (shouldDropInputEvent(q)) { 3540 finish(q, false); 3541 } else { 3542 apply(q, onProcess(q)); 3543 } 3544 } 3545 3546 /** 3547 * Marks the the input event as finished then forwards it to the next stage. 3548 */ 3549 protected void finish(QueuedInputEvent q, boolean handled) { 3550 q.mFlags |= QueuedInputEvent.FLAG_FINISHED; 3551 if (handled) { 3552 q.mFlags |= QueuedInputEvent.FLAG_FINISHED_HANDLED; 3553 } 3554 forward(q); 3555 } 3556 3557 /** 3558 * Forwards the event to the next stage. 3559 */ 3560 protected void forward(QueuedInputEvent q) { 3561 onDeliverToNext(q); 3562 } 3563 3564 /** 3565 * Applies a result code from {@link #onProcess} to the specified event. 3566 */ 3567 protected void apply(QueuedInputEvent q, int result) { 3568 if (result == FORWARD) { 3569 forward(q); 3570 } else if (result == FINISH_HANDLED) { 3571 finish(q, true); 3572 } else if (result == FINISH_NOT_HANDLED) { 3573 finish(q, false); 3574 } else { 3575 throw new IllegalArgumentException("Invalid result: " + result); 3576 } 3577 } 3578 3579 /** 3580 * Called when an event is ready to be processed. 3581 * @return A result code indicating how the event was handled. 3582 */ 3583 protected int onProcess(QueuedInputEvent q) { 3584 return FORWARD; 3585 } 3586 3587 /** 3588 * Called when an event is being delivered to the next stage. 3589 */ 3590 protected void onDeliverToNext(QueuedInputEvent q) { 3591 if (DEBUG_INPUT_STAGES) { 3592 Log.v(TAG, "Done with " + getClass().getSimpleName() + ". " + q); 3593 } 3594 if (mNext != null) { 3595 mNext.deliver(q); 3596 } else { 3597 finishInputEvent(q); 3598 } 3599 } 3600 3601 protected boolean shouldDropInputEvent(QueuedInputEvent q) { 3602 if (mView == null || !mAdded) { 3603 Slog.w(TAG, "Dropping event due to root view being removed: " + q.mEvent); 3604 return true; 3605 } else if ((!mAttachInfo.mHasWindowFocus 3606 && !q.mEvent.isFromSource(InputDevice.SOURCE_CLASS_POINTER)) || mStopped 3607 || (mPausedForTransition && !isBack(q.mEvent))) { 3608 // This is a focus event and the window doesn't currently have input focus or 3609 // has stopped. This could be an event that came back from the previous stage 3610 // but the window has lost focus or stopped in the meantime. 3611 if (isTerminalInputEvent(q.mEvent)) { 3612 // Don't drop terminal input events, however mark them as canceled. 3613 q.mEvent.cancel(); 3614 Slog.w(TAG, "Cancelling event due to no window focus: " + q.mEvent); 3615 return false; 3616 } 3617 3618 // Drop non-terminal input events. 3619 Slog.w(TAG, "Dropping event due to no window focus: " + q.mEvent); 3620 return true; 3621 } 3622 return false; 3623 } 3624 3625 void dump(String prefix, PrintWriter writer) { 3626 if (mNext != null) { 3627 mNext.dump(prefix, writer); 3628 } 3629 } 3630 3631 private boolean isBack(InputEvent event) { 3632 if (event instanceof KeyEvent) { 3633 return ((KeyEvent) event).getKeyCode() == KeyEvent.KEYCODE_BACK; 3634 } else { 3635 return false; 3636 } 3637 } 3638 } 3639 3640 /** 3641 * Base class for implementing an input pipeline stage that supports 3642 * asynchronous and out-of-order processing of input events. 3643 * <p> 3644 * In addition to what a normal input stage can do, an asynchronous 3645 * input stage may also defer an input event that has been delivered to it 3646 * and finish or forward it later. 3647 * </p> 3648 */ 3649 abstract class AsyncInputStage extends InputStage { 3650 private final String mTraceCounter; 3651 3652 private QueuedInputEvent mQueueHead; 3653 private QueuedInputEvent mQueueTail; 3654 private int mQueueLength; 3655 3656 protected static final int DEFER = 3; 3657 3658 /** 3659 * Creates an asynchronous input stage. 3660 * @param next The next stage to which events should be forwarded. 3661 * @param traceCounter The name of a counter to record the size of 3662 * the queue of pending events. 3663 */ 3664 public AsyncInputStage(InputStage next, String traceCounter) { 3665 super(next); 3666 mTraceCounter = traceCounter; 3667 } 3668 3669 /** 3670 * Marks the event as deferred, which is to say that it will be handled 3671 * asynchronously. The caller is responsible for calling {@link #forward} 3672 * or {@link #finish} later when it is done handling the event. 3673 */ 3674 protected void defer(QueuedInputEvent q) { 3675 q.mFlags |= QueuedInputEvent.FLAG_DEFERRED; 3676 enqueue(q); 3677 } 3678 3679 @Override 3680 protected void forward(QueuedInputEvent q) { 3681 // Clear the deferred flag. 3682 q.mFlags &= ~QueuedInputEvent.FLAG_DEFERRED; 3683 3684 // Fast path if the queue is empty. 3685 QueuedInputEvent curr = mQueueHead; 3686 if (curr == null) { 3687 super.forward(q); 3688 return; 3689 } 3690 3691 // Determine whether the event must be serialized behind any others 3692 // before it can be delivered to the next stage. This is done because 3693 // deferred events might be handled out of order by the stage. 3694 final int deviceId = q.mEvent.getDeviceId(); 3695 QueuedInputEvent prev = null; 3696 boolean blocked = false; 3697 while (curr != null && curr != q) { 3698 if (!blocked && deviceId == curr.mEvent.getDeviceId()) { 3699 blocked = true; 3700 } 3701 prev = curr; 3702 curr = curr.mNext; 3703 } 3704 3705 // If the event is blocked, then leave it in the queue to be delivered later. 3706 // Note that the event might not yet be in the queue if it was not previously 3707 // deferred so we will enqueue it if needed. 3708 if (blocked) { 3709 if (curr == null) { 3710 enqueue(q); 3711 } 3712 return; 3713 } 3714 3715 // The event is not blocked. Deliver it immediately. 3716 if (curr != null) { 3717 curr = curr.mNext; 3718 dequeue(q, prev); 3719 } 3720 super.forward(q); 3721 3722 // Dequeuing this event may have unblocked successors. Deliver them. 3723 while (curr != null) { 3724 if (deviceId == curr.mEvent.getDeviceId()) { 3725 if ((curr.mFlags & QueuedInputEvent.FLAG_DEFERRED) != 0) { 3726 break; 3727 } 3728 QueuedInputEvent next = curr.mNext; 3729 dequeue(curr, prev); 3730 super.forward(curr); 3731 curr = next; 3732 } else { 3733 prev = curr; 3734 curr = curr.mNext; 3735 } 3736 } 3737 } 3738 3739 @Override 3740 protected void apply(QueuedInputEvent q, int result) { 3741 if (result == DEFER) { 3742 defer(q); 3743 } else { 3744 super.apply(q, result); 3745 } 3746 } 3747 3748 private void enqueue(QueuedInputEvent q) { 3749 if (mQueueTail == null) { 3750 mQueueHead = q; 3751 mQueueTail = q; 3752 } else { 3753 mQueueTail.mNext = q; 3754 mQueueTail = q; 3755 } 3756 3757 mQueueLength += 1; 3758 Trace.traceCounter(Trace.TRACE_TAG_INPUT, mTraceCounter, mQueueLength); 3759 } 3760 3761 private void dequeue(QueuedInputEvent q, QueuedInputEvent prev) { 3762 if (prev == null) { 3763 mQueueHead = q.mNext; 3764 } else { 3765 prev.mNext = q.mNext; 3766 } 3767 if (mQueueTail == q) { 3768 mQueueTail = prev; 3769 } 3770 q.mNext = null; 3771 3772 mQueueLength -= 1; 3773 Trace.traceCounter(Trace.TRACE_TAG_INPUT, mTraceCounter, mQueueLength); 3774 } 3775 3776 @Override 3777 void dump(String prefix, PrintWriter writer) { 3778 writer.print(prefix); 3779 writer.print(getClass().getName()); 3780 writer.print(": mQueueLength="); 3781 writer.println(mQueueLength); 3782 3783 super.dump(prefix, writer); 3784 } 3785 } 3786 3787 /** 3788 * Delivers pre-ime input events to a native activity. 3789 * Does not support pointer events. 3790 */ 3791 final class NativePreImeInputStage extends AsyncInputStage 3792 implements InputQueue.FinishedInputEventCallback { 3793 public NativePreImeInputStage(InputStage next, String traceCounter) { 3794 super(next, traceCounter); 3795 } 3796 3797 @Override 3798 protected int onProcess(QueuedInputEvent q) { 3799 if (mInputQueue != null && q.mEvent instanceof KeyEvent) { 3800 mInputQueue.sendInputEvent(q.mEvent, q, true, this); 3801 return DEFER; 3802 } 3803 return FORWARD; 3804 } 3805 3806 @Override 3807 public void onFinishedInputEvent(Object token, boolean handled) { 3808 QueuedInputEvent q = (QueuedInputEvent)token; 3809 if (handled) { 3810 finish(q, true); 3811 return; 3812 } 3813 forward(q); 3814 } 3815 } 3816 3817 /** 3818 * Delivers pre-ime input events to the view hierarchy. 3819 * Does not support pointer events. 3820 */ 3821 final class ViewPreImeInputStage extends InputStage { 3822 public ViewPreImeInputStage(InputStage next) { 3823 super(next); 3824 } 3825 3826 @Override 3827 protected int onProcess(QueuedInputEvent q) { 3828 if (q.mEvent instanceof KeyEvent) { 3829 return processKeyEvent(q); 3830 } 3831 return FORWARD; 3832 } 3833 3834 private int processKeyEvent(QueuedInputEvent q) { 3835 final KeyEvent event = (KeyEvent)q.mEvent; 3836 if (mView.dispatchKeyEventPreIme(event)) { 3837 return FINISH_HANDLED; 3838 } 3839 return FORWARD; 3840 } 3841 } 3842 3843 /** 3844 * Delivers input events to the ime. 3845 * Does not support pointer events. 3846 */ 3847 final class ImeInputStage extends AsyncInputStage 3848 implements InputMethodManager.FinishedInputEventCallback { 3849 public ImeInputStage(InputStage next, String traceCounter) { 3850 super(next, traceCounter); 3851 } 3852 3853 @Override 3854 protected int onProcess(QueuedInputEvent q) { 3855 if (mLastWasImTarget && !isInLocalFocusMode()) { 3856 InputMethodManager imm = InputMethodManager.peekInstance(); 3857 if (imm != null) { 3858 final InputEvent event = q.mEvent; 3859 if (DEBUG_IMF) Log.v(TAG, "Sending input event to IME: " + event); 3860 int result = imm.dispatchInputEvent(event, q, this, mHandler); 3861 if (result == InputMethodManager.DISPATCH_HANDLED) { 3862 return FINISH_HANDLED; 3863 } else if (result == InputMethodManager.DISPATCH_NOT_HANDLED) { 3864 // The IME could not handle it, so skip along to the next InputStage 3865 return FORWARD; 3866 } else { 3867 return DEFER; // callback will be invoked later 3868 } 3869 } 3870 } 3871 return FORWARD; 3872 } 3873 3874 @Override 3875 public void onFinishedInputEvent(Object token, boolean handled) { 3876 QueuedInputEvent q = (QueuedInputEvent)token; 3877 if (handled) { 3878 finish(q, true); 3879 return; 3880 } 3881 forward(q); 3882 } 3883 } 3884 3885 /** 3886 * Performs early processing of post-ime input events. 3887 */ 3888 final class EarlyPostImeInputStage extends InputStage { 3889 public EarlyPostImeInputStage(InputStage next) { 3890 super(next); 3891 } 3892 3893 @Override 3894 protected int onProcess(QueuedInputEvent q) { 3895 if (q.mEvent instanceof KeyEvent) { 3896 return processKeyEvent(q); 3897 } else { 3898 final int source = q.mEvent.getSource(); 3899 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) { 3900 return processPointerEvent(q); 3901 } 3902 } 3903 return FORWARD; 3904 } 3905 3906 private int processKeyEvent(QueuedInputEvent q) { 3907 final KeyEvent event = (KeyEvent)q.mEvent; 3908 3909 // If the key's purpose is to exit touch mode then we consume it 3910 // and consider it handled. 3911 if (checkForLeavingTouchModeAndConsume(event)) { 3912 return FINISH_HANDLED; 3913 } 3914 3915 // Make sure the fallback event policy sees all keys that will be 3916 // delivered to the view hierarchy. 3917 mFallbackEventHandler.preDispatchKeyEvent(event); 3918 return FORWARD; 3919 } 3920 3921 private int processPointerEvent(QueuedInputEvent q) { 3922 final MotionEvent event = (MotionEvent)q.mEvent; 3923 3924 // Translate the pointer event for compatibility, if needed. 3925 if (mTranslator != null) { 3926 mTranslator.translateEventInScreenToAppWindow(event); 3927 } 3928 3929 // Enter touch mode on down or scroll. 3930 final int action = event.getAction(); 3931 if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_SCROLL) { 3932 ensureTouchMode(true); 3933 } 3934 3935 // Offset the scroll position. 3936 if (mCurScrollY != 0) { 3937 event.offsetLocation(0, mCurScrollY); 3938 } 3939 3940 // Remember the touch position for possible drag-initiation. 3941 if (event.isTouchEvent()) { 3942 mLastTouchPoint.x = event.getRawX(); 3943 mLastTouchPoint.y = event.getRawY(); 3944 } 3945 return FORWARD; 3946 } 3947 } 3948 3949 /** 3950 * Delivers post-ime input events to a native activity. 3951 */ 3952 final class NativePostImeInputStage extends AsyncInputStage 3953 implements InputQueue.FinishedInputEventCallback { 3954 public NativePostImeInputStage(InputStage next, String traceCounter) { 3955 super(next, traceCounter); 3956 } 3957 3958 @Override 3959 protected int onProcess(QueuedInputEvent q) { 3960 if (mInputQueue != null) { 3961 mInputQueue.sendInputEvent(q.mEvent, q, false, this); 3962 return DEFER; 3963 } 3964 return FORWARD; 3965 } 3966 3967 @Override 3968 public void onFinishedInputEvent(Object token, boolean handled) { 3969 QueuedInputEvent q = (QueuedInputEvent)token; 3970 if (handled) { 3971 finish(q, true); 3972 return; 3973 } 3974 forward(q); 3975 } 3976 } 3977 3978 /** 3979 * Delivers post-ime input events to the view hierarchy. 3980 */ 3981 final class ViewPostImeInputStage extends InputStage { 3982 public ViewPostImeInputStage(InputStage next) { 3983 super(next); 3984 } 3985 3986 @Override 3987 protected int onProcess(QueuedInputEvent q) { 3988 if (q.mEvent instanceof KeyEvent) { 3989 return processKeyEvent(q); 3990 } else { 3991 // If delivering a new non-key event, make sure the window is 3992 // now allowed to start updating. 3993 handleDispatchWindowAnimationStopped(); 3994 final int source = q.mEvent.getSource(); 3995 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) { 3996 return processPointerEvent(q); 3997 } else if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) { 3998 return processTrackballEvent(q); 3999 } else { 4000 return processGenericMotionEvent(q); 4001 } 4002 } 4003 } 4004 4005 @Override 4006 protected void onDeliverToNext(QueuedInputEvent q) { 4007 if (mUnbufferedInputDispatch 4008 && q.mEvent instanceof MotionEvent 4009 && ((MotionEvent)q.mEvent).isTouchEvent() 4010 && isTerminalInputEvent(q.mEvent)) { 4011 mUnbufferedInputDispatch = false; 4012 scheduleConsumeBatchedInput(); 4013 } 4014 super.onDeliverToNext(q); 4015 } 4016 4017 private int processKeyEvent(QueuedInputEvent q) { 4018 final KeyEvent event = (KeyEvent)q.mEvent; 4019 4020 if (event.getAction() != KeyEvent.ACTION_UP) { 4021 // If delivering a new key event, make sure the window is 4022 // now allowed to start updating. 4023 handleDispatchWindowAnimationStopped(); 4024 } 4025 4026 // Deliver the key to the view hierarchy. 4027 if (mView.dispatchKeyEvent(event)) { 4028 return FINISH_HANDLED; 4029 } 4030 4031 if (shouldDropInputEvent(q)) { 4032 return FINISH_NOT_HANDLED; 4033 } 4034 4035 // If the Control modifier is held, try to interpret the key as a shortcut. 4036 if (event.getAction() == KeyEvent.ACTION_DOWN 4037 && event.isCtrlPressed() 4038 && event.getRepeatCount() == 0 4039 && !KeyEvent.isModifierKey(event.getKeyCode())) { 4040 if (mView.dispatchKeyShortcutEvent(event)) { 4041 return FINISH_HANDLED; 4042 } 4043 if (shouldDropInputEvent(q)) { 4044 return FINISH_NOT_HANDLED; 4045 } 4046 } 4047 4048 // Apply the fallback event policy. 4049 if (mFallbackEventHandler.dispatchKeyEvent(event)) { 4050 return FINISH_HANDLED; 4051 } 4052 if (shouldDropInputEvent(q)) { 4053 return FINISH_NOT_HANDLED; 4054 } 4055 4056 // Handle automatic focus changes. 4057 if (event.getAction() == KeyEvent.ACTION_DOWN) { 4058 int direction = 0; 4059 switch (event.getKeyCode()) { 4060 case KeyEvent.KEYCODE_DPAD_LEFT: 4061 if (event.hasNoModifiers()) { 4062 direction = View.FOCUS_LEFT; 4063 } 4064 break; 4065 case KeyEvent.KEYCODE_DPAD_RIGHT: 4066 if (event.hasNoModifiers()) { 4067 direction = View.FOCUS_RIGHT; 4068 } 4069 break; 4070 case KeyEvent.KEYCODE_DPAD_UP: 4071 if (event.hasNoModifiers()) { 4072 direction = View.FOCUS_UP; 4073 } 4074 break; 4075 case KeyEvent.KEYCODE_DPAD_DOWN: 4076 if (event.hasNoModifiers()) { 4077 direction = View.FOCUS_DOWN; 4078 } 4079 break; 4080 case KeyEvent.KEYCODE_TAB: 4081 if (event.hasNoModifiers()) { 4082 direction = View.FOCUS_FORWARD; 4083 } else if (event.hasModifiers(KeyEvent.META_SHIFT_ON)) { 4084 direction = View.FOCUS_BACKWARD; 4085 } 4086 break; 4087 } 4088 if (direction != 0) { 4089 View focused = mView.findFocus(); 4090 if (focused != null) { 4091 View v = focused.focusSearch(direction); 4092 if (v != null && v != focused) { 4093 // do the math the get the interesting rect 4094 // of previous focused into the coord system of 4095 // newly focused view 4096 focused.getFocusedRect(mTempRect); 4097 if (mView instanceof ViewGroup) { 4098 ((ViewGroup) mView).offsetDescendantRectToMyCoords( 4099 focused, mTempRect); 4100 ((ViewGroup) mView).offsetRectIntoDescendantCoords( 4101 v, mTempRect); 4102 } 4103 if (v.requestFocus(direction, mTempRect)) { 4104 playSoundEffect(SoundEffectConstants 4105 .getContantForFocusDirection(direction)); 4106 return FINISH_HANDLED; 4107 } 4108 } 4109 4110 // Give the focused view a last chance to handle the dpad key. 4111 if (mView.dispatchUnhandledMove(focused, direction)) { 4112 return FINISH_HANDLED; 4113 } 4114 } else { 4115 // find the best view to give focus to in this non-touch-mode with no-focus 4116 View v = focusSearch(null, direction); 4117 if (v != null && v.requestFocus(direction)) { 4118 return FINISH_HANDLED; 4119 } 4120 } 4121 } 4122 } 4123 return FORWARD; 4124 } 4125 4126 private int processPointerEvent(QueuedInputEvent q) { 4127 final MotionEvent event = (MotionEvent)q.mEvent; 4128 4129 mAttachInfo.mUnbufferedDispatchRequested = false; 4130 boolean handled = mView.dispatchPointerEvent(event); 4131 if (mAttachInfo.mUnbufferedDispatchRequested && !mUnbufferedInputDispatch) { 4132 mUnbufferedInputDispatch = true; 4133 if (mConsumeBatchedInputScheduled) { 4134 scheduleConsumeBatchedInputImmediately(); 4135 } 4136 } 4137 return handled ? FINISH_HANDLED : FORWARD; 4138 } 4139 4140 private int processTrackballEvent(QueuedInputEvent q) { 4141 final MotionEvent event = (MotionEvent)q.mEvent; 4142 4143 if (mView.dispatchTrackballEvent(event)) { 4144 return FINISH_HANDLED; 4145 } 4146 return FORWARD; 4147 } 4148 4149 private int processGenericMotionEvent(QueuedInputEvent q) { 4150 final MotionEvent event = (MotionEvent)q.mEvent; 4151 4152 // Deliver the event to the view. 4153 if (mView.dispatchGenericMotionEvent(event)) { 4154 return FINISH_HANDLED; 4155 } 4156 return FORWARD; 4157 } 4158 } 4159 4160 /** 4161 * Performs synthesis of new input events from unhandled input events. 4162 */ 4163 final class SyntheticInputStage extends InputStage { 4164 private final SyntheticTrackballHandler mTrackball = new SyntheticTrackballHandler(); 4165 private final SyntheticJoystickHandler mJoystick = new SyntheticJoystickHandler(); 4166 private final SyntheticTouchNavigationHandler mTouchNavigation = 4167 new SyntheticTouchNavigationHandler(); 4168 private final SyntheticKeyboardHandler mKeyboard = new SyntheticKeyboardHandler(); 4169 4170 public SyntheticInputStage() { 4171 super(null); 4172 } 4173 4174 @Override 4175 protected int onProcess(QueuedInputEvent q) { 4176 q.mFlags |= QueuedInputEvent.FLAG_RESYNTHESIZED; 4177 if (q.mEvent instanceof MotionEvent) { 4178 final MotionEvent event = (MotionEvent)q.mEvent; 4179 final int source = event.getSource(); 4180 if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) { 4181 mTrackball.process(event); 4182 return FINISH_HANDLED; 4183 } else if ((source & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { 4184 mJoystick.process(event); 4185 return FINISH_HANDLED; 4186 } else if ((source & InputDevice.SOURCE_TOUCH_NAVIGATION) 4187 == InputDevice.SOURCE_TOUCH_NAVIGATION) { 4188 mTouchNavigation.process(event); 4189 return FINISH_HANDLED; 4190 } 4191 } else if ((q.mFlags & QueuedInputEvent.FLAG_UNHANDLED) != 0) { 4192 mKeyboard.process((KeyEvent)q.mEvent); 4193 return FINISH_HANDLED; 4194 } 4195 4196 return FORWARD; 4197 } 4198 4199 @Override 4200 protected void onDeliverToNext(QueuedInputEvent q) { 4201 if ((q.mFlags & QueuedInputEvent.FLAG_RESYNTHESIZED) == 0) { 4202 // Cancel related synthetic events if any prior stage has handled the event. 4203 if (q.mEvent instanceof MotionEvent) { 4204 final MotionEvent event = (MotionEvent)q.mEvent; 4205 final int source = event.getSource(); 4206 if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) { 4207 mTrackball.cancel(event); 4208 } else if ((source & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { 4209 mJoystick.cancel(event); 4210 } else if ((source & InputDevice.SOURCE_TOUCH_NAVIGATION) 4211 == InputDevice.SOURCE_TOUCH_NAVIGATION) { 4212 mTouchNavigation.cancel(event); 4213 } 4214 } 4215 } 4216 super.onDeliverToNext(q); 4217 } 4218 } 4219 4220 /** 4221 * Creates dpad events from unhandled trackball movements. 4222 */ 4223 final class SyntheticTrackballHandler { 4224 private final TrackballAxis mX = new TrackballAxis(); 4225 private final TrackballAxis mY = new TrackballAxis(); 4226 private long mLastTime; 4227 4228 public void process(MotionEvent event) { 4229 // Translate the trackball event into DPAD keys and try to deliver those. 4230 long curTime = SystemClock.uptimeMillis(); 4231 if ((mLastTime + MAX_TRACKBALL_DELAY) < curTime) { 4232 // It has been too long since the last movement, 4233 // so restart at the beginning. 4234 mX.reset(0); 4235 mY.reset(0); 4236 mLastTime = curTime; 4237 } 4238 4239 final int action = event.getAction(); 4240 final int metaState = event.getMetaState(); 4241 switch (action) { 4242 case MotionEvent.ACTION_DOWN: 4243 mX.reset(2); 4244 mY.reset(2); 4245 enqueueInputEvent(new KeyEvent(curTime, curTime, 4246 KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER, 0, metaState, 4247 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK, 4248 InputDevice.SOURCE_KEYBOARD)); 4249 break; 4250 case MotionEvent.ACTION_UP: 4251 mX.reset(2); 4252 mY.reset(2); 4253 enqueueInputEvent(new KeyEvent(curTime, curTime, 4254 KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER, 0, metaState, 4255 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK, 4256 InputDevice.SOURCE_KEYBOARD)); 4257 break; 4258 } 4259 4260 if (DEBUG_TRACKBALL) Log.v(TAG, "TB X=" + mX.position + " step=" 4261 + mX.step + " dir=" + mX.dir + " acc=" + mX.acceleration 4262 + " move=" + event.getX() 4263 + " / Y=" + mY.position + " step=" 4264 + mY.step + " dir=" + mY.dir + " acc=" + mY.acceleration 4265 + " move=" + event.getY()); 4266 final float xOff = mX.collect(event.getX(), event.getEventTime(), "X"); 4267 final float yOff = mY.collect(event.getY(), event.getEventTime(), "Y"); 4268 4269 // Generate DPAD events based on the trackball movement. 4270 // We pick the axis that has moved the most as the direction of 4271 // the DPAD. When we generate DPAD events for one axis, then the 4272 // other axis is reset -- we don't want to perform DPAD jumps due 4273 // to slight movements in the trackball when making major movements 4274 // along the other axis. 4275 int keycode = 0; 4276 int movement = 0; 4277 float accel = 1; 4278 if (xOff > yOff) { 4279 movement = mX.generate(); 4280 if (movement != 0) { 4281 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_RIGHT 4282 : KeyEvent.KEYCODE_DPAD_LEFT; 4283 accel = mX.acceleration; 4284 mY.reset(2); 4285 } 4286 } else if (yOff > 0) { 4287 movement = mY.generate(); 4288 if (movement != 0) { 4289 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_DOWN 4290 : KeyEvent.KEYCODE_DPAD_UP; 4291 accel = mY.acceleration; 4292 mX.reset(2); 4293 } 4294 } 4295 4296 if (keycode != 0) { 4297 if (movement < 0) movement = -movement; 4298 int accelMovement = (int)(movement * accel); 4299 if (DEBUG_TRACKBALL) Log.v(TAG, "Move: movement=" + movement 4300 + " accelMovement=" + accelMovement 4301 + " accel=" + accel); 4302 if (accelMovement > movement) { 4303 if (DEBUG_TRACKBALL) Log.v(TAG, "Delivering fake DPAD: " 4304 + keycode); 4305 movement--; 4306 int repeatCount = accelMovement - movement; 4307 enqueueInputEvent(new KeyEvent(curTime, curTime, 4308 KeyEvent.ACTION_MULTIPLE, keycode, repeatCount, metaState, 4309 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK, 4310 InputDevice.SOURCE_KEYBOARD)); 4311 } 4312 while (movement > 0) { 4313 if (DEBUG_TRACKBALL) Log.v(TAG, "Delivering fake DPAD: " 4314 + keycode); 4315 movement--; 4316 curTime = SystemClock.uptimeMillis(); 4317 enqueueInputEvent(new KeyEvent(curTime, curTime, 4318 KeyEvent.ACTION_DOWN, keycode, 0, metaState, 4319 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK, 4320 InputDevice.SOURCE_KEYBOARD)); 4321 enqueueInputEvent(new KeyEvent(curTime, curTime, 4322 KeyEvent.ACTION_UP, keycode, 0, metaState, 4323 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK, 4324 InputDevice.SOURCE_KEYBOARD)); 4325 } 4326 mLastTime = curTime; 4327 } 4328 } 4329 4330 public void cancel(MotionEvent event) { 4331 mLastTime = Integer.MIN_VALUE; 4332 4333 // If we reach this, we consumed a trackball event. 4334 // Because we will not translate the trackball event into a key event, 4335 // touch mode will not exit, so we exit touch mode here. 4336 if (mView != null && mAdded) { 4337 ensureTouchMode(false); 4338 } 4339 } 4340 } 4341 4342 /** 4343 * Maintains state information for a single trackball axis, generating 4344 * discrete (DPAD) movements based on raw trackball motion. 4345 */ 4346 static final class TrackballAxis { 4347 /** 4348 * The maximum amount of acceleration we will apply. 4349 */ 4350 static final float MAX_ACCELERATION = 20; 4351 4352 /** 4353 * The maximum amount of time (in milliseconds) between events in order 4354 * for us to consider the user to be doing fast trackball movements, 4355 * and thus apply an acceleration. 4356 */ 4357 static final long FAST_MOVE_TIME = 150; 4358 4359 /** 4360 * Scaling factor to the time (in milliseconds) between events to how 4361 * much to multiple/divide the current acceleration. When movement 4362 * is < FAST_MOVE_TIME this multiplies the acceleration; when > 4363 * FAST_MOVE_TIME it divides it. 4364 */ 4365 static final float ACCEL_MOVE_SCALING_FACTOR = (1.0f/40); 4366 4367 static final float FIRST_MOVEMENT_THRESHOLD = 0.5f; 4368 static final float SECOND_CUMULATIVE_MOVEMENT_THRESHOLD = 2.0f; 4369 static final float SUBSEQUENT_INCREMENTAL_MOVEMENT_THRESHOLD = 1.0f; 4370 4371 float position; 4372 float acceleration = 1; 4373 long lastMoveTime = 0; 4374 int step; 4375 int dir; 4376 int nonAccelMovement; 4377 4378 void reset(int _step) { 4379 position = 0; 4380 acceleration = 1; 4381 lastMoveTime = 0; 4382 step = _step; 4383 dir = 0; 4384 } 4385 4386 /** 4387 * Add trackball movement into the state. If the direction of movement 4388 * has been reversed, the state is reset before adding the 4389 * movement (so that you don't have to compensate for any previously 4390 * collected movement before see the result of the movement in the 4391 * new direction). 4392 * 4393 * @return Returns the absolute value of the amount of movement 4394 * collected so far. 4395 */ 4396 float collect(float off, long time, String axis) { 4397 long normTime; 4398 if (off > 0) { 4399 normTime = (long)(off * FAST_MOVE_TIME); 4400 if (dir < 0) { 4401 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to positive!"); 4402 position = 0; 4403 step = 0; 4404 acceleration = 1; 4405 lastMoveTime = 0; 4406 } 4407 dir = 1; 4408 } else if (off < 0) { 4409 normTime = (long)((-off) * FAST_MOVE_TIME); 4410 if (dir > 0) { 4411 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to negative!"); 4412 position = 0; 4413 step = 0; 4414 acceleration = 1; 4415 lastMoveTime = 0; 4416 } 4417 dir = -1; 4418 } else { 4419 normTime = 0; 4420 } 4421 4422 // The number of milliseconds between each movement that is 4423 // considered "normal" and will not result in any acceleration 4424 // or deceleration, scaled by the offset we have here. 4425 if (normTime > 0) { 4426 long delta = time - lastMoveTime; 4427 lastMoveTime = time; 4428 float acc = acceleration; 4429 if (delta < normTime) { 4430 // The user is scrolling rapidly, so increase acceleration. 4431 float scale = (normTime-delta) * ACCEL_MOVE_SCALING_FACTOR; 4432 if (scale > 1) acc *= scale; 4433 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " accelerate: off=" 4434 + off + " normTime=" + normTime + " delta=" + delta 4435 + " scale=" + scale + " acc=" + acc); 4436 acceleration = acc < MAX_ACCELERATION ? acc : MAX_ACCELERATION; 4437 } else { 4438 // The user is scrolling slowly, so decrease acceleration. 4439 float scale = (delta-normTime) * ACCEL_MOVE_SCALING_FACTOR; 4440 if (scale > 1) acc /= scale; 4441 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " deccelerate: off=" 4442 + off + " normTime=" + normTime + " delta=" + delta 4443 + " scale=" + scale + " acc=" + acc); 4444 acceleration = acc > 1 ? acc : 1; 4445 } 4446 } 4447 position += off; 4448 return Math.abs(position); 4449 } 4450 4451 /** 4452 * Generate the number of discrete movement events appropriate for 4453 * the currently collected trackball movement. 4454 * 4455 * @return Returns the number of discrete movements, either positive 4456 * or negative, or 0 if there is not enough trackball movement yet 4457 * for a discrete movement. 4458 */ 4459 int generate() { 4460 int movement = 0; 4461 nonAccelMovement = 0; 4462 do { 4463 final int dir = position >= 0 ? 1 : -1; 4464 switch (step) { 4465 // If we are going to execute the first step, then we want 4466 // to do this as soon as possible instead of waiting for 4467 // a full movement, in order to make things look responsive. 4468 case 0: 4469 if (Math.abs(position) < FIRST_MOVEMENT_THRESHOLD) { 4470 return movement; 4471 } 4472 movement += dir; 4473 nonAccelMovement += dir; 4474 step = 1; 4475 break; 4476 // If we have generated the first movement, then we need 4477 // to wait for the second complete trackball motion before 4478 // generating the second discrete movement. 4479 case 1: 4480 if (Math.abs(position) < SECOND_CUMULATIVE_MOVEMENT_THRESHOLD) { 4481 return movement; 4482 } 4483 movement += dir; 4484 nonAccelMovement += dir; 4485 position -= SECOND_CUMULATIVE_MOVEMENT_THRESHOLD * dir; 4486 step = 2; 4487 break; 4488 // After the first two, we generate discrete movements 4489 // consistently with the trackball, applying an acceleration 4490 // if the trackball is moving quickly. This is a simple 4491 // acceleration on top of what we already compute based 4492 // on how quickly the wheel is being turned, to apply 4493 // a longer increasing acceleration to continuous movement 4494 // in one direction. 4495 default: 4496 if (Math.abs(position) < SUBSEQUENT_INCREMENTAL_MOVEMENT_THRESHOLD) { 4497 return movement; 4498 } 4499 movement += dir; 4500 position -= dir * SUBSEQUENT_INCREMENTAL_MOVEMENT_THRESHOLD; 4501 float acc = acceleration; 4502 acc *= 1.1f; 4503 acceleration = acc < MAX_ACCELERATION ? acc : acceleration; 4504 break; 4505 } 4506 } while (true); 4507 } 4508 } 4509 4510 /** 4511 * Creates dpad events from unhandled joystick movements. 4512 */ 4513 final class SyntheticJoystickHandler extends Handler { 4514 private final static String TAG = "SyntheticJoystickHandler"; 4515 private final static int MSG_ENQUEUE_X_AXIS_KEY_REPEAT = 1; 4516 private final static int MSG_ENQUEUE_Y_AXIS_KEY_REPEAT = 2; 4517 4518 private int mLastXDirection; 4519 private int mLastYDirection; 4520 private int mLastXKeyCode; 4521 private int mLastYKeyCode; 4522 4523 public SyntheticJoystickHandler() { 4524 super(true); 4525 } 4526 4527 @Override 4528 public void handleMessage(Message msg) { 4529 switch (msg.what) { 4530 case MSG_ENQUEUE_X_AXIS_KEY_REPEAT: 4531 case MSG_ENQUEUE_Y_AXIS_KEY_REPEAT: { 4532 KeyEvent oldEvent = (KeyEvent)msg.obj; 4533 KeyEvent e = KeyEvent.changeTimeRepeat(oldEvent, 4534 SystemClock.uptimeMillis(), 4535 oldEvent.getRepeatCount() + 1); 4536 if (mAttachInfo.mHasWindowFocus) { 4537 enqueueInputEvent(e); 4538 Message m = obtainMessage(msg.what, e); 4539 m.setAsynchronous(true); 4540 sendMessageDelayed(m, ViewConfiguration.getKeyRepeatDelay()); 4541 } 4542 } break; 4543 } 4544 } 4545 4546 public void process(MotionEvent event) { 4547 switch(event.getActionMasked()) { 4548 case MotionEvent.ACTION_CANCEL: 4549 cancel(event); 4550 break; 4551 case MotionEvent.ACTION_MOVE: 4552 update(event, true); 4553 break; 4554 default: 4555 Log.w(TAG, "Unexpected action: " + event.getActionMasked()); 4556 } 4557 } 4558 4559 private void cancel(MotionEvent event) { 4560 removeMessages(MSG_ENQUEUE_X_AXIS_KEY_REPEAT); 4561 removeMessages(MSG_ENQUEUE_Y_AXIS_KEY_REPEAT); 4562 update(event, false); 4563 } 4564 4565 private void update(MotionEvent event, boolean synthesizeNewKeys) { 4566 final long time = event.getEventTime(); 4567 final int metaState = event.getMetaState(); 4568 final int deviceId = event.getDeviceId(); 4569 final int source = event.getSource(); 4570 4571 int xDirection = joystickAxisValueToDirection( 4572 event.getAxisValue(MotionEvent.AXIS_HAT_X)); 4573 if (xDirection == 0) { 4574 xDirection = joystickAxisValueToDirection(event.getX()); 4575 } 4576 4577 int yDirection = joystickAxisValueToDirection( 4578 event.getAxisValue(MotionEvent.AXIS_HAT_Y)); 4579 if (yDirection == 0) { 4580 yDirection = joystickAxisValueToDirection(event.getY()); 4581 } 4582 4583 if (xDirection != mLastXDirection) { 4584 if (mLastXKeyCode != 0) { 4585 removeMessages(MSG_ENQUEUE_X_AXIS_KEY_REPEAT); 4586 enqueueInputEvent(new KeyEvent(time, time, 4587 KeyEvent.ACTION_UP, mLastXKeyCode, 0, metaState, 4588 deviceId, 0, KeyEvent.FLAG_FALLBACK, source)); 4589 mLastXKeyCode = 0; 4590 } 4591 4592 mLastXDirection = xDirection; 4593 4594 if (xDirection != 0 && synthesizeNewKeys) { 4595 mLastXKeyCode = xDirection > 0 4596 ? KeyEvent.KEYCODE_DPAD_RIGHT : KeyEvent.KEYCODE_DPAD_LEFT; 4597 final KeyEvent e = new KeyEvent(time, time, 4598 KeyEvent.ACTION_DOWN, mLastXKeyCode, 0, metaState, 4599 deviceId, 0, KeyEvent.FLAG_FALLBACK, source); 4600 enqueueInputEvent(e); 4601 Message m = obtainMessage(MSG_ENQUEUE_X_AXIS_KEY_REPEAT, e); 4602 m.setAsynchronous(true); 4603 sendMessageDelayed(m, ViewConfiguration.getKeyRepeatTimeout()); 4604 } 4605 } 4606 4607 if (yDirection != mLastYDirection) { 4608 if (mLastYKeyCode != 0) { 4609 removeMessages(MSG_ENQUEUE_Y_AXIS_KEY_REPEAT); 4610 enqueueInputEvent(new KeyEvent(time, time, 4611 KeyEvent.ACTION_UP, mLastYKeyCode, 0, metaState, 4612 deviceId, 0, KeyEvent.FLAG_FALLBACK, source)); 4613 mLastYKeyCode = 0; 4614 } 4615 4616 mLastYDirection = yDirection; 4617 4618 if (yDirection != 0 && synthesizeNewKeys) { 4619 mLastYKeyCode = yDirection > 0 4620 ? KeyEvent.KEYCODE_DPAD_DOWN : KeyEvent.KEYCODE_DPAD_UP; 4621 final KeyEvent e = new KeyEvent(time, time, 4622 KeyEvent.ACTION_DOWN, mLastYKeyCode, 0, metaState, 4623 deviceId, 0, KeyEvent.FLAG_FALLBACK, source); 4624 enqueueInputEvent(e); 4625 Message m = obtainMessage(MSG_ENQUEUE_Y_AXIS_KEY_REPEAT, e); 4626 m.setAsynchronous(true); 4627 sendMessageDelayed(m, ViewConfiguration.getKeyRepeatTimeout()); 4628 } 4629 } 4630 } 4631 4632 private int joystickAxisValueToDirection(float value) { 4633 if (value >= 0.5f) { 4634 return 1; 4635 } else if (value <= -0.5f) { 4636 return -1; 4637 } else { 4638 return 0; 4639 } 4640 } 4641 } 4642 4643 /** 4644 * Creates dpad events from unhandled touch navigation movements. 4645 */ 4646 final class SyntheticTouchNavigationHandler extends Handler { 4647 private static final String LOCAL_TAG = "SyntheticTouchNavigationHandler"; 4648 private static final boolean LOCAL_DEBUG = false; 4649 4650 // Assumed nominal width and height in millimeters of a touch navigation pad, 4651 // if no resolution information is available from the input system. 4652 private static final float DEFAULT_WIDTH_MILLIMETERS = 48; 4653 private static final float DEFAULT_HEIGHT_MILLIMETERS = 48; 4654 4655 /* TODO: These constants should eventually be moved to ViewConfiguration. */ 4656 4657 // The nominal distance traveled to move by one unit. 4658 private static final int TICK_DISTANCE_MILLIMETERS = 12; 4659 4660 // Minimum and maximum fling velocity in ticks per second. 4661 // The minimum velocity should be set such that we perform enough ticks per 4662 // second that the fling appears to be fluid. For example, if we set the minimum 4663 // to 2 ticks per second, then there may be up to half a second delay between the next 4664 // to last and last ticks which is noticeably discrete and jerky. This value should 4665 // probably not be set to anything less than about 4. 4666 // If fling accuracy is a problem then consider tuning the tick distance instead. 4667 private static final float MIN_FLING_VELOCITY_TICKS_PER_SECOND = 6f; 4668 private static final float MAX_FLING_VELOCITY_TICKS_PER_SECOND = 20f; 4669 4670 // Fling velocity decay factor applied after each new key is emitted. 4671 // This parameter controls the deceleration and overall duration of the fling. 4672 // The fling stops automatically when its velocity drops below the minimum 4673 // fling velocity defined above. 4674 private static final float FLING_TICK_DECAY = 0.8f; 4675 4676 /* The input device that we are tracking. */ 4677 4678 private int mCurrentDeviceId = -1; 4679 private int mCurrentSource; 4680 private boolean mCurrentDeviceSupported; 4681 4682 /* Configuration for the current input device. */ 4683 4684 // The scaled tick distance. A movement of this amount should generally translate 4685 // into a single dpad event in a given direction. 4686 private float mConfigTickDistance; 4687 4688 // The minimum and maximum scaled fling velocity. 4689 private float mConfigMinFlingVelocity; 4690 private float mConfigMaxFlingVelocity; 4691 4692 /* Tracking state. */ 4693 4694 // The velocity tracker for detecting flings. 4695 private VelocityTracker mVelocityTracker; 4696 4697 // The active pointer id, or -1 if none. 4698 private int mActivePointerId = -1; 4699 4700 // Location where tracking started. 4701 private float mStartX; 4702 private float mStartY; 4703 4704 // Most recently observed position. 4705 private float mLastX; 4706 private float mLastY; 4707 4708 // Accumulated movement delta since the last direction key was sent. 4709 private float mAccumulatedX; 4710 private float mAccumulatedY; 4711 4712 // Set to true if any movement was delivered to the app. 4713 // Implies that tap slop was exceeded. 4714 private boolean mConsumedMovement; 4715 4716 // The most recently sent key down event. 4717 // The keycode remains set until the direction changes or a fling ends 4718 // so that repeated key events may be generated as required. 4719 private long mPendingKeyDownTime; 4720 private int mPendingKeyCode = KeyEvent.KEYCODE_UNKNOWN; 4721 private int mPendingKeyRepeatCount; 4722 private int mPendingKeyMetaState; 4723 4724 // The current fling velocity while a fling is in progress. 4725 private boolean mFlinging; 4726 private float mFlingVelocity; 4727 4728 public SyntheticTouchNavigationHandler() { 4729 super(true); 4730 } 4731 4732 public void process(MotionEvent event) { 4733 // Update the current device information. 4734 final long time = event.getEventTime(); 4735 final int deviceId = event.getDeviceId(); 4736 final int source = event.getSource(); 4737 if (mCurrentDeviceId != deviceId || mCurrentSource != source) { 4738 finishKeys(time); 4739 finishTracking(time); 4740 mCurrentDeviceId = deviceId; 4741 mCurrentSource = source; 4742 mCurrentDeviceSupported = false; 4743 InputDevice device = event.getDevice(); 4744 if (device != null) { 4745 // In order to support an input device, we must know certain 4746 // characteristics about it, such as its size and resolution. 4747 InputDevice.MotionRange xRange = device.getMotionRange(MotionEvent.AXIS_X); 4748 InputDevice.MotionRange yRange = device.getMotionRange(MotionEvent.AXIS_Y); 4749 if (xRange != null && yRange != null) { 4750 mCurrentDeviceSupported = true; 4751 4752 // Infer the resolution if it not actually known. 4753 float xRes = xRange.getResolution(); 4754 if (xRes <= 0) { 4755 xRes = xRange.getRange() / DEFAULT_WIDTH_MILLIMETERS; 4756 } 4757 float yRes = yRange.getResolution(); 4758 if (yRes <= 0) { 4759 yRes = yRange.getRange() / DEFAULT_HEIGHT_MILLIMETERS; 4760 } 4761 float nominalRes = (xRes + yRes) * 0.5f; 4762 4763 // Precompute all of the configuration thresholds we will need. 4764 mConfigTickDistance = TICK_DISTANCE_MILLIMETERS * nominalRes; 4765 mConfigMinFlingVelocity = 4766 MIN_FLING_VELOCITY_TICKS_PER_SECOND * mConfigTickDistance; 4767 mConfigMaxFlingVelocity = 4768 MAX_FLING_VELOCITY_TICKS_PER_SECOND * mConfigTickDistance; 4769 4770 if (LOCAL_DEBUG) { 4771 Log.d(LOCAL_TAG, "Configured device " + mCurrentDeviceId 4772 + " (" + Integer.toHexString(mCurrentSource) + "): " 4773 + ", mConfigTickDistance=" + mConfigTickDistance 4774 + ", mConfigMinFlingVelocity=" + mConfigMinFlingVelocity 4775 + ", mConfigMaxFlingVelocity=" + mConfigMaxFlingVelocity); 4776 } 4777 } 4778 } 4779 } 4780 if (!mCurrentDeviceSupported) { 4781 return; 4782 } 4783 4784 // Handle the event. 4785 final int action = event.getActionMasked(); 4786 switch (action) { 4787 case MotionEvent.ACTION_DOWN: { 4788 boolean caughtFling = mFlinging; 4789 finishKeys(time); 4790 finishTracking(time); 4791 mActivePointerId = event.getPointerId(0); 4792 mVelocityTracker = VelocityTracker.obtain(); 4793 mVelocityTracker.addMovement(event); 4794 mStartX = event.getX(); 4795 mStartY = event.getY(); 4796 mLastX = mStartX; 4797 mLastY = mStartY; 4798 mAccumulatedX = 0; 4799 mAccumulatedY = 0; 4800 4801 // If we caught a fling, then pretend that the tap slop has already 4802 // been exceeded to suppress taps whose only purpose is to stop the fling. 4803 mConsumedMovement = caughtFling; 4804 break; 4805 } 4806 4807 case MotionEvent.ACTION_MOVE: 4808 case MotionEvent.ACTION_UP: { 4809 if (mActivePointerId < 0) { 4810 break; 4811 } 4812 final int index = event.findPointerIndex(mActivePointerId); 4813 if (index < 0) { 4814 finishKeys(time); 4815 finishTracking(time); 4816 break; 4817 } 4818 4819 mVelocityTracker.addMovement(event); 4820 final float x = event.getX(index); 4821 final float y = event.getY(index); 4822 mAccumulatedX += x - mLastX; 4823 mAccumulatedY += y - mLastY; 4824 mLastX = x; 4825 mLastY = y; 4826 4827 // Consume any accumulated movement so far. 4828 final int metaState = event.getMetaState(); 4829 consumeAccumulatedMovement(time, metaState); 4830 4831 // Detect taps and flings. 4832 if (action == MotionEvent.ACTION_UP) { 4833 if (mConsumedMovement && mPendingKeyCode != KeyEvent.KEYCODE_UNKNOWN) { 4834 // It might be a fling. 4835 mVelocityTracker.computeCurrentVelocity(1000, mConfigMaxFlingVelocity); 4836 final float vx = mVelocityTracker.getXVelocity(mActivePointerId); 4837 final float vy = mVelocityTracker.getYVelocity(mActivePointerId); 4838 if (!startFling(time, vx, vy)) { 4839 finishKeys(time); 4840 } 4841 } 4842 finishTracking(time); 4843 } 4844 break; 4845 } 4846 4847 case MotionEvent.ACTION_CANCEL: { 4848 finishKeys(time); 4849 finishTracking(time); 4850 break; 4851 } 4852 } 4853 } 4854 4855 public void cancel(MotionEvent event) { 4856 if (mCurrentDeviceId == event.getDeviceId() 4857 && mCurrentSource == event.getSource()) { 4858 final long time = event.getEventTime(); 4859 finishKeys(time); 4860 finishTracking(time); 4861 } 4862 } 4863 4864 private void finishKeys(long time) { 4865 cancelFling(); 4866 sendKeyUp(time); 4867 } 4868 4869 private void finishTracking(long time) { 4870 if (mActivePointerId >= 0) { 4871 mActivePointerId = -1; 4872 mVelocityTracker.recycle(); 4873 mVelocityTracker = null; 4874 } 4875 } 4876 4877 private void consumeAccumulatedMovement(long time, int metaState) { 4878 final float absX = Math.abs(mAccumulatedX); 4879 final float absY = Math.abs(mAccumulatedY); 4880 if (absX >= absY) { 4881 if (absX >= mConfigTickDistance) { 4882 mAccumulatedX = consumeAccumulatedMovement(time, metaState, mAccumulatedX, 4883 KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_RIGHT); 4884 mAccumulatedY = 0; 4885 mConsumedMovement = true; 4886 } 4887 } else { 4888 if (absY >= mConfigTickDistance) { 4889 mAccumulatedY = consumeAccumulatedMovement(time, metaState, mAccumulatedY, 4890 KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_DOWN); 4891 mAccumulatedX = 0; 4892 mConsumedMovement = true; 4893 } 4894 } 4895 } 4896 4897 private float consumeAccumulatedMovement(long time, int metaState, 4898 float accumulator, int negativeKeyCode, int positiveKeyCode) { 4899 while (accumulator <= -mConfigTickDistance) { 4900 sendKeyDownOrRepeat(time, negativeKeyCode, metaState); 4901 accumulator += mConfigTickDistance; 4902 } 4903 while (accumulator >= mConfigTickDistance) { 4904 sendKeyDownOrRepeat(time, positiveKeyCode, metaState); 4905 accumulator -= mConfigTickDistance; 4906 } 4907 return accumulator; 4908 } 4909 4910 private void sendKeyDownOrRepeat(long time, int keyCode, int metaState) { 4911 if (mPendingKeyCode != keyCode) { 4912 sendKeyUp(time); 4913 mPendingKeyDownTime = time; 4914 mPendingKeyCode = keyCode; 4915 mPendingKeyRepeatCount = 0; 4916 } else { 4917 mPendingKeyRepeatCount += 1; 4918 } 4919 mPendingKeyMetaState = metaState; 4920 4921 // Note: Normally we would pass FLAG_LONG_PRESS when the repeat count is 1 4922 // but it doesn't quite make sense when simulating the events in this way. 4923 if (LOCAL_DEBUG) { 4924 Log.d(LOCAL_TAG, "Sending key down: keyCode=" + mPendingKeyCode 4925 + ", repeatCount=" + mPendingKeyRepeatCount 4926 + ", metaState=" + Integer.toHexString(mPendingKeyMetaState)); 4927 } 4928 enqueueInputEvent(new KeyEvent(mPendingKeyDownTime, time, 4929 KeyEvent.ACTION_DOWN, mPendingKeyCode, mPendingKeyRepeatCount, 4930 mPendingKeyMetaState, mCurrentDeviceId, 4931 KeyEvent.FLAG_FALLBACK, mCurrentSource)); 4932 } 4933 4934 private void sendKeyUp(long time) { 4935 if (mPendingKeyCode != KeyEvent.KEYCODE_UNKNOWN) { 4936 if (LOCAL_DEBUG) { 4937 Log.d(LOCAL_TAG, "Sending key up: keyCode=" + mPendingKeyCode 4938 + ", metaState=" + Integer.toHexString(mPendingKeyMetaState)); 4939 } 4940 enqueueInputEvent(new KeyEvent(mPendingKeyDownTime, time, 4941 KeyEvent.ACTION_UP, mPendingKeyCode, 0, mPendingKeyMetaState, 4942 mCurrentDeviceId, 0, KeyEvent.FLAG_FALLBACK, 4943 mCurrentSource)); 4944 mPendingKeyCode = KeyEvent.KEYCODE_UNKNOWN; 4945 } 4946 } 4947 4948 private boolean startFling(long time, float vx, float vy) { 4949 if (LOCAL_DEBUG) { 4950 Log.d(LOCAL_TAG, "Considering fling: vx=" + vx + ", vy=" + vy 4951 + ", min=" + mConfigMinFlingVelocity); 4952 } 4953 4954 // Flings must be oriented in the same direction as the preceding movements. 4955 switch (mPendingKeyCode) { 4956 case KeyEvent.KEYCODE_DPAD_LEFT: 4957 if (-vx >= mConfigMinFlingVelocity 4958 && Math.abs(vy) < mConfigMinFlingVelocity) { 4959 mFlingVelocity = -vx; 4960 break; 4961 } 4962 return false; 4963 4964 case KeyEvent.KEYCODE_DPAD_RIGHT: 4965 if (vx >= mConfigMinFlingVelocity 4966 && Math.abs(vy) < mConfigMinFlingVelocity) { 4967 mFlingVelocity = vx; 4968 break; 4969 } 4970 return false; 4971 4972 case KeyEvent.KEYCODE_DPAD_UP: 4973 if (-vy >= mConfigMinFlingVelocity 4974 && Math.abs(vx) < mConfigMinFlingVelocity) { 4975 mFlingVelocity = -vy; 4976 break; 4977 } 4978 return false; 4979 4980 case KeyEvent.KEYCODE_DPAD_DOWN: 4981 if (vy >= mConfigMinFlingVelocity 4982 && Math.abs(vx) < mConfigMinFlingVelocity) { 4983 mFlingVelocity = vy; 4984 break; 4985 } 4986 return false; 4987 } 4988 4989 // Post the first fling event. 4990 mFlinging = postFling(time); 4991 return mFlinging; 4992 } 4993 4994 private boolean postFling(long time) { 4995 // The idea here is to estimate the time when the pointer would have 4996 // traveled one tick distance unit given the current fling velocity. 4997 // This effect creates continuity of motion. 4998 if (mFlingVelocity >= mConfigMinFlingVelocity) { 4999 long delay = (long)(mConfigTickDistance / mFlingVelocity * 1000); 5000 postAtTime(mFlingRunnable, time + delay); 5001 if (LOCAL_DEBUG) { 5002 Log.d(LOCAL_TAG, "Posted fling: velocity=" 5003 + mFlingVelocity + ", delay=" + delay 5004 + ", keyCode=" + mPendingKeyCode); 5005 } 5006 return true; 5007 } 5008 return false; 5009 } 5010 5011 private void cancelFling() { 5012 if (mFlinging) { 5013 removeCallbacks(mFlingRunnable); 5014 mFlinging = false; 5015 } 5016 } 5017 5018 private final Runnable mFlingRunnable = new Runnable() { 5019 @Override 5020 public void run() { 5021 final long time = SystemClock.uptimeMillis(); 5022 sendKeyDownOrRepeat(time, mPendingKeyCode, mPendingKeyMetaState); 5023 mFlingVelocity *= FLING_TICK_DECAY; 5024 if (!postFling(time)) { 5025 mFlinging = false; 5026 finishKeys(time); 5027 } 5028 } 5029 }; 5030 } 5031 5032 final class SyntheticKeyboardHandler { 5033 public void process(KeyEvent event) { 5034 if ((event.getFlags() & KeyEvent.FLAG_FALLBACK) != 0) { 5035 return; 5036 } 5037 5038 final KeyCharacterMap kcm = event.getKeyCharacterMap(); 5039 final int keyCode = event.getKeyCode(); 5040 final int metaState = event.getMetaState(); 5041 5042 // Check for fallback actions specified by the key character map. 5043 KeyCharacterMap.FallbackAction fallbackAction = 5044 kcm.getFallbackAction(keyCode, metaState); 5045 if (fallbackAction != null) { 5046 final int flags = event.getFlags() | KeyEvent.FLAG_FALLBACK; 5047 KeyEvent fallbackEvent = KeyEvent.obtain( 5048 event.getDownTime(), event.getEventTime(), 5049 event.getAction(), fallbackAction.keyCode, 5050 event.getRepeatCount(), fallbackAction.metaState, 5051 event.getDeviceId(), event.getScanCode(), 5052 flags, event.getSource(), null); 5053 fallbackAction.recycle(); 5054 enqueueInputEvent(fallbackEvent); 5055 } 5056 } 5057 } 5058 5059 /** 5060 * Returns true if the key is used for keyboard navigation. 5061 * @param keyEvent The key event. 5062 * @return True if the key is used for keyboard navigation. 5063 */ 5064 private static boolean isNavigationKey(KeyEvent keyEvent) { 5065 switch (keyEvent.getKeyCode()) { 5066 case KeyEvent.KEYCODE_DPAD_LEFT: 5067 case KeyEvent.KEYCODE_DPAD_RIGHT: 5068 case KeyEvent.KEYCODE_DPAD_UP: 5069 case KeyEvent.KEYCODE_DPAD_DOWN: 5070 case KeyEvent.KEYCODE_DPAD_CENTER: 5071 case KeyEvent.KEYCODE_PAGE_UP: 5072 case KeyEvent.KEYCODE_PAGE_DOWN: 5073 case KeyEvent.KEYCODE_MOVE_HOME: 5074 case KeyEvent.KEYCODE_MOVE_END: 5075 case KeyEvent.KEYCODE_TAB: 5076 case KeyEvent.KEYCODE_SPACE: 5077 case KeyEvent.KEYCODE_ENTER: 5078 return true; 5079 } 5080 return false; 5081 } 5082 5083 /** 5084 * Returns true if the key is used for typing. 5085 * @param keyEvent The key event. 5086 * @return True if the key is used for typing. 5087 */ 5088 private static boolean isTypingKey(KeyEvent keyEvent) { 5089 return keyEvent.getUnicodeChar() > 0; 5090 } 5091 5092 /** 5093 * See if the key event means we should leave touch mode (and leave touch mode if so). 5094 * @param event The key event. 5095 * @return Whether this key event should be consumed (meaning the act of 5096 * leaving touch mode alone is considered the event). 5097 */ 5098 private boolean checkForLeavingTouchModeAndConsume(KeyEvent event) { 5099 // Only relevant in touch mode. 5100 if (!mAttachInfo.mInTouchMode) { 5101 return false; 5102 } 5103 5104 // Only consider leaving touch mode on DOWN or MULTIPLE actions, never on UP. 5105 final int action = event.getAction(); 5106 if (action != KeyEvent.ACTION_DOWN && action != KeyEvent.ACTION_MULTIPLE) { 5107 return false; 5108 } 5109 5110 // Don't leave touch mode if the IME told us not to. 5111 if ((event.getFlags() & KeyEvent.FLAG_KEEP_TOUCH_MODE) != 0) { 5112 return false; 5113 } 5114 5115 // If the key can be used for keyboard navigation then leave touch mode 5116 // and select a focused view if needed (in ensureTouchMode). 5117 // When a new focused view is selected, we consume the navigation key because 5118 // navigation doesn't make much sense unless a view already has focus so 5119 // the key's purpose is to set focus. 5120 if (isNavigationKey(event)) { 5121 return ensureTouchMode(false); 5122 } 5123 5124 // If the key can be used for typing then leave touch mode 5125 // and select a focused view if needed (in ensureTouchMode). 5126 // Always allow the view to process the typing key. 5127 if (isTypingKey(event)) { 5128 ensureTouchMode(false); 5129 return false; 5130 } 5131 5132 return false; 5133 } 5134 5135 /* drag/drop */ 5136 void setLocalDragState(Object obj) { 5137 mLocalDragState = obj; 5138 } 5139 5140 private void handleDragEvent(DragEvent event) { 5141 // From the root, only drag start/end/location are dispatched. entered/exited 5142 // are determined and dispatched by the viewgroup hierarchy, who then report 5143 // that back here for ultimate reporting back to the framework. 5144 if (mView != null && mAdded) { 5145 final int what = event.mAction; 5146 5147 if (what == DragEvent.ACTION_DRAG_EXITED) { 5148 // A direct EXITED event means that the window manager knows we've just crossed 5149 // a window boundary, so the current drag target within this one must have 5150 // just been exited. Send it the usual notifications and then we're done 5151 // for now. 5152 mView.dispatchDragEvent(event); 5153 } else { 5154 // Cache the drag description when the operation starts, then fill it in 5155 // on subsequent calls as a convenience 5156 if (what == DragEvent.ACTION_DRAG_STARTED) { 5157 mCurrentDragView = null; // Start the current-recipient tracking 5158 mDragDescription = event.mClipDescription; 5159 } else { 5160 event.mClipDescription = mDragDescription; 5161 } 5162 5163 // For events with a [screen] location, translate into window coordinates 5164 if ((what == DragEvent.ACTION_DRAG_LOCATION) || (what == DragEvent.ACTION_DROP)) { 5165 mDragPoint.set(event.mX, event.mY); 5166 if (mTranslator != null) { 5167 mTranslator.translatePointInScreenToAppWindow(mDragPoint); 5168 } 5169 5170 if (mCurScrollY != 0) { 5171 mDragPoint.offset(0, mCurScrollY); 5172 } 5173 5174 event.mX = mDragPoint.x; 5175 event.mY = mDragPoint.y; 5176 } 5177 5178 // Remember who the current drag target is pre-dispatch 5179 final View prevDragView = mCurrentDragView; 5180 5181 // Now dispatch the drag/drop event 5182 boolean result = mView.dispatchDragEvent(event); 5183 5184 // If we changed apparent drag target, tell the OS about it 5185 if (prevDragView != mCurrentDragView) { 5186 try { 5187 if (prevDragView != null) { 5188 mWindowSession.dragRecipientExited(mWindow); 5189 } 5190 if (mCurrentDragView != null) { 5191 mWindowSession.dragRecipientEntered(mWindow); 5192 } 5193 } catch (RemoteException e) { 5194 Slog.e(TAG, "Unable to note drag target change"); 5195 } 5196 } 5197 5198 // Report the drop result when we're done 5199 if (what == DragEvent.ACTION_DROP) { 5200 mDragDescription = null; 5201 try { 5202 Log.i(TAG, "Reporting drop result: " + result); 5203 mWindowSession.reportDropResult(mWindow, result); 5204 } catch (RemoteException e) { 5205 Log.e(TAG, "Unable to report drop result"); 5206 } 5207 } 5208 5209 // When the drag operation ends, release any local state object 5210 // that may have been in use 5211 if (what == DragEvent.ACTION_DRAG_ENDED) { 5212 setLocalDragState(null); 5213 } 5214 } 5215 } 5216 event.recycle(); 5217 } 5218 5219 public void handleDispatchSystemUiVisibilityChanged(SystemUiVisibilityInfo args) { 5220 if (mSeq != args.seq) { 5221 // The sequence has changed, so we need to update our value and make 5222 // sure to do a traversal afterward so the window manager is given our 5223 // most recent data. 5224 mSeq = args.seq; 5225 mAttachInfo.mForceReportNewAttributes = true; 5226 scheduleTraversals(); 5227 } 5228 if (mView == null) return; 5229 if (args.localChanges != 0) { 5230 mView.updateLocalSystemUiVisibility(args.localValue, args.localChanges); 5231 } 5232 5233 int visibility = args.globalVisibility&View.SYSTEM_UI_CLEARABLE_FLAGS; 5234 if (visibility != mAttachInfo.mGlobalSystemUiVisibility) { 5235 mAttachInfo.mGlobalSystemUiVisibility = visibility; 5236 mView.dispatchSystemUiVisibilityChanged(visibility); 5237 } 5238 } 5239 5240 public void handleDispatchWindowAnimationStarted(int remainingFrameCount) { 5241 if (!mDrawDuringWindowsAnimating && remainingFrameCount != -1) { 5242 mRemainingFrameCount = remainingFrameCount; 5243 mWindowsAnimating = true; 5244 } 5245 } 5246 5247 public void handleDispatchWindowAnimationStopped() { 5248 if (mWindowsAnimating) { 5249 mWindowsAnimating = false; 5250 if (!mDirty.isEmpty() || mIsAnimating || mFullRedrawNeeded) { 5251 scheduleTraversals(); 5252 } 5253 } 5254 } 5255 5256 public void handleDispatchWindowShown() { 5257 mAttachInfo.mTreeObserver.dispatchOnWindowShown(); 5258 } 5259 5260 public void getLastTouchPoint(Point outLocation) { 5261 outLocation.x = (int) mLastTouchPoint.x; 5262 outLocation.y = (int) mLastTouchPoint.y; 5263 } 5264 5265 public void setDragFocus(View newDragTarget) { 5266 if (mCurrentDragView != newDragTarget) { 5267 mCurrentDragView = newDragTarget; 5268 } 5269 } 5270 5271 private AudioManager getAudioManager() { 5272 if (mView == null) { 5273 throw new IllegalStateException("getAudioManager called when there is no mView"); 5274 } 5275 if (mAudioManager == null) { 5276 mAudioManager = (AudioManager) mView.getContext().getSystemService(Context.AUDIO_SERVICE); 5277 } 5278 return mAudioManager; 5279 } 5280 5281 public AccessibilityInteractionController getAccessibilityInteractionController() { 5282 if (mView == null) { 5283 throw new IllegalStateException("getAccessibilityInteractionController" 5284 + " called when there is no mView"); 5285 } 5286 if (mAccessibilityInteractionController == null) { 5287 mAccessibilityInteractionController = new AccessibilityInteractionController(this); 5288 } 5289 return mAccessibilityInteractionController; 5290 } 5291 5292 private int relayoutWindow(WindowManager.LayoutParams params, int viewVisibility, 5293 boolean insetsPending) throws RemoteException { 5294 5295 float appScale = mAttachInfo.mApplicationScale; 5296 boolean restore = false; 5297 if (params != null && mTranslator != null) { 5298 restore = true; 5299 params.backup(); 5300 mTranslator.translateWindowLayout(params); 5301 } 5302 if (params != null) { 5303 if (DBG) Log.d(TAG, "WindowLayout in layoutWindow:" + params); 5304 } 5305 mPendingConfiguration.seq = 0; 5306 //Log.d(TAG, ">>>>>> CALLING relayout"); 5307 if (params != null && mOrigWindowType != params.type) { 5308 // For compatibility with old apps, don't crash here. 5309 if (mTargetSdkVersion < Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 5310 Slog.w(TAG, "Window type can not be changed after " 5311 + "the window is added; ignoring change of " + mView); 5312 params.type = mOrigWindowType; 5313 } 5314 } 5315 int relayoutResult = mWindowSession.relayout( 5316 mWindow, mSeq, params, 5317 (int) (mView.getMeasuredWidth() * appScale + 0.5f), 5318 (int) (mView.getMeasuredHeight() * appScale + 0.5f), 5319 viewVisibility, insetsPending ? WindowManagerGlobal.RELAYOUT_INSETS_PENDING : 0, 5320 mWinFrame, mPendingOverscanInsets, mPendingContentInsets, mPendingVisibleInsets, 5321 mPendingStableInsets, mPendingOutsets, mPendingConfiguration, mSurface); 5322 //Log.d(TAG, "<<<<<< BACK FROM relayout"); 5323 if (restore) { 5324 params.restore(); 5325 } 5326 5327 if (mTranslator != null) { 5328 mTranslator.translateRectInScreenToAppWinFrame(mWinFrame); 5329 mTranslator.translateRectInScreenToAppWindow(mPendingOverscanInsets); 5330 mTranslator.translateRectInScreenToAppWindow(mPendingContentInsets); 5331 mTranslator.translateRectInScreenToAppWindow(mPendingVisibleInsets); 5332 mTranslator.translateRectInScreenToAppWindow(mPendingStableInsets); 5333 } 5334 return relayoutResult; 5335 } 5336 5337 /** 5338 * {@inheritDoc} 5339 */ 5340 @Override 5341 public void playSoundEffect(int effectId) { 5342 checkThread(); 5343 5344 try { 5345 final AudioManager audioManager = getAudioManager(); 5346 5347 switch (effectId) { 5348 case SoundEffectConstants.CLICK: 5349 audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK); 5350 return; 5351 case SoundEffectConstants.NAVIGATION_DOWN: 5352 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_DOWN); 5353 return; 5354 case SoundEffectConstants.NAVIGATION_LEFT: 5355 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_LEFT); 5356 return; 5357 case SoundEffectConstants.NAVIGATION_RIGHT: 5358 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_RIGHT); 5359 return; 5360 case SoundEffectConstants.NAVIGATION_UP: 5361 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_UP); 5362 return; 5363 default: 5364 throw new IllegalArgumentException("unknown effect id " + effectId + 5365 " not defined in " + SoundEffectConstants.class.getCanonicalName()); 5366 } 5367 } catch (IllegalStateException e) { 5368 // Exception thrown by getAudioManager() when mView is null 5369 Log.e(TAG, "FATAL EXCEPTION when attempting to play sound effect: " + e); 5370 e.printStackTrace(); 5371 } 5372 } 5373 5374 /** 5375 * {@inheritDoc} 5376 */ 5377 @Override 5378 public boolean performHapticFeedback(int effectId, boolean always) { 5379 try { 5380 return mWindowSession.performHapticFeedback(mWindow, effectId, always); 5381 } catch (RemoteException e) { 5382 return false; 5383 } 5384 } 5385 5386 /** 5387 * {@inheritDoc} 5388 */ 5389 @Override 5390 public View focusSearch(View focused, int direction) { 5391 checkThread(); 5392 if (!(mView instanceof ViewGroup)) { 5393 return null; 5394 } 5395 return FocusFinder.getInstance().findNextFocus((ViewGroup) mView, focused, direction); 5396 } 5397 5398 public void debug() { 5399 mView.debug(); 5400 } 5401 5402 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { 5403 String innerPrefix = prefix + " "; 5404 writer.print(prefix); writer.println("ViewRoot:"); 5405 writer.print(innerPrefix); writer.print("mAdded="); writer.print(mAdded); 5406 writer.print(" mRemoved="); writer.println(mRemoved); 5407 writer.print(innerPrefix); writer.print("mConsumeBatchedInputScheduled="); 5408 writer.println(mConsumeBatchedInputScheduled); 5409 writer.print(innerPrefix); writer.print("mConsumeBatchedInputImmediatelyScheduled="); 5410 writer.println(mConsumeBatchedInputImmediatelyScheduled); 5411 writer.print(innerPrefix); writer.print("mPendingInputEventCount="); 5412 writer.println(mPendingInputEventCount); 5413 writer.print(innerPrefix); writer.print("mProcessInputEventsScheduled="); 5414 writer.println(mProcessInputEventsScheduled); 5415 writer.print(innerPrefix); writer.print("mTraversalScheduled="); 5416 writer.print(mTraversalScheduled); 5417 if (mTraversalScheduled) { 5418 writer.print(" (barrier="); writer.print(mTraversalBarrier); writer.println(")"); 5419 } else { 5420 writer.println(); 5421 } 5422 mFirstInputStage.dump(innerPrefix, writer); 5423 5424 mChoreographer.dump(prefix, writer); 5425 5426 writer.print(prefix); writer.println("View Hierarchy:"); 5427 dumpViewHierarchy(innerPrefix, writer, mView); 5428 } 5429 5430 private void dumpViewHierarchy(String prefix, PrintWriter writer, View view) { 5431 writer.print(prefix); 5432 if (view == null) { 5433 writer.println("null"); 5434 return; 5435 } 5436 writer.println(view.toString()); 5437 if (!(view instanceof ViewGroup)) { 5438 return; 5439 } 5440 ViewGroup grp = (ViewGroup)view; 5441 final int N = grp.getChildCount(); 5442 if (N <= 0) { 5443 return; 5444 } 5445 prefix = prefix + " "; 5446 for (int i=0; i<N; i++) { 5447 dumpViewHierarchy(prefix, writer, grp.getChildAt(i)); 5448 } 5449 } 5450 5451 public void dumpGfxInfo(int[] info) { 5452 info[0] = info[1] = 0; 5453 if (mView != null) { 5454 getGfxInfo(mView, info); 5455 } 5456 } 5457 5458 private static void getGfxInfo(View view, int[] info) { 5459 RenderNode renderNode = view.mRenderNode; 5460 info[0]++; 5461 if (renderNode != null) { 5462 info[1] += renderNode.getDebugSize(); 5463 } 5464 5465 if (view instanceof ViewGroup) { 5466 ViewGroup group = (ViewGroup) view; 5467 5468 int count = group.getChildCount(); 5469 for (int i = 0; i < count; i++) { 5470 getGfxInfo(group.getChildAt(i), info); 5471 } 5472 } 5473 } 5474 5475 /** 5476 * @param immediate True, do now if not in traversal. False, put on queue and do later. 5477 * @return True, request has been queued. False, request has been completed. 5478 */ 5479 boolean die(boolean immediate) { 5480 // Make sure we do execute immediately if we are in the middle of a traversal or the damage 5481 // done by dispatchDetachedFromWindow will cause havoc on return. 5482 if (immediate && !mIsInTraversal) { 5483 doDie(); 5484 return false; 5485 } 5486 5487 if (!mIsDrawing) { 5488 destroyHardwareRenderer(); 5489 } else { 5490 Log.e(TAG, "Attempting to destroy the window while drawing!\n" + 5491 " window=" + this + ", title=" + mWindowAttributes.getTitle()); 5492 } 5493 mHandler.sendEmptyMessage(MSG_DIE); 5494 return true; 5495 } 5496 5497 void doDie() { 5498 checkThread(); 5499 if (LOCAL_LOGV) Log.v(TAG, "DIE in " + this + " of " + mSurface); 5500 synchronized (this) { 5501 if (mRemoved) { 5502 return; 5503 } 5504 mRemoved = true; 5505 if (mAdded) { 5506 dispatchDetachedFromWindow(); 5507 } 5508 5509 if (mAdded && !mFirst) { 5510 destroyHardwareRenderer(); 5511 5512 if (mView != null) { 5513 int viewVisibility = mView.getVisibility(); 5514 boolean viewVisibilityChanged = mViewVisibility != viewVisibility; 5515 if (mWindowAttributesChanged || viewVisibilityChanged) { 5516 // If layout params have been changed, first give them 5517 // to the window manager to make sure it has the correct 5518 // animation info. 5519 try { 5520 if ((relayoutWindow(mWindowAttributes, viewVisibility, false) 5521 & WindowManagerGlobal.RELAYOUT_RES_FIRST_TIME) != 0) { 5522 mWindowSession.finishDrawing(mWindow); 5523 } 5524 } catch (RemoteException e) { 5525 } 5526 } 5527 5528 mSurface.release(); 5529 } 5530 } 5531 5532 mAdded = false; 5533 } 5534 WindowManagerGlobal.getInstance().doRemoveView(this); 5535 } 5536 5537 public void requestUpdateConfiguration(Configuration config) { 5538 Message msg = mHandler.obtainMessage(MSG_UPDATE_CONFIGURATION, config); 5539 mHandler.sendMessage(msg); 5540 } 5541 5542 public void loadSystemProperties() { 5543 mHandler.post(new Runnable() { 5544 @Override 5545 public void run() { 5546 // Profiling 5547 mProfileRendering = SystemProperties.getBoolean(PROPERTY_PROFILE_RENDERING, false); 5548 profileRendering(mAttachInfo.mHasWindowFocus); 5549 5550 // Hardware rendering 5551 if (mAttachInfo.mHardwareRenderer != null) { 5552 if (mAttachInfo.mHardwareRenderer.loadSystemProperties()) { 5553 invalidate(); 5554 } 5555 } 5556 5557 // Layout debugging 5558 boolean layout = SystemProperties.getBoolean(View.DEBUG_LAYOUT_PROPERTY, false); 5559 if (layout != mAttachInfo.mDebugLayout) { 5560 mAttachInfo.mDebugLayout = layout; 5561 if (!mHandler.hasMessages(MSG_INVALIDATE_WORLD)) { 5562 mHandler.sendEmptyMessageDelayed(MSG_INVALIDATE_WORLD, 200); 5563 } 5564 } 5565 } 5566 }); 5567 } 5568 5569 private void destroyHardwareRenderer() { 5570 HardwareRenderer hardwareRenderer = mAttachInfo.mHardwareRenderer; 5571 5572 if (hardwareRenderer != null) { 5573 if (mView != null) { 5574 hardwareRenderer.destroyHardwareResources(mView); 5575 } 5576 hardwareRenderer.destroy(); 5577 hardwareRenderer.setRequested(false); 5578 5579 mAttachInfo.mHardwareRenderer = null; 5580 mAttachInfo.mHardwareAccelerated = false; 5581 } 5582 } 5583 5584 public void dispatchFinishInputConnection(InputConnection connection) { 5585 Message msg = mHandler.obtainMessage(MSG_FINISH_INPUT_CONNECTION, connection); 5586 mHandler.sendMessage(msg); 5587 } 5588 5589 public void dispatchResized(Rect frame, Rect overscanInsets, Rect contentInsets, 5590 Rect visibleInsets, Rect stableInsets, Rect outsets, boolean reportDraw, 5591 Configuration newConfig) { 5592 if (DEBUG_LAYOUT) Log.v(TAG, "Resizing " + this + ": frame=" + frame.toShortString() 5593 + " contentInsets=" + contentInsets.toShortString() 5594 + " visibleInsets=" + visibleInsets.toShortString() 5595 + " reportDraw=" + reportDraw); 5596 Message msg = mHandler.obtainMessage(reportDraw ? MSG_RESIZED_REPORT : MSG_RESIZED); 5597 if (mTranslator != null) { 5598 mTranslator.translateRectInScreenToAppWindow(frame); 5599 mTranslator.translateRectInScreenToAppWindow(overscanInsets); 5600 mTranslator.translateRectInScreenToAppWindow(contentInsets); 5601 mTranslator.translateRectInScreenToAppWindow(visibleInsets); 5602 } 5603 SomeArgs args = SomeArgs.obtain(); 5604 final boolean sameProcessCall = (Binder.getCallingPid() == android.os.Process.myPid()); 5605 args.arg1 = sameProcessCall ? new Rect(frame) : frame; 5606 args.arg2 = sameProcessCall ? new Rect(contentInsets) : contentInsets; 5607 args.arg3 = sameProcessCall ? new Rect(visibleInsets) : visibleInsets; 5608 args.arg4 = sameProcessCall && newConfig != null ? new Configuration(newConfig) : newConfig; 5609 args.arg5 = sameProcessCall ? new Rect(overscanInsets) : overscanInsets; 5610 args.arg6 = sameProcessCall ? new Rect(stableInsets) : stableInsets; 5611 args.arg7 = sameProcessCall ? new Rect(outsets) : outsets; 5612 msg.obj = args; 5613 mHandler.sendMessage(msg); 5614 } 5615 5616 public void dispatchMoved(int newX, int newY) { 5617 if (DEBUG_LAYOUT) Log.v(TAG, "Window moved " + this + ": newX=" + newX + " newY=" + newY); 5618 if (mTranslator != null) { 5619 PointF point = new PointF(newX, newY); 5620 mTranslator.translatePointInScreenToAppWindow(point); 5621 newX = (int) (point.x + 0.5); 5622 newY = (int) (point.y + 0.5); 5623 } 5624 Message msg = mHandler.obtainMessage(MSG_WINDOW_MOVED, newX, newY); 5625 mHandler.sendMessage(msg); 5626 } 5627 5628 /** 5629 * Represents a pending input event that is waiting in a queue. 5630 * 5631 * Input events are processed in serial order by the timestamp specified by 5632 * {@link InputEvent#getEventTimeNano()}. In general, the input dispatcher delivers 5633 * one input event to the application at a time and waits for the application 5634 * to finish handling it before delivering the next one. 5635 * 5636 * However, because the application or IME can synthesize and inject multiple 5637 * key events at a time without going through the input dispatcher, we end up 5638 * needing a queue on the application's side. 5639 */ 5640 private static final class QueuedInputEvent { 5641 public static final int FLAG_DELIVER_POST_IME = 1 << 0; 5642 public static final int FLAG_DEFERRED = 1 << 1; 5643 public static final int FLAG_FINISHED = 1 << 2; 5644 public static final int FLAG_FINISHED_HANDLED = 1 << 3; 5645 public static final int FLAG_RESYNTHESIZED = 1 << 4; 5646 public static final int FLAG_UNHANDLED = 1 << 5; 5647 5648 public QueuedInputEvent mNext; 5649 5650 public InputEvent mEvent; 5651 public InputEventReceiver mReceiver; 5652 public int mFlags; 5653 5654 public boolean shouldSkipIme() { 5655 if ((mFlags & FLAG_DELIVER_POST_IME) != 0) { 5656 return true; 5657 } 5658 return mEvent instanceof MotionEvent 5659 && mEvent.isFromSource(InputDevice.SOURCE_CLASS_POINTER); 5660 } 5661 5662 public boolean shouldSendToSynthesizer() { 5663 if ((mFlags & FLAG_UNHANDLED) != 0) { 5664 return true; 5665 } 5666 5667 return false; 5668 } 5669 5670 @Override 5671 public String toString() { 5672 StringBuilder sb = new StringBuilder("QueuedInputEvent{flags="); 5673 boolean hasPrevious = false; 5674 hasPrevious = flagToString("DELIVER_POST_IME", FLAG_DELIVER_POST_IME, hasPrevious, sb); 5675 hasPrevious = flagToString("DEFERRED", FLAG_DEFERRED, hasPrevious, sb); 5676 hasPrevious = flagToString("FINISHED", FLAG_FINISHED, hasPrevious, sb); 5677 hasPrevious = flagToString("FINISHED_HANDLED", FLAG_FINISHED_HANDLED, hasPrevious, sb); 5678 hasPrevious = flagToString("RESYNTHESIZED", FLAG_RESYNTHESIZED, hasPrevious, sb); 5679 hasPrevious = flagToString("UNHANDLED", FLAG_UNHANDLED, hasPrevious, sb); 5680 if (!hasPrevious) { 5681 sb.append("0"); 5682 } 5683 sb.append(", hasNextQueuedEvent=" + (mEvent != null ? "true" : "false")); 5684 sb.append(", hasInputEventReceiver=" + (mReceiver != null ? "true" : "false")); 5685 sb.append(", mEvent=" + mEvent + "}"); 5686 return sb.toString(); 5687 } 5688 5689 private boolean flagToString(String name, int flag, 5690 boolean hasPrevious, StringBuilder sb) { 5691 if ((mFlags & flag) != 0) { 5692 if (hasPrevious) { 5693 sb.append("|"); 5694 } 5695 sb.append(name); 5696 return true; 5697 } 5698 return hasPrevious; 5699 } 5700 } 5701 5702 private QueuedInputEvent obtainQueuedInputEvent(InputEvent event, 5703 InputEventReceiver receiver, int flags) { 5704 QueuedInputEvent q = mQueuedInputEventPool; 5705 if (q != null) { 5706 mQueuedInputEventPoolSize -= 1; 5707 mQueuedInputEventPool = q.mNext; 5708 q.mNext = null; 5709 } else { 5710 q = new QueuedInputEvent(); 5711 } 5712 5713 q.mEvent = event; 5714 q.mReceiver = receiver; 5715 q.mFlags = flags; 5716 return q; 5717 } 5718 5719 private void recycleQueuedInputEvent(QueuedInputEvent q) { 5720 q.mEvent = null; 5721 q.mReceiver = null; 5722 5723 if (mQueuedInputEventPoolSize < MAX_QUEUED_INPUT_EVENT_POOL_SIZE) { 5724 mQueuedInputEventPoolSize += 1; 5725 q.mNext = mQueuedInputEventPool; 5726 mQueuedInputEventPool = q; 5727 } 5728 } 5729 5730 void enqueueInputEvent(InputEvent event) { 5731 enqueueInputEvent(event, null, 0, false); 5732 } 5733 5734 void enqueueInputEvent(InputEvent event, 5735 InputEventReceiver receiver, int flags, boolean processImmediately) { 5736 adjustInputEventForCompatibility(event); 5737 QueuedInputEvent q = obtainQueuedInputEvent(event, receiver, flags); 5738 5739 // Always enqueue the input event in order, regardless of its time stamp. 5740 // We do this because the application or the IME may inject key events 5741 // in response to touch events and we want to ensure that the injected keys 5742 // are processed in the order they were received and we cannot trust that 5743 // the time stamp of injected events are monotonic. 5744 QueuedInputEvent last = mPendingInputEventTail; 5745 if (last == null) { 5746 mPendingInputEventHead = q; 5747 mPendingInputEventTail = q; 5748 } else { 5749 last.mNext = q; 5750 mPendingInputEventTail = q; 5751 } 5752 mPendingInputEventCount += 1; 5753 Trace.traceCounter(Trace.TRACE_TAG_INPUT, mPendingInputEventQueueLengthCounterName, 5754 mPendingInputEventCount); 5755 5756 if (processImmediately) { 5757 doProcessInputEvents(); 5758 } else { 5759 scheduleProcessInputEvents(); 5760 } 5761 } 5762 5763 private void scheduleProcessInputEvents() { 5764 if (!mProcessInputEventsScheduled) { 5765 mProcessInputEventsScheduled = true; 5766 Message msg = mHandler.obtainMessage(MSG_PROCESS_INPUT_EVENTS); 5767 msg.setAsynchronous(true); 5768 mHandler.sendMessage(msg); 5769 } 5770 } 5771 5772 void doProcessInputEvents() { 5773 // Deliver all pending input events in the queue. 5774 while (mPendingInputEventHead != null) { 5775 QueuedInputEvent q = mPendingInputEventHead; 5776 mPendingInputEventHead = q.mNext; 5777 if (mPendingInputEventHead == null) { 5778 mPendingInputEventTail = null; 5779 } 5780 q.mNext = null; 5781 5782 mPendingInputEventCount -= 1; 5783 Trace.traceCounter(Trace.TRACE_TAG_INPUT, mPendingInputEventQueueLengthCounterName, 5784 mPendingInputEventCount); 5785 5786 long eventTime = q.mEvent.getEventTimeNano(); 5787 long oldestEventTime = eventTime; 5788 if (q.mEvent instanceof MotionEvent) { 5789 MotionEvent me = (MotionEvent)q.mEvent; 5790 if (me.getHistorySize() > 0) { 5791 oldestEventTime = me.getHistoricalEventTimeNano(0); 5792 } 5793 } 5794 mChoreographer.mFrameInfo.updateInputEventTime(eventTime, oldestEventTime); 5795 5796 deliverInputEvent(q); 5797 } 5798 5799 // We are done processing all input events that we can process right now 5800 // so we can clear the pending flag immediately. 5801 if (mProcessInputEventsScheduled) { 5802 mProcessInputEventsScheduled = false; 5803 mHandler.removeMessages(MSG_PROCESS_INPUT_EVENTS); 5804 } 5805 } 5806 5807 private void deliverInputEvent(QueuedInputEvent q) { 5808 Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW, "deliverInputEvent", 5809 q.mEvent.getSequenceNumber()); 5810 if (mInputEventConsistencyVerifier != null) { 5811 mInputEventConsistencyVerifier.onInputEvent(q.mEvent, 0); 5812 } 5813 5814 InputStage stage; 5815 if (q.shouldSendToSynthesizer()) { 5816 stage = mSyntheticInputStage; 5817 } else { 5818 stage = q.shouldSkipIme() ? mFirstPostImeInputStage : mFirstInputStage; 5819 } 5820 5821 if (stage != null) { 5822 stage.deliver(q); 5823 } else { 5824 finishInputEvent(q); 5825 } 5826 } 5827 5828 private void finishInputEvent(QueuedInputEvent q) { 5829 Trace.asyncTraceEnd(Trace.TRACE_TAG_VIEW, "deliverInputEvent", 5830 q.mEvent.getSequenceNumber()); 5831 5832 if (q.mReceiver != null) { 5833 boolean handled = (q.mFlags & QueuedInputEvent.FLAG_FINISHED_HANDLED) != 0; 5834 q.mReceiver.finishInputEvent(q.mEvent, handled); 5835 } else { 5836 q.mEvent.recycleIfNeededAfterDispatch(); 5837 } 5838 5839 recycleQueuedInputEvent(q); 5840 } 5841 5842 private void adjustInputEventForCompatibility(InputEvent e) { 5843 if (mTargetSdkVersion < Build.VERSION_CODES.M && e instanceof MotionEvent) { 5844 MotionEvent motion = (MotionEvent) e; 5845 final int mask = 5846 MotionEvent.BUTTON_STYLUS_PRIMARY | MotionEvent.BUTTON_STYLUS_SECONDARY; 5847 final int buttonState = motion.getButtonState(); 5848 final int compatButtonState = (buttonState & mask) >> 4; 5849 if (compatButtonState != 0) { 5850 motion.setButtonState(buttonState | compatButtonState); 5851 } 5852 } 5853 } 5854 5855 static boolean isTerminalInputEvent(InputEvent event) { 5856 if (event instanceof KeyEvent) { 5857 final KeyEvent keyEvent = (KeyEvent)event; 5858 return keyEvent.getAction() == KeyEvent.ACTION_UP; 5859 } else { 5860 final MotionEvent motionEvent = (MotionEvent)event; 5861 final int action = motionEvent.getAction(); 5862 return action == MotionEvent.ACTION_UP 5863 || action == MotionEvent.ACTION_CANCEL 5864 || action == MotionEvent.ACTION_HOVER_EXIT; 5865 } 5866 } 5867 5868 void scheduleConsumeBatchedInput() { 5869 if (!mConsumeBatchedInputScheduled) { 5870 mConsumeBatchedInputScheduled = true; 5871 mChoreographer.postCallback(Choreographer.CALLBACK_INPUT, 5872 mConsumedBatchedInputRunnable, null); 5873 } 5874 } 5875 5876 void unscheduleConsumeBatchedInput() { 5877 if (mConsumeBatchedInputScheduled) { 5878 mConsumeBatchedInputScheduled = false; 5879 mChoreographer.removeCallbacks(Choreographer.CALLBACK_INPUT, 5880 mConsumedBatchedInputRunnable, null); 5881 } 5882 } 5883 5884 void scheduleConsumeBatchedInputImmediately() { 5885 if (!mConsumeBatchedInputImmediatelyScheduled) { 5886 unscheduleConsumeBatchedInput(); 5887 mConsumeBatchedInputImmediatelyScheduled = true; 5888 mHandler.post(mConsumeBatchedInputImmediatelyRunnable); 5889 } 5890 } 5891 5892 void doConsumeBatchedInput(long frameTimeNanos) { 5893 if (mConsumeBatchedInputScheduled) { 5894 mConsumeBatchedInputScheduled = false; 5895 if (mInputEventReceiver != null) { 5896 if (mInputEventReceiver.consumeBatchedInputEvents(frameTimeNanos) 5897 && frameTimeNanos != -1) { 5898 // If we consumed a batch here, we want to go ahead and schedule the 5899 // consumption of batched input events on the next frame. Otherwise, we would 5900 // wait until we have more input events pending and might get starved by other 5901 // things occurring in the process. If the frame time is -1, however, then 5902 // we're in a non-batching mode, so there's no need to schedule this. 5903 scheduleConsumeBatchedInput(); 5904 } 5905 } 5906 doProcessInputEvents(); 5907 } 5908 } 5909 5910 final class TraversalRunnable implements Runnable { 5911 @Override 5912 public void run() { 5913 doTraversal(); 5914 } 5915 } 5916 final TraversalRunnable mTraversalRunnable = new TraversalRunnable(); 5917 5918 final class WindowInputEventReceiver extends InputEventReceiver { 5919 public WindowInputEventReceiver(InputChannel inputChannel, Looper looper) { 5920 super(inputChannel, looper); 5921 } 5922 5923 @Override 5924 public void onInputEvent(InputEvent event) { 5925 enqueueInputEvent(event, this, 0, true); 5926 } 5927 5928 @Override 5929 public void onBatchedInputEventPending() { 5930 if (mUnbufferedInputDispatch) { 5931 super.onBatchedInputEventPending(); 5932 } else { 5933 scheduleConsumeBatchedInput(); 5934 } 5935 } 5936 5937 @Override 5938 public void dispose() { 5939 unscheduleConsumeBatchedInput(); 5940 super.dispose(); 5941 } 5942 } 5943 WindowInputEventReceiver mInputEventReceiver; 5944 5945 final class ConsumeBatchedInputRunnable implements Runnable { 5946 @Override 5947 public void run() { 5948 doConsumeBatchedInput(mChoreographer.getFrameTimeNanos()); 5949 } 5950 } 5951 final ConsumeBatchedInputRunnable mConsumedBatchedInputRunnable = 5952 new ConsumeBatchedInputRunnable(); 5953 boolean mConsumeBatchedInputScheduled; 5954 5955 final class ConsumeBatchedInputImmediatelyRunnable implements Runnable { 5956 @Override 5957 public void run() { 5958 doConsumeBatchedInput(-1); 5959 } 5960 } 5961 final ConsumeBatchedInputImmediatelyRunnable mConsumeBatchedInputImmediatelyRunnable = 5962 new ConsumeBatchedInputImmediatelyRunnable(); 5963 boolean mConsumeBatchedInputImmediatelyScheduled; 5964 5965 final class InvalidateOnAnimationRunnable implements Runnable { 5966 private boolean mPosted; 5967 private final ArrayList<View> mViews = new ArrayList<View>(); 5968 private final ArrayList<AttachInfo.InvalidateInfo> mViewRects = 5969 new ArrayList<AttachInfo.InvalidateInfo>(); 5970 private View[] mTempViews; 5971 private AttachInfo.InvalidateInfo[] mTempViewRects; 5972 5973 public void addView(View view) { 5974 synchronized (this) { 5975 mViews.add(view); 5976 postIfNeededLocked(); 5977 } 5978 } 5979 5980 public void addViewRect(AttachInfo.InvalidateInfo info) { 5981 synchronized (this) { 5982 mViewRects.add(info); 5983 postIfNeededLocked(); 5984 } 5985 } 5986 5987 public void removeView(View view) { 5988 synchronized (this) { 5989 mViews.remove(view); 5990 5991 for (int i = mViewRects.size(); i-- > 0; ) { 5992 AttachInfo.InvalidateInfo info = mViewRects.get(i); 5993 if (info.target == view) { 5994 mViewRects.remove(i); 5995 info.recycle(); 5996 } 5997 } 5998 5999 if (mPosted && mViews.isEmpty() && mViewRects.isEmpty()) { 6000 mChoreographer.removeCallbacks(Choreographer.CALLBACK_ANIMATION, this, null); 6001 mPosted = false; 6002 } 6003 } 6004 } 6005 6006 @Override 6007 public void run() { 6008 final int viewCount; 6009 final int viewRectCount; 6010 synchronized (this) { 6011 mPosted = false; 6012 6013 viewCount = mViews.size(); 6014 if (viewCount != 0) { 6015 mTempViews = mViews.toArray(mTempViews != null 6016 ? mTempViews : new View[viewCount]); 6017 mViews.clear(); 6018 } 6019 6020 viewRectCount = mViewRects.size(); 6021 if (viewRectCount != 0) { 6022 mTempViewRects = mViewRects.toArray(mTempViewRects != null 6023 ? mTempViewRects : new AttachInfo.InvalidateInfo[viewRectCount]); 6024 mViewRects.clear(); 6025 } 6026 } 6027 6028 for (int i = 0; i < viewCount; i++) { 6029 mTempViews[i].invalidate(); 6030 mTempViews[i] = null; 6031 } 6032 6033 for (int i = 0; i < viewRectCount; i++) { 6034 final View.AttachInfo.InvalidateInfo info = mTempViewRects[i]; 6035 info.target.invalidate(info.left, info.top, info.right, info.bottom); 6036 info.recycle(); 6037 } 6038 } 6039 6040 private void postIfNeededLocked() { 6041 if (!mPosted) { 6042 mChoreographer.postCallback(Choreographer.CALLBACK_ANIMATION, this, null); 6043 mPosted = true; 6044 } 6045 } 6046 } 6047 final InvalidateOnAnimationRunnable mInvalidateOnAnimationRunnable = 6048 new InvalidateOnAnimationRunnable(); 6049 6050 public void dispatchInvalidateDelayed(View view, long delayMilliseconds) { 6051 Message msg = mHandler.obtainMessage(MSG_INVALIDATE, view); 6052 mHandler.sendMessageDelayed(msg, delayMilliseconds); 6053 } 6054 6055 public void dispatchInvalidateRectDelayed(AttachInfo.InvalidateInfo info, 6056 long delayMilliseconds) { 6057 final Message msg = mHandler.obtainMessage(MSG_INVALIDATE_RECT, info); 6058 mHandler.sendMessageDelayed(msg, delayMilliseconds); 6059 } 6060 6061 public void dispatchInvalidateOnAnimation(View view) { 6062 mInvalidateOnAnimationRunnable.addView(view); 6063 } 6064 6065 public void dispatchInvalidateRectOnAnimation(AttachInfo.InvalidateInfo info) { 6066 mInvalidateOnAnimationRunnable.addViewRect(info); 6067 } 6068 6069 public void cancelInvalidate(View view) { 6070 mHandler.removeMessages(MSG_INVALIDATE, view); 6071 // fixme: might leak the AttachInfo.InvalidateInfo objects instead of returning 6072 // them to the pool 6073 mHandler.removeMessages(MSG_INVALIDATE_RECT, view); 6074 mInvalidateOnAnimationRunnable.removeView(view); 6075 } 6076 6077 public void dispatchInputEvent(InputEvent event) { 6078 dispatchInputEvent(event, null); 6079 } 6080 6081 public void dispatchInputEvent(InputEvent event, InputEventReceiver receiver) { 6082 SomeArgs args = SomeArgs.obtain(); 6083 args.arg1 = event; 6084 args.arg2 = receiver; 6085 Message msg = mHandler.obtainMessage(MSG_DISPATCH_INPUT_EVENT, args); 6086 msg.setAsynchronous(true); 6087 mHandler.sendMessage(msg); 6088 } 6089 6090 public void synthesizeInputEvent(InputEvent event) { 6091 Message msg = mHandler.obtainMessage(MSG_SYNTHESIZE_INPUT_EVENT, event); 6092 msg.setAsynchronous(true); 6093 mHandler.sendMessage(msg); 6094 } 6095 6096 public void dispatchKeyFromIme(KeyEvent event) { 6097 Message msg = mHandler.obtainMessage(MSG_DISPATCH_KEY_FROM_IME, event); 6098 msg.setAsynchronous(true); 6099 mHandler.sendMessage(msg); 6100 } 6101 6102 /** 6103 * Reinject unhandled {@link InputEvent}s in order to synthesize fallbacks events. 6104 * 6105 * Note that it is the responsibility of the caller of this API to recycle the InputEvent it 6106 * passes in. 6107 */ 6108 public void dispatchUnhandledInputEvent(InputEvent event) { 6109 if (event instanceof MotionEvent) { 6110 event = MotionEvent.obtain((MotionEvent) event); 6111 } 6112 synthesizeInputEvent(event); 6113 } 6114 6115 public void dispatchAppVisibility(boolean visible) { 6116 Message msg = mHandler.obtainMessage(MSG_DISPATCH_APP_VISIBILITY); 6117 msg.arg1 = visible ? 1 : 0; 6118 mHandler.sendMessage(msg); 6119 } 6120 6121 public void dispatchGetNewSurface() { 6122 Message msg = mHandler.obtainMessage(MSG_DISPATCH_GET_NEW_SURFACE); 6123 mHandler.sendMessage(msg); 6124 } 6125 6126 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) { 6127 Message msg = Message.obtain(); 6128 msg.what = MSG_WINDOW_FOCUS_CHANGED; 6129 msg.arg1 = hasFocus ? 1 : 0; 6130 msg.arg2 = inTouchMode ? 1 : 0; 6131 mHandler.sendMessage(msg); 6132 } 6133 6134 public void dispatchWindowShown() { 6135 mHandler.sendEmptyMessage(MSG_DISPATCH_WINDOW_SHOWN); 6136 } 6137 6138 public void dispatchCloseSystemDialogs(String reason) { 6139 Message msg = Message.obtain(); 6140 msg.what = MSG_CLOSE_SYSTEM_DIALOGS; 6141 msg.obj = reason; 6142 mHandler.sendMessage(msg); 6143 } 6144 6145 public void dispatchDragEvent(DragEvent event) { 6146 final int what; 6147 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) { 6148 what = MSG_DISPATCH_DRAG_LOCATION_EVENT; 6149 mHandler.removeMessages(what); 6150 } else { 6151 what = MSG_DISPATCH_DRAG_EVENT; 6152 } 6153 Message msg = mHandler.obtainMessage(what, event); 6154 mHandler.sendMessage(msg); 6155 } 6156 6157 public void dispatchSystemUiVisibilityChanged(int seq, int globalVisibility, 6158 int localValue, int localChanges) { 6159 SystemUiVisibilityInfo args = new SystemUiVisibilityInfo(); 6160 args.seq = seq; 6161 args.globalVisibility = globalVisibility; 6162 args.localValue = localValue; 6163 args.localChanges = localChanges; 6164 mHandler.sendMessage(mHandler.obtainMessage(MSG_DISPATCH_SYSTEM_UI_VISIBILITY, args)); 6165 } 6166 6167 public void dispatchWindowAnimationStarted(int remainingFrameCount) { 6168 mHandler.obtainMessage(MSG_DISPATCH_WINDOW_ANIMATION_STARTED, 6169 remainingFrameCount, 0 /* unused */).sendToTarget(); 6170 } 6171 6172 public void dispatchWindowAnimationStopped() { 6173 mHandler.sendEmptyMessage(MSG_DISPATCH_WINDOW_ANIMATION_STOPPED); 6174 } 6175 6176 public void dispatchCheckFocus() { 6177 if (!mHandler.hasMessages(MSG_CHECK_FOCUS)) { 6178 // This will result in a call to checkFocus() below. 6179 mHandler.sendEmptyMessage(MSG_CHECK_FOCUS); 6180 } 6181 } 6182 6183 /** 6184 * Post a callback to send a 6185 * {@link AccessibilityEvent#TYPE_WINDOW_CONTENT_CHANGED} event. 6186 * This event is send at most once every 6187 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}. 6188 */ 6189 private void postSendWindowContentChangedCallback(View source, int changeType) { 6190 if (mSendWindowContentChangedAccessibilityEvent == null) { 6191 mSendWindowContentChangedAccessibilityEvent = 6192 new SendWindowContentChangedAccessibilityEvent(); 6193 } 6194 mSendWindowContentChangedAccessibilityEvent.runOrPost(source, changeType); 6195 } 6196 6197 /** 6198 * Remove a posted callback to send a 6199 * {@link AccessibilityEvent#TYPE_WINDOW_CONTENT_CHANGED} event. 6200 */ 6201 private void removeSendWindowContentChangedCallback() { 6202 if (mSendWindowContentChangedAccessibilityEvent != null) { 6203 mHandler.removeCallbacks(mSendWindowContentChangedAccessibilityEvent); 6204 } 6205 } 6206 6207 @Override 6208 public boolean showContextMenuForChild(View originalView) { 6209 return false; 6210 } 6211 6212 @Override 6213 public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) { 6214 return null; 6215 } 6216 6217 @Override 6218 public ActionMode startActionModeForChild( 6219 View originalView, ActionMode.Callback callback, int type) { 6220 return null; 6221 } 6222 6223 @Override 6224 public void createContextMenu(ContextMenu menu) { 6225 } 6226 6227 @Override 6228 public void childDrawableStateChanged(View child) { 6229 } 6230 6231 @Override 6232 public boolean requestSendAccessibilityEvent(View child, AccessibilityEvent event) { 6233 if (mView == null || mStopped || mPausedForTransition) { 6234 return false; 6235 } 6236 // Intercept accessibility focus events fired by virtual nodes to keep 6237 // track of accessibility focus position in such nodes. 6238 final int eventType = event.getEventType(); 6239 switch (eventType) { 6240 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: { 6241 final long sourceNodeId = event.getSourceNodeId(); 6242 final int accessibilityViewId = AccessibilityNodeInfo.getAccessibilityViewId( 6243 sourceNodeId); 6244 View source = mView.findViewByAccessibilityId(accessibilityViewId); 6245 if (source != null) { 6246 AccessibilityNodeProvider provider = source.getAccessibilityNodeProvider(); 6247 if (provider != null) { 6248 final int virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId( 6249 sourceNodeId); 6250 final AccessibilityNodeInfo node; 6251 if (virtualNodeId == AccessibilityNodeInfo.UNDEFINED_ITEM_ID) { 6252 node = provider.createAccessibilityNodeInfo( 6253 AccessibilityNodeProvider.HOST_VIEW_ID); 6254 } else { 6255 node = provider.createAccessibilityNodeInfo(virtualNodeId); 6256 } 6257 setAccessibilityFocus(source, node); 6258 } 6259 } 6260 } break; 6261 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: { 6262 final long sourceNodeId = event.getSourceNodeId(); 6263 final int accessibilityViewId = AccessibilityNodeInfo.getAccessibilityViewId( 6264 sourceNodeId); 6265 View source = mView.findViewByAccessibilityId(accessibilityViewId); 6266 if (source != null) { 6267 AccessibilityNodeProvider provider = source.getAccessibilityNodeProvider(); 6268 if (provider != null) { 6269 setAccessibilityFocus(null, null); 6270 } 6271 } 6272 } break; 6273 6274 6275 case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED: { 6276 handleWindowContentChangedEvent(event); 6277 } break; 6278 } 6279 mAccessibilityManager.sendAccessibilityEvent(event); 6280 return true; 6281 } 6282 6283 /** 6284 * Updates the focused virtual view, when necessary, in response to a 6285 * content changed event. 6286 * <p> 6287 * This is necessary to get updated bounds after a position change. 6288 * 6289 * @param event an accessibility event of type 6290 * {@link AccessibilityEvent#TYPE_WINDOW_CONTENT_CHANGED} 6291 */ 6292 private void handleWindowContentChangedEvent(AccessibilityEvent event) { 6293 final View focusedHost = mAccessibilityFocusedHost; 6294 if (focusedHost == null || mAccessibilityFocusedVirtualView == null) { 6295 // No virtual view focused, nothing to do here. 6296 return; 6297 } 6298 6299 final AccessibilityNodeProvider provider = focusedHost.getAccessibilityNodeProvider(); 6300 if (provider == null) { 6301 // Error state: virtual view with no provider. Clear focus. 6302 mAccessibilityFocusedHost = null; 6303 mAccessibilityFocusedVirtualView = null; 6304 focusedHost.clearAccessibilityFocusNoCallbacks(); 6305 return; 6306 } 6307 6308 // We only care about change types that may affect the bounds of the 6309 // focused virtual view. 6310 final int changes = event.getContentChangeTypes(); 6311 if ((changes & AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE) == 0 6312 && changes != AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED) { 6313 return; 6314 } 6315 6316 final long eventSourceNodeId = event.getSourceNodeId(); 6317 final int changedViewId = AccessibilityNodeInfo.getAccessibilityViewId(eventSourceNodeId); 6318 6319 // Search up the tree for subtree containment. 6320 boolean hostInSubtree = false; 6321 View root = mAccessibilityFocusedHost; 6322 while (root != null && !hostInSubtree) { 6323 if (changedViewId == root.getAccessibilityViewId()) { 6324 hostInSubtree = true; 6325 } else { 6326 final ViewParent parent = root.getParent(); 6327 if (parent instanceof View) { 6328 root = (View) parent; 6329 } else { 6330 root = null; 6331 } 6332 } 6333 } 6334 6335 // We care only about changes in subtrees containing the host view. 6336 if (!hostInSubtree) { 6337 return; 6338 } 6339 6340 final long focusedSourceNodeId = mAccessibilityFocusedVirtualView.getSourceNodeId(); 6341 int focusedChildId = AccessibilityNodeInfo.getVirtualDescendantId(focusedSourceNodeId); 6342 if (focusedChildId == AccessibilityNodeInfo.UNDEFINED_ITEM_ID) { 6343 // TODO: Should we clear the focused virtual view? 6344 focusedChildId = AccessibilityNodeProvider.HOST_VIEW_ID; 6345 } 6346 6347 // Refresh the node for the focused virtual view. 6348 final Rect oldBounds = mTempRect; 6349 mAccessibilityFocusedVirtualView.getBoundsInScreen(oldBounds); 6350 mAccessibilityFocusedVirtualView = provider.createAccessibilityNodeInfo(focusedChildId); 6351 if (mAccessibilityFocusedVirtualView == null) { 6352 // Error state: The node no longer exists. Clear focus. 6353 mAccessibilityFocusedHost = null; 6354 focusedHost.clearAccessibilityFocusNoCallbacks(); 6355 6356 // This will probably fail, but try to keep the provider's internal 6357 // state consistent by clearing focus. 6358 provider.performAction(focusedChildId, 6359 AccessibilityAction.ACTION_CLEAR_ACCESSIBILITY_FOCUS.getId(), null); 6360 invalidateRectOnScreen(oldBounds); 6361 } else { 6362 // The node was refreshed, invalidate bounds if necessary. 6363 final Rect newBounds = mAccessibilityFocusedVirtualView.getBoundsInScreen(); 6364 if (!oldBounds.equals(newBounds)) { 6365 oldBounds.union(newBounds); 6366 invalidateRectOnScreen(oldBounds); 6367 } 6368 } 6369 } 6370 6371 @Override 6372 public void notifySubtreeAccessibilityStateChanged(View child, View source, int changeType) { 6373 postSendWindowContentChangedCallback(source, changeType); 6374 } 6375 6376 @Override 6377 public boolean canResolveLayoutDirection() { 6378 return true; 6379 } 6380 6381 @Override 6382 public boolean isLayoutDirectionResolved() { 6383 return true; 6384 } 6385 6386 @Override 6387 public int getLayoutDirection() { 6388 return View.LAYOUT_DIRECTION_RESOLVED_DEFAULT; 6389 } 6390 6391 @Override 6392 public boolean canResolveTextDirection() { 6393 return true; 6394 } 6395 6396 @Override 6397 public boolean isTextDirectionResolved() { 6398 return true; 6399 } 6400 6401 @Override 6402 public int getTextDirection() { 6403 return View.TEXT_DIRECTION_RESOLVED_DEFAULT; 6404 } 6405 6406 @Override 6407 public boolean canResolveTextAlignment() { 6408 return true; 6409 } 6410 6411 @Override 6412 public boolean isTextAlignmentResolved() { 6413 return true; 6414 } 6415 6416 @Override 6417 public int getTextAlignment() { 6418 return View.TEXT_ALIGNMENT_RESOLVED_DEFAULT; 6419 } 6420 6421 private View getCommonPredecessor(View first, View second) { 6422 if (mTempHashSet == null) { 6423 mTempHashSet = new HashSet<View>(); 6424 } 6425 HashSet<View> seen = mTempHashSet; 6426 seen.clear(); 6427 View firstCurrent = first; 6428 while (firstCurrent != null) { 6429 seen.add(firstCurrent); 6430 ViewParent firstCurrentParent = firstCurrent.mParent; 6431 if (firstCurrentParent instanceof View) { 6432 firstCurrent = (View) firstCurrentParent; 6433 } else { 6434 firstCurrent = null; 6435 } 6436 } 6437 View secondCurrent = second; 6438 while (secondCurrent != null) { 6439 if (seen.contains(secondCurrent)) { 6440 seen.clear(); 6441 return secondCurrent; 6442 } 6443 ViewParent secondCurrentParent = secondCurrent.mParent; 6444 if (secondCurrentParent instanceof View) { 6445 secondCurrent = (View) secondCurrentParent; 6446 } else { 6447 secondCurrent = null; 6448 } 6449 } 6450 seen.clear(); 6451 return null; 6452 } 6453 6454 void checkThread() { 6455 if (mThread != Thread.currentThread()) { 6456 throw new CalledFromWrongThreadException( 6457 "Only the original thread that created a view hierarchy can touch its views."); 6458 } 6459 } 6460 6461 @Override 6462 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { 6463 // ViewAncestor never intercepts touch event, so this can be a no-op 6464 } 6465 6466 @Override 6467 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { 6468 final boolean scrolled = scrollToRectOrFocus(rectangle, immediate); 6469 if (rectangle != null) { 6470 mTempRect.set(rectangle); 6471 mTempRect.offset(0, -mCurScrollY); 6472 mTempRect.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop); 6473 try { 6474 mWindowSession.onRectangleOnScreenRequested(mWindow, mTempRect); 6475 } catch (RemoteException re) { 6476 /* ignore */ 6477 } 6478 } 6479 return scrolled; 6480 } 6481 6482 @Override 6483 public void childHasTransientStateChanged(View child, boolean hasTransientState) { 6484 // Do nothing. 6485 } 6486 6487 @Override 6488 public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) { 6489 return false; 6490 } 6491 6492 @Override 6493 public void onStopNestedScroll(View target) { 6494 } 6495 6496 @Override 6497 public void onNestedScrollAccepted(View child, View target, int nestedScrollAxes) { 6498 } 6499 6500 @Override 6501 public void onNestedScroll(View target, int dxConsumed, int dyConsumed, 6502 int dxUnconsumed, int dyUnconsumed) { 6503 } 6504 6505 @Override 6506 public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) { 6507 } 6508 6509 @Override 6510 public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) { 6511 return false; 6512 } 6513 6514 @Override 6515 public boolean onNestedPreFling(View target, float velocityX, float velocityY) { 6516 return false; 6517 } 6518 6519 @Override 6520 public boolean onNestedPrePerformAccessibilityAction(View target, int action, Bundle args) { 6521 return false; 6522 } 6523 6524 /** 6525 * Force the window to report its next draw. 6526 * <p> 6527 * This method is only supposed to be used to speed up the interaction from SystemUI and window 6528 * manager when waiting for the first frame to be drawn when turning on the screen. DO NOT USE 6529 * unless you fully understand this interaction. 6530 * @hide 6531 */ 6532 public void setReportNextDraw() { 6533 mReportNextDraw = true; 6534 invalidate(); 6535 } 6536 6537 void changeCanvasOpacity(boolean opaque) { 6538 Log.d(TAG, "changeCanvasOpacity: opaque=" + opaque); 6539 if (mAttachInfo.mHardwareRenderer != null) { 6540 mAttachInfo.mHardwareRenderer.setOpaque(opaque); 6541 } 6542 } 6543 6544 class TakenSurfaceHolder extends BaseSurfaceHolder { 6545 @Override 6546 public boolean onAllowLockCanvas() { 6547 return mDrawingAllowed; 6548 } 6549 6550 @Override 6551 public void onRelayoutContainer() { 6552 // Not currently interesting -- from changing between fixed and layout size. 6553 } 6554 6555 @Override 6556 public void setFormat(int format) { 6557 ((RootViewSurfaceTaker)mView).setSurfaceFormat(format); 6558 } 6559 6560 @Override 6561 public void setType(int type) { 6562 ((RootViewSurfaceTaker)mView).setSurfaceType(type); 6563 } 6564 6565 @Override 6566 public void onUpdateSurface() { 6567 // We take care of format and type changes on our own. 6568 throw new IllegalStateException("Shouldn't be here"); 6569 } 6570 6571 @Override 6572 public boolean isCreating() { 6573 return mIsCreating; 6574 } 6575 6576 @Override 6577 public void setFixedSize(int width, int height) { 6578 throw new UnsupportedOperationException( 6579 "Currently only support sizing from layout"); 6580 } 6581 6582 @Override 6583 public void setKeepScreenOn(boolean screenOn) { 6584 ((RootViewSurfaceTaker)mView).setSurfaceKeepScreenOn(screenOn); 6585 } 6586 } 6587 6588 static class W extends IWindow.Stub { 6589 private final WeakReference<ViewRootImpl> mViewAncestor; 6590 private final IWindowSession mWindowSession; 6591 6592 W(ViewRootImpl viewAncestor) { 6593 mViewAncestor = new WeakReference<ViewRootImpl>(viewAncestor); 6594 mWindowSession = viewAncestor.mWindowSession; 6595 } 6596 6597 @Override 6598 public void resized(Rect frame, Rect overscanInsets, Rect contentInsets, 6599 Rect visibleInsets, Rect stableInsets, Rect outsets, boolean reportDraw, 6600 Configuration newConfig) { 6601 final ViewRootImpl viewAncestor = mViewAncestor.get(); 6602 if (viewAncestor != null) { 6603 viewAncestor.dispatchResized(frame, overscanInsets, contentInsets, 6604 visibleInsets, stableInsets, outsets, reportDraw, newConfig); 6605 } 6606 } 6607 6608 @Override 6609 public void moved(int newX, int newY) { 6610 final ViewRootImpl viewAncestor = mViewAncestor.get(); 6611 if (viewAncestor != null) { 6612 viewAncestor.dispatchMoved(newX, newY); 6613 } 6614 } 6615 6616 @Override 6617 public void dispatchAppVisibility(boolean visible) { 6618 final ViewRootImpl viewAncestor = mViewAncestor.get(); 6619 if (viewAncestor != null) { 6620 viewAncestor.dispatchAppVisibility(visible); 6621 } 6622 } 6623 6624 @Override 6625 public void dispatchGetNewSurface() { 6626 final ViewRootImpl viewAncestor = mViewAncestor.get(); 6627 if (viewAncestor != null) { 6628 viewAncestor.dispatchGetNewSurface(); 6629 } 6630 } 6631 6632 @Override 6633 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) { 6634 final ViewRootImpl viewAncestor = mViewAncestor.get(); 6635 if (viewAncestor != null) { 6636 viewAncestor.windowFocusChanged(hasFocus, inTouchMode); 6637 } 6638 } 6639 6640 private static int checkCallingPermission(String permission) { 6641 try { 6642 return ActivityManagerNative.getDefault().checkPermission( 6643 permission, Binder.getCallingPid(), Binder.getCallingUid()); 6644 } catch (RemoteException e) { 6645 return PackageManager.PERMISSION_DENIED; 6646 } 6647 } 6648 6649 @Override 6650 public void executeCommand(String command, String parameters, ParcelFileDescriptor out) { 6651 final ViewRootImpl viewAncestor = mViewAncestor.get(); 6652 if (viewAncestor != null) { 6653 final View view = viewAncestor.mView; 6654 if (view != null) { 6655 if (checkCallingPermission(Manifest.permission.DUMP) != 6656 PackageManager.PERMISSION_GRANTED) { 6657 throw new SecurityException("Insufficient permissions to invoke" 6658 + " executeCommand() from pid=" + Binder.getCallingPid() 6659 + ", uid=" + Binder.getCallingUid()); 6660 } 6661 6662 OutputStream clientStream = null; 6663 try { 6664 clientStream = new ParcelFileDescriptor.AutoCloseOutputStream(out); 6665 ViewDebug.dispatchCommand(view, command, parameters, clientStream); 6666 } catch (IOException e) { 6667 e.printStackTrace(); 6668 } finally { 6669 if (clientStream != null) { 6670 try { 6671 clientStream.close(); 6672 } catch (IOException e) { 6673 e.printStackTrace(); 6674 } 6675 } 6676 } 6677 } 6678 } 6679 } 6680 6681 @Override 6682 public void closeSystemDialogs(String reason) { 6683 final ViewRootImpl viewAncestor = mViewAncestor.get(); 6684 if (viewAncestor != null) { 6685 viewAncestor.dispatchCloseSystemDialogs(reason); 6686 } 6687 } 6688 6689 @Override 6690 public void dispatchWallpaperOffsets(float x, float y, float xStep, float yStep, 6691 boolean sync) { 6692 if (sync) { 6693 try { 6694 mWindowSession.wallpaperOffsetsComplete(asBinder()); 6695 } catch (RemoteException e) { 6696 } 6697 } 6698 } 6699 6700 @Override 6701 public void dispatchWallpaperCommand(String action, int x, int y, 6702 int z, Bundle extras, boolean sync) { 6703 if (sync) { 6704 try { 6705 mWindowSession.wallpaperCommandComplete(asBinder(), null); 6706 } catch (RemoteException e) { 6707 } 6708 } 6709 } 6710 6711 /* Drag/drop */ 6712 @Override 6713 public void dispatchDragEvent(DragEvent event) { 6714 final ViewRootImpl viewAncestor = mViewAncestor.get(); 6715 if (viewAncestor != null) { 6716 viewAncestor.dispatchDragEvent(event); 6717 } 6718 } 6719 6720 @Override 6721 public void dispatchSystemUiVisibilityChanged(int seq, int globalVisibility, 6722 int localValue, int localChanges) { 6723 final ViewRootImpl viewAncestor = mViewAncestor.get(); 6724 if (viewAncestor != null) { 6725 viewAncestor.dispatchSystemUiVisibilityChanged(seq, globalVisibility, 6726 localValue, localChanges); 6727 } 6728 } 6729 6730 @Override 6731 public void onAnimationStarted(int remainingFrameCount) { 6732 final ViewRootImpl viewAncestor = mViewAncestor.get(); 6733 if (viewAncestor != null) { 6734 viewAncestor.dispatchWindowAnimationStarted(remainingFrameCount); 6735 } 6736 } 6737 6738 @Override 6739 public void onAnimationStopped() { 6740 final ViewRootImpl viewAncestor = mViewAncestor.get(); 6741 if (viewAncestor != null) { 6742 viewAncestor.dispatchWindowAnimationStopped(); 6743 } 6744 } 6745 6746 @Override 6747 public void dispatchWindowShown() { 6748 final ViewRootImpl viewAncestor = mViewAncestor.get(); 6749 if (viewAncestor != null) { 6750 viewAncestor.dispatchWindowShown(); 6751 } 6752 } 6753 } 6754 6755 public static final class CalledFromWrongThreadException extends AndroidRuntimeException { 6756 public CalledFromWrongThreadException(String msg) { 6757 super(msg); 6758 } 6759 } 6760 6761 static HandlerActionQueue getRunQueue() { 6762 HandlerActionQueue rq = sRunQueues.get(); 6763 if (rq != null) { 6764 return rq; 6765 } 6766 rq = new HandlerActionQueue(); 6767 sRunQueues.set(rq); 6768 return rq; 6769 } 6770 6771 /** 6772 * Class for managing the accessibility interaction connection 6773 * based on the global accessibility state. 6774 */ 6775 final class AccessibilityInteractionConnectionManager 6776 implements AccessibilityStateChangeListener { 6777 @Override 6778 public void onAccessibilityStateChanged(boolean enabled) { 6779 if (enabled) { 6780 ensureConnection(); 6781 if (mAttachInfo.mHasWindowFocus) { 6782 mView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED); 6783 View focusedView = mView.findFocus(); 6784 if (focusedView != null && focusedView != mView) { 6785 focusedView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED); 6786 } 6787 } 6788 } else { 6789 ensureNoConnection(); 6790 mHandler.obtainMessage(MSG_CLEAR_ACCESSIBILITY_FOCUS_HOST).sendToTarget(); 6791 } 6792 } 6793 6794 public void ensureConnection() { 6795 final boolean registered = 6796 mAttachInfo.mAccessibilityWindowId != AccessibilityNodeInfo.UNDEFINED_ITEM_ID; 6797 if (!registered) { 6798 mAttachInfo.mAccessibilityWindowId = 6799 mAccessibilityManager.addAccessibilityInteractionConnection(mWindow, 6800 new AccessibilityInteractionConnection(ViewRootImpl.this)); 6801 } 6802 } 6803 6804 public void ensureNoConnection() { 6805 final boolean registered = 6806 mAttachInfo.mAccessibilityWindowId != AccessibilityNodeInfo.UNDEFINED_ITEM_ID; 6807 if (registered) { 6808 mAttachInfo.mAccessibilityWindowId = AccessibilityNodeInfo.UNDEFINED_ITEM_ID; 6809 mAccessibilityManager.removeAccessibilityInteractionConnection(mWindow); 6810 } 6811 } 6812 } 6813 6814 final class HighContrastTextManager implements HighTextContrastChangeListener { 6815 HighContrastTextManager() { 6816 mAttachInfo.mHighContrastText = mAccessibilityManager.isHighTextContrastEnabled(); 6817 } 6818 @Override 6819 public void onHighTextContrastStateChanged(boolean enabled) { 6820 mAttachInfo.mHighContrastText = enabled; 6821 6822 // Destroy Displaylists so they can be recreated with high contrast recordings 6823 destroyHardwareResources(); 6824 6825 // Schedule redraw, which will rerecord + redraw all text 6826 invalidate(); 6827 } 6828 } 6829 6830 /** 6831 * This class is an interface this ViewAncestor provides to the 6832 * AccessibilityManagerService to the latter can interact with 6833 * the view hierarchy in this ViewAncestor. 6834 */ 6835 static final class AccessibilityInteractionConnection 6836 extends IAccessibilityInteractionConnection.Stub { 6837 private final WeakReference<ViewRootImpl> mViewRootImpl; 6838 6839 AccessibilityInteractionConnection(ViewRootImpl viewRootImpl) { 6840 mViewRootImpl = new WeakReference<ViewRootImpl>(viewRootImpl); 6841 } 6842 6843 @Override 6844 public void findAccessibilityNodeInfoByAccessibilityId(long accessibilityNodeId, 6845 Region interactiveRegion, int interactionId, 6846 IAccessibilityInteractionConnectionCallback callback, int flags, 6847 int interrogatingPid, long interrogatingTid, MagnificationSpec spec) { 6848 ViewRootImpl viewRootImpl = mViewRootImpl.get(); 6849 if (viewRootImpl != null && viewRootImpl.mView != null) { 6850 viewRootImpl.getAccessibilityInteractionController() 6851 .findAccessibilityNodeInfoByAccessibilityIdClientThread(accessibilityNodeId, 6852 interactiveRegion, interactionId, callback, flags, interrogatingPid, 6853 interrogatingTid, spec); 6854 } else { 6855 // We cannot make the call and notify the caller so it does not wait. 6856 try { 6857 callback.setFindAccessibilityNodeInfosResult(null, interactionId); 6858 } catch (RemoteException re) { 6859 /* best effort - ignore */ 6860 } 6861 } 6862 } 6863 6864 @Override 6865 public void performAccessibilityAction(long accessibilityNodeId, int action, 6866 Bundle arguments, int interactionId, 6867 IAccessibilityInteractionConnectionCallback callback, int flags, 6868 int interrogatingPid, long interrogatingTid) { 6869 ViewRootImpl viewRootImpl = mViewRootImpl.get(); 6870 if (viewRootImpl != null && viewRootImpl.mView != null) { 6871 viewRootImpl.getAccessibilityInteractionController() 6872 .performAccessibilityActionClientThread(accessibilityNodeId, action, arguments, 6873 interactionId, callback, flags, interrogatingPid, interrogatingTid); 6874 } else { 6875 // We cannot make the call and notify the caller so it does not wait. 6876 try { 6877 callback.setPerformAccessibilityActionResult(false, interactionId); 6878 } catch (RemoteException re) { 6879 /* best effort - ignore */ 6880 } 6881 } 6882 } 6883 6884 @Override 6885 public void findAccessibilityNodeInfosByViewId(long accessibilityNodeId, 6886 String viewId, Region interactiveRegion, int interactionId, 6887 IAccessibilityInteractionConnectionCallback callback, int flags, 6888 int interrogatingPid, long interrogatingTid, MagnificationSpec spec) { 6889 ViewRootImpl viewRootImpl = mViewRootImpl.get(); 6890 if (viewRootImpl != null && viewRootImpl.mView != null) { 6891 viewRootImpl.getAccessibilityInteractionController() 6892 .findAccessibilityNodeInfosByViewIdClientThread(accessibilityNodeId, 6893 viewId, interactiveRegion, interactionId, callback, flags, 6894 interrogatingPid, interrogatingTid, spec); 6895 } else { 6896 // We cannot make the call and notify the caller so it does not wait. 6897 try { 6898 callback.setFindAccessibilityNodeInfoResult(null, interactionId); 6899 } catch (RemoteException re) { 6900 /* best effort - ignore */ 6901 } 6902 } 6903 } 6904 6905 @Override 6906 public void findAccessibilityNodeInfosByText(long accessibilityNodeId, String text, 6907 Region interactiveRegion, int interactionId, 6908 IAccessibilityInteractionConnectionCallback callback, int flags, 6909 int interrogatingPid, long interrogatingTid, MagnificationSpec spec) { 6910 ViewRootImpl viewRootImpl = mViewRootImpl.get(); 6911 if (viewRootImpl != null && viewRootImpl.mView != null) { 6912 viewRootImpl.getAccessibilityInteractionController() 6913 .findAccessibilityNodeInfosByTextClientThread(accessibilityNodeId, text, 6914 interactiveRegion, interactionId, callback, flags, interrogatingPid, 6915 interrogatingTid, spec); 6916 } else { 6917 // We cannot make the call and notify the caller so it does not wait. 6918 try { 6919 callback.setFindAccessibilityNodeInfosResult(null, interactionId); 6920 } catch (RemoteException re) { 6921 /* best effort - ignore */ 6922 } 6923 } 6924 } 6925 6926 @Override 6927 public void findFocus(long accessibilityNodeId, int focusType, Region interactiveRegion, 6928 int interactionId, IAccessibilityInteractionConnectionCallback callback, int flags, 6929 int interrogatingPid, long interrogatingTid, MagnificationSpec spec) { 6930 ViewRootImpl viewRootImpl = mViewRootImpl.get(); 6931 if (viewRootImpl != null && viewRootImpl.mView != null) { 6932 viewRootImpl.getAccessibilityInteractionController() 6933 .findFocusClientThread(accessibilityNodeId, focusType, interactiveRegion, 6934 interactionId, callback, flags, interrogatingPid, interrogatingTid, 6935 spec); 6936 } else { 6937 // We cannot make the call and notify the caller so it does not wait. 6938 try { 6939 callback.setFindAccessibilityNodeInfoResult(null, interactionId); 6940 } catch (RemoteException re) { 6941 /* best effort - ignore */ 6942 } 6943 } 6944 } 6945 6946 @Override 6947 public void focusSearch(long accessibilityNodeId, int direction, Region interactiveRegion, 6948 int interactionId, IAccessibilityInteractionConnectionCallback callback, int flags, 6949 int interrogatingPid, long interrogatingTid, MagnificationSpec spec) { 6950 ViewRootImpl viewRootImpl = mViewRootImpl.get(); 6951 if (viewRootImpl != null && viewRootImpl.mView != null) { 6952 viewRootImpl.getAccessibilityInteractionController() 6953 .focusSearchClientThread(accessibilityNodeId, direction, interactiveRegion, 6954 interactionId, callback, flags, interrogatingPid, interrogatingTid, 6955 spec); 6956 } else { 6957 // We cannot make the call and notify the caller so it does not wait. 6958 try { 6959 callback.setFindAccessibilityNodeInfoResult(null, interactionId); 6960 } catch (RemoteException re) { 6961 /* best effort - ignore */ 6962 } 6963 } 6964 } 6965 } 6966 6967 private class SendWindowContentChangedAccessibilityEvent implements Runnable { 6968 private int mChangeTypes = 0; 6969 6970 public View mSource; 6971 public long mLastEventTimeMillis; 6972 6973 @Override 6974 public void run() { 6975 // The accessibility may be turned off while we were waiting so check again. 6976 if (AccessibilityManager.getInstance(mContext).isEnabled()) { 6977 mLastEventTimeMillis = SystemClock.uptimeMillis(); 6978 AccessibilityEvent event = AccessibilityEvent.obtain(); 6979 event.setEventType(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED); 6980 event.setContentChangeTypes(mChangeTypes); 6981 mSource.sendAccessibilityEventUnchecked(event); 6982 } else { 6983 mLastEventTimeMillis = 0; 6984 } 6985 // In any case reset to initial state. 6986 mSource.resetSubtreeAccessibilityStateChanged(); 6987 mSource = null; 6988 mChangeTypes = 0; 6989 } 6990 6991 public void runOrPost(View source, int changeType) { 6992 if (mSource != null) { 6993 // If there is no common predecessor, then mSource points to 6994 // a removed view, hence in this case always prefer the source. 6995 View predecessor = getCommonPredecessor(mSource, source); 6996 mSource = (predecessor != null) ? predecessor : source; 6997 mChangeTypes |= changeType; 6998 return; 6999 } 7000 mSource = source; 7001 mChangeTypes = changeType; 7002 final long timeSinceLastMillis = SystemClock.uptimeMillis() - mLastEventTimeMillis; 7003 final long minEventIntevalMillis = 7004 ViewConfiguration.getSendRecurringAccessibilityEventsInterval(); 7005 if (timeSinceLastMillis >= minEventIntevalMillis) { 7006 mSource.removeCallbacks(this); 7007 run(); 7008 } else { 7009 mSource.postDelayed(this, minEventIntevalMillis - timeSinceLastMillis); 7010 } 7011 } 7012 } 7013} 7014