blob: 9c19bf14a8bd5e638eff1fecdcd060fc1b404472 [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
Filip Gruszczynski1a4dfe52015-11-15 10:58:57 -080019import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_DECOR_VIEW_VISIBILITY;
Jorim Jaggi2e95a482016-01-14 17:36:55 -080020import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
Chong Zhangf6525ce2016-01-14 17:09:56 -080021import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
22import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL;
23import static android.view.WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY;
Filip Gruszczynski1a4dfe52015-11-15 10:58:57 -080024
Romain Guy6b7bd242010-10-06 19:49:23 -070025import android.Manifest;
Chet Haasecca2c982011-05-20 14:34:18 -070026import android.animation.LayoutTransition;
Romain Guy6b7bd242010-10-06 19:49:23 -070027import android.app.ActivityManagerNative;
28import android.content.ClipDescription;
29import android.content.ComponentCallbacks;
30import android.content.Context;
31import android.content.pm.PackageManager;
32import android.content.res.CompatibilityInfo;
33import android.content.res.Configuration;
34import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.graphics.Canvas;
Alan Viverettefed3f722013-11-14 14:48:20 -080036import android.graphics.Matrix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.graphics.PixelFormat;
Christopher Tate2c095f32010-10-04 14:13:40 -070038import android.graphics.Point;
Christopher Tatea53146c2010-09-07 11:57:52 -070039import android.graphics.PointF;
Romain Guy6b7bd242010-10-06 19:49:23 -070040import android.graphics.PorterDuff;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.graphics.Rect;
42import android.graphics.Region;
Svetoslav Ganov42138042012-03-20 11:51:39 -070043import android.graphics.drawable.Drawable;
Jeff Brownd912e1f2014-04-11 18:46:22 -070044import android.hardware.display.DisplayManager;
45import android.hardware.display.DisplayManager.DisplayListener;
Jun Mukai347e5d42015-12-03 01:13:31 -080046import android.hardware.input.InputManager;
Romain Guy6b7bd242010-10-06 19:49:23 -070047import android.media.AudioManager;
48import android.os.Binder;
Michael Wright5bd69e62015-05-14 14:48:08 +010049import android.os.Build;
Romain Guy6b7bd242010-10-06 19:49:23 -070050import android.os.Bundle;
51import android.os.Debug;
52import android.os.Handler;
Romain Guy6b7bd242010-10-06 19:49:23 -070053import android.os.Looper;
54import android.os.Message;
55import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.os.Process;
Romain Guy6b7bd242010-10-06 19:49:23 -070057import android.os.RemoteException;
Romain Guy6b7bd242010-10-06 19:49:23 -070058import android.os.SystemClock;
Romain Guy59a12ca2011-06-09 17:48:21 -070059import android.os.SystemProperties;
Jeff Brown481c1572012-03-09 14:41:15 -080060import android.os.Trace;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.util.AndroidRuntimeException;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -070062import android.util.DisplayMetrics;
Romain Guy6b7bd242010-10-06 19:49:23 -070063import android.util.Log;
Chet Haase949dbf72010-08-11 18:41:06 -070064import android.util.Slog;
John Reckba6adf62015-02-19 14:36:50 -080065import android.util.TimeUtils;
Dianne Hackborn711e62a2010-11-29 16:38:22 -080066import android.util.TypedValue;
John Reck44fd8d22014-02-26 11:00:11 -080067import android.view.Surface.OutOfResourcesException;
Jeff Browna175a5b2012-02-15 19:18:31 -080068import android.view.View.AttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.view.View.MeasureSpec;
svetoslavganov75986cf2009-05-14 22:28:01 -070070import android.view.accessibility.AccessibilityEvent;
71import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070072import android.view.accessibility.AccessibilityManager.AccessibilityStateChangeListener;
Chris Craikcce47eb2014-07-16 15:12:15 -070073import android.view.accessibility.AccessibilityManager.HighTextContrastChangeListener;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070074import android.view.accessibility.AccessibilityNodeInfo;
Alan Viverette25acc7e2015-05-19 11:32:08 -070075import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
Svetoslav Ganov02107852011-10-03 17:06:56 -070076import android.view.accessibility.AccessibilityNodeProvider;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070077import android.view.accessibility.IAccessibilityInteractionConnection;
78import android.view.accessibility.IAccessibilityInteractionConnectionCallback;
Dianne Hackborn0f761d62010-11-30 22:06:10 -080079import android.view.animation.AccelerateDecelerateInterpolator;
80import android.view.animation.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.view.inputmethod.InputConnection;
82import android.view.inputmethod.InputMethodManager;
83import android.widget.Scroller;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070084
Svetoslav Ganov42138042012-03-20 11:51:39 -070085import com.android.internal.R;
Clara Bayarri75e09792015-07-29 16:20:40 +010086import com.android.internal.os.IResultReceiver;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070087import com.android.internal.os.SomeArgs;
Adam Powell6711f3b2015-05-06 15:57:09 -070088import com.android.internal.policy.PhoneFallbackEventHandler;
Romain Guy6b7bd242010-10-06 19:49:23 -070089import com.android.internal.view.BaseSurfaceHolder;
Romain Guy6b7bd242010-10-06 19:49:23 -070090import com.android.internal.view.RootViewSurfaceTaker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
Jeff Brown5182c782013-10-15 20:31:52 -070092import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093import java.io.IOException;
94import java.io.OutputStream;
Jeff Brown5182c782013-10-15 20:31:52 -070095import java.io.PrintWriter;
Romain Guy6b7bd242010-10-06 19:49:23 -070096import java.lang.ref.WeakReference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import java.util.ArrayList;
Chong Zhang8dbd9ad2015-10-09 10:06:11 -070098import java.util.concurrent.CountDownLatch;
Svetoslav Ganov42138042012-03-20 11:51:39 -070099import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101/**
102 * The top of a view hierarchy, implementing the needed protocol between View
103 * and the WindowManager. This is for the most part an internal implementation
Jeff Brown98365d72012-08-19 20:30:52 -0700104 * detail of {@link WindowManagerGlobal}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 *
106 * {@hide}
107 */
Romain Guy812ccbe2010-06-01 14:07:24 -0700108@SuppressWarnings({"EmptyCatchBlock", "PointlessBooleanExpression"})
Jeff Browna175a5b2012-02-15 19:18:31 -0800109public final class ViewRootImpl implements ViewParent,
John Reck51aaf902015-12-02 15:08:07 -0800110 View.AttachInfo.Callbacks, ThreadedRenderer.HardwareDrawCallbacks {
Dianne Hackborn70a3f672011-08-08 14:32:41 -0700111 private static final String TAG = "ViewRootImpl";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 private static final boolean DBG = false;
Romain Guy812ccbe2010-06-01 14:07:24 -0700113 private static final boolean LOCAL_LOGV = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 /** @noinspection PointlessBooleanExpression*/
115 private static final boolean DEBUG_DRAW = false || LOCAL_LOGV;
116 private static final boolean DEBUG_LAYOUT = false || LOCAL_LOGV;
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800117 private static final boolean DEBUG_DIALOG = false || LOCAL_LOGV;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 private static final boolean DEBUG_INPUT_RESIZE = false || LOCAL_LOGV;
119 private static final boolean DEBUG_ORIENTATION = false || LOCAL_LOGV;
120 private static final boolean DEBUG_TRACKBALL = false || LOCAL_LOGV;
121 private static final boolean DEBUG_IMF = false || LOCAL_LOGV;
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700122 private static final boolean DEBUG_CONFIGURATION = false || LOCAL_LOGV;
Chet Haase2f2022a2011-10-11 06:41:59 -0700123 private static final boolean DEBUG_FPS = false;
Michael Wright06a79252014-05-05 17:45:29 -0700124 private static final boolean DEBUG_INPUT_STAGES = false || LOCAL_LOGV;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125
Romain Guy59a12ca2011-06-09 17:48:21 -0700126 /**
Skuhneb8160872015-09-22 09:51:39 -0700127 * Set to false if we do not want to use the multi threaded renderer. Note that by disabling
128 * this, WindowCallbacks will not fire.
129 */
130 private static final boolean USE_MT_RENDERER = true;
131
132 /**
Romain Guy59a12ca2011-06-09 17:48:21 -0700133 * Set this system property to true to force the view hierarchy to render
134 * at 60 Hz. This can be used to measure the potential framerate.
135 */
Romain Guye9bc11f2013-05-23 12:47:26 -0700136 private static final String PROPERTY_PROFILE_RENDERING = "viewroot.profile_rendering";
Michael Chan53071d62009-05-13 17:29:48 -0700137
Griff Hazena0938022015-03-13 10:01:41 -0700138 // properties used by emulator to determine display shape
Griff Hazena0938022015-03-13 10:01:41 -0700139 public static final String PROPERTY_EMULATOR_WIN_OUTSET_BOTTOM_PX =
140 "ro.emu.win_outset_bottom_px";
Michael Kolb437d3132014-06-20 13:28:44 -0700141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 /**
143 * Maximum time we allow the user to roll the trackball enough to generate
144 * a key event, before resetting the counters.
145 */
146 static final int MAX_TRACKBALL_DELAY = 250;
147
Jorim Jaggi4846ee32016-01-07 17:39:12 +0100148 private static final int RESIZE_MODE_FREEFORM = 0;
149 private static final int RESIZE_MODE_DOCKED_DIVIDER = 1;
150
Alan Viverettebea0c7da2015-09-01 16:00:20 -0400151 static final ThreadLocal<HandlerActionQueue> sRunQueues = new ThreadLocal<HandlerActionQueue>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152
Skuhneb8160872015-09-22 09:51:39 -0700153 static final ArrayList<Runnable> sFirstDrawHandlers = new ArrayList();
Dianne Hackborn2a9094d2010-02-03 19:20:09 -0800154 static boolean sFirstDrawComplete = false;
Craig Mautner8f303ad2013-06-14 11:32:22 -0700155
Skuhneb8160872015-09-22 09:51:39 -0700156 static final ArrayList<ComponentCallbacks> sConfigCallbacks = new ArrayList();
Romain Guy59a12ca2011-06-09 17:48:21 -0700157
Chong Zhangdcee1de2015-10-06 10:26:00 -0700158 final ArrayList<WindowCallbacks> mWindowCallbacks = new ArrayList();
Jeff Brownf9e989d2013-04-04 23:04:03 -0700159 final Context mContext;
Jeff Brown98365d72012-08-19 20:30:52 -0700160 final IWindowSession mWindowSession;
161 final Display mDisplay;
Jeff Brownd912e1f2014-04-11 18:46:22 -0700162 final DisplayManager mDisplayManager;
Dianne Hackbornc2293022013-02-06 23:14:49 -0800163 final String mBasePackageName;
Jeff Brown98365d72012-08-19 20:30:52 -0700164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 final int[] mTmpLocation = new int[2];
Romain Guy8506ab42009-06-11 17:35:47 -0700166
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800167 final TypedValue mTmpValue = new TypedValue();
Jeff Brownf9e989d2013-04-04 23:04:03 -0700168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 final Thread mThread;
170
171 final WindowLeaked mLocation;
172
173 final WindowManager.LayoutParams mWindowAttributes = new WindowManager.LayoutParams();
174
175 final W mWindow;
176
Dianne Hackborn180c4842011-09-13 12:39:25 -0700177 final int mTargetSdkVersion;
178
Dianne Hackborn9a230e02011-10-06 11:51:27 -0700179 int mSeq;
180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 View mView;
Svetoslav Ganov42138042012-03-20 11:51:39 -0700182
183 View mAccessibilityFocusedHost;
184 AccessibilityNodeInfo mAccessibilityFocusedVirtualView;
185
Jun Mukai347e5d42015-12-03 01:13:31 -0800186 // The view which captures mouse input, or null when no one is capturing.
187 View mCapturingView;
188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 int mViewVisibility;
190 boolean mAppVisible = true;
Filip Gruszczynski1a4dfe52015-11-15 10:58:57 -0800191 // For recents to freeform transition we need to keep drawing after the app receives information
192 // that it became invisible. This will ignore that information and depend on the decor view
193 // visibility to control drawing. The decor view visibility will get adjusted when the app get
194 // stopped and that's when the app will stop drawing further frames.
195 private boolean mForceDecorViewVisibility = false;
Dianne Hackborn180c4842011-09-13 12:39:25 -0700196 int mOrigWindowType = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197
Alan Viverette64bf97a2015-09-18 16:42:00 -0400198 /** Whether the window had focus during the most recent traversal. */
199 boolean mHadWindowFocus;
200
201 /**
202 * Whether the window lost focus during a previous traversal and has not
203 * yet gained it back. Used to determine whether a WINDOW_STATE_CHANGE
204 * accessibility events should be sent during traversal.
205 */
206 boolean mLostWindowFocus;
207
Dianne Hackbornce418e62011-03-01 14:31:38 -0800208 // Set to true if the owner of this window is in the stopped state,
209 // so the window should no longer be active.
210 boolean mStopped = false;
Craig Mautner8f303ad2013-06-14 11:32:22 -0700211
Daniel Koulomzin087ae472015-12-16 17:52:25 -0500212 // Set to true if the owner of this window is in ambient mode,
213 // which means it won't receive input events.
214 boolean mIsAmbientMode = false;
215
George Mount41725de2015-04-09 08:23:05 -0700216 // Set to true to stop input during an Activity Transition.
217 boolean mPausedForTransition = false;
218
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700219 boolean mLastInCompatMode = false;
220
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700221 SurfaceHolder.Callback2 mSurfaceHolderCallback;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700222 BaseSurfaceHolder mSurfaceHolder;
223 boolean mIsCreating;
224 boolean mDrawingAllowed;
Craig Mautner8f303ad2013-06-14 11:32:22 -0700225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 final Region mTransparentRegion;
227 final Region mPreviousTransparentRegion;
228
229 int mWidth;
230 int mHeight;
Romain Guy7d7b5492011-01-24 16:33:45 -0800231 Rect mDirty;
Romain Guybb93d552009-03-24 21:04:15 -0700232 boolean mIsAnimating;
Romain Guy8506ab42009-06-11 17:35:47 -0700233
Chong Zhang0275e392015-09-17 10:41:44 -0700234 private boolean mDragResizing;
Jorim Jaggi4846ee32016-01-07 17:39:12 +0100235 private int mResizeMode;
Chong Zhang0275e392015-09-17 10:41:44 -0700236 private int mCanvasOffsetX;
237 private int mCanvasOffsetY;
Jorim Jaggi4846ee32016-01-07 17:39:12 +0100238 private boolean mActivityRelaunched;
Chong Zhang0275e392015-09-17 10:41:44 -0700239
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700240 CompatibilityInfo.Translator mTranslator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241
242 final View.AttachInfo mAttachInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700243 InputChannel mInputChannel;
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -0700244 InputQueue.Callback mInputQueueCallback;
245 InputQueue mInputQueue;
Joe Onorato86f67862010-11-05 18:57:34 -0700246 FallbackEventHandler mFallbackEventHandler;
Jeff Brown96e942d2011-11-30 19:55:01 -0800247 Choreographer mChoreographer;
Igor Murashkina86ab6402013-08-30 12:58:36 -0700248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 final Rect mTempRect; // used in the transaction to not thrash the heap.
250 final Rect mVisRect; // used to retrieve visible rect of focused view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251
252 boolean mTraversalScheduled;
Jeff Browne0dbd002012-02-15 19:34:58 -0800253 int mTraversalBarrier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 boolean mWillDrawSoon;
Craig Mautnerdf2390a2012-08-29 20:59:22 -0700255 /** Set to true while in performTraversals for detecting when die(true) is called from internal
256 * callbacks such as onMeasure, onPreDraw, onDraw and deferring doDie() until later. */
257 boolean mIsInTraversal;
Adrian Roosfa104232014-06-20 16:10:14 -0700258 boolean mApplyInsetsRequested;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 boolean mLayoutRequested;
260 boolean mFirst;
261 boolean mReportNextDraw;
262 boolean mFullRedrawNeeded;
263 boolean mNewSurfaceNeeded;
264 boolean mHasHadWindowFocus;
265 boolean mLastWasImTarget;
Chong Zhang8dbd9ad2015-10-09 10:06:11 -0700266 CountDownLatch mWindowDrawCountDown;
Jorim Jaggi827e0fa2015-05-07 11:41:41 -0700267
Romain Guy1f59e5c2012-05-06 14:11:16 -0700268 boolean mIsDrawing;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700269 int mLastSystemUiVisibility;
Dianne Hackborn9d090892012-06-11 18:35:41 -0700270 int mClientWindowLayoutFlags;
Dianne Hackbornc4aad012013-02-22 15:05:25 -0800271 boolean mLastOverscanRequested;
Jeff Brown4952dfd2011-11-30 19:23:22 -0800272
273 // Pool of queued input events.
274 private static final int MAX_QUEUED_INPUT_EVENT_POOL_SIZE = 10;
275 private QueuedInputEvent mQueuedInputEventPool;
276 private int mQueuedInputEventPoolSize;
Jeff Brown4952dfd2011-11-30 19:23:22 -0800277
Michael Wrightc8a7e542013-03-20 17:58:33 -0700278 /* Input event queue.
Jeff Brownf9e989d2013-04-04 23:04:03 -0700279 * Pending input events are input events waiting to be delivered to the input stages
280 * and handled by the application.
Michael Wrightc8a7e542013-03-20 17:58:33 -0700281 */
282 QueuedInputEvent mPendingInputEventHead;
283 QueuedInputEvent mPendingInputEventTail;
Michael Wright95ae9422013-03-14 10:58:50 -0700284 int mPendingInputEventCount;
Jeff Brown96e942d2011-11-30 19:55:01 -0800285 boolean mProcessInputEventsScheduled;
Michael Wright9d744c72014-02-18 21:27:42 -0800286 boolean mUnbufferedInputDispatch;
Michael Wright95ae9422013-03-14 10:58:50 -0700287 String mPendingInputEventQueueLengthCounterName = "pq";
Jeff Brownf9e989d2013-04-04 23:04:03 -0700288
289 InputStage mFirstInputStage;
290 InputStage mFirstPostImeInputStage;
Michael Wright899d7052014-04-23 17:23:39 -0700291 InputStage mSyntheticInputStage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292
293 boolean mWindowAttributesChanged = false;
Romain Guyf21c9b02011-09-06 16:56:54 -0700294 int mWindowAttributesChangesFlag = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295
296 // These can be accessed by any thread, must be protected with a lock.
Mathias Agopian5583dc62009-07-09 16:28:11 -0700297 // Surface can never be reassigned or cleared (use Surface.clear()).
John Reckb13de072014-11-19 16:33:47 -0800298 final Surface mSurface = new Surface();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299
300 boolean mAdded;
301 boolean mAddedTouchMode;
302
Craig Mautner48d0d182013-06-11 07:53:06 -0700303 final DisplayAdjustments mDisplayAdjustments;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 // These are accessed by multiple threads.
306 final Rect mWinFrame; // frame given by window manager.
307
Dianne Hackbornc4aad012013-02-22 15:05:25 -0800308 final Rect mPendingOverscanInsets = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 final Rect mPendingVisibleInsets = new Rect();
Adrian Roosfa104232014-06-20 16:10:14 -0700310 final Rect mPendingStableInsets = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 final Rect mPendingContentInsets = new Rect();
Filip Gruszczynski2217f612015-05-26 11:32:08 -0700312 final Rect mPendingOutsets = new Rect();
Jorim Jaggia7262a82015-11-03 15:15:40 +0100313 final Rect mPendingBackDropFrame = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 final ViewTreeObserver.InternalInsetsInfo mLastGivenInsets
315 = new ViewTreeObserver.InternalInsetsInfo();
316
Adrian Roosfa104232014-06-20 16:10:14 -0700317 final Rect mDispatchContentInsets = new Rect();
318 final Rect mDispatchStableInsets = new Rect();
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700319
Filip Gruszczynski954289d2015-02-26 15:46:47 -0800320 private WindowInsets mLastWindowInsets;
321
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700322 final Configuration mLastConfiguration = new Configuration();
323 final Configuration mPendingConfiguration = new Configuration();
Romain Guy59a12ca2011-06-09 17:48:21 -0700324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 boolean mScrollMayChange;
326 int mSoftInputMode;
Svetoslav Ganov149567f2013-01-08 15:23:34 -0800327 WeakReference<View> mLastScrolledFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 int mScrollY;
329 int mCurScrollY;
330 Scroller mScroller;
John Recke56e9df2014-02-21 15:45:10 -0800331 static final Interpolator mResizeInterpolator = new AccelerateDecelerateInterpolator();
Chet Haasecca2c982011-05-20 14:34:18 -0700332 private ArrayList<LayoutTransition> mPendingTransitions;
Romain Guy8506ab42009-06-11 17:35:47 -0700333
Romain Guy8506ab42009-06-11 17:35:47 -0700334 final ViewConfiguration mViewConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335
Christopher Tatea53146c2010-09-07 11:57:52 -0700336 /* Drag/drop */
337 ClipDescription mDragDescription;
338 View mCurrentDragView;
Christopher Tate7fb8b562011-01-20 13:46:41 -0800339 volatile Object mLocalDragState;
Christopher Tatea53146c2010-09-07 11:57:52 -0700340 final PointF mDragPoint = new PointF();
Christopher Tate2c095f32010-10-04 14:13:40 -0700341 final PointF mLastTouchPoint = new PointF();
Igor Murashkina86ab6402013-08-30 12:58:36 -0700342
343 private boolean mProfileRendering;
Romain Guyd0750312012-11-29 11:34:43 -0800344 private Choreographer.FrameCallback mRenderProfiler;
345 private boolean mRenderProfilingEnabled;
Christopher Tatea53146c2010-09-07 11:57:52 -0700346
Chet Haase2f2022a2011-10-11 06:41:59 -0700347 // Variables to track frames per second, enabled via DEBUG_FPS flag
348 private long mFpsStartTime = -1;
349 private long mFpsPrevTime = -1;
350 private int mFpsNumFrames;
351
Jun Mukai1db53972015-09-11 18:08:31 -0700352 private int mPointerIconShape = PointerIcon.STYLE_NOT_SPECIFIED;
Jun Mukaid4eaef72015-10-30 15:54:33 -0700353 private PointerIcon mCustomPointerIcon = null;
Jun Mukai1db53972015-09-11 18:08:31 -0700354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 /**
356 * see {@link #playSoundEffect(int)}
357 */
358 AudioManager mAudioManager;
359
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700360 final AccessibilityManager mAccessibilityManager;
361
Gilles Debunne5ac84422011-10-19 09:35:58 -0700362 AccessibilityInteractionController mAccessibilityInteractionController;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700363
364 AccessibilityInteractionConnectionManager mAccessibilityInteractionConnectionManager;
Chris Craikcce47eb2014-07-16 15:12:15 -0700365 HighContrastTextManager mHighContrastTextManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700366
Svetoslav Ganova0156172011-06-26 17:55:44 -0700367 SendWindowContentChangedAccessibilityEvent mSendWindowContentChangedAccessibilityEvent;
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -0700368
Svetoslav Ganov42138042012-03-20 11:51:39 -0700369 HashSet<View> mTempHashSet;
Svetoslav Ganov79311c42012-01-17 20:24:26 -0800370
Dianne Hackborn11ea3342009-07-22 21:48:55 -0700371 private final int mDensity;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700372 private final int mNoncompatDensity;
Adam Powellb08013c2010-09-16 16:28:11 -0700373
Chet Haase97140572012-09-13 14:56:47 -0700374 private boolean mInLayout = false;
375 ArrayList<View> mLayoutRequesters = new ArrayList<View>();
376 boolean mHandlingLayoutInLayoutRequest = false;
377
Fabrice Di Megliob003e282012-10-17 17:20:19 -0700378 private int mViewLayoutDirectionInitial;
Chet Haase97140572012-09-13 14:56:47 -0700379
Craig Mautner8f303ad2013-06-14 11:32:22 -0700380 /** Set to true once doDie() has been called. */
381 private boolean mRemoved;
382
Jeff Brown21bc5c92011-02-28 18:27:14 -0800383 /**
384 * Consistency verifier for debugging purposes.
385 */
386 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
387 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
388 new InputEventConsistencyVerifier(this, 0) : null;
389
Dianne Hackborn9a230e02011-10-06 11:51:27 -0700390 static final class SystemUiVisibilityInfo {
391 int seq;
392 int globalVisibility;
393 int localValue;
394 int localChanges;
395 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700396
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -0800397 private String mTag = TAG;
398
Jeff Brown98365d72012-08-19 20:30:52 -0700399 public ViewRootImpl(Context context, Display display) {
Jeff Brownf9e989d2013-04-04 23:04:03 -0700400 mContext = context;
401 mWindowSession = WindowManagerGlobal.getWindowSession();
Jeff Brown98365d72012-08-19 20:30:52 -0700402 mDisplay = display;
Dianne Hackbornc2293022013-02-06 23:14:49 -0800403 mBasePackageName = context.getBasePackageName();
Jeff Brown98365d72012-08-19 20:30:52 -0700404
Craig Mautner48d0d182013-06-11 07:53:06 -0700405 mDisplayAdjustments = display.getDisplayAdjustments();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 mThread = Thread.currentThread();
408 mLocation = new WindowLeaked(null);
409 mLocation.fillInStackTrace();
410 mWidth = -1;
411 mHeight = -1;
412 mDirty = new Rect();
413 mTempRect = new Rect();
414 mVisRect = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 mWinFrame = new Rect();
Romain Guyfb8b7632010-08-23 21:05:08 -0700416 mWindow = new W(this);
Dianne Hackborn180c4842011-09-13 12:39:25 -0700417 mTargetSdkVersion = context.getApplicationInfo().targetSdkVersion;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 mViewVisibility = View.GONE;
419 mTransparentRegion = new Region();
420 mPreviousTransparentRegion = new Region();
421 mFirst = true; // true for the first time the view is added
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 mAdded = false;
Chris Craikcce47eb2014-07-16 15:12:15 -0700423 mAttachInfo = new View.AttachInfo(mWindowSession, mWindow, display, this, mHandler, this);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700424 mAccessibilityManager = AccessibilityManager.getInstance(context);
425 mAccessibilityInteractionConnectionManager =
426 new AccessibilityInteractionConnectionManager();
Svetoslav Ganov00d0c142012-02-24 11:30:49 -0800427 mAccessibilityManager.addAccessibilityStateChangeListener(
428 mAccessibilityInteractionConnectionManager);
Chris Craikcce47eb2014-07-16 15:12:15 -0700429 mHighContrastTextManager = new HighContrastTextManager();
430 mAccessibilityManager.addHighTextContrastStateChangeListener(
431 mHighContrastTextManager);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 mViewConfiguration = ViewConfiguration.get(context);
Dianne Hackborn11ea3342009-07-22 21:48:55 -0700433 mDensity = context.getResources().getDisplayMetrics().densityDpi;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700434 mNoncompatDensity = context.getResources().getDisplayMetrics().noncompatDensityDpi;
Jorim Jaggib10e33f2015-02-04 21:57:40 +0100435 mFallbackEventHandler = new PhoneFallbackEventHandler(context);
Jeff Brown96e942d2011-11-30 19:55:01 -0800436 mChoreographer = Choreographer.getInstance();
Jeff Brownd912e1f2014-04-11 18:46:22 -0700437 mDisplayManager = (DisplayManager)context.getSystemService(Context.DISPLAY_SERVICE);
Dianne Hackborna53de062012-05-08 18:53:51 -0700438 loadSystemProperties();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 }
440
Dianne Hackborn2a9094d2010-02-03 19:20:09 -0800441 public static void addFirstDrawHandler(Runnable callback) {
442 synchronized (sFirstDrawHandlers) {
443 if (!sFirstDrawComplete) {
444 sFirstDrawHandlers.add(callback);
445 }
446 }
447 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700448
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800449 public static void addConfigCallback(ComponentCallbacks callback) {
450 synchronized (sConfigCallbacks) {
451 sConfigCallbacks.add(callback);
452 }
453 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700454
Chong Zhangdcee1de2015-10-06 10:26:00 -0700455 public void addWindowCallbacks(WindowCallbacks callback) {
Skuhneb8160872015-09-22 09:51:39 -0700456 if (USE_MT_RENDERER) {
Chong Zhangdcee1de2015-10-06 10:26:00 -0700457 synchronized (mWindowCallbacks) {
458 mWindowCallbacks.add(callback);
Skuhneb8160872015-09-22 09:51:39 -0700459 }
460 }
461 }
462
Chong Zhangdcee1de2015-10-06 10:26:00 -0700463 public void removeWindowCallbacks(WindowCallbacks callback) {
Skuhneb8160872015-09-22 09:51:39 -0700464 if (USE_MT_RENDERER) {
Chong Zhangdcee1de2015-10-06 10:26:00 -0700465 synchronized (mWindowCallbacks) {
466 mWindowCallbacks.remove(callback);
Skuhneb8160872015-09-22 09:51:39 -0700467 }
468 }
469 }
470
Chong Zhang8dbd9ad2015-10-09 10:06:11 -0700471 public void reportDrawFinish() {
472 if (mWindowDrawCountDown != null) {
473 mWindowDrawCountDown.countDown();
474 }
475 }
476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 // FIXME for perf testing only
478 private boolean mProfile = false;
479
480 /**
481 * Call this to profile the next traversal call.
482 * FIXME for perf testing only. Remove eventually
483 */
484 public void profile() {
485 mProfile = true;
486 }
487
488 /**
489 * Indicates whether we are in touch mode. Calling this method triggers an IPC
490 * call and should be avoided whenever possible.
491 *
492 * @return True, if the device is in touch mode, false otherwise.
493 *
494 * @hide
495 */
496 static boolean isInTouchMode() {
Jeff Brown98365d72012-08-19 20:30:52 -0700497 IWindowSession windowSession = WindowManagerGlobal.peekWindowSession();
498 if (windowSession != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 try {
Jeff Brown98365d72012-08-19 20:30:52 -0700500 return windowSession.getInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 } catch (RemoteException e) {
502 }
503 }
504 return false;
505 }
506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 /**
508 * We have one child
509 */
Romain Guye4d01122010-06-16 18:44:05 -0700510 public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 synchronized (this) {
512 if (mView == null) {
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700513 mView = view;
Jeff Brownd912e1f2014-04-11 18:46:22 -0700514
515 mAttachInfo.mDisplayState = mDisplay.getState();
516 mDisplayManager.registerDisplayListener(mDisplayListener, mHandler);
517
Fabrice Di Megliob003e282012-10-17 17:20:19 -0700518 mViewLayoutDirectionInitial = mView.getRawLayoutDirection();
Joe Onorato86f67862010-11-05 18:57:34 -0700519 mFallbackEventHandler.setView(view);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700520 mWindowAttributes.copyFrom(attrs);
Dianne Hackbornc2293022013-02-06 23:14:49 -0800521 if (mWindowAttributes.packageName == null) {
522 mWindowAttributes.packageName = mBasePackageName;
523 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700524 attrs = mWindowAttributes;
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -0800525 setTag();
Dianne Hackborn9d090892012-06-11 18:35:41 -0700526 // Keep track of the actual window flags supplied by the client.
527 mClientWindowLayoutFlags = attrs.flags;
Svetoslav Ganov791fd312012-05-14 15:12:30 -0700528
Svetoslav Ganov45a02e02012-06-17 15:07:29 -0700529 setAccessibilityFocus(null, null);
Svetoslav Ganov791fd312012-05-14 15:12:30 -0700530
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700531 if (view instanceof RootViewSurfaceTaker) {
532 mSurfaceHolderCallback =
533 ((RootViewSurfaceTaker)view).willYouTakeTheSurface();
534 if (mSurfaceHolderCallback != null) {
535 mSurfaceHolder = new TakenSurfaceHolder();
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700536 mSurfaceHolder.setFormat(PixelFormat.UNKNOWN);
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700537 }
538 }
Romain Guy1aec9a22011-01-05 09:37:12 -0800539
Alan Viverette49a22e82014-07-12 20:01:27 -0700540 // Compute surface insets required to draw at specified Z value.
541 // TODO: Use real shadow insets for a constant max Z.
Alan Viverette5435a302015-01-29 10:25:34 -0800542 if (!attrs.hasManualSurfaceInsets) {
543 final int surfaceInset = (int) Math.ceil(view.getZ() * 2);
544 attrs.surfaceInsets.set(surfaceInset, surfaceInset, surfaceInset, surfaceInset);
545 }
Alan Viverette49a22e82014-07-12 20:01:27 -0700546
Craig Mautner48d0d182013-06-11 07:53:06 -0700547 CompatibilityInfo compatibilityInfo = mDisplayAdjustments.getCompatibilityInfo();
Romain Guy856d4e12011-10-14 15:47:55 -0700548 mTranslator = compatibilityInfo.getTranslator();
549
Romain Guy1aec9a22011-01-05 09:37:12 -0800550 // If the application owns the surface, don't enable hardware acceleration
551 if (mSurfaceHolder == null) {
Romain Guy3b748a42013-04-17 18:54:38 -0700552 enableHardwareAcceleration(attrs);
Romain Guy1aec9a22011-01-05 09:37:12 -0800553 }
554
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -0700555 boolean restore = false;
Romain Guy35b38ce2009-10-07 13:38:55 -0700556 if (mTranslator != null) {
Romain Guy856d4e12011-10-14 15:47:55 -0700557 mSurface.setCompatibilityTranslator(mTranslator);
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -0700558 restore = true;
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700559 attrs.backup();
560 mTranslator.translateWindowLayout(attrs);
Mitsuru Oshima3d914922009-05-13 22:29:15 -0700561 }
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -0800562 if (DEBUG_LAYOUT) Log.d(mTag, "WindowLayout in setView:" + attrs);
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700563
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700564 if (!compatibilityInfo.supportsScreen()) {
Adam Lesinski95c42972013-10-02 10:13:27 -0700565 attrs.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700566 mLastInCompatMode = true;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700567 }
568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 mSoftInputMode = attrs.softInputMode;
570 mWindowAttributesChanged = true;
Romain Guyf21c9b02011-09-06 16:56:54 -0700571 mWindowAttributesChangesFlag = WindowManager.LayoutParams.EVERYTHING_CHANGED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 mAttachInfo.mRootView = view;
Romain Guy35b38ce2009-10-07 13:38:55 -0700573 mAttachInfo.mScalingRequired = mTranslator != null;
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700574 mAttachInfo.mApplicationScale =
575 mTranslator == null ? 1.0f : mTranslator.applicationScale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 if (panelParentView != null) {
577 mAttachInfo.mPanelParentWindowToken
578 = panelParentView.getApplicationWindowToken();
579 }
580 mAdded = true;
581 int res; /* = WindowManagerImpl.ADD_OKAY; */
Romain Guy8506ab42009-06-11 17:35:47 -0700582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 // Schedule the first layout -before- adding to the window
584 // manager, to make sure we do the relayout before receiving
585 // any other events from the system.
586 requestLayout();
Jeff Browncc4f7db2011-08-30 20:34:48 -0700587 if ((mWindowAttributes.inputFeatures
588 & WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0) {
589 mInputChannel = new InputChannel();
590 }
Filip Gruszczynski1a4dfe52015-11-15 10:58:57 -0800591 mForceDecorViewVisibility = (mWindowAttributes.privateFlags
592 & PRIVATE_FLAG_FORCE_DECOR_VIEW_VISIBILITY) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 try {
Dianne Hackborn180c4842011-09-13 12:39:25 -0700594 mOrigWindowType = mWindowAttributes.type;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700595 mAttachInfo.mRecomputeGlobalAttributes = true;
596 collectViewAttributes();
Jeff Brown98365d72012-08-19 20:30:52 -0700597 res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
598 getHostVisibility(), mDisplay.getDisplayId(),
Filip Gruszczynski0ec13282015-06-25 11:26:01 -0700599 mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
600 mAttachInfo.mOutsets, mInputChannel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 } catch (RemoteException e) {
602 mAdded = false;
603 mView = null;
604 mAttachInfo.mRootView = null;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700605 mInputChannel = null;
Joe Onorato86f67862010-11-05 18:57:34 -0700606 mFallbackEventHandler.setView(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 unscheduleTraversals();
Svetoslav Ganov45a02e02012-06-17 15:07:29 -0700608 setAccessibilityFocus(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 throw new RuntimeException("Adding window failed", e);
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700610 } finally {
611 if (restore) {
612 attrs.restore();
613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700615
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700616 if (mTranslator != null) {
617 mTranslator.translateRectInScreenToAppWindow(mAttachInfo.mContentInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700618 }
Dianne Hackbornc4aad012013-02-22 15:05:25 -0800619 mPendingOverscanInsets.set(0, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 mPendingContentInsets.set(mAttachInfo.mContentInsets);
Adrian Roosfa104232014-06-20 16:10:14 -0700621 mPendingStableInsets.set(mAttachInfo.mStableInsets);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 mPendingVisibleInsets.set(0, 0, 0, 0);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -0800623 if (DEBUG_LAYOUT) Log.v(mTag, "Added window " + mWindow);
Jeff Brown98365d72012-08-19 20:30:52 -0700624 if (res < WindowManagerGlobal.ADD_OKAY) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 mAttachInfo.mRootView = null;
626 mAdded = false;
Joe Onorato86f67862010-11-05 18:57:34 -0700627 mFallbackEventHandler.setView(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 unscheduleTraversals();
Svetoslav Ganov45a02e02012-06-17 15:07:29 -0700629 setAccessibilityFocus(null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 switch (res) {
Jeff Brown98365d72012-08-19 20:30:52 -0700631 case WindowManagerGlobal.ADD_BAD_APP_TOKEN:
632 case WindowManagerGlobal.ADD_BAD_SUBWINDOW_TOKEN:
633 throw new WindowManager.BadTokenException(
Wale Ogunwale74bf0652015-01-12 10:24:36 -0800634 "Unable to add window -- token " + attrs.token
635 + " is not valid; is your activity running?");
Jeff Brown98365d72012-08-19 20:30:52 -0700636 case WindowManagerGlobal.ADD_NOT_APP_TOKEN:
637 throw new WindowManager.BadTokenException(
Wale Ogunwale74bf0652015-01-12 10:24:36 -0800638 "Unable to add window -- token " + attrs.token
639 + " is not for an application");
Jeff Brown98365d72012-08-19 20:30:52 -0700640 case WindowManagerGlobal.ADD_APP_EXITING:
641 throw new WindowManager.BadTokenException(
Wale Ogunwale74bf0652015-01-12 10:24:36 -0800642 "Unable to add window -- app for token " + attrs.token
643 + " is exiting");
Jeff Brown98365d72012-08-19 20:30:52 -0700644 case WindowManagerGlobal.ADD_DUPLICATE_ADD:
645 throw new WindowManager.BadTokenException(
Wale Ogunwale74bf0652015-01-12 10:24:36 -0800646 "Unable to add window -- window " + mWindow
647 + " has already been added");
Jeff Brown98365d72012-08-19 20:30:52 -0700648 case WindowManagerGlobal.ADD_STARTING_NOT_NEEDED:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 // Silently ignore -- we would have just removed it
650 // right away, anyway.
651 return;
Jeff Brown98365d72012-08-19 20:30:52 -0700652 case WindowManagerGlobal.ADD_MULTIPLE_SINGLETON:
Alan Viverette73f6d602015-09-14 16:01:19 -0400653 throw new WindowManager.BadTokenException("Unable to add window "
654 + mWindow + " -- another window of type "
655 + mWindowAttributes.type + " already exists");
Jeff Brown98365d72012-08-19 20:30:52 -0700656 case WindowManagerGlobal.ADD_PERMISSION_DENIED:
Alan Viverette73f6d602015-09-14 16:01:19 -0400657 throw new WindowManager.BadTokenException("Unable to add window "
658 + mWindow + " -- permission denied for window type "
659 + mWindowAttributes.type);
Craig Mautner6018aee2012-10-23 14:27:49 -0700660 case WindowManagerGlobal.ADD_INVALID_DISPLAY:
Alan Viverette73f6d602015-09-14 16:01:19 -0400661 throw new WindowManager.InvalidDisplayException("Unable to add window "
662 + mWindow + " -- the specified display can not be found");
Wale Ogunwale74bf0652015-01-12 10:24:36 -0800663 case WindowManagerGlobal.ADD_INVALID_TYPE:
Alan Viverette73f6d602015-09-14 16:01:19 -0400664 throw new WindowManager.InvalidDisplayException("Unable to add window "
665 + mWindow + " -- the specified window type "
666 + mWindowAttributes.type + " is not valid");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 }
668 throw new RuntimeException(
Wale Ogunwale74bf0652015-01-12 10:24:36 -0800669 "Unable to add window -- unknown error code " + res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700671
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700672 if (view instanceof RootViewSurfaceTaker) {
673 mInputQueueCallback =
674 ((RootViewSurfaceTaker)view).willYouTakeTheInputQueue();
675 }
Jeff Browncc4f7db2011-08-30 20:34:48 -0700676 if (mInputChannel != null) {
677 if (mInputQueueCallback != null) {
Michael Wrighta44dd262013-04-10 21:12:00 -0700678 mInputQueue = new InputQueue();
Jeff Browncc4f7db2011-08-30 20:34:48 -0700679 mInputQueueCallback.onInputQueueCreated(mInputQueue);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700680 }
Michael Wrighta44dd262013-04-10 21:12:00 -0700681 mInputEventReceiver = new WindowInputEventReceiver(mInputChannel,
682 Looper.myLooper());
Jeff Brown46b9ac02010-04-22 18:58:52 -0700683 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 view.assignParent(this);
Jeff Brown98365d72012-08-19 20:30:52 -0700686 mAddedTouchMode = (res & WindowManagerGlobal.ADD_FLAG_IN_TOUCH_MODE) != 0;
687 mAppVisible = (res & WindowManagerGlobal.ADD_FLAG_APP_VISIBLE) != 0;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700688
689 if (mAccessibilityManager.isEnabled()) {
690 mAccessibilityInteractionConnectionManager.ensureConnection();
691 }
Svetoslav Ganov42138042012-03-20 11:51:39 -0700692
693 if (view.getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
694 view.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
695 }
Michael Wright95ae9422013-03-14 10:58:50 -0700696
Jeff Brownf9e989d2013-04-04 23:04:03 -0700697 // Set up the input pipeline.
698 CharSequence counterSuffix = attrs.getTitle();
Michael Wright899d7052014-04-23 17:23:39 -0700699 mSyntheticInputStage = new SyntheticInputStage();
700 InputStage viewPostImeStage = new ViewPostImeInputStage(mSyntheticInputStage);
Jeff Brownf9e989d2013-04-04 23:04:03 -0700701 InputStage nativePostImeStage = new NativePostImeInputStage(viewPostImeStage,
702 "aq:native-post-ime:" + counterSuffix);
703 InputStage earlyPostImeStage = new EarlyPostImeInputStage(nativePostImeStage);
704 InputStage imeStage = new ImeInputStage(earlyPostImeStage,
705 "aq:ime:" + counterSuffix);
706 InputStage viewPreImeStage = new ViewPreImeInputStage(imeStage);
707 InputStage nativePreImeStage = new NativePreImeInputStage(viewPreImeStage,
708 "aq:native-pre-ime:" + counterSuffix);
709
710 mFirstInputStage = nativePreImeStage;
711 mFirstPostImeInputStage = earlyPostImeStage;
712 mPendingInputEventQueueLengthCounterName = "aq:pending:" + counterSuffix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 }
714 }
715 }
716
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -0800717 private void setTag() {
718 final String[] split = mWindowAttributes.getTitle().toString().split("\\.");
719 if (split.length > 0) {
720 mTag = TAG + "[" + split[split.length - 1] + "]";
721 }
722 }
723
keunyoung30f420f2013-08-02 14:23:10 -0700724 /** Whether the window is in local focus mode or not */
725 private boolean isInLocalFocusMode() {
726 return (mWindowAttributes.flags & WindowManager.LayoutParams.FLAG_LOCAL_FOCUS_MODE) != 0;
727 }
728
Dianne Hackborn49b043f2015-05-07 14:21:38 -0700729 public int getWindowFlags() {
730 return mWindowAttributes.flags;
731 }
732
Dianne Hackbornece0f4f2015-06-11 13:29:01 -0700733 public int getDisplayId() {
734 return mDisplay.getDisplayId();
735 }
736
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800737 public CharSequence getTitle() {
738 return mWindowAttributes.getTitle();
739 }
740
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800741 void destroyHardwareResources() {
Romain Guy31f2c2e2011-11-21 10:55:41 -0800742 if (mAttachInfo.mHardwareRenderer != null) {
743 mAttachInfo.mHardwareRenderer.destroyHardwareResources(mView);
John Reckf47a5942014-06-30 16:20:04 -0700744 mAttachInfo.mHardwareRenderer.destroy();
Romain Guy31f2c2e2011-11-21 10:55:41 -0800745 }
746 }
747
Bo Liu845535a2014-03-21 12:06:23 -0700748 public void detachFunctor(long functor) {
John Reck44ac42a2014-05-16 14:46:07 -0700749 if (mAttachInfo.mHardwareRenderer != null) {
750 // Fence so that any pending invokeFunctor() messages will be processed
751 // before we return from detachFunctor.
John Reckf47a5942014-06-30 16:20:04 -0700752 mAttachInfo.mHardwareRenderer.stopDrawing();
John Reck44ac42a2014-05-16 14:46:07 -0700753 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700754 }
755
John Reck3b202512014-06-23 13:13:08 -0700756 /**
757 * Schedules the functor for execution in either kModeProcess or
758 * kModeProcessNoContext, depending on whether or not there is an EGLContext.
759 *
760 * @param functor The native functor to invoke
761 * @param waitForCompletion If true, this will not return until the functor
762 * has invoked. If false, the functor may be invoked
763 * asynchronously.
764 */
Hui Shu9970aee2014-06-23 17:10:30 -0700765 public void invokeFunctor(long functor, boolean waitForCompletion) {
John Reck3b202512014-06-23 13:13:08 -0700766 ThreadedRenderer.invokeFunctor(functor, waitForCompletion);
Bo Liuae738a72014-04-27 16:22:04 -0700767 }
768
John Reck119907c2014-08-14 09:02:01 -0700769 public void registerAnimatingRenderNode(RenderNode animator) {
770 if (mAttachInfo.mHardwareRenderer != null) {
771 mAttachInfo.mHardwareRenderer.registerAnimatingRenderNode(animator);
772 } else {
773 if (mAttachInfo.mPendingAnimatingRenderNodes == null) {
774 mAttachInfo.mPendingAnimatingRenderNodes = new ArrayList<RenderNode>();
775 }
776 mAttachInfo.mPendingAnimatingRenderNodes.add(animator);
777 }
778 }
779
Romain Guy3b748a42013-04-17 18:54:38 -0700780 private void enableHardwareAcceleration(WindowManager.LayoutParams attrs) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -0800781 mAttachInfo.mHardwareAccelerated = false;
782 mAttachInfo.mHardwareAccelerationRequested = false;
Romain Guy4f6aff32011-01-12 16:21:41 -0800783
Romain Guy856d4e12011-10-14 15:47:55 -0700784 // Don't enable hardware acceleration when the application is in compatibility mode
John Reckdd58e792014-04-02 16:54:28 +0000785 if (mTranslator != null) return;
Romain Guy856d4e12011-10-14 15:47:55 -0700786
Dianne Hackborn7eec10e2010-11-12 18:03:47 -0800787 // Try to enable hardware acceleration if requested
Igor Murashkina86ab6402013-08-30 12:58:36 -0700788 final boolean hardwareAccelerated =
Jim Miller1b365922011-03-09 19:38:07 -0800789 (attrs.flags & WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0;
790
John Reckdd58e792014-04-02 16:54:28 +0000791 if (hardwareAccelerated) {
John Reck51aaf902015-12-02 15:08:07 -0800792 if (!ThreadedRenderer.isAvailable()) {
Romain Guy1af23a32011-03-24 16:03:55 -0700793 return;
794 }
795
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700796 // Persistent processes (including the system) should not do
797 // accelerated rendering on low-end devices. In that case,
798 // sRendererDisabled will be set. In addition, the system process
799 // itself should never do accelerated rendering. In that case, both
800 // sRendererDisabled and sSystemRendererDisabled are set. When
801 // sSystemRendererDisabled is set, PRIVATE_FLAG_FORCE_HARDWARE_ACCELERATED
802 // can be used by code on the system process to escape that and enable
803 // HW accelerated drawing. (This is basically for the lock screen.)
Jim Miller1b365922011-03-09 19:38:07 -0800804
John Reck61375a82014-09-18 19:27:48 +0000805 final boolean fakeHwAccelerated = (attrs.privateFlags &
806 WindowManager.LayoutParams.PRIVATE_FLAG_FAKE_HARDWARE_ACCELERATED) != 0;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700807 final boolean forceHwAccelerated = (attrs.privateFlags &
808 WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_HARDWARE_ACCELERATED) != 0;
Jim Miller1b365922011-03-09 19:38:07 -0800809
John Reck61375a82014-09-18 19:27:48 +0000810 if (fakeHwAccelerated) {
811 // This is exclusively for the preview windows the window manager
812 // shows for launching applications, so they will look more like
813 // the app being launched.
814 mAttachInfo.mHardwareAccelerationRequested = true;
John Reck51aaf902015-12-02 15:08:07 -0800815 } else if (!ThreadedRenderer.sRendererDisabled
816 || (ThreadedRenderer.sSystemRendererDisabled && forceHwAccelerated)) {
Romain Guyb051e892010-09-28 19:09:36 -0700817 if (mAttachInfo.mHardwareRenderer != null) {
John Reckf47a5942014-06-30 16:20:04 -0700818 mAttachInfo.mHardwareRenderer.destroy();
Romain Guy211370f2012-02-01 16:10:55 -0800819 }
820
Alan Viverette2b12b582014-10-29 11:11:40 -0700821 final Rect insets = attrs.surfaceInsets;
Alan Viverette2cd23e62014-11-04 17:04:02 -0800822 final boolean hasSurfaceInsets = insets.left != 0 || insets.right != 0
823 || insets.top != 0 || insets.bottom != 0;
Alan Viverette2b12b582014-10-29 11:11:40 -0700824 final boolean translucent = attrs.format != PixelFormat.OPAQUE || hasSurfaceInsets;
John Reck51aaf902015-12-02 15:08:07 -0800825 mAttachInfo.mHardwareRenderer = ThreadedRenderer.create(mContext, translucent);
Romain Guye55945e2013-04-04 15:26:04 -0700826 if (mAttachInfo.mHardwareRenderer != null) {
827 mAttachInfo.mHardwareRenderer.setName(attrs.getTitle().toString());
828 mAttachInfo.mHardwareAccelerated =
829 mAttachInfo.mHardwareAccelerationRequested = true;
830 }
Romain Guye4d01122010-06-16 18:44:05 -0700831 }
832 }
833 }
834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 public View getView() {
836 return mView;
837 }
838
839 final WindowLeaked getLocation() {
840 return mLocation;
841 }
842
843 void setLayoutParams(WindowManager.LayoutParams attrs, boolean newView) {
844 synchronized (this) {
Alan Viverettedbed8932014-08-06 17:54:52 -0700845 final int oldInsetLeft = mWindowAttributes.surfaceInsets.left;
846 final int oldInsetTop = mWindowAttributes.surfaceInsets.top;
847 final int oldInsetRight = mWindowAttributes.surfaceInsets.right;
848 final int oldInsetBottom = mWindowAttributes.surfaceInsets.bottom;
849 final int oldSoftInputMode = mWindowAttributes.softInputMode;
Alan Viverette5435a302015-01-29 10:25:34 -0800850 final boolean oldHasManualSurfaceInsets = mWindowAttributes.hasManualSurfaceInsets;
Alan Viverettedbed8932014-08-06 17:54:52 -0700851
Dianne Hackborn9d090892012-06-11 18:35:41 -0700852 // Keep track of the actual window flags supplied by the client.
853 mClientWindowLayoutFlags = attrs.flags;
Alan Viverettedbed8932014-08-06 17:54:52 -0700854
855 // Preserve compatible window flag if exists.
856 final int compatibleWindowFlag = mWindowAttributes.privateFlags
Adam Lesinski95c42972013-10-02 10:13:27 -0700857 & WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW;
Alan Viverettedbed8932014-08-06 17:54:52 -0700858
859 // Transfer over system UI visibility values as they carry current state.
Craig Mautner3fe38c02012-05-03 17:28:09 -0700860 attrs.systemUiVisibility = mWindowAttributes.systemUiVisibility;
861 attrs.subtreeSystemUiVisibility = mWindowAttributes.subtreeSystemUiVisibility;
Alan Viverettedbed8932014-08-06 17:54:52 -0700862
Romain Guyf21c9b02011-09-06 16:56:54 -0700863 mWindowAttributesChangesFlag = mWindowAttributes.copyFrom(attrs);
John Spurlockbd957402013-10-03 11:38:39 -0400864 if ((mWindowAttributesChangesFlag
865 & WindowManager.LayoutParams.TRANSLUCENT_FLAGS_CHANGED) != 0) {
866 // Recompute system ui visibility.
867 mAttachInfo.mRecomputeGlobalAttributes = true;
868 }
Dianne Hackbornc2293022013-02-06 23:14:49 -0800869 if (mWindowAttributes.packageName == null) {
870 mWindowAttributes.packageName = mBasePackageName;
871 }
Adam Lesinski95c42972013-10-02 10:13:27 -0700872 mWindowAttributes.privateFlags |= compatibleWindowFlag;
Dianne Hackborn9d090892012-06-11 18:35:41 -0700873
Alan Viverettedbed8932014-08-06 17:54:52 -0700874 // Restore old surface insets.
875 mWindowAttributes.surfaceInsets.set(
876 oldInsetLeft, oldInsetTop, oldInsetRight, oldInsetBottom);
Alan Viverette5435a302015-01-29 10:25:34 -0800877 mWindowAttributes.hasManualSurfaceInsets = oldHasManualSurfaceInsets;
Alan Viverettedbed8932014-08-06 17:54:52 -0700878
Dianne Hackborn9d090892012-06-11 18:35:41 -0700879 applyKeepScreenOnFlag(mWindowAttributes);
880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 if (newView) {
882 mSoftInputMode = attrs.softInputMode;
883 requestLayout();
884 }
Alan Viverettedbed8932014-08-06 17:54:52 -0700885
The Android Open Source Project10592532009-03-18 17:39:46 -0700886 // Don't lose the mode we last auto-computed.
Alan Viverettedbed8932014-08-06 17:54:52 -0700887 if ((attrs.softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
The Android Open Source Project10592532009-03-18 17:39:46 -0700888 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED) {
889 mWindowAttributes.softInputMode = (mWindowAttributes.softInputMode
890 & ~WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
Alan Viverettedbed8932014-08-06 17:54:52 -0700891 | (oldSoftInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST);
The Android Open Source Project10592532009-03-18 17:39:46 -0700892 }
Alan Viverettedbed8932014-08-06 17:54:52 -0700893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 mWindowAttributesChanged = true;
895 scheduleTraversals();
896 }
897 }
898
899 void handleAppVisibility(boolean visible) {
900 if (mAppVisible != visible) {
901 mAppVisible = visible;
902 scheduleTraversals();
John Reck73840ea2014-09-22 07:39:18 -0700903 if (!mAppVisible) {
904 WindowManagerGlobal.trimForeground();
905 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 }
907 }
908
909 void handleGetNewSurface() {
910 mNewSurfaceNeeded = true;
911 mFullRedrawNeeded = true;
912 scheduleTraversals();
913 }
914
Jeff Brownd912e1f2014-04-11 18:46:22 -0700915 private final DisplayListener mDisplayListener = new DisplayListener() {
916 @Override
917 public void onDisplayChanged(int displayId) {
918 if (mView != null && mDisplay.getDisplayId() == displayId) {
919 final int oldDisplayState = mAttachInfo.mDisplayState;
920 final int newDisplayState = mDisplay.getState();
921 if (oldDisplayState != newDisplayState) {
922 mAttachInfo.mDisplayState = newDisplayState;
Jeff Brownc2932a12014-11-20 18:04:05 -0800923 pokeDrawLockIfNeeded();
Jeff Brownd912e1f2014-04-11 18:46:22 -0700924 if (oldDisplayState != Display.STATE_UNKNOWN) {
925 final int oldScreenState = toViewScreenState(oldDisplayState);
926 final int newScreenState = toViewScreenState(newDisplayState);
927 if (oldScreenState != newScreenState) {
928 mView.dispatchScreenStateChanged(newScreenState);
929 }
930 if (oldDisplayState == Display.STATE_OFF) {
931 // Draw was suppressed so we need to for it to happen here.
932 mFullRedrawNeeded = true;
933 scheduleTraversals();
934 }
935 }
936 }
Romain Guy7e4e5612012-03-05 14:37:29 -0800937 }
938 }
Jeff Brownd912e1f2014-04-11 18:46:22 -0700939
940 @Override
941 public void onDisplayRemoved(int displayId) {
942 }
943
944 @Override
945 public void onDisplayAdded(int displayId) {
946 }
947
948 private int toViewScreenState(int displayState) {
949 return displayState == Display.STATE_OFF ?
950 View.SCREEN_STATE_OFF : View.SCREEN_STATE_ON;
951 }
952 };
Romain Guy7e4e5612012-03-05 14:37:29 -0800953
Jeff Brownc2932a12014-11-20 18:04:05 -0800954 void pokeDrawLockIfNeeded() {
955 final int displayState = mAttachInfo.mDisplayState;
956 if (mView != null && mAdded && mTraversalScheduled
957 && (displayState == Display.STATE_DOZE
958 || displayState == Display.STATE_DOZE_SUSPEND)) {
959 try {
960 mWindowSession.pokeDrawLock(mWindow);
961 } catch (RemoteException ex) {
962 // System server died, oh well.
963 }
964 }
965 }
966
Craig Mautner6018aee2012-10-23 14:27:49 -0700967 @Override
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700968 public void requestFitSystemWindows() {
969 checkThread();
Adrian Roosfa104232014-06-20 16:10:14 -0700970 mApplyInsetsRequested = true;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700971 scheduleTraversals();
972 }
973
Craig Mautner6018aee2012-10-23 14:27:49 -0700974 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 public void requestLayout() {
Chet Haase3efa7b52012-12-03 08:33:17 -0800976 if (!mHandlingLayoutInLayoutRequest) {
977 checkThread();
978 mLayoutRequested = true;
979 scheduleTraversals();
980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 }
982
Craig Mautner6018aee2012-10-23 14:27:49 -0700983 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 public boolean isLayoutRequested() {
985 return mLayoutRequested;
986 }
987
Romain Guycfef1232012-02-23 13:50:37 -0800988 void invalidate() {
989 mDirty.set(0, 0, mWidth, mHeight);
John Recke4267ea2014-06-03 15:53:15 -0700990 if (!mWillDrawSoon) {
991 scheduleTraversals();
992 }
Romain Guycfef1232012-02-23 13:50:37 -0800993 }
994
Dianne Hackborna53de062012-05-08 18:53:51 -0700995 void invalidateWorld(View view) {
996 view.invalidate();
997 if (view instanceof ViewGroup) {
Romain Guyf84208f2012-09-13 22:50:18 -0700998 ViewGroup parent = (ViewGroup) view;
999 for (int i = 0; i < parent.getChildCount(); i++) {
Dianne Hackborna53de062012-05-08 18:53:51 -07001000 invalidateWorld(parent.getChildAt(i));
1001 }
1002 }
1003 }
1004
Craig Mautner6018aee2012-10-23 14:27:49 -07001005 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 public void invalidateChild(View child, Rect dirty) {
Romain Guycfef1232012-02-23 13:50:37 -08001007 invalidateChildInParent(null, dirty);
1008 }
1009
Craig Mautner8f303ad2013-06-14 11:32:22 -07001010 @Override
Romain Guycfef1232012-02-23 13:50:37 -08001011 public ViewParent invalidateChildInParent(int[] location, Rect dirty) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 checkThread();
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001013 if (DEBUG_DRAW) Log.v(mTag, "Invalidate child: " + dirty);
Romain Guycfef1232012-02-23 13:50:37 -08001014
Chet Haase70d4ba12010-10-06 09:46:45 -07001015 if (dirty == null) {
Chet Haase70d4ba12010-10-06 09:46:45 -07001016 invalidate();
Romain Guycfef1232012-02-23 13:50:37 -08001017 return null;
Chet Haase3561d062012-10-23 12:54:51 -07001018 } else if (dirty.isEmpty() && !mIsAnimating) {
Chet Haase05e91ed2012-07-03 14:17:57 -07001019 return null;
Chet Haase70d4ba12010-10-06 09:46:45 -07001020 }
Romain Guycfef1232012-02-23 13:50:37 -08001021
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001022 if (mCurScrollY != 0 || mTranslator != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 mTempRect.set(dirty);
Romain Guy1e095972009-07-07 11:22:45 -07001024 dirty = mTempRect;
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001025 if (mCurScrollY != 0) {
Romain Guycfef1232012-02-23 13:50:37 -08001026 dirty.offset(0, -mCurScrollY);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001027 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001028 if (mTranslator != null) {
Romain Guy1e095972009-07-07 11:22:45 -07001029 mTranslator.translateRectInAppWindowToScreen(dirty);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001030 }
Romain Guy1e095972009-07-07 11:22:45 -07001031 if (mAttachInfo.mScalingRequired) {
1032 dirty.inset(-1, -1);
1033 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 }
Romain Guycfef1232012-02-23 13:50:37 -08001035
Alan Viverettea7ea65e2015-05-15 11:30:21 -07001036 invalidateRectOnScreen(dirty);
1037
1038 return null;
1039 }
1040
1041 private void invalidateRectOnScreen(Rect dirty) {
Romain Guycfef1232012-02-23 13:50:37 -08001042 final Rect localDirty = mDirty;
1043 if (!localDirty.isEmpty() && !localDirty.contains(dirty)) {
Romain Guy02ccac62011-06-24 13:20:23 -07001044 mAttachInfo.mSetIgnoreDirtyState = true;
Romain Guy7d695942010-12-01 17:22:29 -08001045 mAttachInfo.mIgnoreDirtyState = true;
1046 }
Romain Guycfef1232012-02-23 13:50:37 -08001047
1048 // Add the new dirty rect to the current one
1049 localDirty.union(dirty.left, dirty.top, dirty.right, dirty.bottom);
1050 // Intersect with the bounds of the window to skip
1051 // updates that lie outside of the visible region
Romain Guy7b2f8b82012-03-19 17:18:54 -07001052 final float appScale = mAttachInfo.mApplicationScale;
Chet Haase3561d062012-10-23 12:54:51 -07001053 final boolean intersected = localDirty.intersect(0, 0,
1054 (int) (mWidth * appScale + 0.5f), (int) (mHeight * appScale + 0.5f));
1055 if (!intersected) {
Chet Haaseb78ee0e2012-10-17 11:32:01 -07001056 localDirty.setEmpty();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 }
Chet Haase3561d062012-10-23 12:54:51 -07001058 if (!mWillDrawSoon && (intersected || mIsAnimating)) {
1059 scheduleTraversals();
1060 }
Romain Guy0d9275e2010-10-26 14:22:30 -07001061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062
Daniel Koulomzin087ae472015-12-16 17:52:25 -05001063 public void setIsAmbientMode(boolean ambient) {
1064 mIsAmbientMode = ambient;
1065 }
1066
George Mount41725de2015-04-09 08:23:05 -07001067 void setWindowStopped(boolean stopped) {
Dianne Hackbornce418e62011-03-01 14:31:38 -08001068 if (mStopped != stopped) {
1069 mStopped = stopped;
George Mount41725de2015-04-09 08:23:05 -07001070 if (!mStopped) {
Dianne Hackbornce418e62011-03-01 14:31:38 -08001071 scheduleTraversals();
1072 }
1073 }
1074 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075
George Mount41725de2015-04-09 08:23:05 -07001076 /**
1077 * Block the input events during an Activity Transition. The KEYCODE_BACK event is allowed
1078 * through to allow quick reversal of the Activity Transition.
1079 *
1080 * @param paused true to pause, false to resume.
1081 */
1082 public void setPausedForTransition(boolean paused) {
1083 mPausedForTransition = paused;
1084 }
1085
Craig Mautner8f303ad2013-06-14 11:32:22 -07001086 @Override
Romain Guycfef1232012-02-23 13:50:37 -08001087 public ViewParent getParent() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 return null;
1089 }
1090
Craig Mautner8f303ad2013-06-14 11:32:22 -07001091 @Override
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001092 public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 if (child != mView) {
1094 throw new RuntimeException("child is not mine, honest!");
1095 }
1096 // Note: don't apply scroll offset, because we want to know its
1097 // visibility in the virtual canvas being given to the view hierarchy.
1098 return r.intersect(0, 0, mWidth, mHeight);
1099 }
1100
Igor Murashkina86ab6402013-08-30 12:58:36 -07001101 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 public void bringChildToFront(View child) {
1103 }
1104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 int getHostVisibility() {
Filip Gruszczynski1a4dfe52015-11-15 10:58:57 -08001106 return (mAppVisible || mForceDecorViewVisibility) ? mView.getVisibility() : View.GONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 }
Romain Guy8506ab42009-06-11 17:35:47 -07001108
Chet Haasecca2c982011-05-20 14:34:18 -07001109 /**
1110 * Add LayoutTransition to the list of transitions to be started in the next traversal.
1111 * This list will be cleared after the transitions on the list are start()'ed. These
1112 * transitionsa re added by LayoutTransition itself when it sets up animations. The setup
1113 * happens during the layout phase of traversal, which we want to complete before any of the
1114 * animations are started (because those animations may side-effect properties that layout
1115 * depends upon, like the bounding rectangles of the affected views). So we add the transition
1116 * to the list and it is started just prior to starting the drawing phase of traversal.
1117 *
1118 * @param transition The LayoutTransition to be started on the next traversal.
1119 *
1120 * @hide
1121 */
1122 public void requestTransitionStart(LayoutTransition transition) {
1123 if (mPendingTransitions == null || !mPendingTransitions.contains(transition)) {
1124 if (mPendingTransitions == null) {
1125 mPendingTransitions = new ArrayList<LayoutTransition>();
1126 }
1127 mPendingTransitions.add(transition);
1128 }
1129 }
1130
John Recka5dda642014-05-22 15:43:54 -07001131 /**
1132 * Notifies the HardwareRenderer that a new frame will be coming soon.
1133 * Currently only {@link ThreadedRenderer} cares about this, and uses
1134 * this knowledge to adjust the scheduling of off-thread animations
1135 */
1136 void notifyRendererOfFramePending() {
1137 if (mAttachInfo.mHardwareRenderer != null) {
1138 mAttachInfo.mHardwareRenderer.notifyFramePending();
1139 }
1140 }
1141
Jeff Brownebb2d8d2012-03-23 17:14:34 -07001142 void scheduleTraversals() {
1143 if (!mTraversalScheduled) {
1144 mTraversalScheduled = true;
Jeff Brown3d4e7efe2015-02-26 15:34:16 -08001145 mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier();
Jeff Brownebb2d8d2012-03-23 17:14:34 -07001146 mChoreographer.postCallback(
1147 Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
Michael Wright9d744c72014-02-18 21:27:42 -08001148 if (!mUnbufferedInputDispatch) {
1149 scheduleConsumeBatchedInput();
1150 }
John Recka5dda642014-05-22 15:43:54 -07001151 notifyRendererOfFramePending();
Jeff Brownc2932a12014-11-20 18:04:05 -08001152 pokeDrawLockIfNeeded();
Jeff Brown96e942d2011-11-30 19:55:01 -08001153 }
Jeff Brownebb2d8d2012-03-23 17:14:34 -07001154 }
Jeff Brown96e942d2011-11-30 19:55:01 -08001155
Jeff Brownebb2d8d2012-03-23 17:14:34 -07001156 void unscheduleTraversals() {
1157 if (mTraversalScheduled) {
1158 mTraversalScheduled = false;
Jeff Brown3d4e7efe2015-02-26 15:34:16 -08001159 mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier);
Jeff Brownebb2d8d2012-03-23 17:14:34 -07001160 mChoreographer.removeCallbacks(
1161 Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
1162 }
1163 }
1164
1165 void doTraversal() {
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
Jeff Brownebb2d8d2012-03-23 17:14:34 -07001170 if (mProfile) {
1171 Debug.startMethodTracing("ViewAncestor");
Jeff Brown96e942d2011-11-30 19:55:01 -08001172 }
Jeff Brown96e942d2011-11-30 19:55:01 -08001173
Chris Craike22c59b2015-05-21 18:33:37 -07001174 performTraversals();
Jeff Brown96e942d2011-11-30 19:55:01 -08001175
Jeff Brownebb2d8d2012-03-23 17:14:34 -07001176 if (mProfile) {
1177 Debug.stopMethodTracing();
1178 mProfile = false;
1179 }
Jeff Brown96e942d2011-11-30 19:55:01 -08001180 }
1181 }
1182
Dianne Hackborn9d090892012-06-11 18:35:41 -07001183 private void applyKeepScreenOnFlag(WindowManager.LayoutParams params) {
1184 // Update window's global keep screen on flag: if a view has requested
1185 // that the screen be kept on, then it is always set; otherwise, it is
1186 // set to whatever the client last requested for the global state.
1187 if (mAttachInfo.mKeepScreenOn) {
1188 params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
1189 } else {
1190 params.flags = (params.flags&~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
1191 | (mClientWindowLayoutFlags&WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1192 }
1193 }
1194
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001195 private boolean collectViewAttributes() {
Chris Craikd36a81f2014-07-17 10:16:51 -07001196 if (mAttachInfo.mRecomputeGlobalAttributes) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001197 //Log.i(mTag, "Computing view hierarchy attributes!");
Chris Craikd36a81f2014-07-17 10:16:51 -07001198 mAttachInfo.mRecomputeGlobalAttributes = false;
1199 boolean oldScreenOn = mAttachInfo.mKeepScreenOn;
1200 mAttachInfo.mKeepScreenOn = false;
1201 mAttachInfo.mSystemUiVisibility = 0;
1202 mAttachInfo.mHasSystemUiListeners = false;
1203 mView.dispatchCollectViewAttributes(mAttachInfo, 0);
1204 mAttachInfo.mSystemUiVisibility &= ~mAttachInfo.mDisabledSystemUiVisibility;
Craig Mautner7eac0f52012-09-13 13:14:14 -07001205 WindowManager.LayoutParams params = mWindowAttributes;
Chris Craikd36a81f2014-07-17 10:16:51 -07001206 mAttachInfo.mSystemUiVisibility |= getImpliedSystemUiVisibility(params);
1207 if (mAttachInfo.mKeepScreenOn != oldScreenOn
1208 || mAttachInfo.mSystemUiVisibility != params.subtreeSystemUiVisibility
1209 || mAttachInfo.mHasSystemUiListeners != params.hasSystemUiListeners) {
Dianne Hackborn9d090892012-06-11 18:35:41 -07001210 applyKeepScreenOnFlag(params);
Chris Craikd36a81f2014-07-17 10:16:51 -07001211 params.subtreeSystemUiVisibility = mAttachInfo.mSystemUiVisibility;
1212 params.hasSystemUiListeners = mAttachInfo.mHasSystemUiListeners;
1213 mView.dispatchWindowSystemUiVisiblityChanged(mAttachInfo.mSystemUiVisibility);
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001214 return true;
1215 }
1216 }
1217 return false;
1218 }
1219
John Spurlockbd957402013-10-03 11:38:39 -04001220 private int getImpliedSystemUiVisibility(WindowManager.LayoutParams params) {
1221 int vis = 0;
1222 // Translucent decor window flags imply stable system ui visibility.
1223 if ((params.flags & WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) != 0) {
1224 vis |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
1225 }
1226 if ((params.flags & WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION) != 0) {
1227 vis |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1228 }
1229 return vis;
1230 }
1231
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001232 private boolean measureHierarchy(final View host, final WindowManager.LayoutParams lp,
1233 final Resources res, final int desiredWindowWidth, final int desiredWindowHeight) {
1234 int childWidthMeasureSpec;
1235 int childHeightMeasureSpec;
1236 boolean windowSizeMayChange = false;
1237
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001238 if (DEBUG_ORIENTATION || DEBUG_LAYOUT) Log.v(mTag,
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001239 "Measuring " + host + " in display " + desiredWindowWidth
1240 + "x" + desiredWindowHeight + "...");
1241
1242 boolean goodMeasure = false;
1243 if (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT) {
1244 // On large screens, we don't want to allow dialogs to just
1245 // stretch to fill the entire width of the screen to display
1246 // one line of text. First try doing the layout at a smaller
1247 // size to see if it will fit.
1248 final DisplayMetrics packageMetrics = res.getDisplayMetrics();
1249 res.getValue(com.android.internal.R.dimen.config_prefDialogWidth, mTmpValue, true);
1250 int baseSize = 0;
1251 if (mTmpValue.type == TypedValue.TYPE_DIMENSION) {
1252 baseSize = (int)mTmpValue.getDimension(packageMetrics);
1253 }
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001254 if (DEBUG_DIALOG) Log.v(mTag, "Window " + mView + ": baseSize=" + baseSize);
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001255 if (baseSize != 0 && desiredWindowWidth > baseSize) {
1256 childWidthMeasureSpec = getRootMeasureSpec(baseSize, lp.width);
1257 childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
Jeff Brownc8d2668b2012-05-16 17:23:59 -07001258 performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001259 if (DEBUG_DIALOG) Log.v(mTag, "Window " + mView + ": measured ("
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001260 + host.getMeasuredWidth() + "," + host.getMeasuredHeight() + ")");
1261 if ((host.getMeasuredWidthAndState()&View.MEASURED_STATE_TOO_SMALL) == 0) {
1262 goodMeasure = true;
1263 } else {
1264 // Didn't fit in that size... try expanding a bit.
1265 baseSize = (baseSize+desiredWindowWidth)/2;
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001266 if (DEBUG_DIALOG) Log.v(mTag, "Window " + mView + ": next baseSize="
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001267 + baseSize);
1268 childWidthMeasureSpec = getRootMeasureSpec(baseSize, lp.width);
Jeff Brownc8d2668b2012-05-16 17:23:59 -07001269 performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001270 if (DEBUG_DIALOG) Log.v(mTag, "Window " + mView + ": measured ("
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001271 + host.getMeasuredWidth() + "," + host.getMeasuredHeight() + ")");
1272 if ((host.getMeasuredWidthAndState()&View.MEASURED_STATE_TOO_SMALL) == 0) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001273 if (DEBUG_DIALOG) Log.v(mTag, "Good!");
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001274 goodMeasure = true;
1275 }
1276 }
1277 }
1278 }
1279
1280 if (!goodMeasure) {
1281 childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width);
1282 childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
Jeff Brownc8d2668b2012-05-16 17:23:59 -07001283 performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001284 if (mWidth != host.getMeasuredWidth() || mHeight != host.getMeasuredHeight()) {
1285 windowSizeMayChange = true;
1286 }
1287 }
1288
1289 if (DBG) {
1290 System.out.println("======================================");
1291 System.out.println("performTraversals -- after measure");
1292 host.debug();
1293 }
1294
1295 return windowSizeMayChange;
1296 }
1297
Alan Viverettefed3f722013-11-14 14:48:20 -08001298 /**
1299 * Modifies the input matrix such that it maps view-local coordinates to
1300 * on-screen coordinates.
1301 *
1302 * @param m input matrix to modify
1303 */
1304 void transformMatrixToGlobal(Matrix m) {
Dake Gu7bf379c2014-07-15 16:29:38 -07001305 m.preTranslate(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
Alan Viverettefed3f722013-11-14 14:48:20 -08001306 }
1307
1308 /**
1309 * Modifies the input matrix such that it maps on-screen coordinates to
1310 * view-local coordinates.
1311 *
1312 * @param m input matrix to modify
1313 */
1314 void transformMatrixToLocal(Matrix m) {
Dake Gu7bf379c2014-07-15 16:29:38 -07001315 m.postTranslate(-mAttachInfo.mWindowLeft, -mAttachInfo.mWindowTop);
Alan Viverettefed3f722013-11-14 14:48:20 -08001316 }
1317
Filip Gruszczynski954289d2015-02-26 15:46:47 -08001318 /* package */ WindowInsets getWindowInsets(boolean forceConstruct) {
1319 if (mLastWindowInsets == null || forceConstruct) {
1320 mDispatchContentInsets.set(mAttachInfo.mContentInsets);
1321 mDispatchStableInsets.set(mAttachInfo.mStableInsets);
1322 Rect contentInsets = mDispatchContentInsets;
1323 Rect stableInsets = mDispatchStableInsets;
1324 // For dispatch we preserve old logic, but for direct requests from Views we allow to
1325 // immediately use pending insets.
1326 if (!forceConstruct
1327 && (!mPendingContentInsets.equals(contentInsets) ||
1328 !mPendingStableInsets.equals(stableInsets))) {
1329 contentInsets = mPendingContentInsets;
1330 stableInsets = mPendingStableInsets;
1331 }
Filip Gruszczynski2217f612015-05-26 11:32:08 -07001332 Rect outsets = mAttachInfo.mOutsets;
1333 if (outsets.left > 0 || outsets.top > 0 || outsets.right > 0 || outsets.bottom > 0) {
1334 contentInsets = new Rect(contentInsets.left + outsets.left,
1335 contentInsets.top + outsets.top, contentInsets.right + outsets.right,
1336 contentInsets.bottom + outsets.bottom);
1337 }
Filip Gruszczynski954289d2015-02-26 15:46:47 -08001338 mLastWindowInsets = new WindowInsets(contentInsets,
Adam Powell01f280d2015-05-18 16:07:42 -07001339 null /* windowDecorInsets */, stableInsets,
1340 mContext.getResources().getConfiguration().isScreenRound());
Filip Gruszczynski954289d2015-02-26 15:46:47 -08001341 }
1342 return mLastWindowInsets;
1343 }
1344
Adam Powell2accbf92014-04-16 23:14:57 +00001345 void dispatchApplyInsets(View host) {
Filip Gruszczynski954289d2015-02-26 15:46:47 -08001346 host.dispatchApplyWindowInsets(getWindowInsets(true /* forceConstruct */));
Adam Powell2accbf92014-04-16 23:14:57 +00001347 }
1348
Chong Zhangf6525ce2016-01-14 17:09:56 -08001349 private static boolean shouldUseDisplaySize(final WindowManager.LayoutParams lp) {
1350 return lp.type == TYPE_STATUS_BAR_PANEL
1351 || lp.type == TYPE_INPUT_METHOD
1352 || lp.type == TYPE_VOLUME_OVERLAY;
1353 }
1354
1355 private int dipToPx(int dip) {
1356 final DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
1357 return (int) (displayMetrics.density * dip + 0.5f);
1358 }
1359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 private void performTraversals() {
1361 // cache mView since it is used so much below...
1362 final View host = mView;
1363
1364 if (DBG) {
1365 System.out.println("======================================");
1366 System.out.println("performTraversals");
1367 host.debug();
1368 }
1369
1370 if (host == null || !mAdded)
1371 return;
1372
Craig Mautnerdf2390a2012-08-29 20:59:22 -07001373 mIsInTraversal = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 mWillDrawSoon = true;
Dianne Hackborn711e62a2010-11-29 16:38:22 -08001375 boolean windowSizeMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 boolean newSurface = false;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001377 boolean surfaceChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 WindowManager.LayoutParams lp = mWindowAttributes;
1379
1380 int desiredWindowWidth;
1381 int desiredWindowHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 final int viewVisibility = getHostVisibility();
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001384 final boolean viewVisibilityChanged = !mFirst
1385 && (mViewVisibility != viewVisibility || mNewSurfaceNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386
1387 WindowManager.LayoutParams params = null;
1388 if (mWindowAttributesChanged) {
1389 mWindowAttributesChanged = false;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001390 surfaceChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 params = lp;
1392 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001393 CompatibilityInfo compatibilityInfo = mDisplayAdjustments.getCompatibilityInfo();
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001394 if (compatibilityInfo.supportsScreen() == mLastInCompatMode) {
1395 params = lp;
Jeff Brown96e942d2011-11-30 19:55:01 -08001396 mFullRedrawNeeded = true;
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001397 mLayoutRequested = true;
1398 if (mLastInCompatMode) {
Adam Lesinski95c42972013-10-02 10:13:27 -07001399 params.privateFlags &= ~WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW;
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001400 mLastInCompatMode = false;
1401 } else {
Adam Lesinski95c42972013-10-02 10:13:27 -07001402 params.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW;
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001403 mLastInCompatMode = true;
1404 }
1405 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07001406
Romain Guyf21c9b02011-09-06 16:56:54 -07001407 mWindowAttributesChangesFlag = 0;
Igor Murashkina86ab6402013-08-30 12:58:36 -07001408
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001409 Rect frame = mWinFrame;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 if (mFirst) {
Jeff Brown96e942d2011-11-30 19:55:01 -08001411 mFullRedrawNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 mLayoutRequested = true;
1413
Chong Zhangf6525ce2016-01-14 17:09:56 -08001414 if (shouldUseDisplaySize(lp)) {
Dianne Hackborna239c842011-06-01 12:28:20 -07001415 // NOTE -- system code, won't try to do compat mode.
Jeff Brownbc68a592011-07-25 12:58:12 -07001416 Point size = new Point();
Jeff Brown98365d72012-08-19 20:30:52 -07001417 mDisplay.getRealSize(size);
Jeff Brownbc68a592011-07-25 12:58:12 -07001418 desiredWindowWidth = size.x;
1419 desiredWindowHeight = size.y;
Dianne Hackborna239c842011-06-01 12:28:20 -07001420 } else {
Chong Zhangf6525ce2016-01-14 17:09:56 -08001421 Configuration config = mContext.getResources().getConfiguration();
1422 desiredWindowWidth = dipToPx(config.screenWidthDp);
1423 desiredWindowHeight = dipToPx(config.screenHeightDp);
Dianne Hackborna239c842011-06-01 12:28:20 -07001424 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425
Romain Guyc5d55862011-01-21 19:01:46 -08001426 // We used to use the following condition to choose 32 bits drawing caches:
1427 // PixelFormat.hasAlpha(lp.format) || lp.format == PixelFormat.RGBX_8888
1428 // However, windows are now always 32 bits by default, so choose 32 bits
Chris Craikd36a81f2014-07-17 10:16:51 -07001429 mAttachInfo.mUse32BitDrawingCache = true;
1430 mAttachInfo.mHasWindowFocus = false;
1431 mAttachInfo.mWindowVisibility = viewVisibility;
1432 mAttachInfo.mRecomputeGlobalAttributes = false;
Dianne Hackborn694f79b2010-03-17 19:44:59 -07001433 mLastConfiguration.setTo(host.getResources().getConfiguration());
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001434 mLastSystemUiVisibility = mAttachInfo.mSystemUiVisibility;
Fabrice Di Megliob003e282012-10-17 17:20:19 -07001435 // Set the layout direction if it has not been set before (inherit is the default)
1436 if (mViewLayoutDirectionInitial == View.LAYOUT_DIRECTION_INHERIT) {
1437 host.setLayoutDirection(mLastConfiguration.getLayoutDirection());
1438 }
Chris Craikd36a81f2014-07-17 10:16:51 -07001439 host.dispatchAttachedToWindow(mAttachInfo, 0);
1440 mAttachInfo.mTreeObserver.dispatchOnWindowAttachedChange(true);
Adam Powell2accbf92014-04-16 23:14:57 +00001441 dispatchApplyInsets(host);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001442 //Log.i(mTag, "Screen on initialized: " + attachInfo.mKeepScreenOn);
svetoslavganov75986cf2009-05-14 22:28:01 -07001443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 } else {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001445 desiredWindowWidth = frame.width();
1446 desiredWindowHeight = frame.height();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 if (desiredWindowWidth != mWidth || desiredWindowHeight != mHeight) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001448 if (DEBUG_ORIENTATION) Log.v(mTag, "View " + host + " resized to: " + frame);
Jeff Brown96e942d2011-11-30 19:55:01 -08001449 mFullRedrawNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 mLayoutRequested = true;
Dianne Hackborn711e62a2010-11-29 16:38:22 -08001451 windowSizeMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 }
1453 }
1454
1455 if (viewVisibilityChanged) {
Chris Craikd36a81f2014-07-17 10:16:51 -07001456 mAttachInfo.mWindowVisibility = viewVisibility;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 host.dispatchWindowVisibilityChanged(viewVisibility);
1458 if (viewVisibility != View.VISIBLE || mNewSurfaceNeeded) {
Skuhneb8160872015-09-22 09:51:39 -07001459 endDragResizing();
Romain Guy65b345f2011-07-27 18:51:50 -07001460 destroyHardwareResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 }
1462 if (viewVisibility == View.GONE) {
1463 // After making a window gone, we will count it as being
1464 // shown for the first time the next time it gets focus.
1465 mHasHadWindowFocus = false;
1466 }
1467 }
1468
Alan Viverette7dbc3bf2015-01-28 16:14:36 -08001469 // Non-visible windows can't hold accessibility focus.
1470 if (mAttachInfo.mWindowVisibility != View.VISIBLE) {
1471 host.clearAccessibilityFocus();
1472 }
1473
Chet Haaseb78c2842012-04-19 13:39:50 -07001474 // Execute enqueued actions on every traversal in case a detached view enqueued an action
Chris Craikd36a81f2014-07-17 10:16:51 -07001475 getRunQueue().executeActions(mAttachInfo.mHandler);
Chet Haaseb78c2842012-04-19 13:39:50 -07001476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 boolean insetsChanged = false;
Romain Guy8506ab42009-06-11 17:35:47 -07001478
Craig Mautner72d6f212015-02-19 16:33:09 -08001479 boolean layoutRequested = mLayoutRequested && (!mStopped || mReportNextDraw);
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001480 if (layoutRequested) {
Romain Guy15df6702009-08-17 20:17:30 -07001481
Dianne Hackborn711e62a2010-11-29 16:38:22 -08001482 final Resources res = mView.getContext().getResources();
1483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 if (mFirst) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 // make sure touch mode code executes by setting cached value
1486 // to opposite of the added touch mode.
1487 mAttachInfo.mInTouchMode = !mAddedTouchMode;
Romain Guy2d4cff62010-04-09 15:39:00 -07001488 ensureTouchModeLocally(mAddedTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 } else {
Dianne Hackbornc4aad012013-02-22 15:05:25 -08001490 if (!mPendingOverscanInsets.equals(mAttachInfo.mOverscanInsets)) {
1491 insetsChanged = true;
1492 }
Dianne Hackbornfa6b35b2011-08-24 17:03:54 -07001493 if (!mPendingContentInsets.equals(mAttachInfo.mContentInsets)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 insetsChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 }
Adrian Roosfa104232014-06-20 16:10:14 -07001496 if (!mPendingStableInsets.equals(mAttachInfo.mStableInsets)) {
1497 insetsChanged = true;
1498 }
Dianne Hackbornfa6b35b2011-08-24 17:03:54 -07001499 if (!mPendingVisibleInsets.equals(mAttachInfo.mVisibleInsets)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 mAttachInfo.mVisibleInsets.set(mPendingVisibleInsets);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001501 if (DEBUG_LAYOUT) Log.v(mTag, "Visible insets changing to: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 + mAttachInfo.mVisibleInsets);
1503 }
Filip Gruszczynski2217f612015-05-26 11:32:08 -07001504 if (!mPendingOutsets.equals(mAttachInfo.mOutsets)) {
1505 insetsChanged = true;
1506 }
Chong Zhangf6525ce2016-01-14 17:09:56 -08001507 if (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT
1508 || lp.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
Dianne Hackborn711e62a2010-11-29 16:38:22 -08001509 windowSizeMayChange = true;
Chong Zhangf6525ce2016-01-14 17:09:56 -08001510
1511 if (shouldUseDisplaySize(lp)) {
1512 // NOTE -- system code, won't try to do compat mode.
1513 Point size = new Point();
1514 mDisplay.getRealSize(size);
1515 desiredWindowWidth = size.x;
1516 desiredWindowHeight = size.y;
1517 } else {
1518 Configuration config = res.getConfiguration();
1519 desiredWindowWidth = dipToPx(config.screenWidthDp);
1520 desiredWindowHeight = dipToPx(config.screenHeightDp);
1521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 }
1523 }
1524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 // Ask host how big it wants to be
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001526 windowSizeMayChange |= measureHierarchy(host, lp, res,
1527 desiredWindowWidth, desiredWindowHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 }
1529
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001530 if (collectViewAttributes()) {
1531 params = lp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 }
Chris Craikd36a81f2014-07-17 10:16:51 -07001533 if (mAttachInfo.mForceReportNewAttributes) {
1534 mAttachInfo.mForceReportNewAttributes = false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07001535 params = lp;
1536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537
Chris Craikd36a81f2014-07-17 10:16:51 -07001538 if (mFirst || mAttachInfo.mViewVisibilityChanged) {
1539 mAttachInfo.mViewVisibilityChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 int resizeMode = mSoftInputMode &
1541 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST;
1542 // If we are in auto resize mode, then we need to determine
1543 // what mode to use now.
1544 if (resizeMode == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED) {
Chris Craikd36a81f2014-07-17 10:16:51 -07001545 final int N = mAttachInfo.mScrollContainers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 for (int i=0; i<N; i++) {
Chris Craikd36a81f2014-07-17 10:16:51 -07001547 if (mAttachInfo.mScrollContainers.get(i).isShown()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 resizeMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
1549 }
1550 }
1551 if (resizeMode == 0) {
1552 resizeMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;
1553 }
1554 if ((lp.softInputMode &
1555 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) != resizeMode) {
1556 lp.softInputMode = (lp.softInputMode &
1557 ~WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) |
1558 resizeMode;
1559 params = lp;
1560 }
1561 }
1562 }
Romain Guy8506ab42009-06-11 17:35:47 -07001563
Dianne Hackbornc4aad012013-02-22 15:05:25 -08001564 if (params != null) {
1565 if ((host.mPrivateFlags & View.PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
1566 if (!PixelFormat.formatHasAlpha(params.format)) {
1567 params.format = PixelFormat.TRANSLUCENT;
1568 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 }
Dianne Hackbornc4aad012013-02-22 15:05:25 -08001570 mAttachInfo.mOverscanRequested = (params.flags
1571 & WindowManager.LayoutParams.FLAG_LAYOUT_IN_OVERSCAN) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 }
1573
Adrian Roosfa104232014-06-20 16:10:14 -07001574 if (mApplyInsetsRequested) {
1575 mApplyInsetsRequested = false;
Dianne Hackbornc4aad012013-02-22 15:05:25 -08001576 mLastOverscanRequested = mAttachInfo.mOverscanRequested;
Adam Powell2accbf92014-04-16 23:14:57 +00001577 dispatchApplyInsets(host);
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001578 if (mLayoutRequested) {
1579 // Short-circuit catching a new layout request here, so
1580 // we don't need to go through two layout passes when things
1581 // change due to fitting system windows, which can happen a lot.
1582 windowSizeMayChange |= measureHierarchy(host, lp,
1583 mView.getContext().getResources(),
1584 desiredWindowWidth, desiredWindowHeight);
1585 }
1586 }
1587
1588 if (layoutRequested) {
1589 // Clear this now, so that if anything requests a layout in the
1590 // rest of this function we will catch it and re-run a full
1591 // layout pass.
1592 mLayoutRequested = false;
1593 }
1594
1595 boolean windowShouldResize = layoutRequested && windowSizeMayChange
Dianne Hackborn189ee182010-12-02 21:48:53 -08001596 && ((mWidth != host.getMeasuredWidth() || mHeight != host.getMeasuredHeight())
Romain Guy2e4f4262010-04-06 11:07:52 -07001597 || (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT &&
1598 frame.width() < desiredWindowWidth && frame.width() != mWidth)
1599 || (lp.height == ViewGroup.LayoutParams.WRAP_CONTENT &&
1600 frame.height() < desiredWindowHeight && frame.height() != mHeight));
Jorim Jaggi4846ee32016-01-07 17:39:12 +01001601 windowShouldResize |= mDragResizing && mResizeMode == RESIZE_MODE_FREEFORM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602
Jorim Jaggia7262a82015-11-03 15:15:40 +01001603 // If the backdrop frame doesn't equal to a frame, we are starting a resize operation, so
1604 // force it to be resized.
1605 windowShouldResize |= !mPendingBackDropFrame.equals(mWinFrame);
1606
Jorim Jaggi4846ee32016-01-07 17:39:12 +01001607 // If the activity was just relaunched, it might have unfrozen the task bounds (while
1608 // relaunching), so we need to force a call into window manager to pick up the latest
1609 // bounds.
1610 windowShouldResize |= mActivityRelaunched;
1611
Jeff Brown2e05ec32013-09-30 15:57:43 -07001612 // Determine whether to compute insets.
1613 // If there are no inset listeners remaining then we may still need to compute
1614 // insets in case the old insets were non-empty and must be reset.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 final boolean computesInternalInsets =
Chris Craikd36a81f2014-07-17 10:16:51 -07001616 mAttachInfo.mTreeObserver.hasComputeInternalInsetsListeners()
1617 || mAttachInfo.mHasNonEmptyGivenInternalInsets;
Romain Guy812ccbe2010-06-01 14:07:24 -07001618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 boolean insetsPending = false;
1620 int relayoutResult = 0;
Romain Guy812ccbe2010-06-01 14:07:24 -07001621
Filip Gruszczynski1a4dfe52015-11-15 10:58:57 -08001622 final boolean isViewVisible = viewVisibility == View.VISIBLE;
Romain Guy812ccbe2010-06-01 14:07:24 -07001623 if (mFirst || windowShouldResize || insetsChanged ||
1624 viewVisibilityChanged || params != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625
Alan Viverette64bf97a2015-09-18 16:42:00 -04001626 if (isViewVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 // If this window is giving internal insets to the window
1628 // manager, and it is being added or changing its visibility,
1629 // then we want to first give the window manager "fake"
1630 // insets to cause it to effectively ignore the content of
1631 // the window during layout. This avoids it briefly causing
1632 // other windows to resize/move based on the raw frame of the
1633 // window, waiting until we can finish laying out this window
1634 // and get back to the window manager with the ultimately
1635 // computed insets.
Romain Guy812ccbe2010-06-01 14:07:24 -07001636 insetsPending = computesInternalInsets && (mFirst || viewVisibilityChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 }
1638
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001639 if (mSurfaceHolder != null) {
1640 mSurfaceHolder.mSurfaceLock.lock();
1641 mDrawingAllowed = true;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001642 }
Romain Guy812ccbe2010-06-01 14:07:24 -07001643
Romain Guyc361da82010-10-25 15:29:10 -07001644 boolean hwInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 boolean contentInsetsChanged = false;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001646 boolean hadSurface = mSurface.isValid();
Romain Guy812ccbe2010-06-01 14:07:24 -07001647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 try {
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001649 if (DEBUG_LAYOUT) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001650 Log.i(mTag, "host=w:" + host.getMeasuredWidth() + ", h:" +
Dianne Hackborn189ee182010-12-02 21:48:53 -08001651 host.getMeasuredHeight() + ", params=" + params);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001652 }
Romain Guy2a83f002011-01-18 18:28:21 -08001653
John Reckf7d9c1d2014-04-09 10:01:03 -07001654 if (mAttachInfo.mHardwareRenderer != null) {
1655 // relayoutWindow may decide to destroy mSurface. As that decision
1656 // happens in WindowManager service, we need to be defensive here
1657 // and stop using the surface in case it gets destroyed.
John Reck01a5ea32014-12-03 13:01:07 -08001658 if (mAttachInfo.mHardwareRenderer.pauseSurface(mSurface)) {
1659 // Animations were running so we need to push a frame
1660 // to resume them
1661 mDirty.set(0, 0, mWidth, mHeight);
1662 }
John Reckba6adf62015-02-19 14:36:50 -08001663 mChoreographer.mFrameInfo.addFlags(FrameInfo.FLAG_WINDOW_LAYOUT_CHANGED);
John Reckf7d9c1d2014-04-09 10:01:03 -07001664 }
Romain Guy2a83f002011-01-18 18:28:21 -08001665 final int surfaceGenerationId = mSurface.getGenerationId();
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001666 relayoutResult = relayoutWindow(params, viewVisibility, insetsPending);
1667
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001668 if (DEBUG_LAYOUT) Log.v(mTag, "relayout: frame=" + frame.toShortString()
Dianne Hackbornc4aad012013-02-22 15:05:25 -08001669 + " overscan=" + mPendingOverscanInsets.toShortString()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 + " content=" + mPendingContentInsets.toShortString()
1671 + " visible=" + mPendingVisibleInsets.toShortString()
Adrian Roosfa104232014-06-20 16:10:14 -07001672 + " visible=" + mPendingStableInsets.toShortString()
Filip Gruszczynski0ec13282015-06-25 11:26:01 -07001673 + " outsets=" + mPendingOutsets.toShortString()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 + " surface=" + mSurface);
Romain Guy8506ab42009-06-11 17:35:47 -07001675
Dianne Hackborn694f79b2010-03-17 19:44:59 -07001676 if (mPendingConfiguration.seq != 0) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001677 if (DEBUG_CONFIGURATION) Log.v(mTag, "Visible with new config: "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07001678 + mPendingConfiguration);
riddle_hsu164725c2015-11-12 14:07:12 +08001679 updateConfiguration(new Configuration(mPendingConfiguration), !mFirst);
Dianne Hackborn694f79b2010-03-17 19:44:59 -07001680 mPendingConfiguration.seq = 0;
1681 }
Dianne Hackborn3556c9a2012-05-04 16:31:25 -07001682
Dianne Hackbornc4aad012013-02-22 15:05:25 -08001683 final boolean overscanInsetsChanged = !mPendingOverscanInsets.equals(
1684 mAttachInfo.mOverscanInsets);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 contentInsetsChanged = !mPendingContentInsets.equals(
1686 mAttachInfo.mContentInsets);
Dianne Hackbornc4aad012013-02-22 15:05:25 -08001687 final boolean visibleInsetsChanged = !mPendingVisibleInsets.equals(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 mAttachInfo.mVisibleInsets);
Adrian Roosfa104232014-06-20 16:10:14 -07001689 final boolean stableInsetsChanged = !mPendingStableInsets.equals(
1690 mAttachInfo.mStableInsets);
Filip Gruszczynski2217f612015-05-26 11:32:08 -07001691 final boolean outsetsChanged = !mPendingOutsets.equals(mAttachInfo.mOutsets);
Chong Zhangf4abc2b2015-11-12 23:40:58 -08001692 final boolean surfaceSizeChanged = (relayoutResult
1693 & WindowManagerGlobal.RELAYOUT_RES_SURFACE_RESIZED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 if (contentInsetsChanged) {
1695 mAttachInfo.mContentInsets.set(mPendingContentInsets);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001696 if (DEBUG_LAYOUT) Log.v(mTag, "Content insets changing to: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 + mAttachInfo.mContentInsets);
1698 }
Dianne Hackbornc4aad012013-02-22 15:05:25 -08001699 if (overscanInsetsChanged) {
1700 mAttachInfo.mOverscanInsets.set(mPendingOverscanInsets);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001701 if (DEBUG_LAYOUT) Log.v(mTag, "Overscan insets changing to: "
Dianne Hackbornc4aad012013-02-22 15:05:25 -08001702 + mAttachInfo.mOverscanInsets);
1703 // Need to relayout with content insets.
1704 contentInsetsChanged = true;
1705 }
Adrian Roosfa104232014-06-20 16:10:14 -07001706 if (stableInsetsChanged) {
1707 mAttachInfo.mStableInsets.set(mPendingStableInsets);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001708 if (DEBUG_LAYOUT) Log.v(mTag, "Decor insets changing to: "
Adrian Roosfa104232014-06-20 16:10:14 -07001709 + mAttachInfo.mStableInsets);
1710 // Need to relayout with content insets.
1711 contentInsetsChanged = true;
1712 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001713 if (contentInsetsChanged || mLastSystemUiVisibility !=
Adrian Roosfa104232014-06-20 16:10:14 -07001714 mAttachInfo.mSystemUiVisibility || mApplyInsetsRequested
Filip Gruszczynski2217f612015-05-26 11:32:08 -07001715 || mLastOverscanRequested != mAttachInfo.mOverscanRequested
1716 || outsetsChanged) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001717 mLastSystemUiVisibility = mAttachInfo.mSystemUiVisibility;
Dianne Hackbornc4aad012013-02-22 15:05:25 -08001718 mLastOverscanRequested = mAttachInfo.mOverscanRequested;
Filip Gruszczynski2217f612015-05-26 11:32:08 -07001719 mAttachInfo.mOutsets.set(mPendingOutsets);
Adrian Roosfa104232014-06-20 16:10:14 -07001720 mApplyInsetsRequested = false;
Adam Powell2accbf92014-04-16 23:14:57 +00001721 dispatchApplyInsets(host);
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 if (visibleInsetsChanged) {
1724 mAttachInfo.mVisibleInsets.set(mPendingVisibleInsets);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001725 if (DEBUG_LAYOUT) Log.v(mTag, "Visible insets changing to: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 + mAttachInfo.mVisibleInsets);
1727 }
1728
1729 if (!hadSurface) {
1730 if (mSurface.isValid()) {
1731 // If we are creating a new surface, then we need to
1732 // completely redraw it. Also, when we get to the
1733 // point of drawing it we will hold off and schedule
1734 // a new traversal instead. This is so we can tell the
1735 // window manager about all of the windows being displayed
1736 // before actually drawing them, so it can display then
1737 // all at once.
1738 newSurface = true;
Jeff Brown96e942d2011-11-30 19:55:01 -08001739 mFullRedrawNeeded = true;
Jack Palevich61a6e682009-10-09 17:37:50 -07001740 mPreviousTransparentRegion.setEmpty();
Romain Guy8506ab42009-06-11 17:35:47 -07001741
John Reck63005e62015-05-19 15:00:13 -07001742 // Only initialize up-front if transparent regions are not
1743 // requested, otherwise defer to see if the entire window
1744 // will be transparent
Romain Guyb051e892010-09-28 19:09:36 -07001745 if (mAttachInfo.mHardwareRenderer != null) {
Dianne Hackborn64825172011-03-02 21:32:58 -08001746 try {
Romain Guy786fc932012-07-24 16:24:56 -07001747 hwInitialized = mAttachInfo.mHardwareRenderer.initialize(
John Reck79d81e62013-11-05 13:26:57 -08001748 mSurface);
John Reck63005e62015-05-19 15:00:13 -07001749 if (hwInitialized && (host.mPrivateFlags
1750 & View.PFLAG_REQUEST_TRANSPARENT_REGIONS) == 0) {
1751 // Don't pre-allocate if transparent regions
1752 // are requested as they may not be needed
1753 mSurface.allocateBuffers();
1754 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07001755 } catch (OutOfResourcesException e) {
Romain Guy3696779b2013-01-28 14:04:07 -08001756 handleOutOfResourcesException(e);
Dianne Hackborn64825172011-03-02 21:32:58 -08001757 return;
1758 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 }
1760 }
1761 } else if (!mSurface.isValid()) {
1762 // If the surface has been removed, then reset the scroll
1763 // positions.
Svetoslav Ganov149567f2013-01-08 15:23:34 -08001764 if (mLastScrolledFocus != null) {
1765 mLastScrolledFocus.clear();
1766 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 mScrollY = mCurScrollY = 0;
Adrian Roos0e7ae4e2014-10-01 15:33:22 +02001768 if (mView instanceof RootViewSurfaceTaker) {
1769 ((RootViewSurfaceTaker) mView).onRootViewScrollYChanged(mCurScrollY);
1770 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 if (mScroller != null) {
1772 mScroller.abortAnimation();
1773 }
Romain Guy1d0c7082011-08-03 16:22:24 -07001774 // Our surface is gone
1775 if (mAttachInfo.mHardwareRenderer != null &&
1776 mAttachInfo.mHardwareRenderer.isEnabled()) {
John Reckf47a5942014-06-30 16:20:04 -07001777 mAttachInfo.mHardwareRenderer.destroy();
Romain Guy1d0c7082011-08-03 16:22:24 -07001778 }
Chong Zhangf4abc2b2015-11-12 23:40:58 -08001779 } else if ((surfaceGenerationId != mSurface.getGenerationId()
1780 || surfaceSizeChanged)
1781 && mSurfaceHolder == null
1782 && mAttachInfo.mHardwareRenderer != null) {
Jeff Brown96e942d2011-11-30 19:55:01 -08001783 mFullRedrawNeeded = true;
Dianne Hackborn64825172011-03-02 21:32:58 -08001784 try {
Chong Zhangf4abc2b2015-11-12 23:40:58 -08001785 // Need to do updateSurface (which leads to CanvasContext::setSurface and
1786 // re-create the EGLSurface) if either the Surface changed (as indicated by
1787 // generation id), or WindowManager changed the surface size. The latter is
1788 // because on some chips, changing the consumer side's BufferQueue size may
1789 // not take effect immediately unless we create a new EGLSurface.
1790 // Note that frame size change doesn't always imply surface size change (eg.
1791 // drag resizing uses fullscreen surface), need to check surfaceSizeChanged
1792 // flag from WindowManager.
John Reck79d81e62013-11-05 13:26:57 -08001793 mAttachInfo.mHardwareRenderer.updateSurface(mSurface);
Igor Murashkina86ab6402013-08-30 12:58:36 -07001794 } catch (OutOfResourcesException e) {
Romain Guy3696779b2013-01-28 14:04:07 -08001795 handleOutOfResourcesException(e);
Dianne Hackborn64825172011-03-02 21:32:58 -08001796 return;
1797 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 }
Chong Zhang0275e392015-09-17 10:41:44 -07001799
Jorim Jaggi4846ee32016-01-07 17:39:12 +01001800 final boolean freeformResizing = (relayoutResult
1801 & WindowManagerGlobal.RELAYOUT_RES_DRAG_RESIZING_FREEFORM) != 0;
1802 final boolean dockedResizing = (relayoutResult
1803 & WindowManagerGlobal.RELAYOUT_RES_DRAG_RESIZING_DOCKED) != 0;
1804 final boolean dragResizing = freeformResizing || dockedResizing;
Chong Zhang0275e392015-09-17 10:41:44 -07001805 if (mDragResizing != dragResizing) {
Skuhneb8160872015-09-22 09:51:39 -07001806 if (dragResizing) {
Jorim Jaggia7262a82015-11-03 15:15:40 +01001807 startDragResizing(mPendingBackDropFrame);
Jorim Jaggi4846ee32016-01-07 17:39:12 +01001808 mResizeMode = freeformResizing
1809 ? RESIZE_MODE_FREEFORM
1810 : RESIZE_MODE_DOCKED_DIVIDER;
Skuhneb8160872015-09-22 09:51:39 -07001811 } else {
1812 // We shouldn't come here, but if we come we should end the resize.
1813 endDragResizing();
1814 }
Chong Zhang0275e392015-09-17 10:41:44 -07001815 }
Skuhneb8160872015-09-22 09:51:39 -07001816 if (!USE_MT_RENDERER) {
1817 if (dragResizing) {
1818 mCanvasOffsetX = mWinFrame.left;
1819 mCanvasOffsetY = mWinFrame.top;
1820 } else {
1821 mCanvasOffsetX = mCanvasOffsetY = 0;
1822 }
Chong Zhang0275e392015-09-17 10:41:44 -07001823 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 } catch (RemoteException e) {
1825 }
Romain Guy1d0c7082011-08-03 16:22:24 -07001826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 if (DEBUG_ORIENTATION) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07001828 TAG, "Relayout returned: frame=" + frame + ", surface=" + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829
Chris Craikd36a81f2014-07-17 10:16:51 -07001830 mAttachInfo.mWindowLeft = frame.left;
1831 mAttachInfo.mWindowTop = frame.top;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832
1833 // !!FIXME!! This next section handles the case where we did not get the
1834 // window size we asked for. We should avoid this by getting a maximum size from
1835 // the window session beforehand.
Dianne Hackborn3556c9a2012-05-04 16:31:25 -07001836 if (mWidth != frame.width() || mHeight != frame.height()) {
Dianne Hackborn3556c9a2012-05-04 16:31:25 -07001837 mWidth = frame.width();
1838 mHeight = frame.height();
1839 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001841 if (mSurfaceHolder != null) {
1842 // The app owns the surface; tell it about what is going on.
1843 if (mSurface.isValid()) {
1844 // XXX .copyFrom() doesn't work!
1845 //mSurfaceHolder.mSurface.copyFrom(mSurface);
1846 mSurfaceHolder.mSurface = mSurface;
1847 }
Jeff Brown30bc34f2011-01-25 12:56:56 -08001848 mSurfaceHolder.setSurfaceFrameSize(mWidth, mHeight);
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001849 mSurfaceHolder.mSurfaceLock.unlock();
1850 if (mSurface.isValid()) {
1851 if (!hadSurface) {
1852 mSurfaceHolder.ungetCallbacks();
1853
1854 mIsCreating = true;
1855 mSurfaceHolderCallback.surfaceCreated(mSurfaceHolder);
1856 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1857 if (callbacks != null) {
1858 for (SurfaceHolder.Callback c : callbacks) {
1859 c.surfaceCreated(mSurfaceHolder);
1860 }
1861 }
1862 surfaceChanged = true;
1863 }
1864 if (surfaceChanged) {
1865 mSurfaceHolderCallback.surfaceChanged(mSurfaceHolder,
1866 lp.format, mWidth, mHeight);
1867 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1868 if (callbacks != null) {
1869 for (SurfaceHolder.Callback c : callbacks) {
1870 c.surfaceChanged(mSurfaceHolder, lp.format,
1871 mWidth, mHeight);
1872 }
1873 }
1874 }
1875 mIsCreating = false;
1876 } else if (hadSurface) {
1877 mSurfaceHolder.ungetCallbacks();
1878 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1879 mSurfaceHolderCallback.surfaceDestroyed(mSurfaceHolder);
1880 if (callbacks != null) {
1881 for (SurfaceHolder.Callback c : callbacks) {
1882 c.surfaceDestroyed(mSurfaceHolder);
1883 }
1884 }
1885 mSurfaceHolder.mSurfaceLock.lock();
Jozef BABJAK93c5b6a2011-02-22 09:33:19 +01001886 try {
1887 mSurfaceHolder.mSurface = new Surface();
1888 } finally {
1889 mSurfaceHolder.mSurfaceLock.unlock();
1890 }
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001891 }
1892 }
Romain Guy53389bd2010-09-07 17:16:32 -07001893
John Reck51aaf902015-12-02 15:08:07 -08001894 final ThreadedRenderer hardwareRenderer = mAttachInfo.mHardwareRenderer;
Alan Viverette50210d92015-05-14 18:05:36 -07001895 if (hardwareRenderer != null && hardwareRenderer.isEnabled()) {
1896 if (hwInitialized
1897 || mWidth != hardwareRenderer.getWidth()
1898 || mHeight != hardwareRenderer.getHeight()) {
1899 hardwareRenderer.setup(mWidth, mHeight, mAttachInfo,
1900 mWindowAttributes.surfaceInsets);
Romain Guy03985752011-07-11 15:33:51 -07001901 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 }
1903
Craig Mautner72d6f212015-02-19 16:33:09 -08001904 if (!mStopped || mReportNextDraw) {
Dianne Hackbornce418e62011-03-01 14:31:38 -08001905 boolean focusChangedDueToTouchMode = ensureTouchModeLocally(
Jeff Brown98365d72012-08-19 20:30:52 -07001906 (relayoutResult&WindowManagerGlobal.RELAYOUT_RES_IN_TOUCH_MODE) != 0);
Dianne Hackbornce418e62011-03-01 14:31:38 -08001907 if (focusChangedDueToTouchMode || mWidth != host.getMeasuredWidth()
1908 || mHeight != host.getMeasuredHeight() || contentInsetsChanged) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001909 int childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
1910 int childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
Igor Murashkina86ab6402013-08-30 12:58:36 -07001911
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001912 if (DEBUG_LAYOUT) Log.v(mTag, "Ooops, something changed! mWidth="
Dianne Hackbornce418e62011-03-01 14:31:38 -08001913 + mWidth + " measuredWidth=" + host.getMeasuredWidth()
1914 + " mHeight=" + mHeight
1915 + " measuredHeight=" + host.getMeasuredHeight()
1916 + " coveredInsetsChanged=" + contentInsetsChanged);
Igor Murashkina86ab6402013-08-30 12:58:36 -07001917
Dianne Hackbornce418e62011-03-01 14:31:38 -08001918 // Ask host how big it wants to be
Jeff Brownc8d2668b2012-05-16 17:23:59 -07001919 performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
Igor Murashkina86ab6402013-08-30 12:58:36 -07001920
Dianne Hackbornce418e62011-03-01 14:31:38 -08001921 // Implementation of weights from WindowManager.LayoutParams
1922 // We just grow the dimensions as needed and re-measure if
1923 // needs be
1924 int width = host.getMeasuredWidth();
1925 int height = host.getMeasuredHeight();
1926 boolean measureAgain = false;
Igor Murashkina86ab6402013-08-30 12:58:36 -07001927
Dianne Hackbornce418e62011-03-01 14:31:38 -08001928 if (lp.horizontalWeight > 0.0f) {
1929 width += (int) ((mWidth - width) * lp.horizontalWeight);
1930 childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width,
1931 MeasureSpec.EXACTLY);
1932 measureAgain = true;
1933 }
1934 if (lp.verticalWeight > 0.0f) {
1935 height += (int) ((mHeight - height) * lp.verticalWeight);
1936 childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
1937 MeasureSpec.EXACTLY);
1938 measureAgain = true;
1939 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07001940
Dianne Hackbornce418e62011-03-01 14:31:38 -08001941 if (measureAgain) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08001942 if (DEBUG_LAYOUT) Log.v(mTag,
Dianne Hackbornce418e62011-03-01 14:31:38 -08001943 "And hey let's measure once more: width=" + width
1944 + " height=" + height);
Jeff Brownc8d2668b2012-05-16 17:23:59 -07001945 performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
Dianne Hackbornce418e62011-03-01 14:31:38 -08001946 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07001947
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001948 layoutRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 }
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001951 } else {
1952 // Not the first pass and no window/insets/visibility change but the window
1953 // may have moved and we need check that and if so to update the left and right
1954 // in the attach info. We translate only the window frame since on window move
1955 // the window manager tells us only for the new frame but the insets are the
1956 // same and we do not want to translate them more than once.
Jorim Jaggi2e95a482016-01-14 17:36:55 -08001957 maybeHandleWindowMove(frame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 }
1959
Craig Mautner72d6f212015-02-19 16:33:09 -08001960 final boolean didLayout = layoutRequested && (!mStopped || mReportNextDraw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 boolean triggerGlobalLayoutListener = didLayout
Chris Craikd36a81f2014-07-17 10:16:51 -07001962 || mAttachInfo.mRecomputeGlobalAttributes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 if (didLayout) {
Chet Haase3efa7b52012-12-03 08:33:17 -08001964 performLayout(lp, desiredWindowWidth, desiredWindowHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965
Mathias Agopian54e3d3842013-04-12 15:13:12 -07001966 // By this point all views have been sized and positioned
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 // We can compute the transparent area
1968
Dianne Hackborn4702a852012-08-17 15:18:29 -07001969 if ((host.mPrivateFlags & View.PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 // start out transparent
1971 // TODO: AVOID THAT CALL BY CACHING THE RESULT?
1972 host.getLocationInWindow(mTmpLocation);
1973 mTransparentRegion.set(mTmpLocation[0], mTmpLocation[1],
1974 mTmpLocation[0] + host.mRight - host.mLeft,
1975 mTmpLocation[1] + host.mBottom - host.mTop);
1976
1977 host.gatherTransparentRegion(mTransparentRegion);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001978 if (mTranslator != null) {
1979 mTranslator.translateRegionInWindowToScreen(mTransparentRegion);
1980 }
1981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 if (!mTransparentRegion.equals(mPreviousTransparentRegion)) {
1983 mPreviousTransparentRegion.set(mTransparentRegion);
Mathias Agopian54e3d3842013-04-12 15:13:12 -07001984 mFullRedrawNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 // reconfigure window manager
1986 try {
Jeff Brown98365d72012-08-19 20:30:52 -07001987 mWindowSession.setTransparentRegion(mWindow, mTransparentRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 } catch (RemoteException e) {
1989 }
1990 }
1991 }
1992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 if (DBG) {
1994 System.out.println("======================================");
Chet Haase4610eef2015-12-03 07:38:11 -08001995 System.out.println("performTraversals -- after setFrame");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 host.debug();
1997 }
1998 }
1999
2000 if (triggerGlobalLayoutListener) {
Chris Craikd36a81f2014-07-17 10:16:51 -07002001 mAttachInfo.mRecomputeGlobalAttributes = false;
2002 mAttachInfo.mTreeObserver.dispatchOnGlobalLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 }
2004
2005 if (computesInternalInsets) {
Jeff Brownfbf09772011-01-16 14:06:57 -08002006 // Clear the original insets.
Chris Craikd36a81f2014-07-17 10:16:51 -07002007 final ViewTreeObserver.InternalInsetsInfo insets = mAttachInfo.mGivenInternalInsets;
Jeff Brownfbf09772011-01-16 14:06:57 -08002008 insets.reset();
2009
2010 // Compute new insets in place.
Chris Craikd36a81f2014-07-17 10:16:51 -07002011 mAttachInfo.mTreeObserver.dispatchOnComputeInternalInsets(insets);
2012 mAttachInfo.mHasNonEmptyGivenInternalInsets = !insets.isEmpty();
Jeff Brownfbf09772011-01-16 14:06:57 -08002013
2014 // Tell the window manager.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 if (insetsPending || !mLastGivenInsets.equals(insets)) {
2016 mLastGivenInsets.set(insets);
Jeff Brownfbf09772011-01-16 14:06:57 -08002017
2018 // Translate insets to screen coordinates if needed.
2019 final Rect contentInsets;
2020 final Rect visibleInsets;
2021 final Region touchableRegion;
2022 if (mTranslator != null) {
2023 contentInsets = mTranslator.getTranslatedContentInsets(insets.contentInsets);
2024 visibleInsets = mTranslator.getTranslatedVisibleInsets(insets.visibleInsets);
2025 touchableRegion = mTranslator.getTranslatedTouchableArea(insets.touchableRegion);
2026 } else {
2027 contentInsets = insets.contentInsets;
2028 visibleInsets = insets.visibleInsets;
2029 touchableRegion = insets.touchableRegion;
2030 }
2031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 try {
Jeff Brown98365d72012-08-19 20:30:52 -07002033 mWindowSession.setInsets(mWindow, insets.mTouchableInsets,
Jeff Brownfbf09772011-01-16 14:06:57 -08002034 contentInsets, visibleInsets, touchableRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 } catch (RemoteException e) {
2036 }
2037 }
2038 }
Romain Guy8506ab42009-06-11 17:35:47 -07002039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 if (mFirst) {
2041 // handle first focus request
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002042 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "First: mView.hasFocus()="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 + mView.hasFocus());
2044 if (mView != null) {
2045 if (!mView.hasFocus()) {
2046 mView.requestFocus(View.FOCUS_FORWARD);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002047 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "First: requested focused view="
Svetoslav Ganov149567f2013-01-08 15:23:34 -08002048 + mView.findFocus());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 } else {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002050 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "First: existing focused view="
Svetoslav Ganov149567f2013-01-08 15:23:34 -08002051 + mView.findFocus());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 }
2053 }
2054 }
2055
Alan Viverette64bf97a2015-09-18 16:42:00 -04002056 final boolean changedVisibility = (viewVisibilityChanged || mFirst) && isViewVisible;
2057 final boolean hasWindowFocus = mAttachInfo.mHasWindowFocus && isViewVisible;
2058 final boolean regainedFocus = hasWindowFocus && mLostWindowFocus;
2059 if (regainedFocus) {
2060 mLostWindowFocus = false;
2061 } else if (!hasWindowFocus && mHadWindowFocus) {
2062 mLostWindowFocus = true;
2063 }
2064
2065 if (changedVisibility || regainedFocus) {
2066 host.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2067 }
2068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 mFirst = false;
2070 mWillDrawSoon = false;
2071 mNewSurfaceNeeded = false;
Jorim Jaggi4846ee32016-01-07 17:39:12 +01002072 mActivityRelaunched = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 mViewVisibility = viewVisibility;
Alan Viverette64bf97a2015-09-18 16:42:00 -04002074 mHadWindowFocus = hasWindowFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075
Alan Viverette64bf97a2015-09-18 16:42:00 -04002076 if (hasWindowFocus && !isInLocalFocusMode()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 final boolean imTarget = WindowManager.LayoutParams
2078 .mayUseInputMethod(mWindowAttributes.flags);
2079 if (imTarget != mLastWasImTarget) {
2080 mLastWasImTarget = imTarget;
2081 InputMethodManager imm = InputMethodManager.peekInstance();
2082 if (imm != null && imTarget) {
Alan Viverette64bf97a2015-09-18 16:42:00 -04002083 imm.onPreWindowFocus(mView, hasWindowFocus);
Yohei Yukawa5f059652015-05-14 22:16:41 -07002084 imm.onPostWindowFocus(mView, mView.findFocus(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 mWindowAttributes.softInputMode,
2086 !mHasHadWindowFocus, mWindowAttributes.flags);
2087 }
2088 }
2089 }
Romain Guy8506ab42009-06-11 17:35:47 -07002090
Jeff Brown96e942d2011-11-30 19:55:01 -08002091 // Remember if we must report the next draw.
Jeff Brown98365d72012-08-19 20:30:52 -07002092 if ((relayoutResult & WindowManagerGlobal.RELAYOUT_RES_FIRST_TIME) != 0) {
Jeff Brown96e942d2011-11-30 19:55:01 -08002093 mReportNextDraw = true;
2094 }
2095
Alan Viverette64bf97a2015-09-18 16:42:00 -04002096 boolean cancelDraw = mAttachInfo.mTreeObserver.dispatchOnPreDraw() || !isViewVisible;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097
Chet Haase61158c62011-09-06 22:19:45 -07002098 if (!cancelDraw && !newSurface) {
Chet Haase9c450412015-10-01 13:25:58 -07002099 if (mPendingTransitions != null && mPendingTransitions.size() > 0) {
2100 for (int i = 0; i < mPendingTransitions.size(); ++i) {
2101 mPendingTransitions.get(i).startChangingAnimations();
Chet Haased56c6952011-09-07 08:46:23 -07002102 }
Chet Haase9c450412015-10-01 13:25:58 -07002103 mPendingTransitions.clear();
Chet Haased56c6952011-09-07 08:46:23 -07002104 }
Chet Haase9c450412015-10-01 13:25:58 -07002105
2106 performDraw();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 } else {
Alan Viverette64bf97a2015-09-18 16:42:00 -04002108 if (isViewVisible) {
Chris Wren78cb7cf2012-05-15 12:36:44 -04002109 // Try again
2110 scheduleTraversals();
2111 } else if (mPendingTransitions != null && mPendingTransitions.size() > 0) {
Chet Haased56c6952011-09-07 08:46:23 -07002112 for (int i = 0; i < mPendingTransitions.size(); ++i) {
2113 mPendingTransitions.get(i).endChangingAnimations();
2114 }
2115 mPendingTransitions.clear();
2116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 }
Craig Mautnerdf2390a2012-08-29 20:59:22 -07002118
2119 mIsInTraversal = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 }
2121
Jorim Jaggi2e95a482016-01-14 17:36:55 -08002122 private void maybeHandleWindowMove(Rect frame) {
2123
2124 // TODO: Well, we are checking whether the frame has changed similarly
2125 // to how this is done for the insets. This is however incorrect since
2126 // the insets and the frame are translated. For example, the old frame
2127 // was (1, 1 - 1, 1) and was translated to say (2, 2 - 2, 2), now the new
2128 // reported frame is (2, 2 - 2, 2) which implies no change but this is not
2129 // true since we are comparing a not translated value to a translated one.
2130 // This scenario is rare but we may want to fix that.
2131
2132 final boolean windowMoved = mAttachInfo.mWindowLeft != frame.left
2133 || mAttachInfo.mWindowTop != frame.top;
2134 if (windowMoved) {
2135 if (mTranslator != null) {
2136 mTranslator.translateRectInScreenToAppWinFrame(frame);
2137 }
2138 mAttachInfo.mWindowLeft = frame.left;
2139 mAttachInfo.mWindowTop = frame.top;
2140
2141 // Update the light position for the new window offsets.
2142 if (mAttachInfo.mHardwareRenderer != null) {
2143 mAttachInfo.mHardwareRenderer.setLightCenter(mAttachInfo);
2144 }
2145 }
2146 }
Romain Guy3696779b2013-01-28 14:04:07 -08002147 private void handleOutOfResourcesException(Surface.OutOfResourcesException e) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002148 Log.e(mTag, "OutOfResourcesException initializing HW surface", e);
Romain Guy3696779b2013-01-28 14:04:07 -08002149 try {
2150 if (!mWindowSession.outOfMemory(mWindow) &&
2151 Process.myUid() != Process.SYSTEM_UID) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002152 Slog.w(mTag, "No processes killed for memory; killing self");
Romain Guy3696779b2013-01-28 14:04:07 -08002153 Process.killProcess(Process.myPid());
2154 }
2155 } catch (RemoteException ex) {
2156 }
2157 mLayoutRequested = true; // ask wm for a new surface next time.
2158 }
2159
Jeff Brownc8d2668b2012-05-16 17:23:59 -07002160 private void performMeasure(int childWidthMeasureSpec, int childHeightMeasureSpec) {
2161 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "measure");
2162 try {
2163 mView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
2164 } finally {
2165 Trace.traceEnd(Trace.TRACE_TAG_VIEW);
2166 }
2167 }
2168
Chet Haase97140572012-09-13 14:56:47 -07002169 /**
2170 * Called by {@link android.view.View#isInLayout()} to determine whether the view hierarchy
2171 * is currently undergoing a layout pass.
2172 *
2173 * @return whether the view hierarchy is currently undergoing a layout pass
2174 */
2175 boolean isInLayout() {
2176 return mInLayout;
2177 }
2178
2179 /**
Chet Haasecc699b42012-12-13 09:06:55 -08002180 * Called by {@link android.view.View#requestLayout()} if the view hierarchy is currently
2181 * undergoing a layout pass. requestLayout() should not generally be called during layout,
2182 * unless the container hierarchy knows what it is doing (i.e., it is fine as long as
2183 * all children in that container hierarchy are measured and laid out at the end of the layout
2184 * pass for that container). If requestLayout() is called anyway, we handle it correctly
2185 * by registering all requesters during a frame as it proceeds. At the end of the frame,
2186 * we check all of those views to see if any still have pending layout requests, which
2187 * indicates that they were not correctly handled by their container hierarchy. If that is
2188 * the case, we clear all such flags in the tree, to remove the buggy flag state that leads
2189 * to blank containers, and force a second request/measure/layout pass in this frame. If
Chet Haase97140572012-09-13 14:56:47 -07002190 * more requestLayout() calls are received during that second layout pass, we post those
Chet Haasecc699b42012-12-13 09:06:55 -08002191 * requests to the next frame to avoid possible infinite loops.
2192 *
2193 * <p>The return value from this method indicates whether the request should proceed
2194 * (if it is a request during the first layout pass) or should be skipped and posted to the
2195 * next frame (if it is a request during the second layout pass).</p>
Chet Haase97140572012-09-13 14:56:47 -07002196 *
2197 * @param view the view that requested the layout.
Chet Haasecc699b42012-12-13 09:06:55 -08002198 *
2199 * @return true if request should proceed, false otherwise.
Chet Haase97140572012-09-13 14:56:47 -07002200 */
Chet Haasecc699b42012-12-13 09:06:55 -08002201 boolean requestLayoutDuringLayout(final View view) {
2202 if (view.mParent == null || view.mAttachInfo == null) {
2203 // Would not normally trigger another layout, so just let it pass through as usual
2204 return true;
2205 }
Chet Haase107a4822013-03-13 06:46:50 -07002206 if (!mLayoutRequesters.contains(view)) {
2207 mLayoutRequesters.add(view);
2208 }
Chet Haase97140572012-09-13 14:56:47 -07002209 if (!mHandlingLayoutInLayoutRequest) {
Chet Haase107a4822013-03-13 06:46:50 -07002210 // Let the request proceed normally; it will be processed in a second layout pass
2211 // if necessary
Chet Haasecc699b42012-12-13 09:06:55 -08002212 return true;
Chet Haase97140572012-09-13 14:56:47 -07002213 } else {
Chet Haase107a4822013-03-13 06:46:50 -07002214 // Don't let the request proceed during the second layout pass.
2215 // It will post to the next frame instead.
Chet Haasecc699b42012-12-13 09:06:55 -08002216 return false;
Chet Haase97140572012-09-13 14:56:47 -07002217 }
2218 }
2219
Chet Haase3efa7b52012-12-03 08:33:17 -08002220 private void performLayout(WindowManager.LayoutParams lp, int desiredWindowWidth,
2221 int desiredWindowHeight) {
Jeff Brownc8d2668b2012-05-16 17:23:59 -07002222 mLayoutRequested = false;
2223 mScrollMayChange = true;
Chet Haase97140572012-09-13 14:56:47 -07002224 mInLayout = true;
Jeff Brownc8d2668b2012-05-16 17:23:59 -07002225
2226 final View host = mView;
2227 if (DEBUG_ORIENTATION || DEBUG_LAYOUT) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002228 Log.v(mTag, "Laying out " + host + " to (" +
Jeff Brownc8d2668b2012-05-16 17:23:59 -07002229 host.getMeasuredWidth() + ", " + host.getMeasuredHeight() + ")");
2230 }
2231
Jeff Brownc8d2668b2012-05-16 17:23:59 -07002232 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "layout");
2233 try {
2234 host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());
Chet Haasecc699b42012-12-13 09:06:55 -08002235
Chet Haased5a83522012-11-21 16:24:44 -08002236 mInLayout = false;
Chet Haase97140572012-09-13 14:56:47 -07002237 int numViewsRequestingLayout = mLayoutRequesters.size();
2238 if (numViewsRequestingLayout > 0) {
Chet Haasecc699b42012-12-13 09:06:55 -08002239 // requestLayout() was called during layout.
2240 // If no layout-request flags are set on the requesting views, there is no problem.
2241 // If some requests are still pending, then we need to clear those flags and do
2242 // a full request/measure/layout pass to handle this situation.
Chet Haase107a4822013-03-13 06:46:50 -07002243 ArrayList<View> validLayoutRequesters = getValidLayoutRequesters(mLayoutRequesters,
2244 false);
2245 if (validLayoutRequesters != null) {
2246 // Set this flag to indicate that any further requests are happening during
2247 // the second pass, which may result in posting those requests to the next
2248 // frame instead
Chet Haasecc699b42012-12-13 09:06:55 -08002249 mHandlingLayoutInLayoutRequest = true;
Chet Haase107a4822013-03-13 06:46:50 -07002250
2251 // Process fresh layout requests, then measure and layout
2252 int numValidRequests = validLayoutRequesters.size();
Chet Haasecc699b42012-12-13 09:06:55 -08002253 for (int i = 0; i < numValidRequests; ++i) {
Chet Haase107a4822013-03-13 06:46:50 -07002254 final View view = validLayoutRequesters.get(i);
2255 Log.w("View", "requestLayout() improperly called by " + view +
2256 " during layout: running second layout pass");
2257 view.requestLayout();
Chet Haasecc699b42012-12-13 09:06:55 -08002258 }
2259 measureHierarchy(host, lp, mView.getContext().getResources(),
2260 desiredWindowWidth, desiredWindowHeight);
2261 mInLayout = true;
2262 host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());
Chet Haase107a4822013-03-13 06:46:50 -07002263
Chet Haasecc699b42012-12-13 09:06:55 -08002264 mHandlingLayoutInLayoutRequest = false;
Chet Haase107a4822013-03-13 06:46:50 -07002265
2266 // Check the valid requests again, this time without checking/clearing the
2267 // layout flags, since requests happening during the second pass get noop'd
2268 validLayoutRequesters = getValidLayoutRequesters(mLayoutRequesters, true);
2269 if (validLayoutRequesters != null) {
2270 final ArrayList<View> finalRequesters = validLayoutRequesters;
2271 // Post second-pass requests to the next frame
2272 getRunQueue().post(new Runnable() {
2273 @Override
2274 public void run() {
2275 int numValidRequests = finalRequesters.size();
2276 for (int i = 0; i < numValidRequests; ++i) {
2277 final View view = finalRequesters.get(i);
2278 Log.w("View", "requestLayout() improperly called by " + view +
2279 " during second layout pass: posting in next frame");
2280 view.requestLayout();
2281 }
2282 }
2283 });
2284 }
Chet Haasecc699b42012-12-13 09:06:55 -08002285 }
Chet Haase107a4822013-03-13 06:46:50 -07002286
Chet Haase97140572012-09-13 14:56:47 -07002287 }
Jeff Brownc8d2668b2012-05-16 17:23:59 -07002288 } finally {
2289 Trace.traceEnd(Trace.TRACE_TAG_VIEW);
2290 }
Chet Haase97140572012-09-13 14:56:47 -07002291 mInLayout = false;
Jeff Brownc8d2668b2012-05-16 17:23:59 -07002292 }
2293
Chet Haase107a4822013-03-13 06:46:50 -07002294 /**
2295 * This method is called during layout when there have been calls to requestLayout() during
2296 * layout. It walks through the list of views that requested layout to determine which ones
2297 * still need it, based on visibility in the hierarchy and whether they have already been
2298 * handled (as is usually the case with ListView children).
2299 *
2300 * @param layoutRequesters The list of views that requested layout during layout
2301 * @param secondLayoutRequests Whether the requests were issued during the second layout pass.
2302 * If so, the FORCE_LAYOUT flag was not set on requesters.
2303 * @return A list of the actual views that still need to be laid out.
2304 */
2305 private ArrayList<View> getValidLayoutRequesters(ArrayList<View> layoutRequesters,
2306 boolean secondLayoutRequests) {
2307
2308 int numViewsRequestingLayout = layoutRequesters.size();
2309 ArrayList<View> validLayoutRequesters = null;
2310 for (int i = 0; i < numViewsRequestingLayout; ++i) {
2311 View view = layoutRequesters.get(i);
2312 if (view != null && view.mAttachInfo != null && view.mParent != null &&
2313 (secondLayoutRequests || (view.mPrivateFlags & View.PFLAG_FORCE_LAYOUT) ==
2314 View.PFLAG_FORCE_LAYOUT)) {
2315 boolean gone = false;
2316 View parent = view;
2317 // Only trigger new requests for views in a non-GONE hierarchy
2318 while (parent != null) {
2319 if ((parent.mViewFlags & View.VISIBILITY_MASK) == View.GONE) {
2320 gone = true;
2321 break;
2322 }
2323 if (parent.mParent instanceof View) {
2324 parent = (View) parent.mParent;
2325 } else {
2326 parent = null;
2327 }
2328 }
2329 if (!gone) {
2330 if (validLayoutRequesters == null) {
2331 validLayoutRequesters = new ArrayList<View>();
2332 }
2333 validLayoutRequesters.add(view);
2334 }
2335 }
2336 }
2337 if (!secondLayoutRequests) {
2338 // If we're checking the layout flags, then we need to clean them up also
2339 for (int i = 0; i < numViewsRequestingLayout; ++i) {
2340 View view = layoutRequesters.get(i);
2341 while (view != null &&
2342 (view.mPrivateFlags & View.PFLAG_FORCE_LAYOUT) != 0) {
2343 view.mPrivateFlags &= ~View.PFLAG_FORCE_LAYOUT;
2344 if (view.mParent instanceof View) {
2345 view = (View) view.mParent;
2346 } else {
2347 view = null;
2348 }
2349 }
2350 }
2351 }
2352 layoutRequesters.clear();
2353 return validLayoutRequesters;
2354 }
2355
Igor Murashkina86ab6402013-08-30 12:58:36 -07002356 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 public void requestTransparentRegion(View child) {
2358 // the test below should not fail unless someone is messing with us
2359 checkThread();
2360 if (mView == child) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07002361 mView.mPrivateFlags |= View.PFLAG_REQUEST_TRANSPARENT_REGIONS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 // Need to make sure we re-evaluate the window attributes next
2363 // time around, to ensure the window has the correct format.
2364 mWindowAttributesChanged = true;
Romain Guyf21c9b02011-09-06 16:56:54 -07002365 mWindowAttributesChangesFlag = 0;
Mathias Agopian1bd80ad2010-11-04 17:13:39 -07002366 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 }
2368 }
2369
2370 /**
2371 * Figures out the measure spec for the root view in a window based on it's
2372 * layout params.
2373 *
2374 * @param windowSize
2375 * The available width or height of the window
2376 *
2377 * @param rootDimension
2378 * The layout params for one dimension (width or height) of the
2379 * window.
2380 *
2381 * @return The measure spec to use to measure the root view.
2382 */
Romain Guya998dff2012-03-23 18:58:36 -07002383 private static int getRootMeasureSpec(int windowSize, int rootDimension) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 int measureSpec;
2385 switch (rootDimension) {
2386
Romain Guy980a9382010-01-08 15:06:28 -08002387 case ViewGroup.LayoutParams.MATCH_PARENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 // Window can't resize. Force root view to be windowSize.
2389 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY);
2390 break;
2391 case ViewGroup.LayoutParams.WRAP_CONTENT:
2392 // Window can resize. Set max size for root view.
2393 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST);
2394 break;
2395 default:
2396 // Window wants to be an exact size. Force root view to be that size.
2397 measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);
2398 break;
2399 }
2400 return measureSpec;
2401 }
2402
Alan Viveretteccb11e12014-07-08 16:04:02 -07002403 int mHardwareXOffset;
Dianne Hackborn0f761d62010-11-30 22:06:10 -08002404 int mHardwareYOffset;
Dianne Hackborn0f761d62010-11-30 22:06:10 -08002405
Igor Murashkina86ab6402013-08-30 12:58:36 -07002406 @Override
Chris Craikf6829a02015-03-10 10:28:59 -07002407 public void onHardwarePreDraw(DisplayListCanvas canvas) {
Alan Viveretteccb11e12014-07-08 16:04:02 -07002408 canvas.translate(-mHardwareXOffset, -mHardwareYOffset);
Dianne Hackborn0f761d62010-11-30 22:06:10 -08002409 }
2410
Igor Murashkina86ab6402013-08-30 12:58:36 -07002411 @Override
Chris Craikf6829a02015-03-10 10:28:59 -07002412 public void onHardwarePostDraw(DisplayListCanvas canvas) {
Alan Viverette632af842014-10-28 13:45:11 -07002413 drawAccessibilityFocusedDrawableIfNeeded(canvas);
Dianne Hackborn0f761d62010-11-30 22:06:10 -08002414 }
2415
Chet Haaseed30fd82011-04-22 16:18:45 -07002416 /**
2417 * @hide
2418 */
2419 void outputDisplayList(View view) {
Chris Craik356b5fe2015-07-07 10:39:36 -07002420 view.mRenderNode.output();
John Recke248bd12015-08-05 13:53:53 -07002421 if (mAttachInfo.mHardwareRenderer != null) {
2422 ((ThreadedRenderer)mAttachInfo.mHardwareRenderer).serializeDisplayListTree();
2423 }
Romain Guy59a12ca2011-06-09 17:48:21 -07002424 }
2425
2426 /**
2427 * @see #PROPERTY_PROFILE_RENDERING
2428 */
2429 private void profileRendering(boolean enabled) {
2430 if (mProfileRendering) {
2431 mRenderProfilingEnabled = enabled;
Chris Craikae4f32042013-02-07 12:57:10 -08002432
2433 if (mRenderProfiler != null) {
2434 mChoreographer.removeFrameCallback(mRenderProfiler);
2435 }
2436 if (mRenderProfilingEnabled) {
2437 if (mRenderProfiler == null) {
2438 mRenderProfiler = new Choreographer.FrameCallback() {
2439 @Override
2440 public void doFrame(long frameTimeNanos) {
2441 mDirty.set(0, 0, mWidth, mHeight);
2442 scheduleTraversals();
2443 if (mRenderProfilingEnabled) {
2444 mChoreographer.postFrameCallback(mRenderProfiler);
2445 }
Romain Guy59a12ca2011-06-09 17:48:21 -07002446 }
Chris Craikae4f32042013-02-07 12:57:10 -08002447 };
2448 }
Romain Guy5bb3c732012-11-29 17:52:58 -08002449 mChoreographer.postFrameCallback(mRenderProfiler);
Romain Guy59a12ca2011-06-09 17:48:21 -07002450 } else {
Romain Guy59a12ca2011-06-09 17:48:21 -07002451 mRenderProfiler = null;
Chet Haaseed30fd82011-04-22 16:18:45 -07002452 }
2453 }
2454 }
2455
Chet Haase2f2022a2011-10-11 06:41:59 -07002456 /**
2457 * Called from draw() when DEBUG_FPS is enabled
2458 */
2459 private void trackFPS() {
2460 // Tracks frames per second drawn. First value in a series of draws may be bogus
2461 // because it down not account for the intervening idle time
2462 long nowTime = System.currentTimeMillis();
2463 if (mFpsStartTime < 0) {
2464 mFpsStartTime = mFpsPrevTime = nowTime;
2465 mFpsNumFrames = 0;
2466 } else {
2467 ++mFpsNumFrames;
2468 String thisHash = Integer.toHexString(System.identityHashCode(this));
2469 long frameTime = nowTime - mFpsPrevTime;
2470 long totalTime = nowTime - mFpsStartTime;
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002471 Log.v(mTag, "0x" + thisHash + "\tFrame time:\t" + frameTime);
Chet Haase2f2022a2011-10-11 06:41:59 -07002472 mFpsPrevTime = nowTime;
2473 if (totalTime > 1000) {
2474 float fps = (float) mFpsNumFrames * 1000 / totalTime;
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002475 Log.v(mTag, "0x" + thisHash + "\tFPS:\t" + fps);
Chet Haase2f2022a2011-10-11 06:41:59 -07002476 mFpsStartTime = nowTime;
2477 mFpsNumFrames = 0;
2478 }
2479 }
2480 }
2481
Jeff Brown96e942d2011-11-30 19:55:01 -08002482 private void performDraw() {
Jeff Brownd912e1f2014-04-11 18:46:22 -07002483 if (mAttachInfo.mDisplayState == Display.STATE_OFF && !mReportNextDraw) {
Craig Mautner006f0e42012-03-21 11:00:32 -07002484 return;
2485 }
Romain Guy7e4e5612012-03-05 14:37:29 -08002486
Jeff Brown96e942d2011-11-30 19:55:01 -08002487 final boolean fullRedrawNeeded = mFullRedrawNeeded;
2488 mFullRedrawNeeded = false;
Jeff Brown481c1572012-03-09 14:41:15 -08002489
Romain Guy1f59e5c2012-05-06 14:11:16 -07002490 mIsDrawing = true;
Jeff Brown481c1572012-03-09 14:41:15 -08002491 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "draw");
2492 try {
2493 draw(fullRedrawNeeded);
2494 } finally {
Romain Guy1f59e5c2012-05-06 14:11:16 -07002495 mIsDrawing = false;
Jeff Brown481c1572012-03-09 14:41:15 -08002496 Trace.traceEnd(Trace.TRACE_TAG_VIEW);
2497 }
Jeff Brown96e942d2011-11-30 19:55:01 -08002498
John Reck119907c2014-08-14 09:02:01 -07002499 // For whatever reason we didn't create a HardwareRenderer, end any
2500 // hardware animations that are now dangling
2501 if (mAttachInfo.mPendingAnimatingRenderNodes != null) {
2502 final int count = mAttachInfo.mPendingAnimatingRenderNodes.size();
2503 for (int i = 0; i < count; i++) {
2504 mAttachInfo.mPendingAnimatingRenderNodes.get(i).endAllAnimators();
2505 }
2506 mAttachInfo.mPendingAnimatingRenderNodes.clear();
2507 }
2508
Jeff Brown96e942d2011-11-30 19:55:01 -08002509 if (mReportNextDraw) {
2510 mReportNextDraw = false;
Chong Zhang8dbd9ad2015-10-09 10:06:11 -07002511
2512 // if we're using multi-thread renderer, wait for the window frame draws
2513 if (mWindowDrawCountDown != null) {
2514 try {
2515 mWindowDrawCountDown.await();
2516 } catch (InterruptedException e) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002517 Log.e(mTag, "Window redraw count down interruped!");
Chong Zhang8dbd9ad2015-10-09 10:06:11 -07002518 }
2519 mWindowDrawCountDown = null;
2520 }
2521
John Reck28ad7b52014-04-07 16:59:25 -07002522 if (mAttachInfo.mHardwareRenderer != null) {
2523 mAttachInfo.mHardwareRenderer.fence();
2524 }
Jeff Brown96e942d2011-11-30 19:55:01 -08002525
2526 if (LOCAL_LOGV) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002527 Log.v(mTag, "FINISHED DRAWING: " + mWindowAttributes.getTitle());
Jeff Brown96e942d2011-11-30 19:55:01 -08002528 }
2529 if (mSurfaceHolder != null && mSurface.isValid()) {
2530 mSurfaceHolderCallback.surfaceRedrawNeeded(mSurfaceHolder);
2531 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
2532 if (callbacks != null) {
2533 for (SurfaceHolder.Callback c : callbacks) {
2534 if (c instanceof SurfaceHolder.Callback2) {
Filip Gruszczynski1a4dfe52015-11-15 10:58:57 -08002535 ((SurfaceHolder.Callback2)c).surfaceRedrawNeeded(mSurfaceHolder);
Jeff Brown96e942d2011-11-30 19:55:01 -08002536 }
2537 }
2538 }
2539 }
2540 try {
Jeff Brown98365d72012-08-19 20:30:52 -07002541 mWindowSession.finishDrawing(mWindow);
Jeff Brown96e942d2011-11-30 19:55:01 -08002542 } catch (RemoteException e) {
2543 }
2544 }
2545 }
2546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 private void draw(boolean fullRedrawNeeded) {
2548 Surface surface = mSurface;
Romain Guyfbb93fa2012-12-03 18:50:35 -08002549 if (!surface.isValid()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 return;
2551 }
2552
Chet Haase2f2022a2011-10-11 06:41:59 -07002553 if (DEBUG_FPS) {
2554 trackFPS();
2555 }
2556
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002557 if (!sFirstDrawComplete) {
2558 synchronized (sFirstDrawHandlers) {
2559 sFirstDrawComplete = true;
Romain Guy812ccbe2010-06-01 14:07:24 -07002560 final int count = sFirstDrawHandlers.size();
2561 for (int i = 0; i< count; i++) {
Jeff Browna175a5b2012-02-15 19:18:31 -08002562 mHandler.post(sFirstDrawHandlers.get(i));
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002563 }
2564 }
2565 }
Romain Guy59a12ca2011-06-09 17:48:21 -07002566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 scrollToRectOrFocus(null, false);
2568
Chris Craikd36a81f2014-07-17 10:16:51 -07002569 if (mAttachInfo.mViewScrollChanged) {
2570 mAttachInfo.mViewScrollChanged = false;
2571 mAttachInfo.mTreeObserver.dispatchOnScrollChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 }
Romain Guy8506ab42009-06-11 17:35:47 -07002573
Dianne Hackborn0f761d62010-11-30 22:06:10 -08002574 boolean animating = mScroller != null && mScroller.computeScrollOffset();
Alan Viveretteccb11e12014-07-08 16:04:02 -07002575 final int curScrollY;
Dianne Hackborn0f761d62010-11-30 22:06:10 -08002576 if (animating) {
Alan Viveretteccb11e12014-07-08 16:04:02 -07002577 curScrollY = mScroller.getCurrY();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 } else {
Alan Viveretteccb11e12014-07-08 16:04:02 -07002579 curScrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 }
Alan Viveretteccb11e12014-07-08 16:04:02 -07002581 if (mCurScrollY != curScrollY) {
2582 mCurScrollY = curScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 fullRedrawNeeded = true;
Adrian Roos0e7ae4e2014-10-01 15:33:22 +02002584 if (mView instanceof RootViewSurfaceTaker) {
2585 ((RootViewSurfaceTaker) mView).onRootViewScrollYChanged(mCurScrollY);
2586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 }
Jeff Brown96e942d2011-11-30 19:55:01 -08002588
Chris Craikd36a81f2014-07-17 10:16:51 -07002589 final float appScale = mAttachInfo.mApplicationScale;
2590 final boolean scalingRequired = mAttachInfo.mScalingRequired;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591
Dianne Hackborn0f761d62010-11-30 22:06:10 -08002592 int resizeAlpha = 0;
Dianne Hackborn0f761d62010-11-30 22:06:10 -08002593
Jeff Brown96e942d2011-11-30 19:55:01 -08002594 final Rect dirty = mDirty;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07002595 if (mSurfaceHolder != null) {
2596 // The app owns the surface, we won't draw.
2597 dirty.setEmpty();
Derek Sollenberger8d948352015-07-16 09:27:59 -04002598 if (animating && mScroller != null) {
2599 mScroller.abortAnimation();
Dianne Hackborn0f761d62010-11-30 22:06:10 -08002600 }
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07002601 return;
2602 }
Romain Guy58ef7fb2010-09-13 12:52:37 -07002603
2604 if (fullRedrawNeeded) {
Chris Craikd36a81f2014-07-17 10:16:51 -07002605 mAttachInfo.mIgnoreDirtyState = true;
Romain Guyc3166e12011-08-08 15:00:03 -07002606 dirty.set(0, 0, (int) (mWidth * appScale + 0.5f), (int) (mHeight * appScale + 0.5f));
Romain Guy58ef7fb2010-09-13 12:52:37 -07002607 }
Chet Haasead4f7032011-06-22 09:18:31 -07002608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002610 Log.v(mTag, "Draw " + mView + "/"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611 + mWindowAttributes.getTitle()
2612 + ": dirty={" + dirty.left + "," + dirty.top
2613 + "," + dirty.right + "," + dirty.bottom + "} surface="
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002614 + surface + " surface.isValid()=" + surface.isValid() + ", appScale:" +
2615 appScale + ", width=" + mWidth + ", height=" + mHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 }
2617
Chris Craikd36a81f2014-07-17 10:16:51 -07002618 mAttachInfo.mTreeObserver.dispatchOnDraw();
Romain Guy25eba5c2012-04-04 17:29:03 -07002619
Chong Zhang0275e392015-09-17 10:41:44 -07002620 int xOffset = -mCanvasOffsetX;
2621 int yOffset = -mCanvasOffsetY + curScrollY;
Alan Viverettea51cab92014-07-16 15:15:49 -07002622 final WindowManager.LayoutParams params = mWindowAttributes;
2623 final Rect surfaceInsets = params != null ? params.surfaceInsets : null;
2624 if (surfaceInsets != null) {
2625 xOffset -= surfaceInsets.left;
2626 yOffset -= surfaceInsets.top;
2627
2628 // Offset dirty rect for surface insets.
2629 dirty.offset(surfaceInsets.left, surfaceInsets.right);
2630 }
2631
Alan Viverette632af842014-10-28 13:45:11 -07002632 boolean accessibilityFocusDirty = false;
2633 final Drawable drawable = mAttachInfo.mAccessibilityFocusDrawable;
2634 if (drawable != null) {
2635 final Rect bounds = mAttachInfo.mTmpInvalRect;
2636 final boolean hasFocus = getAccessibilityFocusedRect(bounds);
2637 if (!hasFocus) {
2638 bounds.setEmpty();
2639 }
2640 if (!bounds.equals(drawable.getBounds())) {
2641 accessibilityFocusDirty = true;
2642 }
2643 }
2644
John Reckba6adf62015-02-19 14:36:50 -08002645 mAttachInfo.mDrawingTime =
2646 mChoreographer.getFrameTimeNanos() / TimeUtils.NANOS_PER_MS;
2647
Alan Viverette632af842014-10-28 13:45:11 -07002648 if (!dirty.isEmpty() || mIsAnimating || accessibilityFocusDirty) {
Chris Craikd36a81f2014-07-17 10:16:51 -07002649 if (mAttachInfo.mHardwareRenderer != null && mAttachInfo.mHardwareRenderer.isEnabled()) {
Alan Viverette632af842014-10-28 13:45:11 -07002650 // If accessibility focus moved, always invalidate the root.
2651 boolean invalidateRoot = accessibilityFocusDirty;
2652
Jeff Brown96e942d2011-11-30 19:55:01 -08002653 // Draw with hardware renderer.
2654 mIsAnimating = false;
Alan Viverette632af842014-10-28 13:45:11 -07002655
John Reck0a973302014-07-16 13:29:45 -07002656 if (mHardwareYOffset != yOffset || mHardwareXOffset != xOffset) {
2657 mHardwareYOffset = yOffset;
2658 mHardwareXOffset = xOffset;
Alan Viverette632af842014-10-28 13:45:11 -07002659 invalidateRoot = true;
Alan Viverettef6cf1a02014-08-11 14:13:02 -07002660 }
2661
Alan Viverette632af842014-10-28 13:45:11 -07002662 if (invalidateRoot) {
2663 mAttachInfo.mHardwareRenderer.invalidateRoot();
2664 }
2665
Jeff Brown96e942d2011-11-30 19:55:01 -08002666 dirty.setEmpty();
2667
Skuhne980ee472015-10-06 11:31:31 -07002668 // Stage the content drawn size now. It will be transferred to the renderer
2669 // shortly before the draw commands get send to the renderer.
Chong Zhang8dbd9ad2015-10-09 10:06:11 -07002670 final boolean updated = updateContentDrawBounds();
2671
John Reck61375a82014-09-18 19:27:48 +00002672 mAttachInfo.mHardwareRenderer.draw(mView, mAttachInfo, this);
Chong Zhang8dbd9ad2015-10-09 10:06:11 -07002673
2674 if (updated) {
2675 requestDrawWindow();
2676 }
Romain Guy3696779b2013-01-28 14:04:07 -08002677 } else {
2678 // If we get here with a disabled & requested hardware renderer, something went
2679 // wrong (an invalidate posted right before we destroyed the hardware surface
2680 // for instance) so we should just bail out. Locking the surface with software
2681 // rendering at this point would lock it forever and prevent hardware renderer
2682 // from doing its job when it comes back.
2683 // Before we request a new frame we must however attempt to reinitiliaze the
2684 // hardware renderer if it's in requested state. This would happen after an
2685 // eglTerminate() for instance.
Chris Craikd36a81f2014-07-17 10:16:51 -07002686 if (mAttachInfo.mHardwareRenderer != null &&
2687 !mAttachInfo.mHardwareRenderer.isEnabled() &&
2688 mAttachInfo.mHardwareRenderer.isRequested()) {
Romain Guy3696779b2013-01-28 14:04:07 -08002689
2690 try {
Chris Craikd36a81f2014-07-17 10:16:51 -07002691 mAttachInfo.mHardwareRenderer.initializeIfNeeded(
Alan Viverette50210d92015-05-14 18:05:36 -07002692 mWidth, mHeight, mAttachInfo, mSurface, surfaceInsets);
Igor Murashkina86ab6402013-08-30 12:58:36 -07002693 } catch (OutOfResourcesException e) {
Romain Guy3696779b2013-01-28 14:04:07 -08002694 handleOutOfResourcesException(e);
2695 return;
2696 }
2697
2698 mFullRedrawNeeded = true;
2699 scheduleTraversals();
2700 return;
2701 }
2702
Chris Craikd36a81f2014-07-17 10:16:51 -07002703 if (!drawSoftware(surface, mAttachInfo, xOffset, yOffset, scalingRequired, dirty)) {
Romain Guy3696779b2013-01-28 14:04:07 -08002704 return;
2705 }
Jeff Brown95db2b22011-11-30 19:54:41 -08002706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 }
Romain Guy8506ab42009-06-11 17:35:47 -07002708
Dianne Hackborn0f761d62010-11-30 22:06:10 -08002709 if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 mFullRedrawNeeded = true;
2711 scheduleTraversals();
2712 }
2713 }
2714
Romain Guy25eba5c2012-04-04 17:29:03 -07002715 /**
Alan Viveretteccb11e12014-07-08 16:04:02 -07002716 * @return true if drawing was successful, false if an error occurred
Romain Guy25eba5c2012-04-04 17:29:03 -07002717 */
Alan Viveretteccb11e12014-07-08 16:04:02 -07002718 private boolean drawSoftware(Surface surface, AttachInfo attachInfo, int xoff, int yoff,
Romain Guy25eba5c2012-04-04 17:29:03 -07002719 boolean scalingRequired, Rect dirty) {
2720
2721 // Draw with software renderer.
Alan Viverettea51cab92014-07-16 15:15:49 -07002722 final Canvas canvas;
Romain Guy25eba5c2012-04-04 17:29:03 -07002723 try {
Alan Viverettea51cab92014-07-16 15:15:49 -07002724 final int left = dirty.left;
2725 final int top = dirty.top;
2726 final int right = dirty.right;
2727 final int bottom = dirty.bottom;
Romain Guy25eba5c2012-04-04 17:29:03 -07002728
Romain Guy25eba5c2012-04-04 17:29:03 -07002729 canvas = mSurface.lockCanvas(dirty);
2730
Romain Guye55945e2013-04-04 15:26:04 -07002731 // The dirty rectangle can be modified by Surface.lockCanvas()
2732 //noinspection ConstantConditions
Alan Viverettea51cab92014-07-16 15:15:49 -07002733 if (left != dirty.left || top != dirty.top || right != dirty.right
2734 || bottom != dirty.bottom) {
Romain Guy25eba5c2012-04-04 17:29:03 -07002735 attachInfo.mIgnoreDirtyState = true;
2736 }
2737
2738 // TODO: Do this in native
2739 canvas.setDensity(mDensity);
2740 } catch (Surface.OutOfResourcesException e) {
Romain Guy3696779b2013-01-28 14:04:07 -08002741 handleOutOfResourcesException(e);
Romain Guy25eba5c2012-04-04 17:29:03 -07002742 return false;
2743 } catch (IllegalArgumentException e) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002744 Log.e(mTag, "Could not lock surface", e);
Romain Guy25eba5c2012-04-04 17:29:03 -07002745 // Don't assume this is due to out of memory, it could be
2746 // something else, and if it is something else then we could
2747 // kill stuff (or ourself) for no reason.
2748 mLayoutRequested = true; // ask wm for a new surface next time.
2749 return false;
2750 }
2751
2752 try {
2753 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002754 Log.v(mTag, "Surface " + surface + " drawing to bitmap w="
Romain Guy25eba5c2012-04-04 17:29:03 -07002755 + canvas.getWidth() + ", h=" + canvas.getHeight());
2756 //canvas.drawARGB(255, 255, 0, 0);
2757 }
2758
Romain Guy25eba5c2012-04-04 17:29:03 -07002759 // If this bitmap's format includes an alpha channel, we
2760 // need to clear it before drawing so that the child will
2761 // properly re-composite its drawing on a transparent
2762 // background. This automatically respects the clip/dirty region
2763 // or
2764 // If we are applying an offset, we need to clear the area
2765 // where the offset doesn't appear to avoid having garbage
2766 // left in the blank areas.
Alan Viveretteccb11e12014-07-08 16:04:02 -07002767 if (!canvas.isOpaque() || yoff != 0 || xoff != 0) {
Romain Guy25eba5c2012-04-04 17:29:03 -07002768 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
2769 }
2770
2771 dirty.setEmpty();
2772 mIsAnimating = false;
Dianne Hackborn4702a852012-08-17 15:18:29 -07002773 mView.mPrivateFlags |= View.PFLAG_DRAWN;
Romain Guy25eba5c2012-04-04 17:29:03 -07002774
2775 if (DEBUG_DRAW) {
2776 Context cxt = mView.getContext();
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002777 Log.i(mTag, "Drawing: package:" + cxt.getPackageName() +
Romain Guy25eba5c2012-04-04 17:29:03 -07002778 ", metrics=" + cxt.getResources().getDisplayMetrics() +
2779 ", compatibilityInfo=" + cxt.getResources().getCompatibilityInfo());
2780 }
2781 try {
Alan Viveretteccb11e12014-07-08 16:04:02 -07002782 canvas.translate(-xoff, -yoff);
Romain Guy25eba5c2012-04-04 17:29:03 -07002783 if (mTranslator != null) {
2784 mTranslator.translateCanvas(canvas);
2785 }
Dianne Hackborn908aecc2012-07-31 16:37:34 -07002786 canvas.setScreenDensity(scalingRequired ? mNoncompatDensity : 0);
Romain Guy25eba5c2012-04-04 17:29:03 -07002787 attachInfo.mSetIgnoreDirtyState = false;
2788
Romain Guy25eba5c2012-04-04 17:29:03 -07002789 mView.draw(canvas);
Alan Viverette632af842014-10-28 13:45:11 -07002790
2791 drawAccessibilityFocusedDrawableIfNeeded(canvas);
Romain Guy25eba5c2012-04-04 17:29:03 -07002792 } finally {
2793 if (!attachInfo.mSetIgnoreDirtyState) {
2794 // Only clear the flag if it was not set during the mView.draw() call
2795 attachInfo.mIgnoreDirtyState = false;
2796 }
2797 }
Romain Guy25eba5c2012-04-04 17:29:03 -07002798 } finally {
Romain Guydddcd222012-05-18 15:33:57 -07002799 try {
2800 surface.unlockCanvasAndPost(canvas);
2801 } catch (IllegalArgumentException e) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002802 Log.e(mTag, "Could not unlock surface", e);
Romain Guydddcd222012-05-18 15:33:57 -07002803 mLayoutRequested = true; // ask wm for a new surface next time.
2804 //noinspection ReturnInsideFinallyBlock
2805 return false;
2806 }
Romain Guy25eba5c2012-04-04 17:29:03 -07002807
Romain Guy25eba5c2012-04-04 17:29:03 -07002808 if (LOCAL_LOGV) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002809 Log.v(mTag, "Surface " + surface + " unlockCanvasAndPost");
Romain Guy25eba5c2012-04-04 17:29:03 -07002810 }
2811 }
2812 return true;
2813 }
2814
Alan Viverette632af842014-10-28 13:45:11 -07002815 /**
2816 * We want to draw a highlight around the current accessibility focused.
2817 * Since adding a style for all possible view is not a viable option we
2818 * have this specialized drawing method.
2819 *
2820 * Note: We are doing this here to be able to draw the highlight for
2821 * virtual views in addition to real ones.
2822 *
2823 * @param canvas The canvas on which to draw.
2824 */
2825 private void drawAccessibilityFocusedDrawableIfNeeded(Canvas canvas) {
2826 final Rect bounds = mAttachInfo.mTmpInvalRect;
2827 if (getAccessibilityFocusedRect(bounds)) {
2828 final Drawable drawable = getAccessibilityFocusedDrawable();
2829 if (drawable != null) {
2830 drawable.setBounds(bounds);
2831 drawable.draw(canvas);
2832 }
2833 } else if (mAttachInfo.mAccessibilityFocusDrawable != null) {
2834 mAttachInfo.mAccessibilityFocusDrawable.setBounds(0, 0, 0, 0);
2835 }
2836 }
2837
2838 private boolean getAccessibilityFocusedRect(Rect bounds) {
2839 final AccessibilityManager manager = AccessibilityManager.getInstance(mView.mContext);
2840 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
2841 return false;
2842 }
2843
2844 final View host = mAccessibilityFocusedHost;
2845 if (host == null || host.mAttachInfo == null) {
2846 return false;
2847 }
2848
2849 final AccessibilityNodeProvider provider = host.getAccessibilityNodeProvider();
2850 if (provider == null) {
Svetoslavded133c2015-01-30 20:28:41 -08002851 host.getBoundsOnScreen(bounds, true);
Alan Viverette632af842014-10-28 13:45:11 -07002852 } else if (mAccessibilityFocusedVirtualView != null) {
2853 mAccessibilityFocusedVirtualView.getBoundsInScreen(bounds);
2854 } else {
2855 return false;
2856 }
2857
Alan Viverette2232add2015-05-26 15:24:18 -07002858 // Transform the rect into window-relative coordinates.
Alan Viverette632af842014-10-28 13:45:11 -07002859 final AttachInfo attachInfo = mAttachInfo;
Alan Viverette2232add2015-05-26 15:24:18 -07002860 bounds.offset(0, attachInfo.mViewRootImpl.mScrollY);
Alan Viverette632af842014-10-28 13:45:11 -07002861 bounds.offset(-attachInfo.mWindowLeft, -attachInfo.mWindowTop);
Doris Liu9607fbe2015-05-28 17:17:28 -07002862 if (!bounds.intersect(0, 0, attachInfo.mViewRootImpl.mWidth,
2863 attachInfo.mViewRootImpl.mHeight)) {
2864 // If no intersection, set bounds to empty.
2865 bounds.setEmpty();
2866 }
Alan Viverette632af842014-10-28 13:45:11 -07002867 return !bounds.isEmpty();
2868 }
2869
2870 private Drawable getAccessibilityFocusedDrawable() {
Chris Craikd36a81f2014-07-17 10:16:51 -07002871 // Lazily load the accessibility focus drawable.
2872 if (mAttachInfo.mAccessibilityFocusDrawable == null) {
Alan Viverettef6cf1a02014-08-11 14:13:02 -07002873 final TypedValue value = new TypedValue();
Chris Craikd36a81f2014-07-17 10:16:51 -07002874 final boolean resolved = mView.mContext.getTheme().resolveAttribute(
2875 R.attr.accessibilityFocusedDrawable, value, true);
2876 if (resolved) {
2877 mAttachInfo.mAccessibilityFocusDrawable =
Alan Viverette8eea3ea2014-02-03 18:40:20 -08002878 mView.mContext.getDrawable(value.resourceId);
Svetoslav Ganov42138042012-03-20 11:51:39 -07002879 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07002880 }
Chris Craikd36a81f2014-07-17 10:16:51 -07002881 return mAttachInfo.mAccessibilityFocusDrawable;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002882 }
2883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 boolean scrollToRectOrFocus(Rect rectangle, boolean immediate) {
Chris Craikd36a81f2014-07-17 10:16:51 -07002885 final Rect ci = mAttachInfo.mContentInsets;
2886 final Rect vi = mAttachInfo.mVisibleInsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 int scrollY = 0;
2888 boolean handled = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 if (vi.left > ci.left || vi.top > ci.top
2891 || vi.right > ci.right || vi.bottom > ci.bottom) {
2892 // We'll assume that we aren't going to change the scroll
2893 // offset, since we want to avoid that unless it is actually
2894 // going to make the focus visible... otherwise we scroll
2895 // all over the place.
2896 scrollY = mScrollY;
2897 // We can be called for two different situations: during a draw,
2898 // to update the scroll position if the focus has changed (in which
2899 // case 'rectangle' is null), or in response to a
2900 // requestChildRectangleOnScreen() call (in which case 'rectangle'
2901 // is non-null and we just want to scroll to whatever that
2902 // rectangle is).
Craig Mautner26a84df2013-04-11 19:09:05 -07002903 final View focus = mView.findFocus();
Svetoslav Ganov149567f2013-01-08 15:23:34 -08002904 if (focus == null) {
Romain Guye8b16522009-07-14 13:06:42 -07002905 return false;
2906 }
Svetoslav Ganov149567f2013-01-08 15:23:34 -08002907 View lastScrolledFocus = (mLastScrolledFocus != null) ? mLastScrolledFocus.get() : null;
Craig Mautner26a84df2013-04-11 19:09:05 -07002908 if (focus != lastScrolledFocus) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 // If the focus has changed, then ignore any requests to scroll
2910 // to a rectangle; first we want to make sure the entire focus
2911 // view is visible.
2912 rectangle = null;
2913 }
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002914 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "Eval scroll: focus=" + focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 + " rectangle=" + rectangle + " ci=" + ci
2916 + " vi=" + vi);
Svetoslav Ganov149567f2013-01-08 15:23:34 -08002917 if (focus == lastScrolledFocus && !mScrollMayChange && rectangle == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 // Optimization: if the focus hasn't changed since last
2919 // time, and no layout has happened, then just leave things
2920 // as they are.
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002921 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "Keeping scroll y="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 + mScrollY + " vi=" + vi.toShortString());
Craig Mautner26a84df2013-04-11 19:09:05 -07002923 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 // We need to determine if the currently focused view is
2925 // within the visible part of the window and, if not, apply
2926 // a pan so it can be seen.
Svetoslav Ganov149567f2013-01-08 15:23:34 -08002927 mLastScrolledFocus = new WeakReference<View>(focus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 mScrollMayChange = false;
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002929 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "Need to scroll?");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002930 // Try to find the rectangle from the focus view.
2931 if (focus.getGlobalVisibleRect(mVisRect, null)) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002932 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "Root w="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 + mView.getWidth() + " h=" + mView.getHeight()
2934 + " ci=" + ci.toShortString()
2935 + " vi=" + vi.toShortString());
2936 if (rectangle == null) {
2937 focus.getFocusedRect(mTempRect);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002938 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "Focus " + focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 + ": focusRect=" + mTempRect.toShortString());
Dianne Hackborn1c6a8942010-03-23 16:34:20 -07002940 if (mView instanceof ViewGroup) {
2941 ((ViewGroup) mView).offsetDescendantRectToMyCoords(
2942 focus, mTempRect);
2943 }
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002944 if (DEBUG_INPUT_RESIZE) Log.v(mTag,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 "Focus in window: focusRect="
2946 + mTempRect.toShortString()
2947 + " visRect=" + mVisRect.toShortString());
2948 } else {
2949 mTempRect.set(rectangle);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002950 if (DEBUG_INPUT_RESIZE) Log.v(mTag,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 "Request scroll to rect: "
2952 + mTempRect.toShortString()
2953 + " visRect=" + mVisRect.toShortString());
2954 }
2955 if (mTempRect.intersect(mVisRect)) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002956 if (DEBUG_INPUT_RESIZE) Log.v(mTag,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 "Focus window visible rect: "
2958 + mTempRect.toShortString());
2959 if (mTempRect.height() >
2960 (mView.getHeight()-vi.top-vi.bottom)) {
2961 // If the focus simply is not going to fit, then
2962 // best is probably just to leave things as-is.
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002963 if (DEBUG_INPUT_RESIZE) Log.v(mTag,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 "Too tall; leaving scrollY=" + scrollY);
2965 } else if ((mTempRect.top-scrollY) < vi.top) {
2966 scrollY -= vi.top - (mTempRect.top-scrollY);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002967 if (DEBUG_INPUT_RESIZE) Log.v(mTag,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 "Top covered; scrollY=" + scrollY);
2969 } else if ((mTempRect.bottom-scrollY)
2970 > (mView.getHeight()-vi.bottom)) {
2971 scrollY += (mTempRect.bottom-scrollY)
2972 - (mView.getHeight()-vi.bottom);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002973 if (DEBUG_INPUT_RESIZE) Log.v(mTag,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002974 "Bottom covered; scrollY=" + scrollY);
2975 }
2976 handled = true;
2977 }
2978 }
2979 }
2980 }
Romain Guy8506ab42009-06-11 17:35:47 -07002981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 if (scrollY != mScrollY) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08002983 if (DEBUG_INPUT_RESIZE) Log.v(mTag, "Pan scroll changed: old="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002984 + mScrollY + " , new=" + scrollY);
Derek Sollenberger8d948352015-07-16 09:27:59 -04002985 if (!immediate) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 if (mScroller == null) {
2987 mScroller = new Scroller(mView.getContext());
2988 }
2989 mScroller.startScroll(0, mScrollY, 0, scrollY-mScrollY);
2990 } else if (mScroller != null) {
2991 mScroller.abortAnimation();
2992 }
2993 mScrollY = scrollY;
2994 }
Romain Guy8506ab42009-06-11 17:35:47 -07002995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002996 return handled;
2997 }
Romain Guy8506ab42009-06-11 17:35:47 -07002998
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07002999 /**
3000 * @hide
3001 */
3002 public View getAccessibilityFocusedHost() {
3003 return mAccessibilityFocusedHost;
3004 }
3005
3006 /**
3007 * @hide
3008 */
3009 public AccessibilityNodeInfo getAccessibilityFocusedVirtualView() {
3010 return mAccessibilityFocusedVirtualView;
3011 }
3012
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07003013 void setAccessibilityFocus(View view, AccessibilityNodeInfo node) {
Svetoslav Ganov791fd312012-05-14 15:12:30 -07003014 // If we have a virtual view with accessibility focus we need
3015 // to clear the focus and invalidate the virtual view bounds.
3016 if (mAccessibilityFocusedVirtualView != null) {
3017
3018 AccessibilityNodeInfo focusNode = mAccessibilityFocusedVirtualView;
3019 View focusHost = mAccessibilityFocusedHost;
Svetoslav Ganov791fd312012-05-14 15:12:30 -07003020
3021 // Wipe the state of the current accessibility focus since
3022 // the call into the provider to clear accessibility focus
3023 // will fire an accessibility event which will end up calling
3024 // this method and we want to have clean state when this
3025 // invocation happens.
3026 mAccessibilityFocusedHost = null;
3027 mAccessibilityFocusedVirtualView = null;
3028
Alan Viverette239a0c02013-05-07 17:17:35 -07003029 // Clear accessibility focus on the host after clearing state since
3030 // this method may be reentrant.
3031 focusHost.clearAccessibilityFocusNoCallbacks();
3032
Svetoslav Ganov791fd312012-05-14 15:12:30 -07003033 AccessibilityNodeProvider provider = focusHost.getAccessibilityNodeProvider();
3034 if (provider != null) {
3035 // Invalidate the area of the cleared accessibility focus.
3036 focusNode.getBoundsInParent(mTempRect);
3037 focusHost.invalidate(mTempRect);
3038 // Clear accessibility focus in the virtual node.
3039 final int virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
3040 focusNode.getSourceNodeId());
3041 provider.performAction(virtualNodeId,
3042 AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS, null);
3043 }
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07003044 focusNode.recycle();
Svetoslav Ganov791fd312012-05-14 15:12:30 -07003045 }
3046 if (mAccessibilityFocusedHost != null) {
3047 // Clear accessibility focus in the view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07003048 mAccessibilityFocusedHost.clearAccessibilityFocusNoCallbacks();
3049 }
Svetoslav Ganov791fd312012-05-14 15:12:30 -07003050
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07003051 // Set the new focus host and node.
3052 mAccessibilityFocusedHost = view;
3053 mAccessibilityFocusedVirtualView = node;
John Reck0a973302014-07-16 13:29:45 -07003054
3055 if (mAttachInfo.mHardwareRenderer != null) {
3056 mAttachInfo.mHardwareRenderer.invalidateRoot();
3057 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07003058 }
3059
Jun Mukai347e5d42015-12-03 01:13:31 -08003060 void setPointerCapture(View view) {
3061 if (!mAttachInfo.mHasWindowFocus) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08003062 Log.w(mTag, "Can't set capture if it's not focused.");
Jun Mukai347e5d42015-12-03 01:13:31 -08003063 return;
3064 }
3065 if (mCapturingView == view) {
3066 return;
3067 }
3068 mCapturingView = view;
3069 InputManager.getInstance().setPointerIconDetached(true);
3070 }
3071
3072 void releasePointerCapture(View view) {
3073 if (mCapturingView != view || mCapturingView == null) {
3074 return;
3075 }
3076
3077 mCapturingView = null;
3078 InputManager.getInstance().setPointerIconDetached(false);
3079 }
3080
3081 boolean hasPointerCapture(View view) {
3082 return view != null && mCapturingView == view;
3083 }
3084
Igor Murashkina86ab6402013-08-30 12:58:36 -07003085 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003086 public void requestChildFocus(View child, View focused) {
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08003087 if (DEBUG_INPUT_RESIZE) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08003088 Log.v(mTag, "Request child focus: focus now " + focused);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003089 }
Svetoslav Ganov149567f2013-01-08 15:23:34 -08003090 checkThread();
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08003091 scheduleTraversals();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 }
3093
Igor Murashkina86ab6402013-08-30 12:58:36 -07003094 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003095 public void clearChildFocus(View child) {
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08003096 if (DEBUG_INPUT_RESIZE) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08003097 Log.v(mTag, "Clearing child focus");
Amith Yamasani73eb97f2012-02-14 15:45:15 -08003098 }
Svetoslav Ganov149567f2013-01-08 15:23:34 -08003099 checkThread();
3100 scheduleTraversals();
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08003101 }
Amith Yamasani73eb97f2012-02-14 15:45:15 -08003102
Svetoslav Ganov42138042012-03-20 11:51:39 -07003103 @Override
3104 public ViewParent getParentForAccessibility() {
3105 return null;
3106 }
3107
Igor Murashkina86ab6402013-08-30 12:58:36 -07003108 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 public void focusableViewAvailable(View v) {
3110 checkThread();
Romain Guy1c90f032011-05-24 14:59:50 -07003111 if (mView != null) {
3112 if (!mView.hasFocus()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003113 v.requestFocus();
Romain Guy1c90f032011-05-24 14:59:50 -07003114 } else {
3115 // the one case where will transfer focus away from the current one
3116 // is if the current view is a view group that prefers to give focus
3117 // to its children first AND the view is a descendant of it.
Svetoslav Ganov149567f2013-01-08 15:23:34 -08003118 View focused = mView.findFocus();
3119 if (focused instanceof ViewGroup) {
3120 ViewGroup group = (ViewGroup) focused;
3121 if (group.getDescendantFocusability() == ViewGroup.FOCUS_AFTER_DESCENDANTS
3122 && isViewDescendantOf(v, focused)) {
3123 v.requestFocus();
3124 }
Romain Guy1c90f032011-05-24 14:59:50 -07003125 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 }
3127 }
3128 }
3129
Igor Murashkina86ab6402013-08-30 12:58:36 -07003130 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003131 public void recomputeViewAttributes(View child) {
3132 checkThread();
3133 if (mView == child) {
3134 mAttachInfo.mRecomputeGlobalAttributes = true;
3135 if (!mWillDrawSoon) {
3136 scheduleTraversals();
3137 }
3138 }
3139 }
3140
3141 void dispatchDetachedFromWindow() {
Romain Guy90fc03b2011-01-16 13:07:15 -08003142 if (mView != null && mView.mAttachInfo != null) {
Dianne Hackborn961cae92013-03-20 14:59:43 -07003143 mAttachInfo.mTreeObserver.dispatchOnWindowAttachedChange(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 mView.dispatchDetachedFromWindow();
3145 }
3146
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003147 mAccessibilityInteractionConnectionManager.ensureNoConnection();
3148 mAccessibilityManager.removeAccessibilityStateChangeListener(
3149 mAccessibilityInteractionConnectionManager);
Chris Craikcce47eb2014-07-16 15:12:15 -07003150 mAccessibilityManager.removeHighTextContrastStateChangeListener(
3151 mHighContrastTextManager);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003152 removeSendWindowContentChangedCallback();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003153
Romain Guya998dff2012-03-23 18:58:36 -07003154 destroyHardwareRenderer();
3155
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07003156 setAccessibilityFocus(null, null);
Svetoslav Ganov791fd312012-05-14 15:12:30 -07003157
Craig Mautner8f303ad2013-06-14 11:32:22 -07003158 mView.assignParent(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003159 mView = null;
3160 mAttachInfo.mRootView = null;
3161
Jun Mukai347e5d42015-12-03 01:13:31 -08003162 if (mCapturingView != null) {
3163 releasePointerCapture(mCapturingView);
3164 }
3165
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07003166 mSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167
Jeff Browncc4f7db2011-08-30 20:34:48 -07003168 if (mInputQueueCallback != null && mInputQueue != null) {
3169 mInputQueueCallback.onInputQueueDestroyed(mInputQueue);
Michael Wrighta44dd262013-04-10 21:12:00 -07003170 mInputQueue.dispose();
Jeff Browncc4f7db2011-08-30 20:34:48 -07003171 mInputQueueCallback = null;
3172 mInputQueue = null;
Michael Wrighta44dd262013-04-10 21:12:00 -07003173 }
3174 if (mInputEventReceiver != null) {
Jeff Brown32cbc38552011-12-01 14:01:49 -08003175 mInputEventReceiver.dispose();
3176 mInputEventReceiver = null;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178 try {
Jeff Brown98365d72012-08-19 20:30:52 -07003179 mWindowSession.remove(mWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 } catch (RemoteException e) {
3181 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07003182
Jeff Brown00fa7bd2010-07-02 15:37:36 -07003183 // Dispose the input channel after removing the window so the Window Manager
3184 // doesn't interpret the input channel being closed as an abnormal termination.
3185 if (mInputChannel != null) {
3186 mInputChannel.dispose();
3187 mInputChannel = null;
Jeff Brown349703e2010-06-22 01:27:15 -07003188 }
Jeff Brown96e942d2011-11-30 19:55:01 -08003189
Jeff Brownd912e1f2014-04-11 18:46:22 -07003190 mDisplayManager.unregisterDisplayListener(mDisplayListener);
3191
Jeff Brownebb2d8d2012-03-23 17:14:34 -07003192 unscheduleTraversals();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 }
Romain Guy8506ab42009-06-11 17:35:47 -07003194
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003195 void updateConfiguration(Configuration config, boolean force) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08003196 if (DEBUG_CONFIGURATION) Log.v(mTag,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003197 "Applying new config to window "
3198 + mWindowAttributes.getTitle()
3199 + ": " + config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003200
Craig Mautner48d0d182013-06-11 07:53:06 -07003201 CompatibilityInfo ci = mDisplayAdjustments.getCompatibilityInfo();
3202 if (!ci.equals(CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO)) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003203 config = new Configuration(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003204 ci.applyToConfiguration(mNoncompatDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003205 }
3206
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003207 synchronized (sConfigCallbacks) {
3208 for (int i=sConfigCallbacks.size()-1; i>=0; i--) {
3209 sConfigCallbacks.get(i).onConfigurationChanged(config);
3210 }
3211 }
3212 if (mView != null) {
3213 // At this point the resources have been updated to
3214 // have the most recent config, whatever that is. Use
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003215 // the one in them which may be newer.
Romain Guy1c90f032011-05-24 14:59:50 -07003216 config = mView.getResources().getConfiguration();
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003217 if (force || mLastConfiguration.diff(config) != 0) {
Fabrice Di Megliocf128972012-10-16 20:51:12 -07003218 final int lastLayoutDirection = mLastConfiguration.getLayoutDirection();
3219 final int currentLayoutDirection = config.getLayoutDirection();
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003220 mLastConfiguration.setTo(config);
Fabrice Di Megliob003e282012-10-17 17:20:19 -07003221 if (lastLayoutDirection != currentLayoutDirection &&
3222 mViewLayoutDirectionInitial == View.LAYOUT_DIRECTION_INHERIT) {
Fabrice Di Megliocf128972012-10-16 20:51:12 -07003223 mView.setLayoutDirection(currentLayoutDirection);
3224 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003225 mView.dispatchConfigurationChanged(config);
3226 }
3227 }
3228 }
John Reck05e85842014-04-23 14:48:28 -07003229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003230 /**
3231 * Return true if child is an ancestor of parent, (or equal to the parent).
3232 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07003233 public static boolean isViewDescendantOf(View child, View parent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 if (child == parent) {
3235 return true;
3236 }
3237
3238 final ViewParent theParent = child.getParent();
3239 return (theParent instanceof ViewGroup) && isViewDescendantOf((View) theParent, parent);
3240 }
3241
Yohei Yukawad2e56472015-07-28 17:00:33 -07003242 private static void forceLayout(View view) {
3243 view.forceLayout();
3244 if (view instanceof ViewGroup) {
3245 ViewGroup group = (ViewGroup) view;
3246 final int count = group.getChildCount();
3247 for (int i = 0; i < count; i++) {
3248 forceLayout(group.getChildAt(i));
3249 }
3250 }
3251 }
3252
Jeff Browna175a5b2012-02-15 19:18:31 -08003253 private final static int MSG_INVALIDATE = 1;
3254 private final static int MSG_INVALIDATE_RECT = 2;
3255 private final static int MSG_DIE = 3;
3256 private final static int MSG_RESIZED = 4;
3257 private final static int MSG_RESIZED_REPORT = 5;
3258 private final static int MSG_WINDOW_FOCUS_CHANGED = 6;
keunyoung30f420f2013-08-02 14:23:10 -07003259 private final static int MSG_DISPATCH_INPUT_EVENT = 7;
Jeff Browna175a5b2012-02-15 19:18:31 -08003260 private final static int MSG_DISPATCH_APP_VISIBILITY = 8;
3261 private final static int MSG_DISPATCH_GET_NEW_SURFACE = 9;
Jeff Browna175a5b2012-02-15 19:18:31 -08003262 private final static int MSG_DISPATCH_KEY_FROM_IME = 11;
3263 private final static int MSG_FINISH_INPUT_CONNECTION = 12;
3264 private final static int MSG_CHECK_FOCUS = 13;
3265 private final static int MSG_CLOSE_SYSTEM_DIALOGS = 14;
3266 private final static int MSG_DISPATCH_DRAG_EVENT = 15;
3267 private final static int MSG_DISPATCH_DRAG_LOCATION_EVENT = 16;
3268 private final static int MSG_DISPATCH_SYSTEM_UI_VISIBILITY = 17;
3269 private final static int MSG_UPDATE_CONFIGURATION = 18;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003270 private final static int MSG_PROCESS_INPUT_EVENTS = 19;
Romain Guyfbb93fa2012-12-03 18:50:35 -08003271 private final static int MSG_CLEAR_ACCESSIBILITY_FOCUS_HOST = 21;
Jorim Jaggi827e0fa2015-05-07 11:41:41 -07003272 private final static int MSG_INVALIDATE_WORLD = 22;
3273 private final static int MSG_WINDOW_MOVED = 23;
3274 private final static int MSG_SYNTHESIZE_INPUT_EVENT = 24;
3275 private final static int MSG_DISPATCH_WINDOW_SHOWN = 25;
Clara Bayarri75e09792015-07-29 16:20:40 +01003276 private final static int MSG_REQUEST_KEYBOARD_SHORTCUTS = 26;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003277
Jeff Browna175a5b2012-02-15 19:18:31 -08003278 final class ViewRootHandler extends Handler {
3279 @Override
3280 public String getMessageName(Message message) {
3281 switch (message.what) {
3282 case MSG_INVALIDATE:
3283 return "MSG_INVALIDATE";
3284 case MSG_INVALIDATE_RECT:
3285 return "MSG_INVALIDATE_RECT";
3286 case MSG_DIE:
3287 return "MSG_DIE";
3288 case MSG_RESIZED:
3289 return "MSG_RESIZED";
3290 case MSG_RESIZED_REPORT:
3291 return "MSG_RESIZED_REPORT";
3292 case MSG_WINDOW_FOCUS_CHANGED:
3293 return "MSG_WINDOW_FOCUS_CHANGED";
keunyoung30f420f2013-08-02 14:23:10 -07003294 case MSG_DISPATCH_INPUT_EVENT:
3295 return "MSG_DISPATCH_INPUT_EVENT";
Jeff Browna175a5b2012-02-15 19:18:31 -08003296 case MSG_DISPATCH_APP_VISIBILITY:
3297 return "MSG_DISPATCH_APP_VISIBILITY";
3298 case MSG_DISPATCH_GET_NEW_SURFACE:
3299 return "MSG_DISPATCH_GET_NEW_SURFACE";
Jeff Browna175a5b2012-02-15 19:18:31 -08003300 case MSG_DISPATCH_KEY_FROM_IME:
3301 return "MSG_DISPATCH_KEY_FROM_IME";
3302 case MSG_FINISH_INPUT_CONNECTION:
3303 return "MSG_FINISH_INPUT_CONNECTION";
3304 case MSG_CHECK_FOCUS:
3305 return "MSG_CHECK_FOCUS";
3306 case MSG_CLOSE_SYSTEM_DIALOGS:
3307 return "MSG_CLOSE_SYSTEM_DIALOGS";
3308 case MSG_DISPATCH_DRAG_EVENT:
3309 return "MSG_DISPATCH_DRAG_EVENT";
3310 case MSG_DISPATCH_DRAG_LOCATION_EVENT:
3311 return "MSG_DISPATCH_DRAG_LOCATION_EVENT";
3312 case MSG_DISPATCH_SYSTEM_UI_VISIBILITY:
3313 return "MSG_DISPATCH_SYSTEM_UI_VISIBILITY";
3314 case MSG_UPDATE_CONFIGURATION:
3315 return "MSG_UPDATE_CONFIGURATION";
Jeff Browna175a5b2012-02-15 19:18:31 -08003316 case MSG_PROCESS_INPUT_EVENTS:
3317 return "MSG_PROCESS_INPUT_EVENTS";
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07003318 case MSG_CLEAR_ACCESSIBILITY_FOCUS_HOST:
3319 return "MSG_CLEAR_ACCESSIBILITY_FOCUS_HOST";
Craig Mautner5702d4d2012-06-30 14:10:16 -07003320 case MSG_WINDOW_MOVED:
3321 return "MSG_WINDOW_MOVED";
Michael Wright899d7052014-04-23 17:23:39 -07003322 case MSG_SYNTHESIZE_INPUT_EVENT:
3323 return "MSG_SYNTHESIZE_INPUT_EVENT";
Craig Mautner9c795042014-10-28 19:59:59 -07003324 case MSG_DISPATCH_WINDOW_SHOWN:
3325 return "MSG_DISPATCH_WINDOW_SHOWN";
Jeff Browna175a5b2012-02-15 19:18:31 -08003326 }
3327 return super.getMessageName(message);
Romain Guyf9284692011-07-13 18:46:21 -07003328 }
Romain Guyf9284692011-07-13 18:46:21 -07003329
Jeff Browna175a5b2012-02-15 19:18:31 -08003330 @Override
3331 public void handleMessage(Message msg) {
3332 switch (msg.what) {
3333 case MSG_INVALIDATE:
3334 ((View) msg.obj).invalidate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003335 break;
Jeff Browna175a5b2012-02-15 19:18:31 -08003336 case MSG_INVALIDATE_RECT:
3337 final View.AttachInfo.InvalidateInfo info = (View.AttachInfo.InvalidateInfo) msg.obj;
3338 info.target.invalidate(info.left, info.top, info.right, info.bottom);
Svetoslav Ganovabae2a12012-11-27 16:59:37 -08003339 info.recycle();
Jeff Browna175a5b2012-02-15 19:18:31 -08003340 break;
Jeff Browna175a5b2012-02-15 19:18:31 -08003341 case MSG_PROCESS_INPUT_EVENTS:
3342 mProcessInputEventsScheduled = false;
3343 doProcessInputEvents();
3344 break;
3345 case MSG_DISPATCH_APP_VISIBILITY:
3346 handleAppVisibility(msg.arg1 != 0);
3347 break;
3348 case MSG_DISPATCH_GET_NEW_SURFACE:
3349 handleGetNewSurface();
3350 break;
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003351 case MSG_RESIZED: {
3352 // Recycled in the fall through...
3353 SomeArgs args = (SomeArgs) msg.obj;
Romain Guydfab3632012-10-03 14:53:25 -07003354 if (mWinFrame.equals(args.arg1)
Dianne Hackbornc4aad012013-02-22 15:05:25 -08003355 && mPendingOverscanInsets.equals(args.arg5)
Romain Guydfab3632012-10-03 14:53:25 -07003356 && mPendingContentInsets.equals(args.arg2)
Adrian Roosfa104232014-06-20 16:10:14 -07003357 && mPendingStableInsets.equals(args.arg6)
Romain Guydfab3632012-10-03 14:53:25 -07003358 && mPendingVisibleInsets.equals(args.arg3)
Filip Gruszczynski2217f612015-05-26 11:32:08 -07003359 && mPendingOutsets.equals(args.arg7)
Jorim Jaggi0fe356e2016-01-05 14:43:25 +01003360 && mPendingBackDropFrame.equals(args.arg8)
Romain Guydfab3632012-10-03 14:53:25 -07003361 && args.arg4 == null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08003362 break;
Romain Guycdb86672010-03-18 18:54:50 -07003363 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003364 } // fall through...
Jeff Browna175a5b2012-02-15 19:18:31 -08003365 case MSG_RESIZED_REPORT:
3366 if (mAdded) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003367 SomeArgs args = (SomeArgs) msg.obj;
3368
3369 Configuration config = (Configuration) args.arg4;
Jeff Browna175a5b2012-02-15 19:18:31 -08003370 if (config != null) {
3371 updateConfiguration(config, false);
3372 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003373
3374 mWinFrame.set((Rect) args.arg1);
Dianne Hackbornc4aad012013-02-22 15:05:25 -08003375 mPendingOverscanInsets.set((Rect) args.arg5);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003376 mPendingContentInsets.set((Rect) args.arg2);
Adrian Roosfa104232014-06-20 16:10:14 -07003377 mPendingStableInsets.set((Rect) args.arg6);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003378 mPendingVisibleInsets.set((Rect) args.arg3);
Filip Gruszczynski2217f612015-05-26 11:32:08 -07003379 mPendingOutsets.set((Rect) args.arg7);
Jorim Jaggia7262a82015-11-03 15:15:40 +01003380 mPendingBackDropFrame.set((Rect) args.arg8);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07003381
3382 args.recycle();
3383
Jeff Browna175a5b2012-02-15 19:18:31 -08003384 if (msg.what == MSG_RESIZED_REPORT) {
3385 mReportNextDraw = true;
3386 }
Romain Guy59a12ca2011-06-09 17:48:21 -07003387
Yohei Yukawad2e56472015-07-28 17:00:33 -07003388 if (mView != null) {
3389 forceLayout(mView);
3390 }
3391
Jeff Browna175a5b2012-02-15 19:18:31 -08003392 requestLayout();
3393 }
3394 break;
Craig Mautner5702d4d2012-06-30 14:10:16 -07003395 case MSG_WINDOW_MOVED:
3396 if (mAdded) {
3397 final int w = mWinFrame.width();
3398 final int h = mWinFrame.height();
3399 final int l = msg.arg1;
3400 final int t = msg.arg2;
3401 mWinFrame.left = l;
3402 mWinFrame.right = l + w;
3403 mWinFrame.top = t;
3404 mWinFrame.bottom = t + h;
3405
Jorim Jaggia7262a82015-11-03 15:15:40 +01003406 mPendingBackDropFrame.set(mWinFrame);
3407
Jorim Jaggi844e1712016-01-13 17:39:25 -08003408 // Suppress layouts during resizing - a correct layout will happen when resizing
3409 // is done, and this just increases system load.
Jorim Jaggi2e95a482016-01-14 17:36:55 -08003410 boolean isDockedDivider = mWindowAttributes.type == TYPE_DOCK_DIVIDER;
3411 boolean suppress = (mDragResizing && mResizeMode == RESIZE_MODE_DOCKED_DIVIDER)
3412 || isDockedDivider;
Jorim Jaggi844e1712016-01-13 17:39:25 -08003413 if (!suppress) {
3414 if (mView != null) {
3415 forceLayout(mView);
3416 }
3417 requestLayout();
Jorim Jaggi2e95a482016-01-14 17:36:55 -08003418 } else {
3419 maybeHandleWindowMove(mWinFrame);
Yohei Yukawad2e56472015-07-28 17:00:33 -07003420 }
Craig Mautner5702d4d2012-06-30 14:10:16 -07003421 }
3422 break;
Jeff Browna175a5b2012-02-15 19:18:31 -08003423 case MSG_WINDOW_FOCUS_CHANGED: {
3424 if (mAdded) {
3425 boolean hasWindowFocus = msg.arg1 != 0;
3426 mAttachInfo.mHasWindowFocus = hasWindowFocus;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427
Jeff Browna175a5b2012-02-15 19:18:31 -08003428 profileRendering(hasWindowFocus);
3429
3430 if (hasWindowFocus) {
3431 boolean inTouchMode = msg.arg2 != 0;
3432 ensureTouchModeLocally(inTouchMode);
3433
Romain Guye55945e2013-04-04 15:26:04 -07003434 if (mAttachInfo.mHardwareRenderer != null && mSurface.isValid()){
Jeff Browna175a5b2012-02-15 19:18:31 -08003435 mFullRedrawNeeded = true;
Dianne Hackborn64825172011-03-02 21:32:58 -08003436 try {
Alan Viveretteccb11e12014-07-08 16:04:02 -07003437 final WindowManager.LayoutParams lp = mWindowAttributes;
Alan Viverette49a22e82014-07-12 20:01:27 -07003438 final Rect surfaceInsets = lp != null ? lp.surfaceInsets : null;
Romain Guy3696779b2013-01-28 14:04:07 -08003439 mAttachInfo.mHardwareRenderer.initializeIfNeeded(
Alan Viverette50210d92015-05-14 18:05:36 -07003440 mWidth, mHeight, mAttachInfo, mSurface, surfaceInsets);
Igor Murashkina86ab6402013-08-30 12:58:36 -07003441 } catch (OutOfResourcesException e) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08003442 Log.e(mTag, "OutOfResourcesException locking surface", e);
Jeff Browna175a5b2012-02-15 19:18:31 -08003443 try {
Jeff Brown98365d72012-08-19 20:30:52 -07003444 if (!mWindowSession.outOfMemory(mWindow)) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08003445 Slog.w(mTag, "No processes killed for memory; killing self");
Jeff Browna175a5b2012-02-15 19:18:31 -08003446 Process.killProcess(Process.myPid());
3447 }
3448 } catch (RemoteException ex) {
Dianne Hackborn64825172011-03-02 21:32:58 -08003449 }
Jeff Browna175a5b2012-02-15 19:18:31 -08003450 // Retry in a bit.
3451 sendMessageDelayed(obtainMessage(msg.what, msg.arg1, msg.arg2), 500);
3452 return;
Dianne Hackborn64825172011-03-02 21:32:58 -08003453 }
Dianne Hackborn64825172011-03-02 21:32:58 -08003454 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003455 }
Romain Guy8506ab42009-06-11 17:35:47 -07003456
Jeff Browna175a5b2012-02-15 19:18:31 -08003457 mLastWasImTarget = WindowManager.LayoutParams
3458 .mayUseInputMethod(mWindowAttributes.flags);
Romain Guy8506ab42009-06-11 17:35:47 -07003459
Jeff Browna175a5b2012-02-15 19:18:31 -08003460 InputMethodManager imm = InputMethodManager.peekInstance();
Yohei Yukawa5f059652015-05-14 22:16:41 -07003461 if (imm != null && mLastWasImTarget && !isInLocalFocusMode()) {
3462 imm.onPreWindowFocus(mView, hasWindowFocus);
3463 }
Jeff Browna175a5b2012-02-15 19:18:31 -08003464 if (mView != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08003465 mAttachInfo.mKeyDispatchState.reset();
3466 mView.dispatchWindowFocusChanged(hasWindowFocus);
Dianne Hackborn961cae92013-03-20 14:59:43 -07003467 mAttachInfo.mTreeObserver.dispatchOnWindowFocusChange(hasWindowFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003469
Jeff Browna175a5b2012-02-15 19:18:31 -08003470 // Note: must be done after the focus change callbacks,
3471 // so all of the view state is set up correctly.
3472 if (hasWindowFocus) {
keunyoung30f420f2013-08-02 14:23:10 -07003473 if (imm != null && mLastWasImTarget && !isInLocalFocusMode()) {
Yohei Yukawa5f059652015-05-14 22:16:41 -07003474 imm.onPostWindowFocus(mView, mView.findFocus(),
Jeff Browna175a5b2012-02-15 19:18:31 -08003475 mWindowAttributes.softInputMode,
3476 !mHasHadWindowFocus, mWindowAttributes.flags);
3477 }
3478 // Clear the forward bit. We can just do this directly, since
3479 // the window manager doesn't care about it.
3480 mWindowAttributes.softInputMode &=
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003481 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
Jeff Browna175a5b2012-02-15 19:18:31 -08003482 ((WindowManager.LayoutParams)mView.getLayoutParams())
3483 .softInputMode &=
3484 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
3485 mHasHadWindowFocus = true;
Jun Mukai347e5d42015-12-03 01:13:31 -08003486 } else if (mCapturingView != null) {
3487 releasePointerCapture(mCapturingView);
Jeff Browna175a5b2012-02-15 19:18:31 -08003488 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003489 }
Jeff Browna175a5b2012-02-15 19:18:31 -08003490 } break;
3491 case MSG_DIE:
3492 doDie();
3493 break;
keunyoung30f420f2013-08-02 14:23:10 -07003494 case MSG_DISPATCH_INPUT_EVENT: {
Jae Seo6a6059a2014-04-17 21:35:29 -07003495 SomeArgs args = (SomeArgs)msg.obj;
3496 InputEvent event = (InputEvent)args.arg1;
3497 InputEventReceiver receiver = (InputEventReceiver)args.arg2;
3498 enqueueInputEvent(event, receiver, 0, true);
3499 args.recycle();
Jeff Browna175a5b2012-02-15 19:18:31 -08003500 } break;
Michael Wright899d7052014-04-23 17:23:39 -07003501 case MSG_SYNTHESIZE_INPUT_EVENT: {
3502 InputEvent event = (InputEvent)msg.obj;
3503 enqueueInputEvent(event, null, QueuedInputEvent.FLAG_UNHANDLED, true);
3504 } break;
Jeff Browna175a5b2012-02-15 19:18:31 -08003505 case MSG_DISPATCH_KEY_FROM_IME: {
3506 if (LOCAL_LOGV) Log.v(
3507 TAG, "Dispatching key "
3508 + msg.obj + " from IME to " + mView);
3509 KeyEvent event = (KeyEvent)msg.obj;
3510 if ((event.getFlags()&KeyEvent.FLAG_FROM_SYSTEM) != 0) {
3511 // The IME is trying to say this event is from the
3512 // system! Bad bad bad!
3513 //noinspection UnusedAssignment
Michael Wright899d7052014-04-23 17:23:39 -07003514 event = KeyEvent.changeFlags(event, event.getFlags() &
3515 ~KeyEvent.FLAG_FROM_SYSTEM);
Jeff Browna175a5b2012-02-15 19:18:31 -08003516 }
3517 enqueueInputEvent(event, null, QueuedInputEvent.FLAG_DELIVER_POST_IME, true);
3518 } break;
3519 case MSG_FINISH_INPUT_CONNECTION: {
3520 InputMethodManager imm = InputMethodManager.peekInstance();
3521 if (imm != null) {
3522 imm.reportFinishInputConnection((InputConnection)msg.obj);
3523 }
3524 } break;
3525 case MSG_CHECK_FOCUS: {
3526 InputMethodManager imm = InputMethodManager.peekInstance();
3527 if (imm != null) {
3528 imm.checkFocus();
3529 }
3530 } break;
3531 case MSG_CLOSE_SYSTEM_DIALOGS: {
3532 if (mView != null) {
3533 mView.onCloseSystemDialogs((String)msg.obj);
3534 }
3535 } break;
3536 case MSG_DISPATCH_DRAG_EVENT:
3537 case MSG_DISPATCH_DRAG_LOCATION_EVENT: {
3538 DragEvent event = (DragEvent)msg.obj;
3539 event.mLocalState = mLocalDragState; // only present when this app called startDrag()
3540 handleDragEvent(event);
3541 } break;
3542 case MSG_DISPATCH_SYSTEM_UI_VISIBILITY: {
Romain Guyfbb93fa2012-12-03 18:50:35 -08003543 handleDispatchSystemUiVisibilityChanged((SystemUiVisibilityInfo) msg.obj);
Jeff Browna175a5b2012-02-15 19:18:31 -08003544 } break;
3545 case MSG_UPDATE_CONFIGURATION: {
3546 Configuration config = (Configuration)msg.obj;
3547 if (config.isOtherSeqNewer(mLastConfiguration)) {
3548 config = mLastConfiguration;
3549 }
3550 updateConfiguration(config, false);
3551 } break;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07003552 case MSG_CLEAR_ACCESSIBILITY_FOCUS_HOST: {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07003553 setAccessibilityFocus(null, null);
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07003554 } break;
Dianne Hackborna53de062012-05-08 18:53:51 -07003555 case MSG_INVALIDATE_WORLD: {
Romain Guyf84208f2012-09-13 22:50:18 -07003556 if (mView != null) {
3557 invalidateWorld(mView);
3558 }
Dianne Hackborna53de062012-05-08 18:53:51 -07003559 } break;
Craig Mautner9c795042014-10-28 19:59:59 -07003560 case MSG_DISPATCH_WINDOW_SHOWN: {
3561 handleDispatchWindowShown();
Clara Bayarri75e09792015-07-29 16:20:40 +01003562 } break;
3563 case MSG_REQUEST_KEYBOARD_SHORTCUTS: {
3564 IResultReceiver receiver = (IResultReceiver) msg.obj;
3565 handleRequestKeyboardShortcuts(receiver);
3566 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003568 }
3569 }
Romain Guy51e4d4d2012-03-15 18:30:47 -07003570
Jeff Browna175a5b2012-02-15 19:18:31 -08003571 final ViewRootHandler mHandler = new ViewRootHandler();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003573 /**
3574 * Something in the current window tells us we need to change the touch mode. For
3575 * example, we are not in touch mode, and the user touches the screen.
3576 *
3577 * If the touch mode has changed, tell the window manager, and handle it locally.
3578 *
3579 * @param inTouchMode Whether we want to be in touch mode.
3580 * @return True if the touch mode changed and focus changed was changed as a result
3581 */
3582 boolean ensureTouchMode(boolean inTouchMode) {
3583 if (DBG) Log.d("touchmode", "ensureTouchMode(" + inTouchMode + "), current "
3584 + "touch mode is " + mAttachInfo.mInTouchMode);
3585 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
3586
3587 // tell the window manager
3588 try {
keunyoung30f420f2013-08-02 14:23:10 -07003589 if (!isInLocalFocusMode()) {
3590 mWindowSession.setInTouchMode(inTouchMode);
3591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003592 } catch (RemoteException e) {
3593 throw new RuntimeException(e);
3594 }
3595
3596 // handle the change
Romain Guy2d4cff62010-04-09 15:39:00 -07003597 return ensureTouchModeLocally(inTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003598 }
3599
3600 /**
3601 * Ensure that the touch mode for this window is set, and if it is changing,
3602 * take the appropriate action.
3603 * @param inTouchMode Whether we want to be in touch mode.
3604 * @return True if the touch mode changed and focus changed was changed as a result
3605 */
Romain Guy2d4cff62010-04-09 15:39:00 -07003606 private boolean ensureTouchModeLocally(boolean inTouchMode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003607 if (DBG) Log.d("touchmode", "ensureTouchModeLocally(" + inTouchMode + "), current "
3608 + "touch mode is " + mAttachInfo.mInTouchMode);
3609
3610 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
3611
3612 mAttachInfo.mInTouchMode = inTouchMode;
3613 mAttachInfo.mTreeObserver.dispatchOnTouchModeChanged(inTouchMode);
3614
Romain Guy2d4cff62010-04-09 15:39:00 -07003615 return (inTouchMode) ? enterTouchMode() : leaveTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003616 }
3617
3618 private boolean enterTouchMode() {
Alan Viveretteed7821a2013-07-24 15:49:23 -07003619 if (mView != null && mView.hasFocus()) {
3620 // note: not relying on mFocusedView here because this could
3621 // be when the window is first being added, and mFocused isn't
3622 // set yet.
3623 final View focused = mView.findFocus();
3624 if (focused != null && !focused.isFocusableInTouchMode()) {
3625 final ViewGroup ancestorToTakeFocus = findAncestorToTakeFocusInTouchMode(focused);
3626 if (ancestorToTakeFocus != null) {
3627 // there is an ancestor that wants focus after its
3628 // descendants that is focusable in touch mode.. give it
3629 // focus
3630 return ancestorToTakeFocus.requestFocus();
3631 } else {
Alan Viverette973f3b42013-08-13 16:57:28 -07003632 // There's nothing to focus. Clear and propagate through the
3633 // hierarchy, but don't attempt to place new focus.
Alan Viverette223622a2013-12-17 13:29:02 -08003634 focused.clearFocusInternal(null, true, false);
Alan Viveretteed7821a2013-07-24 15:49:23 -07003635 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003636 }
3637 }
3638 }
3639 return false;
3640 }
3641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003642 /**
3643 * Find an ancestor of focused that wants focus after its descendants and is
3644 * focusable in touch mode.
3645 * @param focused The currently focused view.
3646 * @return An appropriate view, or null if no such view exists.
3647 */
Romain Guya998dff2012-03-23 18:58:36 -07003648 private static ViewGroup findAncestorToTakeFocusInTouchMode(View focused) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003649 ViewParent parent = focused.getParent();
3650 while (parent instanceof ViewGroup) {
3651 final ViewGroup vgParent = (ViewGroup) parent;
3652 if (vgParent.getDescendantFocusability() == ViewGroup.FOCUS_AFTER_DESCENDANTS
3653 && vgParent.isFocusableInTouchMode()) {
3654 return vgParent;
3655 }
3656 if (vgParent.isRootNamespace()) {
3657 return null;
3658 } else {
3659 parent = vgParent.getParent();
3660 }
3661 }
3662 return null;
3663 }
3664
3665 private boolean leaveTouchMode() {
3666 if (mView != null) {
3667 if (mView.hasFocus()) {
Svetoslav Ganov149567f2013-01-08 15:23:34 -08003668 View focusedView = mView.findFocus();
3669 if (!(focusedView instanceof ViewGroup)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670 // some view has focus, let it keep it
Svetoslav Ganovcf8a3b82012-04-30 16:49:59 -07003671 return false;
Svetoslav Ganov149567f2013-01-08 15:23:34 -08003672 } else if (((ViewGroup) focusedView).getDescendantFocusability() !=
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003673 ViewGroup.FOCUS_AFTER_DESCENDANTS) {
3674 // some view group has focus, and doesn't prefer its children
3675 // over itself for focus, so let them keep it.
Svetoslav Ganovcf8a3b82012-04-30 16:49:59 -07003676 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003677 }
3678 }
Svetoslav Ganovcf8a3b82012-04-30 16:49:59 -07003679
3680 // find the best view to give focus to in this brave new non-touch-mode
3681 // world
3682 final View focused = focusSearch(null, View.FOCUS_DOWN);
3683 if (focused != null) {
3684 return focused.requestFocus(View.FOCUS_DOWN);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685 }
3686 }
3687 return false;
3688 }
3689
Jeff Brownf9e989d2013-04-04 23:04:03 -07003690 /**
3691 * Base class for implementing a stage in the chain of responsibility
3692 * for processing input events.
3693 * <p>
3694 * Events are delivered to the stage by the {@link #deliver} method. The stage
3695 * then has the choice of finishing the event or forwarding it to the next stage.
3696 * </p>
3697 */
3698 abstract class InputStage {
3699 private final InputStage mNext;
3700
3701 protected static final int FORWARD = 0;
3702 protected static final int FINISH_HANDLED = 1;
3703 protected static final int FINISH_NOT_HANDLED = 2;
3704
3705 /**
3706 * Creates an input stage.
3707 * @param next The next stage to which events should be forwarded.
3708 */
3709 public InputStage(InputStage next) {
3710 mNext = next;
3711 }
3712
3713 /**
3714 * Delivers an event to be processed.
3715 */
3716 public final void deliver(QueuedInputEvent q) {
3717 if ((q.mFlags & QueuedInputEvent.FLAG_FINISHED) != 0) {
3718 forward(q);
Michael Wright17d28ca2013-10-31 17:47:45 -07003719 } else if (shouldDropInputEvent(q)) {
Jeff Brownf9e989d2013-04-04 23:04:03 -07003720 finish(q, false);
3721 } else {
3722 apply(q, onProcess(q));
3723 }
3724 }
3725
3726 /**
3727 * Marks the the input event as finished then forwards it to the next stage.
3728 */
3729 protected void finish(QueuedInputEvent q, boolean handled) {
3730 q.mFlags |= QueuedInputEvent.FLAG_FINISHED;
3731 if (handled) {
3732 q.mFlags |= QueuedInputEvent.FLAG_FINISHED_HANDLED;
3733 }
3734 forward(q);
3735 }
3736
3737 /**
3738 * Forwards the event to the next stage.
3739 */
3740 protected void forward(QueuedInputEvent q) {
3741 onDeliverToNext(q);
3742 }
3743
3744 /**
3745 * Applies a result code from {@link #onProcess} to the specified event.
3746 */
3747 protected void apply(QueuedInputEvent q, int result) {
3748 if (result == FORWARD) {
3749 forward(q);
3750 } else if (result == FINISH_HANDLED) {
3751 finish(q, true);
3752 } else if (result == FINISH_NOT_HANDLED) {
3753 finish(q, false);
3754 } else {
3755 throw new IllegalArgumentException("Invalid result: " + result);
3756 }
3757 }
3758
3759 /**
3760 * Called when an event is ready to be processed.
3761 * @return A result code indicating how the event was handled.
3762 */
3763 protected int onProcess(QueuedInputEvent q) {
3764 return FORWARD;
3765 }
3766
3767 /**
3768 * Called when an event is being delivered to the next stage.
3769 */
3770 protected void onDeliverToNext(QueuedInputEvent q) {
Michael Wright06a79252014-05-05 17:45:29 -07003771 if (DEBUG_INPUT_STAGES) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08003772 Log.v(mTag, "Done with " + getClass().getSimpleName() + ". " + q);
Michael Wright06a79252014-05-05 17:45:29 -07003773 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07003774 if (mNext != null) {
3775 mNext.deliver(q);
3776 } else {
3777 finishInputEvent(q);
3778 }
3779 }
Jeff Brown5182c782013-10-15 20:31:52 -07003780
Michael Wright17d28ca2013-10-31 17:47:45 -07003781 protected boolean shouldDropInputEvent(QueuedInputEvent q) {
3782 if (mView == null || !mAdded) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08003783 Slog.w(mTag, "Dropping event due to root view being removed: " + q.mEvent);
Michael Wright17d28ca2013-10-31 17:47:45 -07003784 return true;
George Mount41725de2015-04-09 08:23:05 -07003785 } else if ((!mAttachInfo.mHasWindowFocus
3786 && !q.mEvent.isFromSource(InputDevice.SOURCE_CLASS_POINTER)) || mStopped
Daniel Koulomzin087ae472015-12-16 17:52:25 -05003787 || mIsAmbientMode || (mPausedForTransition && !isBack(q.mEvent))) {
Wale Ogunwalec3672cd2014-11-05 15:17:35 -08003788 // This is a focus event and the window doesn't currently have input focus or
3789 // has stopped. This could be an event that came back from the previous stage
3790 // but the window has lost focus or stopped in the meantime.
3791 if (isTerminalInputEvent(q.mEvent)) {
3792 // Don't drop terminal input events, however mark them as canceled.
3793 q.mEvent.cancel();
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08003794 Slog.w(mTag, "Cancelling event due to no window focus: " + q.mEvent);
Wale Ogunwalec3672cd2014-11-05 15:17:35 -08003795 return false;
3796 }
3797
3798 // Drop non-terminal input events.
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08003799 Slog.w(mTag, "Dropping event due to no window focus: " + q.mEvent);
Michael Wright17d28ca2013-10-31 17:47:45 -07003800 return true;
3801 }
3802 return false;
3803 }
3804
Jeff Brown5182c782013-10-15 20:31:52 -07003805 void dump(String prefix, PrintWriter writer) {
3806 if (mNext != null) {
3807 mNext.dump(prefix, writer);
3808 }
3809 }
George Mount41725de2015-04-09 08:23:05 -07003810
3811 private boolean isBack(InputEvent event) {
3812 if (event instanceof KeyEvent) {
3813 return ((KeyEvent) event).getKeyCode() == KeyEvent.KEYCODE_BACK;
3814 } else {
3815 return false;
3816 }
3817 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07003818 }
3819
3820 /**
3821 * Base class for implementing an input pipeline stage that supports
3822 * asynchronous and out-of-order processing of input events.
3823 * <p>
3824 * In addition to what a normal input stage can do, an asynchronous
3825 * input stage may also defer an input event that has been delivered to it
3826 * and finish or forward it later.
3827 * </p>
3828 */
3829 abstract class AsyncInputStage extends InputStage {
3830 private final String mTraceCounter;
3831
3832 private QueuedInputEvent mQueueHead;
3833 private QueuedInputEvent mQueueTail;
3834 private int mQueueLength;
3835
3836 protected static final int DEFER = 3;
3837
3838 /**
3839 * Creates an asynchronous input stage.
3840 * @param next The next stage to which events should be forwarded.
3841 * @param traceCounter The name of a counter to record the size of
3842 * the queue of pending events.
3843 */
3844 public AsyncInputStage(InputStage next, String traceCounter) {
3845 super(next);
3846 mTraceCounter = traceCounter;
3847 }
3848
3849 /**
3850 * Marks the event as deferred, which is to say that it will be handled
3851 * asynchronously. The caller is responsible for calling {@link #forward}
3852 * or {@link #finish} later when it is done handling the event.
3853 */
3854 protected void defer(QueuedInputEvent q) {
3855 q.mFlags |= QueuedInputEvent.FLAG_DEFERRED;
3856 enqueue(q);
3857 }
3858
3859 @Override
3860 protected void forward(QueuedInputEvent q) {
3861 // Clear the deferred flag.
3862 q.mFlags &= ~QueuedInputEvent.FLAG_DEFERRED;
3863
3864 // Fast path if the queue is empty.
3865 QueuedInputEvent curr = mQueueHead;
3866 if (curr == null) {
3867 super.forward(q);
3868 return;
3869 }
3870
3871 // Determine whether the event must be serialized behind any others
3872 // before it can be delivered to the next stage. This is done because
3873 // deferred events might be handled out of order by the stage.
3874 final int deviceId = q.mEvent.getDeviceId();
3875 QueuedInputEvent prev = null;
3876 boolean blocked = false;
3877 while (curr != null && curr != q) {
3878 if (!blocked && deviceId == curr.mEvent.getDeviceId()) {
3879 blocked = true;
3880 }
3881 prev = curr;
3882 curr = curr.mNext;
3883 }
3884
3885 // If the event is blocked, then leave it in the queue to be delivered later.
3886 // Note that the event might not yet be in the queue if it was not previously
3887 // deferred so we will enqueue it if needed.
3888 if (blocked) {
3889 if (curr == null) {
3890 enqueue(q);
3891 }
3892 return;
3893 }
3894
3895 // The event is not blocked. Deliver it immediately.
3896 if (curr != null) {
3897 curr = curr.mNext;
3898 dequeue(q, prev);
3899 }
3900 super.forward(q);
3901
3902 // Dequeuing this event may have unblocked successors. Deliver them.
3903 while (curr != null) {
3904 if (deviceId == curr.mEvent.getDeviceId()) {
3905 if ((curr.mFlags & QueuedInputEvent.FLAG_DEFERRED) != 0) {
3906 break;
3907 }
3908 QueuedInputEvent next = curr.mNext;
3909 dequeue(curr, prev);
3910 super.forward(curr);
3911 curr = next;
3912 } else {
3913 prev = curr;
3914 curr = curr.mNext;
3915 }
3916 }
3917 }
3918
3919 @Override
3920 protected void apply(QueuedInputEvent q, int result) {
3921 if (result == DEFER) {
3922 defer(q);
3923 } else {
3924 super.apply(q, result);
3925 }
3926 }
3927
3928 private void enqueue(QueuedInputEvent q) {
3929 if (mQueueTail == null) {
3930 mQueueHead = q;
3931 mQueueTail = q;
3932 } else {
3933 mQueueTail.mNext = q;
3934 mQueueTail = q;
3935 }
3936
3937 mQueueLength += 1;
3938 Trace.traceCounter(Trace.TRACE_TAG_INPUT, mTraceCounter, mQueueLength);
3939 }
3940
3941 private void dequeue(QueuedInputEvent q, QueuedInputEvent prev) {
3942 if (prev == null) {
3943 mQueueHead = q.mNext;
3944 } else {
3945 prev.mNext = q.mNext;
3946 }
3947 if (mQueueTail == q) {
3948 mQueueTail = prev;
3949 }
3950 q.mNext = null;
3951
3952 mQueueLength -= 1;
3953 Trace.traceCounter(Trace.TRACE_TAG_INPUT, mTraceCounter, mQueueLength);
3954 }
Jeff Brown5182c782013-10-15 20:31:52 -07003955
3956 @Override
3957 void dump(String prefix, PrintWriter writer) {
3958 writer.print(prefix);
3959 writer.print(getClass().getName());
3960 writer.print(": mQueueLength=");
3961 writer.println(mQueueLength);
3962
3963 super.dump(prefix, writer);
3964 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07003965 }
3966
3967 /**
3968 * Delivers pre-ime input events to a native activity.
3969 * Does not support pointer events.
3970 */
Michael Wrighta44dd262013-04-10 21:12:00 -07003971 final class NativePreImeInputStage extends AsyncInputStage
3972 implements InputQueue.FinishedInputEventCallback {
Jeff Brownf9e989d2013-04-04 23:04:03 -07003973 public NativePreImeInputStage(InputStage next, String traceCounter) {
3974 super(next, traceCounter);
3975 }
3976
3977 @Override
3978 protected int onProcess(QueuedInputEvent q) {
Michael Wrighta44dd262013-04-10 21:12:00 -07003979 if (mInputQueue != null && q.mEvent instanceof KeyEvent) {
3980 mInputQueue.sendInputEvent(q.mEvent, q, true, this);
3981 return DEFER;
3982 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07003983 return FORWARD;
3984 }
Michael Wrighta44dd262013-04-10 21:12:00 -07003985
3986 @Override
3987 public void onFinishedInputEvent(Object token, boolean handled) {
3988 QueuedInputEvent q = (QueuedInputEvent)token;
3989 if (handled) {
3990 finish(q, true);
3991 return;
3992 }
3993 forward(q);
3994 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07003995 }
3996
3997 /**
3998 * Delivers pre-ime input events to the view hierarchy.
3999 * Does not support pointer events.
4000 */
4001 final class ViewPreImeInputStage extends InputStage {
4002 public ViewPreImeInputStage(InputStage next) {
4003 super(next);
4004 }
4005
4006 @Override
4007 protected int onProcess(QueuedInputEvent q) {
Jeff Brown481c1572012-03-09 14:41:15 -08004008 if (q.mEvent instanceof KeyEvent) {
Jeff Brownf9e989d2013-04-04 23:04:03 -07004009 return processKeyEvent(q);
4010 }
4011 return FORWARD;
4012 }
4013
4014 private int processKeyEvent(QueuedInputEvent q) {
4015 final KeyEvent event = (KeyEvent)q.mEvent;
4016 if (mView.dispatchKeyEventPreIme(event)) {
4017 return FINISH_HANDLED;
4018 }
4019 return FORWARD;
4020 }
4021 }
4022
4023 /**
4024 * Delivers input events to the ime.
4025 * Does not support pointer events.
4026 */
4027 final class ImeInputStage extends AsyncInputStage
4028 implements InputMethodManager.FinishedInputEventCallback {
4029 public ImeInputStage(InputStage next, String traceCounter) {
4030 super(next, traceCounter);
4031 }
4032
4033 @Override
4034 protected int onProcess(QueuedInputEvent q) {
keunyoung30f420f2013-08-02 14:23:10 -07004035 if (mLastWasImTarget && !isInLocalFocusMode()) {
Jeff Brownf9e989d2013-04-04 23:04:03 -07004036 InputMethodManager imm = InputMethodManager.peekInstance();
4037 if (imm != null) {
4038 final InputEvent event = q.mEvent;
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08004039 if (DEBUG_IMF) Log.v(mTag, "Sending input event to IME: " + event);
Jeff Brownf9e989d2013-04-04 23:04:03 -07004040 int result = imm.dispatchInputEvent(event, q, this, mHandler);
4041 if (result == InputMethodManager.DISPATCH_HANDLED) {
4042 return FINISH_HANDLED;
4043 } else if (result == InputMethodManager.DISPATCH_NOT_HANDLED) {
Adam Lesinskic0f6eed2013-10-28 16:03:19 -07004044 // The IME could not handle it, so skip along to the next InputStage
4045 return FORWARD;
Jeff Brownf9e989d2013-04-04 23:04:03 -07004046 } else {
4047 return DEFER; // callback will be invoked later
4048 }
4049 }
4050 }
4051 return FORWARD;
4052 }
4053
4054 @Override
4055 public void onFinishedInputEvent(Object token, boolean handled) {
4056 QueuedInputEvent q = (QueuedInputEvent)token;
4057 if (handled) {
4058 finish(q, true);
4059 return;
4060 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07004061 forward(q);
4062 }
4063 }
4064
4065 /**
4066 * Performs early processing of post-ime input events.
4067 */
4068 final class EarlyPostImeInputStage extends InputStage {
4069 public EarlyPostImeInputStage(InputStage next) {
4070 super(next);
4071 }
4072
4073 @Override
4074 protected int onProcess(QueuedInputEvent q) {
4075 if (q.mEvent instanceof KeyEvent) {
4076 return processKeyEvent(q);
Jeff Brown4952dfd2011-11-30 19:23:22 -08004077 } else {
Jeff Brown481c1572012-03-09 14:41:15 -08004078 final int source = q.mEvent.getSource();
4079 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
Jeff Brownf9e989d2013-04-04 23:04:03 -07004080 return processPointerEvent(q);
Jeff Brown481c1572012-03-09 14:41:15 -08004081 }
Jeff Brown4952dfd2011-11-30 19:23:22 -08004082 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07004083 return FORWARD;
4084 }
4085
4086 private int processKeyEvent(QueuedInputEvent q) {
4087 final KeyEvent event = (KeyEvent)q.mEvent;
4088
4089 // If the key's purpose is to exit touch mode then we consume it
4090 // and consider it handled.
4091 if (checkForLeavingTouchModeAndConsume(event)) {
4092 return FINISH_HANDLED;
4093 }
4094
4095 // Make sure the fallback event policy sees all keys that will be
4096 // delivered to the view hierarchy.
4097 mFallbackEventHandler.preDispatchKeyEvent(event);
4098 return FORWARD;
4099 }
4100
4101 private int processPointerEvent(QueuedInputEvent q) {
4102 final MotionEvent event = (MotionEvent)q.mEvent;
4103
4104 // Translate the pointer event for compatibility, if needed.
4105 if (mTranslator != null) {
4106 mTranslator.translateEventInScreenToAppWindow(event);
4107 }
4108
4109 // Enter touch mode on down or scroll.
4110 final int action = event.getAction();
4111 if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_SCROLL) {
4112 ensureTouchMode(true);
4113 }
4114
4115 // Offset the scroll position.
4116 if (mCurScrollY != 0) {
4117 event.offsetLocation(0, mCurScrollY);
4118 }
4119
4120 // Remember the touch position for possible drag-initiation.
4121 if (event.isTouchEvent()) {
4122 mLastTouchPoint.x = event.getRawX();
4123 mLastTouchPoint.y = event.getRawY();
4124 }
4125 return FORWARD;
Jeff Brown4952dfd2011-11-30 19:23:22 -08004126 }
4127 }
4128
Jeff Brownf9e989d2013-04-04 23:04:03 -07004129 /**
4130 * Delivers post-ime input events to a native activity.
4131 */
Michael Wrighta44dd262013-04-10 21:12:00 -07004132 final class NativePostImeInputStage extends AsyncInputStage
4133 implements InputQueue.FinishedInputEventCallback {
Jeff Brownf9e989d2013-04-04 23:04:03 -07004134 public NativePostImeInputStage(InputStage next, String traceCounter) {
4135 super(next, traceCounter);
4136 }
4137
4138 @Override
4139 protected int onProcess(QueuedInputEvent q) {
Michael Wrighta44dd262013-04-10 21:12:00 -07004140 if (mInputQueue != null) {
4141 mInputQueue.sendInputEvent(q.mEvent, q, false, this);
4142 return DEFER;
4143 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07004144 return FORWARD;
4145 }
Michael Wrighta44dd262013-04-10 21:12:00 -07004146
4147 @Override
4148 public void onFinishedInputEvent(Object token, boolean handled) {
4149 QueuedInputEvent q = (QueuedInputEvent)token;
4150 if (handled) {
4151 finish(q, true);
4152 return;
4153 }
4154 forward(q);
4155 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07004156 }
4157
4158 /**
4159 * Delivers post-ime input events to the view hierarchy.
4160 */
4161 final class ViewPostImeInputStage extends InputStage {
4162 public ViewPostImeInputStage(InputStage next) {
4163 super(next);
4164 }
4165
4166 @Override
4167 protected int onProcess(QueuedInputEvent q) {
Jeff Brown29c0ed22013-01-14 13:50:37 -08004168 if (q.mEvent instanceof KeyEvent) {
Jeff Brownf9e989d2013-04-04 23:04:03 -07004169 return processKeyEvent(q);
Jeff Brown29c0ed22013-01-14 13:50:37 -08004170 } else {
4171 final int source = q.mEvent.getSource();
Jeff Brownf9e989d2013-04-04 23:04:03 -07004172 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
4173 return processPointerEvent(q);
4174 } else if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) {
4175 return processTrackballEvent(q);
Jeff Brown29c0ed22013-01-14 13:50:37 -08004176 } else {
Jeff Brownf9e989d2013-04-04 23:04:03 -07004177 return processGenericMotionEvent(q);
Jeff Brown29c0ed22013-01-14 13:50:37 -08004178 }
4179 }
Jeff Brown29c0ed22013-01-14 13:50:37 -08004180 }
Jeff Brown29c0ed22013-01-14 13:50:37 -08004181
Michael Wright9d744c72014-02-18 21:27:42 -08004182 @Override
4183 protected void onDeliverToNext(QueuedInputEvent q) {
4184 if (mUnbufferedInputDispatch
4185 && q.mEvent instanceof MotionEvent
4186 && ((MotionEvent)q.mEvent).isTouchEvent()
4187 && isTerminalInputEvent(q.mEvent)) {
4188 mUnbufferedInputDispatch = false;
4189 scheduleConsumeBatchedInput();
4190 }
4191 super.onDeliverToNext(q);
4192 }
4193
Jeff Brownf9e989d2013-04-04 23:04:03 -07004194 private int processKeyEvent(QueuedInputEvent q) {
4195 final KeyEvent event = (KeyEvent)q.mEvent;
4196
4197 // Deliver the key to the view hierarchy.
4198 if (mView.dispatchKeyEvent(event)) {
4199 return FINISH_HANDLED;
Jeff Brown21bc5c92011-02-28 18:27:14 -08004200 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07004201
Michael Wright17d28ca2013-10-31 17:47:45 -07004202 if (shouldDropInputEvent(q)) {
4203 return FINISH_NOT_HANDLED;
4204 }
4205
Jeff Brownf9e989d2013-04-04 23:04:03 -07004206 // If the Control modifier is held, try to interpret the key as a shortcut.
4207 if (event.getAction() == KeyEvent.ACTION_DOWN
4208 && event.isCtrlPressed()
4209 && event.getRepeatCount() == 0
4210 && !KeyEvent.isModifierKey(event.getKeyCode())) {
4211 if (mView.dispatchKeyShortcutEvent(event)) {
4212 return FINISH_HANDLED;
4213 }
Michael Wright17d28ca2013-10-31 17:47:45 -07004214 if (shouldDropInputEvent(q)) {
4215 return FINISH_NOT_HANDLED;
4216 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07004217 }
4218
4219 // Apply the fallback event policy.
4220 if (mFallbackEventHandler.dispatchKeyEvent(event)) {
4221 return FINISH_HANDLED;
4222 }
Michael Wright17d28ca2013-10-31 17:47:45 -07004223 if (shouldDropInputEvent(q)) {
4224 return FINISH_NOT_HANDLED;
4225 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07004226
4227 // Handle automatic focus changes.
4228 if (event.getAction() == KeyEvent.ACTION_DOWN) {
4229 int direction = 0;
4230 switch (event.getKeyCode()) {
4231 case KeyEvent.KEYCODE_DPAD_LEFT:
4232 if (event.hasNoModifiers()) {
4233 direction = View.FOCUS_LEFT;
4234 }
4235 break;
4236 case KeyEvent.KEYCODE_DPAD_RIGHT:
4237 if (event.hasNoModifiers()) {
4238 direction = View.FOCUS_RIGHT;
4239 }
4240 break;
4241 case KeyEvent.KEYCODE_DPAD_UP:
4242 if (event.hasNoModifiers()) {
4243 direction = View.FOCUS_UP;
4244 }
4245 break;
4246 case KeyEvent.KEYCODE_DPAD_DOWN:
4247 if (event.hasNoModifiers()) {
4248 direction = View.FOCUS_DOWN;
4249 }
4250 break;
4251 case KeyEvent.KEYCODE_TAB:
4252 if (event.hasNoModifiers()) {
4253 direction = View.FOCUS_FORWARD;
4254 } else if (event.hasModifiers(KeyEvent.META_SHIFT_ON)) {
4255 direction = View.FOCUS_BACKWARD;
4256 }
4257 break;
4258 }
4259 if (direction != 0) {
4260 View focused = mView.findFocus();
4261 if (focused != null) {
4262 View v = focused.focusSearch(direction);
4263 if (v != null && v != focused) {
4264 // do the math the get the interesting rect
4265 // of previous focused into the coord system of
4266 // newly focused view
4267 focused.getFocusedRect(mTempRect);
4268 if (mView instanceof ViewGroup) {
4269 ((ViewGroup) mView).offsetDescendantRectToMyCoords(
4270 focused, mTempRect);
4271 ((ViewGroup) mView).offsetRectIntoDescendantCoords(
4272 v, mTempRect);
4273 }
4274 if (v.requestFocus(direction, mTempRect)) {
4275 playSoundEffect(SoundEffectConstants
4276 .getContantForFocusDirection(direction));
4277 return FINISH_HANDLED;
4278 }
4279 }
4280
4281 // Give the focused view a last chance to handle the dpad key.
4282 if (mView.dispatchUnhandledMove(focused, direction)) {
4283 return FINISH_HANDLED;
4284 }
4285 } else {
4286 // find the best view to give focus to in this non-touch-mode with no-focus
4287 View v = focusSearch(null, direction);
4288 if (v != null && v.requestFocus(direction)) {
4289 return FINISH_HANDLED;
4290 }
4291 }
4292 }
4293 }
4294 return FORWARD;
Jeff Brown21bc5c92011-02-28 18:27:14 -08004295 }
4296
Jeff Brownf9e989d2013-04-04 23:04:03 -07004297 private int processPointerEvent(QueuedInputEvent q) {
4298 final MotionEvent event = (MotionEvent)q.mEvent;
4299
Jun Mukai1db53972015-09-11 18:08:31 -07004300 if (event.getPointerCount() == 1
4301 && event.isFromSource(InputDevice.SOURCE_MOUSE)) {
4302 if (event.getActionMasked() == MotionEvent.ACTION_HOVER_ENTER
4303 || event.getActionMasked() == MotionEvent.ACTION_HOVER_EXIT) {
4304 // Other apps or the window manager may change the icon shape outside of
4305 // this app, therefore the icon shape has to be reset on enter/exit event.
4306 mPointerIconShape = PointerIcon.STYLE_NOT_SPECIFIED;
4307 }
4308
4309 final float x = event.getX();
4310 final float y = event.getY();
Jun Mukai76c4a122015-10-19 15:51:28 -07004311 if (event.getActionMasked() != MotionEvent.ACTION_HOVER_EXIT
4312 && x >= 0 && x < mView.getWidth() && y >= 0 && y < mView.getHeight()) {
Jun Mukaid4eaef72015-10-30 15:54:33 -07004313 PointerIcon pointerIcon = mView.getPointerIcon(event, x, y);
4314 int pointerShape = (pointerIcon != null) ?
4315 pointerIcon.getStyle() : PointerIcon.STYLE_DEFAULT;
Jun Mukai1db53972015-09-11 18:08:31 -07004316
Jun Mukaid4eaef72015-10-30 15:54:33 -07004317 final InputDevice inputDevice = event.getDevice();
4318 if (inputDevice != null) {
4319 if (mPointerIconShape != pointerShape) {
4320 mPointerIconShape = pointerShape;
4321 if (mPointerIconShape != PointerIcon.STYLE_CUSTOM) {
4322 mCustomPointerIcon = null;
4323 inputDevice.setPointerShape(pointerShape);
4324 }
4325 }
4326 if (mPointerIconShape == PointerIcon.STYLE_CUSTOM &&
4327 !pointerIcon.equals(mCustomPointerIcon)) {
4328 mCustomPointerIcon = pointerIcon;
4329 inputDevice.setCustomPointerIcon(mCustomPointerIcon);
Keisuke Kuroyanagi9a4450b2015-10-27 17:29:53 +09004330 }
Jun Mukai1db53972015-09-11 18:08:31 -07004331 }
Jun Mukai41aab002015-10-01 23:18:47 -07004332 } else if (event.getActionMasked() == MotionEvent.ACTION_HOVER_MOVE) {
4333 mPointerIconShape = PointerIcon.STYLE_NOT_SPECIFIED;
Jun Mukai1db53972015-09-11 18:08:31 -07004334 }
4335 }
4336
Michael Wright9d744c72014-02-18 21:27:42 -08004337 mAttachInfo.mUnbufferedDispatchRequested = false;
Jun Mukai347e5d42015-12-03 01:13:31 -08004338 final View eventTarget =
4339 (event.isFromSource(InputDevice.SOURCE_MOUSE) && mCapturingView != null) ?
4340 mCapturingView : mView;
4341 boolean handled = eventTarget.dispatchPointerEvent(event);
Michael Wright9d744c72014-02-18 21:27:42 -08004342 if (mAttachInfo.mUnbufferedDispatchRequested && !mUnbufferedInputDispatch) {
4343 mUnbufferedInputDispatch = true;
4344 if (mConsumeBatchedInputScheduled) {
4345 scheduleConsumeBatchedInputImmediately();
4346 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07004347 }
Michael Wright9d744c72014-02-18 21:27:42 -08004348 return handled ? FINISH_HANDLED : FORWARD;
Jeff Brown3915bb82010-11-05 15:02:16 -07004349 }
4350
Jeff Brownf9e989d2013-04-04 23:04:03 -07004351 private int processTrackballEvent(QueuedInputEvent q) {
4352 final MotionEvent event = (MotionEvent)q.mEvent;
4353
4354 if (mView.dispatchTrackballEvent(event)) {
4355 return FINISH_HANDLED;
4356 }
4357 return FORWARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004358 }
4359
Jeff Brownf9e989d2013-04-04 23:04:03 -07004360 private int processGenericMotionEvent(QueuedInputEvent q) {
4361 final MotionEvent event = (MotionEvent)q.mEvent;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07004362
Jeff Brownf9e989d2013-04-04 23:04:03 -07004363 // Deliver the event to the view.
4364 if (mView.dispatchGenericMotionEvent(event)) {
4365 return FINISH_HANDLED;
4366 }
4367 return FORWARD;
Jeff Brown3915bb82010-11-05 15:02:16 -07004368 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07004369 }
4370
Jeff Brownf9e989d2013-04-04 23:04:03 -07004371 /**
Jeff Brown678a1252013-04-09 17:46:25 -07004372 * Performs synthesis of new input events from unhandled input events.
Jeff Brownf9e989d2013-04-04 23:04:03 -07004373 */
4374 final class SyntheticInputStage extends InputStage {
Jeff Brown678a1252013-04-09 17:46:25 -07004375 private final SyntheticTrackballHandler mTrackball = new SyntheticTrackballHandler();
4376 private final SyntheticJoystickHandler mJoystick = new SyntheticJoystickHandler();
4377 private final SyntheticTouchNavigationHandler mTouchNavigation =
4378 new SyntheticTouchNavigationHandler();
Michael Wright899d7052014-04-23 17:23:39 -07004379 private final SyntheticKeyboardHandler mKeyboard = new SyntheticKeyboardHandler();
Jeff Brownf9e989d2013-04-04 23:04:03 -07004380
4381 public SyntheticInputStage() {
4382 super(null);
Jeff Brown21bc5c92011-02-28 18:27:14 -08004383 }
4384
Jeff Brownf9e989d2013-04-04 23:04:03 -07004385 @Override
4386 protected int onProcess(QueuedInputEvent q) {
4387 q.mFlags |= QueuedInputEvent.FLAG_RESYNTHESIZED;
4388 if (q.mEvent instanceof MotionEvent) {
Jeff Brown678a1252013-04-09 17:46:25 -07004389 final MotionEvent event = (MotionEvent)q.mEvent;
4390 final int source = event.getSource();
Jeff Brownf9e989d2013-04-04 23:04:03 -07004391 if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) {
Jeff Brown678a1252013-04-09 17:46:25 -07004392 mTrackball.process(event);
4393 return FINISH_HANDLED;
Jeff Brownf9e989d2013-04-04 23:04:03 -07004394 } else if ((source & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown678a1252013-04-09 17:46:25 -07004395 mJoystick.process(event);
4396 return FINISH_HANDLED;
Jeff Brownf9e989d2013-04-04 23:04:03 -07004397 } else if ((source & InputDevice.SOURCE_TOUCH_NAVIGATION)
4398 == InputDevice.SOURCE_TOUCH_NAVIGATION) {
Jeff Brown678a1252013-04-09 17:46:25 -07004399 mTouchNavigation.process(event);
4400 return FINISH_HANDLED;
Jeff Brownf9e989d2013-04-04 23:04:03 -07004401 }
Michael Wright899d7052014-04-23 17:23:39 -07004402 } else if ((q.mFlags & QueuedInputEvent.FLAG_UNHANDLED) != 0) {
4403 mKeyboard.process((KeyEvent)q.mEvent);
4404 return FINISH_HANDLED;
Jeff Brownf9e989d2013-04-04 23:04:03 -07004405 }
Michael Wright899d7052014-04-23 17:23:39 -07004406
Jeff Brownf9e989d2013-04-04 23:04:03 -07004407 return FORWARD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 }
4409
Jeff Brownf9e989d2013-04-04 23:04:03 -07004410 @Override
4411 protected void onDeliverToNext(QueuedInputEvent q) {
4412 if ((q.mFlags & QueuedInputEvent.FLAG_RESYNTHESIZED) == 0) {
4413 // Cancel related synthetic events if any prior stage has handled the event.
4414 if (q.mEvent instanceof MotionEvent) {
Jeff Brown678a1252013-04-09 17:46:25 -07004415 final MotionEvent event = (MotionEvent)q.mEvent;
4416 final int source = event.getSource();
Jeff Brownf9e989d2013-04-04 23:04:03 -07004417 if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) {
Jeff Brown678a1252013-04-09 17:46:25 -07004418 mTrackball.cancel(event);
Jeff Brownf9e989d2013-04-04 23:04:03 -07004419 } else if ((source & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown678a1252013-04-09 17:46:25 -07004420 mJoystick.cancel(event);
Jeff Brownf9e989d2013-04-04 23:04:03 -07004421 } else if ((source & InputDevice.SOURCE_TOUCH_NAVIGATION)
4422 == InputDevice.SOURCE_TOUCH_NAVIGATION) {
Jeff Brown678a1252013-04-09 17:46:25 -07004423 mTouchNavigation.cancel(event);
Jeff Brownf9e989d2013-04-04 23:04:03 -07004424 }
4425 }
4426 }
4427 super.onDeliverToNext(q);
4428 }
Jeff Brown678a1252013-04-09 17:46:25 -07004429 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07004430
Jeff Brown678a1252013-04-09 17:46:25 -07004431 /**
4432 * Creates dpad events from unhandled trackball movements.
4433 */
4434 final class SyntheticTrackballHandler {
4435 private final TrackballAxis mX = new TrackballAxis();
4436 private final TrackballAxis mY = new TrackballAxis();
4437 private long mLastTime;
Jeff Brownf9e989d2013-04-04 23:04:03 -07004438
Jeff Brown678a1252013-04-09 17:46:25 -07004439 public void process(MotionEvent event) {
Jeff Brownf9e989d2013-04-04 23:04:03 -07004440 // Translate the trackball event into DPAD keys and try to deliver those.
Jeff Brownf9e989d2013-04-04 23:04:03 -07004441 long curTime = SystemClock.uptimeMillis();
Jeff Brown678a1252013-04-09 17:46:25 -07004442 if ((mLastTime + MAX_TRACKBALL_DELAY) < curTime) {
Jeff Brownf9e989d2013-04-04 23:04:03 -07004443 // It has been too long since the last movement,
4444 // so restart at the beginning.
Jeff Brown678a1252013-04-09 17:46:25 -07004445 mX.reset(0);
4446 mY.reset(0);
4447 mLastTime = curTime;
Jeff Brownf9e989d2013-04-04 23:04:03 -07004448 }
4449
4450 final int action = event.getAction();
4451 final int metaState = event.getMetaState();
4452 switch (action) {
4453 case MotionEvent.ACTION_DOWN:
Jeff Brown678a1252013-04-09 17:46:25 -07004454 mX.reset(2);
4455 mY.reset(2);
Jeff Brownf9e989d2013-04-04 23:04:03 -07004456 enqueueInputEvent(new KeyEvent(curTime, curTime,
4457 KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER, 0, metaState,
4458 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
4459 InputDevice.SOURCE_KEYBOARD));
4460 break;
4461 case MotionEvent.ACTION_UP:
Jeff Brown678a1252013-04-09 17:46:25 -07004462 mX.reset(2);
4463 mY.reset(2);
Jeff Brownf9e989d2013-04-04 23:04:03 -07004464 enqueueInputEvent(new KeyEvent(curTime, curTime,
4465 KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER, 0, metaState,
4466 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
4467 InputDevice.SOURCE_KEYBOARD));
4468 break;
4469 }
4470
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08004471 if (DEBUG_TRACKBALL) Log.v(mTag, "TB X=" + mX.position + " step="
Jeff Brown678a1252013-04-09 17:46:25 -07004472 + mX.step + " dir=" + mX.dir + " acc=" + mX.acceleration
Jeff Brownf9e989d2013-04-04 23:04:03 -07004473 + " move=" + event.getX()
Jeff Brown678a1252013-04-09 17:46:25 -07004474 + " / Y=" + mY.position + " step="
4475 + mY.step + " dir=" + mY.dir + " acc=" + mY.acceleration
Jeff Brownf9e989d2013-04-04 23:04:03 -07004476 + " move=" + event.getY());
Jeff Brown678a1252013-04-09 17:46:25 -07004477 final float xOff = mX.collect(event.getX(), event.getEventTime(), "X");
4478 final float yOff = mY.collect(event.getY(), event.getEventTime(), "Y");
Jeff Brownf9e989d2013-04-04 23:04:03 -07004479
4480 // Generate DPAD events based on the trackball movement.
4481 // We pick the axis that has moved the most as the direction of
4482 // the DPAD. When we generate DPAD events for one axis, then the
4483 // other axis is reset -- we don't want to perform DPAD jumps due
4484 // to slight movements in the trackball when making major movements
4485 // along the other axis.
4486 int keycode = 0;
4487 int movement = 0;
4488 float accel = 1;
4489 if (xOff > yOff) {
Jeff Brown678a1252013-04-09 17:46:25 -07004490 movement = mX.generate();
Jeff Brownf9e989d2013-04-04 23:04:03 -07004491 if (movement != 0) {
4492 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_RIGHT
4493 : KeyEvent.KEYCODE_DPAD_LEFT;
Jeff Brown678a1252013-04-09 17:46:25 -07004494 accel = mX.acceleration;
4495 mY.reset(2);
Jeff Brownf9e989d2013-04-04 23:04:03 -07004496 }
4497 } else if (yOff > 0) {
Jeff Brown678a1252013-04-09 17:46:25 -07004498 movement = mY.generate();
Jeff Brownf9e989d2013-04-04 23:04:03 -07004499 if (movement != 0) {
4500 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_DOWN
4501 : KeyEvent.KEYCODE_DPAD_UP;
Jeff Brown678a1252013-04-09 17:46:25 -07004502 accel = mY.acceleration;
4503 mX.reset(2);
Jeff Brownf9e989d2013-04-04 23:04:03 -07004504 }
4505 }
4506
4507 if (keycode != 0) {
4508 if (movement < 0) movement = -movement;
4509 int accelMovement = (int)(movement * accel);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08004510 if (DEBUG_TRACKBALL) Log.v(mTag, "Move: movement=" + movement
Jeff Brownf9e989d2013-04-04 23:04:03 -07004511 + " accelMovement=" + accelMovement
4512 + " accel=" + accel);
4513 if (accelMovement > movement) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08004514 if (DEBUG_TRACKBALL) Log.v(mTag, "Delivering fake DPAD: "
Jeff Brownf9e989d2013-04-04 23:04:03 -07004515 + keycode);
4516 movement--;
4517 int repeatCount = accelMovement - movement;
4518 enqueueInputEvent(new KeyEvent(curTime, curTime,
4519 KeyEvent.ACTION_MULTIPLE, keycode, repeatCount, metaState,
4520 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
4521 InputDevice.SOURCE_KEYBOARD));
4522 }
4523 while (movement > 0) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08004524 if (DEBUG_TRACKBALL) Log.v(mTag, "Delivering fake DPAD: "
Jeff Brownf9e989d2013-04-04 23:04:03 -07004525 + keycode);
4526 movement--;
4527 curTime = SystemClock.uptimeMillis();
4528 enqueueInputEvent(new KeyEvent(curTime, curTime,
4529 KeyEvent.ACTION_DOWN, keycode, 0, metaState,
4530 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
4531 InputDevice.SOURCE_KEYBOARD));
4532 enqueueInputEvent(new KeyEvent(curTime, curTime,
4533 KeyEvent.ACTION_UP, keycode, 0, metaState,
4534 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
4535 InputDevice.SOURCE_KEYBOARD));
4536 }
Jeff Brown678a1252013-04-09 17:46:25 -07004537 mLastTime = curTime;
Jeff Brownf9e989d2013-04-04 23:04:03 -07004538 }
Jeff Brownf9e989d2013-04-04 23:04:03 -07004539 }
4540
Jeff Brown678a1252013-04-09 17:46:25 -07004541 public void cancel(MotionEvent event) {
4542 mLastTime = Integer.MIN_VALUE;
Jeff Brown3915bb82010-11-05 15:02:16 -07004543
Jeff Brownf9e989d2013-04-04 23:04:03 -07004544 // If we reach this, we consumed a trackball event.
4545 // Because we will not translate the trackball event into a key event,
4546 // touch mode will not exit, so we exit touch mode here.
4547 if (mView != null && mAdded) {
4548 ensureTouchMode(false);
Jeff Brown00fa7bd2010-07-02 15:37:36 -07004549 }
4550 }
Jeff Brown678a1252013-04-09 17:46:25 -07004551 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004552
Jeff Brown678a1252013-04-09 17:46:25 -07004553 /**
4554 * Maintains state information for a single trackball axis, generating
4555 * discrete (DPAD) movements based on raw trackball motion.
4556 */
4557 static final class TrackballAxis {
4558 /**
4559 * The maximum amount of acceleration we will apply.
4560 */
4561 static final float MAX_ACCELERATION = 20;
4562
4563 /**
4564 * The maximum amount of time (in milliseconds) between events in order
4565 * for us to consider the user to be doing fast trackball movements,
4566 * and thus apply an acceleration.
4567 */
4568 static final long FAST_MOVE_TIME = 150;
4569
4570 /**
4571 * Scaling factor to the time (in milliseconds) between events to how
4572 * much to multiple/divide the current acceleration. When movement
4573 * is < FAST_MOVE_TIME this multiplies the acceleration; when >
4574 * FAST_MOVE_TIME it divides it.
4575 */
4576 static final float ACCEL_MOVE_SCALING_FACTOR = (1.0f/40);
4577
4578 static final float FIRST_MOVEMENT_THRESHOLD = 0.5f;
4579 static final float SECOND_CUMULATIVE_MOVEMENT_THRESHOLD = 2.0f;
4580 static final float SUBSEQUENT_INCREMENTAL_MOVEMENT_THRESHOLD = 1.0f;
4581
4582 float position;
4583 float acceleration = 1;
4584 long lastMoveTime = 0;
4585 int step;
4586 int dir;
4587 int nonAccelMovement;
4588
4589 void reset(int _step) {
4590 position = 0;
4591 acceleration = 1;
4592 lastMoveTime = 0;
4593 step = _step;
4594 dir = 0;
Jeff Brown6f2fba42011-02-19 01:08:02 -08004595 }
4596
Jeff Brown678a1252013-04-09 17:46:25 -07004597 /**
4598 * Add trackball movement into the state. If the direction of movement
4599 * has been reversed, the state is reset before adding the
4600 * movement (so that you don't have to compensate for any previously
4601 * collected movement before see the result of the movement in the
4602 * new direction).
4603 *
4604 * @return Returns the absolute value of the amount of movement
4605 * collected so far.
4606 */
4607 float collect(float off, long time, String axis) {
4608 long normTime;
4609 if (off > 0) {
4610 normTime = (long)(off * FAST_MOVE_TIME);
4611 if (dir < 0) {
4612 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to positive!");
4613 position = 0;
4614 step = 0;
4615 acceleration = 1;
4616 lastMoveTime = 0;
4617 }
4618 dir = 1;
4619 } else if (off < 0) {
4620 normTime = (long)((-off) * FAST_MOVE_TIME);
4621 if (dir > 0) {
4622 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to negative!");
4623 position = 0;
4624 step = 0;
4625 acceleration = 1;
4626 lastMoveTime = 0;
4627 }
4628 dir = -1;
4629 } else {
4630 normTime = 0;
4631 }
4632
4633 // The number of milliseconds between each movement that is
4634 // considered "normal" and will not result in any acceleration
4635 // or deceleration, scaled by the offset we have here.
4636 if (normTime > 0) {
4637 long delta = time - lastMoveTime;
4638 lastMoveTime = time;
4639 float acc = acceleration;
4640 if (delta < normTime) {
4641 // The user is scrolling rapidly, so increase acceleration.
4642 float scale = (normTime-delta) * ACCEL_MOVE_SCALING_FACTOR;
4643 if (scale > 1) acc *= scale;
4644 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " accelerate: off="
4645 + off + " normTime=" + normTime + " delta=" + delta
4646 + " scale=" + scale + " acc=" + acc);
4647 acceleration = acc < MAX_ACCELERATION ? acc : MAX_ACCELERATION;
4648 } else {
4649 // The user is scrolling slowly, so decrease acceleration.
4650 float scale = (delta-normTime) * ACCEL_MOVE_SCALING_FACTOR;
4651 if (scale > 1) acc /= scale;
4652 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " deccelerate: off="
4653 + off + " normTime=" + normTime + " delta=" + delta
4654 + " scale=" + scale + " acc=" + acc);
4655 acceleration = acc > 1 ? acc : 1;
4656 }
4657 }
4658 position += off;
4659 return Math.abs(position);
Jeff Brown6f2fba42011-02-19 01:08:02 -08004660 }
Jeff Browncb1404e2011-01-15 18:14:15 -08004661
Jeff Brown678a1252013-04-09 17:46:25 -07004662 /**
4663 * Generate the number of discrete movement events appropriate for
4664 * the currently collected trackball movement.
4665 *
4666 * @return Returns the number of discrete movements, either positive
4667 * or negative, or 0 if there is not enough trackball movement yet
4668 * for a discrete movement.
4669 */
4670 int generate() {
4671 int movement = 0;
4672 nonAccelMovement = 0;
4673 do {
4674 final int dir = position >= 0 ? 1 : -1;
4675 switch (step) {
4676 // If we are going to execute the first step, then we want
4677 // to do this as soon as possible instead of waiting for
4678 // a full movement, in order to make things look responsive.
4679 case 0:
4680 if (Math.abs(position) < FIRST_MOVEMENT_THRESHOLD) {
4681 return movement;
4682 }
4683 movement += dir;
4684 nonAccelMovement += dir;
4685 step = 1;
4686 break;
4687 // If we have generated the first movement, then we need
4688 // to wait for the second complete trackball motion before
4689 // generating the second discrete movement.
4690 case 1:
4691 if (Math.abs(position) < SECOND_CUMULATIVE_MOVEMENT_THRESHOLD) {
4692 return movement;
4693 }
4694 movement += dir;
4695 nonAccelMovement += dir;
4696 position -= SECOND_CUMULATIVE_MOVEMENT_THRESHOLD * dir;
4697 step = 2;
4698 break;
4699 // After the first two, we generate discrete movements
4700 // consistently with the trackball, applying an acceleration
4701 // if the trackball is moving quickly. This is a simple
4702 // acceleration on top of what we already compute based
4703 // on how quickly the wheel is being turned, to apply
4704 // a longer increasing acceleration to continuous movement
4705 // in one direction.
4706 default:
4707 if (Math.abs(position) < SUBSEQUENT_INCREMENTAL_MOVEMENT_THRESHOLD) {
4708 return movement;
4709 }
4710 movement += dir;
4711 position -= dir * SUBSEQUENT_INCREMENTAL_MOVEMENT_THRESHOLD;
4712 float acc = acceleration;
4713 acc *= 1.1f;
4714 acceleration = acc < MAX_ACCELERATION ? acc : acceleration;
4715 break;
4716 }
4717 } while (true);
4718 }
4719 }
4720
4721 /**
4722 * Creates dpad events from unhandled joystick movements.
4723 */
4724 final class SyntheticJoystickHandler extends Handler {
Michael Wright9adca062014-03-19 11:51:26 -07004725 private final static String TAG = "SyntheticJoystickHandler";
Jeff Brown678a1252013-04-09 17:46:25 -07004726 private final static int MSG_ENQUEUE_X_AXIS_KEY_REPEAT = 1;
4727 private final static int MSG_ENQUEUE_Y_AXIS_KEY_REPEAT = 2;
4728
4729 private int mLastXDirection;
4730 private int mLastYDirection;
4731 private int mLastXKeyCode;
4732 private int mLastYKeyCode;
4733
4734 public SyntheticJoystickHandler() {
4735 super(true);
4736 }
4737
4738 @Override
4739 public void handleMessage(Message msg) {
4740 switch (msg.what) {
4741 case MSG_ENQUEUE_X_AXIS_KEY_REPEAT:
4742 case MSG_ENQUEUE_Y_AXIS_KEY_REPEAT: {
4743 KeyEvent oldEvent = (KeyEvent)msg.obj;
4744 KeyEvent e = KeyEvent.changeTimeRepeat(oldEvent,
4745 SystemClock.uptimeMillis(),
4746 oldEvent.getRepeatCount() + 1);
4747 if (mAttachInfo.mHasWindowFocus) {
4748 enqueueInputEvent(e);
4749 Message m = obtainMessage(msg.what, e);
4750 m.setAsynchronous(true);
4751 sendMessageDelayed(m, ViewConfiguration.getKeyRepeatDelay());
4752 }
4753 } break;
4754 }
4755 }
4756
4757 public void process(MotionEvent event) {
Michael Wright9adca062014-03-19 11:51:26 -07004758 switch(event.getActionMasked()) {
4759 case MotionEvent.ACTION_CANCEL:
4760 cancel(event);
4761 break;
4762 case MotionEvent.ACTION_MOVE:
4763 update(event, true);
4764 break;
4765 default:
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08004766 Log.w(mTag, "Unexpected action: " + event.getActionMasked());
Michael Wright9adca062014-03-19 11:51:26 -07004767 }
Jeff Brown678a1252013-04-09 17:46:25 -07004768 }
4769
Michael Wright9adca062014-03-19 11:51:26 -07004770 private void cancel(MotionEvent event) {
4771 removeMessages(MSG_ENQUEUE_X_AXIS_KEY_REPEAT);
4772 removeMessages(MSG_ENQUEUE_Y_AXIS_KEY_REPEAT);
Jeff Brown678a1252013-04-09 17:46:25 -07004773 update(event, false);
4774 }
4775
4776 private void update(MotionEvent event, boolean synthesizeNewKeys) {
Jeff Brownf9e989d2013-04-04 23:04:03 -07004777 final long time = event.getEventTime();
4778 final int metaState = event.getMetaState();
4779 final int deviceId = event.getDeviceId();
4780 final int source = event.getSource();
4781
4782 int xDirection = joystickAxisValueToDirection(
4783 event.getAxisValue(MotionEvent.AXIS_HAT_X));
4784 if (xDirection == 0) {
4785 xDirection = joystickAxisValueToDirection(event.getX());
Jeff Browncb1404e2011-01-15 18:14:15 -08004786 }
4787
Jeff Brownf9e989d2013-04-04 23:04:03 -07004788 int yDirection = joystickAxisValueToDirection(
4789 event.getAxisValue(MotionEvent.AXIS_HAT_Y));
4790 if (yDirection == 0) {
4791 yDirection = joystickAxisValueToDirection(event.getY());
4792 }
Jeff Browncb1404e2011-01-15 18:14:15 -08004793
Jeff Brown678a1252013-04-09 17:46:25 -07004794 if (xDirection != mLastXDirection) {
4795 if (mLastXKeyCode != 0) {
4796 removeMessages(MSG_ENQUEUE_X_AXIS_KEY_REPEAT);
Jeff Brownf9e989d2013-04-04 23:04:03 -07004797 enqueueInputEvent(new KeyEvent(time, time,
Jeff Brown678a1252013-04-09 17:46:25 -07004798 KeyEvent.ACTION_UP, mLastXKeyCode, 0, metaState,
Jeff Brownf9e989d2013-04-04 23:04:03 -07004799 deviceId, 0, KeyEvent.FLAG_FALLBACK, source));
Jeff Brown678a1252013-04-09 17:46:25 -07004800 mLastXKeyCode = 0;
Jeff Brownf9e989d2013-04-04 23:04:03 -07004801 }
4802
Jeff Brown678a1252013-04-09 17:46:25 -07004803 mLastXDirection = xDirection;
Jeff Brownf9e989d2013-04-04 23:04:03 -07004804
4805 if (xDirection != 0 && synthesizeNewKeys) {
Jeff Brown678a1252013-04-09 17:46:25 -07004806 mLastXKeyCode = xDirection > 0
Jeff Brownf9e989d2013-04-04 23:04:03 -07004807 ? KeyEvent.KEYCODE_DPAD_RIGHT : KeyEvent.KEYCODE_DPAD_LEFT;
4808 final KeyEvent e = new KeyEvent(time, time,
Jeff Brown678a1252013-04-09 17:46:25 -07004809 KeyEvent.ACTION_DOWN, mLastXKeyCode, 0, metaState,
Jeff Brownf9e989d2013-04-04 23:04:03 -07004810 deviceId, 0, KeyEvent.FLAG_FALLBACK, source);
4811 enqueueInputEvent(e);
Jeff Brown678a1252013-04-09 17:46:25 -07004812 Message m = obtainMessage(MSG_ENQUEUE_X_AXIS_KEY_REPEAT, e);
Jeff Brownf9e989d2013-04-04 23:04:03 -07004813 m.setAsynchronous(true);
Michael Wrightb9618522013-04-10 15:20:56 -07004814 sendMessageDelayed(m, ViewConfiguration.getKeyRepeatTimeout());
Jeff Brownf9e989d2013-04-04 23:04:03 -07004815 }
4816 }
4817
Jeff Brown678a1252013-04-09 17:46:25 -07004818 if (yDirection != mLastYDirection) {
4819 if (mLastYKeyCode != 0) {
4820 removeMessages(MSG_ENQUEUE_Y_AXIS_KEY_REPEAT);
Jeff Brownf9e989d2013-04-04 23:04:03 -07004821 enqueueInputEvent(new KeyEvent(time, time,
Jeff Brown678a1252013-04-09 17:46:25 -07004822 KeyEvent.ACTION_UP, mLastYKeyCode, 0, metaState,
Jeff Brownf9e989d2013-04-04 23:04:03 -07004823 deviceId, 0, KeyEvent.FLAG_FALLBACK, source));
Jeff Brown678a1252013-04-09 17:46:25 -07004824 mLastYKeyCode = 0;
Jeff Brownf9e989d2013-04-04 23:04:03 -07004825 }
4826
Jeff Brown678a1252013-04-09 17:46:25 -07004827 mLastYDirection = yDirection;
Jeff Brownf9e989d2013-04-04 23:04:03 -07004828
4829 if (yDirection != 0 && synthesizeNewKeys) {
Jeff Brown678a1252013-04-09 17:46:25 -07004830 mLastYKeyCode = yDirection > 0
Jeff Brownf9e989d2013-04-04 23:04:03 -07004831 ? KeyEvent.KEYCODE_DPAD_DOWN : KeyEvent.KEYCODE_DPAD_UP;
4832 final KeyEvent e = new KeyEvent(time, time,
Jeff Brown678a1252013-04-09 17:46:25 -07004833 KeyEvent.ACTION_DOWN, mLastYKeyCode, 0, metaState,
Jeff Brownf9e989d2013-04-04 23:04:03 -07004834 deviceId, 0, KeyEvent.FLAG_FALLBACK, source);
4835 enqueueInputEvent(e);
Jeff Brown678a1252013-04-09 17:46:25 -07004836 Message m = obtainMessage(MSG_ENQUEUE_Y_AXIS_KEY_REPEAT, e);
Jeff Brownf9e989d2013-04-04 23:04:03 -07004837 m.setAsynchronous(true);
Jeff Brown678a1252013-04-09 17:46:25 -07004838 sendMessageDelayed(m, ViewConfiguration.getKeyRepeatTimeout());
Jeff Brownf9e989d2013-04-04 23:04:03 -07004839 }
Jeff Browncb1404e2011-01-15 18:14:15 -08004840 }
4841 }
4842
Jeff Brownf9e989d2013-04-04 23:04:03 -07004843 private int joystickAxisValueToDirection(float value) {
4844 if (value >= 0.5f) {
4845 return 1;
4846 } else if (value <= -0.5f) {
4847 return -1;
4848 } else {
4849 return 0;
Jeff Browncb1404e2011-01-15 18:14:15 -08004850 }
4851 }
Jeff Brown678a1252013-04-09 17:46:25 -07004852 }
Jeff Browncb1404e2011-01-15 18:14:15 -08004853
Jeff Brown678a1252013-04-09 17:46:25 -07004854 /**
4855 * Creates dpad events from unhandled touch navigation movements.
4856 */
4857 final class SyntheticTouchNavigationHandler extends Handler {
Jeff Brown4dac9012013-04-10 01:03:19 -07004858 private static final String LOCAL_TAG = "SyntheticTouchNavigationHandler";
4859 private static final boolean LOCAL_DEBUG = false;
Jeff Brown678a1252013-04-09 17:46:25 -07004860
Jeff Brown4dac9012013-04-10 01:03:19 -07004861 // Assumed nominal width and height in millimeters of a touch navigation pad,
4862 // if no resolution information is available from the input system.
4863 private static final float DEFAULT_WIDTH_MILLIMETERS = 48;
4864 private static final float DEFAULT_HEIGHT_MILLIMETERS = 48;
Jeff Brown678a1252013-04-09 17:46:25 -07004865
Jeff Brown4dac9012013-04-10 01:03:19 -07004866 /* TODO: These constants should eventually be moved to ViewConfiguration. */
Jeff Brown678a1252013-04-09 17:46:25 -07004867
Jeff Brown4dac9012013-04-10 01:03:19 -07004868 // The nominal distance traveled to move by one unit.
4869 private static final int TICK_DISTANCE_MILLIMETERS = 12;
4870
4871 // Minimum and maximum fling velocity in ticks per second.
4872 // The minimum velocity should be set such that we perform enough ticks per
4873 // second that the fling appears to be fluid. For example, if we set the minimum
4874 // to 2 ticks per second, then there may be up to half a second delay between the next
4875 // to last and last ticks which is noticeably discrete and jerky. This value should
4876 // probably not be set to anything less than about 4.
4877 // If fling accuracy is a problem then consider tuning the tick distance instead.
4878 private static final float MIN_FLING_VELOCITY_TICKS_PER_SECOND = 6f;
4879 private static final float MAX_FLING_VELOCITY_TICKS_PER_SECOND = 20f;
4880
4881 // Fling velocity decay factor applied after each new key is emitted.
4882 // This parameter controls the deceleration and overall duration of the fling.
4883 // The fling stops automatically when its velocity drops below the minimum
4884 // fling velocity defined above.
4885 private static final float FLING_TICK_DECAY = 0.8f;
4886
4887 /* The input device that we are tracking. */
4888
4889 private int mCurrentDeviceId = -1;
4890 private int mCurrentSource;
4891 private boolean mCurrentDeviceSupported;
4892
4893 /* Configuration for the current input device. */
4894
Jeff Brown4dac9012013-04-10 01:03:19 -07004895 // The scaled tick distance. A movement of this amount should generally translate
4896 // into a single dpad event in a given direction.
4897 private float mConfigTickDistance;
4898
4899 // The minimum and maximum scaled fling velocity.
4900 private float mConfigMinFlingVelocity;
4901 private float mConfigMaxFlingVelocity;
4902
4903 /* Tracking state. */
4904
4905 // The velocity tracker for detecting flings.
4906 private VelocityTracker mVelocityTracker;
4907
4908 // The active pointer id, or -1 if none.
4909 private int mActivePointerId = -1;
4910
John Reck79d81e62013-11-05 13:26:57 -08004911 // Location where tracking started.
Jeff Brown4dac9012013-04-10 01:03:19 -07004912 private float mStartX;
4913 private float mStartY;
4914
4915 // Most recently observed position.
4916 private float mLastX;
4917 private float mLastY;
4918
4919 // Accumulated movement delta since the last direction key was sent.
Jeff Brown678a1252013-04-09 17:46:25 -07004920 private float mAccumulatedX;
4921 private float mAccumulatedY;
4922
Jeff Brown4dac9012013-04-10 01:03:19 -07004923 // Set to true if any movement was delivered to the app.
4924 // Implies that tap slop was exceeded.
4925 private boolean mConsumedMovement;
Jeff Brown678a1252013-04-09 17:46:25 -07004926
Jeff Brown4dac9012013-04-10 01:03:19 -07004927 // The most recently sent key down event.
4928 // The keycode remains set until the direction changes or a fling ends
4929 // so that repeated key events may be generated as required.
4930 private long mPendingKeyDownTime;
4931 private int mPendingKeyCode = KeyEvent.KEYCODE_UNKNOWN;
4932 private int mPendingKeyRepeatCount;
4933 private int mPendingKeyMetaState;
Jeff Brown678a1252013-04-09 17:46:25 -07004934
Jeff Brown4dac9012013-04-10 01:03:19 -07004935 // The current fling velocity while a fling is in progress.
4936 private boolean mFlinging;
4937 private float mFlingVelocity;
Jeff Brown678a1252013-04-09 17:46:25 -07004938
4939 public SyntheticTouchNavigationHandler() {
4940 super(true);
Jeff Brown678a1252013-04-09 17:46:25 -07004941 }
4942
4943 public void process(MotionEvent event) {
Jeff Brown4dac9012013-04-10 01:03:19 -07004944 // Update the current device information.
4945 final long time = event.getEventTime();
4946 final int deviceId = event.getDeviceId();
4947 final int source = event.getSource();
4948 if (mCurrentDeviceId != deviceId || mCurrentSource != source) {
4949 finishKeys(time);
4950 finishTracking(time);
4951 mCurrentDeviceId = deviceId;
4952 mCurrentSource = source;
4953 mCurrentDeviceSupported = false;
4954 InputDevice device = event.getDevice();
4955 if (device != null) {
4956 // In order to support an input device, we must know certain
4957 // characteristics about it, such as its size and resolution.
4958 InputDevice.MotionRange xRange = device.getMotionRange(MotionEvent.AXIS_X);
4959 InputDevice.MotionRange yRange = device.getMotionRange(MotionEvent.AXIS_Y);
4960 if (xRange != null && yRange != null) {
4961 mCurrentDeviceSupported = true;
Jeff Brown678a1252013-04-09 17:46:25 -07004962
Jeff Brown4dac9012013-04-10 01:03:19 -07004963 // Infer the resolution if it not actually known.
4964 float xRes = xRange.getResolution();
4965 if (xRes <= 0) {
4966 xRes = xRange.getRange() / DEFAULT_WIDTH_MILLIMETERS;
4967 }
4968 float yRes = yRange.getResolution();
4969 if (yRes <= 0) {
4970 yRes = yRange.getRange() / DEFAULT_HEIGHT_MILLIMETERS;
4971 }
4972 float nominalRes = (xRes + yRes) * 0.5f;
Jeff Brown678a1252013-04-09 17:46:25 -07004973
Jeff Brown4dac9012013-04-10 01:03:19 -07004974 // Precompute all of the configuration thresholds we will need.
Jeff Brown4dac9012013-04-10 01:03:19 -07004975 mConfigTickDistance = TICK_DISTANCE_MILLIMETERS * nominalRes;
4976 mConfigMinFlingVelocity =
4977 MIN_FLING_VELOCITY_TICKS_PER_SECOND * mConfigTickDistance;
4978 mConfigMaxFlingVelocity =
4979 MAX_FLING_VELOCITY_TICKS_PER_SECOND * mConfigTickDistance;
4980
4981 if (LOCAL_DEBUG) {
4982 Log.d(LOCAL_TAG, "Configured device " + mCurrentDeviceId
4983 + " (" + Integer.toHexString(mCurrentSource) + "): "
Jeff Brown4dac9012013-04-10 01:03:19 -07004984 + ", mConfigTickDistance=" + mConfigTickDistance
4985 + ", mConfigMinFlingVelocity=" + mConfigMinFlingVelocity
4986 + ", mConfigMaxFlingVelocity=" + mConfigMaxFlingVelocity);
4987 }
4988 }
4989 }
Jeff Brown678a1252013-04-09 17:46:25 -07004990 }
Jeff Brown4dac9012013-04-10 01:03:19 -07004991 if (!mCurrentDeviceSupported) {
Jeff Brown678a1252013-04-09 17:46:25 -07004992 return;
4993 }
4994
Jeff Brown4dac9012013-04-10 01:03:19 -07004995 // Handle the event.
4996 final int action = event.getActionMasked();
4997 switch (action) {
Jeff Brown678a1252013-04-09 17:46:25 -07004998 case MotionEvent.ACTION_DOWN: {
Jeff Brown4dac9012013-04-10 01:03:19 -07004999 boolean caughtFling = mFlinging;
5000 finishKeys(time);
5001 finishTracking(time);
5002 mActivePointerId = event.getPointerId(0);
5003 mVelocityTracker = VelocityTracker.obtain();
5004 mVelocityTracker.addMovement(event);
Jeff Brown4dac9012013-04-10 01:03:19 -07005005 mStartX = event.getX();
5006 mStartY = event.getY();
5007 mLastX = mStartX;
5008 mLastY = mStartY;
Jeff Brown678a1252013-04-09 17:46:25 -07005009 mAccumulatedX = 0;
5010 mAccumulatedY = 0;
Jeff Brown4dac9012013-04-10 01:03:19 -07005011
5012 // If we caught a fling, then pretend that the tap slop has already
5013 // been exceeded to suppress taps whose only purpose is to stop the fling.
5014 mConsumedMovement = caughtFling;
Jeff Brown678a1252013-04-09 17:46:25 -07005015 break;
5016 }
5017
Jeff Brown4dac9012013-04-10 01:03:19 -07005018 case MotionEvent.ACTION_MOVE:
Jeff Brown678a1252013-04-09 17:46:25 -07005019 case MotionEvent.ACTION_UP: {
Jeff Brown4dac9012013-04-10 01:03:19 -07005020 if (mActivePointerId < 0) {
5021 break;
5022 }
5023 final int index = event.findPointerIndex(mActivePointerId);
5024 if (index < 0) {
5025 finishKeys(time);
5026 finishTracking(time);
5027 break;
5028 }
Jeff Brown678a1252013-04-09 17:46:25 -07005029
Jeff Brown4dac9012013-04-10 01:03:19 -07005030 mVelocityTracker.addMovement(event);
5031 final float x = event.getX(index);
5032 final float y = event.getY(index);
5033 mAccumulatedX += x - mLastX;
5034 mAccumulatedY += y - mLastY;
5035 mLastX = x;
5036 mLastY = y;
5037
5038 // Consume any accumulated movement so far.
5039 final int metaState = event.getMetaState();
5040 consumeAccumulatedMovement(time, metaState);
5041
5042 // Detect taps and flings.
5043 if (action == MotionEvent.ACTION_UP) {
Michael Wright88d7f792013-08-27 15:45:42 -07005044 if (mConsumedMovement && mPendingKeyCode != KeyEvent.KEYCODE_UNKNOWN) {
Jeff Brown4dac9012013-04-10 01:03:19 -07005045 // It might be a fling.
5046 mVelocityTracker.computeCurrentVelocity(1000, mConfigMaxFlingVelocity);
5047 final float vx = mVelocityTracker.getXVelocity(mActivePointerId);
5048 final float vy = mVelocityTracker.getYVelocity(mActivePointerId);
5049 if (!startFling(time, vx, vy)) {
5050 finishKeys(time);
Jeff Brown678a1252013-04-09 17:46:25 -07005051 }
5052 }
Jeff Brown4dac9012013-04-10 01:03:19 -07005053 finishTracking(time);
Jeff Brown678a1252013-04-09 17:46:25 -07005054 }
Jeff Brown4dac9012013-04-10 01:03:19 -07005055 break;
5056 }
5057
5058 case MotionEvent.ACTION_CANCEL: {
5059 finishKeys(time);
5060 finishTracking(time);
Jeff Brown678a1252013-04-09 17:46:25 -07005061 break;
5062 }
5063 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005064 }
Jeff Brown4dac9012013-04-10 01:03:19 -07005065
5066 public void cancel(MotionEvent event) {
5067 if (mCurrentDeviceId == event.getDeviceId()
5068 && mCurrentSource == event.getSource()) {
5069 final long time = event.getEventTime();
5070 finishKeys(time);
5071 finishTracking(time);
5072 }
5073 }
5074
5075 private void finishKeys(long time) {
5076 cancelFling();
5077 sendKeyUp(time);
5078 }
5079
5080 private void finishTracking(long time) {
5081 if (mActivePointerId >= 0) {
5082 mActivePointerId = -1;
5083 mVelocityTracker.recycle();
5084 mVelocityTracker = null;
5085 }
5086 }
5087
5088 private void consumeAccumulatedMovement(long time, int metaState) {
5089 final float absX = Math.abs(mAccumulatedX);
5090 final float absY = Math.abs(mAccumulatedY);
5091 if (absX >= absY) {
5092 if (absX >= mConfigTickDistance) {
5093 mAccumulatedX = consumeAccumulatedMovement(time, metaState, mAccumulatedX,
5094 KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_RIGHT);
5095 mAccumulatedY = 0;
5096 mConsumedMovement = true;
5097 }
5098 } else {
5099 if (absY >= mConfigTickDistance) {
5100 mAccumulatedY = consumeAccumulatedMovement(time, metaState, mAccumulatedY,
5101 KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_DOWN);
5102 mAccumulatedX = 0;
5103 mConsumedMovement = true;
5104 }
5105 }
5106 }
5107
5108 private float consumeAccumulatedMovement(long time, int metaState,
5109 float accumulator, int negativeKeyCode, int positiveKeyCode) {
5110 while (accumulator <= -mConfigTickDistance) {
5111 sendKeyDownOrRepeat(time, negativeKeyCode, metaState);
5112 accumulator += mConfigTickDistance;
5113 }
5114 while (accumulator >= mConfigTickDistance) {
5115 sendKeyDownOrRepeat(time, positiveKeyCode, metaState);
5116 accumulator -= mConfigTickDistance;
5117 }
5118 return accumulator;
5119 }
5120
5121 private void sendKeyDownOrRepeat(long time, int keyCode, int metaState) {
5122 if (mPendingKeyCode != keyCode) {
5123 sendKeyUp(time);
5124 mPendingKeyDownTime = time;
5125 mPendingKeyCode = keyCode;
5126 mPendingKeyRepeatCount = 0;
5127 } else {
5128 mPendingKeyRepeatCount += 1;
5129 }
5130 mPendingKeyMetaState = metaState;
5131
5132 // Note: Normally we would pass FLAG_LONG_PRESS when the repeat count is 1
5133 // but it doesn't quite make sense when simulating the events in this way.
5134 if (LOCAL_DEBUG) {
5135 Log.d(LOCAL_TAG, "Sending key down: keyCode=" + mPendingKeyCode
5136 + ", repeatCount=" + mPendingKeyRepeatCount
5137 + ", metaState=" + Integer.toHexString(mPendingKeyMetaState));
5138 }
5139 enqueueInputEvent(new KeyEvent(mPendingKeyDownTime, time,
5140 KeyEvent.ACTION_DOWN, mPendingKeyCode, mPendingKeyRepeatCount,
5141 mPendingKeyMetaState, mCurrentDeviceId,
5142 KeyEvent.FLAG_FALLBACK, mCurrentSource));
5143 }
5144
5145 private void sendKeyUp(long time) {
5146 if (mPendingKeyCode != KeyEvent.KEYCODE_UNKNOWN) {
5147 if (LOCAL_DEBUG) {
5148 Log.d(LOCAL_TAG, "Sending key up: keyCode=" + mPendingKeyCode
5149 + ", metaState=" + Integer.toHexString(mPendingKeyMetaState));
5150 }
5151 enqueueInputEvent(new KeyEvent(mPendingKeyDownTime, time,
5152 KeyEvent.ACTION_UP, mPendingKeyCode, 0, mPendingKeyMetaState,
5153 mCurrentDeviceId, 0, KeyEvent.FLAG_FALLBACK,
5154 mCurrentSource));
5155 mPendingKeyCode = KeyEvent.KEYCODE_UNKNOWN;
5156 }
5157 }
5158
5159 private boolean startFling(long time, float vx, float vy) {
5160 if (LOCAL_DEBUG) {
5161 Log.d(LOCAL_TAG, "Considering fling: vx=" + vx + ", vy=" + vy
5162 + ", min=" + mConfigMinFlingVelocity);
5163 }
5164
5165 // Flings must be oriented in the same direction as the preceding movements.
5166 switch (mPendingKeyCode) {
5167 case KeyEvent.KEYCODE_DPAD_LEFT:
5168 if (-vx >= mConfigMinFlingVelocity
5169 && Math.abs(vy) < mConfigMinFlingVelocity) {
5170 mFlingVelocity = -vx;
5171 break;
5172 }
5173 return false;
5174
5175 case KeyEvent.KEYCODE_DPAD_RIGHT:
5176 if (vx >= mConfigMinFlingVelocity
5177 && Math.abs(vy) < mConfigMinFlingVelocity) {
5178 mFlingVelocity = vx;
5179 break;
5180 }
5181 return false;
5182
5183 case KeyEvent.KEYCODE_DPAD_UP:
5184 if (-vy >= mConfigMinFlingVelocity
5185 && Math.abs(vx) < mConfigMinFlingVelocity) {
5186 mFlingVelocity = -vy;
5187 break;
5188 }
5189 return false;
5190
5191 case KeyEvent.KEYCODE_DPAD_DOWN:
5192 if (vy >= mConfigMinFlingVelocity
5193 && Math.abs(vx) < mConfigMinFlingVelocity) {
5194 mFlingVelocity = vy;
5195 break;
5196 }
5197 return false;
5198 }
5199
5200 // Post the first fling event.
5201 mFlinging = postFling(time);
5202 return mFlinging;
5203 }
5204
5205 private boolean postFling(long time) {
5206 // The idea here is to estimate the time when the pointer would have
5207 // traveled one tick distance unit given the current fling velocity.
5208 // This effect creates continuity of motion.
5209 if (mFlingVelocity >= mConfigMinFlingVelocity) {
5210 long delay = (long)(mConfigTickDistance / mFlingVelocity * 1000);
5211 postAtTime(mFlingRunnable, time + delay);
5212 if (LOCAL_DEBUG) {
5213 Log.d(LOCAL_TAG, "Posted fling: velocity="
5214 + mFlingVelocity + ", delay=" + delay
5215 + ", keyCode=" + mPendingKeyCode);
5216 }
5217 return true;
5218 }
5219 return false;
5220 }
5221
5222 private void cancelFling() {
5223 if (mFlinging) {
5224 removeCallbacks(mFlingRunnable);
5225 mFlinging = false;
5226 }
5227 }
5228
5229 private final Runnable mFlingRunnable = new Runnable() {
5230 @Override
5231 public void run() {
5232 final long time = SystemClock.uptimeMillis();
5233 sendKeyDownOrRepeat(time, mPendingKeyCode, mPendingKeyMetaState);
5234 mFlingVelocity *= FLING_TICK_DECAY;
5235 if (!postFling(time)) {
5236 mFlinging = false;
5237 finishKeys(time);
5238 }
5239 }
5240 };
Jeff Browncb1404e2011-01-15 18:14:15 -08005241 }
5242
Michael Wright899d7052014-04-23 17:23:39 -07005243 final class SyntheticKeyboardHandler {
5244 public void process(KeyEvent event) {
5245 if ((event.getFlags() & KeyEvent.FLAG_FALLBACK) != 0) {
5246 return;
5247 }
5248
5249 final KeyCharacterMap kcm = event.getKeyCharacterMap();
5250 final int keyCode = event.getKeyCode();
5251 final int metaState = event.getMetaState();
5252
5253 // Check for fallback actions specified by the key character map.
5254 KeyCharacterMap.FallbackAction fallbackAction =
5255 kcm.getFallbackAction(keyCode, metaState);
5256 if (fallbackAction != null) {
5257 final int flags = event.getFlags() | KeyEvent.FLAG_FALLBACK;
5258 KeyEvent fallbackEvent = KeyEvent.obtain(
5259 event.getDownTime(), event.getEventTime(),
5260 event.getAction(), fallbackAction.keyCode,
5261 event.getRepeatCount(), fallbackAction.metaState,
5262 event.getDeviceId(), event.getScanCode(),
5263 flags, event.getSource(), null);
5264 fallbackAction.recycle();
5265 enqueueInputEvent(fallbackEvent);
5266 }
5267 }
5268 }
5269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005270 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005271 * Returns true if the key is used for keyboard navigation.
5272 * @param keyEvent The key event.
5273 * @return True if the key is used for keyboard navigation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005274 */
Jeff Brown4e6319b2010-12-13 10:36:51 -08005275 private static boolean isNavigationKey(KeyEvent keyEvent) {
5276 switch (keyEvent.getKeyCode()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005277 case KeyEvent.KEYCODE_DPAD_LEFT:
5278 case KeyEvent.KEYCODE_DPAD_RIGHT:
5279 case KeyEvent.KEYCODE_DPAD_UP:
5280 case KeyEvent.KEYCODE_DPAD_DOWN:
Jeff Brown4e6319b2010-12-13 10:36:51 -08005281 case KeyEvent.KEYCODE_DPAD_CENTER:
5282 case KeyEvent.KEYCODE_PAGE_UP:
5283 case KeyEvent.KEYCODE_PAGE_DOWN:
5284 case KeyEvent.KEYCODE_MOVE_HOME:
5285 case KeyEvent.KEYCODE_MOVE_END:
5286 case KeyEvent.KEYCODE_TAB:
5287 case KeyEvent.KEYCODE_SPACE:
5288 case KeyEvent.KEYCODE_ENTER:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005289 return true;
5290 }
5291 return false;
5292 }
5293
5294 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005295 * Returns true if the key is used for typing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005296 * @param keyEvent The key event.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005297 * @return True if the key is used for typing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005298 */
Jeff Brown4e6319b2010-12-13 10:36:51 -08005299 private static boolean isTypingKey(KeyEvent keyEvent) {
5300 return keyEvent.getUnicodeChar() > 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005301 }
5302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005303 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005304 * 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 -08005305 * @param event The key event.
5306 * @return Whether this key event should be consumed (meaning the act of
5307 * leaving touch mode alone is considered the event).
5308 */
5309 private boolean checkForLeavingTouchModeAndConsume(KeyEvent event) {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005310 // Only relevant in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005311 if (!mAttachInfo.mInTouchMode) {
5312 return false;
5313 }
5314
Jeff Brown4e6319b2010-12-13 10:36:51 -08005315 // Only consider leaving touch mode on DOWN or MULTIPLE actions, never on UP.
5316 final int action = event.getAction();
5317 if (action != KeyEvent.ACTION_DOWN && action != KeyEvent.ACTION_MULTIPLE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005318 return false;
5319 }
5320
Jeff Brown4e6319b2010-12-13 10:36:51 -08005321 // Don't leave touch mode if the IME told us not to.
5322 if ((event.getFlags() & KeyEvent.FLAG_KEEP_TOUCH_MODE) != 0) {
5323 return false;
5324 }
5325
5326 // If the key can be used for keyboard navigation then leave touch mode
5327 // and select a focused view if needed (in ensureTouchMode).
5328 // When a new focused view is selected, we consume the navigation key because
5329 // navigation doesn't make much sense unless a view already has focus so
5330 // the key's purpose is to set focus.
5331 if (isNavigationKey(event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005332 return ensureTouchMode(false);
5333 }
Jeff Brown4e6319b2010-12-13 10:36:51 -08005334
5335 // If the key can be used for typing then leave touch mode
5336 // and select a focused view if needed (in ensureTouchMode).
5337 // Always allow the view to process the typing key.
5338 if (isTypingKey(event)) {
5339 ensureTouchMode(false);
5340 return false;
5341 }
5342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005343 return false;
5344 }
5345
Christopher Tatea53146c2010-09-07 11:57:52 -07005346 /* drag/drop */
Christopher Tate407b4e92010-11-30 17:14:08 -08005347 void setLocalDragState(Object obj) {
5348 mLocalDragState = obj;
5349 }
5350
Christopher Tatea53146c2010-09-07 11:57:52 -07005351 private void handleDragEvent(DragEvent event) {
5352 // From the root, only drag start/end/location are dispatched. entered/exited
5353 // are determined and dispatched by the viewgroup hierarchy, who then report
5354 // that back here for ultimate reporting back to the framework.
5355 if (mView != null && mAdded) {
5356 final int what = event.mAction;
5357
5358 if (what == DragEvent.ACTION_DRAG_EXITED) {
5359 // A direct EXITED event means that the window manager knows we've just crossed
5360 // a window boundary, so the current drag target within this one must have
5361 // just been exited. Send it the usual notifications and then we're done
5362 // for now.
Chris Tate9d1ab882010-11-02 15:55:39 -07005363 mView.dispatchDragEvent(event);
Christopher Tatea53146c2010-09-07 11:57:52 -07005364 } else {
5365 // Cache the drag description when the operation starts, then fill it in
5366 // on subsequent calls as a convenience
5367 if (what == DragEvent.ACTION_DRAG_STARTED) {
Chris Tate9d1ab882010-11-02 15:55:39 -07005368 mCurrentDragView = null; // Start the current-recipient tracking
Christopher Tatea53146c2010-09-07 11:57:52 -07005369 mDragDescription = event.mClipDescription;
5370 } else {
5371 event.mClipDescription = mDragDescription;
5372 }
5373
5374 // For events with a [screen] location, translate into window coordinates
5375 if ((what == DragEvent.ACTION_DRAG_LOCATION) || (what == DragEvent.ACTION_DROP)) {
5376 mDragPoint.set(event.mX, event.mY);
5377 if (mTranslator != null) {
5378 mTranslator.translatePointInScreenToAppWindow(mDragPoint);
5379 }
5380
5381 if (mCurScrollY != 0) {
5382 mDragPoint.offset(0, mCurScrollY);
5383 }
5384
5385 event.mX = mDragPoint.x;
5386 event.mY = mDragPoint.y;
5387 }
5388
5389 // Remember who the current drag target is pre-dispatch
5390 final View prevDragView = mCurrentDragView;
5391
5392 // Now dispatch the drag/drop event
Chris Tated4533f12010-10-19 15:15:08 -07005393 boolean result = mView.dispatchDragEvent(event);
Christopher Tatea53146c2010-09-07 11:57:52 -07005394
5395 // If we changed apparent drag target, tell the OS about it
5396 if (prevDragView != mCurrentDragView) {
5397 try {
5398 if (prevDragView != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07005399 mWindowSession.dragRecipientExited(mWindow);
Christopher Tatea53146c2010-09-07 11:57:52 -07005400 }
5401 if (mCurrentDragView != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07005402 mWindowSession.dragRecipientEntered(mWindow);
Christopher Tatea53146c2010-09-07 11:57:52 -07005403 }
5404 } catch (RemoteException e) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08005405 Slog.e(mTag, "Unable to note drag target change");
Christopher Tatea53146c2010-09-07 11:57:52 -07005406 }
Christopher Tatea53146c2010-09-07 11:57:52 -07005407 }
Chris Tated4533f12010-10-19 15:15:08 -07005408
Christopher Tate407b4e92010-11-30 17:14:08 -08005409 // Report the drop result when we're done
Chris Tated4533f12010-10-19 15:15:08 -07005410 if (what == DragEvent.ACTION_DROP) {
Christopher Tate1fc014f2011-01-19 12:56:26 -08005411 mDragDescription = null;
Chris Tated4533f12010-10-19 15:15:08 -07005412 try {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08005413 Log.i(mTag, "Reporting drop result: " + result);
Jeff Brown98365d72012-08-19 20:30:52 -07005414 mWindowSession.reportDropResult(mWindow, result);
Chris Tated4533f12010-10-19 15:15:08 -07005415 } catch (RemoteException e) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08005416 Log.e(mTag, "Unable to report drop result");
Chris Tated4533f12010-10-19 15:15:08 -07005417 }
5418 }
Christopher Tate407b4e92010-11-30 17:14:08 -08005419
Vladislav Kaznacheev82063912015-11-20 14:20:13 -08005420 // When the drag operation ends, reset drag-related state
Christopher Tate407b4e92010-11-30 17:14:08 -08005421 if (what == DragEvent.ACTION_DRAG_ENDED) {
5422 setLocalDragState(null);
Vladislav Kaznacheev82063912015-11-20 14:20:13 -08005423 mAttachInfo.mDragToken = null;
Vladislav Kaznacheev4f639742015-11-18 13:21:35 -08005424 if (mAttachInfo.mDragSurface != null) {
5425 mAttachInfo.mDragSurface.release();
5426 mAttachInfo.mDragSurface = null;
5427 }
Christopher Tate407b4e92010-11-30 17:14:08 -08005428 }
Christopher Tatea53146c2010-09-07 11:57:52 -07005429 }
5430 }
5431 event.recycle();
5432 }
5433
Dianne Hackborn9a230e02011-10-06 11:51:27 -07005434 public void handleDispatchSystemUiVisibilityChanged(SystemUiVisibilityInfo args) {
5435 if (mSeq != args.seq) {
5436 // The sequence has changed, so we need to update our value and make
5437 // sure to do a traversal afterward so the window manager is given our
5438 // most recent data.
5439 mSeq = args.seq;
5440 mAttachInfo.mForceReportNewAttributes = true;
Igor Murashkina86ab6402013-08-30 12:58:36 -07005441 scheduleTraversals();
Joe Onorato14782f72011-01-25 19:53:17 -08005442 }
Dianne Hackborn9a230e02011-10-06 11:51:27 -07005443 if (mView == null) return;
5444 if (args.localChanges != 0) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -07005445 mView.updateLocalSystemUiVisibility(args.localValue, args.localChanges);
Dianne Hackborn9a230e02011-10-06 11:51:27 -07005446 }
Chris Craikd36a81f2014-07-17 10:16:51 -07005447
5448 int visibility = args.globalVisibility&View.SYSTEM_UI_CLEARABLE_FLAGS;
5449 if (visibility != mAttachInfo.mGlobalSystemUiVisibility) {
5450 mAttachInfo.mGlobalSystemUiVisibility = visibility;
5451 mView.dispatchSystemUiVisibilityChanged(visibility);
Dianne Hackborncf675782012-05-10 15:07:24 -07005452 }
Joe Onorato664644d2011-01-23 17:53:23 -08005453 }
5454
Craig Mautner9c795042014-10-28 19:59:59 -07005455 public void handleDispatchWindowShown() {
5456 mAttachInfo.mTreeObserver.dispatchOnWindowShown();
5457 }
5458
Clara Bayarri75e09792015-07-29 16:20:40 +01005459 public void handleRequestKeyboardShortcuts(IResultReceiver receiver) {
5460 Bundle data = new Bundle();
5461 ArrayList<KeyboardShortcutGroup> list = new ArrayList<>();
5462 if (mView != null) {
5463 mView.requestKeyboardShortcuts(list);
5464 }
5465 data.putParcelableArrayList(WindowManager.PARCEL_KEY_SHORTCUTS_ARRAY, list);
5466 try {
5467 receiver.send(0, data);
5468 } catch (RemoteException e) {
5469 }
5470 }
5471
Christopher Tate2c095f32010-10-04 14:13:40 -07005472 public void getLastTouchPoint(Point outLocation) {
5473 outLocation.x = (int) mLastTouchPoint.x;
5474 outLocation.y = (int) mLastTouchPoint.y;
5475 }
5476
Chris Tate9d1ab882010-11-02 15:55:39 -07005477 public void setDragFocus(View newDragTarget) {
Christopher Tatea53146c2010-09-07 11:57:52 -07005478 if (mCurrentDragView != newDragTarget) {
Chris Tate048691c2010-10-12 17:39:18 -07005479 mCurrentDragView = newDragTarget;
Christopher Tatea53146c2010-09-07 11:57:52 -07005480 }
Christopher Tatea53146c2010-09-07 11:57:52 -07005481 }
5482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005483 private AudioManager getAudioManager() {
5484 if (mView == null) {
5485 throw new IllegalStateException("getAudioManager called when there is no mView");
5486 }
5487 if (mAudioManager == null) {
5488 mAudioManager = (AudioManager) mView.getContext().getSystemService(Context.AUDIO_SERVICE);
5489 }
5490 return mAudioManager;
5491 }
5492
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005493 public AccessibilityInteractionController getAccessibilityInteractionController() {
5494 if (mView == null) {
5495 throw new IllegalStateException("getAccessibilityInteractionController"
5496 + " called when there is no mView");
5497 }
Gilles Debunne5ac84422011-10-19 09:35:58 -07005498 if (mAccessibilityInteractionController == null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07005499 mAccessibilityInteractionController = new AccessibilityInteractionController(this);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005500 }
Gilles Debunne5ac84422011-10-19 09:35:58 -07005501 return mAccessibilityInteractionController;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005502 }
5503
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07005504 private int relayoutWindow(WindowManager.LayoutParams params, int viewVisibility,
5505 boolean insetsPending) throws RemoteException {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07005506
5507 float appScale = mAttachInfo.mApplicationScale;
Mitsuru Oshima3d914922009-05-13 22:29:15 -07005508 boolean restore = false;
Mitsuru Oshima64f59342009-06-21 00:03:11 -07005509 if (params != null && mTranslator != null) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -07005510 restore = true;
5511 params.backup();
Mitsuru Oshima64f59342009-06-21 00:03:11 -07005512 mTranslator.translateWindowLayout(params);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07005513 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07005514 if (params != null) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08005515 if (DBG) Log.d(mTag, "WindowLayout in layoutWindow:" + params);
Mitsuru Oshima3d914922009-05-13 22:29:15 -07005516 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -07005517 mPendingConfiguration.seq = 0;
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08005518 //Log.d(mTag, ">>>>>> CALLING relayout");
Dianne Hackborn180c4842011-09-13 12:39:25 -07005519 if (params != null && mOrigWindowType != params.type) {
5520 // For compatibility with old apps, don't crash here.
Michael Wright5bd69e62015-05-14 14:48:08 +01005521 if (mTargetSdkVersion < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08005522 Slog.w(mTag, "Window type can not be changed after "
Dianne Hackborn180c4842011-09-13 12:39:25 -07005523 + "the window is added; ignoring change of " + mView);
5524 params.type = mOrigWindowType;
5525 }
5526 }
Jeff Brown98365d72012-08-19 20:30:52 -07005527 int relayoutResult = mWindowSession.relayout(
Dianne Hackborn9a230e02011-10-06 11:51:27 -07005528 mWindow, mSeq, params,
Dianne Hackborn189ee182010-12-02 21:48:53 -08005529 (int) (mView.getMeasuredWidth() * appScale + 0.5f),
5530 (int) (mView.getMeasuredHeight() * appScale + 0.5f),
Jeff Brown98365d72012-08-19 20:30:52 -07005531 viewVisibility, insetsPending ? WindowManagerGlobal.RELAYOUT_INSETS_PENDING : 0,
Dianne Hackbornc4aad012013-02-22 15:05:25 -08005532 mWinFrame, mPendingOverscanInsets, mPendingContentInsets, mPendingVisibleInsets,
Jorim Jaggi2e95a482016-01-14 17:36:55 -08005533 mPendingStableInsets, mPendingOutsets, mPendingBackDropFrame, mPendingConfiguration,
5534 mSurface);
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08005535 //Log.d(mTag, "<<<<<< BACK FROM relayout");
Mitsuru Oshima3d914922009-05-13 22:29:15 -07005536 if (restore) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -07005537 params.restore();
Mitsuru Oshima3d914922009-05-13 22:29:15 -07005538 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07005539
Mitsuru Oshima64f59342009-06-21 00:03:11 -07005540 if (mTranslator != null) {
5541 mTranslator.translateRectInScreenToAppWinFrame(mWinFrame);
Dianne Hackbornc4aad012013-02-22 15:05:25 -08005542 mTranslator.translateRectInScreenToAppWindow(mPendingOverscanInsets);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07005543 mTranslator.translateRectInScreenToAppWindow(mPendingContentInsets);
5544 mTranslator.translateRectInScreenToAppWindow(mPendingVisibleInsets);
Adrian Roosfa104232014-06-20 16:10:14 -07005545 mTranslator.translateRectInScreenToAppWindow(mPendingStableInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07005546 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07005547 return relayoutResult;
5548 }
Romain Guy8506ab42009-06-11 17:35:47 -07005549
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07005550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005551 * {@inheritDoc}
5552 */
Igor Murashkina86ab6402013-08-30 12:58:36 -07005553 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005554 public void playSoundEffect(int effectId) {
5555 checkThread();
5556
Jean-Michel Trivi13b18fd2010-05-05 09:18:15 -07005557 try {
5558 final AudioManager audioManager = getAudioManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005559
Jean-Michel Trivi13b18fd2010-05-05 09:18:15 -07005560 switch (effectId) {
5561 case SoundEffectConstants.CLICK:
5562 audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK);
5563 return;
5564 case SoundEffectConstants.NAVIGATION_DOWN:
5565 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_DOWN);
5566 return;
5567 case SoundEffectConstants.NAVIGATION_LEFT:
5568 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_LEFT);
5569 return;
5570 case SoundEffectConstants.NAVIGATION_RIGHT:
5571 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_RIGHT);
5572 return;
5573 case SoundEffectConstants.NAVIGATION_UP:
5574 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_UP);
5575 return;
5576 default:
5577 throw new IllegalArgumentException("unknown effect id " + effectId +
5578 " not defined in " + SoundEffectConstants.class.getCanonicalName());
5579 }
5580 } catch (IllegalStateException e) {
5581 // Exception thrown by getAudioManager() when mView is null
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08005582 Log.e(mTag, "FATAL EXCEPTION when attempting to play sound effect: " + e);
Jean-Michel Trivi13b18fd2010-05-05 09:18:15 -07005583 e.printStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005584 }
5585 }
5586
5587 /**
5588 * {@inheritDoc}
5589 */
Igor Murashkina86ab6402013-08-30 12:58:36 -07005590 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005591 public boolean performHapticFeedback(int effectId, boolean always) {
5592 try {
Jeff Brown98365d72012-08-19 20:30:52 -07005593 return mWindowSession.performHapticFeedback(mWindow, effectId, always);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005594 } catch (RemoteException e) {
5595 return false;
5596 }
5597 }
5598
5599 /**
5600 * {@inheritDoc}
5601 */
Igor Murashkina86ab6402013-08-30 12:58:36 -07005602 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005603 public View focusSearch(View focused, int direction) {
5604 checkThread();
5605 if (!(mView instanceof ViewGroup)) {
5606 return null;
5607 }
5608 return FocusFinder.getInstance().findNextFocus((ViewGroup) mView, focused, direction);
5609 }
5610
5611 public void debug() {
5612 mView.debug();
5613 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07005614
Jeff Brown5182c782013-10-15 20:31:52 -07005615 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5616 String innerPrefix = prefix + " ";
5617 writer.print(prefix); writer.println("ViewRoot:");
5618 writer.print(innerPrefix); writer.print("mAdded="); writer.print(mAdded);
5619 writer.print(" mRemoved="); writer.println(mRemoved);
5620 writer.print(innerPrefix); writer.print("mConsumeBatchedInputScheduled=");
5621 writer.println(mConsumeBatchedInputScheduled);
Michael Wright9d744c72014-02-18 21:27:42 -08005622 writer.print(innerPrefix); writer.print("mConsumeBatchedInputImmediatelyScheduled=");
5623 writer.println(mConsumeBatchedInputImmediatelyScheduled);
Jeff Brown5182c782013-10-15 20:31:52 -07005624 writer.print(innerPrefix); writer.print("mPendingInputEventCount=");
5625 writer.println(mPendingInputEventCount);
5626 writer.print(innerPrefix); writer.print("mProcessInputEventsScheduled=");
5627 writer.println(mProcessInputEventsScheduled);
5628 writer.print(innerPrefix); writer.print("mTraversalScheduled=");
5629 writer.print(mTraversalScheduled);
Daniel Koulomzin087ae472015-12-16 17:52:25 -05005630 writer.print(innerPrefix); writer.print("mIsAmbientMode=");
5631 writer.print(mIsAmbientMode);
Jeff Brown5182c782013-10-15 20:31:52 -07005632 if (mTraversalScheduled) {
5633 writer.print(" (barrier="); writer.print(mTraversalBarrier); writer.println(")");
5634 } else {
5635 writer.println();
5636 }
5637 mFirstInputStage.dump(innerPrefix, writer);
5638
5639 mChoreographer.dump(prefix, writer);
5640
5641 writer.print(prefix); writer.println("View Hierarchy:");
5642 dumpViewHierarchy(innerPrefix, writer, mView);
5643 }
5644
5645 private void dumpViewHierarchy(String prefix, PrintWriter writer, View view) {
5646 writer.print(prefix);
5647 if (view == null) {
5648 writer.println("null");
5649 return;
5650 }
5651 writer.println(view.toString());
5652 if (!(view instanceof ViewGroup)) {
5653 return;
5654 }
5655 ViewGroup grp = (ViewGroup)view;
5656 final int N = grp.getChildCount();
5657 if (N <= 0) {
5658 return;
5659 }
5660 prefix = prefix + " ";
5661 for (int i=0; i<N; i++) {
5662 dumpViewHierarchy(prefix, writer, grp.getChildAt(i));
5663 }
5664 }
5665
Romain Guy211370f2012-02-01 16:10:55 -08005666 public void dumpGfxInfo(int[] info) {
Romain Guy54ab3472012-06-14 12:52:53 -07005667 info[0] = info[1] = 0;
Romain Guy65b345f2011-07-27 18:51:50 -07005668 if (mView != null) {
5669 getGfxInfo(mView, info);
Romain Guy65b345f2011-07-27 18:51:50 -07005670 }
5671 }
5672
Romain Guya998dff2012-03-23 18:58:36 -07005673 private static void getGfxInfo(View view, int[] info) {
Chris Craik64a12e12014-03-28 18:12:12 -07005674 RenderNode renderNode = view.mRenderNode;
Romain Guy65b345f2011-07-27 18:51:50 -07005675 info[0]++;
Chris Craik64a12e12014-03-28 18:12:12 -07005676 if (renderNode != null) {
John Reckfe5e7b72014-05-23 17:42:28 -07005677 info[1] += renderNode.getDebugSize();
Romain Guy65b345f2011-07-27 18:51:50 -07005678 }
5679
5680 if (view instanceof ViewGroup) {
5681 ViewGroup group = (ViewGroup) view;
5682
5683 int count = group.getChildCount();
5684 for (int i = 0; i < count; i++) {
5685 getGfxInfo(group.getChildAt(i), info);
5686 }
5687 }
5688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005689
Craig Mautner8f303ad2013-06-14 11:32:22 -07005690 /**
5691 * @param immediate True, do now if not in traversal. False, put on queue and do later.
5692 * @return True, request has been queued. False, request has been completed.
5693 */
5694 boolean die(boolean immediate) {
Craig Mautnerdf2390a2012-08-29 20:59:22 -07005695 // Make sure we do execute immediately if we are in the middle of a traversal or the damage
5696 // done by dispatchDetachedFromWindow will cause havoc on return.
5697 if (immediate && !mIsInTraversal) {
Dianne Hackborn94d69142009-09-28 22:14:42 -07005698 doDie();
Craig Mautner8f303ad2013-06-14 11:32:22 -07005699 return false;
Dianne Hackborn94d69142009-09-28 22:14:42 -07005700 }
Craig Mautner8f303ad2013-06-14 11:32:22 -07005701
5702 if (!mIsDrawing) {
5703 destroyHardwareRenderer();
5704 } else {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08005705 Log.e(mTag, "Attempting to destroy the window while drawing!\n" +
Craig Mautner8f303ad2013-06-14 11:32:22 -07005706 " window=" + this + ", title=" + mWindowAttributes.getTitle());
5707 }
5708 mHandler.sendEmptyMessage(MSG_DIE);
5709 return true;
Dianne Hackborn94d69142009-09-28 22:14:42 -07005710 }
5711
5712 void doDie() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005713 checkThread();
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08005714 if (LOCAL_LOGV) Log.v(mTag, "DIE in " + this + " of " + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005715 synchronized (this) {
Craig Mautner8f303ad2013-06-14 11:32:22 -07005716 if (mRemoved) {
5717 return;
5718 }
5719 mRemoved = true;
Romain Guy16260e72011-09-01 14:26:11 -07005720 if (mAdded) {
Romain Guy16260e72011-09-01 14:26:11 -07005721 dispatchDetachedFromWindow();
5722 }
5723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005724 if (mAdded && !mFirst) {
Romain Guy29d89972010-09-22 16:10:57 -07005725 destroyHardwareRenderer();
5726
Romain Guyedbca122012-04-04 18:25:53 -07005727 if (mView != null) {
5728 int viewVisibility = mView.getVisibility();
5729 boolean viewVisibilityChanged = mViewVisibility != viewVisibility;
5730 if (mWindowAttributesChanged || viewVisibilityChanged) {
5731 // If layout params have been changed, first give them
5732 // to the window manager to make sure it has the correct
5733 // animation info.
5734 try {
5735 if ((relayoutWindow(mWindowAttributes, viewVisibility, false)
Jeff Brown98365d72012-08-19 20:30:52 -07005736 & WindowManagerGlobal.RELAYOUT_RES_FIRST_TIME) != 0) {
5737 mWindowSession.finishDrawing(mWindow);
Romain Guyedbca122012-04-04 18:25:53 -07005738 }
5739 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005741 }
Craig Mautner8f303ad2013-06-14 11:32:22 -07005742
Romain Guyedbca122012-04-04 18:25:53 -07005743 mSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005745 }
Romain Guyedbca122012-04-04 18:25:53 -07005746
5747 mAdded = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005748 }
Craig Mautner05eb7302013-06-03 17:24:21 -07005749 WindowManagerGlobal.getInstance().doRemoveView(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005750 }
5751
Dianne Hackborn5fd21692011-06-07 14:09:47 -07005752 public void requestUpdateConfiguration(Configuration config) {
Jeff Browna175a5b2012-02-15 19:18:31 -08005753 Message msg = mHandler.obtainMessage(MSG_UPDATE_CONFIGURATION, config);
5754 mHandler.sendMessage(msg);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07005755 }
5756
Dianne Hackborna53de062012-05-08 18:53:51 -07005757 public void loadSystemProperties() {
Romain Guy5bb3c732012-11-29 17:52:58 -08005758 mHandler.post(new Runnable() {
5759 @Override
5760 public void run() {
5761 // Profiling
5762 mProfileRendering = SystemProperties.getBoolean(PROPERTY_PROFILE_RENDERING, false);
5763 profileRendering(mAttachInfo.mHasWindowFocus);
5764
5765 // Hardware rendering
5766 if (mAttachInfo.mHardwareRenderer != null) {
John Reckcec24ae2013-11-05 13:27:50 -08005767 if (mAttachInfo.mHardwareRenderer.loadSystemProperties()) {
Romain Guy5bb3c732012-11-29 17:52:58 -08005768 invalidate();
5769 }
5770 }
5771
5772 // Layout debugging
5773 boolean layout = SystemProperties.getBoolean(View.DEBUG_LAYOUT_PROPERTY, false);
5774 if (layout != mAttachInfo.mDebugLayout) {
5775 mAttachInfo.mDebugLayout = layout;
5776 if (!mHandler.hasMessages(MSG_INVALIDATE_WORLD)) {
5777 mHandler.sendEmptyMessageDelayed(MSG_INVALIDATE_WORLD, 200);
5778 }
5779 }
Dianne Hackborna53de062012-05-08 18:53:51 -07005780 }
Romain Guy5bb3c732012-11-29 17:52:58 -08005781 });
Dianne Hackborna53de062012-05-08 18:53:51 -07005782 }
5783
Romain Guy29d89972010-09-22 16:10:57 -07005784 private void destroyHardwareRenderer() {
John Reck51aaf902015-12-02 15:08:07 -08005785 ThreadedRenderer hardwareRenderer = mAttachInfo.mHardwareRenderer;
Romain Guya998dff2012-03-23 18:58:36 -07005786
5787 if (hardwareRenderer != null) {
5788 if (mView != null) {
5789 hardwareRenderer.destroyHardwareResources(mView);
5790 }
John Reckf47a5942014-06-30 16:20:04 -07005791 hardwareRenderer.destroy();
Romain Guya998dff2012-03-23 18:58:36 -07005792 hardwareRenderer.setRequested(false);
5793
Chris Craikd36a81f2014-07-17 10:16:51 -07005794 mAttachInfo.mHardwareRenderer = null;
5795 mAttachInfo.mHardwareAccelerated = false;
Romain Guy29d89972010-09-22 16:10:57 -07005796 }
5797 }
5798
Jeff Browna175a5b2012-02-15 19:18:31 -08005799 public void dispatchFinishInputConnection(InputConnection connection) {
5800 Message msg = mHandler.obtainMessage(MSG_FINISH_INPUT_CONNECTION, connection);
5801 mHandler.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005802 }
Mitsuru Oshima3d914922009-05-13 22:29:15 -07005803
Dianne Hackbornc4aad012013-02-22 15:05:25 -08005804 public void dispatchResized(Rect frame, Rect overscanInsets, Rect contentInsets,
Filip Gruszczynski2217f612015-05-26 11:32:08 -07005805 Rect visibleInsets, Rect stableInsets, Rect outsets, boolean reportDraw,
Jorim Jaggi253a20f2015-11-03 12:38:42 +01005806 Configuration newConfig, Rect backDropFrame) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08005807 if (DEBUG_LAYOUT) Log.v(mTag, "Resizing " + this + ": frame=" + frame.toShortString()
Svetoslav Ganov758143e2012-08-06 16:40:27 -07005808 + " contentInsets=" + contentInsets.toShortString()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005809 + " visibleInsets=" + visibleInsets.toShortString()
Chong Zhangd153c4f2015-11-06 20:26:40 -08005810 + " reportDraw=" + reportDraw
5811 + " backDropFrame=" + backDropFrame);
Chong Zhangdcee1de2015-10-06 10:26:00 -07005812
5813 // Tell all listeners that we are resizing the window so that the chrome can get
5814 // updated as fast as possible on a separate thread,
5815 if (mDragResizing) {
5816 synchronized (mWindowCallbacks) {
5817 for (int i = mWindowCallbacks.size() - 1; i >= 0; i--) {
Jorim Jaggi253a20f2015-11-03 12:38:42 +01005818 mWindowCallbacks.get(i).onWindowSizeIsChanging(backDropFrame);
Chong Zhangdcee1de2015-10-06 10:26:00 -07005819 }
5820 }
5821 }
5822
Svetoslav Ganov758143e2012-08-06 16:40:27 -07005823 Message msg = mHandler.obtainMessage(reportDraw ? MSG_RESIZED_REPORT : MSG_RESIZED);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07005824 if (mTranslator != null) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07005825 mTranslator.translateRectInScreenToAppWindow(frame);
Dianne Hackbornc4aad012013-02-22 15:05:25 -08005826 mTranslator.translateRectInScreenToAppWindow(overscanInsets);
Dianne Hackborn3556c9a2012-05-04 16:31:25 -07005827 mTranslator.translateRectInScreenToAppWindow(contentInsets);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07005828 mTranslator.translateRectInScreenToAppWindow(visibleInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07005829 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07005830 SomeArgs args = SomeArgs.obtain();
5831 final boolean sameProcessCall = (Binder.getCallingPid() == android.os.Process.myPid());
5832 args.arg1 = sameProcessCall ? new Rect(frame) : frame;
5833 args.arg2 = sameProcessCall ? new Rect(contentInsets) : contentInsets;
5834 args.arg3 = sameProcessCall ? new Rect(visibleInsets) : visibleInsets;
5835 args.arg4 = sameProcessCall && newConfig != null ? new Configuration(newConfig) : newConfig;
Dianne Hackbornc4aad012013-02-22 15:05:25 -08005836 args.arg5 = sameProcessCall ? new Rect(overscanInsets) : overscanInsets;
Adrian Roosfa104232014-06-20 16:10:14 -07005837 args.arg6 = sameProcessCall ? new Rect(stableInsets) : stableInsets;
Filip Gruszczynski2217f612015-05-26 11:32:08 -07005838 args.arg7 = sameProcessCall ? new Rect(outsets) : outsets;
Jorim Jaggia7262a82015-11-03 15:15:40 +01005839 args.arg8 = sameProcessCall ? new Rect(backDropFrame) : backDropFrame;
Svetoslav Ganov758143e2012-08-06 16:40:27 -07005840 msg.obj = args;
Jeff Browna175a5b2012-02-15 19:18:31 -08005841 mHandler.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005842 }
Chet Haase1f4786b2011-11-02 10:51:52 -07005843
Craig Mautner5702d4d2012-06-30 14:10:16 -07005844 public void dispatchMoved(int newX, int newY) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08005845 if (DEBUG_LAYOUT) Log.v(mTag, "Window moved " + this + ": newX=" + newX + " newY=" + newY);
Craig Mautner5702d4d2012-06-30 14:10:16 -07005846 if (mTranslator != null) {
5847 PointF point = new PointF(newX, newY);
5848 mTranslator.translatePointInScreenToAppWindow(point);
5849 newX = (int) (point.x + 0.5);
5850 newY = (int) (point.y + 0.5);
5851 }
5852 Message msg = mHandler.obtainMessage(MSG_WINDOW_MOVED, newX, newY);
5853 mHandler.sendMessage(msg);
5854 }
5855
Jeff Brown4952dfd2011-11-30 19:23:22 -08005856 /**
5857 * Represents a pending input event that is waiting in a queue.
5858 *
5859 * Input events are processed in serial order by the timestamp specified by
Romain Guy211370f2012-02-01 16:10:55 -08005860 * {@link InputEvent#getEventTimeNano()}. In general, the input dispatcher delivers
Jeff Brown4952dfd2011-11-30 19:23:22 -08005861 * one input event to the application at a time and waits for the application
5862 * to finish handling it before delivering the next one.
5863 *
5864 * However, because the application or IME can synthesize and inject multiple
5865 * key events at a time without going through the input dispatcher, we end up
5866 * needing a queue on the application's side.
5867 */
5868 private static final class QueuedInputEvent {
Jeff Brownf9e989d2013-04-04 23:04:03 -07005869 public static final int FLAG_DELIVER_POST_IME = 1 << 0;
5870 public static final int FLAG_DEFERRED = 1 << 1;
5871 public static final int FLAG_FINISHED = 1 << 2;
5872 public static final int FLAG_FINISHED_HANDLED = 1 << 3;
5873 public static final int FLAG_RESYNTHESIZED = 1 << 4;
Michael Wright899d7052014-04-23 17:23:39 -07005874 public static final int FLAG_UNHANDLED = 1 << 5;
Jeff Brown4952dfd2011-11-30 19:23:22 -08005875
5876 public QueuedInputEvent mNext;
5877
5878 public InputEvent mEvent;
Jeff Brown32cbc38552011-12-01 14:01:49 -08005879 public InputEventReceiver mReceiver;
Jeff Brown4952dfd2011-11-30 19:23:22 -08005880 public int mFlags;
Jeff Brownf9e989d2013-04-04 23:04:03 -07005881
5882 public boolean shouldSkipIme() {
5883 if ((mFlags & FLAG_DELIVER_POST_IME) != 0) {
5884 return true;
5885 }
5886 return mEvent instanceof MotionEvent
5887 && mEvent.isFromSource(InputDevice.SOURCE_CLASS_POINTER);
5888 }
Michael Wright899d7052014-04-23 17:23:39 -07005889
5890 public boolean shouldSendToSynthesizer() {
5891 if ((mFlags & FLAG_UNHANDLED) != 0) {
5892 return true;
5893 }
5894
5895 return false;
5896 }
Michael Wright06a79252014-05-05 17:45:29 -07005897
5898 @Override
5899 public String toString() {
5900 StringBuilder sb = new StringBuilder("QueuedInputEvent{flags=");
5901 boolean hasPrevious = false;
5902 hasPrevious = flagToString("DELIVER_POST_IME", FLAG_DELIVER_POST_IME, hasPrevious, sb);
5903 hasPrevious = flagToString("DEFERRED", FLAG_DEFERRED, hasPrevious, sb);
5904 hasPrevious = flagToString("FINISHED", FLAG_FINISHED, hasPrevious, sb);
5905 hasPrevious = flagToString("FINISHED_HANDLED", FLAG_FINISHED_HANDLED, hasPrevious, sb);
5906 hasPrevious = flagToString("RESYNTHESIZED", FLAG_RESYNTHESIZED, hasPrevious, sb);
5907 hasPrevious = flagToString("UNHANDLED", FLAG_UNHANDLED, hasPrevious, sb);
5908 if (!hasPrevious) {
5909 sb.append("0");
5910 }
5911 sb.append(", hasNextQueuedEvent=" + (mEvent != null ? "true" : "false"));
5912 sb.append(", hasInputEventReceiver=" + (mReceiver != null ? "true" : "false"));
5913 sb.append(", mEvent=" + mEvent + "}");
5914 return sb.toString();
5915 }
5916
5917 private boolean flagToString(String name, int flag,
5918 boolean hasPrevious, StringBuilder sb) {
5919 if ((mFlags & flag) != 0) {
5920 if (hasPrevious) {
5921 sb.append("|");
5922 }
5923 sb.append(name);
5924 return true;
5925 }
5926 return hasPrevious;
5927 }
Jeff Brown4952dfd2011-11-30 19:23:22 -08005928 }
5929
5930 private QueuedInputEvent obtainQueuedInputEvent(InputEvent event,
Jeff Brown32cbc38552011-12-01 14:01:49 -08005931 InputEventReceiver receiver, int flags) {
Jeff Brown4952dfd2011-11-30 19:23:22 -08005932 QueuedInputEvent q = mQueuedInputEventPool;
5933 if (q != null) {
5934 mQueuedInputEventPoolSize -= 1;
5935 mQueuedInputEventPool = q.mNext;
5936 q.mNext = null;
5937 } else {
5938 q = new QueuedInputEvent();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005939 }
5940
Jeff Brown4952dfd2011-11-30 19:23:22 -08005941 q.mEvent = event;
Jeff Brown32cbc38552011-12-01 14:01:49 -08005942 q.mReceiver = receiver;
Jeff Brown4952dfd2011-11-30 19:23:22 -08005943 q.mFlags = flags;
Jeff Brown4952dfd2011-11-30 19:23:22 -08005944 return q;
5945 }
5946
5947 private void recycleQueuedInputEvent(QueuedInputEvent q) {
5948 q.mEvent = null;
Jeff Brown32cbc38552011-12-01 14:01:49 -08005949 q.mReceiver = null;
Jeff Brown4952dfd2011-11-30 19:23:22 -08005950
5951 if (mQueuedInputEventPoolSize < MAX_QUEUED_INPUT_EVENT_POOL_SIZE) {
5952 mQueuedInputEventPoolSize += 1;
5953 q.mNext = mQueuedInputEventPool;
5954 mQueuedInputEventPool = q;
5955 }
5956 }
5957
Jeff Brownf9261d22012-02-03 13:49:15 -08005958 void enqueueInputEvent(InputEvent event) {
5959 enqueueInputEvent(event, null, 0, false);
5960 }
5961
Jeff Brown4952dfd2011-11-30 19:23:22 -08005962 void enqueueInputEvent(InputEvent event,
Jeff Brownf9261d22012-02-03 13:49:15 -08005963 InputEventReceiver receiver, int flags, boolean processImmediately) {
Michael Wright5bd69e62015-05-14 14:48:08 +01005964 adjustInputEventForCompatibility(event);
Jeff Brown32cbc38552011-12-01 14:01:49 -08005965 QueuedInputEvent q = obtainQueuedInputEvent(event, receiver, flags);
Jeff Brown4952dfd2011-11-30 19:23:22 -08005966
Jeff Brown4952dfd2011-11-30 19:23:22 -08005967 // Always enqueue the input event in order, regardless of its time stamp.
5968 // We do this because the application or the IME may inject key events
5969 // in response to touch events and we want to ensure that the injected keys
5970 // are processed in the order they were received and we cannot trust that
5971 // the time stamp of injected events are monotonic.
Michael Wrightc8a7e542013-03-20 17:58:33 -07005972 QueuedInputEvent last = mPendingInputEventTail;
Jeff Brown4952dfd2011-11-30 19:23:22 -08005973 if (last == null) {
Michael Wrightc8a7e542013-03-20 17:58:33 -07005974 mPendingInputEventHead = q;
5975 mPendingInputEventTail = q;
Jeff Brown4952dfd2011-11-30 19:23:22 -08005976 } else {
Jeff Brown4952dfd2011-11-30 19:23:22 -08005977 last.mNext = q;
Michael Wrightc8a7e542013-03-20 17:58:33 -07005978 mPendingInputEventTail = q;
Jeff Brown4952dfd2011-11-30 19:23:22 -08005979 }
Michael Wright95ae9422013-03-14 10:58:50 -07005980 mPendingInputEventCount += 1;
5981 Trace.traceCounter(Trace.TRACE_TAG_INPUT, mPendingInputEventQueueLengthCounterName,
5982 mPendingInputEventCount);
Jeff Brown4952dfd2011-11-30 19:23:22 -08005983
Jeff Brownf9261d22012-02-03 13:49:15 -08005984 if (processImmediately) {
5985 doProcessInputEvents();
5986 } else {
5987 scheduleProcessInputEvents();
5988 }
Jeff Brown4952dfd2011-11-30 19:23:22 -08005989 }
5990
5991 private void scheduleProcessInputEvents() {
Jeff Brown96e942d2011-11-30 19:55:01 -08005992 if (!mProcessInputEventsScheduled) {
5993 mProcessInputEventsScheduled = true;
Jeff Brownebb2d8d2012-03-23 17:14:34 -07005994 Message msg = mHandler.obtainMessage(MSG_PROCESS_INPUT_EVENTS);
5995 msg.setAsynchronous(true);
5996 mHandler.sendMessage(msg);
Jeff Brown4952dfd2011-11-30 19:23:22 -08005997 }
5998 }
5999
Jeff Brownebb2d8d2012-03-23 17:14:34 -07006000 void doProcessInputEvents() {
Jeff Brownf9e989d2013-04-04 23:04:03 -07006001 // Deliver all pending input events in the queue.
Michael Wrightc8a7e542013-03-20 17:58:33 -07006002 while (mPendingInputEventHead != null) {
6003 QueuedInputEvent q = mPendingInputEventHead;
6004 mPendingInputEventHead = q.mNext;
6005 if (mPendingInputEventHead == null) {
6006 mPendingInputEventTail = null;
6007 }
Jeff Brown4952dfd2011-11-30 19:23:22 -08006008 q.mNext = null;
Jeff Brown29c0ed22013-01-14 13:50:37 -08006009
Michael Wright95ae9422013-03-14 10:58:50 -07006010 mPendingInputEventCount -= 1;
6011 Trace.traceCounter(Trace.TRACE_TAG_INPUT, mPendingInputEventQueueLengthCounterName,
6012 mPendingInputEventCount);
6013
John Reckba6adf62015-02-19 14:36:50 -08006014 long eventTime = q.mEvent.getEventTimeNano();
6015 long oldestEventTime = eventTime;
6016 if (q.mEvent instanceof MotionEvent) {
6017 MotionEvent me = (MotionEvent)q.mEvent;
6018 if (me.getHistorySize() > 0) {
6019 oldestEventTime = me.getHistoricalEventTimeNano(0);
6020 }
6021 }
6022 mChoreographer.mFrameInfo.updateInputEventTime(eventTime, oldestEventTime);
6023
Jeff Brownf9e989d2013-04-04 23:04:03 -07006024 deliverInputEvent(q);
Jeff Brown4952dfd2011-11-30 19:23:22 -08006025 }
6026
6027 // We are done processing all input events that we can process right now
6028 // so we can clear the pending flag immediately.
Jeff Brown96e942d2011-11-30 19:55:01 -08006029 if (mProcessInputEventsScheduled) {
6030 mProcessInputEventsScheduled = false;
Jeff Browna175a5b2012-02-15 19:18:31 -08006031 mHandler.removeMessages(MSG_PROCESS_INPUT_EVENTS);
Jeff Brown4952dfd2011-11-30 19:23:22 -08006032 }
6033 }
6034
Jeff Brownf9e989d2013-04-04 23:04:03 -07006035 private void deliverInputEvent(QueuedInputEvent q) {
Michael Wrightd2c3adc2014-02-18 22:50:50 -08006036 Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW, "deliverInputEvent",
6037 q.mEvent.getSequenceNumber());
6038 if (mInputEventConsistencyVerifier != null) {
6039 mInputEventConsistencyVerifier.onInputEvent(q.mEvent, 0);
6040 }
Michael Wrightc8a7e542013-03-20 17:58:33 -07006041
Michael Wright899d7052014-04-23 17:23:39 -07006042 InputStage stage;
6043 if (q.shouldSendToSynthesizer()) {
6044 stage = mSyntheticInputStage;
6045 } else {
6046 stage = q.shouldSkipIme() ? mFirstPostImeInputStage : mFirstInputStage;
6047 }
6048
Michael Wrightd2c3adc2014-02-18 22:50:50 -08006049 if (stage != null) {
6050 stage.deliver(q);
6051 } else {
6052 finishInputEvent(q);
Michael Wrightbf020962013-03-28 17:27:50 -07006053 }
Michael Wrightbf020962013-03-28 17:27:50 -07006054 }
6055
Jeff Brownf9e989d2013-04-04 23:04:03 -07006056 private void finishInputEvent(QueuedInputEvent q) {
Michael Wrightd2c3adc2014-02-18 22:50:50 -08006057 Trace.asyncTraceEnd(Trace.TRACE_TAG_VIEW, "deliverInputEvent",
6058 q.mEvent.getSequenceNumber());
Michael Wright9d744c72014-02-18 21:27:42 -08006059
Jeff Brown32cbc38552011-12-01 14:01:49 -08006060 if (q.mReceiver != null) {
Jeff Brownf9e989d2013-04-04 23:04:03 -07006061 boolean handled = (q.mFlags & QueuedInputEvent.FLAG_FINISHED_HANDLED) != 0;
Jeff Brown32cbc38552011-12-01 14:01:49 -08006062 q.mReceiver.finishInputEvent(q.mEvent, handled);
Jeff Brown92cc2d82011-12-02 01:19:47 -08006063 } else {
6064 q.mEvent.recycleIfNeededAfterDispatch();
Jeff Brown4952dfd2011-11-30 19:23:22 -08006065 }
6066
6067 recycleQueuedInputEvent(q);
Jeff Brown29c0ed22013-01-14 13:50:37 -08006068 }
Jeff Brown4952dfd2011-11-30 19:23:22 -08006069
Michael Wright5bd69e62015-05-14 14:48:08 +01006070 private void adjustInputEventForCompatibility(InputEvent e) {
Dianne Hackborn0e3de6c2015-07-29 15:20:21 -07006071 if (mTargetSdkVersion < Build.VERSION_CODES.M && e instanceof MotionEvent) {
Michael Wright5bd69e62015-05-14 14:48:08 +01006072 MotionEvent motion = (MotionEvent) e;
6073 final int mask =
6074 MotionEvent.BUTTON_STYLUS_PRIMARY | MotionEvent.BUTTON_STYLUS_SECONDARY;
6075 final int buttonState = motion.getButtonState();
6076 final int compatButtonState = (buttonState & mask) >> 4;
6077 if (compatButtonState != 0) {
6078 motion.setButtonState(buttonState | compatButtonState);
6079 }
6080 }
6081 }
6082
Jeff Brownf9e989d2013-04-04 23:04:03 -07006083 static boolean isTerminalInputEvent(InputEvent event) {
Jeff Brown29c0ed22013-01-14 13:50:37 -08006084 if (event instanceof KeyEvent) {
6085 final KeyEvent keyEvent = (KeyEvent)event;
6086 return keyEvent.getAction() == KeyEvent.ACTION_UP;
6087 } else {
6088 final MotionEvent motionEvent = (MotionEvent)event;
6089 final int action = motionEvent.getAction();
6090 return action == MotionEvent.ACTION_UP
6091 || action == MotionEvent.ACTION_CANCEL
6092 || action == MotionEvent.ACTION_HOVER_EXIT;
Jeff Brown4952dfd2011-11-30 19:23:22 -08006093 }
6094 }
6095
Jeff Brownebb2d8d2012-03-23 17:14:34 -07006096 void scheduleConsumeBatchedInput() {
6097 if (!mConsumeBatchedInputScheduled) {
6098 mConsumeBatchedInputScheduled = true;
6099 mChoreographer.postCallback(Choreographer.CALLBACK_INPUT,
6100 mConsumedBatchedInputRunnable, null);
Jeff Brown4a06c802012-02-15 15:06:01 -08006101 }
6102 }
Jeff Brownebb2d8d2012-03-23 17:14:34 -07006103
6104 void unscheduleConsumeBatchedInput() {
6105 if (mConsumeBatchedInputScheduled) {
6106 mConsumeBatchedInputScheduled = false;
6107 mChoreographer.removeCallbacks(Choreographer.CALLBACK_INPUT,
6108 mConsumedBatchedInputRunnable, null);
6109 }
6110 }
6111
Michael Wright9d744c72014-02-18 21:27:42 -08006112 void scheduleConsumeBatchedInputImmediately() {
6113 if (!mConsumeBatchedInputImmediatelyScheduled) {
6114 unscheduleConsumeBatchedInput();
6115 mConsumeBatchedInputImmediatelyScheduled = true;
6116 mHandler.post(mConsumeBatchedInputImmediatelyRunnable);
6117 }
6118 }
6119
Jeff Brown771526c2012-04-27 15:13:25 -07006120 void doConsumeBatchedInput(long frameTimeNanos) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07006121 if (mConsumeBatchedInputScheduled) {
6122 mConsumeBatchedInputScheduled = false;
Jeff Brown330314c2012-04-27 02:20:22 -07006123 if (mInputEventReceiver != null) {
Michael Wright9d744c72014-02-18 21:27:42 -08006124 if (mInputEventReceiver.consumeBatchedInputEvents(frameTimeNanos)
6125 && frameTimeNanos != -1) {
Michael Wright62ce65d2013-10-25 14:50:36 -07006126 // If we consumed a batch here, we want to go ahead and schedule the
6127 // consumption of batched input events on the next frame. Otherwise, we would
6128 // wait until we have more input events pending and might get starved by other
Michael Wright9d744c72014-02-18 21:27:42 -08006129 // things occurring in the process. If the frame time is -1, however, then
6130 // we're in a non-batching mode, so there's no need to schedule this.
Michael Wright62ce65d2013-10-25 14:50:36 -07006131 scheduleConsumeBatchedInput();
6132 }
Jeff Brownebb2d8d2012-03-23 17:14:34 -07006133 }
Jeff Brown330314c2012-04-27 02:20:22 -07006134 doProcessInputEvents();
Jeff Brownebb2d8d2012-03-23 17:14:34 -07006135 }
6136 }
6137
6138 final class TraversalRunnable implements Runnable {
6139 @Override
6140 public void run() {
6141 doTraversal();
6142 }
6143 }
6144 final TraversalRunnable mTraversalRunnable = new TraversalRunnable();
Jeff Brown4a06c802012-02-15 15:06:01 -08006145
Jeff Brown32cbc38552011-12-01 14:01:49 -08006146 final class WindowInputEventReceiver extends InputEventReceiver {
6147 public WindowInputEventReceiver(InputChannel inputChannel, Looper looper) {
6148 super(inputChannel, looper);
Jeff Brown46b9ac02010-04-22 18:58:52 -07006149 }
Jeff Brown32cbc38552011-12-01 14:01:49 -08006150
6151 @Override
6152 public void onInputEvent(InputEvent event) {
Jeff Brownf9261d22012-02-03 13:49:15 -08006153 enqueueInputEvent(event, this, 0, true);
Jeff Brown32cbc38552011-12-01 14:01:49 -08006154 }
Jeff Brown072ec962012-02-07 14:46:57 -08006155
6156 @Override
6157 public void onBatchedInputEventPending() {
Michael Wright9d744c72014-02-18 21:27:42 -08006158 if (mUnbufferedInputDispatch) {
6159 super.onBatchedInputEventPending();
6160 } else {
6161 scheduleConsumeBatchedInput();
6162 }
Jeff Brownebb2d8d2012-03-23 17:14:34 -07006163 }
6164
6165 @Override
6166 public void dispose() {
6167 unscheduleConsumeBatchedInput();
6168 super.dispose();
Jeff Brown072ec962012-02-07 14:46:57 -08006169 }
Jeff Brown32cbc38552011-12-01 14:01:49 -08006170 }
6171 WindowInputEventReceiver mInputEventReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006172
Jeff Brownebb2d8d2012-03-23 17:14:34 -07006173 final class ConsumeBatchedInputRunnable implements Runnable {
6174 @Override
6175 public void run() {
Jeff Brown771526c2012-04-27 15:13:25 -07006176 doConsumeBatchedInput(mChoreographer.getFrameTimeNanos());
Jeff Brownebb2d8d2012-03-23 17:14:34 -07006177 }
6178 }
6179 final ConsumeBatchedInputRunnable mConsumedBatchedInputRunnable =
6180 new ConsumeBatchedInputRunnable();
6181 boolean mConsumeBatchedInputScheduled;
6182
Michael Wright9d744c72014-02-18 21:27:42 -08006183 final class ConsumeBatchedInputImmediatelyRunnable implements Runnable {
6184 @Override
6185 public void run() {
6186 doConsumeBatchedInput(-1);
6187 }
6188 }
6189 final ConsumeBatchedInputImmediatelyRunnable mConsumeBatchedInputImmediatelyRunnable =
6190 new ConsumeBatchedInputImmediatelyRunnable();
6191 boolean mConsumeBatchedInputImmediatelyScheduled;
6192
Jeff Brown6cb7b462012-03-05 13:21:17 -08006193 final class InvalidateOnAnimationRunnable implements Runnable {
6194 private boolean mPosted;
Igor Murashkina86ab6402013-08-30 12:58:36 -07006195 private final ArrayList<View> mViews = new ArrayList<View>();
6196 private final ArrayList<AttachInfo.InvalidateInfo> mViewRects =
Jeff Brown6cb7b462012-03-05 13:21:17 -08006197 new ArrayList<AttachInfo.InvalidateInfo>();
6198 private View[] mTempViews;
6199 private AttachInfo.InvalidateInfo[] mTempViewRects;
6200
6201 public void addView(View view) {
6202 synchronized (this) {
6203 mViews.add(view);
6204 postIfNeededLocked();
6205 }
6206 }
6207
6208 public void addViewRect(AttachInfo.InvalidateInfo info) {
6209 synchronized (this) {
6210 mViewRects.add(info);
6211 postIfNeededLocked();
6212 }
6213 }
6214
6215 public void removeView(View view) {
6216 synchronized (this) {
6217 mViews.remove(view);
6218
6219 for (int i = mViewRects.size(); i-- > 0; ) {
6220 AttachInfo.InvalidateInfo info = mViewRects.get(i);
6221 if (info.target == view) {
6222 mViewRects.remove(i);
Svetoslav Ganovabae2a12012-11-27 16:59:37 -08006223 info.recycle();
Jeff Brown6cb7b462012-03-05 13:21:17 -08006224 }
6225 }
6226
6227 if (mPosted && mViews.isEmpty() && mViewRects.isEmpty()) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07006228 mChoreographer.removeCallbacks(Choreographer.CALLBACK_ANIMATION, this, null);
Jeff Brown6cb7b462012-03-05 13:21:17 -08006229 mPosted = false;
6230 }
6231 }
6232 }
6233
6234 @Override
6235 public void run() {
6236 final int viewCount;
6237 final int viewRectCount;
6238 synchronized (this) {
6239 mPosted = false;
6240
6241 viewCount = mViews.size();
6242 if (viewCount != 0) {
6243 mTempViews = mViews.toArray(mTempViews != null
6244 ? mTempViews : new View[viewCount]);
6245 mViews.clear();
6246 }
6247
6248 viewRectCount = mViewRects.size();
6249 if (viewRectCount != 0) {
6250 mTempViewRects = mViewRects.toArray(mTempViewRects != null
6251 ? mTempViewRects : new AttachInfo.InvalidateInfo[viewRectCount]);
6252 mViewRects.clear();
6253 }
6254 }
6255
6256 for (int i = 0; i < viewCount; i++) {
6257 mTempViews[i].invalidate();
Adam Powell3e3c4ee2012-05-16 17:34:21 -07006258 mTempViews[i] = null;
Jeff Brown6cb7b462012-03-05 13:21:17 -08006259 }
6260
6261 for (int i = 0; i < viewRectCount; i++) {
6262 final View.AttachInfo.InvalidateInfo info = mTempViewRects[i];
6263 info.target.invalidate(info.left, info.top, info.right, info.bottom);
Svetoslav Ganovabae2a12012-11-27 16:59:37 -08006264 info.recycle();
Jeff Brown6cb7b462012-03-05 13:21:17 -08006265 }
6266 }
6267
6268 private void postIfNeededLocked() {
6269 if (!mPosted) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07006270 mChoreographer.postCallback(Choreographer.CALLBACK_ANIMATION, this, null);
Jeff Brown6cb7b462012-03-05 13:21:17 -08006271 mPosted = true;
6272 }
6273 }
6274 }
6275 final InvalidateOnAnimationRunnable mInvalidateOnAnimationRunnable =
6276 new InvalidateOnAnimationRunnable();
6277
Jeff Browna175a5b2012-02-15 19:18:31 -08006278 public void dispatchInvalidateDelayed(View view, long delayMilliseconds) {
6279 Message msg = mHandler.obtainMessage(MSG_INVALIDATE, view);
6280 mHandler.sendMessageDelayed(msg, delayMilliseconds);
6281 }
6282
Jeff Browna175a5b2012-02-15 19:18:31 -08006283 public void dispatchInvalidateRectDelayed(AttachInfo.InvalidateInfo info,
6284 long delayMilliseconds) {
6285 final Message msg = mHandler.obtainMessage(MSG_INVALIDATE_RECT, info);
6286 mHandler.sendMessageDelayed(msg, delayMilliseconds);
6287 }
6288
Jeff Brown6cb7b462012-03-05 13:21:17 -08006289 public void dispatchInvalidateOnAnimation(View view) {
6290 mInvalidateOnAnimationRunnable.addView(view);
6291 }
6292
6293 public void dispatchInvalidateRectOnAnimation(AttachInfo.InvalidateInfo info) {
6294 mInvalidateOnAnimationRunnable.addViewRect(info);
6295 }
6296
6297 public void cancelInvalidate(View view) {
6298 mHandler.removeMessages(MSG_INVALIDATE, view);
6299 // fixme: might leak the AttachInfo.InvalidateInfo objects instead of returning
6300 // them to the pool
6301 mHandler.removeMessages(MSG_INVALIDATE_RECT, view);
6302 mInvalidateOnAnimationRunnable.removeView(view);
6303 }
6304
keunyoung30f420f2013-08-02 14:23:10 -07006305 public void dispatchInputEvent(InputEvent event) {
Jae Seo6a6059a2014-04-17 21:35:29 -07006306 dispatchInputEvent(event, null);
6307 }
6308
6309 public void dispatchInputEvent(InputEvent event, InputEventReceiver receiver) {
6310 SomeArgs args = SomeArgs.obtain();
6311 args.arg1 = event;
6312 args.arg2 = receiver;
6313 Message msg = mHandler.obtainMessage(MSG_DISPATCH_INPUT_EVENT, args);
Jeff Browne0dbd002012-02-15 19:34:58 -08006314 msg.setAsynchronous(true);
Jeff Browna175a5b2012-02-15 19:18:31 -08006315 mHandler.sendMessage(msg);
6316 }
6317
Michael Wright899d7052014-04-23 17:23:39 -07006318 public void synthesizeInputEvent(InputEvent event) {
6319 Message msg = mHandler.obtainMessage(MSG_SYNTHESIZE_INPUT_EVENT, event);
6320 msg.setAsynchronous(true);
6321 mHandler.sendMessage(msg);
6322 }
6323
Jeff Browna175a5b2012-02-15 19:18:31 -08006324 public void dispatchKeyFromIme(KeyEvent event) {
6325 Message msg = mHandler.obtainMessage(MSG_DISPATCH_KEY_FROM_IME, event);
Jeff Browne0dbd002012-02-15 19:34:58 -08006326 msg.setAsynchronous(true);
Jeff Browna175a5b2012-02-15 19:18:31 -08006327 mHandler.sendMessage(msg);
Jeff Browncb1404e2011-01-15 18:14:15 -08006328 }
6329
Michael Wright899d7052014-04-23 17:23:39 -07006330 /**
6331 * Reinject unhandled {@link InputEvent}s in order to synthesize fallbacks events.
6332 *
6333 * Note that it is the responsibility of the caller of this API to recycle the InputEvent it
6334 * passes in.
6335 */
Michael Wright3da28342014-04-22 17:00:11 -07006336 public void dispatchUnhandledInputEvent(InputEvent event) {
Michael Wright899d7052014-04-23 17:23:39 -07006337 if (event instanceof MotionEvent) {
6338 event = MotionEvent.obtain((MotionEvent) event);
Michael Wright3da28342014-04-22 17:00:11 -07006339 }
Michael Wright899d7052014-04-23 17:23:39 -07006340 synthesizeInputEvent(event);
Michael Wright3da28342014-04-22 17:00:11 -07006341 }
6342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006343 public void dispatchAppVisibility(boolean visible) {
Jeff Browna175a5b2012-02-15 19:18:31 -08006344 Message msg = mHandler.obtainMessage(MSG_DISPATCH_APP_VISIBILITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006345 msg.arg1 = visible ? 1 : 0;
Jeff Browna175a5b2012-02-15 19:18:31 -08006346 mHandler.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006347 }
6348
6349 public void dispatchGetNewSurface() {
Jeff Browna175a5b2012-02-15 19:18:31 -08006350 Message msg = mHandler.obtainMessage(MSG_DISPATCH_GET_NEW_SURFACE);
6351 mHandler.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006352 }
6353
6354 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
6355 Message msg = Message.obtain();
Jeff Browna175a5b2012-02-15 19:18:31 -08006356 msg.what = MSG_WINDOW_FOCUS_CHANGED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006357 msg.arg1 = hasFocus ? 1 : 0;
6358 msg.arg2 = inTouchMode ? 1 : 0;
Jeff Browna175a5b2012-02-15 19:18:31 -08006359 mHandler.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006360 }
6361
Craig Mautner9c795042014-10-28 19:59:59 -07006362 public void dispatchWindowShown() {
6363 mHandler.sendEmptyMessage(MSG_DISPATCH_WINDOW_SHOWN);
6364 }
6365
Dianne Hackbornffa42482009-09-23 22:20:11 -07006366 public void dispatchCloseSystemDialogs(String reason) {
6367 Message msg = Message.obtain();
Jeff Browna175a5b2012-02-15 19:18:31 -08006368 msg.what = MSG_CLOSE_SYSTEM_DIALOGS;
Dianne Hackbornffa42482009-09-23 22:20:11 -07006369 msg.obj = reason;
Jeff Browna175a5b2012-02-15 19:18:31 -08006370 mHandler.sendMessage(msg);
Dianne Hackbornffa42482009-09-23 22:20:11 -07006371 }
Christopher Tatea53146c2010-09-07 11:57:52 -07006372
6373 public void dispatchDragEvent(DragEvent event) {
Chris Tate91e9bb32010-10-12 12:58:43 -07006374 final int what;
6375 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
Jeff Browna175a5b2012-02-15 19:18:31 -08006376 what = MSG_DISPATCH_DRAG_LOCATION_EVENT;
6377 mHandler.removeMessages(what);
Chris Tate91e9bb32010-10-12 12:58:43 -07006378 } else {
Jeff Browna175a5b2012-02-15 19:18:31 -08006379 what = MSG_DISPATCH_DRAG_EVENT;
Chris Tate91e9bb32010-10-12 12:58:43 -07006380 }
Jeff Browna175a5b2012-02-15 19:18:31 -08006381 Message msg = mHandler.obtainMessage(what, event);
6382 mHandler.sendMessage(msg);
Christopher Tatea53146c2010-09-07 11:57:52 -07006383 }
6384
Dianne Hackborn9a230e02011-10-06 11:51:27 -07006385 public void dispatchSystemUiVisibilityChanged(int seq, int globalVisibility,
6386 int localValue, int localChanges) {
6387 SystemUiVisibilityInfo args = new SystemUiVisibilityInfo();
6388 args.seq = seq;
6389 args.globalVisibility = globalVisibility;
6390 args.localValue = localValue;
6391 args.localChanges = localChanges;
Jeff Browna175a5b2012-02-15 19:18:31 -08006392 mHandler.sendMessage(mHandler.obtainMessage(MSG_DISPATCH_SYSTEM_UI_VISIBILITY, args));
6393 }
6394
6395 public void dispatchCheckFocus() {
6396 if (!mHandler.hasMessages(MSG_CHECK_FOCUS)) {
6397 // This will result in a call to checkFocus() below.
6398 mHandler.sendEmptyMessage(MSG_CHECK_FOCUS);
6399 }
Joe Onorato664644d2011-01-23 17:53:23 -08006400 }
6401
Clara Bayarri75e09792015-07-29 16:20:40 +01006402 public void dispatchRequestKeyboardShortcuts(IResultReceiver receiver) {
6403 mHandler.obtainMessage(MSG_REQUEST_KEYBOARD_SHORTCUTS, receiver).sendToTarget();
6404 }
6405
svetoslavganov75986cf2009-05-14 22:28:01 -07006406 /**
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07006407 * Post a callback to send a
6408 * {@link AccessibilityEvent#TYPE_WINDOW_CONTENT_CHANGED} event.
Svetoslav Ganova0156172011-06-26 17:55:44 -07006409 * This event is send at most once every
6410 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07006411 */
Alan Viverette77e9a282013-09-12 17:16:09 -07006412 private void postSendWindowContentChangedCallback(View source, int changeType) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006413 if (mSendWindowContentChangedAccessibilityEvent == null) {
6414 mSendWindowContentChangedAccessibilityEvent =
6415 new SendWindowContentChangedAccessibilityEvent();
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07006416 }
Alan Viverette77e9a282013-09-12 17:16:09 -07006417 mSendWindowContentChangedAccessibilityEvent.runOrPost(source, changeType);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07006418 }
6419
6420 /**
6421 * Remove a posted callback to send a
6422 * {@link AccessibilityEvent#TYPE_WINDOW_CONTENT_CHANGED} event.
6423 */
6424 private void removeSendWindowContentChangedCallback() {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006425 if (mSendWindowContentChangedAccessibilityEvent != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08006426 mHandler.removeCallbacks(mSendWindowContentChangedAccessibilityEvent);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07006427 }
6428 }
6429
Igor Murashkina86ab6402013-08-30 12:58:36 -07006430 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006431 public boolean showContextMenuForChild(View originalView) {
6432 return false;
6433 }
6434
Igor Murashkina86ab6402013-08-30 12:58:36 -07006435 @Override
Oren Blasberged391262015-09-01 12:12:51 -07006436 public boolean showContextMenuForChild(View originalView, float x, float y) {
6437 return false;
6438 }
6439
6440 @Override
Adam Powell6e346362010-07-23 10:18:23 -07006441 public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) {
6442 return null;
6443 }
6444
Igor Murashkina86ab6402013-08-30 12:58:36 -07006445 @Override
Clara Bayarri4423d912015-03-02 19:42:48 +00006446 public ActionMode startActionModeForChild(
6447 View originalView, ActionMode.Callback callback, int type) {
6448 return null;
6449 }
6450
6451 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006452 public void createContextMenu(ContextMenu menu) {
6453 }
6454
Igor Murashkina86ab6402013-08-30 12:58:36 -07006455 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006456 public void childDrawableStateChanged(View child) {
6457 }
6458
Igor Murashkina86ab6402013-08-30 12:58:36 -07006459 @Override
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006460 public boolean requestSendAccessibilityEvent(View child, AccessibilityEvent event) {
George Mount41725de2015-04-09 08:23:05 -07006461 if (mView == null || mStopped || mPausedForTransition) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006462 return false;
6463 }
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006464 // Intercept accessibility focus events fired by virtual nodes to keep
6465 // track of accessibility focus position in such nodes.
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006466 final int eventType = event.getEventType();
6467 switch (eventType) {
6468 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006469 final long sourceNodeId = event.getSourceNodeId();
6470 final int accessibilityViewId = AccessibilityNodeInfo.getAccessibilityViewId(
6471 sourceNodeId);
6472 View source = mView.findViewByAccessibilityId(accessibilityViewId);
6473 if (source != null) {
6474 AccessibilityNodeProvider provider = source.getAccessibilityNodeProvider();
6475 if (provider != null) {
Svetoslavb3ba1d42014-09-26 15:20:40 -07006476 final int virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
6477 sourceNodeId);
6478 final AccessibilityNodeInfo node;
6479 if (virtualNodeId == AccessibilityNodeInfo.UNDEFINED_ITEM_ID) {
6480 node = provider.createAccessibilityNodeInfo(
6481 AccessibilityNodeProvider.HOST_VIEW_ID);
6482 } else {
6483 node = provider.createAccessibilityNodeInfo(virtualNodeId);
6484 }
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006485 setAccessibilityFocus(source, node);
6486 }
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006487 }
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006488 } break;
6489 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006490 final long sourceNodeId = event.getSourceNodeId();
6491 final int accessibilityViewId = AccessibilityNodeInfo.getAccessibilityViewId(
6492 sourceNodeId);
6493 View source = mView.findViewByAccessibilityId(accessibilityViewId);
6494 if (source != null) {
6495 AccessibilityNodeProvider provider = source.getAccessibilityNodeProvider();
6496 if (provider != null) {
6497 setAccessibilityFocus(null, null);
6498 }
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006499 }
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006500 } break;
Svetoslavf0c758b2014-09-03 17:47:37 -07006501
6502
6503 case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED: {
Alan Viverette34457f52015-03-25 13:09:20 -07006504 handleWindowContentChangedEvent(event);
Svetoslavf0c758b2014-09-03 17:47:37 -07006505 } break;
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006506 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006507 mAccessibilityManager.sendAccessibilityEvent(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006508 return true;
6509 }
6510
Alan Viverette34457f52015-03-25 13:09:20 -07006511 /**
6512 * Updates the focused virtual view, when necessary, in response to a
6513 * content changed event.
6514 * <p>
6515 * This is necessary to get updated bounds after a position change.
6516 *
6517 * @param event an accessibility event of type
6518 * {@link AccessibilityEvent#TYPE_WINDOW_CONTENT_CHANGED}
6519 */
6520 private void handleWindowContentChangedEvent(AccessibilityEvent event) {
Alan Viverette25acc7e2015-05-19 11:32:08 -07006521 final View focusedHost = mAccessibilityFocusedHost;
6522 if (focusedHost == null || mAccessibilityFocusedVirtualView == null) {
6523 // No virtual view focused, nothing to do here.
Alan Viverette34457f52015-03-25 13:09:20 -07006524 return;
6525 }
6526
Alan Viverette25acc7e2015-05-19 11:32:08 -07006527 final AccessibilityNodeProvider provider = focusedHost.getAccessibilityNodeProvider();
Alan Viverette34457f52015-03-25 13:09:20 -07006528 if (provider == null) {
Alan Viverette25acc7e2015-05-19 11:32:08 -07006529 // Error state: virtual view with no provider. Clear focus.
6530 mAccessibilityFocusedHost = null;
6531 mAccessibilityFocusedVirtualView = null;
6532 focusedHost.clearAccessibilityFocusNoCallbacks();
Alan Viverette34457f52015-03-25 13:09:20 -07006533 return;
6534 }
6535
6536 // We only care about change types that may affect the bounds of the
6537 // focused virtual view.
6538 final int changes = event.getContentChangeTypes();
6539 if ((changes & AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE) == 0
6540 && changes != AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED) {
6541 return;
6542 }
6543
6544 final long eventSourceNodeId = event.getSourceNodeId();
6545 final int changedViewId = AccessibilityNodeInfo.getAccessibilityViewId(eventSourceNodeId);
6546
6547 // Search up the tree for subtree containment.
6548 boolean hostInSubtree = false;
6549 View root = mAccessibilityFocusedHost;
6550 while (root != null && !hostInSubtree) {
6551 if (changedViewId == root.getAccessibilityViewId()) {
6552 hostInSubtree = true;
6553 } else {
6554 final ViewParent parent = root.getParent();
6555 if (parent instanceof View) {
6556 root = (View) parent;
6557 } else {
6558 root = null;
6559 }
6560 }
6561 }
6562
6563 // We care only about changes in subtrees containing the host view.
6564 if (!hostInSubtree) {
6565 return;
6566 }
6567
6568 final long focusedSourceNodeId = mAccessibilityFocusedVirtualView.getSourceNodeId();
6569 int focusedChildId = AccessibilityNodeInfo.getVirtualDescendantId(focusedSourceNodeId);
6570 if (focusedChildId == AccessibilityNodeInfo.UNDEFINED_ITEM_ID) {
6571 // TODO: Should we clear the focused virtual view?
6572 focusedChildId = AccessibilityNodeProvider.HOST_VIEW_ID;
6573 }
6574
6575 // Refresh the node for the focused virtual view.
Alan Viverettea7ea65e2015-05-15 11:30:21 -07006576 final Rect oldBounds = mTempRect;
6577 mAccessibilityFocusedVirtualView.getBoundsInScreen(oldBounds);
Alan Viverette34457f52015-03-25 13:09:20 -07006578 mAccessibilityFocusedVirtualView = provider.createAccessibilityNodeInfo(focusedChildId);
Alan Viverette25acc7e2015-05-19 11:32:08 -07006579 if (mAccessibilityFocusedVirtualView == null) {
6580 // Error state: The node no longer exists. Clear focus.
6581 mAccessibilityFocusedHost = null;
6582 focusedHost.clearAccessibilityFocusNoCallbacks();
6583
6584 // This will probably fail, but try to keep the provider's internal
6585 // state consistent by clearing focus.
6586 provider.performAction(focusedChildId,
6587 AccessibilityAction.ACTION_CLEAR_ACCESSIBILITY_FOCUS.getId(), null);
Alan Viverettea7ea65e2015-05-15 11:30:21 -07006588 invalidateRectOnScreen(oldBounds);
Alan Viverette25acc7e2015-05-19 11:32:08 -07006589 } else {
6590 // The node was refreshed, invalidate bounds if necessary.
6591 final Rect newBounds = mAccessibilityFocusedVirtualView.getBoundsInScreen();
6592 if (!oldBounds.equals(newBounds)) {
6593 oldBounds.union(newBounds);
6594 invalidateRectOnScreen(oldBounds);
6595 }
Alan Viverettea7ea65e2015-05-15 11:30:21 -07006596 }
Alan Viverette34457f52015-03-25 13:09:20 -07006597 }
6598
Svetoslav Ganov42138042012-03-20 11:51:39 -07006599 @Override
Alan Viverette77e9a282013-09-12 17:16:09 -07006600 public void notifySubtreeAccessibilityStateChanged(View child, View source, int changeType) {
6601 postSendWindowContentChangedCallback(source, changeType);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006602 }
6603
Fabrice Di Meglio9dd4c5c2013-02-08 18:15:07 -08006604 @Override
6605 public boolean canResolveLayoutDirection() {
6606 return true;
6607 }
6608
6609 @Override
6610 public boolean isLayoutDirectionResolved() {
6611 return true;
6612 }
6613
6614 @Override
6615 public int getLayoutDirection() {
6616 return View.LAYOUT_DIRECTION_RESOLVED_DEFAULT;
6617 }
6618
6619 @Override
6620 public boolean canResolveTextDirection() {
6621 return true;
6622 }
6623
6624 @Override
6625 public boolean isTextDirectionResolved() {
6626 return true;
6627 }
6628
6629 @Override
6630 public int getTextDirection() {
6631 return View.TEXT_DIRECTION_RESOLVED_DEFAULT;
6632 }
6633
6634 @Override
6635 public boolean canResolveTextAlignment() {
6636 return true;
6637 }
6638
6639 @Override
6640 public boolean isTextAlignmentResolved() {
6641 return true;
6642 }
6643
6644 @Override
6645 public int getTextAlignment() {
6646 return View.TEXT_ALIGNMENT_RESOLVED_DEFAULT;
6647 }
6648
Svetoslav Ganov42138042012-03-20 11:51:39 -07006649 private View getCommonPredecessor(View first, View second) {
Chris Craikd36a81f2014-07-17 10:16:51 -07006650 if (mTempHashSet == null) {
6651 mTempHashSet = new HashSet<View>();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006652 }
Chris Craikd36a81f2014-07-17 10:16:51 -07006653 HashSet<View> seen = mTempHashSet;
6654 seen.clear();
6655 View firstCurrent = first;
6656 while (firstCurrent != null) {
6657 seen.add(firstCurrent);
6658 ViewParent firstCurrentParent = firstCurrent.mParent;
6659 if (firstCurrentParent instanceof View) {
6660 firstCurrent = (View) firstCurrentParent;
6661 } else {
6662 firstCurrent = null;
6663 }
6664 }
6665 View secondCurrent = second;
6666 while (secondCurrent != null) {
6667 if (seen.contains(secondCurrent)) {
6668 seen.clear();
6669 return secondCurrent;
6670 }
6671 ViewParent secondCurrentParent = secondCurrent.mParent;
6672 if (secondCurrentParent instanceof View) {
6673 secondCurrent = (View) secondCurrentParent;
6674 } else {
6675 secondCurrent = null;
6676 }
6677 }
6678 seen.clear();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006679 return null;
6680 }
6681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006682 void checkThread() {
6683 if (mThread != Thread.currentThread()) {
6684 throw new CalledFromWrongThreadException(
6685 "Only the original thread that created a view hierarchy can touch its views.");
6686 }
6687 }
6688
Igor Murashkina86ab6402013-08-30 12:58:36 -07006689 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006690 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07006691 // ViewAncestor never intercepts touch event, so this can be a no-op
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006692 }
6693
Igor Murashkina86ab6402013-08-30 12:58:36 -07006694 @Override
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07006695 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
6696 final boolean scrolled = scrollToRectOrFocus(rectangle, immediate);
6697 if (rectangle != null) {
6698 mTempRect.set(rectangle);
6699 mTempRect.offset(0, -mCurScrollY);
6700 mTempRect.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
6701 try {
Svetoslavf7174e82014-06-12 11:29:35 -07006702 mWindowSession.onRectangleOnScreenRequested(mWindow, mTempRect);
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07006703 } catch (RemoteException re) {
6704 /* ignore */
6705 }
6706 }
6707 return scrolled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006708 }
Romain Guy8506ab42009-06-11 17:35:47 -07006709
Igor Murashkina86ab6402013-08-30 12:58:36 -07006710 @Override
Adam Powell539ee872012-02-03 19:00:49 -08006711 public void childHasTransientStateChanged(View child, boolean hasTransientState) {
6712 // Do nothing.
6713 }
6714
Adam Powell10ba2772014-04-15 09:46:51 -07006715 @Override
6716 public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
6717 return false;
6718 }
6719
6720 @Override
6721 public void onStopNestedScroll(View target) {
6722 }
6723
6724 @Override
6725 public void onNestedScrollAccepted(View child, View target, int nestedScrollAxes) {
6726 }
6727
6728 @Override
6729 public void onNestedScroll(View target, int dxConsumed, int dyConsumed,
6730 int dxUnconsumed, int dyUnconsumed) {
6731 }
6732
6733 @Override
6734 public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
6735 }
6736
6737 @Override
Adam Powellb36e4f92014-05-01 10:23:33 -07006738 public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
Adam Powell10ba2772014-04-15 09:46:51 -07006739 return false;
6740 }
6741
Adam Powellb72be592014-07-16 21:41:31 -07006742 @Override
6743 public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
6744 return false;
6745 }
6746
Adam Powellb6ab0982015-01-07 17:00:12 -08006747 @Override
6748 public boolean onNestedPrePerformAccessibilityAction(View target, int action, Bundle args) {
6749 return false;
6750 }
6751
Jorim Jaggib774e552015-08-24 14:52:45 -07006752 /**
6753 * Force the window to report its next draw.
6754 * <p>
6755 * This method is only supposed to be used to speed up the interaction from SystemUI and window
6756 * manager when waiting for the first frame to be drawn when turning on the screen. DO NOT USE
6757 * unless you fully understand this interaction.
6758 * @hide
6759 */
6760 public void setReportNextDraw() {
6761 mReportNextDraw = true;
6762 invalidate();
6763 }
6764
Craig Mautnerbc57cd12013-08-19 15:47:42 -07006765 void changeCanvasOpacity(boolean opaque) {
Filip Gruszczynski34dab0b2015-12-22 08:29:07 -08006766 Log.d(mTag, "changeCanvasOpacity: opaque=" + opaque);
John Reck63a06672014-05-07 13:45:54 -07006767 if (mAttachInfo.mHardwareRenderer != null) {
6768 mAttachInfo.mHardwareRenderer.setOpaque(opaque);
6769 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -07006770 }
6771
Rob Carr64e516f2015-10-29 00:20:45 +00006772 long getNextFrameNumber() {
6773 long frameNumber = -1;
6774 if (mSurfaceHolder != null) {
6775 mSurfaceHolder.mSurfaceLock.lock();
6776 }
6777 if (mSurface.isValid()) {
6778 frameNumber = mSurface.getNextFrameNumber();
6779 }
6780 if (mSurfaceHolder != null) {
6781 mSurfaceHolder.mSurfaceLock.unlock();
6782 }
6783 return frameNumber;
6784 }
6785
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07006786 class TakenSurfaceHolder extends BaseSurfaceHolder {
6787 @Override
6788 public boolean onAllowLockCanvas() {
6789 return mDrawingAllowed;
6790 }
6791
6792 @Override
6793 public void onRelayoutContainer() {
6794 // Not currently interesting -- from changing between fixed and layout size.
6795 }
6796
Igor Murashkina86ab6402013-08-30 12:58:36 -07006797 @Override
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07006798 public void setFormat(int format) {
6799 ((RootViewSurfaceTaker)mView).setSurfaceFormat(format);
6800 }
6801
Igor Murashkina86ab6402013-08-30 12:58:36 -07006802 @Override
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07006803 public void setType(int type) {
6804 ((RootViewSurfaceTaker)mView).setSurfaceType(type);
6805 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07006806
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07006807 @Override
6808 public void onUpdateSurface() {
6809 // We take care of format and type changes on our own.
6810 throw new IllegalStateException("Shouldn't be here");
6811 }
6812
Igor Murashkina86ab6402013-08-30 12:58:36 -07006813 @Override
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07006814 public boolean isCreating() {
6815 return mIsCreating;
6816 }
6817
6818 @Override
6819 public void setFixedSize(int width, int height) {
6820 throw new UnsupportedOperationException(
6821 "Currently only support sizing from layout");
6822 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07006823
6824 @Override
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07006825 public void setKeepScreenOn(boolean screenOn) {
6826 ((RootViewSurfaceTaker)mView).setSurfaceKeepScreenOn(screenOn);
6827 }
6828 }
Romain Guy8506ab42009-06-11 17:35:47 -07006829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006830 static class W extends IWindow.Stub {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006831 private final WeakReference<ViewRootImpl> mViewAncestor;
Jeff Brown98365d72012-08-19 20:30:52 -07006832 private final IWindowSession mWindowSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006833
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006834 W(ViewRootImpl viewAncestor) {
6835 mViewAncestor = new WeakReference<ViewRootImpl>(viewAncestor);
Jeff Brown98365d72012-08-19 20:30:52 -07006836 mWindowSession = viewAncestor.mWindowSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006837 }
6838
Igor Murashkina86ab6402013-08-30 12:58:36 -07006839 @Override
Dianne Hackbornc4aad012013-02-22 15:05:25 -08006840 public void resized(Rect frame, Rect overscanInsets, Rect contentInsets,
Filip Gruszczynski2217f612015-05-26 11:32:08 -07006841 Rect visibleInsets, Rect stableInsets, Rect outsets, boolean reportDraw,
Jorim Jaggi253a20f2015-11-03 12:38:42 +01006842 Configuration newConfig, Rect backDropFrame) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006843 final ViewRootImpl viewAncestor = mViewAncestor.get();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006844 if (viewAncestor != null) {
Dianne Hackbornc4aad012013-02-22 15:05:25 -08006845 viewAncestor.dispatchResized(frame, overscanInsets, contentInsets,
Jorim Jaggi253a20f2015-11-03 12:38:42 +01006846 visibleInsets, stableInsets, outsets, reportDraw, newConfig, backDropFrame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006847 }
6848 }
6849
Craig Mautner5702d4d2012-06-30 14:10:16 -07006850 @Override
6851 public void moved(int newX, int newY) {
6852 final ViewRootImpl viewAncestor = mViewAncestor.get();
6853 if (viewAncestor != null) {
6854 viewAncestor.dispatchMoved(newX, newY);
6855 }
6856 }
6857
Igor Murashkina86ab6402013-08-30 12:58:36 -07006858 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006859 public void dispatchAppVisibility(boolean visible) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006860 final ViewRootImpl viewAncestor = mViewAncestor.get();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006861 if (viewAncestor != null) {
6862 viewAncestor.dispatchAppVisibility(visible);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006863 }
6864 }
6865
Igor Murashkina86ab6402013-08-30 12:58:36 -07006866 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006867 public void dispatchGetNewSurface() {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006868 final ViewRootImpl viewAncestor = mViewAncestor.get();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006869 if (viewAncestor != null) {
6870 viewAncestor.dispatchGetNewSurface();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006871 }
6872 }
6873
Igor Murashkina86ab6402013-08-30 12:58:36 -07006874 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006875 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006876 final ViewRootImpl viewAncestor = mViewAncestor.get();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006877 if (viewAncestor != null) {
6878 viewAncestor.windowFocusChanged(hasFocus, inTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006879 }
6880 }
6881
6882 private static int checkCallingPermission(String permission) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006883 try {
6884 return ActivityManagerNative.getDefault().checkPermission(
6885 permission, Binder.getCallingPid(), Binder.getCallingUid());
6886 } catch (RemoteException e) {
6887 return PackageManager.PERMISSION_DENIED;
6888 }
6889 }
6890
Igor Murashkina86ab6402013-08-30 12:58:36 -07006891 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006892 public void executeCommand(String command, String parameters, ParcelFileDescriptor out) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006893 final ViewRootImpl viewAncestor = mViewAncestor.get();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006894 if (viewAncestor != null) {
6895 final View view = viewAncestor.mView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006896 if (view != null) {
6897 if (checkCallingPermission(Manifest.permission.DUMP) !=
6898 PackageManager.PERMISSION_GRANTED) {
6899 throw new SecurityException("Insufficient permissions to invoke"
6900 + " executeCommand() from pid=" + Binder.getCallingPid()
6901 + ", uid=" + Binder.getCallingUid());
6902 }
6903
6904 OutputStream clientStream = null;
6905 try {
6906 clientStream = new ParcelFileDescriptor.AutoCloseOutputStream(out);
6907 ViewDebug.dispatchCommand(view, command, parameters, clientStream);
6908 } catch (IOException e) {
6909 e.printStackTrace();
6910 } finally {
6911 if (clientStream != null) {
6912 try {
6913 clientStream.close();
6914 } catch (IOException e) {
6915 e.printStackTrace();
6916 }
6917 }
6918 }
6919 }
6920 }
6921 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07006922
6923 @Override
Dianne Hackbornffa42482009-09-23 22:20:11 -07006924 public void closeSystemDialogs(String reason) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006925 final ViewRootImpl viewAncestor = mViewAncestor.get();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006926 if (viewAncestor != null) {
6927 viewAncestor.dispatchCloseSystemDialogs(reason);
Dianne Hackbornffa42482009-09-23 22:20:11 -07006928 }
6929 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07006930
6931 @Override
Marco Nelissenbf6956b2009-11-09 15:21:13 -08006932 public void dispatchWallpaperOffsets(float x, float y, float xStep, float yStep,
6933 boolean sync) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006934 if (sync) {
6935 try {
Jeff Brown98365d72012-08-19 20:30:52 -07006936 mWindowSession.wallpaperOffsetsComplete(asBinder());
Dianne Hackborn19382ac2009-09-11 21:13:37 -07006937 } catch (RemoteException e) {
6938 }
6939 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07006940 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006941
Igor Murashkina86ab6402013-08-30 12:58:36 -07006942 @Override
Dianne Hackborn75804932009-10-20 20:15:20 -07006943 public void dispatchWallpaperCommand(String action, int x, int y,
6944 int z, Bundle extras, boolean sync) {
6945 if (sync) {
6946 try {
Jeff Brown98365d72012-08-19 20:30:52 -07006947 mWindowSession.wallpaperCommandComplete(asBinder(), null);
Dianne Hackborn75804932009-10-20 20:15:20 -07006948 } catch (RemoteException e) {
6949 }
6950 }
6951 }
Christopher Tatea53146c2010-09-07 11:57:52 -07006952
6953 /* Drag/drop */
Igor Murashkina86ab6402013-08-30 12:58:36 -07006954 @Override
Christopher Tatea53146c2010-09-07 11:57:52 -07006955 public void dispatchDragEvent(DragEvent event) {
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 viewAncestor.dispatchDragEvent(event);
Christopher Tatea53146c2010-09-07 11:57:52 -07006959 }
6960 }
Joe Onorato664644d2011-01-23 17:53:23 -08006961
Igor Murashkina86ab6402013-08-30 12:58:36 -07006962 @Override
Dianne Hackborn9a230e02011-10-06 11:51:27 -07006963 public void dispatchSystemUiVisibilityChanged(int seq, int globalVisibility,
6964 int localValue, int localChanges) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006965 final ViewRootImpl viewAncestor = mViewAncestor.get();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006966 if (viewAncestor != null) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -07006967 viewAncestor.dispatchSystemUiVisibilityChanged(seq, globalVisibility,
6968 localValue, localChanges);
Joe Onorato664644d2011-01-23 17:53:23 -08006969 }
6970 }
Dianne Hackborn12d3a942012-04-27 14:16:30 -07006971
Igor Murashkina86ab6402013-08-30 12:58:36 -07006972 @Override
Craig Mautner9c795042014-10-28 19:59:59 -07006973 public void dispatchWindowShown() {
6974 final ViewRootImpl viewAncestor = mViewAncestor.get();
6975 if (viewAncestor != null) {
6976 viewAncestor.dispatchWindowShown();
6977 }
6978 }
Clara Bayarri75e09792015-07-29 16:20:40 +01006979
6980 @Override
6981 public void requestAppKeyboardShortcuts(IResultReceiver receiver) {
6982 ViewRootImpl viewAncestor = mViewAncestor.get();
6983 if (viewAncestor != null) {
6984 viewAncestor.dispatchRequestKeyboardShortcuts(receiver);
6985 }
6986 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006987 }
6988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006989 public static final class CalledFromWrongThreadException extends AndroidRuntimeException {
6990 public CalledFromWrongThreadException(String msg) {
6991 super(msg);
6992 }
6993 }
6994
Alan Viverettebea0c7da2015-09-01 16:00:20 -04006995 static HandlerActionQueue getRunQueue() {
6996 HandlerActionQueue rq = sRunQueues.get();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006997 if (rq != null) {
6998 return rq;
6999 }
Alan Viverettebea0c7da2015-09-01 16:00:20 -04007000 rq = new HandlerActionQueue();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007001 sRunQueues.set(rq);
7002 return rq;
7003 }
Romain Guy8506ab42009-06-11 17:35:47 -07007004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007005 /**
Skuhneb8160872015-09-22 09:51:39 -07007006 * Start a drag resizing which will inform all listeners that a window resize is taking place.
7007 */
7008 private void startDragResizing(Rect initialBounds) {
7009 if (!mDragResizing) {
7010 mDragResizing = true;
Chong Zhangdcee1de2015-10-06 10:26:00 -07007011 synchronized (mWindowCallbacks) {
7012 for (int i = mWindowCallbacks.size() - 1; i >= 0; i--) {
7013 mWindowCallbacks.get(i).onWindowDragResizeStart(initialBounds);
Skuhneb8160872015-09-22 09:51:39 -07007014 }
7015 }
7016 mFullRedrawNeeded = true;
7017 }
7018 }
7019
7020 /**
7021 * End a drag resize which will inform all listeners that a window resize has ended.
7022 */
7023 private void endDragResizing() {
7024 if (mDragResizing) {
7025 mDragResizing = false;
Chong Zhangdcee1de2015-10-06 10:26:00 -07007026 synchronized (mWindowCallbacks) {
7027 for (int i = mWindowCallbacks.size() - 1; i >= 0; i--) {
7028 mWindowCallbacks.get(i).onWindowDragResizeEnd();
Skuhneb8160872015-09-22 09:51:39 -07007029 }
7030 }
7031 mFullRedrawNeeded = true;
7032 }
7033 }
7034
Chong Zhang8dbd9ad2015-10-09 10:06:11 -07007035 private boolean updateContentDrawBounds() {
7036 boolean updated = false;
7037 synchronized (mWindowCallbacks) {
7038 for (int i = mWindowCallbacks.size() - 1; i >= 0; i--) {
7039 updated |= mWindowCallbacks.get(i).onContentDrawn(
7040 mWindowAttributes.surfaceInsets.left,
7041 mWindowAttributes.surfaceInsets.top,
7042 mWidth, mHeight);
7043 }
7044 }
7045 return updated | (mDragResizing && mReportNextDraw);
7046 }
7047
7048 private void requestDrawWindow() {
7049 if (mReportNextDraw) {
7050 mWindowDrawCountDown = new CountDownLatch(mWindowCallbacks.size());
7051 }
7052 synchronized (mWindowCallbacks) {
7053 for (int i = mWindowCallbacks.size() - 1; i >= 0; i--) {
7054 mWindowCallbacks.get(i).onRequestDraw(mReportNextDraw);
7055 }
7056 }
7057 }
7058
Skuhneb8160872015-09-22 09:51:39 -07007059 /**
Jorim Jaggi4846ee32016-01-07 17:39:12 +01007060 * Tells this instance that its corresponding activity has just relaunched. In this case, we
7061 * need to force a relayout of the window to make sure we get the correct bounds from window
7062 * manager.
7063 */
7064 public void reportActivityRelaunched() {
7065 mActivityRelaunched = true;
7066 }
7067
7068 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007069 * Class for managing the accessibility interaction connection
7070 * based on the global accessibility state.
7071 */
7072 final class AccessibilityInteractionConnectionManager
7073 implements AccessibilityStateChangeListener {
Igor Murashkina86ab6402013-08-30 12:58:36 -07007074 @Override
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007075 public void onAccessibilityStateChanged(boolean enabled) {
7076 if (enabled) {
7077 ensureConnection();
Chris Craikcce47eb2014-07-16 15:12:15 -07007078 if (mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov0d04e242012-02-21 13:46:36 -08007079 mView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
7080 View focusedView = mView.findFocus();
7081 if (focusedView != null && focusedView != mView) {
7082 focusedView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
7083 }
7084 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007085 } else {
7086 ensureNoConnection();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07007087 mHandler.obtainMessage(MSG_CLEAR_ACCESSIBILITY_FOCUS_HOST).sendToTarget();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007088 }
7089 }
7090
7091 public void ensureConnection() {
Chris Craikcce47eb2014-07-16 15:12:15 -07007092 final boolean registered =
Svetoslav8e3feb12014-02-24 13:46:47 -08007093 mAttachInfo.mAccessibilityWindowId != AccessibilityNodeInfo.UNDEFINED_ITEM_ID;
Chris Craikcce47eb2014-07-16 15:12:15 -07007094 if (!registered) {
7095 mAttachInfo.mAccessibilityWindowId =
Svetoslav Ganov0d04e242012-02-21 13:46:36 -08007096 mAccessibilityManager.addAccessibilityInteractionConnection(mWindow,
7097 new AccessibilityInteractionConnection(ViewRootImpl.this));
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007098 }
7099 }
7100
7101 public void ensureNoConnection() {
Svetoslav Ganov0d04e242012-02-21 13:46:36 -08007102 final boolean registered =
Svetoslav8e3feb12014-02-24 13:46:47 -08007103 mAttachInfo.mAccessibilityWindowId != AccessibilityNodeInfo.UNDEFINED_ITEM_ID;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007104 if (registered) {
Svetoslav8e3feb12014-02-24 13:46:47 -08007105 mAttachInfo.mAccessibilityWindowId = AccessibilityNodeInfo.UNDEFINED_ITEM_ID;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007106 mAccessibilityManager.removeAccessibilityInteractionConnection(mWindow);
7107 }
7108 }
7109 }
7110
Chris Craikcce47eb2014-07-16 15:12:15 -07007111 final class HighContrastTextManager implements HighTextContrastChangeListener {
7112 HighContrastTextManager() {
7113 mAttachInfo.mHighContrastText = mAccessibilityManager.isHighTextContrastEnabled();
7114 }
7115 @Override
7116 public void onHighTextContrastStateChanged(boolean enabled) {
7117 mAttachInfo.mHighContrastText = enabled;
7118
7119 // Destroy Displaylists so they can be recreated with high contrast recordings
7120 destroyHardwareResources();
Chris Craikd36a81f2014-07-17 10:16:51 -07007121
7122 // Schedule redraw, which will rerecord + redraw all text
7123 invalidate();
Chris Craikcce47eb2014-07-16 15:12:15 -07007124 }
7125 }
7126
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007127 /**
7128 * This class is an interface this ViewAncestor provides to the
7129 * AccessibilityManagerService to the latter can interact with
7130 * the view hierarchy in this ViewAncestor.
7131 */
Svetoslav Ganovaf5b4f42011-10-28 12:41:38 -07007132 static final class AccessibilityInteractionConnection
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007133 extends IAccessibilityInteractionConnection.Stub {
Svetoslav Ganov02107852011-10-03 17:06:56 -07007134 private final WeakReference<ViewRootImpl> mViewRootImpl;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007135
Svetoslav Ganovf79f4762011-10-28 15:40:32 -07007136 AccessibilityInteractionConnection(ViewRootImpl viewRootImpl) {
7137 mViewRootImpl = new WeakReference<ViewRootImpl>(viewRootImpl);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007138 }
7139
Svetoslav Ganov42138042012-03-20 11:51:39 -07007140 @Override
Svetoslav Ganov02107852011-10-03 17:06:56 -07007141 public void findAccessibilityNodeInfoByAccessibilityId(long accessibilityNodeId,
Svetoslav9ae9ed22014-09-02 16:36:35 -07007142 Region interactiveRegion, int interactionId,
7143 IAccessibilityInteractionConnectionCallback callback, int flags,
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07007144 int interrogatingPid, long interrogatingTid, MagnificationSpec spec) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07007145 ViewRootImpl viewRootImpl = mViewRootImpl.get();
7146 if (viewRootImpl != null && viewRootImpl.mView != null) {
Svetoslav Ganovaf5b4f42011-10-28 12:41:38 -07007147 viewRootImpl.getAccessibilityInteractionController()
Svetoslav Ganov02107852011-10-03 17:06:56 -07007148 .findAccessibilityNodeInfoByAccessibilityIdClientThread(accessibilityNodeId,
Svetoslav9ae9ed22014-09-02 16:36:35 -07007149 interactiveRegion, interactionId, callback, flags, interrogatingPid,
7150 interrogatingTid, spec);
Svetoslav Ganov79311c42012-01-17 20:24:26 -08007151 } else {
7152 // We cannot make the call and notify the caller so it does not wait.
7153 try {
7154 callback.setFindAccessibilityNodeInfosResult(null, interactionId);
7155 } catch (RemoteException re) {
7156 /* best effort - ignore */
7157 }
Svetoslav Ganov601ad802011-06-09 14:47:38 -07007158 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007159 }
7160
Svetoslav Ganov42138042012-03-20 11:51:39 -07007161 @Override
Svetoslav Ganov02107852011-10-03 17:06:56 -07007162 public void performAccessibilityAction(long accessibilityNodeId, int action,
Svetoslav Ganovaa780c12012-04-19 23:01:39 -07007163 Bundle arguments, int interactionId,
7164 IAccessibilityInteractionConnectionCallback callback, int flags,
Svet Ganov7498efd2014-09-03 21:33:00 -07007165 int interrogatingPid, long interrogatingTid) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07007166 ViewRootImpl viewRootImpl = mViewRootImpl.get();
7167 if (viewRootImpl != null && viewRootImpl.mView != null) {
Svetoslav Ganovaf5b4f42011-10-28 12:41:38 -07007168 viewRootImpl.getAccessibilityInteractionController()
Svetoslav Ganovaa780c12012-04-19 23:01:39 -07007169 .performAccessibilityActionClientThread(accessibilityNodeId, action, arguments,
Svet Ganov7498efd2014-09-03 21:33:00 -07007170 interactionId, callback, flags, interrogatingPid, interrogatingTid);
Svetoslav Ganov79311c42012-01-17 20:24:26 -08007171 } else {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007172 // We cannot make the call and notify the caller so it does not wait.
Svetoslav Ganov79311c42012-01-17 20:24:26 -08007173 try {
7174 callback.setPerformAccessibilityActionResult(false, interactionId);
7175 } catch (RemoteException re) {
7176 /* best effort - ignore */
7177 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007178 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007179 }
7180
Svetoslav Ganov42138042012-03-20 11:51:39 -07007181 @Override
Svetoslav Ganov80943d82013-01-02 10:25:37 -08007182 public void findAccessibilityNodeInfosByViewId(long accessibilityNodeId,
Svetoslav9ae9ed22014-09-02 16:36:35 -07007183 String viewId, Region interactiveRegion, int interactionId,
Svetoslav Ganov80943d82013-01-02 10:25:37 -08007184 IAccessibilityInteractionConnectionCallback callback, int flags,
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07007185 int interrogatingPid, long interrogatingTid, MagnificationSpec spec) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07007186 ViewRootImpl viewRootImpl = mViewRootImpl.get();
7187 if (viewRootImpl != null && viewRootImpl.mView != null) {
Svetoslav Ganovaf5b4f42011-10-28 12:41:38 -07007188 viewRootImpl.getAccessibilityInteractionController()
Svetoslav Ganov80943d82013-01-02 10:25:37 -08007189 .findAccessibilityNodeInfosByViewIdClientThread(accessibilityNodeId,
Svetoslav9ae9ed22014-09-02 16:36:35 -07007190 viewId, interactiveRegion, interactionId, callback, flags,
7191 interrogatingPid, interrogatingTid, spec);
Svetoslav Ganov79311c42012-01-17 20:24:26 -08007192 } else {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007193 // We cannot make the call and notify the caller so it does not wait.
Svetoslav Ganov79311c42012-01-17 20:24:26 -08007194 try {
7195 callback.setFindAccessibilityNodeInfoResult(null, interactionId);
7196 } catch (RemoteException re) {
7197 /* best effort - ignore */
7198 }
7199 }
7200 }
7201
Svetoslav Ganov42138042012-03-20 11:51:39 -07007202 @Override
Svetoslav Ganov79311c42012-01-17 20:24:26 -08007203 public void findAccessibilityNodeInfosByText(long accessibilityNodeId, String text,
Svetoslav9ae9ed22014-09-02 16:36:35 -07007204 Region interactiveRegion, int interactionId,
7205 IAccessibilityInteractionConnectionCallback callback, int flags,
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07007206 int interrogatingPid, long interrogatingTid, MagnificationSpec spec) {
Svetoslav Ganov79311c42012-01-17 20:24:26 -08007207 ViewRootImpl viewRootImpl = mViewRootImpl.get();
7208 if (viewRootImpl != null && viewRootImpl.mView != null) {
7209 viewRootImpl.getAccessibilityInteractionController()
7210 .findAccessibilityNodeInfosByTextClientThread(accessibilityNodeId, text,
Svetoslav9ae9ed22014-09-02 16:36:35 -07007211 interactiveRegion, interactionId, callback, flags, interrogatingPid,
7212 interrogatingTid, spec);
Svetoslav Ganov79311c42012-01-17 20:24:26 -08007213 } else {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007214 // We cannot make the call and notify the caller so it does not wait.
Svetoslav Ganov79311c42012-01-17 20:24:26 -08007215 try {
7216 callback.setFindAccessibilityNodeInfosResult(null, interactionId);
7217 } catch (RemoteException re) {
7218 /* best effort - ignore */
7219 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007220 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007221 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007222
Svetoslav Ganov42138042012-03-20 11:51:39 -07007223 @Override
Svetoslav9ae9ed22014-09-02 16:36:35 -07007224 public void findFocus(long accessibilityNodeId, int focusType, Region interactiveRegion,
7225 int interactionId, IAccessibilityInteractionConnectionCallback callback, int flags,
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07007226 int interrogatingPid, long interrogatingTid, MagnificationSpec spec) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007227 ViewRootImpl viewRootImpl = mViewRootImpl.get();
7228 if (viewRootImpl != null && viewRootImpl.mView != null) {
7229 viewRootImpl.getAccessibilityInteractionController()
Svetoslav9ae9ed22014-09-02 16:36:35 -07007230 .findFocusClientThread(accessibilityNodeId, focusType, interactiveRegion,
7231 interactionId, callback, flags, interrogatingPid, interrogatingTid,
7232 spec);
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07007233 } else {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007234 // We cannot make the call and notify the caller so it does not wait.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007235 try {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007236 callback.setFindAccessibilityNodeInfoResult(null, interactionId);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007237 } catch (RemoteException re) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007238 /* best effort - ignore */
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007239 }
7240 }
7241 }
7242
Svetoslav Ganov42138042012-03-20 11:51:39 -07007243 @Override
Svetoslav9ae9ed22014-09-02 16:36:35 -07007244 public void focusSearch(long accessibilityNodeId, int direction, Region interactiveRegion,
7245 int interactionId, IAccessibilityInteractionConnectionCallback callback, int flags,
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07007246 int interrogatingPid, long interrogatingTid, MagnificationSpec spec) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007247 ViewRootImpl viewRootImpl = mViewRootImpl.get();
7248 if (viewRootImpl != null && viewRootImpl.mView != null) {
7249 viewRootImpl.getAccessibilityInteractionController()
Svetoslav9ae9ed22014-09-02 16:36:35 -07007250 .focusSearchClientThread(accessibilityNodeId, direction, interactiveRegion,
7251 interactionId, callback, flags, interrogatingPid, interrogatingTid,
7252 spec);
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07007253 } else {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007254 // We cannot make the call and notify the caller so it does not wait.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007255 try {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007256 callback.setFindAccessibilityNodeInfoResult(null, interactionId);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007257 } catch (RemoteException re) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007258 /* best effort - ignore */
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007259 }
7260 }
7261 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07007262 }
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07007263
Svetoslav Ganova0156172011-06-26 17:55:44 -07007264 private class SendWindowContentChangedAccessibilityEvent implements Runnable {
Alan Viverette77e9a282013-09-12 17:16:09 -07007265 private int mChangeTypes = 0;
7266
Svetoslav Ganov42138042012-03-20 11:51:39 -07007267 public View mSource;
Svetoslav6254f482013-06-04 17:22:14 -07007268 public long mLastEventTimeMillis;
Svetoslav Ganova0156172011-06-26 17:55:44 -07007269
Igor Murashkina86ab6402013-08-30 12:58:36 -07007270 @Override
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07007271 public void run() {
Svetoslav8d820ecf2013-07-15 17:12:41 -07007272 // The accessibility may be turned off while we were waiting so check again.
7273 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7274 mLastEventTimeMillis = SystemClock.uptimeMillis();
7275 AccessibilityEvent event = AccessibilityEvent.obtain();
7276 event.setEventType(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Alan Viverette77e9a282013-09-12 17:16:09 -07007277 event.setContentChangeTypes(mChangeTypes);
Svetoslav8d820ecf2013-07-15 17:12:41 -07007278 mSource.sendAccessibilityEventUnchecked(event);
7279 } else {
7280 mLastEventTimeMillis = 0;
7281 }
7282 // In any case reset to initial state.
Svetoslav6254f482013-06-04 17:22:14 -07007283 mSource.resetSubtreeAccessibilityStateChanged();
7284 mSource = null;
Alan Viverette77e9a282013-09-12 17:16:09 -07007285 mChangeTypes = 0;
Svetoslav6254f482013-06-04 17:22:14 -07007286 }
7287
Alan Viverette77e9a282013-09-12 17:16:09 -07007288 public void runOrPost(View source, int changeType) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007289 if (mSource != null) {
Svetoslav9dafebb2013-06-18 16:29:25 -07007290 // If there is no common predecessor, then mSource points to
7291 // a removed view, hence in this case always prefer the source.
7292 View predecessor = getCommonPredecessor(mSource, source);
7293 mSource = (predecessor != null) ? predecessor : source;
Alan Viverette77e9a282013-09-12 17:16:09 -07007294 mChangeTypes |= changeType;
Svetoslav6254f482013-06-04 17:22:14 -07007295 return;
7296 }
7297 mSource = source;
Alan Viverette77e9a282013-09-12 17:16:09 -07007298 mChangeTypes = changeType;
Svetoslav6254f482013-06-04 17:22:14 -07007299 final long timeSinceLastMillis = SystemClock.uptimeMillis() - mLastEventTimeMillis;
7300 final long minEventIntevalMillis =
7301 ViewConfiguration.getSendRecurringAccessibilityEventsInterval();
7302 if (timeSinceLastMillis >= minEventIntevalMillis) {
Svetoslav9dafebb2013-06-18 16:29:25 -07007303 mSource.removeCallbacks(this);
Svetoslav6254f482013-06-04 17:22:14 -07007304 run();
7305 } else {
7306 mSource.postDelayed(this, minEventIntevalMillis - timeSinceLastMillis);
Svetoslav Ganov79311c42012-01-17 20:24:26 -08007307 }
7308 }
7309 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007310}