2 * Copyright (C) 2006 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import static android.view.WindowCallbacks.RESIZE_MODE_DOCKED_DIVIDER;
20 import static android.view.WindowCallbacks.RESIZE_MODE_FREEFORM;
21 import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_DECOR_VIEW_VISIBILITY;
22 import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
23 import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
24 import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL;
25 import static android.view.WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY;
27 import android.Manifest;
28 import android.animation.LayoutTransition;
29 import android.annotation.NonNull;
30 import android.app.ActivityManagerNative;
31 import android.app.ResourcesManager;
32 import android.content.ClipDescription;
33 import android.content.ComponentCallbacks;
34 import android.content.Context;
35 import android.content.pm.PackageManager;
36 import android.content.res.CompatibilityInfo;
37 import android.content.res.Configuration;
38 import android.content.res.Resources;
39 import android.graphics.Canvas;
40 import android.graphics.Matrix;
41 import android.graphics.PixelFormat;
42 import android.graphics.Point;
43 import android.graphics.PointF;
44 import android.graphics.PorterDuff;
45 import android.graphics.Rect;
46 import android.graphics.Region;
47 import android.graphics.drawable.AnimatedVectorDrawable;
48 import android.graphics.drawable.Drawable;
49 import android.hardware.display.DisplayManager;
50 import android.hardware.display.DisplayManager.DisplayListener;
51 import android.hardware.input.InputManager;
52 import android.media.AudioManager;
53 import android.os.Binder;
54 import android.os.Build;
55 import android.os.Bundle;
56 import android.os.Debug;
57 import android.os.Handler;
58 import android.os.Looper;
59 import android.os.Message;
60 import android.os.ParcelFileDescriptor;
61 import android.os.Process;
62 import android.os.RemoteException;
63 import android.os.SystemClock;
64 import android.os.SystemProperties;
65 import android.os.Trace;
66 import android.util.AndroidRuntimeException;
67 import android.util.DisplayMetrics;
68 import android.util.Log;
69 import android.util.Slog;
70 import android.util.TimeUtils;
71 import android.util.TypedValue;
72 import android.view.Surface.OutOfResourcesException;
73 import android.view.View.AttachInfo;
74 import android.view.View.MeasureSpec;
75 import android.view.accessibility.AccessibilityEvent;
76 import android.view.accessibility.AccessibilityManager;
77 import android.view.accessibility.AccessibilityManager.AccessibilityStateChangeListener;
78 import android.view.accessibility.AccessibilityManager.HighTextContrastChangeListener;
79 import android.view.accessibility.AccessibilityNodeInfo;
80 import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
81 import android.view.accessibility.AccessibilityNodeProvider;
82 import android.view.accessibility.IAccessibilityInteractionConnection;
83 import android.view.accessibility.IAccessibilityInteractionConnectionCallback;
84 import android.view.animation.AccelerateDecelerateInterpolator;
85 import android.view.animation.Interpolator;
86 import android.view.inputmethod.InputMethodManager;
87 import android.widget.Scroller;
89 import com.android.internal.R;
90 import com.android.internal.annotations.GuardedBy;
91 import com.android.internal.os.IResultReceiver;
92 import com.android.internal.os.SomeArgs;
93 import com.android.internal.policy.PhoneFallbackEventHandler;
94 import com.android.internal.view.BaseSurfaceHolder;
95 import com.android.internal.view.RootViewSurfaceTaker;
97 import java.io.FileDescriptor;
98 import java.io.IOException;
99 import java.io.OutputStream;
100 import java.io.PrintWriter;
101 import java.lang.ref.WeakReference;
102 import java.util.ArrayList;
103 import java.util.HashSet;
104 import java.util.concurrent.CountDownLatch;
107 * The top of a view hierarchy, implementing the needed protocol between View
108 * and the WindowManager. This is for the most part an internal implementation
109 * detail of {@link WindowManagerGlobal}.
113 @SuppressWarnings({"EmptyCatchBlock", "PointlessBooleanExpression"})
114 public final class ViewRootImpl implements ViewParent,
115 View.AttachInfo.Callbacks, ThreadedRenderer.HardwareDrawCallbacks {
116 private static final String TAG = "ViewRootImpl";
117 private static final boolean DBG = false;
118 private static final boolean LOCAL_LOGV = false;
119 /** @noinspection PointlessBooleanExpression*/
120 private static final boolean DEBUG_DRAW = false || LOCAL_LOGV;
121 private static final boolean DEBUG_LAYOUT = false || LOCAL_LOGV;
122 private static final boolean DEBUG_DIALOG = false || LOCAL_LOGV;
123 private static final boolean DEBUG_INPUT_RESIZE = false || LOCAL_LOGV;
124 private static final boolean DEBUG_ORIENTATION = false || LOCAL_LOGV;
125 private static final boolean DEBUG_TRACKBALL = false || LOCAL_LOGV;
126 private static final boolean DEBUG_IMF = false || LOCAL_LOGV;
127 private static final boolean DEBUG_CONFIGURATION = false || LOCAL_LOGV;
128 private static final boolean DEBUG_FPS = false;
129 private static final boolean DEBUG_INPUT_STAGES = false || LOCAL_LOGV;
130 private static final boolean DEBUG_KEEP_SCREEN_ON = false || LOCAL_LOGV;
133 * Set to false if we do not want to use the multi threaded renderer. Note that by disabling
134 * this, WindowCallbacks will not fire.
136 private static final boolean USE_MT_RENDERER = true;
139 * Set this system property to true to force the view hierarchy to render
140 * at 60 Hz. This can be used to measure the potential framerate.
142 private static final String PROPERTY_PROFILE_RENDERING = "viewroot.profile_rendering";
144 // properties used by emulator to determine display shape
145 public static final String PROPERTY_EMULATOR_WIN_OUTSET_BOTTOM_PX =
146 "ro.emu.win_outset_bottom_px";
149 * Maximum time we allow the user to roll the trackball enough to generate
150 * a key event, before resetting the counters.
152 static final int MAX_TRACKBALL_DELAY = 250;
154 static final ThreadLocal<HandlerActionQueue> sRunQueues = new ThreadLocal<HandlerActionQueue>();
156 static final ArrayList<Runnable> sFirstDrawHandlers = new ArrayList();
157 static boolean sFirstDrawComplete = false;
159 static final ArrayList<ComponentCallbacks> sConfigCallbacks = new ArrayList();
162 * This list must only be modified by the main thread, so a lock is only needed when changing
163 * the list or when accessing the list from a non-main thread.
165 @GuardedBy("mWindowCallbacks")
166 final ArrayList<WindowCallbacks> mWindowCallbacks = new ArrayList<>();
167 final Context mContext;
168 final IWindowSession mWindowSession;
169 @NonNull Display mDisplay;
170 final DisplayManager mDisplayManager;
171 final String mBasePackageName;
173 final int[] mTmpLocation = new int[2];
175 final TypedValue mTmpValue = new TypedValue();
177 final Thread mThread;
179 final WindowLeaked mLocation;
181 final WindowManager.LayoutParams mWindowAttributes = new WindowManager.LayoutParams();
185 final int mTargetSdkVersion;
191 View mAccessibilityFocusedHost;
192 AccessibilityNodeInfo mAccessibilityFocusedVirtualView;
194 // The view which captures mouse input, or null when no one is capturing.
198 boolean mAppVisible = true;
199 // For recents to freeform transition we need to keep drawing after the app receives information
200 // that it became invisible. This will ignore that information and depend on the decor view
201 // visibility to control drawing. The decor view visibility will get adjusted when the app get
202 // stopped and that's when the app will stop drawing further frames.
203 private boolean mForceDecorViewVisibility = false;
204 int mOrigWindowType = -1;
206 /** Whether the window had focus during the most recent traversal. */
207 boolean mHadWindowFocus;
210 * Whether the window lost focus during a previous traversal and has not
211 * yet gained it back. Used to determine whether a WINDOW_STATE_CHANGE
212 * accessibility events should be sent during traversal.
214 boolean mLostWindowFocus;
216 // Set to true if the owner of this window is in the stopped state,
217 // so the window should no longer be active.
218 boolean mStopped = false;
220 // Set to true if the owner of this window is in ambient mode,
221 // which means it won't receive input events.
222 boolean mIsAmbientMode = false;
224 // Set to true to stop input during an Activity Transition.
225 boolean mPausedForTransition = false;
227 boolean mLastInCompatMode = false;
229 SurfaceHolder.Callback2 mSurfaceHolderCallback;
230 BaseSurfaceHolder mSurfaceHolder;
232 boolean mDrawingAllowed;
234 final Region mTransparentRegion;
235 final Region mPreviousTransparentRegion;
240 boolean mIsAnimating;
242 private boolean mDragResizing;
243 private boolean mInvalidateRootRequested;
244 private int mResizeMode;
245 private int mCanvasOffsetX;
246 private int mCanvasOffsetY;
247 private boolean mActivityRelaunched;
249 CompatibilityInfo.Translator mTranslator;
251 final View.AttachInfo mAttachInfo;
252 InputChannel mInputChannel;
253 InputQueue.Callback mInputQueueCallback;
254 InputQueue mInputQueue;
255 FallbackEventHandler mFallbackEventHandler;
256 Choreographer mChoreographer;
258 final Rect mTempRect; // used in the transaction to not thrash the heap.
259 final Rect mVisRect; // used to retrieve visible rect of focused view.
261 boolean mTraversalScheduled;
262 int mTraversalBarrier;
263 boolean mWillDrawSoon;
264 /** Set to true while in performTraversals for detecting when die(true) is called from internal
265 * callbacks such as onMeasure, onPreDraw, onDraw and deferring doDie() until later. */
266 boolean mIsInTraversal;
267 boolean mApplyInsetsRequested;
268 boolean mLayoutRequested;
270 boolean mReportNextDraw;
271 boolean mFullRedrawNeeded;
272 boolean mNewSurfaceNeeded;
273 boolean mHasHadWindowFocus;
274 boolean mLastWasImTarget;
275 boolean mForceNextWindowRelayout;
276 CountDownLatch mWindowDrawCountDown;
279 int mLastSystemUiVisibility;
280 int mClientWindowLayoutFlags;
281 boolean mLastOverscanRequested;
283 // Pool of queued input events.
284 private static final int MAX_QUEUED_INPUT_EVENT_POOL_SIZE = 10;
285 private QueuedInputEvent mQueuedInputEventPool;
286 private int mQueuedInputEventPoolSize;
288 /* Input event queue.
289 * Pending input events are input events waiting to be delivered to the input stages
290 * and handled by the application.
292 QueuedInputEvent mPendingInputEventHead;
293 QueuedInputEvent mPendingInputEventTail;
294 int mPendingInputEventCount;
295 boolean mProcessInputEventsScheduled;
296 boolean mUnbufferedInputDispatch;
297 String mPendingInputEventQueueLengthCounterName = "pq";
299 InputStage mFirstInputStage;
300 InputStage mFirstPostImeInputStage;
301 InputStage mSyntheticInputStage;
303 boolean mWindowAttributesChanged = false;
304 int mWindowAttributesChangesFlag = 0;
306 // These can be accessed by any thread, must be protected with a lock.
307 // Surface can never be reassigned or cleared (use Surface.clear()).
308 final Surface mSurface = new Surface();
311 boolean mAddedTouchMode;
313 // These are accessed by multiple threads.
314 final Rect mWinFrame; // frame given by window manager.
316 final Rect mPendingOverscanInsets = new Rect();
317 final Rect mPendingVisibleInsets = new Rect();
318 final Rect mPendingStableInsets = new Rect();
319 final Rect mPendingContentInsets = new Rect();
320 final Rect mPendingOutsets = new Rect();
321 final Rect mPendingBackDropFrame = new Rect();
322 boolean mPendingAlwaysConsumeNavBar;
323 final ViewTreeObserver.InternalInsetsInfo mLastGivenInsets
324 = new ViewTreeObserver.InternalInsetsInfo();
326 final Rect mDispatchContentInsets = new Rect();
327 final Rect mDispatchStableInsets = new Rect();
329 private WindowInsets mLastWindowInsets;
331 final Configuration mLastConfiguration = new Configuration();
332 final Configuration mPendingConfiguration = new Configuration();
334 boolean mScrollMayChange;
336 WeakReference<View> mLastScrolledFocus;
340 static final Interpolator mResizeInterpolator = new AccelerateDecelerateInterpolator();
341 private ArrayList<LayoutTransition> mPendingTransitions;
343 final ViewConfiguration mViewConfiguration;
346 ClipDescription mDragDescription;
347 View mCurrentDragView;
348 volatile Object mLocalDragState;
349 final PointF mDragPoint = new PointF();
350 final PointF mLastTouchPoint = new PointF();
351 int mLastTouchSource;
353 private boolean mProfileRendering;
354 private Choreographer.FrameCallback mRenderProfiler;
355 private boolean mRenderProfilingEnabled;
357 // Variables to track frames per second, enabled via DEBUG_FPS flag
358 private long mFpsStartTime = -1;
359 private long mFpsPrevTime = -1;
360 private int mFpsNumFrames;
362 private int mPointerIconType = PointerIcon.TYPE_NOT_SPECIFIED;
363 private PointerIcon mCustomPointerIcon = null;
366 * see {@link #playSoundEffect(int)}
368 AudioManager mAudioManager;
370 final AccessibilityManager mAccessibilityManager;
372 AccessibilityInteractionController mAccessibilityInteractionController;
374 AccessibilityInteractionConnectionManager mAccessibilityInteractionConnectionManager;
375 HighContrastTextManager mHighContrastTextManager;
377 SendWindowContentChangedAccessibilityEvent mSendWindowContentChangedAccessibilityEvent;
379 HashSet<View> mTempHashSet;
381 private final int mDensity;
382 private final int mNoncompatDensity;
384 private boolean mInLayout = false;
385 ArrayList<View> mLayoutRequesters = new ArrayList<View>();
386 boolean mHandlingLayoutInLayoutRequest = false;
388 private int mViewLayoutDirectionInitial;
390 /** Set to true once doDie() has been called. */
391 private boolean mRemoved;
393 private boolean mNeedsHwRendererSetup;
396 * Consistency verifier for debugging purposes.
398 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
399 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
400 new InputEventConsistencyVerifier(this, 0) : null;
402 static final class SystemUiVisibilityInfo {
404 int globalVisibility;
409 private String mTag = TAG;
411 public ViewRootImpl(Context context, Display display) {
413 mWindowSession = WindowManagerGlobal.getWindowSession();
415 mBasePackageName = context.getBasePackageName();
416 mThread = Thread.currentThread();
417 mLocation = new WindowLeaked(null);
418 mLocation.fillInStackTrace();
422 mTempRect = new Rect();
423 mVisRect = new Rect();
424 mWinFrame = new Rect();
425 mWindow = new W(this);
426 mTargetSdkVersion = context.getApplicationInfo().targetSdkVersion;
427 mViewVisibility = View.GONE;
428 mTransparentRegion = new Region();
429 mPreviousTransparentRegion = new Region();
430 mFirst = true; // true for the first time the view is added
432 mAttachInfo = new View.AttachInfo(mWindowSession, mWindow, display, this, mHandler, this);
433 mAccessibilityManager = AccessibilityManager.getInstance(context);
434 mAccessibilityInteractionConnectionManager =
435 new AccessibilityInteractionConnectionManager();
436 mAccessibilityManager.addAccessibilityStateChangeListener(
437 mAccessibilityInteractionConnectionManager);
438 mHighContrastTextManager = new HighContrastTextManager();
439 mAccessibilityManager.addHighTextContrastStateChangeListener(
440 mHighContrastTextManager);
441 mViewConfiguration = ViewConfiguration.get(context);
442 mDensity = context.getResources().getDisplayMetrics().densityDpi;
443 mNoncompatDensity = context.getResources().getDisplayMetrics().noncompatDensityDpi;
444 mFallbackEventHandler = new PhoneFallbackEventHandler(context);
445 mChoreographer = Choreographer.getInstance();
446 mDisplayManager = (DisplayManager)context.getSystemService(Context.DISPLAY_SERVICE);
447 loadSystemProperties();
450 public static void addFirstDrawHandler(Runnable callback) {
451 synchronized (sFirstDrawHandlers) {
452 if (!sFirstDrawComplete) {
453 sFirstDrawHandlers.add(callback);
458 public static void addConfigCallback(ComponentCallbacks callback) {
459 synchronized (sConfigCallbacks) {
460 sConfigCallbacks.add(callback);
464 public void addWindowCallbacks(WindowCallbacks callback) {
465 if (USE_MT_RENDERER) {
466 synchronized (mWindowCallbacks) {
467 mWindowCallbacks.add(callback);
472 public void removeWindowCallbacks(WindowCallbacks callback) {
473 if (USE_MT_RENDERER) {
474 synchronized (mWindowCallbacks) {
475 mWindowCallbacks.remove(callback);
480 public void reportDrawFinish() {
481 if (mWindowDrawCountDown != null) {
482 mWindowDrawCountDown.countDown();
486 // FIXME for perf testing only
487 private boolean mProfile = false;
490 * Call this to profile the next traversal call.
491 * FIXME for perf testing only. Remove eventually
493 public void profile() {
498 * Indicates whether we are in touch mode. Calling this method triggers an IPC
499 * call and should be avoided whenever possible.
501 * @return True, if the device is in touch mode, false otherwise.
505 static boolean isInTouchMode() {
506 IWindowSession windowSession = WindowManagerGlobal.peekWindowSession();
507 if (windowSession != null) {
509 return windowSession.getInTouchMode();
510 } catch (RemoteException e) {
517 * Notifies us that our child has been rebuilt, following
518 * a window preservation operation. In these cases we
519 * keep the same DecorView, but the activity controlling it
520 * is a different instance, and we need to update our
525 public void notifyChildRebuilt() {
526 if (mView instanceof RootViewSurfaceTaker) {
527 mSurfaceHolderCallback =
528 ((RootViewSurfaceTaker)mView).willYouTakeTheSurface();
529 if (mSurfaceHolderCallback != null) {
530 mSurfaceHolder = new TakenSurfaceHolder();
531 mSurfaceHolder.setFormat(PixelFormat.UNKNOWN);
533 mSurfaceHolder = null;
536 mInputQueueCallback =
537 ((RootViewSurfaceTaker)mView).willYouTakeTheInputQueue();
538 if (mInputQueueCallback != null) {
539 mInputQueueCallback.onInputQueueCreated(mInputQueue);
547 public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
548 synchronized (this) {
552 mAttachInfo.mDisplayState = mDisplay.getState();
553 mDisplayManager.registerDisplayListener(mDisplayListener, mHandler);
555 mViewLayoutDirectionInitial = mView.getRawLayoutDirection();
556 mFallbackEventHandler.setView(view);
557 mWindowAttributes.copyFrom(attrs);
558 if (mWindowAttributes.packageName == null) {
559 mWindowAttributes.packageName = mBasePackageName;
561 attrs = mWindowAttributes;
564 if (DEBUG_KEEP_SCREEN_ON && (mClientWindowLayoutFlags
565 & WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) != 0
566 && (attrs.flags&WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) == 0) {
567 Slog.d(mTag, "setView: FLAG_KEEP_SCREEN_ON changed from true to false!");
569 // Keep track of the actual window flags supplied by the client.
570 mClientWindowLayoutFlags = attrs.flags;
572 setAccessibilityFocus(null, null);
574 if (view instanceof RootViewSurfaceTaker) {
575 mSurfaceHolderCallback =
576 ((RootViewSurfaceTaker)view).willYouTakeTheSurface();
577 if (mSurfaceHolderCallback != null) {
578 mSurfaceHolder = new TakenSurfaceHolder();
579 mSurfaceHolder.setFormat(PixelFormat.UNKNOWN);
583 // Compute surface insets required to draw at specified Z value.
584 // TODO: Use real shadow insets for a constant max Z.
585 if (!attrs.hasManualSurfaceInsets) {
586 attrs.setSurfaceInsets(view, false /*manual*/, true /*preservePrevious*/);
589 CompatibilityInfo compatibilityInfo =
590 mDisplay.getDisplayAdjustments().getCompatibilityInfo();
591 mTranslator = compatibilityInfo.getTranslator();
593 // If the application owns the surface, don't enable hardware acceleration
594 if (mSurfaceHolder == null) {
595 enableHardwareAcceleration(attrs);
598 boolean restore = false;
599 if (mTranslator != null) {
600 mSurface.setCompatibilityTranslator(mTranslator);
603 mTranslator.translateWindowLayout(attrs);
605 if (DEBUG_LAYOUT) Log.d(mTag, "WindowLayout in setView:" + attrs);
607 if (!compatibilityInfo.supportsScreen()) {
608 attrs.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW;
609 mLastInCompatMode = true;
612 mSoftInputMode = attrs.softInputMode;
613 mWindowAttributesChanged = true;
614 mWindowAttributesChangesFlag = WindowManager.LayoutParams.EVERYTHING_CHANGED;
615 mAttachInfo.mRootView = view;
616 mAttachInfo.mScalingRequired = mTranslator != null;
617 mAttachInfo.mApplicationScale =
618 mTranslator == null ? 1.0f : mTranslator.applicationScale;
619 if (panelParentView != null) {
620 mAttachInfo.mPanelParentWindowToken
621 = panelParentView.getApplicationWindowToken();
624 int res; /* = WindowManagerImpl.ADD_OKAY; */
626 // Schedule the first layout -before- adding to the window
627 // manager, to make sure we do the relayout before receiving
628 // any other events from the system.
630 if ((mWindowAttributes.inputFeatures
631 & WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0) {
632 mInputChannel = new InputChannel();
634 mForceDecorViewVisibility = (mWindowAttributes.privateFlags
635 & PRIVATE_FLAG_FORCE_DECOR_VIEW_VISIBILITY) != 0;
637 mOrigWindowType = mWindowAttributes.type;
638 mAttachInfo.mRecomputeGlobalAttributes = true;
639 collectViewAttributes();
640 res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
641 getHostVisibility(), mDisplay.getDisplayId(),
642 mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
643 mAttachInfo.mOutsets, mInputChannel);
644 } catch (RemoteException e) {
647 mAttachInfo.mRootView = null;
648 mInputChannel = null;
649 mFallbackEventHandler.setView(null);
650 unscheduleTraversals();
651 setAccessibilityFocus(null, null);
652 throw new RuntimeException("Adding window failed", e);
659 if (mTranslator != null) {
660 mTranslator.translateRectInScreenToAppWindow(mAttachInfo.mContentInsets);
662 mPendingOverscanInsets.set(0, 0, 0, 0);
663 mPendingContentInsets.set(mAttachInfo.mContentInsets);
664 mPendingStableInsets.set(mAttachInfo.mStableInsets);
665 mPendingVisibleInsets.set(0, 0, 0, 0);
666 mAttachInfo.mAlwaysConsumeNavBar =
667 (res & WindowManagerGlobal.ADD_FLAG_ALWAYS_CONSUME_NAV_BAR) != 0;
668 mPendingAlwaysConsumeNavBar = mAttachInfo.mAlwaysConsumeNavBar;
669 if (DEBUG_LAYOUT) Log.v(mTag, "Added window " + mWindow);
670 if (res < WindowManagerGlobal.ADD_OKAY) {
671 mAttachInfo.mRootView = null;
673 mFallbackEventHandler.setView(null);
674 unscheduleTraversals();
675 setAccessibilityFocus(null, null);
677 case WindowManagerGlobal.ADD_BAD_APP_TOKEN:
678 case WindowManagerGlobal.ADD_BAD_SUBWINDOW_TOKEN:
679 throw new WindowManager.BadTokenException(
680 "Unable to add window -- token " + attrs.token
681 + " is not valid; is your activity running?");
682 case WindowManagerGlobal.ADD_NOT_APP_TOKEN:
683 throw new WindowManager.BadTokenException(
684 "Unable to add window -- token " + attrs.token
685 + " is not for an application");
686 case WindowManagerGlobal.ADD_APP_EXITING:
687 throw new WindowManager.BadTokenException(
688 "Unable to add window -- app for token " + attrs.token
690 case WindowManagerGlobal.ADD_DUPLICATE_ADD:
691 throw new WindowManager.BadTokenException(
692 "Unable to add window -- window " + mWindow
693 + " has already been added");
694 case WindowManagerGlobal.ADD_STARTING_NOT_NEEDED:
695 // Silently ignore -- we would have just removed it
696 // right away, anyway.
698 case WindowManagerGlobal.ADD_MULTIPLE_SINGLETON:
699 throw new WindowManager.BadTokenException("Unable to add window "
700 + mWindow + " -- another window of type "
701 + mWindowAttributes.type + " already exists");
702 case WindowManagerGlobal.ADD_PERMISSION_DENIED:
703 throw new WindowManager.BadTokenException("Unable to add window "
704 + mWindow + " -- permission denied for window type "
705 + mWindowAttributes.type);
706 case WindowManagerGlobal.ADD_INVALID_DISPLAY:
707 throw new WindowManager.InvalidDisplayException("Unable to add window "
708 + mWindow + " -- the specified display can not be found");
709 case WindowManagerGlobal.ADD_INVALID_TYPE:
710 throw new WindowManager.InvalidDisplayException("Unable to add window "
711 + mWindow + " -- the specified window type "
712 + mWindowAttributes.type + " is not valid");
714 throw new RuntimeException(
715 "Unable to add window -- unknown error code " + res);
718 if (view instanceof RootViewSurfaceTaker) {
719 mInputQueueCallback =
720 ((RootViewSurfaceTaker)view).willYouTakeTheInputQueue();
722 if (mInputChannel != null) {
723 if (mInputQueueCallback != null) {
724 mInputQueue = new InputQueue();
725 mInputQueueCallback.onInputQueueCreated(mInputQueue);
727 mInputEventReceiver = new WindowInputEventReceiver(mInputChannel,
731 view.assignParent(this);
732 mAddedTouchMode = (res & WindowManagerGlobal.ADD_FLAG_IN_TOUCH_MODE) != 0;
733 mAppVisible = (res & WindowManagerGlobal.ADD_FLAG_APP_VISIBLE) != 0;
735 if (mAccessibilityManager.isEnabled()) {
736 mAccessibilityInteractionConnectionManager.ensureConnection();
739 if (view.getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
740 view.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
743 // Set up the input pipeline.
744 CharSequence counterSuffix = attrs.getTitle();
745 mSyntheticInputStage = new SyntheticInputStage();
746 InputStage viewPostImeStage = new ViewPostImeInputStage(mSyntheticInputStage);
747 InputStage nativePostImeStage = new NativePostImeInputStage(viewPostImeStage,
748 "aq:native-post-ime:" + counterSuffix);
749 InputStage earlyPostImeStage = new EarlyPostImeInputStage(nativePostImeStage);
750 InputStage imeStage = new ImeInputStage(earlyPostImeStage,
751 "aq:ime:" + counterSuffix);
752 InputStage viewPreImeStage = new ViewPreImeInputStage(imeStage);
753 InputStage nativePreImeStage = new NativePreImeInputStage(viewPreImeStage,
754 "aq:native-pre-ime:" + counterSuffix);
756 mFirstInputStage = nativePreImeStage;
757 mFirstPostImeInputStage = earlyPostImeStage;
758 mPendingInputEventQueueLengthCounterName = "aq:pending:" + counterSuffix;
763 private void setTag() {
764 final String[] split = mWindowAttributes.getTitle().toString().split("\\.");
765 if (split.length > 0) {
766 mTag = TAG + "[" + split[split.length - 1] + "]";
770 /** Whether the window is in local focus mode or not */
771 private boolean isInLocalFocusMode() {
772 return (mWindowAttributes.flags & WindowManager.LayoutParams.FLAG_LOCAL_FOCUS_MODE) != 0;
775 public int getWindowFlags() {
776 return mWindowAttributes.flags;
779 public int getDisplayId() {
780 return mDisplay.getDisplayId();
783 public CharSequence getTitle() {
784 return mWindowAttributes.getTitle();
787 void destroyHardwareResources() {
788 if (mAttachInfo.mHardwareRenderer != null) {
789 mAttachInfo.mHardwareRenderer.destroyHardwareResources(mView);
790 mAttachInfo.mHardwareRenderer.destroy();
794 public void detachFunctor(long functor) {
795 if (mAttachInfo.mHardwareRenderer != null) {
796 // Fence so that any pending invokeFunctor() messages will be processed
797 // before we return from detachFunctor.
798 mAttachInfo.mHardwareRenderer.stopDrawing();
803 * Schedules the functor for execution in either kModeProcess or
804 * kModeProcessNoContext, depending on whether or not there is an EGLContext.
806 * @param functor The native functor to invoke
807 * @param waitForCompletion If true, this will not return until the functor
808 * has invoked. If false, the functor may be invoked
811 public static void invokeFunctor(long functor, boolean waitForCompletion) {
812 ThreadedRenderer.invokeFunctor(functor, waitForCompletion);
815 public void registerAnimatingRenderNode(RenderNode animator) {
816 if (mAttachInfo.mHardwareRenderer != null) {
817 mAttachInfo.mHardwareRenderer.registerAnimatingRenderNode(animator);
819 if (mAttachInfo.mPendingAnimatingRenderNodes == null) {
820 mAttachInfo.mPendingAnimatingRenderNodes = new ArrayList<RenderNode>();
822 mAttachInfo.mPendingAnimatingRenderNodes.add(animator);
826 public void registerVectorDrawableAnimator(
827 AnimatedVectorDrawable.VectorDrawableAnimatorRT animator) {
828 if (mAttachInfo.mHardwareRenderer != null) {
829 mAttachInfo.mHardwareRenderer.registerVectorDrawableAnimator(animator);
833 private void enableHardwareAcceleration(WindowManager.LayoutParams attrs) {
834 mAttachInfo.mHardwareAccelerated = false;
835 mAttachInfo.mHardwareAccelerationRequested = false;
837 // Don't enable hardware acceleration when the application is in compatibility mode
838 if (mTranslator != null) return;
840 // Try to enable hardware acceleration if requested
841 final boolean hardwareAccelerated =
842 (attrs.flags & WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0;
844 if (hardwareAccelerated) {
845 if (!ThreadedRenderer.isAvailable()) {
849 // Persistent processes (including the system) should not do
850 // accelerated rendering on low-end devices. In that case,
851 // sRendererDisabled will be set. In addition, the system process
852 // itself should never do accelerated rendering. In that case, both
853 // sRendererDisabled and sSystemRendererDisabled are set. When
854 // sSystemRendererDisabled is set, PRIVATE_FLAG_FORCE_HARDWARE_ACCELERATED
855 // can be used by code on the system process to escape that and enable
856 // HW accelerated drawing. (This is basically for the lock screen.)
858 final boolean fakeHwAccelerated = (attrs.privateFlags &
859 WindowManager.LayoutParams.PRIVATE_FLAG_FAKE_HARDWARE_ACCELERATED) != 0;
860 final boolean forceHwAccelerated = (attrs.privateFlags &
861 WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_HARDWARE_ACCELERATED) != 0;
863 if (fakeHwAccelerated) {
864 // This is exclusively for the preview windows the window manager
865 // shows for launching applications, so they will look more like
866 // the app being launched.
867 mAttachInfo.mHardwareAccelerationRequested = true;
868 } else if (!ThreadedRenderer.sRendererDisabled
869 || (ThreadedRenderer.sSystemRendererDisabled && forceHwAccelerated)) {
870 if (mAttachInfo.mHardwareRenderer != null) {
871 mAttachInfo.mHardwareRenderer.destroy();
874 final Rect insets = attrs.surfaceInsets;
875 final boolean hasSurfaceInsets = insets.left != 0 || insets.right != 0
876 || insets.top != 0 || insets.bottom != 0;
877 final boolean translucent = attrs.format != PixelFormat.OPAQUE || hasSurfaceInsets;
878 mAttachInfo.mHardwareRenderer = ThreadedRenderer.create(mContext, translucent);
879 if (mAttachInfo.mHardwareRenderer != null) {
880 mAttachInfo.mHardwareRenderer.setName(attrs.getTitle().toString());
881 mAttachInfo.mHardwareAccelerated =
882 mAttachInfo.mHardwareAccelerationRequested = true;
888 public View getView() {
892 final WindowLeaked getLocation() {
896 void setLayoutParams(WindowManager.LayoutParams attrs, boolean newView) {
897 synchronized (this) {
898 final int oldInsetLeft = mWindowAttributes.surfaceInsets.left;
899 final int oldInsetTop = mWindowAttributes.surfaceInsets.top;
900 final int oldInsetRight = mWindowAttributes.surfaceInsets.right;
901 final int oldInsetBottom = mWindowAttributes.surfaceInsets.bottom;
902 final int oldSoftInputMode = mWindowAttributes.softInputMode;
903 final boolean oldHasManualSurfaceInsets = mWindowAttributes.hasManualSurfaceInsets;
905 if (DEBUG_KEEP_SCREEN_ON && (mClientWindowLayoutFlags
906 & WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) != 0
907 && (attrs.flags&WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) == 0) {
908 Slog.d(mTag, "setLayoutParams: FLAG_KEEP_SCREEN_ON from true to false!");
911 // Keep track of the actual window flags supplied by the client.
912 mClientWindowLayoutFlags = attrs.flags;
914 // Preserve compatible window flag if exists.
915 final int compatibleWindowFlag = mWindowAttributes.privateFlags
916 & WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW;
918 // Transfer over system UI visibility values as they carry current state.
919 attrs.systemUiVisibility = mWindowAttributes.systemUiVisibility;
920 attrs.subtreeSystemUiVisibility = mWindowAttributes.subtreeSystemUiVisibility;
922 mWindowAttributesChangesFlag = mWindowAttributes.copyFrom(attrs);
923 if ((mWindowAttributesChangesFlag
924 & WindowManager.LayoutParams.TRANSLUCENT_FLAGS_CHANGED) != 0) {
925 // Recompute system ui visibility.
926 mAttachInfo.mRecomputeGlobalAttributes = true;
928 if ((mWindowAttributesChangesFlag
929 & WindowManager.LayoutParams.LAYOUT_CHANGED) != 0) {
930 // Request to update light center.
931 mAttachInfo.mNeedsUpdateLightCenter = true;
933 if (mWindowAttributes.packageName == null) {
934 mWindowAttributes.packageName = mBasePackageName;
936 mWindowAttributes.privateFlags |= compatibleWindowFlag;
938 if (mWindowAttributes.preservePreviousSurfaceInsets) {
939 // Restore old surface insets.
940 mWindowAttributes.surfaceInsets.set(
941 oldInsetLeft, oldInsetTop, oldInsetRight, oldInsetBottom);
942 mWindowAttributes.hasManualSurfaceInsets = oldHasManualSurfaceInsets;
943 } else if (mWindowAttributes.surfaceInsets.left != oldInsetLeft
944 || mWindowAttributes.surfaceInsets.top != oldInsetTop
945 || mWindowAttributes.surfaceInsets.right != oldInsetRight
946 || mWindowAttributes.surfaceInsets.bottom != oldInsetBottom) {
947 mNeedsHwRendererSetup = true;
950 applyKeepScreenOnFlag(mWindowAttributes);
953 mSoftInputMode = attrs.softInputMode;
957 // Don't lose the mode we last auto-computed.
958 if ((attrs.softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
959 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED) {
960 mWindowAttributes.softInputMode = (mWindowAttributes.softInputMode
961 & ~WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
962 | (oldSoftInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST);
965 mWindowAttributesChanged = true;
966 scheduleTraversals();
970 void handleAppVisibility(boolean visible) {
971 if (mAppVisible != visible) {
972 mAppVisible = visible;
973 scheduleTraversals();
975 WindowManagerGlobal.trimForeground();
980 void handleGetNewSurface() {
981 mNewSurfaceNeeded = true;
982 mFullRedrawNeeded = true;
983 scheduleTraversals();
986 private final DisplayListener mDisplayListener = new DisplayListener() {
988 public void onDisplayChanged(int displayId) {
989 if (mView != null && mDisplay.getDisplayId() == displayId) {
990 final int oldDisplayState = mAttachInfo.mDisplayState;
991 final int newDisplayState = mDisplay.getState();
992 if (oldDisplayState != newDisplayState) {
993 mAttachInfo.mDisplayState = newDisplayState;
994 pokeDrawLockIfNeeded();
995 if (oldDisplayState != Display.STATE_UNKNOWN) {
996 final int oldScreenState = toViewScreenState(oldDisplayState);
997 final int newScreenState = toViewScreenState(newDisplayState);
998 if (oldScreenState != newScreenState) {
999 mView.dispatchScreenStateChanged(newScreenState);
1001 if (oldDisplayState == Display.STATE_OFF) {
1002 // Draw was suppressed so we need to for it to happen here.
1003 mFullRedrawNeeded = true;
1004 scheduleTraversals();
1012 public void onDisplayRemoved(int displayId) {
1016 public void onDisplayAdded(int displayId) {
1019 private int toViewScreenState(int displayState) {
1020 return displayState == Display.STATE_OFF ?
1021 View.SCREEN_STATE_OFF : View.SCREEN_STATE_ON;
1025 void pokeDrawLockIfNeeded() {
1026 final int displayState = mAttachInfo.mDisplayState;
1027 if (mView != null && mAdded && mTraversalScheduled
1028 && (displayState == Display.STATE_DOZE
1029 || displayState == Display.STATE_DOZE_SUSPEND)) {
1031 mWindowSession.pokeDrawLock(mWindow);
1032 } catch (RemoteException ex) {
1033 // System server died, oh well.
1039 public void requestFitSystemWindows() {
1041 mApplyInsetsRequested = true;
1042 scheduleTraversals();
1046 public void requestLayout() {
1047 if (!mHandlingLayoutInLayoutRequest) {
1049 mLayoutRequested = true;
1050 scheduleTraversals();
1055 public boolean isLayoutRequested() {
1056 return mLayoutRequested;
1060 mDirty.set(0, 0, mWidth, mHeight);
1061 if (!mWillDrawSoon) {
1062 scheduleTraversals();
1066 void invalidateWorld(View view) {
1068 if (view instanceof ViewGroup) {
1069 ViewGroup parent = (ViewGroup) view;
1070 for (int i = 0; i < parent.getChildCount(); i++) {
1071 invalidateWorld(parent.getChildAt(i));
1077 public void invalidateChild(View child, Rect dirty) {
1078 invalidateChildInParent(null, dirty);
1082 public ViewParent invalidateChildInParent(int[] location, Rect dirty) {
1084 if (DEBUG_DRAW) Log.v(mTag, "Invalidate child: " + dirty);
1086 if (dirty == null) {
1089 } else if (dirty.isEmpty() && !mIsAnimating) {
1093 if (mCurScrollY != 0 || mTranslator != null) {
1094 mTempRect.set(dirty);
1096 if (mCurScrollY != 0) {
1097 dirty.offset(0, -mCurScrollY);
1099 if (mTranslator != null) {
1100 mTranslator.translateRectInAppWindowToScreen(dirty);
1102 if (mAttachInfo.mScalingRequired) {
1103 dirty.inset(-1, -1);
1107 invalidateRectOnScreen(dirty);
1112 private void invalidateRectOnScreen(Rect dirty) {
1113 final Rect localDirty = mDirty;
1114 if (!localDirty.isEmpty() && !localDirty.contains(dirty)) {
1115 mAttachInfo.mSetIgnoreDirtyState = true;
1116 mAttachInfo.mIgnoreDirtyState = true;
1119 // Add the new dirty rect to the current one
1120 localDirty.union(dirty.left, dirty.top, dirty.right, dirty.bottom);
1121 // Intersect with the bounds of the window to skip
1122 // updates that lie outside of the visible region
1123 final float appScale = mAttachInfo.mApplicationScale;
1124 final boolean intersected = localDirty.intersect(0, 0,
1125 (int) (mWidth * appScale + 0.5f), (int) (mHeight * appScale + 0.5f));
1127 localDirty.setEmpty();
1129 if (!mWillDrawSoon && (intersected || mIsAnimating)) {
1130 scheduleTraversals();
1134 public void setIsAmbientMode(boolean ambient) {
1135 mIsAmbientMode = ambient;
1138 void setWindowStopped(boolean stopped) {
1139 if (mStopped != stopped) {
1141 final ThreadedRenderer renderer = mAttachInfo.mHardwareRenderer;
1142 if (renderer != null) {
1143 if (DEBUG_DRAW) Log.d(mTag, "WindowStopped on " + getTitle() + " set to " + mStopped);
1144 renderer.setStopped(mStopped);
1147 scheduleTraversals();
1149 if (renderer != null) {
1150 renderer.destroyHardwareResources(mView);
1157 * Block the input events during an Activity Transition. The KEYCODE_BACK event is allowed
1158 * through to allow quick reversal of the Activity Transition.
1160 * @param paused true to pause, false to resume.
1162 public void setPausedForTransition(boolean paused) {
1163 mPausedForTransition = paused;
1167 public ViewParent getParent() {
1172 public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) {
1173 if (child != mView) {
1174 throw new RuntimeException("child is not mine, honest!");
1176 // Note: don't apply scroll offset, because we want to know its
1177 // visibility in the virtual canvas being given to the view hierarchy.
1178 return r.intersect(0, 0, mWidth, mHeight);
1182 public void bringChildToFront(View child) {
1185 int getHostVisibility() {
1186 return (mAppVisible || mForceDecorViewVisibility) ? mView.getVisibility() : View.GONE;
1190 * Add LayoutTransition to the list of transitions to be started in the next traversal.
1191 * This list will be cleared after the transitions on the list are start()'ed. These
1192 * transitionsa re added by LayoutTransition itself when it sets up animations. The setup
1193 * happens during the layout phase of traversal, which we want to complete before any of the
1194 * animations are started (because those animations may side-effect properties that layout
1195 * depends upon, like the bounding rectangles of the affected views). So we add the transition
1196 * to the list and it is started just prior to starting the drawing phase of traversal.
1198 * @param transition The LayoutTransition to be started on the next traversal.
1202 public void requestTransitionStart(LayoutTransition transition) {
1203 if (mPendingTransitions == null || !mPendingTransitions.contains(transition)) {
1204 if (mPendingTransitions == null) {
1205 mPendingTransitions = new ArrayList<LayoutTransition>();
1207 mPendingTransitions.add(transition);
1212 * Notifies the HardwareRenderer that a new frame will be coming soon.
1213 * Currently only {@link ThreadedRenderer} cares about this, and uses
1214 * this knowledge to adjust the scheduling of off-thread animations
1216 void notifyRendererOfFramePending() {
1217 if (mAttachInfo.mHardwareRenderer != null) {
1218 mAttachInfo.mHardwareRenderer.notifyFramePending();
1222 void scheduleTraversals() {
1223 if (!mTraversalScheduled) {
1224 mTraversalScheduled = true;
1225 mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier();
1226 mChoreographer.postCallback(
1227 Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
1228 if (!mUnbufferedInputDispatch) {
1229 scheduleConsumeBatchedInput();
1231 notifyRendererOfFramePending();
1232 pokeDrawLockIfNeeded();
1236 void unscheduleTraversals() {
1237 if (mTraversalScheduled) {
1238 mTraversalScheduled = false;
1239 mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier);
1240 mChoreographer.removeCallbacks(
1241 Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
1245 void doTraversal() {
1246 if (mTraversalScheduled) {
1247 mTraversalScheduled = false;
1248 mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier);
1251 Debug.startMethodTracing("ViewAncestor");
1254 performTraversals();
1257 Debug.stopMethodTracing();
1263 private void applyKeepScreenOnFlag(WindowManager.LayoutParams params) {
1264 // Update window's global keep screen on flag: if a view has requested
1265 // that the screen be kept on, then it is always set; otherwise, it is
1266 // set to whatever the client last requested for the global state.
1267 if (mAttachInfo.mKeepScreenOn) {
1268 params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
1270 params.flags = (params.flags&~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
1271 | (mClientWindowLayoutFlags&WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1275 private boolean collectViewAttributes() {
1276 if (mAttachInfo.mRecomputeGlobalAttributes) {
1277 //Log.i(mTag, "Computing view hierarchy attributes!");
1278 mAttachInfo.mRecomputeGlobalAttributes = false;
1279 boolean oldScreenOn = mAttachInfo.mKeepScreenOn;
1280 mAttachInfo.mKeepScreenOn = false;
1281 mAttachInfo.mSystemUiVisibility = 0;
1282 mAttachInfo.mHasSystemUiListeners = false;
1283 mView.dispatchCollectViewAttributes(mAttachInfo, 0);
1284 mAttachInfo.mSystemUiVisibility &= ~mAttachInfo.mDisabledSystemUiVisibility;
1285 WindowManager.LayoutParams params = mWindowAttributes;
1286 mAttachInfo.mSystemUiVisibility |= getImpliedSystemUiVisibility(params);
1287 if (mAttachInfo.mKeepScreenOn != oldScreenOn
1288 || mAttachInfo.mSystemUiVisibility != params.subtreeSystemUiVisibility
1289 || mAttachInfo.mHasSystemUiListeners != params.hasSystemUiListeners) {
1290 applyKeepScreenOnFlag(params);
1291 params.subtreeSystemUiVisibility = mAttachInfo.mSystemUiVisibility;
1292 params.hasSystemUiListeners = mAttachInfo.mHasSystemUiListeners;
1293 mView.dispatchWindowSystemUiVisiblityChanged(mAttachInfo.mSystemUiVisibility);
1300 private int getImpliedSystemUiVisibility(WindowManager.LayoutParams params) {
1302 // Translucent decor window flags imply stable system ui visibility.
1303 if ((params.flags & WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) != 0) {
1304 vis |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
1306 if ((params.flags & WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION) != 0) {
1307 vis |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1312 private boolean measureHierarchy(final View host, final WindowManager.LayoutParams lp,
1313 final Resources res, final int desiredWindowWidth, final int desiredWindowHeight) {
1314 int childWidthMeasureSpec;
1315 int childHeightMeasureSpec;
1316 boolean windowSizeMayChange = false;
1318 if (DEBUG_ORIENTATION || DEBUG_LAYOUT) Log.v(mTag,
1319 "Measuring " + host + " in display " + desiredWindowWidth
1320 + "x" + desiredWindowHeight + "...");
1322 boolean goodMeasure = false;
1323 if (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT) {
1324 // On large screens, we don't want to allow dialogs to just
1325 // stretch to fill the entire width of the screen to display
1326 // one line of text. First try doing the layout at a smaller
1327 // size to see if it will fit.
1328 final DisplayMetrics packageMetrics = res.getDisplayMetrics();
1329 res.getValue(com.android.internal.R.dimen.config_prefDialogWidth, mTmpValue, true);
1331 if (mTmpValue.type == TypedValue.TYPE_DIMENSION) {
1332 baseSize = (int)mTmpValue.getDimension(packageMetrics);
1334 if (DEBUG_DIALOG) Log.v(mTag, "Window " + mView + ": baseSize=" + baseSize
1335 + ", desiredWindowWidth=" + desiredWindowWidth);
1336 if (baseSize != 0 && desiredWindowWidth > baseSize) {
1337 childWidthMeasureSpec = getRootMeasureSpec(baseSize, lp.width);
1338 childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
1339 performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
1340 if (DEBUG_DIALOG) Log.v(mTag, "Window " + mView + ": measured ("
1341 + host.getMeasuredWidth() + "," + host.getMeasuredHeight()
1342 + ") from width spec: " + MeasureSpec.toString(childWidthMeasureSpec)
1343 + " and height spec: " + MeasureSpec.toString(childHeightMeasureSpec));
1344 if ((host.getMeasuredWidthAndState()&View.MEASURED_STATE_TOO_SMALL) == 0) {
1347 // Didn't fit in that size... try expanding a bit.
1348 baseSize = (baseSize+desiredWindowWidth)/2;
1349 if (DEBUG_DIALOG) Log.v(mTag, "Window " + mView + ": next baseSize="
1351 childWidthMeasureSpec = getRootMeasureSpec(baseSize, lp.width);
1352 performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
1353 if (DEBUG_DIALOG) Log.v(mTag, "Window " + mView + ": measured ("
1354 + host.getMeasuredWidth() + "," + host.getMeasuredHeight() + ")");
1355 if ((host.getMeasuredWidthAndState()&View.MEASURED_STATE_TOO_SMALL) == 0) {
1356 if (DEBUG_DIALOG) Log.v(mTag, "Good!");
1364 childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width);
1365 childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
1366 performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
1367 if (mWidth != host.getMeasuredWidth() || mHeight != host.getMeasuredHeight()) {
1368 windowSizeMayChange = true;
1373 System.out.println("======================================");
1374 System.out.println("performTraversals -- after measure");
1378 return windowSizeMayChange;
1382 * Modifies the input matrix such that it maps view-local coordinates to
1383 * on-screen coordinates.
1385 * @param m input matrix to modify
1387 void transformMatrixToGlobal(Matrix m) {
1388 m.preTranslate(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
1392 * Modifies the input matrix such that it maps on-screen coordinates to
1393 * view-local coordinates.
1395 * @param m input matrix to modify
1397 void transformMatrixToLocal(Matrix m) {
1398 m.postTranslate(-mAttachInfo.mWindowLeft, -mAttachInfo.mWindowTop);
1401 /* package */ WindowInsets getWindowInsets(boolean forceConstruct) {
1402 if (mLastWindowInsets == null || forceConstruct) {
1403 mDispatchContentInsets.set(mAttachInfo.mContentInsets);
1404 mDispatchStableInsets.set(mAttachInfo.mStableInsets);
1405 Rect contentInsets = mDispatchContentInsets;
1406 Rect stableInsets = mDispatchStableInsets;
1407 // For dispatch we preserve old logic, but for direct requests from Views we allow to
1408 // immediately use pending insets.
1410 && (!mPendingContentInsets.equals(contentInsets) ||
1411 !mPendingStableInsets.equals(stableInsets))) {
1412 contentInsets = mPendingContentInsets;
1413 stableInsets = mPendingStableInsets;
1415 Rect outsets = mAttachInfo.mOutsets;
1416 if (outsets.left > 0 || outsets.top > 0 || outsets.right > 0 || outsets.bottom > 0) {
1417 contentInsets = new Rect(contentInsets.left + outsets.left,
1418 contentInsets.top + outsets.top, contentInsets.right + outsets.right,
1419 contentInsets.bottom + outsets.bottom);
1421 mLastWindowInsets = new WindowInsets(contentInsets,
1422 null /* windowDecorInsets */, stableInsets,
1423 mContext.getResources().getConfiguration().isScreenRound(),
1424 mAttachInfo.mAlwaysConsumeNavBar);
1426 return mLastWindowInsets;
1429 void dispatchApplyInsets(View host) {
1430 host.dispatchApplyWindowInsets(getWindowInsets(true /* forceConstruct */));
1433 private static boolean shouldUseDisplaySize(final WindowManager.LayoutParams lp) {
1434 return lp.type == TYPE_STATUS_BAR_PANEL
1435 || lp.type == TYPE_INPUT_METHOD
1436 || lp.type == TYPE_VOLUME_OVERLAY;
1439 private int dipToPx(int dip) {
1440 final DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
1441 return (int) (displayMetrics.density * dip + 0.5f);
1444 private void performTraversals() {
1445 // cache mView since it is used so much below...
1446 final View host = mView;
1449 System.out.println("======================================");
1450 System.out.println("performTraversals");
1454 if (host == null || !mAdded)
1457 mIsInTraversal = true;
1458 mWillDrawSoon = true;
1459 boolean windowSizeMayChange = false;
1460 boolean newSurface = false;
1461 boolean surfaceChanged = false;
1462 WindowManager.LayoutParams lp = mWindowAttributes;
1464 int desiredWindowWidth;
1465 int desiredWindowHeight;
1467 final int viewVisibility = getHostVisibility();
1468 final boolean viewVisibilityChanged = !mFirst
1469 && (mViewVisibility != viewVisibility || mNewSurfaceNeeded);
1471 WindowManager.LayoutParams params = null;
1472 if (mWindowAttributesChanged) {
1473 mWindowAttributesChanged = false;
1474 surfaceChanged = true;
1477 CompatibilityInfo compatibilityInfo =
1478 mDisplay.getDisplayAdjustments().getCompatibilityInfo();
1479 if (compatibilityInfo.supportsScreen() == mLastInCompatMode) {
1481 mFullRedrawNeeded = true;
1482 mLayoutRequested = true;
1483 if (mLastInCompatMode) {
1484 params.privateFlags &= ~WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW;
1485 mLastInCompatMode = false;
1487 params.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW;
1488 mLastInCompatMode = true;
1492 mWindowAttributesChangesFlag = 0;
1494 Rect frame = mWinFrame;
1496 mFullRedrawNeeded = true;
1497 mLayoutRequested = true;
1499 if (shouldUseDisplaySize(lp)) {
1500 // NOTE -- system code, won't try to do compat mode.
1501 Point size = new Point();
1502 mDisplay.getRealSize(size);
1503 desiredWindowWidth = size.x;
1504 desiredWindowHeight = size.y;
1506 Configuration config = mContext.getResources().getConfiguration();
1507 desiredWindowWidth = dipToPx(config.screenWidthDp);
1508 desiredWindowHeight = dipToPx(config.screenHeightDp);
1511 // We used to use the following condition to choose 32 bits drawing caches:
1512 // PixelFormat.hasAlpha(lp.format) || lp.format == PixelFormat.RGBX_8888
1513 // However, windows are now always 32 bits by default, so choose 32 bits
1514 mAttachInfo.mUse32BitDrawingCache = true;
1515 mAttachInfo.mHasWindowFocus = false;
1516 mAttachInfo.mWindowVisibility = viewVisibility;
1517 mAttachInfo.mRecomputeGlobalAttributes = false;
1518 mLastConfiguration.setTo(host.getResources().getConfiguration());
1519 mLastSystemUiVisibility = mAttachInfo.mSystemUiVisibility;
1520 // Set the layout direction if it has not been set before (inherit is the default)
1521 if (mViewLayoutDirectionInitial == View.LAYOUT_DIRECTION_INHERIT) {
1522 host.setLayoutDirection(mLastConfiguration.getLayoutDirection());
1524 host.dispatchAttachedToWindow(mAttachInfo, 0);
1525 mAttachInfo.mTreeObserver.dispatchOnWindowAttachedChange(true);
1526 dispatchApplyInsets(host);
1527 //Log.i(mTag, "Screen on initialized: " + attachInfo.mKeepScreenOn);
1530 desiredWindowWidth = frame.width();
1531 desiredWindowHeight = frame.height();
1532 if (desiredWindowWidth != mWidth || desiredWindowHeight != mHeight) {
1533 if (DEBUG_ORIENTATION) Log.v(mTag, "View " + host + " resized to: " + frame);
1534 mFullRedrawNeeded = true;
1535 mLayoutRequested = true;
1536 windowSizeMayChange = true;
1540 if (viewVisibilityChanged) {
1541 mAttachInfo.mWindowVisibility = viewVisibility;
1542 host.dispatchWindowVisibilityChanged(viewVisibility);
1543 host.dispatchVisibilityAggregated(viewVisibility == View.VISIBLE);
1544 if (viewVisibility != View.VISIBLE || mNewSurfaceNeeded) {
1546 destroyHardwareResources();
1548 if (viewVisibility == View.GONE) {
1549 // After making a window gone, we will count it as being
1550 // shown for the first time the next time it gets focus.
1551 mHasHadWindowFocus = false;
1555 // Non-visible windows can't hold accessibility focus.
1556 if (mAttachInfo.mWindowVisibility != View.VISIBLE) {
1557 host.clearAccessibilityFocus();
1560 // Execute enqueued actions on every traversal in case a detached view enqueued an action
1561 getRunQueue().executeActions(mAttachInfo.mHandler);
1563 boolean insetsChanged = false;
1565 boolean layoutRequested = mLayoutRequested && (!mStopped || mReportNextDraw);
1566 if (layoutRequested) {
1568 final Resources res = mView.getContext().getResources();
1571 // make sure touch mode code executes by setting cached value
1572 // to opposite of the added touch mode.
1573 mAttachInfo.mInTouchMode = !mAddedTouchMode;
1574 ensureTouchModeLocally(mAddedTouchMode);
1576 if (!mPendingOverscanInsets.equals(mAttachInfo.mOverscanInsets)) {
1577 insetsChanged = true;
1579 if (!mPendingContentInsets.equals(mAttachInfo.mContentInsets)) {
1580 insetsChanged = true;
1582 if (!mPendingStableInsets.equals(mAttachInfo.mStableInsets)) {
1583 insetsChanged = true;
1585 if (!mPendingVisibleInsets.equals(mAttachInfo.mVisibleInsets)) {
1586 mAttachInfo.mVisibleInsets.set(mPendingVisibleInsets);
1587 if (DEBUG_LAYOUT) Log.v(mTag, "Visible insets changing to: "
1588 + mAttachInfo.mVisibleInsets);
1590 if (!mPendingOutsets.equals(mAttachInfo.mOutsets)) {
1591 insetsChanged = true;
1593 if (mPendingAlwaysConsumeNavBar != mAttachInfo.mAlwaysConsumeNavBar) {
1594 insetsChanged = true;
1596 if (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT
1597 || lp.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
1598 windowSizeMayChange = true;
1600 if (shouldUseDisplaySize(lp)) {
1601 // NOTE -- system code, won't try to do compat mode.
1602 Point size = new Point();
1603 mDisplay.getRealSize(size);
1604 desiredWindowWidth = size.x;
1605 desiredWindowHeight = size.y;
1607 Configuration config = res.getConfiguration();
1608 desiredWindowWidth = dipToPx(config.screenWidthDp);
1609 desiredWindowHeight = dipToPx(config.screenHeightDp);
1614 // Ask host how big it wants to be
1615 windowSizeMayChange |= measureHierarchy(host, lp, res,
1616 desiredWindowWidth, desiredWindowHeight);
1619 if (collectViewAttributes()) {
1622 if (mAttachInfo.mForceReportNewAttributes) {
1623 mAttachInfo.mForceReportNewAttributes = false;
1627 if (mFirst || mAttachInfo.mViewVisibilityChanged) {
1628 mAttachInfo.mViewVisibilityChanged = false;
1629 int resizeMode = mSoftInputMode &
1630 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST;
1631 // If we are in auto resize mode, then we need to determine
1632 // what mode to use now.
1633 if (resizeMode == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED) {
1634 final int N = mAttachInfo.mScrollContainers.size();
1635 for (int i=0; i<N; i++) {
1636 if (mAttachInfo.mScrollContainers.get(i).isShown()) {
1637 resizeMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
1640 if (resizeMode == 0) {
1641 resizeMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;
1643 if ((lp.softInputMode &
1644 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) != resizeMode) {
1645 lp.softInputMode = (lp.softInputMode &
1646 ~WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) |
1653 if (params != null) {
1654 if ((host.mPrivateFlags & View.PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
1655 if (!PixelFormat.formatHasAlpha(params.format)) {
1656 params.format = PixelFormat.TRANSLUCENT;
1659 mAttachInfo.mOverscanRequested = (params.flags
1660 & WindowManager.LayoutParams.FLAG_LAYOUT_IN_OVERSCAN) != 0;
1663 if (mApplyInsetsRequested) {
1664 mApplyInsetsRequested = false;
1665 mLastOverscanRequested = mAttachInfo.mOverscanRequested;
1666 dispatchApplyInsets(host);
1667 if (mLayoutRequested) {
1668 // Short-circuit catching a new layout request here, so
1669 // we don't need to go through two layout passes when things
1670 // change due to fitting system windows, which can happen a lot.
1671 windowSizeMayChange |= measureHierarchy(host, lp,
1672 mView.getContext().getResources(),
1673 desiredWindowWidth, desiredWindowHeight);
1677 if (layoutRequested) {
1678 // Clear this now, so that if anything requests a layout in the
1679 // rest of this function we will catch it and re-run a full
1681 mLayoutRequested = false;
1684 boolean windowShouldResize = layoutRequested && windowSizeMayChange
1685 && ((mWidth != host.getMeasuredWidth() || mHeight != host.getMeasuredHeight())
1686 || (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT &&
1687 frame.width() < desiredWindowWidth && frame.width() != mWidth)
1688 || (lp.height == ViewGroup.LayoutParams.WRAP_CONTENT &&
1689 frame.height() < desiredWindowHeight && frame.height() != mHeight));
1690 windowShouldResize |= mDragResizing && mResizeMode == RESIZE_MODE_FREEFORM;
1692 // If the activity was just relaunched, it might have unfrozen the task bounds (while
1693 // relaunching), so we need to force a call into window manager to pick up the latest
1695 windowShouldResize |= mActivityRelaunched;
1697 // Determine whether to compute insets.
1698 // If there are no inset listeners remaining then we may still need to compute
1699 // insets in case the old insets were non-empty and must be reset.
1700 final boolean computesInternalInsets =
1701 mAttachInfo.mTreeObserver.hasComputeInternalInsetsListeners()
1702 || mAttachInfo.mHasNonEmptyGivenInternalInsets;
1704 boolean insetsPending = false;
1705 int relayoutResult = 0;
1706 boolean updatedConfiguration = false;
1708 final int surfaceGenerationId = mSurface.getGenerationId();
1710 final boolean isViewVisible = viewVisibility == View.VISIBLE;
1711 if (mFirst || windowShouldResize || insetsChanged ||
1712 viewVisibilityChanged || params != null || mForceNextWindowRelayout) {
1713 mForceNextWindowRelayout = false;
1715 if (isViewVisible) {
1716 // If this window is giving internal insets to the window
1717 // manager, and it is being added or changing its visibility,
1718 // then we want to first give the window manager "fake"
1719 // insets to cause it to effectively ignore the content of
1720 // the window during layout. This avoids it briefly causing
1721 // other windows to resize/move based on the raw frame of the
1722 // window, waiting until we can finish laying out this window
1723 // and get back to the window manager with the ultimately
1725 insetsPending = computesInternalInsets && (mFirst || viewVisibilityChanged);
1728 if (mSurfaceHolder != null) {
1729 mSurfaceHolder.mSurfaceLock.lock();
1730 mDrawingAllowed = true;
1733 boolean hwInitialized = false;
1734 boolean framesChanged = false;
1735 boolean hadSurface = mSurface.isValid();
1739 Log.i(mTag, "host=w:" + host.getMeasuredWidth() + ", h:" +
1740 host.getMeasuredHeight() + ", params=" + params);
1743 if (mAttachInfo.mHardwareRenderer != null) {
1744 // relayoutWindow may decide to destroy mSurface. As that decision
1745 // happens in WindowManager service, we need to be defensive here
1746 // and stop using the surface in case it gets destroyed.
1747 if (mAttachInfo.mHardwareRenderer.pauseSurface(mSurface)) {
1748 // Animations were running so we need to push a frame
1750 mDirty.set(0, 0, mWidth, mHeight);
1752 mChoreographer.mFrameInfo.addFlags(FrameInfo.FLAG_WINDOW_LAYOUT_CHANGED);
1754 relayoutResult = relayoutWindow(params, viewVisibility, insetsPending);
1756 if (DEBUG_LAYOUT) Log.v(mTag, "relayout: frame=" + frame.toShortString()
1757 + " overscan=" + mPendingOverscanInsets.toShortString()
1758 + " content=" + mPendingContentInsets.toShortString()
1759 + " visible=" + mPendingVisibleInsets.toShortString()
1760 + " visible=" + mPendingStableInsets.toShortString()
1761 + " outsets=" + mPendingOutsets.toShortString()
1762 + " surface=" + mSurface);
1764 if (mPendingConfiguration.seq != 0) {
1765 if (DEBUG_CONFIGURATION) Log.v(mTag, "Visible with new config: "
1766 + mPendingConfiguration);
1767 updateConfiguration(new Configuration(mPendingConfiguration), !mFirst);
1768 mPendingConfiguration.seq = 0;
1769 updatedConfiguration = true;
1772 final boolean overscanInsetsChanged = !mPendingOverscanInsets.equals(
1773 mAttachInfo.mOverscanInsets);
1774 boolean contentInsetsChanged = !mPendingContentInsets.equals(
1775 mAttachInfo.mContentInsets);
1776 final boolean visibleInsetsChanged = !mPendingVisibleInsets.equals(
1777 mAttachInfo.mVisibleInsets);
1778 final boolean stableInsetsChanged = !mPendingStableInsets.equals(
1779 mAttachInfo.mStableInsets);
1780 final boolean outsetsChanged = !mPendingOutsets.equals(mAttachInfo.mOutsets);
1781 final boolean surfaceSizeChanged = (relayoutResult
1782 & WindowManagerGlobal.RELAYOUT_RES_SURFACE_RESIZED) != 0;
1783 final boolean alwaysConsumeNavBarChanged =
1784 mPendingAlwaysConsumeNavBar != mAttachInfo.mAlwaysConsumeNavBar;
1785 if (contentInsetsChanged) {
1786 mAttachInfo.mContentInsets.set(mPendingContentInsets);
1787 if (DEBUG_LAYOUT) Log.v(mTag, "Content insets changing to: "
1788 + mAttachInfo.mContentInsets);
1790 if (overscanInsetsChanged) {
1791 mAttachInfo.mOverscanInsets.set(mPendingOverscanInsets);
1792 if (DEBUG_LAYOUT) Log.v(mTag, "Overscan insets changing to: "
1793 + mAttachInfo.mOverscanInsets);
1794 // Need to relayout with content insets.
1795 contentInsetsChanged = true;
1797 if (stableInsetsChanged) {
1798 mAttachInfo.mStableInsets.set(mPendingStableInsets);
1799 if (DEBUG_LAYOUT) Log.v(mTag, "Decor insets changing to: "
1800 + mAttachInfo.mStableInsets);
1801 // Need to relayout with content insets.
1802 contentInsetsChanged = true;
1804 if (alwaysConsumeNavBarChanged) {
1805 mAttachInfo.mAlwaysConsumeNavBar = mPendingAlwaysConsumeNavBar;
1806 contentInsetsChanged = true;
1808 if (contentInsetsChanged || mLastSystemUiVisibility !=
1809 mAttachInfo.mSystemUiVisibility || mApplyInsetsRequested
1810 || mLastOverscanRequested != mAttachInfo.mOverscanRequested
1811 || outsetsChanged) {
1812 mLastSystemUiVisibility = mAttachInfo.mSystemUiVisibility;
1813 mLastOverscanRequested = mAttachInfo.mOverscanRequested;
1814 mAttachInfo.mOutsets.set(mPendingOutsets);
1815 mApplyInsetsRequested = false;
1816 dispatchApplyInsets(host);
1818 if (visibleInsetsChanged) {
1819 mAttachInfo.mVisibleInsets.set(mPendingVisibleInsets);
1820 if (DEBUG_LAYOUT) Log.v(mTag, "Visible insets changing to: "
1821 + mAttachInfo.mVisibleInsets);
1824 // If any of the insets changed, do a forceLayout on the view so that the
1825 // measure cache is cleared. We might have a pending MSG_RESIZED_REPORT
1826 // that is supposed to take care of it, but since pending insets are
1827 // already modified here, it won't detect the frame change after this.
1828 framesChanged = overscanInsetsChanged
1829 || contentInsetsChanged
1830 || stableInsetsChanged
1831 || visibleInsetsChanged
1833 if (mAdded && mView != null && framesChanged) {
1838 if (mSurface.isValid()) {
1839 // If we are creating a new surface, then we need to
1840 // completely redraw it. Also, when we get to the
1841 // point of drawing it we will hold off and schedule
1842 // a new traversal instead. This is so we can tell the
1843 // window manager about all of the windows being displayed
1844 // before actually drawing them, so it can display then
1847 mFullRedrawNeeded = true;
1848 mPreviousTransparentRegion.setEmpty();
1850 // Only initialize up-front if transparent regions are not
1851 // requested, otherwise defer to see if the entire window
1852 // will be transparent
1853 if (mAttachInfo.mHardwareRenderer != null) {
1855 hwInitialized = mAttachInfo.mHardwareRenderer.initialize(
1857 if (hwInitialized && (host.mPrivateFlags
1858 & View.PFLAG_REQUEST_TRANSPARENT_REGIONS) == 0) {
1859 // Don't pre-allocate if transparent regions
1860 // are requested as they may not be needed
1861 mSurface.allocateBuffers();
1863 } catch (OutOfResourcesException e) {
1864 handleOutOfResourcesException(e);
1869 } else if (!mSurface.isValid()) {
1870 // If the surface has been removed, then reset the scroll
1872 if (mLastScrolledFocus != null) {
1873 mLastScrolledFocus.clear();
1875 mScrollY = mCurScrollY = 0;
1876 if (mView instanceof RootViewSurfaceTaker) {
1877 ((RootViewSurfaceTaker) mView).onRootViewScrollYChanged(mCurScrollY);
1879 if (mScroller != null) {
1880 mScroller.abortAnimation();
1882 // Our surface is gone
1883 if (mAttachInfo.mHardwareRenderer != null &&
1884 mAttachInfo.mHardwareRenderer.isEnabled()) {
1885 mAttachInfo.mHardwareRenderer.destroy();
1887 } else if ((surfaceGenerationId != mSurface.getGenerationId()
1888 || surfaceSizeChanged)
1889 && mSurfaceHolder == null
1890 && mAttachInfo.mHardwareRenderer != null) {
1891 mFullRedrawNeeded = true;
1893 // Need to do updateSurface (which leads to CanvasContext::setSurface and
1894 // re-create the EGLSurface) if either the Surface changed (as indicated by
1895 // generation id), or WindowManager changed the surface size. The latter is
1896 // because on some chips, changing the consumer side's BufferQueue size may
1897 // not take effect immediately unless we create a new EGLSurface.
1898 // Note that frame size change doesn't always imply surface size change (eg.
1899 // drag resizing uses fullscreen surface), need to check surfaceSizeChanged
1900 // flag from WindowManager.
1901 mAttachInfo.mHardwareRenderer.updateSurface(mSurface);
1902 } catch (OutOfResourcesException e) {
1903 handleOutOfResourcesException(e);
1908 final boolean freeformResizing = (relayoutResult
1909 & WindowManagerGlobal.RELAYOUT_RES_DRAG_RESIZING_FREEFORM) != 0;
1910 final boolean dockedResizing = (relayoutResult
1911 & WindowManagerGlobal.RELAYOUT_RES_DRAG_RESIZING_DOCKED) != 0;
1912 final boolean dragResizing = freeformResizing || dockedResizing;
1913 if (mDragResizing != dragResizing) {
1915 mResizeMode = freeformResizing
1916 ? RESIZE_MODE_FREEFORM
1917 : RESIZE_MODE_DOCKED_DIVIDER;
1918 startDragResizing(mPendingBackDropFrame,
1919 mWinFrame.equals(mPendingBackDropFrame), mPendingVisibleInsets,
1920 mPendingStableInsets, mResizeMode);
1922 // We shouldn't come here, but if we come we should end the resize.
1926 if (!USE_MT_RENDERER) {
1928 mCanvasOffsetX = mWinFrame.left;
1929 mCanvasOffsetY = mWinFrame.top;
1931 mCanvasOffsetX = mCanvasOffsetY = 0;
1934 } catch (RemoteException e) {
1937 if (DEBUG_ORIENTATION) Log.v(
1938 TAG, "Relayout returned: frame=" + frame + ", surface=" + mSurface);
1940 mAttachInfo.mWindowLeft = frame.left;
1941 mAttachInfo.mWindowTop = frame.top;
1943 // !!FIXME!! This next section handles the case where we did not get the
1944 // window size we asked for. We should avoid this by getting a maximum size from
1945 // the window session beforehand.
1946 if (mWidth != frame.width() || mHeight != frame.height()) {
1947 mWidth = frame.width();
1948 mHeight = frame.height();
1951 if (mSurfaceHolder != null) {
1952 // The app owns the surface; tell it about what is going on.
1953 if (mSurface.isValid()) {
1954 // XXX .copyFrom() doesn't work!
1955 //mSurfaceHolder.mSurface.copyFrom(mSurface);
1956 mSurfaceHolder.mSurface = mSurface;
1958 mSurfaceHolder.setSurfaceFrameSize(mWidth, mHeight);
1959 mSurfaceHolder.mSurfaceLock.unlock();
1960 if (mSurface.isValid()) {
1962 mSurfaceHolder.ungetCallbacks();
1965 mSurfaceHolderCallback.surfaceCreated(mSurfaceHolder);
1966 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1967 if (callbacks != null) {
1968 for (SurfaceHolder.Callback c : callbacks) {
1969 c.surfaceCreated(mSurfaceHolder);
1972 surfaceChanged = true;
1974 if (surfaceChanged || surfaceGenerationId != mSurface.getGenerationId()) {
1975 mSurfaceHolderCallback.surfaceChanged(mSurfaceHolder,
1976 lp.format, mWidth, mHeight);
1977 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1978 if (callbacks != null) {
1979 for (SurfaceHolder.Callback c : callbacks) {
1980 c.surfaceChanged(mSurfaceHolder, lp.format,
1985 mIsCreating = false;
1986 } else if (hadSurface) {
1987 mSurfaceHolder.ungetCallbacks();
1988 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1989 mSurfaceHolderCallback.surfaceDestroyed(mSurfaceHolder);
1990 if (callbacks != null) {
1991 for (SurfaceHolder.Callback c : callbacks) {
1992 c.surfaceDestroyed(mSurfaceHolder);
1995 mSurfaceHolder.mSurfaceLock.lock();
1997 mSurfaceHolder.mSurface = new Surface();
1999 mSurfaceHolder.mSurfaceLock.unlock();
2004 final ThreadedRenderer hardwareRenderer = mAttachInfo.mHardwareRenderer;
2005 if (hardwareRenderer != null && hardwareRenderer.isEnabled()) {
2007 || mWidth != hardwareRenderer.getWidth()
2008 || mHeight != hardwareRenderer.getHeight()
2009 || mNeedsHwRendererSetup) {
2010 hardwareRenderer.setup(mWidth, mHeight, mAttachInfo,
2011 mWindowAttributes.surfaceInsets);
2012 mNeedsHwRendererSetup = false;
2016 if (!mStopped || mReportNextDraw) {
2017 boolean focusChangedDueToTouchMode = ensureTouchModeLocally(
2018 (relayoutResult&WindowManagerGlobal.RELAYOUT_RES_IN_TOUCH_MODE) != 0);
2019 if (focusChangedDueToTouchMode || mWidth != host.getMeasuredWidth()
2020 || mHeight != host.getMeasuredHeight() || framesChanged ||
2021 updatedConfiguration) {
2022 int childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
2023 int childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
2025 if (DEBUG_LAYOUT) Log.v(mTag, "Ooops, something changed! mWidth="
2026 + mWidth + " measuredWidth=" + host.getMeasuredWidth()
2027 + " mHeight=" + mHeight
2028 + " measuredHeight=" + host.getMeasuredHeight()
2029 + " framesChanged=" + framesChanged);
2031 // Ask host how big it wants to be
2032 performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
2034 // Implementation of weights from WindowManager.LayoutParams
2035 // We just grow the dimensions as needed and re-measure if
2037 int width = host.getMeasuredWidth();
2038 int height = host.getMeasuredHeight();
2039 boolean measureAgain = false;
2041 if (lp.horizontalWeight > 0.0f) {
2042 width += (int) ((mWidth - width) * lp.horizontalWeight);
2043 childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width,
2044 MeasureSpec.EXACTLY);
2045 measureAgain = true;
2047 if (lp.verticalWeight > 0.0f) {
2048 height += (int) ((mHeight - height) * lp.verticalWeight);
2049 childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
2050 MeasureSpec.EXACTLY);
2051 measureAgain = true;
2055 if (DEBUG_LAYOUT) Log.v(mTag,
2056 "And hey let's measure once more: width=" + width
2057 + " height=" + height);
2058 performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
2061 layoutRequested = true;
2065 // Not the first pass and no window/insets/visibility change but the window
2066 // may have moved and we need check that and if so to update the left and right
2067 // in the attach info. We translate only the window frame since on window move
2068 // the window manager tells us only for the new frame but the insets are the
2069 // same and we do not want to translate them more than once.
2070 maybeHandleWindowMove(frame);
2073 final boolean didLayout = layoutRequested && (!mStopped || mReportNextDraw);
2074 boolean triggerGlobalLayoutListener = didLayout
2075 || mAttachInfo.mRecomputeGlobalAttributes;
2077 performLayout(lp, mWidth, mHeight);
2079 // By this point all views have been sized and positioned
2080 // We can compute the transparent area
2082 if ((host.mPrivateFlags & View.PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
2083 // start out transparent
2084 // TODO: AVOID THAT CALL BY CACHING THE RESULT?
2085 host.getLocationInWindow(mTmpLocation);
2086 mTransparentRegion.set(mTmpLocation[0], mTmpLocation[1],
2087 mTmpLocation[0] + host.mRight - host.mLeft,
2088 mTmpLocation[1] + host.mBottom - host.mTop);
2090 host.gatherTransparentRegion(mTransparentRegion);
2091 if (mTranslator != null) {
2092 mTranslator.translateRegionInWindowToScreen(mTransparentRegion);
2095 if (!mTransparentRegion.equals(mPreviousTransparentRegion)) {
2096 mPreviousTransparentRegion.set(mTransparentRegion);
2097 mFullRedrawNeeded = true;
2098 // reconfigure window manager
2100 mWindowSession.setTransparentRegion(mWindow, mTransparentRegion);
2101 } catch (RemoteException e) {
2107 System.out.println("======================================");
2108 System.out.println("performTraversals -- after setFrame");
2113 if (triggerGlobalLayoutListener) {
2114 mAttachInfo.mRecomputeGlobalAttributes = false;
2115 mAttachInfo.mTreeObserver.dispatchOnGlobalLayout();
2118 if (computesInternalInsets) {
2119 // Clear the original insets.
2120 final ViewTreeObserver.InternalInsetsInfo insets = mAttachInfo.mGivenInternalInsets;
2123 // Compute new insets in place.
2124 mAttachInfo.mTreeObserver.dispatchOnComputeInternalInsets(insets);
2125 mAttachInfo.mHasNonEmptyGivenInternalInsets = !insets.isEmpty();
2127 // Tell the window manager.
2128 if (insetsPending || !mLastGivenInsets.equals(insets)) {
2129 mLastGivenInsets.set(insets);
2131 // Translate insets to screen coordinates if needed.
2132 final Rect contentInsets;
2133 final Rect visibleInsets;
2134 final Region touchableRegion;
2135 if (mTranslator != null) {
2136 contentInsets = mTranslator.getTranslatedContentInsets(insets.contentInsets);
2137 visibleInsets = mTranslator.getTranslatedVisibleInsets(insets.visibleInsets);
2138 touchableRegion = mTranslator.getTranslatedTouchableArea(insets.touchableRegion);
2140 contentInsets = insets.contentInsets;
2141 visibleInsets = insets.visibleInsets;
2142 touchableRegion = insets.touchableRegion;
2146 mWindowSession.setInsets(mWindow, insets.mTouchableInsets,
2147 contentInsets, visibleInsets, touchableRegion);
2148 } catch (RemoteException e) {
2154 // handle first focus request
2155 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "First: mView.hasFocus()="
2156 + mView.hasFocus());
2157 if (mView != null) {
2158 if (!mView.hasFocus()) {
2159 mView.requestFocus(View.FOCUS_FORWARD);
2160 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "First: requested focused view="
2161 + mView.findFocus());
2163 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "First: existing focused view="
2164 + mView.findFocus());
2169 final boolean changedVisibility = (viewVisibilityChanged || mFirst) && isViewVisible;
2170 final boolean hasWindowFocus = mAttachInfo.mHasWindowFocus && isViewVisible;
2171 final boolean regainedFocus = hasWindowFocus && mLostWindowFocus;
2172 if (regainedFocus) {
2173 mLostWindowFocus = false;
2174 } else if (!hasWindowFocus && mHadWindowFocus) {
2175 mLostWindowFocus = true;
2178 if (changedVisibility || regainedFocus) {
2179 host.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2183 mWillDrawSoon = false;
2184 mNewSurfaceNeeded = false;
2185 mActivityRelaunched = false;
2186 mViewVisibility = viewVisibility;
2187 mHadWindowFocus = hasWindowFocus;
2189 if (hasWindowFocus && !isInLocalFocusMode()) {
2190 final boolean imTarget = WindowManager.LayoutParams
2191 .mayUseInputMethod(mWindowAttributes.flags);
2192 if (imTarget != mLastWasImTarget) {
2193 mLastWasImTarget = imTarget;
2194 InputMethodManager imm = InputMethodManager.peekInstance();
2195 if (imm != null && imTarget) {
2196 imm.onPreWindowFocus(mView, hasWindowFocus);
2197 imm.onPostWindowFocus(mView, mView.findFocus(),
2198 mWindowAttributes.softInputMode,
2199 !mHasHadWindowFocus, mWindowAttributes.flags);
2204 // Remember if we must report the next draw.
2205 if ((relayoutResult & WindowManagerGlobal.RELAYOUT_RES_FIRST_TIME) != 0) {
2206 mReportNextDraw = true;
2209 boolean cancelDraw = mAttachInfo.mTreeObserver.dispatchOnPreDraw() || !isViewVisible;
2211 if (!cancelDraw && !newSurface) {
2212 if (mPendingTransitions != null && mPendingTransitions.size() > 0) {
2213 for (int i = 0; i < mPendingTransitions.size(); ++i) {
2214 mPendingTransitions.get(i).startChangingAnimations();
2216 mPendingTransitions.clear();
2221 if (isViewVisible) {
2223 scheduleTraversals();
2224 } else if (mPendingTransitions != null && mPendingTransitions.size() > 0) {
2225 for (int i = 0; i < mPendingTransitions.size(); ++i) {
2226 mPendingTransitions.get(i).endChangingAnimations();
2228 mPendingTransitions.clear();
2232 mIsInTraversal = false;
2235 private void maybeHandleWindowMove(Rect frame) {
2237 // TODO: Well, we are checking whether the frame has changed similarly
2238 // to how this is done for the insets. This is however incorrect since
2239 // the insets and the frame are translated. For example, the old frame
2240 // was (1, 1 - 1, 1) and was translated to say (2, 2 - 2, 2), now the new
2241 // reported frame is (2, 2 - 2, 2) which implies no change but this is not
2242 // true since we are comparing a not translated value to a translated one.
2243 // This scenario is rare but we may want to fix that.
2245 final boolean windowMoved = mAttachInfo.mWindowLeft != frame.left
2246 || mAttachInfo.mWindowTop != frame.top;
2248 if (mTranslator != null) {
2249 mTranslator.translateRectInScreenToAppWinFrame(frame);
2251 mAttachInfo.mWindowLeft = frame.left;
2252 mAttachInfo.mWindowTop = frame.top;
2254 if (windowMoved || mAttachInfo.mNeedsUpdateLightCenter) {
2255 // Update the light position for the new offsets.
2256 if (mAttachInfo.mHardwareRenderer != null) {
2257 mAttachInfo.mHardwareRenderer.setLightCenter(mAttachInfo);
2259 mAttachInfo.mNeedsUpdateLightCenter = false;
2263 private void handleOutOfResourcesException(Surface.OutOfResourcesException e) {
2264 Log.e(mTag, "OutOfResourcesException initializing HW surface", e);
2266 if (!mWindowSession.outOfMemory(mWindow) &&
2267 Process.myUid() != Process.SYSTEM_UID) {
2268 Slog.w(mTag, "No processes killed for memory; killing self");
2269 Process.killProcess(Process.myPid());
2271 } catch (RemoteException ex) {
2273 mLayoutRequested = true; // ask wm for a new surface next time.
2276 private void performMeasure(int childWidthMeasureSpec, int childHeightMeasureSpec) {
2277 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "measure");
2279 mView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
2281 Trace.traceEnd(Trace.TRACE_TAG_VIEW);
2286 * Called by {@link android.view.View#isInLayout()} to determine whether the view hierarchy
2287 * is currently undergoing a layout pass.
2289 * @return whether the view hierarchy is currently undergoing a layout pass
2291 boolean isInLayout() {
2296 * Called by {@link android.view.View#requestLayout()} if the view hierarchy is currently
2297 * undergoing a layout pass. requestLayout() should not generally be called during layout,
2298 * unless the container hierarchy knows what it is doing (i.e., it is fine as long as
2299 * all children in that container hierarchy are measured and laid out at the end of the layout
2300 * pass for that container). If requestLayout() is called anyway, we handle it correctly
2301 * by registering all requesters during a frame as it proceeds. At the end of the frame,
2302 * we check all of those views to see if any still have pending layout requests, which
2303 * indicates that they were not correctly handled by their container hierarchy. If that is
2304 * the case, we clear all such flags in the tree, to remove the buggy flag state that leads
2305 * to blank containers, and force a second request/measure/layout pass in this frame. If
2306 * more requestLayout() calls are received during that second layout pass, we post those
2307 * requests to the next frame to avoid possible infinite loops.
2309 * <p>The return value from this method indicates whether the request should proceed
2310 * (if it is a request during the first layout pass) or should be skipped and posted to the
2311 * next frame (if it is a request during the second layout pass).</p>
2313 * @param view the view that requested the layout.
2315 * @return true if request should proceed, false otherwise.
2317 boolean requestLayoutDuringLayout(final View view) {
2318 if (view.mParent == null || view.mAttachInfo == null) {
2319 // Would not normally trigger another layout, so just let it pass through as usual
2322 if (!mLayoutRequesters.contains(view)) {
2323 mLayoutRequesters.add(view);
2325 if (!mHandlingLayoutInLayoutRequest) {
2326 // Let the request proceed normally; it will be processed in a second layout pass
2330 // Don't let the request proceed during the second layout pass.
2331 // It will post to the next frame instead.
2336 private void performLayout(WindowManager.LayoutParams lp, int desiredWindowWidth,
2337 int desiredWindowHeight) {
2338 mLayoutRequested = false;
2339 mScrollMayChange = true;
2342 final View host = mView;
2343 if (DEBUG_ORIENTATION || DEBUG_LAYOUT) {
2344 Log.v(mTag, "Laying out " + host + " to (" +
2345 host.getMeasuredWidth() + ", " + host.getMeasuredHeight() + ")");
2348 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "layout");
2350 host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());
2353 int numViewsRequestingLayout = mLayoutRequesters.size();
2354 if (numViewsRequestingLayout > 0) {
2355 // requestLayout() was called during layout.
2356 // If no layout-request flags are set on the requesting views, there is no problem.
2357 // If some requests are still pending, then we need to clear those flags and do
2358 // a full request/measure/layout pass to handle this situation.
2359 ArrayList<View> validLayoutRequesters = getValidLayoutRequesters(mLayoutRequesters,
2361 if (validLayoutRequesters != null) {
2362 // Set this flag to indicate that any further requests are happening during
2363 // the second pass, which may result in posting those requests to the next
2365 mHandlingLayoutInLayoutRequest = true;
2367 // Process fresh layout requests, then measure and layout
2368 int numValidRequests = validLayoutRequesters.size();
2369 for (int i = 0; i < numValidRequests; ++i) {
2370 final View view = validLayoutRequesters.get(i);
2371 Log.w("View", "requestLayout() improperly called by " + view +
2372 " during layout: running second layout pass");
2373 view.requestLayout();
2375 measureHierarchy(host, lp, mView.getContext().getResources(),
2376 desiredWindowWidth, desiredWindowHeight);
2378 host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());
2380 mHandlingLayoutInLayoutRequest = false;
2382 // Check the valid requests again, this time without checking/clearing the
2383 // layout flags, since requests happening during the second pass get noop'd
2384 validLayoutRequesters = getValidLayoutRequesters(mLayoutRequesters, true);
2385 if (validLayoutRequesters != null) {
2386 final ArrayList<View> finalRequesters = validLayoutRequesters;
2387 // Post second-pass requests to the next frame
2388 getRunQueue().post(new Runnable() {
2391 int numValidRequests = finalRequesters.size();
2392 for (int i = 0; i < numValidRequests; ++i) {
2393 final View view = finalRequesters.get(i);
2394 Log.w("View", "requestLayout() improperly called by " + view +
2395 " during second layout pass: posting in next frame");
2396 view.requestLayout();
2405 Trace.traceEnd(Trace.TRACE_TAG_VIEW);
2411 * This method is called during layout when there have been calls to requestLayout() during
2412 * layout. It walks through the list of views that requested layout to determine which ones
2413 * still need it, based on visibility in the hierarchy and whether they have already been
2414 * handled (as is usually the case with ListView children).
2416 * @param layoutRequesters The list of views that requested layout during layout
2417 * @param secondLayoutRequests Whether the requests were issued during the second layout pass.
2418 * If so, the FORCE_LAYOUT flag was not set on requesters.
2419 * @return A list of the actual views that still need to be laid out.
2421 private ArrayList<View> getValidLayoutRequesters(ArrayList<View> layoutRequesters,
2422 boolean secondLayoutRequests) {
2424 int numViewsRequestingLayout = layoutRequesters.size();
2425 ArrayList<View> validLayoutRequesters = null;
2426 for (int i = 0; i < numViewsRequestingLayout; ++i) {
2427 View view = layoutRequesters.get(i);
2428 if (view != null && view.mAttachInfo != null && view.mParent != null &&
2429 (secondLayoutRequests || (view.mPrivateFlags & View.PFLAG_FORCE_LAYOUT) ==
2430 View.PFLAG_FORCE_LAYOUT)) {
2431 boolean gone = false;
2433 // Only trigger new requests for views in a non-GONE hierarchy
2434 while (parent != null) {
2435 if ((parent.mViewFlags & View.VISIBILITY_MASK) == View.GONE) {
2439 if (parent.mParent instanceof View) {
2440 parent = (View) parent.mParent;
2446 if (validLayoutRequesters == null) {
2447 validLayoutRequesters = new ArrayList<View>();
2449 validLayoutRequesters.add(view);
2453 if (!secondLayoutRequests) {
2454 // If we're checking the layout flags, then we need to clean them up also
2455 for (int i = 0; i < numViewsRequestingLayout; ++i) {
2456 View view = layoutRequesters.get(i);
2457 while (view != null &&
2458 (view.mPrivateFlags & View.PFLAG_FORCE_LAYOUT) != 0) {
2459 view.mPrivateFlags &= ~View.PFLAG_FORCE_LAYOUT;
2460 if (view.mParent instanceof View) {
2461 view = (View) view.mParent;
2468 layoutRequesters.clear();
2469 return validLayoutRequesters;
2473 public void requestTransparentRegion(View child) {
2474 // the test below should not fail unless someone is messing with us
2476 if (mView == child) {
2477 mView.mPrivateFlags |= View.PFLAG_REQUEST_TRANSPARENT_REGIONS;
2478 // Need to make sure we re-evaluate the window attributes next
2479 // time around, to ensure the window has the correct format.
2480 mWindowAttributesChanged = true;
2481 mWindowAttributesChangesFlag = 0;
2487 * Figures out the measure spec for the root view in a window based on it's
2491 * The available width or height of the window
2493 * @param rootDimension
2494 * The layout params for one dimension (width or height) of the
2497 * @return The measure spec to use to measure the root view.
2499 private static int getRootMeasureSpec(int windowSize, int rootDimension) {
2501 switch (rootDimension) {
2503 case ViewGroup.LayoutParams.MATCH_PARENT:
2504 // Window can't resize. Force root view to be windowSize.
2505 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY);
2507 case ViewGroup.LayoutParams.WRAP_CONTENT:
2508 // Window can resize. Set max size for root view.
2509 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST);
2512 // Window wants to be an exact size. Force root view to be that size.
2513 measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);
2519 int mHardwareXOffset;
2520 int mHardwareYOffset;
2523 public void onHardwarePreDraw(DisplayListCanvas canvas) {
2524 canvas.translate(-mHardwareXOffset, -mHardwareYOffset);
2528 public void onHardwarePostDraw(DisplayListCanvas canvas) {
2529 drawAccessibilityFocusedDrawableIfNeeded(canvas);
2530 for (int i = mWindowCallbacks.size() - 1; i >= 0; i--) {
2531 mWindowCallbacks.get(i).onPostDraw(canvas);
2538 void outputDisplayList(View view) {
2539 view.mRenderNode.output();
2540 if (mAttachInfo.mHardwareRenderer != null) {
2541 ((ThreadedRenderer)mAttachInfo.mHardwareRenderer).serializeDisplayListTree();
2546 * @see #PROPERTY_PROFILE_RENDERING
2548 private void profileRendering(boolean enabled) {
2549 if (mProfileRendering) {
2550 mRenderProfilingEnabled = enabled;
2552 if (mRenderProfiler != null) {
2553 mChoreographer.removeFrameCallback(mRenderProfiler);
2555 if (mRenderProfilingEnabled) {
2556 if (mRenderProfiler == null) {
2557 mRenderProfiler = new Choreographer.FrameCallback() {
2559 public void doFrame(long frameTimeNanos) {
2560 mDirty.set(0, 0, mWidth, mHeight);
2561 scheduleTraversals();
2562 if (mRenderProfilingEnabled) {
2563 mChoreographer.postFrameCallback(mRenderProfiler);
2568 mChoreographer.postFrameCallback(mRenderProfiler);
2570 mRenderProfiler = null;
2576 * Called from draw() when DEBUG_FPS is enabled
2578 private void trackFPS() {
2579 // Tracks frames per second drawn. First value in a series of draws may be bogus
2580 // because it down not account for the intervening idle time
2581 long nowTime = System.currentTimeMillis();
2582 if (mFpsStartTime < 0) {
2583 mFpsStartTime = mFpsPrevTime = nowTime;
2587 String thisHash = Integer.toHexString(System.identityHashCode(this));
2588 long frameTime = nowTime - mFpsPrevTime;
2589 long totalTime = nowTime - mFpsStartTime;
2590 Log.v(mTag, "0x" + thisHash + "\tFrame time:\t" + frameTime);
2591 mFpsPrevTime = nowTime;
2592 if (totalTime > 1000) {
2593 float fps = (float) mFpsNumFrames * 1000 / totalTime;
2594 Log.v(mTag, "0x" + thisHash + "\tFPS:\t" + fps);
2595 mFpsStartTime = nowTime;
2601 private void performDraw() {
2602 if (mAttachInfo.mDisplayState == Display.STATE_OFF && !mReportNextDraw) {
2606 final boolean fullRedrawNeeded = mFullRedrawNeeded;
2607 mFullRedrawNeeded = false;
2610 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "draw");
2612 draw(fullRedrawNeeded);
2615 Trace.traceEnd(Trace.TRACE_TAG_VIEW);
2618 // For whatever reason we didn't create a HardwareRenderer, end any
2619 // hardware animations that are now dangling
2620 if (mAttachInfo.mPendingAnimatingRenderNodes != null) {
2621 final int count = mAttachInfo.mPendingAnimatingRenderNodes.size();
2622 for (int i = 0; i < count; i++) {
2623 mAttachInfo.mPendingAnimatingRenderNodes.get(i).endAllAnimators();
2625 mAttachInfo.mPendingAnimatingRenderNodes.clear();
2628 if (mReportNextDraw) {
2629 mReportNextDraw = false;
2631 // if we're using multi-thread renderer, wait for the window frame draws
2632 if (mWindowDrawCountDown != null) {
2634 mWindowDrawCountDown.await();
2635 } catch (InterruptedException e) {
2636 Log.e(mTag, "Window redraw count down interruped!");
2638 mWindowDrawCountDown = null;
2641 if (mAttachInfo.mHardwareRenderer != null) {
2642 mAttachInfo.mHardwareRenderer.fence();
2643 mAttachInfo.mHardwareRenderer.setStopped(mStopped);
2647 Log.v(mTag, "FINISHED DRAWING: " + mWindowAttributes.getTitle());
2649 if (mSurfaceHolder != null && mSurface.isValid()) {
2650 mSurfaceHolderCallback.surfaceRedrawNeeded(mSurfaceHolder);
2651 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
2652 if (callbacks != null) {
2653 for (SurfaceHolder.Callback c : callbacks) {
2654 if (c instanceof SurfaceHolder.Callback2) {
2655 ((SurfaceHolder.Callback2)c).surfaceRedrawNeeded(mSurfaceHolder);
2661 mWindowSession.finishDrawing(mWindow);
2662 } catch (RemoteException e) {
2667 private void draw(boolean fullRedrawNeeded) {
2668 Surface surface = mSurface;
2669 if (!surface.isValid()) {
2677 if (!sFirstDrawComplete) {
2678 synchronized (sFirstDrawHandlers) {
2679 sFirstDrawComplete = true;
2680 final int count = sFirstDrawHandlers.size();
2681 for (int i = 0; i< count; i++) {
2682 mHandler.post(sFirstDrawHandlers.get(i));
2687 scrollToRectOrFocus(null, false);
2689 if (mAttachInfo.mViewScrollChanged) {
2690 mAttachInfo.mViewScrollChanged = false;
2691 mAttachInfo.mTreeObserver.dispatchOnScrollChanged();
2694 boolean animating = mScroller != null && mScroller.computeScrollOffset();
2695 final int curScrollY;
2697 curScrollY = mScroller.getCurrY();
2699 curScrollY = mScrollY;
2701 if (mCurScrollY != curScrollY) {
2702 mCurScrollY = curScrollY;
2703 fullRedrawNeeded = true;
2704 if (mView instanceof RootViewSurfaceTaker) {
2705 ((RootViewSurfaceTaker) mView).onRootViewScrollYChanged(mCurScrollY);
2709 final float appScale = mAttachInfo.mApplicationScale;
2710 final boolean scalingRequired = mAttachInfo.mScalingRequired;
2712 int resizeAlpha = 0;
2714 final Rect dirty = mDirty;
2715 if (mSurfaceHolder != null) {
2716 // The app owns the surface, we won't draw.
2718 if (animating && mScroller != null) {
2719 mScroller.abortAnimation();
2724 if (fullRedrawNeeded) {
2725 mAttachInfo.mIgnoreDirtyState = true;
2726 dirty.set(0, 0, (int) (mWidth * appScale + 0.5f), (int) (mHeight * appScale + 0.5f));
2729 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
2730 Log.v(mTag, "Draw " + mView + "/"
2731 + mWindowAttributes.getTitle()
2732 + ": dirty={" + dirty.left + "," + dirty.top
2733 + "," + dirty.right + "," + dirty.bottom + "} surface="
2734 + surface + " surface.isValid()=" + surface.isValid() + ", appScale:" +
2735 appScale + ", width=" + mWidth + ", height=" + mHeight);
2738 mAttachInfo.mTreeObserver.dispatchOnDraw();
2740 int xOffset = -mCanvasOffsetX;
2741 int yOffset = -mCanvasOffsetY + curScrollY;
2742 final WindowManager.LayoutParams params = mWindowAttributes;
2743 final Rect surfaceInsets = params != null ? params.surfaceInsets : null;
2744 if (surfaceInsets != null) {
2745 xOffset -= surfaceInsets.left;
2746 yOffset -= surfaceInsets.top;
2748 // Offset dirty rect for surface insets.
2749 dirty.offset(surfaceInsets.left, surfaceInsets.right);
2752 boolean accessibilityFocusDirty = false;
2753 final Drawable drawable = mAttachInfo.mAccessibilityFocusDrawable;
2754 if (drawable != null) {
2755 final Rect bounds = mAttachInfo.mTmpInvalRect;
2756 final boolean hasFocus = getAccessibilityFocusedRect(bounds);
2760 if (!bounds.equals(drawable.getBounds())) {
2761 accessibilityFocusDirty = true;
2765 mAttachInfo.mDrawingTime =
2766 mChoreographer.getFrameTimeNanos() / TimeUtils.NANOS_PER_MS;
2768 if (!dirty.isEmpty() || mIsAnimating || accessibilityFocusDirty) {
2769 if (mAttachInfo.mHardwareRenderer != null && mAttachInfo.mHardwareRenderer.isEnabled()) {
2770 // If accessibility focus moved, always invalidate the root.
2771 boolean invalidateRoot = accessibilityFocusDirty || mInvalidateRootRequested;
2772 mInvalidateRootRequested = false;
2774 // Draw with hardware renderer.
2775 mIsAnimating = false;
2777 if (mHardwareYOffset != yOffset || mHardwareXOffset != xOffset) {
2778 mHardwareYOffset = yOffset;
2779 mHardwareXOffset = xOffset;
2780 invalidateRoot = true;
2783 if (invalidateRoot) {
2784 mAttachInfo.mHardwareRenderer.invalidateRoot();
2789 // Stage the content drawn size now. It will be transferred to the renderer
2790 // shortly before the draw commands get send to the renderer.
2791 final boolean updated = updateContentDrawBounds();
2793 if (mReportNextDraw) {
2794 // report next draw overrides setStopped()
2795 // This value is re-sync'd to the value of mStopped
2796 // in the handling of mReportNextDraw post-draw.
2797 mAttachInfo.mHardwareRenderer.setStopped(false);
2801 requestDrawWindow();
2804 mAttachInfo.mHardwareRenderer.draw(mView, mAttachInfo, this);
2806 // If we get here with a disabled & requested hardware renderer, something went
2807 // wrong (an invalidate posted right before we destroyed the hardware surface
2808 // for instance) so we should just bail out. Locking the surface with software
2809 // rendering at this point would lock it forever and prevent hardware renderer
2810 // from doing its job when it comes back.
2811 // Before we request a new frame we must however attempt to reinitiliaze the
2812 // hardware renderer if it's in requested state. This would happen after an
2813 // eglTerminate() for instance.
2814 if (mAttachInfo.mHardwareRenderer != null &&
2815 !mAttachInfo.mHardwareRenderer.isEnabled() &&
2816 mAttachInfo.mHardwareRenderer.isRequested()) {
2819 mAttachInfo.mHardwareRenderer.initializeIfNeeded(
2820 mWidth, mHeight, mAttachInfo, mSurface, surfaceInsets);
2821 } catch (OutOfResourcesException e) {
2822 handleOutOfResourcesException(e);
2826 mFullRedrawNeeded = true;
2827 scheduleTraversals();
2831 if (!drawSoftware(surface, mAttachInfo, xOffset, yOffset, scalingRequired, dirty)) {
2838 mFullRedrawNeeded = true;
2839 scheduleTraversals();
2844 * @return true if drawing was successful, false if an error occurred
2846 private boolean drawSoftware(Surface surface, AttachInfo attachInfo, int xoff, int yoff,
2847 boolean scalingRequired, Rect dirty) {
2849 // Draw with software renderer.
2850 final Canvas canvas;
2852 final int left = dirty.left;
2853 final int top = dirty.top;
2854 final int right = dirty.right;
2855 final int bottom = dirty.bottom;
2857 canvas = mSurface.lockCanvas(dirty);
2859 // The dirty rectangle can be modified by Surface.lockCanvas()
2860 //noinspection ConstantConditions
2861 if (left != dirty.left || top != dirty.top || right != dirty.right
2862 || bottom != dirty.bottom) {
2863 attachInfo.mIgnoreDirtyState = true;
2866 // TODO: Do this in native
2867 canvas.setDensity(mDensity);
2868 } catch (Surface.OutOfResourcesException e) {
2869 handleOutOfResourcesException(e);
2871 } catch (IllegalArgumentException e) {
2872 Log.e(mTag, "Could not lock surface", e);
2873 // Don't assume this is due to out of memory, it could be
2874 // something else, and if it is something else then we could
2875 // kill stuff (or ourself) for no reason.
2876 mLayoutRequested = true; // ask wm for a new surface next time.
2881 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
2882 Log.v(mTag, "Surface " + surface + " drawing to bitmap w="
2883 + canvas.getWidth() + ", h=" + canvas.getHeight());
2884 //canvas.drawARGB(255, 255, 0, 0);
2887 // If this bitmap's format includes an alpha channel, we
2888 // need to clear it before drawing so that the child will
2889 // properly re-composite its drawing on a transparent
2890 // background. This automatically respects the clip/dirty region
2892 // If we are applying an offset, we need to clear the area
2893 // where the offset doesn't appear to avoid having garbage
2894 // left in the blank areas.
2895 if (!canvas.isOpaque() || yoff != 0 || xoff != 0) {
2896 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
2900 mIsAnimating = false;
2901 mView.mPrivateFlags |= View.PFLAG_DRAWN;
2904 Context cxt = mView.getContext();
2905 Log.i(mTag, "Drawing: package:" + cxt.getPackageName() +
2906 ", metrics=" + cxt.getResources().getDisplayMetrics() +
2907 ", compatibilityInfo=" + cxt.getResources().getCompatibilityInfo());
2910 canvas.translate(-xoff, -yoff);
2911 if (mTranslator != null) {
2912 mTranslator.translateCanvas(canvas);
2914 canvas.setScreenDensity(scalingRequired ? mNoncompatDensity : 0);
2915 attachInfo.mSetIgnoreDirtyState = false;
2919 drawAccessibilityFocusedDrawableIfNeeded(canvas);
2921 if (!attachInfo.mSetIgnoreDirtyState) {
2922 // Only clear the flag if it was not set during the mView.draw() call
2923 attachInfo.mIgnoreDirtyState = false;
2928 surface.unlockCanvasAndPost(canvas);
2929 } catch (IllegalArgumentException e) {
2930 Log.e(mTag, "Could not unlock surface", e);
2931 mLayoutRequested = true; // ask wm for a new surface next time.
2932 //noinspection ReturnInsideFinallyBlock
2937 Log.v(mTag, "Surface " + surface + " unlockCanvasAndPost");
2944 * We want to draw a highlight around the current accessibility focused.
2945 * Since adding a style for all possible view is not a viable option we
2946 * have this specialized drawing method.
2948 * Note: We are doing this here to be able to draw the highlight for
2949 * virtual views in addition to real ones.
2951 * @param canvas The canvas on which to draw.
2953 private void drawAccessibilityFocusedDrawableIfNeeded(Canvas canvas) {
2954 final Rect bounds = mAttachInfo.mTmpInvalRect;
2955 if (getAccessibilityFocusedRect(bounds)) {
2956 final Drawable drawable = getAccessibilityFocusedDrawable();
2957 if (drawable != null) {
2958 drawable.setBounds(bounds);
2959 drawable.draw(canvas);
2961 } else if (mAttachInfo.mAccessibilityFocusDrawable != null) {
2962 mAttachInfo.mAccessibilityFocusDrawable.setBounds(0, 0, 0, 0);
2966 private boolean getAccessibilityFocusedRect(Rect bounds) {
2967 final AccessibilityManager manager = AccessibilityManager.getInstance(mView.mContext);
2968 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
2972 final View host = mAccessibilityFocusedHost;
2973 if (host == null || host.mAttachInfo == null) {
2977 final AccessibilityNodeProvider provider = host.getAccessibilityNodeProvider();
2978 if (provider == null) {
2979 host.getBoundsOnScreen(bounds, true);
2980 } else if (mAccessibilityFocusedVirtualView != null) {
2981 mAccessibilityFocusedVirtualView.getBoundsInScreen(bounds);
2986 // Transform the rect into window-relative coordinates.
2987 final AttachInfo attachInfo = mAttachInfo;
2988 bounds.offset(0, attachInfo.mViewRootImpl.mScrollY);
2989 bounds.offset(-attachInfo.mWindowLeft, -attachInfo.mWindowTop);
2990 if (!bounds.intersect(0, 0, attachInfo.mViewRootImpl.mWidth,
2991 attachInfo.mViewRootImpl.mHeight)) {
2992 // If no intersection, set bounds to empty.
2995 return !bounds.isEmpty();
2998 private Drawable getAccessibilityFocusedDrawable() {
2999 // Lazily load the accessibility focus drawable.
3000 if (mAttachInfo.mAccessibilityFocusDrawable == null) {
3001 final TypedValue value = new TypedValue();
3002 final boolean resolved = mView.mContext.getTheme().resolveAttribute(
3003 R.attr.accessibilityFocusedDrawable, value, true);
3005 mAttachInfo.mAccessibilityFocusDrawable =
3006 mView.mContext.getDrawable(value.resourceId);
3009 return mAttachInfo.mAccessibilityFocusDrawable;
3013 * Requests that the root render node is invalidated next time we perform a draw, such that
3014 * {@link WindowCallbacks#onPostDraw} gets called.
3016 public void requestInvalidateRootRenderNode() {
3017 mInvalidateRootRequested = true;
3020 boolean scrollToRectOrFocus(Rect rectangle, boolean immediate) {
3021 final Rect ci = mAttachInfo.mContentInsets;
3022 final Rect vi = mAttachInfo.mVisibleInsets;
3024 boolean handled = false;
3026 if (vi.left > ci.left || vi.top > ci.top
3027 || vi.right > ci.right || vi.bottom > ci.bottom) {
3028 // We'll assume that we aren't going to change the scroll
3029 // offset, since we want to avoid that unless it is actually
3030 // going to make the focus visible... otherwise we scroll
3031 // all over the place.
3033 // We can be called for two different situations: during a draw,
3034 // to update the scroll position if the focus has changed (in which
3035 // case 'rectangle' is null), or in response to a
3036 // requestChildRectangleOnScreen() call (in which case 'rectangle'
3037 // is non-null and we just want to scroll to whatever that
3039 final View focus = mView.findFocus();
3040 if (focus == null) {
3043 View lastScrolledFocus = (mLastScrolledFocus != null) ? mLastScrolledFocus.get() : null;
3044 if (focus != lastScrolledFocus) {
3045 // If the focus has changed, then ignore any requests to scroll
3046 // to a rectangle; first we want to make sure the entire focus
3050 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "Eval scroll: focus=" + focus
3051 + " rectangle=" + rectangle + " ci=" + ci
3053 if (focus == lastScrolledFocus && !mScrollMayChange && rectangle == null) {
3054 // Optimization: if the focus hasn't changed since last
3055 // time, and no layout has happened, then just leave things
3057 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "Keeping scroll y="
3058 + mScrollY + " vi=" + vi.toShortString());
3060 // We need to determine if the currently focused view is
3061 // within the visible part of the window and, if not, apply
3062 // a pan so it can be seen.
3063 mLastScrolledFocus = new WeakReference<View>(focus);
3064 mScrollMayChange = false;
3065 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "Need to scroll?");
3066 // Try to find the rectangle from the focus view.
3067 if (focus.getGlobalVisibleRect(mVisRect, null)) {
3068 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "Root w="
3069 + mView.getWidth() + " h=" + mView.getHeight()
3070 + " ci=" + ci.toShortString()
3071 + " vi=" + vi.toShortString());
3072 if (rectangle == null) {
3073 focus.getFocusedRect(mTempRect);
3074 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "Focus " + focus
3075 + ": focusRect=" + mTempRect.toShortString());
3076 if (mView instanceof ViewGroup) {
3077 ((ViewGroup) mView).offsetDescendantRectToMyCoords(
3080 if (DEBUG_INPUT_RESIZE) Log.v(mTag,
3081 "Focus in window: focusRect="
3082 + mTempRect.toShortString()
3083 + " visRect=" + mVisRect.toShortString());
3085 mTempRect.set(rectangle);
3086 if (DEBUG_INPUT_RESIZE) Log.v(mTag,
3087 "Request scroll to rect: "
3088 + mTempRect.toShortString()
3089 + " visRect=" + mVisRect.toShortString());
3091 if (mTempRect.intersect(mVisRect)) {
3092 if (DEBUG_INPUT_RESIZE) Log.v(mTag,
3093 "Focus window visible rect: "
3094 + mTempRect.toShortString());
3095 if (mTempRect.height() >
3096 (mView.getHeight()-vi.top-vi.bottom)) {
3097 // If the focus simply is not going to fit, then
3098 // best is probably just to leave things as-is.
3099 if (DEBUG_INPUT_RESIZE) Log.v(mTag,
3100 "Too tall; leaving scrollY=" + scrollY);
3102 // Next, check whether top or bottom is covered based on the non-scrolled
3103 // position, and calculate new scrollY (or set it to 0).
3104 // We can't keep using mScrollY here. For example mScrollY is non-zero
3105 // due to IME, then IME goes away. The current value of mScrollY leaves top
3106 // and bottom both visible, but we still need to scroll it back to 0.
3107 else if (mTempRect.top < vi.top) {
3108 scrollY = mTempRect.top - vi.top;
3109 if (DEBUG_INPUT_RESIZE) Log.v(mTag,
3110 "Top covered; scrollY=" + scrollY);
3111 } else if (mTempRect.bottom > (mView.getHeight()-vi.bottom)) {
3112 scrollY = mTempRect.bottom - (mView.getHeight()-vi.bottom);
3113 if (DEBUG_INPUT_RESIZE) Log.v(mTag,
3114 "Bottom covered; scrollY=" + scrollY);
3124 if (scrollY != mScrollY) {
3125 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "Pan scroll changed: old="
3126 + mScrollY + " , new=" + scrollY);
3128 if (mScroller == null) {
3129 mScroller = new Scroller(mView.getContext());
3131 mScroller.startScroll(0, mScrollY, 0, scrollY-mScrollY);
3132 } else if (mScroller != null) {
3133 mScroller.abortAnimation();
3144 public View getAccessibilityFocusedHost() {
3145 return mAccessibilityFocusedHost;
3151 public AccessibilityNodeInfo getAccessibilityFocusedVirtualView() {
3152 return mAccessibilityFocusedVirtualView;
3155 void setAccessibilityFocus(View view, AccessibilityNodeInfo node) {
3156 // If we have a virtual view with accessibility focus we need
3157 // to clear the focus and invalidate the virtual view bounds.
3158 if (mAccessibilityFocusedVirtualView != null) {
3160 AccessibilityNodeInfo focusNode = mAccessibilityFocusedVirtualView;
3161 View focusHost = mAccessibilityFocusedHost;
3163 // Wipe the state of the current accessibility focus since
3164 // the call into the provider to clear accessibility focus
3165 // will fire an accessibility event which will end up calling
3166 // this method and we want to have clean state when this
3167 // invocation happens.
3168 mAccessibilityFocusedHost = null;
3169 mAccessibilityFocusedVirtualView = null;
3171 // Clear accessibility focus on the host after clearing state since
3172 // this method may be reentrant.
3173 focusHost.clearAccessibilityFocusNoCallbacks(
3174 AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
3176 AccessibilityNodeProvider provider = focusHost.getAccessibilityNodeProvider();
3177 if (provider != null) {
3178 // Invalidate the area of the cleared accessibility focus.
3179 focusNode.getBoundsInParent(mTempRect);
3180 focusHost.invalidate(mTempRect);
3181 // Clear accessibility focus in the virtual node.
3182 final int virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
3183 focusNode.getSourceNodeId());
3184 provider.performAction(virtualNodeId,
3185 AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS, null);
3187 focusNode.recycle();
3189 if (mAccessibilityFocusedHost != null) {
3190 // Clear accessibility focus in the view.
3191 mAccessibilityFocusedHost.clearAccessibilityFocusNoCallbacks(
3192 AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
3195 // Set the new focus host and node.
3196 mAccessibilityFocusedHost = view;
3197 mAccessibilityFocusedVirtualView = node;
3199 if (mAttachInfo.mHardwareRenderer != null) {
3200 mAttachInfo.mHardwareRenderer.invalidateRoot();
3205 public void requestChildFocus(View child, View focused) {
3206 if (DEBUG_INPUT_RESIZE) {
3207 Log.v(mTag, "Request child focus: focus now " + focused);
3210 scheduleTraversals();
3214 public void clearChildFocus(View child) {
3215 if (DEBUG_INPUT_RESIZE) {
3216 Log.v(mTag, "Clearing child focus");
3219 scheduleTraversals();
3223 public ViewParent getParentForAccessibility() {
3228 public void focusableViewAvailable(View v) {
3230 if (mView != null) {
3231 if (!mView.hasFocus()) {
3234 // the one case where will transfer focus away from the current one
3235 // is if the current view is a view group that prefers to give focus
3236 // to its children first AND the view is a descendant of it.
3237 View focused = mView.findFocus();
3238 if (focused instanceof ViewGroup) {
3239 ViewGroup group = (ViewGroup) focused;
3240 if (group.getDescendantFocusability() == ViewGroup.FOCUS_AFTER_DESCENDANTS
3241 && isViewDescendantOf(v, focused)) {
3250 public void recomputeViewAttributes(View child) {
3252 if (mView == child) {
3253 mAttachInfo.mRecomputeGlobalAttributes = true;
3254 if (!mWillDrawSoon) {
3255 scheduleTraversals();
3260 void dispatchDetachedFromWindow() {
3261 if (mView != null && mView.mAttachInfo != null) {
3262 mAttachInfo.mTreeObserver.dispatchOnWindowAttachedChange(false);
3263 mView.dispatchDetachedFromWindow();
3266 mAccessibilityInteractionConnectionManager.ensureNoConnection();
3267 mAccessibilityManager.removeAccessibilityStateChangeListener(
3268 mAccessibilityInteractionConnectionManager);
3269 mAccessibilityManager.removeHighTextContrastStateChangeListener(
3270 mHighContrastTextManager);
3271 removeSendWindowContentChangedCallback();
3273 destroyHardwareRenderer();
3275 setAccessibilityFocus(null, null);
3277 mView.assignParent(null);
3279 mAttachInfo.mRootView = null;
3283 if (mInputQueueCallback != null && mInputQueue != null) {
3284 mInputQueueCallback.onInputQueueDestroyed(mInputQueue);
3285 mInputQueue.dispose();
3286 mInputQueueCallback = null;
3289 if (mInputEventReceiver != null) {
3290 mInputEventReceiver.dispose();
3291 mInputEventReceiver = null;
3294 mWindowSession.remove(mWindow);
3295 } catch (RemoteException e) {
3298 // Dispose the input channel after removing the window so the Window Manager
3299 // doesn't interpret the input channel being closed as an abnormal termination.
3300 if (mInputChannel != null) {
3301 mInputChannel.dispose();
3302 mInputChannel = null;
3305 mDisplayManager.unregisterDisplayListener(mDisplayListener);
3307 unscheduleTraversals();
3310 void updateConfiguration(Configuration config, boolean force) {
3311 if (DEBUG_CONFIGURATION) Log.v(mTag,
3312 "Applying new config to window "
3313 + mWindowAttributes.getTitle()
3316 CompatibilityInfo ci = mDisplay.getDisplayAdjustments().getCompatibilityInfo();
3317 if (!ci.equals(CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO)) {
3318 config = new Configuration(config);
3319 ci.applyToConfiguration(mNoncompatDensity, config);
3322 synchronized (sConfigCallbacks) {
3323 for (int i=sConfigCallbacks.size()-1; i>=0; i--) {
3324 sConfigCallbacks.get(i).onConfigurationChanged(config);
3327 if (mView != null) {
3328 // At this point the resources have been updated to
3329 // have the most recent config, whatever that is. Use
3330 // the one in them which may be newer.
3331 final Resources localResources = mView.getResources();
3332 config = localResources.getConfiguration();
3333 if (force || mLastConfiguration.diff(config) != 0) {
3334 // Update the display with new DisplayAdjustments.
3335 mDisplay = ResourcesManager.getInstance().getAdjustedDisplay(
3336 mDisplay.getDisplayId(), localResources.getDisplayAdjustments());
3338 final int lastLayoutDirection = mLastConfiguration.getLayoutDirection();
3339 final int currentLayoutDirection = config.getLayoutDirection();
3340 mLastConfiguration.setTo(config);
3341 if (lastLayoutDirection != currentLayoutDirection &&
3342 mViewLayoutDirectionInitial == View.LAYOUT_DIRECTION_INHERIT) {
3343 mView.setLayoutDirection(currentLayoutDirection);
3345 mView.dispatchConfigurationChanged(config);
3351 * Return true if child is an ancestor of parent, (or equal to the parent).
3353 public static boolean isViewDescendantOf(View child, View parent) {
3354 if (child == parent) {
3358 final ViewParent theParent = child.getParent();
3359 return (theParent instanceof ViewGroup) && isViewDescendantOf((View) theParent, parent);
3362 private static void forceLayout(View view) {
3364 if (view instanceof ViewGroup) {
3365 ViewGroup group = (ViewGroup) view;
3366 final int count = group.getChildCount();
3367 for (int i = 0; i < count; i++) {
3368 forceLayout(group.getChildAt(i));
3373 private final static int MSG_INVALIDATE = 1;
3374 private final static int MSG_INVALIDATE_RECT = 2;
3375 private final static int MSG_DIE = 3;
3376 private final static int MSG_RESIZED = 4;
3377 private final static int MSG_RESIZED_REPORT = 5;
3378 private final static int MSG_WINDOW_FOCUS_CHANGED = 6;
3379 private final static int MSG_DISPATCH_INPUT_EVENT = 7;
3380 private final static int MSG_DISPATCH_APP_VISIBILITY = 8;
3381 private final static int MSG_DISPATCH_GET_NEW_SURFACE = 9;
3382 private final static int MSG_DISPATCH_KEY_FROM_IME = 11;
3383 private final static int MSG_CHECK_FOCUS = 13;
3384 private final static int MSG_CLOSE_SYSTEM_DIALOGS = 14;
3385 private final static int MSG_DISPATCH_DRAG_EVENT = 15;
3386 private final static int MSG_DISPATCH_DRAG_LOCATION_EVENT = 16;
3387 private final static int MSG_DISPATCH_SYSTEM_UI_VISIBILITY = 17;
3388 private final static int MSG_UPDATE_CONFIGURATION = 18;
3389 private final static int MSG_PROCESS_INPUT_EVENTS = 19;
3390 private final static int MSG_CLEAR_ACCESSIBILITY_FOCUS_HOST = 21;
3391 private final static int MSG_INVALIDATE_WORLD = 22;
3392 private final static int MSG_WINDOW_MOVED = 23;
3393 private final static int MSG_SYNTHESIZE_INPUT_EVENT = 24;
3394 private final static int MSG_DISPATCH_WINDOW_SHOWN = 25;
3395 private final static int MSG_REQUEST_KEYBOARD_SHORTCUTS = 26;
3396 private final static int MSG_UPDATE_POINTER_ICON = 27;
3398 final class ViewRootHandler extends Handler {
3400 public String getMessageName(Message message) {
3401 switch (message.what) {
3402 case MSG_INVALIDATE:
3403 return "MSG_INVALIDATE";
3404 case MSG_INVALIDATE_RECT:
3405 return "MSG_INVALIDATE_RECT";
3409 return "MSG_RESIZED";
3410 case MSG_RESIZED_REPORT:
3411 return "MSG_RESIZED_REPORT";
3412 case MSG_WINDOW_FOCUS_CHANGED:
3413 return "MSG_WINDOW_FOCUS_CHANGED";
3414 case MSG_DISPATCH_INPUT_EVENT:
3415 return "MSG_DISPATCH_INPUT_EVENT";
3416 case MSG_DISPATCH_APP_VISIBILITY:
3417 return "MSG_DISPATCH_APP_VISIBILITY";
3418 case MSG_DISPATCH_GET_NEW_SURFACE:
3419 return "MSG_DISPATCH_GET_NEW_SURFACE";
3420 case MSG_DISPATCH_KEY_FROM_IME:
3421 return "MSG_DISPATCH_KEY_FROM_IME";
3422 case MSG_CHECK_FOCUS:
3423 return "MSG_CHECK_FOCUS";
3424 case MSG_CLOSE_SYSTEM_DIALOGS:
3425 return "MSG_CLOSE_SYSTEM_DIALOGS";
3426 case MSG_DISPATCH_DRAG_EVENT:
3427 return "MSG_DISPATCH_DRAG_EVENT";
3428 case MSG_DISPATCH_DRAG_LOCATION_EVENT:
3429 return "MSG_DISPATCH_DRAG_LOCATION_EVENT";
3430 case MSG_DISPATCH_SYSTEM_UI_VISIBILITY:
3431 return "MSG_DISPATCH_SYSTEM_UI_VISIBILITY";
3432 case MSG_UPDATE_CONFIGURATION:
3433 return "MSG_UPDATE_CONFIGURATION";
3434 case MSG_PROCESS_INPUT_EVENTS:
3435 return "MSG_PROCESS_INPUT_EVENTS";
3436 case MSG_CLEAR_ACCESSIBILITY_FOCUS_HOST:
3437 return "MSG_CLEAR_ACCESSIBILITY_FOCUS_HOST";
3438 case MSG_WINDOW_MOVED:
3439 return "MSG_WINDOW_MOVED";
3440 case MSG_SYNTHESIZE_INPUT_EVENT:
3441 return "MSG_SYNTHESIZE_INPUT_EVENT";
3442 case MSG_DISPATCH_WINDOW_SHOWN:
3443 return "MSG_DISPATCH_WINDOW_SHOWN";
3444 case MSG_UPDATE_POINTER_ICON:
3445 return "MSG_UPDATE_POINTER_ICON";
3447 return super.getMessageName(message);
3451 public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
3452 if (msg.what == MSG_REQUEST_KEYBOARD_SHORTCUTS && msg.obj == null) {
3453 // Debugging for b/27963013
3454 throw new NullPointerException(
3455 "Attempted to call MSG_REQUEST_KEYBOARD_SHORTCUTS with null receiver:");
3457 return super.sendMessageAtTime(msg, uptimeMillis);
3461 public void handleMessage(Message msg) {
3463 case MSG_INVALIDATE:
3464 ((View) msg.obj).invalidate();
3466 case MSG_INVALIDATE_RECT:
3467 final View.AttachInfo.InvalidateInfo info = (View.AttachInfo.InvalidateInfo) msg.obj;
3468 info.target.invalidate(info.left, info.top, info.right, info.bottom);
3471 case MSG_PROCESS_INPUT_EVENTS:
3472 mProcessInputEventsScheduled = false;
3473 doProcessInputEvents();
3475 case MSG_DISPATCH_APP_VISIBILITY:
3476 handleAppVisibility(msg.arg1 != 0);
3478 case MSG_DISPATCH_GET_NEW_SURFACE:
3479 handleGetNewSurface();
3482 // Recycled in the fall through...
3483 SomeArgs args = (SomeArgs) msg.obj;
3484 if (mWinFrame.equals(args.arg1)
3485 && mPendingOverscanInsets.equals(args.arg5)
3486 && mPendingContentInsets.equals(args.arg2)
3487 && mPendingStableInsets.equals(args.arg6)
3488 && mPendingVisibleInsets.equals(args.arg3)
3489 && mPendingOutsets.equals(args.arg7)
3490 && mPendingBackDropFrame.equals(args.arg8)
3491 && args.arg4 == null
3492 && args.argi1 == 0) {
3495 } // fall through...
3496 case MSG_RESIZED_REPORT:
3498 SomeArgs args = (SomeArgs) msg.obj;
3500 Configuration config = (Configuration) args.arg4;
3501 if (config != null) {
3502 updateConfiguration(config, false);
3505 final boolean framesChanged = !mWinFrame.equals(args.arg1)
3506 || !mPendingOverscanInsets.equals(args.arg5)
3507 || !mPendingContentInsets.equals(args.arg2)
3508 || !mPendingStableInsets.equals(args.arg6)
3509 || !mPendingVisibleInsets.equals(args.arg3)
3510 || !mPendingOutsets.equals(args.arg7);
3512 mWinFrame.set((Rect) args.arg1);
3513 mPendingOverscanInsets.set((Rect) args.arg5);
3514 mPendingContentInsets.set((Rect) args.arg2);
3515 mPendingStableInsets.set((Rect) args.arg6);
3516 mPendingVisibleInsets.set((Rect) args.arg3);
3517 mPendingOutsets.set((Rect) args.arg7);
3518 mPendingBackDropFrame.set((Rect) args.arg8);
3519 mForceNextWindowRelayout = args.argi1 != 0;
3520 mPendingAlwaysConsumeNavBar = args.argi2 != 0;
3524 if (msg.what == MSG_RESIZED_REPORT) {
3525 mReportNextDraw = true;
3528 if (mView != null && framesChanged) {
3535 case MSG_WINDOW_MOVED:
3537 final int w = mWinFrame.width();
3538 final int h = mWinFrame.height();
3539 final int l = msg.arg1;
3540 final int t = msg.arg2;
3542 mWinFrame.right = l + w;
3544 mWinFrame.bottom = t + h;
3546 mPendingBackDropFrame.set(mWinFrame);
3548 // Suppress layouts during resizing - a correct layout will happen when resizing
3549 // is done, and this just increases system load.
3550 boolean isDockedDivider = mWindowAttributes.type == TYPE_DOCK_DIVIDER;
3551 boolean suppress = (mDragResizing && mResizeMode == RESIZE_MODE_DOCKED_DIVIDER)
3554 if (mView != null) {
3559 maybeHandleWindowMove(mWinFrame);
3563 case MSG_WINDOW_FOCUS_CHANGED: {
3565 boolean hasWindowFocus = msg.arg1 != 0;
3566 mAttachInfo.mHasWindowFocus = hasWindowFocus;
3568 profileRendering(hasWindowFocus);
3570 if (hasWindowFocus) {
3571 boolean inTouchMode = msg.arg2 != 0;
3572 ensureTouchModeLocally(inTouchMode);
3574 if (mAttachInfo.mHardwareRenderer != null && mSurface.isValid()){
3575 mFullRedrawNeeded = true;
3577 final WindowManager.LayoutParams lp = mWindowAttributes;
3578 final Rect surfaceInsets = lp != null ? lp.surfaceInsets : null;
3579 mAttachInfo.mHardwareRenderer.initializeIfNeeded(
3580 mWidth, mHeight, mAttachInfo, mSurface, surfaceInsets);
3581 } catch (OutOfResourcesException e) {
3582 Log.e(mTag, "OutOfResourcesException locking surface", e);
3584 if (!mWindowSession.outOfMemory(mWindow)) {
3585 Slog.w(mTag, "No processes killed for memory; killing self");
3586 Process.killProcess(Process.myPid());
3588 } catch (RemoteException ex) {
3591 sendMessageDelayed(obtainMessage(msg.what, msg.arg1, msg.arg2), 500);
3597 mLastWasImTarget = WindowManager.LayoutParams
3598 .mayUseInputMethod(mWindowAttributes.flags);
3600 InputMethodManager imm = InputMethodManager.peekInstance();
3601 if (imm != null && mLastWasImTarget && !isInLocalFocusMode()) {
3602 imm.onPreWindowFocus(mView, hasWindowFocus);
3604 if (mView != null) {
3605 mAttachInfo.mKeyDispatchState.reset();
3606 mView.dispatchWindowFocusChanged(hasWindowFocus);
3607 mAttachInfo.mTreeObserver.dispatchOnWindowFocusChange(hasWindowFocus);
3610 // Note: must be done after the focus change callbacks,
3611 // so all of the view state is set up correctly.
3612 if (hasWindowFocus) {
3613 if (imm != null && mLastWasImTarget && !isInLocalFocusMode()) {
3614 imm.onPostWindowFocus(mView, mView.findFocus(),
3615 mWindowAttributes.softInputMode,
3616 !mHasHadWindowFocus, mWindowAttributes.flags);
3618 // Clear the forward bit. We can just do this directly, since
3619 // the window manager doesn't care about it.
3620 mWindowAttributes.softInputMode &=
3621 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
3622 ((WindowManager.LayoutParams)mView.getLayoutParams())
3624 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
3625 mHasHadWindowFocus = true;
3632 case MSG_DISPATCH_INPUT_EVENT: {
3633 SomeArgs args = (SomeArgs)msg.obj;
3634 InputEvent event = (InputEvent)args.arg1;
3635 InputEventReceiver receiver = (InputEventReceiver)args.arg2;
3636 enqueueInputEvent(event, receiver, 0, true);
3639 case MSG_SYNTHESIZE_INPUT_EVENT: {
3640 InputEvent event = (InputEvent)msg.obj;
3641 enqueueInputEvent(event, null, QueuedInputEvent.FLAG_UNHANDLED, true);
3643 case MSG_DISPATCH_KEY_FROM_IME: {
3644 if (LOCAL_LOGV) Log.v(
3645 TAG, "Dispatching key "
3646 + msg.obj + " from IME to " + mView);
3647 KeyEvent event = (KeyEvent)msg.obj;
3648 if ((event.getFlags()&KeyEvent.FLAG_FROM_SYSTEM) != 0) {
3649 // The IME is trying to say this event is from the
3650 // system! Bad bad bad!
3651 //noinspection UnusedAssignment
3652 event = KeyEvent.changeFlags(event, event.getFlags() &
3653 ~KeyEvent.FLAG_FROM_SYSTEM);
3655 enqueueInputEvent(event, null, QueuedInputEvent.FLAG_DELIVER_POST_IME, true);
3657 case MSG_CHECK_FOCUS: {
3658 InputMethodManager imm = InputMethodManager.peekInstance();
3663 case MSG_CLOSE_SYSTEM_DIALOGS: {
3664 if (mView != null) {
3665 mView.onCloseSystemDialogs((String)msg.obj);
3668 case MSG_DISPATCH_DRAG_EVENT:
3669 case MSG_DISPATCH_DRAG_LOCATION_EVENT: {
3670 DragEvent event = (DragEvent)msg.obj;
3671 event.mLocalState = mLocalDragState; // only present when this app called startDrag()
3672 handleDragEvent(event);
3674 case MSG_DISPATCH_SYSTEM_UI_VISIBILITY: {
3675 handleDispatchSystemUiVisibilityChanged((SystemUiVisibilityInfo) msg.obj);
3677 case MSG_UPDATE_CONFIGURATION: {
3678 Configuration config = (Configuration)msg.obj;
3679 if (config.isOtherSeqNewer(mLastConfiguration)) {
3680 config = mLastConfiguration;
3682 updateConfiguration(config, false);
3684 case MSG_CLEAR_ACCESSIBILITY_FOCUS_HOST: {
3685 setAccessibilityFocus(null, null);
3687 case MSG_INVALIDATE_WORLD: {
3688 if (mView != null) {
3689 invalidateWorld(mView);
3692 case MSG_DISPATCH_WINDOW_SHOWN: {
3693 handleDispatchWindowShown();
3695 case MSG_REQUEST_KEYBOARD_SHORTCUTS: {
3696 final IResultReceiver receiver = (IResultReceiver) msg.obj;
3697 final int deviceId = msg.arg1;
3698 handleRequestKeyboardShortcuts(receiver, deviceId);
3700 case MSG_UPDATE_POINTER_ICON: {
3701 MotionEvent event = (MotionEvent) msg.obj;
3702 resetPointerIcon(event);
3708 final ViewRootHandler mHandler = new ViewRootHandler();
3711 * Something in the current window tells us we need to change the touch mode. For
3712 * example, we are not in touch mode, and the user touches the screen.
3714 * If the touch mode has changed, tell the window manager, and handle it locally.
3716 * @param inTouchMode Whether we want to be in touch mode.
3717 * @return True if the touch mode changed and focus changed was changed as a result
3719 boolean ensureTouchMode(boolean inTouchMode) {
3720 if (DBG) Log.d("touchmode", "ensureTouchMode(" + inTouchMode + "), current "
3721 + "touch mode is " + mAttachInfo.mInTouchMode);
3722 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
3724 // tell the window manager
3726 mWindowSession.setInTouchMode(inTouchMode);
3727 } catch (RemoteException e) {
3728 throw new RuntimeException(e);
3731 // handle the change
3732 return ensureTouchModeLocally(inTouchMode);
3736 * Ensure that the touch mode for this window is set, and if it is changing,
3737 * take the appropriate action.
3738 * @param inTouchMode Whether we want to be in touch mode.
3739 * @return True if the touch mode changed and focus changed was changed as a result
3741 private boolean ensureTouchModeLocally(boolean inTouchMode) {
3742 if (DBG) Log.d("touchmode", "ensureTouchModeLocally(" + inTouchMode + "), current "
3743 + "touch mode is " + mAttachInfo.mInTouchMode);
3745 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
3747 mAttachInfo.mInTouchMode = inTouchMode;
3748 mAttachInfo.mTreeObserver.dispatchOnTouchModeChanged(inTouchMode);
3750 return (inTouchMode) ? enterTouchMode() : leaveTouchMode();
3753 private boolean enterTouchMode() {
3754 if (mView != null && mView.hasFocus()) {
3755 // note: not relying on mFocusedView here because this could
3756 // be when the window is first being added, and mFocused isn't
3758 final View focused = mView.findFocus();
3759 if (focused != null && !focused.isFocusableInTouchMode()) {
3760 final ViewGroup ancestorToTakeFocus = findAncestorToTakeFocusInTouchMode(focused);
3761 if (ancestorToTakeFocus != null) {
3762 // there is an ancestor that wants focus after its
3763 // descendants that is focusable in touch mode.. give it
3765 return ancestorToTakeFocus.requestFocus();
3767 // There's nothing to focus. Clear and propagate through the
3768 // hierarchy, but don't attempt to place new focus.
3769 focused.clearFocusInternal(null, true, false);
3778 * Find an ancestor of focused that wants focus after its descendants and is
3779 * focusable in touch mode.
3780 * @param focused The currently focused view.
3781 * @return An appropriate view, or null if no such view exists.
3783 private static ViewGroup findAncestorToTakeFocusInTouchMode(View focused) {
3784 ViewParent parent = focused.getParent();
3785 while (parent instanceof ViewGroup) {
3786 final ViewGroup vgParent = (ViewGroup) parent;
3787 if (vgParent.getDescendantFocusability() == ViewGroup.FOCUS_AFTER_DESCENDANTS
3788 && vgParent.isFocusableInTouchMode()) {
3791 if (vgParent.isRootNamespace()) {
3794 parent = vgParent.getParent();
3800 private boolean leaveTouchMode() {
3801 if (mView != null) {
3802 if (mView.hasFocus()) {
3803 View focusedView = mView.findFocus();
3804 if (!(focusedView instanceof ViewGroup)) {
3805 // some view has focus, let it keep it
3807 } else if (((ViewGroup) focusedView).getDescendantFocusability() !=
3808 ViewGroup.FOCUS_AFTER_DESCENDANTS) {
3809 // some view group has focus, and doesn't prefer its children
3810 // over itself for focus, so let them keep it.
3815 // find the best view to give focus to in this brave new non-touch-mode
3817 final View focused = focusSearch(null, View.FOCUS_DOWN);
3818 if (focused != null) {
3819 return focused.requestFocus(View.FOCUS_DOWN);
3826 * Base class for implementing a stage in the chain of responsibility
3827 * for processing input events.
3829 * Events are delivered to the stage by the {@link #deliver} method. The stage
3830 * then has the choice of finishing the event or forwarding it to the next stage.
3833 abstract class InputStage {
3834 private final InputStage mNext;
3836 protected static final int FORWARD = 0;
3837 protected static final int FINISH_HANDLED = 1;
3838 protected static final int FINISH_NOT_HANDLED = 2;
3841 * Creates an input stage.
3842 * @param next The next stage to which events should be forwarded.
3844 public InputStage(InputStage next) {
3849 * Delivers an event to be processed.
3851 public final void deliver(QueuedInputEvent q) {
3852 if ((q.mFlags & QueuedInputEvent.FLAG_FINISHED) != 0) {
3854 } else if (shouldDropInputEvent(q)) {
3857 apply(q, onProcess(q));
3862 * Marks the the input event as finished then forwards it to the next stage.
3864 protected void finish(QueuedInputEvent q, boolean handled) {
3865 q.mFlags |= QueuedInputEvent.FLAG_FINISHED;
3867 q.mFlags |= QueuedInputEvent.FLAG_FINISHED_HANDLED;
3873 * Forwards the event to the next stage.
3875 protected void forward(QueuedInputEvent q) {
3880 * Applies a result code from {@link #onProcess} to the specified event.
3882 protected void apply(QueuedInputEvent q, int result) {
3883 if (result == FORWARD) {
3885 } else if (result == FINISH_HANDLED) {
3887 } else if (result == FINISH_NOT_HANDLED) {
3890 throw new IllegalArgumentException("Invalid result: " + result);
3895 * Called when an event is ready to be processed.
3896 * @return A result code indicating how the event was handled.
3898 protected int onProcess(QueuedInputEvent q) {
3903 * Called when an event is being delivered to the next stage.
3905 protected void onDeliverToNext(QueuedInputEvent q) {
3906 if (DEBUG_INPUT_STAGES) {
3907 Log.v(mTag, "Done with " + getClass().getSimpleName() + ". " + q);
3909 if (mNext != null) {
3912 finishInputEvent(q);
3916 protected boolean shouldDropInputEvent(QueuedInputEvent q) {
3917 if (mView == null || !mAdded) {
3918 Slog.w(mTag, "Dropping event due to root view being removed: " + q.mEvent);
3920 } else if ((!mAttachInfo.mHasWindowFocus
3921 && !q.mEvent.isFromSource(InputDevice.SOURCE_CLASS_POINTER)) || mStopped
3922 || (mIsAmbientMode && !q.mEvent.isFromSource(InputDevice.SOURCE_CLASS_BUTTON))
3923 || (mPausedForTransition && !isBack(q.mEvent))) {
3924 // This is a focus event and the window doesn't currently have input focus or
3925 // has stopped. This could be an event that came back from the previous stage
3926 // but the window has lost focus or stopped in the meantime.
3927 if (isTerminalInputEvent(q.mEvent)) {
3928 // Don't drop terminal input events, however mark them as canceled.
3930 Slog.w(mTag, "Cancelling event due to no window focus: " + q.mEvent);
3934 // Drop non-terminal input events.
3935 Slog.w(mTag, "Dropping event due to no window focus: " + q.mEvent);
3941 void dump(String prefix, PrintWriter writer) {
3942 if (mNext != null) {
3943 mNext.dump(prefix, writer);
3947 private boolean isBack(InputEvent event) {
3948 if (event instanceof KeyEvent) {
3949 return ((KeyEvent) event).getKeyCode() == KeyEvent.KEYCODE_BACK;
3957 * Base class for implementing an input pipeline stage that supports
3958 * asynchronous and out-of-order processing of input events.
3960 * In addition to what a normal input stage can do, an asynchronous
3961 * input stage may also defer an input event that has been delivered to it
3962 * and finish or forward it later.
3965 abstract class AsyncInputStage extends InputStage {
3966 private final String mTraceCounter;
3968 private QueuedInputEvent mQueueHead;
3969 private QueuedInputEvent mQueueTail;
3970 private int mQueueLength;
3972 protected static final int DEFER = 3;
3975 * Creates an asynchronous input stage.
3976 * @param next The next stage to which events should be forwarded.
3977 * @param traceCounter The name of a counter to record the size of
3978 * the queue of pending events.
3980 public AsyncInputStage(InputStage next, String traceCounter) {
3982 mTraceCounter = traceCounter;
3986 * Marks the event as deferred, which is to say that it will be handled
3987 * asynchronously. The caller is responsible for calling {@link #forward}
3988 * or {@link #finish} later when it is done handling the event.
3990 protected void defer(QueuedInputEvent q) {
3991 q.mFlags |= QueuedInputEvent.FLAG_DEFERRED;
3996 protected void forward(QueuedInputEvent q) {
3997 // Clear the deferred flag.
3998 q.mFlags &= ~QueuedInputEvent.FLAG_DEFERRED;
4000 // Fast path if the queue is empty.
4001 QueuedInputEvent curr = mQueueHead;
4007 // Determine whether the event must be serialized behind any others
4008 // before it can be delivered to the next stage. This is done because
4009 // deferred events might be handled out of order by the stage.
4010 final int deviceId = q.mEvent.getDeviceId();
4011 QueuedInputEvent prev = null;
4012 boolean blocked = false;
4013 while (curr != null && curr != q) {
4014 if (!blocked && deviceId == curr.mEvent.getDeviceId()) {
4021 // If the event is blocked, then leave it in the queue to be delivered later.
4022 // Note that the event might not yet be in the queue if it was not previously
4023 // deferred so we will enqueue it if needed.
4031 // The event is not blocked. Deliver it immediately.
4038 // Dequeuing this event may have unblocked successors. Deliver them.
4039 while (curr != null) {
4040 if (deviceId == curr.mEvent.getDeviceId()) {
4041 if ((curr.mFlags & QueuedInputEvent.FLAG_DEFERRED) != 0) {
4044 QueuedInputEvent next = curr.mNext;
4045 dequeue(curr, prev);
4046 super.forward(curr);
4056 protected void apply(QueuedInputEvent q, int result) {
4057 if (result == DEFER) {
4060 super.apply(q, result);
4064 private void enqueue(QueuedInputEvent q) {
4065 if (mQueueTail == null) {
4069 mQueueTail.mNext = q;
4074 Trace.traceCounter(Trace.TRACE_TAG_INPUT, mTraceCounter, mQueueLength);
4077 private void dequeue(QueuedInputEvent q, QueuedInputEvent prev) {
4079 mQueueHead = q.mNext;
4081 prev.mNext = q.mNext;
4083 if (mQueueTail == q) {
4089 Trace.traceCounter(Trace.TRACE_TAG_INPUT, mTraceCounter, mQueueLength);
4093 void dump(String prefix, PrintWriter writer) {
4094 writer.print(prefix);
4095 writer.print(getClass().getName());
4096 writer.print(": mQueueLength=");
4097 writer.println(mQueueLength);
4099 super.dump(prefix, writer);
4104 * Delivers pre-ime input events to a native activity.
4105 * Does not support pointer events.
4107 final class NativePreImeInputStage extends AsyncInputStage
4108 implements InputQueue.FinishedInputEventCallback {
4109 public NativePreImeInputStage(InputStage next, String traceCounter) {
4110 super(next, traceCounter);
4114 protected int onProcess(QueuedInputEvent q) {
4115 if (mInputQueue != null && q.mEvent instanceof KeyEvent) {
4116 mInputQueue.sendInputEvent(q.mEvent, q, true, this);
4123 public void onFinishedInputEvent(Object token, boolean handled) {
4124 QueuedInputEvent q = (QueuedInputEvent)token;
4134 * Delivers pre-ime input events to the view hierarchy.
4135 * Does not support pointer events.
4137 final class ViewPreImeInputStage extends InputStage {
4138 public ViewPreImeInputStage(InputStage next) {
4143 protected int onProcess(QueuedInputEvent q) {
4144 if (q.mEvent instanceof KeyEvent) {
4145 return processKeyEvent(q);
4150 private int processKeyEvent(QueuedInputEvent q) {
4151 final KeyEvent event = (KeyEvent)q.mEvent;
4152 if (mView.dispatchKeyEventPreIme(event)) {
4153 return FINISH_HANDLED;
4160 * Delivers input events to the ime.
4161 * Does not support pointer events.
4163 final class ImeInputStage extends AsyncInputStage
4164 implements InputMethodManager.FinishedInputEventCallback {
4165 public ImeInputStage(InputStage next, String traceCounter) {
4166 super(next, traceCounter);
4170 protected int onProcess(QueuedInputEvent q) {
4171 if (mLastWasImTarget && !isInLocalFocusMode()) {
4172 InputMethodManager imm = InputMethodManager.peekInstance();
4174 final InputEvent event = q.mEvent;
4175 if (DEBUG_IMF) Log.v(mTag, "Sending input event to IME: " + event);
4176 int result = imm.dispatchInputEvent(event, q, this, mHandler);
4177 if (result == InputMethodManager.DISPATCH_HANDLED) {
4178 return FINISH_HANDLED;
4179 } else if (result == InputMethodManager.DISPATCH_NOT_HANDLED) {
4180 // The IME could not handle it, so skip along to the next InputStage
4183 return DEFER; // callback will be invoked later
4191 public void onFinishedInputEvent(Object token, boolean handled) {
4192 QueuedInputEvent q = (QueuedInputEvent)token;
4202 * Performs early processing of post-ime input events.
4204 final class EarlyPostImeInputStage extends InputStage {
4205 public EarlyPostImeInputStage(InputStage next) {
4210 protected int onProcess(QueuedInputEvent q) {
4211 if (q.mEvent instanceof KeyEvent) {
4212 return processKeyEvent(q);
4214 final int source = q.mEvent.getSource();
4215 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
4216 return processPointerEvent(q);
4222 private int processKeyEvent(QueuedInputEvent q) {
4223 final KeyEvent event = (KeyEvent)q.mEvent;
4225 // If the key's purpose is to exit touch mode then we consume it
4226 // and consider it handled.
4227 if (checkForLeavingTouchModeAndConsume(event)) {
4228 return FINISH_HANDLED;
4231 // Make sure the fallback event policy sees all keys that will be
4232 // delivered to the view hierarchy.
4233 mFallbackEventHandler.preDispatchKeyEvent(event);
4237 private int processPointerEvent(QueuedInputEvent q) {
4238 final MotionEvent event = (MotionEvent)q.mEvent;
4240 // Translate the pointer event for compatibility, if needed.
4241 if (mTranslator != null) {
4242 mTranslator.translateEventInScreenToAppWindow(event);
4245 // Enter touch mode on down or scroll.
4246 final int action = event.getAction();
4247 if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_SCROLL) {
4248 ensureTouchMode(true);
4251 // Offset the scroll position.
4252 if (mCurScrollY != 0) {
4253 event.offsetLocation(0, mCurScrollY);
4256 // Remember the touch position for possible drag-initiation.
4257 if (event.isTouchEvent()) {
4258 mLastTouchPoint.x = event.getRawX();
4259 mLastTouchPoint.y = event.getRawY();
4260 mLastTouchSource = event.getSource();
4267 * Delivers post-ime input events to a native activity.
4269 final class NativePostImeInputStage extends AsyncInputStage
4270 implements InputQueue.FinishedInputEventCallback {
4271 public NativePostImeInputStage(InputStage next, String traceCounter) {
4272 super(next, traceCounter);
4276 protected int onProcess(QueuedInputEvent q) {
4277 if (mInputQueue != null) {
4278 mInputQueue.sendInputEvent(q.mEvent, q, false, this);
4285 public void onFinishedInputEvent(Object token, boolean handled) {
4286 QueuedInputEvent q = (QueuedInputEvent)token;
4296 * Delivers post-ime input events to the view hierarchy.
4298 final class ViewPostImeInputStage extends InputStage {
4299 public ViewPostImeInputStage(InputStage next) {
4304 protected int onProcess(QueuedInputEvent q) {
4305 if (q.mEvent instanceof KeyEvent) {
4306 return processKeyEvent(q);
4308 final int source = q.mEvent.getSource();
4309 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
4310 return processPointerEvent(q);
4311 } else if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) {
4312 return processTrackballEvent(q);
4314 return processGenericMotionEvent(q);
4320 protected void onDeliverToNext(QueuedInputEvent q) {
4321 if (mUnbufferedInputDispatch
4322 && q.mEvent instanceof MotionEvent
4323 && ((MotionEvent)q.mEvent).isTouchEvent()
4324 && isTerminalInputEvent(q.mEvent)) {
4325 mUnbufferedInputDispatch = false;
4326 scheduleConsumeBatchedInput();
4328 super.onDeliverToNext(q);
4331 private int processKeyEvent(QueuedInputEvent q) {
4332 final KeyEvent event = (KeyEvent)q.mEvent;
4334 // Deliver the key to the view hierarchy.
4335 if (mView.dispatchKeyEvent(event)) {
4336 return FINISH_HANDLED;
4339 if (shouldDropInputEvent(q)) {
4340 return FINISH_NOT_HANDLED;
4343 // If the Control modifier is held, try to interpret the key as a shortcut.
4344 if (event.getAction() == KeyEvent.ACTION_DOWN
4345 && event.isCtrlPressed()
4346 && event.getRepeatCount() == 0
4347 && !KeyEvent.isModifierKey(event.getKeyCode())) {
4348 if (mView.dispatchKeyShortcutEvent(event)) {
4349 return FINISH_HANDLED;
4351 if (shouldDropInputEvent(q)) {
4352 return FINISH_NOT_HANDLED;
4356 // Apply the fallback event policy.
4357 if (mFallbackEventHandler.dispatchKeyEvent(event)) {
4358 return FINISH_HANDLED;
4360 if (shouldDropInputEvent(q)) {
4361 return FINISH_NOT_HANDLED;
4364 // Handle automatic focus changes.
4365 if (event.getAction() == KeyEvent.ACTION_DOWN) {
4367 switch (event.getKeyCode()) {
4368 case KeyEvent.KEYCODE_DPAD_LEFT:
4369 if (event.hasNoModifiers()) {
4370 direction = View.FOCUS_LEFT;
4373 case KeyEvent.KEYCODE_DPAD_RIGHT:
4374 if (event.hasNoModifiers()) {
4375 direction = View.FOCUS_RIGHT;
4378 case KeyEvent.KEYCODE_DPAD_UP:
4379 if (event.hasNoModifiers()) {
4380 direction = View.FOCUS_UP;
4383 case KeyEvent.KEYCODE_DPAD_DOWN:
4384 if (event.hasNoModifiers()) {
4385 direction = View.FOCUS_DOWN;
4388 case KeyEvent.KEYCODE_TAB:
4389 if (event.hasNoModifiers()) {
4390 direction = View.FOCUS_FORWARD;
4391 } else if (event.hasModifiers(KeyEvent.META_SHIFT_ON)) {
4392 direction = View.FOCUS_BACKWARD;
4396 if (direction != 0) {
4397 View focused = mView.findFocus();
4398 if (focused != null) {
4399 View v = focused.focusSearch(direction);
4400 if (v != null && v != focused) {
4401 // do the math the get the interesting rect
4402 // of previous focused into the coord system of
4403 // newly focused view
4404 focused.getFocusedRect(mTempRect);
4405 if (mView instanceof ViewGroup) {
4406 ((ViewGroup) mView).offsetDescendantRectToMyCoords(
4407 focused, mTempRect);
4408 ((ViewGroup) mView).offsetRectIntoDescendantCoords(
4411 if (v.requestFocus(direction, mTempRect)) {
4412 playSoundEffect(SoundEffectConstants
4413 .getContantForFocusDirection(direction));
4414 return FINISH_HANDLED;
4418 // Give the focused view a last chance to handle the dpad key.
4419 if (mView.dispatchUnhandledMove(focused, direction)) {
4420 return FINISH_HANDLED;
4423 // find the best view to give focus to in this non-touch-mode with no-focus
4424 View v = focusSearch(null, direction);
4425 if (v != null && v.requestFocus(direction)) {
4426 return FINISH_HANDLED;
4434 private int processPointerEvent(QueuedInputEvent q) {
4435 final MotionEvent event = (MotionEvent)q.mEvent;
4437 mAttachInfo.mUnbufferedDispatchRequested = false;
4438 final View eventTarget =
4439 (event.isFromSource(InputDevice.SOURCE_MOUSE) && mCapturingView != null) ?
4440 mCapturingView : mView;
4441 mAttachInfo.mHandlingPointerEvent = true;
4442 boolean handled = eventTarget.dispatchPointerEvent(event);
4443 maybeUpdatePointerIcon(event);
4444 mAttachInfo.mHandlingPointerEvent = false;
4445 if (mAttachInfo.mUnbufferedDispatchRequested && !mUnbufferedInputDispatch) {
4446 mUnbufferedInputDispatch = true;
4447 if (mConsumeBatchedInputScheduled) {
4448 scheduleConsumeBatchedInputImmediately();
4451 return handled ? FINISH_HANDLED : FORWARD;
4454 private void maybeUpdatePointerIcon(MotionEvent event) {
4455 if (event.getPointerCount() == 1 && event.isFromSource(InputDevice.SOURCE_MOUSE)) {
4456 if (event.getActionMasked() == MotionEvent.ACTION_HOVER_ENTER
4457 || event.getActionMasked() == MotionEvent.ACTION_HOVER_EXIT) {
4458 // Other apps or the window manager may change the icon type outside of
4459 // this app, therefore the icon type has to be reset on enter/exit event.
4460 mPointerIconType = PointerIcon.TYPE_NOT_SPECIFIED;
4463 if (event.getActionMasked() != MotionEvent.ACTION_HOVER_EXIT) {
4464 if (!updatePointerIcon(event) &&
4465 event.getActionMasked() == MotionEvent.ACTION_HOVER_MOVE) {
4466 mPointerIconType = PointerIcon.TYPE_NOT_SPECIFIED;
4472 private int processTrackballEvent(QueuedInputEvent q) {
4473 final MotionEvent event = (MotionEvent)q.mEvent;
4475 if (mView.dispatchTrackballEvent(event)) {
4476 return FINISH_HANDLED;
4481 private int processGenericMotionEvent(QueuedInputEvent q) {
4482 final MotionEvent event = (MotionEvent)q.mEvent;
4484 // Deliver the event to the view.
4485 if (mView.dispatchGenericMotionEvent(event)) {
4486 return FINISH_HANDLED;
4492 private void resetPointerIcon(MotionEvent event) {
4493 mPointerIconType = PointerIcon.TYPE_NOT_SPECIFIED;
4494 updatePointerIcon(event);
4497 private boolean updatePointerIcon(MotionEvent event) {
4498 final int pointerIndex = 0;
4499 final float x = event.getX(pointerIndex);
4500 final float y = event.getY(pointerIndex);
4501 if (mView == null) {
4502 // E.g. click outside a popup to dismiss it
4503 Slog.d(mTag, "updatePointerIcon called after view was removed");
4506 if (x < 0 || x >= mView.getWidth() || y < 0 || y >= mView.getHeight()) {
4507 // E.g. when moving window divider with mouse
4508 Slog.d(mTag, "updatePointerIcon called with position out of bounds");
4511 final PointerIcon pointerIcon = mView.onResolvePointerIcon(event, pointerIndex);
4512 final int pointerType = (pointerIcon != null) ?
4513 pointerIcon.getType() : PointerIcon.TYPE_DEFAULT;
4515 if (mPointerIconType != pointerType) {
4516 mPointerIconType = pointerType;
4517 if (mPointerIconType != PointerIcon.TYPE_CUSTOM) {
4518 mCustomPointerIcon = null;
4519 InputManager.getInstance().setPointerIconType(pointerType);
4523 if (mPointerIconType == PointerIcon.TYPE_CUSTOM &&
4524 !pointerIcon.equals(mCustomPointerIcon)) {
4525 mCustomPointerIcon = pointerIcon;
4526 InputManager.getInstance().setCustomPointerIcon(mCustomPointerIcon);
4532 * Performs synthesis of new input events from unhandled input events.
4534 final class SyntheticInputStage extends InputStage {
4535 private final SyntheticTrackballHandler mTrackball = new SyntheticTrackballHandler();
4536 private final SyntheticJoystickHandler mJoystick = new SyntheticJoystickHandler();
4537 private final SyntheticTouchNavigationHandler mTouchNavigation =
4538 new SyntheticTouchNavigationHandler();
4539 private final SyntheticKeyboardHandler mKeyboard = new SyntheticKeyboardHandler();
4541 public SyntheticInputStage() {
4546 protected int onProcess(QueuedInputEvent q) {
4547 q.mFlags |= QueuedInputEvent.FLAG_RESYNTHESIZED;
4548 if (q.mEvent instanceof MotionEvent) {
4549 final MotionEvent event = (MotionEvent)q.mEvent;
4550 final int source = event.getSource();
4551 if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) {
4552 mTrackball.process(event);
4553 return FINISH_HANDLED;
4554 } else if ((source & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
4555 mJoystick.process(event);
4556 return FINISH_HANDLED;
4557 } else if ((source & InputDevice.SOURCE_TOUCH_NAVIGATION)
4558 == InputDevice.SOURCE_TOUCH_NAVIGATION) {
4559 mTouchNavigation.process(event);
4560 return FINISH_HANDLED;
4562 } else if ((q.mFlags & QueuedInputEvent.FLAG_UNHANDLED) != 0) {
4563 mKeyboard.process((KeyEvent)q.mEvent);
4564 return FINISH_HANDLED;
4571 protected void onDeliverToNext(QueuedInputEvent q) {
4572 if ((q.mFlags & QueuedInputEvent.FLAG_RESYNTHESIZED) == 0) {
4573 // Cancel related synthetic events if any prior stage has handled the event.
4574 if (q.mEvent instanceof MotionEvent) {
4575 final MotionEvent event = (MotionEvent)q.mEvent;
4576 final int source = event.getSource();
4577 if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) {
4578 mTrackball.cancel(event);
4579 } else if ((source & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
4580 mJoystick.cancel(event);
4581 } else if ((source & InputDevice.SOURCE_TOUCH_NAVIGATION)
4582 == InputDevice.SOURCE_TOUCH_NAVIGATION) {
4583 mTouchNavigation.cancel(event);
4587 super.onDeliverToNext(q);
4592 * Creates dpad events from unhandled trackball movements.
4594 final class SyntheticTrackballHandler {
4595 private final TrackballAxis mX = new TrackballAxis();
4596 private final TrackballAxis mY = new TrackballAxis();
4597 private long mLastTime;
4599 public void process(MotionEvent event) {
4600 // Translate the trackball event into DPAD keys and try to deliver those.
4601 long curTime = SystemClock.uptimeMillis();
4602 if ((mLastTime + MAX_TRACKBALL_DELAY) < curTime) {
4603 // It has been too long since the last movement,
4604 // so restart at the beginning.
4607 mLastTime = curTime;
4610 final int action = event.getAction();
4611 final int metaState = event.getMetaState();
4613 case MotionEvent.ACTION_DOWN:
4616 enqueueInputEvent(new KeyEvent(curTime, curTime,
4617 KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER, 0, metaState,
4618 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
4619 InputDevice.SOURCE_KEYBOARD));
4621 case MotionEvent.ACTION_UP:
4624 enqueueInputEvent(new KeyEvent(curTime, curTime,
4625 KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER, 0, metaState,
4626 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
4627 InputDevice.SOURCE_KEYBOARD));
4631 if (DEBUG_TRACKBALL) Log.v(mTag, "TB X=" + mX.position + " step="
4632 + mX.step + " dir=" + mX.dir + " acc=" + mX.acceleration
4633 + " move=" + event.getX()
4634 + " / Y=" + mY.position + " step="
4635 + mY.step + " dir=" + mY.dir + " acc=" + mY.acceleration
4636 + " move=" + event.getY());
4637 final float xOff = mX.collect(event.getX(), event.getEventTime(), "X");
4638 final float yOff = mY.collect(event.getY(), event.getEventTime(), "Y");
4640 // Generate DPAD events based on the trackball movement.
4641 // We pick the axis that has moved the most as the direction of
4642 // the DPAD. When we generate DPAD events for one axis, then the
4643 // other axis is reset -- we don't want to perform DPAD jumps due
4644 // to slight movements in the trackball when making major movements
4645 // along the other axis.
4650 movement = mX.generate();
4651 if (movement != 0) {
4652 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_RIGHT
4653 : KeyEvent.KEYCODE_DPAD_LEFT;
4654 accel = mX.acceleration;
4657 } else if (yOff > 0) {
4658 movement = mY.generate();
4659 if (movement != 0) {
4660 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_DOWN
4661 : KeyEvent.KEYCODE_DPAD_UP;
4662 accel = mY.acceleration;
4668 if (movement < 0) movement = -movement;
4669 int accelMovement = (int)(movement * accel);
4670 if (DEBUG_TRACKBALL) Log.v(mTag, "Move: movement=" + movement
4671 + " accelMovement=" + accelMovement
4672 + " accel=" + accel);
4673 if (accelMovement > movement) {
4674 if (DEBUG_TRACKBALL) Log.v(mTag, "Delivering fake DPAD: "
4677 int repeatCount = accelMovement - movement;
4678 enqueueInputEvent(new KeyEvent(curTime, curTime,
4679 KeyEvent.ACTION_MULTIPLE, keycode, repeatCount, metaState,
4680 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
4681 InputDevice.SOURCE_KEYBOARD));
4683 while (movement > 0) {
4684 if (DEBUG_TRACKBALL) Log.v(mTag, "Delivering fake DPAD: "
4687 curTime = SystemClock.uptimeMillis();
4688 enqueueInputEvent(new KeyEvent(curTime, curTime,
4689 KeyEvent.ACTION_DOWN, keycode, 0, metaState,
4690 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
4691 InputDevice.SOURCE_KEYBOARD));
4692 enqueueInputEvent(new KeyEvent(curTime, curTime,
4693 KeyEvent.ACTION_UP, keycode, 0, metaState,
4694 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
4695 InputDevice.SOURCE_KEYBOARD));
4697 mLastTime = curTime;
4701 public void cancel(MotionEvent event) {
4702 mLastTime = Integer.MIN_VALUE;
4704 // If we reach this, we consumed a trackball event.
4705 // Because we will not translate the trackball event into a key event,
4706 // touch mode will not exit, so we exit touch mode here.
4707 if (mView != null && mAdded) {
4708 ensureTouchMode(false);
4714 * Maintains state information for a single trackball axis, generating
4715 * discrete (DPAD) movements based on raw trackball motion.
4717 static final class TrackballAxis {
4719 * The maximum amount of acceleration we will apply.
4721 static final float MAX_ACCELERATION = 20;
4724 * The maximum amount of time (in milliseconds) between events in order
4725 * for us to consider the user to be doing fast trackball movements,
4726 * and thus apply an acceleration.
4728 static final long FAST_MOVE_TIME = 150;
4731 * Scaling factor to the time (in milliseconds) between events to how
4732 * much to multiple/divide the current acceleration. When movement
4733 * is < FAST_MOVE_TIME this multiplies the acceleration; when >
4734 * FAST_MOVE_TIME it divides it.
4736 static final float ACCEL_MOVE_SCALING_FACTOR = (1.0f/40);
4738 static final float FIRST_MOVEMENT_THRESHOLD = 0.5f;
4739 static final float SECOND_CUMULATIVE_MOVEMENT_THRESHOLD = 2.0f;
4740 static final float SUBSEQUENT_INCREMENTAL_MOVEMENT_THRESHOLD = 1.0f;
4743 float acceleration = 1;
4744 long lastMoveTime = 0;
4747 int nonAccelMovement;
4749 void reset(int _step) {
4758 * Add trackball movement into the state. If the direction of movement
4759 * has been reversed, the state is reset before adding the
4760 * movement (so that you don't have to compensate for any previously
4761 * collected movement before see the result of the movement in the
4764 * @return Returns the absolute value of the amount of movement
4767 float collect(float off, long time, String axis) {
4770 normTime = (long)(off * FAST_MOVE_TIME);
4772 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to positive!");
4779 } else if (off < 0) {
4780 normTime = (long)((-off) * FAST_MOVE_TIME);
4782 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to negative!");
4793 // The number of milliseconds between each movement that is
4794 // considered "normal" and will not result in any acceleration
4795 // or deceleration, scaled by the offset we have here.
4797 long delta = time - lastMoveTime;
4798 lastMoveTime = time;
4799 float acc = acceleration;
4800 if (delta < normTime) {
4801 // The user is scrolling rapidly, so increase acceleration.
4802 float scale = (normTime-delta) * ACCEL_MOVE_SCALING_FACTOR;
4803 if (scale > 1) acc *= scale;
4804 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " accelerate: off="
4805 + off + " normTime=" + normTime + " delta=" + delta
4806 + " scale=" + scale + " acc=" + acc);
4807 acceleration = acc < MAX_ACCELERATION ? acc : MAX_ACCELERATION;
4809 // The user is scrolling slowly, so decrease acceleration.
4810 float scale = (delta-normTime) * ACCEL_MOVE_SCALING_FACTOR;
4811 if (scale > 1) acc /= scale;
4812 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " deccelerate: off="
4813 + off + " normTime=" + normTime + " delta=" + delta
4814 + " scale=" + scale + " acc=" + acc);
4815 acceleration = acc > 1 ? acc : 1;
4819 return Math.abs(position);
4823 * Generate the number of discrete movement events appropriate for
4824 * the currently collected trackball movement.
4826 * @return Returns the number of discrete movements, either positive
4827 * or negative, or 0 if there is not enough trackball movement yet
4828 * for a discrete movement.
4832 nonAccelMovement = 0;
4834 final int dir = position >= 0 ? 1 : -1;
4836 // If we are going to execute the first step, then we want
4837 // to do this as soon as possible instead of waiting for
4838 // a full movement, in order to make things look responsive.
4840 if (Math.abs(position) < FIRST_MOVEMENT_THRESHOLD) {
4844 nonAccelMovement += dir;
4847 // If we have generated the first movement, then we need
4848 // to wait for the second complete trackball motion before
4849 // generating the second discrete movement.
4851 if (Math.abs(position) < SECOND_CUMULATIVE_MOVEMENT_THRESHOLD) {
4855 nonAccelMovement += dir;
4856 position -= SECOND_CUMULATIVE_MOVEMENT_THRESHOLD * dir;
4859 // After the first two, we generate discrete movements
4860 // consistently with the trackball, applying an acceleration
4861 // if the trackball is moving quickly. This is a simple
4862 // acceleration on top of what we already compute based
4863 // on how quickly the wheel is being turned, to apply
4864 // a longer increasing acceleration to continuous movement
4865 // in one direction.
4867 if (Math.abs(position) < SUBSEQUENT_INCREMENTAL_MOVEMENT_THRESHOLD) {
4871 position -= dir * SUBSEQUENT_INCREMENTAL_MOVEMENT_THRESHOLD;
4872 float acc = acceleration;
4874 acceleration = acc < MAX_ACCELERATION ? acc : acceleration;
4882 * Creates dpad events from unhandled joystick movements.
4884 final class SyntheticJoystickHandler extends Handler {
4885 private final static String TAG = "SyntheticJoystickHandler";
4886 private final static int MSG_ENQUEUE_X_AXIS_KEY_REPEAT = 1;
4887 private final static int MSG_ENQUEUE_Y_AXIS_KEY_REPEAT = 2;
4889 private int mLastXDirection;
4890 private int mLastYDirection;
4891 private int mLastXKeyCode;
4892 private int mLastYKeyCode;
4894 public SyntheticJoystickHandler() {
4899 public void handleMessage(Message msg) {
4901 case MSG_ENQUEUE_X_AXIS_KEY_REPEAT:
4902 case MSG_ENQUEUE_Y_AXIS_KEY_REPEAT: {
4903 KeyEvent oldEvent = (KeyEvent)msg.obj;
4904 KeyEvent e = KeyEvent.changeTimeRepeat(oldEvent,
4905 SystemClock.uptimeMillis(),
4906 oldEvent.getRepeatCount() + 1);
4907 if (mAttachInfo.mHasWindowFocus) {
4908 enqueueInputEvent(e);
4909 Message m = obtainMessage(msg.what, e);
4910 m.setAsynchronous(true);
4911 sendMessageDelayed(m, ViewConfiguration.getKeyRepeatDelay());
4917 public void process(MotionEvent event) {
4918 switch(event.getActionMasked()) {
4919 case MotionEvent.ACTION_CANCEL:
4922 case MotionEvent.ACTION_MOVE:
4923 update(event, true);
4926 Log.w(mTag, "Unexpected action: " + event.getActionMasked());
4930 private void cancel(MotionEvent event) {
4931 removeMessages(MSG_ENQUEUE_X_AXIS_KEY_REPEAT);
4932 removeMessages(MSG_ENQUEUE_Y_AXIS_KEY_REPEAT);
4933 update(event, false);
4936 private void update(MotionEvent event, boolean synthesizeNewKeys) {
4937 final long time = event.getEventTime();
4938 final int metaState = event.getMetaState();
4939 final int deviceId = event.getDeviceId();
4940 final int source = event.getSource();
4942 int xDirection = joystickAxisValueToDirection(
4943 event.getAxisValue(MotionEvent.AXIS_HAT_X));
4944 if (xDirection == 0) {
4945 xDirection = joystickAxisValueToDirection(event.getX());
4948 int yDirection = joystickAxisValueToDirection(
4949 event.getAxisValue(MotionEvent.AXIS_HAT_Y));
4950 if (yDirection == 0) {
4951 yDirection = joystickAxisValueToDirection(event.getY());
4954 if (xDirection != mLastXDirection) {
4955 if (mLastXKeyCode != 0) {
4956 removeMessages(MSG_ENQUEUE_X_AXIS_KEY_REPEAT);
4957 enqueueInputEvent(new KeyEvent(time, time,
4958 KeyEvent.ACTION_UP, mLastXKeyCode, 0, metaState,
4959 deviceId, 0, KeyEvent.FLAG_FALLBACK, source));
4963 mLastXDirection = xDirection;
4965 if (xDirection != 0 && synthesizeNewKeys) {
4966 mLastXKeyCode = xDirection > 0
4967 ? KeyEvent.KEYCODE_DPAD_RIGHT : KeyEvent.KEYCODE_DPAD_LEFT;
4968 final KeyEvent e = new KeyEvent(time, time,
4969 KeyEvent.ACTION_DOWN, mLastXKeyCode, 0, metaState,
4970 deviceId, 0, KeyEvent.FLAG_FALLBACK, source);
4971 enqueueInputEvent(e);
4972 Message m = obtainMessage(MSG_ENQUEUE_X_AXIS_KEY_REPEAT, e);
4973 m.setAsynchronous(true);
4974 sendMessageDelayed(m, ViewConfiguration.getKeyRepeatTimeout());
4978 if (yDirection != mLastYDirection) {
4979 if (mLastYKeyCode != 0) {
4980 removeMessages(MSG_ENQUEUE_Y_AXIS_KEY_REPEAT);
4981 enqueueInputEvent(new KeyEvent(time, time,
4982 KeyEvent.ACTION_UP, mLastYKeyCode, 0, metaState,
4983 deviceId, 0, KeyEvent.FLAG_FALLBACK, source));
4987 mLastYDirection = yDirection;
4989 if (yDirection != 0 && synthesizeNewKeys) {
4990 mLastYKeyCode = yDirection > 0
4991 ? KeyEvent.KEYCODE_DPAD_DOWN : KeyEvent.KEYCODE_DPAD_UP;
4992 final KeyEvent e = new KeyEvent(time, time,
4993 KeyEvent.ACTION_DOWN, mLastYKeyCode, 0, metaState,
4994 deviceId, 0, KeyEvent.FLAG_FALLBACK, source);
4995 enqueueInputEvent(e);
4996 Message m = obtainMessage(MSG_ENQUEUE_Y_AXIS_KEY_REPEAT, e);
4997 m.setAsynchronous(true);
4998 sendMessageDelayed(m, ViewConfiguration.getKeyRepeatTimeout());
5003 private int joystickAxisValueToDirection(float value) {
5004 if (value >= 0.5f) {
5006 } else if (value <= -0.5f) {
5015 * Creates dpad events from unhandled touch navigation movements.
5017 final class SyntheticTouchNavigationHandler extends Handler {
5018 private static final String LOCAL_TAG = "SyntheticTouchNavigationHandler";
5019 private static final boolean LOCAL_DEBUG = false;
5021 // Assumed nominal width and height in millimeters of a touch navigation pad,
5022 // if no resolution information is available from the input system.
5023 private static final float DEFAULT_WIDTH_MILLIMETERS = 48;
5024 private static final float DEFAULT_HEIGHT_MILLIMETERS = 48;
5026 /* TODO: These constants should eventually be moved to ViewConfiguration. */
5028 // The nominal distance traveled to move by one unit.
5029 private static final int TICK_DISTANCE_MILLIMETERS = 12;
5031 // Minimum and maximum fling velocity in ticks per second.
5032 // The minimum velocity should be set such that we perform enough ticks per
5033 // second that the fling appears to be fluid. For example, if we set the minimum
5034 // to 2 ticks per second, then there may be up to half a second delay between the next
5035 // to last and last ticks which is noticeably discrete and jerky. This value should
5036 // probably not be set to anything less than about 4.
5037 // If fling accuracy is a problem then consider tuning the tick distance instead.
5038 private static final float MIN_FLING_VELOCITY_TICKS_PER_SECOND = 6f;
5039 private static final float MAX_FLING_VELOCITY_TICKS_PER_SECOND = 20f;
5041 // Fling velocity decay factor applied after each new key is emitted.
5042 // This parameter controls the deceleration and overall duration of the fling.
5043 // The fling stops automatically when its velocity drops below the minimum
5044 // fling velocity defined above.
5045 private static final float FLING_TICK_DECAY = 0.8f;
5047 /* The input device that we are tracking. */
5049 private int mCurrentDeviceId = -1;
5050 private int mCurrentSource;
5051 private boolean mCurrentDeviceSupported;
5053 /* Configuration for the current input device. */
5055 // The scaled tick distance. A movement of this amount should generally translate
5056 // into a single dpad event in a given direction.
5057 private float mConfigTickDistance;
5059 // The minimum and maximum scaled fling velocity.
5060 private float mConfigMinFlingVelocity;
5061 private float mConfigMaxFlingVelocity;
5063 /* Tracking state. */
5065 // The velocity tracker for detecting flings.
5066 private VelocityTracker mVelocityTracker;
5068 // The active pointer id, or -1 if none.
5069 private int mActivePointerId = -1;
5071 // Location where tracking started.
5072 private float mStartX;
5073 private float mStartY;
5075 // Most recently observed position.
5076 private float mLastX;
5077 private float mLastY;
5079 // Accumulated movement delta since the last direction key was sent.
5080 private float mAccumulatedX;
5081 private float mAccumulatedY;
5083 // Set to true if any movement was delivered to the app.
5084 // Implies that tap slop was exceeded.
5085 private boolean mConsumedMovement;
5087 // The most recently sent key down event.
5088 // The keycode remains set until the direction changes or a fling ends
5089 // so that repeated key events may be generated as required.
5090 private long mPendingKeyDownTime;
5091 private int mPendingKeyCode = KeyEvent.KEYCODE_UNKNOWN;
5092 private int mPendingKeyRepeatCount;
5093 private int mPendingKeyMetaState;
5095 // The current fling velocity while a fling is in progress.
5096 private boolean mFlinging;
5097 private float mFlingVelocity;
5099 public SyntheticTouchNavigationHandler() {
5103 public void process(MotionEvent event) {
5104 // Update the current device information.
5105 final long time = event.getEventTime();
5106 final int deviceId = event.getDeviceId();
5107 final int source = event.getSource();
5108 if (mCurrentDeviceId != deviceId || mCurrentSource != source) {
5110 finishTracking(time);
5111 mCurrentDeviceId = deviceId;
5112 mCurrentSource = source;
5113 mCurrentDeviceSupported = false;
5114 InputDevice device = event.getDevice();
5115 if (device != null) {
5116 // In order to support an input device, we must know certain
5117 // characteristics about it, such as its size and resolution.
5118 InputDevice.MotionRange xRange = device.getMotionRange(MotionEvent.AXIS_X);
5119 InputDevice.MotionRange yRange = device.getMotionRange(MotionEvent.AXIS_Y);
5120 if (xRange != null && yRange != null) {
5121 mCurrentDeviceSupported = true;
5123 // Infer the resolution if it not actually known.
5124 float xRes = xRange.getResolution();
5126 xRes = xRange.getRange() / DEFAULT_WIDTH_MILLIMETERS;
5128 float yRes = yRange.getResolution();
5130 yRes = yRange.getRange() / DEFAULT_HEIGHT_MILLIMETERS;
5132 float nominalRes = (xRes + yRes) * 0.5f;
5134 // Precompute all of the configuration thresholds we will need.
5135 mConfigTickDistance = TICK_DISTANCE_MILLIMETERS * nominalRes;
5136 mConfigMinFlingVelocity =
5137 MIN_FLING_VELOCITY_TICKS_PER_SECOND * mConfigTickDistance;
5138 mConfigMaxFlingVelocity =
5139 MAX_FLING_VELOCITY_TICKS_PER_SECOND * mConfigTickDistance;
5142 Log.d(LOCAL_TAG, "Configured device " + mCurrentDeviceId
5143 + " (" + Integer.toHexString(mCurrentSource) + "): "
5144 + ", mConfigTickDistance=" + mConfigTickDistance
5145 + ", mConfigMinFlingVelocity=" + mConfigMinFlingVelocity
5146 + ", mConfigMaxFlingVelocity=" + mConfigMaxFlingVelocity);
5151 if (!mCurrentDeviceSupported) {
5155 // Handle the event.
5156 final int action = event.getActionMasked();
5158 case MotionEvent.ACTION_DOWN: {
5159 boolean caughtFling = mFlinging;
5161 finishTracking(time);
5162 mActivePointerId = event.getPointerId(0);
5163 mVelocityTracker = VelocityTracker.obtain();
5164 mVelocityTracker.addMovement(event);
5165 mStartX = event.getX();
5166 mStartY = event.getY();
5172 // If we caught a fling, then pretend that the tap slop has already
5173 // been exceeded to suppress taps whose only purpose is to stop the fling.
5174 mConsumedMovement = caughtFling;
5178 case MotionEvent.ACTION_MOVE:
5179 case MotionEvent.ACTION_UP: {
5180 if (mActivePointerId < 0) {
5183 final int index = event.findPointerIndex(mActivePointerId);
5186 finishTracking(time);
5190 mVelocityTracker.addMovement(event);
5191 final float x = event.getX(index);
5192 final float y = event.getY(index);
5193 mAccumulatedX += x - mLastX;
5194 mAccumulatedY += y - mLastY;
5198 // Consume any accumulated movement so far.
5199 final int metaState = event.getMetaState();
5200 consumeAccumulatedMovement(time, metaState);
5202 // Detect taps and flings.
5203 if (action == MotionEvent.ACTION_UP) {
5204 if (mConsumedMovement && mPendingKeyCode != KeyEvent.KEYCODE_UNKNOWN) {
5205 // It might be a fling.
5206 mVelocityTracker.computeCurrentVelocity(1000, mConfigMaxFlingVelocity);
5207 final float vx = mVelocityTracker.getXVelocity(mActivePointerId);
5208 final float vy = mVelocityTracker.getYVelocity(mActivePointerId);
5209 if (!startFling(time, vx, vy)) {
5213 finishTracking(time);
5218 case MotionEvent.ACTION_CANCEL: {
5220 finishTracking(time);
5226 public void cancel(MotionEvent event) {
5227 if (mCurrentDeviceId == event.getDeviceId()
5228 && mCurrentSource == event.getSource()) {
5229 final long time = event.getEventTime();
5231 finishTracking(time);
5235 private void finishKeys(long time) {
5240 private void finishTracking(long time) {
5241 if (mActivePointerId >= 0) {
5242 mActivePointerId = -1;
5243 mVelocityTracker.recycle();
5244 mVelocityTracker = null;
5248 private void consumeAccumulatedMovement(long time, int metaState) {
5249 final float absX = Math.abs(mAccumulatedX);
5250 final float absY = Math.abs(mAccumulatedY);
5252 if (absX >= mConfigTickDistance) {
5253 mAccumulatedX = consumeAccumulatedMovement(time, metaState, mAccumulatedX,
5254 KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_RIGHT);
5256 mConsumedMovement = true;
5259 if (absY >= mConfigTickDistance) {
5260 mAccumulatedY = consumeAccumulatedMovement(time, metaState, mAccumulatedY,
5261 KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_DOWN);
5263 mConsumedMovement = true;
5268 private float consumeAccumulatedMovement(long time, int metaState,
5269 float accumulator, int negativeKeyCode, int positiveKeyCode) {
5270 while (accumulator <= -mConfigTickDistance) {
5271 sendKeyDownOrRepeat(time, negativeKeyCode, metaState);
5272 accumulator += mConfigTickDistance;
5274 while (accumulator >= mConfigTickDistance) {
5275 sendKeyDownOrRepeat(time, positiveKeyCode, metaState);
5276 accumulator -= mConfigTickDistance;
5281 private void sendKeyDownOrRepeat(long time, int keyCode, int metaState) {
5282 if (mPendingKeyCode != keyCode) {
5284 mPendingKeyDownTime = time;
5285 mPendingKeyCode = keyCode;
5286 mPendingKeyRepeatCount = 0;
5288 mPendingKeyRepeatCount += 1;
5290 mPendingKeyMetaState = metaState;
5292 // Note: Normally we would pass FLAG_LONG_PRESS when the repeat count is 1
5293 // but it doesn't quite make sense when simulating the events in this way.
5295 Log.d(LOCAL_TAG, "Sending key down: keyCode=" + mPendingKeyCode
5296 + ", repeatCount=" + mPendingKeyRepeatCount
5297 + ", metaState=" + Integer.toHexString(mPendingKeyMetaState));
5299 enqueueInputEvent(new KeyEvent(mPendingKeyDownTime, time,
5300 KeyEvent.ACTION_DOWN, mPendingKeyCode, mPendingKeyRepeatCount,
5301 mPendingKeyMetaState, mCurrentDeviceId,
5302 KeyEvent.FLAG_FALLBACK, mCurrentSource));
5305 private void sendKeyUp(long time) {
5306 if (mPendingKeyCode != KeyEvent.KEYCODE_UNKNOWN) {
5308 Log.d(LOCAL_TAG, "Sending key up: keyCode=" + mPendingKeyCode
5309 + ", metaState=" + Integer.toHexString(mPendingKeyMetaState));
5311 enqueueInputEvent(new KeyEvent(mPendingKeyDownTime, time,
5312 KeyEvent.ACTION_UP, mPendingKeyCode, 0, mPendingKeyMetaState,
5313 mCurrentDeviceId, 0, KeyEvent.FLAG_FALLBACK,
5315 mPendingKeyCode = KeyEvent.KEYCODE_UNKNOWN;
5319 private boolean startFling(long time, float vx, float vy) {
5321 Log.d(LOCAL_TAG, "Considering fling: vx=" + vx + ", vy=" + vy
5322 + ", min=" + mConfigMinFlingVelocity);
5325 // Flings must be oriented in the same direction as the preceding movements.
5326 switch (mPendingKeyCode) {
5327 case KeyEvent.KEYCODE_DPAD_LEFT:
5328 if (-vx >= mConfigMinFlingVelocity
5329 && Math.abs(vy) < mConfigMinFlingVelocity) {
5330 mFlingVelocity = -vx;
5335 case KeyEvent.KEYCODE_DPAD_RIGHT:
5336 if (vx >= mConfigMinFlingVelocity
5337 && Math.abs(vy) < mConfigMinFlingVelocity) {
5338 mFlingVelocity = vx;
5343 case KeyEvent.KEYCODE_DPAD_UP:
5344 if (-vy >= mConfigMinFlingVelocity
5345 && Math.abs(vx) < mConfigMinFlingVelocity) {
5346 mFlingVelocity = -vy;
5351 case KeyEvent.KEYCODE_DPAD_DOWN:
5352 if (vy >= mConfigMinFlingVelocity
5353 && Math.abs(vx) < mConfigMinFlingVelocity) {
5354 mFlingVelocity = vy;
5360 // Post the first fling event.
5361 mFlinging = postFling(time);
5365 private boolean postFling(long time) {
5366 // The idea here is to estimate the time when the pointer would have
5367 // traveled one tick distance unit given the current fling velocity.
5368 // This effect creates continuity of motion.
5369 if (mFlingVelocity >= mConfigMinFlingVelocity) {
5370 long delay = (long)(mConfigTickDistance / mFlingVelocity * 1000);
5371 postAtTime(mFlingRunnable, time + delay);
5373 Log.d(LOCAL_TAG, "Posted fling: velocity="
5374 + mFlingVelocity + ", delay=" + delay
5375 + ", keyCode=" + mPendingKeyCode);
5382 private void cancelFling() {
5384 removeCallbacks(mFlingRunnable);
5389 private final Runnable mFlingRunnable = new Runnable() {
5392 final long time = SystemClock.uptimeMillis();
5393 sendKeyDownOrRepeat(time, mPendingKeyCode, mPendingKeyMetaState);
5394 mFlingVelocity *= FLING_TICK_DECAY;
5395 if (!postFling(time)) {
5403 final class SyntheticKeyboardHandler {
5404 public void process(KeyEvent event) {
5405 if ((event.getFlags() & KeyEvent.FLAG_FALLBACK) != 0) {
5409 final KeyCharacterMap kcm = event.getKeyCharacterMap();
5410 final int keyCode = event.getKeyCode();
5411 final int metaState = event.getMetaState();
5413 // Check for fallback actions specified by the key character map.
5414 KeyCharacterMap.FallbackAction fallbackAction =
5415 kcm.getFallbackAction(keyCode, metaState);
5416 if (fallbackAction != null) {
5417 final int flags = event.getFlags() | KeyEvent.FLAG_FALLBACK;
5418 KeyEvent fallbackEvent = KeyEvent.obtain(
5419 event.getDownTime(), event.getEventTime(),
5420 event.getAction(), fallbackAction.keyCode,
5421 event.getRepeatCount(), fallbackAction.metaState,
5422 event.getDeviceId(), event.getScanCode(),
5423 flags, event.getSource(), null);
5424 fallbackAction.recycle();
5425 enqueueInputEvent(fallbackEvent);
5431 * Returns true if the key is used for keyboard navigation.
5432 * @param keyEvent The key event.
5433 * @return True if the key is used for keyboard navigation.
5435 private static boolean isNavigationKey(KeyEvent keyEvent) {
5436 switch (keyEvent.getKeyCode()) {
5437 case KeyEvent.KEYCODE_DPAD_LEFT:
5438 case KeyEvent.KEYCODE_DPAD_RIGHT:
5439 case KeyEvent.KEYCODE_DPAD_UP:
5440 case KeyEvent.KEYCODE_DPAD_DOWN:
5441 case KeyEvent.KEYCODE_DPAD_CENTER:
5442 case KeyEvent.KEYCODE_PAGE_UP:
5443 case KeyEvent.KEYCODE_PAGE_DOWN:
5444 case KeyEvent.KEYCODE_MOVE_HOME:
5445 case KeyEvent.KEYCODE_MOVE_END:
5446 case KeyEvent.KEYCODE_TAB:
5447 case KeyEvent.KEYCODE_SPACE:
5448 case KeyEvent.KEYCODE_ENTER:
5455 * Returns true if the key is used for typing.
5456 * @param keyEvent The key event.
5457 * @return True if the key is used for typing.
5459 private static boolean isTypingKey(KeyEvent keyEvent) {
5460 return keyEvent.getUnicodeChar() > 0;
5464 * See if the key event means we should leave touch mode (and leave touch mode if so).
5465 * @param event The key event.
5466 * @return Whether this key event should be consumed (meaning the act of
5467 * leaving touch mode alone is considered the event).
5469 private boolean checkForLeavingTouchModeAndConsume(KeyEvent event) {
5470 // Only relevant in touch mode.
5471 if (!mAttachInfo.mInTouchMode) {
5475 // Only consider leaving touch mode on DOWN or MULTIPLE actions, never on UP.
5476 final int action = event.getAction();
5477 if (action != KeyEvent.ACTION_DOWN && action != KeyEvent.ACTION_MULTIPLE) {
5481 // Don't leave touch mode if the IME told us not to.
5482 if ((event.getFlags() & KeyEvent.FLAG_KEEP_TOUCH_MODE) != 0) {
5486 // If the key can be used for keyboard navigation then leave touch mode
5487 // and select a focused view if needed (in ensureTouchMode).
5488 // When a new focused view is selected, we consume the navigation key because
5489 // navigation doesn't make much sense unless a view already has focus so
5490 // the key's purpose is to set focus.
5491 if (isNavigationKey(event)) {
5492 return ensureTouchMode(false);
5495 // If the key can be used for typing then leave touch mode
5496 // and select a focused view if needed (in ensureTouchMode).
5497 // Always allow the view to process the typing key.
5498 if (isTypingKey(event)) {
5499 ensureTouchMode(false);
5507 void setLocalDragState(Object obj) {
5508 mLocalDragState = obj;
5511 private void handleDragEvent(DragEvent event) {
5512 // From the root, only drag start/end/location are dispatched. entered/exited
5513 // are determined and dispatched by the viewgroup hierarchy, who then report
5514 // that back here for ultimate reporting back to the framework.
5515 if (mView != null && mAdded) {
5516 final int what = event.mAction;
5518 if (what == DragEvent.ACTION_DRAG_EXITED) {
5519 // A direct EXITED event means that the window manager knows we've just crossed
5520 // a window boundary, so the current drag target within this one must have
5521 // just been exited. Send it the usual notifications and then we're done
5523 mView.dispatchDragEvent(event);
5525 // Cache the drag description when the operation starts, then fill it in
5526 // on subsequent calls as a convenience
5527 if (what == DragEvent.ACTION_DRAG_STARTED) {
5528 mCurrentDragView = null; // Start the current-recipient tracking
5529 mDragDescription = event.mClipDescription;
5531 event.mClipDescription = mDragDescription;
5534 // For events with a [screen] location, translate into window coordinates
5535 if ((what == DragEvent.ACTION_DRAG_LOCATION) || (what == DragEvent.ACTION_DROP)) {
5536 mDragPoint.set(event.mX, event.mY);
5537 if (mTranslator != null) {
5538 mTranslator.translatePointInScreenToAppWindow(mDragPoint);
5541 if (mCurScrollY != 0) {
5542 mDragPoint.offset(0, mCurScrollY);
5545 event.mX = mDragPoint.x;
5546 event.mY = mDragPoint.y;
5549 // Remember who the current drag target is pre-dispatch
5550 final View prevDragView = mCurrentDragView;
5552 // Now dispatch the drag/drop event
5553 boolean result = mView.dispatchDragEvent(event);
5555 // If we changed apparent drag target, tell the OS about it
5556 if (prevDragView != mCurrentDragView) {
5558 if (prevDragView != null) {
5559 mWindowSession.dragRecipientExited(mWindow);
5561 if (mCurrentDragView != null) {
5562 mWindowSession.dragRecipientEntered(mWindow);
5564 } catch (RemoteException e) {
5565 Slog.e(mTag, "Unable to note drag target change");
5569 // Report the drop result when we're done
5570 if (what == DragEvent.ACTION_DROP) {
5571 mDragDescription = null;
5573 Log.i(mTag, "Reporting drop result: " + result);
5574 mWindowSession.reportDropResult(mWindow, result);
5575 } catch (RemoteException e) {
5576 Log.e(mTag, "Unable to report drop result");
5580 // When the drag operation ends, reset drag-related state
5581 if (what == DragEvent.ACTION_DRAG_ENDED) {
5582 setLocalDragState(null);
5583 mAttachInfo.mDragToken = null;
5584 if (mAttachInfo.mDragSurface != null) {
5585 mAttachInfo.mDragSurface.release();
5586 mAttachInfo.mDragSurface = null;
5594 public void handleDispatchSystemUiVisibilityChanged(SystemUiVisibilityInfo args) {
5595 if (mSeq != args.seq) {
5596 // The sequence has changed, so we need to update our value and make
5597 // sure to do a traversal afterward so the window manager is given our
5598 // most recent data.
5600 mAttachInfo.mForceReportNewAttributes = true;
5601 scheduleTraversals();
5603 if (mView == null) return;
5604 if (args.localChanges != 0) {
5605 mView.updateLocalSystemUiVisibility(args.localValue, args.localChanges);
5608 int visibility = args.globalVisibility&View.SYSTEM_UI_CLEARABLE_FLAGS;
5609 if (visibility != mAttachInfo.mGlobalSystemUiVisibility) {
5610 mAttachInfo.mGlobalSystemUiVisibility = visibility;
5611 mView.dispatchSystemUiVisibilityChanged(visibility);
5615 public void handleDispatchWindowShown() {
5616 mAttachInfo.mTreeObserver.dispatchOnWindowShown();
5619 public void handleRequestKeyboardShortcuts(IResultReceiver receiver, int deviceId) {
5620 Bundle data = new Bundle();
5621 ArrayList<KeyboardShortcutGroup> list = new ArrayList<>();
5622 if (mView != null) {
5623 mView.requestKeyboardShortcuts(list, deviceId);
5625 data.putParcelableArrayList(WindowManager.PARCEL_KEY_SHORTCUTS_ARRAY, list);
5627 receiver.send(0, data);
5628 } catch (RemoteException e) {
5632 public void getLastTouchPoint(Point outLocation) {
5633 outLocation.x = (int) mLastTouchPoint.x;
5634 outLocation.y = (int) mLastTouchPoint.y;
5637 public int getLastTouchSource() {
5638 return mLastTouchSource;
5641 public void setDragFocus(View newDragTarget) {
5642 if (mCurrentDragView != newDragTarget) {
5643 mCurrentDragView = newDragTarget;
5647 private AudioManager getAudioManager() {
5648 if (mView == null) {
5649 throw new IllegalStateException("getAudioManager called when there is no mView");
5651 if (mAudioManager == null) {
5652 mAudioManager = (AudioManager) mView.getContext().getSystemService(Context.AUDIO_SERVICE);
5654 return mAudioManager;
5657 public AccessibilityInteractionController getAccessibilityInteractionController() {
5658 if (mView == null) {
5659 throw new IllegalStateException("getAccessibilityInteractionController"
5660 + " called when there is no mView");
5662 if (mAccessibilityInteractionController == null) {
5663 mAccessibilityInteractionController = new AccessibilityInteractionController(this);
5665 return mAccessibilityInteractionController;
5668 private int relayoutWindow(WindowManager.LayoutParams params, int viewVisibility,
5669 boolean insetsPending) throws RemoteException {
5671 float appScale = mAttachInfo.mApplicationScale;
5672 boolean restore = false;
5673 if (params != null && mTranslator != null) {
5676 mTranslator.translateWindowLayout(params);
5678 if (params != null) {
5679 if (DBG) Log.d(mTag, "WindowLayout in layoutWindow:" + params);
5681 mPendingConfiguration.seq = 0;
5682 //Log.d(mTag, ">>>>>> CALLING relayout");
5683 if (params != null && mOrigWindowType != params.type) {
5684 // For compatibility with old apps, don't crash here.
5685 if (mTargetSdkVersion < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
5686 Slog.w(mTag, "Window type can not be changed after "
5687 + "the window is added; ignoring change of " + mView);
5688 params.type = mOrigWindowType;
5691 int relayoutResult = mWindowSession.relayout(
5692 mWindow, mSeq, params,
5693 (int) (mView.getMeasuredWidth() * appScale + 0.5f),
5694 (int) (mView.getMeasuredHeight() * appScale + 0.5f),
5695 viewVisibility, insetsPending ? WindowManagerGlobal.RELAYOUT_INSETS_PENDING : 0,
5696 mWinFrame, mPendingOverscanInsets, mPendingContentInsets, mPendingVisibleInsets,
5697 mPendingStableInsets, mPendingOutsets, mPendingBackDropFrame, mPendingConfiguration,
5700 mPendingAlwaysConsumeNavBar =
5701 (relayoutResult & WindowManagerGlobal.RELAYOUT_RES_CONSUME_ALWAYS_NAV_BAR) != 0;
5703 //Log.d(mTag, "<<<<<< BACK FROM relayout");
5708 if (mTranslator != null) {
5709 mTranslator.translateRectInScreenToAppWinFrame(mWinFrame);
5710 mTranslator.translateRectInScreenToAppWindow(mPendingOverscanInsets);
5711 mTranslator.translateRectInScreenToAppWindow(mPendingContentInsets);
5712 mTranslator.translateRectInScreenToAppWindow(mPendingVisibleInsets);
5713 mTranslator.translateRectInScreenToAppWindow(mPendingStableInsets);
5715 return relayoutResult;
5722 public void playSoundEffect(int effectId) {
5726 final AudioManager audioManager = getAudioManager();
5729 case SoundEffectConstants.CLICK:
5730 audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK);
5732 case SoundEffectConstants.NAVIGATION_DOWN:
5733 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_DOWN);
5735 case SoundEffectConstants.NAVIGATION_LEFT:
5736 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_LEFT);
5738 case SoundEffectConstants.NAVIGATION_RIGHT:
5739 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_RIGHT);
5741 case SoundEffectConstants.NAVIGATION_UP:
5742 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_UP);
5745 throw new IllegalArgumentException("unknown effect id " + effectId +
5746 " not defined in " + SoundEffectConstants.class.getCanonicalName());
5748 } catch (IllegalStateException e) {
5749 // Exception thrown by getAudioManager() when mView is null
5750 Log.e(mTag, "FATAL EXCEPTION when attempting to play sound effect: " + e);
5751 e.printStackTrace();
5759 public boolean performHapticFeedback(int effectId, boolean always) {
5761 return mWindowSession.performHapticFeedback(mWindow, effectId, always);
5762 } catch (RemoteException e) {
5771 public View focusSearch(View focused, int direction) {
5773 if (!(mView instanceof ViewGroup)) {
5776 return FocusFinder.getInstance().findNextFocus((ViewGroup) mView, focused, direction);
5779 public void debug() {
5783 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5784 String innerPrefix = prefix + " ";
5785 writer.print(prefix); writer.println("ViewRoot:");
5786 writer.print(innerPrefix); writer.print("mAdded="); writer.print(mAdded);
5787 writer.print(" mRemoved="); writer.println(mRemoved);
5788 writer.print(innerPrefix); writer.print("mConsumeBatchedInputScheduled=");
5789 writer.println(mConsumeBatchedInputScheduled);
5790 writer.print(innerPrefix); writer.print("mConsumeBatchedInputImmediatelyScheduled=");
5791 writer.println(mConsumeBatchedInputImmediatelyScheduled);
5792 writer.print(innerPrefix); writer.print("mPendingInputEventCount=");
5793 writer.println(mPendingInputEventCount);
5794 writer.print(innerPrefix); writer.print("mProcessInputEventsScheduled=");
5795 writer.println(mProcessInputEventsScheduled);
5796 writer.print(innerPrefix); writer.print("mTraversalScheduled=");
5797 writer.print(mTraversalScheduled);
5798 writer.print(innerPrefix); writer.print("mIsAmbientMode=");
5799 writer.print(mIsAmbientMode);
5800 if (mTraversalScheduled) {
5801 writer.print(" (barrier="); writer.print(mTraversalBarrier); writer.println(")");
5805 mFirstInputStage.dump(innerPrefix, writer);
5807 mChoreographer.dump(prefix, writer);
5809 writer.print(prefix); writer.println("View Hierarchy:");
5810 dumpViewHierarchy(innerPrefix, writer, mView);
5813 private void dumpViewHierarchy(String prefix, PrintWriter writer, View view) {
5814 writer.print(prefix);
5816 writer.println("null");
5819 writer.println(view.toString());
5820 if (!(view instanceof ViewGroup)) {
5823 ViewGroup grp = (ViewGroup)view;
5824 final int N = grp.getChildCount();
5828 prefix = prefix + " ";
5829 for (int i=0; i<N; i++) {
5830 dumpViewHierarchy(prefix, writer, grp.getChildAt(i));
5834 public void dumpGfxInfo(int[] info) {
5835 info[0] = info[1] = 0;
5836 if (mView != null) {
5837 getGfxInfo(mView, info);
5841 private static void getGfxInfo(View view, int[] info) {
5842 RenderNode renderNode = view.mRenderNode;
5844 if (renderNode != null) {
5845 info[1] += renderNode.getDebugSize();
5848 if (view instanceof ViewGroup) {
5849 ViewGroup group = (ViewGroup) view;
5851 int count = group.getChildCount();
5852 for (int i = 0; i < count; i++) {
5853 getGfxInfo(group.getChildAt(i), info);
5859 * @param immediate True, do now if not in traversal. False, put on queue and do later.
5860 * @return True, request has been queued. False, request has been completed.
5862 boolean die(boolean immediate) {
5863 // Make sure we do execute immediately if we are in the middle of a traversal or the damage
5864 // done by dispatchDetachedFromWindow will cause havoc on return.
5865 if (immediate && !mIsInTraversal) {
5871 destroyHardwareRenderer();
5873 Log.e(mTag, "Attempting to destroy the window while drawing!\n" +
5874 " window=" + this + ", title=" + mWindowAttributes.getTitle());
5876 mHandler.sendEmptyMessage(MSG_DIE);
5882 if (LOCAL_LOGV) Log.v(mTag, "DIE in " + this + " of " + mSurface);
5883 synchronized (this) {
5889 dispatchDetachedFromWindow();
5892 if (mAdded && !mFirst) {
5893 destroyHardwareRenderer();
5895 if (mView != null) {
5896 int viewVisibility = mView.getVisibility();
5897 boolean viewVisibilityChanged = mViewVisibility != viewVisibility;
5898 if (mWindowAttributesChanged || viewVisibilityChanged) {
5899 // If layout params have been changed, first give them
5900 // to the window manager to make sure it has the correct
5903 if ((relayoutWindow(mWindowAttributes, viewVisibility, false)
5904 & WindowManagerGlobal.RELAYOUT_RES_FIRST_TIME) != 0) {
5905 mWindowSession.finishDrawing(mWindow);
5907 } catch (RemoteException e) {
5917 WindowManagerGlobal.getInstance().doRemoveView(this);
5920 public void requestUpdateConfiguration(Configuration config) {
5921 Message msg = mHandler.obtainMessage(MSG_UPDATE_CONFIGURATION, config);
5922 mHandler.sendMessage(msg);
5925 public void loadSystemProperties() {
5926 mHandler.post(new Runnable() {
5930 mProfileRendering = SystemProperties.getBoolean(PROPERTY_PROFILE_RENDERING, false);
5931 profileRendering(mAttachInfo.mHasWindowFocus);
5933 // Hardware rendering
5934 if (mAttachInfo.mHardwareRenderer != null) {
5935 if (mAttachInfo.mHardwareRenderer.loadSystemProperties()) {
5941 boolean layout = SystemProperties.getBoolean(View.DEBUG_LAYOUT_PROPERTY, false);
5942 if (layout != mAttachInfo.mDebugLayout) {
5943 mAttachInfo.mDebugLayout = layout;
5944 if (!mHandler.hasMessages(MSG_INVALIDATE_WORLD)) {
5945 mHandler.sendEmptyMessageDelayed(MSG_INVALIDATE_WORLD, 200);
5952 private void destroyHardwareRenderer() {
5953 ThreadedRenderer hardwareRenderer = mAttachInfo.mHardwareRenderer;
5955 if (hardwareRenderer != null) {
5956 if (mView != null) {
5957 hardwareRenderer.destroyHardwareResources(mView);
5959 hardwareRenderer.destroy();
5960 hardwareRenderer.setRequested(false);
5962 mAttachInfo.mHardwareRenderer = null;
5963 mAttachInfo.mHardwareAccelerated = false;
5967 public void dispatchResized(Rect frame, Rect overscanInsets, Rect contentInsets,
5968 Rect visibleInsets, Rect stableInsets, Rect outsets, boolean reportDraw,
5969 Configuration newConfig, Rect backDropFrame, boolean forceLayout,
5970 boolean alwaysConsumeNavBar) {
5971 if (DEBUG_LAYOUT) Log.v(mTag, "Resizing " + this + ": frame=" + frame.toShortString()
5972 + " contentInsets=" + contentInsets.toShortString()
5973 + " visibleInsets=" + visibleInsets.toShortString()
5974 + " reportDraw=" + reportDraw
5975 + " backDropFrame=" + backDropFrame);
5977 // Tell all listeners that we are resizing the window so that the chrome can get
5978 // updated as fast as possible on a separate thread,
5979 if (mDragResizing) {
5980 boolean fullscreen = frame.equals(backDropFrame);
5981 synchronized (mWindowCallbacks) {
5982 for (int i = mWindowCallbacks.size() - 1; i >= 0; i--) {
5983 mWindowCallbacks.get(i).onWindowSizeIsChanging(backDropFrame, fullscreen,
5984 visibleInsets, stableInsets);
5989 Message msg = mHandler.obtainMessage(reportDraw ? MSG_RESIZED_REPORT : MSG_RESIZED);
5990 if (mTranslator != null) {
5991 mTranslator.translateRectInScreenToAppWindow(frame);
5992 mTranslator.translateRectInScreenToAppWindow(overscanInsets);
5993 mTranslator.translateRectInScreenToAppWindow(contentInsets);
5994 mTranslator.translateRectInScreenToAppWindow(visibleInsets);
5996 SomeArgs args = SomeArgs.obtain();
5997 final boolean sameProcessCall = (Binder.getCallingPid() == android.os.Process.myPid());
5998 args.arg1 = sameProcessCall ? new Rect(frame) : frame;
5999 args.arg2 = sameProcessCall ? new Rect(contentInsets) : contentInsets;
6000 args.arg3 = sameProcessCall ? new Rect(visibleInsets) : visibleInsets;
6001 args.arg4 = sameProcessCall && newConfig != null ? new Configuration(newConfig) : newConfig;
6002 args.arg5 = sameProcessCall ? new Rect(overscanInsets) : overscanInsets;
6003 args.arg6 = sameProcessCall ? new Rect(stableInsets) : stableInsets;
6004 args.arg7 = sameProcessCall ? new Rect(outsets) : outsets;
6005 args.arg8 = sameProcessCall ? new Rect(backDropFrame) : backDropFrame;
6006 args.argi1 = forceLayout ? 1 : 0;
6007 args.argi2 = alwaysConsumeNavBar ? 1 : 0;
6009 mHandler.sendMessage(msg);
6012 public void dispatchMoved(int newX, int newY) {
6013 if (DEBUG_LAYOUT) Log.v(mTag, "Window moved " + this + ": newX=" + newX + " newY=" + newY);
6014 if (mTranslator != null) {
6015 PointF point = new PointF(newX, newY);
6016 mTranslator.translatePointInScreenToAppWindow(point);
6017 newX = (int) (point.x + 0.5);
6018 newY = (int) (point.y + 0.5);
6020 Message msg = mHandler.obtainMessage(MSG_WINDOW_MOVED, newX, newY);
6021 mHandler.sendMessage(msg);
6025 * Represents a pending input event that is waiting in a queue.
6027 * Input events are processed in serial order by the timestamp specified by
6028 * {@link InputEvent#getEventTimeNano()}. In general, the input dispatcher delivers
6029 * one input event to the application at a time and waits for the application
6030 * to finish handling it before delivering the next one.
6032 * However, because the application or IME can synthesize and inject multiple
6033 * key events at a time without going through the input dispatcher, we end up
6034 * needing a queue on the application's side.
6036 private static final class QueuedInputEvent {
6037 public static final int FLAG_DELIVER_POST_IME = 1 << 0;
6038 public static final int FLAG_DEFERRED = 1 << 1;
6039 public static final int FLAG_FINISHED = 1 << 2;
6040 public static final int FLAG_FINISHED_HANDLED = 1 << 3;
6041 public static final int FLAG_RESYNTHESIZED = 1 << 4;
6042 public static final int FLAG_UNHANDLED = 1 << 5;
6044 public QueuedInputEvent mNext;
6046 public InputEvent mEvent;
6047 public InputEventReceiver mReceiver;
6050 public boolean shouldSkipIme() {
6051 if ((mFlags & FLAG_DELIVER_POST_IME) != 0) {
6054 return mEvent instanceof MotionEvent
6055 && mEvent.isFromSource(InputDevice.SOURCE_CLASS_POINTER);
6058 public boolean shouldSendToSynthesizer() {
6059 if ((mFlags & FLAG_UNHANDLED) != 0) {
6067 public String toString() {
6068 StringBuilder sb = new StringBuilder("QueuedInputEvent{flags=");
6069 boolean hasPrevious = false;
6070 hasPrevious = flagToString("DELIVER_POST_IME", FLAG_DELIVER_POST_IME, hasPrevious, sb);
6071 hasPrevious = flagToString("DEFERRED", FLAG_DEFERRED, hasPrevious, sb);
6072 hasPrevious = flagToString("FINISHED", FLAG_FINISHED, hasPrevious, sb);
6073 hasPrevious = flagToString("FINISHED_HANDLED", FLAG_FINISHED_HANDLED, hasPrevious, sb);
6074 hasPrevious = flagToString("RESYNTHESIZED", FLAG_RESYNTHESIZED, hasPrevious, sb);
6075 hasPrevious = flagToString("UNHANDLED", FLAG_UNHANDLED, hasPrevious, sb);
6079 sb.append(", hasNextQueuedEvent=" + (mEvent != null ? "true" : "false"));
6080 sb.append(", hasInputEventReceiver=" + (mReceiver != null ? "true" : "false"));
6081 sb.append(", mEvent=" + mEvent + "}");
6082 return sb.toString();
6085 private boolean flagToString(String name, int flag,
6086 boolean hasPrevious, StringBuilder sb) {
6087 if ((mFlags & flag) != 0) {
6098 private QueuedInputEvent obtainQueuedInputEvent(InputEvent event,
6099 InputEventReceiver receiver, int flags) {
6100 QueuedInputEvent q = mQueuedInputEventPool;
6102 mQueuedInputEventPoolSize -= 1;
6103 mQueuedInputEventPool = q.mNext;
6106 q = new QueuedInputEvent();
6110 q.mReceiver = receiver;
6115 private void recycleQueuedInputEvent(QueuedInputEvent q) {
6119 if (mQueuedInputEventPoolSize < MAX_QUEUED_INPUT_EVENT_POOL_SIZE) {
6120 mQueuedInputEventPoolSize += 1;
6121 q.mNext = mQueuedInputEventPool;
6122 mQueuedInputEventPool = q;
6126 void enqueueInputEvent(InputEvent event) {
6127 enqueueInputEvent(event, null, 0, false);
6130 void enqueueInputEvent(InputEvent event,
6131 InputEventReceiver receiver, int flags, boolean processImmediately) {
6132 adjustInputEventForCompatibility(event);
6133 QueuedInputEvent q = obtainQueuedInputEvent(event, receiver, flags);
6135 // Always enqueue the input event in order, regardless of its time stamp.
6136 // We do this because the application or the IME may inject key events
6137 // in response to touch events and we want to ensure that the injected keys
6138 // are processed in the order they were received and we cannot trust that
6139 // the time stamp of injected events are monotonic.
6140 QueuedInputEvent last = mPendingInputEventTail;
6142 mPendingInputEventHead = q;
6143 mPendingInputEventTail = q;
6146 mPendingInputEventTail = q;
6148 mPendingInputEventCount += 1;
6149 Trace.traceCounter(Trace.TRACE_TAG_INPUT, mPendingInputEventQueueLengthCounterName,
6150 mPendingInputEventCount);
6152 if (processImmediately) {
6153 doProcessInputEvents();
6155 scheduleProcessInputEvents();
6159 private void scheduleProcessInputEvents() {
6160 if (!mProcessInputEventsScheduled) {
6161 mProcessInputEventsScheduled = true;
6162 Message msg = mHandler.obtainMessage(MSG_PROCESS_INPUT_EVENTS);
6163 msg.setAsynchronous(true);
6164 mHandler.sendMessage(msg);
6168 void doProcessInputEvents() {
6169 // Deliver all pending input events in the queue.
6170 while (mPendingInputEventHead != null) {
6171 QueuedInputEvent q = mPendingInputEventHead;
6172 mPendingInputEventHead = q.mNext;
6173 if (mPendingInputEventHead == null) {
6174 mPendingInputEventTail = null;
6178 mPendingInputEventCount -= 1;
6179 Trace.traceCounter(Trace.TRACE_TAG_INPUT, mPendingInputEventQueueLengthCounterName,
6180 mPendingInputEventCount);
6182 long eventTime = q.mEvent.getEventTimeNano();
6183 long oldestEventTime = eventTime;
6184 if (q.mEvent instanceof MotionEvent) {
6185 MotionEvent me = (MotionEvent)q.mEvent;
6186 if (me.getHistorySize() > 0) {
6187 oldestEventTime = me.getHistoricalEventTimeNano(0);
6190 mChoreographer.mFrameInfo.updateInputEventTime(eventTime, oldestEventTime);
6192 deliverInputEvent(q);
6195 // We are done processing all input events that we can process right now
6196 // so we can clear the pending flag immediately.
6197 if (mProcessInputEventsScheduled) {
6198 mProcessInputEventsScheduled = false;
6199 mHandler.removeMessages(MSG_PROCESS_INPUT_EVENTS);
6203 private void deliverInputEvent(QueuedInputEvent q) {
6204 Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW, "deliverInputEvent",
6205 q.mEvent.getSequenceNumber());
6206 if (mInputEventConsistencyVerifier != null) {
6207 mInputEventConsistencyVerifier.onInputEvent(q.mEvent, 0);
6211 if (q.shouldSendToSynthesizer()) {
6212 stage = mSyntheticInputStage;
6214 stage = q.shouldSkipIme() ? mFirstPostImeInputStage : mFirstInputStage;
6217 if (stage != null) {
6220 finishInputEvent(q);
6224 private void finishInputEvent(QueuedInputEvent q) {
6225 Trace.asyncTraceEnd(Trace.TRACE_TAG_VIEW, "deliverInputEvent",
6226 q.mEvent.getSequenceNumber());
6228 if (q.mReceiver != null) {
6229 boolean handled = (q.mFlags & QueuedInputEvent.FLAG_FINISHED_HANDLED) != 0;
6230 q.mReceiver.finishInputEvent(q.mEvent, handled);
6232 q.mEvent.recycleIfNeededAfterDispatch();
6235 recycleQueuedInputEvent(q);
6238 private void adjustInputEventForCompatibility(InputEvent e) {
6239 if (mTargetSdkVersion < Build.VERSION_CODES.M && e instanceof MotionEvent) {
6240 MotionEvent motion = (MotionEvent) e;
6242 MotionEvent.BUTTON_STYLUS_PRIMARY | MotionEvent.BUTTON_STYLUS_SECONDARY;
6243 final int buttonState = motion.getButtonState();
6244 final int compatButtonState = (buttonState & mask) >> 4;
6245 if (compatButtonState != 0) {
6246 motion.setButtonState(buttonState | compatButtonState);
6251 static boolean isTerminalInputEvent(InputEvent event) {
6252 if (event instanceof KeyEvent) {
6253 final KeyEvent keyEvent = (KeyEvent)event;
6254 return keyEvent.getAction() == KeyEvent.ACTION_UP;
6256 final MotionEvent motionEvent = (MotionEvent)event;
6257 final int action = motionEvent.getAction();
6258 return action == MotionEvent.ACTION_UP
6259 || action == MotionEvent.ACTION_CANCEL
6260 || action == MotionEvent.ACTION_HOVER_EXIT;
6264 void scheduleConsumeBatchedInput() {
6265 if (!mConsumeBatchedInputScheduled) {
6266 mConsumeBatchedInputScheduled = true;
6267 mChoreographer.postCallback(Choreographer.CALLBACK_INPUT,
6268 mConsumedBatchedInputRunnable, null);
6272 void unscheduleConsumeBatchedInput() {
6273 if (mConsumeBatchedInputScheduled) {
6274 mConsumeBatchedInputScheduled = false;
6275 mChoreographer.removeCallbacks(Choreographer.CALLBACK_INPUT,
6276 mConsumedBatchedInputRunnable, null);
6280 void scheduleConsumeBatchedInputImmediately() {
6281 if (!mConsumeBatchedInputImmediatelyScheduled) {
6282 unscheduleConsumeBatchedInput();
6283 mConsumeBatchedInputImmediatelyScheduled = true;
6284 mHandler.post(mConsumeBatchedInputImmediatelyRunnable);
6288 void doConsumeBatchedInput(long frameTimeNanos) {
6289 if (mConsumeBatchedInputScheduled) {
6290 mConsumeBatchedInputScheduled = false;
6291 if (mInputEventReceiver != null) {
6292 if (mInputEventReceiver.consumeBatchedInputEvents(frameTimeNanos)
6293 && frameTimeNanos != -1) {
6294 // If we consumed a batch here, we want to go ahead and schedule the
6295 // consumption of batched input events on the next frame. Otherwise, we would
6296 // wait until we have more input events pending and might get starved by other
6297 // things occurring in the process. If the frame time is -1, however, then
6298 // we're in a non-batching mode, so there's no need to schedule this.
6299 scheduleConsumeBatchedInput();
6302 doProcessInputEvents();
6306 final class TraversalRunnable implements Runnable {
6312 final TraversalRunnable mTraversalRunnable = new TraversalRunnable();
6314 final class WindowInputEventReceiver extends InputEventReceiver {
6315 public WindowInputEventReceiver(InputChannel inputChannel, Looper looper) {
6316 super(inputChannel, looper);
6320 public void onInputEvent(InputEvent event) {
6321 enqueueInputEvent(event, this, 0, true);
6325 public void onBatchedInputEventPending() {
6326 if (mUnbufferedInputDispatch) {
6327 super.onBatchedInputEventPending();
6329 scheduleConsumeBatchedInput();
6334 public void dispose() {
6335 unscheduleConsumeBatchedInput();
6339 WindowInputEventReceiver mInputEventReceiver;
6341 final class ConsumeBatchedInputRunnable implements Runnable {
6344 doConsumeBatchedInput(mChoreographer.getFrameTimeNanos());
6347 final ConsumeBatchedInputRunnable mConsumedBatchedInputRunnable =
6348 new ConsumeBatchedInputRunnable();
6349 boolean mConsumeBatchedInputScheduled;
6351 final class ConsumeBatchedInputImmediatelyRunnable implements Runnable {
6354 doConsumeBatchedInput(-1);
6357 final ConsumeBatchedInputImmediatelyRunnable mConsumeBatchedInputImmediatelyRunnable =
6358 new ConsumeBatchedInputImmediatelyRunnable();
6359 boolean mConsumeBatchedInputImmediatelyScheduled;
6361 final class InvalidateOnAnimationRunnable implements Runnable {
6362 private boolean mPosted;
6363 private final ArrayList<View> mViews = new ArrayList<View>();
6364 private final ArrayList<AttachInfo.InvalidateInfo> mViewRects =
6365 new ArrayList<AttachInfo.InvalidateInfo>();
6366 private View[] mTempViews;
6367 private AttachInfo.InvalidateInfo[] mTempViewRects;
6369 public void addView(View view) {
6370 synchronized (this) {
6372 postIfNeededLocked();
6376 public void addViewRect(AttachInfo.InvalidateInfo info) {
6377 synchronized (this) {
6378 mViewRects.add(info);
6379 postIfNeededLocked();
6383 public void removeView(View view) {
6384 synchronized (this) {
6385 mViews.remove(view);
6387 for (int i = mViewRects.size(); i-- > 0; ) {
6388 AttachInfo.InvalidateInfo info = mViewRects.get(i);
6389 if (info.target == view) {
6390 mViewRects.remove(i);
6395 if (mPosted && mViews.isEmpty() && mViewRects.isEmpty()) {
6396 mChoreographer.removeCallbacks(Choreographer.CALLBACK_ANIMATION, this, null);
6404 final int viewCount;
6405 final int viewRectCount;
6406 synchronized (this) {
6409 viewCount = mViews.size();
6410 if (viewCount != 0) {
6411 mTempViews = mViews.toArray(mTempViews != null
6412 ? mTempViews : new View[viewCount]);
6416 viewRectCount = mViewRects.size();
6417 if (viewRectCount != 0) {
6418 mTempViewRects = mViewRects.toArray(mTempViewRects != null
6419 ? mTempViewRects : new AttachInfo.InvalidateInfo[viewRectCount]);
6424 for (int i = 0; i < viewCount; i++) {
6425 mTempViews[i].invalidate();
6426 mTempViews[i] = null;
6429 for (int i = 0; i < viewRectCount; i++) {
6430 final View.AttachInfo.InvalidateInfo info = mTempViewRects[i];
6431 info.target.invalidate(info.left, info.top, info.right, info.bottom);
6436 private void postIfNeededLocked() {
6438 mChoreographer.postCallback(Choreographer.CALLBACK_ANIMATION, this, null);
6443 final InvalidateOnAnimationRunnable mInvalidateOnAnimationRunnable =
6444 new InvalidateOnAnimationRunnable();
6446 public void dispatchInvalidateDelayed(View view, long delayMilliseconds) {
6447 Message msg = mHandler.obtainMessage(MSG_INVALIDATE, view);
6448 mHandler.sendMessageDelayed(msg, delayMilliseconds);
6451 public void dispatchInvalidateRectDelayed(AttachInfo.InvalidateInfo info,
6452 long delayMilliseconds) {
6453 final Message msg = mHandler.obtainMessage(MSG_INVALIDATE_RECT, info);
6454 mHandler.sendMessageDelayed(msg, delayMilliseconds);
6457 public void dispatchInvalidateOnAnimation(View view) {
6458 mInvalidateOnAnimationRunnable.addView(view);
6461 public void dispatchInvalidateRectOnAnimation(AttachInfo.InvalidateInfo info) {
6462 mInvalidateOnAnimationRunnable.addViewRect(info);
6465 public void cancelInvalidate(View view) {
6466 mHandler.removeMessages(MSG_INVALIDATE, view);
6467 // fixme: might leak the AttachInfo.InvalidateInfo objects instead of returning
6469 mHandler.removeMessages(MSG_INVALIDATE_RECT, view);
6470 mInvalidateOnAnimationRunnable.removeView(view);
6473 public void dispatchInputEvent(InputEvent event) {
6474 dispatchInputEvent(event, null);
6477 public void dispatchInputEvent(InputEvent event, InputEventReceiver receiver) {
6478 SomeArgs args = SomeArgs.obtain();
6480 args.arg2 = receiver;
6481 Message msg = mHandler.obtainMessage(MSG_DISPATCH_INPUT_EVENT, args);
6482 msg.setAsynchronous(true);
6483 mHandler.sendMessage(msg);
6486 public void synthesizeInputEvent(InputEvent event) {
6487 Message msg = mHandler.obtainMessage(MSG_SYNTHESIZE_INPUT_EVENT, event);
6488 msg.setAsynchronous(true);
6489 mHandler.sendMessage(msg);
6492 public void dispatchKeyFromIme(KeyEvent event) {
6493 Message msg = mHandler.obtainMessage(MSG_DISPATCH_KEY_FROM_IME, event);
6494 msg.setAsynchronous(true);
6495 mHandler.sendMessage(msg);
6499 * Reinject unhandled {@link InputEvent}s in order to synthesize fallbacks events.
6501 * Note that it is the responsibility of the caller of this API to recycle the InputEvent it
6504 public void dispatchUnhandledInputEvent(InputEvent event) {
6505 if (event instanceof MotionEvent) {
6506 event = MotionEvent.obtain((MotionEvent) event);
6508 synthesizeInputEvent(event);
6511 public void dispatchAppVisibility(boolean visible) {
6512 Message msg = mHandler.obtainMessage(MSG_DISPATCH_APP_VISIBILITY);
6513 msg.arg1 = visible ? 1 : 0;
6514 mHandler.sendMessage(msg);
6517 public void dispatchGetNewSurface() {
6518 Message msg = mHandler.obtainMessage(MSG_DISPATCH_GET_NEW_SURFACE);
6519 mHandler.sendMessage(msg);
6522 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
6523 Message msg = Message.obtain();
6524 msg.what = MSG_WINDOW_FOCUS_CHANGED;
6525 msg.arg1 = hasFocus ? 1 : 0;
6526 msg.arg2 = inTouchMode ? 1 : 0;
6527 mHandler.sendMessage(msg);
6530 public void dispatchWindowShown() {
6531 mHandler.sendEmptyMessage(MSG_DISPATCH_WINDOW_SHOWN);
6534 public void dispatchCloseSystemDialogs(String reason) {
6535 Message msg = Message.obtain();
6536 msg.what = MSG_CLOSE_SYSTEM_DIALOGS;
6538 mHandler.sendMessage(msg);
6541 public void dispatchDragEvent(DragEvent event) {
6543 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
6544 what = MSG_DISPATCH_DRAG_LOCATION_EVENT;
6545 mHandler.removeMessages(what);
6547 what = MSG_DISPATCH_DRAG_EVENT;
6549 Message msg = mHandler.obtainMessage(what, event);
6550 mHandler.sendMessage(msg);
6553 public void updatePointerIcon(float x, float y) {
6554 final int what = MSG_UPDATE_POINTER_ICON;
6555 mHandler.removeMessages(what);
6556 final long now = SystemClock.uptimeMillis();
6557 final MotionEvent event = MotionEvent.obtain(
6558 0, now, MotionEvent.ACTION_HOVER_MOVE, x, y, 0);
6559 Message msg = mHandler.obtainMessage(what, event);
6560 mHandler.sendMessage(msg);
6563 public void dispatchSystemUiVisibilityChanged(int seq, int globalVisibility,
6564 int localValue, int localChanges) {
6565 SystemUiVisibilityInfo args = new SystemUiVisibilityInfo();
6567 args.globalVisibility = globalVisibility;
6568 args.localValue = localValue;
6569 args.localChanges = localChanges;
6570 mHandler.sendMessage(mHandler.obtainMessage(MSG_DISPATCH_SYSTEM_UI_VISIBILITY, args));
6573 public void dispatchCheckFocus() {
6574 if (!mHandler.hasMessages(MSG_CHECK_FOCUS)) {
6575 // This will result in a call to checkFocus() below.
6576 mHandler.sendEmptyMessage(MSG_CHECK_FOCUS);
6580 public void dispatchRequestKeyboardShortcuts(IResultReceiver receiver, int deviceId) {
6581 mHandler.obtainMessage(
6582 MSG_REQUEST_KEYBOARD_SHORTCUTS, deviceId, 0, receiver).sendToTarget();
6586 * Post a callback to send a
6587 * {@link AccessibilityEvent#TYPE_WINDOW_CONTENT_CHANGED} event.
6588 * This event is send at most once every
6589 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
6591 private void postSendWindowContentChangedCallback(View source, int changeType) {
6592 if (mSendWindowContentChangedAccessibilityEvent == null) {
6593 mSendWindowContentChangedAccessibilityEvent =
6594 new SendWindowContentChangedAccessibilityEvent();
6596 mSendWindowContentChangedAccessibilityEvent.runOrPost(source, changeType);
6600 * Remove a posted callback to send a
6601 * {@link AccessibilityEvent#TYPE_WINDOW_CONTENT_CHANGED} event.
6603 private void removeSendWindowContentChangedCallback() {
6604 if (mSendWindowContentChangedAccessibilityEvent != null) {
6605 mHandler.removeCallbacks(mSendWindowContentChangedAccessibilityEvent);
6610 public boolean showContextMenuForChild(View originalView) {
6615 public boolean showContextMenuForChild(View originalView, float x, float y) {
6620 public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) {
6625 public ActionMode startActionModeForChild(
6626 View originalView, ActionMode.Callback callback, int type) {
6631 public void createContextMenu(ContextMenu menu) {
6635 public void childDrawableStateChanged(View child) {
6639 public boolean requestSendAccessibilityEvent(View child, AccessibilityEvent event) {
6640 if (mView == null || mStopped || mPausedForTransition) {
6643 // Intercept accessibility focus events fired by virtual nodes to keep
6644 // track of accessibility focus position in such nodes.
6645 final int eventType = event.getEventType();
6646 switch (eventType) {
6647 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
6648 final long sourceNodeId = event.getSourceNodeId();
6649 final int accessibilityViewId = AccessibilityNodeInfo.getAccessibilityViewId(
6651 View source = mView.findViewByAccessibilityId(accessibilityViewId);
6652 if (source != null) {
6653 AccessibilityNodeProvider provider = source.getAccessibilityNodeProvider();
6654 if (provider != null) {
6655 final int virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
6657 final AccessibilityNodeInfo node;
6658 if (virtualNodeId == AccessibilityNodeInfo.UNDEFINED_ITEM_ID) {
6659 node = provider.createAccessibilityNodeInfo(
6660 AccessibilityNodeProvider.HOST_VIEW_ID);
6662 node = provider.createAccessibilityNodeInfo(virtualNodeId);
6664 setAccessibilityFocus(source, node);
6668 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
6669 final long sourceNodeId = event.getSourceNodeId();
6670 final int accessibilityViewId = AccessibilityNodeInfo.getAccessibilityViewId(
6672 View source = mView.findViewByAccessibilityId(accessibilityViewId);
6673 if (source != null) {
6674 AccessibilityNodeProvider provider = source.getAccessibilityNodeProvider();
6675 if (provider != null) {
6676 setAccessibilityFocus(null, null);
6682 case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED: {
6683 handleWindowContentChangedEvent(event);
6686 mAccessibilityManager.sendAccessibilityEvent(event);
6691 * Updates the focused virtual view, when necessary, in response to a
6692 * content changed event.
6694 * This is necessary to get updated bounds after a position change.
6696 * @param event an accessibility event of type
6697 * {@link AccessibilityEvent#TYPE_WINDOW_CONTENT_CHANGED}
6699 private void handleWindowContentChangedEvent(AccessibilityEvent event) {
6700 final View focusedHost = mAccessibilityFocusedHost;
6701 if (focusedHost == null || mAccessibilityFocusedVirtualView == null) {
6702 // No virtual view focused, nothing to do here.
6706 final AccessibilityNodeProvider provider = focusedHost.getAccessibilityNodeProvider();
6707 if (provider == null) {
6708 // Error state: virtual view with no provider. Clear focus.
6709 mAccessibilityFocusedHost = null;
6710 mAccessibilityFocusedVirtualView = null;
6711 focusedHost.clearAccessibilityFocusNoCallbacks(0);
6715 // We only care about change types that may affect the bounds of the
6716 // focused virtual view.
6717 final int changes = event.getContentChangeTypes();
6718 if ((changes & AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE) == 0
6719 && changes != AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED) {
6723 final long eventSourceNodeId = event.getSourceNodeId();
6724 final int changedViewId = AccessibilityNodeInfo.getAccessibilityViewId(eventSourceNodeId);
6726 // Search up the tree for subtree containment.
6727 boolean hostInSubtree = false;
6728 View root = mAccessibilityFocusedHost;
6729 while (root != null && !hostInSubtree) {
6730 if (changedViewId == root.getAccessibilityViewId()) {
6731 hostInSubtree = true;
6733 final ViewParent parent = root.getParent();
6734 if (parent instanceof View) {
6735 root = (View) parent;
6742 // We care only about changes in subtrees containing the host view.
6743 if (!hostInSubtree) {
6747 final long focusedSourceNodeId = mAccessibilityFocusedVirtualView.getSourceNodeId();
6748 int focusedChildId = AccessibilityNodeInfo.getVirtualDescendantId(focusedSourceNodeId);
6749 if (focusedChildId == AccessibilityNodeInfo.UNDEFINED_ITEM_ID) {
6750 // TODO: Should we clear the focused virtual view?
6751 focusedChildId = AccessibilityNodeProvider.HOST_VIEW_ID;
6754 // Refresh the node for the focused virtual view.
6755 final Rect oldBounds = mTempRect;
6756 mAccessibilityFocusedVirtualView.getBoundsInScreen(oldBounds);
6757 mAccessibilityFocusedVirtualView = provider.createAccessibilityNodeInfo(focusedChildId);
6758 if (mAccessibilityFocusedVirtualView == null) {
6759 // Error state: The node no longer exists. Clear focus.
6760 mAccessibilityFocusedHost = null;
6761 focusedHost.clearAccessibilityFocusNoCallbacks(0);
6763 // This will probably fail, but try to keep the provider's internal
6764 // state consistent by clearing focus.
6765 provider.performAction(focusedChildId,
6766 AccessibilityAction.ACTION_CLEAR_ACCESSIBILITY_FOCUS.getId(), null);
6767 invalidateRectOnScreen(oldBounds);
6769 // The node was refreshed, invalidate bounds if necessary.
6770 final Rect newBounds = mAccessibilityFocusedVirtualView.getBoundsInScreen();
6771 if (!oldBounds.equals(newBounds)) {
6772 oldBounds.union(newBounds);
6773 invalidateRectOnScreen(oldBounds);
6779 public void notifySubtreeAccessibilityStateChanged(View child, View source, int changeType) {
6780 postSendWindowContentChangedCallback(source, changeType);
6784 public boolean canResolveLayoutDirection() {
6789 public boolean isLayoutDirectionResolved() {
6794 public int getLayoutDirection() {
6795 return View.LAYOUT_DIRECTION_RESOLVED_DEFAULT;
6799 public boolean canResolveTextDirection() {
6804 public boolean isTextDirectionResolved() {
6809 public int getTextDirection() {
6810 return View.TEXT_DIRECTION_RESOLVED_DEFAULT;
6814 public boolean canResolveTextAlignment() {
6819 public boolean isTextAlignmentResolved() {
6824 public int getTextAlignment() {
6825 return View.TEXT_ALIGNMENT_RESOLVED_DEFAULT;
6828 private View getCommonPredecessor(View first, View second) {
6829 if (mTempHashSet == null) {
6830 mTempHashSet = new HashSet<View>();
6832 HashSet<View> seen = mTempHashSet;
6834 View firstCurrent = first;
6835 while (firstCurrent != null) {
6836 seen.add(firstCurrent);
6837 ViewParent firstCurrentParent = firstCurrent.mParent;
6838 if (firstCurrentParent instanceof View) {
6839 firstCurrent = (View) firstCurrentParent;
6841 firstCurrent = null;
6844 View secondCurrent = second;
6845 while (secondCurrent != null) {
6846 if (seen.contains(secondCurrent)) {
6848 return secondCurrent;
6850 ViewParent secondCurrentParent = secondCurrent.mParent;
6851 if (secondCurrentParent instanceof View) {
6852 secondCurrent = (View) secondCurrentParent;
6854 secondCurrent = null;
6861 void checkThread() {
6862 if (mThread != Thread.currentThread()) {
6863 throw new CalledFromWrongThreadException(
6864 "Only the original thread that created a view hierarchy can touch its views.");
6869 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
6870 // ViewAncestor never intercepts touch event, so this can be a no-op
6874 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
6875 if (rectangle == null) {
6876 return scrollToRectOrFocus(null, immediate);
6878 rectangle.offset(child.getLeft() - child.getScrollX(),
6879 child.getTop() - child.getScrollY());
6880 final boolean scrolled = scrollToRectOrFocus(rectangle, immediate);
6881 mTempRect.set(rectangle);
6882 mTempRect.offset(0, -mCurScrollY);
6883 mTempRect.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
6885 mWindowSession.onRectangleOnScreenRequested(mWindow, mTempRect);
6886 } catch (RemoteException re) {
6893 public void childHasTransientStateChanged(View child, boolean hasTransientState) {
6898 public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
6903 public void onStopNestedScroll(View target) {
6907 public void onNestedScrollAccepted(View child, View target, int nestedScrollAxes) {
6911 public void onNestedScroll(View target, int dxConsumed, int dyConsumed,
6912 int dxUnconsumed, int dyUnconsumed) {
6916 public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
6920 public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
6925 public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
6930 public boolean onNestedPrePerformAccessibilityAction(View target, int action, Bundle args) {
6935 * Force the window to report its next draw.
6937 * This method is only supposed to be used to speed up the interaction from SystemUI and window
6938 * manager when waiting for the first frame to be drawn when turning on the screen. DO NOT USE
6939 * unless you fully understand this interaction.
6942 public void setReportNextDraw() {
6943 mReportNextDraw = true;
6947 void changeCanvasOpacity(boolean opaque) {
6948 Log.d(mTag, "changeCanvasOpacity: opaque=" + opaque);
6949 if (mAttachInfo.mHardwareRenderer != null) {
6950 mAttachInfo.mHardwareRenderer.setOpaque(opaque);
6954 class TakenSurfaceHolder extends BaseSurfaceHolder {
6956 public boolean onAllowLockCanvas() {
6957 return mDrawingAllowed;
6961 public void onRelayoutContainer() {
6962 // Not currently interesting -- from changing between fixed and layout size.
6966 public void setFormat(int format) {
6967 ((RootViewSurfaceTaker)mView).setSurfaceFormat(format);
6971 public void setType(int type) {
6972 ((RootViewSurfaceTaker)mView).setSurfaceType(type);
6976 public void onUpdateSurface() {
6977 // We take care of format and type changes on our own.
6978 throw new IllegalStateException("Shouldn't be here");
6982 public boolean isCreating() {
6987 public void setFixedSize(int width, int height) {
6988 throw new UnsupportedOperationException(
6989 "Currently only support sizing from layout");
6993 public void setKeepScreenOn(boolean screenOn) {
6994 ((RootViewSurfaceTaker)mView).setSurfaceKeepScreenOn(screenOn);
6998 static class W extends IWindow.Stub {
6999 private final WeakReference<ViewRootImpl> mViewAncestor;
7000 private final IWindowSession mWindowSession;
7002 W(ViewRootImpl viewAncestor) {
7003 mViewAncestor = new WeakReference<ViewRootImpl>(viewAncestor);
7004 mWindowSession = viewAncestor.mWindowSession;
7008 public void resized(Rect frame, Rect overscanInsets, Rect contentInsets,
7009 Rect visibleInsets, Rect stableInsets, Rect outsets, boolean reportDraw,
7010 Configuration newConfig, Rect backDropFrame, boolean forceLayout,
7011 boolean alwaysConsumeNavBar) {
7012 final ViewRootImpl viewAncestor = mViewAncestor.get();
7013 if (viewAncestor != null) {
7014 viewAncestor.dispatchResized(frame, overscanInsets, contentInsets,
7015 visibleInsets, stableInsets, outsets, reportDraw, newConfig, backDropFrame,
7016 forceLayout, alwaysConsumeNavBar);
7021 public void moved(int newX, int newY) {
7022 final ViewRootImpl viewAncestor = mViewAncestor.get();
7023 if (viewAncestor != null) {
7024 viewAncestor.dispatchMoved(newX, newY);
7029 public void dispatchAppVisibility(boolean visible) {
7030 final ViewRootImpl viewAncestor = mViewAncestor.get();
7031 if (viewAncestor != null) {
7032 viewAncestor.dispatchAppVisibility(visible);
7037 public void dispatchGetNewSurface() {
7038 final ViewRootImpl viewAncestor = mViewAncestor.get();
7039 if (viewAncestor != null) {
7040 viewAncestor.dispatchGetNewSurface();
7045 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
7046 final ViewRootImpl viewAncestor = mViewAncestor.get();
7047 if (viewAncestor != null) {
7048 viewAncestor.windowFocusChanged(hasFocus, inTouchMode);
7052 private static int checkCallingPermission(String permission) {
7054 return ActivityManagerNative.getDefault().checkPermission(
7055 permission, Binder.getCallingPid(), Binder.getCallingUid());
7056 } catch (RemoteException e) {
7057 return PackageManager.PERMISSION_DENIED;
7062 public void executeCommand(String command, String parameters, ParcelFileDescriptor out) {
7063 final ViewRootImpl viewAncestor = mViewAncestor.get();
7064 if (viewAncestor != null) {
7065 final View view = viewAncestor.mView;
7067 if (checkCallingPermission(Manifest.permission.DUMP) !=
7068 PackageManager.PERMISSION_GRANTED) {
7069 throw new SecurityException("Insufficient permissions to invoke"
7070 + " executeCommand() from pid=" + Binder.getCallingPid()
7071 + ", uid=" + Binder.getCallingUid());
7074 OutputStream clientStream = null;
7076 clientStream = new ParcelFileDescriptor.AutoCloseOutputStream(out);
7077 ViewDebug.dispatchCommand(view, command, parameters, clientStream);
7078 } catch (IOException e) {
7079 e.printStackTrace();
7081 if (clientStream != null) {
7083 clientStream.close();
7084 } catch (IOException e) {
7085 e.printStackTrace();
7094 public void closeSystemDialogs(String reason) {
7095 final ViewRootImpl viewAncestor = mViewAncestor.get();
7096 if (viewAncestor != null) {
7097 viewAncestor.dispatchCloseSystemDialogs(reason);
7102 public void dispatchWallpaperOffsets(float x, float y, float xStep, float yStep,
7106 mWindowSession.wallpaperOffsetsComplete(asBinder());
7107 } catch (RemoteException e) {
7113 public void dispatchWallpaperCommand(String action, int x, int y,
7114 int z, Bundle extras, boolean sync) {
7117 mWindowSession.wallpaperCommandComplete(asBinder(), null);
7118 } catch (RemoteException e) {
7125 public void dispatchDragEvent(DragEvent event) {
7126 final ViewRootImpl viewAncestor = mViewAncestor.get();
7127 if (viewAncestor != null) {
7128 viewAncestor.dispatchDragEvent(event);
7133 public void updatePointerIcon(float x, float y) {
7134 final ViewRootImpl viewAncestor = mViewAncestor.get();
7135 if (viewAncestor != null) {
7136 viewAncestor.updatePointerIcon(x, y);
7141 public void dispatchSystemUiVisibilityChanged(int seq, int globalVisibility,
7142 int localValue, int localChanges) {
7143 final ViewRootImpl viewAncestor = mViewAncestor.get();
7144 if (viewAncestor != null) {
7145 viewAncestor.dispatchSystemUiVisibilityChanged(seq, globalVisibility,
7146 localValue, localChanges);
7151 public void dispatchWindowShown() {
7152 final ViewRootImpl viewAncestor = mViewAncestor.get();
7153 if (viewAncestor != null) {
7154 viewAncestor.dispatchWindowShown();
7159 public void requestAppKeyboardShortcuts(IResultReceiver receiver, int deviceId) {
7160 ViewRootImpl viewAncestor = mViewAncestor.get();
7161 if (viewAncestor != null) {
7162 viewAncestor.dispatchRequestKeyboardShortcuts(receiver, deviceId);
7167 public static final class CalledFromWrongThreadException extends AndroidRuntimeException {
7168 public CalledFromWrongThreadException(String msg) {
7173 static HandlerActionQueue getRunQueue() {
7174 HandlerActionQueue rq = sRunQueues.get();
7178 rq = new HandlerActionQueue();
7184 * Start a drag resizing which will inform all listeners that a window resize is taking place.
7186 private void startDragResizing(Rect initialBounds, boolean fullscreen, Rect systemInsets,
7187 Rect stableInsets, int resizeMode) {
7188 if (!mDragResizing) {
7189 mDragResizing = true;
7190 for (int i = mWindowCallbacks.size() - 1; i >= 0; i--) {
7191 mWindowCallbacks.get(i).onWindowDragResizeStart(initialBounds, fullscreen,
7192 systemInsets, stableInsets, resizeMode);
7194 mFullRedrawNeeded = true;
7199 * End a drag resize which will inform all listeners that a window resize has ended.
7201 private void endDragResizing() {
7202 if (mDragResizing) {
7203 mDragResizing = false;
7204 for (int i = mWindowCallbacks.size() - 1; i >= 0; i--) {
7205 mWindowCallbacks.get(i).onWindowDragResizeEnd();
7207 mFullRedrawNeeded = true;
7211 private boolean updateContentDrawBounds() {
7212 boolean updated = false;
7213 for (int i = mWindowCallbacks.size() - 1; i >= 0; i--) {
7214 updated |= mWindowCallbacks.get(i).onContentDrawn(
7215 mWindowAttributes.surfaceInsets.left,
7216 mWindowAttributes.surfaceInsets.top,
7219 return updated | (mDragResizing && mReportNextDraw);
7222 private void requestDrawWindow() {
7223 if (mReportNextDraw) {
7224 mWindowDrawCountDown = new CountDownLatch(mWindowCallbacks.size());
7226 for (int i = mWindowCallbacks.size() - 1; i >= 0; i--) {
7227 mWindowCallbacks.get(i).onRequestDraw(mReportNextDraw);
7232 * Tells this instance that its corresponding activity has just relaunched. In this case, we
7233 * need to force a relayout of the window to make sure we get the correct bounds from window
7236 public void reportActivityRelaunched() {
7237 mActivityRelaunched = true;
7241 * Class for managing the accessibility interaction connection
7242 * based on the global accessibility state.
7244 final class AccessibilityInteractionConnectionManager
7245 implements AccessibilityStateChangeListener {
7247 public void onAccessibilityStateChanged(boolean enabled) {
7250 if (mAttachInfo.mHasWindowFocus) {
7251 mView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
7252 View focusedView = mView.findFocus();
7253 if (focusedView != null && focusedView != mView) {
7254 focusedView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
7258 ensureNoConnection();
7259 mHandler.obtainMessage(MSG_CLEAR_ACCESSIBILITY_FOCUS_HOST).sendToTarget();
7263 public void ensureConnection() {
7264 final boolean registered =
7265 mAttachInfo.mAccessibilityWindowId != AccessibilityNodeInfo.UNDEFINED_ITEM_ID;
7267 mAttachInfo.mAccessibilityWindowId =
7268 mAccessibilityManager.addAccessibilityInteractionConnection(mWindow,
7269 new AccessibilityInteractionConnection(ViewRootImpl.this));
7273 public void ensureNoConnection() {
7274 final boolean registered =
7275 mAttachInfo.mAccessibilityWindowId != AccessibilityNodeInfo.UNDEFINED_ITEM_ID;
7277 mAttachInfo.mAccessibilityWindowId = AccessibilityNodeInfo.UNDEFINED_ITEM_ID;
7278 mAccessibilityManager.removeAccessibilityInteractionConnection(mWindow);
7283 final class HighContrastTextManager implements HighTextContrastChangeListener {
7284 HighContrastTextManager() {
7285 mAttachInfo.mHighContrastText = mAccessibilityManager.isHighTextContrastEnabled();
7288 public void onHighTextContrastStateChanged(boolean enabled) {
7289 mAttachInfo.mHighContrastText = enabled;
7291 // Destroy Displaylists so they can be recreated with high contrast recordings
7292 destroyHardwareResources();
7294 // Schedule redraw, which will rerecord + redraw all text
7300 * This class is an interface this ViewAncestor provides to the
7301 * AccessibilityManagerService to the latter can interact with
7302 * the view hierarchy in this ViewAncestor.
7304 static final class AccessibilityInteractionConnection
7305 extends IAccessibilityInteractionConnection.Stub {
7306 private final WeakReference<ViewRootImpl> mViewRootImpl;
7308 AccessibilityInteractionConnection(ViewRootImpl viewRootImpl) {
7309 mViewRootImpl = new WeakReference<ViewRootImpl>(viewRootImpl);
7313 public void findAccessibilityNodeInfoByAccessibilityId(long accessibilityNodeId,
7314 Region interactiveRegion, int interactionId,
7315 IAccessibilityInteractionConnectionCallback callback, int flags,
7316 int interrogatingPid, long interrogatingTid, MagnificationSpec spec) {
7317 ViewRootImpl viewRootImpl = mViewRootImpl.get();
7318 if (viewRootImpl != null && viewRootImpl.mView != null) {
7319 viewRootImpl.getAccessibilityInteractionController()
7320 .findAccessibilityNodeInfoByAccessibilityIdClientThread(accessibilityNodeId,
7321 interactiveRegion, interactionId, callback, flags, interrogatingPid,
7322 interrogatingTid, spec);
7324 // We cannot make the call and notify the caller so it does not wait.
7326 callback.setFindAccessibilityNodeInfosResult(null, interactionId);
7327 } catch (RemoteException re) {
7328 /* best effort - ignore */
7334 public void performAccessibilityAction(long accessibilityNodeId, int action,
7335 Bundle arguments, int interactionId,
7336 IAccessibilityInteractionConnectionCallback callback, int flags,
7337 int interrogatingPid, long interrogatingTid) {
7338 ViewRootImpl viewRootImpl = mViewRootImpl.get();
7339 if (viewRootImpl != null && viewRootImpl.mView != null) {
7340 viewRootImpl.getAccessibilityInteractionController()
7341 .performAccessibilityActionClientThread(accessibilityNodeId, action, arguments,
7342 interactionId, callback, flags, interrogatingPid, interrogatingTid);
7344 // We cannot make the call and notify the caller so it does not wait.
7346 callback.setPerformAccessibilityActionResult(false, interactionId);
7347 } catch (RemoteException re) {
7348 /* best effort - ignore */
7354 public void findAccessibilityNodeInfosByViewId(long accessibilityNodeId,
7355 String viewId, Region interactiveRegion, int interactionId,
7356 IAccessibilityInteractionConnectionCallback callback, int flags,
7357 int interrogatingPid, long interrogatingTid, MagnificationSpec spec) {
7358 ViewRootImpl viewRootImpl = mViewRootImpl.get();
7359 if (viewRootImpl != null && viewRootImpl.mView != null) {
7360 viewRootImpl.getAccessibilityInteractionController()
7361 .findAccessibilityNodeInfosByViewIdClientThread(accessibilityNodeId,
7362 viewId, interactiveRegion, interactionId, callback, flags,
7363 interrogatingPid, interrogatingTid, spec);
7365 // We cannot make the call and notify the caller so it does not wait.
7367 callback.setFindAccessibilityNodeInfoResult(null, interactionId);
7368 } catch (RemoteException re) {
7369 /* best effort - ignore */
7375 public void findAccessibilityNodeInfosByText(long accessibilityNodeId, String text,
7376 Region interactiveRegion, int interactionId,
7377 IAccessibilityInteractionConnectionCallback callback, int flags,
7378 int interrogatingPid, long interrogatingTid, MagnificationSpec spec) {
7379 ViewRootImpl viewRootImpl = mViewRootImpl.get();
7380 if (viewRootImpl != null && viewRootImpl.mView != null) {
7381 viewRootImpl.getAccessibilityInteractionController()
7382 .findAccessibilityNodeInfosByTextClientThread(accessibilityNodeId, text,
7383 interactiveRegion, interactionId, callback, flags, interrogatingPid,
7384 interrogatingTid, spec);
7386 // We cannot make the call and notify the caller so it does not wait.
7388 callback.setFindAccessibilityNodeInfosResult(null, interactionId);
7389 } catch (RemoteException re) {
7390 /* best effort - ignore */
7396 public void findFocus(long accessibilityNodeId, int focusType, Region interactiveRegion,
7397 int interactionId, IAccessibilityInteractionConnectionCallback callback, int flags,
7398 int interrogatingPid, long interrogatingTid, MagnificationSpec spec) {
7399 ViewRootImpl viewRootImpl = mViewRootImpl.get();
7400 if (viewRootImpl != null && viewRootImpl.mView != null) {
7401 viewRootImpl.getAccessibilityInteractionController()
7402 .findFocusClientThread(accessibilityNodeId, focusType, interactiveRegion,
7403 interactionId, callback, flags, interrogatingPid, interrogatingTid,
7406 // We cannot make the call and notify the caller so it does not wait.
7408 callback.setFindAccessibilityNodeInfoResult(null, interactionId);
7409 } catch (RemoteException re) {
7410 /* best effort - ignore */
7416 public void focusSearch(long accessibilityNodeId, int direction, Region interactiveRegion,
7417 int interactionId, IAccessibilityInteractionConnectionCallback callback, int flags,
7418 int interrogatingPid, long interrogatingTid, MagnificationSpec spec) {
7419 ViewRootImpl viewRootImpl = mViewRootImpl.get();
7420 if (viewRootImpl != null && viewRootImpl.mView != null) {
7421 viewRootImpl.getAccessibilityInteractionController()
7422 .focusSearchClientThread(accessibilityNodeId, direction, interactiveRegion,
7423 interactionId, callback, flags, interrogatingPid, interrogatingTid,
7426 // We cannot make the call and notify the caller so it does not wait.
7428 callback.setFindAccessibilityNodeInfoResult(null, interactionId);
7429 } catch (RemoteException re) {
7430 /* best effort - ignore */
7436 private class SendWindowContentChangedAccessibilityEvent implements Runnable {
7437 private int mChangeTypes = 0;
7439 public View mSource;
7440 public long mLastEventTimeMillis;
7444 // The accessibility may be turned off while we were waiting so check again.
7445 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7446 mLastEventTimeMillis = SystemClock.uptimeMillis();
7447 AccessibilityEvent event = AccessibilityEvent.obtain();
7448 event.setEventType(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
7449 event.setContentChangeTypes(mChangeTypes);
7450 mSource.sendAccessibilityEventUnchecked(event);
7452 mLastEventTimeMillis = 0;
7454 // In any case reset to initial state.
7455 mSource.resetSubtreeAccessibilityStateChanged();
7460 public void runOrPost(View source, int changeType) {
7461 if (mSource != null) {
7462 // If there is no common predecessor, then mSource points to
7463 // a removed view, hence in this case always prefer the source.
7464 View predecessor = getCommonPredecessor(mSource, source);
7465 mSource = (predecessor != null) ? predecessor : source;
7466 mChangeTypes |= changeType;
7470 mChangeTypes = changeType;
7471 final long timeSinceLastMillis = SystemClock.uptimeMillis() - mLastEventTimeMillis;
7472 final long minEventIntevalMillis =
7473 ViewConfiguration.getSendRecurringAccessibilityEventsInterval();
7474 if (timeSinceLastMillis >= minEventIntevalMillis) {
7475 mSource.removeCallbacks(this);
7478 mSource.postDelayed(this, minEventIntevalMillis - timeSinceLastMillis);