blob: b61465a74bf36804be06d959dcd8cf1c970fc480 [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
19import com.android.internal.view.IInputMethodCallback;
20import com.android.internal.view.IInputMethodSession;
21
22import android.graphics.Canvas;
23import android.graphics.PixelFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.graphics.PorterDuff;
25import android.graphics.Rect;
26import android.graphics.Region;
27import android.os.*;
28import android.os.Process;
29import android.os.SystemProperties;
30import android.util.AndroidRuntimeException;
31import android.util.Config;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -070032import android.util.DisplayMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.util.Log;
34import android.util.EventLog;
35import android.util.SparseArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.view.View.MeasureSpec;
svetoslavganov75986cf2009-05-14 22:28:01 -070037import android.view.accessibility.AccessibilityEvent;
38import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.view.inputmethod.InputConnection;
40import android.view.inputmethod.InputMethodManager;
41import android.widget.Scroller;
42import android.content.pm.PackageManager;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -070043import android.content.res.CompatibilityInfo;
Mitsuru Oshima38ed7d772009-07-21 14:39:34 -070044import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.content.Context;
46import android.app.ActivityManagerNative;
47import android.Manifest;
48import android.media.AudioManager;
49
50import java.lang.ref.WeakReference;
51import java.io.IOException;
52import java.io.OutputStream;
53import java.util.ArrayList;
54
55import javax.microedition.khronos.egl.*;
56import javax.microedition.khronos.opengles.*;
57import static javax.microedition.khronos.opengles.GL10.*;
58
59/**
60 * The top of a view hierarchy, implementing the needed protocol between View
61 * and the WindowManager. This is for the most part an internal implementation
62 * detail of {@link WindowManagerImpl}.
63 *
64 * {@hide}
65 */
66@SuppressWarnings({"EmptyCatchBlock"})
67public final class ViewRoot extends Handler implements ViewParent,
68 View.AttachInfo.Callbacks {
69 private static final String TAG = "ViewRoot";
70 private static final boolean DBG = false;
71 @SuppressWarnings({"ConstantConditionalExpression"})
72 private static final boolean LOCAL_LOGV = false ? Config.LOGD : Config.LOGV;
73 /** @noinspection PointlessBooleanExpression*/
74 private static final boolean DEBUG_DRAW = false || LOCAL_LOGV;
75 private static final boolean DEBUG_LAYOUT = false || LOCAL_LOGV;
76 private static final boolean DEBUG_INPUT_RESIZE = false || LOCAL_LOGV;
77 private static final boolean DEBUG_ORIENTATION = false || LOCAL_LOGV;
78 private static final boolean DEBUG_TRACKBALL = false || LOCAL_LOGV;
79 private static final boolean DEBUG_IMF = false || LOCAL_LOGV;
80 private static final boolean WATCH_POINTER = false;
81
Michael Chan53071d62009-05-13 17:29:48 -070082 private static final boolean MEASURE_LATENCY = false;
83 private static LatencyTimer lt;
84
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 /**
86 * Maximum time we allow the user to roll the trackball enough to generate
87 * a key event, before resetting the counters.
88 */
89 static final int MAX_TRACKBALL_DELAY = 250;
90
91 static long sInstanceCount = 0;
92
93 static IWindowSession sWindowSession;
94
95 static final Object mStaticInit = new Object();
96 static boolean mInitialized = false;
97
98 static final ThreadLocal<RunQueue> sRunQueues = new ThreadLocal<RunQueue>();
99
Romain Guy8506ab42009-06-11 17:35:47 -0700100 private static int sDrawTime;
Romain Guy13922e02009-05-12 17:56:14 -0700101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 long mLastTrackballTime = 0;
103 final TrackballAxis mTrackballAxisX = new TrackballAxis();
104 final TrackballAxis mTrackballAxisY = new TrackballAxis();
105
106 final int[] mTmpLocation = new int[2];
Romain Guy8506ab42009-06-11 17:35:47 -0700107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 final InputMethodCallback mInputMethodCallback;
109 final SparseArray<Object> mPendingEvents = new SparseArray<Object>();
110 int mPendingEventSeq = 0;
Romain Guy8506ab42009-06-11 17:35:47 -0700111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 final Thread mThread;
113
114 final WindowLeaked mLocation;
115
116 final WindowManager.LayoutParams mWindowAttributes = new WindowManager.LayoutParams();
117
118 final W mWindow;
119
120 View mView;
121 View mFocusedView;
122 View mRealFocusedView; // this is not set to null in touch mode
123 int mViewVisibility;
124 boolean mAppVisible = true;
125
126 final Region mTransparentRegion;
127 final Region mPreviousTransparentRegion;
128
129 int mWidth;
130 int mHeight;
131 Rect mDirty; // will be a graphics.Region soon
Romain Guybb93d552009-03-24 21:04:15 -0700132 boolean mIsAnimating;
Romain Guy8506ab42009-06-11 17:35:47 -0700133
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700134 CompatibilityInfo.Translator mTranslator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135
136 final View.AttachInfo mAttachInfo;
137
138 final Rect mTempRect; // used in the transaction to not thrash the heap.
139 final Rect mVisRect; // used to retrieve visible rect of focused view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140
141 boolean mTraversalScheduled;
142 boolean mWillDrawSoon;
143 boolean mLayoutRequested;
144 boolean mFirst;
145 boolean mReportNextDraw;
146 boolean mFullRedrawNeeded;
147 boolean mNewSurfaceNeeded;
148 boolean mHasHadWindowFocus;
149 boolean mLastWasImTarget;
150
151 boolean mWindowAttributesChanged = false;
152
153 // These can be accessed by any thread, must be protected with a lock.
Mathias Agopian5583dc62009-07-09 16:28:11 -0700154 // Surface can never be reassigned or cleared (use Surface.clear()).
155 private final Surface mSurface = new Surface();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156
157 boolean mAdded;
158 boolean mAddedTouchMode;
159
160 /*package*/ int mAddNesting;
161
162 // These are accessed by multiple threads.
163 final Rect mWinFrame; // frame given by window manager.
164
165 final Rect mPendingVisibleInsets = new Rect();
166 final Rect mPendingContentInsets = new Rect();
167 final ViewTreeObserver.InternalInsetsInfo mLastGivenInsets
168 = new ViewTreeObserver.InternalInsetsInfo();
169
170 boolean mScrollMayChange;
171 int mSoftInputMode;
172 View mLastScrolledFocus;
173 int mScrollY;
174 int mCurScrollY;
175 Scroller mScroller;
Romain Guy8506ab42009-06-11 17:35:47 -0700176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 EGL10 mEgl;
178 EGLDisplay mEglDisplay;
179 EGLContext mEglContext;
180 EGLSurface mEglSurface;
181 GL11 mGL;
182 Canvas mGlCanvas;
183 boolean mUseGL;
184 boolean mGlWanted;
185
Romain Guy8506ab42009-06-11 17:35:47 -0700186 final ViewConfiguration mViewConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187
188 /**
189 * see {@link #playSoundEffect(int)}
190 */
191 AudioManager mAudioManager;
192
Dianne Hackborn11ea3342009-07-22 21:48:55 -0700193 private final int mDensity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700195 public static IWindowSession getWindowSession(Looper mainLooper) {
196 synchronized (mStaticInit) {
197 if (!mInitialized) {
198 try {
199 InputMethodManager imm = InputMethodManager.getInstance(mainLooper);
200 sWindowSession = IWindowManager.Stub.asInterface(
201 ServiceManager.getService("window"))
202 .openSession(imm.getClient(), imm.getInputContext());
203 mInitialized = true;
204 } catch (RemoteException e) {
205 }
206 }
207 return sWindowSession;
208 }
209 }
210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 public ViewRoot(Context context) {
212 super();
213
Michael Chan53071d62009-05-13 17:29:48 -0700214 if (MEASURE_LATENCY && lt == null) {
215 lt = new LatencyTimer(100, 1000);
216 }
217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 ++sInstanceCount;
219
220 // Initialize the statics when this class is first instantiated. This is
221 // done here instead of in the static block because Zygote does not
222 // allow the spawning of threads.
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700223 getWindowSession(context.getMainLooper());
224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 mThread = Thread.currentThread();
226 mLocation = new WindowLeaked(null);
227 mLocation.fillInStackTrace();
228 mWidth = -1;
229 mHeight = -1;
230 mDirty = new Rect();
231 mTempRect = new Rect();
232 mVisRect = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 mWinFrame = new Rect();
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700234 mWindow = new W(this, context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 mInputMethodCallback = new InputMethodCallback(this);
236 mViewVisibility = View.GONE;
237 mTransparentRegion = new Region();
238 mPreviousTransparentRegion = new Region();
239 mFirst = true; // true for the first time the view is added
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 mAdded = false;
241 mAttachInfo = new View.AttachInfo(sWindowSession, mWindow, this, this);
242 mViewConfiguration = ViewConfiguration.get(context);
Dianne Hackborn11ea3342009-07-22 21:48:55 -0700243 mDensity = context.getResources().getDisplayMetrics().densityDpi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 }
245
246 @Override
247 protected void finalize() throws Throwable {
248 super.finalize();
249 --sInstanceCount;
250 }
251
252 public static long getInstanceCount() {
253 return sInstanceCount;
254 }
255
256 // FIXME for perf testing only
257 private boolean mProfile = false;
258
259 /**
260 * Call this to profile the next traversal call.
261 * FIXME for perf testing only. Remove eventually
262 */
263 public void profile() {
264 mProfile = true;
265 }
266
267 /**
268 * Indicates whether we are in touch mode. Calling this method triggers an IPC
269 * call and should be avoided whenever possible.
270 *
271 * @return True, if the device is in touch mode, false otherwise.
272 *
273 * @hide
274 */
275 static boolean isInTouchMode() {
276 if (mInitialized) {
277 try {
278 return sWindowSession.getInTouchMode();
279 } catch (RemoteException e) {
280 }
281 }
282 return false;
283 }
284
285 private void initializeGL() {
286 initializeGLInner();
287 int err = mEgl.eglGetError();
288 if (err != EGL10.EGL_SUCCESS) {
289 // give-up on using GL
290 destroyGL();
291 mGlWanted = false;
292 }
293 }
294
295 private void initializeGLInner() {
296 final EGL10 egl = (EGL10) EGLContext.getEGL();
297 mEgl = egl;
298
299 /*
300 * Get to the default display.
301 */
302 final EGLDisplay eglDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
303 mEglDisplay = eglDisplay;
304
305 /*
306 * We can now initialize EGL for that display
307 */
308 int[] version = new int[2];
309 egl.eglInitialize(eglDisplay, version);
310
311 /*
312 * Specify a configuration for our opengl session
313 * and grab the first configuration that matches is
314 */
315 final int[] configSpec = {
316 EGL10.EGL_RED_SIZE, 5,
317 EGL10.EGL_GREEN_SIZE, 6,
318 EGL10.EGL_BLUE_SIZE, 5,
319 EGL10.EGL_DEPTH_SIZE, 0,
320 EGL10.EGL_NONE
321 };
322 final EGLConfig[] configs = new EGLConfig[1];
323 final int[] num_config = new int[1];
324 egl.eglChooseConfig(eglDisplay, configSpec, configs, 1, num_config);
325 final EGLConfig config = configs[0];
326
327 /*
328 * Create an OpenGL ES context. This must be done only once, an
329 * OpenGL context is a somewhat heavy object.
330 */
331 final EGLContext context = egl.eglCreateContext(eglDisplay, config,
332 EGL10.EGL_NO_CONTEXT, null);
333 mEglContext = context;
334
335 /*
336 * Create an EGL surface we can render into.
337 */
338 final EGLSurface surface = egl.eglCreateWindowSurface(eglDisplay, config, mHolder, null);
339 mEglSurface = surface;
340
341 /*
342 * Before we can issue GL commands, we need to make sure
343 * the context is current and bound to a surface.
344 */
345 egl.eglMakeCurrent(eglDisplay, surface, surface, context);
346
347 /*
348 * Get to the appropriate GL interface.
349 * This is simply done by casting the GL context to either
350 * GL10 or GL11.
351 */
352 final GL11 gl = (GL11) context.getGL();
353 mGL = gl;
354 mGlCanvas = new Canvas(gl);
355 mUseGL = true;
356 }
357
358 private void destroyGL() {
359 // inform skia that the context is gone
360 nativeAbandonGlCaches();
361
362 mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE,
363 EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
364 mEgl.eglDestroyContext(mEglDisplay, mEglContext);
365 mEgl.eglDestroySurface(mEglDisplay, mEglSurface);
366 mEgl.eglTerminate(mEglDisplay);
367 mEglContext = null;
368 mEglSurface = null;
369 mEglDisplay = null;
370 mEgl = null;
371 mGlCanvas = null;
372 mGL = null;
373 mUseGL = false;
374 }
375
376 private void checkEglErrors() {
377 if (mUseGL) {
378 int err = mEgl.eglGetError();
379 if (err != EGL10.EGL_SUCCESS) {
380 // something bad has happened revert to
381 // normal rendering.
382 destroyGL();
383 if (err != EGL11.EGL_CONTEXT_LOST) {
384 // we'll try again if it was context lost
385 mGlWanted = false;
386 }
387 }
388 }
389 }
390
391 /**
392 * We have one child
393 */
394 public void setView(View view, WindowManager.LayoutParams attrs,
395 View panelParentView) {
396 synchronized (this) {
397 if (mView == null) {
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700398 mView = view;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700399 mWindowAttributes.copyFrom(attrs);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700400 attrs = mWindowAttributes;
Mitsuru Oshima38ed7d772009-07-21 14:39:34 -0700401 Resources resources = mView.getContext().getResources();
402 CompatibilityInfo compatibilityInfo = resources.getCompatibilityInfo();
Mitsuru Oshima589cebe2009-07-22 20:38:58 -0700403 mTranslator = compatibilityInfo.getTranslator();
Mitsuru Oshima38ed7d772009-07-21 14:39:34 -0700404
405 if (mTranslator != null || !compatibilityInfo.supportsScreen()) {
Mitsuru Oshima240f8a72009-07-22 20:39:14 -0700406 mSurface.setCompatibleDisplayMetrics(resources.getDisplayMetrics(),
407 mTranslator);
Mitsuru Oshima38ed7d772009-07-21 14:39:34 -0700408 }
409
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -0700410 boolean restore = false;
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700411 if (attrs != null && mTranslator != null) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -0700412 restore = true;
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700413 attrs.backup();
414 mTranslator.translateWindowLayout(attrs);
Mitsuru Oshima3d914922009-05-13 22:29:15 -0700415 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700416 if (DEBUG_LAYOUT) Log.d(TAG, "WindowLayout in setView:" + attrs);
417
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700418 if (!compatibilityInfo.supportsScreen()) {
419 attrs.flags |= WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW;
420 }
421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 mSoftInputMode = attrs.softInputMode;
423 mWindowAttributesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 mAttachInfo.mRootView = view;
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700425 mAttachInfo.mScalingRequired = mTranslator == null ? false : true;
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700426 mAttachInfo.mApplicationScale =
427 mTranslator == null ? 1.0f : mTranslator.applicationScale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 if (panelParentView != null) {
429 mAttachInfo.mPanelParentWindowToken
430 = panelParentView.getApplicationWindowToken();
431 }
432 mAdded = true;
433 int res; /* = WindowManagerImpl.ADD_OKAY; */
Romain Guy8506ab42009-06-11 17:35:47 -0700434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 // Schedule the first layout -before- adding to the window
436 // manager, to make sure we do the relayout before receiving
437 // any other events from the system.
438 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 try {
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700440 res = sWindowSession.add(mWindow, mWindowAttributes,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 getHostVisibility(), mAttachInfo.mContentInsets);
442 } catch (RemoteException e) {
443 mAdded = false;
444 mView = null;
445 mAttachInfo.mRootView = null;
446 unscheduleTraversals();
447 throw new RuntimeException("Adding window failed", e);
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700448 } finally {
449 if (restore) {
450 attrs.restore();
451 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 }
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -0700453
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700454 if (mTranslator != null) {
455 mTranslator.translateRectInScreenToAppWindow(mAttachInfo.mContentInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 mPendingContentInsets.set(mAttachInfo.mContentInsets);
458 mPendingVisibleInsets.set(0, 0, 0, 0);
459 if (Config.LOGV) Log.v("ViewRoot", "Added window " + mWindow);
460 if (res < WindowManagerImpl.ADD_OKAY) {
461 mView = null;
462 mAttachInfo.mRootView = null;
463 mAdded = false;
464 unscheduleTraversals();
465 switch (res) {
466 case WindowManagerImpl.ADD_BAD_APP_TOKEN:
467 case WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN:
468 throw new WindowManagerImpl.BadTokenException(
469 "Unable to add window -- token " + attrs.token
470 + " is not valid; is your activity running?");
471 case WindowManagerImpl.ADD_NOT_APP_TOKEN:
472 throw new WindowManagerImpl.BadTokenException(
473 "Unable to add window -- token " + attrs.token
474 + " is not for an application");
475 case WindowManagerImpl.ADD_APP_EXITING:
476 throw new WindowManagerImpl.BadTokenException(
477 "Unable to add window -- app for token " + attrs.token
478 + " is exiting");
479 case WindowManagerImpl.ADD_DUPLICATE_ADD:
480 throw new WindowManagerImpl.BadTokenException(
481 "Unable to add window -- window " + mWindow
482 + " has already been added");
483 case WindowManagerImpl.ADD_STARTING_NOT_NEEDED:
484 // Silently ignore -- we would have just removed it
485 // right away, anyway.
486 return;
487 case WindowManagerImpl.ADD_MULTIPLE_SINGLETON:
488 throw new WindowManagerImpl.BadTokenException(
489 "Unable to add window " + mWindow +
490 " -- another window of this type already exists");
491 case WindowManagerImpl.ADD_PERMISSION_DENIED:
492 throw new WindowManagerImpl.BadTokenException(
493 "Unable to add window " + mWindow +
494 " -- permission denied for this window type");
495 }
496 throw new RuntimeException(
497 "Unable to add window -- unknown error code " + res);
498 }
499 view.assignParent(this);
500 mAddedTouchMode = (res&WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE) != 0;
501 mAppVisible = (res&WindowManagerImpl.ADD_FLAG_APP_VISIBLE) != 0;
502 }
503 }
504 }
505
506 public View getView() {
507 return mView;
508 }
509
510 final WindowLeaked getLocation() {
511 return mLocation;
512 }
513
514 void setLayoutParams(WindowManager.LayoutParams attrs, boolean newView) {
515 synchronized (this) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700516 int oldSoftInputMode = mWindowAttributes.softInputMode;
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -0700517 // preserve compatible window flag if exists.
518 int compatibleWindowFlag =
519 mWindowAttributes.flags & WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 mWindowAttributes.copyFrom(attrs);
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -0700521 mWindowAttributes.flags |= compatibleWindowFlag;
522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 if (newView) {
524 mSoftInputMode = attrs.softInputMode;
525 requestLayout();
526 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700527 // Don't lose the mode we last auto-computed.
528 if ((attrs.softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
529 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED) {
530 mWindowAttributes.softInputMode = (mWindowAttributes.softInputMode
531 & ~WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
532 | (oldSoftInputMode
533 & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST);
534 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 mWindowAttributesChanged = true;
536 scheduleTraversals();
537 }
538 }
539
540 void handleAppVisibility(boolean visible) {
541 if (mAppVisible != visible) {
542 mAppVisible = visible;
543 scheduleTraversals();
544 }
545 }
546
547 void handleGetNewSurface() {
548 mNewSurfaceNeeded = true;
549 mFullRedrawNeeded = true;
550 scheduleTraversals();
551 }
552
553 /**
554 * {@inheritDoc}
555 */
556 public void requestLayout() {
557 checkThread();
558 mLayoutRequested = true;
559 scheduleTraversals();
560 }
561
562 /**
563 * {@inheritDoc}
564 */
565 public boolean isLayoutRequested() {
566 return mLayoutRequested;
567 }
568
569 public void invalidateChild(View child, Rect dirty) {
570 checkThread();
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700571 if (DEBUG_DRAW) Log.v(TAG, "Invalidate child: " + dirty);
572 if (mCurScrollY != 0 || mTranslator != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 mTempRect.set(dirty);
Romain Guy1e095972009-07-07 11:22:45 -0700574 dirty = mTempRect;
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700575 if (mCurScrollY != 0) {
Romain Guy1e095972009-07-07 11:22:45 -0700576 dirty.offset(0, -mCurScrollY);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700577 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700578 if (mTranslator != null) {
Romain Guy1e095972009-07-07 11:22:45 -0700579 mTranslator.translateRectInAppWindowToScreen(dirty);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700580 }
Romain Guy1e095972009-07-07 11:22:45 -0700581 if (mAttachInfo.mScalingRequired) {
582 dirty.inset(-1, -1);
583 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 }
585 mDirty.union(dirty);
586 if (!mWillDrawSoon) {
587 scheduleTraversals();
588 }
589 }
590
591 public ViewParent getParent() {
592 return null;
593 }
594
595 public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
596 invalidateChild(null, dirty);
597 return null;
598 }
599
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700600 public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 if (child != mView) {
602 throw new RuntimeException("child is not mine, honest!");
603 }
604 // Note: don't apply scroll offset, because we want to know its
605 // visibility in the virtual canvas being given to the view hierarchy.
606 return r.intersect(0, 0, mWidth, mHeight);
607 }
608
609 public void bringChildToFront(View child) {
610 }
611
612 public void scheduleTraversals() {
613 if (!mTraversalScheduled) {
614 mTraversalScheduled = true;
615 sendEmptyMessage(DO_TRAVERSAL);
616 }
617 }
618
619 public void unscheduleTraversals() {
620 if (mTraversalScheduled) {
621 mTraversalScheduled = false;
622 removeMessages(DO_TRAVERSAL);
623 }
624 }
625
626 int getHostVisibility() {
627 return mAppVisible ? mView.getVisibility() : View.GONE;
628 }
Romain Guy8506ab42009-06-11 17:35:47 -0700629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 private void performTraversals() {
631 // cache mView since it is used so much below...
632 final View host = mView;
633
634 if (DBG) {
635 System.out.println("======================================");
636 System.out.println("performTraversals");
637 host.debug();
638 }
639
640 if (host == null || !mAdded)
641 return;
642
643 mTraversalScheduled = false;
644 mWillDrawSoon = true;
645 boolean windowResizesToFitContent = false;
646 boolean fullRedrawNeeded = mFullRedrawNeeded;
647 boolean newSurface = false;
648 WindowManager.LayoutParams lp = mWindowAttributes;
649
650 int desiredWindowWidth;
651 int desiredWindowHeight;
652 int childWidthMeasureSpec;
653 int childHeightMeasureSpec;
654
655 final View.AttachInfo attachInfo = mAttachInfo;
656
657 final int viewVisibility = getHostVisibility();
658 boolean viewVisibilityChanged = mViewVisibility != viewVisibility
659 || mNewSurfaceNeeded;
660
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700661 float appScale = mAttachInfo.mApplicationScale;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 WindowManager.LayoutParams params = null;
664 if (mWindowAttributesChanged) {
665 mWindowAttributesChanged = false;
666 params = lp;
667 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700668 Rect frame = mWinFrame;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 if (mFirst) {
670 fullRedrawNeeded = true;
671 mLayoutRequested = true;
672
Romain Guy8506ab42009-06-11 17:35:47 -0700673 DisplayMetrics packageMetrics =
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700674 mView.getContext().getResources().getDisplayMetrics();
675 desiredWindowWidth = packageMetrics.widthPixels;
676 desiredWindowHeight = packageMetrics.heightPixels;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677
678 // For the very first time, tell the view hierarchy that it
679 // is attached to the window. Note that at this point the surface
680 // object is not initialized to its backing store, but soon it
681 // will be (assuming the window is visible).
682 attachInfo.mSurface = mSurface;
683 attachInfo.mHasWindowFocus = false;
684 attachInfo.mWindowVisibility = viewVisibility;
685 attachInfo.mRecomputeGlobalAttributes = false;
686 attachInfo.mKeepScreenOn = false;
687 viewVisibilityChanged = false;
688 host.dispatchAttachedToWindow(attachInfo, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 //Log.i(TAG, "Screen on initialized: " + attachInfo.mKeepScreenOn);
svetoslavganov75986cf2009-05-14 22:28:01 -0700690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 } else {
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700692 desiredWindowWidth = frame.width();
693 desiredWindowHeight = frame.height();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 if (desiredWindowWidth != mWidth || desiredWindowHeight != mHeight) {
695 if (DEBUG_ORIENTATION) Log.v("ViewRoot",
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700696 "View " + host + " resized to: " + frame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 fullRedrawNeeded = true;
698 mLayoutRequested = true;
699 windowResizesToFitContent = true;
700 }
701 }
702
703 if (viewVisibilityChanged) {
704 attachInfo.mWindowVisibility = viewVisibility;
705 host.dispatchWindowVisibilityChanged(viewVisibility);
706 if (viewVisibility != View.VISIBLE || mNewSurfaceNeeded) {
707 if (mUseGL) {
708 destroyGL();
709 }
710 }
711 if (viewVisibility == View.GONE) {
712 // After making a window gone, we will count it as being
713 // shown for the first time the next time it gets focus.
714 mHasHadWindowFocus = false;
715 }
716 }
717
718 boolean insetsChanged = false;
Romain Guy8506ab42009-06-11 17:35:47 -0700719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 if (mLayoutRequested) {
Romain Guy15df6702009-08-17 20:17:30 -0700721 // Execute enqueued actions on every layout in case a view that was detached
722 // enqueued an action after being detached
723 getRunQueue().executeActions(attachInfo.mHandler);
724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 if (mFirst) {
726 host.fitSystemWindows(mAttachInfo.mContentInsets);
727 // make sure touch mode code executes by setting cached value
728 // to opposite of the added touch mode.
729 mAttachInfo.mInTouchMode = !mAddedTouchMode;
730 ensureTouchModeLocally(mAddedTouchMode);
731 } else {
732 if (!mAttachInfo.mContentInsets.equals(mPendingContentInsets)) {
733 mAttachInfo.mContentInsets.set(mPendingContentInsets);
734 host.fitSystemWindows(mAttachInfo.mContentInsets);
735 insetsChanged = true;
736 if (DEBUG_LAYOUT) Log.v(TAG, "Content insets changing to: "
737 + mAttachInfo.mContentInsets);
738 }
739 if (!mAttachInfo.mVisibleInsets.equals(mPendingVisibleInsets)) {
740 mAttachInfo.mVisibleInsets.set(mPendingVisibleInsets);
741 if (DEBUG_LAYOUT) Log.v(TAG, "Visible insets changing to: "
742 + mAttachInfo.mVisibleInsets);
743 }
744 if (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT
745 || lp.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
746 windowResizesToFitContent = true;
747
Romain Guy8506ab42009-06-11 17:35:47 -0700748 DisplayMetrics packageMetrics =
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700749 mView.getContext().getResources().getDisplayMetrics();
750 desiredWindowWidth = packageMetrics.widthPixels;
751 desiredWindowHeight = packageMetrics.heightPixels;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 }
753 }
754
755 childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width);
756 childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
757
758 // Ask host how big it wants to be
759 if (DEBUG_ORIENTATION || DEBUG_LAYOUT) Log.v("ViewRoot",
760 "Measuring " + host + " in display " + desiredWindowWidth
761 + "x" + desiredWindowHeight + "...");
762 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
763
764 if (DBG) {
765 System.out.println("======================================");
766 System.out.println("performTraversals -- after measure");
767 host.debug();
768 }
769 }
770
771 if (attachInfo.mRecomputeGlobalAttributes) {
772 //Log.i(TAG, "Computing screen on!");
773 attachInfo.mRecomputeGlobalAttributes = false;
774 boolean oldVal = attachInfo.mKeepScreenOn;
775 attachInfo.mKeepScreenOn = false;
776 host.dispatchCollectViewAttributes(0);
777 if (attachInfo.mKeepScreenOn != oldVal) {
778 params = lp;
779 //Log.i(TAG, "Keep screen on changed: " + attachInfo.mKeepScreenOn);
780 }
781 }
782
783 if (mFirst || attachInfo.mViewVisibilityChanged) {
784 attachInfo.mViewVisibilityChanged = false;
785 int resizeMode = mSoftInputMode &
786 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST;
787 // If we are in auto resize mode, then we need to determine
788 // what mode to use now.
789 if (resizeMode == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED) {
790 final int N = attachInfo.mScrollContainers.size();
791 for (int i=0; i<N; i++) {
792 if (attachInfo.mScrollContainers.get(i).isShown()) {
793 resizeMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
794 }
795 }
796 if (resizeMode == 0) {
797 resizeMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;
798 }
799 if ((lp.softInputMode &
800 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) != resizeMode) {
801 lp.softInputMode = (lp.softInputMode &
802 ~WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) |
803 resizeMode;
804 params = lp;
805 }
806 }
807 }
Romain Guy8506ab42009-06-11 17:35:47 -0700808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 if (params != null && (host.mPrivateFlags & View.REQUEST_TRANSPARENT_REGIONS) != 0) {
810 if (!PixelFormat.formatHasAlpha(params.format)) {
811 params.format = PixelFormat.TRANSLUCENT;
812 }
813 }
814
815 boolean windowShouldResize = mLayoutRequested && windowResizesToFitContent
816 && (mWidth != host.mMeasuredWidth || mHeight != host.mMeasuredHeight);
817
818 final boolean computesInternalInsets =
819 attachInfo.mTreeObserver.hasComputeInternalInsetsListeners();
820 boolean insetsPending = false;
821 int relayoutResult = 0;
822 if (mFirst || windowShouldResize || insetsChanged
823 || viewVisibilityChanged || params != null) {
824
825 if (viewVisibility == View.VISIBLE) {
826 // If this window is giving internal insets to the window
827 // manager, and it is being added or changing its visibility,
828 // then we want to first give the window manager "fake"
829 // insets to cause it to effectively ignore the content of
830 // the window during layout. This avoids it briefly causing
831 // other windows to resize/move based on the raw frame of the
832 // window, waiting until we can finish laying out this window
833 // and get back to the window manager with the ultimately
834 // computed insets.
835 insetsPending = computesInternalInsets
836 && (mFirst || viewVisibilityChanged);
Romain Guy8506ab42009-06-11 17:35:47 -0700837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 if (mWindowAttributes.memoryType == WindowManager.LayoutParams.MEMORY_TYPE_GPU) {
839 if (params == null) {
840 params = mWindowAttributes;
841 }
842 mGlWanted = true;
843 }
844 }
845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 boolean initialized = false;
847 boolean contentInsetsChanged = false;
Romain Guy13922e02009-05-12 17:56:14 -0700848 boolean visibleInsetsChanged;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 try {
850 boolean hadSurface = mSurface.isValid();
851 int fl = 0;
852 if (params != null) {
853 fl = params.flags;
854 if (attachInfo.mKeepScreenOn) {
855 params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
856 }
857 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700858 if (DEBUG_LAYOUT) {
859 Log.i(TAG, "host=w:" + host.mMeasuredWidth + ", h:" +
860 host.mMeasuredHeight + ", params=" + params);
861 }
862 relayoutResult = relayoutWindow(params, viewVisibility, insetsPending);
863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 if (params != null) {
865 params.flags = fl;
866 }
867
868 if (DEBUG_LAYOUT) Log.v(TAG, "relayout: frame=" + frame.toShortString()
869 + " content=" + mPendingContentInsets.toShortString()
870 + " visible=" + mPendingVisibleInsets.toShortString()
871 + " surface=" + mSurface);
Romain Guy8506ab42009-06-11 17:35:47 -0700872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 contentInsetsChanged = !mPendingContentInsets.equals(
874 mAttachInfo.mContentInsets);
875 visibleInsetsChanged = !mPendingVisibleInsets.equals(
876 mAttachInfo.mVisibleInsets);
877 if (contentInsetsChanged) {
878 mAttachInfo.mContentInsets.set(mPendingContentInsets);
879 host.fitSystemWindows(mAttachInfo.mContentInsets);
880 if (DEBUG_LAYOUT) Log.v(TAG, "Content insets changing to: "
881 + mAttachInfo.mContentInsets);
882 }
883 if (visibleInsetsChanged) {
884 mAttachInfo.mVisibleInsets.set(mPendingVisibleInsets);
885 if (DEBUG_LAYOUT) Log.v(TAG, "Visible insets changing to: "
886 + mAttachInfo.mVisibleInsets);
887 }
888
889 if (!hadSurface) {
890 if (mSurface.isValid()) {
891 // If we are creating a new surface, then we need to
892 // completely redraw it. Also, when we get to the
893 // point of drawing it we will hold off and schedule
894 // a new traversal instead. This is so we can tell the
895 // window manager about all of the windows being displayed
896 // before actually drawing them, so it can display then
897 // all at once.
898 newSurface = true;
899 fullRedrawNeeded = true;
Romain Guy8506ab42009-06-11 17:35:47 -0700900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 if (mGlWanted && !mUseGL) {
902 initializeGL();
903 initialized = mGlCanvas != null;
904 }
905 }
906 } else if (!mSurface.isValid()) {
907 // If the surface has been removed, then reset the scroll
908 // positions.
909 mLastScrolledFocus = null;
910 mScrollY = mCurScrollY = 0;
911 if (mScroller != null) {
912 mScroller.abortAnimation();
913 }
914 }
915 } catch (RemoteException e) {
916 }
917 if (DEBUG_ORIENTATION) Log.v(
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700918 "ViewRoot", "Relayout returned: frame=" + frame + ", surface=" + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919
920 attachInfo.mWindowLeft = frame.left;
921 attachInfo.mWindowTop = frame.top;
922
923 // !!FIXME!! This next section handles the case where we did not get the
924 // window size we asked for. We should avoid this by getting a maximum size from
925 // the window session beforehand.
926 mWidth = frame.width();
927 mHeight = frame.height();
928
929 if (initialized) {
Mitsuru Oshima61324e52009-07-21 15:40:36 -0700930 mGlCanvas.setViewport((int) (mWidth * appScale + 0.5f),
931 (int) (mHeight * appScale + 0.5f));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 }
933
934 boolean focusChangedDueToTouchMode = ensureTouchModeLocally(
935 (relayoutResult&WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE) != 0);
936 if (focusChangedDueToTouchMode || mWidth != host.mMeasuredWidth
937 || mHeight != host.mMeasuredHeight || contentInsetsChanged) {
938 childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
939 childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
940
941 if (DEBUG_LAYOUT) Log.v(TAG, "Ooops, something changed! mWidth="
942 + mWidth + " measuredWidth=" + host.mMeasuredWidth
943 + " mHeight=" + mHeight
944 + " measuredHeight" + host.mMeasuredHeight
945 + " coveredInsetsChanged=" + contentInsetsChanged);
Romain Guy8506ab42009-06-11 17:35:47 -0700946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 // Ask host how big it wants to be
948 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
949
950 // Implementation of weights from WindowManager.LayoutParams
951 // We just grow the dimensions as needed and re-measure if
952 // needs be
953 int width = host.mMeasuredWidth;
954 int height = host.mMeasuredHeight;
955 boolean measureAgain = false;
956
957 if (lp.horizontalWeight > 0.0f) {
958 width += (int) ((mWidth - width) * lp.horizontalWeight);
959 childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width,
960 MeasureSpec.EXACTLY);
961 measureAgain = true;
962 }
963 if (lp.verticalWeight > 0.0f) {
964 height += (int) ((mHeight - height) * lp.verticalWeight);
965 childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
966 MeasureSpec.EXACTLY);
967 measureAgain = true;
968 }
969
970 if (measureAgain) {
971 if (DEBUG_LAYOUT) Log.v(TAG,
972 "And hey let's measure once more: width=" + width
973 + " height=" + height);
974 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
975 }
976
977 mLayoutRequested = true;
978 }
979 }
980
981 final boolean didLayout = mLayoutRequested;
982 boolean triggerGlobalLayoutListener = didLayout
983 || attachInfo.mRecomputeGlobalAttributes;
984 if (didLayout) {
985 mLayoutRequested = false;
986 mScrollMayChange = true;
987 if (DEBUG_ORIENTATION || DEBUG_LAYOUT) Log.v(
988 "ViewRoot", "Laying out " + host + " to (" +
989 host.mMeasuredWidth + ", " + host.mMeasuredHeight + ")");
Romain Guy13922e02009-05-12 17:56:14 -0700990 long startTime = 0L;
991 if (Config.DEBUG && ViewDebug.profileLayout) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 startTime = SystemClock.elapsedRealtime();
993 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 host.layout(0, 0, host.mMeasuredWidth, host.mMeasuredHeight);
995
Romain Guy13922e02009-05-12 17:56:14 -0700996 if (Config.DEBUG && ViewDebug.consistencyCheckEnabled) {
997 if (!host.dispatchConsistencyCheck(ViewDebug.CONSISTENCY_LAYOUT)) {
998 throw new IllegalStateException("The view hierarchy is an inconsistent state,"
999 + "please refer to the logs with the tag "
1000 + ViewDebug.CONSISTENCY_LOG_TAG + " for more infomation.");
1001 }
1002 }
1003
1004 if (Config.DEBUG && ViewDebug.profileLayout) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 EventLog.writeEvent(60001, SystemClock.elapsedRealtime() - startTime);
1006 }
1007
1008 // By this point all views have been sized and positionned
1009 // We can compute the transparent area
1010
1011 if ((host.mPrivateFlags & View.REQUEST_TRANSPARENT_REGIONS) != 0) {
1012 // start out transparent
1013 // TODO: AVOID THAT CALL BY CACHING THE RESULT?
1014 host.getLocationInWindow(mTmpLocation);
1015 mTransparentRegion.set(mTmpLocation[0], mTmpLocation[1],
1016 mTmpLocation[0] + host.mRight - host.mLeft,
1017 mTmpLocation[1] + host.mBottom - host.mTop);
1018
1019 host.gatherTransparentRegion(mTransparentRegion);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001020 if (mTranslator != null) {
1021 mTranslator.translateRegionInWindowToScreen(mTransparentRegion);
1022 }
1023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 if (!mTransparentRegion.equals(mPreviousTransparentRegion)) {
1025 mPreviousTransparentRegion.set(mTransparentRegion);
1026 // reconfigure window manager
1027 try {
1028 sWindowSession.setTransparentRegion(mWindow, mTransparentRegion);
1029 } catch (RemoteException e) {
1030 }
1031 }
1032 }
1033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 if (DBG) {
1035 System.out.println("======================================");
1036 System.out.println("performTraversals -- after setFrame");
1037 host.debug();
1038 }
1039 }
1040
1041 if (triggerGlobalLayoutListener) {
1042 attachInfo.mRecomputeGlobalAttributes = false;
1043 attachInfo.mTreeObserver.dispatchOnGlobalLayout();
1044 }
1045
1046 if (computesInternalInsets) {
1047 ViewTreeObserver.InternalInsetsInfo insets = attachInfo.mGivenInternalInsets;
1048 final Rect givenContent = attachInfo.mGivenInternalInsets.contentInsets;
1049 final Rect givenVisible = attachInfo.mGivenInternalInsets.visibleInsets;
1050 givenContent.left = givenContent.top = givenContent.right
1051 = givenContent.bottom = givenVisible.left = givenVisible.top
1052 = givenVisible.right = givenVisible.bottom = 0;
1053 attachInfo.mTreeObserver.dispatchOnComputeInternalInsets(insets);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001054 Rect contentInsets = insets.contentInsets;
1055 Rect visibleInsets = insets.visibleInsets;
1056 if (mTranslator != null) {
1057 contentInsets = mTranslator.getTranslatedContentInsets(contentInsets);
1058 visibleInsets = mTranslator.getTranslatedVisbileInsets(visibleInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 if (insetsPending || !mLastGivenInsets.equals(insets)) {
1061 mLastGivenInsets.set(insets);
1062 try {
1063 sWindowSession.setInsets(mWindow, insets.mTouchableInsets,
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001064 contentInsets, visibleInsets);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 } catch (RemoteException e) {
1066 }
1067 }
1068 }
Romain Guy8506ab42009-06-11 17:35:47 -07001069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 if (mFirst) {
1071 // handle first focus request
1072 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: mView.hasFocus()="
1073 + mView.hasFocus());
1074 if (mView != null) {
1075 if (!mView.hasFocus()) {
1076 mView.requestFocus(View.FOCUS_FORWARD);
1077 mFocusedView = mRealFocusedView = mView.findFocus();
1078 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: requested focused view="
1079 + mFocusedView);
1080 } else {
1081 mRealFocusedView = mView.findFocus();
1082 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: existing focused view="
1083 + mRealFocusedView);
1084 }
1085 }
1086 }
1087
1088 mFirst = false;
1089 mWillDrawSoon = false;
1090 mNewSurfaceNeeded = false;
1091 mViewVisibility = viewVisibility;
1092
1093 if (mAttachInfo.mHasWindowFocus) {
1094 final boolean imTarget = WindowManager.LayoutParams
1095 .mayUseInputMethod(mWindowAttributes.flags);
1096 if (imTarget != mLastWasImTarget) {
1097 mLastWasImTarget = imTarget;
1098 InputMethodManager imm = InputMethodManager.peekInstance();
1099 if (imm != null && imTarget) {
1100 imm.startGettingWindowFocus(mView);
1101 imm.onWindowFocus(mView, mView.findFocus(),
1102 mWindowAttributes.softInputMode,
1103 !mHasHadWindowFocus, mWindowAttributes.flags);
1104 }
1105 }
1106 }
Romain Guy8506ab42009-06-11 17:35:47 -07001107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 boolean cancelDraw = attachInfo.mTreeObserver.dispatchOnPreDraw();
1109
1110 if (!cancelDraw && !newSurface) {
1111 mFullRedrawNeeded = false;
1112 draw(fullRedrawNeeded);
1113
1114 if ((relayoutResult&WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0
1115 || mReportNextDraw) {
1116 if (LOCAL_LOGV) {
1117 Log.v("ViewRoot", "FINISHED DRAWING: " + mWindowAttributes.getTitle());
1118 }
1119 mReportNextDraw = false;
1120 try {
1121 sWindowSession.finishDrawing(mWindow);
1122 } catch (RemoteException e) {
1123 }
1124 }
1125 } else {
1126 // We were supposed to report when we are done drawing. Since we canceled the
1127 // draw, remember it here.
1128 if ((relayoutResult&WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0) {
1129 mReportNextDraw = true;
1130 }
1131 if (fullRedrawNeeded) {
1132 mFullRedrawNeeded = true;
1133 }
1134 // Try again
1135 scheduleTraversals();
1136 }
1137 }
1138
1139 public void requestTransparentRegion(View child) {
1140 // the test below should not fail unless someone is messing with us
1141 checkThread();
1142 if (mView == child) {
1143 mView.mPrivateFlags |= View.REQUEST_TRANSPARENT_REGIONS;
1144 // Need to make sure we re-evaluate the window attributes next
1145 // time around, to ensure the window has the correct format.
1146 mWindowAttributesChanged = true;
1147 }
1148 }
1149
1150 /**
1151 * Figures out the measure spec for the root view in a window based on it's
1152 * layout params.
1153 *
1154 * @param windowSize
1155 * The available width or height of the window
1156 *
1157 * @param rootDimension
1158 * The layout params for one dimension (width or height) of the
1159 * window.
1160 *
1161 * @return The measure spec to use to measure the root view.
1162 */
1163 private int getRootMeasureSpec(int windowSize, int rootDimension) {
1164 int measureSpec;
1165 switch (rootDimension) {
1166
1167 case ViewGroup.LayoutParams.FILL_PARENT:
1168 // Window can't resize. Force root view to be windowSize.
1169 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY);
1170 break;
1171 case ViewGroup.LayoutParams.WRAP_CONTENT:
1172 // Window can resize. Set max size for root view.
1173 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST);
1174 break;
1175 default:
1176 // Window wants to be an exact size. Force root view to be that size.
1177 measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);
1178 break;
1179 }
1180 return measureSpec;
1181 }
1182
1183 private void draw(boolean fullRedrawNeeded) {
1184 Surface surface = mSurface;
1185 if (surface == null || !surface.isValid()) {
1186 return;
1187 }
1188
1189 scrollToRectOrFocus(null, false);
1190
1191 if (mAttachInfo.mViewScrollChanged) {
1192 mAttachInfo.mViewScrollChanged = false;
1193 mAttachInfo.mTreeObserver.dispatchOnScrollChanged();
1194 }
Romain Guy8506ab42009-06-11 17:35:47 -07001195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 int yoff;
Romain Guy5bcdff42009-05-14 21:27:18 -07001197 final boolean scrolling = mScroller != null && mScroller.computeScrollOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 if (scrolling) {
1199 yoff = mScroller.getCurrY();
1200 } else {
1201 yoff = mScrollY;
1202 }
1203 if (mCurScrollY != yoff) {
1204 mCurScrollY = yoff;
1205 fullRedrawNeeded = true;
1206 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001207 float appScale = mAttachInfo.mApplicationScale;
1208 boolean scalingRequired = mAttachInfo.mScalingRequired;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209
1210 Rect dirty = mDirty;
1211 if (mUseGL) {
1212 if (!dirty.isEmpty()) {
1213 Canvas canvas = mGlCanvas;
Romain Guy5bcdff42009-05-14 21:27:18 -07001214 if (mGL != null && canvas != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 mGL.glDisable(GL_SCISSOR_TEST);
1216 mGL.glClearColor(0, 0, 0, 0);
1217 mGL.glClear(GL_COLOR_BUFFER_BIT);
1218 mGL.glEnable(GL_SCISSOR_TEST);
1219
1220 mAttachInfo.mDrawingTime = SystemClock.uptimeMillis();
Romain Guy5bcdff42009-05-14 21:27:18 -07001221 mAttachInfo.mIgnoreDirtyState = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 mView.mPrivateFlags |= View.DRAWN;
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001223
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001224 int saveCount = canvas.save(Canvas.MATRIX_SAVE_FLAG);
1225 try {
1226 canvas.translate(0, -yoff);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001227 if (mTranslator != null) {
1228 mTranslator.translateCanvas(canvas);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001229 }
Dianne Hackborn0d221012009-07-29 15:41:19 -07001230 canvas.setScreenDensity(scalingRequired
1231 ? DisplayMetrics.DENSITY_DEVICE : 0);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001232 mView.draw(canvas);
Romain Guy13922e02009-05-12 17:56:14 -07001233 if (Config.DEBUG && ViewDebug.consistencyCheckEnabled) {
1234 mView.dispatchConsistencyCheck(ViewDebug.CONSISTENCY_DRAWING);
1235 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001236 } finally {
1237 canvas.restoreToCount(saveCount);
1238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239
Romain Guy5bcdff42009-05-14 21:27:18 -07001240 mAttachInfo.mIgnoreDirtyState = false;
1241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 mEgl.eglSwapBuffers(mEglDisplay, mEglSurface);
1243 checkEglErrors();
1244
Romain Guy13922e02009-05-12 17:56:14 -07001245 if (Config.DEBUG && ViewDebug.showFps) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 int now = (int)SystemClock.elapsedRealtime();
1247 if (sDrawTime != 0) {
1248 nativeShowFPS(canvas, now - sDrawTime);
1249 }
1250 sDrawTime = now;
1251 }
1252 }
1253 }
1254 if (scrolling) {
1255 mFullRedrawNeeded = true;
1256 scheduleTraversals();
1257 }
1258 return;
1259 }
1260
Romain Guy5bcdff42009-05-14 21:27:18 -07001261 if (fullRedrawNeeded) {
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001262 mAttachInfo.mIgnoreDirtyState = true;
Mitsuru Oshima61324e52009-07-21 15:40:36 -07001263 dirty.union(0, 0, (int) (mWidth * appScale + 0.5f), (int) (mHeight * appScale + 0.5f));
Romain Guy5bcdff42009-05-14 21:27:18 -07001264 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265
1266 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
1267 Log.v("ViewRoot", "Draw " + mView + "/"
1268 + mWindowAttributes.getTitle()
1269 + ": dirty={" + dirty.left + "," + dirty.top
1270 + "," + dirty.right + "," + dirty.bottom + "} surface="
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001271 + surface + " surface.isValid()=" + surface.isValid() + ", appScale:" +
1272 appScale + ", width=" + mWidth + ", height=" + mHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 }
1274
1275 Canvas canvas;
1276 try {
Romain Guy5bcdff42009-05-14 21:27:18 -07001277 int left = dirty.left;
1278 int top = dirty.top;
1279 int right = dirty.right;
1280 int bottom = dirty.bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 canvas = surface.lockCanvas(dirty);
Romain Guy5bcdff42009-05-14 21:27:18 -07001282
1283 if (left != dirty.left || top != dirty.top || right != dirty.right ||
1284 bottom != dirty.bottom) {
1285 mAttachInfo.mIgnoreDirtyState = true;
1286 }
1287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 // TODO: Do this in native
Dianne Hackborn11ea3342009-07-22 21:48:55 -07001289 canvas.setDensity(mDensity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 } catch (Surface.OutOfResourcesException e) {
1291 Log.e("ViewRoot", "OutOfResourcesException locking surface", e);
1292 // TODO: we should ask the window manager to do something!
1293 // for now we just do nothing
1294 return;
Dianne Hackbornfd12af42009-08-27 00:44:33 -07001295 } catch (IllegalArgumentException e) {
1296 Log.e("ViewRoot", "IllegalArgumentException locking surface", e);
1297 // TODO: we should ask the window manager to do something!
1298 // for now we just do nothing
1299 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 }
1301
1302 try {
Romain Guybb93d552009-03-24 21:04:15 -07001303 if (!dirty.isEmpty() || mIsAnimating) {
Romain Guy13922e02009-05-12 17:56:14 -07001304 long startTime = 0L;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305
1306 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
1307 Log.v("ViewRoot", "Surface " + surface + " drawing to bitmap w="
1308 + canvas.getWidth() + ", h=" + canvas.getHeight());
1309 //canvas.drawARGB(255, 255, 0, 0);
1310 }
1311
Romain Guy13922e02009-05-12 17:56:14 -07001312 if (Config.DEBUG && ViewDebug.profileDrawing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 startTime = SystemClock.elapsedRealtime();
1314 }
1315
1316 // If this bitmap's format includes an alpha channel, we
1317 // need to clear it before drawing so that the child will
1318 // properly re-composite its drawing on a transparent
1319 // background. This automatically respects the clip/dirty region
Romain Guy5bcdff42009-05-14 21:27:18 -07001320 // or
1321 // If we are applying an offset, we need to clear the area
1322 // where the offset doesn't appear to avoid having garbage
1323 // left in the blank areas.
1324 if (!canvas.isOpaque() || yoff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
1326 }
1327
1328 dirty.setEmpty();
Romain Guybb93d552009-03-24 21:04:15 -07001329 mIsAnimating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 mAttachInfo.mDrawingTime = SystemClock.uptimeMillis();
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001331 mView.mPrivateFlags |= View.DRAWN;
1332
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001333 if (DEBUG_DRAW) {
Romain Guy5bcdff42009-05-14 21:27:18 -07001334 Context cxt = mView.getContext();
1335 Log.i(TAG, "Drawing: package:" + cxt.getPackageName() +
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07001336 ", metrics=" + cxt.getResources().getDisplayMetrics() +
1337 ", compatibilityInfo=" + cxt.getResources().getCompatibilityInfo());
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001338 }
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001339 int saveCount = canvas.save(Canvas.MATRIX_SAVE_FLAG);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001340 try {
1341 canvas.translate(0, -yoff);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001342 if (mTranslator != null) {
1343 mTranslator.translateCanvas(canvas);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001344 }
Dianne Hackborn0d221012009-07-29 15:41:19 -07001345 canvas.setScreenDensity(scalingRequired
1346 ? DisplayMetrics.DENSITY_DEVICE : 0);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001347 mView.draw(canvas);
1348 } finally {
Romain Guy5bcdff42009-05-14 21:27:18 -07001349 mAttachInfo.mIgnoreDirtyState = false;
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001350 canvas.restoreToCount(saveCount);
1351 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352
Romain Guy5bcdff42009-05-14 21:27:18 -07001353 if (Config.DEBUG && ViewDebug.consistencyCheckEnabled) {
1354 mView.dispatchConsistencyCheck(ViewDebug.CONSISTENCY_DRAWING);
1355 }
1356
Romain Guy13922e02009-05-12 17:56:14 -07001357 if (Config.DEBUG && ViewDebug.showFps) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 int now = (int)SystemClock.elapsedRealtime();
1359 if (sDrawTime != 0) {
1360 nativeShowFPS(canvas, now - sDrawTime);
1361 }
1362 sDrawTime = now;
1363 }
1364
Romain Guy13922e02009-05-12 17:56:14 -07001365 if (Config.DEBUG && ViewDebug.profileDrawing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 EventLog.writeEvent(60000, SystemClock.elapsedRealtime() - startTime);
1367 }
1368 }
Romain Guy8506ab42009-06-11 17:35:47 -07001369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 } finally {
1371 surface.unlockCanvasAndPost(canvas);
1372 }
1373
1374 if (LOCAL_LOGV) {
1375 Log.v("ViewRoot", "Surface " + surface + " unlockCanvasAndPost");
1376 }
Romain Guy8506ab42009-06-11 17:35:47 -07001377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 if (scrolling) {
1379 mFullRedrawNeeded = true;
1380 scheduleTraversals();
1381 }
1382 }
1383
1384 boolean scrollToRectOrFocus(Rect rectangle, boolean immediate) {
1385 final View.AttachInfo attachInfo = mAttachInfo;
1386 final Rect ci = attachInfo.mContentInsets;
1387 final Rect vi = attachInfo.mVisibleInsets;
1388 int scrollY = 0;
1389 boolean handled = false;
Romain Guy8506ab42009-06-11 17:35:47 -07001390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 if (vi.left > ci.left || vi.top > ci.top
1392 || vi.right > ci.right || vi.bottom > ci.bottom) {
1393 // We'll assume that we aren't going to change the scroll
1394 // offset, since we want to avoid that unless it is actually
1395 // going to make the focus visible... otherwise we scroll
1396 // all over the place.
1397 scrollY = mScrollY;
1398 // We can be called for two different situations: during a draw,
1399 // to update the scroll position if the focus has changed (in which
1400 // case 'rectangle' is null), or in response to a
1401 // requestChildRectangleOnScreen() call (in which case 'rectangle'
1402 // is non-null and we just want to scroll to whatever that
1403 // rectangle is).
1404 View focus = mRealFocusedView;
Romain Guye8b16522009-07-14 13:06:42 -07001405
1406 // When in touch mode, focus points to the previously focused view,
1407 // which may have been removed from the view hierarchy. The following
1408 // line checks whether the view is still in the hierarchy
1409 if (focus == null || focus.getParent() == null) {
1410 mRealFocusedView = null;
1411 return false;
1412 }
1413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 if (focus != mLastScrolledFocus) {
1415 // If the focus has changed, then ignore any requests to scroll
1416 // to a rectangle; first we want to make sure the entire focus
1417 // view is visible.
1418 rectangle = null;
1419 }
1420 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Eval scroll: focus=" + focus
1421 + " rectangle=" + rectangle + " ci=" + ci
1422 + " vi=" + vi);
1423 if (focus == mLastScrolledFocus && !mScrollMayChange
1424 && rectangle == null) {
1425 // Optimization: if the focus hasn't changed since last
1426 // time, and no layout has happened, then just leave things
1427 // as they are.
1428 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Keeping scroll y="
1429 + mScrollY + " vi=" + vi.toShortString());
1430 } else if (focus != null) {
1431 // We need to determine if the currently focused view is
1432 // within the visible part of the window and, if not, apply
1433 // a pan so it can be seen.
1434 mLastScrolledFocus = focus;
1435 mScrollMayChange = false;
1436 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Need to scroll?");
1437 // Try to find the rectangle from the focus view.
1438 if (focus.getGlobalVisibleRect(mVisRect, null)) {
1439 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Root w="
1440 + mView.getWidth() + " h=" + mView.getHeight()
1441 + " ci=" + ci.toShortString()
1442 + " vi=" + vi.toShortString());
1443 if (rectangle == null) {
1444 focus.getFocusedRect(mTempRect);
1445 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Focus " + focus
1446 + ": focusRect=" + mTempRect.toShortString());
1447 ((ViewGroup) mView).offsetDescendantRectToMyCoords(
1448 focus, mTempRect);
1449 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1450 "Focus in window: focusRect="
1451 + mTempRect.toShortString()
1452 + " visRect=" + mVisRect.toShortString());
1453 } else {
1454 mTempRect.set(rectangle);
1455 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1456 "Request scroll to rect: "
1457 + mTempRect.toShortString()
1458 + " visRect=" + mVisRect.toShortString());
1459 }
1460 if (mTempRect.intersect(mVisRect)) {
1461 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1462 "Focus window visible rect: "
1463 + mTempRect.toShortString());
1464 if (mTempRect.height() >
1465 (mView.getHeight()-vi.top-vi.bottom)) {
1466 // If the focus simply is not going to fit, then
1467 // best is probably just to leave things as-is.
1468 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1469 "Too tall; leaving scrollY=" + scrollY);
1470 } else if ((mTempRect.top-scrollY) < vi.top) {
1471 scrollY -= vi.top - (mTempRect.top-scrollY);
1472 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1473 "Top covered; scrollY=" + scrollY);
1474 } else if ((mTempRect.bottom-scrollY)
1475 > (mView.getHeight()-vi.bottom)) {
1476 scrollY += (mTempRect.bottom-scrollY)
1477 - (mView.getHeight()-vi.bottom);
1478 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1479 "Bottom covered; scrollY=" + scrollY);
1480 }
1481 handled = true;
1482 }
1483 }
1484 }
1485 }
Romain Guy8506ab42009-06-11 17:35:47 -07001486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 if (scrollY != mScrollY) {
1488 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Pan scroll changed: old="
1489 + mScrollY + " , new=" + scrollY);
1490 if (!immediate) {
1491 if (mScroller == null) {
1492 mScroller = new Scroller(mView.getContext());
1493 }
1494 mScroller.startScroll(0, mScrollY, 0, scrollY-mScrollY);
1495 } else if (mScroller != null) {
1496 mScroller.abortAnimation();
1497 }
1498 mScrollY = scrollY;
1499 }
Romain Guy8506ab42009-06-11 17:35:47 -07001500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 return handled;
1502 }
Romain Guy8506ab42009-06-11 17:35:47 -07001503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 public void requestChildFocus(View child, View focused) {
1505 checkThread();
1506 if (mFocusedView != focused) {
1507 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(mFocusedView, focused);
1508 scheduleTraversals();
1509 }
1510 mFocusedView = mRealFocusedView = focused;
1511 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Request child focus: focus now "
1512 + mFocusedView);
1513 }
1514
1515 public void clearChildFocus(View child) {
1516 checkThread();
1517
1518 View oldFocus = mFocusedView;
1519
1520 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Clearing child focus");
1521 mFocusedView = mRealFocusedView = null;
1522 if (mView != null && !mView.hasFocus()) {
1523 // If a view gets the focus, the listener will be invoked from requestChildFocus()
1524 if (!mView.requestFocus(View.FOCUS_FORWARD)) {
1525 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null);
1526 }
1527 } else if (oldFocus != null) {
1528 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null);
1529 }
1530 }
1531
1532
1533 public void focusableViewAvailable(View v) {
1534 checkThread();
1535
1536 if (mView != null && !mView.hasFocus()) {
1537 v.requestFocus();
1538 } else {
1539 // the one case where will transfer focus away from the current one
1540 // is if the current view is a view group that prefers to give focus
1541 // to its children first AND the view is a descendant of it.
1542 mFocusedView = mView.findFocus();
1543 boolean descendantsHaveDibsOnFocus =
1544 (mFocusedView instanceof ViewGroup) &&
1545 (((ViewGroup) mFocusedView).getDescendantFocusability() ==
1546 ViewGroup.FOCUS_AFTER_DESCENDANTS);
1547 if (descendantsHaveDibsOnFocus && isViewDescendantOf(v, mFocusedView)) {
1548 // If a view gets the focus, the listener will be invoked from requestChildFocus()
1549 v.requestFocus();
1550 }
1551 }
1552 }
1553
1554 public void recomputeViewAttributes(View child) {
1555 checkThread();
1556 if (mView == child) {
1557 mAttachInfo.mRecomputeGlobalAttributes = true;
1558 if (!mWillDrawSoon) {
1559 scheduleTraversals();
1560 }
1561 }
1562 }
1563
1564 void dispatchDetachedFromWindow() {
1565 if (Config.LOGV) Log.v("ViewRoot", "Detaching in " + this + " of " + mSurface);
1566
1567 if (mView != null) {
1568 mView.dispatchDetachedFromWindow();
1569 }
1570
1571 mView = null;
1572 mAttachInfo.mRootView = null;
Mathias Agopian5583dc62009-07-09 16:28:11 -07001573 mAttachInfo.mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574
1575 if (mUseGL) {
1576 destroyGL();
1577 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001578 mSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579
1580 try {
1581 sWindowSession.remove(mWindow);
1582 } catch (RemoteException e) {
1583 }
1584 }
Romain Guy8506ab42009-06-11 17:35:47 -07001585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 /**
1587 * Return true if child is an ancestor of parent, (or equal to the parent).
1588 */
1589 private static boolean isViewDescendantOf(View child, View parent) {
1590 if (child == parent) {
1591 return true;
1592 }
1593
1594 final ViewParent theParent = child.getParent();
1595 return (theParent instanceof ViewGroup) && isViewDescendantOf((View) theParent, parent);
1596 }
1597
1598
1599 public final static int DO_TRAVERSAL = 1000;
1600 public final static int DIE = 1001;
1601 public final static int RESIZED = 1002;
1602 public final static int RESIZED_REPORT = 1003;
1603 public final static int WINDOW_FOCUS_CHANGED = 1004;
1604 public final static int DISPATCH_KEY = 1005;
1605 public final static int DISPATCH_POINTER = 1006;
1606 public final static int DISPATCH_TRACKBALL = 1007;
1607 public final static int DISPATCH_APP_VISIBILITY = 1008;
1608 public final static int DISPATCH_GET_NEW_SURFACE = 1009;
1609 public final static int FINISHED_EVENT = 1010;
1610 public final static int DISPATCH_KEY_FROM_IME = 1011;
1611 public final static int FINISH_INPUT_CONNECTION = 1012;
1612 public final static int CHECK_FOCUS = 1013;
1613
1614 @Override
1615 public void handleMessage(Message msg) {
1616 switch (msg.what) {
1617 case View.AttachInfo.INVALIDATE_MSG:
1618 ((View) msg.obj).invalidate();
1619 break;
1620 case View.AttachInfo.INVALIDATE_RECT_MSG:
1621 final View.AttachInfo.InvalidateInfo info = (View.AttachInfo.InvalidateInfo) msg.obj;
1622 info.target.invalidate(info.left, info.top, info.right, info.bottom);
1623 info.release();
1624 break;
1625 case DO_TRAVERSAL:
1626 if (mProfile) {
1627 Debug.startMethodTracing("ViewRoot");
1628 }
1629
1630 performTraversals();
1631
1632 if (mProfile) {
1633 Debug.stopMethodTracing();
1634 mProfile = false;
1635 }
1636 break;
1637 case FINISHED_EVENT:
1638 handleFinishedEvent(msg.arg1, msg.arg2 != 0);
1639 break;
1640 case DISPATCH_KEY:
1641 if (LOCAL_LOGV) Log.v(
1642 "ViewRoot", "Dispatching key "
1643 + msg.obj + " to " + mView);
1644 deliverKeyEvent((KeyEvent)msg.obj, true);
1645 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07001646 case DISPATCH_POINTER: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 MotionEvent event = (MotionEvent)msg.obj;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001648 boolean callWhenDone = msg.arg1 != 0;
1649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 if (event == null) {
1651 try {
Michael Chan53071d62009-05-13 17:29:48 -07001652 long timeBeforeGettingEvents;
1653 if (MEASURE_LATENCY) {
1654 timeBeforeGettingEvents = System.nanoTime();
1655 }
1656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 event = sWindowSession.getPendingPointerMove(mWindow);
Michael Chan53071d62009-05-13 17:29:48 -07001658
1659 if (MEASURE_LATENCY && event != null) {
1660 lt.sample("9 Client got events ", System.nanoTime() - event.getEventTimeNano());
1661 lt.sample("8 Client getting events ", timeBeforeGettingEvents - event.getEventTimeNano());
1662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 } catch (RemoteException e) {
1664 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001665 callWhenDone = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001667 if (event != null && mTranslator != null) {
1668 mTranslator.translateEventInScreenToAppWindow(event);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 try {
1671 boolean handled;
1672 if (mView != null && mAdded && event != null) {
1673
1674 // enter touch mode on the down
1675 boolean isDown = event.getAction() == MotionEvent.ACTION_DOWN;
1676 if (isDown) {
1677 ensureTouchMode(true);
1678 }
1679 if(Config.LOGV) {
1680 captureMotionLog("captureDispatchPointer", event);
1681 }
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001682 if (mCurScrollY != 0) {
1683 event.offsetLocation(0, mCurScrollY);
1684 }
Michael Chan53071d62009-05-13 17:29:48 -07001685 if (MEASURE_LATENCY) {
1686 lt.sample("A Dispatching TouchEvents", System.nanoTime() - event.getEventTimeNano());
1687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 handled = mView.dispatchTouchEvent(event);
Michael Chan53071d62009-05-13 17:29:48 -07001689 if (MEASURE_LATENCY) {
1690 lt.sample("B Dispatched TouchEvents ", System.nanoTime() - event.getEventTimeNano());
1691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 if (!handled && isDown) {
1693 int edgeSlop = mViewConfiguration.getScaledEdgeSlop();
1694
1695 final int edgeFlags = event.getEdgeFlags();
1696 int direction = View.FOCUS_UP;
1697 int x = (int)event.getX();
1698 int y = (int)event.getY();
1699 final int[] deltas = new int[2];
1700
1701 if ((edgeFlags & MotionEvent.EDGE_TOP) != 0) {
1702 direction = View.FOCUS_DOWN;
1703 if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
1704 deltas[0] = edgeSlop;
1705 x += edgeSlop;
1706 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
1707 deltas[0] = -edgeSlop;
1708 x -= edgeSlop;
1709 }
1710 } else if ((edgeFlags & MotionEvent.EDGE_BOTTOM) != 0) {
1711 direction = View.FOCUS_UP;
1712 if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
1713 deltas[0] = edgeSlop;
1714 x += edgeSlop;
1715 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
1716 deltas[0] = -edgeSlop;
1717 x -= edgeSlop;
1718 }
1719 } else if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
1720 direction = View.FOCUS_RIGHT;
1721 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
1722 direction = View.FOCUS_LEFT;
1723 }
1724
1725 if (edgeFlags != 0 && mView instanceof ViewGroup) {
1726 View nearest = FocusFinder.getInstance().findNearestTouchable(
1727 ((ViewGroup) mView), x, y, direction, deltas);
1728 if (nearest != null) {
1729 event.offsetLocation(deltas[0], deltas[1]);
1730 event.setEdgeFlags(0);
1731 mView.dispatchTouchEvent(event);
1732 }
1733 }
1734 }
1735 }
1736 } finally {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001737 if (callWhenDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 try {
1739 sWindowSession.finishKey(mWindow);
1740 } catch (RemoteException e) {
1741 }
1742 }
1743 if (event != null) {
1744 event.recycle();
1745 }
1746 if (LOCAL_LOGV || WATCH_POINTER) Log.i(TAG, "Done dispatching!");
1747 // Let the exception fall through -- the looper will catch
1748 // it and take care of the bad app for us.
1749 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001750 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 case DISPATCH_TRACKBALL:
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001752 deliverTrackballEvent((MotionEvent)msg.obj, msg.arg1 != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 break;
1754 case DISPATCH_APP_VISIBILITY:
1755 handleAppVisibility(msg.arg1 != 0);
1756 break;
1757 case DISPATCH_GET_NEW_SURFACE:
1758 handleGetNewSurface();
1759 break;
1760 case RESIZED:
1761 Rect coveredInsets = ((Rect[])msg.obj)[0];
1762 Rect visibleInsets = ((Rect[])msg.obj)[1];
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 if (mWinFrame.width() == msg.arg1 && mWinFrame.height() == msg.arg2
1765 && mPendingContentInsets.equals(coveredInsets)
1766 && mPendingVisibleInsets.equals(visibleInsets)) {
1767 break;
1768 }
1769 // fall through...
1770 case RESIZED_REPORT:
1771 if (mAdded) {
1772 mWinFrame.left = 0;
1773 mWinFrame.right = msg.arg1;
1774 mWinFrame.top = 0;
1775 mWinFrame.bottom = msg.arg2;
1776 mPendingContentInsets.set(((Rect[])msg.obj)[0]);
1777 mPendingVisibleInsets.set(((Rect[])msg.obj)[1]);
1778 if (msg.what == RESIZED_REPORT) {
1779 mReportNextDraw = true;
1780 }
1781 requestLayout();
1782 }
1783 break;
1784 case WINDOW_FOCUS_CHANGED: {
1785 if (mAdded) {
1786 boolean hasWindowFocus = msg.arg1 != 0;
1787 mAttachInfo.mHasWindowFocus = hasWindowFocus;
1788 if (hasWindowFocus) {
1789 boolean inTouchMode = msg.arg2 != 0;
1790 ensureTouchModeLocally(inTouchMode);
1791
1792 if (mGlWanted) {
1793 checkEglErrors();
1794 // we lost the gl context, so recreate it.
1795 if (mGlWanted && !mUseGL) {
1796 initializeGL();
1797 if (mGlCanvas != null) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001798 float appScale = mAttachInfo.mApplicationScale;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001799 mGlCanvas.setViewport(
Mitsuru Oshima61324e52009-07-21 15:40:36 -07001800 (int) (mWidth * appScale + 0.5f),
1801 (int) (mHeight * appScale + 0.5f));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 }
1803 }
1804 }
1805 }
Romain Guy8506ab42009-06-11 17:35:47 -07001806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 mLastWasImTarget = WindowManager.LayoutParams
1808 .mayUseInputMethod(mWindowAttributes.flags);
Romain Guy8506ab42009-06-11 17:35:47 -07001809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 InputMethodManager imm = InputMethodManager.peekInstance();
1811 if (mView != null) {
1812 if (hasWindowFocus && imm != null && mLastWasImTarget) {
1813 imm.startGettingWindowFocus(mView);
1814 }
1815 mView.dispatchWindowFocusChanged(hasWindowFocus);
1816 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 // Note: must be done after the focus change callbacks,
1819 // so all of the view state is set up correctly.
1820 if (hasWindowFocus) {
1821 if (imm != null && mLastWasImTarget) {
1822 imm.onWindowFocus(mView, mView.findFocus(),
1823 mWindowAttributes.softInputMode,
1824 !mHasHadWindowFocus, mWindowAttributes.flags);
1825 }
1826 // Clear the forward bit. We can just do this directly, since
1827 // the window manager doesn't care about it.
1828 mWindowAttributes.softInputMode &=
1829 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
1830 ((WindowManager.LayoutParams)mView.getLayoutParams())
1831 .softInputMode &=
1832 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
1833 mHasHadWindowFocus = true;
1834 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001835
1836 if (hasWindowFocus && mView != null) {
1837 sendAccessibilityEvents();
1838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 }
1840 } break;
1841 case DIE:
1842 dispatchDetachedFromWindow();
1843 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07001844 case DISPATCH_KEY_FROM_IME: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 if (LOCAL_LOGV) Log.v(
1846 "ViewRoot", "Dispatching key "
1847 + msg.obj + " from IME to " + mView);
The Android Open Source Project10592532009-03-18 17:39:46 -07001848 KeyEvent event = (KeyEvent)msg.obj;
1849 if ((event.getFlags()&KeyEvent.FLAG_FROM_SYSTEM) != 0) {
1850 // The IME is trying to say this event is from the
1851 // system! Bad bad bad!
1852 event = KeyEvent.changeFlags(event,
1853 event.getFlags()&~KeyEvent.FLAG_FROM_SYSTEM);
1854 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 deliverKeyEventToViewHierarchy((KeyEvent)msg.obj, false);
The Android Open Source Project10592532009-03-18 17:39:46 -07001856 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 case FINISH_INPUT_CONNECTION: {
1858 InputMethodManager imm = InputMethodManager.peekInstance();
1859 if (imm != null) {
1860 imm.reportFinishInputConnection((InputConnection)msg.obj);
1861 }
1862 } break;
1863 case CHECK_FOCUS: {
1864 InputMethodManager imm = InputMethodManager.peekInstance();
1865 if (imm != null) {
1866 imm.checkFocus();
1867 }
1868 } break;
1869 }
1870 }
1871
1872 /**
1873 * Something in the current window tells us we need to change the touch mode. For
1874 * example, we are not in touch mode, and the user touches the screen.
1875 *
1876 * If the touch mode has changed, tell the window manager, and handle it locally.
1877 *
1878 * @param inTouchMode Whether we want to be in touch mode.
1879 * @return True if the touch mode changed and focus changed was changed as a result
1880 */
1881 boolean ensureTouchMode(boolean inTouchMode) {
1882 if (DBG) Log.d("touchmode", "ensureTouchMode(" + inTouchMode + "), current "
1883 + "touch mode is " + mAttachInfo.mInTouchMode);
1884 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
1885
1886 // tell the window manager
1887 try {
1888 sWindowSession.setInTouchMode(inTouchMode);
1889 } catch (RemoteException e) {
1890 throw new RuntimeException(e);
1891 }
1892
1893 // handle the change
1894 return ensureTouchModeLocally(inTouchMode);
1895 }
1896
1897 /**
1898 * Ensure that the touch mode for this window is set, and if it is changing,
1899 * take the appropriate action.
1900 * @param inTouchMode Whether we want to be in touch mode.
1901 * @return True if the touch mode changed and focus changed was changed as a result
1902 */
1903 private boolean ensureTouchModeLocally(boolean inTouchMode) {
1904 if (DBG) Log.d("touchmode", "ensureTouchModeLocally(" + inTouchMode + "), current "
1905 + "touch mode is " + mAttachInfo.mInTouchMode);
1906
1907 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
1908
1909 mAttachInfo.mInTouchMode = inTouchMode;
1910 mAttachInfo.mTreeObserver.dispatchOnTouchModeChanged(inTouchMode);
1911
1912 return (inTouchMode) ? enterTouchMode() : leaveTouchMode();
1913 }
1914
1915 private boolean enterTouchMode() {
1916 if (mView != null) {
1917 if (mView.hasFocus()) {
1918 // note: not relying on mFocusedView here because this could
1919 // be when the window is first being added, and mFocused isn't
1920 // set yet.
1921 final View focused = mView.findFocus();
1922 if (focused != null && !focused.isFocusableInTouchMode()) {
1923
1924 final ViewGroup ancestorToTakeFocus =
1925 findAncestorToTakeFocusInTouchMode(focused);
1926 if (ancestorToTakeFocus != null) {
1927 // there is an ancestor that wants focus after its descendants that
1928 // is focusable in touch mode.. give it focus
1929 return ancestorToTakeFocus.requestFocus();
1930 } else {
1931 // nothing appropriate to have focus in touch mode, clear it out
1932 mView.unFocus();
1933 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(focused, null);
1934 mFocusedView = null;
1935 return true;
1936 }
1937 }
1938 }
1939 }
1940 return false;
1941 }
1942
1943
1944 /**
1945 * Find an ancestor of focused that wants focus after its descendants and is
1946 * focusable in touch mode.
1947 * @param focused The currently focused view.
1948 * @return An appropriate view, or null if no such view exists.
1949 */
1950 private ViewGroup findAncestorToTakeFocusInTouchMode(View focused) {
1951 ViewParent parent = focused.getParent();
1952 while (parent instanceof ViewGroup) {
1953 final ViewGroup vgParent = (ViewGroup) parent;
1954 if (vgParent.getDescendantFocusability() == ViewGroup.FOCUS_AFTER_DESCENDANTS
1955 && vgParent.isFocusableInTouchMode()) {
1956 return vgParent;
1957 }
1958 if (vgParent.isRootNamespace()) {
1959 return null;
1960 } else {
1961 parent = vgParent.getParent();
1962 }
1963 }
1964 return null;
1965 }
1966
1967 private boolean leaveTouchMode() {
1968 if (mView != null) {
1969 if (mView.hasFocus()) {
1970 // i learned the hard way to not trust mFocusedView :)
1971 mFocusedView = mView.findFocus();
1972 if (!(mFocusedView instanceof ViewGroup)) {
1973 // some view has focus, let it keep it
1974 return false;
1975 } else if (((ViewGroup)mFocusedView).getDescendantFocusability() !=
1976 ViewGroup.FOCUS_AFTER_DESCENDANTS) {
1977 // some view group has focus, and doesn't prefer its children
1978 // over itself for focus, so let them keep it.
1979 return false;
1980 }
1981 }
1982
1983 // find the best view to give focus to in this brave new non-touch-mode
1984 // world
1985 final View focused = focusSearch(null, View.FOCUS_DOWN);
1986 if (focused != null) {
1987 return focused.requestFocus(View.FOCUS_DOWN);
1988 }
1989 }
1990 return false;
1991 }
1992
1993
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001994 private void deliverTrackballEvent(MotionEvent event, boolean callWhenDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 if (event == null) {
1996 try {
1997 event = sWindowSession.getPendingTrackballMove(mWindow);
1998 } catch (RemoteException e) {
1999 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002000 callWhenDone = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 }
2002
2003 if (DEBUG_TRACKBALL) Log.v(TAG, "Motion event:" + event);
2004
2005 boolean handled = false;
2006 try {
2007 if (event == null) {
2008 handled = true;
2009 } else if (mView != null && mAdded) {
2010 handled = mView.dispatchTrackballEvent(event);
2011 if (!handled) {
2012 // we could do something here, like changing the focus
2013 // or something?
2014 }
2015 }
2016 } finally {
2017 if (handled) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002018 if (callWhenDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 try {
2020 sWindowSession.finishKey(mWindow);
2021 } catch (RemoteException e) {
2022 }
2023 }
2024 if (event != null) {
2025 event.recycle();
2026 }
2027 // If we reach this, we delivered a trackball event to mView and
2028 // mView consumed it. Because we will not translate the trackball
2029 // event into a key event, touch mode will not exit, so we exit
2030 // touch mode here.
2031 ensureTouchMode(false);
2032 //noinspection ReturnInsideFinallyBlock
2033 return;
2034 }
2035 // Let the exception fall through -- the looper will catch
2036 // it and take care of the bad app for us.
2037 }
2038
2039 final TrackballAxis x = mTrackballAxisX;
2040 final TrackballAxis y = mTrackballAxisY;
2041
2042 long curTime = SystemClock.uptimeMillis();
2043 if ((mLastTrackballTime+MAX_TRACKBALL_DELAY) < curTime) {
2044 // It has been too long since the last movement,
2045 // so restart at the beginning.
2046 x.reset(0);
2047 y.reset(0);
2048 mLastTrackballTime = curTime;
2049 }
2050
2051 try {
2052 final int action = event.getAction();
2053 final int metastate = event.getMetaState();
2054 switch (action) {
2055 case MotionEvent.ACTION_DOWN:
2056 x.reset(2);
2057 y.reset(2);
2058 deliverKeyEvent(new KeyEvent(curTime, curTime,
2059 KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER,
2060 0, metastate), false);
2061 break;
2062 case MotionEvent.ACTION_UP:
2063 x.reset(2);
2064 y.reset(2);
2065 deliverKeyEvent(new KeyEvent(curTime, curTime,
2066 KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER,
2067 0, metastate), false);
2068 break;
2069 }
2070
2071 if (DEBUG_TRACKBALL) Log.v(TAG, "TB X=" + x.position + " step="
2072 + x.step + " dir=" + x.dir + " acc=" + x.acceleration
2073 + " move=" + event.getX()
2074 + " / Y=" + y.position + " step="
2075 + y.step + " dir=" + y.dir + " acc=" + y.acceleration
2076 + " move=" + event.getY());
2077 final float xOff = x.collect(event.getX(), event.getEventTime(), "X");
2078 final float yOff = y.collect(event.getY(), event.getEventTime(), "Y");
2079
2080 // Generate DPAD events based on the trackball movement.
2081 // We pick the axis that has moved the most as the direction of
2082 // the DPAD. When we generate DPAD events for one axis, then the
2083 // other axis is reset -- we don't want to perform DPAD jumps due
2084 // to slight movements in the trackball when making major movements
2085 // along the other axis.
2086 int keycode = 0;
2087 int movement = 0;
2088 float accel = 1;
2089 if (xOff > yOff) {
2090 movement = x.generate((2/event.getXPrecision()));
2091 if (movement != 0) {
2092 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_RIGHT
2093 : KeyEvent.KEYCODE_DPAD_LEFT;
2094 accel = x.acceleration;
2095 y.reset(2);
2096 }
2097 } else if (yOff > 0) {
2098 movement = y.generate((2/event.getYPrecision()));
2099 if (movement != 0) {
2100 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_DOWN
2101 : KeyEvent.KEYCODE_DPAD_UP;
2102 accel = y.acceleration;
2103 x.reset(2);
2104 }
2105 }
2106
2107 if (keycode != 0) {
2108 if (movement < 0) movement = -movement;
2109 int accelMovement = (int)(movement * accel);
2110 if (DEBUG_TRACKBALL) Log.v(TAG, "Move: movement=" + movement
2111 + " accelMovement=" + accelMovement
2112 + " accel=" + accel);
2113 if (accelMovement > movement) {
2114 if (DEBUG_TRACKBALL) Log.v("foo", "Delivering fake DPAD: "
2115 + keycode);
2116 movement--;
2117 deliverKeyEvent(new KeyEvent(curTime, curTime,
2118 KeyEvent.ACTION_MULTIPLE, keycode,
2119 accelMovement-movement, metastate), false);
2120 }
2121 while (movement > 0) {
2122 if (DEBUG_TRACKBALL) Log.v("foo", "Delivering fake DPAD: "
2123 + keycode);
2124 movement--;
2125 curTime = SystemClock.uptimeMillis();
2126 deliverKeyEvent(new KeyEvent(curTime, curTime,
2127 KeyEvent.ACTION_DOWN, keycode, 0, event.getMetaState()), false);
2128 deliverKeyEvent(new KeyEvent(curTime, curTime,
2129 KeyEvent.ACTION_UP, keycode, 0, metastate), false);
2130 }
2131 mLastTrackballTime = curTime;
2132 }
2133 } finally {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002134 if (callWhenDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 try {
2136 sWindowSession.finishKey(mWindow);
2137 } catch (RemoteException e) {
2138 }
2139 if (event != null) {
2140 event.recycle();
2141 }
2142 }
2143 // Let the exception fall through -- the looper will catch
2144 // it and take care of the bad app for us.
2145 }
2146 }
2147
2148 /**
2149 * @param keyCode The key code
2150 * @return True if the key is directional.
2151 */
2152 static boolean isDirectional(int keyCode) {
2153 switch (keyCode) {
2154 case KeyEvent.KEYCODE_DPAD_LEFT:
2155 case KeyEvent.KEYCODE_DPAD_RIGHT:
2156 case KeyEvent.KEYCODE_DPAD_UP:
2157 case KeyEvent.KEYCODE_DPAD_DOWN:
2158 return true;
2159 }
2160 return false;
2161 }
2162
2163 /**
2164 * Returns true if this key is a keyboard key.
2165 * @param keyEvent The key event.
2166 * @return whether this key is a keyboard key.
2167 */
2168 private static boolean isKeyboardKey(KeyEvent keyEvent) {
2169 final int convertedKey = keyEvent.getUnicodeChar();
2170 return convertedKey > 0;
2171 }
2172
2173
2174
2175 /**
2176 * See if the key event means we should leave touch mode (and leave touch
2177 * mode if so).
2178 * @param event The key event.
2179 * @return Whether this key event should be consumed (meaning the act of
2180 * leaving touch mode alone is considered the event).
2181 */
2182 private boolean checkForLeavingTouchModeAndConsume(KeyEvent event) {
2183 if (event.getAction() != KeyEvent.ACTION_DOWN) {
2184 return false;
2185 }
2186 if ((event.getFlags()&KeyEvent.FLAG_KEEP_TOUCH_MODE) != 0) {
2187 return false;
2188 }
2189
2190 // only relevant if we are in touch mode
2191 if (!mAttachInfo.mInTouchMode) {
2192 return false;
2193 }
2194
2195 // if something like an edit text has focus and the user is typing,
2196 // leave touch mode
2197 //
2198 // note: the condition of not being a keyboard key is kind of a hacky
2199 // approximation of whether we think the focused view will want the
2200 // key; if we knew for sure whether the focused view would consume
2201 // the event, that would be better.
2202 if (isKeyboardKey(event) && mView != null && mView.hasFocus()) {
2203 mFocusedView = mView.findFocus();
2204 if ((mFocusedView instanceof ViewGroup)
2205 && ((ViewGroup) mFocusedView).getDescendantFocusability() ==
2206 ViewGroup.FOCUS_AFTER_DESCENDANTS) {
2207 // something has focus, but is holding it weakly as a container
2208 return false;
2209 }
2210 if (ensureTouchMode(false)) {
2211 throw new IllegalStateException("should not have changed focus "
2212 + "when leaving touch mode while a view has focus.");
2213 }
2214 return false;
2215 }
2216
2217 if (isDirectional(event.getKeyCode())) {
2218 // no view has focus, so we leave touch mode (and find something
2219 // to give focus to). the event is consumed if we were able to
2220 // find something to give focus to.
2221 return ensureTouchMode(false);
2222 }
2223 return false;
2224 }
2225
2226 /**
Romain Guy8506ab42009-06-11 17:35:47 -07002227 * log motion events
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 */
2229 private static void captureMotionLog(String subTag, MotionEvent ev) {
Romain Guy8506ab42009-06-11 17:35:47 -07002230 //check dynamic switch
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002231 if (ev == null ||
2232 SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_EVENT, 0) == 0) {
2233 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 }
Romain Guy8506ab42009-06-11 17:35:47 -07002235
2236 StringBuilder sb = new StringBuilder(subTag + ": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 sb.append(ev.getDownTime()).append(',');
2238 sb.append(ev.getEventTime()).append(',');
2239 sb.append(ev.getAction()).append(',');
Romain Guy8506ab42009-06-11 17:35:47 -07002240 sb.append(ev.getX()).append(',');
2241 sb.append(ev.getY()).append(',');
2242 sb.append(ev.getPressure()).append(',');
2243 sb.append(ev.getSize()).append(',');
2244 sb.append(ev.getMetaState()).append(',');
2245 sb.append(ev.getXPrecision()).append(',');
2246 sb.append(ev.getYPrecision()).append(',');
2247 sb.append(ev.getDeviceId()).append(',');
2248 sb.append(ev.getEdgeFlags());
2249 Log.d(TAG, sb.toString());
2250 }
2251 /**
2252 * log motion events
2253 */
2254 private static void captureKeyLog(String subTag, KeyEvent ev) {
2255 //check dynamic switch
2256 if (ev == null ||
2257 SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_EVENT, 0) == 0) {
2258 return;
2259 }
2260 StringBuilder sb = new StringBuilder(subTag + ": ");
2261 sb.append(ev.getDownTime()).append(',');
2262 sb.append(ev.getEventTime()).append(',');
2263 sb.append(ev.getAction()).append(',');
2264 sb.append(ev.getKeyCode()).append(',');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 sb.append(ev.getRepeatCount()).append(',');
2266 sb.append(ev.getMetaState()).append(',');
2267 sb.append(ev.getDeviceId()).append(',');
2268 sb.append(ev.getScanCode());
Romain Guy8506ab42009-06-11 17:35:47 -07002269 Log.d(TAG, sb.toString());
2270 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271
2272 int enqueuePendingEvent(Object event, boolean sendDone) {
2273 int seq = mPendingEventSeq+1;
2274 if (seq < 0) seq = 0;
2275 mPendingEventSeq = seq;
2276 mPendingEvents.put(seq, event);
2277 return sendDone ? seq : -seq;
2278 }
2279
2280 Object retrievePendingEvent(int seq) {
2281 if (seq < 0) seq = -seq;
2282 Object event = mPendingEvents.get(seq);
2283 if (event != null) {
2284 mPendingEvents.remove(seq);
2285 }
2286 return event;
2287 }
Romain Guy8506ab42009-06-11 17:35:47 -07002288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 private void deliverKeyEvent(KeyEvent event, boolean sendDone) {
2290 // If mView is null, we just consume the key event because it doesn't
2291 // make sense to do anything else with it.
2292 boolean handled = mView != null
2293 ? mView.dispatchKeyEventPreIme(event) : true;
2294 if (handled) {
2295 if (sendDone) {
2296 if (LOCAL_LOGV) Log.v(
2297 "ViewRoot", "Telling window manager key is finished");
2298 try {
2299 sWindowSession.finishKey(mWindow);
2300 } catch (RemoteException e) {
2301 }
2302 }
2303 return;
2304 }
2305 // If it is possible for this window to interact with the input
2306 // method window, then we want to first dispatch our key events
2307 // to the input method.
2308 if (mLastWasImTarget) {
2309 InputMethodManager imm = InputMethodManager.peekInstance();
2310 if (imm != null && mView != null) {
2311 int seq = enqueuePendingEvent(event, sendDone);
2312 if (DEBUG_IMF) Log.v(TAG, "Sending key event to IME: seq="
2313 + seq + " event=" + event);
2314 imm.dispatchKeyEvent(mView.getContext(), seq, event,
2315 mInputMethodCallback);
2316 return;
2317 }
2318 }
2319 deliverKeyEventToViewHierarchy(event, sendDone);
2320 }
2321
2322 void handleFinishedEvent(int seq, boolean handled) {
2323 final KeyEvent event = (KeyEvent)retrievePendingEvent(seq);
2324 if (DEBUG_IMF) Log.v(TAG, "IME finished event: seq=" + seq
2325 + " handled=" + handled + " event=" + event);
2326 if (event != null) {
2327 final boolean sendDone = seq >= 0;
2328 if (!handled) {
2329 deliverKeyEventToViewHierarchy(event, sendDone);
2330 return;
2331 } else if (sendDone) {
2332 if (LOCAL_LOGV) Log.v(
2333 "ViewRoot", "Telling window manager key is finished");
2334 try {
2335 sWindowSession.finishKey(mWindow);
2336 } catch (RemoteException e) {
2337 }
2338 } else {
2339 Log.w("ViewRoot", "handleFinishedEvent(seq=" + seq
2340 + " handled=" + handled + " ev=" + event
2341 + ") neither delivering nor finishing key");
2342 }
2343 }
2344 }
Romain Guy8506ab42009-06-11 17:35:47 -07002345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 private void deliverKeyEventToViewHierarchy(KeyEvent event, boolean sendDone) {
2347 try {
2348 if (mView != null && mAdded) {
2349 final int action = event.getAction();
2350 boolean isDown = (action == KeyEvent.ACTION_DOWN);
2351
2352 if (checkForLeavingTouchModeAndConsume(event)) {
2353 return;
Romain Guy8506ab42009-06-11 17:35:47 -07002354 }
2355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 if (Config.LOGV) {
2357 captureKeyLog("captureDispatchKeyEvent", event);
2358 }
2359 boolean keyHandled = mView.dispatchKeyEvent(event);
2360
2361 if (!keyHandled && isDown) {
2362 int direction = 0;
2363 switch (event.getKeyCode()) {
2364 case KeyEvent.KEYCODE_DPAD_LEFT:
2365 direction = View.FOCUS_LEFT;
2366 break;
2367 case KeyEvent.KEYCODE_DPAD_RIGHT:
2368 direction = View.FOCUS_RIGHT;
2369 break;
2370 case KeyEvent.KEYCODE_DPAD_UP:
2371 direction = View.FOCUS_UP;
2372 break;
2373 case KeyEvent.KEYCODE_DPAD_DOWN:
2374 direction = View.FOCUS_DOWN;
2375 break;
2376 }
2377
2378 if (direction != 0) {
2379
2380 View focused = mView != null ? mView.findFocus() : null;
2381 if (focused != null) {
2382 View v = focused.focusSearch(direction);
2383 boolean focusPassed = false;
2384 if (v != null && v != focused) {
2385 // do the math the get the interesting rect
2386 // of previous focused into the coord system of
2387 // newly focused view
2388 focused.getFocusedRect(mTempRect);
2389 ((ViewGroup) mView).offsetDescendantRectToMyCoords(focused, mTempRect);
2390 ((ViewGroup) mView).offsetRectIntoDescendantCoords(v, mTempRect);
2391 focusPassed = v.requestFocus(direction, mTempRect);
2392 }
2393
2394 if (!focusPassed) {
2395 mView.dispatchUnhandledMove(focused, direction);
2396 } else {
2397 playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
2398 }
2399 }
2400 }
2401 }
2402 }
2403
2404 } finally {
2405 if (sendDone) {
2406 if (LOCAL_LOGV) Log.v(
2407 "ViewRoot", "Telling window manager key is finished");
2408 try {
2409 sWindowSession.finishKey(mWindow);
2410 } catch (RemoteException e) {
2411 }
2412 }
2413 // Let the exception fall through -- the looper will catch
2414 // it and take care of the bad app for us.
2415 }
2416 }
2417
2418 private AudioManager getAudioManager() {
2419 if (mView == null) {
2420 throw new IllegalStateException("getAudioManager called when there is no mView");
2421 }
2422 if (mAudioManager == null) {
2423 mAudioManager = (AudioManager) mView.getContext().getSystemService(Context.AUDIO_SERVICE);
2424 }
2425 return mAudioManager;
2426 }
2427
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002428 private int relayoutWindow(WindowManager.LayoutParams params, int viewVisibility,
2429 boolean insetsPending) throws RemoteException {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002430
2431 float appScale = mAttachInfo.mApplicationScale;
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002432 boolean restore = false;
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002433 if (params != null && mTranslator != null) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -07002434 restore = true;
2435 params.backup();
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002436 mTranslator.translateWindowLayout(params);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002437 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002438 if (params != null) {
2439 if (DBG) Log.d(TAG, "WindowLayout in layoutWindow:" + params);
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002440 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002441 int relayoutResult = sWindowSession.relayout(
2442 mWindow, params,
Mitsuru Oshima61324e52009-07-21 15:40:36 -07002443 (int) (mView.mMeasuredWidth * appScale + 0.5f),
2444 (int) (mView.mMeasuredHeight * appScale + 0.5f),
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002445 viewVisibility, insetsPending, mWinFrame,
2446 mPendingContentInsets, mPendingVisibleInsets, mSurface);
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002447 if (restore) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -07002448 params.restore();
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002449 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002450
2451 if (mTranslator != null) {
2452 mTranslator.translateRectInScreenToAppWinFrame(mWinFrame);
2453 mTranslator.translateRectInScreenToAppWindow(mPendingContentInsets);
2454 mTranslator.translateRectInScreenToAppWindow(mPendingVisibleInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002455 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002456 return relayoutResult;
2457 }
Romain Guy8506ab42009-06-11 17:35:47 -07002458
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002459 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 * {@inheritDoc}
2461 */
2462 public void playSoundEffect(int effectId) {
2463 checkThread();
2464
2465 final AudioManager audioManager = getAudioManager();
2466
2467 switch (effectId) {
2468 case SoundEffectConstants.CLICK:
2469 audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK);
2470 return;
2471 case SoundEffectConstants.NAVIGATION_DOWN:
2472 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_DOWN);
2473 return;
2474 case SoundEffectConstants.NAVIGATION_LEFT:
2475 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_LEFT);
2476 return;
2477 case SoundEffectConstants.NAVIGATION_RIGHT:
2478 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_RIGHT);
2479 return;
2480 case SoundEffectConstants.NAVIGATION_UP:
2481 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_UP);
2482 return;
2483 default:
2484 throw new IllegalArgumentException("unknown effect id " + effectId +
2485 " not defined in " + SoundEffectConstants.class.getCanonicalName());
2486 }
2487 }
2488
2489 /**
2490 * {@inheritDoc}
2491 */
2492 public boolean performHapticFeedback(int effectId, boolean always) {
2493 try {
2494 return sWindowSession.performHapticFeedback(mWindow, effectId, always);
2495 } catch (RemoteException e) {
2496 return false;
2497 }
2498 }
2499
2500 /**
2501 * {@inheritDoc}
2502 */
2503 public View focusSearch(View focused, int direction) {
2504 checkThread();
2505 if (!(mView instanceof ViewGroup)) {
2506 return null;
2507 }
2508 return FocusFinder.getInstance().findNextFocus((ViewGroup) mView, focused, direction);
2509 }
2510
2511 public void debug() {
2512 mView.debug();
2513 }
2514
2515 public void die(boolean immediate) {
2516 checkThread();
2517 if (Config.LOGV) Log.v("ViewRoot", "DIE in " + this + " of " + mSurface);
2518 synchronized (this) {
2519 if (mAdded && !mFirst) {
2520 int viewVisibility = mView.getVisibility();
2521 boolean viewVisibilityChanged = mViewVisibility != viewVisibility;
2522 if (mWindowAttributesChanged || viewVisibilityChanged) {
2523 // If layout params have been changed, first give them
2524 // to the window manager to make sure it has the correct
2525 // animation info.
2526 try {
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002527 if ((relayoutWindow(mWindowAttributes, viewVisibility, false)
2528 & WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002529 sWindowSession.finishDrawing(mWindow);
2530 }
2531 } catch (RemoteException e) {
2532 }
2533 }
2534
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002535 mSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536 }
2537 if (mAdded) {
2538 mAdded = false;
2539 if (immediate) {
2540 dispatchDetachedFromWindow();
2541 } else if (mView != null) {
2542 sendEmptyMessage(DIE);
2543 }
2544 }
2545 }
2546 }
2547
2548 public void dispatchFinishedEvent(int seq, boolean handled) {
2549 Message msg = obtainMessage(FINISHED_EVENT);
2550 msg.arg1 = seq;
2551 msg.arg2 = handled ? 1 : 0;
2552 sendMessage(msg);
2553 }
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 public void dispatchResized(int w, int h, Rect coveredInsets,
2556 Rect visibleInsets, boolean reportDraw) {
2557 if (DEBUG_LAYOUT) Log.v(TAG, "Resizing " + this + ": w=" + w
2558 + " h=" + h + " coveredInsets=" + coveredInsets.toShortString()
2559 + " visibleInsets=" + visibleInsets.toShortString()
2560 + " reportDraw=" + reportDraw);
2561 Message msg = obtainMessage(reportDraw ? RESIZED_REPORT :RESIZED);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002562 if (mTranslator != null) {
2563 mTranslator.translateRectInScreenToAppWindow(coveredInsets);
2564 mTranslator.translateRectInScreenToAppWindow(visibleInsets);
2565 w *= mTranslator.applicationInvertedScale;
2566 h *= mTranslator.applicationInvertedScale;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002567 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002568 msg.arg1 = w;
2569 msg.arg2 = h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 msg.obj = new Rect[] { new Rect(coveredInsets), new Rect(visibleInsets) };
2571 sendMessage(msg);
2572 }
2573
2574 public void dispatchKey(KeyEvent event) {
2575 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2576 //noinspection ConstantConditions
2577 if (false && event.getKeyCode() == KeyEvent.KEYCODE_CAMERA) {
2578 if (Config.LOGD) Log.d("keydisp",
2579 "===================================================");
2580 if (Config.LOGD) Log.d("keydisp", "Focused view Hierarchy is:");
2581 debug();
2582
2583 if (Config.LOGD) Log.d("keydisp",
2584 "===================================================");
2585 }
2586 }
2587
2588 Message msg = obtainMessage(DISPATCH_KEY);
2589 msg.obj = event;
2590
2591 if (LOCAL_LOGV) Log.v(
2592 "ViewRoot", "sending key " + event + " to " + mView);
2593
2594 sendMessageAtTime(msg, event.getEventTime());
2595 }
2596
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002597 public void dispatchPointer(MotionEvent event, long eventTime,
2598 boolean callWhenDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 Message msg = obtainMessage(DISPATCH_POINTER);
2600 msg.obj = event;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002601 msg.arg1 = callWhenDone ? 1 : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 sendMessageAtTime(msg, eventTime);
2603 }
2604
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002605 public void dispatchTrackball(MotionEvent event, long eventTime,
2606 boolean callWhenDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 Message msg = obtainMessage(DISPATCH_TRACKBALL);
2608 msg.obj = event;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002609 msg.arg1 = callWhenDone ? 1 : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610 sendMessageAtTime(msg, eventTime);
2611 }
2612
2613 public void dispatchAppVisibility(boolean visible) {
2614 Message msg = obtainMessage(DISPATCH_APP_VISIBILITY);
2615 msg.arg1 = visible ? 1 : 0;
2616 sendMessage(msg);
2617 }
2618
2619 public void dispatchGetNewSurface() {
2620 Message msg = obtainMessage(DISPATCH_GET_NEW_SURFACE);
2621 sendMessage(msg);
2622 }
2623
2624 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
2625 Message msg = Message.obtain();
2626 msg.what = WINDOW_FOCUS_CHANGED;
2627 msg.arg1 = hasFocus ? 1 : 0;
2628 msg.arg2 = inTouchMode ? 1 : 0;
2629 sendMessage(msg);
2630 }
2631
svetoslavganov75986cf2009-05-14 22:28:01 -07002632 /**
2633 * The window is getting focus so if there is anything focused/selected
2634 * send an {@link AccessibilityEvent} to announce that.
2635 */
2636 private void sendAccessibilityEvents() {
2637 if (!AccessibilityManager.getInstance(mView.getContext()).isEnabled()) {
2638 return;
2639 }
2640 mView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2641 View focusedView = mView.findFocus();
2642 if (focusedView != null && focusedView != mView) {
2643 focusedView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
2644 }
2645 }
2646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 public boolean showContextMenuForChild(View originalView) {
2648 return false;
2649 }
2650
2651 public void createContextMenu(ContextMenu menu) {
2652 }
2653
2654 public void childDrawableStateChanged(View child) {
2655 }
2656
2657 protected Rect getWindowFrame() {
2658 return mWinFrame;
2659 }
2660
2661 void checkThread() {
2662 if (mThread != Thread.currentThread()) {
2663 throw new CalledFromWrongThreadException(
2664 "Only the original thread that created a view hierarchy can touch its views.");
2665 }
2666 }
2667
2668 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
2669 // ViewRoot never intercepts touch event, so this can be a no-op
2670 }
2671
2672 public boolean requestChildRectangleOnScreen(View child, Rect rectangle,
2673 boolean immediate) {
2674 return scrollToRectOrFocus(rectangle, immediate);
2675 }
Romain Guy8506ab42009-06-11 17:35:47 -07002676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 static class InputMethodCallback extends IInputMethodCallback.Stub {
2678 private WeakReference<ViewRoot> mViewRoot;
2679
2680 public InputMethodCallback(ViewRoot viewRoot) {
2681 mViewRoot = new WeakReference<ViewRoot>(viewRoot);
2682 }
Romain Guy8506ab42009-06-11 17:35:47 -07002683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 public void finishedEvent(int seq, boolean handled) {
2685 final ViewRoot viewRoot = mViewRoot.get();
2686 if (viewRoot != null) {
2687 viewRoot.dispatchFinishedEvent(seq, handled);
2688 }
2689 }
2690
2691 public void sessionCreated(IInputMethodSession session) throws RemoteException {
2692 // Stub -- not for use in the client.
2693 }
2694 }
Romain Guy8506ab42009-06-11 17:35:47 -07002695
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07002696 static class EventCompletion extends Handler {
2697 final IWindow mWindow;
2698 final KeyEvent mKeyEvent;
2699 final boolean mIsPointer;
2700 final MotionEvent mMotionEvent;
Romain Guy8506ab42009-06-11 17:35:47 -07002701
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07002702 EventCompletion(Looper looper, IWindow window, KeyEvent key,
2703 boolean isPointer, MotionEvent motion) {
2704 super(looper);
2705 mWindow = window;
2706 mKeyEvent = key;
2707 mIsPointer = isPointer;
2708 mMotionEvent = motion;
2709 sendEmptyMessage(0);
2710 }
Romain Guy8506ab42009-06-11 17:35:47 -07002711
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07002712 @Override
2713 public void handleMessage(Message msg) {
2714 if (mKeyEvent != null) {
2715 try {
2716 sWindowSession.finishKey(mWindow);
2717 } catch (RemoteException e) {
2718 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002719 } else if (mIsPointer) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07002720 boolean didFinish;
2721 MotionEvent event = mMotionEvent;
2722 if (event == null) {
2723 try {
2724 event = sWindowSession.getPendingPointerMove(mWindow);
2725 } catch (RemoteException e) {
2726 }
2727 didFinish = true;
2728 } else {
2729 didFinish = event.getAction() == MotionEvent.ACTION_OUTSIDE;
2730 }
2731 if (!didFinish) {
2732 try {
2733 sWindowSession.finishKey(mWindow);
2734 } catch (RemoteException e) {
2735 }
2736 }
2737 } else {
2738 MotionEvent event = mMotionEvent;
2739 if (event == null) {
2740 try {
2741 event = sWindowSession.getPendingTrackballMove(mWindow);
2742 } catch (RemoteException e) {
2743 }
2744 } else {
2745 try {
2746 sWindowSession.finishKey(mWindow);
2747 } catch (RemoteException e) {
2748 }
2749 }
2750 }
2751 }
2752 }
Romain Guy8506ab42009-06-11 17:35:47 -07002753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 static class W extends IWindow.Stub {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07002755 private final WeakReference<ViewRoot> mViewRoot;
2756 private final Looper mMainLooper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07002758 public W(ViewRoot viewRoot, Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 mViewRoot = new WeakReference<ViewRoot>(viewRoot);
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07002760 mMainLooper = context.getMainLooper();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 }
2762
2763 public void resized(int w, int h, Rect coveredInsets,
2764 Rect visibleInsets, boolean reportDraw) {
2765 final ViewRoot viewRoot = mViewRoot.get();
2766 if (viewRoot != null) {
2767 viewRoot.dispatchResized(w, h, coveredInsets,
2768 visibleInsets, reportDraw);
2769 }
2770 }
2771
2772 public void dispatchKey(KeyEvent event) {
2773 final ViewRoot viewRoot = mViewRoot.get();
2774 if (viewRoot != null) {
2775 viewRoot.dispatchKey(event);
2776 } else {
2777 Log.w("ViewRoot.W", "Key event " + event + " but no ViewRoot available!");
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07002778 new EventCompletion(mMainLooper, this, event, false, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 }
2780 }
2781
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002782 public void dispatchPointer(MotionEvent event, long eventTime,
2783 boolean callWhenDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 final ViewRoot viewRoot = mViewRoot.get();
Michael Chan53071d62009-05-13 17:29:48 -07002785 if (viewRoot != null) {
2786 if (MEASURE_LATENCY) {
2787 // Note: eventTime is in milliseconds
2788 ViewRoot.lt.sample("* ViewRoot b4 dispatchPtr", System.nanoTime() - eventTime * 1000000);
2789 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002790 viewRoot.dispatchPointer(event, eventTime, callWhenDone);
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07002791 } else {
2792 new EventCompletion(mMainLooper, this, null, true, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 }
2794 }
2795
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002796 public void dispatchTrackball(MotionEvent event, long eventTime,
2797 boolean callWhenDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 final ViewRoot viewRoot = mViewRoot.get();
2799 if (viewRoot != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002800 viewRoot.dispatchTrackball(event, eventTime, callWhenDone);
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07002801 } else {
2802 new EventCompletion(mMainLooper, this, null, false, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 }
2804 }
2805
2806 public void dispatchAppVisibility(boolean visible) {
2807 final ViewRoot viewRoot = mViewRoot.get();
2808 if (viewRoot != null) {
2809 viewRoot.dispatchAppVisibility(visible);
2810 }
2811 }
2812
2813 public void dispatchGetNewSurface() {
2814 final ViewRoot viewRoot = mViewRoot.get();
2815 if (viewRoot != null) {
2816 viewRoot.dispatchGetNewSurface();
2817 }
2818 }
2819
2820 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
2821 final ViewRoot viewRoot = mViewRoot.get();
2822 if (viewRoot != null) {
2823 viewRoot.windowFocusChanged(hasFocus, inTouchMode);
2824 }
2825 }
2826
2827 private static int checkCallingPermission(String permission) {
2828 if (!Process.supportsProcesses()) {
2829 return PackageManager.PERMISSION_GRANTED;
2830 }
2831
2832 try {
2833 return ActivityManagerNative.getDefault().checkPermission(
2834 permission, Binder.getCallingPid(), Binder.getCallingUid());
2835 } catch (RemoteException e) {
2836 return PackageManager.PERMISSION_DENIED;
2837 }
2838 }
2839
2840 public void executeCommand(String command, String parameters, ParcelFileDescriptor out) {
2841 final ViewRoot viewRoot = mViewRoot.get();
2842 if (viewRoot != null) {
2843 final View view = viewRoot.mView;
2844 if (view != null) {
2845 if (checkCallingPermission(Manifest.permission.DUMP) !=
2846 PackageManager.PERMISSION_GRANTED) {
2847 throw new SecurityException("Insufficient permissions to invoke"
2848 + " executeCommand() from pid=" + Binder.getCallingPid()
2849 + ", uid=" + Binder.getCallingUid());
2850 }
2851
2852 OutputStream clientStream = null;
2853 try {
2854 clientStream = new ParcelFileDescriptor.AutoCloseOutputStream(out);
2855 ViewDebug.dispatchCommand(view, command, parameters, clientStream);
2856 } catch (IOException e) {
2857 e.printStackTrace();
2858 } finally {
2859 if (clientStream != null) {
2860 try {
2861 clientStream.close();
2862 } catch (IOException e) {
2863 e.printStackTrace();
2864 }
2865 }
2866 }
2867 }
2868 }
2869 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07002870
2871 public void dispatchWallpaperOffsets(float x, float y) {
2872 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 }
2874
2875 /**
2876 * Maintains state information for a single trackball axis, generating
2877 * discrete (DPAD) movements based on raw trackball motion.
2878 */
2879 static final class TrackballAxis {
2880 /**
2881 * The maximum amount of acceleration we will apply.
2882 */
2883 static final float MAX_ACCELERATION = 20;
Romain Guy8506ab42009-06-11 17:35:47 -07002884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 /**
2886 * The maximum amount of time (in milliseconds) between events in order
2887 * for us to consider the user to be doing fast trackball movements,
2888 * and thus apply an acceleration.
2889 */
2890 static final long FAST_MOVE_TIME = 150;
Romain Guy8506ab42009-06-11 17:35:47 -07002891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 /**
2893 * Scaling factor to the time (in milliseconds) between events to how
2894 * much to multiple/divide the current acceleration. When movement
2895 * is < FAST_MOVE_TIME this multiplies the acceleration; when >
2896 * FAST_MOVE_TIME it divides it.
2897 */
2898 static final float ACCEL_MOVE_SCALING_FACTOR = (1.0f/40);
Romain Guy8506ab42009-06-11 17:35:47 -07002899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 float position;
2901 float absPosition;
2902 float acceleration = 1;
2903 long lastMoveTime = 0;
2904 int step;
2905 int dir;
2906 int nonAccelMovement;
2907
2908 void reset(int _step) {
2909 position = 0;
2910 acceleration = 1;
2911 lastMoveTime = 0;
2912 step = _step;
2913 dir = 0;
2914 }
2915
2916 /**
2917 * Add trackball movement into the state. If the direction of movement
2918 * has been reversed, the state is reset before adding the
2919 * movement (so that you don't have to compensate for any previously
2920 * collected movement before see the result of the movement in the
2921 * new direction).
2922 *
2923 * @return Returns the absolute value of the amount of movement
2924 * collected so far.
2925 */
2926 float collect(float off, long time, String axis) {
2927 long normTime;
2928 if (off > 0) {
2929 normTime = (long)(off * FAST_MOVE_TIME);
2930 if (dir < 0) {
2931 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to positive!");
2932 position = 0;
2933 step = 0;
2934 acceleration = 1;
2935 lastMoveTime = 0;
2936 }
2937 dir = 1;
2938 } else if (off < 0) {
2939 normTime = (long)((-off) * FAST_MOVE_TIME);
2940 if (dir > 0) {
2941 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to negative!");
2942 position = 0;
2943 step = 0;
2944 acceleration = 1;
2945 lastMoveTime = 0;
2946 }
2947 dir = -1;
2948 } else {
2949 normTime = 0;
2950 }
Romain Guy8506ab42009-06-11 17:35:47 -07002951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 // The number of milliseconds between each movement that is
2953 // considered "normal" and will not result in any acceleration
2954 // or deceleration, scaled by the offset we have here.
2955 if (normTime > 0) {
2956 long delta = time - lastMoveTime;
2957 lastMoveTime = time;
2958 float acc = acceleration;
2959 if (delta < normTime) {
2960 // The user is scrolling rapidly, so increase acceleration.
2961 float scale = (normTime-delta) * ACCEL_MOVE_SCALING_FACTOR;
2962 if (scale > 1) acc *= scale;
2963 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " accelerate: off="
2964 + off + " normTime=" + normTime + " delta=" + delta
2965 + " scale=" + scale + " acc=" + acc);
2966 acceleration = acc < MAX_ACCELERATION ? acc : MAX_ACCELERATION;
2967 } else {
2968 // The user is scrolling slowly, so decrease acceleration.
2969 float scale = (delta-normTime) * ACCEL_MOVE_SCALING_FACTOR;
2970 if (scale > 1) acc /= scale;
2971 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " deccelerate: off="
2972 + off + " normTime=" + normTime + " delta=" + delta
2973 + " scale=" + scale + " acc=" + acc);
2974 acceleration = acc > 1 ? acc : 1;
2975 }
2976 }
2977 position += off;
2978 return (absPosition = Math.abs(position));
2979 }
2980
2981 /**
2982 * Generate the number of discrete movement events appropriate for
2983 * the currently collected trackball movement.
2984 *
2985 * @param precision The minimum movement required to generate the
2986 * first discrete movement.
2987 *
2988 * @return Returns the number of discrete movements, either positive
2989 * or negative, or 0 if there is not enough trackball movement yet
2990 * for a discrete movement.
2991 */
2992 int generate(float precision) {
2993 int movement = 0;
2994 nonAccelMovement = 0;
2995 do {
2996 final int dir = position >= 0 ? 1 : -1;
2997 switch (step) {
2998 // If we are going to execute the first step, then we want
2999 // to do this as soon as possible instead of waiting for
3000 // a full movement, in order to make things look responsive.
3001 case 0:
3002 if (absPosition < precision) {
3003 return movement;
3004 }
3005 movement += dir;
3006 nonAccelMovement += dir;
3007 step = 1;
3008 break;
3009 // If we have generated the first movement, then we need
3010 // to wait for the second complete trackball motion before
3011 // generating the second discrete movement.
3012 case 1:
3013 if (absPosition < 2) {
3014 return movement;
3015 }
3016 movement += dir;
3017 nonAccelMovement += dir;
3018 position += dir > 0 ? -2 : 2;
3019 absPosition = Math.abs(position);
3020 step = 2;
3021 break;
3022 // After the first two, we generate discrete movements
3023 // consistently with the trackball, applying an acceleration
3024 // if the trackball is moving quickly. This is a simple
3025 // acceleration on top of what we already compute based
3026 // on how quickly the wheel is being turned, to apply
3027 // a longer increasing acceleration to continuous movement
3028 // in one direction.
3029 default:
3030 if (absPosition < 1) {
3031 return movement;
3032 }
3033 movement += dir;
3034 position += dir >= 0 ? -1 : 1;
3035 absPosition = Math.abs(position);
3036 float acc = acceleration;
3037 acc *= 1.1f;
3038 acceleration = acc < MAX_ACCELERATION ? acc : acceleration;
3039 break;
3040 }
3041 } while (true);
3042 }
3043 }
3044
3045 public static final class CalledFromWrongThreadException extends AndroidRuntimeException {
3046 public CalledFromWrongThreadException(String msg) {
3047 super(msg);
3048 }
3049 }
3050
3051 private SurfaceHolder mHolder = new SurfaceHolder() {
3052 // we only need a SurfaceHolder for opengl. it would be nice
3053 // to implement everything else though, especially the callback
3054 // support (opengl doesn't make use of it right now, but eventually
3055 // will).
3056 public Surface getSurface() {
3057 return mSurface;
3058 }
3059
3060 public boolean isCreating() {
3061 return false;
3062 }
3063
3064 public void addCallback(Callback callback) {
3065 }
3066
3067 public void removeCallback(Callback callback) {
3068 }
3069
3070 public void setFixedSize(int width, int height) {
3071 }
3072
3073 public void setSizeFromLayout() {
3074 }
3075
3076 public void setFormat(int format) {
3077 }
3078
3079 public void setType(int type) {
3080 }
3081
3082 public void setKeepScreenOn(boolean screenOn) {
3083 }
3084
3085 public Canvas lockCanvas() {
3086 return null;
3087 }
3088
3089 public Canvas lockCanvas(Rect dirty) {
3090 return null;
3091 }
3092
3093 public void unlockCanvasAndPost(Canvas canvas) {
3094 }
3095 public Rect getSurfaceFrame() {
3096 return null;
3097 }
3098 };
3099
3100 static RunQueue getRunQueue() {
3101 RunQueue rq = sRunQueues.get();
3102 if (rq != null) {
3103 return rq;
3104 }
3105 rq = new RunQueue();
3106 sRunQueues.set(rq);
3107 return rq;
3108 }
Romain Guy8506ab42009-06-11 17:35:47 -07003109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003110 /**
3111 * @hide
3112 */
3113 static final class RunQueue {
3114 private final ArrayList<HandlerAction> mActions = new ArrayList<HandlerAction>();
3115
3116 void post(Runnable action) {
3117 postDelayed(action, 0);
3118 }
3119
3120 void postDelayed(Runnable action, long delayMillis) {
3121 HandlerAction handlerAction = new HandlerAction();
3122 handlerAction.action = action;
3123 handlerAction.delay = delayMillis;
3124
3125 synchronized (mActions) {
3126 mActions.add(handlerAction);
3127 }
3128 }
3129
3130 void removeCallbacks(Runnable action) {
3131 final HandlerAction handlerAction = new HandlerAction();
3132 handlerAction.action = action;
3133
3134 synchronized (mActions) {
3135 final ArrayList<HandlerAction> actions = mActions;
3136
3137 while (actions.remove(handlerAction)) {
3138 // Keep going
3139 }
3140 }
3141 }
3142
3143 void executeActions(Handler handler) {
3144 synchronized (mActions) {
3145 final ArrayList<HandlerAction> actions = mActions;
3146 final int count = actions.size();
3147
3148 for (int i = 0; i < count; i++) {
3149 final HandlerAction handlerAction = actions.get(i);
3150 handler.postDelayed(handlerAction.action, handlerAction.delay);
3151 }
3152
Romain Guy15df6702009-08-17 20:17:30 -07003153 actions.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154 }
3155 }
3156
3157 private static class HandlerAction {
3158 Runnable action;
3159 long delay;
3160
3161 @Override
3162 public boolean equals(Object o) {
3163 if (this == o) return true;
3164 if (o == null || getClass() != o.getClass()) return false;
3165
3166 HandlerAction that = (HandlerAction) o;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 return !(action != null ? !action.equals(that.action) : that.action != null);
3168
3169 }
3170
3171 @Override
3172 public int hashCode() {
3173 int result = action != null ? action.hashCode() : 0;
3174 result = 31 * result + (int) (delay ^ (delay >>> 32));
3175 return result;
3176 }
3177 }
3178 }
3179
3180 private static native void nativeShowFPS(Canvas canvas, int durationMillis);
3181
3182 // inform skia to just abandon its texture cache IDs
3183 // doesn't call glDeleteTextures
3184 private static native void nativeAbandonGlCaches();
3185}