blob: b9eb0a96f98d513fd82e1c9ba8f99f4ca409620c [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Jorim Jaggia4a58ef2016-01-27 02:10:08 -080019import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Filip Gruszczynski1a4dfe52015-11-15 10:58:57 -080020import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_DECOR_VIEW_VISIBILITY;
Jorim Jaggi2e95a482016-01-14 17:36:55 -080021import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
Chong Zhangf6525ce2016-01-14 17:09:56 -080022import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
23import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL;
24import static android.view.WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY;
Filip Gruszczynski1a4dfe52015-11-15 10:58:57 -080025
Romain Guy6b7bd242010-10-06 19:49:23 -070026import android.Manifest;
Chet Haasecca2c982011-05-20 14:34:18 -070027import android.animation.LayoutTransition;
Romain Guy6b7bd242010-10-06 19:49:23 -070028import android.app.ActivityManagerNative;
29import android.content.ClipDescription;
30import android.content.ComponentCallbacks;
31import android.content.Context;
32import android.content.pm.PackageManager;
33import android.content.res.CompatibilityInfo;
34import android.content.res.Configuration;
35import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.graphics.Canvas;
Alan Viverettefed3f722013-11-14 14:48:20 -080037import android.graphics.Matrix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.graphics.PixelFormat;
Christopher Tate2c095f32010-10-04 14:13:40 -070039import android.graphics.Point;
Christopher Tatea53146c2010-09-07 11:57:52 -070040import android.graphics.PointF;
Romain Guy6b7bd242010-10-06 19:49:23 -070041import android.graphics.PorterDuff;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.graphics.Rect;
43import android.graphics.Region;
Svetoslav Ganov42138042012-03-20 11:51:39 -070044import android.graphics.drawable.Drawable;
Jeff Brownd912e1f2014-04-11 18:46:22 -070045import android.hardware.display.DisplayManager;
46import android.hardware.display.DisplayManager.DisplayListener;
Jun Mukai347e5d42015-12-03 01:13:31 -080047import android.hardware.input.InputManager;
Romain Guy6b7bd242010-10-06 19:49:23 -070048import android.media.AudioManager;
49import android.os.Binder;
Michael Wright5bd69e62015-05-14 14:48:08 +010050import android.os.Build;
Romain Guy6b7bd242010-10-06 19:49:23 -070051import android.os.Bundle;
52import android.os.Debug;
53import android.os.Handler;
Romain Guy6b7bd242010-10-06 19:49:23 -070054import android.os.Looper;
55import android.os.Message;
56import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.os.Process;
Romain Guy6b7bd242010-10-06 19:49:23 -070058import android.os.RemoteException;
Romain Guy6b7bd242010-10-06 19:49:23 -070059import android.os.SystemClock;
Romain Guy59a12ca2011-06-09 17:48:21 -070060import android.os.SystemProperties;
Jeff Brown481c1572012-03-09 14:41:15 -080061import android.os.Trace;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.util.AndroidRuntimeException;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -070063import android.util.DisplayMetrics;
Romain Guy6b7bd242010-10-06 19:49:23 -070064import android.util.Log;
Chet Haase949dbf72010-08-11 18:41:06 -070065import android.util.Slog;
John Reckba6adf62015-02-19 14:36:50 -080066import android.util.TimeUtils;
Dianne Hackborn711e62a2010-11-29 16:38:22 -080067import android.util.TypedValue;
John Reck44fd8d22014-02-26 11:00:11 -080068import android.view.Surface.OutOfResourcesException;
Jeff Browna175a5b2012-02-15 19:18:31 -080069import android.view.View.AttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.view.View.MeasureSpec;
svetoslavganov75986cf2009-05-14 22:28:01 -070071import android.view.accessibility.AccessibilityEvent;
72import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070073import android.view.accessibility.AccessibilityManager.AccessibilityStateChangeListener;
Chris Craikcce47eb2014-07-16 15:12:15 -070074import android.view.accessibility.AccessibilityManager.HighTextContrastChangeListener;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070075import android.view.accessibility.AccessibilityNodeInfo;
Alan Viverette25acc7e2015-05-19 11:32:08 -070076import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
Svetoslav Ganov02107852011-10-03 17:06:56 -070077import android.view.accessibility.AccessibilityNodeProvider;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070078import android.view.accessibility.IAccessibilityInteractionConnection;
79import android.view.accessibility.IAccessibilityInteractionConnectionCallback;
Dianne Hackborn0f761d62010-11-30 22:06:10 -080080import android.view.animation.AccelerateDecelerateInterpolator;
81import android.view.animation.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.view.inputmethod.InputConnection;
83import android.view.inputmethod.InputMethodManager;
84import android.widget.Scroller;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070085
Svetoslav Ganov42138042012-03-20 11:51:39 -070086import com.android.internal.R;
Clara Bayarri75e09792015-07-29 16:20:40 +010087import com.android.internal.os.IResultReceiver;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070088import com.android.internal.os.SomeArgs;
Adam Powell6711f3b2015-05-06 15:57:09 -070089import com.android.internal.policy.PhoneFallbackEventHandler;
Romain Guy6b7bd242010-10-06 19:49:23 -070090import com.android.internal.view.BaseSurfaceHolder;
Romain Guy6b7bd242010-10-06 19:49:23 -070091import com.android.internal.view.RootViewSurfaceTaker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
Jeff Brown5182c782013-10-15 20:31:52 -070093import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import java.io.IOException;
95import java.io.OutputStream;
Jeff Brown5182c782013-10-15 20:31:52 -070096import java.io.PrintWriter;
Romain Guy6b7bd242010-10-06 19:49:23 -070097import java.lang.ref.WeakReference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import java.util.ArrayList;
Chong Zhang8dbd9ad2015-10-09 10:06:11 -070099import java.util.concurrent.CountDownLatch;
Svetoslav Ganov42138042012-03-20 11:51:39 -0700100import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102/**
103 * The top of a view hierarchy, implementing the needed protocol between View
104 * and the WindowManager. This is for the most part an internal implementation
Jeff Brown98365d72012-08-19 20:30:52 -0700105 * detail of {@link WindowManagerGlobal}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 *
107 * {@hide}
108 */
Romain Guy812ccbe2010-06-01 14:07:24 -0700109@SuppressWarnings({"EmptyCatchBlock", "PointlessBooleanExpression"})
Jeff Browna175a5b2012-02-15 19:18:31 -0800110public final class ViewRootImpl implements ViewParent,
John Reck51aaf902015-12-02 15:08:07 -0800111 View.AttachInfo.Callbacks, ThreadedRenderer.HardwareDrawCallbacks {
Dianne Hackborn70a3f672011-08-08 14:32:41 -0700112 private static final String TAG = "ViewRootImpl";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 private static final boolean DBG = false;
Romain Guy812ccbe2010-06-01 14:07:24 -0700114 private static final boolean LOCAL_LOGV = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 /** @noinspection PointlessBooleanExpression*/
116 private static final boolean DEBUG_DRAW = false || LOCAL_LOGV;
117 private static final boolean DEBUG_LAYOUT = false || LOCAL_LOGV;
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800118 private static final boolean DEBUG_DIALOG = false || LOCAL_LOGV;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 private static final boolean DEBUG_INPUT_RESIZE = false || LOCAL_LOGV;
120 private static final boolean DEBUG_ORIENTATION = false || LOCAL_LOGV;
121 private static final boolean DEBUG_TRACKBALL = false || LOCAL_LOGV;
122 private static final boolean DEBUG_IMF = false || LOCAL_LOGV;
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700123 private static final boolean DEBUG_CONFIGURATION = false || LOCAL_LOGV;
Chet Haase2f2022a2011-10-11 06:41:59 -0700124 private static final boolean DEBUG_FPS = false;
Michael Wright06a79252014-05-05 17:45:29 -0700125 private static final boolean DEBUG_INPUT_STAGES = false || LOCAL_LOGV;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126
Romain Guy59a12ca2011-06-09 17:48:21 -0700127 /**
Skuhneb8160872015-09-22 09:51:39 -0700128 * Set to false if we do not want to use the multi threaded renderer. Note that by disabling
129 * this, WindowCallbacks will not fire.
130 */
131 private static final boolean USE_MT_RENDERER = true;
132
133 /**
Romain Guy59a12ca2011-06-09 17:48:21 -0700134 * Set this system property to true to force the view hierarchy to render
135 * at 60 Hz. This can be used to measure the potential framerate.
136 */
Romain Guye9bc11f2013-05-23 12:47:26 -0700137 private static final String PROPERTY_PROFILE_RENDERING = "viewroot.profile_rendering";
Michael Chan53071d62009-05-13 17:29:48 -0700138
Griff Hazena0938022015-03-13 10:01:41 -0700139 // properties used by emulator to determine display shape
Griff Hazena0938022015-03-13 10:01:41 -0700140 public static final String PROPERTY_EMULATOR_WIN_OUTSET_BOTTOM_PX =
141 "ro.emu.win_outset_bottom_px";
Michael Kolb437d3132014-06-20 13:28:44 -0700142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 /**
144 * Maximum time we allow the user to roll the trackball enough to generate
145 * a key event, before resetting the counters.
146 */
147 static final int MAX_TRACKBALL_DELAY = 250;
148
Jorim Jaggi4846ee32016-01-07 17:39:12 +0100149 private static final int RESIZE_MODE_FREEFORM = 0;
150 private static final int RESIZE_MODE_DOCKED_DIVIDER = 1;
151
Alan Viverettebea0c7da2015-09-01 16:00:20 -0400152 static final ThreadLocal<HandlerActionQueue> sRunQueues = new ThreadLocal<HandlerActionQueue>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153
Skuhneb8160872015-09-22 09:51:39 -0700154 static final ArrayList<Runnable> sFirstDrawHandlers = new ArrayList();
Dianne Hackborn2a9094d2010-02-03 19:20:09 -0800155 static boolean sFirstDrawComplete = false;
Craig Mautner8f303ad2013-06-14 11:32:22 -0700156
Skuhneb8160872015-09-22 09:51:39 -0700157 static final ArrayList<ComponentCallbacks> sConfigCallbacks = new ArrayList();
Romain Guy59a12ca2011-06-09 17:48:21 -0700158
Chong Zhangdcee1de2015-10-06 10:26:00 -0700159 final ArrayList<WindowCallbacks> mWindowCallbacks = new ArrayList();
Jeff Brownf9e989d2013-04-04 23:04:03 -0700160 final Context mContext;
Jeff Brown98365d72012-08-19 20:30:52 -0700161 final IWindowSession mWindowSession;
162 final Display mDisplay;
Jeff Brownd912e1f2014-04-11 18:46:22 -0700163 final DisplayManager mDisplayManager;
Dianne Hackbornc2293022013-02-06 23:14:49 -0800164 final String mBasePackageName;
Jeff Brown98365d72012-08-19 20:30:52 -0700165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 final int[] mTmpLocation = new int[2];
Romain Guy8506ab42009-06-11 17:35:47 -0700167
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800168 final TypedValue mTmpValue = new TypedValue();
Jeff Brownf9e989d2013-04-04 23:04:03 -0700169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 final Thread mThread;
171
172 final WindowLeaked mLocation;
173
174 final WindowManager.LayoutParams mWindowAttributes = new WindowManager.LayoutParams();
175
176 final W mWindow;
177
Dianne Hackborn180c4842011-09-13 12:39:25 -0700178 final int mTargetSdkVersion;
179
Dianne Hackborn9a230e02011-10-06 11:51:27 -0700180 int mSeq;
181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 View mView;
Svetoslav Ganov42138042012-03-20 11:51:39 -0700183
184 View mAccessibilityFocusedHost;
185 AccessibilityNodeInfo mAccessibilityFocusedVirtualView;
186
Jun Mukai347e5d42015-12-03 01:13:31 -0800187 // The view which captures mouse input, or null when no one is capturing.
188 View mCapturingView;
189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 int mViewVisibility;
191 boolean mAppVisible = true;
Filip Gruszczynski1a4dfe52015-11-15 10:58:57 -0800192 // For recents to freeform transition we need to keep drawing after the app receives information
193 // that it became invisible. This will ignore that information and depend on the decor view
194 // visibility to control drawing. The decor view visibility will get adjusted when the app get
195 // stopped and that's when the app will stop drawing further frames.
196 private boolean mForceDecorViewVisibility = false;
Dianne Hackborn180c4842011-09-13 12:39:25 -0700197 int mOrigWindowType = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198
Alan Viverette64bf97a2015-09-18 16:42:00 -0400199 /** Whether the window had focus during the most recent traversal. */
200 boolean mHadWindowFocus;
201
202 /**
203 * Whether the window lost focus during a previous traversal and has not
204 * yet gained it back. Used to determine whether a WINDOW_STATE_CHANGE
205 * accessibility events should be sent during traversal.
206 */
207 boolean mLostWindowFocus;
208
Dianne Hackbornce418e62011-03-01 14:31:38 -0800209 // Set to true if the owner of this window is in the stopped state,
210 // so the window should no longer be active.
211 boolean mStopped = false;
Craig Mautner8f303ad2013-06-14 11:32:22 -0700212
Daniel Koulomzin087ae472015-12-16 17:52:25 -0500213 // Set to true if the owner of this window is in ambient mode,
214 // which means it won't receive input events.
215 boolean mIsAmbientMode = false;
216
George Mount41725de2015-04-09 08:23:05 -0700217 // Set to true to stop input during an Activity Transition.
218 boolean mPausedForTransition = false;
219
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700220 boolean mLastInCompatMode = false;
221
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700222 SurfaceHolder.Callback2 mSurfaceHolderCallback;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700223 BaseSurfaceHolder mSurfaceHolder;
224 boolean mIsCreating;
225 boolean mDrawingAllowed;
Craig Mautner8f303ad2013-06-14 11:32:22 -0700226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 final Region mTransparentRegion;
228 final Region mPreviousTransparentRegion;
229
230 int mWidth;
231 int mHeight;
Romain Guy7d7b5492011-01-24 16:33:45 -0800232 Rect mDirty;
Romain Guybb93d552009-03-24 21:04:15 -0700233 boolean mIsAnimating;
Romain Guy8506ab42009-06-11 17:35:47 -0700234
Chong Zhang0275e392015-09-17 10:41:44 -0700235 private boolean mDragResizing;
Jorim Jaggi4846ee32016-01-07 17:39:12 +0100236 private int mResizeMode;
Chong Zhang0275e392015-09-17 10:41:44 -0700237 private int mCanvasOffsetX;
238 private int mCanvasOffsetY;
Jorim Jaggi4846ee32016-01-07 17:39:12 +0100239 private boolean mActivityRelaunched;
Chong Zhang0275e392015-09-17 10:41:44 -0700240
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700241 CompatibilityInfo.Translator mTranslator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242
243 final View.AttachInfo mAttachInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700244 InputChannel mInputChannel;
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -0700245 InputQueue.Callback mInputQueueCallback;
246 InputQueue mInputQueue;
Joe Onorato86f67862010-11-05 18:57:34 -0700247 FallbackEventHandler mFallbackEventHandler;
Jeff Brown96e942d2011-11-30 19:55:01 -0800248 Choreographer mChoreographer;
Igor Murashkina86ab6402013-08-30 12:58:36 -0700249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 final Rect mTempRect; // used in the transaction to not thrash the heap.
251 final Rect mVisRect; // used to retrieve visible rect of focused view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252
253 boolean mTraversalScheduled;
Jeff Browne0dbd002012-02-15 19:34:58 -0800254 int mTraversalBarrier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 boolean mWillDrawSoon;
Craig Mautnerdf2390a2012-08-29 20:59:22 -0700256 /** Set to true while in performTraversals for detecting when die(true) is called from internal
257 * callbacks such as onMeasure, onPreDraw, onDraw and deferring doDie() until later. */
258 boolean mIsInTraversal;
Adrian Roosfa104232014-06-20 16:10:14 -0700259 boolean mApplyInsetsRequested;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 boolean mLayoutRequested;
261 boolean mFirst;
262 boolean mReportNextDraw;
263 boolean mFullRedrawNeeded;
264 boolean mNewSurfaceNeeded;
265 boolean mHasHadWindowFocus;
266 boolean mLastWasImTarget;
Jorim Jaggia4a58ef2016-01-27 02:10:08 -0800267 boolean mForceNextWindowRelayout;
Chong Zhang8dbd9ad2015-10-09 10:06:11 -0700268 CountDownLatch mWindowDrawCountDown;
Jorim Jaggi827e0fa2015-05-07 11:41:41 -0700269
Romain Guy1f59e5c2012-05-06 14:11:16 -0700270 boolean mIsDrawing;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700271 int mLastSystemUiVisibility;
Dianne Hackborn9d090892012-06-11 18:35:41 -0700272 int mClientWindowLayoutFlags;
Dianne Hackbornc4aad012013-02-22 15:05:25 -0800273 boolean mLastOverscanRequested;
Jeff Brown4952dfd2011-11-30 19:23:22 -0800274
275 // Pool of queued input events.
276 private static final int MAX_QUEUED_INPUT_EVENT_POOL_SIZE = 10;
277 private QueuedInputEvent mQueuedInputEventPool;
278 private int mQueuedInputEventPoolSize;
Jeff Brown4952dfd2011-11-30 19:23:22 -0800279
Michael Wrightc8a7e542013-03-20 17:58:33 -0700280 /* Input event queue.
Jeff Brownf9e989d2013-04-04 23:04:03 -0700281 * Pending input events are input events waiting to be delivered to the input stages
282 * and handled by the application.
Michael Wrightc8a7e542013-03-20 17:58:33 -0700283 */
284 QueuedInputEvent mPendingInputEventHead;
285 QueuedInputEvent mPendingInputEventTail;
Michael Wright95ae9422013-03-14 10:58:50 -0700286 int mPendingInputEventCount;
Jeff Brown96e942d2011-11-30 19:55:01 -0800287 boolean mProcessInputEventsScheduled;
Michael Wright9d744c72014-02-18 21:27:42 -0800288 boolean mUnbufferedInputDispatch;
Michael Wright95ae9422013-03-14 10:58:50 -0700289 String mPendingInputEventQueueLengthCounterName = "pq";
Jeff Brownf9e989d2013-04-04 23:04:03 -0700290
291 InputStage mFirstInputStage;
292 InputStage mFirstPostImeInputStage;
Michael Wright899d7052014-04-23 17:23:39 -0700293 InputStage mSyntheticInputStage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294
295 boolean mWindowAttributesChanged = false;
Romain Guyf21c9b02011-09-06 16:56:54 -0700296 int mWindowAttributesChangesFlag = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297
298 // These can be accessed by any thread, must be protected with a lock.
Mathias Agopian5583dc62009-07-09 16:28:11 -0700299 // Surface can never be reassigned or cleared (use Surface.clear()).
John Reckb13de072014-11-19 16:33:47 -0800300 final Surface mSurface = new Surface();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301
302 boolean mAdded;
303 boolean mAddedTouchMode;
304
Craig Mautner48d0d182013-06-11 07:53:06 -0700305 final DisplayAdjustments mDisplayAdjustments;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 // These are accessed by multiple threads.
308 final Rect mWinFrame; // frame given by window manager.
309
Dianne Hackbornc4aad012013-02-22 15:05:25 -0800310 final Rect mPendingOverscanInsets = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 final Rect mPendingVisibleInsets = new Rect();
Adrian Roosfa104232014-06-20 16:10:14 -0700312 final Rect mPendingStableInsets = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 final Rect mPendingContentInsets = new Rect();
Filip Gruszczynski2217f612015-05-26 11:32:08 -0700314 final Rect mPendingOutsets = new Rect();
Jorim Jaggia7262a82015-11-03 15:15:40 +0100315 final Rect mPendingBackDropFrame = new Rect();
Jorim Jaggi0ffd49c2016-02-12 15:04:21 -0800316 boolean mPendingAlwaysConsumeNavBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 final ViewTreeObserver.InternalInsetsInfo mLastGivenInsets
318 = new ViewTreeObserver.InternalInsetsInfo();
319
Adrian Roosfa104232014-06-20 16:10:14 -0700320 final Rect mDispatchContentInsets = new Rect();
321 final Rect mDispatchStableInsets = new Rect();
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700322
Filip Gruszczynski954289d2015-02-26 15:46:47 -0800323 private WindowInsets mLastWindowInsets;
324
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700325 final Configuration mLastConfiguration = new Configuration();
326 final Configuration mPendingConfiguration = new Configuration();
Romain Guy59a12ca2011-06-09 17:48:21 -0700327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 boolean mScrollMayChange;
329 int mSoftInputMode;
Svetoslav Ganov149567f2013-01-08 15:23:34 -0800330 WeakReference<View> mLastScrolledFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 int mScrollY;
332 int mCurScrollY;
333 Scroller mScroller;
John Recke56e9df2014-02-21 15:45:10 -0800334 static final Interpolator mResizeInterpolator = new AccelerateDecelerateInterpolator();
Chet Haasecca2c982011-05-20 14:34:18 -0700335 private ArrayList<LayoutTransition> mPendingTransitions;
Romain Guy8506ab42009-06-11 17:35:47 -0700336
Romain Guy8506ab42009-06-11 17:35:47 -0700337 final ViewConfiguration mViewConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338
Christopher Tatea53146c2010-09-07 11:57:52 -0700339 /* Drag/drop */
340 ClipDescription mDragDescription;
341 View mCurrentDragView;
Christopher Tate7fb8b562011-01-20 13:46:41 -0800342 volatile Object mLocalDragState;
Christopher Tatea53146c2010-09-07 11:57:52 -0700343 final PointF mDragPoint = new PointF();
Christopher Tate2c095f32010-10-04 14:13:40 -0700344 final PointF mLastTouchPoint = new PointF();
Vladislav Kaznacheevba761122016-01-22 12:09:45 -0800345 int mLastTouchSource;
Igor Murashkina86ab6402013-08-30 12:58:36 -0700346
347 private boolean mProfileRendering;
Romain Guyd0750312012-11-29 11:34:43 -0800348 private Choreographer.FrameCallback mRenderProfiler;
349 private boolean mRenderProfilingEnabled;
Christopher Tatea53146c2010-09-07 11:57:52 -0700350
Chet Haase2f2022a2011-10-11 06:41:59 -0700351 // Variables to track frames per second, enabled via DEBUG_FPS flag
352 private long mFpsStartTime = -1;
353 private long mFpsPrevTime = -1;
354 private int mFpsNumFrames;
355
Jun Mukai1db53972015-09-11 18:08:31 -0700356 private int mPointerIconShape = PointerIcon.STYLE_NOT_SPECIFIED;
Jun Mukaid4eaef72015-10-30 15:54:33 -0700357 private PointerIcon mCustomPointerIcon = null;
Jun Mukai1db53972015-09-11 18:08:31 -0700358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 /**
360 * see {@link #playSoundEffect(int)}
361 */
362 AudioManager mAudioManager;
363
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700364 final AccessibilityManager mAccessibilityManager;
365
Gilles Debunne5ac84422011-10-19 09:35:58 -0700366 AccessibilityInteractionController mAccessibilityInteractionController;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700367
368 AccessibilityInteractionConnectionManager mAccessibilityInteractionConnectionManager;
Chris Craikcce47eb2014-07-16 15:12:15 -0700369 HighContrastTextManager mHighContrastTextManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700370
Svetoslav Ganova0156172011-06-26 17:55:44 -0700371 SendWindowContentChangedAccessibilityEvent mSendWindowContentChangedAccessibilityEvent;
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -0700372
Svetoslav Ganov42138042012-03-20 11:51:39 -0700373 HashSet<View> mTempHashSet;
Svetoslav Ganov79311c42012-01-17 20:24:26 -0800374
Dianne Hackborn11ea3342009-07-22 21:48:55 -0700375 private final int mDensity;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700376 private final int mNoncompatDensity;
Adam Powellb08013c2010-09-16 16:28:11 -0700377
Chet Haase97140572012-09-13 14:56:47 -0700378 private boolean mInLayout = false;
379 ArrayList<View> mLayoutRequesters = new ArrayList<View>();
380 boolean mHandlingLayoutInLayoutRequest = false;
381
Fabrice Di Megliob003e282012-10-17 17:20:19 -0700382 private int mViewLayoutDirectionInitial;
Chet Haase97140572012-09-13 14:56:47 -0700383
Craig Mautner8f303ad2013-06-14 11:32:22 -0700384 /** Set to true once doDie() has been called. */
385 private boolean mRemoved;
386
Jeff Brown21bc5c92011-02-28 18:27:14 -0800387 /**
388 * Consistency verifier for debugging purposes.
389 */
390 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
391 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
392 new InputEventConsistencyVerifier(this, 0) : null;
393
Dianne Hackborn9a230e02011-10-06 11:51:27 -0700394 static final class SystemUiVisibilityInfo {
395 int seq;
396 int globalVisibility;
397 int localValue;
398 int localChanges;
399 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700400
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -0800401 private String mTag = TAG;
402
Jeff Brown98365d72012-08-19 20:30:52 -0700403 public ViewRootImpl(Context context, Display display) {
Jeff Brownf9e989d2013-04-04 23:04:03 -0700404 mContext = context;
405 mWindowSession = WindowManagerGlobal.getWindowSession();
Jeff Brown98365d72012-08-19 20:30:52 -0700406 mDisplay = display;
Dianne Hackbornc2293022013-02-06 23:14:49 -0800407 mBasePackageName = context.getBasePackageName();
Jeff Brown98365d72012-08-19 20:30:52 -0700408
Craig Mautner48d0d182013-06-11 07:53:06 -0700409 mDisplayAdjustments = display.getDisplayAdjustments();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 mThread = Thread.currentThread();
412 mLocation = new WindowLeaked(null);
413 mLocation.fillInStackTrace();
414 mWidth = -1;
415 mHeight = -1;
416 mDirty = new Rect();
417 mTempRect = new Rect();
418 mVisRect = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 mWinFrame = new Rect();
Romain Guyfb8b7632010-08-23 21:05:08 -0700420 mWindow = new W(this);
Dianne Hackborn180c4842011-09-13 12:39:25 -0700421 mTargetSdkVersion = context.getApplicationInfo().targetSdkVersion;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 mViewVisibility = View.GONE;
423 mTransparentRegion = new Region();
424 mPreviousTransparentRegion = new Region();
425 mFirst = true; // true for the first time the view is added
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 mAdded = false;
Chris Craikcce47eb2014-07-16 15:12:15 -0700427 mAttachInfo = new View.AttachInfo(mWindowSession, mWindow, display, this, mHandler, this);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700428 mAccessibilityManager = AccessibilityManager.getInstance(context);
429 mAccessibilityInteractionConnectionManager =
430 new AccessibilityInteractionConnectionManager();
Svetoslav Ganov00d0c142012-02-24 11:30:49 -0800431 mAccessibilityManager.addAccessibilityStateChangeListener(
432 mAccessibilityInteractionConnectionManager);
Chris Craikcce47eb2014-07-16 15:12:15 -0700433 mHighContrastTextManager = new HighContrastTextManager();
434 mAccessibilityManager.addHighTextContrastStateChangeListener(
435 mHighContrastTextManager);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 mViewConfiguration = ViewConfiguration.get(context);
Dianne Hackborn11ea3342009-07-22 21:48:55 -0700437 mDensity = context.getResources().getDisplayMetrics().densityDpi;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700438 mNoncompatDensity = context.getResources().getDisplayMetrics().noncompatDensityDpi;
Jorim Jaggib10e33f2015-02-04 21:57:40 +0100439 mFallbackEventHandler = new PhoneFallbackEventHandler(context);
Jeff Brown96e942d2011-11-30 19:55:01 -0800440 mChoreographer = Choreographer.getInstance();
Jeff Brownd912e1f2014-04-11 18:46:22 -0700441 mDisplayManager = (DisplayManager)context.getSystemService(Context.DISPLAY_SERVICE);
Dianne Hackborna53de062012-05-08 18:53:51 -0700442 loadSystemProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 }
444
Dianne Hackborn2a9094d2010-02-03 19:20:09 -0800445 public static void addFirstDrawHandler(Runnable callback) {
446 synchronized (sFirstDrawHandlers) {
447 if (!sFirstDrawComplete) {
448 sFirstDrawHandlers.add(callback);
449 }
450 }
451 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700452
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800453 public static void addConfigCallback(ComponentCallbacks callback) {
454 synchronized (sConfigCallbacks) {
455 sConfigCallbacks.add(callback);
456 }
457 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700458
Chong Zhangdcee1de2015-10-06 10:26:00 -0700459 public void addWindowCallbacks(WindowCallbacks callback) {
Skuhneb8160872015-09-22 09:51:39 -0700460 if (USE_MT_RENDERER) {
Chong Zhangdcee1de2015-10-06 10:26:00 -0700461 synchronized (mWindowCallbacks) {
462 mWindowCallbacks.add(callback);
Skuhneb8160872015-09-22 09:51:39 -0700463 }
464 }
465 }
466
Chong Zhangdcee1de2015-10-06 10:26:00 -0700467 public void removeWindowCallbacks(WindowCallbacks callback) {
Skuhneb8160872015-09-22 09:51:39 -0700468 if (USE_MT_RENDERER) {
Chong Zhangdcee1de2015-10-06 10:26:00 -0700469 synchronized (mWindowCallbacks) {
470 mWindowCallbacks.remove(callback);
Skuhneb8160872015-09-22 09:51:39 -0700471 }
472 }
473 }
474
Chong Zhang8dbd9ad2015-10-09 10:06:11 -0700475 public void reportDrawFinish() {
476 if (mWindowDrawCountDown != null) {
477 mWindowDrawCountDown.countDown();
478 }
479 }
480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 // FIXME for perf testing only
482 private boolean mProfile = false;
483
484 /**
485 * Call this to profile the next traversal call.
486 * FIXME for perf testing only. Remove eventually
487 */
488 public void profile() {
489 mProfile = true;
490 }
491
492 /**
493 * Indicates whether we are in touch mode. Calling this method triggers an IPC
494 * call and should be avoided whenever possible.
495 *
496 * @return True, if the device is in touch mode, false otherwise.
497 *
498 * @hide
499 */
500 static boolean isInTouchMode() {
Jeff Brown98365d72012-08-19 20:30:52 -0700501 IWindowSession windowSession = WindowManagerGlobal.peekWindowSession();
502 if (windowSession != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 try {
Jeff Brown98365d72012-08-19 20:30:52 -0700504 return windowSession.getInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 } catch (RemoteException e) {
506 }
507 }
508 return false;
509 }
510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 /**
512 * We have one child
513 */
Romain Guye4d01122010-06-16 18:44:05 -0700514 public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 synchronized (this) {
516 if (mView == null) {
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700517 mView = view;
Jeff Brownd912e1f2014-04-11 18:46:22 -0700518
519 mAttachInfo.mDisplayState = mDisplay.getState();
520 mDisplayManager.registerDisplayListener(mDisplayListener, mHandler);
521
Fabrice Di Megliob003e282012-10-17 17:20:19 -0700522 mViewLayoutDirectionInitial = mView.getRawLayoutDirection();
Joe Onorato86f67862010-11-05 18:57:34 -0700523 mFallbackEventHandler.setView(view);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700524 mWindowAttributes.copyFrom(attrs);
Dianne Hackbornc2293022013-02-06 23:14:49 -0800525 if (mWindowAttributes.packageName == null) {
526 mWindowAttributes.packageName = mBasePackageName;
527 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700528 attrs = mWindowAttributes;
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -0800529 setTag();
Dianne Hackborn9d090892012-06-11 18:35:41 -0700530 // Keep track of the actual window flags supplied by the client.
531 mClientWindowLayoutFlags = attrs.flags;
Svetoslav Ganov791fd312012-05-14 15:12:30 -0700532
Svetoslav Ganov45a02e02012-06-17 15:07:29 -0700533 setAccessibilityFocus(null, null);
Svetoslav Ganov791fd312012-05-14 15:12:30 -0700534
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700535 if (view instanceof RootViewSurfaceTaker) {
536 mSurfaceHolderCallback =
537 ((RootViewSurfaceTaker)view).willYouTakeTheSurface();
538 if (mSurfaceHolderCallback != null) {
539 mSurfaceHolder = new TakenSurfaceHolder();
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700540 mSurfaceHolder.setFormat(PixelFormat.UNKNOWN);
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700541 }
542 }
Romain Guy1aec9a22011-01-05 09:37:12 -0800543
Alan Viverette49a22e82014-07-12 20:01:27 -0700544 // Compute surface insets required to draw at specified Z value.
545 // TODO: Use real shadow insets for a constant max Z.
Alan Viverette5435a302015-01-29 10:25:34 -0800546 if (!attrs.hasManualSurfaceInsets) {
547 final int surfaceInset = (int) Math.ceil(view.getZ() * 2);
548 attrs.surfaceInsets.set(surfaceInset, surfaceInset, surfaceInset, surfaceInset);
549 }
Alan Viverette49a22e82014-07-12 20:01:27 -0700550
Craig Mautner48d0d182013-06-11 07:53:06 -0700551 CompatibilityInfo compatibilityInfo = mDisplayAdjustments.getCompatibilityInfo();
Romain Guy856d4e12011-10-14 15:47:55 -0700552 mTranslator = compatibilityInfo.getTranslator();
553
Romain Guy1aec9a22011-01-05 09:37:12 -0800554 // If the application owns the surface, don't enable hardware acceleration
555 if (mSurfaceHolder == null) {
Romain Guy3b748a42013-04-17 18:54:38 -0700556 enableHardwareAcceleration(attrs);
Romain Guy1aec9a22011-01-05 09:37:12 -0800557 }
558
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -0700559 boolean restore = false;
Romain Guy35b38ce2009-10-07 13:38:55 -0700560 if (mTranslator != null) {
Romain Guy856d4e12011-10-14 15:47:55 -0700561 mSurface.setCompatibilityTranslator(mTranslator);
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -0700562 restore = true;
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700563 attrs.backup();
564 mTranslator.translateWindowLayout(attrs);
Mitsuru Oshima3d914922009-05-13 22:29:15 -0700565 }
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -0800566 if (DEBUG_LAYOUT) Log.d(mTag, "WindowLayout in setView:" + attrs);
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700567
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700568 if (!compatibilityInfo.supportsScreen()) {
Adam Lesinski95c42972013-10-02 10:13:27 -0700569 attrs.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700570 mLastInCompatMode = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700571 }
572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 mSoftInputMode = attrs.softInputMode;
574 mWindowAttributesChanged = true;
Romain Guyf21c9b02011-09-06 16:56:54 -0700575 mWindowAttributesChangesFlag = WindowManager.LayoutParams.EVERYTHING_CHANGED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 mAttachInfo.mRootView = view;
Romain Guy35b38ce2009-10-07 13:38:55 -0700577 mAttachInfo.mScalingRequired = mTranslator != null;
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700578 mAttachInfo.mApplicationScale =
579 mTranslator == null ? 1.0f : mTranslator.applicationScale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 if (panelParentView != null) {
581 mAttachInfo.mPanelParentWindowToken
582 = panelParentView.getApplicationWindowToken();
583 }
584 mAdded = true;
585 int res; /* = WindowManagerImpl.ADD_OKAY; */
Romain Guy8506ab42009-06-11 17:35:47 -0700586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 // Schedule the first layout -before- adding to the window
588 // manager, to make sure we do the relayout before receiving
589 // any other events from the system.
590 requestLayout();
Jeff Browncc4f7db2011-08-30 20:34:48 -0700591 if ((mWindowAttributes.inputFeatures
592 & WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0) {
593 mInputChannel = new InputChannel();
594 }
Filip Gruszczynski1a4dfe52015-11-15 10:58:57 -0800595 mForceDecorViewVisibility = (mWindowAttributes.privateFlags
596 & PRIVATE_FLAG_FORCE_DECOR_VIEW_VISIBILITY) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 try {
Dianne Hackborn180c4842011-09-13 12:39:25 -0700598 mOrigWindowType = mWindowAttributes.type;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700599 mAttachInfo.mRecomputeGlobalAttributes = true;
600 collectViewAttributes();
Jeff Brown98365d72012-08-19 20:30:52 -0700601 res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
602 getHostVisibility(), mDisplay.getDisplayId(),
Filip Gruszczynski0ec13282015-06-25 11:26:01 -0700603 mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
604 mAttachInfo.mOutsets, mInputChannel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 } catch (RemoteException e) {
606 mAdded = false;
607 mView = null;
608 mAttachInfo.mRootView = null;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700609 mInputChannel = null;
Joe Onorato86f67862010-11-05 18:57:34 -0700610 mFallbackEventHandler.setView(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 unscheduleTraversals();
Svetoslav Ganov45a02e02012-06-17 15:07:29 -0700612 setAccessibilityFocus(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 throw new RuntimeException("Adding window failed", e);
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700614 } finally {
615 if (restore) {
616 attrs.restore();
617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700619
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700620 if (mTranslator != null) {
621 mTranslator.translateRectInScreenToAppWindow(mAttachInfo.mContentInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700622 }
Dianne Hackbornc4aad012013-02-22 15:05:25 -0800623 mPendingOverscanInsets.set(0, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 mPendingContentInsets.set(mAttachInfo.mContentInsets);
Adrian Roosfa104232014-06-20 16:10:14 -0700625 mPendingStableInsets.set(mAttachInfo.mStableInsets);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 mPendingVisibleInsets.set(0, 0, 0, 0);
Jorim Jaggi0ffd49c2016-02-12 15:04:21 -0800627 mAttachInfo.mAlwaysConsumeNavBar =
628 (res & WindowManagerGlobal.ADD_FLAG_ALWAYS_CONSUME_NAV_BAR) != 0;
629 mPendingAlwaysConsumeNavBar = mAttachInfo.mAlwaysConsumeNavBar;
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -0800630 if (DEBUG_LAYOUT) Log.v(mTag, "Added window " + mWindow);
Jeff Brown98365d72012-08-19 20:30:52 -0700631 if (res < WindowManagerGlobal.ADD_OKAY) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 mAttachInfo.mRootView = null;
633 mAdded = false;
Joe Onorato86f67862010-11-05 18:57:34 -0700634 mFallbackEventHandler.setView(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 unscheduleTraversals();
Svetoslav Ganov45a02e02012-06-17 15:07:29 -0700636 setAccessibilityFocus(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 switch (res) {
Jeff Brown98365d72012-08-19 20:30:52 -0700638 case WindowManagerGlobal.ADD_BAD_APP_TOKEN:
639 case WindowManagerGlobal.ADD_BAD_SUBWINDOW_TOKEN:
640 throw new WindowManager.BadTokenException(
Wale Ogunwale74bf0652015-01-12 10:24:36 -0800641 "Unable to add window -- token " + attrs.token
642 + " is not valid; is your activity running?");
Jeff Brown98365d72012-08-19 20:30:52 -0700643 case WindowManagerGlobal.ADD_NOT_APP_TOKEN:
644 throw new WindowManager.BadTokenException(
Wale Ogunwale74bf0652015-01-12 10:24:36 -0800645 "Unable to add window -- token " + attrs.token
646 + " is not for an application");
Jeff Brown98365d72012-08-19 20:30:52 -0700647 case WindowManagerGlobal.ADD_APP_EXITING:
648 throw new WindowManager.BadTokenException(
Wale Ogunwale74bf0652015-01-12 10:24:36 -0800649 "Unable to add window -- app for token " + attrs.token
650 + " is exiting");
Jeff Brown98365d72012-08-19 20:30:52 -0700651 case WindowManagerGlobal.ADD_DUPLICATE_ADD:
652 throw new WindowManager.BadTokenException(
Wale Ogunwale74bf0652015-01-12 10:24:36 -0800653 "Unable to add window -- window " + mWindow
654 + " has already been added");
Jeff Brown98365d72012-08-19 20:30:52 -0700655 case WindowManagerGlobal.ADD_STARTING_NOT_NEEDED:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 // Silently ignore -- we would have just removed it
657 // right away, anyway.
658 return;
Jeff Brown98365d72012-08-19 20:30:52 -0700659 case WindowManagerGlobal.ADD_MULTIPLE_SINGLETON:
Alan Viverette73f6d602015-09-14 16:01:19 -0400660 throw new WindowManager.BadTokenException("Unable to add window "
661 + mWindow + " -- another window of type "
662 + mWindowAttributes.type + " already exists");
Jeff Brown98365d72012-08-19 20:30:52 -0700663 case WindowManagerGlobal.ADD_PERMISSION_DENIED:
Alan Viverette73f6d602015-09-14 16:01:19 -0400664 throw new WindowManager.BadTokenException("Unable to add window "
665 + mWindow + " -- permission denied for window type "
666 + mWindowAttributes.type);
Craig Mautner6018aee2012-10-23 14:27:49 -0700667 case WindowManagerGlobal.ADD_INVALID_DISPLAY:
Alan Viverette73f6d602015-09-14 16:01:19 -0400668 throw new WindowManager.InvalidDisplayException("Unable to add window "
669 + mWindow + " -- the specified display can not be found");
Wale Ogunwale74bf0652015-01-12 10:24:36 -0800670 case WindowManagerGlobal.ADD_INVALID_TYPE:
Alan Viverette73f6d602015-09-14 16:01:19 -0400671 throw new WindowManager.InvalidDisplayException("Unable to add window "
672 + mWindow + " -- the specified window type "
673 + mWindowAttributes.type + " is not valid");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 }
675 throw new RuntimeException(
Wale Ogunwale74bf0652015-01-12 10:24:36 -0800676 "Unable to add window -- unknown error code " + res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700678
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700679 if (view instanceof RootViewSurfaceTaker) {
680 mInputQueueCallback =
681 ((RootViewSurfaceTaker)view).willYouTakeTheInputQueue();
682 }
Jeff Browncc4f7db2011-08-30 20:34:48 -0700683 if (mInputChannel != null) {
684 if (mInputQueueCallback != null) {
Michael Wrighta44dd262013-04-10 21:12:00 -0700685 mInputQueue = new InputQueue();
Jeff Browncc4f7db2011-08-30 20:34:48 -0700686 mInputQueueCallback.onInputQueueCreated(mInputQueue);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700687 }
Michael Wrighta44dd262013-04-10 21:12:00 -0700688 mInputEventReceiver = new WindowInputEventReceiver(mInputChannel,
689 Looper.myLooper());
Jeff Brown46b9ac02010-04-22 18:58:52 -0700690 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 view.assignParent(this);
Jeff Brown98365d72012-08-19 20:30:52 -0700693 mAddedTouchMode = (res & WindowManagerGlobal.ADD_FLAG_IN_TOUCH_MODE) != 0;
694 mAppVisible = (res & WindowManagerGlobal.ADD_FLAG_APP_VISIBLE) != 0;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700695
696 if (mAccessibilityManager.isEnabled()) {
697 mAccessibilityInteractionConnectionManager.ensureConnection();
698 }
Svetoslav Ganov42138042012-03-20 11:51:39 -0700699
700 if (view.getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
701 view.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
702 }
Michael Wright95ae9422013-03-14 10:58:50 -0700703
Jeff Brownf9e989d2013-04-04 23:04:03 -0700704 // Set up the input pipeline.
705 CharSequence counterSuffix = attrs.getTitle();
Michael Wright899d7052014-04-23 17:23:39 -0700706 mSyntheticInputStage = new SyntheticInputStage();
707 InputStage viewPostImeStage = new ViewPostImeInputStage(mSyntheticInputStage);
Jeff Brownf9e989d2013-04-04 23:04:03 -0700708 InputStage nativePostImeStage = new NativePostImeInputStage(viewPostImeStage,
709 "aq:native-post-ime:" + counterSuffix);
710 InputStage earlyPostImeStage = new EarlyPostImeInputStage(nativePostImeStage);
711 InputStage imeStage = new ImeInputStage(earlyPostImeStage,
712 "aq:ime:" + counterSuffix);
713 InputStage viewPreImeStage = new ViewPreImeInputStage(imeStage);
714 InputStage nativePreImeStage = new NativePreImeInputStage(viewPreImeStage,
715 "aq:native-pre-ime:" + counterSuffix);
716
717 mFirstInputStage = nativePreImeStage;
718 mFirstPostImeInputStage = earlyPostImeStage;
719 mPendingInputEventQueueLengthCounterName = "aq:pending:" + counterSuffix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 }
721 }
722 }
723
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -0800724 private void setTag() {
725 final String[] split = mWindowAttributes.getTitle().toString().split("\\.");
726 if (split.length > 0) {
727 mTag = TAG + "[" + split[split.length - 1] + "]";
728 }
729 }
730
keunyoung30f420f2013-08-02 14:23:10 -0700731 /** Whether the window is in local focus mode or not */
732 private boolean isInLocalFocusMode() {
733 return (mWindowAttributes.flags & WindowManager.LayoutParams.FLAG_LOCAL_FOCUS_MODE) != 0;
734 }
735
Dianne Hackborn49b043f2015-05-07 14:21:38 -0700736 public int getWindowFlags() {
737 return mWindowAttributes.flags;
738 }
739
Dianne Hackbornece0f4f2015-06-11 13:29:01 -0700740 public int getDisplayId() {
741 return mDisplay.getDisplayId();
742 }
743
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800744 public CharSequence getTitle() {
745 return mWindowAttributes.getTitle();
746 }
747
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800748 void destroyHardwareResources() {
Romain Guy31f2c2e2011-11-21 10:55:41 -0800749 if (mAttachInfo.mHardwareRenderer != null) {
750 mAttachInfo.mHardwareRenderer.destroyHardwareResources(mView);
John Reckf47a5942014-06-30 16:20:04 -0700751 mAttachInfo.mHardwareRenderer.destroy();
Romain Guy31f2c2e2011-11-21 10:55:41 -0800752 }
753 }
754
Bo Liu845535a2014-03-21 12:06:23 -0700755 public void detachFunctor(long functor) {
John Reck44ac42a2014-05-16 14:46:07 -0700756 if (mAttachInfo.mHardwareRenderer != null) {
757 // Fence so that any pending invokeFunctor() messages will be processed
758 // before we return from detachFunctor.
John Reckf47a5942014-06-30 16:20:04 -0700759 mAttachInfo.mHardwareRenderer.stopDrawing();
John Reck44ac42a2014-05-16 14:46:07 -0700760 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700761 }
762
John Reck3b202512014-06-23 13:13:08 -0700763 /**
764 * Schedules the functor for execution in either kModeProcess or
765 * kModeProcessNoContext, depending on whether or not there is an EGLContext.
766 *
767 * @param functor The native functor to invoke
768 * @param waitForCompletion If true, this will not return until the functor
769 * has invoked. If false, the functor may be invoked
770 * asynchronously.
771 */
Hui Shu9970aee2014-06-23 17:10:30 -0700772 public void invokeFunctor(long functor, boolean waitForCompletion) {
John Reck3b202512014-06-23 13:13:08 -0700773 ThreadedRenderer.invokeFunctor(functor, waitForCompletion);
Bo Liuae738a72014-04-27 16:22:04 -0700774 }
775
John Reck119907c2014-08-14 09:02:01 -0700776 public void registerAnimatingRenderNode(RenderNode animator) {
777 if (mAttachInfo.mHardwareRenderer != null) {
778 mAttachInfo.mHardwareRenderer.registerAnimatingRenderNode(animator);
779 } else {
780 if (mAttachInfo.mPendingAnimatingRenderNodes == null) {
781 mAttachInfo.mPendingAnimatingRenderNodes = new ArrayList<RenderNode>();
782 }
783 mAttachInfo.mPendingAnimatingRenderNodes.add(animator);
784 }
785 }
786
Romain Guy3b748a42013-04-17 18:54:38 -0700787 private void enableHardwareAcceleration(WindowManager.LayoutParams attrs) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -0800788 mAttachInfo.mHardwareAccelerated = false;
789 mAttachInfo.mHardwareAccelerationRequested = false;
Romain Guy4f6aff32011-01-12 16:21:41 -0800790
Romain Guy856d4e12011-10-14 15:47:55 -0700791 // Don't enable hardware acceleration when the application is in compatibility mode
John Reckdd58e792014-04-02 16:54:28 +0000792 if (mTranslator != null) return;
Romain Guy856d4e12011-10-14 15:47:55 -0700793
Dianne Hackborn7eec10e2010-11-12 18:03:47 -0800794 // Try to enable hardware acceleration if requested
Igor Murashkina86ab6402013-08-30 12:58:36 -0700795 final boolean hardwareAccelerated =
Jim Miller1b365922011-03-09 19:38:07 -0800796 (attrs.flags & WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0;
797
John Reckdd58e792014-04-02 16:54:28 +0000798 if (hardwareAccelerated) {
John Reck51aaf902015-12-02 15:08:07 -0800799 if (!ThreadedRenderer.isAvailable()) {
Romain Guy1af23a32011-03-24 16:03:55 -0700800 return;
801 }
802
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700803 // Persistent processes (including the system) should not do
804 // accelerated rendering on low-end devices. In that case,
805 // sRendererDisabled will be set. In addition, the system process
806 // itself should never do accelerated rendering. In that case, both
807 // sRendererDisabled and sSystemRendererDisabled are set. When
808 // sSystemRendererDisabled is set, PRIVATE_FLAG_FORCE_HARDWARE_ACCELERATED
809 // can be used by code on the system process to escape that and enable
810 // HW accelerated drawing. (This is basically for the lock screen.)
Jim Miller1b365922011-03-09 19:38:07 -0800811
John Reck61375a82014-09-18 19:27:48 +0000812 final boolean fakeHwAccelerated = (attrs.privateFlags &
813 WindowManager.LayoutParams.PRIVATE_FLAG_FAKE_HARDWARE_ACCELERATED) != 0;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700814 final boolean forceHwAccelerated = (attrs.privateFlags &
815 WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_HARDWARE_ACCELERATED) != 0;
Jim Miller1b365922011-03-09 19:38:07 -0800816
John Reck61375a82014-09-18 19:27:48 +0000817 if (fakeHwAccelerated) {
818 // This is exclusively for the preview windows the window manager
819 // shows for launching applications, so they will look more like
820 // the app being launched.
821 mAttachInfo.mHardwareAccelerationRequested = true;
John Reck51aaf902015-12-02 15:08:07 -0800822 } else if (!ThreadedRenderer.sRendererDisabled
823 || (ThreadedRenderer.sSystemRendererDisabled && forceHwAccelerated)) {
Romain Guyb051e892010-09-28 19:09:36 -0700824 if (mAttachInfo.mHardwareRenderer != null) {
John Reckf47a5942014-06-30 16:20:04 -0700825 mAttachInfo.mHardwareRenderer.destroy();
Romain Guy211370f2012-02-01 16:10:55 -0800826 }
827
Alan Viverette2b12b582014-10-29 11:11:40 -0700828 final Rect insets = attrs.surfaceInsets;
Alan Viverette2cd23e62014-11-04 17:04:02 -0800829 final boolean hasSurfaceInsets = insets.left != 0 || insets.right != 0
830 || insets.top != 0 || insets.bottom != 0;
Alan Viverette2b12b582014-10-29 11:11:40 -0700831 final boolean translucent = attrs.format != PixelFormat.OPAQUE || hasSurfaceInsets;
John Reck51aaf902015-12-02 15:08:07 -0800832 mAttachInfo.mHardwareRenderer = ThreadedRenderer.create(mContext, translucent);
Romain Guye55945e2013-04-04 15:26:04 -0700833 if (mAttachInfo.mHardwareRenderer != null) {
834 mAttachInfo.mHardwareRenderer.setName(attrs.getTitle().toString());
835 mAttachInfo.mHardwareAccelerated =
836 mAttachInfo.mHardwareAccelerationRequested = true;
837 }
Romain Guye4d01122010-06-16 18:44:05 -0700838 }
839 }
840 }
841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 public View getView() {
843 return mView;
844 }
845
846 final WindowLeaked getLocation() {
847 return mLocation;
848 }
849
850 void setLayoutParams(WindowManager.LayoutParams attrs, boolean newView) {
851 synchronized (this) {
Alan Viverettedbed8932014-08-06 17:54:52 -0700852 final int oldInsetLeft = mWindowAttributes.surfaceInsets.left;
853 final int oldInsetTop = mWindowAttributes.surfaceInsets.top;
854 final int oldInsetRight = mWindowAttributes.surfaceInsets.right;
855 final int oldInsetBottom = mWindowAttributes.surfaceInsets.bottom;
856 final int oldSoftInputMode = mWindowAttributes.softInputMode;
Alan Viverette5435a302015-01-29 10:25:34 -0800857 final boolean oldHasManualSurfaceInsets = mWindowAttributes.hasManualSurfaceInsets;
Alan Viverettedbed8932014-08-06 17:54:52 -0700858
Dianne Hackborn9d090892012-06-11 18:35:41 -0700859 // Keep track of the actual window flags supplied by the client.
860 mClientWindowLayoutFlags = attrs.flags;
Alan Viverettedbed8932014-08-06 17:54:52 -0700861
862 // Preserve compatible window flag if exists.
863 final int compatibleWindowFlag = mWindowAttributes.privateFlags
Adam Lesinski95c42972013-10-02 10:13:27 -0700864 & WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW;
Alan Viverettedbed8932014-08-06 17:54:52 -0700865
866 // Transfer over system UI visibility values as they carry current state.
Craig Mautner3fe38c02012-05-03 17:28:09 -0700867 attrs.systemUiVisibility = mWindowAttributes.systemUiVisibility;
868 attrs.subtreeSystemUiVisibility = mWindowAttributes.subtreeSystemUiVisibility;
Alan Viverettedbed8932014-08-06 17:54:52 -0700869
Romain Guyf21c9b02011-09-06 16:56:54 -0700870 mWindowAttributesChangesFlag = mWindowAttributes.copyFrom(attrs);
John Spurlockbd957402013-10-03 11:38:39 -0400871 if ((mWindowAttributesChangesFlag
872 & WindowManager.LayoutParams.TRANSLUCENT_FLAGS_CHANGED) != 0) {
873 // Recompute system ui visibility.
874 mAttachInfo.mRecomputeGlobalAttributes = true;
875 }
Dianne Hackbornc2293022013-02-06 23:14:49 -0800876 if (mWindowAttributes.packageName == null) {
877 mWindowAttributes.packageName = mBasePackageName;
878 }
Adam Lesinski95c42972013-10-02 10:13:27 -0700879 mWindowAttributes.privateFlags |= compatibleWindowFlag;
Dianne Hackborn9d090892012-06-11 18:35:41 -0700880
Alan Viverettedbed8932014-08-06 17:54:52 -0700881 // Restore old surface insets.
882 mWindowAttributes.surfaceInsets.set(
883 oldInsetLeft, oldInsetTop, oldInsetRight, oldInsetBottom);
Alan Viverette5435a302015-01-29 10:25:34 -0800884 mWindowAttributes.hasManualSurfaceInsets = oldHasManualSurfaceInsets;
Alan Viverettedbed8932014-08-06 17:54:52 -0700885
Dianne Hackborn9d090892012-06-11 18:35:41 -0700886 applyKeepScreenOnFlag(mWindowAttributes);
887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 if (newView) {
889 mSoftInputMode = attrs.softInputMode;
890 requestLayout();
891 }
Alan Viverettedbed8932014-08-06 17:54:52 -0700892
The Android Open Source Project10592532009-03-18 17:39:46 -0700893 // Don't lose the mode we last auto-computed.
Alan Viverettedbed8932014-08-06 17:54:52 -0700894 if ((attrs.softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
The Android Open Source Project10592532009-03-18 17:39:46 -0700895 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED) {
896 mWindowAttributes.softInputMode = (mWindowAttributes.softInputMode
897 & ~WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
Alan Viverettedbed8932014-08-06 17:54:52 -0700898 | (oldSoftInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST);
The Android Open Source Project10592532009-03-18 17:39:46 -0700899 }
Alan Viverettedbed8932014-08-06 17:54:52 -0700900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 mWindowAttributesChanged = true;
902 scheduleTraversals();
903 }
904 }
905
906 void handleAppVisibility(boolean visible) {
907 if (mAppVisible != visible) {
908 mAppVisible = visible;
909 scheduleTraversals();
John Reck73840ea2014-09-22 07:39:18 -0700910 if (!mAppVisible) {
911 WindowManagerGlobal.trimForeground();
912 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 }
914 }
915
916 void handleGetNewSurface() {
917 mNewSurfaceNeeded = true;
918 mFullRedrawNeeded = true;
919 scheduleTraversals();
920 }
921
Jeff Brownd912e1f2014-04-11 18:46:22 -0700922 private final DisplayListener mDisplayListener = new DisplayListener() {
923 @Override
924 public void onDisplayChanged(int displayId) {
925 if (mView != null && mDisplay.getDisplayId() == displayId) {
926 final int oldDisplayState = mAttachInfo.mDisplayState;
927 final int newDisplayState = mDisplay.getState();
928 if (oldDisplayState != newDisplayState) {
929 mAttachInfo.mDisplayState = newDisplayState;
Jeff Brownc2932a12014-11-20 18:04:05 -0800930 pokeDrawLockIfNeeded();
Jeff Brownd912e1f2014-04-11 18:46:22 -0700931 if (oldDisplayState != Display.STATE_UNKNOWN) {
932 final int oldScreenState = toViewScreenState(oldDisplayState);
933 final int newScreenState = toViewScreenState(newDisplayState);
934 if (oldScreenState != newScreenState) {
935 mView.dispatchScreenStateChanged(newScreenState);
936 }
937 if (oldDisplayState == Display.STATE_OFF) {
938 // Draw was suppressed so we need to for it to happen here.
939 mFullRedrawNeeded = true;
940 scheduleTraversals();
941 }
942 }
943 }
Romain Guy7e4e5612012-03-05 14:37:29 -0800944 }
945 }
Jeff Brownd912e1f2014-04-11 18:46:22 -0700946
947 @Override
948 public void onDisplayRemoved(int displayId) {
949 }
950
951 @Override
952 public void onDisplayAdded(int displayId) {
953 }
954
955 private int toViewScreenState(int displayState) {
956 return displayState == Display.STATE_OFF ?
957 View.SCREEN_STATE_OFF : View.SCREEN_STATE_ON;
958 }
959 };
Romain Guy7e4e5612012-03-05 14:37:29 -0800960
Jeff Brownc2932a12014-11-20 18:04:05 -0800961 void pokeDrawLockIfNeeded() {
962 final int displayState = mAttachInfo.mDisplayState;
963 if (mView != null && mAdded && mTraversalScheduled
964 && (displayState == Display.STATE_DOZE
965 || displayState == Display.STATE_DOZE_SUSPEND)) {
966 try {
967 mWindowSession.pokeDrawLock(mWindow);
968 } catch (RemoteException ex) {
969 // System server died, oh well.
970 }
971 }
972 }
973
Craig Mautner6018aee2012-10-23 14:27:49 -0700974 @Override
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700975 public void requestFitSystemWindows() {
976 checkThread();
Adrian Roosfa104232014-06-20 16:10:14 -0700977 mApplyInsetsRequested = true;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700978 scheduleTraversals();
979 }
980
Craig Mautner6018aee2012-10-23 14:27:49 -0700981 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 public void requestLayout() {
Chet Haase3efa7b52012-12-03 08:33:17 -0800983 if (!mHandlingLayoutInLayoutRequest) {
984 checkThread();
985 mLayoutRequested = true;
986 scheduleTraversals();
987 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 }
989
Craig Mautner6018aee2012-10-23 14:27:49 -0700990 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 public boolean isLayoutRequested() {
992 return mLayoutRequested;
993 }
994
Romain Guycfef1232012-02-23 13:50:37 -0800995 void invalidate() {
996 mDirty.set(0, 0, mWidth, mHeight);
John Recke4267ea2014-06-03 15:53:15 -0700997 if (!mWillDrawSoon) {
998 scheduleTraversals();
999 }
Romain Guycfef1232012-02-23 13:50:37 -08001000 }
1001
Dianne Hackborna53de062012-05-08 18:53:51 -07001002 void invalidateWorld(View view) {
1003 view.invalidate();
1004 if (view instanceof ViewGroup) {
Romain Guyf84208f2012-09-13 22:50:18 -07001005 ViewGroup parent = (ViewGroup) view;
1006 for (int i = 0; i < parent.getChildCount(); i++) {
Dianne Hackborna53de062012-05-08 18:53:51 -07001007 invalidateWorld(parent.getChildAt(i));
1008 }
1009 }
1010 }
1011
Craig Mautner6018aee2012-10-23 14:27:49 -07001012 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 public void invalidateChild(View child, Rect dirty) {
Romain Guycfef1232012-02-23 13:50:37 -08001014 invalidateChildInParent(null, dirty);
1015 }
1016
Craig Mautner8f303ad2013-06-14 11:32:22 -07001017 @Override
Romain Guycfef1232012-02-23 13:50:37 -08001018 public ViewParent invalidateChildInParent(int[] location, Rect dirty) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 checkThread();
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001020 if (DEBUG_DRAW) Log.v(mTag, "Invalidate child: " + dirty);
Romain Guycfef1232012-02-23 13:50:37 -08001021
Chet Haase70d4ba12010-10-06 09:46:45 -07001022 if (dirty == null) {
Chet Haase70d4ba12010-10-06 09:46:45 -07001023 invalidate();
Romain Guycfef1232012-02-23 13:50:37 -08001024 return null;
Chet Haase3561d062012-10-23 12:54:51 -07001025 } else if (dirty.isEmpty() && !mIsAnimating) {
Chet Haase05e91ed2012-07-03 14:17:57 -07001026 return null;
Chet Haase70d4ba12010-10-06 09:46:45 -07001027 }
Romain Guycfef1232012-02-23 13:50:37 -08001028
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001029 if (mCurScrollY != 0 || mTranslator != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 mTempRect.set(dirty);
Romain Guy1e095972009-07-07 11:22:45 -07001031 dirty = mTempRect;
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001032 if (mCurScrollY != 0) {
Romain Guycfef1232012-02-23 13:50:37 -08001033 dirty.offset(0, -mCurScrollY);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001034 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001035 if (mTranslator != null) {
Romain Guy1e095972009-07-07 11:22:45 -07001036 mTranslator.translateRectInAppWindowToScreen(dirty);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001037 }
Romain Guy1e095972009-07-07 11:22:45 -07001038 if (mAttachInfo.mScalingRequired) {
1039 dirty.inset(-1, -1);
1040 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 }
Romain Guycfef1232012-02-23 13:50:37 -08001042
Alan Viverettea7ea65e2015-05-15 11:30:21 -07001043 invalidateRectOnScreen(dirty);
1044
1045 return null;
1046 }
1047
1048 private void invalidateRectOnScreen(Rect dirty) {
Romain Guycfef1232012-02-23 13:50:37 -08001049 final Rect localDirty = mDirty;
1050 if (!localDirty.isEmpty() && !localDirty.contains(dirty)) {
Romain Guy02ccac62011-06-24 13:20:23 -07001051 mAttachInfo.mSetIgnoreDirtyState = true;
Romain Guy7d695942010-12-01 17:22:29 -08001052 mAttachInfo.mIgnoreDirtyState = true;
1053 }
Romain Guycfef1232012-02-23 13:50:37 -08001054
1055 // Add the new dirty rect to the current one
1056 localDirty.union(dirty.left, dirty.top, dirty.right, dirty.bottom);
1057 // Intersect with the bounds of the window to skip
1058 // updates that lie outside of the visible region
Romain Guy7b2f8b82012-03-19 17:18:54 -07001059 final float appScale = mAttachInfo.mApplicationScale;
Chet Haase3561d062012-10-23 12:54:51 -07001060 final boolean intersected = localDirty.intersect(0, 0,
1061 (int) (mWidth * appScale + 0.5f), (int) (mHeight * appScale + 0.5f));
1062 if (!intersected) {
Chet Haaseb78ee0e2012-10-17 11:32:01 -07001063 localDirty.setEmpty();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 }
Chet Haase3561d062012-10-23 12:54:51 -07001065 if (!mWillDrawSoon && (intersected || mIsAnimating)) {
1066 scheduleTraversals();
1067 }
Romain Guy0d9275e2010-10-26 14:22:30 -07001068 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069
Daniel Koulomzin087ae472015-12-16 17:52:25 -05001070 public void setIsAmbientMode(boolean ambient) {
1071 mIsAmbientMode = ambient;
1072 }
1073
George Mount41725de2015-04-09 08:23:05 -07001074 void setWindowStopped(boolean stopped) {
Dianne Hackbornce418e62011-03-01 14:31:38 -08001075 if (mStopped != stopped) {
1076 mStopped = stopped;
George Mount41725de2015-04-09 08:23:05 -07001077 if (!mStopped) {
Dianne Hackbornce418e62011-03-01 14:31:38 -08001078 scheduleTraversals();
John Reckd9b16072016-02-23 10:35:19 -08001079 } else {
1080 destroyHardwareResources();
Dianne Hackbornce418e62011-03-01 14:31:38 -08001081 }
1082 }
1083 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084
George Mount41725de2015-04-09 08:23:05 -07001085 /**
1086 * Block the input events during an Activity Transition. The KEYCODE_BACK event is allowed
1087 * through to allow quick reversal of the Activity Transition.
1088 *
1089 * @param paused true to pause, false to resume.
1090 */
1091 public void setPausedForTransition(boolean paused) {
1092 mPausedForTransition = paused;
1093 }
1094
Craig Mautner8f303ad2013-06-14 11:32:22 -07001095 @Override
Romain Guycfef1232012-02-23 13:50:37 -08001096 public ViewParent getParent() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 return null;
1098 }
1099
Craig Mautner8f303ad2013-06-14 11:32:22 -07001100 @Override
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001101 public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 if (child != mView) {
1103 throw new RuntimeException("child is not mine, honest!");
1104 }
1105 // Note: don't apply scroll offset, because we want to know its
1106 // visibility in the virtual canvas being given to the view hierarchy.
1107 return r.intersect(0, 0, mWidth, mHeight);
1108 }
1109
Igor Murashkina86ab6402013-08-30 12:58:36 -07001110 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 public void bringChildToFront(View child) {
1112 }
1113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 int getHostVisibility() {
Filip Gruszczynski1a4dfe52015-11-15 10:58:57 -08001115 return (mAppVisible || mForceDecorViewVisibility) ? mView.getVisibility() : View.GONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 }
Romain Guy8506ab42009-06-11 17:35:47 -07001117
Chet Haasecca2c982011-05-20 14:34:18 -07001118 /**
1119 * Add LayoutTransition to the list of transitions to be started in the next traversal.
1120 * This list will be cleared after the transitions on the list are start()'ed. These
1121 * transitionsa re added by LayoutTransition itself when it sets up animations. The setup
1122 * happens during the layout phase of traversal, which we want to complete before any of the
1123 * animations are started (because those animations may side-effect properties that layout
1124 * depends upon, like the bounding rectangles of the affected views). So we add the transition
1125 * to the list and it is started just prior to starting the drawing phase of traversal.
1126 *
1127 * @param transition The LayoutTransition to be started on the next traversal.
1128 *
1129 * @hide
1130 */
1131 public void requestTransitionStart(LayoutTransition transition) {
1132 if (mPendingTransitions == null || !mPendingTransitions.contains(transition)) {
1133 if (mPendingTransitions == null) {
1134 mPendingTransitions = new ArrayList<LayoutTransition>();
1135 }
1136 mPendingTransitions.add(transition);
1137 }
1138 }
1139
John Recka5dda642014-05-22 15:43:54 -07001140 /**
1141 * Notifies the HardwareRenderer that a new frame will be coming soon.
1142 * Currently only {@link ThreadedRenderer} cares about this, and uses
1143 * this knowledge to adjust the scheduling of off-thread animations
1144 */
1145 void notifyRendererOfFramePending() {
1146 if (mAttachInfo.mHardwareRenderer != null) {
1147 mAttachInfo.mHardwareRenderer.notifyFramePending();
1148 }
1149 }
1150
Jeff Brownebb2d8d2012-03-23 17:14:34 -07001151 void scheduleTraversals() {
1152 if (!mTraversalScheduled) {
1153 mTraversalScheduled = true;
Jeff Brown3d4e7efe2015-02-26 15:34:16 -08001154 mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier();
Jeff Brownebb2d8d2012-03-23 17:14:34 -07001155 mChoreographer.postCallback(
1156 Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
Michael Wright9d744c72014-02-18 21:27:42 -08001157 if (!mUnbufferedInputDispatch) {
1158 scheduleConsumeBatchedInput();
1159 }
John Recka5dda642014-05-22 15:43:54 -07001160 notifyRendererOfFramePending();
Jeff Brownc2932a12014-11-20 18:04:05 -08001161 pokeDrawLockIfNeeded();
Jeff Brown96e942d2011-11-30 19:55:01 -08001162 }
Jeff Brownebb2d8d2012-03-23 17:14:34 -07001163 }
Jeff Brown96e942d2011-11-30 19:55:01 -08001164
Jeff Brownebb2d8d2012-03-23 17:14:34 -07001165 void unscheduleTraversals() {
1166 if (mTraversalScheduled) {
1167 mTraversalScheduled = false;
Jeff Brown3d4e7efe2015-02-26 15:34:16 -08001168 mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier);
Jeff Brownebb2d8d2012-03-23 17:14:34 -07001169 mChoreographer.removeCallbacks(
1170 Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
1171 }
1172 }
1173
1174 void doTraversal() {
1175 if (mTraversalScheduled) {
1176 mTraversalScheduled = false;
Jeff Brown3d4e7efe2015-02-26 15:34:16 -08001177 mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier);
Jeff Brownebb2d8d2012-03-23 17:14:34 -07001178
Jeff Brownebb2d8d2012-03-23 17:14:34 -07001179 if (mProfile) {
1180 Debug.startMethodTracing("ViewAncestor");
Jeff Brown96e942d2011-11-30 19:55:01 -08001181 }
Jeff Brown96e942d2011-11-30 19:55:01 -08001182
Chris Craike22c59b2015-05-21 18:33:37 -07001183 performTraversals();
Jeff Brown96e942d2011-11-30 19:55:01 -08001184
Jeff Brownebb2d8d2012-03-23 17:14:34 -07001185 if (mProfile) {
1186 Debug.stopMethodTracing();
1187 mProfile = false;
1188 }
Jeff Brown96e942d2011-11-30 19:55:01 -08001189 }
1190 }
1191
Dianne Hackborn9d090892012-06-11 18:35:41 -07001192 private void applyKeepScreenOnFlag(WindowManager.LayoutParams params) {
1193 // Update window's global keep screen on flag: if a view has requested
1194 // that the screen be kept on, then it is always set; otherwise, it is
1195 // set to whatever the client last requested for the global state.
1196 if (mAttachInfo.mKeepScreenOn) {
1197 params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
1198 } else {
1199 params.flags = (params.flags&~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
1200 | (mClientWindowLayoutFlags&WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1201 }
1202 }
1203
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001204 private boolean collectViewAttributes() {
Chris Craikd36a81f2014-07-17 10:16:51 -07001205 if (mAttachInfo.mRecomputeGlobalAttributes) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001206 //Log.i(mTag, "Computing view hierarchy attributes!");
Chris Craikd36a81f2014-07-17 10:16:51 -07001207 mAttachInfo.mRecomputeGlobalAttributes = false;
1208 boolean oldScreenOn = mAttachInfo.mKeepScreenOn;
1209 mAttachInfo.mKeepScreenOn = false;
1210 mAttachInfo.mSystemUiVisibility = 0;
1211 mAttachInfo.mHasSystemUiListeners = false;
1212 mView.dispatchCollectViewAttributes(mAttachInfo, 0);
1213 mAttachInfo.mSystemUiVisibility &= ~mAttachInfo.mDisabledSystemUiVisibility;
Craig Mautner7eac0f52012-09-13 13:14:14 -07001214 WindowManager.LayoutParams params = mWindowAttributes;
Chris Craikd36a81f2014-07-17 10:16:51 -07001215 mAttachInfo.mSystemUiVisibility |= getImpliedSystemUiVisibility(params);
1216 if (mAttachInfo.mKeepScreenOn != oldScreenOn
1217 || mAttachInfo.mSystemUiVisibility != params.subtreeSystemUiVisibility
1218 || mAttachInfo.mHasSystemUiListeners != params.hasSystemUiListeners) {
Dianne Hackborn9d090892012-06-11 18:35:41 -07001219 applyKeepScreenOnFlag(params);
Chris Craikd36a81f2014-07-17 10:16:51 -07001220 params.subtreeSystemUiVisibility = mAttachInfo.mSystemUiVisibility;
1221 params.hasSystemUiListeners = mAttachInfo.mHasSystemUiListeners;
1222 mView.dispatchWindowSystemUiVisiblityChanged(mAttachInfo.mSystemUiVisibility);
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001223 return true;
1224 }
1225 }
1226 return false;
1227 }
1228
John Spurlockbd957402013-10-03 11:38:39 -04001229 private int getImpliedSystemUiVisibility(WindowManager.LayoutParams params) {
1230 int vis = 0;
1231 // Translucent decor window flags imply stable system ui visibility.
1232 if ((params.flags & WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) != 0) {
1233 vis |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
1234 }
1235 if ((params.flags & WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION) != 0) {
1236 vis |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1237 }
1238 return vis;
1239 }
1240
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001241 private boolean measureHierarchy(final View host, final WindowManager.LayoutParams lp,
1242 final Resources res, final int desiredWindowWidth, final int desiredWindowHeight) {
1243 int childWidthMeasureSpec;
1244 int childHeightMeasureSpec;
1245 boolean windowSizeMayChange = false;
1246
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001247 if (DEBUG_ORIENTATION || DEBUG_LAYOUT) Log.v(mTag,
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001248 "Measuring " + host + " in display " + desiredWindowWidth
1249 + "x" + desiredWindowHeight + "...");
1250
1251 boolean goodMeasure = false;
1252 if (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT) {
1253 // On large screens, we don't want to allow dialogs to just
1254 // stretch to fill the entire width of the screen to display
1255 // one line of text. First try doing the layout at a smaller
1256 // size to see if it will fit.
1257 final DisplayMetrics packageMetrics = res.getDisplayMetrics();
1258 res.getValue(com.android.internal.R.dimen.config_prefDialogWidth, mTmpValue, true);
1259 int baseSize = 0;
1260 if (mTmpValue.type == TypedValue.TYPE_DIMENSION) {
1261 baseSize = (int)mTmpValue.getDimension(packageMetrics);
1262 }
Filip Gruszczynski1937a4c2016-01-19 16:17:13 -08001263 if (DEBUG_DIALOG) Log.v(mTag, "Window " + mView + ": baseSize=" + baseSize
1264 + ", desiredWindowWidth=" + desiredWindowWidth);
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001265 if (baseSize != 0 && desiredWindowWidth > baseSize) {
1266 childWidthMeasureSpec = getRootMeasureSpec(baseSize, lp.width);
1267 childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
Jeff Brownc8d2668b2012-05-16 17:23:59 -07001268 performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001269 if (DEBUG_DIALOG) Log.v(mTag, "Window " + mView + ": measured ("
Filip Gruszczynski1937a4c2016-01-19 16:17:13 -08001270 + host.getMeasuredWidth() + "," + host.getMeasuredHeight()
1271 + ") from width spec: " + MeasureSpec.toString(childWidthMeasureSpec)
1272 + " and height spec: " + MeasureSpec.toString(childHeightMeasureSpec));
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001273 if ((host.getMeasuredWidthAndState()&View.MEASURED_STATE_TOO_SMALL) == 0) {
1274 goodMeasure = true;
1275 } else {
1276 // Didn't fit in that size... try expanding a bit.
1277 baseSize = (baseSize+desiredWindowWidth)/2;
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001278 if (DEBUG_DIALOG) Log.v(mTag, "Window " + mView + ": next baseSize="
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001279 + baseSize);
1280 childWidthMeasureSpec = getRootMeasureSpec(baseSize, lp.width);
Jeff Brownc8d2668b2012-05-16 17:23:59 -07001281 performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001282 if (DEBUG_DIALOG) Log.v(mTag, "Window " + mView + ": measured ("
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001283 + host.getMeasuredWidth() + "," + host.getMeasuredHeight() + ")");
1284 if ((host.getMeasuredWidthAndState()&View.MEASURED_STATE_TOO_SMALL) == 0) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001285 if (DEBUG_DIALOG) Log.v(mTag, "Good!");
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001286 goodMeasure = true;
1287 }
1288 }
1289 }
1290 }
1291
1292 if (!goodMeasure) {
1293 childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width);
1294 childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
Jeff Brownc8d2668b2012-05-16 17:23:59 -07001295 performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001296 if (mWidth != host.getMeasuredWidth() || mHeight != host.getMeasuredHeight()) {
1297 windowSizeMayChange = true;
1298 }
1299 }
1300
1301 if (DBG) {
1302 System.out.println("======================================");
1303 System.out.println("performTraversals -- after measure");
1304 host.debug();
1305 }
1306
1307 return windowSizeMayChange;
1308 }
1309
Alan Viverettefed3f722013-11-14 14:48:20 -08001310 /**
1311 * Modifies the input matrix such that it maps view-local coordinates to
1312 * on-screen coordinates.
1313 *
1314 * @param m input matrix to modify
1315 */
1316 void transformMatrixToGlobal(Matrix m) {
Dake Gu7bf379c2014-07-15 16:29:38 -07001317 m.preTranslate(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
Alan Viverettefed3f722013-11-14 14:48:20 -08001318 }
1319
1320 /**
1321 * Modifies the input matrix such that it maps on-screen coordinates to
1322 * view-local coordinates.
1323 *
1324 * @param m input matrix to modify
1325 */
1326 void transformMatrixToLocal(Matrix m) {
Dake Gu7bf379c2014-07-15 16:29:38 -07001327 m.postTranslate(-mAttachInfo.mWindowLeft, -mAttachInfo.mWindowTop);
Alan Viverettefed3f722013-11-14 14:48:20 -08001328 }
1329
Filip Gruszczynski954289d2015-02-26 15:46:47 -08001330 /* package */ WindowInsets getWindowInsets(boolean forceConstruct) {
1331 if (mLastWindowInsets == null || forceConstruct) {
1332 mDispatchContentInsets.set(mAttachInfo.mContentInsets);
1333 mDispatchStableInsets.set(mAttachInfo.mStableInsets);
1334 Rect contentInsets = mDispatchContentInsets;
1335 Rect stableInsets = mDispatchStableInsets;
1336 // For dispatch we preserve old logic, but for direct requests from Views we allow to
1337 // immediately use pending insets.
1338 if (!forceConstruct
1339 && (!mPendingContentInsets.equals(contentInsets) ||
1340 !mPendingStableInsets.equals(stableInsets))) {
1341 contentInsets = mPendingContentInsets;
1342 stableInsets = mPendingStableInsets;
1343 }
Filip Gruszczynski2217f612015-05-26 11:32:08 -07001344 Rect outsets = mAttachInfo.mOutsets;
1345 if (outsets.left > 0 || outsets.top > 0 || outsets.right > 0 || outsets.bottom > 0) {
1346 contentInsets = new Rect(contentInsets.left + outsets.left,
1347 contentInsets.top + outsets.top, contentInsets.right + outsets.right,
1348 contentInsets.bottom + outsets.bottom);
1349 }
Filip Gruszczynski954289d2015-02-26 15:46:47 -08001350 mLastWindowInsets = new WindowInsets(contentInsets,
Adam Powell01f280d2015-05-18 16:07:42 -07001351 null /* windowDecorInsets */, stableInsets,
Jorim Jaggi0ffd49c2016-02-12 15:04:21 -08001352 mContext.getResources().getConfiguration().isScreenRound(),
1353 mAttachInfo.mAlwaysConsumeNavBar);
Filip Gruszczynski954289d2015-02-26 15:46:47 -08001354 }
1355 return mLastWindowInsets;
1356 }
1357
Adam Powell2accbf92014-04-16 23:14:57 +00001358 void dispatchApplyInsets(View host) {
Filip Gruszczynski954289d2015-02-26 15:46:47 -08001359 host.dispatchApplyWindowInsets(getWindowInsets(true /* forceConstruct */));
Adam Powell2accbf92014-04-16 23:14:57 +00001360 }
1361
Chong Zhangf6525ce2016-01-14 17:09:56 -08001362 private static boolean shouldUseDisplaySize(final WindowManager.LayoutParams lp) {
1363 return lp.type == TYPE_STATUS_BAR_PANEL
1364 || lp.type == TYPE_INPUT_METHOD
1365 || lp.type == TYPE_VOLUME_OVERLAY;
1366 }
1367
1368 private int dipToPx(int dip) {
1369 final DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
1370 return (int) (displayMetrics.density * dip + 0.5f);
1371 }
1372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 private void performTraversals() {
1374 // cache mView since it is used so much below...
1375 final View host = mView;
1376
1377 if (DBG) {
1378 System.out.println("======================================");
1379 System.out.println("performTraversals");
1380 host.debug();
1381 }
1382
1383 if (host == null || !mAdded)
1384 return;
1385
Craig Mautnerdf2390a2012-08-29 20:59:22 -07001386 mIsInTraversal = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 mWillDrawSoon = true;
Dianne Hackborn711e62a2010-11-29 16:38:22 -08001388 boolean windowSizeMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 boolean newSurface = false;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001390 boolean surfaceChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 WindowManager.LayoutParams lp = mWindowAttributes;
1392
1393 int desiredWindowWidth;
1394 int desiredWindowHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 final int viewVisibility = getHostVisibility();
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001397 final boolean viewVisibilityChanged = !mFirst
1398 && (mViewVisibility != viewVisibility || mNewSurfaceNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399
1400 WindowManager.LayoutParams params = null;
1401 if (mWindowAttributesChanged) {
1402 mWindowAttributesChanged = false;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001403 surfaceChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 params = lp;
1405 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001406 CompatibilityInfo compatibilityInfo = mDisplayAdjustments.getCompatibilityInfo();
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001407 if (compatibilityInfo.supportsScreen() == mLastInCompatMode) {
1408 params = lp;
Jeff Brown96e942d2011-11-30 19:55:01 -08001409 mFullRedrawNeeded = true;
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001410 mLayoutRequested = true;
1411 if (mLastInCompatMode) {
Adam Lesinski95c42972013-10-02 10:13:27 -07001412 params.privateFlags &= ~WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW;
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001413 mLastInCompatMode = false;
1414 } else {
Adam Lesinski95c42972013-10-02 10:13:27 -07001415 params.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW;
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001416 mLastInCompatMode = true;
1417 }
1418 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07001419
Romain Guyf21c9b02011-09-06 16:56:54 -07001420 mWindowAttributesChangesFlag = 0;
Igor Murashkina86ab6402013-08-30 12:58:36 -07001421
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001422 Rect frame = mWinFrame;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 if (mFirst) {
Jeff Brown96e942d2011-11-30 19:55:01 -08001424 mFullRedrawNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 mLayoutRequested = true;
1426
Chong Zhangf6525ce2016-01-14 17:09:56 -08001427 if (shouldUseDisplaySize(lp)) {
Dianne Hackborna239c842011-06-01 12:28:20 -07001428 // NOTE -- system code, won't try to do compat mode.
Jeff Brownbc68a592011-07-25 12:58:12 -07001429 Point size = new Point();
Jeff Brown98365d72012-08-19 20:30:52 -07001430 mDisplay.getRealSize(size);
Jeff Brownbc68a592011-07-25 12:58:12 -07001431 desiredWindowWidth = size.x;
1432 desiredWindowHeight = size.y;
Dianne Hackborna239c842011-06-01 12:28:20 -07001433 } else {
Chong Zhangf6525ce2016-01-14 17:09:56 -08001434 Configuration config = mContext.getResources().getConfiguration();
1435 desiredWindowWidth = dipToPx(config.screenWidthDp);
1436 desiredWindowHeight = dipToPx(config.screenHeightDp);
Dianne Hackborna239c842011-06-01 12:28:20 -07001437 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438
Romain Guyc5d55862011-01-21 19:01:46 -08001439 // We used to use the following condition to choose 32 bits drawing caches:
1440 // PixelFormat.hasAlpha(lp.format) || lp.format == PixelFormat.RGBX_8888
1441 // However, windows are now always 32 bits by default, so choose 32 bits
Chris Craikd36a81f2014-07-17 10:16:51 -07001442 mAttachInfo.mUse32BitDrawingCache = true;
1443 mAttachInfo.mHasWindowFocus = false;
1444 mAttachInfo.mWindowVisibility = viewVisibility;
1445 mAttachInfo.mRecomputeGlobalAttributes = false;
Dianne Hackborn694f79b2010-03-17 19:44:59 -07001446 mLastConfiguration.setTo(host.getResources().getConfiguration());
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001447 mLastSystemUiVisibility = mAttachInfo.mSystemUiVisibility;
Fabrice Di Megliob003e282012-10-17 17:20:19 -07001448 // Set the layout direction if it has not been set before (inherit is the default)
1449 if (mViewLayoutDirectionInitial == View.LAYOUT_DIRECTION_INHERIT) {
1450 host.setLayoutDirection(mLastConfiguration.getLayoutDirection());
1451 }
Chris Craikd36a81f2014-07-17 10:16:51 -07001452 host.dispatchAttachedToWindow(mAttachInfo, 0);
1453 mAttachInfo.mTreeObserver.dispatchOnWindowAttachedChange(true);
Adam Powell2accbf92014-04-16 23:14:57 +00001454 dispatchApplyInsets(host);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001455 //Log.i(mTag, "Screen on initialized: " + attachInfo.mKeepScreenOn);
svetoslavganov75986cf2009-05-14 22:28:01 -07001456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 } else {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001458 desiredWindowWidth = frame.width();
1459 desiredWindowHeight = frame.height();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 if (desiredWindowWidth != mWidth || desiredWindowHeight != mHeight) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001461 if (DEBUG_ORIENTATION) Log.v(mTag, "View " + host + " resized to: " + frame);
Jeff Brown96e942d2011-11-30 19:55:01 -08001462 mFullRedrawNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 mLayoutRequested = true;
Dianne Hackborn711e62a2010-11-29 16:38:22 -08001464 windowSizeMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 }
1466 }
1467
1468 if (viewVisibilityChanged) {
Chris Craikd36a81f2014-07-17 10:16:51 -07001469 mAttachInfo.mWindowVisibility = viewVisibility;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 host.dispatchWindowVisibilityChanged(viewVisibility);
1471 if (viewVisibility != View.VISIBLE || mNewSurfaceNeeded) {
Skuhneb8160872015-09-22 09:51:39 -07001472 endDragResizing();
Romain Guy65b345f2011-07-27 18:51:50 -07001473 destroyHardwareResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 }
1475 if (viewVisibility == View.GONE) {
1476 // After making a window gone, we will count it as being
1477 // shown for the first time the next time it gets focus.
1478 mHasHadWindowFocus = false;
1479 }
1480 }
1481
Alan Viverette7dbc3bf2015-01-28 16:14:36 -08001482 // Non-visible windows can't hold accessibility focus.
1483 if (mAttachInfo.mWindowVisibility != View.VISIBLE) {
1484 host.clearAccessibilityFocus();
1485 }
1486
Chet Haaseb78c2842012-04-19 13:39:50 -07001487 // Execute enqueued actions on every traversal in case a detached view enqueued an action
Chris Craikd36a81f2014-07-17 10:16:51 -07001488 getRunQueue().executeActions(mAttachInfo.mHandler);
Chet Haaseb78c2842012-04-19 13:39:50 -07001489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 boolean insetsChanged = false;
Romain Guy8506ab42009-06-11 17:35:47 -07001491
Craig Mautner72d6f212015-02-19 16:33:09 -08001492 boolean layoutRequested = mLayoutRequested && (!mStopped || mReportNextDraw);
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001493 if (layoutRequested) {
Romain Guy15df6702009-08-17 20:17:30 -07001494
Dianne Hackborn711e62a2010-11-29 16:38:22 -08001495 final Resources res = mView.getContext().getResources();
1496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 if (mFirst) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 // make sure touch mode code executes by setting cached value
1499 // to opposite of the added touch mode.
1500 mAttachInfo.mInTouchMode = !mAddedTouchMode;
Romain Guy2d4cff62010-04-09 15:39:00 -07001501 ensureTouchModeLocally(mAddedTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 } else {
Dianne Hackbornc4aad012013-02-22 15:05:25 -08001503 if (!mPendingOverscanInsets.equals(mAttachInfo.mOverscanInsets)) {
1504 insetsChanged = true;
1505 }
Dianne Hackbornfa6b35b2011-08-24 17:03:54 -07001506 if (!mPendingContentInsets.equals(mAttachInfo.mContentInsets)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 insetsChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 }
Adrian Roosfa104232014-06-20 16:10:14 -07001509 if (!mPendingStableInsets.equals(mAttachInfo.mStableInsets)) {
1510 insetsChanged = true;
1511 }
Dianne Hackbornfa6b35b2011-08-24 17:03:54 -07001512 if (!mPendingVisibleInsets.equals(mAttachInfo.mVisibleInsets)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 mAttachInfo.mVisibleInsets.set(mPendingVisibleInsets);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001514 if (DEBUG_LAYOUT) Log.v(mTag, "Visible insets changing to: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 + mAttachInfo.mVisibleInsets);
1516 }
Filip Gruszczynski2217f612015-05-26 11:32:08 -07001517 if (!mPendingOutsets.equals(mAttachInfo.mOutsets)) {
1518 insetsChanged = true;
1519 }
Jorim Jaggi0ffd49c2016-02-12 15:04:21 -08001520 if (mPendingAlwaysConsumeNavBar != mAttachInfo.mAlwaysConsumeNavBar) {
1521 insetsChanged = true;
1522 }
Chong Zhangf6525ce2016-01-14 17:09:56 -08001523 if (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT
1524 || lp.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
Dianne Hackborn711e62a2010-11-29 16:38:22 -08001525 windowSizeMayChange = true;
Chong Zhangf6525ce2016-01-14 17:09:56 -08001526
1527 if (shouldUseDisplaySize(lp)) {
1528 // NOTE -- system code, won't try to do compat mode.
1529 Point size = new Point();
1530 mDisplay.getRealSize(size);
1531 desiredWindowWidth = size.x;
1532 desiredWindowHeight = size.y;
1533 } else {
1534 Configuration config = res.getConfiguration();
1535 desiredWindowWidth = dipToPx(config.screenWidthDp);
1536 desiredWindowHeight = dipToPx(config.screenHeightDp);
1537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 }
1539 }
1540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 // Ask host how big it wants to be
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001542 windowSizeMayChange |= measureHierarchy(host, lp, res,
1543 desiredWindowWidth, desiredWindowHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 }
1545
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001546 if (collectViewAttributes()) {
1547 params = lp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 }
Chris Craikd36a81f2014-07-17 10:16:51 -07001549 if (mAttachInfo.mForceReportNewAttributes) {
1550 mAttachInfo.mForceReportNewAttributes = false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001551 params = lp;
1552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553
Chris Craikd36a81f2014-07-17 10:16:51 -07001554 if (mFirst || mAttachInfo.mViewVisibilityChanged) {
1555 mAttachInfo.mViewVisibilityChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 int resizeMode = mSoftInputMode &
1557 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST;
1558 // If we are in auto resize mode, then we need to determine
1559 // what mode to use now.
1560 if (resizeMode == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED) {
Chris Craikd36a81f2014-07-17 10:16:51 -07001561 final int N = mAttachInfo.mScrollContainers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 for (int i=0; i<N; i++) {
Chris Craikd36a81f2014-07-17 10:16:51 -07001563 if (mAttachInfo.mScrollContainers.get(i).isShown()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 resizeMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
1565 }
1566 }
1567 if (resizeMode == 0) {
1568 resizeMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;
1569 }
1570 if ((lp.softInputMode &
1571 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) != resizeMode) {
1572 lp.softInputMode = (lp.softInputMode &
1573 ~WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) |
1574 resizeMode;
1575 params = lp;
1576 }
1577 }
1578 }
Romain Guy8506ab42009-06-11 17:35:47 -07001579
Dianne Hackbornc4aad012013-02-22 15:05:25 -08001580 if (params != null) {
1581 if ((host.mPrivateFlags & View.PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
1582 if (!PixelFormat.formatHasAlpha(params.format)) {
1583 params.format = PixelFormat.TRANSLUCENT;
1584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 }
Dianne Hackbornc4aad012013-02-22 15:05:25 -08001586 mAttachInfo.mOverscanRequested = (params.flags
1587 & WindowManager.LayoutParams.FLAG_LAYOUT_IN_OVERSCAN) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 }
1589
Adrian Roosfa104232014-06-20 16:10:14 -07001590 if (mApplyInsetsRequested) {
1591 mApplyInsetsRequested = false;
Dianne Hackbornc4aad012013-02-22 15:05:25 -08001592 mLastOverscanRequested = mAttachInfo.mOverscanRequested;
Adam Powell2accbf92014-04-16 23:14:57 +00001593 dispatchApplyInsets(host);
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001594 if (mLayoutRequested) {
1595 // Short-circuit catching a new layout request here, so
1596 // we don't need to go through two layout passes when things
1597 // change due to fitting system windows, which can happen a lot.
1598 windowSizeMayChange |= measureHierarchy(host, lp,
1599 mView.getContext().getResources(),
1600 desiredWindowWidth, desiredWindowHeight);
1601 }
1602 }
1603
1604 if (layoutRequested) {
1605 // Clear this now, so that if anything requests a layout in the
1606 // rest of this function we will catch it and re-run a full
1607 // layout pass.
1608 mLayoutRequested = false;
1609 }
1610
1611 boolean windowShouldResize = layoutRequested && windowSizeMayChange
Dianne Hackborn189ee182010-12-02 21:48:53 -08001612 && ((mWidth != host.getMeasuredWidth() || mHeight != host.getMeasuredHeight())
Romain Guy2e4f4262010-04-06 11:07:52 -07001613 || (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT &&
1614 frame.width() < desiredWindowWidth && frame.width() != mWidth)
1615 || (lp.height == ViewGroup.LayoutParams.WRAP_CONTENT &&
1616 frame.height() < desiredWindowHeight && frame.height() != mHeight));
Jorim Jaggi4846ee32016-01-07 17:39:12 +01001617 windowShouldResize |= mDragResizing && mResizeMode == RESIZE_MODE_FREEFORM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618
Jorim Jaggi4846ee32016-01-07 17:39:12 +01001619 // If the activity was just relaunched, it might have unfrozen the task bounds (while
1620 // relaunching), so we need to force a call into window manager to pick up the latest
1621 // bounds.
1622 windowShouldResize |= mActivityRelaunched;
1623
Jeff Brown2e05ec32013-09-30 15:57:43 -07001624 // Determine whether to compute insets.
1625 // If there are no inset listeners remaining then we may still need to compute
1626 // insets in case the old insets were non-empty and must be reset.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 final boolean computesInternalInsets =
Chris Craikd36a81f2014-07-17 10:16:51 -07001628 mAttachInfo.mTreeObserver.hasComputeInternalInsetsListeners()
1629 || mAttachInfo.mHasNonEmptyGivenInternalInsets;
Romain Guy812ccbe2010-06-01 14:07:24 -07001630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 boolean insetsPending = false;
1632 int relayoutResult = 0;
Romain Guy812ccbe2010-06-01 14:07:24 -07001633
Filip Gruszczynski1a4dfe52015-11-15 10:58:57 -08001634 final boolean isViewVisible = viewVisibility == View.VISIBLE;
Romain Guy812ccbe2010-06-01 14:07:24 -07001635 if (mFirst || windowShouldResize || insetsChanged ||
Jorim Jaggia4a58ef2016-01-27 02:10:08 -08001636 viewVisibilityChanged || params != null || mForceNextWindowRelayout) {
1637 mForceNextWindowRelayout = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638
Alan Viverette64bf97a2015-09-18 16:42:00 -04001639 if (isViewVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 // If this window is giving internal insets to the window
1641 // manager, and it is being added or changing its visibility,
1642 // then we want to first give the window manager "fake"
1643 // insets to cause it to effectively ignore the content of
1644 // the window during layout. This avoids it briefly causing
1645 // other windows to resize/move based on the raw frame of the
1646 // window, waiting until we can finish laying out this window
1647 // and get back to the window manager with the ultimately
1648 // computed insets.
Romain Guy812ccbe2010-06-01 14:07:24 -07001649 insetsPending = computesInternalInsets && (mFirst || viewVisibilityChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 }
1651
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001652 if (mSurfaceHolder != null) {
1653 mSurfaceHolder.mSurfaceLock.lock();
1654 mDrawingAllowed = true;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001655 }
Romain Guy812ccbe2010-06-01 14:07:24 -07001656
Romain Guyc361da82010-10-25 15:29:10 -07001657 boolean hwInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 boolean contentInsetsChanged = false;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001659 boolean hadSurface = mSurface.isValid();
Romain Guy812ccbe2010-06-01 14:07:24 -07001660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 try {
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001662 if (DEBUG_LAYOUT) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001663 Log.i(mTag, "host=w:" + host.getMeasuredWidth() + ", h:" +
Dianne Hackborn189ee182010-12-02 21:48:53 -08001664 host.getMeasuredHeight() + ", params=" + params);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001665 }
Romain Guy2a83f002011-01-18 18:28:21 -08001666
John Reckf7d9c1d2014-04-09 10:01:03 -07001667 if (mAttachInfo.mHardwareRenderer != null) {
1668 // relayoutWindow may decide to destroy mSurface. As that decision
1669 // happens in WindowManager service, we need to be defensive here
1670 // and stop using the surface in case it gets destroyed.
John Reck01a5ea32014-12-03 13:01:07 -08001671 if (mAttachInfo.mHardwareRenderer.pauseSurface(mSurface)) {
1672 // Animations were running so we need to push a frame
1673 // to resume them
1674 mDirty.set(0, 0, mWidth, mHeight);
1675 }
John Reckba6adf62015-02-19 14:36:50 -08001676 mChoreographer.mFrameInfo.addFlags(FrameInfo.FLAG_WINDOW_LAYOUT_CHANGED);
John Reckf7d9c1d2014-04-09 10:01:03 -07001677 }
Romain Guy2a83f002011-01-18 18:28:21 -08001678 final int surfaceGenerationId = mSurface.getGenerationId();
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001679 relayoutResult = relayoutWindow(params, viewVisibility, insetsPending);
1680
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001681 if (DEBUG_LAYOUT) Log.v(mTag, "relayout: frame=" + frame.toShortString()
Dianne Hackbornc4aad012013-02-22 15:05:25 -08001682 + " overscan=" + mPendingOverscanInsets.toShortString()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 + " content=" + mPendingContentInsets.toShortString()
1684 + " visible=" + mPendingVisibleInsets.toShortString()
Adrian Roosfa104232014-06-20 16:10:14 -07001685 + " visible=" + mPendingStableInsets.toShortString()
Filip Gruszczynski0ec13282015-06-25 11:26:01 -07001686 + " outsets=" + mPendingOutsets.toShortString()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 + " surface=" + mSurface);
Romain Guy8506ab42009-06-11 17:35:47 -07001688
Dianne Hackborn694f79b2010-03-17 19:44:59 -07001689 if (mPendingConfiguration.seq != 0) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001690 if (DEBUG_CONFIGURATION) Log.v(mTag, "Visible with new config: "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07001691 + mPendingConfiguration);
riddle_hsu164725c2015-11-12 14:07:12 +08001692 updateConfiguration(new Configuration(mPendingConfiguration), !mFirst);
Dianne Hackborn694f79b2010-03-17 19:44:59 -07001693 mPendingConfiguration.seq = 0;
1694 }
Dianne Hackborn3556c9a2012-05-04 16:31:25 -07001695
Dianne Hackbornc4aad012013-02-22 15:05:25 -08001696 final boolean overscanInsetsChanged = !mPendingOverscanInsets.equals(
1697 mAttachInfo.mOverscanInsets);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 contentInsetsChanged = !mPendingContentInsets.equals(
1699 mAttachInfo.mContentInsets);
Dianne Hackbornc4aad012013-02-22 15:05:25 -08001700 final boolean visibleInsetsChanged = !mPendingVisibleInsets.equals(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 mAttachInfo.mVisibleInsets);
Adrian Roosfa104232014-06-20 16:10:14 -07001702 final boolean stableInsetsChanged = !mPendingStableInsets.equals(
1703 mAttachInfo.mStableInsets);
Filip Gruszczynski2217f612015-05-26 11:32:08 -07001704 final boolean outsetsChanged = !mPendingOutsets.equals(mAttachInfo.mOutsets);
Chong Zhangf4abc2b2015-11-12 23:40:58 -08001705 final boolean surfaceSizeChanged = (relayoutResult
1706 & WindowManagerGlobal.RELAYOUT_RES_SURFACE_RESIZED) != 0;
Jorim Jaggi0ffd49c2016-02-12 15:04:21 -08001707 final boolean alwaysConsumeNavBarChanged =
1708 mPendingAlwaysConsumeNavBar != mAttachInfo.mAlwaysConsumeNavBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 if (contentInsetsChanged) {
1710 mAttachInfo.mContentInsets.set(mPendingContentInsets);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001711 if (DEBUG_LAYOUT) Log.v(mTag, "Content insets changing to: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 + mAttachInfo.mContentInsets);
1713 }
Dianne Hackbornc4aad012013-02-22 15:05:25 -08001714 if (overscanInsetsChanged) {
1715 mAttachInfo.mOverscanInsets.set(mPendingOverscanInsets);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001716 if (DEBUG_LAYOUT) Log.v(mTag, "Overscan insets changing to: "
Dianne Hackbornc4aad012013-02-22 15:05:25 -08001717 + mAttachInfo.mOverscanInsets);
1718 // Need to relayout with content insets.
1719 contentInsetsChanged = true;
1720 }
Adrian Roosfa104232014-06-20 16:10:14 -07001721 if (stableInsetsChanged) {
1722 mAttachInfo.mStableInsets.set(mPendingStableInsets);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001723 if (DEBUG_LAYOUT) Log.v(mTag, "Decor insets changing to: "
Adrian Roosfa104232014-06-20 16:10:14 -07001724 + mAttachInfo.mStableInsets);
1725 // Need to relayout with content insets.
1726 contentInsetsChanged = true;
1727 }
Jorim Jaggi0ffd49c2016-02-12 15:04:21 -08001728 if (alwaysConsumeNavBarChanged) {
1729 mAttachInfo.mAlwaysConsumeNavBar = mPendingAlwaysConsumeNavBar;
1730 contentInsetsChanged = true;
1731 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001732 if (contentInsetsChanged || mLastSystemUiVisibility !=
Adrian Roosfa104232014-06-20 16:10:14 -07001733 mAttachInfo.mSystemUiVisibility || mApplyInsetsRequested
Filip Gruszczynski2217f612015-05-26 11:32:08 -07001734 || mLastOverscanRequested != mAttachInfo.mOverscanRequested
1735 || outsetsChanged) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001736 mLastSystemUiVisibility = mAttachInfo.mSystemUiVisibility;
Dianne Hackbornc4aad012013-02-22 15:05:25 -08001737 mLastOverscanRequested = mAttachInfo.mOverscanRequested;
Filip Gruszczynski2217f612015-05-26 11:32:08 -07001738 mAttachInfo.mOutsets.set(mPendingOutsets);
Adrian Roosfa104232014-06-20 16:10:14 -07001739 mApplyInsetsRequested = false;
Adam Powell2accbf92014-04-16 23:14:57 +00001740 dispatchApplyInsets(host);
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001741 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 if (visibleInsetsChanged) {
1743 mAttachInfo.mVisibleInsets.set(mPendingVisibleInsets);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001744 if (DEBUG_LAYOUT) Log.v(mTag, "Visible insets changing to: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 + mAttachInfo.mVisibleInsets);
1746 }
1747
1748 if (!hadSurface) {
1749 if (mSurface.isValid()) {
1750 // If we are creating a new surface, then we need to
1751 // completely redraw it. Also, when we get to the
1752 // point of drawing it we will hold off and schedule
1753 // a new traversal instead. This is so we can tell the
1754 // window manager about all of the windows being displayed
1755 // before actually drawing them, so it can display then
1756 // all at once.
1757 newSurface = true;
Jeff Brown96e942d2011-11-30 19:55:01 -08001758 mFullRedrawNeeded = true;
Jack Palevich61a6e682009-10-09 17:37:50 -07001759 mPreviousTransparentRegion.setEmpty();
Romain Guy8506ab42009-06-11 17:35:47 -07001760
John Reck63005e62015-05-19 15:00:13 -07001761 // Only initialize up-front if transparent regions are not
1762 // requested, otherwise defer to see if the entire window
1763 // will be transparent
Romain Guyb051e892010-09-28 19:09:36 -07001764 if (mAttachInfo.mHardwareRenderer != null) {
Dianne Hackborn64825172011-03-02 21:32:58 -08001765 try {
Romain Guy786fc932012-07-24 16:24:56 -07001766 hwInitialized = mAttachInfo.mHardwareRenderer.initialize(
John Reck79d81e62013-11-05 13:26:57 -08001767 mSurface);
John Reck63005e62015-05-19 15:00:13 -07001768 if (hwInitialized && (host.mPrivateFlags
1769 & View.PFLAG_REQUEST_TRANSPARENT_REGIONS) == 0) {
1770 // Don't pre-allocate if transparent regions
1771 // are requested as they may not be needed
1772 mSurface.allocateBuffers();
1773 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07001774 } catch (OutOfResourcesException e) {
Romain Guy3696779b2013-01-28 14:04:07 -08001775 handleOutOfResourcesException(e);
Dianne Hackborn64825172011-03-02 21:32:58 -08001776 return;
1777 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 }
1779 }
1780 } else if (!mSurface.isValid()) {
1781 // If the surface has been removed, then reset the scroll
1782 // positions.
Svetoslav Ganov149567f2013-01-08 15:23:34 -08001783 if (mLastScrolledFocus != null) {
1784 mLastScrolledFocus.clear();
1785 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 mScrollY = mCurScrollY = 0;
Adrian Roos0e7ae4e2014-10-01 15:33:22 +02001787 if (mView instanceof RootViewSurfaceTaker) {
1788 ((RootViewSurfaceTaker) mView).onRootViewScrollYChanged(mCurScrollY);
1789 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 if (mScroller != null) {
1791 mScroller.abortAnimation();
1792 }
Romain Guy1d0c7082011-08-03 16:22:24 -07001793 // Our surface is gone
1794 if (mAttachInfo.mHardwareRenderer != null &&
1795 mAttachInfo.mHardwareRenderer.isEnabled()) {
John Reckf47a5942014-06-30 16:20:04 -07001796 mAttachInfo.mHardwareRenderer.destroy();
Romain Guy1d0c7082011-08-03 16:22:24 -07001797 }
Chong Zhangf4abc2b2015-11-12 23:40:58 -08001798 } else if ((surfaceGenerationId != mSurface.getGenerationId()
1799 || surfaceSizeChanged)
1800 && mSurfaceHolder == null
1801 && mAttachInfo.mHardwareRenderer != null) {
Jeff Brown96e942d2011-11-30 19:55:01 -08001802 mFullRedrawNeeded = true;
Dianne Hackborn64825172011-03-02 21:32:58 -08001803 try {
Chong Zhangf4abc2b2015-11-12 23:40:58 -08001804 // Need to do updateSurface (which leads to CanvasContext::setSurface and
1805 // re-create the EGLSurface) if either the Surface changed (as indicated by
1806 // generation id), or WindowManager changed the surface size. The latter is
1807 // because on some chips, changing the consumer side's BufferQueue size may
1808 // not take effect immediately unless we create a new EGLSurface.
1809 // Note that frame size change doesn't always imply surface size change (eg.
1810 // drag resizing uses fullscreen surface), need to check surfaceSizeChanged
1811 // flag from WindowManager.
John Reck79d81e62013-11-05 13:26:57 -08001812 mAttachInfo.mHardwareRenderer.updateSurface(mSurface);
Igor Murashkina86ab6402013-08-30 12:58:36 -07001813 } catch (OutOfResourcesException e) {
Romain Guy3696779b2013-01-28 14:04:07 -08001814 handleOutOfResourcesException(e);
Dianne Hackborn64825172011-03-02 21:32:58 -08001815 return;
1816 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 }
Chong Zhang0275e392015-09-17 10:41:44 -07001818
Jorim Jaggi4846ee32016-01-07 17:39:12 +01001819 final boolean freeformResizing = (relayoutResult
1820 & WindowManagerGlobal.RELAYOUT_RES_DRAG_RESIZING_FREEFORM) != 0;
1821 final boolean dockedResizing = (relayoutResult
1822 & WindowManagerGlobal.RELAYOUT_RES_DRAG_RESIZING_DOCKED) != 0;
1823 final boolean dragResizing = freeformResizing || dockedResizing;
Chong Zhang0275e392015-09-17 10:41:44 -07001824 if (mDragResizing != dragResizing) {
Skuhneb8160872015-09-22 09:51:39 -07001825 if (dragResizing) {
Jorim Jaggi9511b0f2016-01-29 19:12:44 -08001826 startDragResizing(mPendingBackDropFrame,
1827 mWinFrame.equals(mPendingBackDropFrame), mPendingVisibleInsets,
1828 mPendingStableInsets);
Jorim Jaggi4846ee32016-01-07 17:39:12 +01001829 mResizeMode = freeformResizing
1830 ? RESIZE_MODE_FREEFORM
1831 : RESIZE_MODE_DOCKED_DIVIDER;
Skuhneb8160872015-09-22 09:51:39 -07001832 } else {
1833 // We shouldn't come here, but if we come we should end the resize.
1834 endDragResizing();
1835 }
Chong Zhang0275e392015-09-17 10:41:44 -07001836 }
Skuhneb8160872015-09-22 09:51:39 -07001837 if (!USE_MT_RENDERER) {
1838 if (dragResizing) {
1839 mCanvasOffsetX = mWinFrame.left;
1840 mCanvasOffsetY = mWinFrame.top;
1841 } else {
1842 mCanvasOffsetX = mCanvasOffsetY = 0;
1843 }
Chong Zhang0275e392015-09-17 10:41:44 -07001844 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 } catch (RemoteException e) {
1846 }
Romain Guy1d0c7082011-08-03 16:22:24 -07001847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 if (DEBUG_ORIENTATION) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07001849 TAG, "Relayout returned: frame=" + frame + ", surface=" + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850
Chris Craikd36a81f2014-07-17 10:16:51 -07001851 mAttachInfo.mWindowLeft = frame.left;
1852 mAttachInfo.mWindowTop = frame.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853
1854 // !!FIXME!! This next section handles the case where we did not get the
1855 // window size we asked for. We should avoid this by getting a maximum size from
1856 // the window session beforehand.
Dianne Hackborn3556c9a2012-05-04 16:31:25 -07001857 if (mWidth != frame.width() || mHeight != frame.height()) {
Dianne Hackborn3556c9a2012-05-04 16:31:25 -07001858 mWidth = frame.width();
1859 mHeight = frame.height();
1860 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001862 if (mSurfaceHolder != null) {
1863 // The app owns the surface; tell it about what is going on.
1864 if (mSurface.isValid()) {
1865 // XXX .copyFrom() doesn't work!
1866 //mSurfaceHolder.mSurface.copyFrom(mSurface);
1867 mSurfaceHolder.mSurface = mSurface;
1868 }
Jeff Brown30bc34f2011-01-25 12:56:56 -08001869 mSurfaceHolder.setSurfaceFrameSize(mWidth, mHeight);
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001870 mSurfaceHolder.mSurfaceLock.unlock();
1871 if (mSurface.isValid()) {
1872 if (!hadSurface) {
1873 mSurfaceHolder.ungetCallbacks();
1874
1875 mIsCreating = true;
1876 mSurfaceHolderCallback.surfaceCreated(mSurfaceHolder);
1877 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1878 if (callbacks != null) {
1879 for (SurfaceHolder.Callback c : callbacks) {
1880 c.surfaceCreated(mSurfaceHolder);
1881 }
1882 }
1883 surfaceChanged = true;
1884 }
1885 if (surfaceChanged) {
1886 mSurfaceHolderCallback.surfaceChanged(mSurfaceHolder,
1887 lp.format, mWidth, mHeight);
1888 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1889 if (callbacks != null) {
1890 for (SurfaceHolder.Callback c : callbacks) {
1891 c.surfaceChanged(mSurfaceHolder, lp.format,
1892 mWidth, mHeight);
1893 }
1894 }
1895 }
1896 mIsCreating = false;
1897 } else if (hadSurface) {
1898 mSurfaceHolder.ungetCallbacks();
1899 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1900 mSurfaceHolderCallback.surfaceDestroyed(mSurfaceHolder);
1901 if (callbacks != null) {
1902 for (SurfaceHolder.Callback c : callbacks) {
1903 c.surfaceDestroyed(mSurfaceHolder);
1904 }
1905 }
1906 mSurfaceHolder.mSurfaceLock.lock();
Jozef BABJAK93c5b6a2011-02-22 09:33:19 +01001907 try {
1908 mSurfaceHolder.mSurface = new Surface();
1909 } finally {
1910 mSurfaceHolder.mSurfaceLock.unlock();
1911 }
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001912 }
1913 }
Romain Guy53389bd2010-09-07 17:16:32 -07001914
John Reck51aaf902015-12-02 15:08:07 -08001915 final ThreadedRenderer hardwareRenderer = mAttachInfo.mHardwareRenderer;
Alan Viverette50210d92015-05-14 18:05:36 -07001916 if (hardwareRenderer != null && hardwareRenderer.isEnabled()) {
1917 if (hwInitialized
1918 || mWidth != hardwareRenderer.getWidth()
1919 || mHeight != hardwareRenderer.getHeight()) {
1920 hardwareRenderer.setup(mWidth, mHeight, mAttachInfo,
1921 mWindowAttributes.surfaceInsets);
Romain Guy03985752011-07-11 15:33:51 -07001922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 }
1924
Craig Mautner72d6f212015-02-19 16:33:09 -08001925 if (!mStopped || mReportNextDraw) {
Dianne Hackbornce418e62011-03-01 14:31:38 -08001926 boolean focusChangedDueToTouchMode = ensureTouchModeLocally(
Jeff Brown98365d72012-08-19 20:30:52 -07001927 (relayoutResult&WindowManagerGlobal.RELAYOUT_RES_IN_TOUCH_MODE) != 0);
Dianne Hackbornce418e62011-03-01 14:31:38 -08001928 if (focusChangedDueToTouchMode || mWidth != host.getMeasuredWidth()
1929 || mHeight != host.getMeasuredHeight() || contentInsetsChanged) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001930 int childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
1931 int childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
Igor Murashkina86ab6402013-08-30 12:58:36 -07001932
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001933 if (DEBUG_LAYOUT) Log.v(mTag, "Ooops, something changed! mWidth="
Dianne Hackbornce418e62011-03-01 14:31:38 -08001934 + mWidth + " measuredWidth=" + host.getMeasuredWidth()
1935 + " mHeight=" + mHeight
1936 + " measuredHeight=" + host.getMeasuredHeight()
1937 + " coveredInsetsChanged=" + contentInsetsChanged);
Igor Murashkina86ab6402013-08-30 12:58:36 -07001938
Dianne Hackbornce418e62011-03-01 14:31:38 -08001939 // Ask host how big it wants to be
Jeff Brownc8d2668b2012-05-16 17:23:59 -07001940 performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
Igor Murashkina86ab6402013-08-30 12:58:36 -07001941
Dianne Hackbornce418e62011-03-01 14:31:38 -08001942 // Implementation of weights from WindowManager.LayoutParams
1943 // We just grow the dimensions as needed and re-measure if
1944 // needs be
1945 int width = host.getMeasuredWidth();
1946 int height = host.getMeasuredHeight();
1947 boolean measureAgain = false;
Igor Murashkina86ab6402013-08-30 12:58:36 -07001948
Dianne Hackbornce418e62011-03-01 14:31:38 -08001949 if (lp.horizontalWeight > 0.0f) {
1950 width += (int) ((mWidth - width) * lp.horizontalWeight);
1951 childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width,
1952 MeasureSpec.EXACTLY);
1953 measureAgain = true;
1954 }
1955 if (lp.verticalWeight > 0.0f) {
1956 height += (int) ((mHeight - height) * lp.verticalWeight);
1957 childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
1958 MeasureSpec.EXACTLY);
1959 measureAgain = true;
1960 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07001961
Dianne Hackbornce418e62011-03-01 14:31:38 -08001962 if (measureAgain) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001963 if (DEBUG_LAYOUT) Log.v(mTag,
Dianne Hackbornce418e62011-03-01 14:31:38 -08001964 "And hey let's measure once more: width=" + width
1965 + " height=" + height);
Jeff Brownc8d2668b2012-05-16 17:23:59 -07001966 performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
Dianne Hackbornce418e62011-03-01 14:31:38 -08001967 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07001968
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001969 layoutRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 }
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001972 } else {
1973 // Not the first pass and no window/insets/visibility change but the window
1974 // may have moved and we need check that and if so to update the left and right
1975 // in the attach info. We translate only the window frame since on window move
1976 // the window manager tells us only for the new frame but the insets are the
1977 // same and we do not want to translate them more than once.
Jorim Jaggi2e95a482016-01-14 17:36:55 -08001978 maybeHandleWindowMove(frame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 }
1980
Craig Mautner72d6f212015-02-19 16:33:09 -08001981 final boolean didLayout = layoutRequested && (!mStopped || mReportNextDraw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 boolean triggerGlobalLayoutListener = didLayout
Chris Craikd36a81f2014-07-17 10:16:51 -07001983 || mAttachInfo.mRecomputeGlobalAttributes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 if (didLayout) {
Jorim Jaggi26d02d22016-02-24 18:37:51 -05001985 performLayout(lp, mWidth, mHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986
Mathias Agopian54e3d3842013-04-12 15:13:12 -07001987 // By this point all views have been sized and positioned
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 // We can compute the transparent area
1989
Dianne Hackborn4702a852012-08-17 15:18:29 -07001990 if ((host.mPrivateFlags & View.PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 // start out transparent
1992 // TODO: AVOID THAT CALL BY CACHING THE RESULT?
1993 host.getLocationInWindow(mTmpLocation);
1994 mTransparentRegion.set(mTmpLocation[0], mTmpLocation[1],
1995 mTmpLocation[0] + host.mRight - host.mLeft,
1996 mTmpLocation[1] + host.mBottom - host.mTop);
1997
1998 host.gatherTransparentRegion(mTransparentRegion);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001999 if (mTranslator != null) {
2000 mTranslator.translateRegionInWindowToScreen(mTransparentRegion);
2001 }
2002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 if (!mTransparentRegion.equals(mPreviousTransparentRegion)) {
2004 mPreviousTransparentRegion.set(mTransparentRegion);
Mathias Agopian54e3d3842013-04-12 15:13:12 -07002005 mFullRedrawNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 // reconfigure window manager
2007 try {
Jeff Brown98365d72012-08-19 20:30:52 -07002008 mWindowSession.setTransparentRegion(mWindow, mTransparentRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 } catch (RemoteException e) {
2010 }
2011 }
2012 }
2013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 if (DBG) {
2015 System.out.println("======================================");
Chet Haase4610eef2015-12-03 07:38:11 -08002016 System.out.println("performTraversals -- after setFrame");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 host.debug();
2018 }
2019 }
2020
2021 if (triggerGlobalLayoutListener) {
Chris Craikd36a81f2014-07-17 10:16:51 -07002022 mAttachInfo.mRecomputeGlobalAttributes = false;
2023 mAttachInfo.mTreeObserver.dispatchOnGlobalLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 }
2025
2026 if (computesInternalInsets) {
Jeff Brownfbf09772011-01-16 14:06:57 -08002027 // Clear the original insets.
Chris Craikd36a81f2014-07-17 10:16:51 -07002028 final ViewTreeObserver.InternalInsetsInfo insets = mAttachInfo.mGivenInternalInsets;
Jeff Brownfbf09772011-01-16 14:06:57 -08002029 insets.reset();
2030
2031 // Compute new insets in place.
Chris Craikd36a81f2014-07-17 10:16:51 -07002032 mAttachInfo.mTreeObserver.dispatchOnComputeInternalInsets(insets);
2033 mAttachInfo.mHasNonEmptyGivenInternalInsets = !insets.isEmpty();
Jeff Brownfbf09772011-01-16 14:06:57 -08002034
2035 // Tell the window manager.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 if (insetsPending || !mLastGivenInsets.equals(insets)) {
2037 mLastGivenInsets.set(insets);
Jeff Brownfbf09772011-01-16 14:06:57 -08002038
2039 // Translate insets to screen coordinates if needed.
2040 final Rect contentInsets;
2041 final Rect visibleInsets;
2042 final Region touchableRegion;
2043 if (mTranslator != null) {
2044 contentInsets = mTranslator.getTranslatedContentInsets(insets.contentInsets);
2045 visibleInsets = mTranslator.getTranslatedVisibleInsets(insets.visibleInsets);
2046 touchableRegion = mTranslator.getTranslatedTouchableArea(insets.touchableRegion);
2047 } else {
2048 contentInsets = insets.contentInsets;
2049 visibleInsets = insets.visibleInsets;
2050 touchableRegion = insets.touchableRegion;
2051 }
2052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 try {
Jeff Brown98365d72012-08-19 20:30:52 -07002054 mWindowSession.setInsets(mWindow, insets.mTouchableInsets,
Jeff Brownfbf09772011-01-16 14:06:57 -08002055 contentInsets, visibleInsets, touchableRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 } catch (RemoteException e) {
2057 }
2058 }
2059 }
Romain Guy8506ab42009-06-11 17:35:47 -07002060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 if (mFirst) {
2062 // handle first focus request
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002063 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "First: mView.hasFocus()="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 + mView.hasFocus());
2065 if (mView != null) {
2066 if (!mView.hasFocus()) {
2067 mView.requestFocus(View.FOCUS_FORWARD);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002068 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "First: requested focused view="
Svetoslav Ganov149567f2013-01-08 15:23:34 -08002069 + mView.findFocus());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 } else {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002071 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "First: existing focused view="
Svetoslav Ganov149567f2013-01-08 15:23:34 -08002072 + mView.findFocus());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 }
2074 }
2075 }
2076
Alan Viverette64bf97a2015-09-18 16:42:00 -04002077 final boolean changedVisibility = (viewVisibilityChanged || mFirst) && isViewVisible;
2078 final boolean hasWindowFocus = mAttachInfo.mHasWindowFocus && isViewVisible;
2079 final boolean regainedFocus = hasWindowFocus && mLostWindowFocus;
2080 if (regainedFocus) {
2081 mLostWindowFocus = false;
2082 } else if (!hasWindowFocus && mHadWindowFocus) {
2083 mLostWindowFocus = true;
2084 }
2085
2086 if (changedVisibility || regainedFocus) {
2087 host.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2088 }
2089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 mFirst = false;
2091 mWillDrawSoon = false;
2092 mNewSurfaceNeeded = false;
Jorim Jaggi4846ee32016-01-07 17:39:12 +01002093 mActivityRelaunched = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 mViewVisibility = viewVisibility;
Alan Viverette64bf97a2015-09-18 16:42:00 -04002095 mHadWindowFocus = hasWindowFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096
Alan Viverette64bf97a2015-09-18 16:42:00 -04002097 if (hasWindowFocus && !isInLocalFocusMode()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 final boolean imTarget = WindowManager.LayoutParams
2099 .mayUseInputMethod(mWindowAttributes.flags);
2100 if (imTarget != mLastWasImTarget) {
2101 mLastWasImTarget = imTarget;
2102 InputMethodManager imm = InputMethodManager.peekInstance();
2103 if (imm != null && imTarget) {
Alan Viverette64bf97a2015-09-18 16:42:00 -04002104 imm.onPreWindowFocus(mView, hasWindowFocus);
Yohei Yukawa5f059652015-05-14 22:16:41 -07002105 imm.onPostWindowFocus(mView, mView.findFocus(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 mWindowAttributes.softInputMode,
2107 !mHasHadWindowFocus, mWindowAttributes.flags);
2108 }
2109 }
2110 }
Romain Guy8506ab42009-06-11 17:35:47 -07002111
Jeff Brown96e942d2011-11-30 19:55:01 -08002112 // Remember if we must report the next draw.
Jeff Brown98365d72012-08-19 20:30:52 -07002113 if ((relayoutResult & WindowManagerGlobal.RELAYOUT_RES_FIRST_TIME) != 0) {
Jeff Brown96e942d2011-11-30 19:55:01 -08002114 mReportNextDraw = true;
2115 }
2116
Alan Viverette64bf97a2015-09-18 16:42:00 -04002117 boolean cancelDraw = mAttachInfo.mTreeObserver.dispatchOnPreDraw() || !isViewVisible;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118
Jorim Jaggi3127c2a42016-01-26 18:48:44 -08002119 if (!cancelDraw) {
Chet Haase9c450412015-10-01 13:25:58 -07002120 if (mPendingTransitions != null && mPendingTransitions.size() > 0) {
2121 for (int i = 0; i < mPendingTransitions.size(); ++i) {
2122 mPendingTransitions.get(i).startChangingAnimations();
Chet Haased56c6952011-09-07 08:46:23 -07002123 }
Chet Haase9c450412015-10-01 13:25:58 -07002124 mPendingTransitions.clear();
Chet Haased56c6952011-09-07 08:46:23 -07002125 }
Chet Haase9c450412015-10-01 13:25:58 -07002126
2127 performDraw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 } else {
Alan Viverette64bf97a2015-09-18 16:42:00 -04002129 if (isViewVisible) {
Chris Wren78cb7cf2012-05-15 12:36:44 -04002130 // Try again
2131 scheduleTraversals();
2132 } else if (mPendingTransitions != null && mPendingTransitions.size() > 0) {
Chet Haased56c6952011-09-07 08:46:23 -07002133 for (int i = 0; i < mPendingTransitions.size(); ++i) {
2134 mPendingTransitions.get(i).endChangingAnimations();
2135 }
2136 mPendingTransitions.clear();
2137 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 }
Craig Mautnerdf2390a2012-08-29 20:59:22 -07002139
2140 mIsInTraversal = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 }
2142
Jorim Jaggi2e95a482016-01-14 17:36:55 -08002143 private void maybeHandleWindowMove(Rect frame) {
2144
2145 // TODO: Well, we are checking whether the frame has changed similarly
2146 // to how this is done for the insets. This is however incorrect since
2147 // the insets and the frame are translated. For example, the old frame
2148 // was (1, 1 - 1, 1) and was translated to say (2, 2 - 2, 2), now the new
2149 // reported frame is (2, 2 - 2, 2) which implies no change but this is not
2150 // true since we are comparing a not translated value to a translated one.
2151 // This scenario is rare but we may want to fix that.
2152
2153 final boolean windowMoved = mAttachInfo.mWindowLeft != frame.left
2154 || mAttachInfo.mWindowTop != frame.top;
2155 if (windowMoved) {
2156 if (mTranslator != null) {
2157 mTranslator.translateRectInScreenToAppWinFrame(frame);
2158 }
2159 mAttachInfo.mWindowLeft = frame.left;
2160 mAttachInfo.mWindowTop = frame.top;
2161
2162 // Update the light position for the new window offsets.
2163 if (mAttachInfo.mHardwareRenderer != null) {
2164 mAttachInfo.mHardwareRenderer.setLightCenter(mAttachInfo);
2165 }
2166 }
2167 }
Jorim Jaggi3127c2a42016-01-26 18:48:44 -08002168
Romain Guy3696779b2013-01-28 14:04:07 -08002169 private void handleOutOfResourcesException(Surface.OutOfResourcesException e) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002170 Log.e(mTag, "OutOfResourcesException initializing HW surface", e);
Romain Guy3696779b2013-01-28 14:04:07 -08002171 try {
2172 if (!mWindowSession.outOfMemory(mWindow) &&
2173 Process.myUid() != Process.SYSTEM_UID) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002174 Slog.w(mTag, "No processes killed for memory; killing self");
Romain Guy3696779b2013-01-28 14:04:07 -08002175 Process.killProcess(Process.myPid());
2176 }
2177 } catch (RemoteException ex) {
2178 }
2179 mLayoutRequested = true; // ask wm for a new surface next time.
2180 }
2181
Jeff Brownc8d2668b2012-05-16 17:23:59 -07002182 private void performMeasure(int childWidthMeasureSpec, int childHeightMeasureSpec) {
2183 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "measure");
2184 try {
2185 mView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
2186 } finally {
2187 Trace.traceEnd(Trace.TRACE_TAG_VIEW);
2188 }
2189 }
2190
Chet Haase97140572012-09-13 14:56:47 -07002191 /**
2192 * Called by {@link android.view.View#isInLayout()} to determine whether the view hierarchy
2193 * is currently undergoing a layout pass.
2194 *
2195 * @return whether the view hierarchy is currently undergoing a layout pass
2196 */
2197 boolean isInLayout() {
2198 return mInLayout;
2199 }
2200
2201 /**
Chet Haasecc699b42012-12-13 09:06:55 -08002202 * Called by {@link android.view.View#requestLayout()} if the view hierarchy is currently
2203 * undergoing a layout pass. requestLayout() should not generally be called during layout,
2204 * unless the container hierarchy knows what it is doing (i.e., it is fine as long as
2205 * all children in that container hierarchy are measured and laid out at the end of the layout
2206 * pass for that container). If requestLayout() is called anyway, we handle it correctly
2207 * by registering all requesters during a frame as it proceeds. At the end of the frame,
2208 * we check all of those views to see if any still have pending layout requests, which
2209 * indicates that they were not correctly handled by their container hierarchy. If that is
2210 * the case, we clear all such flags in the tree, to remove the buggy flag state that leads
2211 * to blank containers, and force a second request/measure/layout pass in this frame. If
Chet Haase97140572012-09-13 14:56:47 -07002212 * more requestLayout() calls are received during that second layout pass, we post those
Chet Haasecc699b42012-12-13 09:06:55 -08002213 * requests to the next frame to avoid possible infinite loops.
2214 *
2215 * <p>The return value from this method indicates whether the request should proceed
2216 * (if it is a request during the first layout pass) or should be skipped and posted to the
2217 * next frame (if it is a request during the second layout pass).</p>
Chet Haase97140572012-09-13 14:56:47 -07002218 *
2219 * @param view the view that requested the layout.
Chet Haasecc699b42012-12-13 09:06:55 -08002220 *
2221 * @return true if request should proceed, false otherwise.
Chet Haase97140572012-09-13 14:56:47 -07002222 */
Chet Haasecc699b42012-12-13 09:06:55 -08002223 boolean requestLayoutDuringLayout(final View view) {
2224 if (view.mParent == null || view.mAttachInfo == null) {
2225 // Would not normally trigger another layout, so just let it pass through as usual
2226 return true;
2227 }
Chet Haase107a4822013-03-13 06:46:50 -07002228 if (!mLayoutRequesters.contains(view)) {
2229 mLayoutRequesters.add(view);
2230 }
Chet Haase97140572012-09-13 14:56:47 -07002231 if (!mHandlingLayoutInLayoutRequest) {
Chet Haase107a4822013-03-13 06:46:50 -07002232 // Let the request proceed normally; it will be processed in a second layout pass
2233 // if necessary
Chet Haasecc699b42012-12-13 09:06:55 -08002234 return true;
Chet Haase97140572012-09-13 14:56:47 -07002235 } else {
Chet Haase107a4822013-03-13 06:46:50 -07002236 // Don't let the request proceed during the second layout pass.
2237 // It will post to the next frame instead.
Chet Haasecc699b42012-12-13 09:06:55 -08002238 return false;
Chet Haase97140572012-09-13 14:56:47 -07002239 }
2240 }
2241
Chet Haase3efa7b52012-12-03 08:33:17 -08002242 private void performLayout(WindowManager.LayoutParams lp, int desiredWindowWidth,
2243 int desiredWindowHeight) {
Jeff Brownc8d2668b2012-05-16 17:23:59 -07002244 mLayoutRequested = false;
2245 mScrollMayChange = true;
Chet Haase97140572012-09-13 14:56:47 -07002246 mInLayout = true;
Jeff Brownc8d2668b2012-05-16 17:23:59 -07002247
2248 final View host = mView;
2249 if (DEBUG_ORIENTATION || DEBUG_LAYOUT) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002250 Log.v(mTag, "Laying out " + host + " to (" +
Jeff Brownc8d2668b2012-05-16 17:23:59 -07002251 host.getMeasuredWidth() + ", " + host.getMeasuredHeight() + ")");
2252 }
2253
Jeff Brownc8d2668b2012-05-16 17:23:59 -07002254 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "layout");
2255 try {
2256 host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());
Chet Haasecc699b42012-12-13 09:06:55 -08002257
Chet Haased5a83522012-11-21 16:24:44 -08002258 mInLayout = false;
Chet Haase97140572012-09-13 14:56:47 -07002259 int numViewsRequestingLayout = mLayoutRequesters.size();
2260 if (numViewsRequestingLayout > 0) {
Chet Haasecc699b42012-12-13 09:06:55 -08002261 // requestLayout() was called during layout.
2262 // If no layout-request flags are set on the requesting views, there is no problem.
2263 // If some requests are still pending, then we need to clear those flags and do
2264 // a full request/measure/layout pass to handle this situation.
Chet Haase107a4822013-03-13 06:46:50 -07002265 ArrayList<View> validLayoutRequesters = getValidLayoutRequesters(mLayoutRequesters,
2266 false);
2267 if (validLayoutRequesters != null) {
2268 // Set this flag to indicate that any further requests are happening during
2269 // the second pass, which may result in posting those requests to the next
2270 // frame instead
Chet Haasecc699b42012-12-13 09:06:55 -08002271 mHandlingLayoutInLayoutRequest = true;
Chet Haase107a4822013-03-13 06:46:50 -07002272
2273 // Process fresh layout requests, then measure and layout
2274 int numValidRequests = validLayoutRequesters.size();
Chet Haasecc699b42012-12-13 09:06:55 -08002275 for (int i = 0; i < numValidRequests; ++i) {
Chet Haase107a4822013-03-13 06:46:50 -07002276 final View view = validLayoutRequesters.get(i);
2277 Log.w("View", "requestLayout() improperly called by " + view +
2278 " during layout: running second layout pass");
2279 view.requestLayout();
Chet Haasecc699b42012-12-13 09:06:55 -08002280 }
2281 measureHierarchy(host, lp, mView.getContext().getResources(),
2282 desiredWindowWidth, desiredWindowHeight);
2283 mInLayout = true;
2284 host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());
Chet Haase107a4822013-03-13 06:46:50 -07002285
Chet Haasecc699b42012-12-13 09:06:55 -08002286 mHandlingLayoutInLayoutRequest = false;
Chet Haase107a4822013-03-13 06:46:50 -07002287
2288 // Check the valid requests again, this time without checking/clearing the
2289 // layout flags, since requests happening during the second pass get noop'd
2290 validLayoutRequesters = getValidLayoutRequesters(mLayoutRequesters, true);
2291 if (validLayoutRequesters != null) {
2292 final ArrayList<View> finalRequesters = validLayoutRequesters;
2293 // Post second-pass requests to the next frame
2294 getRunQueue().post(new Runnable() {
2295 @Override
2296 public void run() {
2297 int numValidRequests = finalRequesters.size();
2298 for (int i = 0; i < numValidRequests; ++i) {
2299 final View view = finalRequesters.get(i);
2300 Log.w("View", "requestLayout() improperly called by " + view +
2301 " during second layout pass: posting in next frame");
2302 view.requestLayout();
2303 }
2304 }
2305 });
2306 }
Chet Haasecc699b42012-12-13 09:06:55 -08002307 }
Chet Haase107a4822013-03-13 06:46:50 -07002308
Chet Haase97140572012-09-13 14:56:47 -07002309 }
Jeff Brownc8d2668b2012-05-16 17:23:59 -07002310 } finally {
2311 Trace.traceEnd(Trace.TRACE_TAG_VIEW);
2312 }
Chet Haase97140572012-09-13 14:56:47 -07002313 mInLayout = false;
Jeff Brownc8d2668b2012-05-16 17:23:59 -07002314 }
2315
Chet Haase107a4822013-03-13 06:46:50 -07002316 /**
2317 * This method is called during layout when there have been calls to requestLayout() during
2318 * layout. It walks through the list of views that requested layout to determine which ones
2319 * still need it, based on visibility in the hierarchy and whether they have already been
2320 * handled (as is usually the case with ListView children).
2321 *
2322 * @param layoutRequesters The list of views that requested layout during layout
2323 * @param secondLayoutRequests Whether the requests were issued during the second layout pass.
2324 * If so, the FORCE_LAYOUT flag was not set on requesters.
2325 * @return A list of the actual views that still need to be laid out.
2326 */
2327 private ArrayList<View> getValidLayoutRequesters(ArrayList<View> layoutRequesters,
2328 boolean secondLayoutRequests) {
2329
2330 int numViewsRequestingLayout = layoutRequesters.size();
2331 ArrayList<View> validLayoutRequesters = null;
2332 for (int i = 0; i < numViewsRequestingLayout; ++i) {
2333 View view = layoutRequesters.get(i);
2334 if (view != null && view.mAttachInfo != null && view.mParent != null &&
2335 (secondLayoutRequests || (view.mPrivateFlags & View.PFLAG_FORCE_LAYOUT) ==
2336 View.PFLAG_FORCE_LAYOUT)) {
2337 boolean gone = false;
2338 View parent = view;
2339 // Only trigger new requests for views in a non-GONE hierarchy
2340 while (parent != null) {
2341 if ((parent.mViewFlags & View.VISIBILITY_MASK) == View.GONE) {
2342 gone = true;
2343 break;
2344 }
2345 if (parent.mParent instanceof View) {
2346 parent = (View) parent.mParent;
2347 } else {
2348 parent = null;
2349 }
2350 }
2351 if (!gone) {
2352 if (validLayoutRequesters == null) {
2353 validLayoutRequesters = new ArrayList<View>();
2354 }
2355 validLayoutRequesters.add(view);
2356 }
2357 }
2358 }
2359 if (!secondLayoutRequests) {
2360 // If we're checking the layout flags, then we need to clean them up also
2361 for (int i = 0; i < numViewsRequestingLayout; ++i) {
2362 View view = layoutRequesters.get(i);
2363 while (view != null &&
2364 (view.mPrivateFlags & View.PFLAG_FORCE_LAYOUT) != 0) {
2365 view.mPrivateFlags &= ~View.PFLAG_FORCE_LAYOUT;
2366 if (view.mParent instanceof View) {
2367 view = (View) view.mParent;
2368 } else {
2369 view = null;
2370 }
2371 }
2372 }
2373 }
2374 layoutRequesters.clear();
2375 return validLayoutRequesters;
2376 }
2377
Igor Murashkina86ab6402013-08-30 12:58:36 -07002378 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 public void requestTransparentRegion(View child) {
2380 // the test below should not fail unless someone is messing with us
2381 checkThread();
2382 if (mView == child) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07002383 mView.mPrivateFlags |= View.PFLAG_REQUEST_TRANSPARENT_REGIONS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 // Need to make sure we re-evaluate the window attributes next
2385 // time around, to ensure the window has the correct format.
2386 mWindowAttributesChanged = true;
Romain Guyf21c9b02011-09-06 16:56:54 -07002387 mWindowAttributesChangesFlag = 0;
Mathias Agopian1bd80ad2010-11-04 17:13:39 -07002388 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 }
2390 }
2391
2392 /**
2393 * Figures out the measure spec for the root view in a window based on it's
2394 * layout params.
2395 *
2396 * @param windowSize
2397 * The available width or height of the window
2398 *
2399 * @param rootDimension
2400 * The layout params for one dimension (width or height) of the
2401 * window.
2402 *
2403 * @return The measure spec to use to measure the root view.
2404 */
Romain Guya998dff2012-03-23 18:58:36 -07002405 private static int getRootMeasureSpec(int windowSize, int rootDimension) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 int measureSpec;
2407 switch (rootDimension) {
2408
Romain Guy980a9382010-01-08 15:06:28 -08002409 case ViewGroup.LayoutParams.MATCH_PARENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 // Window can't resize. Force root view to be windowSize.
2411 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY);
2412 break;
2413 case ViewGroup.LayoutParams.WRAP_CONTENT:
2414 // Window can resize. Set max size for root view.
2415 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST);
2416 break;
2417 default:
2418 // Window wants to be an exact size. Force root view to be that size.
2419 measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);
2420 break;
2421 }
2422 return measureSpec;
2423 }
2424
Alan Viveretteccb11e12014-07-08 16:04:02 -07002425 int mHardwareXOffset;
Dianne Hackborn0f761d62010-11-30 22:06:10 -08002426 int mHardwareYOffset;
Dianne Hackborn0f761d62010-11-30 22:06:10 -08002427
Igor Murashkina86ab6402013-08-30 12:58:36 -07002428 @Override
Chris Craikf6829a02015-03-10 10:28:59 -07002429 public void onHardwarePreDraw(DisplayListCanvas canvas) {
Alan Viveretteccb11e12014-07-08 16:04:02 -07002430 canvas.translate(-mHardwareXOffset, -mHardwareYOffset);
Dianne Hackborn0f761d62010-11-30 22:06:10 -08002431 }
2432
Igor Murashkina86ab6402013-08-30 12:58:36 -07002433 @Override
Chris Craikf6829a02015-03-10 10:28:59 -07002434 public void onHardwarePostDraw(DisplayListCanvas canvas) {
Alan Viverette632af842014-10-28 13:45:11 -07002435 drawAccessibilityFocusedDrawableIfNeeded(canvas);
Dianne Hackborn0f761d62010-11-30 22:06:10 -08002436 }
2437
Chet Haaseed30fd82011-04-22 16:18:45 -07002438 /**
2439 * @hide
2440 */
2441 void outputDisplayList(View view) {
Chris Craik356b5fe2015-07-07 10:39:36 -07002442 view.mRenderNode.output();
John Recke248bd12015-08-05 13:53:53 -07002443 if (mAttachInfo.mHardwareRenderer != null) {
2444 ((ThreadedRenderer)mAttachInfo.mHardwareRenderer).serializeDisplayListTree();
2445 }
Romain Guy59a12ca2011-06-09 17:48:21 -07002446 }
2447
2448 /**
2449 * @see #PROPERTY_PROFILE_RENDERING
2450 */
2451 private void profileRendering(boolean enabled) {
2452 if (mProfileRendering) {
2453 mRenderProfilingEnabled = enabled;
Chris Craikae4f32042013-02-07 12:57:10 -08002454
2455 if (mRenderProfiler != null) {
2456 mChoreographer.removeFrameCallback(mRenderProfiler);
2457 }
2458 if (mRenderProfilingEnabled) {
2459 if (mRenderProfiler == null) {
2460 mRenderProfiler = new Choreographer.FrameCallback() {
2461 @Override
2462 public void doFrame(long frameTimeNanos) {
2463 mDirty.set(0, 0, mWidth, mHeight);
2464 scheduleTraversals();
2465 if (mRenderProfilingEnabled) {
2466 mChoreographer.postFrameCallback(mRenderProfiler);
2467 }
Romain Guy59a12ca2011-06-09 17:48:21 -07002468 }
Chris Craikae4f32042013-02-07 12:57:10 -08002469 };
2470 }
Romain Guy5bb3c732012-11-29 17:52:58 -08002471 mChoreographer.postFrameCallback(mRenderProfiler);
Romain Guy59a12ca2011-06-09 17:48:21 -07002472 } else {
Romain Guy59a12ca2011-06-09 17:48:21 -07002473 mRenderProfiler = null;
Chet Haaseed30fd82011-04-22 16:18:45 -07002474 }
2475 }
2476 }
2477
Chet Haase2f2022a2011-10-11 06:41:59 -07002478 /**
2479 * Called from draw() when DEBUG_FPS is enabled
2480 */
2481 private void trackFPS() {
2482 // Tracks frames per second drawn. First value in a series of draws may be bogus
2483 // because it down not account for the intervening idle time
2484 long nowTime = System.currentTimeMillis();
2485 if (mFpsStartTime < 0) {
2486 mFpsStartTime = mFpsPrevTime = nowTime;
2487 mFpsNumFrames = 0;
2488 } else {
2489 ++mFpsNumFrames;
2490 String thisHash = Integer.toHexString(System.identityHashCode(this));
2491 long frameTime = nowTime - mFpsPrevTime;
2492 long totalTime = nowTime - mFpsStartTime;
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002493 Log.v(mTag, "0x" + thisHash + "\tFrame time:\t" + frameTime);
Chet Haase2f2022a2011-10-11 06:41:59 -07002494 mFpsPrevTime = nowTime;
2495 if (totalTime > 1000) {
2496 float fps = (float) mFpsNumFrames * 1000 / totalTime;
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002497 Log.v(mTag, "0x" + thisHash + "\tFPS:\t" + fps);
Chet Haase2f2022a2011-10-11 06:41:59 -07002498 mFpsStartTime = nowTime;
2499 mFpsNumFrames = 0;
2500 }
2501 }
2502 }
2503
Jeff Brown96e942d2011-11-30 19:55:01 -08002504 private void performDraw() {
Jeff Brownd912e1f2014-04-11 18:46:22 -07002505 if (mAttachInfo.mDisplayState == Display.STATE_OFF && !mReportNextDraw) {
Craig Mautner006f0e42012-03-21 11:00:32 -07002506 return;
2507 }
Romain Guy7e4e5612012-03-05 14:37:29 -08002508
Jeff Brown96e942d2011-11-30 19:55:01 -08002509 final boolean fullRedrawNeeded = mFullRedrawNeeded;
2510 mFullRedrawNeeded = false;
Jeff Brown481c1572012-03-09 14:41:15 -08002511
Romain Guy1f59e5c2012-05-06 14:11:16 -07002512 mIsDrawing = true;
Jeff Brown481c1572012-03-09 14:41:15 -08002513 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "draw");
2514 try {
2515 draw(fullRedrawNeeded);
2516 } finally {
Romain Guy1f59e5c2012-05-06 14:11:16 -07002517 mIsDrawing = false;
Jeff Brown481c1572012-03-09 14:41:15 -08002518 Trace.traceEnd(Trace.TRACE_TAG_VIEW);
2519 }
Jeff Brown96e942d2011-11-30 19:55:01 -08002520
John Reck119907c2014-08-14 09:02:01 -07002521 // For whatever reason we didn't create a HardwareRenderer, end any
2522 // hardware animations that are now dangling
2523 if (mAttachInfo.mPendingAnimatingRenderNodes != null) {
2524 final int count = mAttachInfo.mPendingAnimatingRenderNodes.size();
2525 for (int i = 0; i < count; i++) {
2526 mAttachInfo.mPendingAnimatingRenderNodes.get(i).endAllAnimators();
2527 }
2528 mAttachInfo.mPendingAnimatingRenderNodes.clear();
2529 }
2530
Jeff Brown96e942d2011-11-30 19:55:01 -08002531 if (mReportNextDraw) {
2532 mReportNextDraw = false;
Chong Zhang8dbd9ad2015-10-09 10:06:11 -07002533
2534 // if we're using multi-thread renderer, wait for the window frame draws
2535 if (mWindowDrawCountDown != null) {
2536 try {
2537 mWindowDrawCountDown.await();
2538 } catch (InterruptedException e) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002539 Log.e(mTag, "Window redraw count down interruped!");
Chong Zhang8dbd9ad2015-10-09 10:06:11 -07002540 }
2541 mWindowDrawCountDown = null;
2542 }
2543
John Reck28ad7b52014-04-07 16:59:25 -07002544 if (mAttachInfo.mHardwareRenderer != null) {
2545 mAttachInfo.mHardwareRenderer.fence();
2546 }
Jeff Brown96e942d2011-11-30 19:55:01 -08002547
2548 if (LOCAL_LOGV) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002549 Log.v(mTag, "FINISHED DRAWING: " + mWindowAttributes.getTitle());
Jeff Brown96e942d2011-11-30 19:55:01 -08002550 }
2551 if (mSurfaceHolder != null && mSurface.isValid()) {
2552 mSurfaceHolderCallback.surfaceRedrawNeeded(mSurfaceHolder);
2553 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
2554 if (callbacks != null) {
2555 for (SurfaceHolder.Callback c : callbacks) {
2556 if (c instanceof SurfaceHolder.Callback2) {
Filip Gruszczynski1a4dfe52015-11-15 10:58:57 -08002557 ((SurfaceHolder.Callback2)c).surfaceRedrawNeeded(mSurfaceHolder);
Jeff Brown96e942d2011-11-30 19:55:01 -08002558 }
2559 }
2560 }
2561 }
2562 try {
Jeff Brown98365d72012-08-19 20:30:52 -07002563 mWindowSession.finishDrawing(mWindow);
Jeff Brown96e942d2011-11-30 19:55:01 -08002564 } catch (RemoteException e) {
2565 }
2566 }
2567 }
2568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 private void draw(boolean fullRedrawNeeded) {
2570 Surface surface = mSurface;
Romain Guyfbb93fa2012-12-03 18:50:35 -08002571 if (!surface.isValid()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 return;
2573 }
2574
Chet Haase2f2022a2011-10-11 06:41:59 -07002575 if (DEBUG_FPS) {
2576 trackFPS();
2577 }
2578
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002579 if (!sFirstDrawComplete) {
2580 synchronized (sFirstDrawHandlers) {
2581 sFirstDrawComplete = true;
Romain Guy812ccbe2010-06-01 14:07:24 -07002582 final int count = sFirstDrawHandlers.size();
2583 for (int i = 0; i< count; i++) {
Jeff Browna175a5b2012-02-15 19:18:31 -08002584 mHandler.post(sFirstDrawHandlers.get(i));
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002585 }
2586 }
2587 }
Romain Guy59a12ca2011-06-09 17:48:21 -07002588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 scrollToRectOrFocus(null, false);
2590
Chris Craikd36a81f2014-07-17 10:16:51 -07002591 if (mAttachInfo.mViewScrollChanged) {
2592 mAttachInfo.mViewScrollChanged = false;
2593 mAttachInfo.mTreeObserver.dispatchOnScrollChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 }
Romain Guy8506ab42009-06-11 17:35:47 -07002595
Dianne Hackborn0f761d62010-11-30 22:06:10 -08002596 boolean animating = mScroller != null && mScroller.computeScrollOffset();
Alan Viveretteccb11e12014-07-08 16:04:02 -07002597 final int curScrollY;
Dianne Hackborn0f761d62010-11-30 22:06:10 -08002598 if (animating) {
Alan Viveretteccb11e12014-07-08 16:04:02 -07002599 curScrollY = mScroller.getCurrY();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 } else {
Alan Viveretteccb11e12014-07-08 16:04:02 -07002601 curScrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 }
Alan Viveretteccb11e12014-07-08 16:04:02 -07002603 if (mCurScrollY != curScrollY) {
2604 mCurScrollY = curScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605 fullRedrawNeeded = true;
Adrian Roos0e7ae4e2014-10-01 15:33:22 +02002606 if (mView instanceof RootViewSurfaceTaker) {
2607 ((RootViewSurfaceTaker) mView).onRootViewScrollYChanged(mCurScrollY);
2608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 }
Jeff Brown96e942d2011-11-30 19:55:01 -08002610
Chris Craikd36a81f2014-07-17 10:16:51 -07002611 final float appScale = mAttachInfo.mApplicationScale;
2612 final boolean scalingRequired = mAttachInfo.mScalingRequired;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613
Dianne Hackborn0f761d62010-11-30 22:06:10 -08002614 int resizeAlpha = 0;
Dianne Hackborn0f761d62010-11-30 22:06:10 -08002615
Jeff Brown96e942d2011-11-30 19:55:01 -08002616 final Rect dirty = mDirty;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07002617 if (mSurfaceHolder != null) {
2618 // The app owns the surface, we won't draw.
2619 dirty.setEmpty();
Derek Sollenberger8d948352015-07-16 09:27:59 -04002620 if (animating && mScroller != null) {
2621 mScroller.abortAnimation();
Dianne Hackborn0f761d62010-11-30 22:06:10 -08002622 }
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07002623 return;
2624 }
Romain Guy58ef7fb2010-09-13 12:52:37 -07002625
2626 if (fullRedrawNeeded) {
Chris Craikd36a81f2014-07-17 10:16:51 -07002627 mAttachInfo.mIgnoreDirtyState = true;
Romain Guyc3166e12011-08-08 15:00:03 -07002628 dirty.set(0, 0, (int) (mWidth * appScale + 0.5f), (int) (mHeight * appScale + 0.5f));
Romain Guy58ef7fb2010-09-13 12:52:37 -07002629 }
Chet Haasead4f7032011-06-22 09:18:31 -07002630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002632 Log.v(mTag, "Draw " + mView + "/"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002633 + mWindowAttributes.getTitle()
2634 + ": dirty={" + dirty.left + "," + dirty.top
2635 + "," + dirty.right + "," + dirty.bottom + "} surface="
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002636 + surface + " surface.isValid()=" + surface.isValid() + ", appScale:" +
2637 appScale + ", width=" + mWidth + ", height=" + mHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 }
2639
Chris Craikd36a81f2014-07-17 10:16:51 -07002640 mAttachInfo.mTreeObserver.dispatchOnDraw();
Romain Guy25eba5c2012-04-04 17:29:03 -07002641
Chong Zhang0275e392015-09-17 10:41:44 -07002642 int xOffset = -mCanvasOffsetX;
2643 int yOffset = -mCanvasOffsetY + curScrollY;
Alan Viverettea51cab92014-07-16 15:15:49 -07002644 final WindowManager.LayoutParams params = mWindowAttributes;
2645 final Rect surfaceInsets = params != null ? params.surfaceInsets : null;
2646 if (surfaceInsets != null) {
2647 xOffset -= surfaceInsets.left;
2648 yOffset -= surfaceInsets.top;
2649
2650 // Offset dirty rect for surface insets.
2651 dirty.offset(surfaceInsets.left, surfaceInsets.right);
2652 }
2653
Alan Viverette632af842014-10-28 13:45:11 -07002654 boolean accessibilityFocusDirty = false;
2655 final Drawable drawable = mAttachInfo.mAccessibilityFocusDrawable;
2656 if (drawable != null) {
2657 final Rect bounds = mAttachInfo.mTmpInvalRect;
2658 final boolean hasFocus = getAccessibilityFocusedRect(bounds);
2659 if (!hasFocus) {
2660 bounds.setEmpty();
2661 }
2662 if (!bounds.equals(drawable.getBounds())) {
2663 accessibilityFocusDirty = true;
2664 }
2665 }
2666
John Reckba6adf62015-02-19 14:36:50 -08002667 mAttachInfo.mDrawingTime =
2668 mChoreographer.getFrameTimeNanos() / TimeUtils.NANOS_PER_MS;
2669
Alan Viverette632af842014-10-28 13:45:11 -07002670 if (!dirty.isEmpty() || mIsAnimating || accessibilityFocusDirty) {
Chris Craikd36a81f2014-07-17 10:16:51 -07002671 if (mAttachInfo.mHardwareRenderer != null && mAttachInfo.mHardwareRenderer.isEnabled()) {
Alan Viverette632af842014-10-28 13:45:11 -07002672 // If accessibility focus moved, always invalidate the root.
2673 boolean invalidateRoot = accessibilityFocusDirty;
2674
Jeff Brown96e942d2011-11-30 19:55:01 -08002675 // Draw with hardware renderer.
2676 mIsAnimating = false;
Alan Viverette632af842014-10-28 13:45:11 -07002677
John Reck0a973302014-07-16 13:29:45 -07002678 if (mHardwareYOffset != yOffset || mHardwareXOffset != xOffset) {
2679 mHardwareYOffset = yOffset;
2680 mHardwareXOffset = xOffset;
Alan Viverette632af842014-10-28 13:45:11 -07002681 invalidateRoot = true;
Alan Viverettef6cf1a02014-08-11 14:13:02 -07002682 }
2683
Alan Viverette632af842014-10-28 13:45:11 -07002684 if (invalidateRoot) {
2685 mAttachInfo.mHardwareRenderer.invalidateRoot();
2686 }
2687
Jeff Brown96e942d2011-11-30 19:55:01 -08002688 dirty.setEmpty();
2689
Skuhne980ee472015-10-06 11:31:31 -07002690 // Stage the content drawn size now. It will be transferred to the renderer
2691 // shortly before the draw commands get send to the renderer.
Chong Zhang8dbd9ad2015-10-09 10:06:11 -07002692 final boolean updated = updateContentDrawBounds();
2693
John Reck61375a82014-09-18 19:27:48 +00002694 mAttachInfo.mHardwareRenderer.draw(mView, mAttachInfo, this);
Chong Zhang8dbd9ad2015-10-09 10:06:11 -07002695
2696 if (updated) {
2697 requestDrawWindow();
2698 }
Romain Guy3696779b2013-01-28 14:04:07 -08002699 } else {
2700 // If we get here with a disabled & requested hardware renderer, something went
2701 // wrong (an invalidate posted right before we destroyed the hardware surface
2702 // for instance) so we should just bail out. Locking the surface with software
2703 // rendering at this point would lock it forever and prevent hardware renderer
2704 // from doing its job when it comes back.
2705 // Before we request a new frame we must however attempt to reinitiliaze the
2706 // hardware renderer if it's in requested state. This would happen after an
2707 // eglTerminate() for instance.
Chris Craikd36a81f2014-07-17 10:16:51 -07002708 if (mAttachInfo.mHardwareRenderer != null &&
2709 !mAttachInfo.mHardwareRenderer.isEnabled() &&
2710 mAttachInfo.mHardwareRenderer.isRequested()) {
Romain Guy3696779b2013-01-28 14:04:07 -08002711
2712 try {
Chris Craikd36a81f2014-07-17 10:16:51 -07002713 mAttachInfo.mHardwareRenderer.initializeIfNeeded(
Alan Viverette50210d92015-05-14 18:05:36 -07002714 mWidth, mHeight, mAttachInfo, mSurface, surfaceInsets);
Igor Murashkina86ab6402013-08-30 12:58:36 -07002715 } catch (OutOfResourcesException e) {
Romain Guy3696779b2013-01-28 14:04:07 -08002716 handleOutOfResourcesException(e);
2717 return;
2718 }
2719
2720 mFullRedrawNeeded = true;
2721 scheduleTraversals();
2722 return;
2723 }
2724
Chris Craikd36a81f2014-07-17 10:16:51 -07002725 if (!drawSoftware(surface, mAttachInfo, xOffset, yOffset, scalingRequired, dirty)) {
Romain Guy3696779b2013-01-28 14:04:07 -08002726 return;
2727 }
Jeff Brown95db2b22011-11-30 19:54:41 -08002728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 }
Romain Guy8506ab42009-06-11 17:35:47 -07002730
Dianne Hackborn0f761d62010-11-30 22:06:10 -08002731 if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 mFullRedrawNeeded = true;
2733 scheduleTraversals();
2734 }
2735 }
2736
Romain Guy25eba5c2012-04-04 17:29:03 -07002737 /**
Alan Viveretteccb11e12014-07-08 16:04:02 -07002738 * @return true if drawing was successful, false if an error occurred
Romain Guy25eba5c2012-04-04 17:29:03 -07002739 */
Alan Viveretteccb11e12014-07-08 16:04:02 -07002740 private boolean drawSoftware(Surface surface, AttachInfo attachInfo, int xoff, int yoff,
Romain Guy25eba5c2012-04-04 17:29:03 -07002741 boolean scalingRequired, Rect dirty) {
2742
2743 // Draw with software renderer.
Alan Viverettea51cab92014-07-16 15:15:49 -07002744 final Canvas canvas;
Romain Guy25eba5c2012-04-04 17:29:03 -07002745 try {
Alan Viverettea51cab92014-07-16 15:15:49 -07002746 final int left = dirty.left;
2747 final int top = dirty.top;
2748 final int right = dirty.right;
2749 final int bottom = dirty.bottom;
Romain Guy25eba5c2012-04-04 17:29:03 -07002750
Romain Guy25eba5c2012-04-04 17:29:03 -07002751 canvas = mSurface.lockCanvas(dirty);
2752
Romain Guye55945e2013-04-04 15:26:04 -07002753 // The dirty rectangle can be modified by Surface.lockCanvas()
2754 //noinspection ConstantConditions
Alan Viverettea51cab92014-07-16 15:15:49 -07002755 if (left != dirty.left || top != dirty.top || right != dirty.right
2756 || bottom != dirty.bottom) {
Romain Guy25eba5c2012-04-04 17:29:03 -07002757 attachInfo.mIgnoreDirtyState = true;
2758 }
2759
2760 // TODO: Do this in native
2761 canvas.setDensity(mDensity);
2762 } catch (Surface.OutOfResourcesException e) {
Romain Guy3696779b2013-01-28 14:04:07 -08002763 handleOutOfResourcesException(e);
Romain Guy25eba5c2012-04-04 17:29:03 -07002764 return false;
2765 } catch (IllegalArgumentException e) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002766 Log.e(mTag, "Could not lock surface", e);
Romain Guy25eba5c2012-04-04 17:29:03 -07002767 // Don't assume this is due to out of memory, it could be
2768 // something else, and if it is something else then we could
2769 // kill stuff (or ourself) for no reason.
2770 mLayoutRequested = true; // ask wm for a new surface next time.
2771 return false;
2772 }
2773
2774 try {
2775 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002776 Log.v(mTag, "Surface " + surface + " drawing to bitmap w="
Romain Guy25eba5c2012-04-04 17:29:03 -07002777 + canvas.getWidth() + ", h=" + canvas.getHeight());
2778 //canvas.drawARGB(255, 255, 0, 0);
2779 }
2780
Romain Guy25eba5c2012-04-04 17:29:03 -07002781 // If this bitmap's format includes an alpha channel, we
2782 // need to clear it before drawing so that the child will
2783 // properly re-composite its drawing on a transparent
2784 // background. This automatically respects the clip/dirty region
2785 // or
2786 // If we are applying an offset, we need to clear the area
2787 // where the offset doesn't appear to avoid having garbage
2788 // left in the blank areas.
Alan Viveretteccb11e12014-07-08 16:04:02 -07002789 if (!canvas.isOpaque() || yoff != 0 || xoff != 0) {
Romain Guy25eba5c2012-04-04 17:29:03 -07002790 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
2791 }
2792
2793 dirty.setEmpty();
2794 mIsAnimating = false;
Dianne Hackborn4702a852012-08-17 15:18:29 -07002795 mView.mPrivateFlags |= View.PFLAG_DRAWN;
Romain Guy25eba5c2012-04-04 17:29:03 -07002796
2797 if (DEBUG_DRAW) {
2798 Context cxt = mView.getContext();
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002799 Log.i(mTag, "Drawing: package:" + cxt.getPackageName() +
Romain Guy25eba5c2012-04-04 17:29:03 -07002800 ", metrics=" + cxt.getResources().getDisplayMetrics() +
2801 ", compatibilityInfo=" + cxt.getResources().getCompatibilityInfo());
2802 }
2803 try {
Alan Viveretteccb11e12014-07-08 16:04:02 -07002804 canvas.translate(-xoff, -yoff);
Romain Guy25eba5c2012-04-04 17:29:03 -07002805 if (mTranslator != null) {
2806 mTranslator.translateCanvas(canvas);
2807 }
Dianne Hackborn908aecc2012-07-31 16:37:34 -07002808 canvas.setScreenDensity(scalingRequired ? mNoncompatDensity : 0);
Romain Guy25eba5c2012-04-04 17:29:03 -07002809 attachInfo.mSetIgnoreDirtyState = false;
2810
Romain Guy25eba5c2012-04-04 17:29:03 -07002811 mView.draw(canvas);
Alan Viverette632af842014-10-28 13:45:11 -07002812
2813 drawAccessibilityFocusedDrawableIfNeeded(canvas);
Romain Guy25eba5c2012-04-04 17:29:03 -07002814 } finally {
2815 if (!attachInfo.mSetIgnoreDirtyState) {
2816 // Only clear the flag if it was not set during the mView.draw() call
2817 attachInfo.mIgnoreDirtyState = false;
2818 }
2819 }
Romain Guy25eba5c2012-04-04 17:29:03 -07002820 } finally {
Romain Guydddcd222012-05-18 15:33:57 -07002821 try {
2822 surface.unlockCanvasAndPost(canvas);
2823 } catch (IllegalArgumentException e) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002824 Log.e(mTag, "Could not unlock surface", e);
Romain Guydddcd222012-05-18 15:33:57 -07002825 mLayoutRequested = true; // ask wm for a new surface next time.
2826 //noinspection ReturnInsideFinallyBlock
2827 return false;
2828 }
Romain Guy25eba5c2012-04-04 17:29:03 -07002829
Romain Guy25eba5c2012-04-04 17:29:03 -07002830 if (LOCAL_LOGV) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002831 Log.v(mTag, "Surface " + surface + " unlockCanvasAndPost");
Romain Guy25eba5c2012-04-04 17:29:03 -07002832 }
2833 }
2834 return true;
2835 }
2836
Alan Viverette632af842014-10-28 13:45:11 -07002837 /**
2838 * We want to draw a highlight around the current accessibility focused.
2839 * Since adding a style for all possible view is not a viable option we
2840 * have this specialized drawing method.
2841 *
2842 * Note: We are doing this here to be able to draw the highlight for
2843 * virtual views in addition to real ones.
2844 *
2845 * @param canvas The canvas on which to draw.
2846 */
2847 private void drawAccessibilityFocusedDrawableIfNeeded(Canvas canvas) {
2848 final Rect bounds = mAttachInfo.mTmpInvalRect;
2849 if (getAccessibilityFocusedRect(bounds)) {
2850 final Drawable drawable = getAccessibilityFocusedDrawable();
2851 if (drawable != null) {
2852 drawable.setBounds(bounds);
2853 drawable.draw(canvas);
2854 }
2855 } else if (mAttachInfo.mAccessibilityFocusDrawable != null) {
2856 mAttachInfo.mAccessibilityFocusDrawable.setBounds(0, 0, 0, 0);
2857 }
2858 }
2859
2860 private boolean getAccessibilityFocusedRect(Rect bounds) {
2861 final AccessibilityManager manager = AccessibilityManager.getInstance(mView.mContext);
2862 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
2863 return false;
2864 }
2865
2866 final View host = mAccessibilityFocusedHost;
2867 if (host == null || host.mAttachInfo == null) {
2868 return false;
2869 }
2870
2871 final AccessibilityNodeProvider provider = host.getAccessibilityNodeProvider();
2872 if (provider == null) {
Svetoslavded133c2015-01-30 20:28:41 -08002873 host.getBoundsOnScreen(bounds, true);
Alan Viverette632af842014-10-28 13:45:11 -07002874 } else if (mAccessibilityFocusedVirtualView != null) {
2875 mAccessibilityFocusedVirtualView.getBoundsInScreen(bounds);
2876 } else {
2877 return false;
2878 }
2879
Alan Viverette2232add2015-05-26 15:24:18 -07002880 // Transform the rect into window-relative coordinates.
Alan Viverette632af842014-10-28 13:45:11 -07002881 final AttachInfo attachInfo = mAttachInfo;
Alan Viverette2232add2015-05-26 15:24:18 -07002882 bounds.offset(0, attachInfo.mViewRootImpl.mScrollY);
Alan Viverette632af842014-10-28 13:45:11 -07002883 bounds.offset(-attachInfo.mWindowLeft, -attachInfo.mWindowTop);
Doris Liu9607fbe2015-05-28 17:17:28 -07002884 if (!bounds.intersect(0, 0, attachInfo.mViewRootImpl.mWidth,
2885 attachInfo.mViewRootImpl.mHeight)) {
2886 // If no intersection, set bounds to empty.
2887 bounds.setEmpty();
2888 }
Alan Viverette632af842014-10-28 13:45:11 -07002889 return !bounds.isEmpty();
2890 }
2891
2892 private Drawable getAccessibilityFocusedDrawable() {
Chris Craikd36a81f2014-07-17 10:16:51 -07002893 // Lazily load the accessibility focus drawable.
2894 if (mAttachInfo.mAccessibilityFocusDrawable == null) {
Alan Viverettef6cf1a02014-08-11 14:13:02 -07002895 final TypedValue value = new TypedValue();
Chris Craikd36a81f2014-07-17 10:16:51 -07002896 final boolean resolved = mView.mContext.getTheme().resolveAttribute(
2897 R.attr.accessibilityFocusedDrawable, value, true);
2898 if (resolved) {
2899 mAttachInfo.mAccessibilityFocusDrawable =
Alan Viverette8eea3ea2014-02-03 18:40:20 -08002900 mView.mContext.getDrawable(value.resourceId);
Svetoslav Ganov42138042012-03-20 11:51:39 -07002901 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07002902 }
Chris Craikd36a81f2014-07-17 10:16:51 -07002903 return mAttachInfo.mAccessibilityFocusDrawable;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002904 }
2905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 boolean scrollToRectOrFocus(Rect rectangle, boolean immediate) {
Chris Craikd36a81f2014-07-17 10:16:51 -07002907 final Rect ci = mAttachInfo.mContentInsets;
2908 final Rect vi = mAttachInfo.mVisibleInsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 int scrollY = 0;
2910 boolean handled = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 if (vi.left > ci.left || vi.top > ci.top
2913 || vi.right > ci.right || vi.bottom > ci.bottom) {
2914 // We'll assume that we aren't going to change the scroll
2915 // offset, since we want to avoid that unless it is actually
2916 // going to make the focus visible... otherwise we scroll
2917 // all over the place.
2918 scrollY = mScrollY;
2919 // We can be called for two different situations: during a draw,
2920 // to update the scroll position if the focus has changed (in which
2921 // case 'rectangle' is null), or in response to a
2922 // requestChildRectangleOnScreen() call (in which case 'rectangle'
2923 // is non-null and we just want to scroll to whatever that
2924 // rectangle is).
Craig Mautner26a84df2013-04-11 19:09:05 -07002925 final View focus = mView.findFocus();
Svetoslav Ganov149567f2013-01-08 15:23:34 -08002926 if (focus == null) {
Romain Guye8b16522009-07-14 13:06:42 -07002927 return false;
2928 }
Svetoslav Ganov149567f2013-01-08 15:23:34 -08002929 View lastScrolledFocus = (mLastScrolledFocus != null) ? mLastScrolledFocus.get() : null;
Craig Mautner26a84df2013-04-11 19:09:05 -07002930 if (focus != lastScrolledFocus) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002931 // If the focus has changed, then ignore any requests to scroll
2932 // to a rectangle; first we want to make sure the entire focus
2933 // view is visible.
2934 rectangle = null;
2935 }
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002936 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "Eval scroll: focus=" + focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 + " rectangle=" + rectangle + " ci=" + ci
2938 + " vi=" + vi);
Svetoslav Ganov149567f2013-01-08 15:23:34 -08002939 if (focus == lastScrolledFocus && !mScrollMayChange && rectangle == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 // Optimization: if the focus hasn't changed since last
2941 // time, and no layout has happened, then just leave things
2942 // as they are.
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002943 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "Keeping scroll y="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944 + mScrollY + " vi=" + vi.toShortString());
Craig Mautner26a84df2013-04-11 19:09:05 -07002945 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 // We need to determine if the currently focused view is
2947 // within the visible part of the window and, if not, apply
2948 // a pan so it can be seen.
Svetoslav Ganov149567f2013-01-08 15:23:34 -08002949 mLastScrolledFocus = new WeakReference<View>(focus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 mScrollMayChange = false;
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002951 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "Need to scroll?");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 // Try to find the rectangle from the focus view.
2953 if (focus.getGlobalVisibleRect(mVisRect, null)) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002954 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "Root w="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955 + mView.getWidth() + " h=" + mView.getHeight()
2956 + " ci=" + ci.toShortString()
2957 + " vi=" + vi.toShortString());
2958 if (rectangle == null) {
2959 focus.getFocusedRect(mTempRect);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002960 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "Focus " + focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 + ": focusRect=" + mTempRect.toShortString());
Dianne Hackborn1c6a8942010-03-23 16:34:20 -07002962 if (mView instanceof ViewGroup) {
2963 ((ViewGroup) mView).offsetDescendantRectToMyCoords(
2964 focus, mTempRect);
2965 }
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002966 if (DEBUG_INPUT_RESIZE) Log.v(mTag,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 "Focus in window: focusRect="
2968 + mTempRect.toShortString()
2969 + " visRect=" + mVisRect.toShortString());
2970 } else {
2971 mTempRect.set(rectangle);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002972 if (DEBUG_INPUT_RESIZE) Log.v(mTag,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002973 "Request scroll to rect: "
2974 + mTempRect.toShortString()
2975 + " visRect=" + mVisRect.toShortString());
2976 }
2977 if (mTempRect.intersect(mVisRect)) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002978 if (DEBUG_INPUT_RESIZE) Log.v(mTag,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 "Focus window visible rect: "
2980 + mTempRect.toShortString());
2981 if (mTempRect.height() >
2982 (mView.getHeight()-vi.top-vi.bottom)) {
2983 // If the focus simply is not going to fit, then
2984 // best is probably just to leave things as-is.
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002985 if (DEBUG_INPUT_RESIZE) Log.v(mTag,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 "Too tall; leaving scrollY=" + scrollY);
2987 } else if ((mTempRect.top-scrollY) < vi.top) {
2988 scrollY -= vi.top - (mTempRect.top-scrollY);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002989 if (DEBUG_INPUT_RESIZE) Log.v(mTag,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002990 "Top covered; scrollY=" + scrollY);
2991 } else if ((mTempRect.bottom-scrollY)
2992 > (mView.getHeight()-vi.bottom)) {
2993 scrollY += (mTempRect.bottom-scrollY)
2994 - (mView.getHeight()-vi.bottom);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002995 if (DEBUG_INPUT_RESIZE) Log.v(mTag,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002996 "Bottom covered; scrollY=" + scrollY);
2997 }
2998 handled = true;
2999 }
3000 }
3001 }
3002 }
Romain Guy8506ab42009-06-11 17:35:47 -07003003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003004 if (scrollY != mScrollY) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08003005 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "Pan scroll changed: old="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003006 + mScrollY + " , new=" + scrollY);
Derek Sollenberger8d948352015-07-16 09:27:59 -04003007 if (!immediate) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003008 if (mScroller == null) {
3009 mScroller = new Scroller(mView.getContext());
3010 }
3011 mScroller.startScroll(0, mScrollY, 0, scrollY-mScrollY);
3012 } else if (mScroller != null) {
3013 mScroller.abortAnimation();
3014 }
3015 mScrollY = scrollY;
3016 }
Romain Guy8506ab42009-06-11 17:35:47 -07003017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 return handled;
3019 }
Romain Guy8506ab42009-06-11 17:35:47 -07003020
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07003021 /**
3022 * @hide
3023 */
3024 public View getAccessibilityFocusedHost() {
3025 return mAccessibilityFocusedHost;
3026 }
3027
3028 /**
3029 * @hide
3030 */
3031 public AccessibilityNodeInfo getAccessibilityFocusedVirtualView() {
3032 return mAccessibilityFocusedVirtualView;
3033 }
3034
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07003035 void setAccessibilityFocus(View view, AccessibilityNodeInfo node) {
Svetoslav Ganov791fd312012-05-14 15:12:30 -07003036 // If we have a virtual view with accessibility focus we need
3037 // to clear the focus and invalidate the virtual view bounds.
3038 if (mAccessibilityFocusedVirtualView != null) {
3039
3040 AccessibilityNodeInfo focusNode = mAccessibilityFocusedVirtualView;
3041 View focusHost = mAccessibilityFocusedHost;
Svetoslav Ganov791fd312012-05-14 15:12:30 -07003042
3043 // Wipe the state of the current accessibility focus since
3044 // the call into the provider to clear accessibility focus
3045 // will fire an accessibility event which will end up calling
3046 // this method and we want to have clean state when this
3047 // invocation happens.
3048 mAccessibilityFocusedHost = null;
3049 mAccessibilityFocusedVirtualView = null;
3050
Alan Viverette239a0c02013-05-07 17:17:35 -07003051 // Clear accessibility focus on the host after clearing state since
3052 // this method may be reentrant.
3053 focusHost.clearAccessibilityFocusNoCallbacks();
3054
Svetoslav Ganov791fd312012-05-14 15:12:30 -07003055 AccessibilityNodeProvider provider = focusHost.getAccessibilityNodeProvider();
3056 if (provider != null) {
3057 // Invalidate the area of the cleared accessibility focus.
3058 focusNode.getBoundsInParent(mTempRect);
3059 focusHost.invalidate(mTempRect);
3060 // Clear accessibility focus in the virtual node.
3061 final int virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
3062 focusNode.getSourceNodeId());
3063 provider.performAction(virtualNodeId,
3064 AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS, null);
3065 }
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07003066 focusNode.recycle();
Svetoslav Ganov791fd312012-05-14 15:12:30 -07003067 }
3068 if (mAccessibilityFocusedHost != null) {
3069 // Clear accessibility focus in the view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07003070 mAccessibilityFocusedHost.clearAccessibilityFocusNoCallbacks();
3071 }
Svetoslav Ganov791fd312012-05-14 15:12:30 -07003072
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07003073 // Set the new focus host and node.
3074 mAccessibilityFocusedHost = view;
3075 mAccessibilityFocusedVirtualView = node;
John Reck0a973302014-07-16 13:29:45 -07003076
3077 if (mAttachInfo.mHardwareRenderer != null) {
3078 mAttachInfo.mHardwareRenderer.invalidateRoot();
3079 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07003080 }
3081
Jun Mukai347e5d42015-12-03 01:13:31 -08003082 void setPointerCapture(View view) {
3083 if (!mAttachInfo.mHasWindowFocus) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08003084 Log.w(mTag, "Can't set capture if it's not focused.");
Jun Mukai347e5d42015-12-03 01:13:31 -08003085 return;
3086 }
3087 if (mCapturingView == view) {
3088 return;
3089 }
3090 mCapturingView = view;
3091 InputManager.getInstance().setPointerIconDetached(true);
3092 }
3093
3094 void releasePointerCapture(View view) {
3095 if (mCapturingView != view || mCapturingView == null) {
3096 return;
3097 }
3098
3099 mCapturingView = null;
3100 InputManager.getInstance().setPointerIconDetached(false);
3101 }
3102
3103 boolean hasPointerCapture(View view) {
3104 return view != null && mCapturingView == view;
3105 }
3106
Igor Murashkina86ab6402013-08-30 12:58:36 -07003107 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003108 public void requestChildFocus(View child, View focused) {
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08003109 if (DEBUG_INPUT_RESIZE) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08003110 Log.v(mTag, "Request child focus: focus now " + focused);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 }
Svetoslav Ganov149567f2013-01-08 15:23:34 -08003112 checkThread();
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08003113 scheduleTraversals();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003114 }
3115
Igor Murashkina86ab6402013-08-30 12:58:36 -07003116 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003117 public void clearChildFocus(View child) {
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08003118 if (DEBUG_INPUT_RESIZE) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08003119 Log.v(mTag, "Clearing child focus");
Amith Yamasani73eb97f2012-02-14 15:45:15 -08003120 }
Svetoslav Ganov149567f2013-01-08 15:23:34 -08003121 checkThread();
3122 scheduleTraversals();
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08003123 }
Amith Yamasani73eb97f2012-02-14 15:45:15 -08003124
Svetoslav Ganov42138042012-03-20 11:51:39 -07003125 @Override
3126 public ViewParent getParentForAccessibility() {
3127 return null;
3128 }
3129
Igor Murashkina86ab6402013-08-30 12:58:36 -07003130 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003131 public void focusableViewAvailable(View v) {
3132 checkThread();
Romain Guy1c90f032011-05-24 14:59:50 -07003133 if (mView != null) {
3134 if (!mView.hasFocus()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003135 v.requestFocus();
Romain Guy1c90f032011-05-24 14:59:50 -07003136 } else {
3137 // the one case where will transfer focus away from the current one
3138 // is if the current view is a view group that prefers to give focus
3139 // to its children first AND the view is a descendant of it.
Svetoslav Ganov149567f2013-01-08 15:23:34 -08003140 View focused = mView.findFocus();
3141 if (focused instanceof ViewGroup) {
3142 ViewGroup group = (ViewGroup) focused;
3143 if (group.getDescendantFocusability() == ViewGroup.FOCUS_AFTER_DESCENDANTS
3144 && isViewDescendantOf(v, focused)) {
3145 v.requestFocus();
3146 }
Romain Guy1c90f032011-05-24 14:59:50 -07003147 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003148 }
3149 }
3150 }
3151
Igor Murashkina86ab6402013-08-30 12:58:36 -07003152 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003153 public void recomputeViewAttributes(View child) {
3154 checkThread();
3155 if (mView == child) {
3156 mAttachInfo.mRecomputeGlobalAttributes = true;
3157 if (!mWillDrawSoon) {
3158 scheduleTraversals();
3159 }
3160 }
3161 }
3162
3163 void dispatchDetachedFromWindow() {
Romain Guy90fc03b2011-01-16 13:07:15 -08003164 if (mView != null && mView.mAttachInfo != null) {
Dianne Hackborn961cae92013-03-20 14:59:43 -07003165 mAttachInfo.mTreeObserver.dispatchOnWindowAttachedChange(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003166 mView.dispatchDetachedFromWindow();
3167 }
3168
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003169 mAccessibilityInteractionConnectionManager.ensureNoConnection();
3170 mAccessibilityManager.removeAccessibilityStateChangeListener(
3171 mAccessibilityInteractionConnectionManager);
Chris Craikcce47eb2014-07-16 15:12:15 -07003172 mAccessibilityManager.removeHighTextContrastStateChangeListener(
3173 mHighContrastTextManager);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003174 removeSendWindowContentChangedCallback();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003175
Romain Guya998dff2012-03-23 18:58:36 -07003176 destroyHardwareRenderer();
3177
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07003178 setAccessibilityFocus(null, null);
Svetoslav Ganov791fd312012-05-14 15:12:30 -07003179
Craig Mautner8f303ad2013-06-14 11:32:22 -07003180 mView.assignParent(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181 mView = null;
3182 mAttachInfo.mRootView = null;
3183
Jun Mukai347e5d42015-12-03 01:13:31 -08003184 if (mCapturingView != null) {
3185 releasePointerCapture(mCapturingView);
3186 }
3187
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07003188 mSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003189
Jeff Browncc4f7db2011-08-30 20:34:48 -07003190 if (mInputQueueCallback != null && mInputQueue != null) {
3191 mInputQueueCallback.onInputQueueDestroyed(mInputQueue);
Michael Wrighta44dd262013-04-10 21:12:00 -07003192 mInputQueue.dispose();
Jeff Browncc4f7db2011-08-30 20:34:48 -07003193 mInputQueueCallback = null;
3194 mInputQueue = null;
Michael Wrighta44dd262013-04-10 21:12:00 -07003195 }
3196 if (mInputEventReceiver != null) {
Jeff Brown32cbc38552011-12-01 14:01:49 -08003197 mInputEventReceiver.dispose();
3198 mInputEventReceiver = null;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 try {
Jeff Brown98365d72012-08-19 20:30:52 -07003201 mWindowSession.remove(mWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003202 } catch (RemoteException e) {
3203 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07003204
Jeff Brown00fa7bd2010-07-02 15:37:36 -07003205 // Dispose the input channel after removing the window so the Window Manager
3206 // doesn't interpret the input channel being closed as an abnormal termination.
3207 if (mInputChannel != null) {
3208 mInputChannel.dispose();
3209 mInputChannel = null;
Jeff Brown349703e2010-06-22 01:27:15 -07003210 }
Jeff Brown96e942d2011-11-30 19:55:01 -08003211
Jeff Brownd912e1f2014-04-11 18:46:22 -07003212 mDisplayManager.unregisterDisplayListener(mDisplayListener);
3213
Jeff Brownebb2d8d2012-03-23 17:14:34 -07003214 unscheduleTraversals();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 }
Romain Guy8506ab42009-06-11 17:35:47 -07003216
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003217 void updateConfiguration(Configuration config, boolean force) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08003218 if (DEBUG_CONFIGURATION) Log.v(mTag,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003219 "Applying new config to window "
3220 + mWindowAttributes.getTitle()
3221 + ": " + config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003222
Craig Mautner48d0d182013-06-11 07:53:06 -07003223 CompatibilityInfo ci = mDisplayAdjustments.getCompatibilityInfo();
3224 if (!ci.equals(CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO)) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003225 config = new Configuration(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003226 ci.applyToConfiguration(mNoncompatDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003227 }
3228
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003229 synchronized (sConfigCallbacks) {
3230 for (int i=sConfigCallbacks.size()-1; i>=0; i--) {
3231 sConfigCallbacks.get(i).onConfigurationChanged(config);
3232 }
3233 }
3234 if (mView != null) {
3235 // At this point the resources have been updated to
3236 // have the most recent config, whatever that is. Use
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003237 // the one in them which may be newer.
Romain Guy1c90f032011-05-24 14:59:50 -07003238 config = mView.getResources().getConfiguration();
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003239 if (force || mLastConfiguration.diff(config) != 0) {
Fabrice Di Megliocf128972012-10-16 20:51:12 -07003240 final int lastLayoutDirection = mLastConfiguration.getLayoutDirection();
3241 final int currentLayoutDirection = config.getLayoutDirection();
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003242 mLastConfiguration.setTo(config);
Fabrice Di Megliob003e282012-10-17 17:20:19 -07003243 if (lastLayoutDirection != currentLayoutDirection &&
3244 mViewLayoutDirectionInitial == View.LAYOUT_DIRECTION_INHERIT) {
Fabrice Di Megliocf128972012-10-16 20:51:12 -07003245 mView.setLayoutDirection(currentLayoutDirection);
3246 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003247 mView.dispatchConfigurationChanged(config);
3248 }
3249 }
3250 }
John Reck05e85842014-04-23 14:48:28 -07003251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 /**
3253 * Return true if child is an ancestor of parent, (or equal to the parent).
3254 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07003255 public static boolean isViewDescendantOf(View child, View parent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003256 if (child == parent) {
3257 return true;
3258 }
3259
3260 final ViewParent theParent = child.getParent();
3261 return (theParent instanceof ViewGroup) && isViewDescendantOf((View) theParent, parent);
3262 }
3263
Yohei Yukawad2e56472015-07-28 17:00:33 -07003264 private static void forceLayout(View view) {
3265 view.forceLayout();
3266 if (view instanceof ViewGroup) {
3267 ViewGroup group = (ViewGroup) view;
3268 final int count = group.getChildCount();
3269 for (int i = 0; i < count; i++) {
3270 forceLayout(group.getChildAt(i));
3271 }
3272 }
3273 }
3274
Jeff Browna175a5b2012-02-15 19:18:31 -08003275 private final static int MSG_INVALIDATE = 1;
3276 private final static int MSG_INVALIDATE_RECT = 2;
3277 private final static int MSG_DIE = 3;
3278 private final static int MSG_RESIZED = 4;
3279 private final static int MSG_RESIZED_REPORT = 5;
3280 private final static int MSG_WINDOW_FOCUS_CHANGED = 6;
keunyoung30f420f2013-08-02 14:23:10 -07003281 private final static int MSG_DISPATCH_INPUT_EVENT = 7;
Jeff Browna175a5b2012-02-15 19:18:31 -08003282 private final static int MSG_DISPATCH_APP_VISIBILITY = 8;
3283 private final static int MSG_DISPATCH_GET_NEW_SURFACE = 9;
Jeff Browna175a5b2012-02-15 19:18:31 -08003284 private final static int MSG_DISPATCH_KEY_FROM_IME = 11;
3285 private final static int MSG_FINISH_INPUT_CONNECTION = 12;
3286 private final static int MSG_CHECK_FOCUS = 13;
3287 private final static int MSG_CLOSE_SYSTEM_DIALOGS = 14;
3288 private final static int MSG_DISPATCH_DRAG_EVENT = 15;
3289 private final static int MSG_DISPATCH_DRAG_LOCATION_EVENT = 16;
3290 private final static int MSG_DISPATCH_SYSTEM_UI_VISIBILITY = 17;
3291 private final static int MSG_UPDATE_CONFIGURATION = 18;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003292 private final static int MSG_PROCESS_INPUT_EVENTS = 19;
Romain Guyfbb93fa2012-12-03 18:50:35 -08003293 private final static int MSG_CLEAR_ACCESSIBILITY_FOCUS_HOST = 21;
Jorim Jaggi827e0fa2015-05-07 11:41:41 -07003294 private final static int MSG_INVALIDATE_WORLD = 22;
3295 private final static int MSG_WINDOW_MOVED = 23;
3296 private final static int MSG_SYNTHESIZE_INPUT_EVENT = 24;
3297 private final static int MSG_DISPATCH_WINDOW_SHOWN = 25;
Clara Bayarri75e09792015-07-29 16:20:40 +01003298 private final static int MSG_REQUEST_KEYBOARD_SHORTCUTS = 26;
Vladislav Kaznacheevec6a4472016-01-22 12:21:52 -08003299 private final static int MSG_UPDATE_POINTER_ICON = 27;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300
Jeff Browna175a5b2012-02-15 19:18:31 -08003301 final class ViewRootHandler extends Handler {
3302 @Override
3303 public String getMessageName(Message message) {
3304 switch (message.what) {
3305 case MSG_INVALIDATE:
3306 return "MSG_INVALIDATE";
3307 case MSG_INVALIDATE_RECT:
3308 return "MSG_INVALIDATE_RECT";
3309 case MSG_DIE:
3310 return "MSG_DIE";
3311 case MSG_RESIZED:
3312 return "MSG_RESIZED";
3313 case MSG_RESIZED_REPORT:
3314 return "MSG_RESIZED_REPORT";
3315 case MSG_WINDOW_FOCUS_CHANGED:
3316 return "MSG_WINDOW_FOCUS_CHANGED";
keunyoung30f420f2013-08-02 14:23:10 -07003317 case MSG_DISPATCH_INPUT_EVENT:
3318 return "MSG_DISPATCH_INPUT_EVENT";
Jeff Browna175a5b2012-02-15 19:18:31 -08003319 case MSG_DISPATCH_APP_VISIBILITY:
3320 return "MSG_DISPATCH_APP_VISIBILITY";
3321 case MSG_DISPATCH_GET_NEW_SURFACE:
3322 return "MSG_DISPATCH_GET_NEW_SURFACE";
Jeff Browna175a5b2012-02-15 19:18:31 -08003323 case MSG_DISPATCH_KEY_FROM_IME:
3324 return "MSG_DISPATCH_KEY_FROM_IME";
3325 case MSG_FINISH_INPUT_CONNECTION:
3326 return "MSG_FINISH_INPUT_CONNECTION";
3327 case MSG_CHECK_FOCUS:
3328 return "MSG_CHECK_FOCUS";
3329 case MSG_CLOSE_SYSTEM_DIALOGS:
3330 return "MSG_CLOSE_SYSTEM_DIALOGS";
3331 case MSG_DISPATCH_DRAG_EVENT:
3332 return "MSG_DISPATCH_DRAG_EVENT";
3333 case MSG_DISPATCH_DRAG_LOCATION_EVENT:
3334 return "MSG_DISPATCH_DRAG_LOCATION_EVENT";
3335 case MSG_DISPATCH_SYSTEM_UI_VISIBILITY:
3336 return "MSG_DISPATCH_SYSTEM_UI_VISIBILITY";
3337 case MSG_UPDATE_CONFIGURATION:
3338 return "MSG_UPDATE_CONFIGURATION";
Jeff Browna175a5b2012-02-15 19:18:31 -08003339 case MSG_PROCESS_INPUT_EVENTS:
3340 return "MSG_PROCESS_INPUT_EVENTS";
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07003341 case MSG_CLEAR_ACCESSIBILITY_FOCUS_HOST:
3342 return "MSG_CLEAR_ACCESSIBILITY_FOCUS_HOST";
Craig Mautner5702d4d2012-06-30 14:10:16 -07003343 case MSG_WINDOW_MOVED:
3344 return "MSG_WINDOW_MOVED";
Michael Wright899d7052014-04-23 17:23:39 -07003345 case MSG_SYNTHESIZE_INPUT_EVENT:
3346 return "MSG_SYNTHESIZE_INPUT_EVENT";
Craig Mautner9c795042014-10-28 19:59:59 -07003347 case MSG_DISPATCH_WINDOW_SHOWN:
3348 return "MSG_DISPATCH_WINDOW_SHOWN";
Vladislav Kaznacheevec6a4472016-01-22 12:21:52 -08003349 case MSG_UPDATE_POINTER_ICON:
3350 return "MSG_UPDATE_POINTER_ICON";
Jeff Browna175a5b2012-02-15 19:18:31 -08003351 }
3352 return super.getMessageName(message);
Romain Guyf9284692011-07-13 18:46:21 -07003353 }
Romain Guyf9284692011-07-13 18:46:21 -07003354
Jeff Browna175a5b2012-02-15 19:18:31 -08003355 @Override
3356 public void handleMessage(Message msg) {
3357 switch (msg.what) {
3358 case MSG_INVALIDATE:
3359 ((View) msg.obj).invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 break;
Jeff Browna175a5b2012-02-15 19:18:31 -08003361 case MSG_INVALIDATE_RECT:
3362 final View.AttachInfo.InvalidateInfo info = (View.AttachInfo.InvalidateInfo) msg.obj;
3363 info.target.invalidate(info.left, info.top, info.right, info.bottom);
Svetoslav Ganovabae2a12012-11-27 16:59:37 -08003364 info.recycle();
Jeff Browna175a5b2012-02-15 19:18:31 -08003365 break;
Jeff Browna175a5b2012-02-15 19:18:31 -08003366 case MSG_PROCESS_INPUT_EVENTS:
3367 mProcessInputEventsScheduled = false;
3368 doProcessInputEvents();
3369 break;
3370 case MSG_DISPATCH_APP_VISIBILITY:
3371 handleAppVisibility(msg.arg1 != 0);
3372 break;
3373 case MSG_DISPATCH_GET_NEW_SURFACE:
3374 handleGetNewSurface();
3375 break;
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003376 case MSG_RESIZED: {
3377 // Recycled in the fall through...
3378 SomeArgs args = (SomeArgs) msg.obj;
Romain Guydfab3632012-10-03 14:53:25 -07003379 if (mWinFrame.equals(args.arg1)
Dianne Hackbornc4aad012013-02-22 15:05:25 -08003380 && mPendingOverscanInsets.equals(args.arg5)
Romain Guydfab3632012-10-03 14:53:25 -07003381 && mPendingContentInsets.equals(args.arg2)
Adrian Roosfa104232014-06-20 16:10:14 -07003382 && mPendingStableInsets.equals(args.arg6)
Romain Guydfab3632012-10-03 14:53:25 -07003383 && mPendingVisibleInsets.equals(args.arg3)
Filip Gruszczynski2217f612015-05-26 11:32:08 -07003384 && mPendingOutsets.equals(args.arg7)
Jorim Jaggi0fe356e2016-01-05 14:43:25 +01003385 && mPendingBackDropFrame.equals(args.arg8)
Jorim Jaggia4a58ef2016-01-27 02:10:08 -08003386 && args.arg4 == null
3387 && args.argi1 == 0) {
Jeff Browna175a5b2012-02-15 19:18:31 -08003388 break;
Romain Guycdb86672010-03-18 18:54:50 -07003389 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003390 } // fall through...
Jeff Browna175a5b2012-02-15 19:18:31 -08003391 case MSG_RESIZED_REPORT:
3392 if (mAdded) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003393 SomeArgs args = (SomeArgs) msg.obj;
3394
3395 Configuration config = (Configuration) args.arg4;
Jeff Browna175a5b2012-02-15 19:18:31 -08003396 if (config != null) {
3397 updateConfiguration(config, false);
3398 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003399
3400 mWinFrame.set((Rect) args.arg1);
Dianne Hackbornc4aad012013-02-22 15:05:25 -08003401 mPendingOverscanInsets.set((Rect) args.arg5);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003402 mPendingContentInsets.set((Rect) args.arg2);
Adrian Roosfa104232014-06-20 16:10:14 -07003403 mPendingStableInsets.set((Rect) args.arg6);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003404 mPendingVisibleInsets.set((Rect) args.arg3);
Filip Gruszczynski2217f612015-05-26 11:32:08 -07003405 mPendingOutsets.set((Rect) args.arg7);
Jorim Jaggia7262a82015-11-03 15:15:40 +01003406 mPendingBackDropFrame.set((Rect) args.arg8);
Jorim Jaggia4a58ef2016-01-27 02:10:08 -08003407 mForceNextWindowRelayout = args.argi1 != 0;
Jorim Jaggi0ffd49c2016-02-12 15:04:21 -08003408 mPendingAlwaysConsumeNavBar = args.argi2 != 0;
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003409
3410 args.recycle();
3411
Jeff Browna175a5b2012-02-15 19:18:31 -08003412 if (msg.what == MSG_RESIZED_REPORT) {
3413 mReportNextDraw = true;
3414 }
Romain Guy59a12ca2011-06-09 17:48:21 -07003415
Yohei Yukawad2e56472015-07-28 17:00:33 -07003416 if (mView != null) {
3417 forceLayout(mView);
3418 }
3419
Jeff Browna175a5b2012-02-15 19:18:31 -08003420 requestLayout();
3421 }
3422 break;
Craig Mautner5702d4d2012-06-30 14:10:16 -07003423 case MSG_WINDOW_MOVED:
3424 if (mAdded) {
3425 final int w = mWinFrame.width();
3426 final int h = mWinFrame.height();
3427 final int l = msg.arg1;
3428 final int t = msg.arg2;
3429 mWinFrame.left = l;
3430 mWinFrame.right = l + w;
3431 mWinFrame.top = t;
3432 mWinFrame.bottom = t + h;
3433
Jorim Jaggia7262a82015-11-03 15:15:40 +01003434 mPendingBackDropFrame.set(mWinFrame);
3435
Jorim Jaggi844e1712016-01-13 17:39:25 -08003436 // Suppress layouts during resizing - a correct layout will happen when resizing
3437 // is done, and this just increases system load.
Jorim Jaggi2e95a482016-01-14 17:36:55 -08003438 boolean isDockedDivider = mWindowAttributes.type == TYPE_DOCK_DIVIDER;
3439 boolean suppress = (mDragResizing && mResizeMode == RESIZE_MODE_DOCKED_DIVIDER)
3440 || isDockedDivider;
Jorim Jaggi844e1712016-01-13 17:39:25 -08003441 if (!suppress) {
3442 if (mView != null) {
3443 forceLayout(mView);
3444 }
3445 requestLayout();
Jorim Jaggi2e95a482016-01-14 17:36:55 -08003446 } else {
3447 maybeHandleWindowMove(mWinFrame);
Yohei Yukawad2e56472015-07-28 17:00:33 -07003448 }
Craig Mautner5702d4d2012-06-30 14:10:16 -07003449 }
3450 break;
Jeff Browna175a5b2012-02-15 19:18:31 -08003451 case MSG_WINDOW_FOCUS_CHANGED: {
3452 if (mAdded) {
3453 boolean hasWindowFocus = msg.arg1 != 0;
3454 mAttachInfo.mHasWindowFocus = hasWindowFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003455
Jeff Browna175a5b2012-02-15 19:18:31 -08003456 profileRendering(hasWindowFocus);
3457
3458 if (hasWindowFocus) {
3459 boolean inTouchMode = msg.arg2 != 0;
3460 ensureTouchModeLocally(inTouchMode);
3461
Romain Guye55945e2013-04-04 15:26:04 -07003462 if (mAttachInfo.mHardwareRenderer != null && mSurface.isValid()){
Jeff Browna175a5b2012-02-15 19:18:31 -08003463 mFullRedrawNeeded = true;
Dianne Hackborn64825172011-03-02 21:32:58 -08003464 try {
Alan Viveretteccb11e12014-07-08 16:04:02 -07003465 final WindowManager.LayoutParams lp = mWindowAttributes;
Alan Viverette49a22e82014-07-12 20:01:27 -07003466 final Rect surfaceInsets = lp != null ? lp.surfaceInsets : null;
Romain Guy3696779b2013-01-28 14:04:07 -08003467 mAttachInfo.mHardwareRenderer.initializeIfNeeded(
Alan Viverette50210d92015-05-14 18:05:36 -07003468 mWidth, mHeight, mAttachInfo, mSurface, surfaceInsets);
Igor Murashkina86ab6402013-08-30 12:58:36 -07003469 } catch (OutOfResourcesException e) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08003470 Log.e(mTag, "OutOfResourcesException locking surface", e);
Jeff Browna175a5b2012-02-15 19:18:31 -08003471 try {
Jeff Brown98365d72012-08-19 20:30:52 -07003472 if (!mWindowSession.outOfMemory(mWindow)) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08003473 Slog.w(mTag, "No processes killed for memory; killing self");
Jeff Browna175a5b2012-02-15 19:18:31 -08003474 Process.killProcess(Process.myPid());
3475 }
3476 } catch (RemoteException ex) {
Dianne Hackborn64825172011-03-02 21:32:58 -08003477 }
Jeff Browna175a5b2012-02-15 19:18:31 -08003478 // Retry in a bit.
3479 sendMessageDelayed(obtainMessage(msg.what, msg.arg1, msg.arg2), 500);
3480 return;
Dianne Hackborn64825172011-03-02 21:32:58 -08003481 }
Dianne Hackborn64825172011-03-02 21:32:58 -08003482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003483 }
Romain Guy8506ab42009-06-11 17:35:47 -07003484
Jeff Browna175a5b2012-02-15 19:18:31 -08003485 mLastWasImTarget = WindowManager.LayoutParams
3486 .mayUseInputMethod(mWindowAttributes.flags);
Romain Guy8506ab42009-06-11 17:35:47 -07003487
Jeff Browna175a5b2012-02-15 19:18:31 -08003488 InputMethodManager imm = InputMethodManager.peekInstance();
Yohei Yukawa5f059652015-05-14 22:16:41 -07003489 if (imm != null && mLastWasImTarget && !isInLocalFocusMode()) {
3490 imm.onPreWindowFocus(mView, hasWindowFocus);
3491 }
Jeff Browna175a5b2012-02-15 19:18:31 -08003492 if (mView != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08003493 mAttachInfo.mKeyDispatchState.reset();
3494 mView.dispatchWindowFocusChanged(hasWindowFocus);
Dianne Hackborn961cae92013-03-20 14:59:43 -07003495 mAttachInfo.mTreeObserver.dispatchOnWindowFocusChange(hasWindowFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003496 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003497
Jeff Browna175a5b2012-02-15 19:18:31 -08003498 // Note: must be done after the focus change callbacks,
3499 // so all of the view state is set up correctly.
3500 if (hasWindowFocus) {
keunyoung30f420f2013-08-02 14:23:10 -07003501 if (imm != null && mLastWasImTarget && !isInLocalFocusMode()) {
Yohei Yukawa5f059652015-05-14 22:16:41 -07003502 imm.onPostWindowFocus(mView, mView.findFocus(),
Jeff Browna175a5b2012-02-15 19:18:31 -08003503 mWindowAttributes.softInputMode,
3504 !mHasHadWindowFocus, mWindowAttributes.flags);
3505 }
3506 // Clear the forward bit. We can just do this directly, since
3507 // the window manager doesn't care about it.
3508 mWindowAttributes.softInputMode &=
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003509 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
Jeff Browna175a5b2012-02-15 19:18:31 -08003510 ((WindowManager.LayoutParams)mView.getLayoutParams())
3511 .softInputMode &=
3512 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
3513 mHasHadWindowFocus = true;
Jun Mukai347e5d42015-12-03 01:13:31 -08003514 } else if (mCapturingView != null) {
3515 releasePointerCapture(mCapturingView);
Jeff Browna175a5b2012-02-15 19:18:31 -08003516 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003517 }
Jeff Browna175a5b2012-02-15 19:18:31 -08003518 } break;
3519 case MSG_DIE:
3520 doDie();
3521 break;
keunyoung30f420f2013-08-02 14:23:10 -07003522 case MSG_DISPATCH_INPUT_EVENT: {
Jae Seo6a6059a2014-04-17 21:35:29 -07003523 SomeArgs args = (SomeArgs)msg.obj;
3524 InputEvent event = (InputEvent)args.arg1;
3525 InputEventReceiver receiver = (InputEventReceiver)args.arg2;
3526 enqueueInputEvent(event, receiver, 0, true);
3527 args.recycle();
Jeff Browna175a5b2012-02-15 19:18:31 -08003528 } break;
Michael Wright899d7052014-04-23 17:23:39 -07003529 case MSG_SYNTHESIZE_INPUT_EVENT: {
3530 InputEvent event = (InputEvent)msg.obj;
3531 enqueueInputEvent(event, null, QueuedInputEvent.FLAG_UNHANDLED, true);
3532 } break;
Jeff Browna175a5b2012-02-15 19:18:31 -08003533 case MSG_DISPATCH_KEY_FROM_IME: {
3534 if (LOCAL_LOGV) Log.v(
3535 TAG, "Dispatching key "
3536 + msg.obj + " from IME to " + mView);
3537 KeyEvent event = (KeyEvent)msg.obj;
3538 if ((event.getFlags()&KeyEvent.FLAG_FROM_SYSTEM) != 0) {
3539 // The IME is trying to say this event is from the
3540 // system! Bad bad bad!
3541 //noinspection UnusedAssignment
Michael Wright899d7052014-04-23 17:23:39 -07003542 event = KeyEvent.changeFlags(event, event.getFlags() &
3543 ~KeyEvent.FLAG_FROM_SYSTEM);
Jeff Browna175a5b2012-02-15 19:18:31 -08003544 }
3545 enqueueInputEvent(event, null, QueuedInputEvent.FLAG_DELIVER_POST_IME, true);
3546 } break;
3547 case MSG_FINISH_INPUT_CONNECTION: {
3548 InputMethodManager imm = InputMethodManager.peekInstance();
3549 if (imm != null) {
3550 imm.reportFinishInputConnection((InputConnection)msg.obj);
3551 }
3552 } break;
3553 case MSG_CHECK_FOCUS: {
3554 InputMethodManager imm = InputMethodManager.peekInstance();
3555 if (imm != null) {
3556 imm.checkFocus();
3557 }
3558 } break;
3559 case MSG_CLOSE_SYSTEM_DIALOGS: {
3560 if (mView != null) {
3561 mView.onCloseSystemDialogs((String)msg.obj);
3562 }
3563 } break;
3564 case MSG_DISPATCH_DRAG_EVENT:
3565 case MSG_DISPATCH_DRAG_LOCATION_EVENT: {
3566 DragEvent event = (DragEvent)msg.obj;
3567 event.mLocalState = mLocalDragState; // only present when this app called startDrag()
3568 handleDragEvent(event);
3569 } break;
3570 case MSG_DISPATCH_SYSTEM_UI_VISIBILITY: {
Romain Guyfbb93fa2012-12-03 18:50:35 -08003571 handleDispatchSystemUiVisibilityChanged((SystemUiVisibilityInfo) msg.obj);
Jeff Browna175a5b2012-02-15 19:18:31 -08003572 } break;
3573 case MSG_UPDATE_CONFIGURATION: {
3574 Configuration config = (Configuration)msg.obj;
3575 if (config.isOtherSeqNewer(mLastConfiguration)) {
3576 config = mLastConfiguration;
3577 }
3578 updateConfiguration(config, false);
3579 } break;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07003580 case MSG_CLEAR_ACCESSIBILITY_FOCUS_HOST: {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07003581 setAccessibilityFocus(null, null);
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07003582 } break;
Dianne Hackborna53de062012-05-08 18:53:51 -07003583 case MSG_INVALIDATE_WORLD: {
Romain Guyf84208f2012-09-13 22:50:18 -07003584 if (mView != null) {
3585 invalidateWorld(mView);
3586 }
Dianne Hackborna53de062012-05-08 18:53:51 -07003587 } break;
Craig Mautner9c795042014-10-28 19:59:59 -07003588 case MSG_DISPATCH_WINDOW_SHOWN: {
3589 handleDispatchWindowShown();
Clara Bayarri75e09792015-07-29 16:20:40 +01003590 } break;
3591 case MSG_REQUEST_KEYBOARD_SHORTCUTS: {
3592 IResultReceiver receiver = (IResultReceiver) msg.obj;
3593 handleRequestKeyboardShortcuts(receiver);
3594 } break;
Vladislav Kaznacheevec6a4472016-01-22 12:21:52 -08003595 case MSG_UPDATE_POINTER_ICON: {
3596 MotionEvent event = (MotionEvent) msg.obj;
3597 resetPointerIcon(event);
3598 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003599 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003600 }
3601 }
Romain Guy51e4d4d2012-03-15 18:30:47 -07003602
Jeff Browna175a5b2012-02-15 19:18:31 -08003603 final ViewRootHandler mHandler = new ViewRootHandler();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003605 /**
3606 * Something in the current window tells us we need to change the touch mode. For
3607 * example, we are not in touch mode, and the user touches the screen.
3608 *
3609 * If the touch mode has changed, tell the window manager, and handle it locally.
3610 *
3611 * @param inTouchMode Whether we want to be in touch mode.
3612 * @return True if the touch mode changed and focus changed was changed as a result
3613 */
3614 boolean ensureTouchMode(boolean inTouchMode) {
3615 if (DBG) Log.d("touchmode", "ensureTouchMode(" + inTouchMode + "), current "
3616 + "touch mode is " + mAttachInfo.mInTouchMode);
3617 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
3618
3619 // tell the window manager
3620 try {
Matt Wud6bc96d2016-01-14 12:59:24 -08003621 mWindowSession.setInTouchMode(inTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003622 } catch (RemoteException e) {
3623 throw new RuntimeException(e);
3624 }
3625
3626 // handle the change
Romain Guy2d4cff62010-04-09 15:39:00 -07003627 return ensureTouchModeLocally(inTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003628 }
3629
3630 /**
3631 * Ensure that the touch mode for this window is set, and if it is changing,
3632 * take the appropriate action.
3633 * @param inTouchMode Whether we want to be in touch mode.
3634 * @return True if the touch mode changed and focus changed was changed as a result
3635 */
Romain Guy2d4cff62010-04-09 15:39:00 -07003636 private boolean ensureTouchModeLocally(boolean inTouchMode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003637 if (DBG) Log.d("touchmode", "ensureTouchModeLocally(" + inTouchMode + "), current "
3638 + "touch mode is " + mAttachInfo.mInTouchMode);
3639
3640 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
3641
3642 mAttachInfo.mInTouchMode = inTouchMode;
3643 mAttachInfo.mTreeObserver.dispatchOnTouchModeChanged(inTouchMode);
3644
Romain Guy2d4cff62010-04-09 15:39:00 -07003645 return (inTouchMode) ? enterTouchMode() : leaveTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003646 }
3647
3648 private boolean enterTouchMode() {
Alan Viveretteed7821a2013-07-24 15:49:23 -07003649 if (mView != null && mView.hasFocus()) {
3650 // note: not relying on mFocusedView here because this could
3651 // be when the window is first being added, and mFocused isn't
3652 // set yet.
3653 final View focused = mView.findFocus();
3654 if (focused != null && !focused.isFocusableInTouchMode()) {
3655 final ViewGroup ancestorToTakeFocus = findAncestorToTakeFocusInTouchMode(focused);
3656 if (ancestorToTakeFocus != null) {
3657 // there is an ancestor that wants focus after its
3658 // descendants that is focusable in touch mode.. give it
3659 // focus
3660 return ancestorToTakeFocus.requestFocus();
3661 } else {
Alan Viverette973f3b42013-08-13 16:57:28 -07003662 // There's nothing to focus. Clear and propagate through the
3663 // hierarchy, but don't attempt to place new focus.
Alan Viverette223622a2013-12-17 13:29:02 -08003664 focused.clearFocusInternal(null, true, false);
Alan Viveretteed7821a2013-07-24 15:49:23 -07003665 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003666 }
3667 }
3668 }
3669 return false;
3670 }
3671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003672 /**
3673 * Find an ancestor of focused that wants focus after its descendants and is
3674 * focusable in touch mode.
3675 * @param focused The currently focused view.
3676 * @return An appropriate view, or null if no such view exists.
3677 */
Romain Guya998dff2012-03-23 18:58:36 -07003678 private static ViewGroup findAncestorToTakeFocusInTouchMode(View focused) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003679 ViewParent parent = focused.getParent();
3680 while (parent instanceof ViewGroup) {
3681 final ViewGroup vgParent = (ViewGroup) parent;
3682 if (vgParent.getDescendantFocusability() == ViewGroup.FOCUS_AFTER_DESCENDANTS
3683 && vgParent.isFocusableInTouchMode()) {
3684 return vgParent;
3685 }
3686 if (vgParent.isRootNamespace()) {
3687 return null;
3688 } else {
3689 parent = vgParent.getParent();
3690 }
3691 }
3692 return null;
3693 }
3694
3695 private boolean leaveTouchMode() {
3696 if (mView != null) {
3697 if (mView.hasFocus()) {
Svetoslav Ganov149567f2013-01-08 15:23:34 -08003698 View focusedView = mView.findFocus();
3699 if (!(focusedView instanceof ViewGroup)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003700 // some view has focus, let it keep it
Svetoslav Ganovcf8a3b82012-04-30 16:49:59 -07003701 return false;
Svetoslav Ganov149567f2013-01-08 15:23:34 -08003702 } else if (((ViewGroup) focusedView).getDescendantFocusability() !=
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003703 ViewGroup.FOCUS_AFTER_DESCENDANTS) {
3704 // some view group has focus, and doesn't prefer its children
3705 // over itself for focus, so let them keep it.
Svetoslav Ganovcf8a3b82012-04-30 16:49:59 -07003706 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003707 }
3708 }
Svetoslav Ganovcf8a3b82012-04-30 16:49:59 -07003709
3710 // find the best view to give focus to in this brave new non-touch-mode
3711 // world
3712 final View focused = focusSearch(null, View.FOCUS_DOWN);
3713 if (focused != null) {
3714 return focused.requestFocus(View.FOCUS_DOWN);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003715 }
3716 }
3717 return false;
3718 }
3719
Jeff Brownf9e989d2013-04-04 23:04:03 -07003720 /**
3721 * Base class for implementing a stage in the chain of responsibility
3722 * for processing input events.
3723 * <p>
3724 * Events are delivered to the stage by the {@link #deliver} method. The stage
3725 * then has the choice of finishing the event or forwarding it to the next stage.
3726 * </p>
3727 */
3728 abstract class InputStage {
3729 private final InputStage mNext;
3730
3731 protected static final int FORWARD = 0;
3732 protected static final int FINISH_HANDLED = 1;
3733 protected static final int FINISH_NOT_HANDLED = 2;
3734
3735 /**
3736 * Creates an input stage.
3737 * @param next The next stage to which events should be forwarded.
3738 */
3739 public InputStage(InputStage next) {
3740 mNext = next;
3741 }
3742
3743 /**
3744 * Delivers an event to be processed.
3745 */
3746 public final void deliver(QueuedInputEvent q) {
3747 if ((q.mFlags & QueuedInputEvent.FLAG_FINISHED) != 0) {
3748 forward(q);
Michael Wright17d28ca2013-10-31 17:47:45 -07003749 } else if (shouldDropInputEvent(q)) {
Jeff Brownf9e989d2013-04-04 23:04:03 -07003750 finish(q, false);
3751 } else {
3752 apply(q, onProcess(q));
3753 }
3754 }
3755
3756 /**
3757 * Marks the the input event as finished then forwards it to the next stage.
3758 */
3759 protected void finish(QueuedInputEvent q, boolean handled) {
3760 q.mFlags |= QueuedInputEvent.FLAG_FINISHED;
3761 if (handled) {
3762 q.mFlags |= QueuedInputEvent.FLAG_FINISHED_HANDLED;
3763 }
3764 forward(q);
3765 }
3766
3767 /**
3768 * Forwards the event to the next stage.
3769 */
3770 protected void forward(QueuedInputEvent q) {
3771 onDeliverToNext(q);
3772 }
3773
3774 /**
3775 * Applies a result code from {@link #onProcess} to the specified event.
3776 */
3777 protected void apply(QueuedInputEvent q, int result) {
3778 if (result == FORWARD) {
3779 forward(q);
3780 } else if (result == FINISH_HANDLED) {
3781 finish(q, true);
3782 } else if (result == FINISH_NOT_HANDLED) {
3783 finish(q, false);
3784 } else {
3785 throw new IllegalArgumentException("Invalid result: " + result);
3786 }
3787 }
3788
3789 /**
3790 * Called when an event is ready to be processed.
3791 * @return A result code indicating how the event was handled.
3792 */
3793 protected int onProcess(QueuedInputEvent q) {
3794 return FORWARD;
3795 }
3796
3797 /**
3798 * Called when an event is being delivered to the next stage.
3799 */
3800 protected void onDeliverToNext(QueuedInputEvent q) {
Michael Wright06a79252014-05-05 17:45:29 -07003801 if (DEBUG_INPUT_STAGES) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08003802 Log.v(mTag, "Done with " + getClass().getSimpleName() + ". " + q);
Michael Wright06a79252014-05-05 17:45:29 -07003803 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07003804 if (mNext != null) {
3805 mNext.deliver(q);
3806 } else {
3807 finishInputEvent(q);
3808 }
3809 }
Jeff Brown5182c782013-10-15 20:31:52 -07003810
Michael Wright17d28ca2013-10-31 17:47:45 -07003811 protected boolean shouldDropInputEvent(QueuedInputEvent q) {
3812 if (mView == null || !mAdded) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08003813 Slog.w(mTag, "Dropping event due to root view being removed: " + q.mEvent);
Michael Wright17d28ca2013-10-31 17:47:45 -07003814 return true;
George Mount41725de2015-04-09 08:23:05 -07003815 } else if ((!mAttachInfo.mHasWindowFocus
3816 && !q.mEvent.isFromSource(InputDevice.SOURCE_CLASS_POINTER)) || mStopped
Joe LaPenna90776de2016-01-22 07:11:49 -08003817 || (mIsAmbientMode && !q.mEvent.isFromSource(InputDevice.SOURCE_CLASS_BUTTON))
3818 || (mPausedForTransition && !isBack(q.mEvent))) {
Wale Ogunwalec3672cd2014-11-05 15:17:35 -08003819 // This is a focus event and the window doesn't currently have input focus or
3820 // has stopped. This could be an event that came back from the previous stage
3821 // but the window has lost focus or stopped in the meantime.
3822 if (isTerminalInputEvent(q.mEvent)) {
3823 // Don't drop terminal input events, however mark them as canceled.
3824 q.mEvent.cancel();
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08003825 Slog.w(mTag, "Cancelling event due to no window focus: " + q.mEvent);
Wale Ogunwalec3672cd2014-11-05 15:17:35 -08003826 return false;
3827 }
3828
3829 // Drop non-terminal input events.
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08003830 Slog.w(mTag, "Dropping event due to no window focus: " + q.mEvent);
Michael Wright17d28ca2013-10-31 17:47:45 -07003831 return true;
3832 }
3833 return false;
3834 }
3835
Jeff Brown5182c782013-10-15 20:31:52 -07003836 void dump(String prefix, PrintWriter writer) {
3837 if (mNext != null) {
3838 mNext.dump(prefix, writer);
3839 }
3840 }
George Mount41725de2015-04-09 08:23:05 -07003841
3842 private boolean isBack(InputEvent event) {
3843 if (event instanceof KeyEvent) {
3844 return ((KeyEvent) event).getKeyCode() == KeyEvent.KEYCODE_BACK;
3845 } else {
3846 return false;
3847 }
3848 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07003849 }
3850
3851 /**
3852 * Base class for implementing an input pipeline stage that supports
3853 * asynchronous and out-of-order processing of input events.
3854 * <p>
3855 * In addition to what a normal input stage can do, an asynchronous
3856 * input stage may also defer an input event that has been delivered to it
3857 * and finish or forward it later.
3858 * </p>
3859 */
3860 abstract class AsyncInputStage extends InputStage {
3861 private final String mTraceCounter;
3862
3863 private QueuedInputEvent mQueueHead;
3864 private QueuedInputEvent mQueueTail;
3865 private int mQueueLength;
3866
3867 protected static final int DEFER = 3;
3868
3869 /**
3870 * Creates an asynchronous input stage.
3871 * @param next The next stage to which events should be forwarded.
3872 * @param traceCounter The name of a counter to record the size of
3873 * the queue of pending events.
3874 */
3875 public AsyncInputStage(InputStage next, String traceCounter) {
3876 super(next);
3877 mTraceCounter = traceCounter;
3878 }
3879
3880 /**
3881 * Marks the event as deferred, which is to say that it will be handled
3882 * asynchronously. The caller is responsible for calling {@link #forward}
3883 * or {@link #finish} later when it is done handling the event.
3884 */
3885 protected void defer(QueuedInputEvent q) {
3886 q.mFlags |= QueuedInputEvent.FLAG_DEFERRED;
3887 enqueue(q);
3888 }
3889
3890 @Override
3891 protected void forward(QueuedInputEvent q) {
3892 // Clear the deferred flag.
3893 q.mFlags &= ~QueuedInputEvent.FLAG_DEFERRED;
3894
3895 // Fast path if the queue is empty.
3896 QueuedInputEvent curr = mQueueHead;
3897 if (curr == null) {
3898 super.forward(q);
3899 return;
3900 }
3901
3902 // Determine whether the event must be serialized behind any others
3903 // before it can be delivered to the next stage. This is done because
3904 // deferred events might be handled out of order by the stage.
3905 final int deviceId = q.mEvent.getDeviceId();
3906 QueuedInputEvent prev = null;
3907 boolean blocked = false;
3908 while (curr != null && curr != q) {
3909 if (!blocked && deviceId == curr.mEvent.getDeviceId()) {
3910 blocked = true;
3911 }
3912 prev = curr;
3913 curr = curr.mNext;
3914 }
3915
3916 // If the event is blocked, then leave it in the queue to be delivered later.
3917 // Note that the event might not yet be in the queue if it was not previously
3918 // deferred so we will enqueue it if needed.
3919 if (blocked) {
3920 if (curr == null) {
3921 enqueue(q);
3922 }
3923 return;
3924 }
3925
3926 // The event is not blocked. Deliver it immediately.
3927 if (curr != null) {
3928 curr = curr.mNext;
3929 dequeue(q, prev);
3930 }
3931 super.forward(q);
3932
3933 // Dequeuing this event may have unblocked successors. Deliver them.
3934 while (curr != null) {
3935 if (deviceId == curr.mEvent.getDeviceId()) {
3936 if ((curr.mFlags & QueuedInputEvent.FLAG_DEFERRED) != 0) {
3937 break;
3938 }
3939 QueuedInputEvent next = curr.mNext;
3940 dequeue(curr, prev);
3941 super.forward(curr);
3942 curr = next;
3943 } else {
3944 prev = curr;
3945 curr = curr.mNext;
3946 }
3947 }
3948 }
3949
3950 @Override
3951 protected void apply(QueuedInputEvent q, int result) {
3952 if (result == DEFER) {
3953 defer(q);
3954 } else {
3955 super.apply(q, result);
3956 }
3957 }
3958
3959 private void enqueue(QueuedInputEvent q) {
3960 if (mQueueTail == null) {
3961 mQueueHead = q;
3962 mQueueTail = q;
3963 } else {
3964 mQueueTail.mNext = q;
3965 mQueueTail = q;
3966 }
3967
3968 mQueueLength += 1;
3969 Trace.traceCounter(Trace.TRACE_TAG_INPUT, mTraceCounter, mQueueLength);
3970 }
3971
3972 private void dequeue(QueuedInputEvent q, QueuedInputEvent prev) {
3973 if (prev == null) {
3974 mQueueHead = q.mNext;
3975 } else {
3976 prev.mNext = q.mNext;
3977 }
3978 if (mQueueTail == q) {
3979 mQueueTail = prev;
3980 }
3981 q.mNext = null;
3982
3983 mQueueLength -= 1;
3984 Trace.traceCounter(Trace.TRACE_TAG_INPUT, mTraceCounter, mQueueLength);
3985 }
Jeff Brown5182c782013-10-15 20:31:52 -07003986
3987 @Override
3988 void dump(String prefix, PrintWriter writer) {
3989 writer.print(prefix);
3990 writer.print(getClass().getName());
3991 writer.print(": mQueueLength=");
3992 writer.println(mQueueLength);
3993
3994 super.dump(prefix, writer);
3995 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07003996 }
3997
3998 /**
3999 * Delivers pre-ime input events to a native activity.
4000 * Does not support pointer events.
4001 */
Michael Wrighta44dd262013-04-10 21:12:00 -07004002 final class NativePreImeInputStage extends AsyncInputStage
4003 implements InputQueue.FinishedInputEventCallback {
Jeff Brownf9e989d2013-04-04 23:04:03 -07004004 public NativePreImeInputStage(InputStage next, String traceCounter) {
4005 super(next, traceCounter);
4006 }
4007
4008 @Override
4009 protected int onProcess(QueuedInputEvent q) {
Michael Wrighta44dd262013-04-10 21:12:00 -07004010 if (mInputQueue != null && q.mEvent instanceof KeyEvent) {
4011 mInputQueue.sendInputEvent(q.mEvent, q, true, this);
4012 return DEFER;
4013 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07004014 return FORWARD;
4015 }
Michael Wrighta44dd262013-04-10 21:12:00 -07004016
4017 @Override
4018 public void onFinishedInputEvent(Object token, boolean handled) {
4019 QueuedInputEvent q = (QueuedInputEvent)token;
4020 if (handled) {
4021 finish(q, true);
4022 return;
4023 }
4024 forward(q);
4025 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07004026 }
4027
4028 /**
4029 * Delivers pre-ime input events to the view hierarchy.
4030 * Does not support pointer events.
4031 */
4032 final class ViewPreImeInputStage extends InputStage {
4033 public ViewPreImeInputStage(InputStage next) {
4034 super(next);
4035 }
4036
4037 @Override
4038 protected int onProcess(QueuedInputEvent q) {
Jeff Brown481c1572012-03-09 14:41:15 -08004039 if (q.mEvent instanceof KeyEvent) {
Jeff Brownf9e989d2013-04-04 23:04:03 -07004040 return processKeyEvent(q);
4041 }
4042 return FORWARD;
4043 }
4044
4045 private int processKeyEvent(QueuedInputEvent q) {
4046 final KeyEvent event = (KeyEvent)q.mEvent;
4047 if (mView.dispatchKeyEventPreIme(event)) {
4048 return FINISH_HANDLED;
4049 }
4050 return FORWARD;
4051 }
4052 }
4053
4054 /**
4055 * Delivers input events to the ime.
4056 * Does not support pointer events.
4057 */
4058 final class ImeInputStage extends AsyncInputStage
4059 implements InputMethodManager.FinishedInputEventCallback {
4060 public ImeInputStage(InputStage next, String traceCounter) {
4061 super(next, traceCounter);
4062 }
4063
4064 @Override
4065 protected int onProcess(QueuedInputEvent q) {
keunyoung30f420f2013-08-02 14:23:10 -07004066 if (mLastWasImTarget && !isInLocalFocusMode()) {
Jeff Brownf9e989d2013-04-04 23:04:03 -07004067 InputMethodManager imm = InputMethodManager.peekInstance();
4068 if (imm != null) {
4069 final InputEvent event = q.mEvent;
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08004070 if (DEBUG_IMF) Log.v(mTag, "Sending input event to IME: " + event);
Jeff Brownf9e989d2013-04-04 23:04:03 -07004071 int result = imm.dispatchInputEvent(event, q, this, mHandler);
4072 if (result == InputMethodManager.DISPATCH_HANDLED) {
4073 return FINISH_HANDLED;
4074 } else if (result == InputMethodManager.DISPATCH_NOT_HANDLED) {
Adam Lesinskic0f6eed2013-10-28 16:03:19 -07004075 // The IME could not handle it, so skip along to the next InputStage
4076 return FORWARD;
Jeff Brownf9e989d2013-04-04 23:04:03 -07004077 } else {
4078 return DEFER; // callback will be invoked later
4079 }
4080 }
4081 }
4082 return FORWARD;
4083 }
4084
4085 @Override
4086 public void onFinishedInputEvent(Object token, boolean handled) {
4087 QueuedInputEvent q = (QueuedInputEvent)token;
4088 if (handled) {
4089 finish(q, true);
4090 return;
4091 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07004092 forward(q);
4093 }
4094 }
4095
4096 /**
4097 * Performs early processing of post-ime input events.
4098 */
4099 final class EarlyPostImeInputStage extends InputStage {
4100 public EarlyPostImeInputStage(InputStage next) {
4101 super(next);
4102 }
4103
4104 @Override
4105 protected int onProcess(QueuedInputEvent q) {
4106 if (q.mEvent instanceof KeyEvent) {
4107 return processKeyEvent(q);
Jeff Brown4952dfd2011-11-30 19:23:22 -08004108 } else {
Jeff Brown481c1572012-03-09 14:41:15 -08004109 final int source = q.mEvent.getSource();
4110 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
Jeff Brownf9e989d2013-04-04 23:04:03 -07004111 return processPointerEvent(q);
Jeff Brown481c1572012-03-09 14:41:15 -08004112 }
Jeff Brown4952dfd2011-11-30 19:23:22 -08004113 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07004114 return FORWARD;
4115 }
4116
4117 private int processKeyEvent(QueuedInputEvent q) {
4118 final KeyEvent event = (KeyEvent)q.mEvent;
4119
4120 // If the key's purpose is to exit touch mode then we consume it
4121 // and consider it handled.
4122 if (checkForLeavingTouchModeAndConsume(event)) {
4123 return FINISH_HANDLED;
4124 }
4125
4126 // Make sure the fallback event policy sees all keys that will be
4127 // delivered to the view hierarchy.
4128 mFallbackEventHandler.preDispatchKeyEvent(event);
4129 return FORWARD;
4130 }
4131
4132 private int processPointerEvent(QueuedInputEvent q) {
4133 final MotionEvent event = (MotionEvent)q.mEvent;
4134
4135 // Translate the pointer event for compatibility, if needed.
4136 if (mTranslator != null) {
4137 mTranslator.translateEventInScreenToAppWindow(event);
4138 }
4139
4140 // Enter touch mode on down or scroll.
4141 final int action = event.getAction();
4142 if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_SCROLL) {
4143 ensureTouchMode(true);
4144 }
4145
4146 // Offset the scroll position.
4147 if (mCurScrollY != 0) {
4148 event.offsetLocation(0, mCurScrollY);
4149 }
4150
4151 // Remember the touch position for possible drag-initiation.
4152 if (event.isTouchEvent()) {
4153 mLastTouchPoint.x = event.getRawX();
4154 mLastTouchPoint.y = event.getRawY();
Vladislav Kaznacheevba761122016-01-22 12:09:45 -08004155 mLastTouchSource = event.getSource();
Jeff Brownf9e989d2013-04-04 23:04:03 -07004156 }
4157 return FORWARD;
Jeff Brown4952dfd2011-11-30 19:23:22 -08004158 }
4159 }
4160
Jeff Brownf9e989d2013-04-04 23:04:03 -07004161 /**
4162 * Delivers post-ime input events to a native activity.
4163 */
Michael Wrighta44dd262013-04-10 21:12:00 -07004164 final class NativePostImeInputStage extends AsyncInputStage
4165 implements InputQueue.FinishedInputEventCallback {
Jeff Brownf9e989d2013-04-04 23:04:03 -07004166 public NativePostImeInputStage(InputStage next, String traceCounter) {
4167 super(next, traceCounter);
4168 }
4169
4170 @Override
4171 protected int onProcess(QueuedInputEvent q) {
Michael Wrighta44dd262013-04-10 21:12:00 -07004172 if (mInputQueue != null) {
4173 mInputQueue.sendInputEvent(q.mEvent, q, false, this);
4174 return DEFER;
4175 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07004176 return FORWARD;
4177 }
Michael Wrighta44dd262013-04-10 21:12:00 -07004178
4179 @Override
4180 public void onFinishedInputEvent(Object token, boolean handled) {
4181 QueuedInputEvent q = (QueuedInputEvent)token;
4182 if (handled) {
4183 finish(q, true);
4184 return;
4185 }
4186 forward(q);
4187 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07004188 }
4189
4190 /**
4191 * Delivers post-ime input events to the view hierarchy.
4192 */
4193 final class ViewPostImeInputStage extends InputStage {
4194 public ViewPostImeInputStage(InputStage next) {
4195 super(next);
4196 }
4197
4198 @Override
4199 protected int onProcess(QueuedInputEvent q) {
Jeff Brown29c0ed22013-01-14 13:50:37 -08004200 if (q.mEvent instanceof KeyEvent) {
Jeff Brownf9e989d2013-04-04 23:04:03 -07004201 return processKeyEvent(q);
Jeff Brown29c0ed22013-01-14 13:50:37 -08004202 } else {
4203 final int source = q.mEvent.getSource();
Jeff Brownf9e989d2013-04-04 23:04:03 -07004204 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
4205 return processPointerEvent(q);
4206 } else if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) {
4207 return processTrackballEvent(q);
Jeff Brown29c0ed22013-01-14 13:50:37 -08004208 } else {
Jeff Brownf9e989d2013-04-04 23:04:03 -07004209 return processGenericMotionEvent(q);
Jeff Brown29c0ed22013-01-14 13:50:37 -08004210 }
4211 }
Jeff Brown29c0ed22013-01-14 13:50:37 -08004212 }
Jeff Brown29c0ed22013-01-14 13:50:37 -08004213
Michael Wright9d744c72014-02-18 21:27:42 -08004214 @Override
4215 protected void onDeliverToNext(QueuedInputEvent q) {
4216 if (mUnbufferedInputDispatch
4217 && q.mEvent instanceof MotionEvent
4218 && ((MotionEvent)q.mEvent).isTouchEvent()
4219 && isTerminalInputEvent(q.mEvent)) {
4220 mUnbufferedInputDispatch = false;
4221 scheduleConsumeBatchedInput();
4222 }
4223 super.onDeliverToNext(q);
4224 }
4225
Jeff Brownf9e989d2013-04-04 23:04:03 -07004226 private int processKeyEvent(QueuedInputEvent q) {
4227 final KeyEvent event = (KeyEvent)q.mEvent;
4228
4229 // Deliver the key to the view hierarchy.
4230 if (mView.dispatchKeyEvent(event)) {
4231 return FINISH_HANDLED;
Jeff Brown21bc5c92011-02-28 18:27:14 -08004232 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07004233
Michael Wright17d28ca2013-10-31 17:47:45 -07004234 if (shouldDropInputEvent(q)) {
4235 return FINISH_NOT_HANDLED;
4236 }
4237
Jeff Brownf9e989d2013-04-04 23:04:03 -07004238 // If the Control modifier is held, try to interpret the key as a shortcut.
4239 if (event.getAction() == KeyEvent.ACTION_DOWN
4240 && event.isCtrlPressed()
4241 && event.getRepeatCount() == 0
4242 && !KeyEvent.isModifierKey(event.getKeyCode())) {
4243 if (mView.dispatchKeyShortcutEvent(event)) {
4244 return FINISH_HANDLED;
4245 }
Michael Wright17d28ca2013-10-31 17:47:45 -07004246 if (shouldDropInputEvent(q)) {
4247 return FINISH_NOT_HANDLED;
4248 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07004249 }
4250
4251 // Apply the fallback event policy.
4252 if (mFallbackEventHandler.dispatchKeyEvent(event)) {
4253 return FINISH_HANDLED;
4254 }
Michael Wright17d28ca2013-10-31 17:47:45 -07004255 if (shouldDropInputEvent(q)) {
4256 return FINISH_NOT_HANDLED;
4257 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07004258
4259 // Handle automatic focus changes.
4260 if (event.getAction() == KeyEvent.ACTION_DOWN) {
4261 int direction = 0;
4262 switch (event.getKeyCode()) {
4263 case KeyEvent.KEYCODE_DPAD_LEFT:
4264 if (event.hasNoModifiers()) {
4265 direction = View.FOCUS_LEFT;
4266 }
4267 break;
4268 case KeyEvent.KEYCODE_DPAD_RIGHT:
4269 if (event.hasNoModifiers()) {
4270 direction = View.FOCUS_RIGHT;
4271 }
4272 break;
4273 case KeyEvent.KEYCODE_DPAD_UP:
4274 if (event.hasNoModifiers()) {
4275 direction = View.FOCUS_UP;
4276 }
4277 break;
4278 case KeyEvent.KEYCODE_DPAD_DOWN:
4279 if (event.hasNoModifiers()) {
4280 direction = View.FOCUS_DOWN;
4281 }
4282 break;
4283 case KeyEvent.KEYCODE_TAB:
4284 if (event.hasNoModifiers()) {
4285 direction = View.FOCUS_FORWARD;
4286 } else if (event.hasModifiers(KeyEvent.META_SHIFT_ON)) {
4287 direction = View.FOCUS_BACKWARD;
4288 }
4289 break;
4290 }
4291 if (direction != 0) {
4292 View focused = mView.findFocus();
4293 if (focused != null) {
4294 View v = focused.focusSearch(direction);
4295 if (v != null && v != focused) {
4296 // do the math the get the interesting rect
4297 // of previous focused into the coord system of
4298 // newly focused view
4299 focused.getFocusedRect(mTempRect);
4300 if (mView instanceof ViewGroup) {
4301 ((ViewGroup) mView).offsetDescendantRectToMyCoords(
4302 focused, mTempRect);
4303 ((ViewGroup) mView).offsetRectIntoDescendantCoords(
4304 v, mTempRect);
4305 }
4306 if (v.requestFocus(direction, mTempRect)) {
4307 playSoundEffect(SoundEffectConstants
4308 .getContantForFocusDirection(direction));
4309 return FINISH_HANDLED;
4310 }
4311 }
4312
4313 // Give the focused view a last chance to handle the dpad key.
4314 if (mView.dispatchUnhandledMove(focused, direction)) {
4315 return FINISH_HANDLED;
4316 }
4317 } else {
4318 // find the best view to give focus to in this non-touch-mode with no-focus
4319 View v = focusSearch(null, direction);
4320 if (v != null && v.requestFocus(direction)) {
4321 return FINISH_HANDLED;
4322 }
4323 }
4324 }
4325 }
4326 return FORWARD;
Jeff Brown21bc5c92011-02-28 18:27:14 -08004327 }
4328
Jeff Brownf9e989d2013-04-04 23:04:03 -07004329 private int processPointerEvent(QueuedInputEvent q) {
4330 final MotionEvent event = (MotionEvent)q.mEvent;
4331
Vladislav Kaznacheev527905e2016-02-16 16:20:56 -08004332 mAttachInfo.mUnbufferedDispatchRequested = false;
4333 final View eventTarget =
4334 (event.isFromSource(InputDevice.SOURCE_MOUSE) && mCapturingView != null) ?
4335 mCapturingView : mView;
4336 mAttachInfo.mHandlingPointerEvent = true;
4337 boolean handled = eventTarget.dispatchPointerEvent(event);
4338 maybeUpdatePointerIcon(event);
4339 mAttachInfo.mHandlingPointerEvent = false;
4340 if (mAttachInfo.mUnbufferedDispatchRequested && !mUnbufferedInputDispatch) {
4341 mUnbufferedInputDispatch = true;
4342 if (mConsumeBatchedInputScheduled) {
4343 scheduleConsumeBatchedInputImmediately();
4344 }
4345 }
4346 return handled ? FINISH_HANDLED : FORWARD;
4347 }
4348
4349 private void maybeUpdatePointerIcon(MotionEvent event) {
Jun Mukai1db53972015-09-11 18:08:31 -07004350 if (event.getPointerCount() == 1
4351 && event.isFromSource(InputDevice.SOURCE_MOUSE)) {
4352 if (event.getActionMasked() == MotionEvent.ACTION_HOVER_ENTER
4353 || event.getActionMasked() == MotionEvent.ACTION_HOVER_EXIT) {
4354 // Other apps or the window manager may change the icon shape outside of
4355 // this app, therefore the icon shape has to be reset on enter/exit event.
4356 mPointerIconShape = PointerIcon.STYLE_NOT_SPECIFIED;
4357 }
4358
Vladislav Kaznacheevec6a4472016-01-22 12:21:52 -08004359 if (event.getActionMasked() != MotionEvent.ACTION_HOVER_EXIT) {
4360 if (!updatePointerIcon(event) &&
4361 event.getActionMasked() == MotionEvent.ACTION_HOVER_MOVE) {
4362 mPointerIconShape = PointerIcon.STYLE_NOT_SPECIFIED;
Jun Mukai1db53972015-09-11 18:08:31 -07004363 }
4364 }
4365 }
Jeff Brown3915bb82010-11-05 15:02:16 -07004366 }
4367
Jeff Brownf9e989d2013-04-04 23:04:03 -07004368 private int processTrackballEvent(QueuedInputEvent q) {
4369 final MotionEvent event = (MotionEvent)q.mEvent;
4370
4371 if (mView.dispatchTrackballEvent(event)) {
4372 return FINISH_HANDLED;
4373 }
4374 return FORWARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004375 }
4376
Jeff Brownf9e989d2013-04-04 23:04:03 -07004377 private int processGenericMotionEvent(QueuedInputEvent q) {
4378 final MotionEvent event = (MotionEvent)q.mEvent;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07004379
Jeff Brownf9e989d2013-04-04 23:04:03 -07004380 // Deliver the event to the view.
4381 if (mView.dispatchGenericMotionEvent(event)) {
4382 return FINISH_HANDLED;
4383 }
4384 return FORWARD;
Jeff Brown3915bb82010-11-05 15:02:16 -07004385 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07004386 }
4387
Vladislav Kaznacheevec6a4472016-01-22 12:21:52 -08004388 private void resetPointerIcon(MotionEvent event) {
4389 mPointerIconShape = PointerIcon.STYLE_NOT_SPECIFIED;
4390 updatePointerIcon(event);
4391 }
4392
4393 private boolean updatePointerIcon(MotionEvent event) {
4394 final float x = event.getX();
4395 final float y = event.getY();
4396 if (x < 0 || x >= mView.getWidth() || y < 0 || y >= mView.getHeight()) {
4397 Slog.e(mTag, "updatePointerIcon called with position out of bounds");
4398 return false;
4399 }
4400 final PointerIcon pointerIcon = mView.getPointerIcon(event, x, y);
4401 final int pointerShape = (pointerIcon != null) ?
4402 pointerIcon.getStyle() : PointerIcon.STYLE_DEFAULT;
4403
4404 if (mPointerIconShape != pointerShape) {
4405 mPointerIconShape = pointerShape;
4406 if (mPointerIconShape != PointerIcon.STYLE_CUSTOM) {
4407 mCustomPointerIcon = null;
4408 InputManager.getInstance().setPointerIconShape(pointerShape);
4409 return true;
4410 }
4411 }
4412 if (mPointerIconShape == PointerIcon.STYLE_CUSTOM &&
4413 !pointerIcon.equals(mCustomPointerIcon)) {
4414 mCustomPointerIcon = pointerIcon;
4415 InputManager.getInstance().setCustomPointerIcon(mCustomPointerIcon);
4416 }
4417 return true;
4418 }
4419
Jeff Brownf9e989d2013-04-04 23:04:03 -07004420 /**
Jeff Brown678a1252013-04-09 17:46:25 -07004421 * Performs synthesis of new input events from unhandled input events.
Jeff Brownf9e989d2013-04-04 23:04:03 -07004422 */
4423 final class SyntheticInputStage extends InputStage {
Jeff Brown678a1252013-04-09 17:46:25 -07004424 private final SyntheticTrackballHandler mTrackball = new SyntheticTrackballHandler();
4425 private final SyntheticJoystickHandler mJoystick = new SyntheticJoystickHandler();
4426 private final SyntheticTouchNavigationHandler mTouchNavigation =
4427 new SyntheticTouchNavigationHandler();
Michael Wright899d7052014-04-23 17:23:39 -07004428 private final SyntheticKeyboardHandler mKeyboard = new SyntheticKeyboardHandler();
Jeff Brownf9e989d2013-04-04 23:04:03 -07004429
4430 public SyntheticInputStage() {
4431 super(null);
Jeff Brown21bc5c92011-02-28 18:27:14 -08004432 }
4433
Jeff Brownf9e989d2013-04-04 23:04:03 -07004434 @Override
4435 protected int onProcess(QueuedInputEvent q) {
4436 q.mFlags |= QueuedInputEvent.FLAG_RESYNTHESIZED;
4437 if (q.mEvent instanceof MotionEvent) {
Jeff Brown678a1252013-04-09 17:46:25 -07004438 final MotionEvent event = (MotionEvent)q.mEvent;
4439 final int source = event.getSource();
Jeff Brownf9e989d2013-04-04 23:04:03 -07004440 if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) {
Jeff Brown678a1252013-04-09 17:46:25 -07004441 mTrackball.process(event);
4442 return FINISH_HANDLED;
Jeff Brownf9e989d2013-04-04 23:04:03 -07004443 } else if ((source & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown678a1252013-04-09 17:46:25 -07004444 mJoystick.process(event);
4445 return FINISH_HANDLED;
Jeff Brownf9e989d2013-04-04 23:04:03 -07004446 } else if ((source & InputDevice.SOURCE_TOUCH_NAVIGATION)
4447 == InputDevice.SOURCE_TOUCH_NAVIGATION) {
Jeff Brown678a1252013-04-09 17:46:25 -07004448 mTouchNavigation.process(event);
4449 return FINISH_HANDLED;
Jeff Brownf9e989d2013-04-04 23:04:03 -07004450 }
Michael Wright899d7052014-04-23 17:23:39 -07004451 } else if ((q.mFlags & QueuedInputEvent.FLAG_UNHANDLED) != 0) {
4452 mKeyboard.process((KeyEvent)q.mEvent);
4453 return FINISH_HANDLED;
Jeff Brownf9e989d2013-04-04 23:04:03 -07004454 }
Michael Wright899d7052014-04-23 17:23:39 -07004455
Jeff Brownf9e989d2013-04-04 23:04:03 -07004456 return FORWARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004457 }
4458
Jeff Brownf9e989d2013-04-04 23:04:03 -07004459 @Override
4460 protected void onDeliverToNext(QueuedInputEvent q) {
4461 if ((q.mFlags & QueuedInputEvent.FLAG_RESYNTHESIZED) == 0) {
4462 // Cancel related synthetic events if any prior stage has handled the event.
4463 if (q.mEvent instanceof MotionEvent) {
Jeff Brown678a1252013-04-09 17:46:25 -07004464 final MotionEvent event = (MotionEvent)q.mEvent;
4465 final int source = event.getSource();
Jeff Brownf9e989d2013-04-04 23:04:03 -07004466 if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) {
Jeff Brown678a1252013-04-09 17:46:25 -07004467 mTrackball.cancel(event);
Jeff Brownf9e989d2013-04-04 23:04:03 -07004468 } else if ((source & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown678a1252013-04-09 17:46:25 -07004469 mJoystick.cancel(event);
Jeff Brownf9e989d2013-04-04 23:04:03 -07004470 } else if ((source & InputDevice.SOURCE_TOUCH_NAVIGATION)
4471 == InputDevice.SOURCE_TOUCH_NAVIGATION) {
Jeff Brown678a1252013-04-09 17:46:25 -07004472 mTouchNavigation.cancel(event);
Jeff Brownf9e989d2013-04-04 23:04:03 -07004473 }
4474 }
4475 }
4476 super.onDeliverToNext(q);
4477 }
Jeff Brown678a1252013-04-09 17:46:25 -07004478 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07004479
Jeff Brown678a1252013-04-09 17:46:25 -07004480 /**
4481 * Creates dpad events from unhandled trackball movements.
4482 */
4483 final class SyntheticTrackballHandler {
4484 private final TrackballAxis mX = new TrackballAxis();
4485 private final TrackballAxis mY = new TrackballAxis();
4486 private long mLastTime;
Jeff Brownf9e989d2013-04-04 23:04:03 -07004487
Jeff Brown678a1252013-04-09 17:46:25 -07004488 public void process(MotionEvent event) {
Jeff Brownf9e989d2013-04-04 23:04:03 -07004489 // Translate the trackball event into DPAD keys and try to deliver those.
Jeff Brownf9e989d2013-04-04 23:04:03 -07004490 long curTime = SystemClock.uptimeMillis();
Jeff Brown678a1252013-04-09 17:46:25 -07004491 if ((mLastTime + MAX_TRACKBALL_DELAY) < curTime) {
Jeff Brownf9e989d2013-04-04 23:04:03 -07004492 // It has been too long since the last movement,
4493 // so restart at the beginning.
Jeff Brown678a1252013-04-09 17:46:25 -07004494 mX.reset(0);
4495 mY.reset(0);
4496 mLastTime = curTime;
Jeff Brownf9e989d2013-04-04 23:04:03 -07004497 }
4498
4499 final int action = event.getAction();
4500 final int metaState = event.getMetaState();
4501 switch (action) {
4502 case MotionEvent.ACTION_DOWN:
Jeff Brown678a1252013-04-09 17:46:25 -07004503 mX.reset(2);
4504 mY.reset(2);
Jeff Brownf9e989d2013-04-04 23:04:03 -07004505 enqueueInputEvent(new KeyEvent(curTime, curTime,
4506 KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER, 0, metaState,
4507 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
4508 InputDevice.SOURCE_KEYBOARD));
4509 break;
4510 case MotionEvent.ACTION_UP:
Jeff Brown678a1252013-04-09 17:46:25 -07004511 mX.reset(2);
4512 mY.reset(2);
Jeff Brownf9e989d2013-04-04 23:04:03 -07004513 enqueueInputEvent(new KeyEvent(curTime, curTime,
4514 KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER, 0, metaState,
4515 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
4516 InputDevice.SOURCE_KEYBOARD));
4517 break;
4518 }
4519
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08004520 if (DEBUG_TRACKBALL) Log.v(mTag, "TB X=" + mX.position + " step="
Jeff Brown678a1252013-04-09 17:46:25 -07004521 + mX.step + " dir=" + mX.dir + " acc=" + mX.acceleration
Jeff Brownf9e989d2013-04-04 23:04:03 -07004522 + " move=" + event.getX()
Jeff Brown678a1252013-04-09 17:46:25 -07004523 + " / Y=" + mY.position + " step="
4524 + mY.step + " dir=" + mY.dir + " acc=" + mY.acceleration
Jeff Brownf9e989d2013-04-04 23:04:03 -07004525 + " move=" + event.getY());
Jeff Brown678a1252013-04-09 17:46:25 -07004526 final float xOff = mX.collect(event.getX(), event.getEventTime(), "X");
4527 final float yOff = mY.collect(event.getY(), event.getEventTime(), "Y");
Jeff Brownf9e989d2013-04-04 23:04:03 -07004528
4529 // Generate DPAD events based on the trackball movement.
4530 // We pick the axis that has moved the most as the direction of
4531 // the DPAD. When we generate DPAD events for one axis, then the
4532 // other axis is reset -- we don't want to perform DPAD jumps due
4533 // to slight movements in the trackball when making major movements
4534 // along the other axis.
4535 int keycode = 0;
4536 int movement = 0;
4537 float accel = 1;
4538 if (xOff > yOff) {
Jeff Brown678a1252013-04-09 17:46:25 -07004539 movement = mX.generate();
Jeff Brownf9e989d2013-04-04 23:04:03 -07004540 if (movement != 0) {
4541 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_RIGHT
4542 : KeyEvent.KEYCODE_DPAD_LEFT;
Jeff Brown678a1252013-04-09 17:46:25 -07004543 accel = mX.acceleration;
4544 mY.reset(2);
Jeff Brownf9e989d2013-04-04 23:04:03 -07004545 }
4546 } else if (yOff > 0) {
Jeff Brown678a1252013-04-09 17:46:25 -07004547 movement = mY.generate();
Jeff Brownf9e989d2013-04-04 23:04:03 -07004548 if (movement != 0) {
4549 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_DOWN
4550 : KeyEvent.KEYCODE_DPAD_UP;
Jeff Brown678a1252013-04-09 17:46:25 -07004551 accel = mY.acceleration;
4552 mX.reset(2);
Jeff Brownf9e989d2013-04-04 23:04:03 -07004553 }
4554 }
4555
4556 if (keycode != 0) {
4557 if (movement < 0) movement = -movement;
4558 int accelMovement = (int)(movement * accel);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08004559 if (DEBUG_TRACKBALL) Log.v(mTag, "Move: movement=" + movement
Jeff Brownf9e989d2013-04-04 23:04:03 -07004560 + " accelMovement=" + accelMovement
4561 + " accel=" + accel);
4562 if (accelMovement > movement) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08004563 if (DEBUG_TRACKBALL) Log.v(mTag, "Delivering fake DPAD: "
Jeff Brownf9e989d2013-04-04 23:04:03 -07004564 + keycode);
4565 movement--;
4566 int repeatCount = accelMovement - movement;
4567 enqueueInputEvent(new KeyEvent(curTime, curTime,
4568 KeyEvent.ACTION_MULTIPLE, keycode, repeatCount, metaState,
4569 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
4570 InputDevice.SOURCE_KEYBOARD));
4571 }
4572 while (movement > 0) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08004573 if (DEBUG_TRACKBALL) Log.v(mTag, "Delivering fake DPAD: "
Jeff Brownf9e989d2013-04-04 23:04:03 -07004574 + keycode);
4575 movement--;
4576 curTime = SystemClock.uptimeMillis();
4577 enqueueInputEvent(new KeyEvent(curTime, curTime,
4578 KeyEvent.ACTION_DOWN, keycode, 0, metaState,
4579 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
4580 InputDevice.SOURCE_KEYBOARD));
4581 enqueueInputEvent(new KeyEvent(curTime, curTime,
4582 KeyEvent.ACTION_UP, keycode, 0, metaState,
4583 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
4584 InputDevice.SOURCE_KEYBOARD));
4585 }
Jeff Brown678a1252013-04-09 17:46:25 -07004586 mLastTime = curTime;
Jeff Brownf9e989d2013-04-04 23:04:03 -07004587 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07004588 }
4589
Jeff Brown678a1252013-04-09 17:46:25 -07004590 public void cancel(MotionEvent event) {
4591 mLastTime = Integer.MIN_VALUE;
Jeff Brown3915bb82010-11-05 15:02:16 -07004592
Jeff Brownf9e989d2013-04-04 23:04:03 -07004593 // If we reach this, we consumed a trackball event.
4594 // Because we will not translate the trackball event into a key event,
4595 // touch mode will not exit, so we exit touch mode here.
4596 if (mView != null && mAdded) {
4597 ensureTouchMode(false);
Jeff Brown00fa7bd2010-07-02 15:37:36 -07004598 }
4599 }
Jeff Brown678a1252013-04-09 17:46:25 -07004600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004601
Jeff Brown678a1252013-04-09 17:46:25 -07004602 /**
4603 * Maintains state information for a single trackball axis, generating
4604 * discrete (DPAD) movements based on raw trackball motion.
4605 */
4606 static final class TrackballAxis {
4607 /**
4608 * The maximum amount of acceleration we will apply.
4609 */
4610 static final float MAX_ACCELERATION = 20;
4611
4612 /**
4613 * The maximum amount of time (in milliseconds) between events in order
4614 * for us to consider the user to be doing fast trackball movements,
4615 * and thus apply an acceleration.
4616 */
4617 static final long FAST_MOVE_TIME = 150;
4618
4619 /**
4620 * Scaling factor to the time (in milliseconds) between events to how
4621 * much to multiple/divide the current acceleration. When movement
4622 * is < FAST_MOVE_TIME this multiplies the acceleration; when >
4623 * FAST_MOVE_TIME it divides it.
4624 */
4625 static final float ACCEL_MOVE_SCALING_FACTOR = (1.0f/40);
4626
4627 static final float FIRST_MOVEMENT_THRESHOLD = 0.5f;
4628 static final float SECOND_CUMULATIVE_MOVEMENT_THRESHOLD = 2.0f;
4629 static final float SUBSEQUENT_INCREMENTAL_MOVEMENT_THRESHOLD = 1.0f;
4630
4631 float position;
4632 float acceleration = 1;
4633 long lastMoveTime = 0;
4634 int step;
4635 int dir;
4636 int nonAccelMovement;
4637
4638 void reset(int _step) {
4639 position = 0;
4640 acceleration = 1;
4641 lastMoveTime = 0;
4642 step = _step;
4643 dir = 0;
Jeff Brown6f2fba42011-02-19 01:08:02 -08004644 }
4645
Jeff Brown678a1252013-04-09 17:46:25 -07004646 /**
4647 * Add trackball movement into the state. If the direction of movement
4648 * has been reversed, the state is reset before adding the
4649 * movement (so that you don't have to compensate for any previously
4650 * collected movement before see the result of the movement in the
4651 * new direction).
4652 *
4653 * @return Returns the absolute value of the amount of movement
4654 * collected so far.
4655 */
4656 float collect(float off, long time, String axis) {
4657 long normTime;
4658 if (off > 0) {
4659 normTime = (long)(off * FAST_MOVE_TIME);
4660 if (dir < 0) {
4661 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to positive!");
4662 position = 0;
4663 step = 0;
4664 acceleration = 1;
4665 lastMoveTime = 0;
4666 }
4667 dir = 1;
4668 } else if (off < 0) {
4669 normTime = (long)((-off) * FAST_MOVE_TIME);
4670 if (dir > 0) {
4671 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to negative!");
4672 position = 0;
4673 step = 0;
4674 acceleration = 1;
4675 lastMoveTime = 0;
4676 }
4677 dir = -1;
4678 } else {
4679 normTime = 0;
4680 }
4681
4682 // The number of milliseconds between each movement that is
4683 // considered "normal" and will not result in any acceleration
4684 // or deceleration, scaled by the offset we have here.
4685 if (normTime > 0) {
4686 long delta = time - lastMoveTime;
4687 lastMoveTime = time;
4688 float acc = acceleration;
4689 if (delta < normTime) {
4690 // The user is scrolling rapidly, so increase acceleration.
4691 float scale = (normTime-delta) * ACCEL_MOVE_SCALING_FACTOR;
4692 if (scale > 1) acc *= scale;
4693 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " accelerate: off="
4694 + off + " normTime=" + normTime + " delta=" + delta
4695 + " scale=" + scale + " acc=" + acc);
4696 acceleration = acc < MAX_ACCELERATION ? acc : MAX_ACCELERATION;
4697 } else {
4698 // The user is scrolling slowly, so decrease acceleration.
4699 float scale = (delta-normTime) * ACCEL_MOVE_SCALING_FACTOR;
4700 if (scale > 1) acc /= scale;
4701 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " deccelerate: off="
4702 + off + " normTime=" + normTime + " delta=" + delta
4703 + " scale=" + scale + " acc=" + acc);
4704 acceleration = acc > 1 ? acc : 1;
4705 }
4706 }
4707 position += off;
4708 return Math.abs(position);
Jeff Brown6f2fba42011-02-19 01:08:02 -08004709 }
Jeff Browncb1404e2011-01-15 18:14:15 -08004710
Jeff Brown678a1252013-04-09 17:46:25 -07004711 /**
4712 * Generate the number of discrete movement events appropriate for
4713 * the currently collected trackball movement.
4714 *
4715 * @return Returns the number of discrete movements, either positive
4716 * or negative, or 0 if there is not enough trackball movement yet
4717 * for a discrete movement.
4718 */
4719 int generate() {
4720 int movement = 0;
4721 nonAccelMovement = 0;
4722 do {
4723 final int dir = position >= 0 ? 1 : -1;
4724 switch (step) {
4725 // If we are going to execute the first step, then we want
4726 // to do this as soon as possible instead of waiting for
4727 // a full movement, in order to make things look responsive.
4728 case 0:
4729 if (Math.abs(position) < FIRST_MOVEMENT_THRESHOLD) {
4730 return movement;
4731 }
4732 movement += dir;
4733 nonAccelMovement += dir;
4734 step = 1;
4735 break;
4736 // If we have generated the first movement, then we need
4737 // to wait for the second complete trackball motion before
4738 // generating the second discrete movement.
4739 case 1:
4740 if (Math.abs(position) < SECOND_CUMULATIVE_MOVEMENT_THRESHOLD) {
4741 return movement;
4742 }
4743 movement += dir;
4744 nonAccelMovement += dir;
4745 position -= SECOND_CUMULATIVE_MOVEMENT_THRESHOLD * dir;
4746 step = 2;
4747 break;
4748 // After the first two, we generate discrete movements
4749 // consistently with the trackball, applying an acceleration
4750 // if the trackball is moving quickly. This is a simple
4751 // acceleration on top of what we already compute based
4752 // on how quickly the wheel is being turned, to apply
4753 // a longer increasing acceleration to continuous movement
4754 // in one direction.
4755 default:
4756 if (Math.abs(position) < SUBSEQUENT_INCREMENTAL_MOVEMENT_THRESHOLD) {
4757 return movement;
4758 }
4759 movement += dir;
4760 position -= dir * SUBSEQUENT_INCREMENTAL_MOVEMENT_THRESHOLD;
4761 float acc = acceleration;
4762 acc *= 1.1f;
4763 acceleration = acc < MAX_ACCELERATION ? acc : acceleration;
4764 break;
4765 }
4766 } while (true);
4767 }
4768 }
4769
4770 /**
4771 * Creates dpad events from unhandled joystick movements.
4772 */
4773 final class SyntheticJoystickHandler extends Handler {
Michael Wright9adca062014-03-19 11:51:26 -07004774 private final static String TAG = "SyntheticJoystickHandler";
Jeff Brown678a1252013-04-09 17:46:25 -07004775 private final static int MSG_ENQUEUE_X_AXIS_KEY_REPEAT = 1;
4776 private final static int MSG_ENQUEUE_Y_AXIS_KEY_REPEAT = 2;
4777
4778 private int mLastXDirection;
4779 private int mLastYDirection;
4780 private int mLastXKeyCode;
4781 private int mLastYKeyCode;
4782
4783 public SyntheticJoystickHandler() {
4784 super(true);
4785 }
4786
4787 @Override
4788 public void handleMessage(Message msg) {
4789 switch (msg.what) {
4790 case MSG_ENQUEUE_X_AXIS_KEY_REPEAT:
4791 case MSG_ENQUEUE_Y_AXIS_KEY_REPEAT: {
4792 KeyEvent oldEvent = (KeyEvent)msg.obj;
4793 KeyEvent e = KeyEvent.changeTimeRepeat(oldEvent,
4794 SystemClock.uptimeMillis(),
4795 oldEvent.getRepeatCount() + 1);
4796 if (mAttachInfo.mHasWindowFocus) {
4797 enqueueInputEvent(e);
4798 Message m = obtainMessage(msg.what, e);
4799 m.setAsynchronous(true);
4800 sendMessageDelayed(m, ViewConfiguration.getKeyRepeatDelay());
4801 }
4802 } break;
4803 }
4804 }
4805
4806 public void process(MotionEvent event) {
Michael Wright9adca062014-03-19 11:51:26 -07004807 switch(event.getActionMasked()) {
4808 case MotionEvent.ACTION_CANCEL:
4809 cancel(event);
4810 break;
4811 case MotionEvent.ACTION_MOVE:
4812 update(event, true);
4813 break;
4814 default:
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08004815 Log.w(mTag, "Unexpected action: " + event.getActionMasked());
Michael Wright9adca062014-03-19 11:51:26 -07004816 }
Jeff Brown678a1252013-04-09 17:46:25 -07004817 }
4818
Michael Wright9adca062014-03-19 11:51:26 -07004819 private void cancel(MotionEvent event) {
4820 removeMessages(MSG_ENQUEUE_X_AXIS_KEY_REPEAT);
4821 removeMessages(MSG_ENQUEUE_Y_AXIS_KEY_REPEAT);
Jeff Brown678a1252013-04-09 17:46:25 -07004822 update(event, false);
4823 }
4824
4825 private void update(MotionEvent event, boolean synthesizeNewKeys) {
Jeff Brownf9e989d2013-04-04 23:04:03 -07004826 final long time = event.getEventTime();
4827 final int metaState = event.getMetaState();
4828 final int deviceId = event.getDeviceId();
4829 final int source = event.getSource();
4830
4831 int xDirection = joystickAxisValueToDirection(
4832 event.getAxisValue(MotionEvent.AXIS_HAT_X));
4833 if (xDirection == 0) {
4834 xDirection = joystickAxisValueToDirection(event.getX());
Jeff Browncb1404e2011-01-15 18:14:15 -08004835 }
4836
Jeff Brownf9e989d2013-04-04 23:04:03 -07004837 int yDirection = joystickAxisValueToDirection(
4838 event.getAxisValue(MotionEvent.AXIS_HAT_Y));
4839 if (yDirection == 0) {
4840 yDirection = joystickAxisValueToDirection(event.getY());
4841 }
Jeff Browncb1404e2011-01-15 18:14:15 -08004842
Jeff Brown678a1252013-04-09 17:46:25 -07004843 if (xDirection != mLastXDirection) {
4844 if (mLastXKeyCode != 0) {
4845 removeMessages(MSG_ENQUEUE_X_AXIS_KEY_REPEAT);
Jeff Brownf9e989d2013-04-04 23:04:03 -07004846 enqueueInputEvent(new KeyEvent(time, time,
Jeff Brown678a1252013-04-09 17:46:25 -07004847 KeyEvent.ACTION_UP, mLastXKeyCode, 0, metaState,
Jeff Brownf9e989d2013-04-04 23:04:03 -07004848 deviceId, 0, KeyEvent.FLAG_FALLBACK, source));
Jeff Brown678a1252013-04-09 17:46:25 -07004849 mLastXKeyCode = 0;
Jeff Brownf9e989d2013-04-04 23:04:03 -07004850 }
4851
Jeff Brown678a1252013-04-09 17:46:25 -07004852 mLastXDirection = xDirection;
Jeff Brownf9e989d2013-04-04 23:04:03 -07004853
4854 if (xDirection != 0 && synthesizeNewKeys) {
Jeff Brown678a1252013-04-09 17:46:25 -07004855 mLastXKeyCode = xDirection > 0
Jeff Brownf9e989d2013-04-04 23:04:03 -07004856 ? KeyEvent.KEYCODE_DPAD_RIGHT : KeyEvent.KEYCODE_DPAD_LEFT;
4857 final KeyEvent e = new KeyEvent(time, time,
Jeff Brown678a1252013-04-09 17:46:25 -07004858 KeyEvent.ACTION_DOWN, mLastXKeyCode, 0, metaState,
Jeff Brownf9e989d2013-04-04 23:04:03 -07004859 deviceId, 0, KeyEvent.FLAG_FALLBACK, source);
4860 enqueueInputEvent(e);
Jeff Brown678a1252013-04-09 17:46:25 -07004861 Message m = obtainMessage(MSG_ENQUEUE_X_AXIS_KEY_REPEAT, e);
Jeff Brownf9e989d2013-04-04 23:04:03 -07004862 m.setAsynchronous(true);
Michael Wrightb9618522013-04-10 15:20:56 -07004863 sendMessageDelayed(m, ViewConfiguration.getKeyRepeatTimeout());
Jeff Brownf9e989d2013-04-04 23:04:03 -07004864 }
4865 }
4866
Jeff Brown678a1252013-04-09 17:46:25 -07004867 if (yDirection != mLastYDirection) {
4868 if (mLastYKeyCode != 0) {
4869 removeMessages(MSG_ENQUEUE_Y_AXIS_KEY_REPEAT);
Jeff Brownf9e989d2013-04-04 23:04:03 -07004870 enqueueInputEvent(new KeyEvent(time, time,
Jeff Brown678a1252013-04-09 17:46:25 -07004871 KeyEvent.ACTION_UP, mLastYKeyCode, 0, metaState,
Jeff Brownf9e989d2013-04-04 23:04:03 -07004872 deviceId, 0, KeyEvent.FLAG_FALLBACK, source));
Jeff Brown678a1252013-04-09 17:46:25 -07004873 mLastYKeyCode = 0;
Jeff Brownf9e989d2013-04-04 23:04:03 -07004874 }
4875
Jeff Brown678a1252013-04-09 17:46:25 -07004876 mLastYDirection = yDirection;
Jeff Brownf9e989d2013-04-04 23:04:03 -07004877
4878 if (yDirection != 0 && synthesizeNewKeys) {
Jeff Brown678a1252013-04-09 17:46:25 -07004879 mLastYKeyCode = yDirection > 0
Jeff Brownf9e989d2013-04-04 23:04:03 -07004880 ? KeyEvent.KEYCODE_DPAD_DOWN : KeyEvent.KEYCODE_DPAD_UP;
4881 final KeyEvent e = new KeyEvent(time, time,
Jeff Brown678a1252013-04-09 17:46:25 -07004882 KeyEvent.ACTION_DOWN, mLastYKeyCode, 0, metaState,
Jeff Brownf9e989d2013-04-04 23:04:03 -07004883 deviceId, 0, KeyEvent.FLAG_FALLBACK, source);
4884 enqueueInputEvent(e);
Jeff Brown678a1252013-04-09 17:46:25 -07004885 Message m = obtainMessage(MSG_ENQUEUE_Y_AXIS_KEY_REPEAT, e);
Jeff Brownf9e989d2013-04-04 23:04:03 -07004886 m.setAsynchronous(true);
Jeff Brown678a1252013-04-09 17:46:25 -07004887 sendMessageDelayed(m, ViewConfiguration.getKeyRepeatTimeout());
Jeff Brownf9e989d2013-04-04 23:04:03 -07004888 }
Jeff Browncb1404e2011-01-15 18:14:15 -08004889 }
4890 }
4891
Jeff Brownf9e989d2013-04-04 23:04:03 -07004892 private int joystickAxisValueToDirection(float value) {
4893 if (value >= 0.5f) {
4894 return 1;
4895 } else if (value <= -0.5f) {
4896 return -1;
4897 } else {
4898 return 0;
Jeff Browncb1404e2011-01-15 18:14:15 -08004899 }
4900 }
Jeff Brown678a1252013-04-09 17:46:25 -07004901 }
Jeff Browncb1404e2011-01-15 18:14:15 -08004902
Jeff Brown678a1252013-04-09 17:46:25 -07004903 /**
4904 * Creates dpad events from unhandled touch navigation movements.
4905 */
4906 final class SyntheticTouchNavigationHandler extends Handler {
Jeff Brown4dac9012013-04-10 01:03:19 -07004907 private static final String LOCAL_TAG = "SyntheticTouchNavigationHandler";
4908 private static final boolean LOCAL_DEBUG = false;
Jeff Brown678a1252013-04-09 17:46:25 -07004909
Jeff Brown4dac9012013-04-10 01:03:19 -07004910 // Assumed nominal width and height in millimeters of a touch navigation pad,
4911 // if no resolution information is available from the input system.
4912 private static final float DEFAULT_WIDTH_MILLIMETERS = 48;
4913 private static final float DEFAULT_HEIGHT_MILLIMETERS = 48;
Jeff Brown678a1252013-04-09 17:46:25 -07004914
Jeff Brown4dac9012013-04-10 01:03:19 -07004915 /* TODO: These constants should eventually be moved to ViewConfiguration. */
Jeff Brown678a1252013-04-09 17:46:25 -07004916
Jeff Brown4dac9012013-04-10 01:03:19 -07004917 // The nominal distance traveled to move by one unit.
4918 private static final int TICK_DISTANCE_MILLIMETERS = 12;
4919
4920 // Minimum and maximum fling velocity in ticks per second.
4921 // The minimum velocity should be set such that we perform enough ticks per
4922 // second that the fling appears to be fluid. For example, if we set the minimum
4923 // to 2 ticks per second, then there may be up to half a second delay between the next
4924 // to last and last ticks which is noticeably discrete and jerky. This value should
4925 // probably not be set to anything less than about 4.
4926 // If fling accuracy is a problem then consider tuning the tick distance instead.
4927 private static final float MIN_FLING_VELOCITY_TICKS_PER_SECOND = 6f;
4928 private static final float MAX_FLING_VELOCITY_TICKS_PER_SECOND = 20f;
4929
4930 // Fling velocity decay factor applied after each new key is emitted.
4931 // This parameter controls the deceleration and overall duration of the fling.
4932 // The fling stops automatically when its velocity drops below the minimum
4933 // fling velocity defined above.
4934 private static final float FLING_TICK_DECAY = 0.8f;
4935
4936 /* The input device that we are tracking. */
4937
4938 private int mCurrentDeviceId = -1;
4939 private int mCurrentSource;
4940 private boolean mCurrentDeviceSupported;
4941
4942 /* Configuration for the current input device. */
4943
Jeff Brown4dac9012013-04-10 01:03:19 -07004944 // The scaled tick distance. A movement of this amount should generally translate
4945 // into a single dpad event in a given direction.
4946 private float mConfigTickDistance;
4947
4948 // The minimum and maximum scaled fling velocity.
4949 private float mConfigMinFlingVelocity;
4950 private float mConfigMaxFlingVelocity;
4951
4952 /* Tracking state. */
4953
4954 // The velocity tracker for detecting flings.
4955 private VelocityTracker mVelocityTracker;
4956
4957 // The active pointer id, or -1 if none.
4958 private int mActivePointerId = -1;
4959
John Reck79d81e62013-11-05 13:26:57 -08004960 // Location where tracking started.
Jeff Brown4dac9012013-04-10 01:03:19 -07004961 private float mStartX;
4962 private float mStartY;
4963
4964 // Most recently observed position.
4965 private float mLastX;
4966 private float mLastY;
4967
4968 // Accumulated movement delta since the last direction key was sent.
Jeff Brown678a1252013-04-09 17:46:25 -07004969 private float mAccumulatedX;
4970 private float mAccumulatedY;
4971
Jeff Brown4dac9012013-04-10 01:03:19 -07004972 // Set to true if any movement was delivered to the app.
4973 // Implies that tap slop was exceeded.
4974 private boolean mConsumedMovement;
Jeff Brown678a1252013-04-09 17:46:25 -07004975
Jeff Brown4dac9012013-04-10 01:03:19 -07004976 // The most recently sent key down event.
4977 // The keycode remains set until the direction changes or a fling ends
4978 // so that repeated key events may be generated as required.
4979 private long mPendingKeyDownTime;
4980 private int mPendingKeyCode = KeyEvent.KEYCODE_UNKNOWN;
4981 private int mPendingKeyRepeatCount;
4982 private int mPendingKeyMetaState;
Jeff Brown678a1252013-04-09 17:46:25 -07004983
Jeff Brown4dac9012013-04-10 01:03:19 -07004984 // The current fling velocity while a fling is in progress.
4985 private boolean mFlinging;
4986 private float mFlingVelocity;
Jeff Brown678a1252013-04-09 17:46:25 -07004987
4988 public SyntheticTouchNavigationHandler() {
4989 super(true);
Jeff Brown678a1252013-04-09 17:46:25 -07004990 }
4991
4992 public void process(MotionEvent event) {
Jeff Brown4dac9012013-04-10 01:03:19 -07004993 // Update the current device information.
4994 final long time = event.getEventTime();
4995 final int deviceId = event.getDeviceId();
4996 final int source = event.getSource();
4997 if (mCurrentDeviceId != deviceId || mCurrentSource != source) {
4998 finishKeys(time);
4999 finishTracking(time);
5000 mCurrentDeviceId = deviceId;
5001 mCurrentSource = source;
5002 mCurrentDeviceSupported = false;
5003 InputDevice device = event.getDevice();
5004 if (device != null) {
5005 // In order to support an input device, we must know certain
5006 // characteristics about it, such as its size and resolution.
5007 InputDevice.MotionRange xRange = device.getMotionRange(MotionEvent.AXIS_X);
5008 InputDevice.MotionRange yRange = device.getMotionRange(MotionEvent.AXIS_Y);
5009 if (xRange != null && yRange != null) {
5010 mCurrentDeviceSupported = true;
Jeff Brown678a1252013-04-09 17:46:25 -07005011
Jeff Brown4dac9012013-04-10 01:03:19 -07005012 // Infer the resolution if it not actually known.
5013 float xRes = xRange.getResolution();
5014 if (xRes <= 0) {
5015 xRes = xRange.getRange() / DEFAULT_WIDTH_MILLIMETERS;
5016 }
5017 float yRes = yRange.getResolution();
5018 if (yRes <= 0) {
5019 yRes = yRange.getRange() / DEFAULT_HEIGHT_MILLIMETERS;
5020 }
5021 float nominalRes = (xRes + yRes) * 0.5f;
Jeff Brown678a1252013-04-09 17:46:25 -07005022
Jeff Brown4dac9012013-04-10 01:03:19 -07005023 // Precompute all of the configuration thresholds we will need.
Jeff Brown4dac9012013-04-10 01:03:19 -07005024 mConfigTickDistance = TICK_DISTANCE_MILLIMETERS * nominalRes;
5025 mConfigMinFlingVelocity =
5026 MIN_FLING_VELOCITY_TICKS_PER_SECOND * mConfigTickDistance;
5027 mConfigMaxFlingVelocity =
5028 MAX_FLING_VELOCITY_TICKS_PER_SECOND * mConfigTickDistance;
5029
5030 if (LOCAL_DEBUG) {
5031 Log.d(LOCAL_TAG, "Configured device " + mCurrentDeviceId
5032 + " (" + Integer.toHexString(mCurrentSource) + "): "
Jeff Brown4dac9012013-04-10 01:03:19 -07005033 + ", mConfigTickDistance=" + mConfigTickDistance
5034 + ", mConfigMinFlingVelocity=" + mConfigMinFlingVelocity
5035 + ", mConfigMaxFlingVelocity=" + mConfigMaxFlingVelocity);
5036 }
5037 }
5038 }
Jeff Brown678a1252013-04-09 17:46:25 -07005039 }
Jeff Brown4dac9012013-04-10 01:03:19 -07005040 if (!mCurrentDeviceSupported) {
Jeff Brown678a1252013-04-09 17:46:25 -07005041 return;
5042 }
5043
Jeff Brown4dac9012013-04-10 01:03:19 -07005044 // Handle the event.
5045 final int action = event.getActionMasked();
5046 switch (action) {
Jeff Brown678a1252013-04-09 17:46:25 -07005047 case MotionEvent.ACTION_DOWN: {
Jeff Brown4dac9012013-04-10 01:03:19 -07005048 boolean caughtFling = mFlinging;
5049 finishKeys(time);
5050 finishTracking(time);
5051 mActivePointerId = event.getPointerId(0);
5052 mVelocityTracker = VelocityTracker.obtain();
5053 mVelocityTracker.addMovement(event);
Jeff Brown4dac9012013-04-10 01:03:19 -07005054 mStartX = event.getX();
5055 mStartY = event.getY();
5056 mLastX = mStartX;
5057 mLastY = mStartY;
Jeff Brown678a1252013-04-09 17:46:25 -07005058 mAccumulatedX = 0;
5059 mAccumulatedY = 0;
Jeff Brown4dac9012013-04-10 01:03:19 -07005060
5061 // If we caught a fling, then pretend that the tap slop has already
5062 // been exceeded to suppress taps whose only purpose is to stop the fling.
5063 mConsumedMovement = caughtFling;
Jeff Brown678a1252013-04-09 17:46:25 -07005064 break;
5065 }
5066
Jeff Brown4dac9012013-04-10 01:03:19 -07005067 case MotionEvent.ACTION_MOVE:
Jeff Brown678a1252013-04-09 17:46:25 -07005068 case MotionEvent.ACTION_UP: {
Jeff Brown4dac9012013-04-10 01:03:19 -07005069 if (mActivePointerId < 0) {
5070 break;
5071 }
5072 final int index = event.findPointerIndex(mActivePointerId);
5073 if (index < 0) {
5074 finishKeys(time);
5075 finishTracking(time);
5076 break;
5077 }
Jeff Brown678a1252013-04-09 17:46:25 -07005078
Jeff Brown4dac9012013-04-10 01:03:19 -07005079 mVelocityTracker.addMovement(event);
5080 final float x = event.getX(index);
5081 final float y = event.getY(index);
5082 mAccumulatedX += x - mLastX;
5083 mAccumulatedY += y - mLastY;
5084 mLastX = x;
5085 mLastY = y;
5086
5087 // Consume any accumulated movement so far.
5088 final int metaState = event.getMetaState();
5089 consumeAccumulatedMovement(time, metaState);
5090
5091 // Detect taps and flings.
5092 if (action == MotionEvent.ACTION_UP) {
Michael Wright88d7f792013-08-27 15:45:42 -07005093 if (mConsumedMovement && mPendingKeyCode != KeyEvent.KEYCODE_UNKNOWN) {
Jeff Brown4dac9012013-04-10 01:03:19 -07005094 // It might be a fling.
5095 mVelocityTracker.computeCurrentVelocity(1000, mConfigMaxFlingVelocity);
5096 final float vx = mVelocityTracker.getXVelocity(mActivePointerId);
5097 final float vy = mVelocityTracker.getYVelocity(mActivePointerId);
5098 if (!startFling(time, vx, vy)) {
5099 finishKeys(time);
Jeff Brown678a1252013-04-09 17:46:25 -07005100 }
5101 }
Jeff Brown4dac9012013-04-10 01:03:19 -07005102 finishTracking(time);
Jeff Brown678a1252013-04-09 17:46:25 -07005103 }
Jeff Brown4dac9012013-04-10 01:03:19 -07005104 break;
5105 }
5106
5107 case MotionEvent.ACTION_CANCEL: {
5108 finishKeys(time);
5109 finishTracking(time);
Jeff Brown678a1252013-04-09 17:46:25 -07005110 break;
5111 }
5112 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005113 }
Jeff Brown4dac9012013-04-10 01:03:19 -07005114
5115 public void cancel(MotionEvent event) {
5116 if (mCurrentDeviceId == event.getDeviceId()
5117 && mCurrentSource == event.getSource()) {
5118 final long time = event.getEventTime();
5119 finishKeys(time);
5120 finishTracking(time);
5121 }
5122 }
5123
5124 private void finishKeys(long time) {
5125 cancelFling();
5126 sendKeyUp(time);
5127 }
5128
5129 private void finishTracking(long time) {
5130 if (mActivePointerId >= 0) {
5131 mActivePointerId = -1;
5132 mVelocityTracker.recycle();
5133 mVelocityTracker = null;
5134 }
5135 }
5136
5137 private void consumeAccumulatedMovement(long time, int metaState) {
5138 final float absX = Math.abs(mAccumulatedX);
5139 final float absY = Math.abs(mAccumulatedY);
5140 if (absX >= absY) {
5141 if (absX >= mConfigTickDistance) {
5142 mAccumulatedX = consumeAccumulatedMovement(time, metaState, mAccumulatedX,
5143 KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_RIGHT);
5144 mAccumulatedY = 0;
5145 mConsumedMovement = true;
5146 }
5147 } else {
5148 if (absY >= mConfigTickDistance) {
5149 mAccumulatedY = consumeAccumulatedMovement(time, metaState, mAccumulatedY,
5150 KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_DOWN);
5151 mAccumulatedX = 0;
5152 mConsumedMovement = true;
5153 }
5154 }
5155 }
5156
5157 private float consumeAccumulatedMovement(long time, int metaState,
5158 float accumulator, int negativeKeyCode, int positiveKeyCode) {
5159 while (accumulator <= -mConfigTickDistance) {
5160 sendKeyDownOrRepeat(time, negativeKeyCode, metaState);
5161 accumulator += mConfigTickDistance;
5162 }
5163 while (accumulator >= mConfigTickDistance) {
5164 sendKeyDownOrRepeat(time, positiveKeyCode, metaState);
5165 accumulator -= mConfigTickDistance;
5166 }
5167 return accumulator;
5168 }
5169
5170 private void sendKeyDownOrRepeat(long time, int keyCode, int metaState) {
5171 if (mPendingKeyCode != keyCode) {
5172 sendKeyUp(time);
5173 mPendingKeyDownTime = time;
5174 mPendingKeyCode = keyCode;
5175 mPendingKeyRepeatCount = 0;
5176 } else {
5177 mPendingKeyRepeatCount += 1;
5178 }
5179 mPendingKeyMetaState = metaState;
5180
5181 // Note: Normally we would pass FLAG_LONG_PRESS when the repeat count is 1
5182 // but it doesn't quite make sense when simulating the events in this way.
5183 if (LOCAL_DEBUG) {
5184 Log.d(LOCAL_TAG, "Sending key down: keyCode=" + mPendingKeyCode
5185 + ", repeatCount=" + mPendingKeyRepeatCount
5186 + ", metaState=" + Integer.toHexString(mPendingKeyMetaState));
5187 }
5188 enqueueInputEvent(new KeyEvent(mPendingKeyDownTime, time,
5189 KeyEvent.ACTION_DOWN, mPendingKeyCode, mPendingKeyRepeatCount,
5190 mPendingKeyMetaState, mCurrentDeviceId,
5191 KeyEvent.FLAG_FALLBACK, mCurrentSource));
5192 }
5193
5194 private void sendKeyUp(long time) {
5195 if (mPendingKeyCode != KeyEvent.KEYCODE_UNKNOWN) {
5196 if (LOCAL_DEBUG) {
5197 Log.d(LOCAL_TAG, "Sending key up: keyCode=" + mPendingKeyCode
5198 + ", metaState=" + Integer.toHexString(mPendingKeyMetaState));
5199 }
5200 enqueueInputEvent(new KeyEvent(mPendingKeyDownTime, time,
5201 KeyEvent.ACTION_UP, mPendingKeyCode, 0, mPendingKeyMetaState,
5202 mCurrentDeviceId, 0, KeyEvent.FLAG_FALLBACK,
5203 mCurrentSource));
5204 mPendingKeyCode = KeyEvent.KEYCODE_UNKNOWN;
5205 }
5206 }
5207
5208 private boolean startFling(long time, float vx, float vy) {
5209 if (LOCAL_DEBUG) {
5210 Log.d(LOCAL_TAG, "Considering fling: vx=" + vx + ", vy=" + vy
5211 + ", min=" + mConfigMinFlingVelocity);
5212 }
5213
5214 // Flings must be oriented in the same direction as the preceding movements.
5215 switch (mPendingKeyCode) {
5216 case KeyEvent.KEYCODE_DPAD_LEFT:
5217 if (-vx >= mConfigMinFlingVelocity
5218 && Math.abs(vy) < mConfigMinFlingVelocity) {
5219 mFlingVelocity = -vx;
5220 break;
5221 }
5222 return false;
5223
5224 case KeyEvent.KEYCODE_DPAD_RIGHT:
5225 if (vx >= mConfigMinFlingVelocity
5226 && Math.abs(vy) < mConfigMinFlingVelocity) {
5227 mFlingVelocity = vx;
5228 break;
5229 }
5230 return false;
5231
5232 case KeyEvent.KEYCODE_DPAD_UP:
5233 if (-vy >= mConfigMinFlingVelocity
5234 && Math.abs(vx) < mConfigMinFlingVelocity) {
5235 mFlingVelocity = -vy;
5236 break;
5237 }
5238 return false;
5239
5240 case KeyEvent.KEYCODE_DPAD_DOWN:
5241 if (vy >= mConfigMinFlingVelocity
5242 && Math.abs(vx) < mConfigMinFlingVelocity) {
5243 mFlingVelocity = vy;
5244 break;
5245 }
5246 return false;
5247 }
5248
5249 // Post the first fling event.
5250 mFlinging = postFling(time);
5251 return mFlinging;
5252 }
5253
5254 private boolean postFling(long time) {
5255 // The idea here is to estimate the time when the pointer would have
5256 // traveled one tick distance unit given the current fling velocity.
5257 // This effect creates continuity of motion.
5258 if (mFlingVelocity >= mConfigMinFlingVelocity) {
5259 long delay = (long)(mConfigTickDistance / mFlingVelocity * 1000);
5260 postAtTime(mFlingRunnable, time + delay);
5261 if (LOCAL_DEBUG) {
5262 Log.d(LOCAL_TAG, "Posted fling: velocity="
5263 + mFlingVelocity + ", delay=" + delay
5264 + ", keyCode=" + mPendingKeyCode);
5265 }
5266 return true;
5267 }
5268 return false;
5269 }
5270
5271 private void cancelFling() {
5272 if (mFlinging) {
5273 removeCallbacks(mFlingRunnable);
5274 mFlinging = false;
5275 }
5276 }
5277
5278 private final Runnable mFlingRunnable = new Runnable() {
5279 @Override
5280 public void run() {
5281 final long time = SystemClock.uptimeMillis();
5282 sendKeyDownOrRepeat(time, mPendingKeyCode, mPendingKeyMetaState);
5283 mFlingVelocity *= FLING_TICK_DECAY;
5284 if (!postFling(time)) {
5285 mFlinging = false;
5286 finishKeys(time);
5287 }
5288 }
5289 };
Jeff Browncb1404e2011-01-15 18:14:15 -08005290 }
5291
Michael Wright899d7052014-04-23 17:23:39 -07005292 final class SyntheticKeyboardHandler {
5293 public void process(KeyEvent event) {
5294 if ((event.getFlags() & KeyEvent.FLAG_FALLBACK) != 0) {
5295 return;
5296 }
5297
5298 final KeyCharacterMap kcm = event.getKeyCharacterMap();
5299 final int keyCode = event.getKeyCode();
5300 final int metaState = event.getMetaState();
5301
5302 // Check for fallback actions specified by the key character map.
5303 KeyCharacterMap.FallbackAction fallbackAction =
5304 kcm.getFallbackAction(keyCode, metaState);
5305 if (fallbackAction != null) {
5306 final int flags = event.getFlags() | KeyEvent.FLAG_FALLBACK;
5307 KeyEvent fallbackEvent = KeyEvent.obtain(
5308 event.getDownTime(), event.getEventTime(),
5309 event.getAction(), fallbackAction.keyCode,
5310 event.getRepeatCount(), fallbackAction.metaState,
5311 event.getDeviceId(), event.getScanCode(),
5312 flags, event.getSource(), null);
5313 fallbackAction.recycle();
5314 enqueueInputEvent(fallbackEvent);
5315 }
5316 }
5317 }
5318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005319 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005320 * Returns true if the key is used for keyboard navigation.
5321 * @param keyEvent The key event.
5322 * @return True if the key is used for keyboard navigation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005323 */
Jeff Brown4e6319b2010-12-13 10:36:51 -08005324 private static boolean isNavigationKey(KeyEvent keyEvent) {
5325 switch (keyEvent.getKeyCode()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005326 case KeyEvent.KEYCODE_DPAD_LEFT:
5327 case KeyEvent.KEYCODE_DPAD_RIGHT:
5328 case KeyEvent.KEYCODE_DPAD_UP:
5329 case KeyEvent.KEYCODE_DPAD_DOWN:
Jeff Brown4e6319b2010-12-13 10:36:51 -08005330 case KeyEvent.KEYCODE_DPAD_CENTER:
5331 case KeyEvent.KEYCODE_PAGE_UP:
5332 case KeyEvent.KEYCODE_PAGE_DOWN:
5333 case KeyEvent.KEYCODE_MOVE_HOME:
5334 case KeyEvent.KEYCODE_MOVE_END:
5335 case KeyEvent.KEYCODE_TAB:
5336 case KeyEvent.KEYCODE_SPACE:
5337 case KeyEvent.KEYCODE_ENTER:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005338 return true;
5339 }
5340 return false;
5341 }
5342
5343 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005344 * Returns true if the key is used for typing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005345 * @param keyEvent The key event.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005346 * @return True if the key is used for typing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005347 */
Jeff Brown4e6319b2010-12-13 10:36:51 -08005348 private static boolean isTypingKey(KeyEvent keyEvent) {
5349 return keyEvent.getUnicodeChar() > 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005350 }
5351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005352 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005353 * See if the key event means we should leave touch mode (and leave touch mode if so).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005354 * @param event The key event.
5355 * @return Whether this key event should be consumed (meaning the act of
5356 * leaving touch mode alone is considered the event).
5357 */
5358 private boolean checkForLeavingTouchModeAndConsume(KeyEvent event) {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005359 // Only relevant in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005360 if (!mAttachInfo.mInTouchMode) {
5361 return false;
5362 }
5363
Jeff Brown4e6319b2010-12-13 10:36:51 -08005364 // Only consider leaving touch mode on DOWN or MULTIPLE actions, never on UP.
5365 final int action = event.getAction();
5366 if (action != KeyEvent.ACTION_DOWN && action != KeyEvent.ACTION_MULTIPLE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005367 return false;
5368 }
5369
Jeff Brown4e6319b2010-12-13 10:36:51 -08005370 // Don't leave touch mode if the IME told us not to.
5371 if ((event.getFlags() & KeyEvent.FLAG_KEEP_TOUCH_MODE) != 0) {
5372 return false;
5373 }
5374
5375 // If the key can be used for keyboard navigation then leave touch mode
5376 // and select a focused view if needed (in ensureTouchMode).
5377 // When a new focused view is selected, we consume the navigation key because
5378 // navigation doesn't make much sense unless a view already has focus so
5379 // the key's purpose is to set focus.
5380 if (isNavigationKey(event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005381 return ensureTouchMode(false);
5382 }
Jeff Brown4e6319b2010-12-13 10:36:51 -08005383
5384 // If the key can be used for typing then leave touch mode
5385 // and select a focused view if needed (in ensureTouchMode).
5386 // Always allow the view to process the typing key.
5387 if (isTypingKey(event)) {
5388 ensureTouchMode(false);
5389 return false;
5390 }
5391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005392 return false;
5393 }
5394
Christopher Tatea53146c2010-09-07 11:57:52 -07005395 /* drag/drop */
Christopher Tate407b4e92010-11-30 17:14:08 -08005396 void setLocalDragState(Object obj) {
5397 mLocalDragState = obj;
5398 }
5399
Christopher Tatea53146c2010-09-07 11:57:52 -07005400 private void handleDragEvent(DragEvent event) {
5401 // From the root, only drag start/end/location are dispatched. entered/exited
5402 // are determined and dispatched by the viewgroup hierarchy, who then report
5403 // that back here for ultimate reporting back to the framework.
5404 if (mView != null && mAdded) {
5405 final int what = event.mAction;
5406
5407 if (what == DragEvent.ACTION_DRAG_EXITED) {
5408 // A direct EXITED event means that the window manager knows we've just crossed
5409 // a window boundary, so the current drag target within this one must have
5410 // just been exited. Send it the usual notifications and then we're done
5411 // for now.
Chris Tate9d1ab882010-11-02 15:55:39 -07005412 mView.dispatchDragEvent(event);
Christopher Tatea53146c2010-09-07 11:57:52 -07005413 } else {
5414 // Cache the drag description when the operation starts, then fill it in
5415 // on subsequent calls as a convenience
5416 if (what == DragEvent.ACTION_DRAG_STARTED) {
Chris Tate9d1ab882010-11-02 15:55:39 -07005417 mCurrentDragView = null; // Start the current-recipient tracking
Christopher Tatea53146c2010-09-07 11:57:52 -07005418 mDragDescription = event.mClipDescription;
5419 } else {
5420 event.mClipDescription = mDragDescription;
5421 }
5422
5423 // For events with a [screen] location, translate into window coordinates
5424 if ((what == DragEvent.ACTION_DRAG_LOCATION) || (what == DragEvent.ACTION_DROP)) {
5425 mDragPoint.set(event.mX, event.mY);
5426 if (mTranslator != null) {
5427 mTranslator.translatePointInScreenToAppWindow(mDragPoint);
5428 }
5429
5430 if (mCurScrollY != 0) {
5431 mDragPoint.offset(0, mCurScrollY);
5432 }
5433
5434 event.mX = mDragPoint.x;
5435 event.mY = mDragPoint.y;
5436 }
5437
5438 // Remember who the current drag target is pre-dispatch
5439 final View prevDragView = mCurrentDragView;
5440
5441 // Now dispatch the drag/drop event
Chris Tated4533f12010-10-19 15:15:08 -07005442 boolean result = mView.dispatchDragEvent(event);
Christopher Tatea53146c2010-09-07 11:57:52 -07005443
5444 // If we changed apparent drag target, tell the OS about it
5445 if (prevDragView != mCurrentDragView) {
5446 try {
5447 if (prevDragView != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07005448 mWindowSession.dragRecipientExited(mWindow);
Christopher Tatea53146c2010-09-07 11:57:52 -07005449 }
5450 if (mCurrentDragView != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07005451 mWindowSession.dragRecipientEntered(mWindow);
Christopher Tatea53146c2010-09-07 11:57:52 -07005452 }
5453 } catch (RemoteException e) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08005454 Slog.e(mTag, "Unable to note drag target change");
Christopher Tatea53146c2010-09-07 11:57:52 -07005455 }
Christopher Tatea53146c2010-09-07 11:57:52 -07005456 }
Chris Tated4533f12010-10-19 15:15:08 -07005457
Christopher Tate407b4e92010-11-30 17:14:08 -08005458 // Report the drop result when we're done
Chris Tated4533f12010-10-19 15:15:08 -07005459 if (what == DragEvent.ACTION_DROP) {
Christopher Tate1fc014f2011-01-19 12:56:26 -08005460 mDragDescription = null;
Chris Tated4533f12010-10-19 15:15:08 -07005461 try {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08005462 Log.i(mTag, "Reporting drop result: " + result);
Jeff Brown98365d72012-08-19 20:30:52 -07005463 mWindowSession.reportDropResult(mWindow, result);
Chris Tated4533f12010-10-19 15:15:08 -07005464 } catch (RemoteException e) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08005465 Log.e(mTag, "Unable to report drop result");
Chris Tated4533f12010-10-19 15:15:08 -07005466 }
5467 }
Christopher Tate407b4e92010-11-30 17:14:08 -08005468
Vladislav Kaznacheev82063912015-11-20 14:20:13 -08005469 // When the drag operation ends, reset drag-related state
Christopher Tate407b4e92010-11-30 17:14:08 -08005470 if (what == DragEvent.ACTION_DRAG_ENDED) {
5471 setLocalDragState(null);
Vladislav Kaznacheev82063912015-11-20 14:20:13 -08005472 mAttachInfo.mDragToken = null;
Vladislav Kaznacheev4f639742015-11-18 13:21:35 -08005473 if (mAttachInfo.mDragSurface != null) {
5474 mAttachInfo.mDragSurface.release();
5475 mAttachInfo.mDragSurface = null;
5476 }
Christopher Tate407b4e92010-11-30 17:14:08 -08005477 }
Christopher Tatea53146c2010-09-07 11:57:52 -07005478 }
5479 }
5480 event.recycle();
5481 }
5482
Dianne Hackborn9a230e02011-10-06 11:51:27 -07005483 public void handleDispatchSystemUiVisibilityChanged(SystemUiVisibilityInfo args) {
5484 if (mSeq != args.seq) {
5485 // The sequence has changed, so we need to update our value and make
5486 // sure to do a traversal afterward so the window manager is given our
5487 // most recent data.
5488 mSeq = args.seq;
5489 mAttachInfo.mForceReportNewAttributes = true;
Igor Murashkina86ab6402013-08-30 12:58:36 -07005490 scheduleTraversals();
Joe Onorato14782f72011-01-25 19:53:17 -08005491 }
Dianne Hackborn9a230e02011-10-06 11:51:27 -07005492 if (mView == null) return;
5493 if (args.localChanges != 0) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -07005494 mView.updateLocalSystemUiVisibility(args.localValue, args.localChanges);
Dianne Hackborn9a230e02011-10-06 11:51:27 -07005495 }
Chris Craikd36a81f2014-07-17 10:16:51 -07005496
5497 int visibility = args.globalVisibility&View.SYSTEM_UI_CLEARABLE_FLAGS;
5498 if (visibility != mAttachInfo.mGlobalSystemUiVisibility) {
5499 mAttachInfo.mGlobalSystemUiVisibility = visibility;
5500 mView.dispatchSystemUiVisibilityChanged(visibility);
Dianne Hackborncf675782012-05-10 15:07:24 -07005501 }
Joe Onorato664644d2011-01-23 17:53:23 -08005502 }
5503
Craig Mautner9c795042014-10-28 19:59:59 -07005504 public void handleDispatchWindowShown() {
5505 mAttachInfo.mTreeObserver.dispatchOnWindowShown();
5506 }
5507
Clara Bayarri75e09792015-07-29 16:20:40 +01005508 public void handleRequestKeyboardShortcuts(IResultReceiver receiver) {
5509 Bundle data = new Bundle();
5510 ArrayList<KeyboardShortcutGroup> list = new ArrayList<>();
5511 if (mView != null) {
5512 mView.requestKeyboardShortcuts(list);
5513 }
5514 data.putParcelableArrayList(WindowManager.PARCEL_KEY_SHORTCUTS_ARRAY, list);
5515 try {
5516 receiver.send(0, data);
5517 } catch (RemoteException e) {
5518 }
5519 }
5520
Christopher Tate2c095f32010-10-04 14:13:40 -07005521 public void getLastTouchPoint(Point outLocation) {
5522 outLocation.x = (int) mLastTouchPoint.x;
5523 outLocation.y = (int) mLastTouchPoint.y;
5524 }
5525
Vladislav Kaznacheevba761122016-01-22 12:09:45 -08005526 public int getLastTouchSource() {
5527 return mLastTouchSource;
5528 }
5529
Chris Tate9d1ab882010-11-02 15:55:39 -07005530 public void setDragFocus(View newDragTarget) {
Christopher Tatea53146c2010-09-07 11:57:52 -07005531 if (mCurrentDragView != newDragTarget) {
Chris Tate048691c2010-10-12 17:39:18 -07005532 mCurrentDragView = newDragTarget;
Christopher Tatea53146c2010-09-07 11:57:52 -07005533 }
Christopher Tatea53146c2010-09-07 11:57:52 -07005534 }
5535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005536 private AudioManager getAudioManager() {
5537 if (mView == null) {
5538 throw new IllegalStateException("getAudioManager called when there is no mView");
5539 }
5540 if (mAudioManager == null) {
5541 mAudioManager = (AudioManager) mView.getContext().getSystemService(Context.AUDIO_SERVICE);
5542 }
5543 return mAudioManager;
5544 }
5545
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005546 public AccessibilityInteractionController getAccessibilityInteractionController() {
5547 if (mView == null) {
5548 throw new IllegalStateException("getAccessibilityInteractionController"
5549 + " called when there is no mView");
5550 }
Gilles Debunne5ac84422011-10-19 09:35:58 -07005551 if (mAccessibilityInteractionController == null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07005552 mAccessibilityInteractionController = new AccessibilityInteractionController(this);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005553 }
Gilles Debunne5ac84422011-10-19 09:35:58 -07005554 return mAccessibilityInteractionController;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005555 }
5556
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07005557 private int relayoutWindow(WindowManager.LayoutParams params, int viewVisibility,
5558 boolean insetsPending) throws RemoteException {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07005559
5560 float appScale = mAttachInfo.mApplicationScale;
Mitsuru Oshima3d914922009-05-13 22:29:15 -07005561 boolean restore = false;
Mitsuru Oshima64f59342009-06-21 00:03:11 -07005562 if (params != null && mTranslator != null) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -07005563 restore = true;
5564 params.backup();
Mitsuru Oshima64f59342009-06-21 00:03:11 -07005565 mTranslator.translateWindowLayout(params);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07005566 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07005567 if (params != null) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08005568 if (DBG) Log.d(mTag, "WindowLayout in layoutWindow:" + params);
Mitsuru Oshima3d914922009-05-13 22:29:15 -07005569 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -07005570 mPendingConfiguration.seq = 0;
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08005571 //Log.d(mTag, ">>>>>> CALLING relayout");
Dianne Hackborn180c4842011-09-13 12:39:25 -07005572 if (params != null && mOrigWindowType != params.type) {
5573 // For compatibility with old apps, don't crash here.
Michael Wright5bd69e62015-05-14 14:48:08 +01005574 if (mTargetSdkVersion < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08005575 Slog.w(mTag, "Window type can not be changed after "
Dianne Hackborn180c4842011-09-13 12:39:25 -07005576 + "the window is added; ignoring change of " + mView);
5577 params.type = mOrigWindowType;
5578 }
5579 }
Jeff Brown98365d72012-08-19 20:30:52 -07005580 int relayoutResult = mWindowSession.relayout(
Dianne Hackborn9a230e02011-10-06 11:51:27 -07005581 mWindow, mSeq, params,
Dianne Hackborn189ee182010-12-02 21:48:53 -08005582 (int) (mView.getMeasuredWidth() * appScale + 0.5f),
5583 (int) (mView.getMeasuredHeight() * appScale + 0.5f),
Jeff Brown98365d72012-08-19 20:30:52 -07005584 viewVisibility, insetsPending ? WindowManagerGlobal.RELAYOUT_INSETS_PENDING : 0,
Dianne Hackbornc4aad012013-02-22 15:05:25 -08005585 mWinFrame, mPendingOverscanInsets, mPendingContentInsets, mPendingVisibleInsets,
Jorim Jaggi2e95a482016-01-14 17:36:55 -08005586 mPendingStableInsets, mPendingOutsets, mPendingBackDropFrame, mPendingConfiguration,
5587 mSurface);
Jorim Jaggi0ffd49c2016-02-12 15:04:21 -08005588
5589 mPendingAlwaysConsumeNavBar =
5590 (relayoutResult & WindowManagerGlobal.RELAYOUT_RES_CONSUME_ALWAYS_NAV_BAR) != 0;
5591
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08005592 //Log.d(mTag, "<<<<<< BACK FROM relayout");
Mitsuru Oshima3d914922009-05-13 22:29:15 -07005593 if (restore) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -07005594 params.restore();
Mitsuru Oshima3d914922009-05-13 22:29:15 -07005595 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07005596
Mitsuru Oshima64f59342009-06-21 00:03:11 -07005597 if (mTranslator != null) {
5598 mTranslator.translateRectInScreenToAppWinFrame(mWinFrame);
Dianne Hackbornc4aad012013-02-22 15:05:25 -08005599 mTranslator.translateRectInScreenToAppWindow(mPendingOverscanInsets);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07005600 mTranslator.translateRectInScreenToAppWindow(mPendingContentInsets);
5601 mTranslator.translateRectInScreenToAppWindow(mPendingVisibleInsets);
Adrian Roosfa104232014-06-20 16:10:14 -07005602 mTranslator.translateRectInScreenToAppWindow(mPendingStableInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07005603 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07005604 return relayoutResult;
5605 }
Romain Guy8506ab42009-06-11 17:35:47 -07005606
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07005607 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005608 * {@inheritDoc}
5609 */
Igor Murashkina86ab6402013-08-30 12:58:36 -07005610 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005611 public void playSoundEffect(int effectId) {
5612 checkThread();
5613
Jean-Michel Trivi13b18fd2010-05-05 09:18:15 -07005614 try {
5615 final AudioManager audioManager = getAudioManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005616
Jean-Michel Trivi13b18fd2010-05-05 09:18:15 -07005617 switch (effectId) {
5618 case SoundEffectConstants.CLICK:
5619 audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK);
5620 return;
5621 case SoundEffectConstants.NAVIGATION_DOWN:
5622 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_DOWN);
5623 return;
5624 case SoundEffectConstants.NAVIGATION_LEFT:
5625 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_LEFT);
5626 return;
5627 case SoundEffectConstants.NAVIGATION_RIGHT:
5628 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_RIGHT);
5629 return;
5630 case SoundEffectConstants.NAVIGATION_UP:
5631 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_UP);
5632 return;
5633 default:
5634 throw new IllegalArgumentException("unknown effect id " + effectId +
5635 " not defined in " + SoundEffectConstants.class.getCanonicalName());
5636 }
5637 } catch (IllegalStateException e) {
5638 // Exception thrown by getAudioManager() when mView is null
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08005639 Log.e(mTag, "FATAL EXCEPTION when attempting to play sound effect: " + e);
Jean-Michel Trivi13b18fd2010-05-05 09:18:15 -07005640 e.printStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005641 }
5642 }
5643
5644 /**
5645 * {@inheritDoc}
5646 */
Igor Murashkina86ab6402013-08-30 12:58:36 -07005647 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005648 public boolean performHapticFeedback(int effectId, boolean always) {
5649 try {
Jeff Brown98365d72012-08-19 20:30:52 -07005650 return mWindowSession.performHapticFeedback(mWindow, effectId, always);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005651 } catch (RemoteException e) {
5652 return false;
5653 }
5654 }
5655
5656 /**
5657 * {@inheritDoc}
5658 */
Igor Murashkina86ab6402013-08-30 12:58:36 -07005659 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005660 public View focusSearch(View focused, int direction) {
5661 checkThread();
5662 if (!(mView instanceof ViewGroup)) {
5663 return null;
5664 }
5665 return FocusFinder.getInstance().findNextFocus((ViewGroup) mView, focused, direction);
5666 }
5667
5668 public void debug() {
5669 mView.debug();
5670 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07005671
Jeff Brown5182c782013-10-15 20:31:52 -07005672 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5673 String innerPrefix = prefix + " ";
5674 writer.print(prefix); writer.println("ViewRoot:");
5675 writer.print(innerPrefix); writer.print("mAdded="); writer.print(mAdded);
5676 writer.print(" mRemoved="); writer.println(mRemoved);
5677 writer.print(innerPrefix); writer.print("mConsumeBatchedInputScheduled=");
5678 writer.println(mConsumeBatchedInputScheduled);
Michael Wright9d744c72014-02-18 21:27:42 -08005679 writer.print(innerPrefix); writer.print("mConsumeBatchedInputImmediatelyScheduled=");
5680 writer.println(mConsumeBatchedInputImmediatelyScheduled);
Jeff Brown5182c782013-10-15 20:31:52 -07005681 writer.print(innerPrefix); writer.print("mPendingInputEventCount=");
5682 writer.println(mPendingInputEventCount);
5683 writer.print(innerPrefix); writer.print("mProcessInputEventsScheduled=");
5684 writer.println(mProcessInputEventsScheduled);
5685 writer.print(innerPrefix); writer.print("mTraversalScheduled=");
5686 writer.print(mTraversalScheduled);
Daniel Koulomzin087ae472015-12-16 17:52:25 -05005687 writer.print(innerPrefix); writer.print("mIsAmbientMode=");
5688 writer.print(mIsAmbientMode);
Jeff Brown5182c782013-10-15 20:31:52 -07005689 if (mTraversalScheduled) {
5690 writer.print(" (barrier="); writer.print(mTraversalBarrier); writer.println(")");
5691 } else {
5692 writer.println();
5693 }
5694 mFirstInputStage.dump(innerPrefix, writer);
5695
5696 mChoreographer.dump(prefix, writer);
5697
5698 writer.print(prefix); writer.println("View Hierarchy:");
5699 dumpViewHierarchy(innerPrefix, writer, mView);
5700 }
5701
5702 private void dumpViewHierarchy(String prefix, PrintWriter writer, View view) {
5703 writer.print(prefix);
5704 if (view == null) {
5705 writer.println("null");
5706 return;
5707 }
5708 writer.println(view.toString());
5709 if (!(view instanceof ViewGroup)) {
5710 return;
5711 }
5712 ViewGroup grp = (ViewGroup)view;
5713 final int N = grp.getChildCount();
5714 if (N <= 0) {
5715 return;
5716 }
5717 prefix = prefix + " ";
5718 for (int i=0; i<N; i++) {
5719 dumpViewHierarchy(prefix, writer, grp.getChildAt(i));
5720 }
5721 }
5722
Romain Guy211370f2012-02-01 16:10:55 -08005723 public void dumpGfxInfo(int[] info) {
Romain Guy54ab3472012-06-14 12:52:53 -07005724 info[0] = info[1] = 0;
Romain Guy65b345f2011-07-27 18:51:50 -07005725 if (mView != null) {
5726 getGfxInfo(mView, info);
Romain Guy65b345f2011-07-27 18:51:50 -07005727 }
5728 }
5729
Romain Guya998dff2012-03-23 18:58:36 -07005730 private static void getGfxInfo(View view, int[] info) {
Chris Craik64a12e12014-03-28 18:12:12 -07005731 RenderNode renderNode = view.mRenderNode;
Romain Guy65b345f2011-07-27 18:51:50 -07005732 info[0]++;
Chris Craik64a12e12014-03-28 18:12:12 -07005733 if (renderNode != null) {
John Reckfe5e7b72014-05-23 17:42:28 -07005734 info[1] += renderNode.getDebugSize();
Romain Guy65b345f2011-07-27 18:51:50 -07005735 }
5736
5737 if (view instanceof ViewGroup) {
5738 ViewGroup group = (ViewGroup) view;
5739
5740 int count = group.getChildCount();
5741 for (int i = 0; i < count; i++) {
5742 getGfxInfo(group.getChildAt(i), info);
5743 }
5744 }
5745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005746
Craig Mautner8f303ad2013-06-14 11:32:22 -07005747 /**
5748 * @param immediate True, do now if not in traversal. False, put on queue and do later.
5749 * @return True, request has been queued. False, request has been completed.
5750 */
5751 boolean die(boolean immediate) {
Craig Mautnerdf2390a2012-08-29 20:59:22 -07005752 // Make sure we do execute immediately if we are in the middle of a traversal or the damage
5753 // done by dispatchDetachedFromWindow will cause havoc on return.
5754 if (immediate && !mIsInTraversal) {
Dianne Hackborn94d69142009-09-28 22:14:42 -07005755 doDie();
Craig Mautner8f303ad2013-06-14 11:32:22 -07005756 return false;
Dianne Hackborn94d69142009-09-28 22:14:42 -07005757 }
Craig Mautner8f303ad2013-06-14 11:32:22 -07005758
5759 if (!mIsDrawing) {
5760 destroyHardwareRenderer();
5761 } else {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08005762 Log.e(mTag, "Attempting to destroy the window while drawing!\n" +
Craig Mautner8f303ad2013-06-14 11:32:22 -07005763 " window=" + this + ", title=" + mWindowAttributes.getTitle());
5764 }
5765 mHandler.sendEmptyMessage(MSG_DIE);
5766 return true;
Dianne Hackborn94d69142009-09-28 22:14:42 -07005767 }
5768
5769 void doDie() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005770 checkThread();
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08005771 if (LOCAL_LOGV) Log.v(mTag, "DIE in " + this + " of " + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005772 synchronized (this) {
Craig Mautner8f303ad2013-06-14 11:32:22 -07005773 if (mRemoved) {
5774 return;
5775 }
5776 mRemoved = true;
Romain Guy16260e72011-09-01 14:26:11 -07005777 if (mAdded) {
Romain Guy16260e72011-09-01 14:26:11 -07005778 dispatchDetachedFromWindow();
5779 }
5780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005781 if (mAdded && !mFirst) {
Romain Guy29d89972010-09-22 16:10:57 -07005782 destroyHardwareRenderer();
5783
Romain Guyedbca122012-04-04 18:25:53 -07005784 if (mView != null) {
5785 int viewVisibility = mView.getVisibility();
5786 boolean viewVisibilityChanged = mViewVisibility != viewVisibility;
5787 if (mWindowAttributesChanged || viewVisibilityChanged) {
5788 // If layout params have been changed, first give them
5789 // to the window manager to make sure it has the correct
5790 // animation info.
5791 try {
5792 if ((relayoutWindow(mWindowAttributes, viewVisibility, false)
Jeff Brown98365d72012-08-19 20:30:52 -07005793 & WindowManagerGlobal.RELAYOUT_RES_FIRST_TIME) != 0) {
5794 mWindowSession.finishDrawing(mWindow);
Romain Guyedbca122012-04-04 18:25:53 -07005795 }
5796 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005797 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005798 }
Craig Mautner8f303ad2013-06-14 11:32:22 -07005799
Romain Guyedbca122012-04-04 18:25:53 -07005800 mSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005801 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005802 }
Romain Guyedbca122012-04-04 18:25:53 -07005803
5804 mAdded = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005805 }
Craig Mautner05eb7302013-06-03 17:24:21 -07005806 WindowManagerGlobal.getInstance().doRemoveView(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005807 }
5808
Dianne Hackborn5fd21692011-06-07 14:09:47 -07005809 public void requestUpdateConfiguration(Configuration config) {
Jeff Browna175a5b2012-02-15 19:18:31 -08005810 Message msg = mHandler.obtainMessage(MSG_UPDATE_CONFIGURATION, config);
5811 mHandler.sendMessage(msg);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07005812 }
5813
Dianne Hackborna53de062012-05-08 18:53:51 -07005814 public void loadSystemProperties() {
Romain Guy5bb3c732012-11-29 17:52:58 -08005815 mHandler.post(new Runnable() {
5816 @Override
5817 public void run() {
5818 // Profiling
5819 mProfileRendering = SystemProperties.getBoolean(PROPERTY_PROFILE_RENDERING, false);
5820 profileRendering(mAttachInfo.mHasWindowFocus);
5821
5822 // Hardware rendering
5823 if (mAttachInfo.mHardwareRenderer != null) {
John Reckcec24ae2013-11-05 13:27:50 -08005824 if (mAttachInfo.mHardwareRenderer.loadSystemProperties()) {
Romain Guy5bb3c732012-11-29 17:52:58 -08005825 invalidate();
5826 }
5827 }
5828
5829 // Layout debugging
5830 boolean layout = SystemProperties.getBoolean(View.DEBUG_LAYOUT_PROPERTY, false);
5831 if (layout != mAttachInfo.mDebugLayout) {
5832 mAttachInfo.mDebugLayout = layout;
5833 if (!mHandler.hasMessages(MSG_INVALIDATE_WORLD)) {
5834 mHandler.sendEmptyMessageDelayed(MSG_INVALIDATE_WORLD, 200);
5835 }
5836 }
Dianne Hackborna53de062012-05-08 18:53:51 -07005837 }
Romain Guy5bb3c732012-11-29 17:52:58 -08005838 });
Dianne Hackborna53de062012-05-08 18:53:51 -07005839 }
5840
Romain Guy29d89972010-09-22 16:10:57 -07005841 private void destroyHardwareRenderer() {
John Reck51aaf902015-12-02 15:08:07 -08005842 ThreadedRenderer hardwareRenderer = mAttachInfo.mHardwareRenderer;
Romain Guya998dff2012-03-23 18:58:36 -07005843
5844 if (hardwareRenderer != null) {
5845 if (mView != null) {
5846 hardwareRenderer.destroyHardwareResources(mView);
5847 }
John Reckf47a5942014-06-30 16:20:04 -07005848 hardwareRenderer.destroy();
Romain Guya998dff2012-03-23 18:58:36 -07005849 hardwareRenderer.setRequested(false);
5850
Chris Craikd36a81f2014-07-17 10:16:51 -07005851 mAttachInfo.mHardwareRenderer = null;
5852 mAttachInfo.mHardwareAccelerated = false;
Romain Guy29d89972010-09-22 16:10:57 -07005853 }
5854 }
5855
Jeff Browna175a5b2012-02-15 19:18:31 -08005856 public void dispatchFinishInputConnection(InputConnection connection) {
5857 Message msg = mHandler.obtainMessage(MSG_FINISH_INPUT_CONNECTION, connection);
5858 mHandler.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005859 }
Mitsuru Oshima3d914922009-05-13 22:29:15 -07005860
Dianne Hackbornc4aad012013-02-22 15:05:25 -08005861 public void dispatchResized(Rect frame, Rect overscanInsets, Rect contentInsets,
Filip Gruszczynski2217f612015-05-26 11:32:08 -07005862 Rect visibleInsets, Rect stableInsets, Rect outsets, boolean reportDraw,
Jorim Jaggi0ffd49c2016-02-12 15:04:21 -08005863 Configuration newConfig, Rect backDropFrame, boolean forceLayout,
5864 boolean alwaysConsumeNavBar) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08005865 if (DEBUG_LAYOUT) Log.v(mTag, "Resizing " + this + ": frame=" + frame.toShortString()
Svetoslav Ganov758143e2012-08-06 16:40:27 -07005866 + " contentInsets=" + contentInsets.toShortString()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005867 + " visibleInsets=" + visibleInsets.toShortString()
Chong Zhangd153c4f2015-11-06 20:26:40 -08005868 + " reportDraw=" + reportDraw
5869 + " backDropFrame=" + backDropFrame);
Chong Zhangdcee1de2015-10-06 10:26:00 -07005870
5871 // Tell all listeners that we are resizing the window so that the chrome can get
5872 // updated as fast as possible on a separate thread,
5873 if (mDragResizing) {
Jorim Jaggi9511b0f2016-01-29 19:12:44 -08005874 boolean fullscreen = frame.equals(backDropFrame);
Chong Zhangdcee1de2015-10-06 10:26:00 -07005875 synchronized (mWindowCallbacks) {
5876 for (int i = mWindowCallbacks.size() - 1; i >= 0; i--) {
Jorim Jaggi9511b0f2016-01-29 19:12:44 -08005877 mWindowCallbacks.get(i).onWindowSizeIsChanging(backDropFrame, fullscreen,
5878 visibleInsets, stableInsets);
Chong Zhangdcee1de2015-10-06 10:26:00 -07005879 }
5880 }
5881 }
5882
Svetoslav Ganov758143e2012-08-06 16:40:27 -07005883 Message msg = mHandler.obtainMessage(reportDraw ? MSG_RESIZED_REPORT : MSG_RESIZED);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07005884 if (mTranslator != null) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07005885 mTranslator.translateRectInScreenToAppWindow(frame);
Dianne Hackbornc4aad012013-02-22 15:05:25 -08005886 mTranslator.translateRectInScreenToAppWindow(overscanInsets);
Dianne Hackborn3556c9a2012-05-04 16:31:25 -07005887 mTranslator.translateRectInScreenToAppWindow(contentInsets);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07005888 mTranslator.translateRectInScreenToAppWindow(visibleInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07005889 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07005890 SomeArgs args = SomeArgs.obtain();
5891 final boolean sameProcessCall = (Binder.getCallingPid() == android.os.Process.myPid());
5892 args.arg1 = sameProcessCall ? new Rect(frame) : frame;
5893 args.arg2 = sameProcessCall ? new Rect(contentInsets) : contentInsets;
5894 args.arg3 = sameProcessCall ? new Rect(visibleInsets) : visibleInsets;
5895 args.arg4 = sameProcessCall && newConfig != null ? new Configuration(newConfig) : newConfig;
Dianne Hackbornc4aad012013-02-22 15:05:25 -08005896 args.arg5 = sameProcessCall ? new Rect(overscanInsets) : overscanInsets;
Adrian Roosfa104232014-06-20 16:10:14 -07005897 args.arg6 = sameProcessCall ? new Rect(stableInsets) : stableInsets;
Filip Gruszczynski2217f612015-05-26 11:32:08 -07005898 args.arg7 = sameProcessCall ? new Rect(outsets) : outsets;
Jorim Jaggia7262a82015-11-03 15:15:40 +01005899 args.arg8 = sameProcessCall ? new Rect(backDropFrame) : backDropFrame;
Jorim Jaggia4a58ef2016-01-27 02:10:08 -08005900 args.argi1 = forceLayout ? 1 : 0;
Jorim Jaggi0ffd49c2016-02-12 15:04:21 -08005901 args.argi2 = alwaysConsumeNavBar ? 1 : 0;
Svetoslav Ganov758143e2012-08-06 16:40:27 -07005902 msg.obj = args;
Jeff Browna175a5b2012-02-15 19:18:31 -08005903 mHandler.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005904 }
Chet Haase1f4786b2011-11-02 10:51:52 -07005905
Craig Mautner5702d4d2012-06-30 14:10:16 -07005906 public void dispatchMoved(int newX, int newY) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08005907 if (DEBUG_LAYOUT) Log.v(mTag, "Window moved " + this + ": newX=" + newX + " newY=" + newY);
Craig Mautner5702d4d2012-06-30 14:10:16 -07005908 if (mTranslator != null) {
5909 PointF point = new PointF(newX, newY);
5910 mTranslator.translatePointInScreenToAppWindow(point);
5911 newX = (int) (point.x + 0.5);
5912 newY = (int) (point.y + 0.5);
5913 }
5914 Message msg = mHandler.obtainMessage(MSG_WINDOW_MOVED, newX, newY);
5915 mHandler.sendMessage(msg);
5916 }
5917
Jeff Brown4952dfd2011-11-30 19:23:22 -08005918 /**
5919 * Represents a pending input event that is waiting in a queue.
5920 *
5921 * Input events are processed in serial order by the timestamp specified by
Romain Guy211370f2012-02-01 16:10:55 -08005922 * {@link InputEvent#getEventTimeNano()}. In general, the input dispatcher delivers
Jeff Brown4952dfd2011-11-30 19:23:22 -08005923 * one input event to the application at a time and waits for the application
5924 * to finish handling it before delivering the next one.
5925 *
5926 * However, because the application or IME can synthesize and inject multiple
5927 * key events at a time without going through the input dispatcher, we end up
5928 * needing a queue on the application's side.
5929 */
5930 private static final class QueuedInputEvent {
Jeff Brownf9e989d2013-04-04 23:04:03 -07005931 public static final int FLAG_DELIVER_POST_IME = 1 << 0;
5932 public static final int FLAG_DEFERRED = 1 << 1;
5933 public static final int FLAG_FINISHED = 1 << 2;
5934 public static final int FLAG_FINISHED_HANDLED = 1 << 3;
5935 public static final int FLAG_RESYNTHESIZED = 1 << 4;
Michael Wright899d7052014-04-23 17:23:39 -07005936 public static final int FLAG_UNHANDLED = 1 << 5;
Jeff Brown4952dfd2011-11-30 19:23:22 -08005937
5938 public QueuedInputEvent mNext;
5939
5940 public InputEvent mEvent;
Jeff Brown32cbc38552011-12-01 14:01:49 -08005941 public InputEventReceiver mReceiver;
Jeff Brown4952dfd2011-11-30 19:23:22 -08005942 public int mFlags;
Jeff Brownf9e989d2013-04-04 23:04:03 -07005943
5944 public boolean shouldSkipIme() {
5945 if ((mFlags & FLAG_DELIVER_POST_IME) != 0) {
5946 return true;
5947 }
5948 return mEvent instanceof MotionEvent
5949 && mEvent.isFromSource(InputDevice.SOURCE_CLASS_POINTER);
5950 }
Michael Wright899d7052014-04-23 17:23:39 -07005951
5952 public boolean shouldSendToSynthesizer() {
5953 if ((mFlags & FLAG_UNHANDLED) != 0) {
5954 return true;
5955 }
5956
5957 return false;
5958 }
Michael Wright06a79252014-05-05 17:45:29 -07005959
5960 @Override
5961 public String toString() {
5962 StringBuilder sb = new StringBuilder("QueuedInputEvent{flags=");
5963 boolean hasPrevious = false;
5964 hasPrevious = flagToString("DELIVER_POST_IME", FLAG_DELIVER_POST_IME, hasPrevious, sb);
5965 hasPrevious = flagToString("DEFERRED", FLAG_DEFERRED, hasPrevious, sb);
5966 hasPrevious = flagToString("FINISHED", FLAG_FINISHED, hasPrevious, sb);
5967 hasPrevious = flagToString("FINISHED_HANDLED", FLAG_FINISHED_HANDLED, hasPrevious, sb);
5968 hasPrevious = flagToString("RESYNTHESIZED", FLAG_RESYNTHESIZED, hasPrevious, sb);
5969 hasPrevious = flagToString("UNHANDLED", FLAG_UNHANDLED, hasPrevious, sb);
5970 if (!hasPrevious) {
5971 sb.append("0");
5972 }
5973 sb.append(", hasNextQueuedEvent=" + (mEvent != null ? "true" : "false"));
5974 sb.append(", hasInputEventReceiver=" + (mReceiver != null ? "true" : "false"));
5975 sb.append(", mEvent=" + mEvent + "}");
5976 return sb.toString();
5977 }
5978
5979 private boolean flagToString(String name, int flag,
5980 boolean hasPrevious, StringBuilder sb) {
5981 if ((mFlags & flag) != 0) {
5982 if (hasPrevious) {
5983 sb.append("|");
5984 }
5985 sb.append(name);
5986 return true;
5987 }
5988 return hasPrevious;
5989 }
Jeff Brown4952dfd2011-11-30 19:23:22 -08005990 }
5991
5992 private QueuedInputEvent obtainQueuedInputEvent(InputEvent event,
Jeff Brown32cbc38552011-12-01 14:01:49 -08005993 InputEventReceiver receiver, int flags) {
Jeff Brown4952dfd2011-11-30 19:23:22 -08005994 QueuedInputEvent q = mQueuedInputEventPool;
5995 if (q != null) {
5996 mQueuedInputEventPoolSize -= 1;
5997 mQueuedInputEventPool = q.mNext;
5998 q.mNext = null;
5999 } else {
6000 q = new QueuedInputEvent();
Jeff Brown46b9ac02010-04-22 18:58:52 -07006001 }
6002
Jeff Brown4952dfd2011-11-30 19:23:22 -08006003 q.mEvent = event;
Jeff Brown32cbc38552011-12-01 14:01:49 -08006004 q.mReceiver = receiver;
Jeff Brown4952dfd2011-11-30 19:23:22 -08006005 q.mFlags = flags;
Jeff Brown4952dfd2011-11-30 19:23:22 -08006006 return q;
6007 }
6008
6009 private void recycleQueuedInputEvent(QueuedInputEvent q) {
6010 q.mEvent = null;
Jeff Brown32cbc38552011-12-01 14:01:49 -08006011 q.mReceiver = null;
Jeff Brown4952dfd2011-11-30 19:23:22 -08006012
6013 if (mQueuedInputEventPoolSize < MAX_QUEUED_INPUT_EVENT_POOL_SIZE) {
6014 mQueuedInputEventPoolSize += 1;
6015 q.mNext = mQueuedInputEventPool;
6016 mQueuedInputEventPool = q;
6017 }
6018 }
6019
Jeff Brownf9261d22012-02-03 13:49:15 -08006020 void enqueueInputEvent(InputEvent event) {
6021 enqueueInputEvent(event, null, 0, false);
6022 }
6023
Jeff Brown4952dfd2011-11-30 19:23:22 -08006024 void enqueueInputEvent(InputEvent event,
Jeff Brownf9261d22012-02-03 13:49:15 -08006025 InputEventReceiver receiver, int flags, boolean processImmediately) {
Michael Wright5bd69e62015-05-14 14:48:08 +01006026 adjustInputEventForCompatibility(event);
Jeff Brown32cbc38552011-12-01 14:01:49 -08006027 QueuedInputEvent q = obtainQueuedInputEvent(event, receiver, flags);
Jeff Brown4952dfd2011-11-30 19:23:22 -08006028
Jeff Brown4952dfd2011-11-30 19:23:22 -08006029 // Always enqueue the input event in order, regardless of its time stamp.
6030 // We do this because the application or the IME may inject key events
6031 // in response to touch events and we want to ensure that the injected keys
6032 // are processed in the order they were received and we cannot trust that
6033 // the time stamp of injected events are monotonic.
Michael Wrightc8a7e542013-03-20 17:58:33 -07006034 QueuedInputEvent last = mPendingInputEventTail;
Jeff Brown4952dfd2011-11-30 19:23:22 -08006035 if (last == null) {
Michael Wrightc8a7e542013-03-20 17:58:33 -07006036 mPendingInputEventHead = q;
6037 mPendingInputEventTail = q;
Jeff Brown4952dfd2011-11-30 19:23:22 -08006038 } else {
Jeff Brown4952dfd2011-11-30 19:23:22 -08006039 last.mNext = q;
Michael Wrightc8a7e542013-03-20 17:58:33 -07006040 mPendingInputEventTail = q;
Jeff Brown4952dfd2011-11-30 19:23:22 -08006041 }
Michael Wright95ae9422013-03-14 10:58:50 -07006042 mPendingInputEventCount += 1;
6043 Trace.traceCounter(Trace.TRACE_TAG_INPUT, mPendingInputEventQueueLengthCounterName,
6044 mPendingInputEventCount);
Jeff Brown4952dfd2011-11-30 19:23:22 -08006045
Jeff Brownf9261d22012-02-03 13:49:15 -08006046 if (processImmediately) {
6047 doProcessInputEvents();
6048 } else {
6049 scheduleProcessInputEvents();
6050 }
Jeff Brown4952dfd2011-11-30 19:23:22 -08006051 }
6052
6053 private void scheduleProcessInputEvents() {
Jeff Brown96e942d2011-11-30 19:55:01 -08006054 if (!mProcessInputEventsScheduled) {
6055 mProcessInputEventsScheduled = true;
Jeff Brownebb2d8d2012-03-23 17:14:34 -07006056 Message msg = mHandler.obtainMessage(MSG_PROCESS_INPUT_EVENTS);
6057 msg.setAsynchronous(true);
6058 mHandler.sendMessage(msg);
Jeff Brown4952dfd2011-11-30 19:23:22 -08006059 }
6060 }
6061
Jeff Brownebb2d8d2012-03-23 17:14:34 -07006062 void doProcessInputEvents() {
Jeff Brownf9e989d2013-04-04 23:04:03 -07006063 // Deliver all pending input events in the queue.
Michael Wrightc8a7e542013-03-20 17:58:33 -07006064 while (mPendingInputEventHead != null) {
6065 QueuedInputEvent q = mPendingInputEventHead;
6066 mPendingInputEventHead = q.mNext;
6067 if (mPendingInputEventHead == null) {
6068 mPendingInputEventTail = null;
6069 }
Jeff Brown4952dfd2011-11-30 19:23:22 -08006070 q.mNext = null;
Jeff Brown29c0ed22013-01-14 13:50:37 -08006071
Michael Wright95ae9422013-03-14 10:58:50 -07006072 mPendingInputEventCount -= 1;
6073 Trace.traceCounter(Trace.TRACE_TAG_INPUT, mPendingInputEventQueueLengthCounterName,
6074 mPendingInputEventCount);
6075
John Reckba6adf62015-02-19 14:36:50 -08006076 long eventTime = q.mEvent.getEventTimeNano();
6077 long oldestEventTime = eventTime;
6078 if (q.mEvent instanceof MotionEvent) {
6079 MotionEvent me = (MotionEvent)q.mEvent;
6080 if (me.getHistorySize() > 0) {
6081 oldestEventTime = me.getHistoricalEventTimeNano(0);
6082 }
6083 }
6084 mChoreographer.mFrameInfo.updateInputEventTime(eventTime, oldestEventTime);
6085
Jeff Brownf9e989d2013-04-04 23:04:03 -07006086 deliverInputEvent(q);
Jeff Brown4952dfd2011-11-30 19:23:22 -08006087 }
6088
6089 // We are done processing all input events that we can process right now
6090 // so we can clear the pending flag immediately.
Jeff Brown96e942d2011-11-30 19:55:01 -08006091 if (mProcessInputEventsScheduled) {
6092 mProcessInputEventsScheduled = false;
Jeff Browna175a5b2012-02-15 19:18:31 -08006093 mHandler.removeMessages(MSG_PROCESS_INPUT_EVENTS);
Jeff Brown4952dfd2011-11-30 19:23:22 -08006094 }
6095 }
6096
Jeff Brownf9e989d2013-04-04 23:04:03 -07006097 private void deliverInputEvent(QueuedInputEvent q) {
Michael Wrightd2c3adc2014-02-18 22:50:50 -08006098 Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW, "deliverInputEvent",
6099 q.mEvent.getSequenceNumber());
6100 if (mInputEventConsistencyVerifier != null) {
6101 mInputEventConsistencyVerifier.onInputEvent(q.mEvent, 0);
6102 }
Michael Wrightc8a7e542013-03-20 17:58:33 -07006103
Michael Wright899d7052014-04-23 17:23:39 -07006104 InputStage stage;
6105 if (q.shouldSendToSynthesizer()) {
6106 stage = mSyntheticInputStage;
6107 } else {
6108 stage = q.shouldSkipIme() ? mFirstPostImeInputStage : mFirstInputStage;
6109 }
6110
Michael Wrightd2c3adc2014-02-18 22:50:50 -08006111 if (stage != null) {
6112 stage.deliver(q);
6113 } else {
6114 finishInputEvent(q);
Michael Wrightbf020962013-03-28 17:27:50 -07006115 }
Michael Wrightbf020962013-03-28 17:27:50 -07006116 }
6117
Jeff Brownf9e989d2013-04-04 23:04:03 -07006118 private void finishInputEvent(QueuedInputEvent q) {
Michael Wrightd2c3adc2014-02-18 22:50:50 -08006119 Trace.asyncTraceEnd(Trace.TRACE_TAG_VIEW, "deliverInputEvent",
6120 q.mEvent.getSequenceNumber());
Michael Wright9d744c72014-02-18 21:27:42 -08006121
Jeff Brown32cbc38552011-12-01 14:01:49 -08006122 if (q.mReceiver != null) {
Jeff Brownf9e989d2013-04-04 23:04:03 -07006123 boolean handled = (q.mFlags & QueuedInputEvent.FLAG_FINISHED_HANDLED) != 0;
Jeff Brown32cbc38552011-12-01 14:01:49 -08006124 q.mReceiver.finishInputEvent(q.mEvent, handled);
Jeff Brown92cc2d82011-12-02 01:19:47 -08006125 } else {
6126 q.mEvent.recycleIfNeededAfterDispatch();
Jeff Brown4952dfd2011-11-30 19:23:22 -08006127 }
6128
6129 recycleQueuedInputEvent(q);
Jeff Brown29c0ed22013-01-14 13:50:37 -08006130 }
Jeff Brown4952dfd2011-11-30 19:23:22 -08006131
Michael Wright5bd69e62015-05-14 14:48:08 +01006132 private void adjustInputEventForCompatibility(InputEvent e) {
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07006133 if (mTargetSdkVersion < Build.VERSION_CODES.M && e instanceof MotionEvent) {
Michael Wright5bd69e62015-05-14 14:48:08 +01006134 MotionEvent motion = (MotionEvent) e;
6135 final int mask =
6136 MotionEvent.BUTTON_STYLUS_PRIMARY | MotionEvent.BUTTON_STYLUS_SECONDARY;
6137 final int buttonState = motion.getButtonState();
6138 final int compatButtonState = (buttonState & mask) >> 4;
6139 if (compatButtonState != 0) {
6140 motion.setButtonState(buttonState | compatButtonState);
6141 }
6142 }
6143 }
6144
Jeff Brownf9e989d2013-04-04 23:04:03 -07006145 static boolean isTerminalInputEvent(InputEvent event) {
Jeff Brown29c0ed22013-01-14 13:50:37 -08006146 if (event instanceof KeyEvent) {
6147 final KeyEvent keyEvent = (KeyEvent)event;
6148 return keyEvent.getAction() == KeyEvent.ACTION_UP;
6149 } else {
6150 final MotionEvent motionEvent = (MotionEvent)event;
6151 final int action = motionEvent.getAction();
6152 return action == MotionEvent.ACTION_UP
6153 || action == MotionEvent.ACTION_CANCEL
6154 || action == MotionEvent.ACTION_HOVER_EXIT;
Jeff Brown4952dfd2011-11-30 19:23:22 -08006155 }
6156 }
6157
Jeff Brownebb2d8d2012-03-23 17:14:34 -07006158 void scheduleConsumeBatchedInput() {
6159 if (!mConsumeBatchedInputScheduled) {
6160 mConsumeBatchedInputScheduled = true;
6161 mChoreographer.postCallback(Choreographer.CALLBACK_INPUT,
6162 mConsumedBatchedInputRunnable, null);
Jeff Brown4a06c802012-02-15 15:06:01 -08006163 }
6164 }
Jeff Brownebb2d8d2012-03-23 17:14:34 -07006165
6166 void unscheduleConsumeBatchedInput() {
6167 if (mConsumeBatchedInputScheduled) {
6168 mConsumeBatchedInputScheduled = false;
6169 mChoreographer.removeCallbacks(Choreographer.CALLBACK_INPUT,
6170 mConsumedBatchedInputRunnable, null);
6171 }
6172 }
6173
Michael Wright9d744c72014-02-18 21:27:42 -08006174 void scheduleConsumeBatchedInputImmediately() {
6175 if (!mConsumeBatchedInputImmediatelyScheduled) {
6176 unscheduleConsumeBatchedInput();
6177 mConsumeBatchedInputImmediatelyScheduled = true;
6178 mHandler.post(mConsumeBatchedInputImmediatelyRunnable);
6179 }
6180 }
6181
Jeff Brown771526c2012-04-27 15:13:25 -07006182 void doConsumeBatchedInput(long frameTimeNanos) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07006183 if (mConsumeBatchedInputScheduled) {
6184 mConsumeBatchedInputScheduled = false;
Jeff Brown330314c2012-04-27 02:20:22 -07006185 if (mInputEventReceiver != null) {
Michael Wright9d744c72014-02-18 21:27:42 -08006186 if (mInputEventReceiver.consumeBatchedInputEvents(frameTimeNanos)
6187 && frameTimeNanos != -1) {
Michael Wright62ce65d2013-10-25 14:50:36 -07006188 // If we consumed a batch here, we want to go ahead and schedule the
6189 // consumption of batched input events on the next frame. Otherwise, we would
6190 // wait until we have more input events pending and might get starved by other
Michael Wright9d744c72014-02-18 21:27:42 -08006191 // things occurring in the process. If the frame time is -1, however, then
6192 // we're in a non-batching mode, so there's no need to schedule this.
Michael Wright62ce65d2013-10-25 14:50:36 -07006193 scheduleConsumeBatchedInput();
6194 }
Jeff Brownebb2d8d2012-03-23 17:14:34 -07006195 }
Jeff Brown330314c2012-04-27 02:20:22 -07006196 doProcessInputEvents();
Jeff Brownebb2d8d2012-03-23 17:14:34 -07006197 }
6198 }
6199
6200 final class TraversalRunnable implements Runnable {
6201 @Override
6202 public void run() {
6203 doTraversal();
6204 }
6205 }
6206 final TraversalRunnable mTraversalRunnable = new TraversalRunnable();
Jeff Brown4a06c802012-02-15 15:06:01 -08006207
Jeff Brown32cbc38552011-12-01 14:01:49 -08006208 final class WindowInputEventReceiver extends InputEventReceiver {
6209 public WindowInputEventReceiver(InputChannel inputChannel, Looper looper) {
6210 super(inputChannel, looper);
Jeff Brown46b9ac02010-04-22 18:58:52 -07006211 }
Jeff Brown32cbc38552011-12-01 14:01:49 -08006212
6213 @Override
6214 public void onInputEvent(InputEvent event) {
Jeff Brownf9261d22012-02-03 13:49:15 -08006215 enqueueInputEvent(event, this, 0, true);
Jeff Brown32cbc38552011-12-01 14:01:49 -08006216 }
Jeff Brown072ec962012-02-07 14:46:57 -08006217
6218 @Override
6219 public void onBatchedInputEventPending() {
Michael Wright9d744c72014-02-18 21:27:42 -08006220 if (mUnbufferedInputDispatch) {
6221 super.onBatchedInputEventPending();
6222 } else {
6223 scheduleConsumeBatchedInput();
6224 }
Jeff Brownebb2d8d2012-03-23 17:14:34 -07006225 }
6226
6227 @Override
6228 public void dispose() {
6229 unscheduleConsumeBatchedInput();
6230 super.dispose();
Jeff Brown072ec962012-02-07 14:46:57 -08006231 }
Jeff Brown32cbc38552011-12-01 14:01:49 -08006232 }
6233 WindowInputEventReceiver mInputEventReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006234
Jeff Brownebb2d8d2012-03-23 17:14:34 -07006235 final class ConsumeBatchedInputRunnable implements Runnable {
6236 @Override
6237 public void run() {
Jeff Brown771526c2012-04-27 15:13:25 -07006238 doConsumeBatchedInput(mChoreographer.getFrameTimeNanos());
Jeff Brownebb2d8d2012-03-23 17:14:34 -07006239 }
6240 }
6241 final ConsumeBatchedInputRunnable mConsumedBatchedInputRunnable =
6242 new ConsumeBatchedInputRunnable();
6243 boolean mConsumeBatchedInputScheduled;
6244
Michael Wright9d744c72014-02-18 21:27:42 -08006245 final class ConsumeBatchedInputImmediatelyRunnable implements Runnable {
6246 @Override
6247 public void run() {
6248 doConsumeBatchedInput(-1);
6249 }
6250 }
6251 final ConsumeBatchedInputImmediatelyRunnable mConsumeBatchedInputImmediatelyRunnable =
6252 new ConsumeBatchedInputImmediatelyRunnable();
6253 boolean mConsumeBatchedInputImmediatelyScheduled;
6254
Jeff Brown6cb7b462012-03-05 13:21:17 -08006255 final class InvalidateOnAnimationRunnable implements Runnable {
6256 private boolean mPosted;
Igor Murashkina86ab6402013-08-30 12:58:36 -07006257 private final ArrayList<View> mViews = new ArrayList<View>();
6258 private final ArrayList<AttachInfo.InvalidateInfo> mViewRects =
Jeff Brown6cb7b462012-03-05 13:21:17 -08006259 new ArrayList<AttachInfo.InvalidateInfo>();
6260 private View[] mTempViews;
6261 private AttachInfo.InvalidateInfo[] mTempViewRects;
6262
6263 public void addView(View view) {
6264 synchronized (this) {
6265 mViews.add(view);
6266 postIfNeededLocked();
6267 }
6268 }
6269
6270 public void addViewRect(AttachInfo.InvalidateInfo info) {
6271 synchronized (this) {
6272 mViewRects.add(info);
6273 postIfNeededLocked();
6274 }
6275 }
6276
6277 public void removeView(View view) {
6278 synchronized (this) {
6279 mViews.remove(view);
6280
6281 for (int i = mViewRects.size(); i-- > 0; ) {
6282 AttachInfo.InvalidateInfo info = mViewRects.get(i);
6283 if (info.target == view) {
6284 mViewRects.remove(i);
Svetoslav Ganovabae2a12012-11-27 16:59:37 -08006285 info.recycle();
Jeff Brown6cb7b462012-03-05 13:21:17 -08006286 }
6287 }
6288
6289 if (mPosted && mViews.isEmpty() && mViewRects.isEmpty()) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07006290 mChoreographer.removeCallbacks(Choreographer.CALLBACK_ANIMATION, this, null);
Jeff Brown6cb7b462012-03-05 13:21:17 -08006291 mPosted = false;
6292 }
6293 }
6294 }
6295
6296 @Override
6297 public void run() {
6298 final int viewCount;
6299 final int viewRectCount;
6300 synchronized (this) {
6301 mPosted = false;
6302
6303 viewCount = mViews.size();
6304 if (viewCount != 0) {
6305 mTempViews = mViews.toArray(mTempViews != null
6306 ? mTempViews : new View[viewCount]);
6307 mViews.clear();
6308 }
6309
6310 viewRectCount = mViewRects.size();
6311 if (viewRectCount != 0) {
6312 mTempViewRects = mViewRects.toArray(mTempViewRects != null
6313 ? mTempViewRects : new AttachInfo.InvalidateInfo[viewRectCount]);
6314 mViewRects.clear();
6315 }
6316 }
6317
6318 for (int i = 0; i < viewCount; i++) {
6319 mTempViews[i].invalidate();
Adam Powell3e3c4ee2012-05-16 17:34:21 -07006320 mTempViews[i] = null;
Jeff Brown6cb7b462012-03-05 13:21:17 -08006321 }
6322
6323 for (int i = 0; i < viewRectCount; i++) {
6324 final View.AttachInfo.InvalidateInfo info = mTempViewRects[i];
6325 info.target.invalidate(info.left, info.top, info.right, info.bottom);
Svetoslav Ganovabae2a12012-11-27 16:59:37 -08006326 info.recycle();
Jeff Brown6cb7b462012-03-05 13:21:17 -08006327 }
6328 }
6329
6330 private void postIfNeededLocked() {
6331 if (!mPosted) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07006332 mChoreographer.postCallback(Choreographer.CALLBACK_ANIMATION, this, null);
Jeff Brown6cb7b462012-03-05 13:21:17 -08006333 mPosted = true;
6334 }
6335 }
6336 }
6337 final InvalidateOnAnimationRunnable mInvalidateOnAnimationRunnable =
6338 new InvalidateOnAnimationRunnable();
6339
Jeff Browna175a5b2012-02-15 19:18:31 -08006340 public void dispatchInvalidateDelayed(View view, long delayMilliseconds) {
6341 Message msg = mHandler.obtainMessage(MSG_INVALIDATE, view);
6342 mHandler.sendMessageDelayed(msg, delayMilliseconds);
6343 }
6344
Jeff Browna175a5b2012-02-15 19:18:31 -08006345 public void dispatchInvalidateRectDelayed(AttachInfo.InvalidateInfo info,
6346 long delayMilliseconds) {
6347 final Message msg = mHandler.obtainMessage(MSG_INVALIDATE_RECT, info);
6348 mHandler.sendMessageDelayed(msg, delayMilliseconds);
6349 }
6350
Jeff Brown6cb7b462012-03-05 13:21:17 -08006351 public void dispatchInvalidateOnAnimation(View view) {
6352 mInvalidateOnAnimationRunnable.addView(view);
6353 }
6354
6355 public void dispatchInvalidateRectOnAnimation(AttachInfo.InvalidateInfo info) {
6356 mInvalidateOnAnimationRunnable.addViewRect(info);
6357 }
6358
6359 public void cancelInvalidate(View view) {
6360 mHandler.removeMessages(MSG_INVALIDATE, view);
6361 // fixme: might leak the AttachInfo.InvalidateInfo objects instead of returning
6362 // them to the pool
6363 mHandler.removeMessages(MSG_INVALIDATE_RECT, view);
6364 mInvalidateOnAnimationRunnable.removeView(view);
6365 }
6366
keunyoung30f420f2013-08-02 14:23:10 -07006367 public void dispatchInputEvent(InputEvent event) {
Jae Seo6a6059a2014-04-17 21:35:29 -07006368 dispatchInputEvent(event, null);
6369 }
6370
6371 public void dispatchInputEvent(InputEvent event, InputEventReceiver receiver) {
6372 SomeArgs args = SomeArgs.obtain();
6373 args.arg1 = event;
6374 args.arg2 = receiver;
6375 Message msg = mHandler.obtainMessage(MSG_DISPATCH_INPUT_EVENT, args);
Jeff Browne0dbd002012-02-15 19:34:58 -08006376 msg.setAsynchronous(true);
Jeff Browna175a5b2012-02-15 19:18:31 -08006377 mHandler.sendMessage(msg);
6378 }
6379
Michael Wright899d7052014-04-23 17:23:39 -07006380 public void synthesizeInputEvent(InputEvent event) {
6381 Message msg = mHandler.obtainMessage(MSG_SYNTHESIZE_INPUT_EVENT, event);
6382 msg.setAsynchronous(true);
6383 mHandler.sendMessage(msg);
6384 }
6385
Jeff Browna175a5b2012-02-15 19:18:31 -08006386 public void dispatchKeyFromIme(KeyEvent event) {
6387 Message msg = mHandler.obtainMessage(MSG_DISPATCH_KEY_FROM_IME, event);
Jeff Browne0dbd002012-02-15 19:34:58 -08006388 msg.setAsynchronous(true);
Jeff Browna175a5b2012-02-15 19:18:31 -08006389 mHandler.sendMessage(msg);
Jeff Browncb1404e2011-01-15 18:14:15 -08006390 }
6391
Michael Wright899d7052014-04-23 17:23:39 -07006392 /**
6393 * Reinject unhandled {@link InputEvent}s in order to synthesize fallbacks events.
6394 *
6395 * Note that it is the responsibility of the caller of this API to recycle the InputEvent it
6396 * passes in.
6397 */
Michael Wright3da28342014-04-22 17:00:11 -07006398 public void dispatchUnhandledInputEvent(InputEvent event) {
Michael Wright899d7052014-04-23 17:23:39 -07006399 if (event instanceof MotionEvent) {
6400 event = MotionEvent.obtain((MotionEvent) event);
Michael Wright3da28342014-04-22 17:00:11 -07006401 }
Michael Wright899d7052014-04-23 17:23:39 -07006402 synthesizeInputEvent(event);
Michael Wright3da28342014-04-22 17:00:11 -07006403 }
6404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006405 public void dispatchAppVisibility(boolean visible) {
Jeff Browna175a5b2012-02-15 19:18:31 -08006406 Message msg = mHandler.obtainMessage(MSG_DISPATCH_APP_VISIBILITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006407 msg.arg1 = visible ? 1 : 0;
Jeff Browna175a5b2012-02-15 19:18:31 -08006408 mHandler.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006409 }
6410
6411 public void dispatchGetNewSurface() {
Jeff Browna175a5b2012-02-15 19:18:31 -08006412 Message msg = mHandler.obtainMessage(MSG_DISPATCH_GET_NEW_SURFACE);
6413 mHandler.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006414 }
6415
6416 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
6417 Message msg = Message.obtain();
Jeff Browna175a5b2012-02-15 19:18:31 -08006418 msg.what = MSG_WINDOW_FOCUS_CHANGED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006419 msg.arg1 = hasFocus ? 1 : 0;
6420 msg.arg2 = inTouchMode ? 1 : 0;
Jeff Browna175a5b2012-02-15 19:18:31 -08006421 mHandler.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006422 }
6423
Craig Mautner9c795042014-10-28 19:59:59 -07006424 public void dispatchWindowShown() {
6425 mHandler.sendEmptyMessage(MSG_DISPATCH_WINDOW_SHOWN);
6426 }
6427
Dianne Hackbornffa42482009-09-23 22:20:11 -07006428 public void dispatchCloseSystemDialogs(String reason) {
6429 Message msg = Message.obtain();
Jeff Browna175a5b2012-02-15 19:18:31 -08006430 msg.what = MSG_CLOSE_SYSTEM_DIALOGS;
Dianne Hackbornffa42482009-09-23 22:20:11 -07006431 msg.obj = reason;
Jeff Browna175a5b2012-02-15 19:18:31 -08006432 mHandler.sendMessage(msg);
Dianne Hackbornffa42482009-09-23 22:20:11 -07006433 }
Christopher Tatea53146c2010-09-07 11:57:52 -07006434
6435 public void dispatchDragEvent(DragEvent event) {
Chris Tate91e9bb32010-10-12 12:58:43 -07006436 final int what;
6437 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
Jeff Browna175a5b2012-02-15 19:18:31 -08006438 what = MSG_DISPATCH_DRAG_LOCATION_EVENT;
6439 mHandler.removeMessages(what);
Chris Tate91e9bb32010-10-12 12:58:43 -07006440 } else {
Jeff Browna175a5b2012-02-15 19:18:31 -08006441 what = MSG_DISPATCH_DRAG_EVENT;
Chris Tate91e9bb32010-10-12 12:58:43 -07006442 }
Jeff Browna175a5b2012-02-15 19:18:31 -08006443 Message msg = mHandler.obtainMessage(what, event);
6444 mHandler.sendMessage(msg);
Christopher Tatea53146c2010-09-07 11:57:52 -07006445 }
6446
Vladislav Kaznacheevec6a4472016-01-22 12:21:52 -08006447 public void updatePointerIcon(float x, float y) {
6448 final int what = MSG_UPDATE_POINTER_ICON;
6449 mHandler.removeMessages(what);
6450 final long now = SystemClock.uptimeMillis();
6451 final MotionEvent event = MotionEvent.obtain(
6452 0, now, MotionEvent.ACTION_HOVER_MOVE, x, y, 0);
6453 Message msg = mHandler.obtainMessage(what, event);
6454 mHandler.sendMessage(msg);
6455 }
6456
Dianne Hackborn9a230e02011-10-06 11:51:27 -07006457 public void dispatchSystemUiVisibilityChanged(int seq, int globalVisibility,
6458 int localValue, int localChanges) {
6459 SystemUiVisibilityInfo args = new SystemUiVisibilityInfo();
6460 args.seq = seq;
6461 args.globalVisibility = globalVisibility;
6462 args.localValue = localValue;
6463 args.localChanges = localChanges;
Jeff Browna175a5b2012-02-15 19:18:31 -08006464 mHandler.sendMessage(mHandler.obtainMessage(MSG_DISPATCH_SYSTEM_UI_VISIBILITY, args));
6465 }
6466
6467 public void dispatchCheckFocus() {
6468 if (!mHandler.hasMessages(MSG_CHECK_FOCUS)) {
6469 // This will result in a call to checkFocus() below.
6470 mHandler.sendEmptyMessage(MSG_CHECK_FOCUS);
6471 }
Joe Onorato664644d2011-01-23 17:53:23 -08006472 }
6473
Clara Bayarri75e09792015-07-29 16:20:40 +01006474 public void dispatchRequestKeyboardShortcuts(IResultReceiver receiver) {
6475 mHandler.obtainMessage(MSG_REQUEST_KEYBOARD_SHORTCUTS, receiver).sendToTarget();
6476 }
6477
svetoslavganov75986cf2009-05-14 22:28:01 -07006478 /**
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07006479 * Post a callback to send a
6480 * {@link AccessibilityEvent#TYPE_WINDOW_CONTENT_CHANGED} event.
Svetoslav Ganova0156172011-06-26 17:55:44 -07006481 * This event is send at most once every
6482 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07006483 */
Alan Viverette77e9a282013-09-12 17:16:09 -07006484 private void postSendWindowContentChangedCallback(View source, int changeType) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006485 if (mSendWindowContentChangedAccessibilityEvent == null) {
6486 mSendWindowContentChangedAccessibilityEvent =
6487 new SendWindowContentChangedAccessibilityEvent();
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07006488 }
Alan Viverette77e9a282013-09-12 17:16:09 -07006489 mSendWindowContentChangedAccessibilityEvent.runOrPost(source, changeType);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07006490 }
6491
6492 /**
6493 * Remove a posted callback to send a
6494 * {@link AccessibilityEvent#TYPE_WINDOW_CONTENT_CHANGED} event.
6495 */
6496 private void removeSendWindowContentChangedCallback() {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006497 if (mSendWindowContentChangedAccessibilityEvent != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08006498 mHandler.removeCallbacks(mSendWindowContentChangedAccessibilityEvent);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07006499 }
6500 }
6501
Igor Murashkina86ab6402013-08-30 12:58:36 -07006502 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006503 public boolean showContextMenuForChild(View originalView) {
6504 return false;
6505 }
6506
Igor Murashkina86ab6402013-08-30 12:58:36 -07006507 @Override
Oren Blasberged391262015-09-01 12:12:51 -07006508 public boolean showContextMenuForChild(View originalView, float x, float y) {
6509 return false;
6510 }
6511
6512 @Override
Adam Powell6e346362010-07-23 10:18:23 -07006513 public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) {
6514 return null;
6515 }
6516
Igor Murashkina86ab6402013-08-30 12:58:36 -07006517 @Override
Clara Bayarri4423d912015-03-02 19:42:48 +00006518 public ActionMode startActionModeForChild(
6519 View originalView, ActionMode.Callback callback, int type) {
6520 return null;
6521 }
6522
6523 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006524 public void createContextMenu(ContextMenu menu) {
6525 }
6526
Igor Murashkina86ab6402013-08-30 12:58:36 -07006527 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006528 public void childDrawableStateChanged(View child) {
6529 }
6530
Igor Murashkina86ab6402013-08-30 12:58:36 -07006531 @Override
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006532 public boolean requestSendAccessibilityEvent(View child, AccessibilityEvent event) {
George Mount41725de2015-04-09 08:23:05 -07006533 if (mView == null || mStopped || mPausedForTransition) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006534 return false;
6535 }
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006536 // Intercept accessibility focus events fired by virtual nodes to keep
6537 // track of accessibility focus position in such nodes.
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006538 final int eventType = event.getEventType();
6539 switch (eventType) {
6540 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006541 final long sourceNodeId = event.getSourceNodeId();
6542 final int accessibilityViewId = AccessibilityNodeInfo.getAccessibilityViewId(
6543 sourceNodeId);
6544 View source = mView.findViewByAccessibilityId(accessibilityViewId);
6545 if (source != null) {
6546 AccessibilityNodeProvider provider = source.getAccessibilityNodeProvider();
6547 if (provider != null) {
Svetoslavb3ba1d42014-09-26 15:20:40 -07006548 final int virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
6549 sourceNodeId);
6550 final AccessibilityNodeInfo node;
6551 if (virtualNodeId == AccessibilityNodeInfo.UNDEFINED_ITEM_ID) {
6552 node = provider.createAccessibilityNodeInfo(
6553 AccessibilityNodeProvider.HOST_VIEW_ID);
6554 } else {
6555 node = provider.createAccessibilityNodeInfo(virtualNodeId);
6556 }
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006557 setAccessibilityFocus(source, node);
6558 }
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006559 }
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006560 } break;
6561 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006562 final long sourceNodeId = event.getSourceNodeId();
6563 final int accessibilityViewId = AccessibilityNodeInfo.getAccessibilityViewId(
6564 sourceNodeId);
6565 View source = mView.findViewByAccessibilityId(accessibilityViewId);
6566 if (source != null) {
6567 AccessibilityNodeProvider provider = source.getAccessibilityNodeProvider();
6568 if (provider != null) {
6569 setAccessibilityFocus(null, null);
6570 }
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006571 }
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006572 } break;
Svetoslavf0c758b2014-09-03 17:47:37 -07006573
6574
6575 case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED: {
Alan Viverette34457f52015-03-25 13:09:20 -07006576 handleWindowContentChangedEvent(event);
Svetoslavf0c758b2014-09-03 17:47:37 -07006577 } break;
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006578 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006579 mAccessibilityManager.sendAccessibilityEvent(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006580 return true;
6581 }
6582
Alan Viverette34457f52015-03-25 13:09:20 -07006583 /**
6584 * Updates the focused virtual view, when necessary, in response to a
6585 * content changed event.
6586 * <p>
6587 * This is necessary to get updated bounds after a position change.
6588 *
6589 * @param event an accessibility event of type
6590 * {@link AccessibilityEvent#TYPE_WINDOW_CONTENT_CHANGED}
6591 */
6592 private void handleWindowContentChangedEvent(AccessibilityEvent event) {
Alan Viverette25acc7e2015-05-19 11:32:08 -07006593 final View focusedHost = mAccessibilityFocusedHost;
6594 if (focusedHost == null || mAccessibilityFocusedVirtualView == null) {
6595 // No virtual view focused, nothing to do here.
Alan Viverette34457f52015-03-25 13:09:20 -07006596 return;
6597 }
6598
Alan Viverette25acc7e2015-05-19 11:32:08 -07006599 final AccessibilityNodeProvider provider = focusedHost.getAccessibilityNodeProvider();
Alan Viverette34457f52015-03-25 13:09:20 -07006600 if (provider == null) {
Alan Viverette25acc7e2015-05-19 11:32:08 -07006601 // Error state: virtual view with no provider. Clear focus.
6602 mAccessibilityFocusedHost = null;
6603 mAccessibilityFocusedVirtualView = null;
6604 focusedHost.clearAccessibilityFocusNoCallbacks();
Alan Viverette34457f52015-03-25 13:09:20 -07006605 return;
6606 }
6607
6608 // We only care about change types that may affect the bounds of the
6609 // focused virtual view.
6610 final int changes = event.getContentChangeTypes();
6611 if ((changes & AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE) == 0
6612 && changes != AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED) {
6613 return;
6614 }
6615
6616 final long eventSourceNodeId = event.getSourceNodeId();
6617 final int changedViewId = AccessibilityNodeInfo.getAccessibilityViewId(eventSourceNodeId);
6618
6619 // Search up the tree for subtree containment.
6620 boolean hostInSubtree = false;
6621 View root = mAccessibilityFocusedHost;
6622 while (root != null && !hostInSubtree) {
6623 if (changedViewId == root.getAccessibilityViewId()) {
6624 hostInSubtree = true;
6625 } else {
6626 final ViewParent parent = root.getParent();
6627 if (parent instanceof View) {
6628 root = (View) parent;
6629 } else {
6630 root = null;
6631 }
6632 }
6633 }
6634
6635 // We care only about changes in subtrees containing the host view.
6636 if (!hostInSubtree) {
6637 return;
6638 }
6639
6640 final long focusedSourceNodeId = mAccessibilityFocusedVirtualView.getSourceNodeId();
6641 int focusedChildId = AccessibilityNodeInfo.getVirtualDescendantId(focusedSourceNodeId);
6642 if (focusedChildId == AccessibilityNodeInfo.UNDEFINED_ITEM_ID) {
6643 // TODO: Should we clear the focused virtual view?
6644 focusedChildId = AccessibilityNodeProvider.HOST_VIEW_ID;
6645 }
6646
6647 // Refresh the node for the focused virtual view.
Alan Viverettea7ea65e2015-05-15 11:30:21 -07006648 final Rect oldBounds = mTempRect;
6649 mAccessibilityFocusedVirtualView.getBoundsInScreen(oldBounds);
Alan Viverette34457f52015-03-25 13:09:20 -07006650 mAccessibilityFocusedVirtualView = provider.createAccessibilityNodeInfo(focusedChildId);
Alan Viverette25acc7e2015-05-19 11:32:08 -07006651 if (mAccessibilityFocusedVirtualView == null) {
6652 // Error state: The node no longer exists. Clear focus.
6653 mAccessibilityFocusedHost = null;
6654 focusedHost.clearAccessibilityFocusNoCallbacks();
6655
6656 // This will probably fail, but try to keep the provider's internal
6657 // state consistent by clearing focus.
6658 provider.performAction(focusedChildId,
6659 AccessibilityAction.ACTION_CLEAR_ACCESSIBILITY_FOCUS.getId(), null);
Alan Viverettea7ea65e2015-05-15 11:30:21 -07006660 invalidateRectOnScreen(oldBounds);
Alan Viverette25acc7e2015-05-19 11:32:08 -07006661 } else {
6662 // The node was refreshed, invalidate bounds if necessary.
6663 final Rect newBounds = mAccessibilityFocusedVirtualView.getBoundsInScreen();
6664 if (!oldBounds.equals(newBounds)) {
6665 oldBounds.union(newBounds);
6666 invalidateRectOnScreen(oldBounds);
6667 }
Alan Viverettea7ea65e2015-05-15 11:30:21 -07006668 }
Alan Viverette34457f52015-03-25 13:09:20 -07006669 }
6670
Svetoslav Ganov42138042012-03-20 11:51:39 -07006671 @Override
Alan Viverette77e9a282013-09-12 17:16:09 -07006672 public void notifySubtreeAccessibilityStateChanged(View child, View source, int changeType) {
6673 postSendWindowContentChangedCallback(source, changeType);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006674 }
6675
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -08006676 @Override
6677 public boolean canResolveLayoutDirection() {
6678 return true;
6679 }
6680
6681 @Override
6682 public boolean isLayoutDirectionResolved() {
6683 return true;
6684 }
6685
6686 @Override
6687 public int getLayoutDirection() {
6688 return View.LAYOUT_DIRECTION_RESOLVED_DEFAULT;
6689 }
6690
6691 @Override
6692 public boolean canResolveTextDirection() {
6693 return true;
6694 }
6695
6696 @Override
6697 public boolean isTextDirectionResolved() {
6698 return true;
6699 }
6700
6701 @Override
6702 public int getTextDirection() {
6703 return View.TEXT_DIRECTION_RESOLVED_DEFAULT;
6704 }
6705
6706 @Override
6707 public boolean canResolveTextAlignment() {
6708 return true;
6709 }
6710
6711 @Override
6712 public boolean isTextAlignmentResolved() {
6713 return true;
6714 }
6715
6716 @Override
6717 public int getTextAlignment() {
6718 return View.TEXT_ALIGNMENT_RESOLVED_DEFAULT;
6719 }
6720
Svetoslav Ganov42138042012-03-20 11:51:39 -07006721 private View getCommonPredecessor(View first, View second) {
Chris Craikd36a81f2014-07-17 10:16:51 -07006722 if (mTempHashSet == null) {
6723 mTempHashSet = new HashSet<View>();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006724 }
Chris Craikd36a81f2014-07-17 10:16:51 -07006725 HashSet<View> seen = mTempHashSet;
6726 seen.clear();
6727 View firstCurrent = first;
6728 while (firstCurrent != null) {
6729 seen.add(firstCurrent);
6730 ViewParent firstCurrentParent = firstCurrent.mParent;
6731 if (firstCurrentParent instanceof View) {
6732 firstCurrent = (View) firstCurrentParent;
6733 } else {
6734 firstCurrent = null;
6735 }
6736 }
6737 View secondCurrent = second;
6738 while (secondCurrent != null) {
6739 if (seen.contains(secondCurrent)) {
6740 seen.clear();
6741 return secondCurrent;
6742 }
6743 ViewParent secondCurrentParent = secondCurrent.mParent;
6744 if (secondCurrentParent instanceof View) {
6745 secondCurrent = (View) secondCurrentParent;
6746 } else {
6747 secondCurrent = null;
6748 }
6749 }
6750 seen.clear();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006751 return null;
6752 }
6753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006754 void checkThread() {
6755 if (mThread != Thread.currentThread()) {
6756 throw new CalledFromWrongThreadException(
6757 "Only the original thread that created a view hierarchy can touch its views.");
6758 }
6759 }
6760
Igor Murashkina86ab6402013-08-30 12:58:36 -07006761 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006762 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07006763 // ViewAncestor never intercepts touch event, so this can be a no-op
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006764 }
6765
Igor Murashkina86ab6402013-08-30 12:58:36 -07006766 @Override
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07006767 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Yigit Boyard62d5e92016-01-19 18:56:20 -08006768 if (rectangle == null) {
6769 return scrollToRectOrFocus(null, immediate);
6770 }
6771 rectangle.offset(child.getLeft() - child.getScrollX(),
6772 child.getTop() - child.getScrollY());
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07006773 final boolean scrolled = scrollToRectOrFocus(rectangle, immediate);
Yigit Boyard62d5e92016-01-19 18:56:20 -08006774 mTempRect.set(rectangle);
6775 mTempRect.offset(0, -mCurScrollY);
6776 mTempRect.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
6777 try {
6778 mWindowSession.onRectangleOnScreenRequested(mWindow, mTempRect);
6779 } catch (RemoteException re) {
6780 /* ignore */
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07006781 }
6782 return scrolled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006783 }
Romain Guy8506ab42009-06-11 17:35:47 -07006784
Igor Murashkina86ab6402013-08-30 12:58:36 -07006785 @Override
Adam Powell539ee872012-02-03 19:00:49 -08006786 public void childHasTransientStateChanged(View child, boolean hasTransientState) {
6787 // Do nothing.
6788 }
6789
Adam Powell10ba2772014-04-15 09:46:51 -07006790 @Override
6791 public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
6792 return false;
6793 }
6794
6795 @Override
6796 public void onStopNestedScroll(View target) {
6797 }
6798
6799 @Override
6800 public void onNestedScrollAccepted(View child, View target, int nestedScrollAxes) {
6801 }
6802
6803 @Override
6804 public void onNestedScroll(View target, int dxConsumed, int dyConsumed,
6805 int dxUnconsumed, int dyUnconsumed) {
6806 }
6807
6808 @Override
6809 public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
6810 }
6811
6812 @Override
Adam Powellb36e4f92014-05-01 10:23:33 -07006813 public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
Adam Powell10ba2772014-04-15 09:46:51 -07006814 return false;
6815 }
6816
Adam Powellb72be592014-07-16 21:41:31 -07006817 @Override
6818 public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
6819 return false;
6820 }
6821
Adam Powellb6ab0982015-01-07 17:00:12 -08006822 @Override
6823 public boolean onNestedPrePerformAccessibilityAction(View target, int action, Bundle args) {
6824 return false;
6825 }
6826
Jorim Jaggib774e552015-08-24 14:52:45 -07006827 /**
6828 * Force the window to report its next draw.
6829 * <p>
6830 * This method is only supposed to be used to speed up the interaction from SystemUI and window
6831 * manager when waiting for the first frame to be drawn when turning on the screen. DO NOT USE
6832 * unless you fully understand this interaction.
6833 * @hide
6834 */
6835 public void setReportNextDraw() {
6836 mReportNextDraw = true;
6837 invalidate();
6838 }
6839
Craig Mautnerbc57cd12013-08-19 15:47:42 -07006840 void changeCanvasOpacity(boolean opaque) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08006841 Log.d(mTag, "changeCanvasOpacity: opaque=" + opaque);
John Reck63a06672014-05-07 13:45:54 -07006842 if (mAttachInfo.mHardwareRenderer != null) {
6843 mAttachInfo.mHardwareRenderer.setOpaque(opaque);
6844 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -07006845 }
6846
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07006847 class TakenSurfaceHolder extends BaseSurfaceHolder {
6848 @Override
6849 public boolean onAllowLockCanvas() {
6850 return mDrawingAllowed;
6851 }
6852
6853 @Override
6854 public void onRelayoutContainer() {
6855 // Not currently interesting -- from changing between fixed and layout size.
6856 }
6857
Igor Murashkina86ab6402013-08-30 12:58:36 -07006858 @Override
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07006859 public void setFormat(int format) {
6860 ((RootViewSurfaceTaker)mView).setSurfaceFormat(format);
6861 }
6862
Igor Murashkina86ab6402013-08-30 12:58:36 -07006863 @Override
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07006864 public void setType(int type) {
6865 ((RootViewSurfaceTaker)mView).setSurfaceType(type);
6866 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07006867
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07006868 @Override
6869 public void onUpdateSurface() {
6870 // We take care of format and type changes on our own.
6871 throw new IllegalStateException("Shouldn't be here");
6872 }
6873
Igor Murashkina86ab6402013-08-30 12:58:36 -07006874 @Override
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07006875 public boolean isCreating() {
6876 return mIsCreating;
6877 }
6878
6879 @Override
6880 public void setFixedSize(int width, int height) {
6881 throw new UnsupportedOperationException(
6882 "Currently only support sizing from layout");
6883 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07006884
6885 @Override
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07006886 public void setKeepScreenOn(boolean screenOn) {
6887 ((RootViewSurfaceTaker)mView).setSurfaceKeepScreenOn(screenOn);
6888 }
6889 }
Romain Guy8506ab42009-06-11 17:35:47 -07006890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006891 static class W extends IWindow.Stub {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006892 private final WeakReference<ViewRootImpl> mViewAncestor;
Jeff Brown98365d72012-08-19 20:30:52 -07006893 private final IWindowSession mWindowSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006894
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006895 W(ViewRootImpl viewAncestor) {
6896 mViewAncestor = new WeakReference<ViewRootImpl>(viewAncestor);
Jeff Brown98365d72012-08-19 20:30:52 -07006897 mWindowSession = viewAncestor.mWindowSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006898 }
6899
Igor Murashkina86ab6402013-08-30 12:58:36 -07006900 @Override
Dianne Hackbornc4aad012013-02-22 15:05:25 -08006901 public void resized(Rect frame, Rect overscanInsets, Rect contentInsets,
Filip Gruszczynski2217f612015-05-26 11:32:08 -07006902 Rect visibleInsets, Rect stableInsets, Rect outsets, boolean reportDraw,
Jorim Jaggi0ffd49c2016-02-12 15:04:21 -08006903 Configuration newConfig, Rect backDropFrame, boolean forceLayout,
6904 boolean alwaysConsumeNavBar) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006905 final ViewRootImpl viewAncestor = mViewAncestor.get();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006906 if (viewAncestor != null) {
Dianne Hackbornc4aad012013-02-22 15:05:25 -08006907 viewAncestor.dispatchResized(frame, overscanInsets, contentInsets,
Jorim Jaggia4a58ef2016-01-27 02:10:08 -08006908 visibleInsets, stableInsets, outsets, reportDraw, newConfig, backDropFrame,
Jorim Jaggi0ffd49c2016-02-12 15:04:21 -08006909 forceLayout, alwaysConsumeNavBar);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006910 }
6911 }
6912
Craig Mautner5702d4d2012-06-30 14:10:16 -07006913 @Override
6914 public void moved(int newX, int newY) {
6915 final ViewRootImpl viewAncestor = mViewAncestor.get();
6916 if (viewAncestor != null) {
6917 viewAncestor.dispatchMoved(newX, newY);
6918 }
6919 }
6920
Igor Murashkina86ab6402013-08-30 12:58:36 -07006921 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006922 public void dispatchAppVisibility(boolean visible) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006923 final ViewRootImpl viewAncestor = mViewAncestor.get();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006924 if (viewAncestor != null) {
6925 viewAncestor.dispatchAppVisibility(visible);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006926 }
6927 }
6928
Igor Murashkina86ab6402013-08-30 12:58:36 -07006929 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006930 public void dispatchGetNewSurface() {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006931 final ViewRootImpl viewAncestor = mViewAncestor.get();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006932 if (viewAncestor != null) {
6933 viewAncestor.dispatchGetNewSurface();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006934 }
6935 }
6936
Igor Murashkina86ab6402013-08-30 12:58:36 -07006937 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006938 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006939 final ViewRootImpl viewAncestor = mViewAncestor.get();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006940 if (viewAncestor != null) {
6941 viewAncestor.windowFocusChanged(hasFocus, inTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006942 }
6943 }
6944
6945 private static int checkCallingPermission(String permission) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006946 try {
6947 return ActivityManagerNative.getDefault().checkPermission(
6948 permission, Binder.getCallingPid(), Binder.getCallingUid());
6949 } catch (RemoteException e) {
6950 return PackageManager.PERMISSION_DENIED;
6951 }
6952 }
6953
Igor Murashkina86ab6402013-08-30 12:58:36 -07006954 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006955 public void executeCommand(String command, String parameters, ParcelFileDescriptor out) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006956 final ViewRootImpl viewAncestor = mViewAncestor.get();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006957 if (viewAncestor != null) {
6958 final View view = viewAncestor.mView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006959 if (view != null) {
6960 if (checkCallingPermission(Manifest.permission.DUMP) !=
6961 PackageManager.PERMISSION_GRANTED) {
6962 throw new SecurityException("Insufficient permissions to invoke"
6963 + " executeCommand() from pid=" + Binder.getCallingPid()
6964 + ", uid=" + Binder.getCallingUid());
6965 }
6966
6967 OutputStream clientStream = null;
6968 try {
6969 clientStream = new ParcelFileDescriptor.AutoCloseOutputStream(out);
6970 ViewDebug.dispatchCommand(view, command, parameters, clientStream);
6971 } catch (IOException e) {
6972 e.printStackTrace();
6973 } finally {
6974 if (clientStream != null) {
6975 try {
6976 clientStream.close();
6977 } catch (IOException e) {
6978 e.printStackTrace();
6979 }
6980 }
6981 }
6982 }
6983 }
6984 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07006985
6986 @Override
Dianne Hackbornffa42482009-09-23 22:20:11 -07006987 public void closeSystemDialogs(String reason) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006988 final ViewRootImpl viewAncestor = mViewAncestor.get();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006989 if (viewAncestor != null) {
6990 viewAncestor.dispatchCloseSystemDialogs(reason);
Dianne Hackbornffa42482009-09-23 22:20:11 -07006991 }
6992 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07006993
6994 @Override
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006995 public void dispatchWallpaperOffsets(float x, float y, float xStep, float yStep,
6996 boolean sync) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006997 if (sync) {
6998 try {
Jeff Brown98365d72012-08-19 20:30:52 -07006999 mWindowSession.wallpaperOffsetsComplete(asBinder());
Dianne Hackborn19382ac2009-09-11 21:13:37 -07007000 } catch (RemoteException e) {
7001 }
7002 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07007003 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007004
Igor Murashkina86ab6402013-08-30 12:58:36 -07007005 @Override
Dianne Hackborn75804932009-10-20 20:15:20 -07007006 public void dispatchWallpaperCommand(String action, int x, int y,
7007 int z, Bundle extras, boolean sync) {
7008 if (sync) {
7009 try {
Jeff Brown98365d72012-08-19 20:30:52 -07007010 mWindowSession.wallpaperCommandComplete(asBinder(), null);
Dianne Hackborn75804932009-10-20 20:15:20 -07007011 } catch (RemoteException e) {
7012 }
7013 }
7014 }
Christopher Tatea53146c2010-09-07 11:57:52 -07007015
7016 /* Drag/drop */
Igor Murashkina86ab6402013-08-30 12:58:36 -07007017 @Override
Christopher Tatea53146c2010-09-07 11:57:52 -07007018 public void dispatchDragEvent(DragEvent event) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007019 final ViewRootImpl viewAncestor = mViewAncestor.get();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007020 if (viewAncestor != null) {
7021 viewAncestor.dispatchDragEvent(event);
Christopher Tatea53146c2010-09-07 11:57:52 -07007022 }
7023 }
Joe Onorato664644d2011-01-23 17:53:23 -08007024
Igor Murashkina86ab6402013-08-30 12:58:36 -07007025 @Override
Vladislav Kaznacheevec6a4472016-01-22 12:21:52 -08007026 public void updatePointerIcon(float x, float y) {
7027 final ViewRootImpl viewAncestor = mViewAncestor.get();
7028 if (viewAncestor != null) {
7029 viewAncestor.updatePointerIcon(x, y);
7030 }
7031 }
7032
7033 @Override
Dianne Hackborn9a230e02011-10-06 11:51:27 -07007034 public void dispatchSystemUiVisibilityChanged(int seq, int globalVisibility,
7035 int localValue, int localChanges) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007036 final ViewRootImpl viewAncestor = mViewAncestor.get();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007037 if (viewAncestor != null) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -07007038 viewAncestor.dispatchSystemUiVisibilityChanged(seq, globalVisibility,
7039 localValue, localChanges);
Joe Onorato664644d2011-01-23 17:53:23 -08007040 }
7041 }
Dianne Hackborn12d3a942012-04-27 14:16:30 -07007042
Igor Murashkina86ab6402013-08-30 12:58:36 -07007043 @Override
Craig Mautner9c795042014-10-28 19:59:59 -07007044 public void dispatchWindowShown() {
7045 final ViewRootImpl viewAncestor = mViewAncestor.get();
7046 if (viewAncestor != null) {
7047 viewAncestor.dispatchWindowShown();
7048 }
7049 }
Clara Bayarri75e09792015-07-29 16:20:40 +01007050
7051 @Override
7052 public void requestAppKeyboardShortcuts(IResultReceiver receiver) {
7053 ViewRootImpl viewAncestor = mViewAncestor.get();
7054 if (viewAncestor != null) {
7055 viewAncestor.dispatchRequestKeyboardShortcuts(receiver);
7056 }
7057 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007058 }
7059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007060 public static final class CalledFromWrongThreadException extends AndroidRuntimeException {
7061 public CalledFromWrongThreadException(String msg) {
7062 super(msg);
7063 }
7064 }
7065
Alan Viverettebea0c7da2015-09-01 16:00:20 -04007066 static HandlerActionQueue getRunQueue() {
7067 HandlerActionQueue rq = sRunQueues.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007068 if (rq != null) {
7069 return rq;
7070 }
Alan Viverettebea0c7da2015-09-01 16:00:20 -04007071 rq = new HandlerActionQueue();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007072 sRunQueues.set(rq);
7073 return rq;
7074 }
Romain Guy8506ab42009-06-11 17:35:47 -07007075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007076 /**
Skuhneb8160872015-09-22 09:51:39 -07007077 * Start a drag resizing which will inform all listeners that a window resize is taking place.
7078 */
Jorim Jaggi9511b0f2016-01-29 19:12:44 -08007079 private void startDragResizing(Rect initialBounds, boolean fullscreen, Rect systemInsets,
7080 Rect stableInsets) {
Skuhneb8160872015-09-22 09:51:39 -07007081 if (!mDragResizing) {
7082 mDragResizing = true;
Chong Zhangdcee1de2015-10-06 10:26:00 -07007083 synchronized (mWindowCallbacks) {
7084 for (int i = mWindowCallbacks.size() - 1; i >= 0; i--) {
Jorim Jaggi9511b0f2016-01-29 19:12:44 -08007085 mWindowCallbacks.get(i).onWindowDragResizeStart(initialBounds, fullscreen,
7086 systemInsets, stableInsets);
Skuhneb8160872015-09-22 09:51:39 -07007087 }
7088 }
7089 mFullRedrawNeeded = true;
7090 }
7091 }
7092
7093 /**
7094 * End a drag resize which will inform all listeners that a window resize has ended.
7095 */
7096 private void endDragResizing() {
7097 if (mDragResizing) {
7098 mDragResizing = false;
Chong Zhangdcee1de2015-10-06 10:26:00 -07007099 synchronized (mWindowCallbacks) {
7100 for (int i = mWindowCallbacks.size() - 1; i >= 0; i--) {
7101 mWindowCallbacks.get(i).onWindowDragResizeEnd();
Skuhneb8160872015-09-22 09:51:39 -07007102 }
7103 }
7104 mFullRedrawNeeded = true;
7105 }
7106 }
7107
Chong Zhang8dbd9ad2015-10-09 10:06:11 -07007108 private boolean updateContentDrawBounds() {
7109 boolean updated = false;
7110 synchronized (mWindowCallbacks) {
7111 for (int i = mWindowCallbacks.size() - 1; i >= 0; i--) {
7112 updated |= mWindowCallbacks.get(i).onContentDrawn(
7113 mWindowAttributes.surfaceInsets.left,
7114 mWindowAttributes.surfaceInsets.top,
7115 mWidth, mHeight);
7116 }
7117 }
7118 return updated | (mDragResizing && mReportNextDraw);
7119 }
7120
7121 private void requestDrawWindow() {
7122 if (mReportNextDraw) {
7123 mWindowDrawCountDown = new CountDownLatch(mWindowCallbacks.size());
7124 }
7125 synchronized (mWindowCallbacks) {
7126 for (int i = mWindowCallbacks.size() - 1; i >= 0; i--) {
7127 mWindowCallbacks.get(i).onRequestDraw(mReportNextDraw);
7128 }
7129 }
7130 }
7131
Skuhneb8160872015-09-22 09:51:39 -07007132 /**
Jorim Jaggi4846ee32016-01-07 17:39:12 +01007133 * Tells this instance that its corresponding activity has just relaunched. In this case, we
7134 * need to force a relayout of the window to make sure we get the correct bounds from window
7135 * manager.
7136 */
7137 public void reportActivityRelaunched() {
7138 mActivityRelaunched = true;
7139 }
7140
7141 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007142 * Class for managing the accessibility interaction connection
7143 * based on the global accessibility state.
7144 */
7145 final class AccessibilityInteractionConnectionManager
7146 implements AccessibilityStateChangeListener {
Igor Murashkina86ab6402013-08-30 12:58:36 -07007147 @Override
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007148 public void onAccessibilityStateChanged(boolean enabled) {
7149 if (enabled) {
7150 ensureConnection();
Chris Craikcce47eb2014-07-16 15:12:15 -07007151 if (mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov0d04e242012-02-21 13:46:36 -08007152 mView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
7153 View focusedView = mView.findFocus();
7154 if (focusedView != null && focusedView != mView) {
7155 focusedView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
7156 }
7157 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007158 } else {
7159 ensureNoConnection();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07007160 mHandler.obtainMessage(MSG_CLEAR_ACCESSIBILITY_FOCUS_HOST).sendToTarget();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007161 }
7162 }
7163
7164 public void ensureConnection() {
Chris Craikcce47eb2014-07-16 15:12:15 -07007165 final boolean registered =
Svetoslav8e3feb12014-02-24 13:46:47 -08007166 mAttachInfo.mAccessibilityWindowId != AccessibilityNodeInfo.UNDEFINED_ITEM_ID;
Chris Craikcce47eb2014-07-16 15:12:15 -07007167 if (!registered) {
7168 mAttachInfo.mAccessibilityWindowId =
Svetoslav Ganov0d04e242012-02-21 13:46:36 -08007169 mAccessibilityManager.addAccessibilityInteractionConnection(mWindow,
7170 new AccessibilityInteractionConnection(ViewRootImpl.this));
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007171 }
7172 }
7173
7174 public void ensureNoConnection() {
Svetoslav Ganov0d04e242012-02-21 13:46:36 -08007175 final boolean registered =
Svetoslav8e3feb12014-02-24 13:46:47 -08007176 mAttachInfo.mAccessibilityWindowId != AccessibilityNodeInfo.UNDEFINED_ITEM_ID;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007177 if (registered) {
Svetoslav8e3feb12014-02-24 13:46:47 -08007178 mAttachInfo.mAccessibilityWindowId = AccessibilityNodeInfo.UNDEFINED_ITEM_ID;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007179 mAccessibilityManager.removeAccessibilityInteractionConnection(mWindow);
7180 }
7181 }
7182 }
7183
Chris Craikcce47eb2014-07-16 15:12:15 -07007184 final class HighContrastTextManager implements HighTextContrastChangeListener {
7185 HighContrastTextManager() {
7186 mAttachInfo.mHighContrastText = mAccessibilityManager.isHighTextContrastEnabled();
7187 }
7188 @Override
7189 public void onHighTextContrastStateChanged(boolean enabled) {
7190 mAttachInfo.mHighContrastText = enabled;
7191
7192 // Destroy Displaylists so they can be recreated with high contrast recordings
7193 destroyHardwareResources();
Chris Craikd36a81f2014-07-17 10:16:51 -07007194
7195 // Schedule redraw, which will rerecord + redraw all text
7196 invalidate();
Chris Craikcce47eb2014-07-16 15:12:15 -07007197 }
7198 }
7199
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007200 /**
7201 * This class is an interface this ViewAncestor provides to the
7202 * AccessibilityManagerService to the latter can interact with
7203 * the view hierarchy in this ViewAncestor.
7204 */
Svetoslav Ganovaf5b4f42011-10-28 12:41:38 -07007205 static final class AccessibilityInteractionConnection
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007206 extends IAccessibilityInteractionConnection.Stub {
Svetoslav Ganov02107852011-10-03 17:06:56 -07007207 private final WeakReference<ViewRootImpl> mViewRootImpl;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007208
Svetoslav Ganovf79f4762011-10-28 15:40:32 -07007209 AccessibilityInteractionConnection(ViewRootImpl viewRootImpl) {
7210 mViewRootImpl = new WeakReference<ViewRootImpl>(viewRootImpl);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007211 }
7212
Svetoslav Ganov42138042012-03-20 11:51:39 -07007213 @Override
Svetoslav Ganov02107852011-10-03 17:06:56 -07007214 public void findAccessibilityNodeInfoByAccessibilityId(long accessibilityNodeId,
Svetoslav9ae9ed22014-09-02 16:36:35 -07007215 Region interactiveRegion, int interactionId,
7216 IAccessibilityInteractionConnectionCallback callback, int flags,
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07007217 int interrogatingPid, long interrogatingTid, MagnificationSpec spec) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07007218 ViewRootImpl viewRootImpl = mViewRootImpl.get();
7219 if (viewRootImpl != null && viewRootImpl.mView != null) {
Svetoslav Ganovaf5b4f42011-10-28 12:41:38 -07007220 viewRootImpl.getAccessibilityInteractionController()
Svetoslav Ganov02107852011-10-03 17:06:56 -07007221 .findAccessibilityNodeInfoByAccessibilityIdClientThread(accessibilityNodeId,
Svetoslav9ae9ed22014-09-02 16:36:35 -07007222 interactiveRegion, interactionId, callback, flags, interrogatingPid,
7223 interrogatingTid, spec);
Svetoslav Ganov79311c42012-01-17 20:24:26 -08007224 } else {
7225 // We cannot make the call and notify the caller so it does not wait.
7226 try {
7227 callback.setFindAccessibilityNodeInfosResult(null, interactionId);
7228 } catch (RemoteException re) {
7229 /* best effort - ignore */
7230 }
Svetoslav Ganov601ad802011-06-09 14:47:38 -07007231 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007232 }
7233
Svetoslav Ganov42138042012-03-20 11:51:39 -07007234 @Override
Svetoslav Ganov02107852011-10-03 17:06:56 -07007235 public void performAccessibilityAction(long accessibilityNodeId, int action,
Svetoslav Ganovaa780c12012-04-19 23:01:39 -07007236 Bundle arguments, int interactionId,
7237 IAccessibilityInteractionConnectionCallback callback, int flags,
Svet Ganov7498efd2014-09-03 21:33:00 -07007238 int interrogatingPid, long interrogatingTid) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07007239 ViewRootImpl viewRootImpl = mViewRootImpl.get();
7240 if (viewRootImpl != null && viewRootImpl.mView != null) {
Svetoslav Ganovaf5b4f42011-10-28 12:41:38 -07007241 viewRootImpl.getAccessibilityInteractionController()
Svetoslav Ganovaa780c12012-04-19 23:01:39 -07007242 .performAccessibilityActionClientThread(accessibilityNodeId, action, arguments,
Svet Ganov7498efd2014-09-03 21:33:00 -07007243 interactionId, callback, flags, interrogatingPid, interrogatingTid);
Svetoslav Ganov79311c42012-01-17 20:24:26 -08007244 } else {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007245 // We cannot make the call and notify the caller so it does not wait.
Svetoslav Ganov79311c42012-01-17 20:24:26 -08007246 try {
7247 callback.setPerformAccessibilityActionResult(false, interactionId);
7248 } catch (RemoteException re) {
7249 /* best effort - ignore */
7250 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007251 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007252 }
7253
Svetoslav Ganov42138042012-03-20 11:51:39 -07007254 @Override
Svetoslav Ganov80943d82013-01-02 10:25:37 -08007255 public void findAccessibilityNodeInfosByViewId(long accessibilityNodeId,
Svetoslav9ae9ed22014-09-02 16:36:35 -07007256 String viewId, Region interactiveRegion, int interactionId,
Svetoslav Ganov80943d82013-01-02 10:25:37 -08007257 IAccessibilityInteractionConnectionCallback callback, int flags,
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07007258 int interrogatingPid, long interrogatingTid, MagnificationSpec spec) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07007259 ViewRootImpl viewRootImpl = mViewRootImpl.get();
7260 if (viewRootImpl != null && viewRootImpl.mView != null) {
Svetoslav Ganovaf5b4f42011-10-28 12:41:38 -07007261 viewRootImpl.getAccessibilityInteractionController()
Svetoslav Ganov80943d82013-01-02 10:25:37 -08007262 .findAccessibilityNodeInfosByViewIdClientThread(accessibilityNodeId,
Svetoslav9ae9ed22014-09-02 16:36:35 -07007263 viewId, interactiveRegion, interactionId, callback, flags,
7264 interrogatingPid, interrogatingTid, spec);
Svetoslav Ganov79311c42012-01-17 20:24:26 -08007265 } else {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007266 // We cannot make the call and notify the caller so it does not wait.
Svetoslav Ganov79311c42012-01-17 20:24:26 -08007267 try {
7268 callback.setFindAccessibilityNodeInfoResult(null, interactionId);
7269 } catch (RemoteException re) {
7270 /* best effort - ignore */
7271 }
7272 }
7273 }
7274
Svetoslav Ganov42138042012-03-20 11:51:39 -07007275 @Override
Svetoslav Ganov79311c42012-01-17 20:24:26 -08007276 public void findAccessibilityNodeInfosByText(long accessibilityNodeId, String text,
Svetoslav9ae9ed22014-09-02 16:36:35 -07007277 Region interactiveRegion, int interactionId,
7278 IAccessibilityInteractionConnectionCallback callback, int flags,
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07007279 int interrogatingPid, long interrogatingTid, MagnificationSpec spec) {
Svetoslav Ganov79311c42012-01-17 20:24:26 -08007280 ViewRootImpl viewRootImpl = mViewRootImpl.get();
7281 if (viewRootImpl != null && viewRootImpl.mView != null) {
7282 viewRootImpl.getAccessibilityInteractionController()
7283 .findAccessibilityNodeInfosByTextClientThread(accessibilityNodeId, text,
Svetoslav9ae9ed22014-09-02 16:36:35 -07007284 interactiveRegion, interactionId, callback, flags, interrogatingPid,
7285 interrogatingTid, spec);
Svetoslav Ganov79311c42012-01-17 20:24:26 -08007286 } else {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007287 // We cannot make the call and notify the caller so it does not wait.
Svetoslav Ganov79311c42012-01-17 20:24:26 -08007288 try {
7289 callback.setFindAccessibilityNodeInfosResult(null, interactionId);
7290 } catch (RemoteException re) {
7291 /* best effort - ignore */
7292 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007293 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007294 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007295
Svetoslav Ganov42138042012-03-20 11:51:39 -07007296 @Override
Svetoslav9ae9ed22014-09-02 16:36:35 -07007297 public void findFocus(long accessibilityNodeId, int focusType, Region interactiveRegion,
7298 int interactionId, IAccessibilityInteractionConnectionCallback callback, int flags,
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07007299 int interrogatingPid, long interrogatingTid, MagnificationSpec spec) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007300 ViewRootImpl viewRootImpl = mViewRootImpl.get();
7301 if (viewRootImpl != null && viewRootImpl.mView != null) {
7302 viewRootImpl.getAccessibilityInteractionController()
Svetoslav9ae9ed22014-09-02 16:36:35 -07007303 .findFocusClientThread(accessibilityNodeId, focusType, interactiveRegion,
7304 interactionId, callback, flags, interrogatingPid, interrogatingTid,
7305 spec);
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07007306 } else {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007307 // We cannot make the call and notify the caller so it does not wait.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007308 try {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007309 callback.setFindAccessibilityNodeInfoResult(null, interactionId);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007310 } catch (RemoteException re) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007311 /* best effort - ignore */
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007312 }
7313 }
7314 }
7315
Svetoslav Ganov42138042012-03-20 11:51:39 -07007316 @Override
Svetoslav9ae9ed22014-09-02 16:36:35 -07007317 public void focusSearch(long accessibilityNodeId, int direction, Region interactiveRegion,
7318 int interactionId, IAccessibilityInteractionConnectionCallback callback, int flags,
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07007319 int interrogatingPid, long interrogatingTid, MagnificationSpec spec) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007320 ViewRootImpl viewRootImpl = mViewRootImpl.get();
7321 if (viewRootImpl != null && viewRootImpl.mView != null) {
7322 viewRootImpl.getAccessibilityInteractionController()
Svetoslav9ae9ed22014-09-02 16:36:35 -07007323 .focusSearchClientThread(accessibilityNodeId, direction, interactiveRegion,
7324 interactionId, callback, flags, interrogatingPid, interrogatingTid,
7325 spec);
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07007326 } else {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007327 // We cannot make the call and notify the caller so it does not wait.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007328 try {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007329 callback.setFindAccessibilityNodeInfoResult(null, interactionId);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007330 } catch (RemoteException re) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007331 /* best effort - ignore */
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007332 }
7333 }
7334 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007335 }
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07007336
Svetoslav Ganova0156172011-06-26 17:55:44 -07007337 private class SendWindowContentChangedAccessibilityEvent implements Runnable {
Alan Viverette77e9a282013-09-12 17:16:09 -07007338 private int mChangeTypes = 0;
7339
Svetoslav Ganov42138042012-03-20 11:51:39 -07007340 public View mSource;
Svetoslav6254f482013-06-04 17:22:14 -07007341 public long mLastEventTimeMillis;
Svetoslav Ganova0156172011-06-26 17:55:44 -07007342
Igor Murashkina86ab6402013-08-30 12:58:36 -07007343 @Override
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07007344 public void run() {
Svetoslav8d820ecf2013-07-15 17:12:41 -07007345 // The accessibility may be turned off while we were waiting so check again.
7346 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7347 mLastEventTimeMillis = SystemClock.uptimeMillis();
7348 AccessibilityEvent event = AccessibilityEvent.obtain();
7349 event.setEventType(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Alan Viverette77e9a282013-09-12 17:16:09 -07007350 event.setContentChangeTypes(mChangeTypes);
Svetoslav8d820ecf2013-07-15 17:12:41 -07007351 mSource.sendAccessibilityEventUnchecked(event);
7352 } else {
7353 mLastEventTimeMillis = 0;
7354 }
7355 // In any case reset to initial state.
Svetoslav6254f482013-06-04 17:22:14 -07007356 mSource.resetSubtreeAccessibilityStateChanged();
7357 mSource = null;
Alan Viverette77e9a282013-09-12 17:16:09 -07007358 mChangeTypes = 0;
Svetoslav6254f482013-06-04 17:22:14 -07007359 }
7360
Alan Viverette77e9a282013-09-12 17:16:09 -07007361 public void runOrPost(View source, int changeType) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007362 if (mSource != null) {
Svetoslav9dafebb2013-06-18 16:29:25 -07007363 // If there is no common predecessor, then mSource points to
7364 // a removed view, hence in this case always prefer the source.
7365 View predecessor = getCommonPredecessor(mSource, source);
7366 mSource = (predecessor != null) ? predecessor : source;
Alan Viverette77e9a282013-09-12 17:16:09 -07007367 mChangeTypes |= changeType;
Svetoslav6254f482013-06-04 17:22:14 -07007368 return;
7369 }
7370 mSource = source;
Alan Viverette77e9a282013-09-12 17:16:09 -07007371 mChangeTypes = changeType;
Svetoslav6254f482013-06-04 17:22:14 -07007372 final long timeSinceLastMillis = SystemClock.uptimeMillis() - mLastEventTimeMillis;
7373 final long minEventIntevalMillis =
7374 ViewConfiguration.getSendRecurringAccessibilityEventsInterval();
7375 if (timeSinceLastMillis >= minEventIntevalMillis) {
Svetoslav9dafebb2013-06-18 16:29:25 -07007376 mSource.removeCallbacks(this);
Svetoslav6254f482013-06-04 17:22:14 -07007377 run();
7378 } else {
7379 mSource.postDelayed(this, minEventIntevalMillis - timeSinceLastMillis);
Svetoslav Ganov79311c42012-01-17 20:24:26 -08007380 }
7381 }
7382 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007383}